1/**************************************************************************/
2/* render_forward_clustered.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 "render_forward_clustered.h"
32#include "core/config/project_settings.h"
33#include "core/object/worker_thread_pool.h"
34#include "servers/rendering/renderer_rd/framebuffer_cache_rd.h"
35#include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
36#include "servers/rendering/renderer_rd/storage_rd/light_storage.h"
37#include "servers/rendering/renderer_rd/storage_rd/mesh_storage.h"
38#include "servers/rendering/renderer_rd/storage_rd/particles_storage.h"
39#include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
40#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
41#include "servers/rendering/rendering_device.h"
42#include "servers/rendering/rendering_server_default.h"
43
44using namespace RendererSceneRenderImplementation;
45
46void RenderForwardClustered::RenderBufferDataForwardClustered::ensure_specular() {
47 ERR_FAIL_NULL(render_buffers);
48
49 if (!render_buffers->has_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_SPECULAR)) {
50 RD::DataFormat format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
51 uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
52 if (render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
53 usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
54 } else {
55 usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
56 }
57
58 render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_SPECULAR, format, usage_bits);
59
60 if (render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
61 usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
62 render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_SPECULAR_MSAA, format, usage_bits, render_buffers->get_texture_samples());
63 }
64 }
65}
66
67void RenderForwardClustered::RenderBufferDataForwardClustered::ensure_normal_roughness_texture() {
68 ERR_FAIL_NULL(render_buffers);
69
70 if (!render_buffers->has_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_ROUGHNESS)) {
71 RD::DataFormat format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
72 uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
73
74 if (render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
75 usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
76 } else {
77 usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
78 }
79
80 render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_ROUGHNESS, format, usage_bits);
81
82 if (render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
83 usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
84 render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_ROUGHNESS_MSAA, format, usage_bits, render_buffers->get_texture_samples());
85 }
86 }
87}
88
89void RenderForwardClustered::RenderBufferDataForwardClustered::ensure_voxelgi() {
90 ERR_FAIL_NULL(render_buffers);
91
92 if (!render_buffers->has_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_VOXEL_GI)) {
93 RD::DataFormat format = RD::DATA_FORMAT_R8G8_UINT;
94 uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
95 if (render_buffers->get_msaa_3d() == RS::VIEWPORT_MSAA_DISABLED) {
96 usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
97 }
98
99 render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_VOXEL_GI, format, usage_bits);
100
101 if (render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
102 usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
103 render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_VOXEL_GI_MSAA, format, usage_bits, render_buffers->get_texture_samples());
104 }
105 }
106}
107
108void RenderForwardClustered::RenderBufferDataForwardClustered::free_data() {
109 // JIC, should already have been cleared
110 if (render_buffers) {
111 render_buffers->clear_context(RB_SCOPE_FORWARD_CLUSTERED);
112 render_buffers->clear_context(RB_SCOPE_SSDS);
113 render_buffers->clear_context(RB_SCOPE_SSIL);
114 render_buffers->clear_context(RB_SCOPE_SSAO);
115 render_buffers->clear_context(RB_SCOPE_SSR);
116 }
117
118 if (cluster_builder) {
119 memdelete(cluster_builder);
120 cluster_builder = nullptr;
121 }
122
123 if (!render_sdfgi_uniform_set.is_null() && RD::get_singleton()->uniform_set_is_valid(render_sdfgi_uniform_set)) {
124 RD::get_singleton()->free(render_sdfgi_uniform_set);
125 }
126}
127
128void RenderForwardClustered::RenderBufferDataForwardClustered::configure(RenderSceneBuffersRD *p_render_buffers) {
129 if (render_buffers) {
130 // JIC
131 free_data();
132 }
133
134 render_buffers = p_render_buffers;
135 ERR_FAIL_NULL(render_buffers);
136
137 if (cluster_builder == nullptr) {
138 cluster_builder = memnew(ClusterBuilderRD);
139 }
140 cluster_builder->set_shared(RenderForwardClustered::get_singleton()->get_cluster_builder_shared());
141
142 RID sampler = RendererRD::MaterialStorage::get_singleton()->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
143 cluster_builder->setup(p_render_buffers->get_internal_size(), p_render_buffers->get_max_cluster_elements(), p_render_buffers->get_depth_texture(), sampler, p_render_buffers->get_internal_texture());
144}
145
146RID RenderForwardClustered::RenderBufferDataForwardClustered::get_color_only_fb() {
147 ERR_FAIL_NULL_V(render_buffers, RID());
148
149 bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED;
150
151 RID color = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA) : render_buffers->get_internal_texture();
152 RID depth = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA) : render_buffers->get_depth_texture();
153
154 if (render_buffers->has_texture(RB_SCOPE_VRS, RB_TEXTURE)) {
155 RID vrs_texture = render_buffers->get_texture(RB_SCOPE_VRS, RB_TEXTURE);
156
157 return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), color, depth, vrs_texture);
158 } else {
159 return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), color, depth);
160 }
161}
162
163RID RenderForwardClustered::RenderBufferDataForwardClustered::get_color_pass_fb(uint32_t p_color_pass_flags) {
164 ERR_FAIL_NULL_V(render_buffers, RID());
165 bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED;
166
167 int v_count = (p_color_pass_flags & COLOR_PASS_FLAG_MULTIVIEW) ? render_buffers->get_view_count() : 1;
168 RID color = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA) : render_buffers->get_internal_texture();
169
170 RID specular;
171 if (p_color_pass_flags & COLOR_PASS_FLAG_SEPARATE_SPECULAR) {
172 ensure_specular();
173 specular = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_SPECULAR_MSAA : RB_TEX_SPECULAR);
174 }
175
176 RID velocity_buffer;
177 if (p_color_pass_flags & COLOR_PASS_FLAG_MOTION_VECTORS) {
178 render_buffers->ensure_velocity();
179 velocity_buffer = render_buffers->get_velocity_buffer(use_msaa);
180 }
181
182 RID depth = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA) : render_buffers->get_depth_texture();
183
184 if (render_buffers->has_texture(RB_SCOPE_VRS, RB_TEXTURE)) {
185 RID vrs_texture = render_buffers->get_texture(RB_SCOPE_VRS, RB_TEXTURE);
186
187 return FramebufferCacheRD::get_singleton()->get_cache_multiview(v_count, color, specular, velocity_buffer, depth, vrs_texture);
188 } else {
189 return FramebufferCacheRD::get_singleton()->get_cache_multiview(v_count, color, specular, velocity_buffer, depth);
190 }
191}
192
193RID RenderForwardClustered::RenderBufferDataForwardClustered::get_depth_fb(DepthFrameBufferType p_type) {
194 ERR_FAIL_NULL_V(render_buffers, RID());
195 bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED;
196
197 RID depth = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA) : render_buffers->get_depth_texture();
198
199 switch (p_type) {
200 case DEPTH_FB: {
201 return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), depth);
202 } break;
203 case DEPTH_FB_ROUGHNESS: {
204 ensure_normal_roughness_texture();
205
206 RID normal_roughness_buffer = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_ROUGHNESS_MSAA : RB_TEX_ROUGHNESS);
207
208 return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), depth, normal_roughness_buffer);
209 } break;
210 case DEPTH_FB_ROUGHNESS_VOXELGI: {
211 ensure_normal_roughness_texture();
212 ensure_voxelgi();
213
214 RID normal_roughness_buffer = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_ROUGHNESS_MSAA : RB_TEX_ROUGHNESS);
215 RID voxelgi_buffer = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_VOXEL_GI_MSAA : RB_TEX_VOXEL_GI);
216
217 return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), depth, normal_roughness_buffer, voxelgi_buffer);
218 } break;
219 default: {
220 ERR_FAIL_V(RID());
221 } break;
222 }
223}
224
225RID RenderForwardClustered::RenderBufferDataForwardClustered::get_specular_only_fb() {
226 bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED;
227
228 RID specular = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_SPECULAR_MSAA : RB_TEX_SPECULAR);
229
230 return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), specular);
231}
232
233void RenderForwardClustered::setup_render_buffer_data(Ref<RenderSceneBuffersRD> p_render_buffers) {
234 Ref<RenderBufferDataForwardClustered> data;
235 data.instantiate();
236 p_render_buffers->set_custom_data(RB_SCOPE_FORWARD_CLUSTERED, data);
237
238 Ref<RendererRD::GI::RenderBuffersGI> rbgi;
239 rbgi.instantiate();
240 p_render_buffers->set_custom_data(RB_SCOPE_GI, rbgi);
241}
242
243bool RenderForwardClustered::free(RID p_rid) {
244 if (RendererSceneRenderRD::free(p_rid)) {
245 return true;
246 }
247 return false;
248}
249
250/// RENDERING ///
251
252template <RenderForwardClustered::PassMode p_pass_mode, uint32_t p_color_pass_flags>
253void RenderForwardClustered::_render_list_template(RenderingDevice::DrawListID p_draw_list, RenderingDevice::FramebufferFormatID p_framebuffer_Format, RenderListParameters *p_params, uint32_t p_from_element, uint32_t p_to_element) {
254 RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton();
255 RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton();
256 RD::DrawListID draw_list = p_draw_list;
257 RD::FramebufferFormatID framebuffer_format = p_framebuffer_Format;
258
259 //global scope bindings
260 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, render_base_uniform_set, SCENE_UNIFORM_SET);
261 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_params->render_pass_uniform_set, RENDER_PASS_UNIFORM_SET);
262 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, scene_shader.default_vec4_xform_uniform_set, TRANSFORMS_UNIFORM_SET);
263
264 RID prev_material_uniform_set;
265
266 RID prev_vertex_array_rd;
267 RID prev_index_array_rd;
268 RID prev_pipeline_rd;
269 RID prev_xforms_uniform_set;
270
271 bool shadow_pass = (p_pass_mode == PASS_MODE_SHADOW) || (p_pass_mode == PASS_MODE_SHADOW_DP);
272
273 SceneState::PushConstant push_constant;
274
275 if constexpr (p_pass_mode == PASS_MODE_DEPTH_MATERIAL) {
276 push_constant.uv_offset = Math::make_half_float(p_params->uv_offset.y) << 16;
277 push_constant.uv_offset |= Math::make_half_float(p_params->uv_offset.x);
278 } else {
279 push_constant.uv_offset = 0;
280 }
281
282 bool should_request_redraw = false;
283
284 for (uint32_t i = p_from_element; i < p_to_element; i++) {
285 const GeometryInstanceSurfaceDataCache *surf = p_params->elements[i];
286 const RenderElementInfo &element_info = p_params->element_info[i];
287
288 if ((p_pass_mode == PASS_MODE_COLOR && !(p_color_pass_flags & COLOR_PASS_FLAG_TRANSPARENT)) && !(surf->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE)) {
289 continue; // Objects with "Depth-prepass" transparency are included in both render lists, but should only be rendered in the transparent pass
290 }
291
292 if (surf->owner->instance_count == 0) {
293 continue;
294 }
295
296 push_constant.base_index = i + p_params->element_offset;
297
298 RID material_uniform_set;
299 SceneShaderForwardClustered::ShaderData *shader;
300 void *mesh_surface;
301
302 if (shadow_pass || p_pass_mode == PASS_MODE_DEPTH) { //regular depth pass can use these too
303 material_uniform_set = surf->material_uniform_set_shadow;
304 shader = surf->shader_shadow;
305 mesh_surface = surf->surface_shadow;
306
307 } else {
308#ifdef DEBUG_ENABLED
309 if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_LIGHTING)) {
310 material_uniform_set = scene_shader.default_material_uniform_set;
311 shader = scene_shader.default_material_shader_ptr;
312 } else if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW)) {
313 material_uniform_set = scene_shader.overdraw_material_uniform_set;
314 shader = scene_shader.overdraw_material_shader_ptr;
315 } else if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_PSSM_SPLITS)) {
316 material_uniform_set = scene_shader.debug_shadow_splits_material_uniform_set;
317 shader = scene_shader.debug_shadow_splits_material_shader_ptr;
318 } else {
319#endif
320 material_uniform_set = surf->material_uniform_set;
321 shader = surf->shader;
322 surf->material->set_as_used();
323#ifdef DEBUG_ENABLED
324 }
325#endif
326 mesh_surface = surf->surface;
327 }
328
329 if (!mesh_surface) {
330 continue;
331 }
332
333 //request a redraw if one of the shaders uses TIME
334 if (shader->uses_time) {
335 should_request_redraw = true;
336 }
337
338 //find cull variant
339 SceneShaderForwardClustered::ShaderData::CullVariant cull_variant;
340
341 if (p_pass_mode == PASS_MODE_DEPTH_MATERIAL || p_pass_mode == PASS_MODE_SDF || ((p_pass_mode == PASS_MODE_SHADOW || p_pass_mode == PASS_MODE_SHADOW_DP) && surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_DOUBLE_SIDED_SHADOWS)) {
342 cull_variant = SceneShaderForwardClustered::ShaderData::CULL_VARIANT_DOUBLE_SIDED;
343 } else {
344 bool mirror = surf->owner->mirror;
345 if (p_params->reverse_cull) {
346 mirror = !mirror;
347 }
348 cull_variant = mirror ? SceneShaderForwardClustered::ShaderData::CULL_VARIANT_REVERSED : SceneShaderForwardClustered::ShaderData::CULL_VARIANT_NORMAL;
349 }
350
351 RS::PrimitiveType primitive = surf->primitive;
352 RID xforms_uniform_set = surf->owner->transforms_uniform_set;
353
354 SceneShaderForwardClustered::PipelineVersion pipeline_version = SceneShaderForwardClustered::PIPELINE_VERSION_MAX; // Assigned to silence wrong -Wmaybe-initialized.
355 uint32_t pipeline_color_pass_flags = 0;
356 uint32_t pipeline_specialization = 0;
357
358 if constexpr (p_pass_mode == PASS_MODE_COLOR) {
359 if (element_info.uses_softshadow) {
360 pipeline_specialization |= SceneShaderForwardClustered::SHADER_SPECIALIZATION_SOFT_SHADOWS;
361 }
362 if (element_info.uses_projector) {
363 pipeline_specialization |= SceneShaderForwardClustered::SHADER_SPECIALIZATION_PROJECTOR;
364 }
365
366 if (p_params->use_directional_soft_shadow) {
367 pipeline_specialization |= SceneShaderForwardClustered::SHADER_SPECIALIZATION_DIRECTIONAL_SOFT_SHADOWS;
368 }
369 }
370
371 switch (p_pass_mode) {
372 case PASS_MODE_COLOR: {
373 if (element_info.uses_lightmap) {
374 pipeline_color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_LIGHTMAP;
375 } else {
376 if (element_info.uses_forward_gi) {
377 pipeline_specialization |= SceneShaderForwardClustered::SHADER_SPECIALIZATION_FORWARD_GI;
378 }
379 }
380
381 if constexpr ((p_color_pass_flags & COLOR_PASS_FLAG_SEPARATE_SPECULAR) != 0) {
382 pipeline_color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_SEPARATE_SPECULAR;
383 }
384
385 if constexpr ((p_color_pass_flags & COLOR_PASS_FLAG_MOTION_VECTORS) != 0) {
386 pipeline_color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MOTION_VECTORS;
387 }
388
389 if constexpr ((p_color_pass_flags & COLOR_PASS_FLAG_TRANSPARENT) != 0) {
390 pipeline_color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_TRANSPARENT;
391 }
392
393 if constexpr ((p_color_pass_flags & COLOR_PASS_FLAG_MULTIVIEW) != 0) {
394 pipeline_color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MULTIVIEW;
395 }
396
397 pipeline_version = SceneShaderForwardClustered::PIPELINE_VERSION_COLOR_PASS;
398 } break;
399 case PASS_MODE_SHADOW:
400 case PASS_MODE_DEPTH: {
401 pipeline_version = p_params->view_count > 1 ? SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_MULTIVIEW : SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS;
402 } break;
403 case PASS_MODE_SHADOW_DP: {
404 ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for shadow DP pass");
405 pipeline_version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_DP;
406 } break;
407 case PASS_MODE_DEPTH_NORMAL_ROUGHNESS: {
408 pipeline_version = p_params->view_count > 1 ? SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_MULTIVIEW : SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS;
409 } break;
410 case PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI: {
411 pipeline_version = p_params->view_count > 1 ? SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI_MULTIVIEW : SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI;
412 } break;
413 case PASS_MODE_DEPTH_MATERIAL: {
414 ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for material pass");
415 pipeline_version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_MATERIAL;
416 } break;
417 case PASS_MODE_SDF: {
418 // Note, SDF is prepared in world space, this shouldn't be a multiview buffer even when stereoscopic rendering is used.
419 ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for SDF pass");
420 pipeline_version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_SDF;
421 } break;
422 }
423
424 PipelineCacheRD *pipeline = nullptr;
425
426 if constexpr (p_pass_mode == PASS_MODE_COLOR) {
427 pipeline = &shader->color_pipelines[cull_variant][primitive][pipeline_color_pass_flags];
428 } else {
429 pipeline = &shader->pipelines[cull_variant][primitive][pipeline_version];
430 }
431
432 RD::VertexFormatID vertex_format = -1;
433 RID vertex_array_rd;
434 RID index_array_rd;
435
436 //skeleton and blend shape
437 bool pipeline_motion_vectors = pipeline_color_pass_flags & SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MOTION_VECTORS;
438 if (surf->owner->mesh_instance.is_valid()) {
439 mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, pipeline->get_vertex_input_mask(), pipeline_motion_vectors, vertex_array_rd, vertex_format);
440 } else {
441 mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, pipeline->get_vertex_input_mask(), pipeline_motion_vectors, vertex_array_rd, vertex_format);
442 }
443
444 index_array_rd = mesh_storage->mesh_surface_get_index_array(mesh_surface, element_info.lod_index);
445
446 if (prev_vertex_array_rd != vertex_array_rd) {
447 RD::get_singleton()->draw_list_bind_vertex_array(draw_list, vertex_array_rd);
448 prev_vertex_array_rd = vertex_array_rd;
449 }
450
451 if (prev_index_array_rd != index_array_rd) {
452 if (index_array_rd.is_valid()) {
453 RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array_rd);
454 }
455 prev_index_array_rd = index_array_rd;
456 }
457
458 RID pipeline_rd = pipeline->get_render_pipeline(vertex_format, framebuffer_format, p_params->force_wireframe, 0, pipeline_specialization);
459
460 if (pipeline_rd != prev_pipeline_rd) {
461 // checking with prev shader does not make so much sense, as
462 // the pipeline may still be different.
463 RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline_rd);
464 prev_pipeline_rd = pipeline_rd;
465 }
466
467 if (xforms_uniform_set.is_valid() && prev_xforms_uniform_set != xforms_uniform_set) {
468 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, xforms_uniform_set, TRANSFORMS_UNIFORM_SET);
469 prev_xforms_uniform_set = xforms_uniform_set;
470 }
471
472 if (material_uniform_set != prev_material_uniform_set) {
473 // Update uniform set.
474 if (material_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(material_uniform_set)) { // Material may not have a uniform set.
475 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, material_uniform_set, MATERIAL_UNIFORM_SET);
476 }
477
478 prev_material_uniform_set = material_uniform_set;
479 }
480
481 if (surf->owner->base_flags & INSTANCE_DATA_FLAG_PARTICLES) {
482 particles_storage->particles_get_instance_buffer_motion_vectors_offsets(surf->owner->data->base, push_constant.multimesh_motion_vectors_current_offset, push_constant.multimesh_motion_vectors_previous_offset);
483 } else if (surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH) {
484 mesh_storage->_multimesh_get_motion_vectors_offsets(surf->owner->data->base, push_constant.multimesh_motion_vectors_current_offset, push_constant.multimesh_motion_vectors_previous_offset);
485 } else {
486 push_constant.multimesh_motion_vectors_current_offset = 0;
487 push_constant.multimesh_motion_vectors_previous_offset = 0;
488 }
489
490 RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(SceneState::PushConstant));
491
492 uint32_t instance_count = surf->owner->instance_count > 1 ? surf->owner->instance_count : element_info.repeat;
493 if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_PARTICLE_TRAILS) {
494 instance_count /= surf->owner->trail_steps;
495 }
496
497 RD::get_singleton()->draw_list_draw(draw_list, index_array_rd.is_valid(), instance_count);
498 i += element_info.repeat - 1; //skip equal elements
499 }
500
501 // Make the actual redraw request
502 if (should_request_redraw) {
503 RenderingServerDefault::redraw_request();
504 }
505}
506
507void RenderForwardClustered::_render_list(RenderingDevice::DrawListID p_draw_list, RenderingDevice::FramebufferFormatID p_framebuffer_Format, RenderListParameters *p_params, uint32_t p_from_element, uint32_t p_to_element) {
508 //use template for faster performance (pass mode comparisons are inlined)
509
510 switch (p_params->pass_mode) {
511#define VALID_FLAG_COMBINATION(f) \
512 case f: { \
513 _render_list_template<PASS_MODE_COLOR, f>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); \
514 } break;
515
516 case PASS_MODE_COLOR: {
517 switch (p_params->color_pass_flags) {
518 VALID_FLAG_COMBINATION(0);
519 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_TRANSPARENT);
520 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_TRANSPARENT | COLOR_PASS_FLAG_MULTIVIEW);
521 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_TRANSPARENT | COLOR_PASS_FLAG_MOTION_VECTORS);
522 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_SEPARATE_SPECULAR);
523 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_SEPARATE_SPECULAR | COLOR_PASS_FLAG_MULTIVIEW);
524 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_SEPARATE_SPECULAR | COLOR_PASS_FLAG_MOTION_VECTORS);
525 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_MULTIVIEW);
526 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_MULTIVIEW | COLOR_PASS_FLAG_MOTION_VECTORS);
527 VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_MOTION_VECTORS);
528 default: {
529 ERR_FAIL_MSG("Invalid color pass flag combination " + itos(p_params->color_pass_flags));
530 }
531 }
532
533 } break;
534 case PASS_MODE_SHADOW: {
535 _render_list_template<PASS_MODE_SHADOW>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element);
536 } break;
537 case PASS_MODE_SHADOW_DP: {
538 _render_list_template<PASS_MODE_SHADOW_DP>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element);
539 } break;
540 case PASS_MODE_DEPTH: {
541 _render_list_template<PASS_MODE_DEPTH>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element);
542 } break;
543 case PASS_MODE_DEPTH_NORMAL_ROUGHNESS: {
544 _render_list_template<PASS_MODE_DEPTH_NORMAL_ROUGHNESS>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element);
545 } break;
546 case PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI: {
547 _render_list_template<PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element);
548 } break;
549 case PASS_MODE_DEPTH_MATERIAL: {
550 _render_list_template<PASS_MODE_DEPTH_MATERIAL>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element);
551 } break;
552 case PASS_MODE_SDF: {
553 _render_list_template<PASS_MODE_SDF>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element);
554 } break;
555 }
556}
557
558void RenderForwardClustered::_render_list_thread_function(uint32_t p_thread, RenderListParameters *p_params) {
559 uint32_t render_total = p_params->element_count;
560 uint32_t total_threads = WorkerThreadPool::get_singleton()->get_thread_count();
561 uint32_t render_from = p_thread * render_total / total_threads;
562 uint32_t render_to = (p_thread + 1 == total_threads) ? render_total : ((p_thread + 1) * render_total / total_threads);
563 _render_list(thread_draw_lists[p_thread], p_params->framebuffer_format, p_params, render_from, render_to);
564}
565
566void RenderForwardClustered::_render_list_with_threads(RenderListParameters *p_params, RID p_framebuffer, RD::InitialAction p_initial_color_action, RD::FinalAction p_final_color_action, RD::InitialAction p_initial_depth_action, RD::FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const Vector<RID> &p_storage_textures) {
567 RD::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(p_framebuffer);
568 p_params->framebuffer_format = fb_format;
569
570 if ((uint32_t)p_params->element_count > render_list_thread_threshold && false) { // secondary command buffers need more testing at this time
571 //multi threaded
572 thread_draw_lists.resize(WorkerThreadPool::get_singleton()->get_thread_count());
573 RD::get_singleton()->draw_list_begin_split(p_framebuffer, thread_draw_lists.size(), thread_draw_lists.ptr(), p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, p_clear_color_values, p_clear_depth, p_clear_stencil, p_region, p_storage_textures);
574 WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &RenderForwardClustered::_render_list_thread_function, p_params, thread_draw_lists.size(), -1, true, SNAME("ForwardClusteredRenderList"));
575 WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
576 RD::get_singleton()->draw_list_end(p_params->barrier);
577 } else {
578 //single threaded
579 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer, p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, p_clear_color_values, p_clear_depth, p_clear_stencil, p_region, p_storage_textures);
580 _render_list(draw_list, fb_format, p_params, 0, p_params->element_count);
581 RD::get_singleton()->draw_list_end(p_params->barrier);
582 }
583}
584
585void RenderForwardClustered::_setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_opaque_render_buffers, bool p_pancake_shadows, int p_index) {
586 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
587
588 Ref<RenderSceneBuffersRD> rd = p_render_data->render_buffers;
589 RID env = is_environment(p_render_data->environment) ? p_render_data->environment : RID();
590 RID reflection_probe_instance = p_render_data->reflection_probe.is_valid() ? light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe) : RID();
591
592 // May do this earlier in RenderSceneRenderRD::render_scene
593 if (p_index >= (int)scene_state.uniform_buffers.size()) {
594 uint32_t from = scene_state.uniform_buffers.size();
595 scene_state.uniform_buffers.resize(p_index + 1);
596 for (uint32_t i = from; i < scene_state.uniform_buffers.size(); i++) {
597 scene_state.uniform_buffers[i] = p_render_data->scene_data->create_uniform_buffer();
598 }
599 }
600
601 p_render_data->scene_data->update_ubo(scene_state.uniform_buffers[p_index], get_debug_draw_mode(), env, reflection_probe_instance, p_render_data->camera_attributes, p_flip_y, p_pancake_shadows, p_screen_size, p_default_bg_color, _render_buffers_get_luminance_multiplier(), p_opaque_render_buffers);
602
603 // now do implementation UBO
604
605 scene_state.ubo.cluster_shift = get_shift_from_power_of_2(p_render_data->cluster_size);
606 scene_state.ubo.max_cluster_element_count_div_32 = p_render_data->cluster_max_elements / 32;
607 {
608 uint32_t cluster_screen_width = (p_screen_size.width - 1) / p_render_data->cluster_size + 1;
609 uint32_t cluster_screen_height = (p_screen_size.height - 1) / p_render_data->cluster_size + 1;
610 scene_state.ubo.cluster_type_size = cluster_screen_width * cluster_screen_height * (scene_state.ubo.max_cluster_element_count_div_32 + 32);
611 scene_state.ubo.cluster_width = cluster_screen_width;
612 }
613
614 scene_state.ubo.gi_upscale_for_msaa = false;
615 scene_state.ubo.volumetric_fog_enabled = false;
616
617 if (rd.is_valid()) {
618 if (rd->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
619 scene_state.ubo.gi_upscale_for_msaa = true;
620 }
621
622 if (rd->has_custom_data(RB_SCOPE_FOG)) {
623 Ref<RendererRD::Fog::VolumetricFog> fog = rd->get_custom_data(RB_SCOPE_FOG);
624
625 scene_state.ubo.volumetric_fog_enabled = true;
626 float fog_end = fog->length;
627 if (fog_end > 0.0) {
628 scene_state.ubo.volumetric_fog_inv_length = 1.0 / fog_end;
629 } else {
630 scene_state.ubo.volumetric_fog_inv_length = 1.0;
631 }
632
633 float fog_detail_spread = fog->spread; //reverse lookup
634 if (fog_detail_spread > 0.0) {
635 scene_state.ubo.volumetric_fog_detail_spread = 1.0 / fog_detail_spread;
636 } else {
637 scene_state.ubo.volumetric_fog_detail_spread = 1.0;
638 }
639 }
640 }
641
642 if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
643 scene_state.ubo.ss_effects_flags = 0;
644 } else if (p_render_data->reflection_probe.is_null() && is_environment(p_render_data->environment)) {
645 scene_state.ubo.ssao_ao_affect = environment_get_ssao_ao_channel_affect(p_render_data->environment);
646 scene_state.ubo.ssao_light_affect = environment_get_ssao_direct_light_affect(p_render_data->environment);
647 uint32_t ss_flags = 0;
648 if (p_opaque_render_buffers) {
649 ss_flags |= environment_get_ssao_enabled(p_render_data->environment) ? 1 : 0;
650 ss_flags |= environment_get_ssil_enabled(p_render_data->environment) ? 2 : 0;
651 }
652 scene_state.ubo.ss_effects_flags = ss_flags;
653 } else {
654 scene_state.ubo.ss_effects_flags = 0;
655 }
656
657 if (p_index >= (int)scene_state.implementation_uniform_buffers.size()) {
658 uint32_t from = scene_state.implementation_uniform_buffers.size();
659 scene_state.implementation_uniform_buffers.resize(p_index + 1);
660 for (uint32_t i = from; i < scene_state.implementation_uniform_buffers.size(); i++) {
661 scene_state.implementation_uniform_buffers[i] = RD::get_singleton()->uniform_buffer_create(sizeof(SceneState::UBO));
662 }
663 }
664
665 RD::get_singleton()->buffer_update(scene_state.implementation_uniform_buffers[p_index], 0, sizeof(SceneState::UBO), &scene_state.ubo, RD::BARRIER_MASK_RASTER);
666}
667
668void RenderForwardClustered::_update_instance_data_buffer(RenderListType p_render_list) {
669 if (scene_state.instance_data[p_render_list].size() > 0) {
670 if (scene_state.instance_buffer[p_render_list] == RID() || scene_state.instance_buffer_size[p_render_list] < scene_state.instance_data[p_render_list].size()) {
671 if (scene_state.instance_buffer[p_render_list] != RID()) {
672 RD::get_singleton()->free(scene_state.instance_buffer[p_render_list]);
673 }
674 uint32_t new_size = nearest_power_of_2_templated(MAX(uint64_t(INSTANCE_DATA_BUFFER_MIN_SIZE), scene_state.instance_data[p_render_list].size()));
675 scene_state.instance_buffer[p_render_list] = RD::get_singleton()->storage_buffer_create(new_size * sizeof(SceneState::InstanceData));
676 scene_state.instance_buffer_size[p_render_list] = new_size;
677 }
678 RD::get_singleton()->buffer_update(scene_state.instance_buffer[p_render_list], 0, sizeof(SceneState::InstanceData) * scene_state.instance_data[p_render_list].size(), scene_state.instance_data[p_render_list].ptr(), RD::BARRIER_MASK_RASTER);
679 }
680}
681void RenderForwardClustered::_fill_instance_data(RenderListType p_render_list, int *p_render_info, uint32_t p_offset, int32_t p_max_elements, bool p_update_buffer) {
682 RenderList *rl = &render_list[p_render_list];
683 uint32_t element_total = p_max_elements >= 0 ? uint32_t(p_max_elements) : rl->elements.size();
684
685 scene_state.instance_data[p_render_list].resize(p_offset + element_total);
686 rl->element_info.resize(p_offset + element_total);
687
688 if (p_render_info) {
689 p_render_info[RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] += element_total;
690 }
691 uint64_t frame = RSG::rasterizer->get_frame_number();
692 uint32_t repeats = 0;
693 GeometryInstanceSurfaceDataCache *prev_surface = nullptr;
694 for (uint32_t i = 0; i < element_total; i++) {
695 GeometryInstanceSurfaceDataCache *surface = rl->elements[i + p_offset];
696 GeometryInstanceForwardClustered *inst = surface->owner;
697
698 SceneState::InstanceData &instance_data = scene_state.instance_data[p_render_list][i + p_offset];
699
700 if (inst->prev_transform_dirty && frame > inst->prev_transform_change_frame + 1 && inst->prev_transform_change_frame) {
701 inst->prev_transform = inst->transform;
702 inst->prev_transform_dirty = false;
703 }
704
705 if (inst->store_transform_cache) {
706 RendererRD::MaterialStorage::store_transform(inst->transform, instance_data.transform);
707 RendererRD::MaterialStorage::store_transform(inst->prev_transform, instance_data.prev_transform);
708
709#ifdef REAL_T_IS_DOUBLE
710 // Split the origin into two components, the float approximation and the missing precision
711 // In the shader we will combine these back together to restore the lost precision.
712 RendererRD::MaterialStorage::split_double(inst->transform.origin.x, &instance_data.transform[12], &instance_data.transform[3]);
713 RendererRD::MaterialStorage::split_double(inst->transform.origin.y, &instance_data.transform[13], &instance_data.transform[7]);
714 RendererRD::MaterialStorage::split_double(inst->transform.origin.z, &instance_data.transform[14], &instance_data.transform[11]);
715#endif
716 } else {
717 RendererRD::MaterialStorage::store_transform(Transform3D(), instance_data.transform);
718 RendererRD::MaterialStorage::store_transform(Transform3D(), instance_data.prev_transform);
719 }
720
721 instance_data.flags = inst->flags_cache;
722 instance_data.gi_offset = inst->gi_offset_cache;
723 instance_data.layer_mask = inst->layer_mask;
724 instance_data.instance_uniforms_ofs = uint32_t(inst->shader_uniforms_offset);
725 instance_data.lightmap_uv_scale[0] = inst->lightmap_uv_scale.position.x;
726 instance_data.lightmap_uv_scale[1] = inst->lightmap_uv_scale.position.y;
727 instance_data.lightmap_uv_scale[2] = inst->lightmap_uv_scale.size.x;
728 instance_data.lightmap_uv_scale[3] = inst->lightmap_uv_scale.size.y;
729
730 bool cant_repeat = instance_data.flags & INSTANCE_DATA_FLAG_MULTIMESH || inst->mesh_instance.is_valid();
731
732 if (prev_surface != nullptr && !cant_repeat && prev_surface->sort.sort_key1 == surface->sort.sort_key1 && prev_surface->sort.sort_key2 == surface->sort.sort_key2 && inst->mirror == prev_surface->owner->mirror && repeats < RenderElementInfo::MAX_REPEATS) {
733 //this element is the same as the previous one, count repeats to draw it using instancing
734 repeats++;
735 } else {
736 if (repeats > 0) {
737 for (uint32_t j = 1; j <= repeats; j++) {
738 rl->element_info[p_offset + i - j].repeat = j;
739 }
740 }
741 repeats = 1;
742 if (p_render_info) {
743 p_render_info[RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME]++;
744 }
745 }
746
747 RenderElementInfo &element_info = rl->element_info[p_offset + i];
748
749 element_info.lod_index = surface->sort.lod_index;
750 element_info.uses_forward_gi = surface->sort.uses_forward_gi;
751 element_info.uses_lightmap = surface->sort.uses_lightmap;
752 element_info.uses_softshadow = surface->sort.uses_softshadow;
753 element_info.uses_projector = surface->sort.uses_projector;
754
755 if (cant_repeat) {
756 prev_surface = nullptr;
757 } else {
758 prev_surface = surface;
759 }
760 }
761
762 if (repeats > 0) {
763 for (uint32_t j = 1; j <= repeats; j++) {
764 rl->element_info[p_offset + element_total - j].repeat = j;
765 }
766 }
767
768 if (p_update_buffer) {
769 _update_instance_data_buffer(p_render_list);
770 }
771}
772
773_FORCE_INLINE_ static uint32_t _indices_to_primitives(RS::PrimitiveType p_primitive, uint32_t p_indices) {
774 static const uint32_t divisor[RS::PRIMITIVE_MAX] = { 1, 2, 1, 3, 1 };
775 static const uint32_t subtractor[RS::PRIMITIVE_MAX] = { 0, 0, 1, 0, 1 };
776 return (p_indices - subtractor[p_primitive]) / divisor[p_primitive];
777}
778void RenderForwardClustered::_fill_render_list(RenderListType p_render_list, const RenderDataRD *p_render_data, PassMode p_pass_mode, uint32_t p_color_pass_flags = 0, bool p_using_sdfgi, bool p_using_opaque_gi, bool p_append) {
779 RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton();
780
781 if (p_render_list == RENDER_LIST_OPAQUE) {
782 scene_state.used_sss = false;
783 scene_state.used_screen_texture = false;
784 scene_state.used_normal_texture = false;
785 scene_state.used_depth_texture = false;
786 scene_state.used_lightmap = false;
787 }
788 uint32_t lightmap_captures_used = 0;
789
790 Plane near_plane = Plane(-p_render_data->scene_data->cam_transform.basis.get_column(Vector3::AXIS_Z), p_render_data->scene_data->cam_transform.origin);
791 near_plane.d += p_render_data->scene_data->cam_projection.get_z_near();
792 float z_max = p_render_data->scene_data->cam_projection.get_z_far() - p_render_data->scene_data->cam_projection.get_z_near();
793
794 RenderList *rl = &render_list[p_render_list];
795 _update_dirty_geometry_instances();
796
797 if (!p_append) {
798 rl->clear();
799 if (p_render_list == RENDER_LIST_OPAQUE) {
800 render_list[RENDER_LIST_ALPHA].clear(); //opaque fills alpha too
801 }
802 }
803
804 //fill list
805
806 for (int i = 0; i < (int)p_render_data->instances->size(); i++) {
807 GeometryInstanceForwardClustered *inst = static_cast<GeometryInstanceForwardClustered *>((*p_render_data->instances)[i]);
808
809 Vector3 center = inst->transform.origin;
810 if (p_render_data->scene_data->cam_orthogonal) {
811 if (inst->use_aabb_center) {
812 center = inst->transformed_aabb.get_support(-near_plane.normal);
813 }
814 inst->depth = near_plane.distance_to(center) - inst->sorting_offset;
815 } else {
816 if (inst->use_aabb_center) {
817 center = inst->transformed_aabb.position + (inst->transformed_aabb.size * 0.5);
818 }
819 inst->depth = p_render_data->scene_data->cam_transform.origin.distance_to(center) - inst->sorting_offset;
820 }
821 uint32_t depth_layer = CLAMP(int(inst->depth * 16 / z_max), 0, 15);
822
823 uint32_t flags = inst->base_flags; //fill flags if appropriate
824
825 if (inst->non_uniform_scale) {
826 flags |= INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE;
827 }
828 bool uses_lightmap = false;
829 bool uses_gi = false;
830 float fade_alpha = 1.0;
831
832 if (inst->fade_near || inst->fade_far) {
833 float fade_dist = inst->transform.origin.distance_to(p_render_data->scene_data->cam_transform.origin);
834 // Use `smoothstep()` to make opacity changes more gradual and less noticeable to the player.
835 if (inst->fade_far && fade_dist > inst->fade_far_begin) {
836 fade_alpha = Math::smoothstep(0.0f, 1.0f, 1.0f - (fade_dist - inst->fade_far_begin) / (inst->fade_far_end - inst->fade_far_begin));
837 } else if (inst->fade_near && fade_dist < inst->fade_near_end) {
838 fade_alpha = Math::smoothstep(0.0f, 1.0f, (fade_dist - inst->fade_near_begin) / (inst->fade_near_end - inst->fade_near_begin));
839 }
840 }
841
842 fade_alpha *= inst->force_alpha * inst->parent_fade_alpha;
843
844 flags = (flags & ~INSTANCE_DATA_FLAGS_FADE_MASK) | (uint32_t(fade_alpha * 255.0) << INSTANCE_DATA_FLAGS_FADE_SHIFT);
845
846 if (p_render_list == RENDER_LIST_OPAQUE) {
847 // Setup GI
848 if (inst->lightmap_instance.is_valid()) {
849 int32_t lightmap_cull_index = -1;
850 for (uint32_t j = 0; j < scene_state.lightmaps_used; j++) {
851 if (scene_state.lightmap_ids[j] == inst->lightmap_instance) {
852 lightmap_cull_index = j;
853 break;
854 }
855 }
856 if (lightmap_cull_index >= 0) {
857 inst->gi_offset_cache = inst->lightmap_slice_index << 16;
858 inst->gi_offset_cache |= lightmap_cull_index;
859 flags |= INSTANCE_DATA_FLAG_USE_LIGHTMAP;
860 if (scene_state.lightmap_has_sh[lightmap_cull_index]) {
861 flags |= INSTANCE_DATA_FLAG_USE_SH_LIGHTMAP;
862 }
863 uses_lightmap = true;
864 } else {
865 inst->gi_offset_cache = 0xFFFFFFFF;
866 }
867
868 } else if (inst->lightmap_sh) {
869 if (lightmap_captures_used < scene_state.max_lightmap_captures) {
870 const Color *src_capture = inst->lightmap_sh->sh;
871 LightmapCaptureData &lcd = scene_state.lightmap_captures[lightmap_captures_used];
872 for (int j = 0; j < 9; j++) {
873 lcd.sh[j * 4 + 0] = src_capture[j].r;
874 lcd.sh[j * 4 + 1] = src_capture[j].g;
875 lcd.sh[j * 4 + 2] = src_capture[j].b;
876 lcd.sh[j * 4 + 3] = src_capture[j].a;
877 }
878 flags |= INSTANCE_DATA_FLAG_USE_LIGHTMAP_CAPTURE;
879 inst->gi_offset_cache = lightmap_captures_used;
880 lightmap_captures_used++;
881 uses_lightmap = true;
882 }
883
884 } else {
885 if (p_using_opaque_gi) {
886 flags |= INSTANCE_DATA_FLAG_USE_GI_BUFFERS;
887 }
888
889 if (inst->voxel_gi_instances[0].is_valid()) {
890 uint32_t probe0_index = 0xFFFF;
891 uint32_t probe1_index = 0xFFFF;
892
893 for (uint32_t j = 0; j < scene_state.voxelgis_used; j++) {
894 if (scene_state.voxelgi_ids[j] == inst->voxel_gi_instances[0]) {
895 probe0_index = j;
896 } else if (scene_state.voxelgi_ids[j] == inst->voxel_gi_instances[1]) {
897 probe1_index = j;
898 }
899 }
900
901 if (probe0_index == 0xFFFF && probe1_index != 0xFFFF) {
902 //0 must always exist if a probe exists
903 SWAP(probe0_index, probe1_index);
904 }
905
906 inst->gi_offset_cache = probe0_index | (probe1_index << 16);
907 flags |= INSTANCE_DATA_FLAG_USE_VOXEL_GI;
908 uses_gi = true;
909 } else {
910 if (p_using_sdfgi && inst->can_sdfgi) {
911 flags |= INSTANCE_DATA_FLAG_USE_SDFGI;
912 uses_gi = true;
913 }
914 inst->gi_offset_cache = 0xFFFFFFFF;
915 }
916 }
917 }
918 inst->flags_cache = flags;
919
920 GeometryInstanceSurfaceDataCache *surf = inst->surface_caches;
921
922 while (surf) {
923 surf->sort.uses_forward_gi = 0;
924 surf->sort.uses_lightmap = 0;
925
926 // LOD
927
928 if (p_render_data->scene_data->screen_mesh_lod_threshold > 0.0 && mesh_storage->mesh_surface_has_lod(surf->surface)) {
929 float distance = 0.0;
930
931 // Check if camera is NOT inside the mesh AABB.
932 if (!inst->transformed_aabb.has_point(p_render_data->scene_data->cam_transform.origin)) {
933 // Get the LOD support points on the mesh AABB.
934 Vector3 lod_support_min = inst->transformed_aabb.get_support(p_render_data->scene_data->cam_transform.basis.get_column(Vector3::AXIS_Z));
935 Vector3 lod_support_max = inst->transformed_aabb.get_support(-p_render_data->scene_data->cam_transform.basis.get_column(Vector3::AXIS_Z));
936
937 // Get the distances to those points on the AABB from the camera origin.
938 float distance_min = (float)p_render_data->scene_data->cam_transform.origin.distance_to(lod_support_min);
939 float distance_max = (float)p_render_data->scene_data->cam_transform.origin.distance_to(lod_support_max);
940
941 if (distance_min * distance_max < 0.0) {
942 //crossing plane
943 distance = 0.0;
944 } else if (distance_min >= 0.0) {
945 distance = distance_min;
946 } else if (distance_max <= 0.0) {
947 distance = -distance_max;
948 }
949 }
950 if (p_render_data->scene_data->cam_orthogonal) {
951 distance = 1.0;
952 }
953
954 uint32_t indices = 0;
955 surf->sort.lod_index = mesh_storage->mesh_surface_get_lod(surf->surface, inst->lod_model_scale * inst->lod_bias, distance * p_render_data->scene_data->lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, indices);
956 if (p_render_data->render_info) {
957 indices = _indices_to_primitives(surf->primitive, indices);
958 if (p_render_list == RENDER_LIST_OPAQUE) { //opaque
959 p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices;
960 } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow
961 p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices;
962 }
963 }
964 } else {
965 surf->sort.lod_index = 0;
966 if (p_render_data->render_info) {
967 uint32_t to_draw = mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface);
968 to_draw = _indices_to_primitives(surf->primitive, to_draw);
969 to_draw *= inst->instance_count;
970 if (p_render_list == RENDER_LIST_OPAQUE) { //opaque
971 p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw;
972 } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow
973 p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw;
974 }
975 }
976 }
977
978 // ADD Element
979 if (p_pass_mode == PASS_MODE_COLOR) {
980#ifdef DEBUG_ENABLED
981 bool force_alpha = unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW);
982#else
983 bool force_alpha = false;
984#endif
985
986 if (fade_alpha < 0.999) {
987 force_alpha = true;
988 }
989
990 if (!force_alpha && (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE))) {
991 rl->add_element(surf);
992 }
993 if (force_alpha || (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA)) {
994 render_list[RENDER_LIST_ALPHA].add_element(surf);
995 if (uses_gi) {
996 surf->sort.uses_forward_gi = 1;
997 }
998 }
999
1000 if (uses_lightmap) {
1001 surf->sort.uses_lightmap = 1;
1002 scene_state.used_lightmap = true;
1003 }
1004
1005 if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_SUBSURFACE_SCATTERING) {
1006 scene_state.used_sss = true;
1007 }
1008 if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_SCREEN_TEXTURE) {
1009 scene_state.used_screen_texture = true;
1010 }
1011 if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_NORMAL_TEXTURE) {
1012 scene_state.used_normal_texture = true;
1013 }
1014 if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_DEPTH_TEXTURE) {
1015 scene_state.used_depth_texture = true;
1016 }
1017 } else if (p_pass_mode == PASS_MODE_SHADOW || p_pass_mode == PASS_MODE_SHADOW_DP) {
1018 if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW) {
1019 rl->add_element(surf);
1020 }
1021 } else {
1022 if (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE)) {
1023 rl->add_element(surf);
1024 }
1025 }
1026
1027 surf->sort.depth_layer = depth_layer;
1028
1029 surf = surf->next;
1030 }
1031 }
1032
1033 if (p_render_list == RENDER_LIST_OPAQUE && lightmap_captures_used) {
1034 RD::get_singleton()->buffer_update(scene_state.lightmap_capture_buffer, 0, sizeof(LightmapCaptureData) * lightmap_captures_used, scene_state.lightmap_captures, RD::BARRIER_MASK_RASTER);
1035 }
1036}
1037
1038void RenderForwardClustered::_setup_voxelgis(const PagedArray<RID> &p_voxelgis) {
1039 scene_state.voxelgis_used = MIN(p_voxelgis.size(), uint32_t(MAX_VOXEL_GI_INSTANCESS));
1040 for (uint32_t i = 0; i < scene_state.voxelgis_used; i++) {
1041 scene_state.voxelgi_ids[i] = p_voxelgis[i];
1042 }
1043}
1044
1045void RenderForwardClustered::_setup_lightmaps(const RenderDataRD *p_render_data, const PagedArray<RID> &p_lightmaps, const Transform3D &p_cam_transform) {
1046 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
1047
1048 scene_state.lightmaps_used = 0;
1049 for (int i = 0; i < (int)p_lightmaps.size(); i++) {
1050 if (i >= (int)scene_state.max_lightmaps) {
1051 break;
1052 }
1053
1054 RID lightmap = light_storage->lightmap_instance_get_lightmap(p_lightmaps[i]);
1055
1056 Basis to_lm = light_storage->lightmap_instance_get_transform(p_lightmaps[i]).basis.inverse() * p_cam_transform.basis;
1057 to_lm = to_lm.inverse().transposed(); //will transform normals
1058 RendererRD::MaterialStorage::store_transform_3x3(to_lm, scene_state.lightmaps[i].normal_xform);
1059 scene_state.lightmaps[i].exposure_normalization = 1.0;
1060 if (p_render_data->camera_attributes.is_valid()) {
1061 float baked_exposure = light_storage->lightmap_get_baked_exposure_normalization(lightmap);
1062 float enf = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1063 scene_state.lightmaps[i].exposure_normalization = enf / baked_exposure;
1064 }
1065
1066 scene_state.lightmap_ids[i] = p_lightmaps[i];
1067 scene_state.lightmap_has_sh[i] = light_storage->lightmap_uses_spherical_harmonics(lightmap);
1068
1069 scene_state.lightmaps_used++;
1070 }
1071 if (scene_state.lightmaps_used > 0) {
1072 RD::get_singleton()->buffer_update(scene_state.lightmap_buffer, 0, sizeof(LightmapData) * scene_state.lightmaps_used, scene_state.lightmaps, RD::BARRIER_MASK_RASTER);
1073 }
1074}
1075
1076/* SDFGI */
1077
1078void RenderForwardClustered::_update_sdfgi(RenderDataRD *p_render_data) {
1079 Ref<RenderSceneBuffersRD> rb;
1080 if (p_render_data && p_render_data->render_buffers.is_valid()) {
1081 rb = p_render_data->render_buffers;
1082 }
1083
1084 if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_SDFGI)) {
1085 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
1086 float exposure_normalization = 1.0;
1087
1088 if (p_render_data->camera_attributes.is_valid()) {
1089 exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1090 }
1091 for (int i = 0; i < p_render_data->render_sdfgi_region_count; i++) {
1092 sdfgi->render_region(rb, p_render_data->render_sdfgi_regions[i].region, p_render_data->render_sdfgi_regions[i].instances, exposure_normalization);
1093 }
1094 if (p_render_data->sdfgi_update_data->update_static) {
1095 sdfgi->render_static_lights(p_render_data, rb, p_render_data->sdfgi_update_data->static_cascade_count, p_render_data->sdfgi_update_data->static_cascade_indices, p_render_data->sdfgi_update_data->static_positional_lights);
1096 }
1097 }
1098}
1099
1100/* Debug */
1101
1102void RenderForwardClustered::_debug_draw_cluster(Ref<RenderSceneBuffersRD> p_render_buffers) {
1103 if (p_render_buffers.is_valid() && current_cluster_builder != nullptr) {
1104 RS::ViewportDebugDraw dd = get_debug_draw_mode();
1105
1106 if (dd == RS::VIEWPORT_DEBUG_DRAW_CLUSTER_OMNI_LIGHTS || dd == RS::VIEWPORT_DEBUG_DRAW_CLUSTER_SPOT_LIGHTS || dd == RS::VIEWPORT_DEBUG_DRAW_CLUSTER_DECALS || dd == RS::VIEWPORT_DEBUG_DRAW_CLUSTER_REFLECTION_PROBES) {
1107 ClusterBuilderRD::ElementType elem_type = ClusterBuilderRD::ELEMENT_TYPE_MAX;
1108 switch (dd) {
1109 case RS::VIEWPORT_DEBUG_DRAW_CLUSTER_OMNI_LIGHTS:
1110 elem_type = ClusterBuilderRD::ELEMENT_TYPE_OMNI_LIGHT;
1111 break;
1112 case RS::VIEWPORT_DEBUG_DRAW_CLUSTER_SPOT_LIGHTS:
1113 elem_type = ClusterBuilderRD::ELEMENT_TYPE_SPOT_LIGHT;
1114 break;
1115 case RS::VIEWPORT_DEBUG_DRAW_CLUSTER_DECALS:
1116 elem_type = ClusterBuilderRD::ELEMENT_TYPE_DECAL;
1117 break;
1118 case RS::VIEWPORT_DEBUG_DRAW_CLUSTER_REFLECTION_PROBES:
1119 elem_type = ClusterBuilderRD::ELEMENT_TYPE_REFLECTION_PROBE;
1120 break;
1121 default: {
1122 }
1123 }
1124 current_cluster_builder->debug(elem_type);
1125 }
1126 }
1127}
1128
1129////////////////////////////////////////////////////////////////////////////////
1130// FOG SHADER
1131
1132void RenderForwardClustered::_update_volumetric_fog(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_environment, const Projection &p_cam_projection, const Transform3D &p_cam_transform, const Transform3D &p_prev_cam_inv_transform, RID p_shadow_atlas, int p_directional_light_count, bool p_use_directional_shadows, int p_positional_light_count, int p_voxel_gi_count, const PagedArray<RID> &p_fog_volumes) {
1133 ERR_FAIL_COND(p_render_buffers.is_null());
1134
1135 Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
1136 ERR_FAIL_COND(rb_data.is_null());
1137
1138 ERR_FAIL_COND(!p_render_buffers->has_custom_data(RB_SCOPE_GI));
1139 Ref<RendererRD::GI::RenderBuffersGI> rbgi = p_render_buffers->get_custom_data(RB_SCOPE_GI);
1140
1141 Ref<RendererRD::GI::SDFGI> sdfgi;
1142 if (p_render_buffers->has_custom_data(RB_SCOPE_SDFGI)) {
1143 sdfgi = p_render_buffers->get_custom_data(RB_SCOPE_SDFGI);
1144 }
1145
1146 Size2i size = p_render_buffers->get_internal_size();
1147 float ratio = float(size.x) / float((size.x + size.y) / 2);
1148 uint32_t target_width = uint32_t(float(get_volumetric_fog_size()) * ratio);
1149 uint32_t target_height = uint32_t(float(get_volumetric_fog_size()) / ratio);
1150
1151 if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) {
1152 Ref<RendererRD::Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG);
1153 //validate
1154 if (p_environment.is_null() || !environment_get_volumetric_fog_enabled(p_environment) || fog->width != target_width || fog->height != target_height || fog->depth != get_volumetric_fog_depth()) {
1155 p_render_buffers->set_custom_data(RB_SCOPE_FOG, Ref<RenderBufferCustomDataRD>());
1156 }
1157 }
1158
1159 if (p_environment.is_null() || !environment_get_volumetric_fog_enabled(p_environment)) {
1160 //no reason to enable or update, bye
1161 return;
1162 }
1163
1164 if (p_environment.is_valid() && environment_get_volumetric_fog_enabled(p_environment) && !p_render_buffers->has_custom_data(RB_SCOPE_FOG)) {
1165 //required volumetric fog but not existing, create
1166 Ref<RendererRD::Fog::VolumetricFog> fog;
1167
1168 fog.instantiate();
1169 fog->init(Vector3i(target_width, target_height, get_volumetric_fog_depth()), sky.sky_shader.default_shader_rd);
1170
1171 p_render_buffers->set_custom_data(RB_SCOPE_FOG, fog);
1172 }
1173
1174 if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) {
1175 Ref<RendererRD::Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG);
1176
1177 RendererRD::Fog::VolumetricFogSettings settings;
1178 settings.rb_size = size;
1179 settings.time = time;
1180 settings.is_using_radiance_cubemap_array = is_using_radiance_cubemap_array();
1181 settings.max_cluster_elements = RendererRD::LightStorage::get_singleton()->get_max_cluster_elements();
1182 settings.volumetric_fog_filter_active = get_volumetric_fog_filter_active();
1183
1184 settings.shadow_sampler = shadow_sampler;
1185 settings.shadow_atlas_depth = RendererRD::LightStorage::get_singleton()->owns_shadow_atlas(p_shadow_atlas) ? RendererRD::LightStorage::get_singleton()->shadow_atlas_get_texture(p_shadow_atlas) : RID();
1186 settings.voxel_gi_buffer = rbgi->get_voxel_gi_buffer();
1187 settings.omni_light_buffer = RendererRD::LightStorage::get_singleton()->get_omni_light_buffer();
1188 settings.spot_light_buffer = RendererRD::LightStorage::get_singleton()->get_spot_light_buffer();
1189 settings.directional_shadow_depth = RendererRD::LightStorage::get_singleton()->directional_shadow_get_texture();
1190 settings.directional_light_buffer = RendererRD::LightStorage::get_singleton()->get_directional_light_buffer();
1191
1192 settings.vfog = fog;
1193 settings.cluster_builder = rb_data->cluster_builder;
1194 settings.rbgi = rbgi;
1195 settings.sdfgi = sdfgi;
1196 settings.env = p_environment;
1197 settings.sky = &sky;
1198 settings.gi = &gi;
1199
1200 RendererRD::Fog::get_singleton()->volumetric_fog_update(settings, p_cam_projection, p_cam_transform, p_prev_cam_inv_transform, p_shadow_atlas, p_directional_light_count, p_use_directional_shadows, p_positional_light_count, p_voxel_gi_count, p_fog_volumes);
1201 }
1202}
1203
1204/* Lighting */
1205
1206void RenderForwardClustered::setup_added_reflection_probe(const Transform3D &p_transform, const Vector3 &p_half_size) {
1207 if (current_cluster_builder != nullptr) {
1208 current_cluster_builder->add_box(ClusterBuilderRD::BOX_TYPE_REFLECTION_PROBE, p_transform, p_half_size);
1209 }
1210}
1211
1212void RenderForwardClustered::setup_added_light(const RS::LightType p_type, const Transform3D &p_transform, float p_radius, float p_spot_aperture) {
1213 if (current_cluster_builder != nullptr) {
1214 current_cluster_builder->add_light(p_type == RS::LIGHT_SPOT ? ClusterBuilderRD::LIGHT_TYPE_SPOT : ClusterBuilderRD::LIGHT_TYPE_OMNI, p_transform, p_radius, p_spot_aperture);
1215 }
1216}
1217
1218void RenderForwardClustered::setup_added_decal(const Transform3D &p_transform, const Vector3 &p_half_size) {
1219 if (current_cluster_builder != nullptr) {
1220 current_cluster_builder->add_box(ClusterBuilderRD::BOX_TYPE_DECAL, p_transform, p_half_size);
1221 }
1222}
1223
1224/* Render scene */
1225
1226void RenderForwardClustered::_process_ssao(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_environment, const RID *p_normal_buffers, const Projection *p_projections) {
1227 ERR_FAIL_NULL(ss_effects);
1228 ERR_FAIL_COND(p_render_buffers.is_null());
1229 ERR_FAIL_COND(p_environment.is_null());
1230
1231 Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
1232 ERR_FAIL_COND(rb_data.is_null());
1233
1234 RENDER_TIMESTAMP("Process SSAO");
1235
1236 RendererRD::SSEffects::SSAOSettings settings;
1237 settings.radius = environment_get_ssao_radius(p_environment);
1238 settings.intensity = environment_get_ssao_intensity(p_environment);
1239 settings.power = environment_get_ssao_power(p_environment);
1240 settings.detail = environment_get_ssao_detail(p_environment);
1241 settings.horizon = environment_get_ssao_horizon(p_environment);
1242 settings.sharpness = environment_get_ssao_sharpness(p_environment);
1243 settings.full_screen_size = p_render_buffers->get_internal_size();
1244
1245 ss_effects->ssao_allocate_buffers(p_render_buffers, rb_data->ss_effects_data.ssao, settings);
1246
1247 for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) {
1248 ss_effects->generate_ssao(p_render_buffers, rb_data->ss_effects_data.ssao, v, p_normal_buffers[v], p_projections[v], settings);
1249 }
1250}
1251
1252void RenderForwardClustered::_process_ssil(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_environment, const RID *p_normal_buffers, const Projection *p_projections, const Transform3D &p_transform) {
1253 ERR_FAIL_NULL(ss_effects);
1254 ERR_FAIL_COND(p_render_buffers.is_null());
1255 ERR_FAIL_COND(p_environment.is_null());
1256
1257 Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
1258 ERR_FAIL_COND(rb_data.is_null());
1259
1260 RENDER_TIMESTAMP("Process SSIL");
1261
1262 RendererRD::SSEffects::SSILSettings settings;
1263 settings.radius = environment_get_ssil_radius(p_environment);
1264 settings.intensity = environment_get_ssil_intensity(p_environment);
1265 settings.sharpness = environment_get_ssil_sharpness(p_environment);
1266 settings.normal_rejection = environment_get_ssil_normal_rejection(p_environment);
1267 settings.full_screen_size = p_render_buffers->get_internal_size();
1268
1269 ss_effects->ssil_allocate_buffers(p_render_buffers, rb_data->ss_effects_data.ssil, settings);
1270
1271 Transform3D transform = p_transform;
1272 transform.set_origin(Vector3(0.0, 0.0, 0.0));
1273
1274 for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) {
1275 Projection correction;
1276 correction.set_depth_correction(true);
1277 Projection projection = correction * p_projections[v];
1278 Projection last_frame_projection = rb_data->ss_effects_data.last_frame_projections[v] * Projection(rb_data->ss_effects_data.last_frame_transform.affine_inverse()) * Projection(transform) * projection.inverse();
1279
1280 ss_effects->screen_space_indirect_lighting(p_render_buffers, rb_data->ss_effects_data.ssil, v, p_normal_buffers[v], p_projections[v], last_frame_projection, settings);
1281
1282 rb_data->ss_effects_data.last_frame_projections[v] = projection;
1283 }
1284 rb_data->ss_effects_data.last_frame_transform = transform;
1285}
1286
1287void RenderForwardClustered::_copy_framebuffer_to_ssil(Ref<RenderSceneBuffersRD> p_render_buffers) {
1288 ERR_FAIL_COND(p_render_buffers.is_null());
1289
1290 if (p_render_buffers->has_texture(RB_SCOPE_SSIL, RB_LAST_FRAME)) {
1291 Size2i size = p_render_buffers->get_internal_size();
1292 uint32_t mipmaps = p_render_buffers->get_texture_format(RB_SCOPE_SSIL, RB_LAST_FRAME).mipmaps;
1293 for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) {
1294 RID source = p_render_buffers->get_internal_texture(v);
1295 RID dest = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_LAST_FRAME, v, 0);
1296 copy_effects->copy_to_rect(source, dest, Rect2i(0, 0, size.x, size.y));
1297
1298 int width = size.x;
1299 int height = size.y;
1300 for (uint32_t m = 1; m < mipmaps; m++) {
1301 width = MAX(1, width >> 1);
1302 height = MAX(1, height >> 1);
1303
1304 source = dest;
1305 dest = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_LAST_FRAME, v, m);
1306
1307 copy_effects->make_mipmap(source, dest, Size2i(width, height));
1308 }
1309 }
1310 }
1311}
1312
1313void RenderForwardClustered::_pre_opaque_render(RenderDataRD *p_render_data, bool p_use_ssao, bool p_use_ssil, bool p_use_gi, const RID *p_normal_roughness_slices, RID p_voxel_gi_buffer) {
1314 // Render shadows while GI is rendering, due to how barriers are handled, this should happen at the same time
1315 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
1316 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
1317
1318 Ref<RenderSceneBuffersRD> rb = p_render_data->render_buffers;
1319 Ref<RenderBufferDataForwardClustered> rb_data;
1320 if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_FORWARD_CLUSTERED)) {
1321 // Our forward clustered custom data buffer will only be available when we're rendering our normal view.
1322 // This will not be available when rendering reflection probes.
1323 rb_data = rb->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
1324 }
1325
1326 if (rb.is_valid() && p_use_gi && rb->has_custom_data(RB_SCOPE_SDFGI)) {
1327 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
1328 sdfgi->store_probes();
1329 }
1330
1331 p_render_data->cube_shadows.clear();
1332 p_render_data->shadows.clear();
1333 p_render_data->directional_shadows.clear();
1334
1335 Plane camera_plane(-p_render_data->scene_data->cam_transform.basis.get_column(Vector3::AXIS_Z), p_render_data->scene_data->cam_transform.origin);
1336 float lod_distance_multiplier = p_render_data->scene_data->cam_projection.get_lod_multiplier();
1337 {
1338 for (int i = 0; i < p_render_data->render_shadow_count; i++) {
1339 RID li = p_render_data->render_shadows[i].light;
1340 RID base = light_storage->light_instance_get_base_light(li);
1341
1342 if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) {
1343 p_render_data->directional_shadows.push_back(i);
1344 } else if (light_storage->light_get_type(base) == RS::LIGHT_OMNI && light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) {
1345 p_render_data->cube_shadows.push_back(i);
1346 } else {
1347 p_render_data->shadows.push_back(i);
1348 }
1349 }
1350
1351 //cube shadows are rendered in their own way
1352 for (const int &index : p_render_data->cube_shadows) {
1353 _render_shadow_pass(p_render_data->render_shadows[index].light, p_render_data->shadow_atlas, p_render_data->render_shadows[index].pass, p_render_data->render_shadows[index].instances, camera_plane, lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, true, true, true, p_render_data->render_info);
1354 }
1355
1356 if (p_render_data->directional_shadows.size()) {
1357 //open the pass for directional shadows
1358 light_storage->update_directional_shadow_atlas();
1359 RD::get_singleton()->draw_list_begin(light_storage->direction_shadow_get_fb(), RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_CONTINUE);
1360 RD::get_singleton()->draw_list_end();
1361 }
1362 }
1363
1364 // Render GI
1365
1366 bool render_shadows = p_render_data->directional_shadows.size() || p_render_data->shadows.size();
1367 bool render_gi = rb.is_valid() && p_use_gi;
1368
1369 if (render_shadows && render_gi) {
1370 RENDER_TIMESTAMP("Render GI + Render Shadows (Parallel)");
1371 } else if (render_shadows) {
1372 RENDER_TIMESTAMP("Render Shadows");
1373 } else if (render_gi) {
1374 RENDER_TIMESTAMP("Render GI");
1375 }
1376
1377 //prepare shadow rendering
1378 if (render_shadows) {
1379 _render_shadow_begin();
1380
1381 //render directional shadows
1382 for (uint32_t i = 0; i < p_render_data->directional_shadows.size(); i++) {
1383 _render_shadow_pass(p_render_data->render_shadows[p_render_data->directional_shadows[i]].light, p_render_data->shadow_atlas, p_render_data->render_shadows[p_render_data->directional_shadows[i]].pass, p_render_data->render_shadows[p_render_data->directional_shadows[i]].instances, camera_plane, lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, false, i == p_render_data->directional_shadows.size() - 1, false, p_render_data->render_info);
1384 }
1385 //render positional shadows
1386 for (uint32_t i = 0; i < p_render_data->shadows.size(); i++) {
1387 _render_shadow_pass(p_render_data->render_shadows[p_render_data->shadows[i]].light, p_render_data->shadow_atlas, p_render_data->render_shadows[p_render_data->shadows[i]].pass, p_render_data->render_shadows[p_render_data->shadows[i]].instances, camera_plane, lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, i == 0, i == p_render_data->shadows.size() - 1, true, p_render_data->render_info);
1388 }
1389
1390 _render_shadow_process();
1391 }
1392
1393 //start GI
1394 if (render_gi) {
1395 gi.process_gi(rb, p_normal_roughness_slices, p_voxel_gi_buffer, p_render_data->environment, p_render_data->scene_data->view_count, p_render_data->scene_data->view_projection, p_render_data->scene_data->view_eye_offset, p_render_data->scene_data->cam_transform, *p_render_data->voxel_gi_instances);
1396 }
1397
1398 //Do shadow rendering (in parallel with GI)
1399 if (render_shadows) {
1400 _render_shadow_end(RD::BARRIER_MASK_NO_BARRIER);
1401 }
1402
1403 if (render_gi) {
1404 RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER); //use a later barrier
1405 }
1406
1407 if (rb_data.is_valid() && ss_effects) {
1408 // Note, in multiview we're allocating buffers for each eye/view we're rendering.
1409 // This should allow most of the processing to happen in parallel even if we're doing
1410 // drawcalls per eye/view. It will all sync up at the barrier.
1411
1412 if (p_use_ssao || p_use_ssil) {
1413 // Convert our depth buffer data to linear data in
1414 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
1415 ss_effects->downsample_depth(rb, v, p_render_data->scene_data->view_projection[v]);
1416 }
1417
1418 if (p_use_ssao) {
1419 _process_ssao(rb, p_render_data->environment, p_normal_roughness_slices, p_render_data->scene_data->view_projection);
1420 }
1421
1422 if (p_use_ssil) {
1423 _process_ssil(rb, p_render_data->environment, p_normal_roughness_slices, p_render_data->scene_data->view_projection, p_render_data->scene_data->cam_transform);
1424 }
1425 }
1426 }
1427
1428 //full barrier here, we need raster, transfer and compute and it depends from the previous work
1429 RD::get_singleton()->barrier(RD::BARRIER_MASK_ALL_BARRIERS, RD::BARRIER_MASK_ALL_BARRIERS);
1430
1431 if (current_cluster_builder) {
1432 // Note: when rendering stereoscopic (multiview) we are using our combined frustum projection to create
1433 // our cluster data. We use reprojection in the shader to adjust for our left/right eye.
1434 // This only works as we don't filter our cluster by depth buffer.
1435 // If we ever make this optimization we should make it optional and only use it in mono.
1436 // What we win by filtering out a few lights, we loose by having to do the work double for stereo.
1437 current_cluster_builder->begin(p_render_data->scene_data->cam_transform, p_render_data->scene_data->cam_projection, !p_render_data->reflection_probe.is_valid());
1438 }
1439
1440 bool using_shadows = true;
1441
1442 if (p_render_data->reflection_probe.is_valid()) {
1443 if (!RSG::light_storage->reflection_probe_renders_shadows(light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe))) {
1444 using_shadows = false;
1445 }
1446 } else {
1447 //do not render reflections when rendering a reflection probe
1448 light_storage->update_reflection_probe_buffer(p_render_data, *p_render_data->reflection_probes, p_render_data->scene_data->cam_transform.affine_inverse(), p_render_data->environment);
1449 }
1450
1451 uint32_t directional_light_count = 0;
1452 uint32_t positional_light_count = 0;
1453 light_storage->update_light_buffers(p_render_data, *p_render_data->lights, p_render_data->scene_data->cam_transform, p_render_data->shadow_atlas, using_shadows, directional_light_count, positional_light_count, p_render_data->directional_light_soft_shadows);
1454 texture_storage->update_decal_buffer(*p_render_data->decals, p_render_data->scene_data->cam_transform);
1455
1456 p_render_data->directional_light_count = directional_light_count;
1457
1458 if (current_cluster_builder) {
1459 current_cluster_builder->bake_cluster();
1460 }
1461
1462 if (rb_data.is_valid()) {
1463 bool directional_shadows = RendererRD::LightStorage::get_singleton()->has_directional_shadows(directional_light_count);
1464 _update_volumetric_fog(rb, p_render_data->environment, p_render_data->scene_data->cam_projection, p_render_data->scene_data->cam_transform, p_render_data->scene_data->prev_cam_transform.affine_inverse(), p_render_data->shadow_atlas, directional_light_count, directional_shadows, positional_light_count, p_render_data->voxel_gi_count, *p_render_data->fog_volumes);
1465 }
1466}
1467
1468void RenderForwardClustered::_process_ssr(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_dest_framebuffer, const RID *p_normal_slices, RID p_specular_buffer, const RID *p_metallic_slices, RID p_environment, const Projection *p_projections, const Vector3 *p_eye_offsets, bool p_use_additive) {
1469 ERR_FAIL_NULL(ss_effects);
1470 ERR_FAIL_COND(p_render_buffers.is_null());
1471
1472 Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
1473 ERR_FAIL_COND(rb_data.is_null());
1474
1475 Size2i internal_size = p_render_buffers->get_internal_size();
1476 bool can_use_effects = internal_size.x >= 8 && internal_size.y >= 8;
1477 uint32_t view_count = p_render_buffers->get_view_count();
1478
1479 if (!can_use_effects) {
1480 //just copy
1481 copy_effects->merge_specular(p_dest_framebuffer, p_specular_buffer, p_use_additive ? RID() : p_render_buffers->get_internal_texture(), RID(), view_count);
1482 return;
1483 }
1484
1485 ERR_FAIL_COND(p_environment.is_null());
1486 ERR_FAIL_COND(!environment_get_ssr_enabled(p_environment));
1487
1488 ss_effects->ssr_allocate_buffers(p_render_buffers, rb_data->ss_effects_data.ssr, _render_buffers_get_color_format());
1489 ss_effects->screen_space_reflection(p_render_buffers, rb_data->ss_effects_data.ssr, p_normal_slices, p_metallic_slices, environment_get_ssr_max_steps(p_environment), environment_get_ssr_fade_in(p_environment), environment_get_ssr_fade_out(p_environment), environment_get_ssr_depth_tolerance(p_environment), p_projections, p_eye_offsets);
1490
1491 RID output = p_render_buffers->get_texture(RB_SCOPE_SSR, RB_OUTPUT);
1492 copy_effects->merge_specular(p_dest_framebuffer, p_specular_buffer, p_use_additive ? RID() : p_render_buffers->get_internal_texture(), output, view_count);
1493}
1494
1495void RenderForwardClustered::_process_sss(Ref<RenderSceneBuffersRD> p_render_buffers, const Projection &p_camera) {
1496 ERR_FAIL_COND(p_render_buffers.is_null());
1497
1498 Size2i internal_size = p_render_buffers->get_internal_size();
1499 bool can_use_effects = internal_size.x >= 8 && internal_size.y >= 8;
1500
1501 if (!can_use_effects) {
1502 //just copy
1503 return;
1504 }
1505
1506 p_render_buffers->allocate_blur_textures();
1507
1508 for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) {
1509 RID internal_texture = p_render_buffers->get_internal_texture(v);
1510 RID depth_texture = p_render_buffers->get_depth_texture(v);
1511 ss_effects->sub_surface_scattering(p_render_buffers, internal_texture, depth_texture, p_camera, internal_size);
1512 }
1513}
1514
1515void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Color &p_default_bg_color) {
1516 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
1517
1518 ERR_FAIL_NULL(p_render_data);
1519
1520 Ref<RenderSceneBuffersRD> rb = p_render_data->render_buffers;
1521 ERR_FAIL_COND(rb.is_null());
1522 Ref<RenderBufferDataForwardClustered> rb_data;
1523 if (rb->has_custom_data(RB_SCOPE_FORWARD_CLUSTERED)) {
1524 // Our forward clustered custom data buffer will only be available when we're rendering our normal view.
1525 // This will not be available when rendering reflection probes.
1526 rb_data = rb->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
1527 }
1528 bool is_reflection_probe = p_render_data->reflection_probe.is_valid();
1529
1530 static const int texture_multisamples[RS::VIEWPORT_MSAA_MAX] = { 1, 2, 4, 8 };
1531
1532 //first of all, make a new render pass
1533 //fill up ubo
1534
1535 RENDER_TIMESTAMP("Prepare 3D Scene");
1536
1537 // sdfgi first
1538 _update_sdfgi(p_render_data);
1539
1540 // assign render indices to voxel_gi_instances
1541 for (uint32_t i = 0; i < (uint32_t)p_render_data->voxel_gi_instances->size(); i++) {
1542 RID voxel_gi_instance = (*p_render_data->voxel_gi_instances)[i];
1543 gi.voxel_gi_instance_set_render_index(voxel_gi_instance, i);
1544 }
1545
1546 // obtain cluster builder
1547 if (light_storage->owns_reflection_probe_instance(p_render_data->reflection_probe)) {
1548 current_cluster_builder = light_storage->reflection_probe_instance_get_cluster_builder(p_render_data->reflection_probe, &cluster_builder_shared);
1549
1550 if (p_render_data->camera_attributes.is_valid()) {
1551 light_storage->reflection_probe_set_baked_exposure(light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe), RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes));
1552 }
1553 } else if (rb_data.is_valid()) {
1554 current_cluster_builder = rb_data->cluster_builder;
1555
1556 p_render_data->voxel_gi_count = 0;
1557
1558 if (rb->has_custom_data(RB_SCOPE_SDFGI)) {
1559 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
1560 if (sdfgi.is_valid()) {
1561 sdfgi->update_cascades();
1562 sdfgi->pre_process_gi(p_render_data->scene_data->cam_transform, p_render_data);
1563 sdfgi->update_light();
1564 }
1565 }
1566
1567 gi.setup_voxel_gi_instances(p_render_data, p_render_data->render_buffers, p_render_data->scene_data->cam_transform, *p_render_data->voxel_gi_instances, p_render_data->voxel_gi_count);
1568 } else {
1569 ERR_PRINT("No render buffer nor reflection atlas, bug"); //should never happen, will crash
1570 current_cluster_builder = nullptr;
1571 }
1572
1573 if (current_cluster_builder != nullptr) {
1574 p_render_data->cluster_buffer = current_cluster_builder->get_cluster_buffer();
1575 p_render_data->cluster_size = current_cluster_builder->get_cluster_size();
1576 p_render_data->cluster_max_elements = current_cluster_builder->get_max_cluster_elements();
1577 }
1578
1579 _update_vrs(rb);
1580
1581 RENDER_TIMESTAMP("Setup 3D Scene");
1582
1583 // check if we need motion vectors
1584 if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_MOTION_VECTORS) {
1585 p_render_data->scene_data->calculate_motion_vectors = true;
1586 } else if (!is_reflection_probe && rb->get_use_taa()) {
1587 p_render_data->scene_data->calculate_motion_vectors = true;
1588 } else {
1589 p_render_data->scene_data->calculate_motion_vectors = false;
1590 }
1591
1592 //p_render_data->scene_data->subsurface_scatter_width = subsurface_scatter_size;
1593 p_render_data->scene_data->directional_light_count = 0;
1594 p_render_data->scene_data->opaque_prepass_threshold = 0.99f;
1595
1596 Size2i screen_size;
1597 RID color_framebuffer;
1598 RID color_only_framebuffer;
1599 RID depth_framebuffer;
1600
1601 PassMode depth_pass_mode = PASS_MODE_DEPTH;
1602 uint32_t color_pass_flags = 0;
1603 Vector<Color> depth_pass_clear;
1604 bool using_separate_specular = false;
1605 bool using_ssr = false;
1606 bool using_sdfgi = false;
1607 bool using_voxelgi = false;
1608 bool reverse_cull = p_render_data->scene_data->cam_transform.basis.determinant() < 0;
1609 bool using_ssil = !is_reflection_probe && p_render_data->environment.is_valid() && environment_get_ssil_enabled(p_render_data->environment);
1610
1611 if (is_reflection_probe) {
1612 uint32_t resolution = light_storage->reflection_probe_instance_get_resolution(p_render_data->reflection_probe);
1613 screen_size.x = resolution;
1614 screen_size.y = resolution;
1615
1616 color_framebuffer = light_storage->reflection_probe_instance_get_framebuffer(p_render_data->reflection_probe, p_render_data->reflection_probe_pass);
1617 color_only_framebuffer = color_framebuffer;
1618 depth_framebuffer = light_storage->reflection_probe_instance_get_depth_framebuffer(p_render_data->reflection_probe, p_render_data->reflection_probe_pass);
1619
1620 if (light_storage->reflection_probe_is_interior(light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe))) {
1621 p_render_data->environment = RID(); //no environment on interiors
1622 }
1623
1624 reverse_cull = true; // for some reason our views are inverted
1625 } else {
1626 screen_size = rb->get_internal_size();
1627
1628 if (rb->get_use_taa() || get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_MOTION_VECTORS) {
1629 color_pass_flags |= COLOR_PASS_FLAG_MOTION_VECTORS;
1630 scene_shader.enable_advanced_shader_group();
1631 }
1632
1633 if (p_render_data->voxel_gi_instances->size() > 0) {
1634 using_voxelgi = true;
1635 }
1636
1637 if (p_render_data->environment.is_valid()) {
1638 if (environment_get_sdfgi_enabled(p_render_data->environment)) {
1639 using_sdfgi = true;
1640 }
1641 if (environment_get_ssr_enabled(p_render_data->environment)) {
1642 using_separate_specular = true;
1643 using_ssr = true;
1644 color_pass_flags |= COLOR_PASS_FLAG_SEPARATE_SPECULAR;
1645 }
1646 }
1647
1648 if (p_render_data->scene_data->view_count > 1) {
1649 color_pass_flags |= COLOR_PASS_FLAG_MULTIVIEW;
1650 // Try enabling here in case is_xr_enabled() returns false.
1651 scene_shader.shader.enable_group(SceneShaderForwardClustered::SHADER_GROUP_MULTIVIEW);
1652 }
1653
1654 color_framebuffer = rb_data->get_color_pass_fb(color_pass_flags);
1655 color_only_framebuffer = rb_data->get_color_only_fb();
1656 }
1657
1658 p_render_data->scene_data->emissive_exposure_normalization = -1.0;
1659
1660 RD::get_singleton()->draw_command_begin_label("Render Setup");
1661
1662 _setup_lightmaps(p_render_data, *p_render_data->lightmaps, p_render_data->scene_data->cam_transform);
1663 _setup_voxelgis(*p_render_data->voxel_gi_instances);
1664 _setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, false);
1665
1666 _update_render_base_uniform_set(); //may have changed due to the above (light buffer enlarged, as an example)
1667
1668 _fill_render_list(RENDER_LIST_OPAQUE, p_render_data, PASS_MODE_COLOR, color_pass_flags, using_sdfgi, using_sdfgi || using_voxelgi);
1669 render_list[RENDER_LIST_OPAQUE].sort_by_key();
1670 render_list[RENDER_LIST_ALPHA].sort_by_reverse_depth_and_priority();
1671 _fill_instance_data(RENDER_LIST_OPAQUE, p_render_data->render_info ? p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE] : (int *)nullptr);
1672 _fill_instance_data(RENDER_LIST_ALPHA);
1673
1674 RD::get_singleton()->draw_command_end_label();
1675
1676 if (!is_reflection_probe) {
1677 if (using_voxelgi) {
1678 depth_pass_mode = PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI;
1679 } else if (p_render_data->environment.is_valid()) {
1680 if (environment_get_ssr_enabled(p_render_data->environment) ||
1681 environment_get_sdfgi_enabled(p_render_data->environment) ||
1682 environment_get_ssao_enabled(p_render_data->environment) ||
1683 using_ssil ||
1684 get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_NORMAL_BUFFER ||
1685 scene_state.used_normal_texture) {
1686 depth_pass_mode = PASS_MODE_DEPTH_NORMAL_ROUGHNESS;
1687 }
1688 } else if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_NORMAL_BUFFER || scene_state.used_normal_texture) {
1689 depth_pass_mode = PASS_MODE_DEPTH_NORMAL_ROUGHNESS;
1690 }
1691
1692 switch (depth_pass_mode) {
1693 case PASS_MODE_DEPTH: {
1694 depth_framebuffer = rb_data->get_depth_fb();
1695 } break;
1696 case PASS_MODE_DEPTH_NORMAL_ROUGHNESS: {
1697 depth_framebuffer = rb_data->get_depth_fb(RenderBufferDataForwardClustered::DEPTH_FB_ROUGHNESS);
1698 depth_pass_clear.push_back(Color(0.5, 0.5, 0.5, 0));
1699 } break;
1700 case PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI: {
1701 depth_framebuffer = rb_data->get_depth_fb(RenderBufferDataForwardClustered::DEPTH_FB_ROUGHNESS_VOXELGI);
1702 depth_pass_clear.push_back(Color(0.5, 0.5, 0.5, 0));
1703 depth_pass_clear.push_back(Color(0, 0, 0, 0));
1704 } break;
1705 default: {
1706 };
1707 }
1708 }
1709
1710 bool using_sss = rb_data.is_valid() && !is_reflection_probe && scene_state.used_sss && ss_effects->sss_get_quality() != RS::SUB_SURFACE_SCATTERING_QUALITY_DISABLED;
1711
1712 if (using_sss && !using_separate_specular) {
1713 using_separate_specular = true;
1714 color_pass_flags |= COLOR_PASS_FLAG_SEPARATE_SPECULAR;
1715 color_framebuffer = rb_data->get_color_pass_fb(color_pass_flags);
1716 }
1717
1718 if (using_sss || using_separate_specular || scene_state.used_lightmap || using_voxelgi) {
1719 scene_shader.enable_advanced_shader_group(p_render_data->scene_data->view_count > 1);
1720 }
1721
1722 RID radiance_texture;
1723 bool draw_sky = false;
1724 bool draw_sky_fog_only = false;
1725 // We invert luminance_multiplier for sky so that we can combine it with exposure value.
1726 float sky_energy_multiplier = 1.0 / _render_buffers_get_luminance_multiplier();
1727
1728 Color clear_color;
1729 bool keep_color = false;
1730
1731 if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW) {
1732 clear_color = Color(0, 0, 0, 1); //in overdraw mode, BG should always be black
1733 } else if (is_environment(p_render_data->environment)) {
1734 RS::EnvironmentBG bg_mode = environment_get_background(p_render_data->environment);
1735 float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment);
1736 bg_energy_multiplier *= environment_get_bg_intensity(p_render_data->environment);
1737
1738 if (p_render_data->camera_attributes.is_valid()) {
1739 bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1740 }
1741
1742 switch (bg_mode) {
1743 case RS::ENV_BG_CLEAR_COLOR: {
1744 clear_color = p_default_bg_color;
1745 clear_color.r *= bg_energy_multiplier;
1746 clear_color.g *= bg_energy_multiplier;
1747 clear_color.b *= bg_energy_multiplier;
1748 if ((rb->has_custom_data(RB_SCOPE_FOG)) || environment_get_fog_enabled(p_render_data->environment)) {
1749 draw_sky_fog_only = true;
1750 RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear()));
1751 }
1752 } break;
1753 case RS::ENV_BG_COLOR: {
1754 clear_color = environment_get_bg_color(p_render_data->environment);
1755 clear_color.r *= bg_energy_multiplier;
1756 clear_color.g *= bg_energy_multiplier;
1757 clear_color.b *= bg_energy_multiplier;
1758 if ((rb->has_custom_data(RB_SCOPE_FOG)) || environment_get_fog_enabled(p_render_data->environment)) {
1759 draw_sky_fog_only = true;
1760 RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear()));
1761 }
1762 } break;
1763 case RS::ENV_BG_SKY: {
1764 draw_sky = true;
1765 } break;
1766 case RS::ENV_BG_CANVAS: {
1767 if (!is_reflection_probe) {
1768 RID texture = RendererRD::TextureStorage::get_singleton()->render_target_get_rd_texture(rb->get_render_target());
1769 bool convert_to_linear = !RendererRD::TextureStorage::get_singleton()->render_target_is_using_hdr(rb->get_render_target());
1770 copy_effects->copy_to_fb_rect(texture, color_only_framebuffer, Rect2i(), false, false, false, false, RID(), false, false, convert_to_linear);
1771 }
1772 keep_color = true;
1773 } break;
1774 case RS::ENV_BG_KEEP: {
1775 keep_color = true;
1776 } break;
1777 case RS::ENV_BG_CAMERA_FEED: {
1778 } break;
1779 default: {
1780 }
1781 }
1782
1783 // setup sky if used for ambient, reflections, or background
1784 if (draw_sky || draw_sky_fog_only || environment_get_reflection_source(p_render_data->environment) == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(p_render_data->environment) == RS::ENV_AMBIENT_SOURCE_SKY) {
1785 RENDER_TIMESTAMP("Setup Sky");
1786 RD::get_singleton()->draw_command_begin_label("Setup Sky");
1787
1788 // Setup our sky render information for this frame/viewport
1789 if (is_reflection_probe) {
1790 Vector3 eye_offset;
1791 Projection correction;
1792 correction.set_depth_correction(true);
1793 Projection projection = correction * p_render_data->scene_data->cam_projection;
1794
1795 sky.setup_sky(p_render_data->environment, rb, *p_render_data->lights, p_render_data->camera_attributes, 1, &projection, &eye_offset, p_render_data->scene_data->cam_transform, projection, screen_size, this);
1796 } else {
1797 sky.setup_sky(p_render_data->environment, rb, *p_render_data->lights, p_render_data->camera_attributes, p_render_data->scene_data->view_count, p_render_data->scene_data->view_projection, p_render_data->scene_data->view_eye_offset, p_render_data->scene_data->cam_transform, p_render_data->scene_data->cam_projection, screen_size, this);
1798 }
1799
1800 sky_energy_multiplier *= bg_energy_multiplier;
1801
1802 RID sky_rid = environment_get_sky(p_render_data->environment);
1803 if (sky_rid.is_valid()) {
1804 sky.update_radiance_buffers(rb, p_render_data->environment, p_render_data->scene_data->cam_transform.origin, time, sky_energy_multiplier);
1805 radiance_texture = sky.sky_get_radiance_texture_rd(sky_rid);
1806 } else {
1807 // do not try to draw sky if invalid
1808 draw_sky = false;
1809 }
1810
1811 if (draw_sky || draw_sky_fog_only) {
1812 // update sky half/quarter res buffers (if required)
1813 sky.update_res_buffers(rb, p_render_data->environment, time, sky_energy_multiplier);
1814 }
1815
1816 RD::get_singleton()->draw_command_end_label();
1817 }
1818 } else {
1819 clear_color = p_default_bg_color;
1820 }
1821
1822 bool debug_voxelgis = get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_VOXEL_GI_ALBEDO || get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_VOXEL_GI_LIGHTING || get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_VOXEL_GI_EMISSION;
1823 bool debug_sdfgi_probes = get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SDFGI_PROBES;
1824 bool depth_pre_pass = bool(GLOBAL_GET("rendering/driver/depth_prepass/enable")) && depth_framebuffer.is_valid();
1825
1826 bool using_ssao = depth_pre_pass && !is_reflection_probe && p_render_data->environment.is_valid() && environment_get_ssao_enabled(p_render_data->environment);
1827 bool continue_depth = false;
1828 if (depth_pre_pass) { //depth pre pass
1829
1830 bool needs_pre_resolve = _needs_post_prepass_render(p_render_data, using_sdfgi || using_voxelgi);
1831 if (needs_pre_resolve) {
1832 RENDER_TIMESTAMP("GI + Render Depth Pre-Pass (Parallel)");
1833 } else {
1834 RENDER_TIMESTAMP("Render Depth Pre-Pass");
1835 }
1836 if (needs_pre_resolve) {
1837 //pre clear the depth framebuffer, as AMD (and maybe others?) use compute for it, and barrier other compute shaders.
1838 RD::get_singleton()->draw_list_begin(depth_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_CONTINUE, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_CONTINUE, depth_pass_clear);
1839 RD::get_singleton()->draw_list_end();
1840 //start compute processes here, so they run at the same time as depth pre-pass
1841 _post_prepass_render(p_render_data, using_sdfgi || using_voxelgi);
1842 }
1843
1844 RD::get_singleton()->draw_command_begin_label("Render Depth Pre-Pass");
1845
1846 RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_OPAQUE, nullptr, RID());
1847
1848 bool finish_depth = using_ssao || using_ssil || using_sdfgi || using_voxelgi;
1849 RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].element_info.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, depth_pass_mode, 0, rb_data.is_null(), p_render_data->directional_light_soft_shadows, rp_uniform_set, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME, Vector2(), p_render_data->scene_data->lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, p_render_data->scene_data->view_count);
1850 _render_list_with_threads(&render_list_params, depth_framebuffer, needs_pre_resolve ? RD::INITIAL_ACTION_CONTINUE : RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, needs_pre_resolve ? RD::INITIAL_ACTION_CONTINUE : RD::INITIAL_ACTION_CLEAR, finish_depth ? RD::FINAL_ACTION_READ : RD::FINAL_ACTION_CONTINUE, needs_pre_resolve ? Vector<Color>() : depth_pass_clear);
1851
1852 RD::get_singleton()->draw_command_end_label();
1853
1854 if (needs_pre_resolve) {
1855 _pre_resolve_render(p_render_data, using_sdfgi || using_voxelgi);
1856 }
1857
1858 if (rb->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
1859 RENDER_TIMESTAMP("Resolve Depth Pre-Pass (MSAA)");
1860 RD::get_singleton()->draw_command_begin_label("Resolve Depth Pre-Pass (MSAA)");
1861 if (depth_pass_mode == PASS_MODE_DEPTH_NORMAL_ROUGHNESS || depth_pass_mode == PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI) {
1862 if (needs_pre_resolve) {
1863 RD::get_singleton()->barrier(RD::BARRIER_MASK_RASTER, RD::BARRIER_MASK_COMPUTE);
1864 }
1865 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
1866 resolve_effects->resolve_gi(rb->get_depth_msaa(v), rb_data->get_normal_roughness_msaa(v), using_voxelgi ? rb_data->get_voxelgi_msaa(v) : RID(), rb->get_depth_texture(v), rb_data->get_normal_roughness(v), using_voxelgi ? rb_data->get_voxelgi(v) : RID(), rb->get_internal_size(), texture_multisamples[rb->get_msaa_3d()]);
1867 }
1868 } else if (finish_depth) {
1869 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
1870 resolve_effects->resolve_depth(rb->get_depth_msaa(v), rb->get_depth_texture(v), rb->get_internal_size(), texture_multisamples[rb->get_msaa_3d()]);
1871 }
1872 }
1873 RD::get_singleton()->draw_command_end_label();
1874 }
1875
1876 continue_depth = !finish_depth;
1877 }
1878
1879 RID normal_roughness_views[RendererSceneRender::MAX_RENDER_VIEWS];
1880 if (rb_data.is_valid() && rb_data->has_normal_roughness()) {
1881 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
1882 normal_roughness_views[v] = rb_data->get_normal_roughness(v);
1883 }
1884 }
1885 _pre_opaque_render(p_render_data, using_ssao, using_ssil, using_sdfgi || using_voxelgi, normal_roughness_views, rb_data.is_valid() && rb_data->has_voxelgi() ? rb_data->get_voxelgi() : RID());
1886
1887 RD::get_singleton()->draw_command_begin_label("Render Opaque Pass");
1888
1889 p_render_data->scene_data->directional_light_count = p_render_data->directional_light_count;
1890 p_render_data->scene_data->opaque_prepass_threshold = 0.0f;
1891
1892 _setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, true);
1893
1894 RENDER_TIMESTAMP("Render Opaque Pass");
1895
1896 RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_OPAQUE, p_render_data, radiance_texture, true);
1897
1898 bool can_continue_color = !scene_state.used_screen_texture && !using_ssr && !using_sss;
1899 bool can_continue_depth = !(scene_state.used_depth_texture || scene_state.used_normal_texture) && !using_ssr && !using_sss;
1900
1901 {
1902 bool will_continue_color = (can_continue_color || draw_sky || draw_sky_fog_only || debug_voxelgis || debug_sdfgi_probes);
1903 bool will_continue_depth = (can_continue_depth || draw_sky || draw_sky_fog_only || debug_voxelgis || debug_sdfgi_probes);
1904
1905 Vector<Color> c;
1906 {
1907 Color cc = clear_color.srgb_to_linear();
1908 if (using_separate_specular || rb_data.is_valid()) {
1909 cc.a = 0; //subsurf scatter must be 0
1910 }
1911 c.push_back(cc);
1912
1913 if (rb_data.is_valid()) {
1914 c.push_back(Color(0, 0, 0, 0)); // Separate specular
1915 c.push_back(Color(0, 0, 0, 0)); // Motion vectors
1916 }
1917 }
1918
1919 RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].element_info.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, PASS_MODE_COLOR, color_pass_flags, rb_data.is_null(), p_render_data->directional_light_soft_shadows, rp_uniform_set, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME, Vector2(), p_render_data->scene_data->lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, p_render_data->scene_data->view_count);
1920 _render_list_with_threads(&render_list_params, color_framebuffer, keep_color ? RD::INITIAL_ACTION_KEEP : RD::INITIAL_ACTION_CLEAR, will_continue_color ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ, depth_pre_pass ? (continue_depth ? RD::INITIAL_ACTION_CONTINUE : RD::INITIAL_ACTION_KEEP) : RD::INITIAL_ACTION_CLEAR, will_continue_depth ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ, c, 1.0, 0);
1921 if (will_continue_color && using_separate_specular) {
1922 // close the specular framebuffer, as it's no longer used
1923 RD::get_singleton()->draw_list_begin(rb_data->get_specular_only_fb(), RD::INITIAL_ACTION_CONTINUE, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CONTINUE, RD::FINAL_ACTION_CONTINUE);
1924 RD::get_singleton()->draw_list_end();
1925 }
1926 }
1927
1928 RD::get_singleton()->draw_command_end_label();
1929
1930 if (debug_voxelgis) {
1931 //debug voxelgis
1932 bool will_continue_color = (can_continue_color || draw_sky || draw_sky_fog_only);
1933 bool will_continue_depth = (can_continue_depth || draw_sky || draw_sky_fog_only);
1934
1935 Projection dc;
1936 dc.set_depth_correction(true);
1937 Projection cm = (dc * p_render_data->scene_data->cam_projection) * Projection(p_render_data->scene_data->cam_transform.affine_inverse());
1938 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(color_only_framebuffer, RD::INITIAL_ACTION_CONTINUE, will_continue_color ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CONTINUE, will_continue_depth ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ);
1939 RD::get_singleton()->draw_command_begin_label("Debug VoxelGIs");
1940 for (int i = 0; i < (int)p_render_data->voxel_gi_instances->size(); i++) {
1941 gi.debug_voxel_gi((*p_render_data->voxel_gi_instances)[i], draw_list, color_only_framebuffer, cm, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_VOXEL_GI_LIGHTING, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_VOXEL_GI_EMISSION, 1.0);
1942 }
1943 RD::get_singleton()->draw_command_end_label();
1944 RD::get_singleton()->draw_list_end();
1945 }
1946
1947 if (debug_sdfgi_probes) {
1948 //debug sdfgi
1949 bool will_continue_color = (can_continue_color || draw_sky || draw_sky_fog_only);
1950 bool will_continue_depth = (can_continue_depth || draw_sky || draw_sky_fog_only);
1951
1952 Projection dc;
1953 dc.set_depth_correction(true);
1954 Projection cms[RendererSceneRender::MAX_RENDER_VIEWS];
1955 for (uint32_t v = 0; v < p_render_data->scene_data->view_count; v++) {
1956 cms[v] = (dc * p_render_data->scene_data->view_projection[v]) * Projection(p_render_data->scene_data->cam_transform.affine_inverse());
1957 }
1958 _debug_sdfgi_probes(rb, color_only_framebuffer, p_render_data->scene_data->view_count, cms, will_continue_color, will_continue_depth);
1959 }
1960
1961 if (draw_sky || draw_sky_fog_only) {
1962 RENDER_TIMESTAMP("Render Sky");
1963
1964 RD::get_singleton()->draw_command_begin_label("Draw Sky");
1965 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(color_only_framebuffer, RD::INITIAL_ACTION_CONTINUE, can_continue_color ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CONTINUE, can_continue_depth ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ);
1966
1967 sky.draw_sky(draw_list, rb, p_render_data->environment, color_only_framebuffer, time, sky_energy_multiplier);
1968
1969 RD::get_singleton()->draw_list_end();
1970 RD::get_singleton()->draw_command_end_label();
1971 }
1972 if (rb->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
1973 RENDER_TIMESTAMP("Resolve MSAA");
1974
1975 if (!can_continue_color) {
1976 // Handle views individual, might want to look at rewriting our resolve to do both layers in one pass.
1977 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
1978 RD::get_singleton()->texture_resolve_multisample(rb->get_color_msaa(v), rb->get_internal_texture(v));
1979 }
1980 if (using_separate_specular) {
1981 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
1982 RD::get_singleton()->texture_resolve_multisample(rb_data->get_specular_msaa(v), rb_data->get_specular(v));
1983 }
1984 }
1985 }
1986
1987 if (!can_continue_depth) {
1988 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
1989 resolve_effects->resolve_depth(rb->get_depth_msaa(v), rb->get_depth_texture(v), rb->get_internal_size(), texture_multisamples[rb->get_msaa_3d()]);
1990 }
1991 }
1992 }
1993
1994 if (using_separate_specular) {
1995 if (using_sss) {
1996 RENDER_TIMESTAMP("Sub-Surface Scattering");
1997 RD::get_singleton()->draw_command_begin_label("Process Sub-Surface Scattering");
1998 _process_sss(rb, p_render_data->scene_data->cam_projection);
1999 RD::get_singleton()->draw_command_end_label();
2000 }
2001
2002 if (using_ssr) {
2003 RENDER_TIMESTAMP("Screen-Space Reflections");
2004 RD::get_singleton()->draw_command_begin_label("Process Screen-Space Reflections");
2005 RID specular_views[RendererSceneRender::MAX_RENDER_VIEWS];
2006 for (uint32_t v = 0; v < p_render_data->scene_data->view_count; v++) {
2007 specular_views[v] = rb_data->get_specular(v);
2008 }
2009 _process_ssr(rb, color_only_framebuffer, normal_roughness_views, rb_data->get_specular(), specular_views, p_render_data->environment, p_render_data->scene_data->view_projection, p_render_data->scene_data->view_eye_offset, rb->get_msaa_3d() == RS::VIEWPORT_MSAA_DISABLED);
2010 RD::get_singleton()->draw_command_end_label();
2011 } else {
2012 //just mix specular back
2013 RENDER_TIMESTAMP("Merge Specular");
2014 copy_effects->merge_specular(color_only_framebuffer, rb_data->get_specular(), rb->get_msaa_3d() == RS::VIEWPORT_MSAA_DISABLED ? RID() : rb->get_internal_texture(), RID(), p_render_data->scene_data->view_count);
2015 }
2016 }
2017
2018 if (using_separate_specular && is_environment(p_render_data->environment) && (environment_get_background(p_render_data->environment) == RS::ENV_BG_CANVAS)) {
2019 // Canvas background mode does not clear the color buffer, but copies over it. If screen-space specular effects are enabled and the background is blank,
2020 // this results in ghosting due to the separate specular buffer copy. Need to explicitly clear the specular buffer once we're done with it to fix it.
2021 RENDER_TIMESTAMP("Clear Separate Specular (Canvas Background Mode)");
2022 Vector<Color> blank_clear_color;
2023 blank_clear_color.push_back(Color(0.0, 0.0, 0.0));
2024 RD::get_singleton()->draw_list_begin(rb_data->get_specular_only_fb(), RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, blank_clear_color);
2025 RD::get_singleton()->draw_list_end();
2026 }
2027
2028 if (scene_state.used_screen_texture) {
2029 RENDER_TIMESTAMP("Copy Screen Texture");
2030
2031 // Copy screen texture to backbuffer so we can read from it
2032 _render_buffers_copy_screen_texture(p_render_data);
2033 }
2034
2035 if (scene_state.used_depth_texture) {
2036 RENDER_TIMESTAMP("Copy Depth Texture");
2037
2038 // Copy depth texture to backbuffer so we can read from it
2039 _render_buffers_copy_depth_texture(p_render_data);
2040 }
2041
2042 RENDER_TIMESTAMP("Render 3D Transparent Pass");
2043
2044 RD::get_singleton()->draw_command_begin_label("Render 3D Transparent Pass");
2045
2046 rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_ALPHA, p_render_data, radiance_texture, true);
2047
2048 _setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, false);
2049
2050 {
2051 uint32_t transparent_color_pass_flags = (color_pass_flags | COLOR_PASS_FLAG_TRANSPARENT) & ~(COLOR_PASS_FLAG_SEPARATE_SPECULAR);
2052 RID alpha_framebuffer = rb_data.is_valid() ? rb_data->get_color_pass_fb(transparent_color_pass_flags) : color_only_framebuffer;
2053 RenderListParameters render_list_params(render_list[RENDER_LIST_ALPHA].elements.ptr(), render_list[RENDER_LIST_ALPHA].element_info.ptr(), render_list[RENDER_LIST_ALPHA].elements.size(), false, PASS_MODE_COLOR, transparent_color_pass_flags, rb_data.is_null(), p_render_data->directional_light_soft_shadows, rp_uniform_set, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME, Vector2(), p_render_data->scene_data->lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, p_render_data->scene_data->view_count);
2054 _render_list_with_threads(&render_list_params, alpha_framebuffer, can_continue_color ? RD::INITIAL_ACTION_CONTINUE : RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, can_continue_depth ? RD::INITIAL_ACTION_CONTINUE : RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ);
2055 }
2056
2057 RD::get_singleton()->draw_command_end_label();
2058
2059 RENDER_TIMESTAMP("Resolve");
2060
2061 RD::get_singleton()->draw_command_begin_label("Resolve");
2062
2063 if (rb_data.is_valid() && rb->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) {
2064 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
2065 RD::get_singleton()->texture_resolve_multisample(rb->get_color_msaa(v), rb->get_internal_texture(v));
2066 resolve_effects->resolve_depth(rb->get_depth_msaa(v), rb->get_depth_texture(v), rb->get_internal_size(), texture_multisamples[rb->get_msaa_3d()]);
2067 }
2068 if (taa && rb->get_use_taa()) {
2069 taa->msaa_resolve(rb);
2070 }
2071 }
2072
2073 RD::get_singleton()->draw_command_end_label();
2074
2075 RD::get_singleton()->draw_command_begin_label("Copy framebuffer for SSIL");
2076 if (using_ssil) {
2077 RENDER_TIMESTAMP("Copy Final Framebuffer (SSIL)");
2078 _copy_framebuffer_to_ssil(rb);
2079 }
2080 RD::get_singleton()->draw_command_end_label();
2081
2082 if (rb_data.is_valid() && taa && rb->get_use_taa()) {
2083 RENDER_TIMESTAMP("TAA")
2084 taa->process(rb, _render_buffers_get_color_format(), p_render_data->scene_data->z_near, p_render_data->scene_data->z_far);
2085 }
2086
2087 if (rb_data.is_valid()) {
2088 _debug_draw_cluster(rb);
2089
2090 RENDER_TIMESTAMP("Tonemap");
2091
2092 _render_buffers_post_process_and_tonemap(p_render_data);
2093 }
2094
2095 if (rb_data.is_valid()) {
2096 _render_buffers_debug_draw(p_render_data);
2097
2098 if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SDFGI && rb->has_custom_data(RB_SCOPE_SDFGI)) {
2099 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
2100 Vector<RID> view_rids;
2101
2102 // SDFGI renders at internal resolution, need to check if our debug correctly supports outputting upscaled.
2103 Size2i size = rb->get_internal_size();
2104 RID source_texture = rb->get_internal_texture();
2105 for (uint32_t v = 0; v < rb->get_view_count(); v++) {
2106 view_rids.push_back(rb->get_internal_texture(v));
2107 }
2108
2109 sdfgi->debug_draw(p_render_data->scene_data->view_count, p_render_data->scene_data->view_projection, p_render_data->scene_data->cam_transform, size.x, size.y, rb->get_render_target(), source_texture, view_rids);
2110 }
2111 }
2112}
2113
2114void RenderForwardClustered::_render_buffers_debug_draw(const RenderDataRD *p_render_data) {
2115 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
2116
2117 Ref<RenderSceneBuffersRD> rb = p_render_data->render_buffers;
2118 ERR_FAIL_COND(rb.is_null());
2119
2120 Ref<RenderBufferDataForwardClustered> rb_data = rb->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
2121 ERR_FAIL_COND(rb_data.is_null());
2122
2123 RendererSceneRenderRD::_render_buffers_debug_draw(p_render_data);
2124
2125 RID render_target = rb->get_render_target();
2126
2127 if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SSAO && rb->has_texture(RB_SCOPE_SSAO, RB_FINAL)) {
2128 RID final = rb->get_texture_slice(RB_SCOPE_SSAO, RB_FINAL, 0, 0);
2129 Size2i rtsize = texture_storage->render_target_get_size(render_target);
2130 copy_effects->copy_to_fb_rect(final, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, true);
2131 }
2132
2133 if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SSIL && rb->has_texture(RB_SCOPE_SSIL, RB_FINAL)) {
2134 RID final = rb->get_texture_slice(RB_SCOPE_SSIL, RB_FINAL, 0, 0);
2135 Size2i rtsize = texture_storage->render_target_get_size(render_target);
2136 copy_effects->copy_to_fb_rect(final, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, false);
2137 }
2138
2139 if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_GI_BUFFER && rb->has_texture(RB_SCOPE_GI, RB_TEX_AMBIENT)) {
2140 Size2i rtsize = texture_storage->render_target_get_size(render_target);
2141 RID ambient_texture = rb->get_texture(RB_SCOPE_GI, RB_TEX_AMBIENT);
2142 RID reflection_texture = rb->get_texture(RB_SCOPE_GI, RB_TEX_REFLECTION);
2143 copy_effects->copy_to_fb_rect(ambient_texture, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, false, false, true, reflection_texture, rb->get_view_count() > 1);
2144 }
2145}
2146
2147void RenderForwardClustered::_render_shadow_pass(RID p_light, RID p_shadow_atlas, int p_pass, const PagedArray<RenderGeometryInstance *> &p_instances, const Plane &p_camera_plane, float p_lod_distance_multiplier, float p_screen_mesh_lod_threshold, bool p_open_pass, bool p_close_pass, bool p_clear_region, RenderingMethod::RenderInfo *p_render_info) {
2148 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
2149
2150 ERR_FAIL_COND(!light_storage->owns_light_instance(p_light));
2151
2152 RID base = light_storage->light_instance_get_base_light(p_light);
2153
2154 Rect2i atlas_rect;
2155 uint32_t atlas_size = 1;
2156 RID atlas_fb;
2157
2158 bool reverse_cull_face = light_storage->light_get_reverse_cull_face_mode(base);
2159 bool using_dual_paraboloid = false;
2160 bool using_dual_paraboloid_flip = false;
2161 Vector2i dual_paraboloid_offset;
2162 RID render_fb;
2163 RID render_texture;
2164 float zfar;
2165
2166 bool use_pancake = false;
2167 bool render_cubemap = false;
2168 bool finalize_cubemap = false;
2169
2170 bool flip_y = false;
2171
2172 Projection light_projection;
2173 Transform3D light_transform;
2174
2175 if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) {
2176 //set pssm stuff
2177 uint64_t last_scene_shadow_pass = light_storage->light_instance_get_shadow_pass(p_light);
2178 if (last_scene_shadow_pass != get_scene_pass()) {
2179 light_storage->light_instance_set_directional_rect(p_light, light_storage->get_directional_shadow_rect());
2180 light_storage->directional_shadow_increase_current_light();
2181 light_storage->light_instance_set_shadow_pass(p_light, get_scene_pass());
2182 }
2183
2184 use_pancake = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE) > 0;
2185 light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass);
2186 light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass);
2187
2188 atlas_rect = light_storage->light_instance_get_directional_rect(p_light);
2189
2190 if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
2191 atlas_rect.size.width /= 2;
2192 atlas_rect.size.height /= 2;
2193
2194 if (p_pass == 1) {
2195 atlas_rect.position.x += atlas_rect.size.width;
2196 } else if (p_pass == 2) {
2197 atlas_rect.position.y += atlas_rect.size.height;
2198 } else if (p_pass == 3) {
2199 atlas_rect.position += atlas_rect.size;
2200 }
2201 } else if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
2202 atlas_rect.size.height /= 2;
2203
2204 if (p_pass == 0) {
2205 } else {
2206 atlas_rect.position.y += atlas_rect.size.height;
2207 }
2208 }
2209
2210 float directional_shadow_size = light_storage->directional_shadow_get_size();
2211 Rect2 atlas_rect_norm = atlas_rect;
2212 atlas_rect_norm.position /= directional_shadow_size;
2213 atlas_rect_norm.size /= directional_shadow_size;
2214 light_storage->light_instance_set_directional_shadow_atlas_rect(p_light, p_pass, atlas_rect_norm);
2215
2216 zfar = RSG::light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE);
2217
2218 render_fb = light_storage->direction_shadow_get_fb();
2219 render_texture = RID();
2220 flip_y = true;
2221
2222 } else {
2223 //set from shadow atlas
2224
2225 ERR_FAIL_COND(!light_storage->owns_shadow_atlas(p_shadow_atlas));
2226 ERR_FAIL_COND(!light_storage->shadow_atlas_owns_light_instance(p_shadow_atlas, p_light));
2227
2228 RSG::light_storage->shadow_atlas_update(p_shadow_atlas);
2229
2230 uint32_t key = light_storage->shadow_atlas_get_light_instance_key(p_shadow_atlas, p_light);
2231
2232 uint32_t quadrant = (key >> RendererRD::LightStorage::QUADRANT_SHIFT) & 0x3;
2233 uint32_t shadow = key & RendererRD::LightStorage::SHADOW_INDEX_MASK;
2234 uint32_t subdivision = light_storage->shadow_atlas_get_quadrant_subdivision(p_shadow_atlas, quadrant);
2235
2236 ERR_FAIL_INDEX((int)shadow, light_storage->shadow_atlas_get_quadrant_shadow_size(p_shadow_atlas, quadrant));
2237
2238 uint32_t shadow_atlas_size = light_storage->shadow_atlas_get_size(p_shadow_atlas);
2239 uint32_t quadrant_size = shadow_atlas_size >> 1;
2240
2241 atlas_rect.position.x = (quadrant & 1) * quadrant_size;
2242 atlas_rect.position.y = (quadrant >> 1) * quadrant_size;
2243
2244 uint32_t shadow_size = (quadrant_size / subdivision);
2245 atlas_rect.position.x += (shadow % subdivision) * shadow_size;
2246 atlas_rect.position.y += (shadow / subdivision) * shadow_size;
2247
2248 atlas_rect.size.width = shadow_size;
2249 atlas_rect.size.height = shadow_size;
2250
2251 zfar = light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE);
2252
2253 if (light_storage->light_get_type(base) == RS::LIGHT_OMNI) {
2254 bool wrap = (shadow + 1) % subdivision == 0;
2255 dual_paraboloid_offset = wrap ? Vector2i(1 - subdivision, 1) : Vector2i(1, 0);
2256
2257 if (light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) {
2258 render_texture = light_storage->get_cubemap(shadow_size / 2);
2259 render_fb = light_storage->get_cubemap_fb(shadow_size / 2, p_pass);
2260
2261 light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass);
2262 light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass);
2263 render_cubemap = true;
2264 finalize_cubemap = p_pass == 5;
2265 atlas_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas);
2266
2267 atlas_size = shadow_atlas_size;
2268
2269 if (p_pass == 0) {
2270 _render_shadow_begin();
2271 }
2272
2273 } else {
2274 atlas_rect.position.x += 1;
2275 atlas_rect.position.y += 1;
2276 atlas_rect.size.x -= 2;
2277 atlas_rect.size.y -= 2;
2278
2279 atlas_rect.position += p_pass * atlas_rect.size * dual_paraboloid_offset;
2280
2281 light_projection = light_storage->light_instance_get_shadow_camera(p_light, 0);
2282 light_transform = light_storage->light_instance_get_shadow_transform(p_light, 0);
2283
2284 using_dual_paraboloid = true;
2285 using_dual_paraboloid_flip = p_pass == 1;
2286 render_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas);
2287 flip_y = true;
2288 }
2289
2290 } else if (light_storage->light_get_type(base) == RS::LIGHT_SPOT) {
2291 light_projection = light_storage->light_instance_get_shadow_camera(p_light, 0);
2292 light_transform = light_storage->light_instance_get_shadow_transform(p_light, 0);
2293
2294 render_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas);
2295
2296 flip_y = true;
2297 }
2298 }
2299
2300 if (render_cubemap) {
2301 //rendering to cubemap
2302 _render_shadow_append(render_fb, p_instances, light_projection, light_transform, zfar, 0, 0, reverse_cull_face, false, false, use_pancake, p_camera_plane, p_lod_distance_multiplier, p_screen_mesh_lod_threshold, Rect2(), false, true, true, true, p_render_info);
2303 if (finalize_cubemap) {
2304 _render_shadow_process();
2305 _render_shadow_end();
2306 //reblit
2307 Rect2 atlas_rect_norm = atlas_rect;
2308 atlas_rect_norm.position /= float(atlas_size);
2309 atlas_rect_norm.size /= float(atlas_size);
2310 copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), light_projection.get_z_far(), false);
2311 atlas_rect_norm.position += Vector2(dual_paraboloid_offset) * atlas_rect_norm.size;
2312 copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), light_projection.get_z_far(), true);
2313
2314 //restore transform so it can be properly used
2315 light_storage->light_instance_set_shadow_transform(p_light, Projection(), light_storage->light_instance_get_base_transform(p_light), zfar, 0, 0, 0);
2316 }
2317
2318 } else {
2319 //render shadow
2320 _render_shadow_append(render_fb, p_instances, light_projection, light_transform, zfar, 0, 0, reverse_cull_face, using_dual_paraboloid, using_dual_paraboloid_flip, use_pancake, p_camera_plane, p_lod_distance_multiplier, p_screen_mesh_lod_threshold, atlas_rect, flip_y, p_clear_region, p_open_pass, p_close_pass, p_render_info);
2321 }
2322}
2323
2324void RenderForwardClustered::_render_shadow_begin() {
2325 scene_state.shadow_passes.clear();
2326 RD::get_singleton()->draw_command_begin_label("Shadow Setup");
2327 _update_render_base_uniform_set();
2328
2329 render_list[RENDER_LIST_SECONDARY].clear();
2330 scene_state.instance_data[RENDER_LIST_SECONDARY].clear();
2331}
2332
2333void RenderForwardClustered::_render_shadow_append(RID p_framebuffer, const PagedArray<RenderGeometryInstance *> &p_instances, const Projection &p_projection, const Transform3D &p_transform, float p_zfar, float p_bias, float p_normal_bias, bool p_reverse_cull_face, bool p_use_dp, bool p_use_dp_flip, bool p_use_pancake, const Plane &p_camera_plane, float p_lod_distance_multiplier, float p_screen_mesh_lod_threshold, const Rect2i &p_rect, bool p_flip_y, bool p_clear_region, bool p_begin, bool p_end, RenderingMethod::RenderInfo *p_render_info) {
2334 uint32_t shadow_pass_index = scene_state.shadow_passes.size();
2335
2336 SceneState::ShadowPass shadow_pass;
2337
2338 RenderSceneDataRD scene_data;
2339 scene_data.cam_projection = p_projection;
2340 scene_data.cam_transform = p_transform;
2341 scene_data.view_projection[0] = p_projection;
2342 scene_data.z_far = p_zfar;
2343 scene_data.z_near = 0.0;
2344 scene_data.lod_distance_multiplier = p_lod_distance_multiplier;
2345 scene_data.dual_paraboloid_side = p_use_dp_flip ? -1 : 1;
2346 scene_data.opaque_prepass_threshold = 0.1f;
2347 scene_data.time = time;
2348 scene_data.time_step = time_step;
2349
2350 RenderDataRD render_data;
2351 render_data.scene_data = &scene_data;
2352 render_data.cluster_size = 1;
2353 render_data.cluster_max_elements = 32;
2354 render_data.instances = &p_instances;
2355 render_data.render_info = p_render_info;
2356
2357 _setup_environment(&render_data, true, Vector2(1, 1), !p_flip_y, Color(), false, p_use_pancake, shadow_pass_index);
2358
2359 if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) {
2360 scene_data.screen_mesh_lod_threshold = 0.0;
2361 } else {
2362 scene_data.screen_mesh_lod_threshold = p_screen_mesh_lod_threshold;
2363 }
2364
2365 PassMode pass_mode = p_use_dp ? PASS_MODE_SHADOW_DP : PASS_MODE_SHADOW;
2366
2367 uint32_t render_list_from = render_list[RENDER_LIST_SECONDARY].elements.size();
2368 _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode, 0, false, false, true);
2369 uint32_t render_list_size = render_list[RENDER_LIST_SECONDARY].elements.size() - render_list_from;
2370 render_list[RENDER_LIST_SECONDARY].sort_by_key_range(render_list_from, render_list_size);
2371 _fill_instance_data(RENDER_LIST_SECONDARY, p_render_info ? p_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW] : (int *)nullptr, render_list_from, render_list_size, false);
2372
2373 {
2374 //regular forward for now
2375 bool flip_cull = p_use_dp_flip;
2376 if (p_flip_y) {
2377 flip_cull = !flip_cull;
2378 }
2379
2380 if (p_reverse_cull_face) {
2381 flip_cull = !flip_cull;
2382 }
2383
2384 shadow_pass.element_from = render_list_from;
2385 shadow_pass.element_count = render_list_size;
2386 shadow_pass.flip_cull = flip_cull;
2387 shadow_pass.pass_mode = pass_mode;
2388
2389 shadow_pass.rp_uniform_set = RID(); //will be filled later when instance buffer is complete
2390 shadow_pass.camera_plane = p_camera_plane;
2391 shadow_pass.screen_mesh_lod_threshold = scene_data.screen_mesh_lod_threshold;
2392 shadow_pass.lod_distance_multiplier = scene_data.lod_distance_multiplier;
2393
2394 shadow_pass.framebuffer = p_framebuffer;
2395 shadow_pass.initial_depth_action = p_begin ? (p_clear_region ? RD::INITIAL_ACTION_CLEAR_REGION : RD::INITIAL_ACTION_CLEAR) : (p_clear_region ? RD::INITIAL_ACTION_CLEAR_REGION_CONTINUE : RD::INITIAL_ACTION_CONTINUE);
2396 shadow_pass.final_depth_action = p_end ? RD::FINAL_ACTION_READ : RD::FINAL_ACTION_CONTINUE;
2397 shadow_pass.rect = p_rect;
2398
2399 scene_state.shadow_passes.push_back(shadow_pass);
2400 }
2401}
2402
2403void RenderForwardClustered::_render_shadow_process() {
2404 _update_instance_data_buffer(RENDER_LIST_SECONDARY);
2405 //render shadows one after the other, so this can be done un-barriered and the driver can optimize (as well as allow us to run compute at the same time)
2406
2407 for (uint32_t i = 0; i < scene_state.shadow_passes.size(); i++) {
2408 //render passes need to be configured after instance buffer is done, since they need the latest version
2409 SceneState::ShadowPass &shadow_pass = scene_state.shadow_passes[i];
2410 shadow_pass.rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), false, i);
2411 }
2412
2413 RD::get_singleton()->draw_command_end_label();
2414}
2415void RenderForwardClustered::_render_shadow_end(uint32_t p_barrier) {
2416 RD::get_singleton()->draw_command_begin_label("Shadow Render");
2417
2418 for (SceneState::ShadowPass &shadow_pass : scene_state.shadow_passes) {
2419 RenderListParameters render_list_parameters(render_list[RENDER_LIST_SECONDARY].elements.ptr() + shadow_pass.element_from, render_list[RENDER_LIST_SECONDARY].element_info.ptr() + shadow_pass.element_from, shadow_pass.element_count, shadow_pass.flip_cull, shadow_pass.pass_mode, 0, true, false, shadow_pass.rp_uniform_set, false, Vector2(), shadow_pass.lod_distance_multiplier, shadow_pass.screen_mesh_lod_threshold, 1, shadow_pass.element_from, RD::BARRIER_MASK_NO_BARRIER);
2420 _render_list_with_threads(&render_list_parameters, shadow_pass.framebuffer, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, shadow_pass.initial_depth_action, shadow_pass.final_depth_action, Vector<Color>(), 1.0, 0, shadow_pass.rect);
2421 }
2422
2423 if (p_barrier != RD::BARRIER_MASK_NO_BARRIER) {
2424 RD::get_singleton()->barrier(RD::BARRIER_MASK_RASTER, p_barrier);
2425 }
2426 RD::get_singleton()->draw_command_end_label();
2427}
2428
2429void RenderForwardClustered::_render_particle_collider_heightfield(RID p_fb, const Transform3D &p_cam_transform, const Projection &p_cam_projection, const PagedArray<RenderGeometryInstance *> &p_instances) {
2430 RENDER_TIMESTAMP("Setup GPUParticlesCollisionHeightField3D");
2431
2432 RD::get_singleton()->draw_command_begin_label("Render Collider Heightfield");
2433
2434 RenderSceneDataRD scene_data;
2435 scene_data.cam_projection = p_cam_projection;
2436 scene_data.cam_transform = p_cam_transform;
2437 scene_data.view_projection[0] = p_cam_projection;
2438 scene_data.z_near = 0.0;
2439 scene_data.z_far = p_cam_projection.get_z_far();
2440 scene_data.dual_paraboloid_side = 0;
2441 scene_data.opaque_prepass_threshold = 0.0;
2442 scene_data.time = time;
2443 scene_data.time_step = time_step;
2444
2445 RenderDataRD render_data;
2446 render_data.scene_data = &scene_data;
2447 render_data.cluster_size = 1;
2448 render_data.cluster_max_elements = 32;
2449 render_data.instances = &p_instances;
2450
2451 _update_render_base_uniform_set();
2452
2453 _setup_environment(&render_data, true, Vector2(1, 1), true, Color(), false, false);
2454
2455 PassMode pass_mode = PASS_MODE_SHADOW;
2456
2457 _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
2458 render_list[RENDER_LIST_SECONDARY].sort_by_key();
2459 _fill_instance_data(RENDER_LIST_SECONDARY);
2460
2461 RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID());
2462
2463 RENDER_TIMESTAMP("Render Collider Heightfield");
2464
2465 {
2466 //regular forward for now
2467 RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].element_info.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), false, pass_mode, 0, true, false, rp_uniform_set);
2468 _render_list_with_threads(&render_list_params, p_fb, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ);
2469 }
2470 RD::get_singleton()->draw_command_end_label();
2471}
2472
2473void RenderForwardClustered::_render_material(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region, float p_exposure_normalization) {
2474 RENDER_TIMESTAMP("Setup Rendering 3D Material");
2475
2476 RD::get_singleton()->draw_command_begin_label("Render 3D Material");
2477
2478 RenderSceneDataRD scene_data;
2479 scene_data.cam_projection = p_cam_projection;
2480 scene_data.cam_transform = p_cam_transform;
2481 scene_data.view_projection[0] = p_cam_projection;
2482 scene_data.dual_paraboloid_side = 0;
2483 scene_data.material_uv2_mode = false;
2484 scene_data.opaque_prepass_threshold = 0.0f;
2485 scene_data.emissive_exposure_normalization = p_exposure_normalization;
2486 scene_data.time = time;
2487 scene_data.time_step = time_step;
2488
2489 RenderDataRD render_data;
2490 render_data.scene_data = &scene_data;
2491 render_data.cluster_size = 1;
2492 render_data.cluster_max_elements = 32;
2493 render_data.instances = &p_instances;
2494
2495 scene_shader.enable_advanced_shader_group();
2496
2497 _update_render_base_uniform_set();
2498
2499 _setup_environment(&render_data, true, Vector2(1, 1), false, Color());
2500
2501 PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL;
2502 _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
2503 render_list[RENDER_LIST_SECONDARY].sort_by_key();
2504 _fill_instance_data(RENDER_LIST_SECONDARY);
2505
2506 RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID());
2507
2508 RENDER_TIMESTAMP("Render 3D Material");
2509
2510 {
2511 RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].element_info.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), true, pass_mode, 0, true, false, rp_uniform_set);
2512 //regular forward for now
2513 Vector<Color> clear = {
2514 Color(0, 0, 0, 0),
2515 Color(0, 0, 0, 0),
2516 Color(0, 0, 0, 0),
2517 Color(0, 0, 0, 0),
2518 Color(0, 0, 0, 0)
2519 };
2520
2521 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, clear, 1.0, 0, p_region);
2522 _render_list(draw_list, RD::get_singleton()->framebuffer_get_format(p_framebuffer), &render_list_params, 0, render_list_params.element_count);
2523 RD::get_singleton()->draw_list_end();
2524 }
2525
2526 RD::get_singleton()->draw_command_end_label();
2527}
2528
2529void RenderForwardClustered::_render_uv2(const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) {
2530 RENDER_TIMESTAMP("Setup Rendering UV2");
2531
2532 RD::get_singleton()->draw_command_begin_label("Render UV2");
2533
2534 RenderSceneDataRD scene_data;
2535 scene_data.dual_paraboloid_side = 0;
2536 scene_data.material_uv2_mode = true;
2537 scene_data.opaque_prepass_threshold = 0.0;
2538 scene_data.emissive_exposure_normalization = -1.0;
2539
2540 RenderDataRD render_data;
2541 render_data.scene_data = &scene_data;
2542 render_data.cluster_size = 1;
2543 render_data.cluster_max_elements = 32;
2544 render_data.instances = &p_instances;
2545
2546 scene_shader.enable_advanced_shader_group();
2547
2548 _update_render_base_uniform_set();
2549
2550 _setup_environment(&render_data, true, Vector2(1, 1), false, Color());
2551
2552 PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL;
2553 _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
2554 render_list[RENDER_LIST_SECONDARY].sort_by_key();
2555 _fill_instance_data(RENDER_LIST_SECONDARY);
2556
2557 RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID());
2558
2559 RENDER_TIMESTAMP("Render 3D Material");
2560
2561 {
2562 RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].element_info.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), true, pass_mode, 0, true, false, rp_uniform_set, true);
2563 //regular forward for now
2564 Vector<Color> clear = {
2565 Color(0, 0, 0, 0),
2566 Color(0, 0, 0, 0),
2567 Color(0, 0, 0, 0),
2568 Color(0, 0, 0, 0),
2569 Color(0, 0, 0, 0)
2570 };
2571 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, clear, 1.0, 0, p_region);
2572
2573 const int uv_offset_count = 9;
2574 static const Vector2 uv_offsets[uv_offset_count] = {
2575 Vector2(-1, 1),
2576 Vector2(1, 1),
2577 Vector2(1, -1),
2578 Vector2(-1, -1),
2579 Vector2(-1, 0),
2580 Vector2(1, 0),
2581 Vector2(0, -1),
2582 Vector2(0, 1),
2583 Vector2(0, 0),
2584
2585 };
2586
2587 for (int i = 0; i < uv_offset_count; i++) {
2588 Vector2 ofs = uv_offsets[i];
2589 ofs.x /= p_region.size.width;
2590 ofs.y /= p_region.size.height;
2591 render_list_params.uv_offset = ofs;
2592 _render_list(draw_list, RD::get_singleton()->framebuffer_get_format(p_framebuffer), &render_list_params, 0, render_list_params.element_count); //first wireframe, for pseudo conservative
2593 }
2594 render_list_params.uv_offset = Vector2();
2595 render_list_params.force_wireframe = false;
2596 _render_list(draw_list, RD::get_singleton()->framebuffer_get_format(p_framebuffer), &render_list_params, 0, render_list_params.element_count); //second regular triangles
2597
2598 RD::get_singleton()->draw_list_end();
2599 }
2600
2601 RD::get_singleton()->draw_command_end_label();
2602}
2603
2604void RenderForwardClustered::_render_sdfgi(Ref<RenderSceneBuffersRD> p_render_buffers, const Vector3i &p_from, const Vector3i &p_size, const AABB &p_bounds, const PagedArray<RenderGeometryInstance *> &p_instances, const RID &p_albedo_texture, const RID &p_emission_texture, const RID &p_emission_aniso_texture, const RID &p_geom_facing_texture, float p_exposure_normalization) {
2605 RENDER_TIMESTAMP("Render SDFGI");
2606
2607 RD::get_singleton()->draw_command_begin_label("Render SDFGI Voxel");
2608
2609 RenderSceneDataRD scene_data;
2610
2611 RenderDataRD render_data;
2612 render_data.scene_data = &scene_data;
2613 render_data.cluster_size = 1;
2614 render_data.cluster_max_elements = 32;
2615 render_data.instances = &p_instances;
2616
2617 _update_render_base_uniform_set();
2618
2619 PassMode pass_mode = PASS_MODE_SDF;
2620 _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
2621 render_list[RENDER_LIST_SECONDARY].sort_by_key();
2622 _fill_instance_data(RENDER_LIST_SECONDARY);
2623
2624 Vector3 half_size = p_bounds.size * 0.5;
2625 Vector3 center = p_bounds.position + half_size;
2626
2627 Vector<RID> sbs = {
2628 p_albedo_texture,
2629 p_emission_texture,
2630 p_emission_aniso_texture,
2631 p_geom_facing_texture
2632 };
2633
2634 //print_line("re-render " + p_from + " - " + p_size + " bounds " + p_bounds);
2635 for (int i = 0; i < 3; i++) {
2636 scene_state.ubo.sdf_offset[i] = p_from[i];
2637 scene_state.ubo.sdf_size[i] = p_size[i];
2638 }
2639
2640 for (int i = 0; i < 3; i++) {
2641 Vector3 axis;
2642 axis[i] = 1.0;
2643 Vector3 up, right;
2644 int right_axis = (i + 1) % 3;
2645 int up_axis = (i + 2) % 3;
2646 up[up_axis] = 1.0;
2647 right[right_axis] = 1.0;
2648
2649 Size2i fb_size;
2650 fb_size.x = p_size[right_axis];
2651 fb_size.y = p_size[up_axis];
2652
2653 scene_data.cam_transform.origin = center + axis * half_size;
2654 scene_data.cam_transform.basis.set_column(0, right);
2655 scene_data.cam_transform.basis.set_column(1, up);
2656 scene_data.cam_transform.basis.set_column(2, axis);
2657
2658 //print_line("pass: " + itos(i) + " xform " + scene_data.cam_transform);
2659
2660 float h_size = half_size[right_axis];
2661 float v_size = half_size[up_axis];
2662 float d_size = half_size[i] * 2.0;
2663 scene_data.cam_projection.set_orthogonal(-h_size, h_size, -v_size, v_size, 0, d_size);
2664 //print_line("pass: " + itos(i) + " cam hsize: " + rtos(h_size) + " vsize: " + rtos(v_size) + " dsize " + rtos(d_size));
2665
2666 Transform3D to_bounds;
2667 to_bounds.origin = p_bounds.position;
2668 to_bounds.basis.scale(p_bounds.size);
2669
2670 RendererRD::MaterialStorage::store_transform(to_bounds.affine_inverse() * scene_data.cam_transform, scene_state.ubo.sdf_to_bounds);
2671
2672 scene_data.emissive_exposure_normalization = p_exposure_normalization;
2673 _setup_environment(&render_data, true, Vector2(1, 1), false, Color());
2674
2675 RID rp_uniform_set = _setup_sdfgi_render_pass_uniform_set(p_albedo_texture, p_emission_texture, p_emission_aniso_texture, p_geom_facing_texture);
2676
2677 HashMap<Size2i, RID>::Iterator E = sdfgi_framebuffer_size_cache.find(fb_size);
2678 if (!E) {
2679 RID fb = RD::get_singleton()->framebuffer_create_empty(fb_size);
2680 E = sdfgi_framebuffer_size_cache.insert(fb_size, fb);
2681 }
2682
2683 RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].element_info.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), true, pass_mode, 0, true, false, rp_uniform_set, false);
2684 _render_list_with_threads(&render_list_params, E->value, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 1.0, 0, Rect2(), sbs);
2685 }
2686
2687 RD::get_singleton()->draw_command_end_label();
2688}
2689
2690void RenderForwardClustered::base_uniforms_changed() {
2691 if (!render_base_uniform_set.is_null() && RD::get_singleton()->uniform_set_is_valid(render_base_uniform_set)) {
2692 RD::get_singleton()->free(render_base_uniform_set);
2693 }
2694 render_base_uniform_set = RID();
2695}
2696
2697void RenderForwardClustered::_update_render_base_uniform_set() {
2698 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
2699 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
2700
2701 if (render_base_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(render_base_uniform_set) || (lightmap_texture_array_version != light_storage->lightmap_array_get_version()) || base_uniform_set_updated) {
2702 base_uniform_set_updated = false;
2703
2704 if (render_base_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(render_base_uniform_set)) {
2705 RD::get_singleton()->free(render_base_uniform_set);
2706 }
2707
2708 lightmap_texture_array_version = light_storage->lightmap_array_get_version();
2709
2710 Vector<RD::Uniform> uniforms;
2711
2712 {
2713 RD::Uniform u;
2714 u.binding = 2;
2715 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2716 u.append_id(scene_shader.shadow_sampler);
2717 uniforms.push_back(u);
2718 }
2719
2720 {
2721 RD::Uniform u;
2722 u.binding = 3;
2723 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2724 RID sampler;
2725 switch (decals_get_filter()) {
2726 case RS::DECAL_FILTER_NEAREST: {
2727 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2728 } break;
2729 case RS::DECAL_FILTER_LINEAR: {
2730 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2731 } break;
2732 case RS::DECAL_FILTER_NEAREST_MIPMAPS: {
2733 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2734 } break;
2735 case RS::DECAL_FILTER_LINEAR_MIPMAPS: {
2736 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2737 } break;
2738 case RS::DECAL_FILTER_NEAREST_MIPMAPS_ANISOTROPIC: {
2739 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2740 } break;
2741 case RS::DECAL_FILTER_LINEAR_MIPMAPS_ANISOTROPIC: {
2742 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2743 } break;
2744 }
2745
2746 u.append_id(sampler);
2747 uniforms.push_back(u);
2748 }
2749
2750 {
2751 RD::Uniform u;
2752 u.binding = 4;
2753 u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
2754 RID sampler;
2755 switch (light_projectors_get_filter()) {
2756 case RS::LIGHT_PROJECTOR_FILTER_NEAREST: {
2757 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2758 } break;
2759 case RS::LIGHT_PROJECTOR_FILTER_LINEAR: {
2760 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2761 } break;
2762 case RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS: {
2763 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2764 } break;
2765 case RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS: {
2766 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2767 } break;
2768 case RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS_ANISOTROPIC: {
2769 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2770 } break;
2771 case RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS_ANISOTROPIC: {
2772 sampler = material_storage->sampler_rd_get_custom(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
2773 } break;
2774 }
2775
2776 u.append_id(sampler);
2777 uniforms.push_back(u);
2778 }
2779
2780 {
2781 RD::Uniform u;
2782 u.binding = 5;
2783 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2784 u.append_id(RendererRD::LightStorage::get_singleton()->get_omni_light_buffer());
2785 uniforms.push_back(u);
2786 }
2787 {
2788 RD::Uniform u;
2789 u.binding = 6;
2790 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2791 u.append_id(RendererRD::LightStorage::get_singleton()->get_spot_light_buffer());
2792 uniforms.push_back(u);
2793 }
2794
2795 {
2796 RD::Uniform u;
2797 u.binding = 7;
2798 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2799 u.append_id(RendererRD::LightStorage::get_singleton()->get_reflection_probe_buffer());
2800 uniforms.push_back(u);
2801 }
2802 {
2803 RD::Uniform u;
2804 u.binding = 8;
2805 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
2806 u.append_id(RendererRD::LightStorage::get_singleton()->get_directional_light_buffer());
2807 uniforms.push_back(u);
2808 }
2809 {
2810 RD::Uniform u;
2811 u.binding = 9;
2812 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2813 u.append_id(scene_state.lightmap_buffer);
2814 uniforms.push_back(u);
2815 }
2816 {
2817 RD::Uniform u;
2818 u.binding = 10;
2819 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2820 u.append_id(scene_state.lightmap_capture_buffer);
2821 uniforms.push_back(u);
2822 }
2823 {
2824 RD::Uniform u;
2825 u.binding = 11;
2826 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2827 RID decal_atlas = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture();
2828 u.append_id(decal_atlas);
2829 uniforms.push_back(u);
2830 }
2831 {
2832 RD::Uniform u;
2833 u.binding = 12;
2834 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2835 RID decal_atlas = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture_srgb();
2836 u.append_id(decal_atlas);
2837 uniforms.push_back(u);
2838 }
2839 {
2840 RD::Uniform u;
2841 u.binding = 13;
2842 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2843 u.append_id(RendererRD::TextureStorage::get_singleton()->get_decal_buffer());
2844 uniforms.push_back(u);
2845 }
2846
2847 {
2848 RD::Uniform u;
2849 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2850 u.binding = 14;
2851 u.append_id(RendererRD::MaterialStorage::get_singleton()->global_shader_uniforms_get_storage_buffer());
2852 uniforms.push_back(u);
2853 }
2854
2855 {
2856 RD::Uniform u;
2857 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
2858 u.binding = 15;
2859 u.append_id(sdfgi_get_ubo());
2860 uniforms.push_back(u);
2861 }
2862
2863 uniforms.append_array(material_storage->get_default_sampler_uniforms(SAMPLERS_BINDING_FIRST_INDEX));
2864
2865 render_base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, scene_shader.default_shader_rd, SCENE_UNIFORM_SET);
2866 }
2867}
2868
2869RID RenderForwardClustered::_setup_render_pass_uniform_set(RenderListType p_render_list, const RenderDataRD *p_render_data, RID p_radiance_texture, bool p_use_directional_shadow_atlas, int p_index) {
2870 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
2871 RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
2872
2873 bool is_multiview = false;
2874
2875 Ref<RenderSceneBuffersRD> rb; // handy for not having to fully type out p_render_data->render_buffers all the time...
2876 Ref<RenderBufferDataForwardClustered> rb_data;
2877 if (p_render_data && p_render_data->render_buffers.is_valid()) {
2878 rb = p_render_data->render_buffers;
2879 is_multiview = rb->get_view_count() > 1;
2880 if (rb->has_custom_data(RB_SCOPE_FORWARD_CLUSTERED)) {
2881 // Our forward clustered custom data buffer will only be available when we're rendering our normal view.
2882 // This will not be available when rendering reflection probes.
2883 rb_data = rb->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
2884 }
2885 }
2886
2887 //default render buffer and scene state uniform set
2888
2889 Vector<RD::Uniform> uniforms;
2890
2891 {
2892 RD::Uniform u;
2893 u.binding = 0;
2894 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
2895 u.append_id(scene_state.uniform_buffers[p_index]);
2896 uniforms.push_back(u);
2897 }
2898 {
2899 RD::Uniform u;
2900 u.binding = 1;
2901 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
2902 u.append_id(scene_state.implementation_uniform_buffers[p_index]);
2903 uniforms.push_back(u);
2904 }
2905 {
2906 RD::Uniform u;
2907 u.binding = 2;
2908 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
2909 RID instance_buffer = scene_state.instance_buffer[p_render_list];
2910 if (instance_buffer == RID()) {
2911 instance_buffer = scene_shader.default_vec4_xform_buffer; // any buffer will do since its not used
2912 }
2913 u.append_id(instance_buffer);
2914 uniforms.push_back(u);
2915 }
2916 {
2917 RID radiance_texture;
2918 if (p_radiance_texture.is_valid()) {
2919 radiance_texture = p_radiance_texture;
2920 } else {
2921 radiance_texture = texture_storage->texture_rd_get_default(is_using_radiance_cubemap_array() ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK);
2922 }
2923 RD::Uniform u;
2924 u.binding = 3;
2925 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2926 u.append_id(radiance_texture);
2927 uniforms.push_back(u);
2928 }
2929 {
2930 RID ref_texture = (p_render_data && p_render_data->reflection_atlas.is_valid()) ? light_storage->reflection_atlas_get_texture(p_render_data->reflection_atlas) : RID();
2931 RD::Uniform u;
2932 u.binding = 4;
2933 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2934 if (ref_texture.is_valid()) {
2935 u.append_id(ref_texture);
2936 } else {
2937 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK));
2938 }
2939 uniforms.push_back(u);
2940 }
2941 {
2942 RD::Uniform u;
2943 u.binding = 5;
2944 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2945 RID texture;
2946 if (p_render_data && p_render_data->shadow_atlas.is_valid()) {
2947 texture = RendererRD::LightStorage::get_singleton()->shadow_atlas_get_texture(p_render_data->shadow_atlas);
2948 }
2949 if (!texture.is_valid()) {
2950 texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH);
2951 }
2952 u.append_id(texture);
2953 uniforms.push_back(u);
2954 }
2955 {
2956 RD::Uniform u;
2957 u.binding = 6;
2958 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2959 if (p_use_directional_shadow_atlas && RendererRD::LightStorage::get_singleton()->directional_shadow_get_texture().is_valid()) {
2960 u.append_id(RendererRD::LightStorage::get_singleton()->directional_shadow_get_texture());
2961 } else {
2962 u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH));
2963 }
2964 uniforms.push_back(u);
2965 }
2966 {
2967 RD::Uniform u;
2968 u.binding = 7;
2969 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2970
2971 RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
2972 for (uint32_t i = 0; i < scene_state.max_lightmaps; i++) {
2973 if (p_render_data && i < p_render_data->lightmaps->size()) {
2974 RID base = light_storage->lightmap_instance_get_lightmap((*p_render_data->lightmaps)[i]);
2975 RID texture = light_storage->lightmap_get_texture(base);
2976 RID rd_texture = texture_storage->texture_get_rd_texture(texture);
2977 u.append_id(rd_texture);
2978 } else {
2979 u.append_id(default_tex);
2980 }
2981 }
2982
2983 uniforms.push_back(u);
2984 }
2985 {
2986 RD::Uniform u;
2987 u.binding = 8;
2988 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2989 RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
2990 for (int i = 0; i < MAX_VOXEL_GI_INSTANCESS; i++) {
2991 if (p_render_data && i < (int)p_render_data->voxel_gi_instances->size()) {
2992 RID tex = gi.voxel_gi_instance_get_texture((*p_render_data->voxel_gi_instances)[i]);
2993 if (!tex.is_valid()) {
2994 tex = default_tex;
2995 }
2996 u.append_id(tex);
2997 } else {
2998 u.append_id(default_tex);
2999 }
3000 }
3001
3002 uniforms.push_back(u);
3003 }
3004
3005 {
3006 RD::Uniform u;
3007 u.binding = 9;
3008 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
3009 RID cb = (p_render_data && p_render_data->cluster_buffer.is_valid()) ? p_render_data->cluster_buffer : scene_shader.default_vec4_xform_buffer;
3010 u.append_id(cb);
3011 uniforms.push_back(u);
3012 }
3013
3014 {
3015 RD::Uniform u;
3016 u.binding = 10;
3017 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3018 RID texture;
3019 if (rb.is_valid() && rb->has_texture(RB_SCOPE_BUFFERS, RB_TEX_BACK_DEPTH)) {
3020 texture = rb->get_texture(RB_SCOPE_BUFFERS, RB_TEX_BACK_DEPTH);
3021 } else {
3022 texture = texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_DEPTH : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH);
3023 }
3024 u.append_id(texture);
3025 uniforms.push_back(u);
3026 }
3027 {
3028 RD::Uniform u;
3029 u.binding = 11;
3030 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3031 RID bbt = rb_data.is_valid() ? rb->get_back_buffer_texture() : RID();
3032 RID texture = bbt.is_valid() ? bbt : texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK);
3033 u.append_id(texture);
3034 uniforms.push_back(u);
3035 }
3036
3037 {
3038 RD::Uniform u;
3039 u.binding = 12;
3040 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3041 RID texture = rb_data.is_valid() && rb_data->has_normal_roughness() ? rb_data->get_normal_roughness() : texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_NORMAL : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_NORMAL);
3042 u.append_id(texture);
3043 uniforms.push_back(u);
3044 }
3045
3046 {
3047 RD::Uniform u;
3048 u.binding = 13;
3049 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3050 RID aot = rb.is_valid() && rb->has_texture(RB_SCOPE_SSAO, RB_FINAL) ? rb->get_texture(RB_SCOPE_SSAO, RB_FINAL) : RID();
3051 RID texture = aot.is_valid() ? aot : texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK);
3052 u.append_id(texture);
3053 uniforms.push_back(u);
3054 }
3055
3056 {
3057 RD::Uniform u;
3058 u.binding = 14;
3059 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3060 RID texture = rb_data.is_valid() && rb->has_texture(RB_SCOPE_GI, RB_TEX_AMBIENT) ? rb->get_texture(RB_SCOPE_GI, RB_TEX_AMBIENT) : texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK);
3061 u.append_id(texture);
3062 uniforms.push_back(u);
3063 }
3064
3065 {
3066 RD::Uniform u;
3067 u.binding = 15;
3068 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3069 RID texture = rb_data.is_valid() && rb->has_texture(RB_SCOPE_GI, RB_TEX_REFLECTION) ? rb->get_texture(RB_SCOPE_GI, RB_TEX_REFLECTION) : texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK);
3070 u.append_id(texture);
3071 uniforms.push_back(u);
3072 }
3073 {
3074 RD::Uniform u;
3075 u.binding = 16;
3076 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3077 RID t;
3078 if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_SDFGI)) {
3079 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
3080 t = sdfgi->lightprobe_texture;
3081 }
3082 if (t.is_null()) {
3083 t = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
3084 }
3085 u.append_id(t);
3086 uniforms.push_back(u);
3087 }
3088 {
3089 RD::Uniform u;
3090 u.binding = 17;
3091 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3092 RID t;
3093 if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_SDFGI)) {
3094 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
3095 t = sdfgi->occlusion_texture;
3096 }
3097 if (t.is_null()) {
3098 t = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
3099 }
3100 u.append_id(t);
3101 uniforms.push_back(u);
3102 }
3103 {
3104 RD::Uniform u;
3105 u.binding = 18;
3106 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
3107 RID voxel_gi;
3108 if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_GI)) {
3109 Ref<RendererRD::GI::RenderBuffersGI> rbgi = rb->get_custom_data(RB_SCOPE_GI);
3110 voxel_gi = rbgi->get_voxel_gi_buffer();
3111 }
3112 u.append_id(voxel_gi.is_valid() ? voxel_gi : render_buffers_get_default_voxel_gi_buffer());
3113 uniforms.push_back(u);
3114 }
3115 {
3116 RD::Uniform u;
3117 u.binding = 19;
3118 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3119 RID vfog;
3120 if (rb_data.is_valid() && rb->has_custom_data(RB_SCOPE_FOG)) {
3121 Ref<RendererRD::Fog::VolumetricFog> fog = rb->get_custom_data(RB_SCOPE_FOG);
3122 vfog = fog->fog_map;
3123 if (vfog.is_null()) {
3124 vfog = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
3125 }
3126 } else {
3127 vfog = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
3128 }
3129 u.append_id(vfog);
3130 uniforms.push_back(u);
3131 }
3132 {
3133 RD::Uniform u;
3134 u.binding = 20;
3135 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3136 RID ssil = rb.is_valid() && rb->has_texture(RB_SCOPE_SSIL, RB_FINAL) ? rb->get_texture(RB_SCOPE_SSIL, RB_FINAL) : RID();
3137 RID texture = ssil.is_valid() ? ssil : texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK);
3138 u.append_id(texture);
3139 uniforms.push_back(u);
3140 }
3141
3142 return UniformSetCacheRD::get_singleton()->get_cache_vec(scene_shader.default_shader_rd, RENDER_PASS_UNIFORM_SET, uniforms);
3143}
3144
3145RID RenderForwardClustered::_setup_sdfgi_render_pass_uniform_set(RID p_albedo_texture, RID p_emission_texture, RID p_emission_aniso_texture, RID p_geom_facing_texture) {
3146 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
3147 Vector<RD::Uniform> uniforms;
3148
3149 {
3150 RD::Uniform u;
3151 u.binding = 0;
3152 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
3153 u.append_id(scene_state.uniform_buffers[0]);
3154 uniforms.push_back(u);
3155 }
3156 {
3157 RD::Uniform u;
3158 u.binding = 1;
3159 u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
3160 u.append_id(scene_state.implementation_uniform_buffers[0]);
3161 uniforms.push_back(u);
3162 }
3163 {
3164 RD::Uniform u;
3165 u.binding = 2;
3166 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
3167 RID instance_buffer = scene_state.instance_buffer[RENDER_LIST_SECONDARY];
3168 if (instance_buffer == RID()) {
3169 instance_buffer = scene_shader.default_vec4_xform_buffer; // any buffer will do since its not used
3170 }
3171 u.append_id(instance_buffer);
3172 uniforms.push_back(u);
3173 }
3174 {
3175 // No radiance texture.
3176 RID radiance_texture = texture_storage->texture_rd_get_default(is_using_radiance_cubemap_array() ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK);
3177 RD::Uniform u;
3178 u.binding = 3;
3179 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3180 u.append_id(radiance_texture);
3181 uniforms.push_back(u);
3182 }
3183
3184 {
3185 // No reflection atlas.
3186 RID ref_texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK);
3187 RD::Uniform u;
3188 u.binding = 4;
3189 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3190 u.append_id(ref_texture);
3191 uniforms.push_back(u);
3192 }
3193
3194 {
3195 // No shadow atlas.
3196 RD::Uniform u;
3197 u.binding = 5;
3198 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3199 RID texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH);
3200 u.append_id(texture);
3201 uniforms.push_back(u);
3202 }
3203
3204 {
3205 // No directional shadow atlas.
3206 RD::Uniform u;
3207 u.binding = 6;
3208 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3209 RID texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH);
3210 u.append_id(texture);
3211 uniforms.push_back(u);
3212 }
3213
3214 {
3215 // No Lightmaps
3216 RD::Uniform u;
3217 u.binding = 7;
3218 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3219
3220 RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
3221 for (uint32_t i = 0; i < scene_state.max_lightmaps; i++) {
3222 u.append_id(default_tex);
3223 }
3224
3225 uniforms.push_back(u);
3226 }
3227
3228 {
3229 // No VoxelGIs
3230 RD::Uniform u;
3231 u.binding = 8;
3232 u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3233
3234 RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
3235 for (int i = 0; i < MAX_VOXEL_GI_INSTANCESS; i++) {
3236 u.append_id(default_tex);
3237 }
3238
3239 uniforms.push_back(u);
3240 }
3241
3242 {
3243 RD::Uniform u;
3244 u.binding = 9;
3245 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
3246 RID cb = scene_shader.default_vec4_xform_buffer;
3247 u.append_id(cb);
3248 uniforms.push_back(u);
3249 }
3250
3251 // actual sdfgi stuff
3252
3253 {
3254 RD::Uniform u;
3255 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
3256 u.binding = 10;
3257 u.append_id(p_albedo_texture);
3258 uniforms.push_back(u);
3259 }
3260 {
3261 RD::Uniform u;
3262 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
3263 u.binding = 11;
3264 u.append_id(p_emission_texture);
3265 uniforms.push_back(u);
3266 }
3267 {
3268 RD::Uniform u;
3269 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
3270 u.binding = 12;
3271 u.append_id(p_emission_aniso_texture);
3272 uniforms.push_back(u);
3273 }
3274 {
3275 RD::Uniform u;
3276 u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
3277 u.binding = 13;
3278 u.append_id(p_geom_facing_texture);
3279 uniforms.push_back(u);
3280 }
3281
3282 return UniformSetCacheRD::get_singleton()->get_cache_vec(scene_shader.default_shader_sdfgi_rd, RENDER_PASS_UNIFORM_SET, uniforms);
3283}
3284
3285RID RenderForwardClustered::_render_buffers_get_normal_texture(Ref<RenderSceneBuffersRD> p_render_buffers) {
3286 Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED);
3287
3288 return p_render_buffers->get_msaa_3d() == RS::VIEWPORT_MSAA_DISABLED ? rb_data->get_normal_roughness() : rb_data->get_normal_roughness_msaa();
3289}
3290
3291RID RenderForwardClustered::_render_buffers_get_velocity_texture(Ref<RenderSceneBuffersRD> p_render_buffers) {
3292 return p_render_buffers->get_velocity_buffer(p_render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED);
3293}
3294
3295void RenderForwardClustered::environment_set_ssao_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) {
3296 ss_effects->ssao_set_quality(p_quality, p_half_size, p_adaptive_target, p_blur_passes, p_fadeout_from, p_fadeout_to);
3297}
3298
3299void RenderForwardClustered::environment_set_ssil_quality(RS::EnvironmentSSILQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) {
3300 ss_effects->ssil_set_quality(p_quality, p_half_size, p_adaptive_target, p_blur_passes, p_fadeout_from, p_fadeout_to);
3301}
3302
3303void RenderForwardClustered::environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) {
3304 ss_effects->ssr_set_roughness_quality(p_quality);
3305}
3306
3307void RenderForwardClustered::sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) {
3308 ss_effects->sss_set_quality(p_quality);
3309}
3310
3311void RenderForwardClustered::sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) {
3312 ss_effects->sss_set_scale(p_scale, p_depth_scale);
3313}
3314
3315RenderForwardClustered *RenderForwardClustered::singleton = nullptr;
3316
3317void RenderForwardClustered::sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) {
3318 Ref<RenderSceneBuffersRD> rb = p_render_buffers;
3319 ERR_FAIL_COND(rb.is_null());
3320 Ref<RendererRD::GI::SDFGI> sdfgi;
3321 if (rb->has_custom_data(RB_SCOPE_SDFGI)) {
3322 sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
3323 }
3324
3325 bool needs_sdfgi = p_environment.is_valid() && environment_get_sdfgi_enabled(p_environment);
3326
3327 if (!needs_sdfgi) {
3328 if (sdfgi.is_valid()) {
3329 // delete it
3330 sdfgi.unref();
3331 rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
3332 }
3333 return;
3334 }
3335
3336 // Ensure advanced shaders are available if SDFGI is used.
3337 // Call here as this is the first entry point for SDFGI.
3338 scene_shader.enable_advanced_shader_group();
3339
3340 static const uint32_t history_frames_to_converge[RS::ENV_SDFGI_CONVERGE_MAX] = { 5, 10, 15, 20, 25, 30 };
3341 uint32_t requested_history_size = history_frames_to_converge[gi.sdfgi_frames_to_converge];
3342
3343 if (sdfgi.is_valid() && (sdfgi->num_cascades != environment_get_sdfgi_cascades(p_environment) || sdfgi->min_cell_size != environment_get_sdfgi_min_cell_size(p_environment) || requested_history_size != sdfgi->history_size || sdfgi->uses_occlusion != environment_get_sdfgi_use_occlusion(p_environment) || sdfgi->y_scale_mode != environment_get_sdfgi_y_scale(p_environment))) {
3344 //configuration changed, erase
3345 sdfgi.unref();
3346 rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
3347 }
3348
3349 if (sdfgi.is_null()) {
3350 // re-create
3351 sdfgi = gi.create_sdfgi(p_environment, p_world_position, requested_history_size);
3352 rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
3353 } else {
3354 //check for updates
3355 sdfgi->update(p_environment, p_world_position);
3356 }
3357}
3358
3359int RenderForwardClustered::sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const {
3360 Ref<RenderSceneBuffersRD> rb = p_render_buffers;
3361 ERR_FAIL_COND_V(rb.is_null(), 0);
3362
3363 if (!rb->has_custom_data(RB_SCOPE_SDFGI)) {
3364 return 0;
3365 }
3366 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
3367
3368 int dirty_count = 0;
3369 for (const RendererRD::GI::SDFGI::Cascade &c : sdfgi->cascades) {
3370 if (c.dirty_regions == RendererRD::GI::SDFGI::Cascade::DIRTY_ALL) {
3371 dirty_count++;
3372 } else {
3373 for (int j = 0; j < 3; j++) {
3374 if (c.dirty_regions[j] != 0) {
3375 dirty_count++;
3376 }
3377 }
3378 }
3379 }
3380
3381 return dirty_count;
3382}
3383
3384AABB RenderForwardClustered::sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const {
3385 AABB bounds;
3386 Vector3i from;
3387 Vector3i size;
3388
3389 Ref<RenderSceneBuffersRD> rb = p_render_buffers;
3390 ERR_FAIL_COND_V(rb.is_null(), AABB());
3391 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
3392 ERR_FAIL_COND_V(sdfgi.is_null(), AABB());
3393
3394 int c = sdfgi->get_pending_region_data(p_region, from, size, bounds);
3395 ERR_FAIL_COND_V(c == -1, AABB());
3396 return bounds;
3397}
3398
3399uint32_t RenderForwardClustered::sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const {
3400 AABB bounds;
3401 Vector3i from;
3402 Vector3i size;
3403
3404 Ref<RenderSceneBuffersRD> rb = p_render_buffers;
3405 ERR_FAIL_COND_V(rb.is_null(), -1);
3406 Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
3407 ERR_FAIL_COND_V(sdfgi.is_null(), -1);
3408
3409 return sdfgi->get_pending_region_data(p_region, from, size, bounds);
3410}
3411
3412void RenderForwardClustered::GeometryInstanceForwardClustered::_mark_dirty() {
3413 if (dirty_list_element.in_list()) {
3414 return;
3415 }
3416
3417 //clear surface caches
3418 GeometryInstanceSurfaceDataCache *surf = surface_caches;
3419
3420 while (surf) {
3421 GeometryInstanceSurfaceDataCache *next = surf->next;
3422 RenderForwardClustered::get_singleton()->geometry_instance_surface_alloc.free(surf);
3423 surf = next;
3424 }
3425
3426 surface_caches = nullptr;
3427
3428 RenderForwardClustered::get_singleton()->geometry_instance_dirty_list.add(&dirty_list_element);
3429}
3430
3431void RenderForwardClustered::_geometry_instance_add_surface_with_material(GeometryInstanceForwardClustered *ginstance, uint32_t p_surface, SceneShaderForwardClustered::MaterialData *p_material, uint32_t p_material_id, uint32_t p_shader_id, RID p_mesh) {
3432 RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton();
3433
3434 bool has_read_screen_alpha = p_material->shader_data->uses_screen_texture || p_material->shader_data->uses_depth_texture || p_material->shader_data->uses_normal_texture;
3435 bool has_base_alpha = (p_material->shader_data->uses_alpha && (!p_material->shader_data->uses_alpha_clip || p_material->shader_data->uses_alpha_antialiasing)) || has_read_screen_alpha;
3436 bool has_blend_alpha = p_material->shader_data->uses_blend_alpha;
3437 bool has_alpha = has_base_alpha || has_blend_alpha;
3438
3439 uint32_t flags = 0;
3440
3441 if (p_material->shader_data->uses_sss) {
3442 flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SUBSURFACE_SCATTERING;
3443 }
3444
3445 if (p_material->shader_data->uses_screen_texture) {
3446 flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SCREEN_TEXTURE;
3447 }
3448
3449 if (p_material->shader_data->uses_depth_texture) {
3450 flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_DEPTH_TEXTURE;
3451 }
3452
3453 if (p_material->shader_data->uses_normal_texture) {
3454 flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_NORMAL_TEXTURE;
3455 }
3456
3457 if (ginstance->data->cast_double_sided_shadows) {
3458 flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_DOUBLE_SIDED_SHADOWS;
3459 }
3460
3461 if (has_alpha || has_read_screen_alpha || p_material->shader_data->depth_draw == SceneShaderForwardClustered::ShaderData::DEPTH_DRAW_DISABLED || p_material->shader_data->depth_test == SceneShaderForwardClustered::ShaderData::DEPTH_TEST_DISABLED) {
3462 //material is only meant for alpha pass
3463 flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA;
3464 if ((p_material->shader_data->uses_depth_prepass_alpha || p_material->shader_data->uses_alpha_antialiasing) && !(p_material->shader_data->depth_draw == SceneShaderForwardClustered::ShaderData::DEPTH_DRAW_DISABLED || p_material->shader_data->depth_test == SceneShaderForwardClustered::ShaderData::DEPTH_TEST_DISABLED)) {
3465 flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH;
3466 flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW;
3467 }
3468 } else {
3469 flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE;
3470 flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH;
3471 flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW;
3472 }
3473
3474 if (p_material->shader_data->uses_particle_trails) {
3475 flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_PARTICLE_TRAILS;
3476 }
3477
3478 SceneShaderForwardClustered::MaterialData *material_shadow = nullptr;
3479 void *surface_shadow = nullptr;
3480 if (!p_material->shader_data->uses_particle_trails && !p_material->shader_data->writes_modelview_or_projection && !p_material->shader_data->uses_vertex && !p_material->shader_data->uses_position && !p_material->shader_data->uses_discard && !p_material->shader_data->uses_depth_prepass_alpha && !p_material->shader_data->uses_alpha_clip && !p_material->shader_data->uses_alpha_antialiasing && p_material->shader_data->cull_mode == SceneShaderForwardClustered::ShaderData::CULL_BACK && !p_material->shader_data->uses_point_size) {
3481 flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SHARED_SHADOW_MATERIAL;
3482 material_shadow = static_cast<SceneShaderForwardClustered::MaterialData *>(RendererRD::MaterialStorage::get_singleton()->material_get_data(scene_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D));
3483
3484 RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh);
3485
3486 if (shadow_mesh.is_valid()) {
3487 surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, p_surface);
3488 }
3489
3490 } else {
3491 material_shadow = p_material;
3492 }
3493
3494 GeometryInstanceSurfaceDataCache *sdcache = geometry_instance_surface_alloc.alloc();
3495
3496 sdcache->flags = flags;
3497
3498 sdcache->shader = p_material->shader_data;
3499 sdcache->material = p_material;
3500 sdcache->material_uniform_set = p_material->uniform_set;
3501 sdcache->surface = mesh_storage->mesh_get_surface(p_mesh, p_surface);
3502 sdcache->primitive = mesh_storage->mesh_surface_get_primitive(sdcache->surface);
3503 sdcache->surface_index = p_surface;
3504
3505 if (ginstance->data->dirty_dependencies) {
3506 RSG::utilities->base_update_dependency(p_mesh, &ginstance->data->dependency_tracker);
3507 }
3508
3509 //shadow
3510 sdcache->shader_shadow = material_shadow->shader_data;
3511 sdcache->material_uniform_set_shadow = material_shadow->uniform_set;
3512
3513 sdcache->surface_shadow = surface_shadow ? surface_shadow : sdcache->surface;
3514
3515 sdcache->owner = ginstance;
3516
3517 sdcache->next = ginstance->surface_caches;
3518 ginstance->surface_caches = sdcache;
3519
3520 //sortkey
3521
3522 sdcache->sort.sort_key1 = 0;
3523 sdcache->sort.sort_key2 = 0;
3524
3525 sdcache->sort.surface_index = p_surface;
3526 sdcache->sort.material_id_low = p_material_id & 0xFFFF;
3527 sdcache->sort.material_id_hi = p_material_id >> 16;
3528 sdcache->sort.shader_id = p_shader_id;
3529 sdcache->sort.geometry_id = p_mesh.get_local_index(); //only meshes can repeat anyway
3530 sdcache->sort.uses_forward_gi = ginstance->can_sdfgi;
3531 sdcache->sort.priority = p_material->priority;
3532 sdcache->sort.uses_projector = ginstance->using_projectors;
3533 sdcache->sort.uses_softshadow = ginstance->using_softshadows;
3534}
3535
3536void RenderForwardClustered::_geometry_instance_add_surface_with_material_chain(GeometryInstanceForwardClustered *ginstance, uint32_t p_surface, SceneShaderForwardClustered::MaterialData *p_material, RID p_mat_src, RID p_mesh) {
3537 SceneShaderForwardClustered::MaterialData *material = p_material;
3538 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
3539
3540 _geometry_instance_add_surface_with_material(ginstance, p_surface, material, p_mat_src.get_local_index(), material_storage->material_get_shader_id(p_mat_src), p_mesh);
3541
3542 while (material->next_pass.is_valid()) {
3543 RID next_pass = material->next_pass;
3544 material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(next_pass, RendererRD::MaterialStorage::SHADER_TYPE_3D));
3545 if (!material || !material->shader_data->valid) {
3546 break;
3547 }
3548 if (ginstance->data->dirty_dependencies) {
3549 material_storage->material_update_dependency(next_pass, &ginstance->data->dependency_tracker);
3550 }
3551 _geometry_instance_add_surface_with_material(ginstance, p_surface, material, next_pass.get_local_index(), material_storage->material_get_shader_id(next_pass), p_mesh);
3552 }
3553}
3554
3555void RenderForwardClustered::_geometry_instance_add_surface(GeometryInstanceForwardClustered *ginstance, uint32_t p_surface, RID p_material, RID p_mesh) {
3556 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
3557 RID m_src;
3558
3559 m_src = ginstance->data->material_override.is_valid() ? ginstance->data->material_override : p_material;
3560
3561 SceneShaderForwardClustered::MaterialData *material = nullptr;
3562
3563 if (m_src.is_valid()) {
3564 material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(m_src, RendererRD::MaterialStorage::SHADER_TYPE_3D));
3565 if (!material || !material->shader_data->valid) {
3566 material = nullptr;
3567 }
3568 }
3569
3570 if (material) {
3571 if (ginstance->data->dirty_dependencies) {
3572 material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker);
3573 }
3574 } else {
3575 material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(scene_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D));
3576 m_src = scene_shader.default_material;
3577 }
3578
3579 ERR_FAIL_COND(!material);
3580
3581 _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material, m_src, p_mesh);
3582
3583 if (ginstance->data->material_overlay.is_valid()) {
3584 m_src = ginstance->data->material_overlay;
3585
3586 material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(m_src, RendererRD::MaterialStorage::SHADER_TYPE_3D));
3587 if (material && material->shader_data->valid) {
3588 if (ginstance->data->dirty_dependencies) {
3589 material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker);
3590 }
3591
3592 _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material, m_src, p_mesh);
3593 }
3594 }
3595}
3596
3597void RenderForwardClustered::_geometry_instance_update(RenderGeometryInstance *p_geometry_instance) {
3598 RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton();
3599 RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton();
3600 GeometryInstanceForwardClustered *ginstance = static_cast<GeometryInstanceForwardClustered *>(p_geometry_instance);
3601
3602 if (ginstance->data->dirty_dependencies) {
3603 ginstance->data->dependency_tracker.update_begin();
3604 }
3605
3606 //add geometry for drawing
3607 switch (ginstance->data->base_type) {
3608 case RS::INSTANCE_MESH: {
3609 const RID *materials = nullptr;
3610 uint32_t surface_count;
3611 RID mesh = ginstance->data->base;
3612
3613 materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
3614 if (materials) {
3615 //if no materials, no surfaces.
3616 const RID *inst_materials = ginstance->data->surface_materials.ptr();
3617 uint32_t surf_mat_count = ginstance->data->surface_materials.size();
3618
3619 for (uint32_t j = 0; j < surface_count; j++) {
3620 RID material = (j < surf_mat_count && inst_materials[j].is_valid()) ? inst_materials[j] : materials[j];
3621 _geometry_instance_add_surface(ginstance, j, material, mesh);
3622 }
3623 }
3624
3625 ginstance->instance_count = 1;
3626
3627 } break;
3628
3629 case RS::INSTANCE_MULTIMESH: {
3630 RID mesh = mesh_storage->multimesh_get_mesh(ginstance->data->base);
3631 if (mesh.is_valid()) {
3632 const RID *materials = nullptr;
3633 uint32_t surface_count;
3634
3635 materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
3636 if (materials) {
3637 for (uint32_t j = 0; j < surface_count; j++) {
3638 _geometry_instance_add_surface(ginstance, j, materials[j], mesh);
3639 }
3640 }
3641
3642 ginstance->instance_count = mesh_storage->multimesh_get_instances_to_draw(ginstance->data->base);
3643 }
3644
3645 } break;
3646#if 0
3647 case RS::INSTANCE_IMMEDIATE: {
3648 RasterizerStorageGLES3::Immediate *immediate = storage->immediate_owner.get_or_null(inst->base);
3649 ERR_CONTINUE(!immediate);
3650
3651 _add_geometry(immediate, inst, nullptr, -1, p_depth_pass, p_shadow_pass);
3652
3653 } break;
3654#endif
3655 case RS::INSTANCE_PARTICLES: {
3656 int draw_passes = particles_storage->particles_get_draw_passes(ginstance->data->base);
3657
3658 for (int j = 0; j < draw_passes; j++) {
3659 RID mesh = particles_storage->particles_get_draw_pass_mesh(ginstance->data->base, j);
3660 if (!mesh.is_valid()) {
3661 continue;
3662 }
3663
3664 const RID *materials = nullptr;
3665 uint32_t surface_count;
3666
3667 materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
3668 if (materials) {
3669 for (uint32_t k = 0; k < surface_count; k++) {
3670 _geometry_instance_add_surface(ginstance, k, materials[k], mesh);
3671 }
3672 }
3673 }
3674
3675 ginstance->instance_count = particles_storage->particles_get_amount(ginstance->data->base, ginstance->trail_steps);
3676
3677 } break;
3678
3679 default: {
3680 }
3681 }
3682
3683 //Fill push constant
3684
3685 ginstance->base_flags = 0;
3686
3687 bool store_transform = true;
3688
3689 if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) {
3690 ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH;
3691 if (mesh_storage->multimesh_get_transform_format(ginstance->data->base) == RS::MULTIMESH_TRANSFORM_2D) {
3692 ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D;
3693 }
3694 if (mesh_storage->multimesh_uses_colors(ginstance->data->base)) {
3695 ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR;
3696 }
3697 if (mesh_storage->multimesh_uses_custom_data(ginstance->data->base)) {
3698 ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA;
3699 }
3700
3701 ginstance->transforms_uniform_set = mesh_storage->multimesh_get_3d_uniform_set(ginstance->data->base, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET);
3702
3703 } else if (ginstance->data->base_type == RS::INSTANCE_PARTICLES) {
3704 ginstance->base_flags |= INSTANCE_DATA_FLAG_PARTICLES;
3705 ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH;
3706
3707 ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR;
3708 ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA;
3709
3710 //for particles, stride is the trail size
3711 ginstance->base_flags |= (ginstance->trail_steps << INSTANCE_DATA_FLAGS_PARTICLE_TRAIL_SHIFT);
3712
3713 if (!particles_storage->particles_is_using_local_coords(ginstance->data->base)) {
3714 store_transform = false;
3715 }
3716 ginstance->transforms_uniform_set = particles_storage->particles_get_instance_buffer_uniform_set(ginstance->data->base, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET);
3717
3718 if (particles_storage->particles_get_frame_counter(ginstance->data->base) == 0) {
3719 // Particles haven't been cleared or updated, update once now to ensure they are ready to render.
3720 particles_storage->update_particles();
3721 }
3722
3723 if (ginstance->data->dirty_dependencies) {
3724 particles_storage->particles_update_dependency(ginstance->data->base, &ginstance->data->dependency_tracker);
3725 }
3726 } else if (ginstance->data->base_type == RS::INSTANCE_MESH) {
3727 if (mesh_storage->skeleton_is_valid(ginstance->data->skeleton)) {
3728 ginstance->transforms_uniform_set = mesh_storage->skeleton_get_3d_uniform_set(ginstance->data->skeleton, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET);
3729 if (ginstance->data->dirty_dependencies) {
3730 mesh_storage->skeleton_update_dependency(ginstance->data->skeleton, &ginstance->data->dependency_tracker);
3731 }
3732 } else {
3733 ginstance->transforms_uniform_set = RID();
3734 }
3735 }
3736
3737 ginstance->store_transform_cache = store_transform;
3738 ginstance->can_sdfgi = false;
3739
3740 if (!RendererRD::LightStorage::get_singleton()->lightmap_instance_is_valid(ginstance->lightmap_instance)) {
3741 if (ginstance->voxel_gi_instances[0].is_null() && (ginstance->data->use_baked_light || ginstance->data->use_dynamic_gi)) {
3742 ginstance->can_sdfgi = true;
3743 }
3744 }
3745
3746 if (ginstance->data->dirty_dependencies) {
3747 ginstance->data->dependency_tracker.update_end();
3748 ginstance->data->dirty_dependencies = false;
3749 }
3750
3751 ginstance->dirty_list_element.remove_from_list();
3752}
3753
3754void RenderForwardClustered::_update_dirty_geometry_instances() {
3755 while (geometry_instance_dirty_list.first()) {
3756 _geometry_instance_update(geometry_instance_dirty_list.first()->self());
3757 }
3758}
3759
3760void RenderForwardClustered::_geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker) {
3761 switch (p_notification) {
3762 case Dependency::DEPENDENCY_CHANGED_MATERIAL:
3763 case Dependency::DEPENDENCY_CHANGED_MESH:
3764 case Dependency::DEPENDENCY_CHANGED_PARTICLES:
3765 case Dependency::DEPENDENCY_CHANGED_PARTICLES_INSTANCES:
3766 case Dependency::DEPENDENCY_CHANGED_MULTIMESH:
3767 case Dependency::DEPENDENCY_CHANGED_SKELETON_DATA: {
3768 static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty();
3769 static_cast<GeometryInstanceForwardClustered *>(p_tracker->userdata)->data->dirty_dependencies = true;
3770 } break;
3771 case Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES: {
3772 GeometryInstanceForwardClustered *ginstance = static_cast<GeometryInstanceForwardClustered *>(p_tracker->userdata);
3773 if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) {
3774 ginstance->instance_count = RendererRD::MeshStorage::get_singleton()->multimesh_get_instances_to_draw(ginstance->data->base);
3775 }
3776 } break;
3777 default: {
3778 //rest of notifications of no interest
3779 } break;
3780 }
3781}
3782void RenderForwardClustered::_geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker) {
3783 static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty();
3784 static_cast<GeometryInstanceForwardClustered *>(p_tracker->userdata)->data->dirty_dependencies = true;
3785}
3786
3787RenderGeometryInstance *RenderForwardClustered::geometry_instance_create(RID p_base) {
3788 RS::InstanceType type = RSG::utilities->get_base_type(p_base);
3789 ERR_FAIL_COND_V(!((1 << type) & RS::INSTANCE_GEOMETRY_MASK), nullptr);
3790
3791 GeometryInstanceForwardClustered *ginstance = geometry_instance_alloc.alloc();
3792 ginstance->data = memnew(GeometryInstanceForwardClustered::Data);
3793
3794 ginstance->data->base = p_base;
3795 ginstance->data->base_type = type;
3796 ginstance->data->dependency_tracker.userdata = ginstance;
3797 ginstance->data->dependency_tracker.changed_callback = _geometry_instance_dependency_changed;
3798 ginstance->data->dependency_tracker.deleted_callback = _geometry_instance_dependency_deleted;
3799
3800 ginstance->_mark_dirty();
3801
3802 return ginstance;
3803}
3804
3805void RenderForwardClustered::GeometryInstanceForwardClustered::set_transform(const Transform3D &p_transform, const AABB &p_aabb, const AABB &p_transformed_aabbb) {
3806 uint64_t frame = RSG::rasterizer->get_frame_number();
3807 if (frame != prev_transform_change_frame) {
3808 prev_transform = transform;
3809 prev_transform_change_frame = frame;
3810 prev_transform_dirty = true;
3811 }
3812
3813 RenderGeometryInstanceBase::set_transform(p_transform, p_aabb, p_transformed_aabbb);
3814}
3815
3816void RenderForwardClustered::GeometryInstanceForwardClustered::set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) {
3817 lightmap_instance = p_lightmap_instance;
3818 lightmap_uv_scale = p_lightmap_uv_scale;
3819 lightmap_slice_index = p_lightmap_slice_index;
3820
3821 _mark_dirty();
3822}
3823
3824void RenderForwardClustered::GeometryInstanceForwardClustered::set_lightmap_capture(const Color *p_sh9) {
3825 if (p_sh9) {
3826 if (lightmap_sh == nullptr) {
3827 lightmap_sh = RenderForwardClustered::get_singleton()->geometry_instance_lightmap_sh.alloc();
3828 }
3829
3830 memcpy(lightmap_sh->sh, p_sh9, sizeof(Color) * 9);
3831 } else {
3832 if (lightmap_sh != nullptr) {
3833 RenderForwardClustered::get_singleton()->geometry_instance_lightmap_sh.free(lightmap_sh);
3834 lightmap_sh = nullptr;
3835 }
3836 }
3837 _mark_dirty();
3838}
3839
3840void RenderForwardClustered::geometry_instance_free(RenderGeometryInstance *p_geometry_instance) {
3841 GeometryInstanceForwardClustered *ginstance = static_cast<GeometryInstanceForwardClustered *>(p_geometry_instance);
3842 ERR_FAIL_COND(!ginstance);
3843 if (ginstance->lightmap_sh != nullptr) {
3844 geometry_instance_lightmap_sh.free(ginstance->lightmap_sh);
3845 }
3846 GeometryInstanceSurfaceDataCache *surf = ginstance->surface_caches;
3847 while (surf) {
3848 GeometryInstanceSurfaceDataCache *next = surf->next;
3849 geometry_instance_surface_alloc.free(surf);
3850 surf = next;
3851 }
3852 memdelete(ginstance->data);
3853 geometry_instance_alloc.free(ginstance);
3854}
3855
3856uint32_t RenderForwardClustered::geometry_instance_get_pair_mask() {
3857 return (1 << RS::INSTANCE_VOXEL_GI);
3858}
3859
3860void RenderForwardClustered::GeometryInstanceForwardClustered::pair_voxel_gi_instances(const RID *p_voxel_gi_instances, uint32_t p_voxel_gi_instance_count) {
3861 if (p_voxel_gi_instance_count > 0) {
3862 voxel_gi_instances[0] = p_voxel_gi_instances[0];
3863 } else {
3864 voxel_gi_instances[0] = RID();
3865 }
3866
3867 if (p_voxel_gi_instance_count > 1) {
3868 voxel_gi_instances[1] = p_voxel_gi_instances[1];
3869 } else {
3870 voxel_gi_instances[1] = RID();
3871 }
3872}
3873
3874void RenderForwardClustered::GeometryInstanceForwardClustered::set_softshadow_projector_pairing(bool p_softshadow, bool p_projector) {
3875 using_projectors = p_projector;
3876 using_softshadows = p_softshadow;
3877 _mark_dirty();
3878}
3879
3880void RenderForwardClustered::_update_shader_quality_settings() {
3881 Vector<RD::PipelineSpecializationConstant> spec_constants;
3882
3883 RD::PipelineSpecializationConstant sc;
3884 sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT;
3885
3886 sc.constant_id = SPEC_CONSTANT_SOFT_SHADOW_SAMPLES;
3887 sc.int_value = soft_shadow_samples_get();
3888
3889 spec_constants.push_back(sc);
3890
3891 sc.constant_id = SPEC_CONSTANT_PENUMBRA_SHADOW_SAMPLES;
3892 sc.int_value = penumbra_shadow_samples_get();
3893
3894 spec_constants.push_back(sc);
3895
3896 sc.constant_id = SPEC_CONSTANT_DIRECTIONAL_SOFT_SHADOW_SAMPLES;
3897 sc.int_value = directional_soft_shadow_samples_get();
3898
3899 spec_constants.push_back(sc);
3900
3901 sc.constant_id = SPEC_CONSTANT_DIRECTIONAL_PENUMBRA_SHADOW_SAMPLES;
3902 sc.int_value = directional_penumbra_shadow_samples_get();
3903
3904 spec_constants.push_back(sc);
3905
3906 sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
3907 sc.constant_id = SPEC_CONSTANT_DECAL_FILTER;
3908 sc.bool_value = decals_get_filter() == RS::DECAL_FILTER_NEAREST_MIPMAPS ||
3909 decals_get_filter() == RS::DECAL_FILTER_LINEAR_MIPMAPS ||
3910 decals_get_filter() == RS::DECAL_FILTER_NEAREST_MIPMAPS_ANISOTROPIC ||
3911 decals_get_filter() == RS::DECAL_FILTER_LINEAR_MIPMAPS_ANISOTROPIC;
3912
3913 spec_constants.push_back(sc);
3914
3915 sc.constant_id = SPEC_CONSTANT_PROJECTOR_FILTER;
3916 sc.bool_value = light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS ||
3917 light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS ||
3918 light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS_ANISOTROPIC ||
3919 light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS_ANISOTROPIC;
3920
3921 spec_constants.push_back(sc);
3922
3923 scene_shader.set_default_specialization_constants(spec_constants);
3924
3925 base_uniforms_changed(); //also need this
3926}
3927
3928RenderForwardClustered::RenderForwardClustered() {
3929 singleton = this;
3930
3931 /* SCENE SHADER */
3932
3933 {
3934 String defines;
3935 defines += "\n#define MAX_ROUGHNESS_LOD " + itos(get_roughness_layers() - 1) + ".0\n";
3936 if (is_using_radiance_cubemap_array()) {
3937 defines += "\n#define USE_RADIANCE_CUBEMAP_ARRAY \n";
3938 }
3939 defines += "\n#define SDFGI_OCT_SIZE " + itos(gi.sdfgi_get_lightprobe_octahedron_size()) + "\n";
3940 defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(MAX_DIRECTIONAL_LIGHTS) + "\n";
3941
3942 {
3943 //lightmaps
3944 scene_state.max_lightmaps = MAX_LIGHTMAPS;
3945 defines += "\n#define MAX_LIGHTMAP_TEXTURES " + itos(scene_state.max_lightmaps) + "\n";
3946 defines += "\n#define MAX_LIGHTMAPS " + itos(scene_state.max_lightmaps) + "\n";
3947
3948 scene_state.lightmap_buffer = RD::get_singleton()->storage_buffer_create(sizeof(LightmapData) * scene_state.max_lightmaps);
3949 }
3950 {
3951 //captures
3952 scene_state.max_lightmap_captures = 2048;
3953 scene_state.lightmap_captures = memnew_arr(LightmapCaptureData, scene_state.max_lightmap_captures);
3954 scene_state.lightmap_capture_buffer = RD::get_singleton()->storage_buffer_create(sizeof(LightmapCaptureData) * scene_state.max_lightmap_captures);
3955 }
3956 {
3957 defines += "\n#define MATERIAL_UNIFORM_SET " + itos(MATERIAL_UNIFORM_SET) + "\n";
3958 defines += "\n#define SAMPLERS_BINDING_FIRST_INDEX " + itos(SAMPLERS_BINDING_FIRST_INDEX) + "\n";
3959 }
3960#ifdef REAL_T_IS_DOUBLE
3961 {
3962 defines += "\n#define USE_DOUBLE_PRECISION \n";
3963 }
3964#endif
3965
3966 scene_shader.init(defines);
3967 }
3968
3969 /* shadow sampler */
3970 {
3971 RD::SamplerState sampler;
3972 sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST;
3973 sampler.min_filter = RD::SAMPLER_FILTER_NEAREST;
3974 sampler.enable_compare = true;
3975 sampler.compare_op = RD::COMPARE_OP_LESS;
3976 shadow_sampler = RD::get_singleton()->sampler_create(sampler);
3977 }
3978
3979 render_list_thread_threshold = GLOBAL_GET("rendering/limits/forward_renderer/threaded_render_minimum_instances");
3980
3981 _update_shader_quality_settings();
3982
3983 resolve_effects = memnew(RendererRD::Resolve());
3984 taa = memnew(RendererRD::TAA);
3985 ss_effects = memnew(RendererRD::SSEffects);
3986}
3987
3988RenderForwardClustered::~RenderForwardClustered() {
3989 if (ss_effects != nullptr) {
3990 memdelete(ss_effects);
3991 ss_effects = nullptr;
3992 }
3993
3994 if (taa != nullptr) {
3995 memdelete(taa);
3996 taa = nullptr;
3997 }
3998
3999 if (resolve_effects != nullptr) {
4000 memdelete(resolve_effects);
4001 resolve_effects = nullptr;
4002 }
4003
4004 RD::get_singleton()->free(shadow_sampler);
4005 RSG::light_storage->directional_shadow_atlas_set_size(0);
4006
4007 {
4008 for (const RID &rid : scene_state.uniform_buffers) {
4009 RD::get_singleton()->free(rid);
4010 }
4011 for (const RID &rid : scene_state.implementation_uniform_buffers) {
4012 RD::get_singleton()->free(rid);
4013 }
4014 RD::get_singleton()->free(scene_state.lightmap_buffer);
4015 RD::get_singleton()->free(scene_state.lightmap_capture_buffer);
4016 for (uint32_t i = 0; i < RENDER_LIST_MAX; i++) {
4017 if (scene_state.instance_buffer[i] != RID()) {
4018 RD::get_singleton()->free(scene_state.instance_buffer[i]);
4019 }
4020 }
4021 memdelete_arr(scene_state.lightmap_captures);
4022 }
4023
4024 while (sdfgi_framebuffer_size_cache.begin()) {
4025 RD::get_singleton()->free(sdfgi_framebuffer_size_cache.begin()->value);
4026 sdfgi_framebuffer_size_cache.remove(sdfgi_framebuffer_size_cache.begin());
4027 }
4028}
4029