Blender V5.3
draw_command_generate.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
14#include "draw_shader_shared.hh"
15
16namespace draw {
17
18[[force_inline]] uint atomicAddAndGet(uint &dst, uint val)
19{
20 return atomicAdd(dst, val) + val;
21}
22
24 [[storage(0, read_write)]] DrawGroup (&group_buf)[];
25 [[storage(1, read)]] uint (&visibility_buf)[];
26 [[storage(2, read)]] DrawPrototype (&prototype_buf)[];
27 [[storage(3, write)]] DrawCommand (&command_buf)[];
29
35
36 /* This is only called by the last thread executed over the group's prototype draws. */
37 void write_draw_call(DrawGroup group, uint group_id)
38 {
39 const bool indexed_draw = group.base_index != -1;
40
41 const uint back_facing_len = group_buf[group_id].back_facing_counter;
42 const uint back_facing_start = group.start * uint(view_len);
43 const uint front_facing_len = group_buf[group_id].front_facing_counter;
44 const uint front_facing_start = (group.start + (group.len - group.front_facing_len)) *
45 uint(view_len);
46
47 /* Back-facing command. */
48 DrawCommand cmd;
49 if (indexed_draw) {
50 DrawCommandIndexed cmd_indexed;
51 cmd_indexed.vertex_len = uint(group.vertex_len);
52 cmd_indexed.instance_len = back_facing_len;
53 cmd_indexed.vertex_first = uint(group.vertex_first);
54 cmd_indexed.base_index = uint(group.base_index);
55 cmd_indexed.instance_first = back_facing_start;
56 cmd.indexed = cmd_indexed;
57 }
58 else {
59 DrawCommandArray cmd_array;
60 cmd_array.vertex_len = uint(group.vertex_len);
61 cmd_array.instance_len = back_facing_len;
62 cmd_array.vertex_first = uint(group.vertex_first);
63 cmd_array.instance_first = back_facing_start;
64 cmd.array = cmd_array;
65 }
66 command_buf[group_id * 2 + 0] = cmd;
67
68 /* Front-facing command. */
69 if (indexed_draw) {
70 DrawCommandIndexed cmd_indexed;
71 cmd_indexed.vertex_len = uint(group.vertex_len);
72 cmd_indexed.instance_len = front_facing_len;
73 cmd_indexed.vertex_first = uint(group.vertex_first);
74 cmd_indexed.base_index = uint(group.base_index);
75 cmd_indexed.instance_first = front_facing_start;
76 cmd.indexed = cmd_indexed;
77 }
78 else {
79 DrawCommandArray cmd_array;
80 cmd_array.vertex_len = uint(group.vertex_len);
81 cmd_array.instance_len = front_facing_len;
82 cmd_array.vertex_first = uint(group.vertex_first);
83 cmd_array.instance_first = front_facing_start;
84 cmd.array = cmd_array;
85 }
86 command_buf[group_id * 2 + 1] = cmd;
87
88 /* Reset the counters for a next command gen dispatch. Avoids re-sending the whole data just
89 * for this purpose. Only the last thread will execute this so it is thread-safe. */
90 group_buf[group_id].front_facing_counter = 0u;
91 group_buf[group_id].back_facing_counter = 0u;
92 group_buf[group_id].total_counter = 0u;
93 }
94};
95
98 [[global_invocation_id]] const uint3 global_id,
99 [[local_invocation_id]] const uint3 local_id,
100 [[work_group_id]] const uint3 workgroup_id)
101{
102 int proto_id = int(global_id.x);
103 if (proto_id >= srt.prototype_len) {
104 return;
105 }
106
107 DrawPrototype proto = srt.prototype_buf[proto_id];
108 const uint group_id = proto.group_id;
109 bool is_inverted = (proto.res_id & 0x80000000u) != 0;
110 uint resource_id = (proto.res_id & 0x7FFFFFFFu);
111
112 /* Visibility test result. */
113 uint visible_instance_len = 0;
114 if (srt.visibility_word_per_draw > 0) {
115 uint visibility_word = resource_id * uint(srt.visibility_word_per_draw);
116 for (int i = 0; i < srt.visibility_word_per_draw; i++, visibility_word++) {
117 /* NOTE: This assumes `proto.instance_len` is 1. */
118 /* TODO: Assert. */
119 visible_instance_len += uint(bitCount(srt.visibility_buf[visibility_word]));
120 }
121 }
122 else {
123 if ((srt.visibility_buf[resource_id / 32u] & (1u << (resource_id % 32u))) != 0) {
124 visible_instance_len = proto.instance_len;
125 }
126 }
127 bool is_visible = visible_instance_len > 0;
128
129 DrawGroup group = srt.group_buf[group_id];
130
131 if (!is_visible) {
132 /* Skip the draw but still count towards the completion. */
133 uint group_count = atomicAddAndGet(srt.group_buf[group_id].total_counter, proto.instance_len);
134 if (group_count == group.len) {
135 srt.write_draw_call(group, group_id);
136 }
137 return;
138 }
139
140 uint back_facing_len = (group.len - group.front_facing_len) * uint(srt.view_len);
141 uint dst_index = group.start * uint(srt.view_len);
142 if (is_inverted) {
143 uint offset = atomicAdd(srt.group_buf[group_id].back_facing_counter, visible_instance_len);
144 dst_index += offset;
145 uint group_count = atomicAddAndGet(srt.group_buf[group_id].total_counter, proto.instance_len);
146 if (group_count == group.len) {
147 srt.write_draw_call(group, group_id);
148 }
149 }
150 else {
151 uint offset = atomicAdd(srt.group_buf[group_id].front_facing_counter, visible_instance_len);
152 dst_index += back_facing_len + offset;
153 uint group_count = atomicAddAndGet(srt.group_buf[group_id].total_counter, proto.instance_len);
154 if (group_count == group.len) {
155 srt.write_draw_call(group, group_id);
156 }
157 }
158
159 /* Fill resource_id buffer for each instance of this draw. */
160 if (srt.visibility_word_per_draw > 0) {
161 uint visibility_word = resource_id * uint(srt.visibility_word_per_draw);
162 for (int i = 0; i < srt.visibility_word_per_draw; i++, visibility_word++) {
163 uint word = srt.visibility_buf[visibility_word];
164 uint view_index = uint(i) * 32u;
165 while (word != 0u) {
166 if ((word & 1u) != 0u) {
167 if (srt.use_custom_ids) {
168 srt.resource_id_buf[dst_index * 2] = view_index | (resource_id << srt.view_shift);
169 srt.resource_id_buf[dst_index * 2 + 1] = proto.custom_id;
170 }
171 else {
172 srt.resource_id_buf[dst_index] = view_index | (resource_id << srt.view_shift);
173 }
174 dst_index++;
175 }
176 view_index++;
177 word >>= 1u;
178 }
179 }
180 }
181 else {
182 for (uint i = dst_index; i < dst_index + visible_instance_len; i++) {
183 if (srt.use_custom_ids) {
184 srt.resource_id_buf[i * 2] = resource_id;
185 srt.resource_id_buf[i * 2 + 1] = proto.custom_id;
186 }
187 else {
188 srt.resource_id_buf[i] = resource_id;
189 }
190 }
191 }
192}
193
195
196} // namespace draw
#define DRW_RESOURCE_ID_SLOT
#define DRW_COMMAND_GROUP_SIZE
#define storage(slot, qualifiers)
#define work_group_id
#define push_constant
#define resource_table
#define global_invocation_id
#define local_invocation_id
#define compute
#define force_inline
#define local_size(...)
int atomicAdd(int &, int) RET
VecBase< int, D > bitCount(VecOp< int, D >) RET
PipelineCompute command_generate(command_gen)
uint atomicAddAndGet(uint &dst, uint val)
void command_gen(GenerateCommand &srt, const uint3 global_id, const uint3 local_id, const uint3 workgroup_id)
void write_draw_call(DrawGroup group, uint group_id)