2023-08-24 10:54:59 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2022-09-02 18:30:48 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert DrawPrototype into draw commands.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-25 10:57:02 +02:00
|
|
|
#include "draw_view_infos.hh"
|
2024-11-13 12:32:39 +01:00
|
|
|
|
|
|
|
|
COMPUTE_SHADER_CREATE_INFO(draw_command_generate)
|
|
|
|
|
|
2022-09-02 18:30:48 +02:00
|
|
|
#define atomicAddAndGet(dst, val) (atomicAdd(dst, val) + val)
|
|
|
|
|
|
|
|
|
|
/* This is only called by the last thread executed over the group's prototype draws. */
|
|
|
|
|
void write_draw_call(DrawGroup group, uint group_id)
|
|
|
|
|
{
|
|
|
|
|
DrawCommand cmd;
|
|
|
|
|
cmd.vertex_len = group.vertex_len;
|
|
|
|
|
cmd.vertex_first = group.vertex_first;
|
2023-01-23 16:25:04 +01:00
|
|
|
bool indexed_draw = group.base_index != -1;
|
2024-10-11 18:09:57 +02:00
|
|
|
|
|
|
|
|
/* Back-facing command. */
|
|
|
|
|
uint back_facing_start = group.start * view_len;
|
2023-01-23 16:25:04 +01:00
|
|
|
if (indexed_draw) {
|
2022-09-02 18:30:48 +02:00
|
|
|
cmd.base_index = group.base_index;
|
2024-10-11 18:09:57 +02:00
|
|
|
cmd.instance_first_indexed = back_facing_start;
|
2022-09-02 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2024-10-11 18:09:57 +02:00
|
|
|
cmd._instance_first_array = back_facing_start;
|
2022-09-02 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
cmd.instance_len = group_buf[group_id].back_facing_counter;
|
|
|
|
|
command_buf[group_id * 2 + 0] = cmd;
|
2023-01-23 16:25:04 +01:00
|
|
|
|
2022-09-02 18:30:48 +02:00
|
|
|
/* Front-facing command. */
|
2024-10-11 18:09:57 +02:00
|
|
|
uint front_facing_start = (group.start + (group.len - group.front_facing_len)) * view_len;
|
2023-01-23 16:25:04 +01:00
|
|
|
if (indexed_draw) {
|
|
|
|
|
cmd.instance_first_indexed = front_facing_start;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cmd._instance_first_array = front_facing_start;
|
|
|
|
|
}
|
2022-09-02 18:30:48 +02:00
|
|
|
cmd.instance_len = group_buf[group_id].front_facing_counter;
|
|
|
|
|
command_buf[group_id * 2 + 1] = cmd;
|
|
|
|
|
|
2023-09-03 16:13:08 +10:00
|
|
|
/* Reset the counters for a next command gen dispatch. Avoids re-sending the whole data just
|
2022-09-06 16:25:20 +10:00
|
|
|
* for this purpose. Only the last thread will execute this so it is thread-safe. */
|
2022-09-02 18:30:48 +02:00
|
|
|
group_buf[group_id].front_facing_counter = 0u;
|
|
|
|
|
group_buf[group_id].back_facing_counter = 0u;
|
|
|
|
|
group_buf[group_id].total_counter = 0u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
|
{
|
2023-07-14 11:09:39 +02:00
|
|
|
int proto_id = int(gl_GlobalInvocationID.x);
|
2022-09-02 18:30:48 +02:00
|
|
|
if (proto_id >= prototype_len) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DrawPrototype proto = prototype_buf[proto_id];
|
|
|
|
|
uint group_id = proto.group_id;
|
2025-07-03 16:04:48 +02:00
|
|
|
bool is_inverted = (proto.res_index & 0x80000000u) != 0;
|
|
|
|
|
uint resource_index = (proto.res_index & 0x7FFFFFFFu);
|
2022-09-02 18:30:48 +02:00
|
|
|
|
|
|
|
|
/* Visibility test result. */
|
2022-11-14 00:42:31 +01:00
|
|
|
uint visible_instance_len = 0;
|
|
|
|
|
if (visibility_word_per_draw > 0) {
|
|
|
|
|
uint visibility_word = resource_index * visibility_word_per_draw;
|
2023-07-14 11:09:39 +02:00
|
|
|
for (int i = 0; i < visibility_word_per_draw; i++, visibility_word++) {
|
2023-01-09 18:56:17 +11:00
|
|
|
/* NOTE: This assumes `proto.instance_len` is 1. */
|
2022-12-29 18:20:48 +01:00
|
|
|
/* TODO: Assert. */
|
2022-11-14 00:42:31 +01:00
|
|
|
visible_instance_len += bitCount(visibility_buf[visibility_word]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if ((visibility_buf[resource_index / 32u] & (1u << (resource_index % 32u))) != 0) {
|
|
|
|
|
visible_instance_len = proto.instance_len;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bool is_visible = visible_instance_len > 0;
|
2022-09-02 18:30:48 +02:00
|
|
|
|
|
|
|
|
DrawGroup group = group_buf[group_id];
|
|
|
|
|
|
|
|
|
|
if (!is_visible) {
|
|
|
|
|
/* Skip the draw but still count towards the completion. */
|
|
|
|
|
if (atomicAddAndGet(group_buf[group_id].total_counter, proto.instance_len) == group.len) {
|
|
|
|
|
write_draw_call(group, group_id);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-11 18:09:57 +02:00
|
|
|
uint back_facing_len = (group.len - group.front_facing_len) * view_len;
|
|
|
|
|
uint dst_index = group.start * view_len;
|
2022-09-02 18:30:48 +02:00
|
|
|
if (is_inverted) {
|
2022-11-14 00:42:31 +01:00
|
|
|
uint offset = atomicAdd(group_buf[group_id].back_facing_counter, visible_instance_len);
|
2022-09-02 18:30:48 +02:00
|
|
|
dst_index += offset;
|
|
|
|
|
if (atomicAddAndGet(group_buf[group_id].total_counter, proto.instance_len) == group.len) {
|
|
|
|
|
write_draw_call(group, group_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2022-11-14 00:42:31 +01:00
|
|
|
uint offset = atomicAdd(group_buf[group_id].front_facing_counter, visible_instance_len);
|
2022-09-02 18:30:48 +02:00
|
|
|
dst_index += back_facing_len + offset;
|
|
|
|
|
if (atomicAddAndGet(group_buf[group_id].total_counter, proto.instance_len) == group.len) {
|
|
|
|
|
write_draw_call(group, group_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 18:20:48 +01:00
|
|
|
/* Fill resource_id buffer for each instance of this draw. */
|
2022-11-14 00:42:31 +01:00
|
|
|
if (visibility_word_per_draw > 0) {
|
|
|
|
|
uint visibility_word = resource_index * visibility_word_per_draw;
|
2023-07-14 11:09:39 +02:00
|
|
|
for (int i = 0; i < visibility_word_per_draw; i++, visibility_word++) {
|
2022-11-14 00:42:31 +01:00
|
|
|
uint word = visibility_buf[visibility_word];
|
|
|
|
|
uint view_index = i * 32u;
|
|
|
|
|
while (word != 0u) {
|
|
|
|
|
if ((word & 1u) != 0u) {
|
2023-03-01 21:42:25 +01:00
|
|
|
if (use_custom_ids) {
|
|
|
|
|
resource_id_buf[dst_index * 2] = view_index | (resource_index << view_shift);
|
|
|
|
|
resource_id_buf[dst_index * 2 + 1] = proto.custom_id;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
resource_id_buf[dst_index] = view_index | (resource_index << view_shift);
|
|
|
|
|
}
|
|
|
|
|
dst_index++;
|
2022-11-14 00:42:31 +01:00
|
|
|
}
|
|
|
|
|
view_index++;
|
|
|
|
|
word >>= 1u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (uint i = dst_index; i < dst_index + visible_instance_len; i++) {
|
2023-03-01 21:42:25 +01:00
|
|
|
if (use_custom_ids) {
|
|
|
|
|
resource_id_buf[i * 2] = resource_index;
|
|
|
|
|
resource_id_buf[i * 2 + 1] = proto.custom_id;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
resource_id_buf[i] = resource_index;
|
|
|
|
|
}
|
2022-11-14 00:42:31 +01:00
|
|
|
}
|
2022-09-02 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
}
|