Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/environment/gi.cpp
10279 views
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(voxel_gi->octree_buffer);
71
RD::get_singleton()->free(voxel_gi->data_buffer);
72
if (voxel_gi->sdf_texture.is_valid()) {
73
RD::get_singleton()->free(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(c.light_data);
1130
RD::get_singleton()->free(c.light_aniso_0_tex);
1131
RD::get_singleton()->free(c.light_aniso_1_tex);
1132
RD::get_singleton()->free(c.sdf_tex);
1133
RD::get_singleton()->free(c.solid_cell_dispatch_buffer_storage);
1134
RD::get_singleton()->free(c.solid_cell_dispatch_buffer_call);
1135
RD::get_singleton()->free(c.solid_cell_buffer);
1136
RD::get_singleton()->free(c.lightprobe_history_tex);
1137
RD::get_singleton()->free(c.lightprobe_average_tex);
1138
RD::get_singleton()->free(c.lights_buffer);
1139
}
1140
1141
RD::get_singleton()->free(render_albedo);
1142
RD::get_singleton()->free(render_emission);
1143
RD::get_singleton()->free(render_emission_aniso);
1144
1145
RD::get_singleton()->free(render_sdf[0]);
1146
RD::get_singleton()->free(render_sdf[1]);
1147
1148
RD::get_singleton()->free(render_sdf_half[0]);
1149
RD::get_singleton()->free(render_sdf_half[1]);
1150
1151
for (int i = 0; i < 8; i++) {
1152
RD::get_singleton()->free(render_occlusion[i]);
1153
}
1154
1155
RD::get_singleton()->free(render_geom_facing);
1156
1157
RD::get_singleton()->free(lightprobe_data);
1158
RD::get_singleton()->free(lightprobe_history_scroll);
1159
RD::get_singleton()->free(lightprobe_average_scroll);
1160
RD::get_singleton()->free(occlusion_data);
1161
RD::get_singleton()->free(ambient_texture);
1162
1163
RD::get_singleton()->free(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(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(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(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]);
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
RID sky_uniform_set = gi->sdfgi_shader.integrate_default_sky_uniform_set;
1310
push_constant.sky_flags = 0;
1311
push_constant.y_mult = y_mult;
1312
1313
if (reads_sky && p_env.is_valid()) {
1314
push_constant.sky_energy = RendererSceneRenderRD::get_singleton()->environment_get_bg_energy_multiplier(p_env);
1315
1316
if (RendererSceneRenderRD::get_singleton()->environment_get_background(p_env) == RS::ENV_BG_CLEAR_COLOR) {
1317
push_constant.sky_flags |= SDFGIShader::IntegratePushConstant::SKY_FLAGS_MODE_COLOR;
1318
Color c = RSG::texture_storage->get_default_clear_color().srgb_to_linear();
1319
push_constant.sky_color_or_orientation[0] = c.r;
1320
push_constant.sky_color_or_orientation[1] = c.g;
1321
push_constant.sky_color_or_orientation[2] = c.b;
1322
} else if (RendererSceneRenderRD::get_singleton()->environment_get_background(p_env) == RS::ENV_BG_COLOR) {
1323
push_constant.sky_flags |= SDFGIShader::IntegratePushConstant::SKY_FLAGS_MODE_COLOR;
1324
Color c = RendererSceneRenderRD::get_singleton()->environment_get_bg_color(p_env);
1325
push_constant.sky_color_or_orientation[0] = c.r;
1326
push_constant.sky_color_or_orientation[1] = c.g;
1327
push_constant.sky_color_or_orientation[2] = c.b;
1328
1329
} else if (RendererSceneRenderRD::get_singleton()->environment_get_background(p_env) == RS::ENV_BG_SKY) {
1330
if (p_sky && p_sky->radiance.is_valid()) {
1331
if (integrate_sky_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(integrate_sky_uniform_set)) {
1332
Vector<RD::Uniform> uniforms;
1333
1334
{
1335
RD::Uniform u;
1336
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1337
u.binding = 0;
1338
u.append_id(p_sky->radiance);
1339
uniforms.push_back(u);
1340
}
1341
1342
{
1343
RD::Uniform u;
1344
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
1345
u.binding = 1;
1346
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));
1347
uniforms.push_back(u);
1348
}
1349
1350
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);
1351
}
1352
sky_uniform_set = integrate_sky_uniform_set;
1353
push_constant.sky_flags |= SDFGIShader::IntegratePushConstant::SKY_FLAGS_MODE_SKY;
1354
1355
// Encode sky orientation as quaternion in existing push constants.
1356
const Basis sky_basis = RendererSceneRenderRD::get_singleton()->environment_get_sky_orientation(p_env);
1357
const Quaternion sky_quaternion = sky_basis.get_quaternion().inverse();
1358
push_constant.sky_color_or_orientation[0] = sky_quaternion.x;
1359
push_constant.sky_color_or_orientation[1] = sky_quaternion.y;
1360
push_constant.sky_color_or_orientation[2] = sky_quaternion.z;
1361
// 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.
1362
push_constant.sky_flags |= SDFGIShader::IntegratePushConstant::SKY_FLAGS_ORIENTATION_SIGN * (sky_quaternion.w < 0.0 ? 0 : 1);
1363
}
1364
}
1365
}
1366
1367
render_pass++;
1368
1369
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1370
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_PROCESS]);
1371
1372
int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
1373
for (uint32_t i = 0; i < cascades.size(); i++) {
1374
push_constant.cascade = i;
1375
push_constant.world_offset[0] = cascades[i].position.x / probe_divisor;
1376
push_constant.world_offset[1] = cascades[i].position.y / probe_divisor;
1377
push_constant.world_offset[2] = cascades[i].position.z / probe_divisor;
1378
1379
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[i].integrate_uniform_set, 0);
1380
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sky_uniform_set, 1);
1381
1382
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::IntegratePushConstant));
1383
RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
1384
}
1385
1386
RD::get_singleton()->compute_list_end();
1387
RD::get_singleton()->draw_command_end_label();
1388
}
1389
1390
void GI::SDFGI::store_probes() {
1391
RD::get_singleton()->draw_command_begin_label("SDFGI Store Probes");
1392
1393
SDFGIShader::IntegratePushConstant push_constant;
1394
push_constant.grid_size[1] = cascade_size;
1395
push_constant.grid_size[2] = cascade_size;
1396
push_constant.grid_size[0] = cascade_size;
1397
push_constant.max_cascades = cascades.size();
1398
push_constant.probe_axis_size = probe_axis_count;
1399
push_constant.history_index = render_pass % history_size;
1400
push_constant.history_size = history_size;
1401
static const uint32_t ray_count[RS::ENV_SDFGI_RAY_COUNT_MAX] = { 4, 8, 16, 32, 64, 96, 128 };
1402
push_constant.ray_count = ray_count[gi->sdfgi_ray_count];
1403
push_constant.ray_bias = probe_bias;
1404
push_constant.image_size[0] = probe_axis_count * probe_axis_count;
1405
push_constant.image_size[1] = probe_axis_count;
1406
push_constant.store_ambient_texture = false;
1407
1408
push_constant.sky_flags = 0;
1409
push_constant.y_mult = y_mult;
1410
1411
// Then store values into the lightprobe texture. Separating these steps has a small performance hit, but it allows for multiple bounces
1412
RENDER_TIMESTAMP("Average SDFGI Probes");
1413
1414
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1415
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_STORE]);
1416
1417
//convert to octahedral to store
1418
push_constant.image_size[0] *= SDFGI::LIGHTPROBE_OCT_SIZE;
1419
push_constant.image_size[1] *= SDFGI::LIGHTPROBE_OCT_SIZE;
1420
1421
for (uint32_t i = 0; i < cascades.size(); i++) {
1422
push_constant.cascade = i;
1423
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[i].integrate_uniform_set, 0);
1424
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
1425
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::IntegratePushConstant));
1426
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);
1427
}
1428
1429
RD::get_singleton()->compute_list_end();
1430
1431
RD::get_singleton()->draw_command_end_label();
1432
}
1433
1434
int GI::SDFGI::get_pending_region_data(int p_region, Vector3i &r_local_offset, Vector3i &r_local_size, AABB &r_bounds) const {
1435
int dirty_count = 0;
1436
for (uint32_t i = 0; i < cascades.size(); i++) {
1437
const SDFGI::Cascade &c = cascades[i];
1438
1439
if (c.dirty_regions == SDFGI::Cascade::DIRTY_ALL) {
1440
if (dirty_count == p_region) {
1441
r_local_offset = Vector3i();
1442
r_local_size = Vector3i(1, 1, 1) * cascade_size;
1443
1444
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);
1445
r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
1446
return i;
1447
}
1448
dirty_count++;
1449
} else {
1450
for (int j = 0; j < 3; j++) {
1451
if (c.dirty_regions[j] != 0) {
1452
if (dirty_count == p_region) {
1453
Vector3i from = Vector3i(0, 0, 0);
1454
Vector3i to = Vector3i(1, 1, 1) * cascade_size;
1455
1456
if (c.dirty_regions[j] > 0) {
1457
//fill from the beginning
1458
to[j] = c.dirty_regions[j];
1459
} else {
1460
//fill from the end
1461
from[j] = to[j] + c.dirty_regions[j];
1462
}
1463
1464
for (int k = 0; k < j; k++) {
1465
// "chip" away previous regions to avoid re-voxelizing the same thing
1466
if (c.dirty_regions[k] > 0) {
1467
from[k] += c.dirty_regions[k];
1468
} else if (c.dirty_regions[k] < 0) {
1469
to[k] += c.dirty_regions[k];
1470
}
1471
}
1472
1473
r_local_offset = from;
1474
r_local_size = to - from;
1475
1476
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);
1477
r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
1478
1479
return i;
1480
}
1481
1482
dirty_count++;
1483
}
1484
}
1485
}
1486
}
1487
return -1;
1488
}
1489
1490
void GI::SDFGI::update_cascades() {
1491
//update cascades
1492
SDFGI::Cascade::UBO cascade_data[SDFGI::MAX_CASCADES];
1493
int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
1494
1495
for (uint32_t i = 0; i < cascades.size(); i++) {
1496
Vector3 pos = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascades[i].position)) * cascades[i].cell_size;
1497
1498
cascade_data[i].offset[0] = pos.x;
1499
cascade_data[i].offset[1] = pos.y;
1500
cascade_data[i].offset[2] = pos.z;
1501
cascade_data[i].to_cell = 1.0 / cascades[i].cell_size;
1502
cascade_data[i].probe_offset[0] = cascades[i].position.x / probe_divisor;
1503
cascade_data[i].probe_offset[1] = cascades[i].position.y / probe_divisor;
1504
cascade_data[i].probe_offset[2] = cascades[i].position.z / probe_divisor;
1505
cascade_data[i].pad = 0;
1506
}
1507
1508
RD::get_singleton()->buffer_update(cascades_ubo, 0, sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES, cascade_data);
1509
}
1510
1511
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) {
1512
RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
1513
RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
1514
RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
1515
1516
for (uint32_t v = 0; v < p_view_count; v++) {
1517
if (!debug_uniform_set[v].is_valid() || !RD::get_singleton()->uniform_set_is_valid(debug_uniform_set[v])) {
1518
Vector<RD::Uniform> uniforms;
1519
{
1520
RD::Uniform u;
1521
u.binding = 1;
1522
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1523
for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
1524
if (i < cascades.size()) {
1525
u.append_id(cascades[i].sdf_tex);
1526
} else {
1527
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1528
}
1529
}
1530
uniforms.push_back(u);
1531
}
1532
{
1533
RD::Uniform u;
1534
u.binding = 2;
1535
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1536
for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
1537
if (i < cascades.size()) {
1538
u.append_id(cascades[i].light_tex);
1539
} else {
1540
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1541
}
1542
}
1543
uniforms.push_back(u);
1544
}
1545
{
1546
RD::Uniform u;
1547
u.binding = 3;
1548
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1549
for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
1550
if (i < cascades.size()) {
1551
u.append_id(cascades[i].light_aniso_0_tex);
1552
} else {
1553
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1554
}
1555
}
1556
uniforms.push_back(u);
1557
}
1558
{
1559
RD::Uniform u;
1560
u.binding = 4;
1561
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1562
for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
1563
if (i < cascades.size()) {
1564
u.append_id(cascades[i].light_aniso_1_tex);
1565
} else {
1566
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
1567
}
1568
}
1569
uniforms.push_back(u);
1570
}
1571
{
1572
RD::Uniform u;
1573
u.binding = 5;
1574
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1575
u.append_id(occlusion_texture);
1576
uniforms.push_back(u);
1577
}
1578
{
1579
RD::Uniform u;
1580
u.binding = 8;
1581
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
1582
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
1583
uniforms.push_back(u);
1584
}
1585
{
1586
RD::Uniform u;
1587
u.binding = 9;
1588
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1589
u.append_id(cascades_ubo);
1590
uniforms.push_back(u);
1591
}
1592
{
1593
RD::Uniform u;
1594
u.binding = 10;
1595
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1596
u.append_id(p_texture_views[v]);
1597
uniforms.push_back(u);
1598
}
1599
{
1600
RD::Uniform u;
1601
u.binding = 11;
1602
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1603
u.append_id(lightprobe_texture);
1604
uniforms.push_back(u);
1605
}
1606
debug_uniform_set[v] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.debug_shader_version, 0);
1607
}
1608
1609
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1610
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.debug_pipeline);
1611
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, debug_uniform_set[v], 0);
1612
1613
SDFGIShader::DebugPushConstant push_constant;
1614
push_constant.grid_size[0] = cascade_size;
1615
push_constant.grid_size[1] = cascade_size;
1616
push_constant.grid_size[2] = cascade_size;
1617
push_constant.max_cascades = cascades.size();
1618
push_constant.screen_size[0] = p_width;
1619
push_constant.screen_size[1] = p_height;
1620
push_constant.y_mult = y_mult;
1621
1622
push_constant.z_near = -p_projections[v].get_z_near();
1623
1624
for (int i = 0; i < 3; i++) {
1625
for (int j = 0; j < 3; j++) {
1626
push_constant.cam_basis[i][j] = p_transform.basis.rows[j][i];
1627
}
1628
}
1629
push_constant.cam_origin[0] = p_transform.origin[0];
1630
push_constant.cam_origin[1] = p_transform.origin[1];
1631
push_constant.cam_origin[2] = p_transform.origin[2];
1632
1633
// need to properly unproject for asymmetric projection matrices in stereo..
1634
Projection inv_projection = p_projections[v].inverse();
1635
for (int i = 0; i < 4; i++) {
1636
for (int j = 0; j < 3; j++) {
1637
push_constant.inv_projection[j][i] = inv_projection.columns[i][j];
1638
}
1639
}
1640
1641
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::DebugPushConstant));
1642
1643
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_width, p_height, 1);
1644
RD::get_singleton()->compute_list_end();
1645
}
1646
1647
Size2i rtsize = texture_storage->render_target_get_size(p_render_target);
1648
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);
1649
}
1650
1651
void GI::SDFGI::debug_probes(RID p_framebuffer, const uint32_t p_view_count, const Projection *p_camera_with_transforms) {
1652
RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
1653
1654
// setup scene data
1655
{
1656
SDFGIShader::DebugProbesSceneData scene_data;
1657
1658
if (debug_probes_scene_data_ubo.is_null()) {
1659
debug_probes_scene_data_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGIShader::DebugProbesSceneData));
1660
}
1661
1662
for (uint32_t v = 0; v < p_view_count; v++) {
1663
RendererRD::MaterialStorage::store_camera(p_camera_with_transforms[v], scene_data.projection[v]);
1664
}
1665
1666
RD::get_singleton()->buffer_update(debug_probes_scene_data_ubo, 0, sizeof(SDFGIShader::DebugProbesSceneData), &scene_data);
1667
}
1668
1669
// setup push constant
1670
SDFGIShader::DebugProbesPushConstant push_constant;
1671
1672
//gen spheres from strips
1673
uint32_t band_points = 16;
1674
push_constant.band_power = 4;
1675
push_constant.sections_in_band = ((band_points / 2) - 1);
1676
push_constant.band_mask = band_points - 2;
1677
push_constant.section_arc = Math::TAU / float(push_constant.sections_in_band);
1678
push_constant.y_mult = y_mult;
1679
1680
uint32_t total_points = push_constant.sections_in_band * band_points;
1681
uint32_t total_probes = probe_axis_count * probe_axis_count * probe_axis_count;
1682
1683
push_constant.grid_size[0] = cascade_size;
1684
push_constant.grid_size[1] = cascade_size;
1685
push_constant.grid_size[2] = cascade_size;
1686
push_constant.cascade = 0;
1687
1688
push_constant.probe_axis_size = probe_axis_count;
1689
1690
if (!debug_probes_uniform_set.is_valid() || !RD::get_singleton()->uniform_set_is_valid(debug_probes_uniform_set)) {
1691
Vector<RD::Uniform> uniforms;
1692
{
1693
RD::Uniform u;
1694
u.binding = 1;
1695
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1696
u.append_id(cascades_ubo);
1697
uniforms.push_back(u);
1698
}
1699
{
1700
RD::Uniform u;
1701
u.binding = 2;
1702
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1703
u.append_id(lightprobe_texture);
1704
uniforms.push_back(u);
1705
}
1706
{
1707
RD::Uniform u;
1708
u.binding = 3;
1709
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
1710
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
1711
uniforms.push_back(u);
1712
}
1713
{
1714
RD::Uniform u;
1715
u.binding = 4;
1716
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1717
u.append_id(occlusion_texture);
1718
uniforms.push_back(u);
1719
}
1720
{
1721
RD::Uniform u;
1722
u.binding = 5;
1723
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1724
u.append_id(debug_probes_scene_data_ubo);
1725
uniforms.push_back(u);
1726
}
1727
1728
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);
1729
}
1730
1731
SDFGIShader::ProbeDebugMode mode = p_view_count > 1 ? SDFGIShader::PROBE_DEBUG_PROBES_MULTIVIEW : SDFGIShader::PROBE_DEBUG_PROBES;
1732
1733
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer);
1734
RD::get_singleton()->draw_command_begin_label("Debug SDFGI");
1735
1736
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)));
1737
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, debug_probes_uniform_set, 0);
1738
RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(SDFGIShader::DebugProbesPushConstant));
1739
RD::get_singleton()->draw_list_draw(draw_list, false, total_probes, total_points);
1740
1741
if (gi->sdfgi_debug_probe_dir != Vector3()) {
1742
uint32_t cascade = 0;
1743
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);
1744
Vector3 probe_size = cascades[cascade].cell_size * (cascade_size / SDFGI::PROBE_DIVISOR) * Vector3(1.0, 1.0 / y_mult, 1.0);
1745
Vector3 ray_from = gi->sdfgi_debug_probe_pos;
1746
Vector3 ray_to = gi->sdfgi_debug_probe_pos + gi->sdfgi_debug_probe_dir * cascades[cascade].cell_size * Math::sqrt(3.0) * cascade_size;
1747
float sphere_radius = 0.2;
1748
float closest_dist = 1e20;
1749
gi->sdfgi_debug_probe_enabled = false;
1750
1751
Vector3i probe_from = cascades[cascade].position / (cascade_size / SDFGI::PROBE_DIVISOR);
1752
for (int i = 0; i < (SDFGI::PROBE_DIVISOR + 1); i++) {
1753
for (int j = 0; j < (SDFGI::PROBE_DIVISOR + 1); j++) {
1754
for (int k = 0; k < (SDFGI::PROBE_DIVISOR + 1); k++) {
1755
Vector3 pos = offset + probe_size * Vector3(i, j, k);
1756
Vector3 res;
1757
if (Geometry3D::segment_intersects_sphere(ray_from, ray_to, pos, sphere_radius, &res)) {
1758
float d = ray_from.distance_to(res);
1759
if (d < closest_dist) {
1760
closest_dist = d;
1761
gi->sdfgi_debug_probe_enabled = true;
1762
gi->sdfgi_debug_probe_index = probe_from + Vector3i(i, j, k);
1763
}
1764
}
1765
}
1766
}
1767
}
1768
1769
gi->sdfgi_debug_probe_dir = Vector3();
1770
}
1771
1772
if (gi->sdfgi_debug_probe_enabled) {
1773
uint32_t cascade = 0;
1774
uint32_t probe_cells = (cascade_size / SDFGI::PROBE_DIVISOR);
1775
Vector3i probe_from = cascades[cascade].position / probe_cells;
1776
Vector3i ofs = gi->sdfgi_debug_probe_index - probe_from;
1777
if (ofs.x < 0 || ofs.y < 0 || ofs.z < 0) {
1778
return;
1779
}
1780
if (ofs.x > SDFGI::PROBE_DIVISOR || ofs.y > SDFGI::PROBE_DIVISOR || ofs.z > SDFGI::PROBE_DIVISOR) {
1781
return;
1782
}
1783
1784
uint32_t mult = (SDFGI::PROBE_DIVISOR + 1);
1785
uint32_t index = ofs.z * mult * mult + ofs.y * mult + ofs.x;
1786
1787
push_constant.probe_debug_index = index;
1788
1789
uint32_t cell_count = probe_cells * 2 * probe_cells * 2 * probe_cells * 2;
1790
1791
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)));
1792
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, debug_probes_uniform_set, 0);
1793
RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(SDFGIShader::DebugProbesPushConstant));
1794
RD::get_singleton()->draw_list_draw(draw_list, false, cell_count, total_points);
1795
}
1796
1797
RD::get_singleton()->draw_command_end_label();
1798
RD::get_singleton()->draw_list_end();
1799
}
1800
1801
void GI::SDFGI::pre_process_gi(const Transform3D &p_transform, RenderDataRD *p_render_data) {
1802
if (p_render_data->sdfgi_update_data == nullptr) {
1803
return;
1804
}
1805
1806
RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
1807
/* Update general SDFGI Buffer */
1808
1809
SDFGIData sdfgi_data;
1810
1811
sdfgi_data.grid_size[0] = cascade_size;
1812
sdfgi_data.grid_size[1] = cascade_size;
1813
sdfgi_data.grid_size[2] = cascade_size;
1814
1815
sdfgi_data.max_cascades = cascades.size();
1816
sdfgi_data.probe_axis_size = probe_axis_count;
1817
sdfgi_data.cascade_probe_size[0] = sdfgi_data.probe_axis_size - 1; //float version for performance
1818
sdfgi_data.cascade_probe_size[1] = sdfgi_data.probe_axis_size - 1;
1819
sdfgi_data.cascade_probe_size[2] = sdfgi_data.probe_axis_size - 1;
1820
1821
float csize = cascade_size;
1822
sdfgi_data.probe_to_uvw = 1.0 / float(sdfgi_data.cascade_probe_size[0]);
1823
sdfgi_data.use_occlusion = uses_occlusion;
1824
//sdfgi_data.energy = energy;
1825
1826
sdfgi_data.y_mult = y_mult;
1827
1828
float cascade_voxel_size = (csize / sdfgi_data.cascade_probe_size[0]);
1829
float occlusion_clamp = (cascade_voxel_size - 0.5) / cascade_voxel_size;
1830
sdfgi_data.occlusion_clamp[0] = occlusion_clamp;
1831
sdfgi_data.occlusion_clamp[1] = occlusion_clamp;
1832
sdfgi_data.occlusion_clamp[2] = occlusion_clamp;
1833
sdfgi_data.normal_bias = (normal_bias / csize) * sdfgi_data.cascade_probe_size[0];
1834
1835
//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 ) );
1836
//vec3 probe_uv_offset = (ivec3(OCT_SIZE+2,OCT_SIZE+2,(OCT_SIZE+2) * params.probe_axis_size)) * tex_pixel_size.xyx;
1837
1838
uint32_t oct_size = SDFGI::LIGHTPROBE_OCT_SIZE;
1839
1840
sdfgi_data.lightprobe_tex_pixel_size[0] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size * sdfgi_data.probe_axis_size);
1841
sdfgi_data.lightprobe_tex_pixel_size[1] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size);
1842
sdfgi_data.lightprobe_tex_pixel_size[2] = 1.0;
1843
1844
sdfgi_data.energy = energy;
1845
1846
sdfgi_data.lightprobe_uv_offset[0] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[0];
1847
sdfgi_data.lightprobe_uv_offset[1] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[1];
1848
sdfgi_data.lightprobe_uv_offset[2] = float((oct_size + 2) * sdfgi_data.probe_axis_size) * sdfgi_data.lightprobe_tex_pixel_size[0];
1849
1850
sdfgi_data.occlusion_renormalize[0] = 0.5;
1851
sdfgi_data.occlusion_renormalize[1] = 1.0;
1852
sdfgi_data.occlusion_renormalize[2] = 1.0 / float(sdfgi_data.max_cascades);
1853
1854
int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
1855
1856
for (uint32_t i = 0; i < sdfgi_data.max_cascades; i++) {
1857
SDFGIData::ProbeCascadeData &c = sdfgi_data.cascades[i];
1858
Vector3 pos = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascades[i].position)) * cascades[i].cell_size;
1859
Vector3 cam_origin = p_transform.origin;
1860
cam_origin.y *= y_mult;
1861
pos -= cam_origin; //make pos local to camera, to reduce numerical error
1862
c.position[0] = pos.x;
1863
c.position[1] = pos.y;
1864
c.position[2] = pos.z;
1865
c.to_probe = 1.0 / (float(cascade_size) * cascades[i].cell_size / float(probe_axis_count - 1));
1866
1867
Vector3i probe_ofs = cascades[i].position / probe_divisor;
1868
c.probe_world_offset[0] = probe_ofs.x;
1869
c.probe_world_offset[1] = probe_ofs.y;
1870
c.probe_world_offset[2] = probe_ofs.z;
1871
1872
c.to_cell = 1.0 / cascades[i].cell_size;
1873
c.exposure_normalization = 1.0;
1874
if (p_render_data->camera_attributes.is_valid()) {
1875
float exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1876
c.exposure_normalization = exposure_normalization / cascades[i].baked_exposure_normalization;
1877
}
1878
}
1879
1880
RD::get_singleton()->buffer_update(gi->sdfgi_ubo, 0, sizeof(SDFGIData), &sdfgi_data);
1881
1882
/* Update dynamic lights in SDFGI cascades */
1883
1884
for (uint32_t i = 0; i < cascades.size(); i++) {
1885
SDFGI::Cascade &cascade = cascades[i];
1886
1887
SDFGIShader::Light lights[SDFGI::MAX_DYNAMIC_LIGHTS];
1888
uint32_t idx = 0;
1889
for (uint32_t j = 0; j < (uint32_t)p_render_data->sdfgi_update_data->directional_lights->size(); j++) {
1890
if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
1891
break;
1892
}
1893
1894
RID light_instance = p_render_data->sdfgi_update_data->directional_lights->get(j);
1895
ERR_CONTINUE(!light_storage->owns_light_instance(light_instance));
1896
1897
RID light = light_storage->light_instance_get_base_light(light_instance);
1898
Transform3D light_transform = light_storage->light_instance_get_base_transform(light_instance);
1899
1900
if (RSG::light_storage->light_directional_get_sky_mode(light) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
1901
continue;
1902
}
1903
1904
Vector3 dir = -light_transform.basis.get_column(Vector3::AXIS_Z);
1905
dir.y *= y_mult;
1906
dir.normalize();
1907
lights[idx].direction[0] = dir.x;
1908
lights[idx].direction[1] = dir.y;
1909
lights[idx].direction[2] = dir.z;
1910
Color color = RSG::light_storage->light_get_color(light);
1911
color = color.srgb_to_linear();
1912
lights[idx].color[0] = color.r;
1913
lights[idx].color[1] = color.g;
1914
lights[idx].color[2] = color.b;
1915
lights[idx].type = RS::LIGHT_DIRECTIONAL;
1916
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);
1917
if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
1918
lights[idx].energy *= RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INTENSITY);
1919
}
1920
1921
if (p_render_data->camera_attributes.is_valid()) {
1922
lights[idx].energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1923
}
1924
1925
lights[idx].has_shadow = RSG::light_storage->light_has_shadow(light);
1926
1927
idx++;
1928
}
1929
1930
AABB cascade_aabb;
1931
cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascade.position)) * cascade.cell_size;
1932
cascade_aabb.size = Vector3(1, 1, 1) * cascade_size * cascade.cell_size;
1933
1934
for (uint32_t j = 0; j < p_render_data->sdfgi_update_data->positional_light_count; j++) {
1935
if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
1936
break;
1937
}
1938
1939
RID light_instance = p_render_data->sdfgi_update_data->positional_light_instances[j];
1940
ERR_CONTINUE(!light_storage->owns_light_instance(light_instance));
1941
1942
RID light = light_storage->light_instance_get_base_light(light_instance);
1943
AABB light_aabb = light_storage->light_instance_get_base_aabb(light_instance);
1944
Transform3D light_transform = light_storage->light_instance_get_base_transform(light_instance);
1945
1946
uint32_t max_sdfgi_cascade = RSG::light_storage->light_get_max_sdfgi_cascade(light);
1947
if (i > max_sdfgi_cascade) {
1948
continue;
1949
}
1950
1951
if (!cascade_aabb.intersects(light_aabb)) {
1952
continue;
1953
}
1954
1955
Vector3 dir = -light_transform.basis.get_column(Vector3::AXIS_Z);
1956
//faster to not do this here
1957
//dir.y *= y_mult;
1958
//dir.normalize();
1959
lights[idx].direction[0] = dir.x;
1960
lights[idx].direction[1] = dir.y;
1961
lights[idx].direction[2] = dir.z;
1962
Vector3 pos = light_transform.origin;
1963
pos.y *= y_mult;
1964
lights[idx].position[0] = pos.x;
1965
lights[idx].position[1] = pos.y;
1966
lights[idx].position[2] = pos.z;
1967
Color color = RSG::light_storage->light_get_color(light);
1968
color = color.srgb_to_linear();
1969
lights[idx].color[0] = color.r;
1970
lights[idx].color[1] = color.g;
1971
lights[idx].color[2] = color.b;
1972
lights[idx].type = RSG::light_storage->light_get_type(light);
1973
1974
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);
1975
if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
1976
lights[idx].energy *= RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INTENSITY);
1977
1978
// Convert from Luminous Power to Luminous Intensity
1979
if (lights[idx].type == RS::LIGHT_OMNI) {
1980
lights[idx].energy *= 1.0 / (Math::PI * 4.0);
1981
} else if (lights[idx].type == RS::LIGHT_SPOT) {
1982
// Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
1983
// We make this assumption to keep them easy to control.
1984
lights[idx].energy *= 1.0 / Math::PI;
1985
}
1986
}
1987
1988
if (p_render_data->camera_attributes.is_valid()) {
1989
lights[idx].energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1990
}
1991
1992
lights[idx].has_shadow = RSG::light_storage->light_has_shadow(light);
1993
lights[idx].attenuation = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
1994
lights[idx].radius = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_RANGE);
1995
lights[idx].cos_spot_angle = Math::cos(Math::deg_to_rad(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE)));
1996
lights[idx].inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
1997
1998
idx++;
1999
}
2000
2001
if (idx > 0) {
2002
RD::get_singleton()->buffer_update(cascade.lights_buffer, 0, idx * sizeof(SDFGIShader::Light), lights);
2003
}
2004
2005
cascade_dynamic_light_count[i] = idx;
2006
}
2007
}
2008
2009
void GI::SDFGI::render_region(Ref<RenderSceneBuffersRD> p_render_buffers, int p_region, const PagedArray<RenderGeometryInstance *> &p_instances, float p_exposure_normalization) {
2010
//print_line("rendering region " + itos(p_region));
2011
ERR_FAIL_COND(p_render_buffers.is_null()); // we wouldn't be here if this failed but...
2012
AABB bounds;
2013
Vector3i from;
2014
Vector3i size;
2015
2016
int cascade_prev = get_pending_region_data(p_region - 1, from, size, bounds);
2017
int cascade_next = get_pending_region_data(p_region + 1, from, size, bounds);
2018
int cascade = get_pending_region_data(p_region, from, size, bounds);
2019
ERR_FAIL_COND(cascade < 0);
2020
2021
if (cascade_prev != cascade) {
2022
//initialize render
2023
RD::get_singleton()->texture_clear(render_albedo, Color(0, 0, 0, 0), 0, 1, 0, 1);
2024
RD::get_singleton()->texture_clear(render_emission, Color(0, 0, 0, 0), 0, 1, 0, 1);
2025
RD::get_singleton()->texture_clear(render_emission_aniso, Color(0, 0, 0, 0), 0, 1, 0, 1);
2026
RD::get_singleton()->texture_clear(render_geom_facing, Color(0, 0, 0, 0), 0, 1, 0, 1);
2027
}
2028
2029
//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));
2030
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);
2031
2032
if (cascade_next != cascade) {
2033
RD::get_singleton()->draw_command_begin_label("SDFGI Pre-Process Cascade");
2034
2035
RENDER_TIMESTAMP("> SDFGI Update SDF");
2036
//done rendering! must update SDF
2037
//clear dispatch indirect data
2038
2039
SDFGIShader::PreprocessPushConstant push_constant;
2040
memset(&push_constant, 0, sizeof(SDFGIShader::PreprocessPushConstant));
2041
2042
RENDER_TIMESTAMP("SDFGI Scroll SDF");
2043
2044
//scroll
2045
if (cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
2046
//for scroll
2047
Vector3i dirty = cascades[cascade].dirty_regions;
2048
push_constant.scroll[0] = dirty.x;
2049
push_constant.scroll[1] = dirty.y;
2050
push_constant.scroll[2] = dirty.z;
2051
} else {
2052
//for no scroll
2053
push_constant.scroll[0] = 0;
2054
push_constant.scroll[1] = 0;
2055
push_constant.scroll[2] = 0;
2056
}
2057
2058
cascades[cascade].all_dynamic_lights_dirty = true;
2059
cascades[cascade].baked_exposure_normalization = p_exposure_normalization;
2060
2061
push_constant.grid_size = cascade_size;
2062
push_constant.cascade = cascade;
2063
2064
if (cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
2065
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);
2066
2067
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
2068
2069
//must pre scroll existing data because not all is dirty
2070
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_SCROLL]);
2071
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].scroll_uniform_set, 0);
2072
2073
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2074
RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cascades[cascade].solid_cell_dispatch_buffer_call, 0);
2075
// no barrier do all together
2076
2077
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_SCROLL_OCCLUSION]);
2078
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].scroll_occlusion_uniform_set, 0);
2079
2080
Vector3i dirty = cascades[cascade].dirty_regions;
2081
Vector3i groups;
2082
groups.x = cascade_size - Math::abs(dirty.x);
2083
groups.y = cascade_size - Math::abs(dirty.y);
2084
groups.z = cascade_size - Math::abs(dirty.z);
2085
2086
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2087
RD::get_singleton()->compute_list_dispatch_threads(compute_list, groups.x, groups.y, groups.z);
2088
2089
//no barrier, continue together
2090
2091
{
2092
//scroll probes and their history also
2093
2094
SDFGIShader::IntegratePushConstant ipush_constant;
2095
ipush_constant.grid_size[1] = cascade_size;
2096
ipush_constant.grid_size[2] = cascade_size;
2097
ipush_constant.grid_size[0] = cascade_size;
2098
ipush_constant.max_cascades = cascades.size();
2099
ipush_constant.probe_axis_size = probe_axis_count;
2100
ipush_constant.history_index = 0;
2101
ipush_constant.history_size = history_size;
2102
ipush_constant.ray_count = 0;
2103
ipush_constant.ray_bias = 0;
2104
ipush_constant.sky_flags = 0;
2105
ipush_constant.sky_energy = 0;
2106
ipush_constant.sky_color_or_orientation[0] = 0;
2107
ipush_constant.sky_color_or_orientation[1] = 0;
2108
ipush_constant.sky_color_or_orientation[2] = 0;
2109
ipush_constant.y_mult = y_mult;
2110
ipush_constant.store_ambient_texture = false;
2111
2112
ipush_constant.image_size[0] = probe_axis_count * probe_axis_count;
2113
ipush_constant.image_size[1] = probe_axis_count;
2114
2115
int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
2116
ipush_constant.cascade = cascade;
2117
ipush_constant.world_offset[0] = cascades[cascade].position.x / probe_divisor;
2118
ipush_constant.world_offset[1] = cascades[cascade].position.y / probe_divisor;
2119
ipush_constant.world_offset[2] = cascades[cascade].position.z / probe_divisor;
2120
2121
ipush_constant.scroll[0] = dirty.x / probe_divisor;
2122
ipush_constant.scroll[1] = dirty.y / probe_divisor;
2123
ipush_constant.scroll[2] = dirty.z / probe_divisor;
2124
2125
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_SCROLL]);
2126
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
2127
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
2128
RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
2129
RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
2130
2131
RD::get_singleton()->compute_list_add_barrier(compute_list);
2132
2133
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_SCROLL_STORE]);
2134
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
2135
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
2136
RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
2137
RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
2138
2139
RD::get_singleton()->compute_list_add_barrier(compute_list);
2140
2141
if (bounce_feedback > 0.0) {
2142
//multibounce requires this to be stored so direct light can read from it
2143
2144
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_STORE]);
2145
2146
//convert to octahedral to store
2147
ipush_constant.image_size[0] *= SDFGI::LIGHTPROBE_OCT_SIZE;
2148
ipush_constant.image_size[1] *= SDFGI::LIGHTPROBE_OCT_SIZE;
2149
2150
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
2151
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
2152
RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
2153
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);
2154
}
2155
}
2156
2157
//ok finally barrier
2158
RD::get_singleton()->compute_list_end();
2159
}
2160
2161
//clear dispatch indirect data
2162
uint32_t dispatch_indirect_data[4] = { 0, 0, 0, 0 };
2163
RD::get_singleton()->buffer_update(cascades[cascade].solid_cell_dispatch_buffer_storage, 0, sizeof(uint32_t) * 4, dispatch_indirect_data);
2164
2165
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
2166
2167
bool half_size = true; //much faster, very little difference
2168
static const int optimized_jf_group_size = 8;
2169
2170
if (half_size) {
2171
push_constant.grid_size >>= 1;
2172
2173
uint32_t cascade_half_size = cascade_size >> 1;
2174
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF]);
2175
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_initialize_half_uniform_set, 0);
2176
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2177
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
2178
RD::get_singleton()->compute_list_add_barrier(compute_list);
2179
2180
//must start with regular jumpflood
2181
2182
push_constant.half_size = true;
2183
{
2184
RENDER_TIMESTAMP("SDFGI Jump Flood (Half-Size)");
2185
2186
uint32_t s = cascade_half_size;
2187
2188
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD]);
2189
2190
int jf_us = 0;
2191
//start with regular jump flood for very coarse reads, as this is impossible to optimize
2192
while (s > 1) {
2193
s /= 2;
2194
push_constant.step_size = s;
2195
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_half_uniform_set[jf_us], 0);
2196
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2197
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
2198
RD::get_singleton()->compute_list_add_barrier(compute_list);
2199
jf_us = jf_us == 0 ? 1 : 0;
2200
2201
if (cascade_half_size / (s / 2) >= optimized_jf_group_size) {
2202
break;
2203
}
2204
}
2205
2206
RENDER_TIMESTAMP("SDFGI Jump Flood Optimized (Half-Size)");
2207
2208
//continue with optimized jump flood for smaller reads
2209
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
2210
while (s > 1) {
2211
s /= 2;
2212
push_constant.step_size = s;
2213
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_half_uniform_set[jf_us], 0);
2214
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2215
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
2216
RD::get_singleton()->compute_list_add_barrier(compute_list);
2217
jf_us = jf_us == 0 ? 1 : 0;
2218
}
2219
}
2220
2221
// restore grid size for last passes
2222
push_constant.grid_size = cascade_size;
2223
2224
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_UPSCALE]);
2225
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_upscale_uniform_set, 0);
2226
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2227
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2228
RD::get_singleton()->compute_list_add_barrier(compute_list);
2229
2230
//run one pass of fullsize jumpflood to fix up half size artifacts
2231
2232
push_constant.half_size = false;
2233
push_constant.step_size = 1;
2234
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
2235
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[upscale_jfa_uniform_set_index], 0);
2236
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2237
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2238
RD::get_singleton()->compute_list_add_barrier(compute_list);
2239
2240
} else {
2241
//full size jumpflood
2242
RENDER_TIMESTAMP("SDFGI Jump Flood (Full-Size)");
2243
2244
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE]);
2245
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_initialize_uniform_set, 0);
2246
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2247
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2248
2249
RD::get_singleton()->compute_list_add_barrier(compute_list);
2250
2251
push_constant.half_size = false;
2252
{
2253
uint32_t s = cascade_size;
2254
2255
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD]);
2256
2257
int jf_us = 0;
2258
//start with regular jump flood for very coarse reads, as this is impossible to optimize
2259
while (s > 1) {
2260
s /= 2;
2261
push_constant.step_size = s;
2262
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[jf_us], 0);
2263
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2264
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2265
RD::get_singleton()->compute_list_add_barrier(compute_list);
2266
jf_us = jf_us == 0 ? 1 : 0;
2267
2268
if (cascade_size / (s / 2) >= optimized_jf_group_size) {
2269
break;
2270
}
2271
}
2272
2273
RENDER_TIMESTAMP("SDFGI Jump Flood Optimized (Full-Size)");
2274
2275
//continue with optimized jump flood for smaller reads
2276
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
2277
while (s > 1) {
2278
s /= 2;
2279
push_constant.step_size = s;
2280
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[jf_us], 0);
2281
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2282
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2283
RD::get_singleton()->compute_list_add_barrier(compute_list);
2284
jf_us = jf_us == 0 ? 1 : 0;
2285
}
2286
}
2287
}
2288
2289
RENDER_TIMESTAMP("SDFGI Occlusion");
2290
2291
// occlusion
2292
{
2293
uint32_t probe_size = cascade_size / SDFGI::PROBE_DIVISOR;
2294
Vector3i probe_global_pos = cascades[cascade].position / probe_size;
2295
2296
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_OCCLUSION]);
2297
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, occlusion_uniform_set, 0);
2298
for (int i = 0; i < 8; i++) {
2299
//dispatch all at once for performance
2300
Vector3i offset(i & 1, (i >> 1) & 1, (i >> 2) & 1);
2301
2302
if ((probe_global_pos.x & 1) != 0) {
2303
offset.x = (offset.x + 1) & 1;
2304
}
2305
if ((probe_global_pos.y & 1) != 0) {
2306
offset.y = (offset.y + 1) & 1;
2307
}
2308
if ((probe_global_pos.z & 1) != 0) {
2309
offset.z = (offset.z + 1) & 1;
2310
}
2311
push_constant.probe_offset[0] = offset.x;
2312
push_constant.probe_offset[1] = offset.y;
2313
push_constant.probe_offset[2] = offset.z;
2314
push_constant.occlusion_index = i;
2315
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2316
2317
Vector3i groups = Vector3i(probe_size + 1, probe_size + 1, probe_size + 1) - offset; //if offset, it's one less probe per axis to compute
2318
RD::get_singleton()->compute_list_dispatch(compute_list, groups.x, groups.y, groups.z);
2319
}
2320
RD::get_singleton()->compute_list_add_barrier(compute_list);
2321
}
2322
2323
RENDER_TIMESTAMP("SDFGI Store");
2324
2325
// store
2326
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_STORE]);
2327
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].sdf_store_uniform_set, 0);
2328
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
2329
RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
2330
2331
RD::get_singleton()->compute_list_end();
2332
2333
//clear these textures, as they will have previous garbage on next draw
2334
RD::get_singleton()->texture_clear(cascades[cascade].light_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
2335
RD::get_singleton()->texture_clear(cascades[cascade].light_aniso_0_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
2336
RD::get_singleton()->texture_clear(cascades[cascade].light_aniso_1_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
2337
2338
#if 0
2339
Vector<uint8_t> data = RD::get_singleton()->texture_get_data(cascades[cascade].sdf, 0);
2340
Ref<Image> img;
2341
img.instantiate();
2342
for (uint32_t i = 0; i < cascade_size; i++) {
2343
Vector<uint8_t> subarr = data.slice(128 * 128 * i, 128 * 128 * (i + 1));
2344
img->set_data(cascade_size, cascade_size, false, Image::FORMAT_L8, subarr);
2345
img->save_png("res://cascade_sdf_" + itos(cascade) + "_" + itos(i) + ".png");
2346
}
2347
2348
//finalize render and update sdf
2349
#endif
2350
2351
#if 0
2352
Vector<uint8_t> data = RD::get_singleton()->texture_get_data(render_albedo, 0);
2353
Ref<Image> img;
2354
img.instantiate();
2355
for (uint32_t i = 0; i < cascade_size; i++) {
2356
Vector<uint8_t> subarr = data.slice(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2);
2357
img->createcascade_size, cascade_size, false, Image::FORMAT_RGB565, subarr);
2358
img->convert(Image::FORMAT_RGBA8);
2359
img->save_png("res://cascade_" + itos(cascade) + "_" + itos(i) + ".png");
2360
}
2361
2362
//finalize render and update sdf
2363
#endif
2364
2365
RENDER_TIMESTAMP("< SDFGI Update SDF");
2366
RD::get_singleton()->draw_command_end_label();
2367
}
2368
}
2369
2370
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) {
2371
ERR_FAIL_COND(p_render_buffers.is_null()); // we wouldn't be here if this failed but...
2372
2373
RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
2374
2375
RD::get_singleton()->draw_command_begin_label("SDFGI Render Static Lights");
2376
2377
update_cascades();
2378
2379
SDFGIShader::Light lights[SDFGI::MAX_STATIC_LIGHTS];
2380
uint32_t light_count[SDFGI::MAX_STATIC_LIGHTS];
2381
2382
for (uint32_t i = 0; i < p_cascade_count; i++) {
2383
ERR_CONTINUE(p_cascade_indices[i] >= cascades.size());
2384
2385
SDFGI::Cascade &cc = cascades[p_cascade_indices[i]];
2386
2387
{ //fill light buffer
2388
2389
AABB cascade_aabb;
2390
cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cc.position)) * cc.cell_size;
2391
cascade_aabb.size = Vector3(1, 1, 1) * cascade_size * cc.cell_size;
2392
2393
int idx = 0;
2394
2395
for (uint32_t j = 0; j < (uint32_t)p_positional_light_cull_result[i].size(); j++) {
2396
if (idx == SDFGI::MAX_STATIC_LIGHTS) {
2397
break;
2398
}
2399
2400
RID light_instance = p_positional_light_cull_result[i][j];
2401
ERR_CONTINUE(!light_storage->owns_light_instance(light_instance));
2402
2403
RID light = light_storage->light_instance_get_base_light(light_instance);
2404
AABB light_aabb = light_storage->light_instance_get_base_aabb(light_instance);
2405
Transform3D light_transform = light_storage->light_instance_get_base_transform(light_instance);
2406
2407
uint32_t max_sdfgi_cascade = RSG::light_storage->light_get_max_sdfgi_cascade(light);
2408
if (p_cascade_indices[i] > max_sdfgi_cascade) {
2409
continue;
2410
}
2411
2412
if (!cascade_aabb.intersects(light_aabb)) {
2413
continue;
2414
}
2415
2416
lights[idx].type = RSG::light_storage->light_get_type(light);
2417
2418
Vector3 dir = -light_transform.basis.get_column(Vector3::AXIS_Z);
2419
if (lights[idx].type == RS::LIGHT_DIRECTIONAL) {
2420
dir.y *= y_mult; //only makes sense for directional
2421
dir.normalize();
2422
}
2423
lights[idx].direction[0] = dir.x;
2424
lights[idx].direction[1] = dir.y;
2425
lights[idx].direction[2] = dir.z;
2426
Vector3 pos = light_transform.origin;
2427
pos.y *= y_mult;
2428
lights[idx].position[0] = pos.x;
2429
lights[idx].position[1] = pos.y;
2430
lights[idx].position[2] = pos.z;
2431
Color color = RSG::light_storage->light_get_color(light);
2432
color = color.srgb_to_linear();
2433
lights[idx].color[0] = color.r;
2434
lights[idx].color[1] = color.g;
2435
lights[idx].color[2] = color.b;
2436
2437
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);
2438
if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
2439
lights[idx].energy *= RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INTENSITY);
2440
2441
// Convert from Luminous Power to Luminous Intensity
2442
if (lights[idx].type == RS::LIGHT_OMNI) {
2443
lights[idx].energy *= 1.0 / (Math::PI * 4.0);
2444
} else if (lights[idx].type == RS::LIGHT_SPOT) {
2445
// Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
2446
// We make this assumption to keep them easy to control.
2447
lights[idx].energy *= 1.0 / Math::PI;
2448
}
2449
}
2450
2451
if (p_render_data->camera_attributes.is_valid()) {
2452
lights[idx].energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
2453
}
2454
2455
lights[idx].has_shadow = RSG::light_storage->light_has_shadow(light);
2456
lights[idx].attenuation = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
2457
lights[idx].radius = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_RANGE);
2458
lights[idx].cos_spot_angle = Math::cos(Math::deg_to_rad(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE)));
2459
lights[idx].inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
2460
2461
idx++;
2462
}
2463
2464
if (idx > 0) {
2465
RD::get_singleton()->buffer_update(cc.lights_buffer, 0, idx * sizeof(SDFGIShader::Light), lights);
2466
}
2467
2468
light_count[i] = idx;
2469
}
2470
}
2471
2472
for (uint32_t i = 0; i < p_cascade_count; i++) {
2473
ERR_CONTINUE(p_cascade_indices[i] >= cascades.size());
2474
2475
SDFGI::Cascade &cc = cascades[p_cascade_indices[i]];
2476
if (light_count[i] > 0) {
2477
RD::get_singleton()->buffer_copy(cc.solid_cell_dispatch_buffer_storage, cc.solid_cell_dispatch_buffer_call, 0, 0, sizeof(uint32_t) * 4);
2478
}
2479
}
2480
2481
/* Static Lights */
2482
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
2483
2484
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.direct_light_pipeline[SDFGIShader::DIRECT_LIGHT_MODE_STATIC]);
2485
2486
SDFGIShader::DirectLightPushConstant dl_push_constant;
2487
2488
dl_push_constant.grid_size[0] = cascade_size;
2489
dl_push_constant.grid_size[1] = cascade_size;
2490
dl_push_constant.grid_size[2] = cascade_size;
2491
dl_push_constant.max_cascades = cascades.size();
2492
dl_push_constant.probe_axis_size = probe_axis_count;
2493
dl_push_constant.bounce_feedback = 0.0; // this is static light, do not multibounce yet
2494
dl_push_constant.y_mult = y_mult;
2495
dl_push_constant.use_occlusion = uses_occlusion;
2496
2497
//all must be processed
2498
dl_push_constant.process_offset = 0;
2499
dl_push_constant.process_increment = 1;
2500
2501
for (uint32_t i = 0; i < p_cascade_count; i++) {
2502
ERR_CONTINUE(p_cascade_indices[i] >= cascades.size());
2503
2504
SDFGI::Cascade &cc = cascades[p_cascade_indices[i]];
2505
2506
dl_push_constant.light_count = light_count[i];
2507
dl_push_constant.cascade = p_cascade_indices[i];
2508
2509
if (dl_push_constant.light_count > 0) {
2510
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cc.sdf_direct_light_static_uniform_set, 0);
2511
RD::get_singleton()->compute_list_set_push_constant(compute_list, &dl_push_constant, sizeof(SDFGIShader::DirectLightPushConstant));
2512
RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cc.solid_cell_dispatch_buffer_call, 0);
2513
}
2514
}
2515
2516
RD::get_singleton()->compute_list_end();
2517
2518
RD::get_singleton()->draw_command_end_label();
2519
}
2520
2521
////////////////////////////////////////////////////////////////////////////////
2522
// VoxelGIInstance
2523
2524
void GI::VoxelGIInstance::update(bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects) {
2525
RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
2526
RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
2527
2528
uint32_t data_version = gi->voxel_gi_get_data_version(probe);
2529
2530
// (RE)CREATE IF NEEDED
2531
2532
if (last_probe_data_version != data_version) {
2533
//need to re-create everything
2534
free_resources();
2535
2536
Vector3i octree_size = gi->voxel_gi_get_octree_size(probe);
2537
2538
if (octree_size != Vector3i()) {
2539
//can create a 3D texture
2540
Vector<int> levels = gi->voxel_gi_get_level_counts(probe);
2541
2542
RD::TextureFormat tf;
2543
tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
2544
tf.width = octree_size.x;
2545
tf.height = octree_size.y;
2546
tf.depth = octree_size.z;
2547
tf.texture_type = RD::TEXTURE_TYPE_3D;
2548
tf.mipmaps = levels.size();
2549
2550
tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
2551
2552
texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
2553
RD::get_singleton()->set_resource_name(texture, "VoxelGI Instance Texture");
2554
2555
RD::get_singleton()->texture_clear(texture, Color(0, 0, 0, 0), 0, levels.size(), 0, 1);
2556
2557
{
2558
int total_elements = 0;
2559
for (int i = 0; i < levels.size(); i++) {
2560
total_elements += levels[i];
2561
}
2562
2563
write_buffer = RD::get_singleton()->storage_buffer_create(total_elements * 16);
2564
}
2565
2566
for (int i = 0; i < levels.size(); i++) {
2567
VoxelGIInstance::Mipmap mipmap;
2568
mipmap.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), texture, 0, i, 1, RD::TEXTURE_SLICE_3D);
2569
mipmap.level = levels.size() - i - 1;
2570
mipmap.cell_offset = 0;
2571
for (uint32_t j = 0; j < mipmap.level; j++) {
2572
mipmap.cell_offset += levels[j];
2573
}
2574
mipmap.cell_count = levels[mipmap.level];
2575
2576
Vector<RD::Uniform> uniforms;
2577
{
2578
RD::Uniform u;
2579
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2580
u.binding = 1;
2581
u.append_id(gi->voxel_gi_get_octree_buffer(probe));
2582
uniforms.push_back(u);
2583
}
2584
{
2585
RD::Uniform u;
2586
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2587
u.binding = 2;
2588
u.append_id(gi->voxel_gi_get_data_buffer(probe));
2589
uniforms.push_back(u);
2590
}
2591
2592
{
2593
RD::Uniform u;
2594
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2595
u.binding = 4;
2596
u.append_id(write_buffer);
2597
uniforms.push_back(u);
2598
}
2599
{
2600
RD::Uniform u;
2601
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2602
u.binding = 9;
2603
u.append_id(gi->voxel_gi_get_sdf_texture(probe));
2604
uniforms.push_back(u);
2605
}
2606
{
2607
RD::Uniform u;
2608
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2609
u.binding = 10;
2610
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
2611
uniforms.push_back(u);
2612
}
2613
2614
{
2615
Vector<RD::Uniform> copy_uniforms = uniforms;
2616
if (i == 0) {
2617
{
2618
RD::Uniform u;
2619
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
2620
u.binding = 3;
2621
u.append_id(gi->voxel_gi_lights_uniform);
2622
copy_uniforms.push_back(u);
2623
}
2624
2625
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);
2626
2627
copy_uniforms = uniforms; //restore
2628
2629
{
2630
RD::Uniform u;
2631
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2632
u.binding = 5;
2633
u.append_id(texture);
2634
copy_uniforms.push_back(u);
2635
}
2636
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);
2637
} else {
2638
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);
2639
}
2640
}
2641
2642
{
2643
RD::Uniform u;
2644
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2645
u.binding = 5;
2646
u.append_id(mipmap.texture);
2647
uniforms.push_back(u);
2648
}
2649
2650
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);
2651
2652
mipmaps.push_back(mipmap);
2653
}
2654
2655
{
2656
uint32_t dynamic_map_size = MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
2657
uint32_t oversample = nearest_power_of_2_templated(4);
2658
int mipmap_index = 0;
2659
2660
while (mipmap_index < mipmaps.size()) {
2661
VoxelGIInstance::DynamicMap dmap;
2662
2663
if (oversample > 0) {
2664
dmap.size = dynamic_map_size * (1 << oversample);
2665
dmap.mipmap = -1;
2666
oversample--;
2667
} else {
2668
dmap.size = dynamic_map_size >> mipmap_index;
2669
dmap.mipmap = mipmap_index;
2670
mipmap_index++;
2671
}
2672
2673
RD::TextureFormat dtf;
2674
dtf.width = dmap.size;
2675
dtf.height = dmap.size;
2676
dtf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
2677
dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
2678
2679
if (dynamic_maps.is_empty()) {
2680
dtf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
2681
}
2682
dmap.texture = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2683
RD::get_singleton()->set_resource_name(dmap.texture, "VoxelGI Instance DMap Texture");
2684
2685
if (dynamic_maps.is_empty()) {
2686
// Render depth for first one.
2687
// Use 16-bit depth when supported to improve performance.
2688
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;
2689
dtf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
2690
dmap.fb_depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2691
RD::get_singleton()->set_resource_name(dmap.fb_depth, "VoxelGI Instance DMap FB Depth");
2692
}
2693
2694
//just use depth as-is
2695
dtf.format = RD::DATA_FORMAT_R32_SFLOAT;
2696
dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
2697
2698
dmap.depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2699
RD::get_singleton()->set_resource_name(dmap.depth, "VoxelGI Instance DMap Depth");
2700
2701
if (dynamic_maps.is_empty()) {
2702
dtf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
2703
dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
2704
dmap.albedo = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2705
RD::get_singleton()->set_resource_name(dmap.albedo, "VoxelGI Instance DMap Albedo");
2706
dmap.normal = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2707
RD::get_singleton()->set_resource_name(dmap.normal, "VoxelGI Instance DMap Normal");
2708
dmap.orm = RD::get_singleton()->texture_create(dtf, RD::TextureView());
2709
RD::get_singleton()->set_resource_name(dmap.orm, "VoxelGI Instance DMap ORM");
2710
2711
Vector<RID> fb;
2712
fb.push_back(dmap.albedo);
2713
fb.push_back(dmap.normal);
2714
fb.push_back(dmap.orm);
2715
fb.push_back(dmap.texture); //emission
2716
fb.push_back(dmap.depth);
2717
fb.push_back(dmap.fb_depth);
2718
2719
dmap.fb = RD::get_singleton()->framebuffer_create(fb);
2720
2721
{
2722
Vector<RD::Uniform> uniforms;
2723
{
2724
RD::Uniform u;
2725
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
2726
u.binding = 3;
2727
u.append_id(gi->voxel_gi_lights_uniform);
2728
uniforms.push_back(u);
2729
}
2730
2731
{
2732
RD::Uniform u;
2733
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2734
u.binding = 5;
2735
u.append_id(dmap.albedo);
2736
uniforms.push_back(u);
2737
}
2738
{
2739
RD::Uniform u;
2740
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2741
u.binding = 6;
2742
u.append_id(dmap.normal);
2743
uniforms.push_back(u);
2744
}
2745
{
2746
RD::Uniform u;
2747
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2748
u.binding = 7;
2749
u.append_id(dmap.orm);
2750
uniforms.push_back(u);
2751
}
2752
{
2753
RD::Uniform u;
2754
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2755
u.binding = 8;
2756
u.append_id(dmap.fb_depth);
2757
uniforms.push_back(u);
2758
}
2759
{
2760
RD::Uniform u;
2761
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2762
u.binding = 9;
2763
u.append_id(gi->voxel_gi_get_sdf_texture(probe));
2764
uniforms.push_back(u);
2765
}
2766
{
2767
RD::Uniform u;
2768
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2769
u.binding = 10;
2770
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
2771
uniforms.push_back(u);
2772
}
2773
{
2774
RD::Uniform u;
2775
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2776
u.binding = 11;
2777
u.append_id(dmap.texture);
2778
uniforms.push_back(u);
2779
}
2780
{
2781
RD::Uniform u;
2782
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2783
u.binding = 12;
2784
u.append_id(dmap.depth);
2785
uniforms.push_back(u);
2786
}
2787
2788
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);
2789
}
2790
} else {
2791
bool plot = dmap.mipmap >= 0;
2792
bool write = dmap.mipmap < (mipmaps.size() - 1);
2793
2794
Vector<RD::Uniform> uniforms;
2795
2796
{
2797
RD::Uniform u;
2798
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2799
u.binding = 5;
2800
u.append_id(dynamic_maps[dynamic_maps.size() - 1].texture);
2801
uniforms.push_back(u);
2802
}
2803
{
2804
RD::Uniform u;
2805
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2806
u.binding = 6;
2807
u.append_id(dynamic_maps[dynamic_maps.size() - 1].depth);
2808
uniforms.push_back(u);
2809
}
2810
2811
if (write) {
2812
{
2813
RD::Uniform u;
2814
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2815
u.binding = 7;
2816
u.append_id(dmap.texture);
2817
uniforms.push_back(u);
2818
}
2819
{
2820
RD::Uniform u;
2821
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2822
u.binding = 8;
2823
u.append_id(dmap.depth);
2824
uniforms.push_back(u);
2825
}
2826
}
2827
2828
{
2829
RD::Uniform u;
2830
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2831
u.binding = 9;
2832
u.append_id(gi->voxel_gi_get_sdf_texture(probe));
2833
uniforms.push_back(u);
2834
}
2835
{
2836
RD::Uniform u;
2837
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2838
u.binding = 10;
2839
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
2840
uniforms.push_back(u);
2841
}
2842
2843
if (plot) {
2844
{
2845
RD::Uniform u;
2846
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
2847
u.binding = 11;
2848
u.append_id(mipmaps[dmap.mipmap].texture);
2849
uniforms.push_back(u);
2850
}
2851
}
2852
2853
dmap.uniform_set = RD::get_singleton()->uniform_set_create(
2854
uniforms,
2855
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)],
2856
0);
2857
}
2858
2859
dynamic_maps.push_back(dmap);
2860
}
2861
}
2862
}
2863
2864
last_probe_data_version = data_version;
2865
p_update_light_instances = true; //just in case
2866
2867
RendererSceneRenderRD::get_singleton()->base_uniforms_changed();
2868
}
2869
2870
// UDPDATE TIME
2871
2872
if (has_dynamic_object_data) {
2873
//if it has dynamic object data, it needs to be cleared
2874
RD::get_singleton()->texture_clear(texture, Color(0, 0, 0, 0), 0, mipmaps.size(), 0, 1);
2875
}
2876
2877
uint32_t light_count = 0;
2878
2879
if (p_update_light_instances || p_dynamic_objects.size() > 0) {
2880
light_count = MIN(gi->voxel_gi_max_lights, (uint32_t)p_light_instances.size());
2881
2882
{
2883
Transform3D to_cell = gi->voxel_gi_get_to_cell_xform(probe);
2884
Transform3D to_probe_xform = to_cell * transform.affine_inverse();
2885
2886
//update lights
2887
2888
for (uint32_t i = 0; i < light_count; i++) {
2889
VoxelGILight &l = gi->voxel_gi_lights[i];
2890
RID light_instance = p_light_instances[i];
2891
RID light = light_storage->light_instance_get_base_light(light_instance);
2892
2893
l.type = RSG::light_storage->light_get_type(light);
2894
if (l.type == RS::LIGHT_DIRECTIONAL && RSG::light_storage->light_directional_get_sky_mode(light) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
2895
light_count--;
2896
continue;
2897
}
2898
2899
l.attenuation = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
2900
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);
2901
2902
if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
2903
l.energy *= RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INTENSITY);
2904
2905
l.energy *= gi->voxel_gi_get_baked_exposure_normalization(probe);
2906
2907
// Convert from Luminous Power to Luminous Intensity
2908
if (l.type == RS::LIGHT_OMNI) {
2909
l.energy *= 1.0 / (Math::PI * 4.0);
2910
} else if (l.type == RS::LIGHT_SPOT) {
2911
// Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
2912
// We make this assumption to keep them easy to control.
2913
l.energy *= 1.0 / Math::PI;
2914
}
2915
}
2916
2917
l.radius = to_cell.basis.xform(Vector3(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_RANGE), 0, 0)).length();
2918
Color color = RSG::light_storage->light_get_color(light).srgb_to_linear();
2919
l.color[0] = color.r;
2920
l.color[1] = color.g;
2921
l.color[2] = color.b;
2922
2923
l.cos_spot_angle = Math::cos(Math::deg_to_rad(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE)));
2924
l.inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
2925
2926
Transform3D xform = light_storage->light_instance_get_base_transform(light_instance);
2927
2928
Vector3 pos = to_probe_xform.xform(xform.origin);
2929
Vector3 dir = to_probe_xform.basis.xform(-xform.basis.get_column(2)).normalized();
2930
2931
l.position[0] = pos.x;
2932
l.position[1] = pos.y;
2933
l.position[2] = pos.z;
2934
2935
l.direction[0] = dir.x;
2936
l.direction[1] = dir.y;
2937
l.direction[2] = dir.z;
2938
2939
l.has_shadow = RSG::light_storage->light_has_shadow(light);
2940
}
2941
2942
RD::get_singleton()->buffer_update(gi->voxel_gi_lights_uniform, 0, sizeof(VoxelGILight) * light_count, gi->voxel_gi_lights);
2943
}
2944
}
2945
2946
if (has_dynamic_object_data || p_update_light_instances || p_dynamic_objects.size()) {
2947
// PROCESS MIPMAPS
2948
if (mipmaps.size()) {
2949
//can update mipmaps
2950
2951
Vector3i probe_size = gi->voxel_gi_get_octree_size(probe);
2952
2953
Vector3 ps = probe_size / gi->voxel_gi_get_bounds(probe).size;
2954
float cell_size = (1.0 / MAX(MAX(ps.x, ps.y), ps.z)); // probe size relative to 1 unit in world space
2955
2956
VoxelGIPushConstant push_constant;
2957
2958
push_constant.limits[0] = probe_size.x;
2959
push_constant.limits[1] = probe_size.y;
2960
push_constant.limits[2] = probe_size.z;
2961
push_constant.stack_size = mipmaps.size();
2962
push_constant.emission_scale = 1.0;
2963
push_constant.propagation = gi->voxel_gi_get_propagation(probe);
2964
push_constant.dynamic_range = gi->voxel_gi_get_dynamic_range(probe);
2965
push_constant.light_count = light_count;
2966
push_constant.aniso_strength = 0;
2967
push_constant.cell_size = cell_size;
2968
2969
/* print_line("probe update to version " + itos(last_probe_version));
2970
print_line("propagation " + rtos(push_constant.propagation));
2971
print_line("dynrange " + rtos(push_constant.dynamic_range));
2972
*/
2973
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
2974
2975
int passes;
2976
if (p_update_light_instances) {
2977
passes = gi->voxel_gi_is_using_two_bounces(probe) ? 2 : 1;
2978
} else {
2979
passes = 1; //only re-blitting is necessary
2980
}
2981
int wg_size = 64;
2982
int64_t wg_limit_x = (int64_t)RD::get_singleton()->limit_get(RD::LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
2983
2984
for (int pass = 0; pass < passes; pass++) {
2985
if (p_update_light_instances) {
2986
for (int i = 0; i < mipmaps.size(); i++) {
2987
if (i == 0) {
2988
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]);
2989
} else if (i == 1) {
2990
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_COMPUTE_MIPMAP]);
2991
}
2992
2993
if (pass == 1 || i > 0) {
2994
RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
2995
}
2996
if (pass == 0 || i > 0) {
2997
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].uniform_set, 0);
2998
} else {
2999
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].second_bounce_uniform_set, 0);
3000
}
3001
3002
push_constant.cell_offset = mipmaps[i].cell_offset;
3003
push_constant.cell_count = mipmaps[i].cell_count;
3004
3005
int64_t wg_todo = (mipmaps[i].cell_count + wg_size - 1) / wg_size;
3006
while (wg_todo) {
3007
int64_t wg_count = MIN(wg_todo, wg_limit_x);
3008
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIPushConstant));
3009
RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
3010
wg_todo -= wg_count;
3011
push_constant.cell_offset += wg_count * wg_size;
3012
}
3013
}
3014
3015
RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
3016
}
3017
3018
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_WRITE_TEXTURE]);
3019
3020
for (int i = 0; i < mipmaps.size(); i++) {
3021
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].write_uniform_set, 0);
3022
3023
push_constant.cell_offset = mipmaps[i].cell_offset;
3024
push_constant.cell_count = mipmaps[i].cell_count;
3025
3026
int64_t wg_todo = (mipmaps[i].cell_count + wg_size - 1) / wg_size;
3027
while (wg_todo) {
3028
int64_t wg_count = MIN(wg_todo, wg_limit_x);
3029
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIPushConstant));
3030
RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
3031
wg_todo -= wg_count;
3032
push_constant.cell_offset += wg_count * wg_size;
3033
}
3034
}
3035
}
3036
3037
RD::get_singleton()->compute_list_end();
3038
}
3039
}
3040
3041
has_dynamic_object_data = false; //clear until dynamic object data is used again
3042
3043
if (p_dynamic_objects.size() && dynamic_maps.size()) {
3044
Vector3i octree_size = gi->voxel_gi_get_octree_size(probe);
3045
int multiplier = dynamic_maps[0].size / MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
3046
3047
Transform3D oversample_scale;
3048
oversample_scale.basis.scale(Vector3(multiplier, multiplier, multiplier));
3049
3050
Transform3D to_cell = oversample_scale * gi->voxel_gi_get_to_cell_xform(probe);
3051
Transform3D to_world_xform = transform * to_cell.affine_inverse();
3052
Transform3D to_probe_xform = to_world_xform.affine_inverse();
3053
3054
AABB probe_aabb(Vector3(), octree_size);
3055
3056
//this could probably be better parallelized in compute..
3057
for (int i = 0; i < (int)p_dynamic_objects.size(); i++) {
3058
RenderGeometryInstance *instance = p_dynamic_objects[i];
3059
3060
//transform aabb to voxel_gi
3061
AABB aabb = (to_probe_xform * instance->get_transform()).xform(instance->get_aabb());
3062
3063
//this needs to wrap to grid resolution to avoid jitter
3064
//also extend margin a bit just in case
3065
Vector3i begin = aabb.position - Vector3i(1, 1, 1);
3066
Vector3i end = aabb.position + aabb.size + Vector3i(1, 1, 1);
3067
3068
for (int j = 0; j < 3; j++) {
3069
if ((end[j] - begin[j]) & 1) {
3070
end[j]++; //for half extents split, it needs to be even
3071
}
3072
begin[j] = MAX(begin[j], 0);
3073
end[j] = MIN(end[j], octree_size[j] * multiplier);
3074
}
3075
3076
//aabb = aabb.intersection(probe_aabb); //intersect
3077
aabb.position = begin;
3078
aabb.size = end - begin;
3079
3080
//print_line("aabb: " + aabb);
3081
3082
for (int j = 0; j < 6; j++) {
3083
//if (j != 0 && j != 3) {
3084
// continue;
3085
//}
3086
static const Vector3 render_z[6] = {
3087
Vector3(1, 0, 0),
3088
Vector3(0, 1, 0),
3089
Vector3(0, 0, 1),
3090
Vector3(-1, 0, 0),
3091
Vector3(0, -1, 0),
3092
Vector3(0, 0, -1),
3093
};
3094
static const Vector3 render_up[6] = {
3095
Vector3(0, 1, 0),
3096
Vector3(0, 0, 1),
3097
Vector3(0, 1, 0),
3098
Vector3(0, 1, 0),
3099
Vector3(0, 0, 1),
3100
Vector3(0, 1, 0),
3101
};
3102
3103
Vector3 render_dir = render_z[j];
3104
Vector3 up_dir = render_up[j];
3105
3106
Vector3 center = aabb.get_center();
3107
Transform3D xform;
3108
xform.set_look_at(center - aabb.size * 0.5 * render_dir, center, up_dir);
3109
3110
Vector3 x_dir = xform.basis.get_column(0).abs();
3111
int x_axis = int(Vector3(0, 1, 2).dot(x_dir));
3112
Vector3 y_dir = xform.basis.get_column(1).abs();
3113
int y_axis = int(Vector3(0, 1, 2).dot(y_dir));
3114
Vector3 z_dir = -xform.basis.get_column(2);
3115
int z_axis = int(Vector3(0, 1, 2).dot(z_dir.abs()));
3116
3117
Rect2i rect(aabb.position[x_axis], aabb.position[y_axis], aabb.size[x_axis], aabb.size[y_axis]);
3118
bool x_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_column(0)) < 0);
3119
bool y_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_column(1)) < 0);
3120
bool z_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_column(2)) > 0);
3121
3122
Projection cm;
3123
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]);
3124
3125
if (RendererSceneRenderRD::get_singleton()->cull_argument.size() == 0) {
3126
RendererSceneRenderRD::get_singleton()->cull_argument.push_back(nullptr);
3127
}
3128
RendererSceneRenderRD::get_singleton()->cull_argument[0] = instance;
3129
3130
float exposure_normalization = 1.0;
3131
if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
3132
exposure_normalization = gi->voxel_gi_get_baked_exposure_normalization(probe);
3133
}
3134
3135
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);
3136
3137
Vector3 ps = octree_size / gi->voxel_gi_get_bounds(probe).size;
3138
float cell_size = (1.0 / MAX(MAX(ps.x, ps.y), ps.z)); // probe size relative to 1 unit in world space
3139
3140
VoxelGIDynamicPushConstant push_constant;
3141
memset(&push_constant, 0, sizeof(VoxelGIDynamicPushConstant));
3142
push_constant.limits[0] = octree_size.x;
3143
push_constant.limits[1] = octree_size.y;
3144
push_constant.limits[2] = octree_size.z;
3145
push_constant.light_count = p_light_instances.size();
3146
push_constant.x_dir[0] = x_dir[0];
3147
push_constant.x_dir[1] = x_dir[1];
3148
push_constant.x_dir[2] = x_dir[2];
3149
push_constant.y_dir[0] = y_dir[0];
3150
push_constant.y_dir[1] = y_dir[1];
3151
push_constant.y_dir[2] = y_dir[2];
3152
push_constant.z_dir[0] = z_dir[0];
3153
push_constant.z_dir[1] = z_dir[1];
3154
push_constant.z_dir[2] = z_dir[2];
3155
push_constant.z_base = xform.origin[z_axis];
3156
push_constant.z_sign = (z_flip ? -1.0 : 1.0);
3157
push_constant.pos_multiplier = float(1.0) / multiplier;
3158
push_constant.dynamic_range = gi->voxel_gi_get_dynamic_range(probe);
3159
push_constant.flip_x = x_flip;
3160
push_constant.flip_y = y_flip;
3161
push_constant.rect_pos[0] = rect.position[0];
3162
push_constant.rect_pos[1] = rect.position[1];
3163
push_constant.rect_size[0] = rect.size[0];
3164
push_constant.rect_size[1] = rect.size[1];
3165
push_constant.prev_rect_ofs[0] = 0;
3166
push_constant.prev_rect_ofs[1] = 0;
3167
push_constant.prev_rect_size[0] = 0;
3168
push_constant.prev_rect_size[1] = 0;
3169
push_constant.on_mipmap = false;
3170
push_constant.propagation = gi->voxel_gi_get_propagation(probe);
3171
push_constant.cell_size = cell_size;
3172
push_constant.pad[0] = 0;
3173
push_constant.pad[1] = 0;
3174
3175
//process lighting
3176
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
3177
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING]);
3178
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, dynamic_maps[0].uniform_set, 0);
3179
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIDynamicPushConstant));
3180
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);
3181
//print_line("rect: " + itos(i) + ": " + rect);
3182
3183
for (int k = 1; k < dynamic_maps.size(); k++) {
3184
// enlarge the rect if needed so all pixels fit when downscaled,
3185
// this ensures downsampling is smooth and optimal because no pixels are left behind
3186
3187
//x
3188
if (rect.position.x & 1) {
3189
rect.size.x++;
3190
push_constant.prev_rect_ofs[0] = 1; //this is used to ensure reading is also optimal
3191
} else {
3192
push_constant.prev_rect_ofs[0] = 0;
3193
}
3194
if (rect.size.x & 1) {
3195
rect.size.x++;
3196
}
3197
3198
rect.position.x >>= 1;
3199
rect.size.x = MAX(1, rect.size.x >> 1);
3200
3201
//y
3202
if (rect.position.y & 1) {
3203
rect.size.y++;
3204
push_constant.prev_rect_ofs[1] = 1;
3205
} else {
3206
push_constant.prev_rect_ofs[1] = 0;
3207
}
3208
if (rect.size.y & 1) {
3209
rect.size.y++;
3210
}
3211
3212
rect.position.y >>= 1;
3213
rect.size.y = MAX(1, rect.size.y >> 1);
3214
3215
//shrink limits to ensure plot does not go outside map
3216
if (dynamic_maps[k].mipmap > 0) {
3217
for (int l = 0; l < 3; l++) {
3218
push_constant.limits[l] = MAX(1, push_constant.limits[l] >> 1);
3219
}
3220
}
3221
3222
//print_line("rect: " + itos(i) + ": " + rect);
3223
push_constant.rect_pos[0] = rect.position[0];
3224
push_constant.rect_pos[1] = rect.position[1];
3225
push_constant.prev_rect_size[0] = push_constant.rect_size[0];
3226
push_constant.prev_rect_size[1] = push_constant.rect_size[1];
3227
push_constant.rect_size[0] = rect.size[0];
3228
push_constant.rect_size[1] = rect.size[1];
3229
push_constant.on_mipmap = dynamic_maps[k].mipmap > 0;
3230
3231
RD::get_singleton()->compute_list_add_barrier(compute_list);
3232
3233
if (dynamic_maps[k].mipmap < 0) {
3234
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE]);
3235
} else if (k < dynamic_maps.size() - 1) {
3236
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]);
3237
} else {
3238
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_PLOT]);
3239
}
3240
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, dynamic_maps[k].uniform_set, 0);
3241
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIDynamicPushConstant));
3242
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);
3243
}
3244
3245
RD::get_singleton()->compute_list_end();
3246
}
3247
}
3248
3249
has_dynamic_object_data = true; //clear until dynamic object data is used again
3250
}
3251
3252
last_probe_version = gi->voxel_gi_get_version(probe);
3253
}
3254
3255
void GI::VoxelGIInstance::free_resources() {
3256
if (texture.is_valid()) {
3257
RD::get_singleton()->free(texture);
3258
RD::get_singleton()->free(write_buffer);
3259
3260
texture = RID();
3261
write_buffer = RID();
3262
mipmaps.clear();
3263
}
3264
3265
for (int i = 0; i < dynamic_maps.size(); i++) {
3266
RD::get_singleton()->free(dynamic_maps[i].texture);
3267
RD::get_singleton()->free(dynamic_maps[i].depth);
3268
3269
// these only exist on the first level...
3270
if (dynamic_maps[i].fb_depth.is_valid()) {
3271
RD::get_singleton()->free(dynamic_maps[i].fb_depth);
3272
}
3273
if (dynamic_maps[i].albedo.is_valid()) {
3274
RD::get_singleton()->free(dynamic_maps[i].albedo);
3275
}
3276
if (dynamic_maps[i].normal.is_valid()) {
3277
RD::get_singleton()->free(dynamic_maps[i].normal);
3278
}
3279
if (dynamic_maps[i].orm.is_valid()) {
3280
RD::get_singleton()->free(dynamic_maps[i].orm);
3281
}
3282
}
3283
dynamic_maps.clear();
3284
}
3285
3286
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) {
3287
RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
3288
3289
if (mipmaps.is_empty()) {
3290
return;
3291
}
3292
3293
Projection cam_transform = (p_camera_with_transform * Projection(transform)) * Projection(gi->voxel_gi_get_to_cell_xform(probe).affine_inverse());
3294
3295
int level = 0;
3296
Vector3i octree_size = gi->voxel_gi_get_octree_size(probe);
3297
3298
VoxelGIDebugPushConstant push_constant;
3299
push_constant.alpha = p_alpha;
3300
push_constant.dynamic_range = gi->voxel_gi_get_dynamic_range(probe);
3301
push_constant.cell_offset = mipmaps[level].cell_offset;
3302
push_constant.level = level;
3303
3304
push_constant.bounds[0] = octree_size.x >> level;
3305
push_constant.bounds[1] = octree_size.y >> level;
3306
push_constant.bounds[2] = octree_size.z >> level;
3307
push_constant.pad = 0;
3308
3309
for (int i = 0; i < 4; i++) {
3310
for (int j = 0; j < 4; j++) {
3311
push_constant.projection[i * 4 + j] = cam_transform.columns[i][j];
3312
}
3313
}
3314
3315
if (gi->voxel_gi_debug_uniform_set.is_valid()) {
3316
RD::get_singleton()->free(gi->voxel_gi_debug_uniform_set);
3317
}
3318
Vector<RD::Uniform> uniforms;
3319
{
3320
RD::Uniform u;
3321
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
3322
u.binding = 1;
3323
u.append_id(gi->voxel_gi_get_data_buffer(probe));
3324
uniforms.push_back(u);
3325
}
3326
{
3327
RD::Uniform u;
3328
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3329
u.binding = 2;
3330
u.append_id(texture);
3331
uniforms.push_back(u);
3332
}
3333
{
3334
RD::Uniform u;
3335
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
3336
u.binding = 3;
3337
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
3338
uniforms.push_back(u);
3339
}
3340
3341
int cell_count;
3342
if (!p_emission && p_lighting && has_dynamic_object_data) {
3343
cell_count = push_constant.bounds[0] * push_constant.bounds[1] * push_constant.bounds[2];
3344
} else {
3345
cell_count = mipmaps[level].cell_count;
3346
}
3347
3348
gi->voxel_gi_debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->voxel_gi_debug_shader_version_shaders[0], 0);
3349
3350
int voxel_gi_debug_pipeline = VOXEL_GI_DEBUG_COLOR;
3351
if (p_emission) {
3352
voxel_gi_debug_pipeline = VOXEL_GI_DEBUG_EMISSION;
3353
} else if (p_lighting) {
3354
voxel_gi_debug_pipeline = has_dynamic_object_data ? VOXEL_GI_DEBUG_LIGHT_FULL : VOXEL_GI_DEBUG_LIGHT;
3355
}
3356
RD::get_singleton()->draw_list_bind_render_pipeline(
3357
p_draw_list,
3358
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)));
3359
RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, gi->voxel_gi_debug_uniform_set, 0);
3360
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(VoxelGIDebugPushConstant));
3361
RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, 36);
3362
}
3363
3364
////////////////////////////////////////////////////////////////////////////////
3365
// GI
3366
3367
GI::GI() {
3368
singleton = this;
3369
3370
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)));
3371
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)));
3372
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)));
3373
}
3374
3375
GI::~GI() {
3376
if (voxel_gi_debug_shader_version.is_valid()) {
3377
voxel_gi_debug_shader.version_free(voxel_gi_debug_shader_version);
3378
}
3379
if (voxel_gi_lighting_shader_version.is_valid()) {
3380
voxel_gi_shader.version_free(voxel_gi_lighting_shader_version);
3381
}
3382
if (shader_version.is_valid()) {
3383
shader.version_free(shader_version);
3384
}
3385
if (sdfgi_shader.debug_probes_shader.is_valid()) {
3386
sdfgi_shader.debug_probes.version_free(sdfgi_shader.debug_probes_shader);
3387
}
3388
if (sdfgi_shader.debug_shader.is_valid()) {
3389
sdfgi_shader.debug.version_free(sdfgi_shader.debug_shader);
3390
}
3391
if (sdfgi_shader.direct_light_shader.is_valid()) {
3392
sdfgi_shader.direct_light.version_free(sdfgi_shader.direct_light_shader);
3393
}
3394
if (sdfgi_shader.integrate_shader.is_valid()) {
3395
sdfgi_shader.integrate.version_free(sdfgi_shader.integrate_shader);
3396
}
3397
if (sdfgi_shader.preprocess_shader.is_valid()) {
3398
sdfgi_shader.preprocess.version_free(sdfgi_shader.preprocess_shader);
3399
}
3400
3401
singleton = nullptr;
3402
}
3403
3404
void GI::init(SkyRD *p_sky) {
3405
RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
3406
RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
3407
3408
/* GI */
3409
3410
{
3411
//kinda complicated to compute the amount of slots, we try to use as many as we can
3412
3413
voxel_gi_lights = memnew_arr(VoxelGILight, voxel_gi_max_lights);
3414
voxel_gi_lights_uniform = RD::get_singleton()->uniform_buffer_create(voxel_gi_max_lights * sizeof(VoxelGILight));
3415
voxel_gi_quality = RS::VoxelGIQuality(CLAMP(int(GLOBAL_GET("rendering/global_illumination/voxel_gi/quality")), 0, 1));
3416
3417
String defines = "\n#define MAX_LIGHTS " + itos(voxel_gi_max_lights) + "\n";
3418
3419
Vector<String> versions;
3420
versions.push_back("\n#define MODE_COMPUTE_LIGHT\n");
3421
versions.push_back("\n#define MODE_SECOND_BOUNCE\n");
3422
versions.push_back("\n#define MODE_UPDATE_MIPMAPS\n");
3423
versions.push_back("\n#define MODE_WRITE_TEXTURE\n");
3424
versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_LIGHTING\n");
3425
versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
3426
versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n");
3427
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");
3428
3429
voxel_gi_shader.initialize(versions, defines);
3430
voxel_gi_lighting_shader_version = voxel_gi_shader.version_create();
3431
for (int i = 0; i < VOXEL_GI_SHADER_VERSION_MAX; i++) {
3432
voxel_gi_lighting_shader_version_shaders[i] = voxel_gi_shader.version_get_shader(voxel_gi_lighting_shader_version, i);
3433
voxel_gi_lighting_shader_version_pipelines[i] = RD::get_singleton()->compute_pipeline_create(voxel_gi_lighting_shader_version_shaders[i]);
3434
}
3435
}
3436
3437
{
3438
String defines;
3439
Vector<String> versions;
3440
versions.push_back("\n#define MODE_DEBUG_COLOR\n");
3441
versions.push_back("\n#define MODE_DEBUG_LIGHT\n");
3442
versions.push_back("\n#define MODE_DEBUG_EMISSION\n");
3443
versions.push_back("\n#define MODE_DEBUG_LIGHT\n#define MODE_DEBUG_LIGHT_FULL\n");
3444
3445
voxel_gi_debug_shader.initialize(versions, defines);
3446
voxel_gi_debug_shader_version = voxel_gi_debug_shader.version_create();
3447
for (int i = 0; i < VOXEL_GI_DEBUG_MAX; i++) {
3448
voxel_gi_debug_shader_version_shaders[i] = voxel_gi_debug_shader.version_get_shader(voxel_gi_debug_shader_version, i);
3449
3450
RD::PipelineRasterizationState rs;
3451
rs.cull_mode = RD::POLYGON_CULL_FRONT;
3452
RD::PipelineDepthStencilState ds;
3453
ds.enable_depth_test = true;
3454
ds.enable_depth_write = true;
3455
ds.depth_compare_operator = RD::COMPARE_OP_GREATER_OR_EQUAL;
3456
3457
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);
3458
}
3459
}
3460
3461
/* SDGFI */
3462
3463
{
3464
Vector<String> preprocess_modes;
3465
preprocess_modes.push_back("\n#define MODE_SCROLL\n");
3466
preprocess_modes.push_back("\n#define MODE_SCROLL_OCCLUSION\n");
3467
preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD\n");
3468
preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD_HALF\n");
3469
preprocess_modes.push_back("\n#define MODE_JUMPFLOOD\n");
3470
preprocess_modes.push_back("\n#define MODE_JUMPFLOOD_OPTIMIZED\n");
3471
preprocess_modes.push_back("\n#define MODE_UPSCALE_JUMP_FLOOD\n");
3472
preprocess_modes.push_back("\n#define MODE_OCCLUSION\n");
3473
preprocess_modes.push_back("\n#define MODE_STORE\n");
3474
String defines = "\n#define OCCLUSION_SIZE " + itos(SDFGI::CASCADE_SIZE / SDFGI::PROBE_DIVISOR) + "\n";
3475
sdfgi_shader.preprocess.initialize(preprocess_modes, defines);
3476
sdfgi_shader.preprocess_shader = sdfgi_shader.preprocess.version_create();
3477
for (int i = 0; i < SDFGIShader::PRE_PROCESS_MAX; i++) {
3478
sdfgi_shader.preprocess_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, i));
3479
}
3480
}
3481
3482
{
3483
//calculate tables
3484
String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3485
3486
Vector<String> direct_light_modes;
3487
direct_light_modes.push_back("\n#define MODE_PROCESS_STATIC\n");
3488
direct_light_modes.push_back("\n#define MODE_PROCESS_DYNAMIC\n");
3489
sdfgi_shader.direct_light.initialize(direct_light_modes, defines);
3490
sdfgi_shader.direct_light_shader = sdfgi_shader.direct_light.version_create();
3491
for (int i = 0; i < SDFGIShader::DIRECT_LIGHT_MODE_MAX; i++) {
3492
sdfgi_shader.direct_light_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.direct_light.version_get_shader(sdfgi_shader.direct_light_shader, i));
3493
}
3494
}
3495
3496
{
3497
//calculate tables
3498
String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3499
defines += "\n#define SH_SIZE " + itos(SDFGI::SH_SIZE) + "\n";
3500
if (p_sky->sky_use_cubemap_array) {
3501
defines += "\n#define USE_CUBEMAP_ARRAY\n";
3502
}
3503
3504
Vector<String> integrate_modes;
3505
integrate_modes.push_back("\n#define MODE_PROCESS\n");
3506
integrate_modes.push_back("\n#define MODE_STORE\n");
3507
integrate_modes.push_back("\n#define MODE_SCROLL\n");
3508
integrate_modes.push_back("\n#define MODE_SCROLL_STORE\n");
3509
sdfgi_shader.integrate.initialize(integrate_modes, defines);
3510
sdfgi_shader.integrate_shader = sdfgi_shader.integrate.version_create();
3511
3512
for (int i = 0; i < SDFGIShader::INTEGRATE_MODE_MAX; i++) {
3513
sdfgi_shader.integrate_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, i));
3514
}
3515
3516
{
3517
Vector<RD::Uniform> uniforms;
3518
3519
{
3520
RD::Uniform u;
3521
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3522
u.binding = 0;
3523
if (p_sky->sky_use_cubemap_array) {
3524
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_WHITE));
3525
} else {
3526
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_WHITE));
3527
}
3528
uniforms.push_back(u);
3529
}
3530
{
3531
RD::Uniform u;
3532
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
3533
u.binding = 1;
3534
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
3535
uniforms.push_back(u);
3536
}
3537
3538
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);
3539
}
3540
}
3541
3542
//GK
3543
{
3544
//calculate tables
3545
String defines = "\n#define SDFGI_OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3546
3547
Vector<ShaderRD::VariantDefine> variants;
3548
for (uint32_t vrs = 0; vrs < 2; vrs++) {
3549
String vrs_base = vrs ? "\n#define USE_VRS\n" : "";
3550
Group group = vrs ? GROUP_VRS : GROUP_NORMAL;
3551
bool default_enabled = vrs == 0;
3552
variants.push_back(ShaderRD::VariantDefine(group, vrs_base + "\n#define USE_VOXEL_GI_INSTANCES\n", default_enabled)); // MODE_VOXEL_GI
3553
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
3554
variants.push_back(ShaderRD::VariantDefine(group, vrs_base + "\n#define USE_SDFGI\n", default_enabled)); // MODE_SDFGI
3555
variants.push_back(ShaderRD::VariantDefine(group, vrs_base + "\n#define USE_SDFGI\n\n#define USE_VOXEL_GI_INSTANCES\n", default_enabled)); // MODE_COMBINED
3556
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
3557
}
3558
3559
shader.initialize(variants, defines);
3560
3561
bool vrs_supported = RendererSceneRenderRD::get_singleton()->is_vrs_supported();
3562
if (vrs_supported) {
3563
shader.enable_group(GROUP_VRS);
3564
}
3565
3566
shader_version = shader.version_create();
3567
3568
Vector<RD::PipelineSpecializationConstant> specialization_constants;
3569
3570
{
3571
RD::PipelineSpecializationConstant sc;
3572
sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
3573
sc.constant_id = 0; // SHADER_SPECIALIZATION_HALF_RES
3574
sc.bool_value = false;
3575
specialization_constants.push_back(sc);
3576
3577
sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
3578
sc.constant_id = 1; // SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX
3579
sc.bool_value = false;
3580
specialization_constants.push_back(sc);
3581
3582
sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
3583
sc.constant_id = 2; // SHADER_SPECIALIZATION_USE_VRS
3584
sc.bool_value = false;
3585
specialization_constants.push_back(sc);
3586
}
3587
3588
for (int v = 0; v < SHADER_SPECIALIZATION_VARIATIONS; v++) {
3589
specialization_constants.ptrw()[0].bool_value = (v & SHADER_SPECIALIZATION_HALF_RES) ? true : false;
3590
specialization_constants.ptrw()[1].bool_value = (v & SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX) ? true : false;
3591
specialization_constants.ptrw()[2].bool_value = (v & SHADER_SPECIALIZATION_USE_VRS) ? true : false;
3592
3593
int variant_base = vrs_supported ? MODE_MAX : 0;
3594
for (int i = 0; i < MODE_MAX; i++) {
3595
pipelines[v][i] = RD::get_singleton()->compute_pipeline_create(shader.version_get_shader(shader_version, variant_base + i), specialization_constants);
3596
}
3597
}
3598
3599
sdfgi_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGIData));
3600
}
3601
{
3602
String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3603
Vector<String> debug_modes;
3604
debug_modes.push_back("");
3605
sdfgi_shader.debug.initialize(debug_modes, defines);
3606
sdfgi_shader.debug_shader = sdfgi_shader.debug.version_create();
3607
sdfgi_shader.debug_shader_version = sdfgi_shader.debug.version_get_shader(sdfgi_shader.debug_shader, 0);
3608
sdfgi_shader.debug_pipeline = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.debug_shader_version);
3609
}
3610
{
3611
String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
3612
3613
Vector<String> versions;
3614
versions.push_back("\n#define MODE_PROBES\n");
3615
versions.push_back("\n#define MODE_PROBES\n#define USE_MULTIVIEW\n");
3616
versions.push_back("\n#define MODE_VISIBILITY\n");
3617
versions.push_back("\n#define MODE_VISIBILITY\n#define USE_MULTIVIEW\n");
3618
3619
sdfgi_shader.debug_probes.initialize(versions, defines);
3620
3621
// TODO disable multiview versions if turned off
3622
3623
sdfgi_shader.debug_probes_shader = sdfgi_shader.debug_probes.version_create();
3624
3625
{
3626
RD::PipelineRasterizationState rs;
3627
rs.cull_mode = RD::POLYGON_CULL_DISABLED;
3628
RD::PipelineDepthStencilState ds;
3629
ds.enable_depth_test = true;
3630
ds.enable_depth_write = true;
3631
ds.depth_compare_operator = RD::COMPARE_OP_GREATER_OR_EQUAL;
3632
for (int i = 0; i < SDFGIShader::PROBE_DEBUG_MAX; i++) {
3633
// TODO check if version is enabled
3634
3635
RID debug_probes_shader_version = sdfgi_shader.debug_probes.version_get_shader(sdfgi_shader.debug_probes_shader, i);
3636
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);
3637
}
3638
}
3639
}
3640
default_voxel_gi_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(VoxelGIData) * MAX_VOXEL_GI_INSTANCES);
3641
half_resolution = GLOBAL_GET("rendering/global_illumination/gi/use_half_resolution");
3642
}
3643
3644
void GI::free() {
3645
if (default_voxel_gi_buffer.is_valid()) {
3646
RD::get_singleton()->free(default_voxel_gi_buffer);
3647
}
3648
if (voxel_gi_lights_uniform.is_valid()) {
3649
RD::get_singleton()->free(voxel_gi_lights_uniform);
3650
}
3651
if (sdfgi_ubo.is_valid()) {
3652
RD::get_singleton()->free(sdfgi_ubo);
3653
}
3654
3655
if (voxel_gi_lights) {
3656
memdelete_arr(voxel_gi_lights);
3657
}
3658
}
3659
3660
Ref<GI::SDFGI> GI::create_sdfgi(RID p_env, const Vector3 &p_world_position, uint32_t p_requested_history_size) {
3661
Ref<SDFGI> sdfgi;
3662
sdfgi.instantiate();
3663
3664
sdfgi->create(p_env, p_world_position, p_requested_history_size, this);
3665
3666
return sdfgi;
3667
}
3668
3669
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) {
3670
ERR_FAIL_COND(p_render_buffers.is_null());
3671
3672
RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
3673
ERR_FAIL_NULL(texture_storage);
3674
3675
r_voxel_gi_instances_used = 0;
3676
3677
Ref<RenderBuffersGI> rbgi = p_render_buffers->get_custom_data(RB_SCOPE_GI);
3678
ERR_FAIL_COND(rbgi.is_null());
3679
3680
RID voxel_gi_buffer = rbgi->get_voxel_gi_buffer();
3681
VoxelGIData voxel_gi_data[MAX_VOXEL_GI_INSTANCES];
3682
3683
bool voxel_gi_instances_changed = false;
3684
3685
Transform3D to_camera;
3686
to_camera.origin = p_transform.origin; //only translation, make local
3687
3688
for (int i = 0; i < MAX_VOXEL_GI_INSTANCES; i++) {
3689
RID texture;
3690
if (i < (int)p_voxel_gi_instances.size()) {
3691
VoxelGIInstance *gipi = voxel_gi_instance_owner.get_or_null(p_voxel_gi_instances[i]);
3692
3693
if (gipi) {
3694
texture = gipi->texture;
3695
VoxelGIData &gipd = voxel_gi_data[i];
3696
3697
RID base_probe = gipi->probe;
3698
3699
Transform3D to_cell = voxel_gi_get_to_cell_xform(gipi->probe) * gipi->transform.affine_inverse() * to_camera;
3700
3701
gipd.xform[0] = to_cell.basis.rows[0][0];
3702
gipd.xform[1] = to_cell.basis.rows[1][0];
3703
gipd.xform[2] = to_cell.basis.rows[2][0];
3704
gipd.xform[3] = 0;
3705
gipd.xform[4] = to_cell.basis.rows[0][1];
3706
gipd.xform[5] = to_cell.basis.rows[1][1];
3707
gipd.xform[6] = to_cell.basis.rows[2][1];
3708
gipd.xform[7] = 0;
3709
gipd.xform[8] = to_cell.basis.rows[0][2];
3710
gipd.xform[9] = to_cell.basis.rows[1][2];
3711
gipd.xform[10] = to_cell.basis.rows[2][2];
3712
gipd.xform[11] = 0;
3713
gipd.xform[12] = to_cell.origin.x;
3714
gipd.xform[13] = to_cell.origin.y;
3715
gipd.xform[14] = to_cell.origin.z;
3716
gipd.xform[15] = 1;
3717
3718
Vector3 bounds = voxel_gi_get_octree_size(base_probe);
3719
3720
gipd.bounds[0] = bounds.x;
3721
gipd.bounds[1] = bounds.y;
3722
gipd.bounds[2] = bounds.z;
3723
3724
gipd.dynamic_range = voxel_gi_get_dynamic_range(base_probe) * voxel_gi_get_energy(base_probe);
3725
gipd.bias = voxel_gi_get_bias(base_probe);
3726
gipd.normal_bias = voxel_gi_get_normal_bias(base_probe);
3727
gipd.blend_ambient = !voxel_gi_is_interior(base_probe);
3728
gipd.mipmaps = gipi->mipmaps.size();
3729
gipd.exposure_normalization = 1.0;
3730
if (p_render_data->camera_attributes.is_valid()) {
3731
float exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
3732
gipd.exposure_normalization = exposure_normalization / voxel_gi_get_baked_exposure_normalization(base_probe);
3733
}
3734
}
3735
3736
r_voxel_gi_instances_used++;
3737
}
3738
3739
if (texture == RID()) {
3740
texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
3741
}
3742
3743
if (texture != rbgi->voxel_gi_textures[i]) {
3744
voxel_gi_instances_changed = true;
3745
rbgi->voxel_gi_textures[i] = texture;
3746
}
3747
}
3748
3749
if (voxel_gi_instances_changed) {
3750
for (uint32_t v = 0; v < RendererSceneRender::MAX_RENDER_VIEWS; v++) {
3751
if (RD::get_singleton()->uniform_set_is_valid(rbgi->uniform_set[v])) {
3752
RD::get_singleton()->free(rbgi->uniform_set[v]);
3753
}
3754
rbgi->uniform_set[v] = RID();
3755
}
3756
3757
if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) {
3758
// VoxelGI instances have changed, so we need to update volumetric fog.
3759
Ref<RendererRD::Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG);
3760
fog->sync_gi_dependent_sets_validity(true);
3761
}
3762
}
3763
3764
if (p_voxel_gi_instances.size() > 0) {
3765
RD::get_singleton()->draw_command_begin_label("VoxelGIs Setup");
3766
3767
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);
3768
3769
RD::get_singleton()->draw_command_end_label();
3770
}
3771
}
3772
3773
RID GI::RenderBuffersGI::get_voxel_gi_buffer() {
3774
if (voxel_gi_buffer.is_null()) {
3775
voxel_gi_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(GI::VoxelGIData) * GI::MAX_VOXEL_GI_INSTANCES);
3776
}
3777
return voxel_gi_buffer;
3778
}
3779
3780
void GI::RenderBuffersGI::free_data() {
3781
for (uint32_t v = 0; v < RendererSceneRender::MAX_RENDER_VIEWS; v++) {
3782
if (RD::get_singleton()->uniform_set_is_valid(uniform_set[v])) {
3783
RD::get_singleton()->free(uniform_set[v]);
3784
}
3785
uniform_set[v] = RID();
3786
}
3787
3788
if (scene_data_ubo.is_valid()) {
3789
RD::get_singleton()->free(scene_data_ubo);
3790
scene_data_ubo = RID();
3791
}
3792
3793
if (voxel_gi_buffer.is_valid()) {
3794
RD::get_singleton()->free(voxel_gi_buffer);
3795
voxel_gi_buffer = RID();
3796
}
3797
}
3798
3799
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) {
3800
RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
3801
RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
3802
3803
ERR_FAIL_COND_MSG(p_view_count > 2, "Maximum of 2 views supported for Processing GI.");
3804
3805
RD::get_singleton()->draw_command_begin_label("GI Render");
3806
3807
ERR_FAIL_COND(p_render_buffers.is_null());
3808
3809
Ref<RenderBuffersGI> rbgi = p_render_buffers->get_custom_data(RB_SCOPE_GI);
3810
ERR_FAIL_COND(rbgi.is_null());
3811
3812
Size2i internal_size = p_render_buffers->get_internal_size();
3813
3814
if (rbgi->using_half_size_gi != half_resolution) {
3815
p_render_buffers->clear_context(RB_SCOPE_GI);
3816
}
3817
3818
if (!p_render_buffers->has_texture(RB_SCOPE_GI, RB_TEX_AMBIENT)) {
3819
Size2i size = internal_size;
3820
uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
3821
3822
if (half_resolution) {
3823
size.x >>= 1;
3824
size.y >>= 1;
3825
}
3826
3827
p_render_buffers->create_texture(RB_SCOPE_GI, RB_TEX_AMBIENT, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, usage_bits, RD::TEXTURE_SAMPLES_1, size);
3828
p_render_buffers->create_texture(RB_SCOPE_GI, RB_TEX_REFLECTION, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, usage_bits, RD::TEXTURE_SAMPLES_1, size);
3829
3830
rbgi->using_half_size_gi = half_resolution;
3831
}
3832
3833
// Setup our scene data
3834
{
3835
SceneData scene_data;
3836
3837
if (rbgi->scene_data_ubo.is_null()) {
3838
rbgi->scene_data_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SceneData));
3839
}
3840
3841
Projection correction;
3842
correction.set_depth_correction(false);
3843
3844
for (uint32_t v = 0; v < p_view_count; v++) {
3845
Projection temp = correction * p_projections[v];
3846
3847
RendererRD::MaterialStorage::store_camera(temp.inverse(), scene_data.inv_projection[v]);
3848
scene_data.eye_offset[v][0] = p_eye_offsets[v].x;
3849
scene_data.eye_offset[v][1] = p_eye_offsets[v].y;
3850
scene_data.eye_offset[v][2] = p_eye_offsets[v].z;
3851
scene_data.eye_offset[v][3] = 0.0;
3852
}
3853
3854
// Note that we will be ignoring the origin of this transform.
3855
RendererRD::MaterialStorage::store_transform(p_cam_transform, scene_data.cam_transform);
3856
3857
scene_data.screen_size[0] = internal_size.x;
3858
scene_data.screen_size[1] = internal_size.y;
3859
3860
RD::get_singleton()->buffer_update(rbgi->scene_data_ubo, 0, sizeof(SceneData), &scene_data);
3861
}
3862
3863
// Now compute the contents of our buffers.
3864
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
3865
3866
// Render each eye separately.
3867
// We need to look into whether we can make our compute shader use Multiview but not sure that works or makes a difference..
3868
3869
// setup our push constant
3870
3871
PushConstant push_constant;
3872
3873
push_constant.max_voxel_gi_instances = MIN((uint64_t)MAX_VOXEL_GI_INSTANCES, p_voxel_gi_instances.size());
3874
push_constant.high_quality_vct = voxel_gi_quality == RS::VOXEL_GI_QUALITY_HIGH;
3875
3876
// these should be the same for all views
3877
push_constant.orthogonal = p_projections[0].is_orthogonal();
3878
push_constant.z_near = p_projections[0].get_z_near();
3879
push_constant.z_far = p_projections[0].get_z_far();
3880
3881
// these are only used if we have 1 view, else we use the projections in our scene data
3882
push_constant.proj_info[0] = -2.0f / (internal_size.x * p_projections[0].columns[0][0]);
3883
push_constant.proj_info[1] = -2.0f / (internal_size.y * p_projections[0].columns[1][1]);
3884
push_constant.proj_info[2] = (1.0f - p_projections[0].columns[0][2]) / p_projections[0].columns[0][0];
3885
push_constant.proj_info[3] = (1.0f + p_projections[0].columns[1][2]) / p_projections[0].columns[1][1];
3886
3887
bool use_sdfgi = p_render_buffers->has_custom_data(RB_SCOPE_SDFGI);
3888
bool use_voxel_gi_instances = push_constant.max_voxel_gi_instances > 0;
3889
3890
Ref<SDFGI> sdfgi;
3891
if (use_sdfgi) {
3892
sdfgi = p_render_buffers->get_custom_data(RB_SCOPE_SDFGI);
3893
}
3894
3895
uint32_t pipeline_specialization = 0;
3896
if (rbgi->using_half_size_gi) {
3897
pipeline_specialization |= SHADER_SPECIALIZATION_HALF_RES;
3898
}
3899
if (p_view_count > 1) {
3900
pipeline_specialization |= SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX;
3901
}
3902
bool has_vrs_texture = p_render_buffers->has_texture(RB_SCOPE_VRS, RB_TEXTURE);
3903
if (has_vrs_texture) {
3904
pipeline_specialization |= SHADER_SPECIALIZATION_USE_VRS;
3905
}
3906
3907
bool without_sampler = RD::get_singleton()->sampler_is_format_supported_for_filter(RD::DATA_FORMAT_R8G8_UINT, RD::SAMPLER_FILTER_LINEAR);
3908
Mode mode;
3909
if (use_sdfgi && use_voxel_gi_instances) {
3910
mode = without_sampler ? MODE_COMBINED_WITHOUT_SAMPLER : MODE_COMBINED;
3911
} else if (use_sdfgi) {
3912
mode = MODE_SDFGI;
3913
} else {
3914
mode = without_sampler ? MODE_VOXEL_GI_WITHOUT_SAMPLER : MODE_VOXEL_GI;
3915
}
3916
3917
for (uint32_t v = 0; v < p_view_count; v++) {
3918
push_constant.view_index = v;
3919
3920
// setup our uniform set
3921
if (rbgi->uniform_set[v].is_null() || !RD::get_singleton()->uniform_set_is_valid(rbgi->uniform_set[v])) {
3922
Vector<RD::Uniform> uniforms;
3923
{
3924
RD::Uniform u;
3925
u.binding = 1;
3926
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3927
for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
3928
if (use_sdfgi && j < sdfgi->cascades.size()) {
3929
u.append_id(sdfgi->cascades[j].sdf_tex);
3930
} else {
3931
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3932
}
3933
}
3934
uniforms.push_back(u);
3935
}
3936
{
3937
RD::Uniform u;
3938
u.binding = 2;
3939
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3940
for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
3941
if (use_sdfgi && j < sdfgi->cascades.size()) {
3942
u.append_id(sdfgi->cascades[j].light_tex);
3943
} else {
3944
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3945
}
3946
}
3947
uniforms.push_back(u);
3948
}
3949
{
3950
RD::Uniform u;
3951
u.binding = 3;
3952
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3953
for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
3954
if (use_sdfgi && j < sdfgi->cascades.size()) {
3955
u.append_id(sdfgi->cascades[j].light_aniso_0_tex);
3956
} else {
3957
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3958
}
3959
}
3960
uniforms.push_back(u);
3961
}
3962
{
3963
RD::Uniform u;
3964
u.binding = 4;
3965
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3966
for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
3967
if (use_sdfgi && j < sdfgi->cascades.size()) {
3968
u.append_id(sdfgi->cascades[j].light_aniso_1_tex);
3969
} else {
3970
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3971
}
3972
}
3973
uniforms.push_back(u);
3974
}
3975
{
3976
RD::Uniform u;
3977
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3978
u.binding = 5;
3979
if (use_sdfgi) {
3980
u.append_id(sdfgi->occlusion_texture);
3981
} else {
3982
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE));
3983
}
3984
uniforms.push_back(u);
3985
}
3986
{
3987
RD::Uniform u;
3988
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
3989
u.binding = 6;
3990
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
3991
uniforms.push_back(u);
3992
}
3993
{
3994
RD::Uniform u;
3995
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
3996
u.binding = 7;
3997
u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
3998
uniforms.push_back(u);
3999
}
4000
{
4001
RD::Uniform u;
4002
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
4003
u.binding = 9;
4004
u.append_id(p_render_buffers->get_texture_slice(RB_SCOPE_GI, RB_TEX_AMBIENT, v, 0));
4005
uniforms.push_back(u);
4006
}
4007
4008
{
4009
RD::Uniform u;
4010
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
4011
u.binding = 10;
4012
u.append_id(p_render_buffers->get_texture_slice(RB_SCOPE_GI, RB_TEX_REFLECTION, v, 0));
4013
uniforms.push_back(u);
4014
}
4015
4016
{
4017
RD::Uniform u;
4018
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4019
u.binding = 11;
4020
if (use_sdfgi) {
4021
u.append_id(sdfgi->lightprobe_texture);
4022
} else {
4023
u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE));
4024
}
4025
uniforms.push_back(u);
4026
}
4027
{
4028
RD::Uniform u;
4029
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4030
u.binding = 12;
4031
u.append_id(p_render_buffers->get_depth_texture(v));
4032
uniforms.push_back(u);
4033
}
4034
{
4035
RD::Uniform u;
4036
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4037
u.binding = 13;
4038
u.append_id(p_normal_roughness_slices[v]);
4039
uniforms.push_back(u);
4040
}
4041
{
4042
RD::Uniform u;
4043
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4044
u.binding = 14;
4045
RID buffer = p_voxel_gi_buffer.is_valid() ? p_voxel_gi_buffer : texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK);
4046
u.append_id(buffer);
4047
uniforms.push_back(u);
4048
}
4049
{
4050
RD::Uniform u;
4051
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
4052
u.binding = 15;
4053
u.append_id(sdfgi_ubo);
4054
uniforms.push_back(u);
4055
}
4056
{
4057
RD::Uniform u;
4058
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
4059
u.binding = 16;
4060
u.append_id(rbgi->get_voxel_gi_buffer());
4061
uniforms.push_back(u);
4062
}
4063
{
4064
RD::Uniform u;
4065
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
4066
u.binding = 17;
4067
for (int i = 0; i < MAX_VOXEL_GI_INSTANCES; i++) {
4068
u.append_id(rbgi->voxel_gi_textures[i]);
4069
}
4070
uniforms.push_back(u);
4071
}
4072
{
4073
RD::Uniform u;
4074
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
4075
u.binding = 18;
4076
u.append_id(rbgi->scene_data_ubo);
4077
uniforms.push_back(u);
4078
}
4079
if (RendererSceneRenderRD::get_singleton()->is_vrs_supported()) {
4080
RD::Uniform u;
4081
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
4082
u.binding = 19;
4083
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);
4084
u.append_id(buffer);
4085
uniforms.push_back(u);
4086
}
4087
4088
bool vrs_supported = RendererSceneRenderRD::get_singleton()->is_vrs_supported();
4089
int variant_base = vrs_supported ? MODE_MAX : 0;
4090
rbgi->uniform_set[v] = RD::get_singleton()->uniform_set_create(uniforms, shader.version_get_shader(shader_version, variant_base), 0);
4091
}
4092
4093
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipelines[pipeline_specialization][mode]);
4094
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rbgi->uniform_set[v], 0);
4095
RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
4096
4097
if (rbgi->using_half_size_gi) {
4098
RD::get_singleton()->compute_list_dispatch_threads(compute_list, internal_size.x >> 1, internal_size.y >> 1, 1);
4099
} else {
4100
RD::get_singleton()->compute_list_dispatch_threads(compute_list, internal_size.x, internal_size.y, 1);
4101
}
4102
}
4103
4104
RD::get_singleton()->compute_list_end();
4105
RD::get_singleton()->draw_command_end_label();
4106
}
4107
4108
RID GI::voxel_gi_instance_create(RID p_base) {
4109
VoxelGIInstance voxel_gi;
4110
voxel_gi.gi = this;
4111
voxel_gi.probe = p_base;
4112
RID rid = voxel_gi_instance_owner.make_rid(voxel_gi);
4113
return rid;
4114
}
4115
4116
void GI::voxel_gi_instance_free(RID p_rid) {
4117
GI::VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_rid);
4118
voxel_gi->free_resources();
4119
voxel_gi_instance_owner.free(p_rid);
4120
}
4121
4122
void GI::voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) {
4123
VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_probe);
4124
ERR_FAIL_NULL(voxel_gi);
4125
4126
voxel_gi->transform = p_xform;
4127
}
4128
4129
bool GI::voxel_gi_needs_update(RID p_probe) const {
4130
VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_probe);
4131
ERR_FAIL_NULL_V(voxel_gi, false);
4132
4133
return voxel_gi->last_probe_version != voxel_gi_get_version(voxel_gi->probe);
4134
}
4135
4136
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) {
4137
VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_probe);
4138
ERR_FAIL_NULL(voxel_gi);
4139
4140
voxel_gi->update(p_update_light_instances, p_light_instances, p_dynamic_objects);
4141
}
4142
4143
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) {
4144
VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_voxel_gi);
4145
ERR_FAIL_NULL(voxel_gi);
4146
4147
voxel_gi->debug(p_draw_list, p_framebuffer, p_camera_with_transform, p_lighting, p_emission, p_alpha);
4148
}
4149
4150
void GI::enable_vrs_shader_group() {
4151
shader.enable_group(GROUP_VRS);
4152
}
4153
4154