Debian Codegraph @

servers/rendering/renderer_rd/environment/gi.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* gi.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 "gi.h"
32
33 #include "core/config/project_settings.h"
34 #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
35 #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
36 #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
37 #include "servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h"
38 #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
39 #include "servers/rendering/rendering_server_default.h"
40
41 using namespace RendererRD;
42
43 const Vector3i GI::SDFGI::Cascade::DIRTY_ALL = Vector3i(0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF);
44
45 GI *GI::singleton = nullptr;
46
47 ////////////////////////////////////////////////////////////////////////////////
48 // VOXEL GI STORAGE
49
50 RID GI::voxel_gi_allocate() {
51 return voxel_gi_owner.allocate_rid();
52 }
53
54 void GI::voxel_gi_free(RID p_voxel_gi) {
55 voxel_gi_allocate_data(p_voxel_gi, Transform3D(), AABB(), Vector3i(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<int>()); //deallocate
56 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
57 voxel_gi->dependency.deleted_notify(p_voxel_gi);
58 voxel_gi_owner.free(p_voxel_gi);
59 }
60
61 void GI::voxel_gi_initialize(RID p_voxel_gi) {
62 voxel_gi_owner.initialize_rid(p_voxel_gi, VoxelGI());
63 }
64
65 void GI::voxel_gi_allocate_data(RID p_voxel_gi, const Transform3D &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) {
66 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
67 ERR_FAIL_NULL(voxel_gi);
68
69 if (voxel_gi->octree_buffer.is_valid()) {
70 RD::get_singleton()->free_rid(voxel_gi->octree_buffer);
71 RD::get_singleton()->free_rid(voxel_gi->data_buffer);
72 if (voxel_gi->sdf_texture.is_valid()) {
73 RD::get_singleton()->free_rid(voxel_gi->sdf_texture);
74 }
75
76 voxel_gi->sdf_texture = RID();
77 voxel_gi->octree_buffer = RID();
78 voxel_gi->data_buffer = RID();
79 voxel_gi->octree_buffer_size = 0;
80 voxel_gi->data_buffer_size = 0;
81 voxel_gi->cell_count = 0;
82 }
83
84 voxel_gi->to_cell_xform = p_to_cell_xform;
85 voxel_gi->bounds = p_aabb;
86 voxel_gi->octree_size = p_octree_size;
87 voxel_gi->level_counts = p_level_counts;
88
89 if (p_octree_cells.size()) {
90 ERR_FAIL_COND(p_octree_cells.size() % 32 != 0); //cells size must be a multiple of 32
91
92 uint32_t cell_count = p_octree_cells.size() / 32;
93
94 ERR_FAIL_COND(p_data_cells.size() != (int)cell_count * 16); //see that data size matches
95
96 voxel_gi->cell_count = cell_count;
97 voxel_gi->octree_buffer = RD::get_singleton()->storage_buffer_create(p_octree_cells.size(), p_octree_cells);
98 voxel_gi->octree_buffer_size = p_octree_cells.size();
99 voxel_gi->data_buffer = RD::get_singleton()->storage_buffer_create(p_data_cells.size(), p_data_cells);
100 voxel_gi->data_buffer_size = p_data_cells.size();
101
102 if (p_distance_field.size()) {
103 RD::TextureFormat tf;
104 tf.format = RD::DATA_FORMAT_R8_UNORM;
105 tf.width = voxel_gi->octree_size.x;
106 tf.height = voxel_gi->octree_size.y;
107 tf.depth = voxel_gi->octree_size.z;
108 tf.texture_type = RD::TEXTURE_TYPE_3D;
109 tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
110 Vector<Vector<uint8_t>> s;
111 s.push_back(p_distance_field);
112 voxel_gi->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView(), s);
113 RD::get_singleton()->set_resource_name(voxel_gi->sdf_texture, "VoxelGI SDF Texture");
114 }
115 #if 0
116 {
117 RD::TextureFormat tf;
118 tf.format = RD::DATA_FORMAT_R8_UNORM;
119 tf.width = voxel_gi->octree_size.x;
120 tf.height = voxel_gi->octree_size.y;
121 tf.depth = voxel_gi->octree_size.z;
122 tf.type = RD::TEXTURE_TYPE_3D;
123 tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
124 tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UNORM);
125 tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UINT);
126 voxel_gi->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
127 RD::get_singleton()->set_resource_name(voxel_gi->sdf_texture, "VoxelGI SDF Texture");
128 }
129 RID shared_tex;
130 {
131 RD::TextureView tv;
132 tv.format_override = RD::DATA_FORMAT_R8_UINT;
133 shared_tex = RD::get_singleton()->texture_create_shared(tv, voxel_gi->sdf_texture);
134 }
135 //update SDF texture
136 Vector<RD::Uniform> uniforms;
137 {
138 RD::Uniform u;
139 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
140 u.binding = 1;
141 u.append_id(voxel_gi->octree_buffer);
142 uniforms.push_back(u);
143 }
144 {
145 RD::Uniform u;
146 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
147 u.binding = 2;
148 u.append_id(voxel_gi->data_buffer);
149 uniforms.push_back(u);
150 }
151 {
152 RD::Uniform u;
153 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
154 u.binding = 3;
155 u.append_id(shared_tex);
156 uniforms.push_back(u);
157 }
158
159 RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, voxel_gi_sdf_shader_version_shader, 0);
160
161 {
162 uint32_t push_constant[4] = { 0, 0, 0, 0 };
163
164 for (int i = 0; i < voxel_gi->level_counts.size() - 1; i++) {
165 push_constant[0] += voxel_gi->level_counts[i];
166 }
167 push_constant[1] = push_constant[0] + voxel_gi->level_counts[voxel_gi->level_counts.size() - 1];
168
169 print_line("offset: " + itos(push_constant[0]));
170 print_line("size: " + itos(push_constant[1]));
171 //create SDF
172 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
173 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, voxel_gi_sdf_shader_pipeline);
174 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0);
175 RD::get_singleton()->compute_list_set_push_constant(compute_list, push_constant, sizeof(uint32_t) * 4);
176 RD::get_singleton()->compute_list_dispatch(compute_list, voxel_gi->octree_size.x / 4, voxel_gi->octree_size.y / 4, voxel_gi->octree_size.z / 4);
177 RD::get_singleton()->compute_list_end();
178 }
179
180 RD::get_singleton()->free(uniform_set);
181 RD::get_singleton()->free(shared_tex);
182 }
183 #endif
184 }
185
186 voxel_gi->version++;
187 voxel_gi->data_version++;
188
189 voxel_gi->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
190 }
191
192 AABB GI::voxel_gi_get_bounds(RID p_voxel_gi) const {
193 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
194 ERR_FAIL_NULL_V(voxel_gi, AABB());
195
196 return voxel_gi->bounds;
197 }
198
199 Vector3i GI::voxel_gi_get_octree_size(RID p_voxel_gi) const {
200 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
201 ERR_FAIL_NULL_V(voxel_gi, Vector3i());
202 return voxel_gi->octree_size;
203 }
204
205 Vector<uint8_t> GI::voxel_gi_get_octree_cells(RID p_voxel_gi) const {
206 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
207 ERR_FAIL_NULL_V(voxel_gi, Vector<uint8_t>());
208
209 if (voxel_gi->octree_buffer.is_valid()) {
210 return RD::get_singleton()->buffer_get_data(voxel_gi->octree_buffer);
211 }
212 return Vector<uint8_t>();
213 }
214
215 Vector<uint8_t> GI::voxel_gi_get_data_cells(RID p_voxel_gi) const {
216 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
217 ERR_FAIL_NULL_V(voxel_gi, Vector<uint8_t>());
218
219 if (voxel_gi->data_buffer.is_valid()) {
220 return RD::get_singleton()->buffer_get_data(voxel_gi->data_buffer);
221 }
222 return Vector<uint8_t>();
223 }
224
225 Vector<uint8_t> GI::voxel_gi_get_distance_field(RID p_voxel_gi) const {
226 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
227 ERR_FAIL_NULL_V(voxel_gi, Vector<uint8_t>());
228
229 if (voxel_gi->data_buffer.is_valid()) {
230 return RD::get_singleton()->texture_get_data(voxel_gi->sdf_texture, 0);
231 }
232 return Vector<uint8_t>();
233 }
234
235 Vector<int> GI::voxel_gi_get_level_counts(RID p_voxel_gi) const {
236 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
237 ERR_FAIL_NULL_V(voxel_gi, Vector<int>());
238
239 return voxel_gi->level_counts;
240 }
241
242 Transform3D GI::voxel_gi_get_to_cell_xform(RID p_voxel_gi) const {
243 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
244 ERR_FAIL_NULL_V(voxel_gi, Transform3D());
245
246 return voxel_gi->to_cell_xform;
247 }
248
249 void GI::voxel_gi_set_dynamic_range(RID p_voxel_gi, float p_range) {
250 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
251 ERR_FAIL_NULL(voxel_gi);
252
253 voxel_gi->dynamic_range = p_range;
254 voxel_gi->version++;
255 }
256
257 float GI::voxel_gi_get_dynamic_range(RID p_voxel_gi) const {
258 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
259 ERR_FAIL_NULL_V(voxel_gi, 0);
260
261 return voxel_gi->dynamic_range;
262 }
263
264 void GI::voxel_gi_set_propagation(RID p_voxel_gi, float p_range) {
265 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
266 ERR_FAIL_NULL(voxel_gi);
267
268 voxel_gi->propagation = p_range;
269 voxel_gi->version++;
270 }
271
272 float GI::voxel_gi_get_propagation(RID p_voxel_gi) const {
273 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
274 ERR_FAIL_NULL_V(voxel_gi, 0);
275 return voxel_gi->propagation;
276 }
277
278 void GI::voxel_gi_set_energy(RID p_voxel_gi, float p_energy) {
279 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
280 ERR_FAIL_NULL(voxel_gi);
281
282 voxel_gi->energy = p_energy;
283 }
284
285 float GI::voxel_gi_get_energy(RID p_voxel_gi) const {
286 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
287 ERR_FAIL_NULL_V(voxel_gi, 0);
288 return voxel_gi->energy;
289 }
290
291 void GI::voxel_gi_set_baked_exposure_normalization(RID p_voxel_gi, float p_baked_exposure) {
292 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
293 ERR_FAIL_NULL(voxel_gi);
294
295 voxel_gi->baked_exposure = p_baked_exposure;
296 }
297
298 float GI::voxel_gi_get_baked_exposure_normalization(RID p_voxel_gi) const {
299 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
300 ERR_FAIL_NULL_V(voxel_gi, 0);
301 return voxel_gi->baked_exposure;
302 }
303
304 void GI::voxel_gi_set_bias(RID p_voxel_gi, float p_bias) {
305 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
306 ERR_FAIL_NULL(voxel_gi);
307
308 voxel_gi->bias = p_bias;
309 }
310
311 float GI::voxel_gi_get_bias(RID p_voxel_gi) const {
312 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
313 ERR_FAIL_NULL_V(voxel_gi, 0);
314 return voxel_gi->bias;
315 }
316
317 void GI::voxel_gi_set_normal_bias(RID p_voxel_gi, float p_normal_bias) {
318 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
319 ERR_FAIL_NULL(voxel_gi);
320
321 voxel_gi->normal_bias = p_normal_bias;
322 }
323
324 float GI::voxel_gi_get_normal_bias(RID p_voxel_gi) const {
325 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
326 ERR_FAIL_NULL_V(voxel_gi, 0);
327 return voxel_gi->normal_bias;
328 }
329
330 void GI::voxel_gi_set_interior(RID p_voxel_gi, bool p_enable) {
331 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
332 ERR_FAIL_NULL(voxel_gi);
333
334 voxel_gi->interior = p_enable;
335 }
336
337 void GI::voxel_gi_set_use_two_bounces(RID p_voxel_gi, bool p_enable) {
338 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
339 ERR_FAIL_NULL(voxel_gi);
340
341 voxel_gi->use_two_bounces = p_enable;
342 voxel_gi->version++;
343 }
344
345 bool GI::voxel_gi_is_using_two_bounces(RID p_voxel_gi) const {
346 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
347 ERR_FAIL_NULL_V(voxel_gi, false);
348 return voxel_gi->use_two_bounces;
349 }
350
351 bool GI::voxel_gi_is_interior(RID p_voxel_gi) const {
352 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
353 ERR_FAIL_NULL_V(voxel_gi, false);
354 return voxel_gi->interior;
355 }
356
357 uint32_t GI::voxel_gi_get_version(RID p_voxel_gi) const {
358 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
359 ERR_FAIL_NULL_V(voxel_gi, 0);
360 return voxel_gi->version;
361 }
362
363 uint32_t GI::voxel_gi_get_data_version(RID p_voxel_gi) {
364 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
365 ERR_FAIL_NULL_V(voxel_gi, 0);
366 return voxel_gi->data_version;
367 }
368
369 RID GI::voxel_gi_get_octree_buffer(RID p_voxel_gi) const {
370 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
371 ERR_FAIL_NULL_V(voxel_gi, RID());
372 return voxel_gi->octree_buffer;
373 }
374
375 RID GI::voxel_gi_get_data_buffer(RID p_voxel_gi) const {
376 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
377 ERR_FAIL_NULL_V(voxel_gi, RID());
378 return voxel_gi->data_buffer;
379 }
380
381 RID GI::voxel_gi_get_sdf_texture(RID p_voxel_gi) {
382 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
383 ERR_FAIL_NULL_V(voxel_gi, RID());
384
385 return voxel_gi->sdf_texture;
386 }
387
388 Dependency *GI::voxel_gi_get_dependency(RID p_voxel_gi) const {
389 VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
390 ERR_FAIL_NULL_V(voxel_gi, nullptr);
391
392 return &voxel_gi->dependency;
393 }
394
395 void GI::sdfgi_reset() {
396 sdfgi_current_version++;
397 }
398
399 ////////////////////////////////////////////////////////////////////////////////
400 // SDFGI
401
402 static RID create_clear_texture(const RD::TextureFormat &p_format, const String &p_name) {
403 RID texture = RD::get_singleton()->texture_create(p_format, RD::TextureView());
404 ERR_FAIL_COND_V_MSG(texture.is_null(), RID(), String("Cannot create texture: ") + p_name);
405
406 RD::get_singleton()->set_resource_name(texture, p_name);
407 RD::get_singleton()->texture_clear(texture, Color(0, 0, 0, 0), 0, p_format.mipmaps, 0, p_format.array_layers);
408
409 return texture;
410 }
411
412 void GI::SDFGI::create(RID p_env, const Vector3 &p_world_position, uint32_t p_requested_history_size, GI *p_gi) {
413 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
414 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
415
416 gi = p_gi;
417 num_cascades = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_cascades(p_env);
418 min_cell_size = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_min_cell_size(p_env);
419 uses_occlusion = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_use_occlusion(p_env);
420 y_scale_mode = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_y_scale(p_env);
421 static const float y_scale[3] = { 2.0, 1.5, 1.0 };
422 y_mult = y_scale[y_scale_mode];
423 version = gi->sdfgi_current_version;
424 cascades.resize(num_cascades);
425 probe_axis_count = SDFGI::PROBE_DIVISOR + 1;
426 solid_cell_ratio = gi->sdfgi_solid_cell_ratio;
427 solid_cell_count = uint32_t(float(cascade_size * cascade_size * cascade_size) * solid_cell_ratio);
428
429 float base_cell_size = min_cell_size;
430
431 RD::TextureFormat tf_sdf;
432 tf_sdf.format = RD::DATA_FORMAT_R8_UNORM;
433 tf_sdf.width = cascade_size; // Always 64x64
434 tf_sdf.height = cascade_size;
435 tf_sdf.depth = cascade_size;
436 tf_sdf.texture_type = RD::TEXTURE_TYPE_3D;
437 tf_sdf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
438
439 {
440 RD::TextureFormat tf_render = tf_sdf;
441 tf_render.format = RD::DATA_FORMAT_R16_UINT;
442 render_albedo = create_clear_texture(tf_render, "SDFGI Render Albedo");
443
444 tf_render.format = RD::DATA_FORMAT_R32_UINT;
445 render_emission = create_clear_texture(tf_render, "SDFGI Render Emission");
446 render_emission_aniso = create_clear_texture(tf_render, "SDFGI Render Emission Aniso");
447
448 tf_render.format = RD::DATA_FORMAT_R8_UNORM; //at least its easy to visualize
449
450 for (int i = 0; i < 8; i++) {
451 render_occlusion[i] = create_clear_texture(tf_render, String("SDFGI Render Occlusion ") + itos(i));
452 }
453
454 tf_render.format = RD::DATA_FORMAT_R32_UINT;
455 render_geom_facing = create_clear_texture(tf_render, "SDFGI Render Geometry Facing");
456
457 tf_render.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
458 render_sdf[0] = create_clear_texture(tf_render, "SDFGI Render SDF 0");
459 render_sdf[1] = create_clear_texture(tf_render, "SDFGI Render SDF 1");
460
461 tf_render.width /= 2;
462 tf_render.height /= 2;
463 tf_render.depth /= 2;
464
465 render_sdf_half[0] = create_clear_texture(tf_render, "SDFGI Render SDF Half 0");
466 render_sdf_half[1] = create_clear_texture(tf_render, "SDFGI Render SDF Half 1");
467 }
468
469 RD::TextureFormat tf_occlusion = tf_sdf;
470 tf_occlusion.format = RD::DATA_FORMAT_R16_UINT;
471 tf_occlusion.shareable_formats.push_back(RD::DATA_FORMAT_R16_UINT);
472 tf_occlusion.shareable_formats.push_back(RD::DATA_FORMAT_R4G4B4A4_UNORM_PACK16);
473 tf_occlusion.depth *= cascades.size(); //use depth for occlusion slices
474 tf_occlusion.width *= 2; //use width for the other half
475
476 RD::TextureFormat tf_light = tf_sdf;
477 tf_light.format = RD::DATA_FORMAT_R32_UINT;
478 tf_light.shareable_formats.push_back(RD::DATA_FORMAT_R32_UINT);
479 tf_light.shareable_formats.push_back(RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
480
481 RD::TextureFormat tf_aniso0 = tf_sdf;
482 tf_aniso0.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
483 RD::TextureFormat tf_aniso1 = tf_sdf;
484 tf_aniso1.format = RD::DATA_FORMAT_R8G8_UNORM;
485
486 int passes = nearest_shift(cascade_size) - 1;
487
488 //store lightprobe SH
489 RD::TextureFormat tf_probes;
490 tf_probes.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
491 tf_probes.width = probe_axis_count * probe_axis_count;
492 tf_probes.height = probe_axis_count * SDFGI::SH_SIZE;
493 tf_probes.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
494 tf_probes.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
495
496 history_size = p_requested_history_size;
497
498 RD::TextureFormat tf_probe_history = tf_probes;
499 tf_probe_history.format = RD::DATA_FORMAT_R16G16B16A16_SINT; //signed integer because SH are signed
500 tf_probe_history.array_layers = history_size;
501
502 RD::TextureFormat tf_probe_average = tf_probes;
503 tf_probe_average.format = RD::DATA_FORMAT_R32G32B32A32_SINT; //signed integer because SH are signed
504 tf_probe_average.texture_type = RD::TEXTURE_TYPE_2D;
505
506 lightprobe_history_scroll = create_clear_texture(tf_probe_history, "SDFGI LightProbe History Scroll");
507 lightprobe_average_scroll = create_clear_texture(tf_probe_average, "SDFGI LightProbe Average Scroll");
508
509 {
510 //octahedral lightprobes
511 RD::TextureFormat tf_octprobes = tf_probes;
512 tf_octprobes.array_layers = cascades.size() * 2;
513 tf_octprobes.format = RD::DATA_FORMAT_R32_UINT; //pack well with RGBE
514 tf_octprobes.width = probe_axis_count * probe_axis_count * (SDFGI::LIGHTPROBE_OCT_SIZE + 2);
515 tf_octprobes.height = probe_axis_count * (SDFGI::LIGHTPROBE_OCT_SIZE + 2);
516 tf_octprobes.shareable_formats.push_back(RD::DATA_FORMAT_R32_UINT);
517 tf_octprobes.shareable_formats.push_back(RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
518 //lightprobe texture is an octahedral texture
519
520 lightprobe_data = create_clear_texture(tf_octprobes, "SDFGI LightProbe Data");
521 RD::TextureView tv;
522 tv.format_override = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
523 lightprobe_texture = RD::get_singleton()->texture_create_shared(tv, lightprobe_data);
524
525 //texture handling ambient data, to integrate with volumetric foc
526 RD::TextureFormat tf_ambient = tf_probes;
527 tf_ambient.array_layers = cascades.size();
528 tf_ambient.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT; //pack well with RGBE
529 tf_ambient.width = probe_axis_count * probe_axis_count;
530 tf_ambient.height = probe_axis_count;
531 tf_ambient.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
532 //lightprobe texture is an octahedral texture
533 ambient_texture = create_clear_texture(tf_ambient, "SDFGI Ambient Texture");
534 }
535
536 cascades_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES);
537
538 occlusion_data = create_clear_texture(tf_occlusion, "SDFGI Occlusion Data");
539 {
540 RD::TextureView tv;
541 tv.format_override = RD::DATA_FORMAT_R4G4B4A4_UNORM_PACK16;
542 occlusion_texture = RD::get_singleton()->texture_create_shared(tv, occlusion_data);
543 }
544
545 for (SDFGI::Cascade &cascade : cascades) {
546 /* 3D Textures */
547
548 cascade.sdf_tex = create_clear_texture(tf_sdf, "SDFGI Cascade SDF Texture");
549
550 cascade.light_data = create_clear_texture(tf_light, "SDFGI Cascade Light Data");
551
552 cascade.light_aniso_0_tex = create_clear_texture(tf_aniso0, "SDFGI Cascade Light Aniso 0 Texture");
553 cascade.light_aniso_1_tex = create_clear_texture(tf_aniso1, "SDFGI Cascade Light Aniso 1 Texture");
554
555 {
556 RD::TextureView tv;
557 tv.format_override = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
558 cascade.light_tex = RD::get_singleton()->texture_create_shared(tv, cascade.light_data);
559 }
560
561 cascade.cell_size = base_cell_size;
562 Vector3 world_position = p_world_position;
563 world_position.y *= y_mult;
564 int32_t probe_cells = cascade_size / SDFGI::PROBE_DIVISOR;
565 Vector3 probe_size = Vector3(1, 1, 1) * cascade.cell_size * probe_cells;
566 Vector3i probe_pos = Vector3i((world_position / probe_size + Vector3(0.5, 0.5, 0.5)).floor());
567 cascade.position = probe_pos * probe_cells;
568
569 cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
570
571 base_cell_size *= 2.0;
572
573 /* Probe History */
574
575 cascade.lightprobe_history_tex = RD::get_singleton()->texture_create(tf_probe_history, RD::TextureView());
576 RD::get_singleton()->set_resource_name(cascade.lightprobe_history_tex, "SDFGI Cascade LightProbe History Texture");
577 RD::get_singleton()->texture_clear(cascade.lightprobe_history_tex, Color(0, 0, 0, 0), 0, 1, 0, tf_probe_history.array_layers); //needs to be cleared for average to work
578
579 cascade.lightprobe_average_tex = RD::get_singleton()->texture_create(tf_probe_average, RD::TextureView());
580 RD::get_singleton()->set_resource_name(cascade.lightprobe_average_tex, "SDFGI Cascade LightProbe Average Texture");
581 RD::get_singleton()->texture_clear(cascade.lightprobe_average_tex, Color(0, 0, 0, 0), 0, 1, 0, 1); //needs to be cleared for average to work
582
583 /* Buffers */
584
585 cascade.solid_cell_buffer = RD::get_singleton()->storage_buffer_create(sizeof(SDFGI::Cascade::SolidCell) * solid_cell_count);
586 cascade.solid_cell_dispatch_buffer_storage = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4, Vector<uint8_t>());
587 cascade.solid_cell_dispatch_buffer_call = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4, Vector<uint8_t>(), RD::STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT);
588 cascade.lights_buffer = RD::get_singleton()->storage_buffer_create(sizeof(SDFGIShader::Light) * MAX(SDFGI::MAX_STATIC_LIGHTS, SDFGI::MAX_DYNAMIC_LIGHTS));
589 {
590 Vector<RD::Uniform> uniforms;
591 {
592 RD::Uniform u;
593 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
594 u.binding = 1;
595 u.append_id(render_sdf[(passes & 1) ? 1 : 0]); //if passes are even, we read from buffer 0, else we read from buffer 1
596 uniforms.push_back(u);
597 }
598 {
599 RD::Uniform u;
600 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
601 u.binding = 2;
602 u.append_id(render_albedo);
603 uniforms.push_back(u);
604 }
605 {
606 RD::Uniform u;
607 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
608 u.binding = 3;
609 for (int j = 0; j < 8; j++) {
610 u.append_id(render_occlusion[j]);
611 }
612 uniforms.push_back(u);
613 }
614 {
615 RD::Uniform u;
616 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
617 u.binding = 4;
618 u.append_id(render_emission);
619 uniforms.push_back(u);
620 }
621 {
622 RD::Uniform u;
623 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
624 u.binding = 5;
625 u.append_id(render_emission_aniso);
626 uniforms.push_back(u);
627 }
628 {
629 RD::Uniform u;
630 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
631 u.binding = 6;
632 u.append_id(render_geom_facing);
633 uniforms.push_back(u);
634 }
635
636 {
637 RD::Uniform u;
638 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
639 u.binding = 7;
640 u.append_id(cascade.sdf_tex);
641 uniforms.push_back(u);
642 }
643 {
644 RD::Uniform u;
645 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
646 u.binding = 8;
647 u.append_id(occlusion_data);
648 uniforms.push_back(u);
649 }
650 {
651 RD::Uniform u;
652 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
653 u.binding = 10;
654 u.append_id(cascade.solid_cell_dispatch_buffer_storage);
655 uniforms.push_back(u);
656 }
657 {
658 RD::Uniform u;
659 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
660 u.binding = 11;
661 u.append_id(cascade.solid_cell_buffer);
662 uniforms.push_back(u);
663 }
664
665 cascade.sdf_store_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_STORE), 0);
666 }
667
668 {
669 Vector<RD::Uniform> uniforms;
670 {
671 RD::Uniform u;
672 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
673 u.binding = 1;
674 u.append_id(render_albedo);
675 uniforms.push_back(u);
676 }
677 {
678 RD::Uniform u;
679 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
680 u.binding = 2;
681 u.append_id(render_geom_facing);
682 uniforms.push_back(u);
683 }
684 {
685 RD::Uniform u;
686 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
687 u.binding = 3;
688 u.append_id(render_emission);
689 uniforms.push_back(u);
690 }
691 {
692 RD::Uniform u;
693 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
694 u.binding = 4;
695 u.append_id(render_emission_aniso);
696 uniforms.push_back(u);
697 }
698 {
699 RD::Uniform u;
700 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
701 u.binding = 5;
702 u.append_id(cascade.solid_cell_dispatch_buffer_storage);
703 uniforms.push_back(u);
704 }
705 {
706 RD::Uniform u;
707 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
708 u.binding = 6;
709 u.append_id(cascade.solid_cell_buffer);
710 uniforms.push_back(u);
711 }
712
713 cascade.scroll_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_SCROLL), 0);
714 }
715 {
716 Vector<RD::Uniform> uniforms;
717 {
718 RD::Uniform u;
719 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
720 u.binding = 1;
721 for (int j = 0; j < 8; j++) {
722 u.append_id(render_occlusion[j]);
723 }
724 uniforms.push_back(u);
725 }
726 {
727 RD::Uniform u;
728 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
729 u.binding = 2;
730 u.append_id(occlusion_data);
731 uniforms.push_back(u);
732 }
733
734 cascade.scroll_occlusion_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_SCROLL_OCCLUSION), 0);
735 }
736 }
737
738 //direct light
739 for (SDFGI::Cascade &cascade : cascades) {
740 Vector<RD::Uniform> uniforms;
741 {
742 RD::Uniform u;
743 u.binding = 1;
744 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
745 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
746 if (j < cascades.size()) {
747 u.append_id(cascades[j].sdf_tex);
748 } else {
749 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
750 }
751 }
752 uniforms.push_back(u);
753 }
754 {
755 RD::Uniform u;
756 u.binding = 2;
757 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
758 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
759 uniforms.push_back(u);
760 }
761 {
762 RD::Uniform u;
763 u.binding = 3;
764 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
765 u.append_id(cascade.solid_cell_dispatch_buffer_storage);
766 uniforms.push_back(u);
767 }
768 {
769 RD::Uniform u;
770 u.binding = 4;
771 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
772 u.append_id(cascade.solid_cell_buffer);
773 uniforms.push_back(u);
774 }
775 {
776 RD::Uniform u;
777 u.binding = 5;
778 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
779 u.append_id(cascade.light_data);
780 uniforms.push_back(u);
781 }
782 {
783 RD::Uniform u;
784 u.binding = 6;
785 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
786 u.append_id(cascade.light_aniso_0_tex);
787 uniforms.push_back(u);
788 }
789 {
790 RD::Uniform u;
791 u.binding = 7;
792 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
793 u.append_id(cascade.light_aniso_1_tex);
794 uniforms.push_back(u);
795 }
796 {
797 RD::Uniform u;
798 u.binding = 8;
799 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
800 u.append_id(cascades_ubo);
801 uniforms.push_back(u);
802 }
803 {
804 RD::Uniform u;
805 u.binding = 9;
806 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
807 u.append_id(cascade.lights_buffer);
808 uniforms.push_back(u);
809 }
810 {
811 RD::Uniform u;
812 u.binding = 10;
813 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
814 u.append_id(lightprobe_texture);
815 uniforms.push_back(u);
816 }
817 {
818 RD::Uniform u;
819 u.binding = 11;
820 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
821 u.append_id(occlusion_texture);
822 uniforms.push_back(u);
823 }
824
825 cascade.sdf_direct_light_static_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.direct_light.version_get_shader(gi->sdfgi_shader.direct_light_shader, SDFGIShader::DIRECT_LIGHT_MODE_STATIC), 0);
826 cascade.sdf_direct_light_dynamic_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.direct_light.version_get_shader(gi->sdfgi_shader.direct_light_shader, SDFGIShader::DIRECT_LIGHT_MODE_DYNAMIC), 0);
827 }
828
829 //preprocess initialize uniform set
830 {
831 Vector<RD::Uniform> uniforms;
832 {
833 RD::Uniform u;
834 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
835 u.binding = 1;
836 u.append_id(render_albedo);
837 uniforms.push_back(u);
838 }
839 {
840 RD::Uniform u;
841 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
842 u.binding = 2;
843 u.append_id(render_sdf[0]);
844 uniforms.push_back(u);
845 }
846
847 sdf_initialize_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE), 0);
848 }
849
850 {
851 Vector<RD::Uniform> uniforms;
852 {
853 RD::Uniform u;
854 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
855 u.binding = 1;
856 u.append_id(render_albedo);
857 uniforms.push_back(u);
858 }
859 {
860 RD::Uniform u;
861 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
862 u.binding = 2;
863 u.append_id(render_sdf_half[0]);
864 uniforms.push_back(u);
865 }
866
867 sdf_initialize_half_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF), 0);
868 }
869
870 //jump flood uniform set
871 {
872 Vector<RD::Uniform> uniforms;
873 {
874 RD::Uniform u;
875 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
876 u.binding = 1;
877 u.append_id(render_sdf[0]);
878 uniforms.push_back(u);
879 }
880 {
881 RD::Uniform u;
882 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
883 u.binding = 2;
884 u.append_id(render_sdf[1]);
885 uniforms.push_back(u);
886 }
887
888 jump_flood_uniform_set[0] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
889 RID aux0 = uniforms.write[0].get_id(0);
890 RID aux1 = uniforms.write[1].get_id(0);
891 uniforms.write[0].set_id(0, aux1);
892 uniforms.write[1].set_id(0, aux0);
893 jump_flood_uniform_set[1] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
894 }
895 //jump flood half uniform set
896 {
897 Vector<RD::Uniform> uniforms;
898 {
899 RD::Uniform u;
900 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
901 u.binding = 1;
902 u.append_id(render_sdf_half[0]);
903 uniforms.push_back(u);
904 }
905 {
906 RD::Uniform u;
907 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
908 u.binding = 2;
909 u.append_id(render_sdf_half[1]);
910 uniforms.push_back(u);
911 }
912
913 jump_flood_half_uniform_set[0] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
914 RID aux0 = uniforms.write[0].get_id(0);
915 RID aux1 = uniforms.write[1].get_id(0);
916 uniforms.write[0].set_id(0, aux1);
917 uniforms.write[1].set_id(0, aux0);
918 jump_flood_half_uniform_set[1] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
919 }
920
921 //upscale half size sdf
922 {
923 Vector<RD::Uniform> uniforms;
924 {
925 RD::Uniform u;
926 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
927 u.binding = 1;
928 u.append_id(render_albedo);
929 uniforms.push_back(u);
930 }
931 {
932 RD::Uniform u;
933 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
934 u.binding = 2;
935 u.append_id(render_sdf_half[(passes & 1) ? 0 : 1]); //reverse pass order because half size
936 uniforms.push_back(u);
937 }
938 {
939 RD::Uniform u;
940 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
941 u.binding = 3;
942 u.append_id(render_sdf[(passes & 1) ? 0 : 1]); //reverse pass order because it needs an extra JFA pass
943 uniforms.push_back(u);
944 }
945
946 upscale_jfa_uniform_set_index = (passes & 1) ? 0 : 1;
947 sdf_upscale_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD_UPSCALE), 0);
948 }
949
950 //occlusion uniform set
951 {
952 Vector<RD::Uniform> uniforms;
953 {
954 RD::Uniform u;
955 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
956 u.binding = 1;
957 u.append_id(render_albedo);
958 uniforms.push_back(u);
959 }
960 {
961 RD::Uniform u;
962 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
963 u.binding = 2;
964 for (int i = 0; i < 8; i++) {
965 u.append_id(render_occlusion[i]);
966 }
967 uniforms.push_back(u);
968 }
969 {
970 RD::Uniform u;
971 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
972 u.binding = 3;
973 u.append_id(render_geom_facing);
974 uniforms.push_back(u);
975 }
976
977 occlusion_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_OCCLUSION), 0);
978 }
979
980 for (uint32_t i = 0; i < cascades.size(); i++) {
981 //integrate uniform
982
983 Vector<RD::Uniform> uniforms;
984
985 {
986 RD::Uniform u;
987 u.binding = 1;
988 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
989 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
990 if (j < cascades.size()) {
991 u.append_id(cascades[j].sdf_tex);
992 } else {
993 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
994 }
995 }
996 uniforms.push_back(u);
997 }
998 {
999 RD::Uniform u;
1000 u.binding = 2;
1001 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1002 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
1003 if (j < cascades.size()) {
1004 u.append_id(cascades[j].light_tex);
1005 } else {
1006 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1007 }
1008 }
1009 uniforms.push_back(u);
1010 }
1011 {
1012 RD::Uniform u;
1013 u.binding = 3;
1014 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1015 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
1016 if (j < cascades.size()) {
1017 u.append_id(cascades[j].light_aniso_0_tex);
1018 } else {
1019 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1020 }
1021 }
1022 uniforms.push_back(u);
1023 }
1024 {
1025 RD::Uniform u;
1026 u.binding = 4;
1027 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1028 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
1029 if (j < cascades.size()) {
1030 u.append_id(cascades[j].light_aniso_1_tex);
1031 } else {
1032 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1033 }
1034 }
1035 uniforms.push_back(u);
1036 }
1037 {
1038 RD::Uniform u;
1039 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
1040 u.binding = 6;
1041 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
1042 uniforms.push_back(u);
1043 }
1044
1045 {
1046 RD::Uniform u;
1047 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1048 u.binding = 7;
1049 u.append_id(cascades_ubo);
1050 uniforms.push_back(u);
1051 }
1052 {
1053 RD::Uniform u;
1054 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1055 u.binding = 8;
1056 u.append_id(lightprobe_data);
1057 uniforms.push_back(u);
1058 }
1059
1060 {
1061 RD::Uniform u;
1062 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1063 u.binding = 9;
1064 u.append_id(cascades[i].lightprobe_history_tex);
1065 uniforms.push_back(u);
1066 }
1067 {
1068 RD::Uniform u;
1069 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1070 u.binding = 10;
1071 u.append_id(cascades[i].lightprobe_average_tex);
1072 uniforms.push_back(u);
1073 }
1074
1075 {
1076 RD::Uniform u;
1077 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1078 u.binding = 11;
1079 u.append_id(lightprobe_history_scroll);
1080 uniforms.push_back(u);
1081 }
1082 {
1083 RD::Uniform u;
1084 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1085 u.binding = 12;
1086 u.append_id(lightprobe_average_scroll);
1087 uniforms.push_back(u);
1088 }
1089 {
1090 RD::Uniform u;
1091 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1092 u.binding = 13;
1093 RID parent_average;
1094 if (cascades.size() == 1) {
1095 // If there is only one SDFGI cascade, we can't use the previous cascade for blending.
1096 parent_average = cascades[i].lightprobe_average_tex;
1097 } else if (i < cascades.size() - 1) {
1098 parent_average = cascades[i + 1].lightprobe_average_tex;
1099 } else {
1100 parent_average = cascades[i - 1].lightprobe_average_tex; //to use something, but it won't be used
1101 }
1102 u.append_id(parent_average);
1103 uniforms.push_back(u);
1104 }
1105 {
1106 RD::Uniform u;
1107 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1108 u.binding = 14;
1109 u.append_id(ambient_texture);
1110 uniforms.push_back(u);
1111 }
1112
1113 cascades[i].integrate_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.integrate.version_get_shader(gi->sdfgi_shader.integrate_shader, 0), 0);
1114 }
1115
1116 bounce_feedback = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_bounce_feedback(p_env);
1117 energy = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_energy(p_env);
1118 normal_bias = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_normal_bias(p_env);
1119 probe_bias = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_probe_bias(p_env);
1120 reads_sky = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_read_sky_light(p_env);
1121 }
1122
1123 void GI::SDFGI::free_data() {
1124 // we don't free things here, we handle SDFGI differently at the moment destructing the object when it needs to change.
1125 }
1126
1127 GI::SDFGI::~SDFGI() {
1128 for (const SDFGI::Cascade &c : cascades) {
1129 RD::get_singleton()->free_rid(c.light_data);
1130 RD::get_singleton()->free_rid(c.light_aniso_0_tex);
1131 RD::get_singleton()->free_rid(c.light_aniso_1_tex);
1132 RD::get_singleton()->free_rid(c.sdf_tex);
1133 RD::get_singleton()->free_rid(c.solid_cell_dispatch_buffer_storage);
1134 RD::get_singleton()->free_rid(c.solid_cell_dispatch_buffer_call);
1135 RD::get_singleton()->free_rid(c.solid_cell_buffer);
1136 RD::get_singleton()->free_rid(c.lightprobe_history_tex);
1137 RD::get_singleton()->free_rid(c.lightprobe_average_tex);
1138 RD::get_singleton()->free_rid(c.lights_buffer);
1139 }
1140
1141 RD::get_singleton()->free_rid(render_albedo);
1142 RD::get_singleton()->free_rid(render_emission);
1143 RD::get_singleton()->free_rid(render_emission_aniso);
1144
1145 RD::get_singleton()->free_rid(render_sdf[0]);
1146 RD::get_singleton()->free_rid(render_sdf[1]);
1147
1148 RD::get_singleton()->free_rid(render_sdf_half[0]);
1149 RD::get_singleton()->free_rid(render_sdf_half[1]);
1150
1151 for (int i = 0; i < 8; i++) {
1152 RD::get_singleton()->free_rid(render_occlusion[i]);
1153 }
1154
1155 RD::get_singleton()->free_rid(render_geom_facing);
1156
1157 RD::get_singleton()->free_rid(lightprobe_data);
1158 RD::get_singleton()->free_rid(lightprobe_history_scroll);
1159 RD::get_singleton()->free_rid(lightprobe_average_scroll);
1160 RD::get_singleton()->free_rid(occlusion_data);
1161 RD::get_singleton()->free_rid(ambient_texture);
1162
1163 RD::get_singleton()->free_rid(cascades_ubo);
1164
1165 for (uint32_t v = 0; v < RendererSceneRender::MAX_RENDER_VIEWS; v++) {
1166 if (RD::get_singleton()->uniform_set_is_valid(debug_uniform_set[v])) {
1167 RD::get_singleton()->free_rid(debug_uniform_set[v]);
1168 }
1169 debug_uniform_set[v] = RID();
1170 }
1171
1172 if (RD::get_singleton()->uniform_set_is_valid(debug_probes_uniform_set)) {
1173 RD::get_singleton()->free_rid(debug_probes_uniform_set);
1174 }
1175 debug_probes_uniform_set = RID();
1176
1177 if (debug_probes_scene_data_ubo.is_valid()) {
1178 RD::get_singleton()->free_rid(debug_probes_scene_data_ubo);
1179 debug_probes_scene_data_ubo = RID();
1180 }
1181 }
1182
1183 void GI::SDFGI::update(RID p_env, const Vector3 &p_world_position) {
1184 bounce_feedback = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_bounce_feedback(p_env);
1185 energy = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_energy(p_env);
1186 normal_bias = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_normal_bias(p_env);
1187 probe_bias = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_probe_bias(p_env);
1188 reads_sky = RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_read_sky_light(p_env);
1189
1190 int32_t drag_margin = (cascade_size / SDFGI::PROBE_DIVISOR) / 2;
1191
1192 for (SDFGI::Cascade &cascade : cascades) {
1193 cascade.dirty_regions = Vector3i();
1194
1195 Vector3 probe_half_size = Vector3(1, 1, 1) * cascade.cell_size * float(cascade_size / SDFGI::PROBE_DIVISOR) * 0.5;
1196 probe_half_size = Vector3(0, 0, 0);
1197
1198 Vector3 world_position = p_world_position;
1199 world_position.y *= y_mult;
1200 Vector3i pos_in_cascade = Vector3i((world_position + probe_half_size) / cascade.cell_size);
1201
1202 for (int j = 0; j < 3; j++) {
1203 if (pos_in_cascade[j] < cascade.position[j]) {
1204 while (pos_in_cascade[j] < (cascade.position[j] - drag_margin)) {
1205 cascade.position[j] -= drag_margin * 2;
1206 cascade.dirty_regions[j] += drag_margin * 2;
1207 }
1208 } else if (pos_in_cascade[j] > cascade.position[j]) {
1209 while (pos_in_cascade[j] > (cascade.position[j] + drag_margin)) {
1210 cascade.position[j] += drag_margin * 2;
1211 cascade.dirty_regions[j] -= drag_margin * 2;
1212 }
1213 }
1214
1215 if (cascade.dirty_regions[j] == 0) {
1216 continue; // not dirty
1217 } else if (uint32_t(Math::abs(cascade.dirty_regions[j])) >= cascade_size) {
1218 //moved too much, just redraw everything (make all dirty)
1219 cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
1220 break;
1221 }
1222 }
1223
1224 if (cascade.dirty_regions != Vector3i() && cascade.dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
1225 //see how much the total dirty volume represents from the total volume
1226 uint32_t total_volume = cascade_size * cascade_size * cascade_size;
1227 uint32_t safe_volume = 1;
1228 for (int j = 0; j < 3; j++) {
1229 safe_volume *= cascade_size - Math::abs(cascade.dirty_regions[j]);
1230 }
1231 uint32_t dirty_volume = total_volume - safe_volume;
1232 if (dirty_volume > (safe_volume / 2)) {
1233 //more than half the volume is dirty, make all dirty so its only rendered once
1234 cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
1235 }
1236 }
1237 }
1238 }
1239
1240 void GI::SDFGI::update_light() {
1241 RD::get_singleton()->draw_command_begin_label("SDFGI Update Dynamic Light");
1242
1243 for (uint32_t i = 0; i < cascades.size(); i++) {
1244 RD::get_singleton()->buffer_copy(cascades[i].solid_cell_dispatch_buffer_storage, cascades[i].solid_cell_dispatch_buffer_call, 0, 0, sizeof(uint32_t) * 4);
1245 }
1246
1247 /* Update dynamic light */
1248
1249 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1250 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.direct_light_pipeline[SDFGIShader::DIRECT_LIGHT_MODE_DYNAMIC].get_rid());
1251
1252 SDFGIShader::DirectLightPushConstant push_constant;
1253
1254 push_constant.grid_size[0] = cascade_size;
1255 push_constant.grid_size[1] = cascade_size;
1256 push_constant.grid_size[2] = cascade_size;
1257 push_constant.max_cascades = cascades.size();
1258 push_constant.probe_axis_size = probe_axis_count;
1259 push_constant.bounce_feedback = bounce_feedback;
1260 push_constant.y_mult = y_mult;
1261 push_constant.use_occlusion = uses_occlusion;
1262
1263 for (uint32_t i = 0; i < cascades.size(); i++) {
1264 SDFGI::Cascade &cascade = cascades[i];
1265 push_constant.light_count = cascade_dynamic_light_count[i];
1266 push_constant.cascade = i;
1267
1268 if (cascades[i].all_dynamic_lights_dirty || gi->sdfgi_frames_to_update_light == RS::ENV_SDFGI_UPDATE_LIGHT_IN_1_FRAME) {
1269 push_constant.process_offset = 0;
1270 push_constant.process_increment = 1;
1271 } else {
1272 static const uint32_t frames_to_update_table[RS::ENV_SDFGI_UPDATE_LIGHT_MAX] = {
1273 1, 2, 4, 8, 16
1274 };
1275
1276 uint32_t frames_to_update = frames_to_update_table[gi->sdfgi_frames_to_update_light];
1277
1278 push_constant.process_offset = RSG::rasterizer->get_frame_number() % frames_to_update;
1279 push_constant.process_increment = frames_to_update;
1280 }
1281 cascades[i].all_dynamic_lights_dirty = false;
1282
1283 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascade.sdf_direct_light_dynamic_uniform_set, 0);
1284 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::DirectLightPushConstant));
1285 RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cascade.solid_cell_dispatch_buffer_call, 0);
1286 }
1287 RD::get_singleton()->compute_list_end();
1288 RD::get_singleton()->draw_command_end_label();
1289 }
1290
1291 void GI::SDFGI::update_probes(RID p_env, SkyRD::Sky *p_sky) {
1292 RD::get_singleton()->draw_command_begin_label("SDFGI Update Probes");
1293
1294 SDFGIShader::IntegratePushConstant push_constant;
1295 push_constant.grid_size[1] = cascade_size;
1296 push_constant.grid_size[2] = cascade_size;
1297 push_constant.grid_size[0] = cascade_size;
1298 push_constant.max_cascades = cascades.size();
1299 push_constant.probe_axis_size = probe_axis_count;
1300 push_constant.history_index = render_pass % history_size;
1301 push_constant.history_size = history_size;
1302 static const uint32_t ray_count[RS::ENV_SDFGI_RAY_COUNT_MAX] = { 4, 8, 16, 32, 64, 96, 128 };
1303 push_constant.ray_count = ray_count[gi->sdfgi_ray_count];
1304 push_constant.ray_bias = probe_bias;
1305 push_constant.image_size[0] = probe_axis_count * probe_axis_count;
1306 push_constant.image_size[1] = probe_axis_count;
1307 push_constant.store_ambient_texture = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_enabled(p_env);
1308
1309 const float sky_irradiance_border_size = p_sky != nullptr ? p_sky->uv_border_size : 0.0f;
1310 push_constant.sky_irradiance_border_size[0] = sky_irradiance_border_size;
1311 push_constant.sky_irradiance_border_size[1] = 1.0 - sky_irradiance_border_size * 2.0f;
1312
1313 RID sky_uniform_set = gi->sdfgi_shader.integrate_default_sky_uniform_set;
1314 push_constant.sky_flags = 0;
1315 push_constant.y_mult = y_mult;
1316
1317 if (reads_sky && p_env.is_valid()) {
1318 push_constant.sky_energy = RendererSceneRenderRD::get_singleton()->environment_get_bg_energy_multiplier(p_env);
1319
1320 if (RendererSceneRenderRD::get_singleton()->environment_get_background(p_env) == RS::ENV_BG_CLEAR_COLOR) {
1321 push_constant.sky_flags |= SDFGIShader::IntegratePushConstant::SKY_FLAGS_MODE_COLOR;
1322 Color c = RSG::texture_storage->get_default_clear_color().srgb_to_linear();
1323 push_constant.sky_color_or_orientation[0] = c.r;
1324 push_constant.sky_color_or_orientation[1] = c.g;
1325 push_constant.sky_color_or_orientation[2] = c.b;
1326 } else if (RendererSceneRenderRD::get_singleton()->environment_get_background(p_env) == RS::ENV_BG_COLOR) {
1327 push_constant.sky_flags |= SDFGIShader::IntegratePushConstant::SKY_FLAGS_MODE_COLOR;
1328 Color c = RendererSceneRenderRD::get_singleton()->environment_get_bg_color(p_env);
1329 push_constant.sky_color_or_orientation[0] = c.r;
1330 push_constant.sky_color_or_orientation[1] = c.g;
1331 push_constant.sky_color_or_orientation[2] = c.b;
1332
1333 } else if (RendererSceneRenderRD::get_singleton()->environment_get_background(p_env) == RS::ENV_BG_SKY) {
1334 if (p_sky && p_sky->radiance.is_valid()) {
1335 if (integrate_sky_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(integrate_sky_uniform_set)) {
1336 Vector<RD::Uniform> uniforms;
1337
1338 {
1339 RD::Uniform u;
1340 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1341 u.binding = 0;
1342 u.append_id(p_sky->radiance);
1343 uniforms.push_back(u);
1344 }
1345
1346 {
1347 RD::Uniform u;
1348 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
1349 u.binding = 1;
1350 u.append_id(RendererRD::MaterialStorage::get_singleton()->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
1351 uniforms.push_back(u);
1352 }
1353
1354 integrate_sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.integrate.version_get_shader(gi->sdfgi_shader.integrate_shader, 0), 1);
1355 }
1356 sky_uniform_set = integrate_sky_uniform_set;
1357 push_constant.sky_flags |= SDFGIShader::IntegratePushConstant::SKY_FLAGS_MODE_SKY;
1358
1359 // Encode sky orientation as quaternion in existing push constants.
1360 const Basis sky_basis = RendererSceneRenderRD::get_singleton()->environment_get_sky_orientation(p_env);
1361 const Quaternion sky_quaternion = sky_basis.get_quaternion().inverse();
1362 push_constant.sky_color_or_orientation[0] = sky_quaternion.x;
1363 push_constant.sky_color_or_orientation[1] = sky_quaternion.y;
1364 push_constant.sky_color_or_orientation[2] = sky_quaternion.z;
1365 // Ideally we would reconstruct the largest component for least error, but sky contribution to GI is low frequency so just needs to get the idea across.
1366 push_constant.sky_flags |= SDFGIShader::IntegratePushConstant::SKY_FLAGS_ORIENTATION_SIGN * (sky_quaternion.w < 0.0 ? 0 : 1);
1367 }
1368 }
1369 }
1370
1371 render_pass++;
1372
1373 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1374 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_PROCESS].get_rid());
1375
1376 int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
1377 for (uint32_t i = 0; i < cascades.size(); i++) {
1378 push_constant.cascade = i;
1379 push_constant.world_offset[0] = cascades[i].position.x / probe_divisor;
1380 push_constant.world_offset[1] = cascades[i].position.y / probe_divisor;
1381 push_constant.world_offset[2] = cascades[i].position.z / probe_divisor;
1382
1383 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[i].integrate_uniform_set, 0);
1384 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sky_uniform_set, 1);
1385
1386 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::IntegratePushConstant));
1387 RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
1388 }
1389
1390 RD::get_singleton()->compute_list_end();
1391 RD::get_singleton()->draw_command_end_label();
1392 }
1393
1394 void GI::SDFGI::store_probes() {
1395 RD::get_singleton()->draw_command_begin_label("SDFGI Store Probes");
1396
1397 SDFGIShader::IntegratePushConstant push_constant;
1398 push_constant.grid_size[1] = cascade_size;
1399 push_constant.grid_size[2] = cascade_size;
1400 push_constant.grid_size[0] = cascade_size;
1401 push_constant.max_cascades = cascades.size();
1402 push_constant.probe_axis_size = probe_axis_count;
1403 push_constant.history_index = render_pass % history_size;
1404 push_constant.history_size = history_size;
1405 static const uint32_t ray_count[RS::ENV_SDFGI_RAY_COUNT_MAX] = { 4, 8, 16, 32, 64, 96, 128 };
1406 push_constant.ray_count = ray_count[gi->sdfgi_ray_count];
1407 push_constant.ray_bias = probe_bias;
1408 push_constant.image_size[0] = probe_axis_count * probe_axis_count;
1409 push_constant.image_size[1] = probe_axis_count;
1410 push_constant.store_ambient_texture = false;
1411
1412 push_constant.sky_flags = 0;
1413 push_constant.y_mult = y_mult;
1414
1415 // Then store values into the lightprobe texture. Separating these steps has a small performance hit, but it allows for multiple bounces
1416 RENDER_TIMESTAMP("Average SDFGI Probes");
1417
1418 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1419 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_STORE].get_rid());
1420
1421 //convert to octahedral to store
1422 push_constant.image_size[0] *= SDFGI::LIGHTPROBE_OCT_SIZE;
1423 push_constant.image_size[1] *= SDFGI::LIGHTPROBE_OCT_SIZE;
1424
1425 for (uint32_t i = 0; i < cascades.size(); i++) {
1426 push_constant.cascade = i;
1427 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[i].integrate_uniform_set, 0);
1428 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
1429 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::IntegratePushConstant));
1430 RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, 1);
1431 }
1432
1433 RD::get_singleton()->compute_list_end();
1434
1435 RD::get_singleton()->draw_command_end_label();
1436 }
1437
1438 int GI::SDFGI::get_pending_region_data(int p_region, Vector3i &r_local_offset, Vector3i &r_local_size, AABB &r_bounds) const {
1439 int dirty_count = 0;
1440 for (uint32_t i = 0; i < cascades.size(); i++) {
1441 const SDFGI::Cascade &c = cascades[i];
1442
1443 if (c.dirty_regions == SDFGI::Cascade::DIRTY_ALL) {
1444 if (dirty_count == p_region) {
1445 r_local_offset = Vector3i();
1446 r_local_size = Vector3i(1, 1, 1) * cascade_size;
1447
1448 r_bounds.position = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + c.position)) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
1449 r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
1450 return i;
1451 }
1452 dirty_count++;
1453 } else {
1454 for (int j = 0; j < 3; j++) {
1455 if (c.dirty_regions[j] != 0) {
1456 if (dirty_count == p_region) {
1457 Vector3i from = Vector3i(0, 0, 0);
1458 Vector3i to = Vector3i(1, 1, 1) * cascade_size;
1459
1460 if (c.dirty_regions[j] > 0) {
1461 //fill from the beginning
1462 to[j] = c.dirty_regions[j];
1463 } else {
1464 //fill from the end
1465 from[j] = to[j] + c.dirty_regions[j];
1466 }
1467
1468 for (int k = 0; k < j; k++) {
1469 // "chip" away previous regions to avoid re-voxelizing the same thing
1470 if (c.dirty_regions[k] > 0) {
1471 from[k] += c.dirty_regions[k];
1472 } else if (c.dirty_regions[k] < 0) {
1473 to[k] += c.dirty_regions[k];
1474 }
1475 }
1476
1477 r_local_offset = from;
1478 r_local_size = to - from;
1479
1480 r_bounds.position = Vector3(from + Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + c.position) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
1481 r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
1482
1483 return i;
1484 }
1485
1486 dirty_count++;
1487 }
1488 }
1489 }
1490 }
1491 return -1;
1492 }
1493
1494 void GI::SDFGI::update_cascades() {
1495 //update cascades
1496 SDFGI::Cascade::UBO cascade_data[SDFGI::MAX_CASCADES];
1497 int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
1498
1499 for (uint32_t i = 0; i < cascades.size(); i++) {
1500 Vector3 pos = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascades[i].position)) * cascades[i].cell_size;
1501
1502 cascade_data[i].offset[0] = pos.x;
1503 cascade_data[i].offset[1] = pos.y;
1504 cascade_data[i].offset[2] = pos.z;
1505 cascade_data[i].to_cell = 1.0 / cascades[i].cell_size;
1506 cascade_data[i].probe_offset[0] = cascades[i].position.x / probe_divisor;
1507 cascade_data[i].probe_offset[1] = cascades[i].position.y / probe_divisor;
1508 cascade_data[i].probe_offset[2] = cascades[i].position.z / probe_divisor;
1509 cascade_data[i].pad = 0;
1510 }
1511
1512 RD::get_singleton()->buffer_update(cascades_ubo, 0, sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES, cascade_data);
1513 }
1514
1515 void GI::SDFGI::debug_draw(uint32_t p_view_count, const Projection *p_projections, const Transform3D &p_transform, int p_width, int p_height, RID p_render_target, RID p_texture, const Vector<RID> &p_texture_views) {
1516 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
1517 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
1518 RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
1519
1520 for (uint32_t v = 0; v < p_view_count; v++) {
1521 if (!debug_uniform_set[v].is_valid() || !RD::get_singleton()->uniform_set_is_valid(debug_uniform_set[v])) {
1522 Vector<RD::Uniform> uniforms;
1523 {
1524 RD::Uniform u;
1525 u.binding = 1;
1526 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1527 for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
1528 if (i < cascades.size()) {
1529 u.append_id(cascades[i].sdf_tex);
1530 } else {
1531 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1532 }
1533 }
1534 uniforms.push_back(u);
1535 }
1536 {
1537 RD::Uniform u;
1538 u.binding = 2;
1539 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1540 for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
1541 if (i < cascades.size()) {
1542 u.append_id(cascades[i].light_tex);
1543 } else {
1544 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1545 }
1546 }
1547 uniforms.push_back(u);
1548 }
1549 {
1550 RD::Uniform u;
1551 u.binding = 3;
1552 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1553 for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
1554 if (i < cascades.size()) {
1555 u.append_id(cascades[i].light_aniso_0_tex);
1556 } else {
1557 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1558 }
1559 }
1560 uniforms.push_back(u);
1561 }
1562 {
1563 RD::Uniform u;
1564 u.binding = 4;
1565 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1566 for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
1567 if (i < cascades.size()) {
1568 u.append_id(cascades[i].light_aniso_1_tex);
1569 } else {
1570 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1571 }
1572 }
1573 uniforms.push_back(u);
1574 }
1575 {
1576 RD::Uniform u;
1577 u.binding = 5;
1578 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1579 u.append_id(occlusion_texture);
1580 uniforms.push_back(u);
1581 }
1582 {
1583 RD::Uniform u;
1584 u.binding = 8;
1585 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
1586 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
1587 uniforms.push_back(u);
1588 }
1589 {
1590 RD::Uniform u;
1591 u.binding = 9;
1592 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1593 u.append_id(cascades_ubo);
1594 uniforms.push_back(u);
1595 }
1596 {
1597 RD::Uniform u;
1598 u.binding = 10;
1599 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1600 u.append_id(p_texture_views[v]);
1601 uniforms.push_back(u);
1602 }
1603 {
1604 RD::Uniform u;
1605 u.binding = 11;
1606 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1607 u.append_id(lightprobe_texture);
1608 uniforms.push_back(u);
1609 }
1610 debug_uniform_set[v] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.debug_shader_version, 0);
1611 }
1612
1613 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1614 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.debug_pipeline.get_rid());
1615 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, debug_uniform_set[v], 0);
1616
1617 SDFGIShader::DebugPushConstant push_constant;
1618 push_constant.grid_size[0] = cascade_size;
1619 push_constant.grid_size[1] = cascade_size;
1620 push_constant.grid_size[2] = cascade_size;
1621 push_constant.max_cascades = cascades.size();
1622 push_constant.screen_size[0] = p_width;
1623 push_constant.screen_size[1] = p_height;
1624 push_constant.y_mult = y_mult;
1625
1626 push_constant.z_near = -p_projections[v].get_z_near();
1627
1628 for (int i = 0; i < 3; i++) {
1629 for (int j = 0; j < 3; j++) {
1630 push_constant.cam_basis[i][j] = p_transform.basis.rows[j][i];
1631 }
1632 }
1633 push_constant.cam_origin[0] = p_transform.origin[0];
1634 push_constant.cam_origin[1] = p_transform.origin[1];
1635 push_constant.cam_origin[2] = p_transform.origin[2];
1636
1637 // need to properly unproject for asymmetric projection matrices in stereo..
1638 Projection inv_projection = p_projections[v].inverse();
1639 for (int i = 0; i < 4; i++) {
1640 for (int j = 0; j < 3; j++) {
1641 push_constant.inv_projection[j][i] = inv_projection.columns[i][j];
1642 }
1643 }
1644
1645 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::DebugPushConstant));
1646
1647 RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_width, p_height, 1);
1648 RD::get_singleton()->compute_list_end();
1649 }
1650
1651 Size2i rtsize = texture_storage->render_target_get_size(p_render_target);
1652 copy_effects->copy_to_fb_rect(p_texture, texture_storage->render_target_get_rd_framebuffer(p_render_target), Rect2i(Point2i(), rtsize), true, false, false, false, RID(), p_view_count > 1);
1653 }
1654
1655 void GI::SDFGI::debug_probes(RID p_framebuffer, const uint32_t p_view_count, const Projection *p_camera_with_transforms) {
1656 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
1657
1658 // setup scene data
1659 {
1660 SDFGIShader::DebugProbesSceneData scene_data;
1661
1662 if (debug_probes_scene_data_ubo.is_null()) {
1663 debug_probes_scene_data_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGIShader::DebugProbesSceneData));
1664 }
1665
1666 for (uint32_t v = 0; v < p_view_count; v++) {
1667 RendererRD::MaterialStorage::store_camera(p_camera_with_transforms[v], scene_data.projection[v]);
1668 }
1669
1670 RD::get_singleton()->buffer_update(debug_probes_scene_data_ubo, 0, sizeof(SDFGIShader::DebugProbesSceneData), &scene_data);
1671 }
1672
1673 // setup push constant
1674 SDFGIShader::DebugProbesPushConstant push_constant;
1675
1676 //gen spheres from strips
1677 uint32_t band_points = 16;
1678 push_constant.band_power = 4;
1679 push_constant.sections_in_band = ((band_points / 2) - 1);
1680 push_constant.band_mask = band_points - 2;
1681 push_constant.section_arc = Math::TAU / float(push_constant.sections_in_band);
1682 push_constant.y_mult = y_mult;
1683
1684 uint32_t total_points = push_constant.sections_in_band * band_points;
1685 uint32_t total_probes = probe_axis_count * probe_axis_count * probe_axis_count;
1686
1687 push_constant.grid_size[0] = cascade_size;
1688 push_constant.grid_size[1] = cascade_size;
1689 push_constant.grid_size[2] = cascade_size;
1690 push_constant.cascade = 0;
1691
1692 push_constant.probe_axis_size = probe_axis_count;
1693
1694 if (!debug_probes_uniform_set.is_valid() || !RD::get_singleton()->uniform_set_is_valid(debug_probes_uniform_set)) {
1695 Vector<RD::Uniform> uniforms;
1696 {
1697 RD::Uniform u;
1698 u.binding = 1;
1699 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1700 u.append_id(cascades_ubo);
1701 uniforms.push_back(u);
1702 }
1703 {
1704 RD::Uniform u;
1705 u.binding = 2;
1706 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1707 u.append_id(lightprobe_texture);
1708 uniforms.push_back(u);
1709 }
1710 {
1711 RD::Uniform u;
1712 u.binding = 3;
1713 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
1714 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
1715 uniforms.push_back(u);
1716 }
1717 {
1718 RD::Uniform u;
1719 u.binding = 4;
1720 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1721 u.append_id(occlusion_texture);
1722 uniforms.push_back(u);
1723 }
1724 {
1725 RD::Uniform u;
1726 u.binding = 5;
1727 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1728 u.append_id(debug_probes_scene_data_ubo);
1729 uniforms.push_back(u);
1730 }
1731
1732 debug_probes_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.debug_probes.version_get_shader(gi->sdfgi_shader.debug_probes_shader, 0), 0);
1733 }
1734
1735 SDFGIShader::ProbeDebugMode mode = p_view_count > 1 ? SDFGIShader::PROBE_DEBUG_PROBES_MULTIVIEW : SDFGIShader::PROBE_DEBUG_PROBES;
1736
1737 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer);
1738 RD::get_singleton()->draw_command_begin_label("Debug SDFGI");
1739
1740 RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, gi->sdfgi_shader.debug_probes_pipeline[mode].get_render_pipeline(RD::INVALID_FORMAT_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
1741 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, debug_probes_uniform_set, 0);
1742 RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(SDFGIShader::DebugProbesPushConstant));
1743 RD::get_singleton()->draw_list_draw(draw_list, false, total_probes, total_points);
1744
1745 if (gi->sdfgi_debug_probe_dir != Vector3()) {
1746 uint32_t cascade = 0;
1747 Vector3 offset = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascades[cascade].position)) * cascades[cascade].cell_size * Vector3(1.0, 1.0 / y_mult, 1.0);
1748 Vector3 probe_size = cascades[cascade].cell_size * (cascade_size / SDFGI::PROBE_DIVISOR) * Vector3(1.0, 1.0 / y_mult, 1.0);
1749 Vector3 ray_from = gi->sdfgi_debug_probe_pos;
1750 Vector3 ray_to = gi->sdfgi_debug_probe_pos + gi->sdfgi_debug_probe_dir * cascades[cascade].cell_size * Math::sqrt(3.0) * cascade_size;
1751 float sphere_radius = 0.2;
1752 float closest_dist = 1e20;
1753 gi->sdfgi_debug_probe_enabled = false;
1754
1755 Vector3i probe_from = cascades[cascade].position / (cascade_size / SDFGI::PROBE_DIVISOR);
1756 for (int i = 0; i < (SDFGI::PROBE_DIVISOR + 1); i++) {
1757 for (int j = 0; j < (SDFGI::PROBE_DIVISOR + 1); j++) {
1758 for (int k = 0; k < (SDFGI::PROBE_DIVISOR + 1); k++) {
1759 Vector3 pos = offset + probe_size * Vector3(i, j, k);
1760 Vector3 res;
1761 if (Geometry3D::segment_intersects_sphere(ray_from, ray_to, pos, sphere_radius, &res)) {
1762 float d = ray_from.distance_to(res);
1763 if (d < closest_dist) {
1764 closest_dist = d;
1765 gi->sdfgi_debug_probe_enabled = true;
1766 gi->sdfgi_debug_probe_index = probe_from + Vector3i(i, j, k);
1767 }
1768 }
1769 }
1770 }
1771 }
1772
1773 gi->sdfgi_debug_probe_dir = Vector3();
1774 }
1775
1776 if (gi->sdfgi_debug_probe_enabled) {
1777 uint32_t cascade = 0;
1778 uint32_t probe_cells = (cascade_size / SDFGI::PROBE_DIVISOR);
1779 Vector3i probe_from = cascades[cascade].position / probe_cells;
1780 Vector3i ofs = gi->sdfgi_debug_probe_index - probe_from;
1781 if (ofs.x < 0 || ofs.y < 0 || ofs.z < 0) {
1782 return;
1783 }
1784 if (ofs.x > SDFGI::PROBE_DIVISOR || ofs.y > SDFGI::PROBE_DIVISOR || ofs.z > SDFGI::PROBE_DIVISOR) {
1785 return;
1786 }
1787
1788 uint32_t mult = (SDFGI::PROBE_DIVISOR + 1);
1789 uint32_t index = ofs.z * mult * mult + ofs.y * mult + ofs.x;
1790
1791 push_constant.probe_debug_index = index;
1792
1793 uint32_t cell_count = probe_cells * 2 * probe_cells * 2 * probe_cells * 2;
1794
1795 RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, gi->sdfgi_shader.debug_probes_pipeline[p_view_count > 1 ? SDFGIShader::PROBE_DEBUG_VISIBILITY_MULTIVIEW : SDFGIShader::PROBE_DEBUG_VISIBILITY].get_render_pipeline(RD::INVALID_FORMAT_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
1796 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, debug_probes_uniform_set, 0);
1797 RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(SDFGIShader::DebugProbesPushConstant));
1798 RD::get_singleton()->draw_list_draw(draw_list, false, cell_count, total_points);
1799 }
1800
1801 RD::get_singleton()->draw_command_end_label();
1802 RD::get_singleton()->draw_list_end();
1803 }
1804
1805 void GI::SDFGI::pre_process_gi(const Transform3D &p_transform, RenderDataRD *p_render_data) {
1806 if (p_render_data->sdfgi_update_data == nullptr) {
1807 return;
1808 }
1809
1810 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
1811 /* Update general SDFGI Buffer */
1812
1813 SDFGIData sdfgi_data;
1814
1815 sdfgi_data.grid_size[0] = cascade_size;
1816 sdfgi_data.grid_size[1] = cascade_size;
1817 sdfgi_data.grid_size[2] = cascade_size;
1818
1819 sdfgi_data.max_cascades = cascades.size();
1820 sdfgi_data.probe_axis_size = probe_axis_count;
1821 sdfgi_data.cascade_probe_size[0] = sdfgi_data.probe_axis_size - 1; //float version for performance
1822 sdfgi_data.cascade_probe_size[1] = sdfgi_data.probe_axis_size - 1;
1823 sdfgi_data.cascade_probe_size[2] = sdfgi_data.probe_axis_size - 1;
1824
1825 float csize = cascade_size;
1826 sdfgi_data.probe_to_uvw = 1.0 / float(sdfgi_data.cascade_probe_size[0]);
1827 sdfgi_data.use_occlusion = uses_occlusion;
1828 //sdfgi_data.energy = energy;
1829
1830 sdfgi_data.y_mult = y_mult;
1831
1832 float cascade_voxel_size = (csize / sdfgi_data.cascade_probe_size[0]);
1833 float occlusion_clamp = (cascade_voxel_size - 0.5) / cascade_voxel_size;
1834 sdfgi_data.occlusion_clamp[0] = occlusion_clamp;
1835 sdfgi_data.occlusion_clamp[1] = occlusion_clamp;
1836 sdfgi_data.occlusion_clamp[2] = occlusion_clamp;
1837 sdfgi_data.normal_bias = (normal_bias / csize) * sdfgi_data.cascade_probe_size[0];
1838
1839 //vec2 tex_pixel_size = 1.0 / vec2(ivec2( (OCT_SIZE+2) * params.probe_axis_size * params.probe_axis_size, (OCT_SIZE+2) * params.probe_axis_size ) );
1840 //vec3 probe_uv_offset = (ivec3(OCT_SIZE+2,OCT_SIZE+2,(OCT_SIZE+2) * params.probe_axis_size)) * tex_pixel_size.xyx;
1841
1842 uint32_t oct_size = SDFGI::LIGHTPROBE_OCT_SIZE;
1843
1844 sdfgi_data.lightprobe_tex_pixel_size[0] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size * sdfgi_data.probe_axis_size);
1845 sdfgi_data.lightprobe_tex_pixel_size[1] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size);
1846 sdfgi_data.lightprobe_tex_pixel_size[2] = 1.0;
1847
1848 sdfgi_data.energy = energy;
1849
1850 sdfgi_data.lightprobe_uv_offset[0] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[0];
1851 sdfgi_data.lightprobe_uv_offset[1] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[1];
1852 sdfgi_data.lightprobe_uv_offset[2] = float((oct_size + 2) * sdfgi_data.probe_axis_size) * sdfgi_data.lightprobe_tex_pixel_size[0];
1853
1854 sdfgi_data.occlusion_renormalize[0] = 0.5;
1855 sdfgi_data.occlusion_renormalize[1] = 1.0;
1856 sdfgi_data.occlusion_renormalize[2] = 1.0 / float(sdfgi_data.max_cascades);
1857
1858 int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
1859
1860 for (uint32_t i = 0; i < sdfgi_data.max_cascades; i++) {
1861 SDFGIData::ProbeCascadeData &c = sdfgi_data.cascades[i];
1862 Vector3 pos = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascades[i].position)) * cascades[i].cell_size;
1863 Vector3 cam_origin = p_transform.origin;
1864 cam_origin.y *= y_mult;
1865 pos -= cam_origin; //make pos local to camera, to reduce numerical error
1866 c.position[0] = pos.x;
1867 c.position[1] = pos.y;
1868 c.position[2] = pos.z;
1869 c.to_probe = 1.0 / (float(cascade_size) * cascades[i].cell_size / float(probe_axis_count - 1));
1870
1871 Vector3i probe_ofs = cascades[i].position / probe_divisor;
1872 c.probe_world_offset[0] = probe_ofs.x;
1873 c.probe_world_offset[1] = probe_ofs.y;
1874 c.probe_world_offset[2] = probe_ofs.z;
1875
1876 c.to_cell = 1.0 / cascades[i].cell_size;
1877 c.exposure_normalization = 1.0;
1878 if (p_render_data->camera_attributes.is_valid()) {
1879 float exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1880 c.exposure_normalization = exposure_normalization / cascades[i].baked_exposure_normalization;
1881 }
1882 }
1883
1884 RD::get_singleton()->buffer_update(gi->sdfgi_ubo, 0, sizeof(SDFGIData), &sdfgi_data);
1885
1886 /* Update dynamic lights in SDFGI cascades */
1887
1888 for (uint32_t i = 0; i < cascades.size(); i++) {
1889 SDFGI::Cascade &cascade = cascades[i];
1890
1891 SDFGIShader::Light lights[SDFGI::MAX_DYNAMIC_LIGHTS];
1892 uint32_t idx = 0;
1893 for (uint32_t j = 0; j < (uint32_t)p_render_data->sdfgi_update_data->directional_lights->size(); j++) {
1894 if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
1895 break;
1896 }
1897
1898 RID light_instance = p_render_data->sdfgi_update_data->directional_lights->get(j);
1899 ERR_CONTINUE(!light_storage->owns_light_instance(light_instance));
1900
1901 RID light = light_storage->light_instance_get_base_light(light_instance);
1902 Transform3D light_transform = light_storage->light_instance_get_base_transform(light_instance);
1903
1904 if (RSG::light_storage->light_directional_get_sky_mode(light) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
1905 continue;
1906 }
1907
1908 Vector3 dir = -light_transform.basis.get_column(Vector3::AXIS_Z);
1909 dir.y *= y_mult;
1910 dir.normalize();
1911 lights[idx].direction[0] = dir.x;
1912 lights[idx].direction[1] = dir.y;
1913 lights[idx].direction[2] = dir.z;
1914 Color color = RSG::light_storage->light_get_color(light);
1915 color = color.srgb_to_linear();
1916 lights[idx].color[0] = color.r;
1917 lights[idx].color[1] = color.g;
1918 lights[idx].color[2] = color.b;
1919 lights[idx].type = RS::LIGHT_DIRECTIONAL;
1920 lights[idx].energy = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ENERGY) * RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
1921 if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
1922 lights[idx].energy *= RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INTENSITY);
1923 }
1924
1925 if (p_render_data->camera_attributes.is_valid()) {
1926 lights[idx].energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1927 }
1928
1929 lights[idx].has_shadow = RSG::light_storage->light_has_shadow(light);
1930
1931 idx++;
1932 }
1933
1934 AABB cascade_aabb;
1935 cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascade.position)) * cascade.cell_size;
1936 cascade_aabb.size = Vector3(1, 1, 1) * cascade_size * cascade.cell_size;
1937
1938 for (uint32_t j = 0; j < p_render_data->sdfgi_update_data->positional_light_count; j++) {
1939 if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
1940 break;
1941 }
1942
1943 RID light_instance = p_render_data->sdfgi_update_data->positional_light_instances[j];
1944 ERR_CONTINUE(!light_storage->owns_light_instance(light_instance));
1945
1946 RID light = light_storage->light_instance_get_base_light(light_instance);
1947 AABB light_aabb = light_storage->light_instance_get_base_aabb(light_instance);
1948 Transform3D light_transform = light_storage->light_instance_get_base_transform(light_instance);
1949
1950 uint32_t max_sdfgi_cascade = RSG::light_storage->light_get_max_sdfgi_cascade(light);
1951 if (i > max_sdfgi_cascade) {
1952 continue;
1953 }
1954
1955 if (!cascade_aabb.intersects(light_aabb)) {
1956 continue;
1957 }
1958
1959 Vector3 dir = -light_transform.basis.get_column(Vector3::AXIS_Z);
1960 //faster to not do this here
1961 //dir.y *= y_mult;
1962 //dir.normalize();
1963 lights[idx].direction[0] = dir.x;
1964 lights[idx].direction[1] = dir.y;
1965 lights[idx].direction[2] = dir.z;
1966 Vector3 pos = light_transform.origin;
1967 pos.y *= y_mult;
1968 lights[idx].position[0] = pos.x;
1969 lights[idx].position[1] = pos.y;
1970 lights[idx].position[2] = pos.z;
1971 Color color = RSG::light_storage->light_get_color(light);
1972 color = color.srgb_to_linear();
1973 lights[idx].color[0] = color.r;
1974 lights[idx].color[1] = color.g;
1975 lights[idx].color[2] = color.b;
1976 lights[idx].type = RSG::light_storage->light_get_type(light);
1977
1978 lights[idx].energy = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ENERGY) * RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
1979 if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
1980 lights[idx].energy *= RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INTENSITY);
1981
1982 // Convert from Luminous Power to Luminous Intensity
1983 if (lights[idx].type == RS::LIGHT_OMNI) {
1984 lights[idx].energy *= 1.0 / (Math::PI * 4.0);
1985 } else if (lights[idx].type == RS::LIGHT_SPOT) {
1986 // Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
1987 // We make this assumption to keep them easy to control.
1988 lights[idx].energy *= 1.0 / Math::PI;
1989 }
1990 }
1991
1992 if (p_render_data->camera_attributes.is_valid()) {
1993 lights[idx].energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1994 }
1995
1996 lights[idx].has_shadow = RSG::light_storage->light_has_shadow(light);
1997 lights[idx].attenuation = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
1998 lights[idx].radius = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_RANGE);
1999 lights[idx].cos_spot_angle = Math::cos(Math::deg_to_rad(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE)));
2000 lights[idx].inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
2001
2002 idx++;
2003 }
2004
2005 if (idx > 0) {
2006 RD::get_singleton()->buffer_update(cascade.lights_buffer, 0, idx * sizeof(SDFGIShader::Light), lights);
2007 }
2008
2009 cascade_dynamic_light_count[i] = idx;
2010 }
2011 }
2012
2013 void GI::SDFGI::render_region(Ref<RenderSceneBuffersRD> p_render_buffers, int p_region, const PagedArray<RenderGeometryInstance *> &p_instances, float p_exposure_normalization) {
2014 //print_line("rendering region " + itos(p_region));
2015 ERR_FAIL_COND(p_render_buffers.is_null()); // we wouldn't be here if this failed but...
2016 AABB bounds;
2017 Vector3i from;
2018 Vector3i size;
2019
2020 int cascade_prev = get_pending_region_data(p_region - 1, from, size, bounds);
2021 int cascade_next = get_pending_region_data(p_region + 1, from, size, bounds);
2022 int cascade = get_pending_region_data(p_region, from, size, bounds);
2023 ERR_FAIL_COND(cascade < 0);
2024
2025 if (cascade_prev != cascade) {
2026 //initialize render
2027 RD::get_singleton()->texture_clear(render_albedo, Color(0, 0, 0, 0), 0, 1, 0, 1);
2028 RD::get_singleton()->texture_clear(render_emission, Color(0, 0, 0, 0), 0, 1, 0, 1);
2029 RD::get_singleton()->texture_clear(render_emission_aniso, Color(0, 0, 0, 0), 0, 1, 0, 1);
2030 RD::get_singleton()->texture_clear(render_geom_facing, Color(0, 0, 0, 0), 0, 1, 0, 1);
2031 }
2032
2033 //print_line("rendering cascade " + itos(p_region) + " objects: " + itos(p_cull_count) + " bounds: " + bounds + " from: " + from + " size: " + size + " cell size: " + rtos(cascades[cascade].cell_size));
2034 RendererSceneRenderRD::get_singleton()->_render_sdfgi(p_render_buffers, from, size, bounds, p_instances, render_albedo, render_emission, render_emission_aniso, render_geom_facing, p_exposure_normalization);
2035
2036 if (cascade_next != cascade) {
2037 RD::get_singleton()->draw_command_begin_label("SDFGI Pre-Process Cascade");
2038
2039 RENDER_TIMESTAMP("> SDFGI Update SDF");
2040 //done rendering! must update SDF
2041 //clear dispatch indirect data
2042
2043 SDFGIShader::PreprocessPushConstant push_constant;
2044 memset(&push_constant, 0, sizeof(SDFGIShader::PreprocessPushConstant));
2045
2046 RENDER_TIMESTAMP("SDFGI Scroll SDF");
2047
2048 //scroll
2049 if (cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
2050 //for scroll
2051 Vector3i dirty = cascades[cascade].dirty_regions;
2052 push_constant.scroll[0] = dirty.x;
2053 push_constant.scroll[1] = dirty.y;
2054 push_constant.scroll[2] = dirty.z;
2055 } else {
2056 //for no scroll
2057 push_constant.scroll[0] = 0;
2058 push_constant.scroll[1] = 0;
2059 push_constant.scroll[2] = 0;
2060 }
2061
2062 cascades[cascade].all_dynamic_lights_dirty = true;
2063 cascades[cascade].baked_exposure_normalization = p_exposure_normalization;
2064
2065 push_constant.grid_size = cascade_size;
2066 push_constant.cascade = cascade;
2067
2068 if (cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
2069 RD::get_singleton()->buffer_copy(cascades[cascade].solid_cell_dispatch_buffer_storage, cascades[cascade].solid_cell_dispatch_buffer_call, 0, 0, sizeof(uint32_t) * 4);
2070
2071 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
2072
2073 //must pre scroll existing data because not all is dirty
2074 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_SCROLL].get_rid());
2075 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].scroll_uniform_set, 0);
2076
2077 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2078 RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cascades[cascade].solid_cell_dispatch_buffer_call, 0);
2079 // no barrier do all together
2080
2081 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_SCROLL_OCCLUSION].get_rid());
2082 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].scroll_occlusion_uniform_set, 0);
2083
2084 Vector3i dirty = cascades[cascade].dirty_regions;
2085 Vector3i groups;
2086 groups.x = cascade_size - Math::abs(dirty.x);
2087 groups.y = cascade_size - Math::abs(dirty.y);
2088 groups.z = cascade_size - Math::abs(dirty.z);
2089
2090 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2091 RD::get_singleton()->compute_list_dispatch_threads(compute_list, groups.x, groups.y, groups.z);
2092
2093 //no barrier, continue together
2094
2095 {
2096 //scroll probes and their history also
2097
2098 SDFGIShader::IntegratePushConstant ipush_constant;
2099 ipush_constant.grid_size[1] = cascade_size;
2100 ipush_constant.grid_size[2] = cascade_size;
2101 ipush_constant.grid_size[0] = cascade_size;
2102 ipush_constant.max_cascades = cascades.size();
2103 ipush_constant.probe_axis_size = probe_axis_count;
2104 ipush_constant.history_index = 0;
2105 ipush_constant.history_size = history_size;
2106 ipush_constant.ray_count = 0;
2107 ipush_constant.ray_bias = 0;
2108 ipush_constant.sky_flags = 0;
2109 ipush_constant.sky_energy = 0;
2110 ipush_constant.sky_color_or_orientation[0] = 0;
2111 ipush_constant.sky_color_or_orientation[1] = 0;
2112 ipush_constant.sky_color_or_orientation[2] = 0;
2113 ipush_constant.y_mult = y_mult;
2114 ipush_constant.store_ambient_texture = false;
2115
2116 ipush_constant.image_size[0] = probe_axis_count * probe_axis_count;
2117 ipush_constant.image_size[1] = probe_axis_count;
2118
2119 int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
2120 ipush_constant.cascade = cascade;
2121 ipush_constant.world_offset[0] = cascades[cascade].position.x / probe_divisor;
2122 ipush_constant.world_offset[1] = cascades[cascade].position.y / probe_divisor;
2123 ipush_constant.world_offset[2] = cascades[cascade].position.z / probe_divisor;
2124
2125 ipush_constant.scroll[0] = dirty.x / probe_divisor;
2126 ipush_constant.scroll[1] = dirty.y / probe_divisor;
2127 ipush_constant.scroll[2] = dirty.z / probe_divisor;
2128
2129 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_SCROLL].get_rid());
2130 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
2131 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
2132 RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
2133 RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
2134
2135 RD::get_singleton()->compute_list_add_barrier(compute_list);
2136
2137 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_SCROLL_STORE].get_rid());
2138 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
2139 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
2140 RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
2141 RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
2142
2143 RD::get_singleton()->compute_list_add_barrier(compute_list);
2144
2145 if (bounce_feedback > 0.0) {
2146 //multibounce requires this to be stored so direct light can read from it
2147
2148 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_STORE].get_rid());
2149
2150 //convert to octahedral to store
2151 ipush_constant.image_size[0] *= SDFGI::LIGHTPROBE_OCT_SIZE;
2152 ipush_constant.image_size[1] *= SDFGI::LIGHTPROBE_OCT_SIZE;
2153
2154 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
2155 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
2156 RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
2157 RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, 1);
2158 }
2159 }
2160
2161 //ok finally barrier
2162 RD::get_singleton()->compute_list_end();
2163 }
2164
2165 //clear dispatch indirect data
2166 uint32_t dispatch_indirect_data[4] = { 0, 0, 0, 0 };
2167 RD::get_singleton()->buffer_update(cascades[cascade].solid_cell_dispatch_buffer_storage, 0, sizeof(uint32_t) * 4, dispatch_indirect_data);
2168
2169 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
2170
2171 bool half_size = true; //much faster, very little difference
2172 static const int optimized_jf_group_size = 8;
2173
2174 if (half_size) {
2175 push_constant.grid_size >>= 1;
2176
2177 uint32_t cascade_half_size = cascade_size >> 1;
2178 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF].get_rid());
2179 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_initialize_half_uniform_set, 0);
2180 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2181 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
2182 RD::get_singleton()->compute_list_add_barrier(compute_list);
2183
2184 //must start with regular jumpflood
2185
2186 push_constant.half_size = true;
2187 {
2188 RENDER_TIMESTAMP("SDFGI Jump Flood (Half-Size)");
2189
2190 uint32_t s = cascade_half_size;
2191
2192 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD].get_rid());
2193
2194 int jf_us = 0;
2195 //start with regular jump flood for very coarse reads, as this is impossible to optimize
2196 while (s > 1) {
2197 s /= 2;
2198 push_constant.step_size = s;
2199 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_half_uniform_set[jf_us], 0);
2200 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2201 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
2202 RD::get_singleton()->compute_list_add_barrier(compute_list);
2203 jf_us = jf_us == 0 ? 1 : 0;
2204
2205 if (cascade_half_size / (s / 2) >= optimized_jf_group_size) {
2206 break;
2207 }
2208 }
2209
2210 RENDER_TIMESTAMP("SDFGI Jump Flood Optimized (Half-Size)");
2211
2212 //continue with optimized jump flood for smaller reads
2213 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED].get_rid());
2214 while (s > 1) {
2215 s /= 2;
2216 push_constant.step_size = s;
2217 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_half_uniform_set[jf_us], 0);
2218 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2219 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
2220 RD::get_singleton()->compute_list_add_barrier(compute_list);
2221 jf_us = jf_us == 0 ? 1 : 0;
2222 }
2223 }
2224
2225 // restore grid size for last passes
2226 push_constant.grid_size = cascade_size;
2227
2228 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_UPSCALE].get_rid());
2229 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_upscale_uniform_set, 0);
2230 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2231 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2232 RD::get_singleton()->compute_list_add_barrier(compute_list);
2233
2234 //run one pass of fullsize jumpflood to fix up half size artifacts
2235
2236 push_constant.half_size = false;
2237 push_constant.step_size = 1;
2238 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED].get_rid());
2239 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[upscale_jfa_uniform_set_index], 0);
2240 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2241 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2242 RD::get_singleton()->compute_list_add_barrier(compute_list);
2243
2244 } else {
2245 //full size jumpflood
2246 RENDER_TIMESTAMP("SDFGI Jump Flood (Full-Size)");
2247
2248 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE].get_rid());
2249 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_initialize_uniform_set, 0);
2250 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2251 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2252
2253 RD::get_singleton()->compute_list_add_barrier(compute_list);
2254
2255 push_constant.half_size = false;
2256 {
2257 uint32_t s = cascade_size;
2258
2259 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD].get_rid());
2260
2261 int jf_us = 0;
2262 //start with regular jump flood for very coarse reads, as this is impossible to optimize
2263 while (s > 1) {
2264 s /= 2;
2265 push_constant.step_size = s;
2266 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[jf_us], 0);
2267 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2268 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2269 RD::get_singleton()->compute_list_add_barrier(compute_list);
2270 jf_us = jf_us == 0 ? 1 : 0;
2271
2272 if (cascade_size / (s / 2) >= optimized_jf_group_size) {
2273 break;
2274 }
2275 }
2276
2277 RENDER_TIMESTAMP("SDFGI Jump Flood Optimized (Full-Size)");
2278
2279 //continue with optimized jump flood for smaller reads
2280 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED].get_rid());
2281 while (s > 1) {
2282 s /= 2;
2283 push_constant.step_size = s;
2284 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[jf_us], 0);
2285 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2286 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2287 RD::get_singleton()->compute_list_add_barrier(compute_list);
2288 jf_us = jf_us == 0 ? 1 : 0;
2289 }
2290 }
2291 }
2292
2293 RENDER_TIMESTAMP("SDFGI Occlusion");
2294
2295 // occlusion
2296 {
2297 uint32_t probe_size = cascade_size / SDFGI::PROBE_DIVISOR;
2298 Vector3i probe_global_pos = cascades[cascade].position / probe_size;
2299
2300 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_OCCLUSION].get_rid());
2301 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, occlusion_uniform_set, 0);
2302 for (int i = 0; i < 8; i++) {
2303 //dispatch all at once for performance
2304 Vector3i offset(i & 1, (i >> 1) & 1, (i >> 2) & 1);
2305
2306 if ((probe_global_pos.x & 1) != 0) {
2307 offset.x = (offset.x + 1) & 1;
2308 }
2309 if ((probe_global_pos.y & 1) != 0) {
2310 offset.y = (offset.y + 1) & 1;
2311 }
2312 if ((probe_global_pos.z & 1) != 0) {
2313 offset.z = (offset.z + 1) & 1;
2314 }
2315 push_constant.probe_offset[0] = offset.x;
2316 push_constant.probe_offset[1] = offset.y;
2317 push_constant.probe_offset[2] = offset.z;
2318 push_constant.occlusion_index = i;
2319 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2320
2321 Vector3i groups = Vector3i(probe_size + 1, probe_size + 1, probe_size + 1) - offset; //if offset, it's one less probe per axis to compute
2322 RD::get_singleton()->compute_list_dispatch(compute_list, groups.x, groups.y, groups.z);
2323 }
2324 RD::get_singleton()->compute_list_add_barrier(compute_list);
2325 }
2326
2327 RENDER_TIMESTAMP("SDFGI Store");
2328
2329 // store
2330 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_STORE].get_rid());
2331 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].sdf_store_uniform_set, 0);
2332 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2333 RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2334
2335 RD::get_singleton()->compute_list_end();
2336
2337 //clear these textures, as they will have previous garbage on next draw
2338 RD::get_singleton()->texture_clear(cascades[cascade].light_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
2339 RD::get_singleton()->texture_clear(cascades[cascade].light_aniso_0_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
2340 RD::get_singleton()->texture_clear(cascades[cascade].light_aniso_1_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
2341
2342 #if 0
2343 Vector<uint8_t> data = RD::get_singleton()->texture_get_data(cascades[cascade].sdf, 0);
2344 Ref<Image> img;
2345 img.instantiate();
2346 for (uint32_t i = 0; i < cascade_size; i++) {
2347 Vector<uint8_t> subarr = data.slice(128 * 128 * i, 128 * 128 * (i + 1));
2348 img->set_data(cascade_size, cascade_size, false, Image::FORMAT_L8, subarr);
2349 img->save_png("res://cascade_sdf_" + itos(cascade) + "_" + itos(i) + ".png");
2350 }
2351
2352 //finalize render and update sdf
2353 #endif
2354
2355 #if 0
2356 Vector<uint8_t> data = RD::get_singleton()->texture_get_data(render_albedo, 0);
2357 Ref<Image> img;
2358 img.instantiate();
2359 for (uint32_t i = 0; i < cascade_size; i++) {
2360 Vector<uint8_t> subarr = data.slice(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2);
2361 img->createcascade_size, cascade_size, false, Image::FORMAT_RGB565, subarr);
2362 img->convert(Image::FORMAT_RGBA8);
2363 img->save_png("res://cascade_" + itos(cascade) + "_" + itos(i) + ".png");
2364 }
2365
2366 //finalize render and update sdf
2367 #endif
2368
2369 RENDER_TIMESTAMP("< SDFGI Update SDF");
2370 RD::get_singleton()->draw_command_end_label();
2371 }
2372 }
2373
2374 void GI::SDFGI::render_static_lights(RenderDataRD *p_render_data, Ref<RenderSceneBuffersRD> p_render_buffers, uint32_t p_cascade_count, const uint32_t *p_cascade_indices, const PagedArray<RID> *p_positional_light_cull_result) {
2375 ERR_FAIL_COND(p_render_buffers.is_null()); // we wouldn't be here if this failed but...
2376
2377 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
2378
2379 RD::get_singleton()->draw_command_begin_label("SDFGI Render Static Lights");
2380
2381 update_cascades();
2382
2383 SDFGIShader::Light lights[SDFGI::MAX_STATIC_LIGHTS];
2384 uint32_t light_count[SDFGI::MAX_STATIC_LIGHTS];
2385
2386 for (uint32_t i = 0; i < p_cascade_count; i++) {
2387 ERR_CONTINUE(p_cascade_indices[i] >= cascades.size());
2388
2389 SDFGI::Cascade &cc = cascades[p_cascade_indices[i]];
2390
2391 { //fill light buffer
2392
2393 AABB cascade_aabb;
2394 cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cc.position)) * cc.cell_size;
2395 cascade_aabb.size = Vector3(1, 1, 1) * cascade_size * cc.cell_size;
2396
2397 int idx = 0;
2398
2399 for (uint32_t j = 0; j < (uint32_t)p_positional_light_cull_result[i].size(); j++) {
2400 if (idx == SDFGI::MAX_STATIC_LIGHTS) {
2401 break;
2402 }
2403
2404 RID light_instance = p_positional_light_cull_result[i][j];
2405 ERR_CONTINUE(!light_storage->owns_light_instance(light_instance));
2406
2407 RID light = light_storage->light_instance_get_base_light(light_instance);
2408 AABB light_aabb = light_storage->light_instance_get_base_aabb(light_instance);
2409 Transform3D light_transform = light_storage->light_instance_get_base_transform(light_instance);
2410
2411 uint32_t max_sdfgi_cascade = RSG::light_storage->light_get_max_sdfgi_cascade(light);
2412 if (p_cascade_indices[i] > max_sdfgi_cascade) {
2413 continue;
2414 }
2415
2416 if (!cascade_aabb.intersects(light_aabb)) {
2417 continue;
2418 }
2419
2420 lights[idx].type = RSG::light_storage->light_get_type(light);
2421
2422 Vector3 dir = -light_transform.basis.get_column(Vector3::AXIS_Z);
2423 if (lights[idx].type == RS::LIGHT_DIRECTIONAL) {
2424 dir.y *= y_mult; //only makes sense for directional
2425 dir.normalize();
2426 }
2427 lights[idx].direction[0] = dir.x;
2428 lights[idx].direction[1] = dir.y;
2429 lights[idx].direction[2] = dir.z;
2430 Vector3 pos = light_transform.origin;
2431 pos.y *= y_mult;
2432 lights[idx].position[0] = pos.x;
2433 lights[idx].position[1] = pos.y;
2434 lights[idx].position[2] = pos.z;
2435 Color color = RSG::light_storage->light_get_color(light);
2436 color = color.srgb_to_linear();
2437 lights[idx].color[0] = color.r;
2438 lights[idx].color[1] = color.g;
2439 lights[idx].color[2] = color.b;
2440
2441 lights[idx].energy = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ENERGY) * RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
2442 if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
2443 lights[idx].energy *= RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INTENSITY);
2444
2445 // Convert from Luminous Power to Luminous Intensity
2446 if (lights[idx].type == RS::LIGHT_OMNI) {
2447 lights[idx].energy *= 1.0 / (Math::PI * 4.0);
2448 } else if (lights[idx].type == RS::LIGHT_SPOT) {
2449 // Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
2450 // We make this assumption to keep them easy to control.
2451 lights[idx].energy *= 1.0 / Math::PI;
2452 }
2453 }
2454
2455 if (p_render_data->camera_attributes.is_valid()) {
2456 lights[idx].energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
2457 }
2458
2459 lights[idx].has_shadow = RSG::light_storage->light_has_shadow(light);
2460 lights[idx].attenuation = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
2461 lights[idx].radius = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_RANGE);
2462 lights[idx].cos_spot_angle = Math::cos(Math::deg_to_rad(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE)));
2463 lights[idx].inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
2464
2465 idx++;
2466 }
2467
2468 if (idx > 0) {
2469 RD::get_singleton()->buffer_update(cc.lights_buffer, 0, idx * sizeof(SDFGIShader::Light), lights);
2470 }
2471
2472 light_count[i] = idx;
2473 }
2474 }
2475
2476 for (uint32_t i = 0; i < p_cascade_count; i++) {
2477 ERR_CONTINUE(p_cascade_indices[i] >= cascades.size());
2478
2479 SDFGI::Cascade &cc = cascades[p_cascade_indices[i]];
2480 if (light_count[i] > 0) {
2481 RD::get_singleton()->buffer_copy(cc.solid_cell_dispatch_buffer_storage, cc.solid_cell_dispatch_buffer_call, 0, 0, sizeof(uint32_t) * 4);
2482 }
2483 }
2484
2485 /* Static Lights */
2486 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
2487
2488 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.direct_light_pipeline[SDFGIShader::DIRECT_LIGHT_MODE_STATIC].get_rid());
2489
2490 SDFGIShader::DirectLightPushConstant dl_push_constant;
2491
2492 dl_push_constant.grid_size[0] = cascade_size;
2493 dl_push_constant.grid_size[1] = cascade_size;
2494 dl_push_constant.grid_size[2] = cascade_size;
2495 dl_push_constant.max_cascades = cascades.size();
2496 dl_push_constant.probe_axis_size = probe_axis_count;
2497 dl_push_constant.bounce_feedback = 0.0; // this is static light, do not multibounce yet
2498 dl_push_constant.y_mult = y_mult;
2499 dl_push_constant.use_occlusion = uses_occlusion;
2500
2501 //all must be processed
2502 dl_push_constant.process_offset = 0;
2503 dl_push_constant.process_increment = 1;
2504
2505 for (uint32_t i = 0; i < p_cascade_count; i++) {
2506 ERR_CONTINUE(p_cascade_indices[i] >= cascades.size());
2507
2508 SDFGI::Cascade &cc = cascades[p_cascade_indices[i]];
2509
2510 dl_push_constant.light_count = light_count[i];
2511 dl_push_constant.cascade = p_cascade_indices[i];
2512
2513 if (dl_push_constant.light_count > 0) {
2514 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cc.sdf_direct_light_static_uniform_set, 0);
2515 RD::get_singleton()->compute_list_set_push_constant(compute_list, &dl_push_constant, sizeof(SDFGIShader::DirectLightPushConstant));
2516 RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cc.solid_cell_dispatch_buffer_call, 0);
2517 }
2518 }
2519
2520 RD::get_singleton()->compute_list_end();
2521
2522 RD::get_singleton()->draw_command_end_label();
2523 }
2524
2525 ////////////////////////////////////////////////////////////////////////////////
2526 // VoxelGIInstance
2527
2528 void GI::VoxelGIInstance::update(bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects) {
2529 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
2530 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
2531
2532 uint32_t data_version = gi->voxel_gi_get_data_version(probe);
2533
2534 // (RE)CREATE IF NEEDED
2535
2536 if (last_probe_data_version != data_version) {
2537 //need to re-create everything
2538 free_resources();
2539
2540 Vector3i octree_size = gi->voxel_gi_get_octree_size(probe);
2541
2542 if (octree_size != Vector3i()) {
2543 //can create a 3D texture
2544 Vector<int> levels = gi->voxel_gi_get_level_counts(probe);
2545
2546 RD::TextureFormat tf;
2547 tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
2548 tf.width = octree_size.x;
2549 tf.height = octree_size.y;
2550 tf.depth = octree_size.z;
2551 tf.texture_type = RD::TEXTURE_TYPE_3D;
2552 tf.mipmaps = levels.size();
2553
2554 tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
2555
2556 texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
2557 RD::get_singleton()->set_resource_name(texture, "VoxelGI Instance Texture");
2558
2559 RD::get_singleton()->texture_clear(texture, Color(0, 0, 0, 0), 0, levels.size(), 0, 1);
2560
2561 {
2562 int total_elements = 0;
2563 for (int i = 0; i < levels.size(); i++) {
2564 total_elements += levels[i];
2565 }
2566
2567 write_buffer = RD::get_singleton()->storage_buffer_create(total_elements * 16);
2568 }
2569
2570 for (int i = 0; i < levels.size(); i++) {
2571 VoxelGIInstance::Mipmap mipmap;
2572 mipmap.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), texture, 0, i, 1, RD::TEXTURE_SLICE_3D);
2573 mipmap.level = levels.size() - i - 1;
2574 mipmap.cell_offset = 0;
2575 for (uint32_t j = 0; j < mipmap.level; j++) {
2576 mipmap.cell_offset += levels[j];
2577 }
2578 mipmap.cell_count = levels[mipmap.level];
2579
2580 Vector<RD::Uniform> uniforms;
2581 {
2582 RD::Uniform u;
2583 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2584 u.binding = 1;
2585 u.append_id(gi->voxel_gi_get_octree_buffer(probe));
2586 uniforms.push_back(u);
2587 }
2588 {
2589 RD::Uniform u;
2590 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2591 u.binding = 2;
2592 u.append_id(gi->voxel_gi_get_data_buffer(probe));
2593 uniforms.push_back(u);
2594 }
2595
2596 {
2597 RD::Uniform u;
2598 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2599 u.binding = 4;
2600 u.append_id(write_buffer);
2601 uniforms.push_back(u);
2602 }
2603 {
2604 RD::Uniform u;
2605 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2606 u.binding = 9;
2607 u.append_id(gi->voxel_gi_get_sdf_texture(probe));
2608 uniforms.push_back(u);
2609 }
2610 {
2611 RD::Uniform u;
2612 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2613 u.binding = 10;
2614 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
2615 uniforms.push_back(u);
2616 }
2617
2618 {
2619 Vector<RD::Uniform> copy_uniforms = uniforms;
2620 if (i == 0) {
2621 {
2622 RD::Uniform u;
2623 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
2624 u.binding = 3;
2625 u.append_id(gi->voxel_gi_lights_uniform);
2626 copy_uniforms.push_back(u);
2627 }
2628
2629 mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_COMPUTE_LIGHT], 0);
2630
2631 copy_uniforms = uniforms; //restore
2632
2633 {
2634 RD::Uniform u;
2635 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2636 u.binding = 5;
2637 u.append_id(texture);
2638 copy_uniforms.push_back(u);
2639 }
2640 mipmap.second_bounce_uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_COMPUTE_SECOND_BOUNCE], 0);
2641 } else {
2642 mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_COMPUTE_MIPMAP], 0);
2643 }
2644 }
2645
2646 {
2647 RD::Uniform u;
2648 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2649 u.binding = 5;
2650 u.append_id(mipmap.texture);
2651 uniforms.push_back(u);
2652 }
2653
2654 mipmap.write_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_WRITE_TEXTURE], 0);
2655
2656 mipmaps.push_back(mipmap);
2657 }
2658
2659 {
2660 uint32_t dynamic_map_size = MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
2661 uint32_t oversample = nearest_power_of_2_templated(4);
2662 int mipmap_index = 0;
2663
2664 while (mipmap_index < mipmaps.size()) {
2665 VoxelGIInstance::DynamicMap dmap;
2666
2667 if (oversample > 0) {
2668 dmap.size = dynamic_map_size * (1 << oversample);
2669 dmap.mipmap = -1;
2670 oversample--;
2671 } else {
2672 dmap.size = dynamic_map_size >> mipmap_index;
2673 dmap.mipmap = mipmap_index;
2674 mipmap_index++;
2675 }
2676
2677 RD::TextureFormat dtf;
2678 dtf.width = dmap.size;
2679 dtf.height = dmap.size;
2680 dtf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
2681 dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
2682
2683 if (dynamic_maps.is_empty()) {
2684 dtf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
2685 }
2686 dmap.texture = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2687 RD::get_singleton()->set_resource_name(dmap.texture, "VoxelGI Instance DMap Texture");
2688
2689 if (dynamic_maps.is_empty()) {
2690 // Render depth for first one.
2691 // Use 16-bit depth when supported to improve performance.
2692 dtf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D16_UNORM, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D16_UNORM : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
2693 dtf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
2694 dmap.fb_depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2695 RD::get_singleton()->set_resource_name(dmap.fb_depth, "VoxelGI Instance DMap FB Depth");
2696 }
2697
2698 //just use depth as-is
2699 dtf.format = RD::DATA_FORMAT_R32_SFLOAT;
2700 dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
2701
2702 dmap.depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2703 RD::get_singleton()->set_resource_name(dmap.depth, "VoxelGI Instance DMap Depth");
2704
2705 if (dynamic_maps.is_empty()) {
2706 dtf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
2707 dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
2708 dmap.albedo = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2709 RD::get_singleton()->set_resource_name(dmap.albedo, "VoxelGI Instance DMap Albedo");
2710 dmap.normal = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2711 RD::get_singleton()->set_resource_name(dmap.normal, "VoxelGI Instance DMap Normal");
2712 dmap.orm = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2713 RD::get_singleton()->set_resource_name(dmap.orm, "VoxelGI Instance DMap ORM");
2714
2715 Vector<RID> fb;
2716 fb.push_back(dmap.albedo);
2717 fb.push_back(dmap.normal);
2718 fb.push_back(dmap.orm);
2719 fb.push_back(dmap.texture); //emission
2720 fb.push_back(dmap.depth);
2721 fb.push_back(dmap.fb_depth);
2722
2723 dmap.fb = RD::get_singleton()->framebuffer_create(fb);
2724
2725 {
2726 Vector<RD::Uniform> uniforms;
2727 {
2728 RD::Uniform u;
2729 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
2730 u.binding = 3;
2731 u.append_id(gi->voxel_gi_lights_uniform);
2732 uniforms.push_back(u);
2733 }
2734
2735 {
2736 RD::Uniform u;
2737 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2738 u.binding = 5;
2739 u.append_id(dmap.albedo);
2740 uniforms.push_back(u);
2741 }
2742 {
2743 RD::Uniform u;
2744 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2745 u.binding = 6;
2746 u.append_id(dmap.normal);
2747 uniforms.push_back(u);
2748 }
2749 {
2750 RD::Uniform u;
2751 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2752 u.binding = 7;
2753 u.append_id(dmap.orm);
2754 uniforms.push_back(u);
2755 }
2756 {
2757 RD::Uniform u;
2758 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2759 u.binding = 8;
2760 u.append_id(dmap.fb_depth);
2761 uniforms.push_back(u);
2762 }
2763 {
2764 RD::Uniform u;
2765 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2766 u.binding = 9;
2767 u.append_id(gi->voxel_gi_get_sdf_texture(probe));
2768 uniforms.push_back(u);
2769 }
2770 {
2771 RD::Uniform u;
2772 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2773 u.binding = 10;
2774 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
2775 uniforms.push_back(u);
2776 }
2777 {
2778 RD::Uniform u;
2779 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2780 u.binding = 11;
2781 u.append_id(dmap.texture);
2782 uniforms.push_back(u);
2783 }
2784 {
2785 RD::Uniform u;
2786 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2787 u.binding = 12;
2788 u.append_id(dmap.depth);
2789 uniforms.push_back(u);
2790 }
2791
2792 dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING], 0);
2793 }
2794 } else {
2795 bool plot = dmap.mipmap >= 0;
2796 bool write = dmap.mipmap < (mipmaps.size() - 1);
2797
2798 Vector<RD::Uniform> uniforms;
2799
2800 {
2801 RD::Uniform u;
2802 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2803 u.binding = 5;
2804 u.append_id(dynamic_maps[dynamic_maps.size() - 1].texture);
2805 uniforms.push_back(u);
2806 }
2807 {
2808 RD::Uniform u;
2809 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2810 u.binding = 6;
2811 u.append_id(dynamic_maps[dynamic_maps.size() - 1].depth);
2812 uniforms.push_back(u);
2813 }
2814
2815 if (write) {
2816 {
2817 RD::Uniform u;
2818 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2819 u.binding = 7;
2820 u.append_id(dmap.texture);
2821 uniforms.push_back(u);
2822 }
2823 {
2824 RD::Uniform u;
2825 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2826 u.binding = 8;
2827 u.append_id(dmap.depth);
2828 uniforms.push_back(u);
2829 }
2830 }
2831
2832 {
2833 RD::Uniform u;
2834 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2835 u.binding = 9;
2836 u.append_id(gi->voxel_gi_get_sdf_texture(probe));
2837 uniforms.push_back(u);
2838 }
2839 {
2840 RD::Uniform u;
2841 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2842 u.binding = 10;
2843 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
2844 uniforms.push_back(u);
2845 }
2846
2847 if (plot) {
2848 {
2849 RD::Uniform u;
2850 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2851 u.binding = 11;
2852 u.append_id(mipmaps[dmap.mipmap].texture);
2853 uniforms.push_back(u);
2854 }
2855 }
2856
2857 dmap.uniform_set = RD::get_singleton()->uniform_set_create(
2858 uniforms,
2859 gi->voxel_gi_lighting_shader_version_shaders[(write && plot) ? VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT : (write ? VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE : VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_PLOT)],
2860 0);
2861 }
2862
2863 dynamic_maps.push_back(dmap);
2864 }
2865 }
2866 }
2867
2868 last_probe_data_version = data_version;
2869 p_update_light_instances = true; //just in case
2870
2871 RendererSceneRenderRD::get_singleton()->base_uniforms_changed();
2872 }
2873
2874 // UDPDATE TIME
2875
2876 if (has_dynamic_object_data) {
2877 //if it has dynamic object data, it needs to be cleared
2878 RD::get_singleton()->texture_clear(texture, Color(0, 0, 0, 0), 0, mipmaps.size(), 0, 1);
2879 }
2880
2881 uint32_t light_count = 0;
2882
2883 if (p_update_light_instances || p_dynamic_objects.size() > 0) {
2884 light_count = MIN(gi->voxel_gi_max_lights, (uint32_t)p_light_instances.size());
2885
2886 {
2887 Transform3D to_cell = gi->voxel_gi_get_to_cell_xform(probe);
2888 Transform3D to_probe_xform = to_cell * transform.affine_inverse();
2889
2890 //update lights
2891
2892 for (uint32_t i = 0; i < light_count; i++) {
2893 VoxelGILight &l = gi->voxel_gi_lights[i];
2894 RID light_instance = p_light_instances[i];
2895 RID light = light_storage->light_instance_get_base_light(light_instance);
2896
2897 l.type = RSG::light_storage->light_get_type(light);
2898 if (l.type == RS::LIGHT_DIRECTIONAL && RSG::light_storage->light_directional_get_sky_mode(light) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
2899 light_count--;
2900 continue;
2901 }
2902
2903 l.attenuation = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
2904 l.energy = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ENERGY) * RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
2905
2906 if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
2907 l.energy *= RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INTENSITY);
2908
2909 l.energy *= gi->voxel_gi_get_baked_exposure_normalization(probe);
2910
2911 // Convert from Luminous Power to Luminous Intensity
2912 if (l.type == RS::LIGHT_OMNI) {
2913 l.energy *= 1.0 / (Math::PI * 4.0);
2914 } else if (l.type == RS::LIGHT_SPOT) {
2915 // Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
2916 // We make this assumption to keep them easy to control.
2917 l.energy *= 1.0 / Math::PI;
2918 }
2919 }
2920
2921 l.radius = to_cell.basis.xform(Vector3(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_RANGE), 0, 0)).length();
2922 Color color = RSG::light_storage->light_get_color(light).srgb_to_linear();
2923 l.color[0] = color.r;
2924 l.color[1] = color.g;
2925 l.color[2] = color.b;
2926
2927 l.cos_spot_angle = Math::cos(Math::deg_to_rad(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE)));
2928 l.inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
2929
2930 Transform3D xform = light_storage->light_instance_get_base_transform(light_instance);
2931
2932 Vector3 pos = to_probe_xform.xform(xform.origin);
2933 Vector3 dir = to_probe_xform.basis.xform(-xform.basis.get_column(2)).normalized();
2934
2935 l.position[0] = pos.x;
2936 l.position[1] = pos.y;
2937 l.position[2] = pos.z;
2938
2939 l.direction[0] = dir.x;
2940 l.direction[1] = dir.y;
2941 l.direction[2] = dir.z;
2942
2943 l.has_shadow = RSG::light_storage->light_has_shadow(light);
2944 }
2945
2946 RD::get_singleton()->buffer_update(gi->voxel_gi_lights_uniform, 0, sizeof(VoxelGILight) * light_count, gi->voxel_gi_lights);
2947 }
2948 }
2949
2950 if (has_dynamic_object_data || p_update_light_instances || p_dynamic_objects.size()) {
2951 // PROCESS MIPMAPS
2952 if (mipmaps.size()) {
2953 //can update mipmaps
2954
2955 Vector3i probe_size = gi->voxel_gi_get_octree_size(probe);
2956
2957 Vector3 ps = probe_size / gi->voxel_gi_get_bounds(probe).size;
2958 float cell_size = (1.0 / MAX(MAX(ps.x, ps.y), ps.z)); // probe size relative to 1 unit in world space
2959
2960 VoxelGIPushConstant push_constant;
2961
2962 push_constant.limits[0] = probe_size.x;
2963 push_constant.limits[1] = probe_size.y;
2964 push_constant.limits[2] = probe_size.z;
2965 push_constant.stack_size = mipmaps.size();
2966 push_constant.emission_scale = 1.0;
2967 push_constant.propagation = gi->voxel_gi_get_propagation(probe);
2968 push_constant.dynamic_range = gi->voxel_gi_get_dynamic_range(probe);
2969 push_constant.light_count = light_count;
2970 push_constant.aniso_strength = 0;
2971 push_constant.cell_size = cell_size;
2972
2973 /* print_line("probe update to version " + itos(last_probe_version));
2974 print_line("propagation " + rtos(push_constant.propagation));
2975 print_line("dynrange " + rtos(push_constant.dynamic_range));
2976 */
2977 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
2978
2979 int passes;
2980 if (p_update_light_instances) {
2981 passes = gi->voxel_gi_is_using_two_bounces(probe) ? 2 : 1;
2982 } else {
2983 passes = 1; //only re-blitting is necessary
2984 }
2985 int wg_size = 64;
2986 int64_t wg_limit_x = (int64_t)RD::get_singleton()->limit_get(RD::LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
2987
2988 for (int pass = 0; pass < passes; pass++) {
2989 if (p_update_light_instances) {
2990 for (int i = 0; i < mipmaps.size(); i++) {
2991 if (i == 0) {
2992 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[pass == 0 ? VOXEL_GI_SHADER_VERSION_COMPUTE_LIGHT : VOXEL_GI_SHADER_VERSION_COMPUTE_SECOND_BOUNCE].get_rid());
2993 } else if (i == 1) {
2994 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_COMPUTE_MIPMAP].get_rid());
2995 }
2996
2997 if (pass == 1 || i > 0) {
2998 RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
2999 }
3000 if (pass == 0 || i > 0) {
3001 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].uniform_set, 0);
3002 } else {
3003 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].second_bounce_uniform_set, 0);
3004 }
3005
3006 push_constant.cell_offset = mipmaps[i].cell_offset;
3007 push_constant.cell_count = mipmaps[i].cell_count;
3008
3009 int64_t wg_todo = (mipmaps[i].cell_count + wg_size - 1) / wg_size;
3010 while (wg_todo) {
3011 int64_t wg_count = MIN(wg_todo, wg_limit_x);
3012 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIPushConstant));
3013 RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
3014 wg_todo -= wg_count;
3015 push_constant.cell_offset += wg_count * wg_size;
3016 }
3017 }
3018
3019 RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
3020 }
3021
3022 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_WRITE_TEXTURE].get_rid());
3023
3024 for (int i = 0; i < mipmaps.size(); i++) {
3025 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].write_uniform_set, 0);
3026
3027 push_constant.cell_offset = mipmaps[i].cell_offset;
3028 push_constant.cell_count = mipmaps[i].cell_count;
3029
3030 int64_t wg_todo = (mipmaps[i].cell_count + wg_size - 1) / wg_size;
3031 while (wg_todo) {
3032 int64_t wg_count = MIN(wg_todo, wg_limit_x);
3033 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIPushConstant));
3034 RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
3035 wg_todo -= wg_count;
3036 push_constant.cell_offset += wg_count * wg_size;
3037 }
3038 }
3039 }
3040
3041 RD::get_singleton()->compute_list_end();
3042 }
3043 }
3044
3045 has_dynamic_object_data = false; //clear until dynamic object data is used again
3046
3047 if (p_dynamic_objects.size() && dynamic_maps.size()) {
3048 Vector3i octree_size = gi->voxel_gi_get_octree_size(probe);
3049 int multiplier = dynamic_maps[0].size / MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
3050
3051 Transform3D oversample_scale;
3052 oversample_scale.basis.scale(Vector3(multiplier, multiplier, multiplier));
3053
3054 Transform3D to_cell = oversample_scale * gi->voxel_gi_get_to_cell_xform(probe);
3055 Transform3D to_world_xform = transform * to_cell.affine_inverse();
3056 Transform3D to_probe_xform = to_world_xform.affine_inverse();
3057
3058 AABB probe_aabb(Vector3(), octree_size);
3059
3060 //this could probably be better parallelized in compute..
3061 for (int i = 0; i < (int)p_dynamic_objects.size(); i++) {
3062 RenderGeometryInstance *instance = p_dynamic_objects[i];
3063
3064 //transform aabb to voxel_gi
3065 AABB aabb = (to_probe_xform * instance->get_transform()).xform(instance->get_aabb());
3066
3067 //this needs to wrap to grid resolution to avoid jitter
3068 //also extend margin a bit just in case
3069 Vector3i begin = aabb.position - Vector3i(1, 1, 1);
3070 Vector3i end = aabb.position + aabb.size + Vector3i(1, 1, 1);
3071
3072 for (int j = 0; j < 3; j++) {
3073 if ((end[j] - begin[j]) & 1) {
3074 end[j]++; //for half extents split, it needs to be even
3075 }
3076 begin[j] = MAX(begin[j], 0);
3077 end[j] = MIN(end[j], octree_size[j] * multiplier);
3078 }
3079
3080 //aabb = aabb.intersection(probe_aabb); //intersect
3081 aabb.position = begin;
3082 aabb.size = end - begin;
3083
3084 //print_line("aabb: " + aabb);
3085
3086 for (int j = 0; j < 6; j++) {
3087 //if (j != 0 && j != 3) {
3088 // continue;
3089 //}
3090 static const Vector3 render_z[6] = {
3091 Vector3(1, 0, 0),
3092 Vector3(0, 1, 0),
3093 Vector3(0, 0, 1),
3094 Vector3(-1, 0, 0),
3095 Vector3(0, -1, 0),
3096 Vector3(0, 0, -1),
3097 };
3098 static const Vector3 render_up[6] = {
3099 Vector3(0, 1, 0),
3100 Vector3(0, 0, 1),
3101 Vector3(0, 1, 0),
3102 Vector3(0, 1, 0),
3103 Vector3(0, 0, 1),
3104 Vector3(0, 1, 0),
3105 };
3106
3107 Vector3 render_dir = render_z[j];
3108 Vector3 up_dir = render_up[j];
3109
3110 Vector3 center = aabb.get_center();
3111 Transform3D xform;
3112 xform.set_look_at(center - aabb.size * 0.5 * render_dir, center, up_dir);
3113
3114 Vector3 x_dir = xform.basis.get_column(0).abs();
3115 int x_axis = int(Vector3(0, 1, 2).dot(x_dir));
3116 Vector3 y_dir = xform.basis.get_column(1).abs();
3117 int y_axis = int(Vector3(0, 1, 2).dot(y_dir));
3118 Vector3 z_dir = -xform.basis.get_column(2);
3119 int z_axis = int(Vector3(0, 1, 2).dot(z_dir.abs()));
3120
3121 Rect2i rect(aabb.position[x_axis], aabb.position[y_axis], aabb.size[x_axis], aabb.size[y_axis]);
3122 bool x_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_column(0)) < 0);
3123 bool y_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_column(1)) < 0);
3124 bool z_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_column(2)) > 0);
3125
3126 Projection cm;
3127 cm.set_orthogonal(-rect.size.width / 2, rect.size.width / 2, -rect.size.height / 2, rect.size.height / 2, 0.0001, aabb.size[z_axis]);
3128
3129 if (RendererSceneRenderRD::get_singleton()->cull_argument.size() == 0) {
3130 RendererSceneRenderRD::get_singleton()->cull_argument.push_back(nullptr);
3131 }
3132 RendererSceneRenderRD::get_singleton()->cull_argument[0] = instance;
3133
3134 float exposure_normalization = 1.0;
3135 if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
3136 exposure_normalization = gi->voxel_gi_get_baked_exposure_normalization(probe);
3137 }
3138
3139 RendererSceneRenderRD::get_singleton()->_render_material(to_world_xform * xform, cm, true, RendererSceneRenderRD::get_singleton()->cull_argument, dynamic_maps[0].fb, Rect2i(Vector2i(), rect.size), exposure_normalization);
3140
3141 Vector3 ps = octree_size / gi->voxel_gi_get_bounds(probe).size;
3142 float cell_size = (1.0 / MAX(MAX(ps.x, ps.y), ps.z)); // probe size relative to 1 unit in world space
3143
3144 VoxelGIDynamicPushConstant push_constant;
3145 memset(&push_constant, 0, sizeof(VoxelGIDynamicPushConstant));
3146 push_constant.limits[0] = octree_size.x;
3147 push_constant.limits[1] = octree_size.y;
3148 push_constant.limits[2] = octree_size.z;
3149 push_constant.light_count = p_light_instances.size();
3150 push_constant.x_dir[0] = x_dir[0];
3151 push_constant.x_dir[1] = x_dir[1];
3152 push_constant.x_dir[2] = x_dir[2];
3153 push_constant.y_dir[0] = y_dir[0];
3154 push_constant.y_dir[1] = y_dir[1];
3155 push_constant.y_dir[2] = y_dir[2];
3156 push_constant.z_dir[0] = z_dir[0];
3157 push_constant.z_dir[1] = z_dir[1];
3158 push_constant.z_dir[2] = z_dir[2];
3159 push_constant.z_base = xform.origin[z_axis];
3160 push_constant.z_sign = (z_flip ? -1.0 : 1.0);
3161 push_constant.pos_multiplier = float(1.0) / multiplier;
3162 push_constant.dynamic_range = gi->voxel_gi_get_dynamic_range(probe);
3163 push_constant.flip_x = x_flip;
3164 push_constant.flip_y = y_flip;
3165 push_constant.rect_pos[0] = rect.position[0];
3166 push_constant.rect_pos[1] = rect.position[1];
3167 push_constant.rect_size[0] = rect.size[0];
3168 push_constant.rect_size[1] = rect.size[1];
3169 push_constant.prev_rect_ofs[0] = 0;
3170 push_constant.prev_rect_ofs[1] = 0;
3171 push_constant.prev_rect_size[0] = 0;
3172 push_constant.prev_rect_size[1] = 0;
3173 push_constant.on_mipmap = false;
3174 push_constant.propagation = gi->voxel_gi_get_propagation(probe);
3175 push_constant.cell_size = cell_size;
3176 push_constant.pad[0] = 0;
3177 push_constant.pad[1] = 0;
3178
3179 //process lighting
3180 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
3181 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING].get_rid());
3182 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, dynamic_maps[0].uniform_set, 0);
3183 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIDynamicPushConstant));
3184 RD::get_singleton()->compute_list_dispatch(compute_list, Math::division_round_up(rect.size.x, 8), Math::division_round_up(rect.size.y, 8), 1);
3185 //print_line("rect: " + itos(i) + ": " + rect);
3186
3187 for (int k = 1; k < dynamic_maps.size(); k++) {
3188 // enlarge the rect if needed so all pixels fit when downscaled,
3189 // this ensures downsampling is smooth and optimal because no pixels are left behind
3190
3191 //x
3192 if (rect.position.x & 1) {
3193 rect.size.x++;
3194 push_constant.prev_rect_ofs[0] = 1; //this is used to ensure reading is also optimal
3195 } else {
3196 push_constant.prev_rect_ofs[0] = 0;
3197 }
3198 if (rect.size.x & 1) {
3199 rect.size.x++;
3200 }
3201
3202 rect.position.x >>= 1;
3203 rect.size.x = MAX(1, rect.size.x >> 1);
3204
3205 //y
3206 if (rect.position.y & 1) {
3207 rect.size.y++;
3208 push_constant.prev_rect_ofs[1] = 1;
3209 } else {
3210 push_constant.prev_rect_ofs[1] = 0;
3211 }
3212 if (rect.size.y & 1) {
3213 rect.size.y++;
3214 }
3215
3216 rect.position.y >>= 1;
3217 rect.size.y = MAX(1, rect.size.y >> 1);
3218
3219 //shrink limits to ensure plot does not go outside map
3220 if (dynamic_maps[k].mipmap > 0) {
3221 for (int l = 0; l < 3; l++) {
3222 push_constant.limits[l] = MAX(1, push_constant.limits[l] >> 1);
3223 }
3224 }
3225
3226 //print_line("rect: " + itos(i) + ": " + rect);
3227 push_constant.rect_pos[0] = rect.position[0];
3228 push_constant.rect_pos[1] = rect.position[1];
3229 push_constant.prev_rect_size[0] = push_constant.rect_size[0];
3230 push_constant.prev_rect_size[1] = push_constant.rect_size[1];
3231 push_constant.rect_size[0] = rect.size[0];
3232 push_constant.rect_size[1] = rect.size[1];
3233 push_constant.on_mipmap = dynamic_maps[k].mipmap > 0;
3234
3235 RD::get_singleton()->compute_list_add_barrier(compute_list);
3236
3237 if (dynamic_maps[k].mipmap < 0) {
3238 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE].get_rid());
3239 } else if (k < dynamic_maps.size() - 1) {
3240 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT].get_rid());
3241 } else {
3242 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_PLOT].get_rid());
3243 }
3244 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, dynamic_maps[k].uniform_set, 0);
3245 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIDynamicPushConstant));
3246 RD::get_singleton()->compute_list_dispatch(compute_list, Math::division_round_up(rect.size.x, 8), Math::division_round_up(rect.size.y, 8), 1);
3247 }
3248
3249 RD::get_singleton()->compute_list_end();
3250 }
3251 }
3252
3253 has_dynamic_object_data = true; //clear until dynamic object data is used again
3254 }
3255
3256 last_probe_version = gi->voxel_gi_get_version(probe);
3257 }
3258
3259 void GI::VoxelGIInstance::free_resources() {
3260 if (texture.is_valid()) {
3261 RD::get_singleton()->free_rid(texture);
3262 RD::get_singleton()->free_rid(write_buffer);
3263
3264 texture = RID();
3265 write_buffer = RID();
3266 mipmaps.clear();
3267 }
3268
3269 for (int i = 0; i < dynamic_maps.size(); i++) {
3270 RD::get_singleton()->free_rid(dynamic_maps[i].texture);
3271 RD::get_singleton()->free_rid(dynamic_maps[i].depth);
3272
3273 // these only exist on the first level...
3274 if (dynamic_maps[i].fb_depth.is_valid()) {
3275 RD::get_singleton()->free_rid(dynamic_maps[i].fb_depth);
3276 }
3277 if (dynamic_maps[i].albedo.is_valid()) {
3278 RD::get_singleton()->free_rid(dynamic_maps[i].albedo);
3279 }
3280 if (dynamic_maps[i].normal.is_valid()) {
3281 RD::get_singleton()->free_rid(dynamic_maps[i].normal);
3282 }
3283 if (dynamic_maps[i].orm.is_valid()) {
3284 RD::get_singleton()->free_rid(dynamic_maps[i].orm);
3285 }
3286 }
3287 dynamic_maps.clear();
3288 }
3289
3290 void GI::VoxelGIInstance::debug(RD::DrawListID p_draw_list, RID p_framebuffer, const Projection &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha) {
3291 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
3292
3293 if (mipmaps.is_empty()) {
3294 return;
3295 }
3296
3297 Projection cam_transform = (p_camera_with_transform * Projection(transform)) * Projection(gi->voxel_gi_get_to_cell_xform(probe).affine_inverse());
3298
3299 int level = 0;
3300 Vector3i octree_size = gi->voxel_gi_get_octree_size(probe);
3301
3302 VoxelGIDebugPushConstant push_constant;
3303 push_constant.alpha = p_alpha;
3304 push_constant.dynamic_range = gi->voxel_gi_get_dynamic_range(probe);
3305 push_constant.cell_offset = mipmaps[level].cell_offset;
3306 push_constant.level = level;
3307
3308 push_constant.bounds[0] = octree_size.x >> level;
3309 push_constant.bounds[1] = octree_size.y >> level;
3310 push_constant.bounds[2] = octree_size.z >> level;
3311 push_constant.pad = 0;
3312
3313 for (int i = 0; i < 4; i++) {
3314 for (int j = 0; j < 4; j++) {
3315 push_constant.projection[i * 4 + j] = cam_transform.columns[i][j];
3316 }
3317 }
3318
3319 if (gi->voxel_gi_debug_uniform_set.is_valid()) {
3320 RD::get_singleton()->free_rid(gi->voxel_gi_debug_uniform_set);
3321 }
3322 Vector<RD::Uniform> uniforms;
3323 {
3324 RD::Uniform u;
3325 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
3326 u.binding = 1;
3327 u.append_id(gi->voxel_gi_get_data_buffer(probe));
3328 uniforms.push_back(u);
3329 }
3330 {
3331 RD::Uniform u;
3332 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3333 u.binding = 2;
3334 u.append_id(texture);
3335 uniforms.push_back(u);
3336 }
3337 {
3338 RD::Uniform u;
3339 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
3340 u.binding = 3;
3341 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
3342 uniforms.push_back(u);
3343 }
3344
3345 int cell_count;
3346 if (!p_emission && p_lighting && has_dynamic_object_data) {
3347 cell_count = push_constant.bounds[0] * push_constant.bounds[1] * push_constant.bounds[2];
3348 } else {
3349 cell_count = mipmaps[level].cell_count;
3350 }
3351
3352 gi->voxel_gi_debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->voxel_gi_debug_shader_version_shaders[0], 0);
3353
3354 int voxel_gi_debug_pipeline = VOXEL_GI_DEBUG_COLOR;
3355 if (p_emission) {
3356 voxel_gi_debug_pipeline = VOXEL_GI_DEBUG_EMISSION;
3357 } else if (p_lighting) {
3358 voxel_gi_debug_pipeline = has_dynamic_object_data ? VOXEL_GI_DEBUG_LIGHT_FULL : VOXEL_GI_DEBUG_LIGHT;
3359 }
3360 RD::get_singleton()->draw_list_bind_render_pipeline(
3361 p_draw_list,
3362 gi->voxel_gi_debug_shader_version_pipelines[voxel_gi_debug_pipeline].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
3363 RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, gi->voxel_gi_debug_uniform_set, 0);
3364 RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(VoxelGIDebugPushConstant));
3365 RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, 36);
3366 }
3367
3368 ////////////////////////////////////////////////////////////////////////////////
3369 // GI
3370
3371 GI::GI() {
3372 singleton = this;
3373
3374 sdfgi_ray_count = RS::EnvironmentSDFGIRayCount(CLAMP(int32_t(GLOBAL_GET("rendering/global_illumination/sdfgi/probe_ray_count")), 0, int32_t(RS::ENV_SDFGI_RAY_COUNT_MAX - 1)));
3375 sdfgi_frames_to_converge = RS::EnvironmentSDFGIFramesToConverge(CLAMP(int32_t(GLOBAL_GET("rendering/global_illumination/sdfgi/frames_to_converge")), 0, int32_t(RS::ENV_SDFGI_CONVERGE_MAX - 1)));
3376 sdfgi_frames_to_update_light = RS::EnvironmentSDFGIFramesToUpdateLight(CLAMP(int32_t(GLOBAL_GET("rendering/global_illumination/sdfgi/frames_to_update_lights")), 0, int32_t(RS::ENV_SDFGI_UPDATE_LIGHT_MAX - 1)));
3377 }
3378
3379 GI::~GI() {
3380 for (int v = 0; v < SHADER_SPECIALIZATION_VARIATIONS; v++) {
3381 for (int i = 0; i < MODE_MAX; i++) {
3382 pipelines[v][i].free();
3383 }
3384 }
3385
3386 sdfgi_shader.debug_pipeline.free();
3387
3388 for (int i = 0; i < SDFGIShader::DIRECT_LIGHT_MODE_MAX; i++) {
3389 sdfgi_shader.direct_light_pipeline[i].free();
3390 }
3391
3392 for (int i = 0; i < SDFGIShader::INTEGRATE_MODE_MAX; i++) {
3393 sdfgi_shader.integrate_pipeline[i].free();
3394 }
3395
3396 for (int i = 0; i < SDFGIShader::PRE_PROCESS_MAX; i++) {
3397 sdfgi_shader.preprocess_pipeline[i].free();
3398 }
3399
3400 for (int i = 0; i < VOXEL_GI_SHADER_VERSION_MAX; i++) {
3401 voxel_gi_lighting_shader_version_pipelines[i].free();
3402 }
3403
3404 if (voxel_gi_debug_shader_version.is_valid()) {
3405 voxel_gi_debug_shader.version_free(voxel_gi_debug_shader_version);
3406 }
3407 if (voxel_gi_lighting_shader_version.is_valid()) {
3408 voxel_gi_shader.version_free(voxel_gi_lighting_shader_version);
3409 }
3410 if (shader_version.is_valid()) {
3411 shader.version_free(shader_version);
3412 }
3413 if (sdfgi_shader.debug_probes_shader.is_valid()) {
3414 sdfgi_shader.debug_probes.version_free(sdfgi_shader.debug_probes_shader);
3415 }
3416 if (sdfgi_shader.debug_shader.is_valid()) {
3417 sdfgi_shader.debug.version_free(sdfgi_shader.debug_shader);
3418 }
3419 if (sdfgi_shader.direct_light_shader.is_valid()) {
3420 sdfgi_shader.direct_light.version_free(sdfgi_shader.direct_light_shader);
3421 }
3422 if (sdfgi_shader.integrate_shader.is_valid()) {
3423 sdfgi_shader.integrate.version_free(sdfgi_shader.integrate_shader);
3424 }
3425 if (sdfgi_shader.preprocess_shader.is_valid()) {
3426 sdfgi_shader.preprocess.version_free(sdfgi_shader.preprocess_shader);
3427 }
3428
3429 singleton = nullptr;
3430 }
3431
3432 void GI::init(SkyRD *p_sky) {
3433 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
3434 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
3435
3436 /* GI */
3437
3438 {
3439 //kinda complicated to compute the amount of slots, we try to use as many as we can
3440
3441 voxel_gi_lights = memnew_arr(VoxelGILight, voxel_gi_max_lights);
3442 voxel_gi_lights_uniform = RD::get_singleton()->uniform_buffer_create(voxel_gi_max_lights * sizeof(VoxelGILight));
3443 voxel_gi_quality = RS::VoxelGIQuality(CLAMP(int(GLOBAL_GET("rendering/global_illumination/voxel_gi/quality")), 0, 1));
3444
3445 String defines = "\n#define MAX_LIGHTS " + itos(voxel_gi_max_lights) + "\n";
3446
3447 Vector<String> versions;
3448 versions.push_back("\n#define MODE_COMPUTE_LIGHT\n");
3449 versions.push_back("\n#define MODE_SECOND_BOUNCE\n");
3450 versions.push_back("\n#define MODE_UPDATE_MIPMAPS\n");
3451 versions.push_back("\n#define MODE_WRITE_TEXTURE\n");
3452 versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_LIGHTING\n");
3453 versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
3454 versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n");
3455 versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
3456
3457 voxel_gi_shader.initialize(versions, defines);
3458 voxel_gi_lighting_shader_version = voxel_gi_shader.version_create();
3459 for (int i = 0; i < VOXEL_GI_SHADER_VERSION_MAX; i++) {
3460 voxel_gi_lighting_shader_version_shaders[i] = voxel_gi_shader.version_get_shader(voxel_gi_lighting_shader_version, i);
3461 voxel_gi_lighting_shader_version_pipelines[i].create_compute_pipeline(voxel_gi_lighting_shader_version_shaders[i]);
3462 }
3463 }
3464
3465 {
3466 String defines;
3467 Vector<String> versions;
3468 versions.push_back("\n#define MODE_DEBUG_COLOR\n");
3469 versions.push_back("\n#define MODE_DEBUG_LIGHT\n");
3470 versions.push_back("\n#define MODE_DEBUG_EMISSION\n");
3471 versions.push_back("\n#define MODE_DEBUG_LIGHT\n#define MODE_DEBUG_LIGHT_FULL\n");
3472
3473 voxel_gi_debug_shader.initialize(versions, defines);
3474 voxel_gi_debug_shader_version = voxel_gi_debug_shader.version_create();
3475 for (int i = 0; i < VOXEL_GI_DEBUG_MAX; i++) {
3476 voxel_gi_debug_shader_version_shaders[i] = voxel_gi_debug_shader.version_get_shader(voxel_gi_debug_shader_version, i);
3477
3478 RD::PipelineRasterizationState rs;
3479 rs.cull_mode = RD::POLYGON_CULL_FRONT;
3480 RD::PipelineDepthStencilState ds;
3481 ds.enable_depth_test = true;
3482 ds.enable_depth_write = true;
3483 ds.depth_compare_operator = RD::COMPARE_OP_GREATER_OR_EQUAL;
3484
3485 voxel_gi_debug_shader_version_pipelines[i].setup(voxel_gi_debug_shader_version_shaders[i], RD::RENDER_PRIMITIVE_TRIANGLES, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0);
3486 }
3487 }
3488
3489 /* SDGFI */
3490
3491 {
3492 Vector<String> preprocess_modes;
3493 preprocess_modes.push_back("\n#define MODE_SCROLL\n");
3494 preprocess_modes.push_back("\n#define MODE_SCROLL_OCCLUSION\n");
3495 preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD\n");
3496 preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD_HALF\n");
3497 preprocess_modes.push_back("\n#define MODE_JUMPFLOOD\n");
3498 preprocess_modes.push_back("\n#define MODE_JUMPFLOOD_OPTIMIZED\n");
3499 preprocess_modes.push_back("\n#define MODE_UPSCALE_JUMP_FLOOD\n");
3500 preprocess_modes.push_back("\n#define MODE_OCCLUSION\n");
3501 preprocess_modes.push_back("\n#define MODE_STORE\n");
3502 String defines = "\n#define OCCLUSION_SIZE " + itos(SDFGI::CASCADE_SIZE / SDFGI::PROBE_DIVISOR) + "\n";
3503 sdfgi_shader.preprocess.initialize(preprocess_modes, defines);
3504 sdfgi_shader.preprocess_shader = sdfgi_shader.preprocess.version_create();
3505 for (int i = 0; i < SDFGIShader::PRE_PROCESS_MAX; i++) {
3506 sdfgi_shader.preprocess_pipeline[i].create_compute_pipeline(sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, i));
3507 }
3508 }
3509
3510 {
3511 //calculate tables
3512 String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3513
3514 Vector<String> direct_light_modes;
3515 direct_light_modes.push_back("\n#define MODE_PROCESS_STATIC\n");
3516 direct_light_modes.push_back("\n#define MODE_PROCESS_DYNAMIC\n");
3517 sdfgi_shader.direct_light.initialize(direct_light_modes, defines);
3518 sdfgi_shader.direct_light_shader = sdfgi_shader.direct_light.version_create();
3519 for (int i = 0; i < SDFGIShader::DIRECT_LIGHT_MODE_MAX; i++) {
3520 sdfgi_shader.direct_light_pipeline[i].create_compute_pipeline(sdfgi_shader.direct_light.version_get_shader(sdfgi_shader.direct_light_shader, i));
3521 }
3522 }
3523
3524 {
3525 //calculate tables
3526 String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3527 defines += "\n#define SH_SIZE " + itos(SDFGI::SH_SIZE) + "\n";
3528 if (p_sky->sky_use_octmap_array) {
3529 defines += "\n#define USE_OCTMAP_ARRAY\n";
3530 }
3531
3532 Vector<String> integrate_modes;
3533 integrate_modes.push_back("\n#define MODE_PROCESS\n");
3534 integrate_modes.push_back("\n#define MODE_STORE\n");
3535 integrate_modes.push_back("\n#define MODE_SCROLL\n");
3536 integrate_modes.push_back("\n#define MODE_SCROLL_STORE\n");
3537 sdfgi_shader.integrate.initialize(integrate_modes, defines);
3538 sdfgi_shader.integrate_shader = sdfgi_shader.integrate.version_create();
3539
3540 for (int i = 0; i < SDFGIShader::INTEGRATE_MODE_MAX; i++) {
3541 sdfgi_shader.integrate_pipeline[i].create_compute_pipeline(sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, i));
3542 }
3543
3544 {
3545 Vector<RD::Uniform> uniforms;
3546
3547 {
3548 RD::Uniform u;
3549 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3550 u.binding = 0;
3551 if (p_sky->sky_use_octmap_array) {
3552 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE));
3553 } else {
3554 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
3555 }
3556 uniforms.push_back(u);
3557 }
3558 {
3559 RD::Uniform u;
3560 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
3561 u.binding = 1;
3562 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
3563 uniforms.push_back(u);
3564 }
3565
3566 sdfgi_shader.integrate_default_sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, 0), 1);
3567 }
3568 }
3569
3570 //GK
3571 {
3572 //calculate tables
3573 String defines = "\n#define SDFGI_OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3574
3575 Vector<ShaderRD::VariantDefine> variants;
3576 for (uint32_t vrs = 0; vrs < 2; vrs++) {
3577 String vrs_base = vrs ? "\n#define USE_VRS\n" : "";
3578 Group group = vrs ? GROUP_VRS : GROUP_NORMAL;
3579 bool default_enabled = vrs == 0;
3580 variants.push_back(ShaderRD::VariantDefine(group, vrs_base + "\n#define USE_VOXEL_GI_INSTANCES\n", default_enabled)); // MODE_VOXEL_GI
3581 variants.push_back(ShaderRD::VariantDefine(group, vrs_base + "\n#define USE_VOXEL_GI_INSTANCES\n#define SAMPLE_VOXEL_GI_NEAREST\n", default_enabled)); // MODE_VOXEL_GI_WITHOUT_SAMPLER
3582 variants.push_back(ShaderRD::VariantDefine(group, vrs_base + "\n#define USE_SDFGI\n", default_enabled)); // MODE_SDFGI
3583 variants.push_back(ShaderRD::VariantDefine(group, vrs_base + "\n#define USE_SDFGI\n\n#define USE_VOXEL_GI_INSTANCES\n", default_enabled)); // MODE_COMBINED
3584 variants.push_back(ShaderRD::VariantDefine(group, vrs_base + "\n#define USE_SDFGI\n\n#define USE_VOXEL_GI_INSTANCES\n#define SAMPLE_VOXEL_GI_NEAREST\n", default_enabled)); // MODE_COMBINED_WITHOUT_SAMPLER
3585 }
3586
3587 shader.initialize(variants, defines);
3588
3589 bool vrs_supported = RendererSceneRenderRD::get_singleton()->is_vrs_supported();
3590 if (vrs_supported) {
3591 shader.enable_group(GROUP_VRS);
3592 }
3593
3594 shader_version = shader.version_create();
3595
3596 Vector<RD::PipelineSpecializationConstant> specialization_constants;
3597
3598 {
3599 RD::PipelineSpecializationConstant sc;
3600 sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
3601 sc.constant_id = 0; // SHADER_SPECIALIZATION_HALF_RES
3602 sc.bool_value = false;
3603 specialization_constants.push_back(sc);
3604
3605 sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
3606 sc.constant_id = 1; // SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX
3607 sc.bool_value = false;
3608 specialization_constants.push_back(sc);
3609
3610 sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
3611 sc.constant_id = 2; // SHADER_SPECIALIZATION_USE_VRS
3612 sc.bool_value = false;
3613 specialization_constants.push_back(sc);
3614 }
3615
3616 for (int v = 0; v < SHADER_SPECIALIZATION_VARIATIONS; v++) {
3617 specialization_constants.ptrw()[0].bool_value = (v & SHADER_SPECIALIZATION_HALF_RES) ? true : false;
3618 specialization_constants.ptrw()[1].bool_value = (v & SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX) ? true : false;
3619 specialization_constants.ptrw()[2].bool_value = (v & SHADER_SPECIALIZATION_USE_VRS) ? true : false;
3620
3621 int variant_base = vrs_supported ? MODE_MAX : 0;
3622 for (int i = 0; i < MODE_MAX; i++) {
3623 pipelines[v][i].create_compute_pipeline(shader.version_get_shader(shader_version, variant_base + i), specialization_constants);
3624 }
3625 }
3626
3627 sdfgi_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGIData));
3628 }
3629 {
3630 String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3631 Vector<String> debug_modes;
3632 debug_modes.push_back("");
3633 sdfgi_shader.debug.initialize(debug_modes, defines);
3634 sdfgi_shader.debug_shader = sdfgi_shader.debug.version_create();
3635 sdfgi_shader.debug_shader_version = sdfgi_shader.debug.version_get_shader(sdfgi_shader.debug_shader, 0);
3636 sdfgi_shader.debug_pipeline.create_compute_pipeline(sdfgi_shader.debug_shader_version);
3637 }
3638 {
3639 String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3640
3641 Vector<String> versions;
3642 versions.push_back("\n#define MODE_PROBES\n");
3643 versions.push_back("\n#define MODE_PROBES\n#define USE_MULTIVIEW\n");
3644 versions.push_back("\n#define MODE_VISIBILITY\n");
3645 versions.push_back("\n#define MODE_VISIBILITY\n#define USE_MULTIVIEW\n");
3646
3647 sdfgi_shader.debug_probes.initialize(versions, defines);
3648
3649 // TODO disable multiview versions if turned off
3650
3651 sdfgi_shader.debug_probes_shader = sdfgi_shader.debug_probes.version_create();
3652
3653 {
3654 RD::PipelineRasterizationState rs;
3655 rs.cull_mode = RD::POLYGON_CULL_DISABLED;
3656 RD::PipelineDepthStencilState ds;
3657 ds.enable_depth_test = true;
3658 ds.enable_depth_write = true;
3659 ds.depth_compare_operator = RD::COMPARE_OP_GREATER_OR_EQUAL;
3660 for (int i = 0; i < SDFGIShader::PROBE_DEBUG_MAX; i++) {
3661 // TODO check if version is enabled
3662
3663 RID debug_probes_shader_version = sdfgi_shader.debug_probes.version_get_shader(sdfgi_shader.debug_probes_shader, i);
3664 sdfgi_shader.debug_probes_pipeline[i].setup(debug_probes_shader_version, RD::RENDER_PRIMITIVE_TRIANGLE_STRIPS, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0);
3665 }
3666 }
3667 }
3668 default_voxel_gi_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(VoxelGIData) * MAX_VOXEL_GI_INSTANCES);
3669 half_resolution = GLOBAL_GET("rendering/global_illumination/gi/use_half_resolution");
3670 }
3671
3672 void GI::free() {
3673 if (default_voxel_gi_buffer.is_valid()) {
3674 RD::get_singleton()->free_rid(default_voxel_gi_buffer);
3675 }
3676 if (voxel_gi_lights_uniform.is_valid()) {
3677 RD::get_singleton()->free_rid(voxel_gi_lights_uniform);
3678 }
3679 if (sdfgi_ubo.is_valid()) {
3680 RD::get_singleton()->free_rid(sdfgi_ubo);
3681 }
3682
3683 if (voxel_gi_lights) {
3684 memdelete_arr(voxel_gi_lights);
3685 }
3686 }
3687
3688 Ref<GI::SDFGI> GI::create_sdfgi(RID p_env, const Vector3 &p_world_position, uint32_t p_requested_history_size) {
3689 Ref<SDFGI> sdfgi;
3690 sdfgi.instantiate();
3691
3692 sdfgi->create(p_env, p_world_position, p_requested_history_size, this);
3693
3694 return sdfgi;
3695 }
3696
3697 void GI::setup_voxel_gi_instances(RenderDataRD *p_render_data, Ref<RenderSceneBuffersRD> p_render_buffers, const Transform3D &p_transform, const PagedArray<RID> &p_voxel_gi_instances, uint32_t &r_voxel_gi_instances_used) {
3698 ERR_FAIL_COND(p_render_buffers.is_null());
3699
3700 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
3701 ERR_FAIL_NULL(texture_storage);
3702
3703 r_voxel_gi_instances_used = 0;
3704
3705 Ref<RenderBuffersGI> rbgi = p_render_buffers->get_custom_data(RB_SCOPE_GI);
3706 ERR_FAIL_COND(rbgi.is_null());
3707
3708 RID voxel_gi_buffer = rbgi->get_voxel_gi_buffer();
3709 VoxelGIData voxel_gi_data[MAX_VOXEL_GI_INSTANCES];
3710
3711 bool voxel_gi_instances_changed = false;
3712
3713 Transform3D to_camera;
3714 to_camera.origin = p_transform.origin; //only translation, make local
3715
3716 for (int i = 0; i < MAX_VOXEL_GI_INSTANCES; i++) {
3717 RID texture;
3718 if (i < (int)p_voxel_gi_instances.size()) {
3719 VoxelGIInstance *gipi = voxel_gi_instance_owner.get_or_null(p_voxel_gi_instances[i]);
3720
3721 if (gipi) {
3722 texture = gipi->texture;
3723 VoxelGIData &gipd = voxel_gi_data[i];
3724
3725 RID base_probe = gipi->probe;
3726
3727 Transform3D to_cell = voxel_gi_get_to_cell_xform(gipi->probe) * gipi->transform.affine_inverse() * to_camera;
3728
3729 gipd.xform[0] = to_cell.basis.rows[0][0];
3730 gipd.xform[1] = to_cell.basis.rows[1][0];
3731 gipd.xform[2] = to_cell.basis.rows[2][0];
3732 gipd.xform[3] = 0;
3733 gipd.xform[4] = to_cell.basis.rows[0][1];
3734 gipd.xform[5] = to_cell.basis.rows[1][1];
3735 gipd.xform[6] = to_cell.basis.rows[2][1];
3736 gipd.xform[7] = 0;
3737 gipd.xform[8] = to_cell.basis.rows[0][2];
3738 gipd.xform[9] = to_cell.basis.rows[1][2];
3739 gipd.xform[10] = to_cell.basis.rows[2][2];
3740 gipd.xform[11] = 0;
3741 gipd.xform[12] = to_cell.origin.x;
3742 gipd.xform[13] = to_cell.origin.y;
3743 gipd.xform[14] = to_cell.origin.z;
3744 gipd.xform[15] = 1;
3745
3746 Vector3 bounds = voxel_gi_get_octree_size(base_probe);
3747
3748 gipd.bounds[0] = bounds.x;
3749 gipd.bounds[1] = bounds.y;
3750 gipd.bounds[2] = bounds.z;
3751
3752 gipd.dynamic_range = voxel_gi_get_dynamic_range(base_probe) * voxel_gi_get_energy(base_probe);
3753 gipd.bias = voxel_gi_get_bias(base_probe);
3754 gipd.normal_bias = voxel_gi_get_normal_bias(base_probe);
3755 gipd.blend_ambient = !voxel_gi_is_interior(base_probe);
3756 gipd.mipmaps = gipi->mipmaps.size();
3757 gipd.exposure_normalization = 1.0;
3758 if (p_render_data->camera_attributes.is_valid()) {
3759 float exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
3760 gipd.exposure_normalization = exposure_normalization / voxel_gi_get_baked_exposure_normalization(base_probe);
3761 }
3762 }
3763
3764 r_voxel_gi_instances_used++;
3765 }
3766
3767 if (texture == RID()) {
3768 texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
3769 }
3770
3771 if (texture != rbgi->voxel_gi_textures[i]) {
3772 voxel_gi_instances_changed = true;
3773 rbgi->voxel_gi_textures[i] = texture;
3774 }
3775 }
3776
3777 if (voxel_gi_instances_changed) {
3778 for (uint32_t v = 0; v < RendererSceneRender::MAX_RENDER_VIEWS; v++) {
3779 if (RD::get_singleton()->uniform_set_is_valid(rbgi->uniform_set[v])) {
3780 RD::get_singleton()->free_rid(rbgi->uniform_set[v]);
3781 }
3782 rbgi->uniform_set[v] = RID();
3783 }
3784
3785 if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) {
3786 // VoxelGI instances have changed, so we need to update volumetric fog.
3787 Ref<RendererRD::Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG);
3788 fog->sync_gi_dependent_sets_validity(true);
3789 }
3790 }
3791
3792 if (p_voxel_gi_instances.size() > 0) {
3793 RD::get_singleton()->draw_command_begin_label("VoxelGIs Setup");
3794
3795 RD::get_singleton()->buffer_update(voxel_gi_buffer, 0, sizeof(VoxelGIData) * MIN((uint64_t)MAX_VOXEL_GI_INSTANCES, p_voxel_gi_instances.size()), voxel_gi_data);
3796
3797 RD::get_singleton()->draw_command_end_label();
3798 }
3799 }
3800
3801 RID GI::RenderBuffersGI::get_voxel_gi_buffer() {
3802 if (voxel_gi_buffer.is_null()) {
3803 voxel_gi_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(GI::VoxelGIData) * GI::MAX_VOXEL_GI_INSTANCES);
3804 }
3805 return voxel_gi_buffer;
3806 }
3807
3808 void GI::RenderBuffersGI::free_data() {
3809 for (uint32_t v = 0; v < RendererSceneRender::MAX_RENDER_VIEWS; v++) {
3810 if (RD::get_singleton()->uniform_set_is_valid(uniform_set[v])) {
3811 RD::get_singleton()->free_rid(uniform_set[v]);
3812 }
3813 uniform_set[v] = RID();
3814 }
3815
3816 if (scene_data_ubo.is_valid()) {
3817 RD::get_singleton()->free_rid(scene_data_ubo);
3818 scene_data_ubo = RID();
3819 }
3820
3821 if (voxel_gi_buffer.is_valid()) {
3822 RD::get_singleton()->free_rid(voxel_gi_buffer);
3823 voxel_gi_buffer = RID();
3824 }
3825 }
3826
3827 void GI::process_gi(Ref<RenderSceneBuffersRD> p_render_buffers, const RID *p_normal_roughness_slices, RID p_voxel_gi_buffer, RID p_environment, uint32_t p_view_count, const Projection *p_projections, const Vector3 *p_eye_offsets, const Transform3D &p_cam_transform, const PagedArray<RID> &p_voxel_gi_instances) {
3828 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
3829 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
3830
3831 ERR_FAIL_COND_MSG(p_view_count > 2, "Maximum of 2 views supported for Processing GI.");
3832
3833 RD::get_singleton()->draw_command_begin_label("GI Render");
3834
3835 ERR_FAIL_COND(p_render_buffers.is_null());
3836
3837 Ref<RenderBuffersGI> rbgi = p_render_buffers->get_custom_data(RB_SCOPE_GI);
3838 ERR_FAIL_COND(rbgi.is_null());
3839
3840 Size2i internal_size = p_render_buffers->get_internal_size();
3841
3842 if (rbgi->using_half_size_gi != half_resolution) {
3843 p_render_buffers->clear_context(RB_SCOPE_GI);
3844 }
3845
3846 if (!p_render_buffers->has_texture(RB_SCOPE_GI, RB_TEX_AMBIENT)) {
3847 Size2i size = internal_size;
3848 uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
3849
3850 if (half_resolution) {
3851 size.x >>= 1;
3852 size.y >>= 1;
3853 }
3854
3855 p_render_buffers->create_texture(RB_SCOPE_GI, RB_TEX_AMBIENT, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, usage_bits, RD::TEXTURE_SAMPLES_1, size);
3856 p_render_buffers->create_texture(RB_SCOPE_GI, RB_TEX_REFLECTION, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, usage_bits, RD::TEXTURE_SAMPLES_1, size);
3857
3858 rbgi->using_half_size_gi = half_resolution;
3859 }
3860
3861 // Setup our scene data
3862 {
3863 SceneData scene_data;
3864
3865 if (rbgi->scene_data_ubo.is_null()) {
3866 rbgi->scene_data_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SceneData));
3867 }
3868
3869 Projection correction;
3870 correction.set_depth_correction(false);
3871
3872 for (uint32_t v = 0; v < p_view_count; v++) {
3873 Projection temp = correction * p_projections[v];
3874
3875 RendererRD::MaterialStorage::store_camera(temp.inverse(), scene_data.inv_projection[v]);
3876 scene_data.eye_offset[v][0] = p_eye_offsets[v].x;
3877 scene_data.eye_offset[v][1] = p_eye_offsets[v].y;
3878 scene_data.eye_offset[v][2] = p_eye_offsets[v].z;
3879 scene_data.eye_offset[v][3] = 0.0;
3880 }
3881
3882 // Note that we will be ignoring the origin of this transform.
3883 RendererRD::MaterialStorage::store_transform(p_cam_transform, scene_data.cam_transform);
3884
3885 scene_data.screen_size[0] = internal_size.x;
3886 scene_data.screen_size[1] = internal_size.y;
3887
3888 RD::get_singleton()->buffer_update(rbgi->scene_data_ubo, 0, sizeof(SceneData), &scene_data);
3889 }
3890
3891 // Now compute the contents of our buffers.
3892 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
3893
3894 // Render each eye separately.
3895 // We need to look into whether we can make our compute shader use Multiview but not sure that works or makes a difference..
3896
3897 // setup our push constant
3898
3899 PushConstant push_constant;
3900
3901 push_constant.max_voxel_gi_instances = MIN((uint64_t)MAX_VOXEL_GI_INSTANCES, p_voxel_gi_instances.size());
3902 push_constant.high_quality_vct = voxel_gi_quality == RS::VOXEL_GI_QUALITY_HIGH;
3903
3904 // these should be the same for all views
3905 push_constant.orthogonal = p_projections[0].is_orthogonal();
3906 push_constant.z_near = p_projections[0].get_z_near();
3907 push_constant.z_far = p_projections[0].get_z_far();
3908
3909 // these are only used if we have 1 view, else we use the projections in our scene data
3910 push_constant.proj_info[0] = -2.0f / (internal_size.x * p_projections[0].columns[0][0]);
3911 push_constant.proj_info[1] = -2.0f / (internal_size.y * p_projections[0].columns[1][1]);
3912 push_constant.proj_info[2] = (1.0f - p_projections[0].columns[0][2]) / p_projections[0].columns[0][0];
3913 push_constant.proj_info[3] = (1.0f + p_projections[0].columns[1][2]) / p_projections[0].columns[1][1];
3914
3915 bool use_sdfgi = p_render_buffers->has_custom_data(RB_SCOPE_SDFGI);
3916 bool use_voxel_gi_instances = push_constant.max_voxel_gi_instances > 0;
3917
3918 Ref<SDFGI> sdfgi;
3919 if (use_sdfgi) {
3920 sdfgi = p_render_buffers->get_custom_data(RB_SCOPE_SDFGI);
3921 }
3922
3923 uint32_t pipeline_specialization = 0;
3924 if (rbgi->using_half_size_gi) {
3925 pipeline_specialization |= SHADER_SPECIALIZATION_HALF_RES;
3926 }
3927 if (p_view_count > 1) {
3928 pipeline_specialization |= SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX;
3929 }
3930 bool has_vrs_texture = p_render_buffers->has_texture(RB_SCOPE_VRS, RB_TEXTURE);
3931 if (has_vrs_texture) {
3932 pipeline_specialization |= SHADER_SPECIALIZATION_USE_VRS;
3933 }
3934
3935 bool without_sampler = RD::get_singleton()->sampler_is_format_supported_for_filter(RD::DATA_FORMAT_R8G8_UINT, RD::SAMPLER_FILTER_LINEAR);
3936 Mode mode;
3937 if (use_sdfgi && use_voxel_gi_instances) {
3938 mode = without_sampler ? MODE_COMBINED_WITHOUT_SAMPLER : MODE_COMBINED;
3939 } else if (use_sdfgi) {
3940 mode = MODE_SDFGI;
3941 } else {
3942 mode = without_sampler ? MODE_VOXEL_GI_WITHOUT_SAMPLER : MODE_VOXEL_GI;
3943 }
3944
3945 for (uint32_t v = 0; v < p_view_count; v++) {
3946 push_constant.view_index = v;
3947
3948 // setup our uniform set
3949 if (rbgi->uniform_set[v].is_null() || !RD::get_singleton()->uniform_set_is_valid(rbgi->uniform_set[v])) {
3950 Vector<RD::Uniform> uniforms;
3951 {
3952 RD::Uniform u;
3953 u.binding = 1;
3954 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3955 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
3956 if (use_sdfgi && j < sdfgi->cascades.size()) {
3957 u.append_id(sdfgi->cascades[j].sdf_tex);
3958 } else {
3959 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3960 }
3961 }
3962 uniforms.push_back(u);
3963 }
3964 {
3965 RD::Uniform u;
3966 u.binding = 2;
3967 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3968 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
3969 if (use_sdfgi && j < sdfgi->cascades.size()) {
3970 u.append_id(sdfgi->cascades[j].light_tex);
3971 } else {
3972 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3973 }
3974 }
3975 uniforms.push_back(u);
3976 }
3977 {
3978 RD::Uniform u;
3979 u.binding = 3;
3980 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3981 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
3982 if (use_sdfgi && j < sdfgi->cascades.size()) {
3983 u.append_id(sdfgi->cascades[j].light_aniso_0_tex);
3984 } else {
3985 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3986 }
3987 }
3988 uniforms.push_back(u);
3989 }
3990 {
3991 RD::Uniform u;
3992 u.binding = 4;
3993 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3994 for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
3995 if (use_sdfgi && j < sdfgi->cascades.size()) {
3996 u.append_id(sdfgi->cascades[j].light_aniso_1_tex);
3997 } else {
3998 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3999 }
4000 }
4001 uniforms.push_back(u);
4002 }
4003 {
4004 RD::Uniform u;
4005 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4006 u.binding = 5;
4007 if (use_sdfgi) {
4008 u.append_id(sdfgi->occlusion_texture);
4009 } else {
4010 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
4011 }
4012 uniforms.push_back(u);
4013 }
4014 {
4015 RD::Uniform u;
4016 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
4017 u.binding = 6;
4018 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
4019 uniforms.push_back(u);
4020 }
4021 {
4022 RD::Uniform u;
4023 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
4024 u.binding = 7;
4025 u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
4026 uniforms.push_back(u);
4027 }
4028 {
4029 RD::Uniform u;
4030 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
4031 u.binding = 9;
4032 u.append_id(p_render_buffers->get_texture_slice(RB_SCOPE_GI, RB_TEX_AMBIENT, v, 0));
4033 uniforms.push_back(u);
4034 }
4035
4036 {
4037 RD::Uniform u;
4038 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
4039 u.binding = 10;
4040 u.append_id(p_render_buffers->get_texture_slice(RB_SCOPE_GI, RB_TEX_REFLECTION, v, 0));
4041 uniforms.push_back(u);
4042 }
4043
4044 {
4045 RD::Uniform u;
4046 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4047 u.binding = 11;
4048 if (use_sdfgi) {
4049 u.append_id(sdfgi->lightprobe_texture);
4050 } else {
4051 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE));
4052 }
4053 uniforms.push_back(u);
4054 }
4055 {
4056 RD::Uniform u;
4057 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4058 u.binding = 12;
4059 u.append_id(p_render_buffers->get_depth_texture(v));
4060 uniforms.push_back(u);
4061 }
4062 {
4063 RD::Uniform u;
4064 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4065 u.binding = 13;
4066 u.append_id(p_normal_roughness_slices[v]);
4067 uniforms.push_back(u);
4068 }
4069 {
4070 RD::Uniform u;
4071 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4072 u.binding = 14;
4073 RID buffer = p_voxel_gi_buffer.is_valid() ? p_voxel_gi_buffer : texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK);
4074 u.append_id(buffer);
4075 uniforms.push_back(u);
4076 }
4077 {
4078 RD::Uniform u;
4079 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
4080 u.binding = 15;
4081 u.append_id(sdfgi_ubo);
4082 uniforms.push_back(u);
4083 }
4084 {
4085 RD::Uniform u;
4086 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
4087 u.binding = 16;
4088 u.append_id(rbgi->get_voxel_gi_buffer());
4089 uniforms.push_back(u);
4090 }
4091 {
4092 RD::Uniform u;
4093 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4094 u.binding = 17;
4095 for (int i = 0; i < MAX_VOXEL_GI_INSTANCES; i++) {
4096 u.append_id(rbgi->voxel_gi_textures[i]);
4097 }
4098 uniforms.push_back(u);
4099 }
4100 {
4101 RD::Uniform u;
4102 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
4103 u.binding = 18;
4104 u.append_id(rbgi->scene_data_ubo);
4105 uniforms.push_back(u);
4106 }
4107 if (RendererSceneRenderRD::get_singleton()->is_vrs_supported()) {
4108 RD::Uniform u;
4109 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
4110 u.binding = 19;
4111 RID buffer = has_vrs_texture ? p_render_buffers->get_texture_slice(RB_SCOPE_VRS, RB_TEXTURE, v, 0) : texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_VRS);
4112 u.append_id(buffer);
4113 uniforms.push_back(u);
4114 }
4115
4116 bool vrs_supported = RendererSceneRenderRD::get_singleton()->is_vrs_supported();
4117 int variant_base = vrs_supported ? MODE_MAX : 0;
4118 rbgi->uniform_set[v] = RD::get_singleton()->uniform_set_create(uniforms, shader.version_get_shader(shader_version, variant_base), 0);
4119 }
4120
4121 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipelines[pipeline_specialization][mode].get_rid());
4122 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rbgi->uniform_set[v], 0);
4123 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
4124
4125 if (rbgi->using_half_size_gi) {
4126 RD::get_singleton()->compute_list_dispatch_threads(compute_list, internal_size.x >> 1, internal_size.y >> 1, 1);
4127 } else {
4128 RD::get_singleton()->compute_list_dispatch_threads(compute_list, internal_size.x, internal_size.y, 1);
4129 }
4130 }
4131
4132 RD::get_singleton()->compute_list_end();
4133 RD::get_singleton()->draw_command_end_label();
4134 }
4135
4136 RID GI::voxel_gi_instance_create(RID p_base) {
4137 VoxelGIInstance voxel_gi;
4138 voxel_gi.gi = this;
4139 voxel_gi.probe = p_base;
4140 RID rid = voxel_gi_instance_owner.make_rid(voxel_gi);
4141 return rid;
4142 }
4143
4144 void GI::voxel_gi_instance_free(RID p_rid) {
4145 GI::VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_rid);
4146 voxel_gi->free_resources();
4147 voxel_gi_instance_owner.free(p_rid);
4148 }
4149
4150 void GI::voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) {
4151 VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_probe);
4152 ERR_FAIL_NULL(voxel_gi);
4153
4154 voxel_gi->transform = p_xform;
4155 }
4156
4157 bool GI::voxel_gi_needs_update(RID p_probe) const {
4158 VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_probe);
4159 ERR_FAIL_NULL_V(voxel_gi, false);
4160
4161 return voxel_gi->last_probe_version != voxel_gi_get_version(voxel_gi->probe);
4162 }
4163
4164 void GI::voxel_gi_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects) {
4165 VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_probe);
4166 ERR_FAIL_NULL(voxel_gi);
4167
4168 voxel_gi->update(p_update_light_instances, p_light_instances, p_dynamic_objects);
4169 }
4170
4171 void GI::debug_voxel_gi(RID p_voxel_gi, RD::DrawListID p_draw_list, RID p_framebuffer, const Projection &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha) {
4172 VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_voxel_gi);
4173 ERR_FAIL_NULL(voxel_gi);
4174
4175 voxel_gi->debug(p_draw_list, p_framebuffer, p_camera_with_transform, p_lighting, p_emission, p_alpha);
4176 }
4177
4178 void GI::enable_vrs_shader_group() {
4179 shader.enable_group(GROUP_VRS);
4180 }