Skip to content
0.2.0-dev Download ZIP
596 lines ·26.7 KB Raw
1/**************************************************************************/
2/* cluster_builder_rd.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "cluster_builder_rd.h"
32
33#include "servers/rendering/rendering_device.h"
34#include "servers/rendering/rendering_server_globals.h" // IWYU pragma: keep. RENDER_TIMESTAMP macro uses RSG.
35
36ClusterBuilderSharedDataRD::ClusterBuilderSharedDataRD() {
37 RD::VertexFormatID vertex_format;
38
39 {
40 Vector<RD::VertexAttribute> attributes;
41 {
42 RD::VertexAttribute va;
43 va.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
44 va.stride = sizeof(float) * 3;
45 attributes.push_back(va);
46 }
47 vertex_format = RD::get_singleton()->vertex_format_create(attributes);
48 }
49
50 {
51 RD::FramebufferFormatID fb_format;
52 RD::PipelineColorBlendState blend_state;
53 RD::PipelineRasterizationState rasterization_state;
54 RD::PipelineMultisampleState ms;
55 rasterization_state.enable_depth_clamp = true;
56 ms.sample_count = RD::TEXTURE_SAMPLES_4;
57
58 Vector<String> variants;
59 variants.push_back("");
60 variants.push_back("\n#define USE_ATTACHMENT\n");
61
62 ClusterRender::ShaderVariant shader_variant;
63 RenderingDevice *rd = RD::get_singleton();
64 if (rd->has_feature(RD::SUPPORTS_FRAGMENT_SHADER_WITH_ONLY_SIDE_EFFECTS)) {
65 fb_format = rd->framebuffer_format_create_empty();
66 blend_state = RD::PipelineColorBlendState::create_disabled();
67 shader_variant = ClusterRender::SHADER_NORMAL;
68 } else {
69 Vector<RD::AttachmentFormat> afs;
70 afs.push_back(RD::AttachmentFormat());
71 afs.write[0].usage_flags = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
72 fb_format = rd->framebuffer_format_create(afs);
73 blend_state = RD::PipelineColorBlendState::create_blend();
74 shader_variant = ClusterRender::SHADER_USE_ATTACHMENT;
75 }
76
77 cluster_render.cluster_render_shader.initialize(variants);
78 cluster_render.shader_version = cluster_render.cluster_render_shader.version_create();
79 cluster_render.shader = cluster_render.cluster_render_shader.version_get_shader(cluster_render.shader_version, shader_variant);
80 cluster_render.shader_pipelines[ClusterRender::PIPELINE_NORMAL] = RD::get_singleton()->render_pipeline_create(cluster_render.shader, fb_format, vertex_format, RD::RENDER_PRIMITIVE_TRIANGLES, rasterization_state, RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
81
82 // On Apple platforms, gl_HelperInvocation (simd_is_helper_thread()) is unreliable with MSAA, causing rendering artifacts.
83 // Disable the helper invocation check for the MSAA pipeline via specialization constant.
84 Vector<RD::PipelineSpecializationConstant> specialization_constants;
85#if defined(MACOS_ENABLED) || defined(APPLE_EMBEDDED_ENABLED)
86 {
87 RD::PipelineSpecializationConstant sc;
88 sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
89 sc.constant_id = 0; // sc_use_helper_check
90 sc.bool_value = false;
91 specialization_constants.push_back(sc);
92 }
93#endif
94 cluster_render.shader_pipelines[ClusterRender::PIPELINE_MSAA] = RD::get_singleton()->render_pipeline_create(cluster_render.shader, fb_format, vertex_format, RD::RENDER_PRIMITIVE_TRIANGLES, rasterization_state, ms, RD::PipelineDepthStencilState(), blend_state, 0, 0, specialization_constants);
95 }
96 {
97 Vector<String> versions;
98 versions.push_back("");
99 cluster_store.cluster_store_shader.initialize(versions);
100 cluster_store.shader_version = cluster_store.cluster_store_shader.version_create();
101 cluster_store.shader = cluster_store.cluster_store_shader.version_get_shader(cluster_store.shader_version, 0);
102 cluster_store.shader_pipeline = RD::get_singleton()->compute_pipeline_create(cluster_store.shader);
103 }
104 {
105 Vector<String> versions;
106 versions.push_back("");
107 cluster_debug.cluster_debug_shader.initialize(versions);
108 cluster_debug.shader_version = cluster_debug.cluster_debug_shader.version_create();
109 cluster_debug.shader = cluster_debug.cluster_debug_shader.version_get_shader(cluster_debug.shader_version, 0);
110 cluster_debug.shader_pipeline = RD::get_singleton()->compute_pipeline_create(cluster_debug.shader);
111 }
112
113 { // Sphere mesh data.
114 static const uint32_t icosphere_vertex_count = 42;
115 static const float icosphere_vertices[icosphere_vertex_count * 3] = {
116 0, 0, -1, 0.7236073, -0.5257253, -0.4472195, -0.276388, -0.8506492, -0.4472199, -0.8944262, 0, -0.4472156, -0.276388, 0.8506492, -0.4472199, 0.7236073, 0.5257253, -0.4472195, 0.276388, -0.8506492, 0.4472199, -0.7236073, -0.5257253, 0.4472195, -0.7236073, 0.5257253, 0.4472195, 0.276388, 0.8506492, 0.4472199, 0.8944262, 0, 0.4472156, 0, 0, 1, -0.1624555, -0.4999952, -0.8506544, 0.4253227, -0.3090114, -0.8506542, 0.2628688, -0.8090116, -0.5257377, 0.8506479, 0, -0.5257359, 0.4253227, 0.3090114, -0.8506542, -0.5257298, 0, -0.8506517, -0.6881894, -0.4999969, -0.5257362, -0.1624555, 0.4999952, -0.8506544, -0.6881894, 0.4999969, -0.5257362, 0.2628688, 0.8090116, -0.5257377, 0.9510579, -0.3090126, 0, 0.9510579, 0.3090126, 0, 0, -1, 0, 0.5877856, -0.8090167, 0, -0.9510579, -0.3090126, 0, -0.5877856, -0.8090167, 0, -0.5877856, 0.8090167, 0, -0.9510579, 0.3090126, 0, 0.5877856, 0.8090167, 0, 0, 1, 0, 0.6881894, -0.4999969, 0.5257362, -0.2628688, -0.8090116, 0.5257377, -0.8506479, 0, 0.5257359, -0.2628688, 0.8090116, 0.5257377, 0.6881894, 0.4999969, 0.5257362, 0.1624555, -0.4999952, 0.8506544, 0.5257298, 0, 0.8506517, -0.4253227, -0.3090114, 0.8506542, -0.4253227, 0.3090114, 0.8506542, 0.1624555, 0.4999952, 0.8506544
117 };
118 static const uint32_t icosphere_triangle_count = 80;
119 static const uint16_t icosphere_triangle_indices[icosphere_triangle_count * 3] = {
120 0, 13, 12, 1, 13, 15, 0, 12, 17, 0, 17, 19, 0, 19, 16, 1, 15, 22, 2, 14, 24, 3, 18, 26, 4, 20, 28, 5, 21, 30, 1, 22, 25, 2, 24, 27, 3, 26, 29, 4, 28, 31, 5, 30, 23, 6, 32, 37, 7, 33, 39, 8, 34, 40, 9, 35, 41, 10, 36, 38, 38, 41, 11, 38, 36, 41, 36, 9, 41, 41, 40, 11, 41, 35, 40, 35, 8, 40, 40, 39, 11, 40, 34, 39, 34, 7, 39, 39, 37, 11, 39, 33, 37, 33, 6, 37, 37, 38, 11, 37, 32, 38, 32, 10, 38, 23, 36, 10, 23, 30, 36, 30, 9, 36, 31, 35, 9, 31, 28, 35, 28, 8, 35, 29, 34, 8, 29, 26, 34, 26, 7, 34, 27, 33, 7, 27, 24, 33, 24, 6, 33, 25, 32, 6, 25, 22, 32, 22, 10, 32, 30, 31, 9, 30, 21, 31, 21, 4, 31, 28, 29, 8, 28, 20, 29, 20, 3, 29, 26, 27, 7, 26, 18, 27, 18, 2, 27, 24, 25, 6, 24, 14, 25, 14, 1, 25, 22, 23, 10, 22, 15, 23, 15, 5, 23, 16, 21, 5, 16, 19, 21, 19, 4, 21, 19, 20, 4, 19, 17, 20, 17, 3, 20, 17, 18, 3, 17, 12, 18, 12, 2, 18, 15, 16, 5, 15, 13, 16, 13, 0, 16, 12, 14, 2, 12, 13, 14, 13, 1, 14
121 };
122
123 Vector<uint8_t> vertex_data;
124 vertex_data.resize(sizeof(float) * icosphere_vertex_count * 3);
125 memcpy(vertex_data.ptrw(), icosphere_vertices, vertex_data.size());
126
127 sphere_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
128
129 Vector<uint8_t> index_data;
130 index_data.resize(sizeof(uint16_t) * icosphere_triangle_count * 3);
131 memcpy(index_data.ptrw(), icosphere_triangle_indices, index_data.size());
132
133 sphere_index_buffer = RD::get_singleton()->index_buffer_create(icosphere_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
134
135 Vector<RID> buffers;
136 buffers.push_back(sphere_vertex_buffer);
137
138 sphere_vertex_array = RD::get_singleton()->vertex_array_create(icosphere_vertex_count, vertex_format, buffers);
139
140 sphere_index_array = RD::get_singleton()->index_array_create(sphere_index_buffer, 0, icosphere_triangle_count * 3);
141
142 float min_d = 1e20;
143 for (uint32_t i = 0; i < icosphere_triangle_count; i++) {
144 Vector3 vertices[3];
145 for (uint32_t j = 0; j < 3; j++) {
146 uint32_t index = icosphere_triangle_indices[i * 3 + j];
147 for (uint32_t k = 0; k < 3; k++) {
148 vertices[j][k] = icosphere_vertices[index * 3 + k];
149 }
150 }
151 Plane p(vertices[0], vertices[1], vertices[2]);
152 min_d = MIN(Math::abs(p.d), min_d);
153 }
154 sphere_overfit = 1.0 / min_d;
155 }
156
157 { // Cone mesh data.
158 static const uint32_t cone_vertex_count = 99;
159 static const float cone_vertices[cone_vertex_count * 3] = {
160 0, 1, -1, 0.1950903, 0.9807853, -1, 0.3826835, 0.9238795, -1, 0.5555703, 0.8314696, -1, 0.7071068, 0.7071068, -1, 0.8314697, 0.5555702, -1, 0.9238795, 0.3826834, -1, 0.9807853, 0.1950903, -1, 1, 0, -1, 0.9807853, -0.1950902, -1, 0.9238796, -0.3826833, -1, 0.8314697, -0.5555702, -1, 0.7071068, -0.7071068, -1, 0.5555702, -0.8314697, -1, 0.3826833, -0.9238796, -1, 0.1950901, -0.9807853, -1, -3.25841e-7, -1, -1, -0.1950907, -0.9807852, -1, -0.3826839, -0.9238793, -1, -0.5555707, -0.8314693, -1, -0.7071073, -0.7071063, -1, -0.83147, -0.5555697, -1, -0.9238799, -0.3826827, -1, 0, 0, 0, -0.9807854, -0.1950894, -1, -1, 9.65599e-7, -1, -0.9807851, 0.1950913, -1, -0.9238791, 0.3826845, -1, -0.8314689, 0.5555713, -1, -0.7071059, 0.7071077, -1, -0.5555691, 0.8314704, -1, -0.3826821, 0.9238801, -1, -0.1950888, 0.9807856, -1
161 };
162 static const uint32_t cone_triangle_count = 62;
163 static const uint16_t cone_triangle_indices[cone_triangle_count * 3] = {
164 0, 23, 1, 1, 23, 2, 2, 23, 3, 3, 23, 4, 4, 23, 5, 5, 23, 6, 6, 23, 7, 7, 23, 8, 8, 23, 9, 9, 23, 10, 10, 23, 11, 11, 23, 12, 12, 23, 13, 13, 23, 14, 14, 23, 15, 15, 23, 16, 16, 23, 17, 17, 23, 18, 18, 23, 19, 19, 23, 20, 20, 23, 21, 21, 23, 22, 22, 23, 24, 24, 23, 25, 25, 23, 26, 26, 23, 27, 27, 23, 28, 28, 23, 29, 29, 23, 30, 30, 23, 31, 31, 23, 32, 32, 23, 0, 7, 15, 24, 32, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 3, 6, 7, 3, 7, 8, 9, 9, 10, 7, 10, 11, 7, 11, 12, 15, 12, 13, 15, 13, 14, 15, 15, 16, 17, 17, 18, 19, 19, 20, 24, 20, 21, 24, 21, 22, 24, 24, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32, 32, 1, 3, 15, 17, 24, 17, 19, 24, 24, 26, 32, 26, 28, 32, 28, 30, 32, 32, 3, 7, 7, 11, 15, 32, 7, 24
165 };
166
167 Vector<uint8_t> vertex_data;
168 vertex_data.resize(sizeof(float) * cone_vertex_count * 3);
169 memcpy(vertex_data.ptrw(), cone_vertices, vertex_data.size());
170
171 cone_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
172
173 Vector<uint8_t> index_data;
174 index_data.resize(sizeof(uint16_t) * cone_triangle_count * 3);
175 memcpy(index_data.ptrw(), cone_triangle_indices, index_data.size());
176
177 cone_index_buffer = RD::get_singleton()->index_buffer_create(cone_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
178
179 Vector<RID> buffers;
180 buffers.push_back(cone_vertex_buffer);
181
182 cone_vertex_array = RD::get_singleton()->vertex_array_create(cone_vertex_count, vertex_format, buffers);
183
184 cone_index_array = RD::get_singleton()->index_array_create(cone_index_buffer, 0, cone_triangle_count * 3);
185
186 float min_d = 1e20;
187 for (uint32_t i = 0; i < cone_triangle_count; i++) {
188 Vector3 vertices[3];
189 int32_t zero_index = -1;
190 for (uint32_t j = 0; j < 3; j++) {
191 uint32_t index = cone_triangle_indices[i * 3 + j];
192 for (uint32_t k = 0; k < 3; k++) {
193 vertices[j][k] = cone_vertices[index * 3 + k];
194 }
195 if (vertices[j] == Vector3()) {
196 zero_index = j;
197 }
198 }
199
200 if (zero_index != -1) {
201 Vector3 a = vertices[(zero_index + 1) % 3];
202 Vector3 b = vertices[(zero_index + 2) % 3];
203 Vector3 c = a + Vector3(0, 0, 1);
204 Plane p(a, b, c);
205 min_d = MIN(Math::abs(p.d), min_d);
206 }
207 }
208 cone_overfit = 1.0 / min_d;
209 }
210
211 { // Box mesh data.
212 static const uint32_t box_vertex_count = 8;
213 static const float box_vertices[box_vertex_count * 3] = {
214 -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1
215 };
216 static const uint32_t box_triangle_count = 12;
217 static const uint16_t box_triangle_indices[box_triangle_count * 3] = {
218 1, 2, 0, 3, 6, 2, 7, 4, 6, 5, 0, 4, 6, 0, 2, 3, 5, 7, 1, 3, 2, 3, 7, 6, 7, 5, 4, 5, 1, 0, 6, 4, 0, 3, 1, 5
219 };
220
221 Vector<uint8_t> vertex_data;
222 vertex_data.resize(sizeof(float) * box_vertex_count * 3);
223 memcpy(vertex_data.ptrw(), box_vertices, vertex_data.size());
224
225 box_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
226
227 Vector<uint8_t> index_data;
228 index_data.resize(sizeof(uint16_t) * box_triangle_count * 3);
229 memcpy(index_data.ptrw(), box_triangle_indices, index_data.size());
230
231 box_index_buffer = RD::get_singleton()->index_buffer_create(box_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
232
233 Vector<RID> buffers;
234 buffers.push_back(box_vertex_buffer);
235
236 box_vertex_array = RD::get_singleton()->vertex_array_create(box_vertex_count, vertex_format, buffers);
237
238 box_index_array = RD::get_singleton()->index_array_create(box_index_buffer, 0, box_triangle_count * 3);
239 }
240}
241ClusterBuilderSharedDataRD::~ClusterBuilderSharedDataRD() {
242 RD::get_singleton()->free_rid(sphere_vertex_buffer);
243 RD::get_singleton()->free_rid(sphere_index_buffer);
244 RD::get_singleton()->free_rid(cone_vertex_buffer);
245 RD::get_singleton()->free_rid(cone_index_buffer);
246 RD::get_singleton()->free_rid(box_vertex_buffer);
247 RD::get_singleton()->free_rid(box_index_buffer);
248
249 cluster_render.cluster_render_shader.version_free(cluster_render.shader_version);
250 cluster_store.cluster_store_shader.version_free(cluster_store.shader_version);
251 cluster_debug.cluster_debug_shader.version_free(cluster_debug.shader_version);
252}
253
254/////////////////////////////
255
256void ClusterBuilderRD::_clear() {
257 if (cluster_buffer.is_null()) {
258 return;
259 }
260
261 RD::get_singleton()->free_rid(cluster_buffer);
262 RD::get_singleton()->free_rid(cluster_render_buffer);
263 RD::get_singleton()->free_rid(element_buffer);
264 cluster_buffer = RID();
265 cluster_render_buffer = RID();
266 element_buffer = RID();
267
268 memfree(render_elements);
269
270 render_elements = nullptr;
271 render_element_max = 0;
272 render_element_count = 0;
273
274 RD::get_singleton()->free_rid(framebuffer);
275 framebuffer = RID();
276
277 cluster_render_uniform_set = RID();
278 cluster_store_uniform_set = RID();
279}
280
281void ClusterBuilderRD::setup(Size2i p_screen_size, uint32_t p_max_elements, RID p_depth_buffer, RID p_depth_buffer_sampler, RID p_color_buffer) {
282 ERR_FAIL_COND(p_max_elements == 0);
283 ERR_FAIL_COND(p_screen_size.x < 1);
284 ERR_FAIL_COND(p_screen_size.y < 1);
285
286 _clear();
287
288 screen_size = p_screen_size;
289
290 cluster_screen_size.width = Math::division_round_up((uint32_t)p_screen_size.width, cluster_size);
291 cluster_screen_size.height = Math::division_round_up((uint32_t)p_screen_size.height, cluster_size);
292
293 max_elements_by_type = p_max_elements;
294 if (max_elements_by_type % 32) { // Needs to be aligned to 32.
295 max_elements_by_type += 32 - (max_elements_by_type % 32);
296 }
297
298 cluster_buffer_size = cluster_screen_size.x * cluster_screen_size.y * (max_elements_by_type / 32 + 32) * ELEMENT_TYPE_MAX * 4;
299
300 render_element_max = max_elements_by_type * ELEMENT_TYPE_MAX;
301
302 uint32_t element_tag_bits_size = render_element_max / 32;
303 uint32_t element_tag_depth_bits_size = render_element_max;
304
305 cluster_render_buffer_size = cluster_screen_size.x * cluster_screen_size.y * (element_tag_bits_size + element_tag_depth_bits_size) * 4; // Tag bits (element was used) and tag depth (depth range in which it was used).
306
307 cluster_render_buffer = RD::get_singleton()->storage_buffer_create(cluster_render_buffer_size);
308 cluster_buffer = RD::get_singleton()->storage_buffer_create(cluster_buffer_size);
309
310 render_elements = static_cast<RenderElementData *>(memalloc(sizeof(RenderElementData) * render_element_max));
311 render_element_count = 0;
312
313 element_buffer = RD::get_singleton()->storage_buffer_create(sizeof(RenderElementData) * render_element_max);
314
315 uint32_t div_value = 1 << divisor;
316 if (use_msaa) {
317 framebuffer = RD::get_singleton()->framebuffer_create_empty(p_screen_size / div_value, RD::TEXTURE_SAMPLES_4);
318 } else {
319 framebuffer = RD::get_singleton()->framebuffer_create_empty(p_screen_size / div_value);
320 }
321
322 {
323 Vector<RD::Uniform> uniforms;
324 {
325 RD::Uniform u;
326 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
327 u.binding = 1;
328 u.append_id(state_uniform);
329 uniforms.push_back(u);
330 }
331 {
332 RD::Uniform u;
333 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
334 u.binding = 2;
335 u.append_id(element_buffer);
336 uniforms.push_back(u);
337 }
338 {
339 RD::Uniform u;
340 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
341 u.binding = 3;
342 u.append_id(cluster_render_buffer);
343 uniforms.push_back(u);
344 }
345
346 cluster_render_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_render.shader, 0);
347 }
348
349 {
350 Vector<RD::Uniform> uniforms;
351 {
352 RD::Uniform u;
353 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
354 u.binding = 1;
355 u.append_id(cluster_render_buffer);
356 uniforms.push_back(u);
357 }
358 {
359 RD::Uniform u;
360 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
361 u.binding = 2;
362 u.append_id(cluster_buffer);
363 uniforms.push_back(u);
364 }
365
366 {
367 RD::Uniform u;
368 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
369 u.binding = 3;
370 u.append_id(element_buffer);
371 uniforms.push_back(u);
372 }
373
374 cluster_store_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_store.shader, 0);
375 }
376
377 if (p_color_buffer.is_valid()) {
378 Vector<RD::Uniform> uniforms;
379 {
380 RD::Uniform u;
381 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
382 u.binding = 1;
383 u.append_id(cluster_buffer);
384 uniforms.push_back(u);
385 }
386 {
387 RD::Uniform u;
388 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
389 u.binding = 2;
390 u.append_id(p_color_buffer);
391 uniforms.push_back(u);
392 }
393
394 {
395 RD::Uniform u;
396 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
397 u.binding = 3;
398 u.append_id(p_depth_buffer);
399 uniforms.push_back(u);
400 }
401 {
402 RD::Uniform u;
403 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
404 u.binding = 4;
405 u.append_id(p_depth_buffer_sampler);
406 uniforms.push_back(u);
407 }
408
409 debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_debug.shader, 0);
410 } else {
411 debug_uniform_set = RID();
412 }
413}
414
415void ClusterBuilderRD::begin(const Transform3D &p_view_transform, const Projection &p_cam_projection, bool p_flip_y) {
416 view_xform = p_view_transform.affine_inverse();
417 projection = p_cam_projection;
418 z_near = projection.get_z_near();
419 z_far = projection.get_z_far();
420 camera_orthogonal = p_cam_projection.is_orthogonal();
421 adjusted_projection = projection;
422 if (!camera_orthogonal) {
423 adjusted_projection.adjust_perspective_znear(0.0001);
424 }
425
426 Projection correction;
427 correction.set_depth_correction(p_flip_y);
428 projection = correction * projection;
429 adjusted_projection = correction * adjusted_projection;
430
431 // Reset counts.
432 render_element_count = 0;
433 for (uint32_t i = 0; i < ELEMENT_TYPE_MAX; i++) {
434 cluster_count_by_type[i] = 0;
435 }
436}
437
438void ClusterBuilderRD::bake_cluster() {
439 RENDER_TIMESTAMP("> Bake 3D Cluster");
440
441 RD::get_singleton()->draw_command_begin_label("Bake Light Cluster");
442
443 // Clear cluster buffer.
444 RD::get_singleton()->buffer_clear(cluster_buffer, 0, cluster_buffer_size);
445
446 if (render_element_count > 0) {
447 // Clear render buffer.
448 RD::get_singleton()->buffer_clear(cluster_render_buffer, 0, cluster_render_buffer_size);
449
450 { // Fill state uniform.
451
452 StateUniform state;
453
454 RendererRD::MaterialStorage::store_camera(adjusted_projection, state.projection);
455 state.inv_z_far = 1.0 / z_far;
456 state.screen_to_clusters_shift = Math::get_shift_from_power_of_2(cluster_size);
457 state.screen_to_clusters_shift -= divisor; //screen is smaller, shift one less
458
459 state.cluster_screen_width = cluster_screen_size.x;
460 state.cluster_depth_offset = (render_element_max / 32);
461 state.cluster_data_size = state.cluster_depth_offset + render_element_max;
462
463 RD::get_singleton()->buffer_update(state_uniform, 0, sizeof(StateUniform), &state);
464 }
465
466 // Update instances.
467
468 RD::get_singleton()->buffer_update(element_buffer, 0, sizeof(RenderElementData) * render_element_count, render_elements);
469
470 RENDER_TIMESTAMP("Render 3D Cluster Elements");
471
472 // Render elements.
473 {
474 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
475 ClusterBuilderSharedDataRD::ClusterRender::PushConstant push_constant = {};
476
477 RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, shared->cluster_render.shader_pipelines[use_msaa ? ClusterBuilderSharedDataRD::ClusterRender::PIPELINE_MSAA : ClusterBuilderSharedDataRD::ClusterRender::PIPELINE_NORMAL]);
478 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, cluster_render_uniform_set, 0);
479
480 for (uint32_t i = 0; i < render_element_count;) {
481 push_constant.base_index = i;
482 switch (render_elements[i].type) {
483 case ELEMENT_TYPE_OMNI_LIGHT: {
484 RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->sphere_vertex_array);
485 RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->sphere_index_array);
486 } break;
487 case ELEMENT_TYPE_SPOT_LIGHT: {
488 // If the spot angle is above a certain threshold, use a sphere instead of a cone for building the clusters
489 // since the cone gets too flat/large (spot angle close to 90 degrees) or
490 // can't even cover the affected area of the light (spot angle above 90 degrees).
491 if (render_elements[i].has_wide_spot_angle) {
492 RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->sphere_vertex_array);
493 RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->sphere_index_array);
494 } else {
495 RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->cone_vertex_array);
496 RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->cone_index_array);
497 }
498 } break;
499 case ELEMENT_TYPE_AREA_LIGHT: {
500 RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->box_vertex_array);
501 RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->box_index_array);
502 } break;
503 case ELEMENT_TYPE_DECAL:
504 case ELEMENT_TYPE_REFLECTION_PROBE: {
505 RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->box_vertex_array);
506 RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->box_index_array);
507 } break;
508 }
509
510 RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterRender::PushConstant));
511
512 uint32_t instances = 1;
513 RD::get_singleton()->draw_list_draw(draw_list, true, instances);
514 i += instances;
515 }
516 RD::get_singleton()->draw_list_end();
517 }
518 // Store elements.
519 RENDER_TIMESTAMP("Pack 3D Cluster Elements");
520
521 {
522 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
523 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shared->cluster_store.shader_pipeline);
524 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cluster_store_uniform_set, 0);
525
526 ClusterBuilderSharedDataRD::ClusterStore::PushConstant push_constant;
527 push_constant.cluster_render_data_size = render_element_max / 32 + render_element_max;
528 push_constant.max_render_element_count_div_32 = render_element_max / 32;
529 push_constant.cluster_screen_size[0] = cluster_screen_size.x;
530 push_constant.cluster_screen_size[1] = cluster_screen_size.y;
531
532 push_constant.render_element_count_div_32 = Math::division_round_up(render_element_count, 32U);
533 push_constant.max_cluster_element_count_div_32 = max_elements_by_type / 32;
534 push_constant.pad1 = 0;
535 push_constant.pad2 = 0;
536
537 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterStore::PushConstant));
538
539 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cluster_screen_size.x, cluster_screen_size.y, 1);
540
541 RD::get_singleton()->compute_list_end();
542 }
543 }
544 RENDER_TIMESTAMP("< Bake 3D Cluster");
545 RD::get_singleton()->draw_command_end_label();
546}
547
548void ClusterBuilderRD::debug(ElementType p_element) {
549 ERR_FAIL_COND(debug_uniform_set.is_null());
550 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
551 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shared->cluster_debug.shader_pipeline);
552 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, debug_uniform_set, 0);
553
554 ClusterBuilderSharedDataRD::ClusterDebug::PushConstant push_constant;
555 push_constant.screen_size[0] = screen_size.x;
556 push_constant.screen_size[1] = screen_size.y;
557 push_constant.cluster_screen_size[0] = cluster_screen_size.x;
558 push_constant.cluster_screen_size[1] = cluster_screen_size.y;
559 push_constant.cluster_shift = Math::get_shift_from_power_of_2(cluster_size);
560 push_constant.cluster_type = p_element;
561 push_constant.orthogonal = camera_orthogonal;
562 push_constant.z_far = z_far;
563 push_constant.z_near = z_near;
564 push_constant.max_cluster_element_count_div_32 = max_elements_by_type / 32;
565
566 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterDebug::PushConstant));
567
568 RD::get_singleton()->compute_list_dispatch_threads(compute_list, screen_size.x, screen_size.y, 1);
569
570 RD::get_singleton()->compute_list_end();
571}
572
573RID ClusterBuilderRD::get_cluster_buffer() const {
574 return cluster_buffer;
575}
576
577uint32_t ClusterBuilderRD::get_cluster_size() const {
578 return cluster_size;
579}
580
581uint32_t ClusterBuilderRD::get_max_cluster_elements() const {
582 return max_elements_by_type;
583}
584
585void ClusterBuilderRD::set_shared(ClusterBuilderSharedDataRD *p_shared) {
586 shared = p_shared;
587}
588
589ClusterBuilderRD::ClusterBuilderRD() {
590 state_uniform = RD::get_singleton()->uniform_buffer_create(sizeof(StateUniform));
591}
592
593ClusterBuilderRD::~ClusterBuilderRD() {
594 _clear();
595 RD::get_singleton()->free_rid(state_uniform);
596}