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