| 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 "servers/rendering/renderer_rd/environment/fog.h" |
| 34 | #include "servers/rendering/renderer_rd/framebuffer_cache_rd.h" |
| 35 | #include "servers/rendering/renderer_rd/storage_rd/light_storage.h" |
| 36 | #include "servers/rendering/renderer_rd/storage_rd/mesh_storage.h" |
| 37 | #include "servers/rendering/renderer_rd/storage_rd/particles_storage.h" |
| 38 | #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h" |
| 39 | #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h" |
| 40 | #include "servers/rendering/rendering_device.h" |
| 41 | #include "servers/rendering/rendering_server_default.h" |
| 42 | |
| 43 | using namespace RendererSceneRenderImplementation; |
| 44 | |
| 45 | #define PRELOAD_PIPELINES_ON_SURFACE_CACHE_CONSTRUCTION 1 |
| 46 | |
| 47 | #define FADE_ALPHA_PASS_THRESHOLD 0.999 |
| 48 | |
| 49 | void RenderForwardClustered::RenderBufferDataForwardClustered::ensure_specular() { |
| 50 | ERR_FAIL_NULL(render_buffers); |
| 51 | |
| 52 | if (!render_buffers->has_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_SPECULAR)) { |
| 53 | bool msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED; |
| 54 | render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_SPECULAR, get_specular_format(), get_specular_usage_bits(msaa, false, render_buffers->get_can_be_storage())); |
| 55 | if (msaa) { |
| 56 | render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_SPECULAR_MSAA, get_specular_format(), get_specular_usage_bits(false, msaa, render_buffers->get_can_be_storage()), render_buffers->get_texture_samples()); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void RenderForwardClustered::RenderBufferDataForwardClustered::ensure_normal_roughness_texture() { |
| 62 | ERR_FAIL_NULL(render_buffers); |
| 63 | |
| 64 | if (!render_buffers->has_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_NORMAL_ROUGHNESS)) { |
| 65 | bool msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED; |
| 66 | render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_NORMAL_ROUGHNESS, get_normal_roughness_format(), get_normal_roughness_usage_bits(msaa, false, render_buffers->get_can_be_storage())); |
| 67 | if (msaa) { |
| 68 | render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_NORMAL_ROUGHNESS_MSAA, get_normal_roughness_format(), get_normal_roughness_usage_bits(false, msaa, render_buffers->get_can_be_storage()), render_buffers->get_texture_samples()); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void RenderForwardClustered::RenderBufferDataForwardClustered::ensure_voxelgi() { |
| 74 | ERR_FAIL_NULL(render_buffers); |
| 75 | |
| 76 | if (!render_buffers->has_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_VOXEL_GI)) { |
| 77 | bool msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED; |
| 78 | render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_VOXEL_GI, get_voxelgi_format(), get_voxelgi_usage_bits(msaa, false, render_buffers->get_can_be_storage())); |
| 79 | if (msaa) { |
| 80 | render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, RB_TEX_VOXEL_GI_MSAA, get_voxelgi_format(), get_voxelgi_usage_bits(false, msaa, render_buffers->get_can_be_storage()), render_buffers->get_texture_samples()); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void RenderForwardClustered::RenderBufferDataForwardClustered::ensure_fsr2(RendererRD::FSR2Effect *p_effect) { |
| 86 | if (fsr2_context == nullptr) { |
| 87 | fsr2_context = p_effect->create_context(render_buffers->get_internal_size(), render_buffers->get_target_size()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | #ifdef METAL_MFXTEMPORAL_ENABLED |
| 92 | bool RenderForwardClustered::RenderBufferDataForwardClustered::ensure_mfx_temporal(RendererRD::MFXTemporalEffect *p_effect) { |
| 93 | if (mfx_temporal_context == nullptr) { |
| 94 | RendererRD::MFXTemporalEffect::CreateParams params; |
| 95 | params.input_size = render_buffers->get_internal_size(); |
| 96 | params.output_size = render_buffers->get_target_size(); |
| 97 | params.input_format = render_buffers->get_base_data_format(); |
| 98 | params.depth_format = render_buffers->get_depth_format(false, false, render_buffers->get_can_be_storage()); |
| 99 | params.motion_format = render_buffers->get_velocity_format(); |
| 100 | params.reactive_format = render_buffers->get_base_data_format(); // Reactive is derived from input. |
| 101 | params.output_format = render_buffers->get_base_data_format(); |
| 102 | params.motion_vector_scale = render_buffers->get_internal_size(); |
| 103 | mfx_temporal_context = p_effect->create_context(params); |
| 104 | return true; |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | void RenderForwardClustered::RenderBufferDataForwardClustered::free_data() { |
| 111 | // JIC, should already have been cleared |
| 112 | if (render_buffers) { |
| 113 | render_buffers->clear_context(RB_SCOPE_FORWARD_CLUSTERED); |
| 114 | render_buffers->clear_context(RB_SCOPE_SSDS); |
| 115 | render_buffers->clear_context(RB_SCOPE_SSIL); |
| 116 | render_buffers->clear_context(RB_SCOPE_SSAO); |
| 117 | render_buffers->clear_context(RB_SCOPE_SSR); |
| 118 | } |
| 119 | |
| 120 | if (cluster_builder) { |
| 121 | memdelete(cluster_builder); |
| 122 | cluster_builder = nullptr; |
| 123 | } |
| 124 | |
| 125 | if (fsr2_context) { |
| 126 | memdelete(fsr2_context); |
| 127 | fsr2_context = nullptr; |
| 128 | } |
| 129 | |
| 130 | #ifdef METAL_MFXTEMPORAL_ENABLED |
| 131 | if (mfx_temporal_context) { |
| 132 | memdelete(mfx_temporal_context); |
| 133 | mfx_temporal_context = nullptr; |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | if (!render_sdfgi_uniform_set.is_null() && RD::get_singleton()->uniform_set_is_valid(render_sdfgi_uniform_set)) { |
| 138 | RD::get_singleton()->free_rid(render_sdfgi_uniform_set); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void RenderForwardClustered::RenderBufferDataForwardClustered::configure(RenderSceneBuffersRD *p_render_buffers) { |
| 143 | if (render_buffers) { |
| 144 | // JIC |
| 145 | free_data(); |
| 146 | } |
| 147 | |
| 148 | render_buffers = p_render_buffers; |
| 149 | ERR_FAIL_NULL(render_buffers); |
| 150 | |
| 151 | if (cluster_builder == nullptr) { |
| 152 | cluster_builder = memnew(ClusterBuilderRD); |
| 153 | } |
| 154 | cluster_builder->set_shared(RenderForwardClustered::get_singleton()->get_cluster_builder_shared()); |
| 155 | |
| 156 | RID sampler = RendererRD::MaterialStorage::get_singleton()->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 157 | 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()); |
| 158 | } |
| 159 | |
| 160 | RID RenderForwardClustered::RenderBufferDataForwardClustered::get_color_only_fb() { |
| 161 | ERR_FAIL_NULL_V(render_buffers, RID()); |
| 162 | |
| 163 | bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED; |
| 164 | |
| 165 | RID color = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA) : render_buffers->get_internal_texture(); |
| 166 | RID depth = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA) : render_buffers->get_depth_texture(); |
| 167 | |
| 168 | if (render_buffers->has_texture(RB_SCOPE_VRS, RB_TEXTURE)) { |
| 169 | RID vrs_texture = render_buffers->get_texture(RB_SCOPE_VRS, RB_TEXTURE); |
| 170 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), color, depth, vrs_texture); |
| 171 | } else { |
| 172 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), color, depth); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | RID RenderForwardClustered::RenderBufferDataForwardClustered::get_color_pass_fb(uint32_t p_color_pass_flags) { |
| 177 | ERR_FAIL_NULL_V(render_buffers, RID()); |
| 178 | bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED; |
| 179 | |
| 180 | int v_count = (p_color_pass_flags & COLOR_PASS_FLAG_MULTIVIEW) ? render_buffers->get_view_count() : 1; |
| 181 | RID color = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA) : render_buffers->get_internal_texture(); |
| 182 | |
| 183 | RID specular; |
| 184 | if (p_color_pass_flags & COLOR_PASS_FLAG_SEPARATE_SPECULAR) { |
| 185 | ensure_specular(); |
| 186 | specular = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_SPECULAR_MSAA : RB_TEX_SPECULAR); |
| 187 | } |
| 188 | |
| 189 | RID velocity_buffer; |
| 190 | if (p_color_pass_flags & COLOR_PASS_FLAG_MOTION_VECTORS) { |
| 191 | render_buffers->ensure_velocity(); |
| 192 | velocity_buffer = render_buffers->get_velocity_buffer(use_msaa); |
| 193 | } |
| 194 | |
| 195 | RID depth = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA) : render_buffers->get_depth_texture(); |
| 196 | |
| 197 | if (render_buffers->has_texture(RB_SCOPE_VRS, RB_TEXTURE)) { |
| 198 | RID vrs_texture = render_buffers->get_texture(RB_SCOPE_VRS, RB_TEXTURE); |
| 199 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(v_count, color, specular, velocity_buffer, depth, vrs_texture); |
| 200 | } else { |
| 201 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(v_count, color, specular, velocity_buffer, depth); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | RID RenderForwardClustered::RenderBufferDataForwardClustered::get_depth_fb(DepthFrameBufferType p_type) { |
| 206 | ERR_FAIL_NULL_V(render_buffers, RID()); |
| 207 | bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED; |
| 208 | |
| 209 | RID depth = use_msaa ? render_buffers->get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA) : render_buffers->get_depth_texture(); |
| 210 | |
| 211 | switch (p_type) { |
| 212 | case DEPTH_FB: { |
| 213 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), depth); |
| 214 | } break; |
| 215 | case DEPTH_FB_ROUGHNESS: { |
| 216 | ensure_normal_roughness_texture(); |
| 217 | |
| 218 | RID normal_roughness_buffer = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_NORMAL_ROUGHNESS_MSAA : RB_TEX_NORMAL_ROUGHNESS); |
| 219 | |
| 220 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), depth, normal_roughness_buffer); |
| 221 | } break; |
| 222 | case DEPTH_FB_ROUGHNESS_VOXELGI: { |
| 223 | ensure_normal_roughness_texture(); |
| 224 | ensure_voxelgi(); |
| 225 | |
| 226 | RID normal_roughness_buffer = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_NORMAL_ROUGHNESS_MSAA : RB_TEX_NORMAL_ROUGHNESS); |
| 227 | RID voxelgi_buffer = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_VOXEL_GI_MSAA : RB_TEX_VOXEL_GI); |
| 228 | |
| 229 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), depth, normal_roughness_buffer, voxelgi_buffer); |
| 230 | } break; |
| 231 | default: { |
| 232 | ERR_FAIL_V(RID()); |
| 233 | } break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | RID RenderForwardClustered::RenderBufferDataForwardClustered::get_specular_only_fb() { |
| 238 | bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED; |
| 239 | |
| 240 | RID specular = render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, use_msaa ? RB_TEX_SPECULAR_MSAA : RB_TEX_SPECULAR); |
| 241 | |
| 242 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), specular); |
| 243 | } |
| 244 | |
| 245 | RID RenderForwardClustered::RenderBufferDataForwardClustered::get_velocity_only_fb() { |
| 246 | bool use_msaa = render_buffers->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED; |
| 247 | |
| 248 | RID velocity = render_buffers->get_texture(RB_SCOPE_BUFFERS, use_msaa ? RB_TEX_VELOCITY_MSAA : RB_TEX_VELOCITY); |
| 249 | |
| 250 | return FramebufferCacheRD::get_singleton()->get_cache_multiview(render_buffers->get_view_count(), velocity); |
| 251 | } |
| 252 | |
| 253 | RD::DataFormat RenderForwardClustered::RenderBufferDataForwardClustered::get_specular_format() { |
| 254 | return RD::DATA_FORMAT_R16G16B16A16_SFLOAT; |
| 255 | } |
| 256 | |
| 257 | uint32_t RenderForwardClustered::RenderBufferDataForwardClustered::get_specular_usage_bits(bool p_resolve, bool p_msaa, bool p_storage) { |
| 258 | return RenderSceneBuffersRD::get_color_usage_bits(p_resolve, p_msaa, p_storage); |
| 259 | } |
| 260 | |
| 261 | RD::DataFormat RenderForwardClustered::RenderBufferDataForwardClustered::get_normal_roughness_format() { |
| 262 | return RD::DATA_FORMAT_R8G8B8A8_UNORM; |
| 263 | } |
| 264 | |
| 265 | uint32_t RenderForwardClustered::RenderBufferDataForwardClustered::get_normal_roughness_usage_bits(bool p_resolve, bool p_msaa, bool p_storage) { |
| 266 | return RenderSceneBuffersRD::get_color_usage_bits(p_resolve, p_msaa, p_storage); |
| 267 | } |
| 268 | |
| 269 | RD::DataFormat RenderForwardClustered::RenderBufferDataForwardClustered::get_voxelgi_format() { |
| 270 | return RD::DATA_FORMAT_R8G8_UINT; |
| 271 | } |
| 272 | |
| 273 | uint32_t RenderForwardClustered::RenderBufferDataForwardClustered::get_voxelgi_usage_bits(bool p_resolve, bool p_msaa, bool p_storage) { |
| 274 | return RenderSceneBuffersRD::get_color_usage_bits(p_resolve, p_msaa, p_storage); |
| 275 | } |
| 276 | |
| 277 | void RenderForwardClustered::setup_render_buffer_data(Ref<RenderSceneBuffersRD> p_render_buffers) { |
| 278 | Ref<RenderBufferDataForwardClustered> data; |
| 279 | data.instantiate(); |
| 280 | p_render_buffers->set_custom_data(RB_SCOPE_FORWARD_CLUSTERED, data); |
| 281 | |
| 282 | Ref<RendererRD::GI::RenderBuffersGI> rbgi; |
| 283 | rbgi.instantiate(); |
| 284 | p_render_buffers->set_custom_data(RB_SCOPE_GI, rbgi); |
| 285 | } |
| 286 | |
| 287 | bool RenderForwardClustered::free(RID p_rid) { |
| 288 | if (RendererSceneRenderRD::free(p_rid)) { |
| 289 | return true; |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | void RenderForwardClustered::update() { |
| 295 | RendererSceneRenderRD::update(); |
| 296 | _update_global_pipeline_data_requirements_from_project(); |
| 297 | _update_global_pipeline_data_requirements_from_light_storage(); |
| 298 | } |
| 299 | |
| 300 | /// RENDERING /// |
| 301 | |
| 302 | template <RenderForwardClustered::PassMode p_pass_mode, uint32_t p_color_pass_flags> |
| 303 | void 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) { |
| 304 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 305 | RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton(); |
| 306 | RD::DrawListID draw_list = p_draw_list; |
| 307 | RD::FramebufferFormatID framebuffer_format = p_framebuffer_Format; |
| 308 | |
| 309 | //global scope bindings |
| 310 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, render_base_uniform_set, SCENE_UNIFORM_SET); |
| 311 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_params->render_pass_uniform_set, RENDER_PASS_UNIFORM_SET); |
| 312 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, scene_shader.default_vec4_xform_uniform_set, TRANSFORMS_UNIFORM_SET); |
| 313 | |
| 314 | RID prev_material_uniform_set; |
| 315 | |
| 316 | RID prev_vertex_array_rd; |
| 317 | RID prev_index_array_rd; |
| 318 | RID prev_xforms_uniform_set; |
| 319 | |
| 320 | SceneShaderForwardClustered::ShaderData *shader = nullptr; |
| 321 | SceneShaderForwardClustered::ShaderData *prev_shader = nullptr; |
| 322 | SceneShaderForwardClustered::ShaderData::PipelineKey pipeline_key; |
| 323 | uint32_t pipeline_hash = 0; |
| 324 | uint32_t prev_pipeline_hash = 0; |
| 325 | |
| 326 | bool shadow_pass = (p_pass_mode == PASS_MODE_SHADOW) || (p_pass_mode == PASS_MODE_SHADOW_DP); |
| 327 | |
| 328 | SceneState::PushConstant push_constant; |
| 329 | |
| 330 | if constexpr (p_pass_mode == PASS_MODE_DEPTH_MATERIAL) { |
| 331 | push_constant.uv_offset = Math::make_half_float(p_params->uv_offset.y) << 16; |
| 332 | push_constant.uv_offset |= Math::make_half_float(p_params->uv_offset.x); |
| 333 | } else { |
| 334 | push_constant.uv_offset = 0; |
| 335 | } |
| 336 | |
| 337 | bool should_request_redraw = false; |
| 338 | |
| 339 | for (uint32_t i = p_from_element; i < p_to_element; i++) { |
| 340 | const GeometryInstanceSurfaceDataCache *surf = p_params->elements[i]; |
| 341 | const RenderElementInfo &element_info = p_params->element_info[i]; |
| 342 | |
| 343 | if (p_pass_mode == PASS_MODE_COLOR && surf->color_pass_inclusion_mask && (p_color_pass_flags & surf->color_pass_inclusion_mask) == 0) { |
| 344 | // Some surfaces can be repeated in multiple render lists. We exclude them from being rendered on the color pass based on the |
| 345 | // features supported by the pass compared to the exclusion mask. |
| 346 | continue; |
| 347 | } |
| 348 | |
| 349 | if (surf->owner->instance_count == 0) { |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | push_constant.base_index = i + p_params->element_offset; |
| 354 | |
| 355 | RID material_uniform_set; |
| 356 | void *mesh_surface; |
| 357 | |
| 358 | if (shadow_pass || p_pass_mode == PASS_MODE_DEPTH) { //regular depth pass can use these too |
| 359 | material_uniform_set = surf->material_uniform_set_shadow; |
| 360 | shader = surf->shader_shadow; |
| 361 | mesh_surface = surf->surface_shadow; |
| 362 | |
| 363 | } else { |
| 364 | #ifdef DEBUG_ENABLED |
| 365 | if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_LIGHTING)) { |
| 366 | material_uniform_set = scene_shader.default_material_uniform_set; |
| 367 | shader = scene_shader.default_material_shader_ptr; |
| 368 | } else if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW)) { |
| 369 | material_uniform_set = scene_shader.overdraw_material_uniform_set; |
| 370 | shader = scene_shader.overdraw_material_shader_ptr; |
| 371 | } else if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_PSSM_SPLITS)) { |
| 372 | material_uniform_set = scene_shader.debug_shadow_splits_material_uniform_set; |
| 373 | shader = scene_shader.debug_shadow_splits_material_shader_ptr; |
| 374 | } else { |
| 375 | #endif |
| 376 | material_uniform_set = surf->material_uniform_set; |
| 377 | shader = surf->shader; |
| 378 | surf->material->set_as_used(); |
| 379 | #ifdef DEBUG_ENABLED |
| 380 | } |
| 381 | #endif |
| 382 | mesh_surface = surf->surface; |
| 383 | } |
| 384 | |
| 385 | if (!mesh_surface) { |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | //request a redraw if one of the shaders uses TIME |
| 390 | if (shader->uses_time) { |
| 391 | should_request_redraw = true; |
| 392 | } |
| 393 | |
| 394 | // Determine the cull variant. |
| 395 | SceneShaderForwardClustered::ShaderData::CullVariant cull_variant = SceneShaderForwardClustered::ShaderData::CULL_VARIANT_MAX; |
| 396 | if constexpr (p_pass_mode == PASS_MODE_DEPTH_MATERIAL || p_pass_mode == PASS_MODE_SDF) { |
| 397 | cull_variant = SceneShaderForwardClustered::ShaderData::CULL_VARIANT_DOUBLE_SIDED; |
| 398 | } else { |
| 399 | if constexpr (p_pass_mode == PASS_MODE_SHADOW || p_pass_mode == PASS_MODE_SHADOW_DP) { |
| 400 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_DOUBLE_SIDED_SHADOWS) { |
| 401 | cull_variant = SceneShaderForwardClustered::ShaderData::CULL_VARIANT_DOUBLE_SIDED; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (cull_variant == SceneShaderForwardClustered::ShaderData::CULL_VARIANT_MAX) { |
| 406 | bool mirror = surf->owner->mirror; |
| 407 | if (p_params->reverse_cull) { |
| 408 | mirror = !mirror; |
| 409 | } |
| 410 | |
| 411 | cull_variant = mirror ? SceneShaderForwardClustered::ShaderData::CULL_VARIANT_REVERSED : SceneShaderForwardClustered::ShaderData::CULL_VARIANT_NORMAL; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | pipeline_key.primitive_type = surf->primitive; |
| 416 | |
| 417 | RID xforms_uniform_set = surf->owner->transforms_uniform_set; |
| 418 | |
| 419 | SceneShaderForwardClustered::ShaderSpecialization pipeline_specialization = p_params->base_specialization; |
| 420 | pipeline_specialization.multimesh = bool(surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH); |
| 421 | pipeline_specialization.multimesh_format_2d = bool(surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D); |
| 422 | pipeline_specialization.multimesh_has_color = bool(surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR); |
| 423 | pipeline_specialization.multimesh_has_custom_data = bool(surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA); |
| 424 | |
| 425 | if constexpr (p_pass_mode == PASS_MODE_COLOR) { |
| 426 | pipeline_specialization.use_light_soft_shadows = element_info.uses_softshadow; |
| 427 | pipeline_specialization.use_light_projector = element_info.uses_projector; |
| 428 | pipeline_specialization.use_directional_soft_shadows = p_params->use_directional_soft_shadow; |
| 429 | } |
| 430 | |
| 431 | pipeline_key.color_pass_flags = 0; |
| 432 | |
| 433 | switch (p_pass_mode) { |
| 434 | case PASS_MODE_COLOR: { |
| 435 | if (element_info.uses_lightmap) { |
| 436 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_LIGHTMAP; |
| 437 | } else { |
| 438 | pipeline_specialization.use_forward_gi = element_info.uses_forward_gi; |
| 439 | } |
| 440 | |
| 441 | if constexpr ((p_color_pass_flags & COLOR_PASS_FLAG_SEPARATE_SPECULAR) != 0) { |
| 442 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_SEPARATE_SPECULAR; |
| 443 | } |
| 444 | |
| 445 | if constexpr ((p_color_pass_flags & COLOR_PASS_FLAG_MOTION_VECTORS) != 0) { |
| 446 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MOTION_VECTORS; |
| 447 | } |
| 448 | |
| 449 | if constexpr ((p_color_pass_flags & COLOR_PASS_FLAG_TRANSPARENT) != 0) { |
| 450 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_TRANSPARENT; |
| 451 | } |
| 452 | |
| 453 | if constexpr ((p_color_pass_flags & COLOR_PASS_FLAG_MULTIVIEW) != 0) { |
| 454 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MULTIVIEW; |
| 455 | } |
| 456 | |
| 457 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_COLOR_PASS; |
| 458 | } break; |
| 459 | case PASS_MODE_SHADOW: |
| 460 | case PASS_MODE_DEPTH: { |
| 461 | pipeline_key.version = p_params->view_count > 1 ? SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_MULTIVIEW : SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS; |
| 462 | } break; |
| 463 | case PASS_MODE_SHADOW_DP: { |
| 464 | ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for shadow DP pass"); |
| 465 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_DP; |
| 466 | } break; |
| 467 | case PASS_MODE_DEPTH_NORMAL_ROUGHNESS: { |
| 468 | pipeline_key.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; |
| 469 | } break; |
| 470 | case PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI: { |
| 471 | pipeline_key.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; |
| 472 | } break; |
| 473 | case PASS_MODE_DEPTH_MATERIAL: { |
| 474 | ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for material pass"); |
| 475 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_MATERIAL; |
| 476 | } break; |
| 477 | case PASS_MODE_SDF: { |
| 478 | // Note, SDF is prepared in world space, this shouldn't be a multiview buffer even when stereoscopic rendering is used. |
| 479 | ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for SDF pass"); |
| 480 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_SDF; |
| 481 | } break; |
| 482 | } |
| 483 | |
| 484 | pipeline_key.framebuffer_format_id = framebuffer_format; |
| 485 | pipeline_key.wireframe = p_params->force_wireframe; |
| 486 | pipeline_key.ubershader = 0; |
| 487 | |
| 488 | bool emulate_point_size = shader->uses_point_size && scene_shader.emulate_point_size; |
| 489 | |
| 490 | const RD::PolygonCullMode cull_mode = shader->get_cull_mode_from_cull_variant(cull_variant); |
| 491 | RID vertex_array_rd; |
| 492 | RID index_array_rd; |
| 493 | RID pipeline_rd; |
| 494 | const uint32_t ubershader_iterations = 2; |
| 495 | bool pipeline_valid = false; |
| 496 | while (pipeline_key.ubershader < ubershader_iterations) { |
| 497 | // Skeleton and blend shape. |
| 498 | RD::VertexFormatID vertex_format = -1; |
| 499 | bool pipeline_motion_vectors = pipeline_key.color_pass_flags & SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MOTION_VECTORS; |
| 500 | uint64_t input_mask = shader->get_vertex_input_mask(pipeline_key.version, pipeline_key.color_pass_flags, pipeline_key.ubershader); |
| 501 | if (surf->owner->mesh_instance.is_valid()) { |
| 502 | mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, input_mask, pipeline_motion_vectors, emulate_point_size, vertex_array_rd, vertex_format); |
| 503 | } else { |
| 504 | mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, input_mask, pipeline_motion_vectors, emulate_point_size, vertex_array_rd, vertex_format); |
| 505 | } |
| 506 | |
| 507 | pipeline_key.vertex_format_id = vertex_format; |
| 508 | |
| 509 | if (pipeline_key.ubershader) { |
| 510 | pipeline_key.shader_specialization = {}; |
| 511 | pipeline_key.cull_mode = RD::POLYGON_CULL_DISABLED; |
| 512 | } else { |
| 513 | pipeline_key.shader_specialization = pipeline_specialization; |
| 514 | pipeline_key.cull_mode = cull_mode; |
| 515 | } |
| 516 | |
| 517 | pipeline_hash = pipeline_key.hash(); |
| 518 | |
| 519 | if (shader != prev_shader || pipeline_hash != prev_pipeline_hash) { |
| 520 | RS::PipelineSource pipeline_source = pipeline_key.ubershader ? RS::PIPELINE_SOURCE_DRAW : RS::PIPELINE_SOURCE_SPECIALIZATION; |
| 521 | pipeline_rd = shader->pipeline_hash_map.get_pipeline(pipeline_key, pipeline_hash, pipeline_key.ubershader, pipeline_source); |
| 522 | |
| 523 | if (pipeline_rd.is_valid()) { |
| 524 | pipeline_valid = true; |
| 525 | prev_shader = shader; |
| 526 | prev_pipeline_hash = pipeline_hash; |
| 527 | break; |
| 528 | } else { |
| 529 | pipeline_key.ubershader++; |
| 530 | } |
| 531 | } else { |
| 532 | // The same pipeline is bound already. |
| 533 | pipeline_valid = true; |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | if (pipeline_valid) { |
| 539 | if (!emulate_point_size) { |
| 540 | index_array_rd = mesh_storage->mesh_surface_get_index_array(mesh_surface, element_info.lod_index); |
| 541 | } else { |
| 542 | index_array_rd = RID(); |
| 543 | } |
| 544 | |
| 545 | if (prev_vertex_array_rd != vertex_array_rd) { |
| 546 | RD::get_singleton()->draw_list_bind_vertex_array(draw_list, vertex_array_rd); |
| 547 | prev_vertex_array_rd = vertex_array_rd; |
| 548 | } |
| 549 | |
| 550 | if (prev_index_array_rd != index_array_rd) { |
| 551 | if (index_array_rd.is_valid()) { |
| 552 | RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array_rd); |
| 553 | } |
| 554 | prev_index_array_rd = index_array_rd; |
| 555 | } |
| 556 | |
| 557 | if (!pipeline_rd.is_null()) { |
| 558 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline_rd); |
| 559 | } |
| 560 | |
| 561 | if (xforms_uniform_set.is_valid() && prev_xforms_uniform_set != xforms_uniform_set) { |
| 562 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, xforms_uniform_set, TRANSFORMS_UNIFORM_SET); |
| 563 | prev_xforms_uniform_set = xforms_uniform_set; |
| 564 | } |
| 565 | |
| 566 | if (material_uniform_set != prev_material_uniform_set) { |
| 567 | // Update uniform set. |
| 568 | if (material_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(material_uniform_set)) { // Material may not have a uniform set. |
| 569 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, material_uniform_set, MATERIAL_UNIFORM_SET); |
| 570 | } |
| 571 | |
| 572 | prev_material_uniform_set = material_uniform_set; |
| 573 | } |
| 574 | |
| 575 | if (surf->owner->base_flags & INSTANCE_DATA_FLAG_PARTICLES) { |
| 576 | 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); |
| 577 | } else if (surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH) { |
| 578 | 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); |
| 579 | } else { |
| 580 | push_constant.multimesh_motion_vectors_current_offset = 0; |
| 581 | push_constant.multimesh_motion_vectors_previous_offset = 0; |
| 582 | } |
| 583 | |
| 584 | size_t push_constant_size = 0; |
| 585 | if (pipeline_key.ubershader) { |
| 586 | push_constant_size = sizeof(SceneState::PushConstant); |
| 587 | push_constant.ubershader.specialization = pipeline_specialization; |
| 588 | push_constant.ubershader.constants = {}; |
| 589 | push_constant.ubershader.constants.cull_mode = cull_mode; |
| 590 | } else { |
| 591 | push_constant_size = sizeof(SceneState::PushConstant) - sizeof(SceneState::PushConstantUbershader); |
| 592 | } |
| 593 | |
| 594 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, push_constant_size); |
| 595 | |
| 596 | uint32_t instance_count = surf->owner->instance_count > 1 ? surf->owner->instance_count : element_info.repeat; |
| 597 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_PARTICLE_TRAILS) { |
| 598 | instance_count /= surf->owner->trail_steps; |
| 599 | } |
| 600 | |
| 601 | bool indirect = bool(surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH_INDIRECT); |
| 602 | |
| 603 | if (emulate_point_size) { |
| 604 | if (indirect) { |
| 605 | WARN_PRINT("Indirect draws are not supported when emulating point size."); |
| 606 | } |
| 607 | RD::get_singleton()->draw_list_draw(draw_list, false, mesh_storage->mesh_surface_get_vertex_count(mesh_surface), instance_count * 6); |
| 608 | } else if (indirect) { |
| 609 | RD::get_singleton()->draw_list_draw_indirect(draw_list, index_array_rd.is_valid(), mesh_storage->_multimesh_get_command_buffer_rd_rid(surf->owner->data->base), surf->surface_index * sizeof(uint32_t) * mesh_storage->INDIRECT_MULTIMESH_COMMAND_STRIDE, 1, 0); |
| 610 | } else { |
| 611 | RD::get_singleton()->draw_list_draw(draw_list, index_array_rd.is_valid(), instance_count); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | i += element_info.repeat - 1; //skip equal elements |
| 616 | } |
| 617 | |
| 618 | // Make the actual redraw request |
| 619 | if (should_request_redraw) { |
| 620 | RenderingServerDefault::redraw_request(); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | void 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) { |
| 625 | //use template for faster performance (pass mode comparisons are inlined) |
| 626 | |
| 627 | switch (p_params->pass_mode) { |
| 628 | #define VALID_FLAG_COMBINATION(f) \ |
| 629 | case f: { \ |
| 630 | _render_list_template<PASS_MODE_COLOR, f>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); \ |
| 631 | } break; |
| 632 | |
| 633 | case PASS_MODE_COLOR: { |
| 634 | switch (p_params->color_pass_flags) { |
| 635 | VALID_FLAG_COMBINATION(0); |
| 636 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_TRANSPARENT); |
| 637 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_TRANSPARENT | COLOR_PASS_FLAG_MULTIVIEW); |
| 638 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_TRANSPARENT | COLOR_PASS_FLAG_MOTION_VECTORS); |
| 639 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_SEPARATE_SPECULAR); |
| 640 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_SEPARATE_SPECULAR | COLOR_PASS_FLAG_MULTIVIEW); |
| 641 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_SEPARATE_SPECULAR | COLOR_PASS_FLAG_MOTION_VECTORS); |
| 642 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_MULTIVIEW); |
| 643 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_MULTIVIEW | COLOR_PASS_FLAG_MOTION_VECTORS); |
| 644 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_MOTION_VECTORS); |
| 645 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_SEPARATE_SPECULAR | COLOR_PASS_FLAG_MULTIVIEW | COLOR_PASS_FLAG_MOTION_VECTORS); |
| 646 | VALID_FLAG_COMBINATION(COLOR_PASS_FLAG_TRANSPARENT | COLOR_PASS_FLAG_MULTIVIEW | COLOR_PASS_FLAG_MOTION_VECTORS); |
| 647 | default: { |
| 648 | ERR_FAIL_MSG("Invalid color pass flag combination " + itos(p_params->color_pass_flags)); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | } break; |
| 653 | case PASS_MODE_SHADOW: { |
| 654 | _render_list_template<PASS_MODE_SHADOW>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 655 | } break; |
| 656 | case PASS_MODE_SHADOW_DP: { |
| 657 | _render_list_template<PASS_MODE_SHADOW_DP>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 658 | } break; |
| 659 | case PASS_MODE_DEPTH: { |
| 660 | _render_list_template<PASS_MODE_DEPTH>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 661 | } break; |
| 662 | case PASS_MODE_DEPTH_NORMAL_ROUGHNESS: { |
| 663 | _render_list_template<PASS_MODE_DEPTH_NORMAL_ROUGHNESS>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 664 | } break; |
| 665 | case PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI: { |
| 666 | _render_list_template<PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 667 | } break; |
| 668 | case PASS_MODE_DEPTH_MATERIAL: { |
| 669 | _render_list_template<PASS_MODE_DEPTH_MATERIAL>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 670 | } break; |
| 671 | case PASS_MODE_SDF: { |
| 672 | _render_list_template<PASS_MODE_SDF>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 673 | } break; |
| 674 | default: { |
| 675 | // Unknown pass mode. |
| 676 | } break; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | void RenderForwardClustered::_render_list_with_draw_list(RenderListParameters *p_params, RID p_framebuffer, BitField<RD::DrawFlags> p_draw_flags, const Vector<Color> &p_clear_color_values, float p_clear_depth_value, uint32_t p_clear_stencil_value, const Rect2 &p_region) { |
| 681 | RD::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(p_framebuffer); |
| 682 | p_params->framebuffer_format = fb_format; |
| 683 | |
| 684 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer, p_draw_flags, p_clear_color_values, p_clear_depth_value, p_clear_stencil_value, p_region); |
| 685 | _render_list(draw_list, fb_format, p_params, 0, p_params->element_count); |
| 686 | RD::get_singleton()->draw_list_end(); |
| 687 | } |
| 688 | |
| 689 | void RenderForwardClustered::_setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, const Size2 &p_viewport_size, const Color &p_default_bg_color, bool p_opaque_render_buffers, bool p_apply_alpha_multiplier, bool p_pancake_shadows, int p_index) { |
| 690 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 691 | |
| 692 | Ref<RenderSceneBuffersRD> rd = p_render_data->render_buffers; |
| 693 | RID env = is_environment(p_render_data->environment) ? p_render_data->environment : RID(); |
| 694 | RID reflection_probe_instance = p_render_data->reflection_probe.is_valid() ? light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe) : RID(); |
| 695 | |
| 696 | // May do this earlier in RenderSceneRenderRD::render_scene |
| 697 | if (p_index >= (int)scene_state.uniform_buffers.size()) { |
| 698 | uint32_t from = scene_state.uniform_buffers.size(); |
| 699 | scene_state.uniform_buffers.resize(p_index + 1); |
| 700 | for (uint32_t i = from; i < scene_state.uniform_buffers.size(); i++) { |
| 701 | scene_state.uniform_buffers[i] = p_render_data->scene_data->create_uniform_buffer(); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | float luminance_multiplier = rd.is_valid() ? rd->get_luminance_multiplier() : 1.0; |
| 706 | |
| 707 | 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_pancake_shadows, p_screen_size, p_viewport_size, p_default_bg_color, luminance_multiplier, p_opaque_render_buffers, p_apply_alpha_multiplier); |
| 708 | |
| 709 | // now do implementation UBO |
| 710 | |
| 711 | scene_state.ubo.cluster_shift = get_shift_from_power_of_2(p_render_data->cluster_size); |
| 712 | scene_state.ubo.max_cluster_element_count_div_32 = p_render_data->cluster_max_elements / 32; |
| 713 | { |
| 714 | uint32_t cluster_screen_width = Math::division_round_up((uint32_t)p_screen_size.width, p_render_data->cluster_size); |
| 715 | uint32_t cluster_screen_height = Math::division_round_up((uint32_t)p_screen_size.height, p_render_data->cluster_size); |
| 716 | scene_state.ubo.cluster_type_size = cluster_screen_width * cluster_screen_height * (scene_state.ubo.max_cluster_element_count_div_32 + 32); |
| 717 | scene_state.ubo.cluster_width = cluster_screen_width; |
| 718 | } |
| 719 | |
| 720 | scene_state.ubo.gi_upscale_for_msaa = false; |
| 721 | scene_state.ubo.volumetric_fog_enabled = false; |
| 722 | |
| 723 | if (rd.is_valid()) { |
| 724 | if (rd->get_msaa_3d() != RS::VIEWPORT_MSAA_DISABLED) { |
| 725 | scene_state.ubo.gi_upscale_for_msaa = true; |
| 726 | } |
| 727 | |
| 728 | if (rd->has_custom_data(RB_SCOPE_FOG)) { |
| 729 | Ref<RendererRD::Fog::VolumetricFog> fog = rd->get_custom_data(RB_SCOPE_FOG); |
| 730 | |
| 731 | scene_state.ubo.volumetric_fog_enabled = true; |
| 732 | float fog_end = fog->length; |
| 733 | if (fog_end > 0.0) { |
| 734 | scene_state.ubo.volumetric_fog_inv_length = 1.0 / fog_end; |
| 735 | } else { |
| 736 | scene_state.ubo.volumetric_fog_inv_length = 1.0; |
| 737 | } |
| 738 | |
| 739 | float fog_detail_spread = fog->spread; //reverse lookup |
| 740 | if (fog_detail_spread > 0.0) { |
| 741 | scene_state.ubo.volumetric_fog_detail_spread = 1.0 / fog_detail_spread; |
| 742 | } else { |
| 743 | scene_state.ubo.volumetric_fog_detail_spread = 1.0; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) { |
| 749 | scene_state.ubo.ss_effects_flags = 0; |
| 750 | } else if (p_render_data->reflection_probe.is_null() && is_environment(p_render_data->environment)) { |
| 751 | scene_state.ubo.ssao_ao_affect = environment_get_ssao_ao_channel_affect(p_render_data->environment); |
| 752 | scene_state.ubo.ssao_light_affect = environment_get_ssao_direct_light_affect(p_render_data->environment); |
| 753 | uint32_t ss_flags = 0; |
| 754 | if (p_opaque_render_buffers) { |
| 755 | ss_flags |= environment_get_ssao_enabled(p_render_data->environment) ? (1 << 0) : 0; |
| 756 | ss_flags |= environment_get_ssil_enabled(p_render_data->environment) ? (1 << 1) : 0; |
| 757 | ss_flags |= environment_get_ssr_enabled(p_render_data->environment) ? (1 << 2) : 0; |
| 758 | |
| 759 | if (rd.is_valid()) { |
| 760 | Ref<RenderBufferDataForwardClustered> rb_data; |
| 761 | if (rd->has_custom_data(RB_SCOPE_FORWARD_CLUSTERED)) { |
| 762 | rb_data = rd->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 763 | ss_flags |= (rb_data.is_valid() && !rb_data->ss_effects_data.ssr.half_size) ? (1 << 3) : 0; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | scene_state.ubo.ss_effects_flags = ss_flags; |
| 768 | } else { |
| 769 | scene_state.ubo.ss_effects_flags = 0; |
| 770 | } |
| 771 | |
| 772 | if (p_index >= (int)scene_state.implementation_uniform_buffers.size()) { |
| 773 | uint32_t from = scene_state.implementation_uniform_buffers.size(); |
| 774 | scene_state.implementation_uniform_buffers.resize(p_index + 1); |
| 775 | for (uint32_t i = from; i < scene_state.implementation_uniform_buffers.size(); i++) { |
| 776 | scene_state.implementation_uniform_buffers[i] = RD::get_singleton()->uniform_buffer_create(sizeof(SceneState::UBO)); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | RD::get_singleton()->buffer_update(scene_state.implementation_uniform_buffers[p_index], 0, sizeof(SceneState::UBO), &scene_state.ubo); |
| 781 | } |
| 782 | |
| 783 | void RenderForwardClustered::SceneState::grow_instance_buffer(RenderListType p_render_list, uint32_t p_req_element_count, bool p_append) { |
| 784 | if (p_req_element_count > 0) { |
| 785 | if (instance_buffer[p_render_list].get_size(0u) < p_req_element_count * sizeof(SceneState::InstanceData)) { |
| 786 | instance_buffer[p_render_list].uninit(); |
| 787 | uint32_t new_size = nearest_power_of_2_templated(MAX(uint64_t(INSTANCE_DATA_BUFFER_MIN_SIZE), p_req_element_count)); |
| 788 | instance_buffer[p_render_list].set_storage_size(0u, new_size * sizeof(SceneState::InstanceData)); |
| 789 | curr_gpu_ptr[p_render_list] = nullptr; |
| 790 | } |
| 791 | |
| 792 | const bool must_remap = instance_buffer[p_render_list].prepare_for_map(p_append); |
| 793 | if (must_remap) { |
| 794 | curr_gpu_ptr[p_render_list] = nullptr; |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | void 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) { |
| 800 | RenderList *rl = &render_list[p_render_list]; |
| 801 | uint32_t element_total = p_max_elements >= 0 ? uint32_t(p_max_elements) : rl->elements.size(); |
| 802 | |
| 803 | rl->element_info.resize(p_offset + element_total); |
| 804 | |
| 805 | // If p_offset == 0, grow_instance_buffer resets and increment the buffer. |
| 806 | // If this behavior ever changes, _render_shadow_begin may need to change. |
| 807 | scene_state.grow_instance_buffer(p_render_list, p_offset + element_total, p_offset != 0u); |
| 808 | if (!scene_state.curr_gpu_ptr[p_render_list] && element_total > 0u) { |
| 809 | // The old buffer was replaced for another larger one. We must start copying from scratch. |
| 810 | element_total += p_offset; |
| 811 | p_offset = 0u; |
| 812 | scene_state.curr_gpu_ptr[p_render_list] = reinterpret_cast<SceneState::InstanceData *>(scene_state.instance_buffer[p_render_list].map_raw_for_upload(0u)); |
| 813 | } |
| 814 | |
| 815 | if (p_render_info) { |
| 816 | p_render_info[RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] += element_total; |
| 817 | } |
| 818 | |
| 819 | uint32_t repeats = 0; |
| 820 | GeometryInstanceSurfaceDataCache *prev_surface = nullptr; |
| 821 | for (uint32_t i = 0; i < element_total; i++) { |
| 822 | GeometryInstanceSurfaceDataCache *surface = rl->elements[i + p_offset]; |
| 823 | GeometryInstanceForwardClustered *inst = surface->owner; |
| 824 | |
| 825 | SceneState::InstanceData instance_data; |
| 826 | |
| 827 | if (likely(inst->store_transform_cache)) { |
| 828 | RendererRD::MaterialStorage::store_transform_transposed_3x4(inst->transform, instance_data.transform); |
| 829 | RendererRD::MaterialStorage::store_transform_transposed_3x4(inst->prev_transform, instance_data.prev_transform); |
| 830 | |
| 831 | #ifdef REAL_T_IS_DOUBLE |
| 832 | // Split the origin into two components, the float approximation and the missing precision. |
| 833 | // In the shader we will combine these back together to restore the lost precision. |
| 834 | RendererRD::MaterialStorage::split_double(inst->transform.origin.x, &instance_data.transform[3], &instance_data.model_precision[0]); |
| 835 | RendererRD::MaterialStorage::split_double(inst->transform.origin.y, &instance_data.transform[7], &instance_data.model_precision[1]); |
| 836 | RendererRD::MaterialStorage::split_double(inst->transform.origin.z, &instance_data.transform[11], &instance_data.model_precision[2]); |
| 837 | RendererRD::MaterialStorage::split_double(inst->prev_transform.origin.x, &instance_data.prev_transform[3], &instance_data.prev_model_precision[0]); |
| 838 | RendererRD::MaterialStorage::split_double(inst->prev_transform.origin.y, &instance_data.prev_transform[7], &instance_data.prev_model_precision[1]); |
| 839 | RendererRD::MaterialStorage::split_double(inst->prev_transform.origin.z, &instance_data.prev_transform[11], &instance_data.prev_model_precision[2]); |
| 840 | #endif |
| 841 | } else { |
| 842 | RendererRD::MaterialStorage::store_transform_transposed_3x4(Transform3D(), instance_data.transform); |
| 843 | RendererRD::MaterialStorage::store_transform_transposed_3x4(Transform3D(), instance_data.prev_transform); |
| 844 | } |
| 845 | |
| 846 | instance_data.flags = inst->flags_cache; |
| 847 | instance_data.gi_offset = inst->gi_offset_cache; |
| 848 | instance_data.layer_mask = inst->layer_mask; |
| 849 | instance_data.instance_uniforms_ofs = uint32_t(inst->shader_uniforms_offset); |
| 850 | instance_data.set_lightmap_uv_scale(inst->lightmap_uv_scale); |
| 851 | |
| 852 | AABB surface_aabb = AABB(Vector3(0.0, 0.0, 0.0), Vector3(1.0, 1.0, 1.0)); |
| 853 | uint64_t format = RendererRD::MeshStorage::get_singleton()->mesh_surface_get_format(surface->surface); |
| 854 | Vector4 uv_scale = Vector4(0.0, 0.0, 0.0, 0.0); |
| 855 | |
| 856 | if (format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) { |
| 857 | surface_aabb = RendererRD::MeshStorage::get_singleton()->mesh_surface_get_aabb(surface->surface); |
| 858 | uv_scale = RendererRD::MeshStorage::get_singleton()->mesh_surface_get_uv_scale(surface->surface); |
| 859 | } |
| 860 | |
| 861 | instance_data.set_compressed_aabb(surface_aabb); |
| 862 | instance_data.set_uv_scale(uv_scale); |
| 863 | |
| 864 | scene_state.curr_gpu_ptr[p_render_list][i + p_offset] = instance_data; |
| 865 | |
| 866 | const bool cant_repeat = instance_data.flags & INSTANCE_DATA_FLAG_MULTIMESH || inst->mesh_instance.is_valid(); |
| 867 | |
| 868 | 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) { |
| 869 | //this element is the same as the previous one, count repeats to draw it using instancing |
| 870 | repeats++; |
| 871 | } else { |
| 872 | if (repeats > 0) { |
| 873 | for (uint32_t j = 1; j <= repeats; j++) { |
| 874 | rl->element_info[p_offset + i - j].repeat = j; |
| 875 | } |
| 876 | } |
| 877 | repeats = 1; |
| 878 | if (p_render_info) { |
| 879 | p_render_info[RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME]++; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | RenderElementInfo &element_info = rl->element_info[p_offset + i]; |
| 884 | |
| 885 | element_info.value = uint32_t(surface->sort.sort_key1 & 0xFFF); |
| 886 | |
| 887 | if (cant_repeat) { |
| 888 | prev_surface = nullptr; |
| 889 | } else { |
| 890 | prev_surface = surface; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | if (repeats > 0) { |
| 895 | for (uint32_t j = 1; j <= repeats; j++) { |
| 896 | rl->element_info[p_offset + element_total - j].repeat = j; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | if (p_update_buffer && element_total > 0u) { |
| 901 | RenderingDevice::get_singleton()->buffer_flush(scene_state.instance_buffer[p_render_list]._get(0u)); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | _FORCE_INLINE_ static uint32_t _indices_to_primitives(RS::PrimitiveType p_primitive, uint32_t p_indices) { |
| 906 | static const uint32_t divisor[RS::PRIMITIVE_MAX] = { 1, 2, 1, 3, 1 }; |
| 907 | static const uint32_t subtractor[RS::PRIMITIVE_MAX] = { 0, 0, 1, 0, 2 }; |
| 908 | return (p_indices - subtractor[p_primitive]) / divisor[p_primitive]; |
| 909 | } |
| 910 | void RenderForwardClustered::_fill_render_list(RenderListType p_render_list, const RenderDataRD *p_render_data, PassMode p_pass_mode, bool p_using_sdfgi, bool p_using_opaque_gi, bool p_using_motion_pass, bool p_append) { |
| 911 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 912 | uint64_t frame = RSG::rasterizer->get_frame_number(); |
| 913 | |
| 914 | if (p_render_list == RENDER_LIST_OPAQUE) { |
| 915 | scene_state.used_sss = false; |
| 916 | scene_state.used_screen_texture = false; |
| 917 | scene_state.used_normal_texture = false; |
| 918 | scene_state.used_depth_texture = false; |
| 919 | scene_state.used_lightmap = false; |
| 920 | scene_state.used_opaque_stencil = false; |
| 921 | } |
| 922 | uint32_t lightmap_captures_used = 0; |
| 923 | |
| 924 | 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); |
| 925 | near_plane.d += p_render_data->scene_data->cam_projection.get_z_near(); |
| 926 | float z_max = p_render_data->scene_data->cam_projection.get_z_far() - p_render_data->scene_data->cam_projection.get_z_near(); |
| 927 | |
| 928 | RenderList *rl = &render_list[p_render_list]; |
| 929 | _update_dirty_geometry_instances(); |
| 930 | |
| 931 | if (!p_append) { |
| 932 | rl->clear(); |
| 933 | if (p_render_list == RENDER_LIST_OPAQUE) { |
| 934 | // Opaque fills motion and alpha lists. |
| 935 | render_list[RENDER_LIST_MOTION].clear(); |
| 936 | render_list[RENDER_LIST_ALPHA].clear(); |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | //fill list |
| 941 | |
| 942 | for (int i = 0; i < (int)p_render_data->instances->size(); i++) { |
| 943 | GeometryInstanceForwardClustered *inst = static_cast<GeometryInstanceForwardClustered *>((*p_render_data->instances)[i]); |
| 944 | |
| 945 | Vector3 center = inst->transform.origin; |
| 946 | if (p_render_data->scene_data->cam_orthogonal) { |
| 947 | if (inst->use_aabb_center) { |
| 948 | center = inst->transformed_aabb.get_support(-near_plane.normal); |
| 949 | } |
| 950 | inst->depth = near_plane.distance_to(center) - inst->sorting_offset; |
| 951 | } else { |
| 952 | if (inst->use_aabb_center) { |
| 953 | center = inst->transformed_aabb.position + (inst->transformed_aabb.size * 0.5); |
| 954 | } |
| 955 | inst->depth = p_render_data->scene_data->cam_transform.origin.distance_to(center) - inst->sorting_offset; |
| 956 | } |
| 957 | uint32_t depth_layer = CLAMP(int(inst->depth * 16 / z_max), 0, 15); |
| 958 | |
| 959 | uint32_t flags = inst->base_flags; //fill flags if appropriate |
| 960 | |
| 961 | if (inst->non_uniform_scale) { |
| 962 | flags |= INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE; |
| 963 | } |
| 964 | bool uses_lightmap = false; |
| 965 | bool uses_gi = false; |
| 966 | bool uses_motion = false; |
| 967 | float fade_alpha = 1.0; |
| 968 | |
| 969 | if (inst->fade_near || inst->fade_far) { |
| 970 | float fade_dist = inst->transformed_aabb.get_center().distance_to(p_render_data->scene_data->cam_transform.origin); |
| 971 | // Use `smoothstep()` to make opacity changes more gradual and less noticeable to the player. |
| 972 | if (inst->fade_far && fade_dist > inst->fade_far_begin) { |
| 973 | fade_alpha = Math::smoothstep(0.0f, 1.0f, 1.0f - (fade_dist - inst->fade_far_begin) / (inst->fade_far_end - inst->fade_far_begin)); |
| 974 | } else if (inst->fade_near && fade_dist < inst->fade_near_end) { |
| 975 | fade_alpha = Math::smoothstep(0.0f, 1.0f, (fade_dist - inst->fade_near_begin) / (inst->fade_near_end - inst->fade_near_begin)); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | fade_alpha *= inst->force_alpha * inst->parent_fade_alpha; |
| 980 | |
| 981 | flags = (flags & ~INSTANCE_DATA_FLAGS_FADE_MASK) | (uint32_t(fade_alpha * 255.0) << INSTANCE_DATA_FLAGS_FADE_SHIFT); |
| 982 | |
| 983 | if (p_render_list == RENDER_LIST_OPAQUE) { |
| 984 | // Setup GI |
| 985 | if (inst->lightmap_instance.is_valid()) { |
| 986 | int32_t lightmap_cull_index = -1; |
| 987 | for (uint32_t j = 0; j < scene_state.lightmaps_used; j++) { |
| 988 | if (scene_state.lightmap_ids[j] == inst->lightmap_instance) { |
| 989 | lightmap_cull_index = j; |
| 990 | break; |
| 991 | } |
| 992 | } |
| 993 | if (lightmap_cull_index >= 0) { |
| 994 | inst->gi_offset_cache = inst->lightmap_slice_index << 16; |
| 995 | inst->gi_offset_cache |= lightmap_cull_index; |
| 996 | flags |= INSTANCE_DATA_FLAG_USE_LIGHTMAP; |
| 997 | if (scene_state.lightmap_has_sh[lightmap_cull_index]) { |
| 998 | flags |= INSTANCE_DATA_FLAG_USE_SH_LIGHTMAP; |
| 999 | } |
| 1000 | uses_lightmap = true; |
| 1001 | } else { |
| 1002 | inst->gi_offset_cache = 0xFFFFFFFF; |
| 1003 | } |
| 1004 | |
| 1005 | } else if (inst->lightmap_sh) { |
| 1006 | if (lightmap_captures_used < scene_state.max_lightmap_captures) { |
| 1007 | const Color *src_capture = inst->lightmap_sh->sh; |
| 1008 | LightmapCaptureData &lcd = scene_state.lightmap_captures[lightmap_captures_used]; |
| 1009 | for (int j = 0; j < 9; j++) { |
| 1010 | lcd.sh[j * 4 + 0] = src_capture[j].r; |
| 1011 | lcd.sh[j * 4 + 1] = src_capture[j].g; |
| 1012 | lcd.sh[j * 4 + 2] = src_capture[j].b; |
| 1013 | lcd.sh[j * 4 + 3] = src_capture[j].a; |
| 1014 | } |
| 1015 | flags |= INSTANCE_DATA_FLAG_USE_LIGHTMAP_CAPTURE; |
| 1016 | inst->gi_offset_cache = lightmap_captures_used; |
| 1017 | lightmap_captures_used++; |
| 1018 | uses_lightmap = true; |
| 1019 | } |
| 1020 | |
| 1021 | } else { |
| 1022 | if (p_using_opaque_gi) { |
| 1023 | flags |= INSTANCE_DATA_FLAG_USE_GI_BUFFERS; |
| 1024 | } |
| 1025 | |
| 1026 | if (inst->voxel_gi_instances[0].is_valid()) { |
| 1027 | uint32_t probe0_index = 0xFFFF; |
| 1028 | uint32_t probe1_index = 0xFFFF; |
| 1029 | |
| 1030 | for (uint32_t j = 0; j < scene_state.voxelgis_used; j++) { |
| 1031 | if (scene_state.voxelgi_ids[j] == inst->voxel_gi_instances[0]) { |
| 1032 | probe0_index = j; |
| 1033 | } else if (scene_state.voxelgi_ids[j] == inst->voxel_gi_instances[1]) { |
| 1034 | probe1_index = j; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | if (probe0_index == 0xFFFF && probe1_index != 0xFFFF) { |
| 1039 | //0 must always exist if a probe exists |
| 1040 | SWAP(probe0_index, probe1_index); |
| 1041 | } |
| 1042 | |
| 1043 | inst->gi_offset_cache = probe0_index | (probe1_index << 16); |
| 1044 | flags |= INSTANCE_DATA_FLAG_USE_VOXEL_GI; |
| 1045 | uses_gi = true; |
| 1046 | } else { |
| 1047 | if (p_using_sdfgi && inst->can_sdfgi) { |
| 1048 | flags |= INSTANCE_DATA_FLAG_USE_SDFGI; |
| 1049 | uses_gi = true; |
| 1050 | } |
| 1051 | inst->gi_offset_cache = 0xFFFFFFFF; |
| 1052 | } |
| 1053 | } |
| 1054 | if (p_pass_mode == PASS_MODE_DEPTH_NORMAL_ROUGHNESS || p_pass_mode == PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI || p_pass_mode == PASS_MODE_COLOR) { |
| 1055 | bool transform_changed = inst->transform_status == GeometryInstanceForwardClustered::TransformStatus::MOVED; |
| 1056 | bool has_mesh_instance = inst->mesh_instance.is_valid(); |
| 1057 | bool uses_particles = inst->base_flags & INSTANCE_DATA_FLAG_PARTICLES; |
| 1058 | bool is_multimesh_with_motion = !uses_particles && (inst->base_flags & INSTANCE_DATA_FLAG_MULTIMESH) && mesh_storage->_multimesh_uses_motion_vectors_offsets(inst->data->base); |
| 1059 | bool is_dynamic = transform_changed || has_mesh_instance || uses_particles || is_multimesh_with_motion; |
| 1060 | if (p_pass_mode == PASS_MODE_COLOR && p_using_motion_pass) { |
| 1061 | uses_motion = is_dynamic; |
| 1062 | } else if (is_dynamic) { |
| 1063 | flags |= INSTANCE_DATA_FLAGS_DYNAMIC; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | inst->flags_cache = flags; |
| 1068 | |
| 1069 | GeometryInstanceSurfaceDataCache *surf = inst->surface_caches; |
| 1070 | |
| 1071 | float lod_distance = 0.0; |
| 1072 | |
| 1073 | if (p_render_data->scene_data->cam_orthogonal) { |
| 1074 | lod_distance = 1.0; |
| 1075 | } else { |
| 1076 | Vector3 aabb_min = inst->transformed_aabb.position; |
| 1077 | Vector3 aabb_max = inst->transformed_aabb.position + inst->transformed_aabb.size; |
| 1078 | Vector3 camera_position = p_render_data->scene_data->main_cam_transform.origin; |
| 1079 | Vector3 surface_distance = Vector3(0.0, 0.0, 0.0).max(aabb_min - camera_position).max(camera_position - aabb_max); |
| 1080 | |
| 1081 | lod_distance = surface_distance.length(); |
| 1082 | } |
| 1083 | |
| 1084 | if (unlikely(inst->transform_status != GeometryInstanceForwardClustered::TransformStatus::NONE && frame > inst->prev_transform_change_frame + 1 && inst->prev_transform_change_frame)) { |
| 1085 | inst->prev_transform = inst->transform; |
| 1086 | inst->transform_status = GeometryInstanceForwardClustered::TransformStatus::NONE; |
| 1087 | } |
| 1088 | |
| 1089 | while (surf) { |
| 1090 | surf->sort.uses_forward_gi = 0; |
| 1091 | surf->sort.uses_lightmap = 0; |
| 1092 | |
| 1093 | // LOD |
| 1094 | if (p_render_data->scene_data->screen_mesh_lod_threshold > 0.0 && mesh_storage->mesh_surface_has_lod(surf->surface)) { |
| 1095 | uint32_t indices = 0; |
| 1096 | surf->sort.lod_index = mesh_storage->mesh_surface_get_lod(surf->surface, inst->lod_model_scale * inst->lod_bias, lod_distance * p_render_data->scene_data->lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, indices); |
| 1097 | if (p_render_data->render_info) { |
| 1098 | indices = _indices_to_primitives(surf->primitive, indices); |
| 1099 | if (p_render_list == RENDER_LIST_OPAQUE) { //opaque |
| 1100 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices; |
| 1101 | } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow |
| 1102 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices; |
| 1103 | } |
| 1104 | } |
| 1105 | } else { |
| 1106 | surf->sort.lod_index = 0; |
| 1107 | if (p_render_data->render_info) { |
| 1108 | // This does not include primitives rendered via indirect draw calls. |
| 1109 | uint32_t to_draw = mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface); |
| 1110 | to_draw = _indices_to_primitives(surf->primitive, to_draw); |
| 1111 | to_draw *= inst->instance_count; |
| 1112 | if (p_render_list == RENDER_LIST_OPAQUE) { //opaque |
| 1113 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw; |
| 1114 | } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow |
| 1115 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | // ADD Element |
| 1121 | if (p_pass_mode == PASS_MODE_COLOR) { |
| 1122 | #ifdef DEBUG_ENABLED |
| 1123 | bool force_alpha = unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW); |
| 1124 | #else |
| 1125 | bool force_alpha = false; |
| 1126 | #endif |
| 1127 | |
| 1128 | if (fade_alpha < FADE_ALPHA_PASS_THRESHOLD) { |
| 1129 | force_alpha = true; |
| 1130 | } |
| 1131 | |
| 1132 | if (!force_alpha && (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE))) { |
| 1133 | rl->add_element(surf); |
| 1134 | } |
| 1135 | |
| 1136 | if (force_alpha || (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA)) { |
| 1137 | surf->color_pass_inclusion_mask = COLOR_PASS_FLAG_TRANSPARENT; |
| 1138 | render_list[RENDER_LIST_ALPHA].add_element(surf); |
| 1139 | if (uses_gi) { |
| 1140 | surf->sort.uses_forward_gi = 1; |
| 1141 | } |
| 1142 | } else if (p_using_motion_pass && (uses_motion || (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_MOTION_VECTOR))) { |
| 1143 | surf->color_pass_inclusion_mask = COLOR_PASS_FLAG_MOTION_VECTORS; |
| 1144 | render_list[RENDER_LIST_MOTION].add_element(surf); |
| 1145 | } else { |
| 1146 | surf->color_pass_inclusion_mask = 0; |
| 1147 | } |
| 1148 | |
| 1149 | if (uses_lightmap) { |
| 1150 | surf->sort.uses_lightmap = 1; |
| 1151 | scene_state.used_lightmap = true; |
| 1152 | } |
| 1153 | |
| 1154 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_SUBSURFACE_SCATTERING) { |
| 1155 | scene_state.used_sss = true; |
| 1156 | } |
| 1157 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_SCREEN_TEXTURE) { |
| 1158 | scene_state.used_screen_texture = true; |
| 1159 | } |
| 1160 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_NORMAL_TEXTURE) { |
| 1161 | scene_state.used_normal_texture = true; |
| 1162 | } |
| 1163 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_DEPTH_TEXTURE) { |
| 1164 | scene_state.used_depth_texture = true; |
| 1165 | } |
| 1166 | if ((surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_STENCIL) && !force_alpha && (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE))) { |
| 1167 | scene_state.used_opaque_stencil = true; |
| 1168 | } |
| 1169 | } else if (p_pass_mode == PASS_MODE_SHADOW || p_pass_mode == PASS_MODE_SHADOW_DP) { |
| 1170 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW) { |
| 1171 | rl->add_element(surf); |
| 1172 | } |
| 1173 | } else if (p_pass_mode == PASS_MODE_DEPTH_MATERIAL) { |
| 1174 | if (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE | GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA)) { |
| 1175 | rl->add_element(surf); |
| 1176 | } |
| 1177 | } else { |
| 1178 | if (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE)) { |
| 1179 | rl->add_element(surf); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | surf->sort.depth_layer = depth_layer; |
| 1184 | |
| 1185 | surf = surf->next; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | if (p_render_list == RENDER_LIST_OPAQUE && lightmap_captures_used) { |
| 1190 | RD::get_singleton()->buffer_update(scene_state.lightmap_capture_buffer, 0, sizeof(LightmapCaptureData) * lightmap_captures_used, scene_state.lightmap_captures); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | void RenderForwardClustered::_setup_voxelgis(const PagedArray<RID> &p_voxelgis) { |
| 1195 | scene_state.voxelgis_used = MIN(p_voxelgis.size(), uint32_t(MAX_VOXEL_GI_INSTANCESS)); |
| 1196 | for (uint32_t i = 0; i < scene_state.voxelgis_used; i++) { |
| 1197 | scene_state.voxelgi_ids[i] = p_voxelgis[i]; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | void RenderForwardClustered::_setup_lightmaps(const RenderDataRD *p_render_data, const PagedArray<RID> &p_lightmaps, const Transform3D &p_cam_transform) { |
| 1202 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 1203 | |
| 1204 | scene_state.lightmaps_used = 0; |
| 1205 | for (int i = 0; i < (int)p_lightmaps.size(); i++) { |
| 1206 | if (i >= (int)scene_state.max_lightmaps) { |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | RID lightmap = light_storage->lightmap_instance_get_lightmap(p_lightmaps[i]); |
| 1211 | |
| 1212 | // Transform (for directional lightmaps). |
| 1213 | Basis to_lm = light_storage->lightmap_instance_get_transform(p_lightmaps[i]).basis.inverse() * p_cam_transform.basis; |
| 1214 | to_lm = to_lm.inverse().transposed(); //will transform normals |
| 1215 | RendererRD::MaterialStorage::store_transform_3x3(to_lm, scene_state.lightmaps[i].normal_xform); |
| 1216 | |
| 1217 | // Light texture size. |
| 1218 | Vector2i lightmap_size = light_storage->lightmap_get_light_texture_size(lightmap); |
| 1219 | scene_state.lightmaps[i].texture_size[0] = lightmap_size[0]; |
| 1220 | scene_state.lightmaps[i].texture_size[1] = lightmap_size[1]; |
| 1221 | |
| 1222 | // Exposure. |
| 1223 | scene_state.lightmaps[i].exposure_normalization = 1.0; |
| 1224 | scene_state.lightmaps[i].flags = light_storage->lightmap_get_shadowmask_mode(lightmap); |
| 1225 | if (p_render_data->camera_attributes.is_valid()) { |
| 1226 | float baked_exposure = light_storage->lightmap_get_baked_exposure_normalization(lightmap); |
| 1227 | float enf = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes); |
| 1228 | scene_state.lightmaps[i].exposure_normalization = enf / baked_exposure; |
| 1229 | } |
| 1230 | |
| 1231 | scene_state.lightmap_ids[i] = p_lightmaps[i]; |
| 1232 | scene_state.lightmap_has_sh[i] = light_storage->lightmap_uses_spherical_harmonics(lightmap); |
| 1233 | |
| 1234 | scene_state.lightmaps_used++; |
| 1235 | } |
| 1236 | if (scene_state.lightmaps_used > 0) { |
| 1237 | RD::get_singleton()->buffer_update(scene_state.lightmap_buffer, 0, sizeof(LightmapData) * scene_state.lightmaps_used, scene_state.lightmaps); |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | /* SDFGI */ |
| 1242 | |
| 1243 | void RenderForwardClustered::_update_sdfgi(RenderDataRD *p_render_data) { |
| 1244 | if (p_render_data->sdfgi_update_data == nullptr) { |
| 1245 | return; |
| 1246 | } |
| 1247 | |
| 1248 | Ref<RenderSceneBuffersRD> rb; |
| 1249 | if (p_render_data && p_render_data->render_buffers.is_valid()) { |
| 1250 | rb = p_render_data->render_buffers; |
| 1251 | } |
| 1252 | |
| 1253 | if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_SDFGI)) { |
| 1254 | RENDER_TIMESTAMP("Render SDFGI"); |
| 1255 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 1256 | float exposure_normalization = 1.0; |
| 1257 | |
| 1258 | if (p_render_data->camera_attributes.is_valid()) { |
| 1259 | exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes); |
| 1260 | } |
| 1261 | for (int i = 0; i < p_render_data->render_sdfgi_region_count; i++) { |
| 1262 | sdfgi->render_region(rb, p_render_data->render_sdfgi_regions[i].region, p_render_data->render_sdfgi_regions[i].instances, exposure_normalization); |
| 1263 | } |
| 1264 | if (p_render_data->sdfgi_update_data->update_static) { |
| 1265 | 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); |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /* Debug */ |
| 1271 | |
| 1272 | void RenderForwardClustered::_debug_draw_cluster(Ref<RenderSceneBuffersRD> p_render_buffers) { |
| 1273 | if (p_render_buffers.is_valid() && current_cluster_builder != nullptr) { |
| 1274 | RS::ViewportDebugDraw dd = get_debug_draw_mode(); |
| 1275 | |
| 1276 | 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) { |
| 1277 | ClusterBuilderRD::ElementType elem_type = ClusterBuilderRD::ELEMENT_TYPE_MAX; |
| 1278 | switch (dd) { |
| 1279 | case RS::VIEWPORT_DEBUG_DRAW_CLUSTER_OMNI_LIGHTS: |
| 1280 | elem_type = ClusterBuilderRD::ELEMENT_TYPE_OMNI_LIGHT; |
| 1281 | break; |
| 1282 | case RS::VIEWPORT_DEBUG_DRAW_CLUSTER_SPOT_LIGHTS: |
| 1283 | elem_type = ClusterBuilderRD::ELEMENT_TYPE_SPOT_LIGHT; |
| 1284 | break; |
| 1285 | case RS::VIEWPORT_DEBUG_DRAW_CLUSTER_DECALS: |
| 1286 | elem_type = ClusterBuilderRD::ELEMENT_TYPE_DECAL; |
| 1287 | break; |
| 1288 | case RS::VIEWPORT_DEBUG_DRAW_CLUSTER_REFLECTION_PROBES: |
| 1289 | elem_type = ClusterBuilderRD::ELEMENT_TYPE_REFLECTION_PROBE; |
| 1290 | break; |
| 1291 | default: { |
| 1292 | } |
| 1293 | } |
| 1294 | current_cluster_builder->debug(elem_type); |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | //////////////////////////////////////////////////////////////////////////////// |
| 1300 | // FOG SHADER |
| 1301 | |
| 1302 | void 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) { |
| 1303 | ERR_FAIL_COND(p_render_buffers.is_null()); |
| 1304 | |
| 1305 | Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 1306 | ERR_FAIL_COND(rb_data.is_null()); |
| 1307 | |
| 1308 | ERR_FAIL_COND(!p_render_buffers->has_custom_data(RB_SCOPE_GI)); |
| 1309 | Ref<RendererRD::GI::RenderBuffersGI> rbgi = p_render_buffers->get_custom_data(RB_SCOPE_GI); |
| 1310 | |
| 1311 | Ref<RendererRD::GI::SDFGI> sdfgi; |
| 1312 | if (p_render_buffers->has_custom_data(RB_SCOPE_SDFGI)) { |
| 1313 | sdfgi = p_render_buffers->get_custom_data(RB_SCOPE_SDFGI); |
| 1314 | } |
| 1315 | |
| 1316 | Size2i size = p_render_buffers->get_internal_size(); |
| 1317 | float ratio = float(size.x) / float((size.x + size.y) / 2); |
| 1318 | uint32_t target_width = uint32_t(float(get_volumetric_fog_size()) * ratio); |
| 1319 | uint32_t target_height = uint32_t(float(get_volumetric_fog_size()) / ratio); |
| 1320 | |
| 1321 | if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) { |
| 1322 | Ref<RendererRD::Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG); |
| 1323 | //validate |
| 1324 | 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()) { |
| 1325 | p_render_buffers->set_custom_data(RB_SCOPE_FOG, Ref<RenderBufferCustomDataRD>()); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | if (p_environment.is_null() || !environment_get_volumetric_fog_enabled(p_environment)) { |
| 1330 | //no reason to enable or update, bye |
| 1331 | return; |
| 1332 | } |
| 1333 | |
| 1334 | if (p_environment.is_valid() && environment_get_volumetric_fog_enabled(p_environment) && !p_render_buffers->has_custom_data(RB_SCOPE_FOG)) { |
| 1335 | //required volumetric fog but not existing, create |
| 1336 | Ref<RendererRD::Fog::VolumetricFog> fog; |
| 1337 | |
| 1338 | fog.instantiate(); |
| 1339 | fog->init(Vector3i(target_width, target_height, get_volumetric_fog_depth()), sky.sky_shader.default_shader_rd); |
| 1340 | |
| 1341 | p_render_buffers->set_custom_data(RB_SCOPE_FOG, fog); |
| 1342 | } |
| 1343 | |
| 1344 | if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) { |
| 1345 | Ref<RendererRD::Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG); |
| 1346 | |
| 1347 | RendererRD::Fog::VolumetricFogSettings settings; |
| 1348 | settings.rb_size = size; |
| 1349 | settings.time = time; |
| 1350 | settings.is_using_radiance_octmap_array = is_using_radiance_octmap_array(); |
| 1351 | settings.max_cluster_elements = RendererRD::LightStorage::get_singleton()->get_max_cluster_elements(); |
| 1352 | settings.volumetric_fog_filter_active = get_volumetric_fog_filter_active(); |
| 1353 | |
| 1354 | settings.shadow_sampler = shadow_sampler; |
| 1355 | 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(); |
| 1356 | settings.voxel_gi_buffer = rbgi->get_voxel_gi_buffer(); |
| 1357 | settings.omni_light_buffer = RendererRD::LightStorage::get_singleton()->get_omni_light_buffer(); |
| 1358 | settings.spot_light_buffer = RendererRD::LightStorage::get_singleton()->get_spot_light_buffer(); |
| 1359 | settings.directional_shadow_depth = RendererRD::LightStorage::get_singleton()->directional_shadow_get_texture(); |
| 1360 | settings.directional_light_buffer = RendererRD::LightStorage::get_singleton()->get_directional_light_buffer(); |
| 1361 | |
| 1362 | settings.vfog = fog; |
| 1363 | settings.cluster_builder = rb_data->cluster_builder; |
| 1364 | settings.rbgi = rbgi; |
| 1365 | settings.sdfgi = sdfgi; |
| 1366 | settings.env = p_environment; |
| 1367 | settings.sky = &sky; |
| 1368 | settings.gi = &gi; |
| 1369 | |
| 1370 | 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); |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | /* Lighting */ |
| 1375 | |
| 1376 | void RenderForwardClustered::setup_added_reflection_probe(const Transform3D &p_transform, const Vector3 &p_half_size) { |
| 1377 | if (current_cluster_builder != nullptr) { |
| 1378 | current_cluster_builder->add_box(ClusterBuilderRD::BOX_TYPE_REFLECTION_PROBE, p_transform, p_half_size); |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | void RenderForwardClustered::setup_added_light(const RS::LightType p_type, const Transform3D &p_transform, float p_radius, float p_spot_aperture) { |
| 1383 | if (current_cluster_builder != nullptr) { |
| 1384 | 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); |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | void RenderForwardClustered::setup_added_decal(const Transform3D &p_transform, const Vector3 &p_half_size) { |
| 1389 | if (current_cluster_builder != nullptr) { |
| 1390 | current_cluster_builder->add_box(ClusterBuilderRD::BOX_TYPE_DECAL, p_transform, p_half_size); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | /* Render scene */ |
| 1395 | |
| 1396 | void RenderForwardClustered::_process_ssao(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_environment, const RID *p_normal_buffers, const Projection *p_projections) { |
| 1397 | ERR_FAIL_NULL(ss_effects); |
| 1398 | ERR_FAIL_COND(p_render_buffers.is_null()); |
| 1399 | ERR_FAIL_COND(p_environment.is_null()); |
| 1400 | |
| 1401 | Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 1402 | ERR_FAIL_COND(rb_data.is_null()); |
| 1403 | |
| 1404 | RENDER_TIMESTAMP("Process SSAO"); |
| 1405 | |
| 1406 | RendererRD::SSEffects::SSAOSettings settings; |
| 1407 | settings.radius = environment_get_ssao_radius(p_environment); |
| 1408 | settings.intensity = environment_get_ssao_intensity(p_environment); |
| 1409 | settings.power = environment_get_ssao_power(p_environment); |
| 1410 | settings.detail = environment_get_ssao_detail(p_environment); |
| 1411 | settings.horizon = environment_get_ssao_horizon(p_environment); |
| 1412 | settings.sharpness = environment_get_ssao_sharpness(p_environment); |
| 1413 | settings.full_screen_size = p_render_buffers->get_internal_size(); |
| 1414 | |
| 1415 | ss_effects->ssao_allocate_buffers(p_render_buffers, rb_data->ss_effects_data.ssao, settings); |
| 1416 | |
| 1417 | for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) { |
| 1418 | ss_effects->generate_ssao(p_render_buffers, rb_data->ss_effects_data.ssao, v, p_normal_buffers[v], p_projections[v], settings); |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | void RenderForwardClustered::_process_ssil(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_environment, const RID *p_normal_buffers, const Projection *p_projections, const Transform3D &p_transform) { |
| 1423 | ERR_FAIL_NULL(ss_effects); |
| 1424 | ERR_FAIL_COND(p_render_buffers.is_null()); |
| 1425 | ERR_FAIL_COND(p_environment.is_null()); |
| 1426 | |
| 1427 | Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 1428 | ERR_FAIL_COND(rb_data.is_null()); |
| 1429 | |
| 1430 | RENDER_TIMESTAMP("Process SSIL"); |
| 1431 | |
| 1432 | RendererRD::SSEffects::SSILSettings settings; |
| 1433 | settings.radius = environment_get_ssil_radius(p_environment); |
| 1434 | settings.intensity = environment_get_ssil_intensity(p_environment); |
| 1435 | settings.sharpness = environment_get_ssil_sharpness(p_environment); |
| 1436 | settings.normal_rejection = environment_get_ssil_normal_rejection(p_environment); |
| 1437 | settings.full_screen_size = p_render_buffers->get_internal_size(); |
| 1438 | |
| 1439 | ss_effects->ssil_allocate_buffers(p_render_buffers, rb_data->ss_effects_data.ssil, settings); |
| 1440 | |
| 1441 | Transform3D transform = p_transform; |
| 1442 | transform.set_origin(Vector3(0.0, 0.0, 0.0)); |
| 1443 | |
| 1444 | for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) { |
| 1445 | Projection correction; |
| 1446 | correction.set_depth_correction(true); |
| 1447 | Projection projection = correction * p_projections[v]; |
| 1448 | Projection last_frame_projection = rb_data->ss_effects_data.ssil_last_frame_projections[v] * Projection(rb_data->ss_effects_data.ssil_last_frame_transform.affine_inverse()) * Projection(transform) * projection.inverse(); |
| 1449 | |
| 1450 | 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); |
| 1451 | |
| 1452 | rb_data->ss_effects_data.ssil_last_frame_projections[v] = projection; |
| 1453 | } |
| 1454 | rb_data->ss_effects_data.ssil_last_frame_transform = transform; |
| 1455 | } |
| 1456 | |
| 1457 | void RenderForwardClustered::_process_ssr(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_environment, const RID *p_normal_slices, const Projection *p_projections, const Vector3 *p_eye_offsets, const Transform3D &p_transform) { |
| 1458 | ERR_FAIL_NULL(ss_effects); |
| 1459 | ERR_FAIL_COND(p_render_buffers.is_null()); |
| 1460 | |
| 1461 | Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 1462 | ERR_FAIL_COND(rb_data.is_null()); |
| 1463 | |
| 1464 | RENDER_TIMESTAMP("Process SSR"); |
| 1465 | |
| 1466 | ss_effects->ssr_allocate_buffers(p_render_buffers, rb_data->ss_effects_data.ssr, p_render_buffers->get_base_data_format()); |
| 1467 | |
| 1468 | Projection reprojections[RendererSceneRender::MAX_RENDER_VIEWS]; |
| 1469 | |
| 1470 | for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) { |
| 1471 | Projection correction; |
| 1472 | correction.set_depth_correction(true); |
| 1473 | |
| 1474 | Projection projection = correction * p_projections[v]; |
| 1475 | reprojections[v] = rb_data->ss_effects_data.ssr_last_frame_projections[v] * Projection(rb_data->ss_effects_data.ssr_last_frame_transform.affine_inverse()) * Projection(p_transform) * projection.inverse(); |
| 1476 | |
| 1477 | rb_data->ss_effects_data.ssr_last_frame_projections[v] = projection; |
| 1478 | } |
| 1479 | rb_data->ss_effects_data.ssr_last_frame_transform = p_transform; |
| 1480 | |
| 1481 | ss_effects->screen_space_reflection(p_render_buffers, rb_data->ss_effects_data.ssr, p_normal_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, reprojections, p_eye_offsets, *copy_effects); |
| 1482 | } |
| 1483 | |
| 1484 | void RenderForwardClustered::_copy_framebuffer_to_ss_effects(Ref<RenderSceneBuffersRD> p_render_buffers, bool p_use_ssil, bool p_use_ssr) { |
| 1485 | ERR_FAIL_NULL(ss_effects); |
| 1486 | ERR_FAIL_COND(p_render_buffers.is_null()); |
| 1487 | |
| 1488 | ss_effects->copy_internal_texture_to_last_frame(p_render_buffers, *copy_effects); |
| 1489 | } |
| 1490 | |
| 1491 | void RenderForwardClustered::_pre_opaque_render(RenderDataRD *p_render_data, bool p_use_ssao, bool p_use_ssil, bool p_use_ssr, bool p_use_gi, const RID *p_normal_roughness_slices, RID p_voxel_gi_buffer) { |
| 1492 | // Render shadows while GI is rendering, due to how barriers are handled, this should happen at the same time |
| 1493 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 1494 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 1495 | |
| 1496 | Ref<RenderSceneBuffersRD> rb = p_render_data->render_buffers; |
| 1497 | Ref<RenderBufferDataForwardClustered> rb_data; |
| 1498 | if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_FORWARD_CLUSTERED)) { |
| 1499 | // Our forward clustered custom data buffer will only be available when we're rendering our normal view. |
| 1500 | // This will not be available when rendering reflection probes. |
| 1501 | rb_data = rb->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 1502 | } |
| 1503 | |
| 1504 | if (rb.is_valid() && p_use_gi && rb->has_custom_data(RB_SCOPE_SDFGI)) { |
| 1505 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 1506 | sdfgi->store_probes(); |
| 1507 | } |
| 1508 | |
| 1509 | Size2i viewport_size = Size2i(1, 1); |
| 1510 | if (rb.is_valid()) { |
| 1511 | viewport_size = rb->get_internal_size(); |
| 1512 | } |
| 1513 | |
| 1514 | p_render_data->cube_shadows.clear(); |
| 1515 | p_render_data->shadows.clear(); |
| 1516 | p_render_data->directional_shadows.clear(); |
| 1517 | |
| 1518 | float lod_distance_multiplier = p_render_data->scene_data->cam_projection.get_lod_multiplier(); |
| 1519 | { |
| 1520 | for (int i = 0; i < p_render_data->render_shadow_count; i++) { |
| 1521 | RID li = p_render_data->render_shadows[i].light; |
| 1522 | RID base = light_storage->light_instance_get_base_light(li); |
| 1523 | |
| 1524 | if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) { |
| 1525 | p_render_data->directional_shadows.push_back(i); |
| 1526 | } else if (light_storage->light_get_type(base) == RS::LIGHT_OMNI && light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) { |
| 1527 | p_render_data->cube_shadows.push_back(i); |
| 1528 | } else { |
| 1529 | p_render_data->shadows.push_back(i); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | RENDER_TIMESTAMP("Render OmniLight Shadows"); |
| 1534 | // Cube shadows are rendered in their own way. |
| 1535 | for (const int &index : p_render_data->cube_shadows) { |
| 1536 | _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, lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, true, true, true, p_render_data->render_info, viewport_size, p_render_data->scene_data->cam_transform); |
| 1537 | } |
| 1538 | |
| 1539 | if (p_render_data->directional_shadows.size()) { |
| 1540 | //open the pass for directional shadows |
| 1541 | light_storage->update_directional_shadow_atlas(); |
| 1542 | RD::get_singleton()->draw_list_begin(light_storage->direction_shadow_get_fb(), RD::DRAW_CLEAR_DEPTH, Vector<Color>(), 0.0f); |
| 1543 | RD::get_singleton()->draw_list_end(); |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | // Render GI |
| 1548 | |
| 1549 | bool render_shadows = p_render_data->directional_shadows.size() || p_render_data->shadows.size(); |
| 1550 | bool render_gi = rb.is_valid() && p_use_gi; |
| 1551 | |
| 1552 | if (render_shadows && render_gi) { |
| 1553 | RENDER_TIMESTAMP("Render GI + Render Shadows (Parallel)"); |
| 1554 | } else if (render_shadows) { |
| 1555 | RENDER_TIMESTAMP("Render Shadows"); |
| 1556 | } else if (render_gi) { |
| 1557 | RENDER_TIMESTAMP("Render GI"); |
| 1558 | } |
| 1559 | |
| 1560 | //prepare shadow rendering |
| 1561 | if (render_shadows) { |
| 1562 | _render_shadow_begin(); |
| 1563 | |
| 1564 | //render directional shadows |
| 1565 | for (uint32_t i = 0; i < p_render_data->directional_shadows.size(); i++) { |
| 1566 | _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, 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, viewport_size, p_render_data->scene_data->cam_transform); |
| 1567 | } |
| 1568 | //render positional shadows |
| 1569 | for (uint32_t i = 0; i < p_render_data->shadows.size(); i++) { |
| 1570 | _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, 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, viewport_size, p_render_data->scene_data->cam_transform); |
| 1571 | } |
| 1572 | |
| 1573 | _render_shadow_process(); |
| 1574 | } |
| 1575 | |
| 1576 | if (render_gi) { |
| 1577 | 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); |
| 1578 | } |
| 1579 | |
| 1580 | if (render_shadows) { |
| 1581 | _render_shadow_end(); |
| 1582 | } |
| 1583 | |
| 1584 | if (rb_data.is_valid() && ss_effects) { |
| 1585 | // Note, in multiview we're allocating buffers for each eye/view we're rendering. |
| 1586 | // This should allow most of the processing to happen in parallel even if we're doing |
| 1587 | // drawcalls per eye/view. It will all sync up at the barrier. |
| 1588 | |
| 1589 | if (p_use_ssil || p_use_ssr) { |
| 1590 | ss_effects->allocate_last_frame_buffer(rb, p_use_ssil, p_use_ssr); |
| 1591 | } |
| 1592 | |
| 1593 | if (p_use_ssao || p_use_ssil) { |
| 1594 | RENDER_TIMESTAMP("Prepare Depth for SSAO/SSIL"); |
| 1595 | // Convert our depth buffer data to linear data in |
| 1596 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 1597 | ss_effects->downsample_depth(rb, v, p_render_data->scene_data->view_projection[v]); |
| 1598 | } |
| 1599 | |
| 1600 | if (p_use_ssao) { |
| 1601 | _process_ssao(rb, p_render_data->environment, p_normal_roughness_slices, p_render_data->scene_data->view_projection); |
| 1602 | } |
| 1603 | |
| 1604 | if (p_use_ssil) { |
| 1605 | _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); |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | if (p_use_ssr) { |
| 1610 | _process_ssr(rb, p_render_data->environment, p_normal_roughness_slices, p_render_data->scene_data->view_projection, p_render_data->scene_data->view_eye_offset, p_render_data->scene_data->cam_transform); |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | RENDER_TIMESTAMP("Pre Opaque Render"); |
| 1615 | |
| 1616 | if (current_cluster_builder) { |
| 1617 | // Note: when rendering stereoscopic (multiview) we are using our combined frustum projection to create |
| 1618 | // our cluster data. We use reprojection in the shader to adjust for our left/right eye. |
| 1619 | // This only works as we don't filter our cluster by depth buffer. |
| 1620 | // If we ever make this optimization we should make it optional and only use it in mono. |
| 1621 | // What we win by filtering out a few lights, we loose by having to do the work double for stereo. |
| 1622 | 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()); |
| 1623 | } |
| 1624 | |
| 1625 | bool using_shadows = true; |
| 1626 | |
| 1627 | if (p_render_data->reflection_probe.is_valid()) { |
| 1628 | if (!RSG::light_storage->reflection_probe_renders_shadows(light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe))) { |
| 1629 | using_shadows = false; |
| 1630 | } |
| 1631 | } else { |
| 1632 | //do not render reflections when rendering a reflection probe |
| 1633 | 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); |
| 1634 | } |
| 1635 | |
| 1636 | uint32_t directional_light_count = 0; |
| 1637 | uint32_t positional_light_count = 0; |
| 1638 | 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); |
| 1639 | texture_storage->update_decal_buffer(*p_render_data->decals, p_render_data->scene_data->cam_transform); |
| 1640 | |
| 1641 | p_render_data->directional_light_count = directional_light_count; |
| 1642 | |
| 1643 | if (current_cluster_builder) { |
| 1644 | current_cluster_builder->bake_cluster(); |
| 1645 | } |
| 1646 | |
| 1647 | if (rb_data.is_valid()) { |
| 1648 | RENDER_TIMESTAMP("Update Volumetric Fog"); |
| 1649 | bool directional_shadows = RendererRD::LightStorage::get_singleton()->has_directional_shadows(directional_light_count); |
| 1650 | _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); |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | void RenderForwardClustered::_process_sss(Ref<RenderSceneBuffersRD> p_render_buffers, const Projection &p_camera) { |
| 1655 | ERR_FAIL_COND(p_render_buffers.is_null()); |
| 1656 | |
| 1657 | Size2i internal_size = p_render_buffers->get_internal_size(); |
| 1658 | bool can_use_effects = internal_size.x >= 8 && internal_size.y >= 8; |
| 1659 | |
| 1660 | if (!can_use_effects) { |
| 1661 | //just copy |
| 1662 | return; |
| 1663 | } |
| 1664 | |
| 1665 | p_render_buffers->allocate_blur_textures(); |
| 1666 | |
| 1667 | for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) { |
| 1668 | RID internal_texture = p_render_buffers->get_internal_texture(v); |
| 1669 | RID depth_texture = p_render_buffers->get_depth_texture(v); |
| 1670 | ss_effects->sub_surface_scattering(p_render_buffers, internal_texture, depth_texture, p_camera, internal_size); |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Color &p_default_bg_color) { |
| 1675 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 1676 | |
| 1677 | ERR_FAIL_NULL(p_render_data); |
| 1678 | |
| 1679 | Ref<RenderSceneBuffersRD> rb = p_render_data->render_buffers; |
| 1680 | ERR_FAIL_COND(rb.is_null()); |
| 1681 | Ref<RenderBufferDataForwardClustered> rb_data; |
| 1682 | if (rb->has_custom_data(RB_SCOPE_FORWARD_CLUSTERED)) { |
| 1683 | // Our forward clustered custom data buffer will only be available when we're rendering our normal view. |
| 1684 | // This will not be available when rendering reflection probes. |
| 1685 | rb_data = rb->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 1686 | } |
| 1687 | bool is_reflection_probe = p_render_data->reflection_probe.is_valid(); |
| 1688 | |
| 1689 | static const int texture_multisamples[RS::VIEWPORT_MSAA_MAX] = { 1, 2, 4, 8 }; |
| 1690 | |
| 1691 | //first of all, make a new render pass |
| 1692 | //fill up ubo |
| 1693 | |
| 1694 | RENDER_TIMESTAMP("Prepare 3D Scene"); |
| 1695 | |
| 1696 | // get info about our rendering effects |
| 1697 | bool ce_needs_motion_vectors = _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS); |
| 1698 | bool ce_needs_normal_roughness = _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_NEEDS_ROUGHNESS); |
| 1699 | bool ce_needs_separate_specular = _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_NEEDS_SEPARATE_SPECULAR); |
| 1700 | |
| 1701 | // sdfgi first |
| 1702 | _update_sdfgi(p_render_data); |
| 1703 | |
| 1704 | // assign render indices to voxel_gi_instances |
| 1705 | for (uint32_t i = 0; i < (uint32_t)p_render_data->voxel_gi_instances->size(); i++) { |
| 1706 | RID voxel_gi_instance = (*p_render_data->voxel_gi_instances)[i]; |
| 1707 | gi.voxel_gi_instance_set_render_index(voxel_gi_instance, i); |
| 1708 | } |
| 1709 | |
| 1710 | // obtain cluster builder |
| 1711 | if (light_storage->owns_reflection_probe_instance(p_render_data->reflection_probe)) { |
| 1712 | current_cluster_builder = light_storage->reflection_probe_instance_get_cluster_builder(p_render_data->reflection_probe, &cluster_builder_shared); |
| 1713 | |
| 1714 | if (p_render_data->camera_attributes.is_valid()) { |
| 1715 | 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)); |
| 1716 | } |
| 1717 | } else if (rb_data.is_valid()) { |
| 1718 | current_cluster_builder = rb_data->cluster_builder; |
| 1719 | |
| 1720 | p_render_data->voxel_gi_count = 0; |
| 1721 | |
| 1722 | if (rb->has_custom_data(RB_SCOPE_SDFGI)) { |
| 1723 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 1724 | if (sdfgi.is_valid()) { |
| 1725 | sdfgi->update_cascades(); |
| 1726 | sdfgi->pre_process_gi(p_render_data->scene_data->cam_transform, p_render_data); |
| 1727 | sdfgi->update_light(); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | 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); |
| 1732 | } else { |
| 1733 | ERR_PRINT("No render buffer nor reflection atlas, bug"); // Should never happen! |
| 1734 | current_cluster_builder = nullptr; |
| 1735 | return; // No point in continuing, we'll just crash. |
| 1736 | } |
| 1737 | |
| 1738 | ERR_FAIL_NULL(current_cluster_builder); |
| 1739 | |
| 1740 | p_render_data->cluster_buffer = current_cluster_builder->get_cluster_buffer(); |
| 1741 | p_render_data->cluster_size = current_cluster_builder->get_cluster_size(); |
| 1742 | p_render_data->cluster_max_elements = current_cluster_builder->get_max_cluster_elements(); |
| 1743 | |
| 1744 | _update_vrs(rb); |
| 1745 | |
| 1746 | RENDER_TIMESTAMP("Setup 3D Scene"); |
| 1747 | |
| 1748 | bool using_debug_mvs = get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_MOTION_VECTORS; |
| 1749 | bool using_taa = rb->get_use_taa(); |
| 1750 | |
| 1751 | enum { |
| 1752 | SCALE_NONE, |
| 1753 | SCALE_FSR2, |
| 1754 | SCALE_MFX, |
| 1755 | } scale_type = SCALE_NONE; |
| 1756 | |
| 1757 | switch (rb->get_scaling_3d_mode()) { |
| 1758 | case RS::VIEWPORT_SCALING_3D_MODE_FSR2: |
| 1759 | scale_type = SCALE_FSR2; |
| 1760 | break; |
| 1761 | case RS::VIEWPORT_SCALING_3D_MODE_METALFX_TEMPORAL: |
| 1762 | #ifdef METAL_MFXTEMPORAL_ENABLED |
| 1763 | scale_type = SCALE_MFX; |
| 1764 | #else |
| 1765 | scale_type = SCALE_NONE; |
| 1766 | #endif |
| 1767 | break; |
| 1768 | default: |
| 1769 | break; |
| 1770 | } |
| 1771 | |
| 1772 | bool using_upscaling = scale_type != SCALE_NONE; |
| 1773 | |
| 1774 | // check if we need motion vectors |
| 1775 | bool motion_vectors_required; |
| 1776 | if (using_debug_mvs) { |
| 1777 | motion_vectors_required = true; |
| 1778 | } else if (ce_needs_motion_vectors) { |
| 1779 | motion_vectors_required = true; |
| 1780 | } else if (!is_reflection_probe && using_taa) { |
| 1781 | motion_vectors_required = true; |
| 1782 | } else if (!is_reflection_probe && using_upscaling) { |
| 1783 | motion_vectors_required = true; |
| 1784 | } else { |
| 1785 | motion_vectors_required = false; |
| 1786 | } |
| 1787 | |
| 1788 | //p_render_data->scene_data->subsurface_scatter_width = subsurface_scatter_size; |
| 1789 | p_render_data->scene_data->calculate_motion_vectors = motion_vectors_required; |
| 1790 | p_render_data->scene_data->directional_light_count = 0; |
| 1791 | p_render_data->scene_data->opaque_prepass_threshold = 0.99f; |
| 1792 | |
| 1793 | Size2i screen_size; |
| 1794 | RID color_framebuffer; |
| 1795 | RID color_only_framebuffer; |
| 1796 | RID depth_framebuffer; |
| 1797 | RendererRD::MaterialStorage::Samplers samplers; |
| 1798 | |
| 1799 | PassMode depth_pass_mode = PASS_MODE_DEPTH; |
| 1800 | uint32_t color_pass_flags = 0; |
| 1801 | Vector<Color> depth_pass_clear; |
| 1802 | bool using_separate_specular = false; |
| 1803 | bool using_ssr = false; |
| 1804 | bool using_sdfgi = false; |
| 1805 | bool using_voxelgi = false; |
| 1806 | bool reverse_cull = p_render_data->scene_data->cam_transform.basis.determinant() < 0; |
| 1807 | bool using_ssil = !is_reflection_probe && p_render_data->environment.is_valid() && environment_get_ssil_enabled(p_render_data->environment); |
| 1808 | bool using_motion_pass = rb_data.is_valid() && using_upscaling; |
| 1809 | |
| 1810 | if (is_reflection_probe) { |
| 1811 | uint32_t resolution = light_storage->reflection_probe_instance_get_resolution(p_render_data->reflection_probe); |
| 1812 | screen_size.x = resolution; |
| 1813 | screen_size.y = resolution; |
| 1814 | |
| 1815 | color_framebuffer = light_storage->reflection_probe_instance_get_framebuffer(p_render_data->reflection_probe, p_render_data->reflection_probe_pass); |
| 1816 | color_only_framebuffer = color_framebuffer; |
| 1817 | depth_framebuffer = light_storage->reflection_probe_instance_get_depth_framebuffer(p_render_data->reflection_probe, p_render_data->reflection_probe_pass); |
| 1818 | |
| 1819 | if (light_storage->reflection_probe_is_interior(light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe))) { |
| 1820 | p_render_data->environment = RID(); //no environment on interiors |
| 1821 | } |
| 1822 | |
| 1823 | reverse_cull = true; // for some reason our views are inverted |
| 1824 | samplers = RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default(); |
| 1825 | |
| 1826 | // Indicate pipelines for reflection probes are required. |
| 1827 | global_pipeline_data_required.use_reflection_probes = true; |
| 1828 | } else { |
| 1829 | screen_size = rb->get_internal_size(); |
| 1830 | |
| 1831 | if (p_render_data->scene_data->calculate_motion_vectors) { |
| 1832 | color_pass_flags |= COLOR_PASS_FLAG_MOTION_VECTORS; |
| 1833 | scene_shader.enable_advanced_shader_group(); |
| 1834 | |
| 1835 | // Indicate pipelines for motion vectors are required. |
| 1836 | global_pipeline_data_required.use_motion_vectors = true; |
| 1837 | } |
| 1838 | |
| 1839 | if (p_render_data->voxel_gi_instances->size() > 0) { |
| 1840 | using_voxelgi = true; |
| 1841 | } |
| 1842 | |
| 1843 | if (p_render_data->environment.is_valid()) { |
| 1844 | if (environment_get_sdfgi_enabled(p_render_data->environment) && get_debug_draw_mode() != RS::VIEWPORT_DEBUG_DRAW_UNSHADED) { |
| 1845 | using_sdfgi = true; |
| 1846 | } |
| 1847 | if (environment_get_ssr_enabled(p_render_data->environment)) { |
| 1848 | if (!p_render_data->transparent_bg) { |
| 1849 | using_ssr = true; |
| 1850 | } else { |
| 1851 | WARN_PRINT_ONCE("Screen-space reflections are not supported in viewports with a transparent background. Disabling SSR in transparent viewport."); |
| 1852 | } |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | if (p_render_data->scene_data->view_count > 1) { |
| 1857 | color_pass_flags |= COLOR_PASS_FLAG_MULTIVIEW; |
| 1858 | // Try enabling here in case is_xr_enabled() returns false. |
| 1859 | scene_shader.shader.enable_group(SceneShaderForwardClustered::SHADER_GROUP_MULTIVIEW); |
| 1860 | |
| 1861 | // Indicate pipelines for multiview are required. |
| 1862 | global_pipeline_data_required.use_multiview = true; |
| 1863 | } |
| 1864 | |
| 1865 | color_framebuffer = rb_data->get_color_pass_fb(color_pass_flags); |
| 1866 | color_only_framebuffer = rb_data->get_color_only_fb(); |
| 1867 | samplers = rb->get_samplers(); |
| 1868 | } |
| 1869 | |
| 1870 | p_render_data->scene_data->emissive_exposure_normalization = -1.0; |
| 1871 | |
| 1872 | RD::get_singleton()->draw_command_begin_label("Render Setup"); |
| 1873 | |
| 1874 | _setup_lightmaps(p_render_data, *p_render_data->lightmaps, p_render_data->scene_data->cam_transform); |
| 1875 | _setup_voxelgis(*p_render_data->voxel_gi_instances); |
| 1876 | _setup_environment(p_render_data, is_reflection_probe, screen_size, screen_size, p_default_bg_color, false); |
| 1877 | |
| 1878 | // May have changed due to the above (light buffer enlarged, as an example). |
| 1879 | _update_render_base_uniform_set(); |
| 1880 | |
| 1881 | _fill_render_list(RENDER_LIST_OPAQUE, p_render_data, PASS_MODE_COLOR, using_sdfgi, using_sdfgi || using_voxelgi, using_motion_pass); |
| 1882 | render_list[RENDER_LIST_OPAQUE].sort_by_key(); |
| 1883 | render_list[RENDER_LIST_MOTION].sort_by_key(); |
| 1884 | render_list[RENDER_LIST_ALPHA].sort_by_reverse_depth_and_priority(); |
| 1885 | |
| 1886 | int *render_info = p_render_data->render_info ? p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE] : (int *)nullptr; |
| 1887 | _fill_instance_data(RENDER_LIST_OPAQUE, render_info); |
| 1888 | _fill_instance_data(RENDER_LIST_MOTION, render_info); |
| 1889 | _fill_instance_data(RENDER_LIST_ALPHA, render_info); |
| 1890 | |
| 1891 | RD::get_singleton()->draw_command_end_label(); |
| 1892 | |
| 1893 | if (!is_reflection_probe) { |
| 1894 | if (using_voxelgi) { |
| 1895 | depth_pass_mode = PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI; |
| 1896 | } else if (p_render_data->environment.is_valid()) { |
| 1897 | if (using_ssr || |
| 1898 | using_sdfgi || |
| 1899 | environment_get_ssao_enabled(p_render_data->environment) || |
| 1900 | using_ssil || |
| 1901 | ce_needs_normal_roughness || |
| 1902 | get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_NORMAL_BUFFER || |
| 1903 | scene_state.used_normal_texture) { |
| 1904 | depth_pass_mode = PASS_MODE_DEPTH_NORMAL_ROUGHNESS; |
| 1905 | } |
| 1906 | } else if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_NORMAL_BUFFER || scene_state.used_normal_texture) { |
| 1907 | depth_pass_mode = PASS_MODE_DEPTH_NORMAL_ROUGHNESS; |
| 1908 | } |
| 1909 | |
| 1910 | switch (depth_pass_mode) { |
| 1911 | case PASS_MODE_DEPTH: { |
| 1912 | depth_framebuffer = rb_data->get_depth_fb(); |
| 1913 | } break; |
| 1914 | case PASS_MODE_DEPTH_NORMAL_ROUGHNESS: { |
| 1915 | depth_framebuffer = rb_data->get_depth_fb(RenderBufferDataForwardClustered::DEPTH_FB_ROUGHNESS); |
| 1916 | depth_pass_clear.push_back(Color(0, 0, 0, 0)); |
| 1917 | } break; |
| 1918 | case PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI: { |
| 1919 | depth_framebuffer = rb_data->get_depth_fb(RenderBufferDataForwardClustered::DEPTH_FB_ROUGHNESS_VOXELGI); |
| 1920 | depth_pass_clear.push_back(Color(0, 0, 0, 0)); |
| 1921 | depth_pass_clear.push_back(Color(0, 0, 0, 0)); |
| 1922 | } break; |
| 1923 | default: { |
| 1924 | }; |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | 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; |
| 1929 | |
| 1930 | if (using_sss && p_render_data->transparent_bg) { |
| 1931 | WARN_PRINT_ONCE("Sub-surface scattering is not supported in viewports with a transparent background. Disabling SSS in transparent viewport."); |
| 1932 | using_sss = false; |
| 1933 | } |
| 1934 | |
| 1935 | if ((using_sss || ce_needs_separate_specular) && !using_separate_specular) { |
| 1936 | using_separate_specular = true; |
| 1937 | color_pass_flags |= COLOR_PASS_FLAG_SEPARATE_SPECULAR; |
| 1938 | color_framebuffer = rb_data->get_color_pass_fb(color_pass_flags); |
| 1939 | } |
| 1940 | |
| 1941 | // Ensure this is allocated so we don't get a stutter the first time an object with SSS appears on screen. |
| 1942 | if (global_surface_data.sss_used && !is_reflection_probe) { |
| 1943 | rb_data->ensure_specular(); |
| 1944 | } |
| 1945 | |
| 1946 | if (global_surface_data.normal_texture_used && !is_reflection_probe) { |
| 1947 | rb_data->ensure_normal_roughness_texture(); |
| 1948 | } |
| 1949 | |
| 1950 | if (using_sss || using_separate_specular || scene_state.used_lightmap || using_voxelgi || global_surface_data.sss_used) { |
| 1951 | scene_shader.enable_advanced_shader_group(p_render_data->scene_data->view_count > 1); |
| 1952 | } |
| 1953 | |
| 1954 | // Update the global pipeline requirements with all the features found to be in use in this scene. |
| 1955 | if (depth_pass_mode == PASS_MODE_DEPTH_NORMAL_ROUGHNESS || global_surface_data.normal_texture_used) { |
| 1956 | global_pipeline_data_required.use_normal_and_roughness = true; |
| 1957 | } |
| 1958 | |
| 1959 | if (scene_state.used_lightmap || scene_state.lightmaps_used > 0) { |
| 1960 | global_pipeline_data_required.use_lightmaps = true; |
| 1961 | } |
| 1962 | |
| 1963 | if (using_voxelgi) { |
| 1964 | global_pipeline_data_required.use_voxelgi = true; |
| 1965 | } |
| 1966 | |
| 1967 | if (using_separate_specular || global_surface_data.sss_used) { |
| 1968 | global_pipeline_data_required.use_separate_specular = true; |
| 1969 | } |
| 1970 | |
| 1971 | // Update the compiled pipelines if any of the requirements have changed. |
| 1972 | _update_dirty_geometry_pipelines(); |
| 1973 | |
| 1974 | RID radiance_texture; |
| 1975 | bool draw_sky = false; |
| 1976 | bool draw_sky_fog_only = false; |
| 1977 | // We invert luminance_multiplier for sky so that we can combine it with exposure value. |
| 1978 | float sky_luminance_multiplier = 1.0 / rb->get_luminance_multiplier(); |
| 1979 | float sky_brightness_multiplier = 1.0; |
| 1980 | |
| 1981 | Color clear_color; |
| 1982 | bool load_color = false; |
| 1983 | |
| 1984 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { |
| 1985 | clear_color = Color(0, 0, 0, 1); //in overdraw mode, BG should always be black |
| 1986 | } else if (is_environment(p_render_data->environment)) { |
| 1987 | RS::EnvironmentBG bg_mode = environment_get_background(p_render_data->environment); |
| 1988 | float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment); |
| 1989 | bg_energy_multiplier *= environment_get_bg_intensity(p_render_data->environment); |
| 1990 | RS::EnvironmentReflectionSource reflection_source = environment_get_reflection_source(p_render_data->environment); |
| 1991 | |
| 1992 | if (p_render_data->camera_attributes.is_valid()) { |
| 1993 | bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes); |
| 1994 | } |
| 1995 | |
| 1996 | switch (bg_mode) { |
| 1997 | case RS::ENV_BG_CLEAR_COLOR: { |
| 1998 | clear_color = p_default_bg_color; |
| 1999 | clear_color.r *= bg_energy_multiplier; |
| 2000 | clear_color.g *= bg_energy_multiplier; |
| 2001 | clear_color.b *= bg_energy_multiplier; |
| 2002 | if (!p_render_data->transparent_bg && (rb->has_custom_data(RB_SCOPE_FOG) || environment_get_fog_enabled(p_render_data->environment))) { |
| 2003 | draw_sky_fog_only = true; |
| 2004 | RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear())); |
| 2005 | } |
| 2006 | } break; |
| 2007 | case RS::ENV_BG_COLOR: { |
| 2008 | clear_color = environment_get_bg_color(p_render_data->environment); |
| 2009 | clear_color.r *= bg_energy_multiplier; |
| 2010 | clear_color.g *= bg_energy_multiplier; |
| 2011 | clear_color.b *= bg_energy_multiplier; |
| 2012 | if (!p_render_data->transparent_bg && (rb->has_custom_data(RB_SCOPE_FOG) || environment_get_fog_enabled(p_render_data->environment))) { |
| 2013 | draw_sky_fog_only = true; |
| 2014 | RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear())); |
| 2015 | } |
| 2016 | } break; |
| 2017 | case RS::ENV_BG_SKY: { |
| 2018 | draw_sky = !p_render_data->transparent_bg; |
| 2019 | } break; |
| 2020 | case RS::ENV_BG_CANVAS: { |
| 2021 | if (!is_reflection_probe) { |
| 2022 | RID texture = RendererRD::TextureStorage::get_singleton()->render_target_get_rd_texture(rb->get_render_target()); |
| 2023 | bool convert_to_linear = !RendererRD::TextureStorage::get_singleton()->render_target_is_using_hdr(rb->get_render_target()); |
| 2024 | copy_effects->copy_to_fb_rect(texture, color_only_framebuffer, Rect2i(), false, false, false, false, RID(), false, false, convert_to_linear); |
| 2025 | } |
| 2026 | load_color = true; |
| 2027 | } break; |
| 2028 | case RS::ENV_BG_KEEP: { |
| 2029 | load_color = true; |
| 2030 | } break; |
| 2031 | case RS::ENV_BG_CAMERA_FEED: { |
| 2032 | } break; |
| 2033 | default: { |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | // setup sky if used for ambient, reflections, or background |
| 2038 | if (draw_sky || draw_sky_fog_only || (reflection_source == RS::ENV_REFLECTION_SOURCE_BG && bg_mode == RS::ENV_BG_SKY) || reflection_source == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(p_render_data->environment) == RS::ENV_AMBIENT_SOURCE_SKY) { |
| 2039 | RENDER_TIMESTAMP("Setup Sky"); |
| 2040 | RD::get_singleton()->draw_command_begin_label("Setup Sky"); |
| 2041 | |
| 2042 | // Setup our sky render information for this frame/viewport |
| 2043 | sky.setup_sky(p_render_data, screen_size); |
| 2044 | |
| 2045 | sky_brightness_multiplier *= bg_energy_multiplier; |
| 2046 | |
| 2047 | RID sky_rid = environment_get_sky(p_render_data->environment); |
| 2048 | if (sky_rid.is_valid()) { |
| 2049 | sky.update_radiance_buffers(rb, p_render_data->environment, p_render_data->scene_data->cam_transform.origin, time, sky_luminance_multiplier, sky_brightness_multiplier); |
| 2050 | radiance_texture = sky.sky_get_radiance_texture_rd(sky_rid); |
| 2051 | } else { |
| 2052 | // do not try to draw sky if invalid |
| 2053 | draw_sky = false; |
| 2054 | } |
| 2055 | |
| 2056 | if (draw_sky || draw_sky_fog_only) { |
| 2057 | // update sky half/quarter res buffers (if required) |
| 2058 | sky.update_res_buffers(rb, p_render_data->environment, time, sky_luminance_multiplier, sky_brightness_multiplier); |
| 2059 | } |
| 2060 | |
| 2061 | RD::get_singleton()->draw_command_end_label(); |
| 2062 | } |
| 2063 | } else { |
| 2064 | clear_color = p_default_bg_color; |
| 2065 | } |
| 2066 | |
| 2067 | RS::ViewportMSAA msaa = rb->get_msaa_3d(); |
| 2068 | bool use_msaa = msaa != RS::VIEWPORT_MSAA_DISABLED; |
| 2069 | |
| 2070 | bool ce_pre_opaque_resolved_color = use_msaa && _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_COLOR, RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_OPAQUE); |
| 2071 | bool ce_post_opaque_resolved_color = use_msaa && _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_COLOR, RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_POST_OPAQUE); |
| 2072 | bool ce_pre_transparent_resolved_color = use_msaa && _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_COLOR, RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_TRANSPARENT); |
| 2073 | |
| 2074 | bool ce_pre_opaque_resolved_depth = use_msaa && _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_DEPTH, RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_OPAQUE); |
| 2075 | bool ce_post_opaque_resolved_depth = use_msaa && _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_DEPTH, RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_POST_OPAQUE); |
| 2076 | bool ce_pre_transparent_resolved_depth = use_msaa && _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_DEPTH, RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_TRANSPARENT); |
| 2077 | |
| 2078 | 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; |
| 2079 | bool debug_sdfgi_probes = get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SDFGI_PROBES; |
| 2080 | bool force_depth_pre_pass = scene_state.used_opaque_stencil; |
| 2081 | bool depth_pre_pass = (force_depth_pre_pass || bool(GLOBAL_GET_CACHED(bool, "rendering/driver/depth_prepass/enable"))) && depth_framebuffer.is_valid(); |
| 2082 | |
| 2083 | SceneShaderForwardClustered::ShaderSpecialization base_specialization = scene_shader.default_specialization; |
| 2084 | base_specialization.use_depth_fog = p_render_data->environment.is_valid() && environment_get_fog_mode(p_render_data->environment) == RS::EnvironmentFogMode::ENV_FOG_MODE_DEPTH; |
| 2085 | |
| 2086 | bool using_ssao = depth_pre_pass && !is_reflection_probe && p_render_data->environment.is_valid() && environment_get_ssao_enabled(p_render_data->environment); |
| 2087 | |
| 2088 | if (depth_pre_pass) { //depth pre pass |
| 2089 | bool needs_pre_resolve = _needs_post_prepass_render(p_render_data, using_sdfgi || using_voxelgi); |
| 2090 | if (needs_pre_resolve) { |
| 2091 | RENDER_TIMESTAMP("GI + Render Depth Pre-Pass (Parallel)"); |
| 2092 | } else { |
| 2093 | RENDER_TIMESTAMP("Render Depth Pre-Pass"); |
| 2094 | } |
| 2095 | if (needs_pre_resolve) { |
| 2096 | //pre clear the depth framebuffer, as AMD (and maybe others?) use compute for it, and barrier other compute shaders. |
| 2097 | RD::get_singleton()->draw_list_begin(depth_framebuffer, RD::DRAW_CLEAR_ALL, depth_pass_clear, 0.0f); |
| 2098 | RD::get_singleton()->draw_list_end(); |
| 2099 | //start compute processes here, so they run at the same time as depth pre-pass |
| 2100 | _post_prepass_render(p_render_data, using_sdfgi || using_voxelgi); |
| 2101 | } |
| 2102 | |
| 2103 | RD::get_singleton()->draw_command_begin_label("Render Depth Pre-Pass"); |
| 2104 | |
| 2105 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_OPAQUE, nullptr, RID(), samplers); |
| 2106 | |
| 2107 | bool finish_depth = using_ssao || using_ssil || using_sdfgi || using_voxelgi || ce_pre_opaque_resolved_depth || ce_post_opaque_resolved_depth; |
| 2108 | 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, 0, base_specialization); |
| 2109 | _render_list_with_draw_list(&render_list_params, depth_framebuffer, RD::DrawFlags(needs_pre_resolve ? RD::DRAW_DEFAULT_ALL : RD::DRAW_CLEAR_ALL), depth_pass_clear, 0.0f, 0u, p_render_data->render_region); |
| 2110 | |
| 2111 | RD::get_singleton()->draw_command_end_label(); |
| 2112 | |
| 2113 | if (use_msaa) { |
| 2114 | RENDER_TIMESTAMP("Resolve Depth Pre-Pass (MSAA)"); |
| 2115 | RD::get_singleton()->draw_command_begin_label("Resolve Depth Pre-Pass (MSAA)"); |
| 2116 | if (depth_pass_mode == PASS_MODE_DEPTH_NORMAL_ROUGHNESS || depth_pass_mode == PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI) { |
| 2117 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2118 | 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[msaa]); |
| 2119 | } |
| 2120 | } else if (finish_depth) { |
| 2121 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2122 | resolve_effects->resolve_depth(rb->get_depth_msaa(v), rb->get_depth_texture(v), rb->get_internal_size(), texture_multisamples[msaa]); |
| 2123 | } |
| 2124 | } |
| 2125 | RD::get_singleton()->draw_command_end_label(); |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | { |
| 2130 | if (ce_pre_opaque_resolved_color) { |
| 2131 | // We haven't rendered color data yet so... |
| 2132 | WARN_PRINT_ONCE("Pre opaque rendering effects can't access resolved color buffers."); |
| 2133 | } |
| 2134 | |
| 2135 | if (ce_pre_opaque_resolved_depth && !depth_pre_pass) { |
| 2136 | // We haven't rendered depth data yet so... |
| 2137 | WARN_PRINT_ONCE("Pre opaque rendering effects can't access resolved depth buffers."); |
| 2138 | } |
| 2139 | |
| 2140 | RENDER_TIMESTAMP("Process Pre Opaque Compositor Effects"); |
| 2141 | _process_compositor_effects(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_OPAQUE, p_render_data); |
| 2142 | } |
| 2143 | |
| 2144 | RID normal_roughness_views[RendererSceneRender::MAX_RENDER_VIEWS]; |
| 2145 | if (rb_data.is_valid() && rb_data->has_normal_roughness()) { |
| 2146 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2147 | normal_roughness_views[v] = rb_data->get_normal_roughness(v); |
| 2148 | } |
| 2149 | } |
| 2150 | _pre_opaque_render(p_render_data, using_ssao, using_ssil, using_ssr, using_sdfgi || using_voxelgi, normal_roughness_views, rb_data.is_valid() && rb_data->has_voxelgi() ? rb_data->get_voxelgi() : RID()); |
| 2151 | |
| 2152 | RENDER_TIMESTAMP("Render Opaque Pass"); |
| 2153 | |
| 2154 | RD::get_singleton()->draw_command_begin_label("Render Opaque Pass"); |
| 2155 | |
| 2156 | p_render_data->scene_data->directional_light_count = p_render_data->directional_light_count; |
| 2157 | p_render_data->scene_data->opaque_prepass_threshold = 0.0f; |
| 2158 | |
| 2159 | // Shadow pass can change the base uniform set samplers. |
| 2160 | _update_render_base_uniform_set(); |
| 2161 | |
| 2162 | _setup_environment(p_render_data, is_reflection_probe, screen_size, screen_size, p_default_bg_color, true, using_motion_pass); |
| 2163 | |
| 2164 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_OPAQUE, p_render_data, radiance_texture, samplers, true); |
| 2165 | |
| 2166 | { |
| 2167 | bool render_motion_pass = !render_list[RENDER_LIST_MOTION].elements.is_empty(); |
| 2168 | |
| 2169 | { |
| 2170 | Vector<Color> c; |
| 2171 | if (!load_color) { |
| 2172 | Color cc = clear_color.srgb_to_linear(); |
| 2173 | if (using_separate_specular || rb_data.is_valid()) { |
| 2174 | // Effects that rely on separate specular, like subsurface scattering, must clear the alpha to zero. |
| 2175 | cc.a = 0; |
| 2176 | } |
| 2177 | c.push_back(cc); |
| 2178 | |
| 2179 | if (rb_data.is_valid()) { |
| 2180 | c.push_back(Color(0, 0, 0, 0)); // Separate specular. |
| 2181 | c.push_back(Color(0, 0, 0, 0)); // Motion vector. Pushed to the clear color vector even if the framebuffer isn't bound. |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | uint32_t opaque_color_pass_flags = using_motion_pass ? (color_pass_flags & ~uint32_t(COLOR_PASS_FLAG_MOTION_VECTORS)) : color_pass_flags; |
| 2186 | RID opaque_framebuffer = using_motion_pass ? rb_data->get_color_pass_fb(opaque_color_pass_flags) : color_framebuffer; |
| 2187 | 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, opaque_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, 0, base_specialization); |
| 2188 | _render_list_with_draw_list(&render_list_params, opaque_framebuffer, RD::DrawFlags(load_color ? RD::DRAW_DEFAULT_ALL : RD::DRAW_CLEAR_COLOR_ALL) | (depth_pre_pass ? RD::DRAW_DEFAULT_ALL : RD::DRAW_CLEAR_DEPTH), c, 0.0f, 0u, p_render_data->render_region); |
| 2189 | } |
| 2190 | |
| 2191 | RD::get_singleton()->draw_command_end_label(); |
| 2192 | |
| 2193 | if (using_motion_pass) { |
| 2194 | if (scale_type == SCALE_MFX) { |
| 2195 | motion_vectors_store->process(rb, |
| 2196 | p_render_data->scene_data->cam_projection, p_render_data->scene_data->cam_transform, |
| 2197 | p_render_data->scene_data->prev_cam_projection, p_render_data->scene_data->prev_cam_transform); |
| 2198 | } else { |
| 2199 | Vector<Color> motion_vector_clear_colors; |
| 2200 | motion_vector_clear_colors.push_back(Color(-1, -1, 0, 0)); |
| 2201 | RD::get_singleton()->draw_list_begin(rb_data->get_velocity_only_fb(), RD::DRAW_CLEAR_ALL, motion_vector_clear_colors); |
| 2202 | RD::get_singleton()->draw_list_end(); |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | if (render_motion_pass) { |
| 2207 | RD::get_singleton()->draw_command_begin_label("Render Motion Pass"); |
| 2208 | |
| 2209 | RENDER_TIMESTAMP("Render Motion Pass"); |
| 2210 | |
| 2211 | rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_MOTION, p_render_data, radiance_texture, samplers, true); |
| 2212 | |
| 2213 | RenderListParameters render_list_params(render_list[RENDER_LIST_MOTION].elements.ptr(), render_list[RENDER_LIST_MOTION].element_info.ptr(), render_list[RENDER_LIST_MOTION].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, 0, base_specialization); |
| 2214 | _render_list_with_draw_list(&render_list_params, color_framebuffer); |
| 2215 | |
| 2216 | RD::get_singleton()->draw_command_end_label(); |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | { |
| 2221 | if (ce_post_opaque_resolved_color) { |
| 2222 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2223 | RD::get_singleton()->texture_resolve_multisample(rb->get_color_msaa(v), rb->get_internal_texture(v)); |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | if (ce_post_opaque_resolved_depth) { |
| 2228 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2229 | resolve_effects->resolve_depth(rb->get_depth_msaa(v), rb->get_depth_texture(v), rb->get_internal_size(), texture_multisamples[msaa]); |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | RENDER_TIMESTAMP("Process Post Opaque Compositor Effects"); |
| 2234 | _process_compositor_effects(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_POST_OPAQUE, p_render_data); |
| 2235 | } |
| 2236 | |
| 2237 | if (debug_voxelgis) { |
| 2238 | Projection dc; |
| 2239 | dc.set_depth_correction(true); |
| 2240 | Projection cm = (dc * p_render_data->scene_data->cam_projection) * Projection(p_render_data->scene_data->cam_transform.affine_inverse()); |
| 2241 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(color_only_framebuffer); |
| 2242 | RD::get_singleton()->draw_command_begin_label("Debug VoxelGIs"); |
| 2243 | for (int i = 0; i < (int)p_render_data->voxel_gi_instances->size(); i++) { |
| 2244 | 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); |
| 2245 | } |
| 2246 | RD::get_singleton()->draw_command_end_label(); |
| 2247 | RD::get_singleton()->draw_list_end(); |
| 2248 | } |
| 2249 | |
| 2250 | if (debug_sdfgi_probes) { |
| 2251 | Projection dc; |
| 2252 | dc.set_depth_correction(true); |
| 2253 | Projection cms[RendererSceneRender::MAX_RENDER_VIEWS]; |
| 2254 | for (uint32_t v = 0; v < p_render_data->scene_data->view_count; v++) { |
| 2255 | cms[v] = (dc * p_render_data->scene_data->view_projection[v]) * Projection(p_render_data->scene_data->cam_transform.affine_inverse()); |
| 2256 | } |
| 2257 | _debug_sdfgi_probes(rb, color_only_framebuffer, p_render_data->scene_data->view_count, cms); |
| 2258 | } |
| 2259 | |
| 2260 | if (draw_sky || draw_sky_fog_only) { |
| 2261 | RENDER_TIMESTAMP("Render Sky"); |
| 2262 | |
| 2263 | RD::get_singleton()->draw_command_begin_label("Draw Sky"); |
| 2264 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(color_only_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0u, p_render_data->render_region); |
| 2265 | |
| 2266 | sky.draw_sky(draw_list, rb, p_render_data->environment, color_only_framebuffer, time, sky_luminance_multiplier, sky_brightness_multiplier); |
| 2267 | |
| 2268 | RD::get_singleton()->draw_list_end(); |
| 2269 | RD::get_singleton()->draw_command_end_label(); |
| 2270 | } |
| 2271 | |
| 2272 | if (use_msaa) { |
| 2273 | RENDER_TIMESTAMP("Resolve MSAA"); |
| 2274 | |
| 2275 | if (scene_state.used_screen_texture || using_separate_specular || ce_pre_transparent_resolved_color) { |
| 2276 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2277 | RD::get_singleton()->texture_resolve_multisample(rb->get_color_msaa(v), rb->get_internal_texture(v)); |
| 2278 | } |
| 2279 | if (using_separate_specular) { |
| 2280 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2281 | RD::get_singleton()->texture_resolve_multisample(rb_data->get_specular_msaa(v), rb_data->get_specular(v)); |
| 2282 | } |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | if (scene_state.used_depth_texture || scene_state.used_normal_texture || using_separate_specular || ce_needs_normal_roughness || ce_pre_transparent_resolved_depth) { |
| 2287 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2288 | resolve_effects->resolve_depth(rb->get_depth_msaa(v), rb->get_depth_texture(v), rb->get_internal_size(), texture_multisamples[msaa]); |
| 2289 | } |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | { |
| 2294 | RENDER_TIMESTAMP("Process Post Sky Compositor Effects"); |
| 2295 | // Don't need to check for depth or color resolve here, we've already triggered it. |
| 2296 | _process_compositor_effects(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_POST_SKY, p_render_data); |
| 2297 | } |
| 2298 | |
| 2299 | if (using_separate_specular) { |
| 2300 | if (using_sss) { |
| 2301 | RENDER_TIMESTAMP("Sub-Surface Scattering"); |
| 2302 | RD::get_singleton()->draw_command_begin_label("Process Sub-Surface Scattering"); |
| 2303 | _process_sss(rb, p_render_data->scene_data->cam_projection); |
| 2304 | RD::get_singleton()->draw_command_end_label(); |
| 2305 | } |
| 2306 | |
| 2307 | { |
| 2308 | //just mix specular back |
| 2309 | RENDER_TIMESTAMP("Merge Specular"); |
| 2310 | copy_effects->merge_specular(color_only_framebuffer, rb_data->get_specular(), !use_msaa ? RID() : rb->get_internal_texture(), RID(), p_render_data->scene_data->view_count); |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | if (using_separate_specular && is_environment(p_render_data->environment) && (environment_get_background(p_render_data->environment) == RS::ENV_BG_CANVAS)) { |
| 2315 | // 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, |
| 2316 | // 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. |
| 2317 | RENDER_TIMESTAMP("Clear Separate Specular (Canvas Background Mode)"); |
| 2318 | Vector<Color> blank_clear_color; |
| 2319 | blank_clear_color.push_back(Color(0.0, 0.0, 0.0)); |
| 2320 | RD::get_singleton()->draw_list_begin(rb_data->get_specular_only_fb(), RD::DRAW_CLEAR_ALL, blank_clear_color); |
| 2321 | RD::get_singleton()->draw_list_end(); |
| 2322 | } |
| 2323 | |
| 2324 | if (rb_data.is_valid() && using_upscaling) { |
| 2325 | // Make sure the upscaled texture is initialized, but not necessarily filled, before running screen copies |
| 2326 | // so it properly detect if a dedicated copy texture should be used. |
| 2327 | rb->ensure_upscaled(); |
| 2328 | } |
| 2329 | |
| 2330 | if (scene_state.used_screen_texture || global_surface_data.screen_texture_used) { |
| 2331 | RENDER_TIMESTAMP("Copy Screen Texture"); |
| 2332 | |
| 2333 | _render_buffers_ensure_screen_texture(p_render_data); |
| 2334 | |
| 2335 | if (scene_state.used_screen_texture) { |
| 2336 | // Copy screen texture to backbuffer so we can read from it |
| 2337 | _render_buffers_copy_screen_texture(p_render_data); |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | if (scene_state.used_depth_texture || global_surface_data.depth_texture_used) { |
| 2342 | RENDER_TIMESTAMP("Copy Depth Texture"); |
| 2343 | |
| 2344 | _render_buffers_ensure_depth_texture(p_render_data); |
| 2345 | |
| 2346 | if (scene_state.used_depth_texture) { |
| 2347 | // Copy depth texture to backbuffer so we can read from it |
| 2348 | _render_buffers_copy_depth_texture(p_render_data); |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | { |
| 2353 | if (using_separate_specular) { |
| 2354 | // Our specular will be combined back in (and effects, subsurface scattering and/or ssr applied), |
| 2355 | // so if we've requested this, we need another copy. |
| 2356 | // Fairly unlikely scenario though. |
| 2357 | |
| 2358 | if (ce_pre_transparent_resolved_color) { |
| 2359 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2360 | RD::get_singleton()->texture_resolve_multisample(rb->get_color_msaa(v), rb->get_internal_texture(v)); |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | if (ce_pre_transparent_resolved_depth) { |
| 2365 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2366 | resolve_effects->resolve_depth(rb->get_depth_msaa(v), rb->get_depth_texture(v), rb->get_internal_size(), texture_multisamples[msaa]); |
| 2367 | } |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | RENDER_TIMESTAMP("Process Pre Transparent Compositor Effects"); |
| 2372 | _process_compositor_effects(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_TRANSPARENT, p_render_data); |
| 2373 | } |
| 2374 | |
| 2375 | RENDER_TIMESTAMP("Render 3D Transparent Pass"); |
| 2376 | |
| 2377 | RD::get_singleton()->draw_command_begin_label("Render 3D Transparent Pass"); |
| 2378 | |
| 2379 | rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_ALPHA, p_render_data, radiance_texture, samplers, true); |
| 2380 | |
| 2381 | _setup_environment(p_render_data, is_reflection_probe, screen_size, screen_size, p_default_bg_color, false); |
| 2382 | |
| 2383 | { |
| 2384 | uint32_t transparent_color_pass_flags = (color_pass_flags | uint32_t(COLOR_PASS_FLAG_TRANSPARENT)) & ~uint32_t(COLOR_PASS_FLAG_SEPARATE_SPECULAR); |
| 2385 | // Motion vectors should not be overwritten by transparent objects. |
| 2386 | transparent_color_pass_flags &= ~uint32_t(COLOR_PASS_FLAG_MOTION_VECTORS); |
| 2387 | |
| 2388 | RID alpha_framebuffer = rb_data.is_valid() ? rb_data->get_color_pass_fb(transparent_color_pass_flags) : color_only_framebuffer; |
| 2389 | 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(), reverse_cull, 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, 0, base_specialization); |
| 2390 | _render_list_with_draw_list(&render_list_params, alpha_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 0.0f, 0u, p_render_data->render_region); |
| 2391 | } |
| 2392 | |
| 2393 | RD::get_singleton()->draw_command_end_label(); |
| 2394 | |
| 2395 | RENDER_TIMESTAMP("Resolve"); |
| 2396 | |
| 2397 | RD::get_singleton()->draw_command_begin_label("Resolve"); |
| 2398 | |
| 2399 | if (rb_data.is_valid() && use_msaa) { |
| 2400 | bool resolve_velocity_buffer = (using_taa || using_upscaling || ce_needs_motion_vectors) && rb->has_velocity_buffer(true); |
| 2401 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2402 | RD::get_singleton()->texture_resolve_multisample(rb->get_color_msaa(v), rb->get_internal_texture(v)); |
| 2403 | resolve_effects->resolve_depth(rb->get_depth_msaa(v), rb->get_depth_texture(v), rb->get_internal_size(), texture_multisamples[msaa]); |
| 2404 | |
| 2405 | if (resolve_velocity_buffer) { |
| 2406 | RD::get_singleton()->texture_resolve_multisample(rb->get_velocity_buffer(true, v), rb->get_velocity_buffer(false, v)); |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | RD::get_singleton()->draw_command_end_label(); |
| 2412 | |
| 2413 | RD::get_singleton()->draw_command_begin_label("Copy Framebuffer for SSIL/SSR"); |
| 2414 | if (using_ssil || using_ssr) { |
| 2415 | RENDER_TIMESTAMP("Copy Final Framebuffer (SSIL/SSR)"); |
| 2416 | _copy_framebuffer_to_ss_effects(rb, using_ssil, using_ssr); |
| 2417 | } |
| 2418 | RD::get_singleton()->draw_command_end_label(); |
| 2419 | |
| 2420 | { |
| 2421 | RENDER_TIMESTAMP("Process Post Transparent Compositor Effects"); |
| 2422 | _process_compositor_effects(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_POST_TRANSPARENT, p_render_data); |
| 2423 | } |
| 2424 | |
| 2425 | if (rb_data.is_valid() && (using_upscaling || using_taa)) { |
| 2426 | if (scale_type == SCALE_FSR2) { |
| 2427 | rb_data->ensure_fsr2(fsr2_effect); |
| 2428 | |
| 2429 | RID exposure; |
| 2430 | if (RSG::camera_attributes->camera_attributes_uses_auto_exposure(p_render_data->camera_attributes)) { |
| 2431 | exposure = luminance->get_current_luminance_buffer(rb); |
| 2432 | } |
| 2433 | |
| 2434 | RD::get_singleton()->draw_command_begin_label("FSR2"); |
| 2435 | RENDER_TIMESTAMP("FSR2"); |
| 2436 | |
| 2437 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2438 | real_t fov = p_render_data->scene_data->cam_projection.get_fov(); |
| 2439 | real_t aspect = p_render_data->scene_data->cam_projection.get_aspect(); |
| 2440 | real_t fovy = p_render_data->scene_data->cam_projection.get_fovy(fov, 1.0 / aspect); |
| 2441 | Vector2 jitter = p_render_data->scene_data->taa_jitter * Vector2(rb->get_internal_size()) * 0.5f; |
| 2442 | RendererRD::FSR2Effect::Parameters params; |
| 2443 | params.context = rb_data->get_fsr2_context(); |
| 2444 | params.internal_size = rb->get_internal_size(); |
| 2445 | params.sharpness = CLAMP(1.0f - (rb->get_fsr_sharpness() / 2.0f), 0.0f, 1.0f); |
| 2446 | params.color = rb->get_internal_texture(v); |
| 2447 | params.depth = rb->get_depth_texture(v); |
| 2448 | params.velocity = rb->get_velocity_buffer(false, v); |
| 2449 | params.reactive = rb->get_internal_texture_reactive(v); |
| 2450 | params.exposure = exposure; |
| 2451 | params.output = rb->get_upscaled_texture(v); |
| 2452 | params.z_near = p_render_data->scene_data->z_near; |
| 2453 | params.z_far = p_render_data->scene_data->z_far; |
| 2454 | params.fovy = fovy; |
| 2455 | params.jitter = jitter; |
| 2456 | params.delta_time = float(time_step); |
| 2457 | params.reset_accumulation = false; // FIXME: The engine does not provide a way to reset the accumulation. |
| 2458 | |
| 2459 | Projection correction; |
| 2460 | correction.set_depth_correction(true, true, false); |
| 2461 | |
| 2462 | const Projection &prev_proj = p_render_data->scene_data->prev_cam_projection; |
| 2463 | const Projection &cur_proj = p_render_data->scene_data->cam_projection; |
| 2464 | const Transform3D &prev_transform = p_render_data->scene_data->prev_cam_transform; |
| 2465 | const Transform3D &cur_transform = p_render_data->scene_data->cam_transform; |
| 2466 | params.reprojection = (correction * prev_proj) * prev_transform.affine_inverse() * cur_transform * (correction * cur_proj).inverse(); |
| 2467 | |
| 2468 | fsr2_effect->upscale(params); |
| 2469 | } |
| 2470 | |
| 2471 | RD::get_singleton()->draw_command_end_label(); |
| 2472 | } else if (scale_type == SCALE_MFX) { |
| 2473 | #ifdef METAL_MFXTEMPORAL_ENABLED |
| 2474 | bool reset = rb_data->ensure_mfx_temporal(mfx_temporal_effect); |
| 2475 | |
| 2476 | RID exposure; |
| 2477 | if (RSG::camera_attributes->camera_attributes_uses_auto_exposure(p_render_data->camera_attributes)) { |
| 2478 | exposure = luminance->get_current_luminance_buffer(rb); |
| 2479 | } |
| 2480 | |
| 2481 | RD::get_singleton()->draw_command_begin_label("MetalFX Temporal"); |
| 2482 | // Scale to ±0.5. |
| 2483 | Vector2 jitter = p_render_data->scene_data->taa_jitter * 0.5f; |
| 2484 | jitter *= Vector2(1.0, -1.0); // Flip y-axis as bottom left is origin. |
| 2485 | |
| 2486 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2487 | RendererRD::MFXTemporalEffect::Params params; |
| 2488 | params.src = rb->get_internal_texture(v); |
| 2489 | params.depth = rb->get_depth_texture(v); |
| 2490 | params.motion = rb->get_velocity_buffer(false, v); |
| 2491 | params.exposure = exposure; |
| 2492 | params.dst = rb->get_upscaled_texture(v); |
| 2493 | params.jitter_offset = jitter; |
| 2494 | params.reset = reset; |
| 2495 | |
| 2496 | mfx_temporal_effect->process(rb_data->get_mfx_temporal_context(), params); |
| 2497 | } |
| 2498 | |
| 2499 | RD::get_singleton()->draw_command_end_label(); |
| 2500 | #endif |
| 2501 | } else if (using_taa) { |
| 2502 | RD::get_singleton()->draw_command_begin_label("TAA"); |
| 2503 | RENDER_TIMESTAMP("TAA"); |
| 2504 | taa->process(rb, rb->get_base_data_format(), p_render_data->scene_data->z_near, p_render_data->scene_data->z_far); |
| 2505 | RD::get_singleton()->draw_command_end_label(); |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | if (rb_data.is_valid()) { |
| 2510 | _debug_draw_cluster(rb); |
| 2511 | |
| 2512 | RENDER_TIMESTAMP("Tonemap"); |
| 2513 | |
| 2514 | _render_buffers_post_process_and_tonemap(p_render_data); |
| 2515 | } |
| 2516 | |
| 2517 | if (rb_data.is_valid()) { |
| 2518 | _render_buffers_debug_draw(p_render_data); |
| 2519 | |
| 2520 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SDFGI && rb->has_custom_data(RB_SCOPE_SDFGI)) { |
| 2521 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 2522 | Vector<RID> view_rids; |
| 2523 | |
| 2524 | // SDFGI renders at internal resolution, need to check if our debug correctly supports outputting upscaled. |
| 2525 | Size2i size = rb->get_internal_size(); |
| 2526 | RID source_texture = rb->get_internal_texture(); |
| 2527 | for (uint32_t v = 0; v < rb->get_view_count(); v++) { |
| 2528 | view_rids.push_back(rb->get_internal_texture(v)); |
| 2529 | } |
| 2530 | |
| 2531 | 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); |
| 2532 | } |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | void RenderForwardClustered::_render_buffers_debug_draw(const RenderDataRD *p_render_data) { |
| 2537 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 2538 | |
| 2539 | Ref<RenderSceneBuffersRD> rb = p_render_data->render_buffers; |
| 2540 | ERR_FAIL_COND(rb.is_null()); |
| 2541 | |
| 2542 | Ref<RenderBufferDataForwardClustered> rb_data = rb->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 2543 | ERR_FAIL_COND(rb_data.is_null()); |
| 2544 | |
| 2545 | RendererSceneRenderRD::_render_buffers_debug_draw(p_render_data); |
| 2546 | |
| 2547 | RID render_target = rb->get_render_target(); |
| 2548 | |
| 2549 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SSAO && rb->has_texture(RB_SCOPE_SSAO, RB_FINAL)) { |
| 2550 | RID final = rb->get_texture_slice(RB_SCOPE_SSAO, RB_FINAL, 0, 0); |
| 2551 | Size2i rtsize = texture_storage->render_target_get_size(render_target); |
| 2552 | copy_effects->copy_to_fb_rect(final, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, true); |
| 2553 | } |
| 2554 | |
| 2555 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SSIL && rb->has_texture(RB_SCOPE_SSIL, RB_FINAL)) { |
| 2556 | RID final = rb->get_texture_slice(RB_SCOPE_SSIL, RB_FINAL, 0, 0); |
| 2557 | Size2i rtsize = texture_storage->render_target_get_size(render_target); |
| 2558 | copy_effects->copy_to_fb_rect(final, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, false); |
| 2559 | } |
| 2560 | |
| 2561 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_GI_BUFFER && rb->has_texture(RB_SCOPE_GI, RB_TEX_AMBIENT)) { |
| 2562 | Size2i rtsize = texture_storage->render_target_get_size(render_target); |
| 2563 | RID ambient_texture = rb->get_texture(RB_SCOPE_GI, RB_TEX_AMBIENT); |
| 2564 | RID reflection_texture = rb->get_texture(RB_SCOPE_GI, RB_TEX_REFLECTION); |
| 2565 | 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); |
| 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | void RenderForwardClustered::_render_shadow_pass(RID p_light, RID p_shadow_atlas, int p_pass, const PagedArray<RenderGeometryInstance *> &p_instances, 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, const Size2i &p_viewport_size, const Transform3D &p_main_cam_transform) { |
| 2570 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 2571 | |
| 2572 | ERR_FAIL_COND(!light_storage->owns_light_instance(p_light)); |
| 2573 | |
| 2574 | RID base = light_storage->light_instance_get_base_light(p_light); |
| 2575 | |
| 2576 | Rect2i atlas_rect; |
| 2577 | uint32_t atlas_size = 1; |
| 2578 | RID atlas_fb; |
| 2579 | |
| 2580 | bool reverse_cull_face = light_storage->light_get_reverse_cull_face_mode(base); |
| 2581 | bool using_dual_paraboloid = false; |
| 2582 | bool using_dual_paraboloid_flip = false; |
| 2583 | Vector2i dual_paraboloid_offset; |
| 2584 | RID render_fb; |
| 2585 | RID render_texture; |
| 2586 | float zfar; |
| 2587 | |
| 2588 | bool use_pancake = false; |
| 2589 | bool render_cubemap = false; |
| 2590 | bool finalize_cubemap = false; |
| 2591 | |
| 2592 | bool flip_y = false; |
| 2593 | |
| 2594 | Projection light_projection; |
| 2595 | Transform3D light_transform; |
| 2596 | |
| 2597 | if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) { |
| 2598 | //set pssm stuff |
| 2599 | uint64_t last_scene_shadow_pass = light_storage->light_instance_get_shadow_pass(p_light); |
| 2600 | if (last_scene_shadow_pass != get_scene_pass()) { |
| 2601 | light_storage->light_instance_set_directional_rect(p_light, light_storage->get_directional_shadow_rect()); |
| 2602 | light_storage->directional_shadow_increase_current_light(); |
| 2603 | light_storage->light_instance_set_shadow_pass(p_light, get_scene_pass()); |
| 2604 | } |
| 2605 | |
| 2606 | use_pancake = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE) > 0; |
| 2607 | light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass); |
| 2608 | light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass); |
| 2609 | |
| 2610 | atlas_rect = light_storage->light_instance_get_directional_rect(p_light); |
| 2611 | |
| 2612 | if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) { |
| 2613 | atlas_rect.size.width /= 2; |
| 2614 | atlas_rect.size.height /= 2; |
| 2615 | |
| 2616 | if (p_pass == 1) { |
| 2617 | atlas_rect.position.x += atlas_rect.size.width; |
| 2618 | } else if (p_pass == 2) { |
| 2619 | atlas_rect.position.y += atlas_rect.size.height; |
| 2620 | } else if (p_pass == 3) { |
| 2621 | atlas_rect.position += atlas_rect.size; |
| 2622 | } |
| 2623 | } else if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) { |
| 2624 | atlas_rect.size.height /= 2; |
| 2625 | |
| 2626 | if (p_pass == 0) { |
| 2627 | } else { |
| 2628 | atlas_rect.position.y += atlas_rect.size.height; |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | float directional_shadow_size = light_storage->directional_shadow_get_size(); |
| 2633 | Rect2 atlas_rect_norm = atlas_rect; |
| 2634 | atlas_rect_norm.position /= directional_shadow_size; |
| 2635 | atlas_rect_norm.size /= directional_shadow_size; |
| 2636 | light_storage->light_instance_set_directional_shadow_atlas_rect(p_light, p_pass, atlas_rect_norm); |
| 2637 | |
| 2638 | zfar = RSG::light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE); |
| 2639 | |
| 2640 | render_fb = light_storage->direction_shadow_get_fb(); |
| 2641 | render_texture = RID(); |
| 2642 | flip_y = true; |
| 2643 | |
| 2644 | } else { |
| 2645 | //set from shadow atlas |
| 2646 | |
| 2647 | ERR_FAIL_COND(!light_storage->owns_shadow_atlas(p_shadow_atlas)); |
| 2648 | ERR_FAIL_COND(!light_storage->shadow_atlas_owns_light_instance(p_shadow_atlas, p_light)); |
| 2649 | |
| 2650 | RSG::light_storage->shadow_atlas_update(p_shadow_atlas); |
| 2651 | |
| 2652 | uint32_t key = light_storage->shadow_atlas_get_light_instance_key(p_shadow_atlas, p_light); |
| 2653 | |
| 2654 | uint32_t quadrant = (key >> RendererRD::LightStorage::QUADRANT_SHIFT) & 0x3; |
| 2655 | uint32_t shadow = key & RendererRD::LightStorage::SHADOW_INDEX_MASK; |
| 2656 | uint32_t subdivision = light_storage->shadow_atlas_get_quadrant_subdivision(p_shadow_atlas, quadrant); |
| 2657 | |
| 2658 | ERR_FAIL_INDEX((int)shadow, light_storage->shadow_atlas_get_quadrant_shadow_size(p_shadow_atlas, quadrant)); |
| 2659 | |
| 2660 | uint32_t shadow_atlas_size = light_storage->shadow_atlas_get_size(p_shadow_atlas); |
| 2661 | uint32_t quadrant_size = shadow_atlas_size >> 1; |
| 2662 | |
| 2663 | atlas_rect.position.x = (quadrant & 1) * quadrant_size; |
| 2664 | atlas_rect.position.y = (quadrant >> 1) * quadrant_size; |
| 2665 | |
| 2666 | uint32_t shadow_size = (quadrant_size / subdivision); |
| 2667 | atlas_rect.position.x += (shadow % subdivision) * shadow_size; |
| 2668 | atlas_rect.position.y += (shadow / subdivision) * shadow_size; |
| 2669 | |
| 2670 | atlas_rect.size.width = shadow_size; |
| 2671 | atlas_rect.size.height = shadow_size; |
| 2672 | |
| 2673 | zfar = light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE); |
| 2674 | |
| 2675 | if (light_storage->light_get_type(base) == RS::LIGHT_OMNI) { |
| 2676 | bool wrap = (shadow + 1) % subdivision == 0; |
| 2677 | dual_paraboloid_offset = wrap ? Vector2i(1 - subdivision, 1) : Vector2i(1, 0); |
| 2678 | |
| 2679 | if (light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) { |
| 2680 | render_texture = light_storage->get_cubemap(shadow_size / 2); |
| 2681 | render_fb = light_storage->get_cubemap_fb(shadow_size / 2, p_pass); |
| 2682 | |
| 2683 | light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass); |
| 2684 | light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass); |
| 2685 | render_cubemap = true; |
| 2686 | finalize_cubemap = p_pass == 5; |
| 2687 | atlas_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas); |
| 2688 | |
| 2689 | atlas_size = shadow_atlas_size; |
| 2690 | |
| 2691 | if (p_pass == 0) { |
| 2692 | _render_shadow_begin(); |
| 2693 | } |
| 2694 | |
| 2695 | } else { |
| 2696 | atlas_rect.position.x += 1; |
| 2697 | atlas_rect.position.y += 1; |
| 2698 | atlas_rect.size.x -= 2; |
| 2699 | atlas_rect.size.y -= 2; |
| 2700 | |
| 2701 | atlas_rect.position += p_pass * atlas_rect.size * dual_paraboloid_offset; |
| 2702 | |
| 2703 | light_projection = light_storage->light_instance_get_shadow_camera(p_light, 0); |
| 2704 | light_transform = light_storage->light_instance_get_shadow_transform(p_light, 0); |
| 2705 | |
| 2706 | using_dual_paraboloid = true; |
| 2707 | using_dual_paraboloid_flip = p_pass == 1; |
| 2708 | render_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas); |
| 2709 | flip_y = true; |
| 2710 | } |
| 2711 | |
| 2712 | } else if (light_storage->light_get_type(base) == RS::LIGHT_SPOT) { |
| 2713 | light_projection = light_storage->light_instance_get_shadow_camera(p_light, 0); |
| 2714 | light_transform = light_storage->light_instance_get_shadow_transform(p_light, 0); |
| 2715 | |
| 2716 | render_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas); |
| 2717 | |
| 2718 | flip_y = true; |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | if (render_cubemap) { |
| 2723 | //rendering to cubemap |
| 2724 | _render_shadow_append(render_fb, p_instances, light_projection, light_transform, zfar, 0, 0, reverse_cull_face, false, false, use_pancake, p_lod_distance_multiplier, p_screen_mesh_lod_threshold, Rect2(), false, true, true, true, p_render_info, p_viewport_size, p_main_cam_transform); |
| 2725 | if (finalize_cubemap) { |
| 2726 | _render_shadow_process(); |
| 2727 | _render_shadow_end(); |
| 2728 | //reblit |
| 2729 | Rect2 atlas_rect_norm = atlas_rect; |
| 2730 | atlas_rect_norm.position /= float(atlas_size); |
| 2731 | atlas_rect_norm.size /= float(atlas_size); |
| 2732 | copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), zfar, false); |
| 2733 | atlas_rect_norm.position += Vector2(dual_paraboloid_offset) * atlas_rect_norm.size; |
| 2734 | copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), zfar, true); |
| 2735 | |
| 2736 | //restore transform so it can be properly used |
| 2737 | light_storage->light_instance_set_shadow_transform(p_light, Projection(), light_storage->light_instance_get_base_transform(p_light), zfar, 0, 0, 0); |
| 2738 | } |
| 2739 | |
| 2740 | } else { |
| 2741 | //render shadow |
| 2742 | _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_lod_distance_multiplier, p_screen_mesh_lod_threshold, atlas_rect, flip_y, p_clear_region, p_open_pass, p_close_pass, p_render_info, p_viewport_size, p_main_cam_transform); |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | void RenderForwardClustered::_render_shadow_begin() { |
| 2747 | scene_state.shadow_passes.clear(); |
| 2748 | RD::get_singleton()->draw_command_begin_label("Shadow Setup"); |
| 2749 | _update_render_base_uniform_set(); |
| 2750 | |
| 2751 | render_list[RENDER_LIST_SECONDARY].clear(); |
| 2752 | // No need to reset scene_state.curr_gpu_ptr or scene_state.instance_buffer[RENDER_LIST_SECONDARY] |
| 2753 | // because _fill_instance_data will do that if it detects p_offset == 0u. |
| 2754 | } |
| 2755 | |
| 2756 | void 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, 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, const Size2i &p_viewport_size, const Transform3D &p_main_cam_transform) { |
| 2757 | uint32_t shadow_pass_index = scene_state.shadow_passes.size(); |
| 2758 | |
| 2759 | SceneState::ShadowPass shadow_pass; |
| 2760 | |
| 2761 | RenderSceneDataRD scene_data; |
| 2762 | scene_data.flip_y = !p_flip_y; // Q: Why is this inverted? Do we assume flip in shadow logic? |
| 2763 | scene_data.cam_projection = p_projection; |
| 2764 | scene_data.cam_transform = p_transform; |
| 2765 | scene_data.view_projection[0] = p_projection; |
| 2766 | scene_data.z_far = p_zfar; |
| 2767 | scene_data.z_near = 0.0; |
| 2768 | scene_data.lod_distance_multiplier = p_lod_distance_multiplier; |
| 2769 | scene_data.dual_paraboloid_side = p_use_dp_flip ? -1 : 1; |
| 2770 | scene_data.opaque_prepass_threshold = 0.1f; |
| 2771 | scene_data.time = time; |
| 2772 | scene_data.time_step = time_step; |
| 2773 | scene_data.main_cam_transform = p_main_cam_transform; |
| 2774 | scene_data.shadow_pass = true; |
| 2775 | |
| 2776 | RenderDataRD render_data; |
| 2777 | render_data.scene_data = &scene_data; |
| 2778 | render_data.cluster_size = 1; |
| 2779 | render_data.cluster_max_elements = 32; |
| 2780 | render_data.instances = &p_instances; |
| 2781 | render_data.render_info = p_render_info; |
| 2782 | |
| 2783 | Size2i screen_size = RD::get_singleton()->framebuffer_get_size(p_framebuffer); |
| 2784 | Size2i viewport_size = p_rect.size; |
| 2785 | if (viewport_size == Size2()) { |
| 2786 | viewport_size = screen_size; |
| 2787 | } |
| 2788 | _setup_environment(&render_data, true, screen_size, viewport_size, Color(), false, false, p_use_pancake, shadow_pass_index); |
| 2789 | |
| 2790 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) { |
| 2791 | scene_data.screen_mesh_lod_threshold = 0.0; |
| 2792 | } else { |
| 2793 | scene_data.screen_mesh_lod_threshold = p_screen_mesh_lod_threshold; |
| 2794 | } |
| 2795 | |
| 2796 | PassMode pass_mode = p_use_dp ? PASS_MODE_SHADOW_DP : PASS_MODE_SHADOW; |
| 2797 | |
| 2798 | uint32_t render_list_from = render_list[RENDER_LIST_SECONDARY].elements.size(); |
| 2799 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode, false, false, false, true); |
| 2800 | uint32_t render_list_size = render_list[RENDER_LIST_SECONDARY].elements.size() - render_list_from; |
| 2801 | render_list[RENDER_LIST_SECONDARY].sort_by_key_range(render_list_from, render_list_size); |
| 2802 | _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); |
| 2803 | |
| 2804 | { |
| 2805 | //regular forward for now |
| 2806 | bool flip_cull = p_use_dp_flip; |
| 2807 | if (p_flip_y) { |
| 2808 | flip_cull = !flip_cull; |
| 2809 | } |
| 2810 | |
| 2811 | if (p_reverse_cull_face) { |
| 2812 | flip_cull = !flip_cull; |
| 2813 | } |
| 2814 | |
| 2815 | shadow_pass.element_from = render_list_from; |
| 2816 | shadow_pass.element_count = render_list_size; |
| 2817 | shadow_pass.flip_cull = flip_cull; |
| 2818 | shadow_pass.pass_mode = pass_mode; |
| 2819 | |
| 2820 | shadow_pass.rp_uniform_set = RID(); //will be filled later when instance buffer is complete |
| 2821 | shadow_pass.screen_mesh_lod_threshold = scene_data.screen_mesh_lod_threshold; |
| 2822 | shadow_pass.lod_distance_multiplier = scene_data.lod_distance_multiplier; |
| 2823 | |
| 2824 | shadow_pass.framebuffer = p_framebuffer; |
| 2825 | shadow_pass.clear_depth = p_begin || p_clear_region; |
| 2826 | shadow_pass.rect = p_rect; |
| 2827 | |
| 2828 | scene_state.shadow_passes.push_back(shadow_pass); |
| 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | void RenderForwardClustered::_render_shadow_process() { |
| 2833 | RenderingDevice *rd = RenderingDevice::get_singleton(); |
| 2834 | if (scene_state.instance_buffer[RENDER_LIST_SECONDARY].get_size(0u) > 0u) { |
| 2835 | rd->buffer_flush(scene_state.instance_buffer[RENDER_LIST_SECONDARY]._get(0u)); |
| 2836 | } |
| 2837 | |
| 2838 | //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) |
| 2839 | |
| 2840 | for (uint32_t i = 0; i < scene_state.shadow_passes.size(); i++) { |
| 2841 | //render passes need to be configured after instance buffer is done, since they need the latest version |
| 2842 | SceneState::ShadowPass &shadow_pass = scene_state.shadow_passes[i]; |
| 2843 | shadow_pass.rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default(), false, i); |
| 2844 | } |
| 2845 | |
| 2846 | RD::get_singleton()->draw_command_end_label(); |
| 2847 | } |
| 2848 | void RenderForwardClustered::_render_shadow_end() { |
| 2849 | RD::get_singleton()->draw_command_begin_label("Shadow Render"); |
| 2850 | |
| 2851 | for (SceneState::ShadowPass &shadow_pass : scene_state.shadow_passes) { |
| 2852 | 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); |
| 2853 | _render_list_with_draw_list(&render_list_parameters, shadow_pass.framebuffer, shadow_pass.clear_depth ? RD::DRAW_CLEAR_DEPTH : RD::DRAW_DEFAULT_ALL, Vector<Color>(), 0.0f, 0, shadow_pass.rect); |
| 2854 | } |
| 2855 | |
| 2856 | RD::get_singleton()->draw_command_end_label(); |
| 2857 | } |
| 2858 | |
| 2859 | void RenderForwardClustered::_render_particle_collider_heightfield(RID p_fb, const Transform3D &p_cam_transform, const Projection &p_cam_projection, const PagedArray<RenderGeometryInstance *> &p_instances) { |
| 2860 | RENDER_TIMESTAMP("Setup GPUParticlesCollisionHeightField3D"); |
| 2861 | |
| 2862 | RD::get_singleton()->draw_command_begin_label("Render Collider Heightfield"); |
| 2863 | |
| 2864 | RenderSceneDataRD scene_data; |
| 2865 | scene_data.flip_y = true; |
| 2866 | scene_data.cam_projection = p_cam_projection; |
| 2867 | scene_data.cam_transform = p_cam_transform; |
| 2868 | scene_data.view_projection[0] = p_cam_projection; |
| 2869 | scene_data.z_near = 0.0; |
| 2870 | scene_data.z_far = p_cam_projection.get_z_far(); |
| 2871 | scene_data.dual_paraboloid_side = 0; |
| 2872 | scene_data.opaque_prepass_threshold = 0.0; |
| 2873 | scene_data.time = time; |
| 2874 | scene_data.time_step = time_step; |
| 2875 | scene_data.main_cam_transform = p_cam_transform; |
| 2876 | scene_data.shadow_pass = true; // Not a shadow pass, but should be treated like one. |
| 2877 | |
| 2878 | RenderDataRD render_data; |
| 2879 | render_data.scene_data = &scene_data; |
| 2880 | render_data.cluster_size = 1; |
| 2881 | render_data.cluster_max_elements = 32; |
| 2882 | render_data.instances = &p_instances; |
| 2883 | |
| 2884 | _update_render_base_uniform_set(); |
| 2885 | |
| 2886 | Size2i screen_size = RD::get_singleton()->framebuffer_get_size(p_fb); |
| 2887 | _setup_environment(&render_data, true, screen_size, screen_size, Color(), false, false, false); |
| 2888 | |
| 2889 | PassMode pass_mode = PASS_MODE_SHADOW; |
| 2890 | |
| 2891 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode); |
| 2892 | render_list[RENDER_LIST_SECONDARY].sort_by_key(); |
| 2893 | _fill_instance_data(RENDER_LIST_SECONDARY); |
| 2894 | |
| 2895 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default()); |
| 2896 | |
| 2897 | RENDER_TIMESTAMP("Render Collider Heightfield"); |
| 2898 | |
| 2899 | { |
| 2900 | //regular forward for now |
| 2901 | 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); |
| 2902 | _render_list_with_draw_list(&render_list_params, p_fb, RD::DRAW_CLEAR_ALL); |
| 2903 | } |
| 2904 | RD::get_singleton()->draw_command_end_label(); |
| 2905 | } |
| 2906 | |
| 2907 | void 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) { |
| 2908 | RENDER_TIMESTAMP("Setup Rendering 3D Material"); |
| 2909 | |
| 2910 | RD::get_singleton()->draw_command_begin_label("Render 3D Material"); |
| 2911 | |
| 2912 | RenderSceneDataRD scene_data; |
| 2913 | scene_data.cam_projection = p_cam_projection; |
| 2914 | scene_data.cam_transform = p_cam_transform; |
| 2915 | scene_data.view_projection[0] = p_cam_projection; |
| 2916 | scene_data.dual_paraboloid_side = 0; |
| 2917 | scene_data.material_uv2_mode = false; |
| 2918 | scene_data.opaque_prepass_threshold = 0.0f; |
| 2919 | scene_data.emissive_exposure_normalization = p_exposure_normalization; |
| 2920 | scene_data.time = time; |
| 2921 | scene_data.time_step = time_step; |
| 2922 | scene_data.main_cam_transform = p_cam_transform; |
| 2923 | |
| 2924 | RenderDataRD render_data; |
| 2925 | render_data.scene_data = &scene_data; |
| 2926 | render_data.cluster_size = 1; |
| 2927 | render_data.cluster_max_elements = 32; |
| 2928 | render_data.instances = &p_instances; |
| 2929 | |
| 2930 | scene_shader.enable_advanced_shader_group(); |
| 2931 | |
| 2932 | _update_render_base_uniform_set(); |
| 2933 | |
| 2934 | Size2i screen_size = RD::get_singleton()->framebuffer_get_size(p_framebuffer); |
| 2935 | Size2i viewport_size = p_region.size; |
| 2936 | if (viewport_size == Size2()) { |
| 2937 | viewport_size = screen_size; |
| 2938 | } |
| 2939 | _setup_environment(&render_data, true, screen_size, viewport_size, Color()); |
| 2940 | |
| 2941 | PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL; |
| 2942 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode); |
| 2943 | render_list[RENDER_LIST_SECONDARY].sort_by_key(); |
| 2944 | _fill_instance_data(RENDER_LIST_SECONDARY); |
| 2945 | |
| 2946 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default()); |
| 2947 | |
| 2948 | RENDER_TIMESTAMP("Render 3D Material"); |
| 2949 | |
| 2950 | { |
| 2951 | 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); |
| 2952 | //regular forward for now |
| 2953 | Vector<Color> clear = { |
| 2954 | Color(0, 0, 0, 0), |
| 2955 | Color(0, 0, 0, 0), |
| 2956 | Color(0, 0, 0, 0), |
| 2957 | Color(0, 0, 0, 0), |
| 2958 | Color(0, 0, 0, 0) |
| 2959 | }; |
| 2960 | |
| 2961 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer, RD::DRAW_CLEAR_ALL, clear, 0.0f, 0, p_region); |
| 2962 | _render_list(draw_list, RD::get_singleton()->framebuffer_get_format(p_framebuffer), &render_list_params, 0, render_list_params.element_count); |
| 2963 | RD::get_singleton()->draw_list_end(); |
| 2964 | } |
| 2965 | |
| 2966 | RD::get_singleton()->draw_command_end_label(); |
| 2967 | } |
| 2968 | |
| 2969 | void RenderForwardClustered::_render_uv2(const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) { |
| 2970 | RENDER_TIMESTAMP("Setup Rendering UV2"); |
| 2971 | |
| 2972 | RD::get_singleton()->draw_command_begin_label("Render UV2"); |
| 2973 | |
| 2974 | RenderSceneDataRD scene_data; |
| 2975 | scene_data.dual_paraboloid_side = 0; |
| 2976 | scene_data.material_uv2_mode = true; |
| 2977 | scene_data.opaque_prepass_threshold = 0.0; |
| 2978 | scene_data.emissive_exposure_normalization = -1.0; |
| 2979 | |
| 2980 | RenderDataRD render_data; |
| 2981 | render_data.scene_data = &scene_data; |
| 2982 | render_data.cluster_size = 1; |
| 2983 | render_data.cluster_max_elements = 32; |
| 2984 | render_data.instances = &p_instances; |
| 2985 | |
| 2986 | scene_shader.enable_advanced_shader_group(); |
| 2987 | |
| 2988 | _update_render_base_uniform_set(); |
| 2989 | |
| 2990 | Size2i screen_size = RD::get_singleton()->framebuffer_get_size(p_framebuffer); |
| 2991 | Size2i viewport_size = p_region.size; |
| 2992 | if (viewport_size == Size2()) { |
| 2993 | viewport_size = screen_size; |
| 2994 | } |
| 2995 | _setup_environment(&render_data, true, screen_size, viewport_size, Color()); |
| 2996 | |
| 2997 | PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL; |
| 2998 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode); |
| 2999 | render_list[RENDER_LIST_SECONDARY].sort_by_key(); |
| 3000 | _fill_instance_data(RENDER_LIST_SECONDARY); |
| 3001 | |
| 3002 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default()); |
| 3003 | |
| 3004 | RENDER_TIMESTAMP("Render 3D Material"); |
| 3005 | |
| 3006 | { |
| 3007 | 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); |
| 3008 | //regular forward for now |
| 3009 | Vector<Color> clear = { |
| 3010 | Color(0, 0, 0, 0), |
| 3011 | Color(0, 0, 0, 0), |
| 3012 | Color(0, 0, 0, 0), |
| 3013 | Color(0, 0, 0, 0), |
| 3014 | Color(0, 0, 0, 0) |
| 3015 | }; |
| 3016 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer, RD::DRAW_CLEAR_ALL, clear, 0.0f, 0, p_region); |
| 3017 | |
| 3018 | const int uv_offset_count = 9; |
| 3019 | static const Vector2 uv_offsets[uv_offset_count] = { |
| 3020 | Vector2(-1, 1), |
| 3021 | Vector2(1, 1), |
| 3022 | Vector2(1, -1), |
| 3023 | Vector2(-1, -1), |
| 3024 | Vector2(-1, 0), |
| 3025 | Vector2(1, 0), |
| 3026 | Vector2(0, -1), |
| 3027 | Vector2(0, 1), |
| 3028 | Vector2(0, 0), |
| 3029 | |
| 3030 | }; |
| 3031 | |
| 3032 | for (int i = 0; i < uv_offset_count; i++) { |
| 3033 | Vector2 ofs = uv_offsets[i]; |
| 3034 | ofs.x /= p_region.size.width; |
| 3035 | ofs.y /= p_region.size.height; |
| 3036 | render_list_params.uv_offset = ofs; |
| 3037 | _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 |
| 3038 | } |
| 3039 | render_list_params.uv_offset = Vector2(); |
| 3040 | render_list_params.force_wireframe = false; |
| 3041 | _render_list(draw_list, RD::get_singleton()->framebuffer_get_format(p_framebuffer), &render_list_params, 0, render_list_params.element_count); //second regular triangles |
| 3042 | |
| 3043 | RD::get_singleton()->draw_list_end(); |
| 3044 | } |
| 3045 | |
| 3046 | RD::get_singleton()->draw_command_end_label(); |
| 3047 | } |
| 3048 | |
| 3049 | void 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) { |
| 3050 | RENDER_TIMESTAMP("Render SDFGI"); |
| 3051 | |
| 3052 | RD::get_singleton()->draw_command_begin_label("Render SDFGI Voxel"); |
| 3053 | |
| 3054 | RenderSceneDataRD scene_data; |
| 3055 | |
| 3056 | RenderDataRD render_data; |
| 3057 | render_data.scene_data = &scene_data; |
| 3058 | render_data.cluster_size = 1; |
| 3059 | render_data.cluster_max_elements = 32; |
| 3060 | render_data.instances = &p_instances; |
| 3061 | |
| 3062 | _update_render_base_uniform_set(); |
| 3063 | |
| 3064 | // Indicate pipelines for SDFGI are required. |
| 3065 | global_pipeline_data_required.use_sdfgi = true; |
| 3066 | |
| 3067 | PassMode pass_mode = PASS_MODE_SDF; |
| 3068 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode); |
| 3069 | render_list[RENDER_LIST_SECONDARY].sort_by_key(); |
| 3070 | _fill_instance_data(RENDER_LIST_SECONDARY); |
| 3071 | |
| 3072 | Vector3 half_size = p_bounds.size * 0.5; |
| 3073 | Vector3 center = p_bounds.position + half_size; |
| 3074 | |
| 3075 | //print_line("re-render " + p_from + " - " + p_size + " bounds " + p_bounds); |
| 3076 | for (int i = 0; i < 3; i++) { |
| 3077 | scene_state.ubo.sdf_offset[i] = p_from[i]; |
| 3078 | scene_state.ubo.sdf_size[i] = p_size[i]; |
| 3079 | } |
| 3080 | |
| 3081 | for (int i = 0; i < 3; i++) { |
| 3082 | Vector3 axis; |
| 3083 | axis[i] = 1.0; |
| 3084 | Vector3 up, right; |
| 3085 | int right_axis = (i + 1) % 3; |
| 3086 | int up_axis = (i + 2) % 3; |
| 3087 | up[up_axis] = 1.0; |
| 3088 | right[right_axis] = 1.0; |
| 3089 | |
| 3090 | Size2i fb_size; |
| 3091 | fb_size.x = p_size[right_axis]; |
| 3092 | fb_size.y = p_size[up_axis]; |
| 3093 | |
| 3094 | scene_data.cam_transform.origin = center + axis * half_size; |
| 3095 | scene_data.cam_transform.basis.set_column(0, right); |
| 3096 | scene_data.cam_transform.basis.set_column(1, up); |
| 3097 | scene_data.cam_transform.basis.set_column(2, axis); |
| 3098 | |
| 3099 | //print_line("pass: " + itos(i) + " xform " + scene_data.cam_transform); |
| 3100 | |
| 3101 | float h_size = half_size[right_axis]; |
| 3102 | float v_size = half_size[up_axis]; |
| 3103 | float d_size = half_size[i] * 2.0; |
| 3104 | scene_data.cam_projection.set_orthogonal(-h_size, h_size, -v_size, v_size, 0, d_size); |
| 3105 | //print_line("pass: " + itos(i) + " cam hsize: " + rtos(h_size) + " vsize: " + rtos(v_size) + " dsize " + rtos(d_size)); |
| 3106 | |
| 3107 | Transform3D to_bounds; |
| 3108 | to_bounds.origin = p_bounds.position; |
| 3109 | to_bounds.basis.scale(p_bounds.size); |
| 3110 | |
| 3111 | RendererRD::MaterialStorage::store_transform(to_bounds.affine_inverse() * scene_data.cam_transform, scene_state.ubo.sdf_to_bounds); |
| 3112 | |
| 3113 | scene_data.emissive_exposure_normalization = p_exposure_normalization; |
| 3114 | _setup_environment(&render_data, true, fb_size, fb_size, Color()); |
| 3115 | |
| 3116 | RID rp_uniform_set = _setup_sdfgi_render_pass_uniform_set(p_albedo_texture, p_emission_texture, p_emission_aniso_texture, p_geom_facing_texture, RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default()); |
| 3117 | |
| 3118 | HashMap<Size2i, RID>::Iterator E = sdfgi_framebuffer_size_cache.find(fb_size); |
| 3119 | if (!E) { |
| 3120 | RID fb = RD::get_singleton()->framebuffer_create_empty(fb_size); |
| 3121 | E = sdfgi_framebuffer_size_cache.insert(fb_size, fb); |
| 3122 | } |
| 3123 | |
| 3124 | 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); |
| 3125 | _render_list_with_draw_list(&render_list_params, E->value); |
| 3126 | } |
| 3127 | |
| 3128 | RD::get_singleton()->draw_command_end_label(); |
| 3129 | } |
| 3130 | |
| 3131 | void RenderForwardClustered::base_uniforms_changed() { |
| 3132 | if (!render_base_uniform_set.is_null() && RD::get_singleton()->uniform_set_is_valid(render_base_uniform_set)) { |
| 3133 | RD::get_singleton()->free_rid(render_base_uniform_set); |
| 3134 | } |
| 3135 | render_base_uniform_set = RID(); |
| 3136 | } |
| 3137 | |
| 3138 | void RenderForwardClustered::_update_render_base_uniform_set() { |
| 3139 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 3140 | |
| 3141 | 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())) { |
| 3142 | if (render_base_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(render_base_uniform_set)) { |
| 3143 | RD::get_singleton()->free_rid(render_base_uniform_set); |
| 3144 | } |
| 3145 | |
| 3146 | lightmap_texture_array_version = light_storage->lightmap_array_get_version(); |
| 3147 | |
| 3148 | Vector<RD::Uniform> uniforms; |
| 3149 | |
| 3150 | { |
| 3151 | RD::Uniform u; |
| 3152 | u.binding = 2; |
| 3153 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 3154 | u.append_id(scene_shader.shadow_sampler); |
| 3155 | uniforms.push_back(u); |
| 3156 | } |
| 3157 | |
| 3158 | { |
| 3159 | RD::Uniform u; |
| 3160 | u.binding = 3; |
| 3161 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3162 | u.append_id(RendererRD::LightStorage::get_singleton()->get_omni_light_buffer()); |
| 3163 | uniforms.push_back(u); |
| 3164 | } |
| 3165 | { |
| 3166 | RD::Uniform u; |
| 3167 | u.binding = 4; |
| 3168 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3169 | u.append_id(RendererRD::LightStorage::get_singleton()->get_spot_light_buffer()); |
| 3170 | uniforms.push_back(u); |
| 3171 | } |
| 3172 | |
| 3173 | { |
| 3174 | RD::Uniform u; |
| 3175 | u.binding = 5; |
| 3176 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3177 | u.append_id(RendererRD::LightStorage::get_singleton()->get_reflection_probe_buffer()); |
| 3178 | uniforms.push_back(u); |
| 3179 | } |
| 3180 | { |
| 3181 | RD::Uniform u; |
| 3182 | u.binding = 6; |
| 3183 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 3184 | u.append_id(RendererRD::LightStorage::get_singleton()->get_directional_light_buffer()); |
| 3185 | uniforms.push_back(u); |
| 3186 | } |
| 3187 | { |
| 3188 | RD::Uniform u; |
| 3189 | u.binding = 7; |
| 3190 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3191 | u.append_id(scene_state.lightmap_buffer); |
| 3192 | uniforms.push_back(u); |
| 3193 | } |
| 3194 | { |
| 3195 | RD::Uniform u; |
| 3196 | u.binding = 8; |
| 3197 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3198 | u.append_id(scene_state.lightmap_capture_buffer); |
| 3199 | uniforms.push_back(u); |
| 3200 | } |
| 3201 | { |
| 3202 | RD::Uniform u; |
| 3203 | u.binding = 9; |
| 3204 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3205 | RID decal_atlas = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture(); |
| 3206 | u.append_id(decal_atlas); |
| 3207 | uniforms.push_back(u); |
| 3208 | } |
| 3209 | { |
| 3210 | RD::Uniform u; |
| 3211 | u.binding = 10; |
| 3212 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3213 | RID decal_atlas = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture_srgb(); |
| 3214 | u.append_id(decal_atlas); |
| 3215 | uniforms.push_back(u); |
| 3216 | } |
| 3217 | { |
| 3218 | RD::Uniform u; |
| 3219 | u.binding = 11; |
| 3220 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3221 | u.append_id(RendererRD::TextureStorage::get_singleton()->get_decal_buffer()); |
| 3222 | uniforms.push_back(u); |
| 3223 | } |
| 3224 | |
| 3225 | { |
| 3226 | RD::Uniform u; |
| 3227 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3228 | u.binding = 12; |
| 3229 | u.append_id(RendererRD::MaterialStorage::get_singleton()->global_shader_uniforms_get_storage_buffer()); |
| 3230 | uniforms.push_back(u); |
| 3231 | } |
| 3232 | |
| 3233 | { |
| 3234 | RD::Uniform u; |
| 3235 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 3236 | u.binding = 13; |
| 3237 | u.append_id(sdfgi_get_ubo()); |
| 3238 | uniforms.push_back(u); |
| 3239 | } |
| 3240 | |
| 3241 | { |
| 3242 | RD::Uniform u; |
| 3243 | u.binding = 14; |
| 3244 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 3245 | u.append_id(RendererRD::MaterialStorage::get_singleton()->sampler_rd_get_default(RS::CanvasItemTextureFilter::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED)); |
| 3246 | uniforms.push_back(u); |
| 3247 | } |
| 3248 | |
| 3249 | { |
| 3250 | RD::Uniform u; |
| 3251 | u.binding = 15; |
| 3252 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3253 | u.append_id(best_fit_normal.texture); |
| 3254 | uniforms.push_back(u); |
| 3255 | } |
| 3256 | |
| 3257 | { |
| 3258 | RD::Uniform u; |
| 3259 | u.binding = 16; |
| 3260 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3261 | u.append_id(dfg_lut.texture); |
| 3262 | uniforms.push_back(u); |
| 3263 | } |
| 3264 | |
| 3265 | render_base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, scene_shader.default_shader_rd, SCENE_UNIFORM_SET); |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | RID RenderForwardClustered::_setup_render_pass_uniform_set(RenderListType p_render_list, const RenderDataRD *p_render_data, RID p_radiance_texture, const RendererRD::MaterialStorage::Samplers &p_samplers, bool p_use_directional_shadow_atlas, int p_index) { |
| 3270 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 3271 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 3272 | |
| 3273 | bool is_multiview = false; |
| 3274 | |
| 3275 | Ref<RenderSceneBuffersRD> rb; // handy for not having to fully type out p_render_data->render_buffers all the time... |
| 3276 | Ref<RenderBufferDataForwardClustered> rb_data; |
| 3277 | if (p_render_data && p_render_data->render_buffers.is_valid()) { |
| 3278 | rb = p_render_data->render_buffers; |
| 3279 | is_multiview = rb->get_view_count() > 1; |
| 3280 | if (rb->has_custom_data(RB_SCOPE_FORWARD_CLUSTERED)) { |
| 3281 | // Our forward clustered custom data buffer will only be available when we're rendering our normal view. |
| 3282 | // This will not be available when rendering reflection probes. |
| 3283 | rb_data = rb->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | //default render buffer and scene state uniform set |
| 3288 | |
| 3289 | thread_local LocalVector<RD::Uniform> uniforms; |
| 3290 | uniforms.clear(); |
| 3291 | |
| 3292 | { |
| 3293 | RD::Uniform u; |
| 3294 | u.binding = 0; |
| 3295 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 3296 | u.append_id(scene_state.uniform_buffers[p_index]); |
| 3297 | uniforms.push_back(u); |
| 3298 | } |
| 3299 | { |
| 3300 | RD::Uniform u; |
| 3301 | u.binding = 1; |
| 3302 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 3303 | u.append_id(scene_state.implementation_uniform_buffers[p_index]); |
| 3304 | uniforms.push_back(u); |
| 3305 | } |
| 3306 | { |
| 3307 | RD::Uniform u; |
| 3308 | u.binding = 2; |
| 3309 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC; |
| 3310 | if (scene_state.instance_buffer[p_render_list].get_size(0u) == 0u) { |
| 3311 | // Any buffer will do since it's not used, so just create one. |
| 3312 | // We can't use scene_shader.default_vec4_xform_buffer because it's not dynamic. |
| 3313 | scene_state.instance_buffer[p_render_list].set_storage_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData)); |
| 3314 | scene_state.instance_buffer[p_render_list].prepare_for_upload(); |
| 3315 | } |
| 3316 | RID instance_buffer = scene_state.instance_buffer[p_render_list]._get(0u); |
| 3317 | u.append_id(instance_buffer); |
| 3318 | uniforms.push_back(u); |
| 3319 | } |
| 3320 | { |
| 3321 | RID radiance_texture; |
| 3322 | if (p_radiance_texture.is_valid()) { |
| 3323 | radiance_texture = p_radiance_texture; |
| 3324 | } else { |
| 3325 | radiance_texture = texture_storage->texture_rd_get_default(is_using_radiance_octmap_array() ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK); |
| 3326 | } |
| 3327 | RD::Uniform u; |
| 3328 | u.binding = 3; |
| 3329 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3330 | u.append_id(radiance_texture); |
| 3331 | uniforms.push_back(u); |
| 3332 | } |
| 3333 | { |
| 3334 | 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(); |
| 3335 | RD::Uniform u; |
| 3336 | u.binding = 4; |
| 3337 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3338 | if (ref_texture.is_valid()) { |
| 3339 | u.append_id(ref_texture); |
| 3340 | } else { |
| 3341 | u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK)); |
| 3342 | } |
| 3343 | uniforms.push_back(u); |
| 3344 | } |
| 3345 | { |
| 3346 | RD::Uniform u; |
| 3347 | u.binding = 5; |
| 3348 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3349 | RID texture; |
| 3350 | if (p_render_data && p_render_data->shadow_atlas.is_valid()) { |
| 3351 | texture = RendererRD::LightStorage::get_singleton()->shadow_atlas_get_texture(p_render_data->shadow_atlas); |
| 3352 | } |
| 3353 | if (!texture.is_valid()) { |
| 3354 | texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH); |
| 3355 | } |
| 3356 | u.append_id(texture); |
| 3357 | uniforms.push_back(u); |
| 3358 | } |
| 3359 | { |
| 3360 | RD::Uniform u; |
| 3361 | u.binding = 6; |
| 3362 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3363 | if (p_use_directional_shadow_atlas && RendererRD::LightStorage::get_singleton()->directional_shadow_get_texture().is_valid()) { |
| 3364 | u.append_id(RendererRD::LightStorage::get_singleton()->directional_shadow_get_texture()); |
| 3365 | } else { |
| 3366 | u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH)); |
| 3367 | } |
| 3368 | uniforms.push_back(u); |
| 3369 | } |
| 3370 | { |
| 3371 | Vector<RID> textures; |
| 3372 | textures.resize(scene_state.max_lightmaps * 2); |
| 3373 | |
| 3374 | RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE); |
| 3375 | for (uint32_t i = 0; i < scene_state.max_lightmaps * 2; i++) { |
| 3376 | uint32_t current_lightmap_index = i < scene_state.max_lightmaps ? i : i - scene_state.max_lightmaps; |
| 3377 | |
| 3378 | if (p_render_data && current_lightmap_index < p_render_data->lightmaps->size()) { |
| 3379 | RID base = light_storage->lightmap_instance_get_lightmap((*p_render_data->lightmaps)[current_lightmap_index]); |
| 3380 | RID texture; |
| 3381 | |
| 3382 | if (i < scene_state.max_lightmaps) { |
| 3383 | // Lightmap |
| 3384 | texture = light_storage->lightmap_get_texture(base); |
| 3385 | } else { |
| 3386 | // Shadowmask |
| 3387 | texture = light_storage->shadowmask_get_texture(base); |
| 3388 | } |
| 3389 | |
| 3390 | if (texture.is_valid()) { |
| 3391 | RID rd_texture = texture_storage->texture_get_rd_texture(texture); |
| 3392 | textures.write[i] = rd_texture; |
| 3393 | continue; |
| 3394 | } |
| 3395 | } |
| 3396 | |
| 3397 | textures.write[i] = default_tex; |
| 3398 | } |
| 3399 | RD::Uniform u(RD::UNIFORM_TYPE_TEXTURE, 7, textures); |
| 3400 | uniforms.push_back(u); |
| 3401 | } |
| 3402 | { |
| 3403 | RD::Uniform u; |
| 3404 | u.binding = 8; |
| 3405 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3406 | RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE); |
| 3407 | for (int i = 0; i < MAX_VOXEL_GI_INSTANCESS; i++) { |
| 3408 | if (p_render_data && i < (int)p_render_data->voxel_gi_instances->size()) { |
| 3409 | RID tex = gi.voxel_gi_instance_get_texture((*p_render_data->voxel_gi_instances)[i]); |
| 3410 | if (!tex.is_valid()) { |
| 3411 | tex = default_tex; |
| 3412 | } |
| 3413 | u.append_id(tex); |
| 3414 | } else { |
| 3415 | u.append_id(default_tex); |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | uniforms.push_back(u); |
| 3420 | } |
| 3421 | |
| 3422 | { |
| 3423 | RD::Uniform u; |
| 3424 | u.binding = 9; |
| 3425 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3426 | RID cb = (p_render_data && p_render_data->cluster_buffer.is_valid()) ? p_render_data->cluster_buffer : scene_shader.default_vec4_xform_buffer; |
| 3427 | u.append_id(cb); |
| 3428 | uniforms.push_back(u); |
| 3429 | } |
| 3430 | |
| 3431 | { |
| 3432 | RD::Uniform u; |
| 3433 | u.binding = 10; |
| 3434 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 3435 | RID sampler; |
| 3436 | switch (decals_get_filter()) { |
| 3437 | case RS::DECAL_FILTER_NEAREST: { |
| 3438 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3439 | } break; |
| 3440 | case RS::DECAL_FILTER_LINEAR: { |
| 3441 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3442 | } break; |
| 3443 | case RS::DECAL_FILTER_NEAREST_MIPMAPS: { |
| 3444 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3445 | } break; |
| 3446 | case RS::DECAL_FILTER_LINEAR_MIPMAPS: { |
| 3447 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3448 | } break; |
| 3449 | case RS::DECAL_FILTER_NEAREST_MIPMAPS_ANISOTROPIC: { |
| 3450 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3451 | } break; |
| 3452 | case RS::DECAL_FILTER_LINEAR_MIPMAPS_ANISOTROPIC: { |
| 3453 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3454 | } break; |
| 3455 | } |
| 3456 | |
| 3457 | u.append_id(sampler); |
| 3458 | uniforms.push_back(u); |
| 3459 | } |
| 3460 | |
| 3461 | { |
| 3462 | RD::Uniform u; |
| 3463 | u.binding = 11; |
| 3464 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 3465 | RID sampler; |
| 3466 | switch (light_projectors_get_filter()) { |
| 3467 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST: { |
| 3468 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3469 | } break; |
| 3470 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR: { |
| 3471 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3472 | } break; |
| 3473 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS: { |
| 3474 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3475 | } break; |
| 3476 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS: { |
| 3477 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3478 | } break; |
| 3479 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS_ANISOTROPIC: { |
| 3480 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3481 | } break; |
| 3482 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS_ANISOTROPIC: { |
| 3483 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3484 | } break; |
| 3485 | } |
| 3486 | |
| 3487 | u.append_id(sampler); |
| 3488 | uniforms.push_back(u); |
| 3489 | } |
| 3490 | |
| 3491 | p_samplers.append_uniforms(uniforms, 12); |
| 3492 | |
| 3493 | { |
| 3494 | RD::Uniform u; |
| 3495 | u.binding = 24; |
| 3496 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3497 | RID texture; |
| 3498 | if (rb.is_valid() && rb->has_texture(RB_SCOPE_BUFFERS, RB_TEX_BACK_DEPTH)) { |
| 3499 | texture = rb->get_texture(RB_SCOPE_BUFFERS, RB_TEX_BACK_DEPTH); |
| 3500 | } else { |
| 3501 | texture = texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_DEPTH : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH); |
| 3502 | } |
| 3503 | u.append_id(texture); |
| 3504 | uniforms.push_back(u); |
| 3505 | } |
| 3506 | { |
| 3507 | RD::Uniform u; |
| 3508 | u.binding = 25; |
| 3509 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3510 | RID bbt = rb_data.is_valid() ? rb->get_back_buffer_texture() : RID(); |
| 3511 | 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); |
| 3512 | u.append_id(texture); |
| 3513 | uniforms.push_back(u); |
| 3514 | } |
| 3515 | |
| 3516 | { |
| 3517 | RD::Uniform u; |
| 3518 | u.binding = 26; |
| 3519 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3520 | 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); |
| 3521 | u.append_id(texture); |
| 3522 | uniforms.push_back(u); |
| 3523 | } |
| 3524 | |
| 3525 | { |
| 3526 | RD::Uniform u; |
| 3527 | u.binding = 27; |
| 3528 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3529 | RID aot = rb.is_valid() && rb->has_texture(RB_SCOPE_SSAO, RB_FINAL) ? rb->get_texture(RB_SCOPE_SSAO, RB_FINAL) : RID(); |
| 3530 | 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); |
| 3531 | u.append_id(texture); |
| 3532 | uniforms.push_back(u); |
| 3533 | } |
| 3534 | |
| 3535 | { |
| 3536 | RD::Uniform u; |
| 3537 | u.binding = 28; |
| 3538 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3539 | 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); |
| 3540 | u.append_id(texture); |
| 3541 | uniforms.push_back(u); |
| 3542 | } |
| 3543 | |
| 3544 | { |
| 3545 | RD::Uniform u; |
| 3546 | u.binding = 29; |
| 3547 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3548 | 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); |
| 3549 | u.append_id(texture); |
| 3550 | uniforms.push_back(u); |
| 3551 | } |
| 3552 | { |
| 3553 | RD::Uniform u; |
| 3554 | u.binding = 30; |
| 3555 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3556 | RID t; |
| 3557 | if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_SDFGI)) { |
| 3558 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 3559 | t = sdfgi->lightprobe_texture; |
| 3560 | } |
| 3561 | if (t.is_null()) { |
| 3562 | t = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE); |
| 3563 | } |
| 3564 | u.append_id(t); |
| 3565 | uniforms.push_back(u); |
| 3566 | } |
| 3567 | { |
| 3568 | RD::Uniform u; |
| 3569 | u.binding = 31; |
| 3570 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3571 | RID t; |
| 3572 | if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_SDFGI)) { |
| 3573 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 3574 | t = sdfgi->occlusion_texture; |
| 3575 | } |
| 3576 | if (t.is_null()) { |
| 3577 | t = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE); |
| 3578 | } |
| 3579 | u.append_id(t); |
| 3580 | uniforms.push_back(u); |
| 3581 | } |
| 3582 | { |
| 3583 | RD::Uniform u; |
| 3584 | u.binding = 32; |
| 3585 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 3586 | RID voxel_gi; |
| 3587 | if (rb.is_valid() && rb->has_custom_data(RB_SCOPE_GI)) { |
| 3588 | Ref<RendererRD::GI::RenderBuffersGI> rbgi = rb->get_custom_data(RB_SCOPE_GI); |
| 3589 | voxel_gi = rbgi->get_voxel_gi_buffer(); |
| 3590 | } |
| 3591 | u.append_id(voxel_gi.is_valid() ? voxel_gi : render_buffers_get_default_voxel_gi_buffer()); |
| 3592 | uniforms.push_back(u); |
| 3593 | } |
| 3594 | { |
| 3595 | RD::Uniform u; |
| 3596 | u.binding = 33; |
| 3597 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3598 | RID vfog; |
| 3599 | if (rb_data.is_valid() && rb->has_custom_data(RB_SCOPE_FOG)) { |
| 3600 | Ref<RendererRD::Fog::VolumetricFog> fog = rb->get_custom_data(RB_SCOPE_FOG); |
| 3601 | vfog = fog->fog_map; |
| 3602 | if (vfog.is_null()) { |
| 3603 | vfog = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE); |
| 3604 | } |
| 3605 | } else { |
| 3606 | vfog = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE); |
| 3607 | } |
| 3608 | u.append_id(vfog); |
| 3609 | uniforms.push_back(u); |
| 3610 | } |
| 3611 | { |
| 3612 | RD::Uniform u; |
| 3613 | u.binding = 34; |
| 3614 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3615 | RID ssil = rb.is_valid() && rb->has_texture(RB_SCOPE_SSIL, RB_FINAL) ? rb->get_texture(RB_SCOPE_SSIL, RB_FINAL) : RID(); |
| 3616 | 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); |
| 3617 | u.append_id(texture); |
| 3618 | uniforms.push_back(u); |
| 3619 | } |
| 3620 | { |
| 3621 | RD::Uniform u; |
| 3622 | u.binding = 35; |
| 3623 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3624 | |
| 3625 | RID ssr; |
| 3626 | if (rb_data.is_valid()) { |
| 3627 | if (rb_data->ss_effects_data.ssr.half_size) { |
| 3628 | if (rb->has_texture(RB_SCOPE_SSR, RB_FINAL)) { |
| 3629 | ssr = rb->get_texture(RB_SCOPE_SSR, RB_FINAL); |
| 3630 | } |
| 3631 | } else { |
| 3632 | if (rb->has_texture(RB_SCOPE_SSR, RB_SSR)) { |
| 3633 | ssr = rb->get_texture(RB_SCOPE_SSR, RB_SSR); |
| 3634 | } |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | RID texture = ssr.is_valid() ? ssr : texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK); |
| 3639 | u.append_id(texture); |
| 3640 | uniforms.push_back(u); |
| 3641 | } |
| 3642 | { |
| 3643 | RD::Uniform u; |
| 3644 | u.binding = 36; |
| 3645 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3646 | |
| 3647 | RID ssr_mip_level = (rb_data.is_valid() && !rb_data->ss_effects_data.ssr.half_size && rb->has_texture(RB_SCOPE_SSR, RB_MIP_LEVEL)) ? rb->get_texture(RB_SCOPE_SSR, RB_MIP_LEVEL) : RID(); |
| 3648 | RID texture = ssr_mip_level.is_valid() ? ssr_mip_level : texture_storage->texture_rd_get_default(is_multiview ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK); |
| 3649 | u.append_id(texture); |
| 3650 | uniforms.push_back(u); |
| 3651 | } |
| 3652 | |
| 3653 | return UniformSetCacheRD::get_singleton()->get_cache_vec(scene_shader.default_shader_rd, RENDER_PASS_UNIFORM_SET, uniforms); |
| 3654 | } |
| 3655 | |
| 3656 | RID 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, const RendererRD::MaterialStorage::Samplers &p_samplers) { |
| 3657 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 3658 | thread_local LocalVector<RD::Uniform> uniforms; |
| 3659 | uniforms.clear(); |
| 3660 | |
| 3661 | { |
| 3662 | RD::Uniform u; |
| 3663 | u.binding = 0; |
| 3664 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 3665 | u.append_id(scene_state.uniform_buffers[0]); |
| 3666 | uniforms.push_back(u); |
| 3667 | } |
| 3668 | { |
| 3669 | RD::Uniform u; |
| 3670 | u.binding = 1; |
| 3671 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 3672 | u.append_id(scene_state.implementation_uniform_buffers[0]); |
| 3673 | uniforms.push_back(u); |
| 3674 | } |
| 3675 | { |
| 3676 | RD::Uniform u; |
| 3677 | u.binding = 2; |
| 3678 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC; |
| 3679 | if (scene_state.instance_buffer[RENDER_LIST_SECONDARY].get_size(0u) == 0u) { |
| 3680 | // Any buffer will do since it's not used, so just create one. |
| 3681 | // We can't use scene_shader.default_vec4_xform_buffer because it's not dynamic. |
| 3682 | scene_state.instance_buffer[RENDER_LIST_SECONDARY].set_storage_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData)); |
| 3683 | scene_state.instance_buffer[RENDER_LIST_SECONDARY].prepare_for_upload(); |
| 3684 | } |
| 3685 | RID instance_buffer = scene_state.instance_buffer[RENDER_LIST_SECONDARY]._get(0u); |
| 3686 | u.append_id(instance_buffer); |
| 3687 | uniforms.push_back(u); |
| 3688 | } |
| 3689 | { |
| 3690 | // No radiance texture. |
| 3691 | RID radiance_texture = texture_storage->texture_rd_get_default(is_using_radiance_octmap_array() ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK); |
| 3692 | RD::Uniform u; |
| 3693 | u.binding = 3; |
| 3694 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3695 | u.append_id(radiance_texture); |
| 3696 | uniforms.push_back(u); |
| 3697 | } |
| 3698 | |
| 3699 | { |
| 3700 | // No reflection atlas. |
| 3701 | RID ref_texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK); |
| 3702 | RD::Uniform u; |
| 3703 | u.binding = 4; |
| 3704 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3705 | u.append_id(ref_texture); |
| 3706 | uniforms.push_back(u); |
| 3707 | } |
| 3708 | |
| 3709 | { |
| 3710 | // No shadow atlas. |
| 3711 | RD::Uniform u; |
| 3712 | u.binding = 5; |
| 3713 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3714 | RID texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH); |
| 3715 | u.append_id(texture); |
| 3716 | uniforms.push_back(u); |
| 3717 | } |
| 3718 | |
| 3719 | { |
| 3720 | // No directional shadow atlas. |
| 3721 | RD::Uniform u; |
| 3722 | u.binding = 6; |
| 3723 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3724 | RID texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH); |
| 3725 | u.append_id(texture); |
| 3726 | uniforms.push_back(u); |
| 3727 | } |
| 3728 | |
| 3729 | { |
| 3730 | // No Lightmaps |
| 3731 | RD::Uniform u; |
| 3732 | u.binding = 7; |
| 3733 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3734 | |
| 3735 | RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE); |
| 3736 | for (uint32_t i = 0; i < scene_state.max_lightmaps * 2; i++) { |
| 3737 | u.append_id(default_tex); |
| 3738 | } |
| 3739 | |
| 3740 | uniforms.push_back(u); |
| 3741 | } |
| 3742 | |
| 3743 | { |
| 3744 | // No VoxelGIs |
| 3745 | RD::Uniform u; |
| 3746 | u.binding = 8; |
| 3747 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 3748 | |
| 3749 | RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE); |
| 3750 | for (int i = 0; i < MAX_VOXEL_GI_INSTANCESS; i++) { |
| 3751 | u.append_id(default_tex); |
| 3752 | } |
| 3753 | |
| 3754 | uniforms.push_back(u); |
| 3755 | } |
| 3756 | |
| 3757 | { |
| 3758 | RD::Uniform u; |
| 3759 | u.binding = 9; |
| 3760 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 3761 | RID cb = scene_shader.default_vec4_xform_buffer; |
| 3762 | u.append_id(cb); |
| 3763 | uniforms.push_back(u); |
| 3764 | } |
| 3765 | |
| 3766 | { |
| 3767 | RD::Uniform u; |
| 3768 | u.binding = 10; |
| 3769 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 3770 | RID sampler; |
| 3771 | switch (decals_get_filter()) { |
| 3772 | case RS::DECAL_FILTER_NEAREST: { |
| 3773 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3774 | } break; |
| 3775 | case RS::DECAL_FILTER_LINEAR: { |
| 3776 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3777 | } break; |
| 3778 | case RS::DECAL_FILTER_NEAREST_MIPMAPS: { |
| 3779 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3780 | } break; |
| 3781 | case RS::DECAL_FILTER_LINEAR_MIPMAPS: { |
| 3782 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3783 | } break; |
| 3784 | case RS::DECAL_FILTER_NEAREST_MIPMAPS_ANISOTROPIC: { |
| 3785 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3786 | } break; |
| 3787 | case RS::DECAL_FILTER_LINEAR_MIPMAPS_ANISOTROPIC: { |
| 3788 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3789 | } break; |
| 3790 | } |
| 3791 | |
| 3792 | u.append_id(sampler); |
| 3793 | uniforms.push_back(u); |
| 3794 | } |
| 3795 | |
| 3796 | { |
| 3797 | RD::Uniform u; |
| 3798 | u.binding = 11; |
| 3799 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 3800 | RID sampler; |
| 3801 | switch (light_projectors_get_filter()) { |
| 3802 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST: { |
| 3803 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3804 | } break; |
| 3805 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR: { |
| 3806 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3807 | } break; |
| 3808 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS: { |
| 3809 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3810 | } break; |
| 3811 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS: { |
| 3812 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3813 | } break; |
| 3814 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS_ANISOTROPIC: { |
| 3815 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3816 | } break; |
| 3817 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS_ANISOTROPIC: { |
| 3818 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 3819 | } break; |
| 3820 | } |
| 3821 | |
| 3822 | u.append_id(sampler); |
| 3823 | uniforms.push_back(u); |
| 3824 | } |
| 3825 | |
| 3826 | p_samplers.append_uniforms(uniforms, 12); |
| 3827 | |
| 3828 | // actual sdfgi stuff |
| 3829 | |
| 3830 | { |
| 3831 | RD::Uniform u; |
| 3832 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
| 3833 | u.binding = 24; |
| 3834 | u.append_id(p_albedo_texture); |
| 3835 | uniforms.push_back(u); |
| 3836 | } |
| 3837 | { |
| 3838 | RD::Uniform u; |
| 3839 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
| 3840 | u.binding = 25; |
| 3841 | u.append_id(p_emission_texture); |
| 3842 | uniforms.push_back(u); |
| 3843 | } |
| 3844 | { |
| 3845 | RD::Uniform u; |
| 3846 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
| 3847 | u.binding = 26; |
| 3848 | u.append_id(p_emission_aniso_texture); |
| 3849 | uniforms.push_back(u); |
| 3850 | } |
| 3851 | { |
| 3852 | RD::Uniform u; |
| 3853 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
| 3854 | u.binding = 27; |
| 3855 | u.append_id(p_geom_facing_texture); |
| 3856 | uniforms.push_back(u); |
| 3857 | } |
| 3858 | |
| 3859 | if (scene_shader.default_shader_sdfgi_rd.is_null()) { |
| 3860 | // The variant for SDF from the default material should only be retrieved when SDFGI is required. |
| 3861 | ERR_FAIL_NULL_V(scene_shader.default_material_shader_ptr, RID()); |
| 3862 | scene_shader.enable_advanced_shader_group(); |
| 3863 | scene_shader.default_shader_sdfgi_rd = scene_shader.default_material_shader_ptr->get_shader_variant(SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_SDF, 0, true); |
| 3864 | ERR_FAIL_COND_V(scene_shader.default_shader_sdfgi_rd.is_null(), RID()); |
| 3865 | } |
| 3866 | |
| 3867 | return UniformSetCacheRD::get_singleton()->get_cache_vec(scene_shader.default_shader_sdfgi_rd, RENDER_PASS_UNIFORM_SET, uniforms); |
| 3868 | } |
| 3869 | |
| 3870 | RID RenderForwardClustered::_render_buffers_get_normal_texture(Ref<RenderSceneBuffersRD> p_render_buffers) { |
| 3871 | Ref<RenderBufferDataForwardClustered> rb_data = p_render_buffers->get_custom_data(RB_SCOPE_FORWARD_CLUSTERED); |
| 3872 | |
| 3873 | return rb_data->get_normal_roughness(); |
| 3874 | } |
| 3875 | |
| 3876 | RID RenderForwardClustered::_render_buffers_get_velocity_texture(Ref<RenderSceneBuffersRD> p_render_buffers) { |
| 3877 | return p_render_buffers->get_velocity_buffer(false); |
| 3878 | } |
| 3879 | |
| 3880 | void 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) { |
| 3881 | ERR_FAIL_NULL(ss_effects); |
| 3882 | ERR_FAIL_COND(p_quality < RS::EnvironmentSSAOQuality::ENV_SSAO_QUALITY_VERY_LOW || p_quality > RS::EnvironmentSSAOQuality::ENV_SSAO_QUALITY_ULTRA); |
| 3883 | ss_effects->ssao_set_quality(p_quality, p_half_size, p_adaptive_target, p_blur_passes, p_fadeout_from, p_fadeout_to); |
| 3884 | } |
| 3885 | |
| 3886 | void 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) { |
| 3887 | ERR_FAIL_NULL(ss_effects); |
| 3888 | ERR_FAIL_COND(p_quality < RS::EnvironmentSSILQuality::ENV_SSIL_QUALITY_VERY_LOW || p_quality > RS::EnvironmentSSILQuality::ENV_SSIL_QUALITY_ULTRA); |
| 3889 | ss_effects->ssil_set_quality(p_quality, p_half_size, p_adaptive_target, p_blur_passes, p_fadeout_from, p_fadeout_to); |
| 3890 | } |
| 3891 | |
| 3892 | void RenderForwardClustered::environment_set_ssr_half_size(bool p_half_size) { |
| 3893 | ERR_FAIL_NULL(ss_effects); |
| 3894 | ss_effects->ssr_set_half_size(p_half_size); |
| 3895 | } |
| 3896 | |
| 3897 | void RenderForwardClustered::environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) { |
| 3898 | WARN_PRINT_ONCE("environment_set_ssr_roughness_quality has been deprecated and no longer does anything."); |
| 3899 | } |
| 3900 | |
| 3901 | void RenderForwardClustered::sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) { |
| 3902 | ERR_FAIL_NULL(ss_effects); |
| 3903 | ERR_FAIL_COND(p_quality < RS::SubSurfaceScatteringQuality::SUB_SURFACE_SCATTERING_QUALITY_DISABLED || p_quality > RS::SubSurfaceScatteringQuality::SUB_SURFACE_SCATTERING_QUALITY_HIGH); |
| 3904 | ss_effects->sss_set_quality(p_quality); |
| 3905 | } |
| 3906 | |
| 3907 | void RenderForwardClustered::sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) { |
| 3908 | ERR_FAIL_NULL(ss_effects); |
| 3909 | ss_effects->sss_set_scale(p_scale, p_depth_scale); |
| 3910 | } |
| 3911 | |
| 3912 | RenderForwardClustered *RenderForwardClustered::singleton = nullptr; |
| 3913 | |
| 3914 | void RenderForwardClustered::sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) { |
| 3915 | Ref<RenderSceneBuffersRD> rb = p_render_buffers; |
| 3916 | ERR_FAIL_COND(rb.is_null()); |
| 3917 | Ref<RendererRD::GI::SDFGI> sdfgi; |
| 3918 | if (rb->has_custom_data(RB_SCOPE_SDFGI)) { |
| 3919 | sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 3920 | } |
| 3921 | |
| 3922 | bool needs_sdfgi = p_environment.is_valid() && environment_get_sdfgi_enabled(p_environment); |
| 3923 | bool needs_reset = sdfgi.is_valid() ? sdfgi->version != gi.sdfgi_current_version : false; |
| 3924 | |
| 3925 | if (!needs_sdfgi || needs_reset) { |
| 3926 | if (sdfgi.is_valid()) { |
| 3927 | // delete it |
| 3928 | sdfgi.unref(); |
| 3929 | rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi); |
| 3930 | } |
| 3931 | |
| 3932 | if (!needs_sdfgi) { |
| 3933 | return; |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | // Ensure advanced shaders are available if SDFGI is used. |
| 3938 | // Call here as this is the first entry point for SDFGI. |
| 3939 | scene_shader.enable_advanced_shader_group(); |
| 3940 | |
| 3941 | static const uint32_t history_frames_to_converge[RS::ENV_SDFGI_CONVERGE_MAX] = { 5, 10, 15, 20, 25, 30 }; |
| 3942 | uint32_t requested_history_size = history_frames_to_converge[gi.sdfgi_frames_to_converge]; |
| 3943 | |
| 3944 | 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))) { |
| 3945 | //configuration changed, erase |
| 3946 | sdfgi.unref(); |
| 3947 | rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi); |
| 3948 | } |
| 3949 | |
| 3950 | if (sdfgi.is_null()) { |
| 3951 | // re-create |
| 3952 | sdfgi = gi.create_sdfgi(p_environment, p_world_position, requested_history_size); |
| 3953 | rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi); |
| 3954 | } else { |
| 3955 | //check for updates |
| 3956 | sdfgi->update(p_environment, p_world_position); |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | int RenderForwardClustered::sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const { |
| 3961 | Ref<RenderSceneBuffersRD> rb = p_render_buffers; |
| 3962 | ERR_FAIL_COND_V(rb.is_null(), 0); |
| 3963 | |
| 3964 | if (!rb->has_custom_data(RB_SCOPE_SDFGI)) { |
| 3965 | return 0; |
| 3966 | } |
| 3967 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 3968 | |
| 3969 | int dirty_count = 0; |
| 3970 | for (const RendererRD::GI::SDFGI::Cascade &c : sdfgi->cascades) { |
| 3971 | if (c.dirty_regions == RendererRD::GI::SDFGI::Cascade::DIRTY_ALL) { |
| 3972 | dirty_count++; |
| 3973 | } else { |
| 3974 | for (int j = 0; j < 3; j++) { |
| 3975 | if (c.dirty_regions[j] != 0) { |
| 3976 | dirty_count++; |
| 3977 | } |
| 3978 | } |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | return dirty_count; |
| 3983 | } |
| 3984 | |
| 3985 | AABB RenderForwardClustered::sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const { |
| 3986 | AABB bounds; |
| 3987 | Vector3i from; |
| 3988 | Vector3i size; |
| 3989 | |
| 3990 | Ref<RenderSceneBuffersRD> rb = p_render_buffers; |
| 3991 | ERR_FAIL_COND_V(rb.is_null(), AABB()); |
| 3992 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 3993 | ERR_FAIL_COND_V(sdfgi.is_null(), AABB()); |
| 3994 | |
| 3995 | int c = sdfgi->get_pending_region_data(p_region, from, size, bounds); |
| 3996 | ERR_FAIL_COND_V(c == -1, AABB()); |
| 3997 | return bounds; |
| 3998 | } |
| 3999 | |
| 4000 | uint32_t RenderForwardClustered::sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const { |
| 4001 | AABB bounds; |
| 4002 | Vector3i from; |
| 4003 | Vector3i size; |
| 4004 | |
| 4005 | Ref<RenderSceneBuffersRD> rb = p_render_buffers; |
| 4006 | ERR_FAIL_COND_V(rb.is_null(), -1); |
| 4007 | Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI); |
| 4008 | ERR_FAIL_COND_V(sdfgi.is_null(), -1); |
| 4009 | |
| 4010 | return sdfgi->get_pending_region_data(p_region, from, size, bounds); |
| 4011 | } |
| 4012 | |
| 4013 | void RenderForwardClustered::GeometryInstanceForwardClustered::_mark_dirty() { |
| 4014 | if (dirty_list_element.in_list()) { |
| 4015 | return; |
| 4016 | } |
| 4017 | |
| 4018 | //clear surface caches |
| 4019 | GeometryInstanceSurfaceDataCache *surf = surface_caches; |
| 4020 | |
| 4021 | while (surf) { |
| 4022 | GeometryInstanceSurfaceDataCache *next = surf->next; |
| 4023 | RenderForwardClustered::get_singleton()->geometry_instance_surface_alloc.free(surf); |
| 4024 | surf = next; |
| 4025 | } |
| 4026 | |
| 4027 | surface_caches = nullptr; |
| 4028 | |
| 4029 | RenderForwardClustered::get_singleton()->geometry_instance_dirty_list.add(&dirty_list_element); |
| 4030 | } |
| 4031 | |
| 4032 | void RenderForwardClustered::_update_global_pipeline_data_requirements_from_project() { |
| 4033 | const int msaa_3d_mode = GLOBAL_GET_CACHED(int, "rendering/anti_aliasing/quality/msaa_3d"); |
| 4034 | const bool directional_shadow_16_bits = GLOBAL_GET_CACHED(bool, "rendering/lights_and_shadows/directional_shadow/16_bits"); |
| 4035 | const bool positional_shadow_16_bits = GLOBAL_GET_CACHED(bool, "rendering/lights_and_shadows/positional_shadow/atlas_16_bits"); |
| 4036 | global_pipeline_data_required.use_16_bit_shadows = directional_shadow_16_bits || positional_shadow_16_bits; |
| 4037 | global_pipeline_data_required.use_32_bit_shadows = !directional_shadow_16_bits || !positional_shadow_16_bits; |
| 4038 | global_pipeline_data_required.texture_samples = RenderSceneBuffersRD::msaa_to_samples(RS::ViewportMSAA(msaa_3d_mode)); |
| 4039 | } |
| 4040 | |
| 4041 | void RenderForwardClustered::_update_global_pipeline_data_requirements_from_light_storage() { |
| 4042 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 4043 | global_pipeline_data_required.use_shadow_cubemaps = light_storage->get_shadow_cubemaps_used(); |
| 4044 | global_pipeline_data_required.use_shadow_dual_paraboloid = light_storage->get_shadow_dual_paraboloid_used(); |
| 4045 | } |
| 4046 | |
| 4047 | void 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) { |
| 4048 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 4049 | uint32_t flags = 0; |
| 4050 | |
| 4051 | if (p_material->shader_data->uses_sss) { |
| 4052 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SUBSURFACE_SCATTERING; |
| 4053 | global_surface_data.sss_used = true; |
| 4054 | } |
| 4055 | |
| 4056 | if (p_material->shader_data->uses_screen_texture) { |
| 4057 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SCREEN_TEXTURE; |
| 4058 | global_surface_data.screen_texture_used = true; |
| 4059 | } |
| 4060 | |
| 4061 | if (p_material->shader_data->uses_depth_texture) { |
| 4062 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_DEPTH_TEXTURE; |
| 4063 | global_surface_data.depth_texture_used = true; |
| 4064 | } |
| 4065 | |
| 4066 | if (p_material->shader_data->uses_normal_texture) { |
| 4067 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_NORMAL_TEXTURE; |
| 4068 | global_surface_data.normal_texture_used = true; |
| 4069 | } |
| 4070 | |
| 4071 | if (ginstance->data->cast_double_sided_shadows) { |
| 4072 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_DOUBLE_SIDED_SHADOWS; |
| 4073 | } |
| 4074 | |
| 4075 | if (p_material->shader_data->stencil_enabled) { |
| 4076 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_STENCIL; |
| 4077 | } |
| 4078 | |
| 4079 | if (p_material->shader_data->uses_alpha_pass()) { |
| 4080 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA; |
| 4081 | if (p_material->shader_data->uses_depth_in_alpha_pass()) { |
| 4082 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH; |
| 4083 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW; |
| 4084 | } |
| 4085 | } else { |
| 4086 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE; |
| 4087 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH; |
| 4088 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW; |
| 4089 | } |
| 4090 | |
| 4091 | if (p_material->shader_data->uses_particle_trails) { |
| 4092 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_PARTICLE_TRAILS; |
| 4093 | } |
| 4094 | |
| 4095 | if (p_material->shader_data->is_animated()) { |
| 4096 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_MOTION_VECTOR; |
| 4097 | } |
| 4098 | |
| 4099 | if (p_material->shader_data->stencil_enabled) { |
| 4100 | if (p_material->shader_data->stencil_flags & SceneShaderForwardClustered::ShaderData::STENCIL_FLAG_READ) { |
| 4101 | // Stencil materials which read from the stencil buffer must be in the alpha pass. |
| 4102 | // This is critical to preserve compatibility once we'll have the compositor. |
| 4103 | if (!(flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA)) { |
| 4104 | String shader_path = p_material->shader_data->path.is_empty() ? "" : "(" + p_material->shader_data->path + ")"; |
| 4105 | ERR_PRINT_ED(vformat("Attempting to use a shader %s that reads stencil but is not in the alpha queue. Ensure the material uses alpha blending or has depth_draw disabled or depth_test disabled.", shader_path)); |
| 4106 | } |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | SceneShaderForwardClustered::MaterialData *material_shadow = nullptr; |
| 4111 | void *surface_shadow = nullptr; |
| 4112 | if (p_material->shader_data->uses_shared_shadow_material()) { |
| 4113 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SHARED_SHADOW_MATERIAL; |
| 4114 | material_shadow = static_cast<SceneShaderForwardClustered::MaterialData *>(RendererRD::MaterialStorage::get_singleton()->material_get_data(scene_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 4115 | |
| 4116 | RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh); |
| 4117 | if (shadow_mesh.is_valid()) { |
| 4118 | surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, p_surface); |
| 4119 | } |
| 4120 | } else { |
| 4121 | material_shadow = p_material; |
| 4122 | } |
| 4123 | |
| 4124 | GeometryInstanceSurfaceDataCache *sdcache = geometry_instance_surface_alloc.alloc(); |
| 4125 | |
| 4126 | sdcache->flags = flags; |
| 4127 | |
| 4128 | sdcache->shader = p_material->shader_data; |
| 4129 | sdcache->material = p_material; |
| 4130 | sdcache->material_uniform_set = p_material->uniform_set; |
| 4131 | sdcache->surface = mesh_storage->mesh_get_surface(p_mesh, p_surface); |
| 4132 | sdcache->primitive = mesh_storage->mesh_surface_get_primitive(sdcache->surface); |
| 4133 | sdcache->surface_index = p_surface; |
| 4134 | |
| 4135 | if (ginstance->data->dirty_dependencies) { |
| 4136 | RSG::utilities->base_update_dependency(p_mesh, &ginstance->data->dependency_tracker); |
| 4137 | } |
| 4138 | |
| 4139 | //shadow |
| 4140 | sdcache->shader_shadow = material_shadow->shader_data; |
| 4141 | sdcache->material_uniform_set_shadow = material_shadow->uniform_set; |
| 4142 | |
| 4143 | sdcache->surface_shadow = surface_shadow ? surface_shadow : sdcache->surface; |
| 4144 | |
| 4145 | sdcache->owner = ginstance; |
| 4146 | |
| 4147 | sdcache->next = ginstance->surface_caches; |
| 4148 | ginstance->surface_caches = sdcache; |
| 4149 | |
| 4150 | //sortkey |
| 4151 | |
| 4152 | sdcache->sort.sort_key1 = 0; |
| 4153 | sdcache->sort.sort_key2 = 0; |
| 4154 | |
| 4155 | sdcache->sort.surface_index = p_surface; |
| 4156 | sdcache->sort.material_id_hi = (p_material_id & 0xFF000000) >> 24; |
| 4157 | sdcache->sort.material_id_lo = (p_material_id & 0x00FFFFFF); |
| 4158 | sdcache->sort.shader_id = p_shader_id; |
| 4159 | sdcache->sort.geometry_id = p_mesh.get_local_index(); //only meshes can repeat anyway |
| 4160 | sdcache->sort.uses_forward_gi = ginstance->can_sdfgi; |
| 4161 | sdcache->sort.priority = p_material->priority; |
| 4162 | sdcache->sort.uses_projector = ginstance->using_projectors; |
| 4163 | sdcache->sort.uses_softshadow = ginstance->using_softshadows; |
| 4164 | |
| 4165 | uint64_t format = RendererRD::MeshStorage::get_singleton()->mesh_surface_get_format(sdcache->surface); |
| 4166 | if (p_material->shader_data->uses_tangent && !(format & RS::ARRAY_FORMAT_TANGENT)) { |
| 4167 | String shader_path = p_material->shader_data->path.is_empty() ? "" : "(" + p_material->shader_data->path + ")"; |
| 4168 | String mesh_path = mesh_storage->mesh_get_path(p_mesh).is_empty() ? "" : "(" + mesh_storage->mesh_get_path(p_mesh) + ")"; |
| 4169 | WARN_PRINT_ED(vformat("Attempting to use a shader %s that requires tangents with a mesh %s that doesn't contain tangents. Ensure that meshes are imported with the 'ensure_tangents' option. If creating your own meshes, add an `ARRAY_TANGENT` array (when using ArrayMesh) or call `generate_tangents()` (when using SurfaceTool).", shader_path, mesh_path)); |
| 4170 | } |
| 4171 | |
| 4172 | #if PRELOAD_PIPELINES_ON_SURFACE_CACHE_CONSTRUCTION |
| 4173 | if (!sdcache->compilation_dirty_element.in_list()) { |
| 4174 | geometry_surface_compilation_dirty_list.add(&sdcache->compilation_dirty_element); |
| 4175 | } |
| 4176 | |
| 4177 | if (!sdcache->compilation_all_element.in_list()) { |
| 4178 | geometry_surface_compilation_all_list.add(&sdcache->compilation_all_element); |
| 4179 | } |
| 4180 | #endif |
| 4181 | } |
| 4182 | |
| 4183 | void 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) { |
| 4184 | SceneShaderForwardClustered::MaterialData *material = p_material; |
| 4185 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 4186 | |
| 4187 | _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); |
| 4188 | |
| 4189 | while (material->next_pass.is_valid()) { |
| 4190 | RID next_pass = material->next_pass; |
| 4191 | material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(next_pass, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 4192 | if (!material || !material->shader_data->is_valid()) { |
| 4193 | break; |
| 4194 | } |
| 4195 | if (ginstance->data->dirty_dependencies) { |
| 4196 | material_storage->material_update_dependency(next_pass, &ginstance->data->dependency_tracker); |
| 4197 | } |
| 4198 | _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); |
| 4199 | } |
| 4200 | } |
| 4201 | |
| 4202 | void RenderForwardClustered::_geometry_instance_add_surface(GeometryInstanceForwardClustered *ginstance, uint32_t p_surface, RID p_material, RID p_mesh) { |
| 4203 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 4204 | RID m_src; |
| 4205 | |
| 4206 | m_src = ginstance->data->material_override.is_valid() ? ginstance->data->material_override : p_material; |
| 4207 | |
| 4208 | SceneShaderForwardClustered::MaterialData *material = nullptr; |
| 4209 | |
| 4210 | if (m_src.is_valid()) { |
| 4211 | material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(m_src, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 4212 | if (!material || !material->shader_data->is_valid()) { |
| 4213 | material = nullptr; |
| 4214 | } |
| 4215 | } |
| 4216 | |
| 4217 | if (material) { |
| 4218 | if (ginstance->data->dirty_dependencies) { |
| 4219 | material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker); |
| 4220 | } |
| 4221 | } else { |
| 4222 | material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(scene_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 4223 | m_src = scene_shader.default_material; |
| 4224 | } |
| 4225 | |
| 4226 | ERR_FAIL_NULL(material); |
| 4227 | |
| 4228 | _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material, m_src, p_mesh); |
| 4229 | |
| 4230 | if (ginstance->data->material_overlay.is_valid()) { |
| 4231 | m_src = ginstance->data->material_overlay; |
| 4232 | |
| 4233 | material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(m_src, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 4234 | if (material && material->shader_data->is_valid()) { |
| 4235 | if (ginstance->data->dirty_dependencies) { |
| 4236 | material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker); |
| 4237 | } |
| 4238 | |
| 4239 | _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material, m_src, p_mesh); |
| 4240 | } |
| 4241 | } |
| 4242 | } |
| 4243 | |
| 4244 | void RenderForwardClustered::_geometry_instance_update(RenderGeometryInstance *p_geometry_instance) { |
| 4245 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 4246 | RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton(); |
| 4247 | GeometryInstanceForwardClustered *ginstance = static_cast<GeometryInstanceForwardClustered *>(p_geometry_instance); |
| 4248 | |
| 4249 | if (ginstance->data->dirty_dependencies) { |
| 4250 | ginstance->data->dependency_tracker.update_begin(); |
| 4251 | } |
| 4252 | |
| 4253 | //add geometry for drawing |
| 4254 | switch (ginstance->data->base_type) { |
| 4255 | case RS::INSTANCE_MESH: { |
| 4256 | const RID *materials = nullptr; |
| 4257 | uint32_t surface_count; |
| 4258 | RID mesh = ginstance->data->base; |
| 4259 | |
| 4260 | materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count); |
| 4261 | if (materials) { |
| 4262 | //if no materials, no surfaces. |
| 4263 | const RID *inst_materials = ginstance->data->surface_materials.ptr(); |
| 4264 | uint32_t surf_mat_count = ginstance->data->surface_materials.size(); |
| 4265 | |
| 4266 | for (uint32_t j = 0; j < surface_count; j++) { |
| 4267 | RID material = (j < surf_mat_count && inst_materials[j].is_valid()) ? inst_materials[j] : materials[j]; |
| 4268 | _geometry_instance_add_surface(ginstance, j, material, mesh); |
| 4269 | } |
| 4270 | } |
| 4271 | |
| 4272 | ginstance->instance_count = 1; |
| 4273 | |
| 4274 | } break; |
| 4275 | |
| 4276 | case RS::INSTANCE_MULTIMESH: { |
| 4277 | RID mesh = mesh_storage->multimesh_get_mesh(ginstance->data->base); |
| 4278 | if (mesh.is_valid()) { |
| 4279 | const RID *materials = nullptr; |
| 4280 | uint32_t surface_count; |
| 4281 | |
| 4282 | materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count); |
| 4283 | if (materials) { |
| 4284 | for (uint32_t j = 0; j < surface_count; j++) { |
| 4285 | _geometry_instance_add_surface(ginstance, j, materials[j], mesh); |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | ginstance->instance_count = mesh_storage->multimesh_get_instances_to_draw(ginstance->data->base); |
| 4290 | } |
| 4291 | |
| 4292 | } break; |
| 4293 | #if 0 |
| 4294 | case RS::INSTANCE_IMMEDIATE: { |
| 4295 | RasterizerStorageGLES3::Immediate *immediate = storage->immediate_owner.get_or_null(inst->base); |
| 4296 | ERR_CONTINUE(!immediate); |
| 4297 | |
| 4298 | _add_geometry(immediate, inst, nullptr, -1, p_depth_pass, p_shadow_pass); |
| 4299 | |
| 4300 | } break; |
| 4301 | #endif |
| 4302 | case RS::INSTANCE_PARTICLES: { |
| 4303 | int draw_passes = particles_storage->particles_get_draw_passes(ginstance->data->base); |
| 4304 | |
| 4305 | for (int j = 0; j < draw_passes; j++) { |
| 4306 | RID mesh = particles_storage->particles_get_draw_pass_mesh(ginstance->data->base, j); |
| 4307 | if (!mesh.is_valid()) { |
| 4308 | continue; |
| 4309 | } |
| 4310 | |
| 4311 | const RID *materials = nullptr; |
| 4312 | uint32_t surface_count; |
| 4313 | |
| 4314 | materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count); |
| 4315 | if (materials) { |
| 4316 | for (uint32_t k = 0; k < surface_count; k++) { |
| 4317 | _geometry_instance_add_surface(ginstance, k, materials[k], mesh); |
| 4318 | } |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | ginstance->instance_count = particles_storage->particles_get_amount(ginstance->data->base, ginstance->trail_steps); |
| 4323 | |
| 4324 | } break; |
| 4325 | |
| 4326 | default: { |
| 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | //Fill push constant |
| 4331 | |
| 4332 | ginstance->base_flags = 0; |
| 4333 | |
| 4334 | bool store_transform = true; |
| 4335 | if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) { |
| 4336 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH; |
| 4337 | |
| 4338 | if (mesh_storage->multimesh_get_transform_format(ginstance->data->base) == RS::MULTIMESH_TRANSFORM_2D) { |
| 4339 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D; |
| 4340 | } |
| 4341 | if (mesh_storage->multimesh_uses_colors(ginstance->data->base)) { |
| 4342 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR; |
| 4343 | } |
| 4344 | if (mesh_storage->multimesh_uses_custom_data(ginstance->data->base)) { |
| 4345 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA; |
| 4346 | } |
| 4347 | if (mesh_storage->multimesh_uses_indirect(ginstance->data->base)) { |
| 4348 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_INDIRECT; |
| 4349 | } |
| 4350 | |
| 4351 | ginstance->transforms_uniform_set = mesh_storage->multimesh_get_3d_uniform_set(ginstance->data->base, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET); |
| 4352 | |
| 4353 | } else if (ginstance->data->base_type == RS::INSTANCE_PARTICLES) { |
| 4354 | ginstance->base_flags |= INSTANCE_DATA_FLAG_PARTICLES; |
| 4355 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH; |
| 4356 | |
| 4357 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR; |
| 4358 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA; |
| 4359 | |
| 4360 | //for particles, stride is the trail size |
| 4361 | ginstance->base_flags |= (ginstance->trail_steps << INSTANCE_DATA_FLAGS_PARTICLE_TRAIL_SHIFT); |
| 4362 | |
| 4363 | if (!particles_storage->particles_is_using_local_coords(ginstance->data->base)) { |
| 4364 | store_transform = false; |
| 4365 | } |
| 4366 | ginstance->transforms_uniform_set = particles_storage->particles_get_instance_buffer_uniform_set(ginstance->data->base, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET); |
| 4367 | |
| 4368 | if (particles_storage->particles_get_frame_counter(ginstance->data->base) == 0) { |
| 4369 | // Particles haven't been cleared or updated, update once now to ensure they are ready to render. |
| 4370 | particles_storage->update_particles(); |
| 4371 | } |
| 4372 | |
| 4373 | if (ginstance->data->dirty_dependencies) { |
| 4374 | particles_storage->particles_update_dependency(ginstance->data->base, &ginstance->data->dependency_tracker); |
| 4375 | } |
| 4376 | } else if (ginstance->data->base_type == RS::INSTANCE_MESH) { |
| 4377 | if (mesh_storage->skeleton_is_valid(ginstance->data->skeleton)) { |
| 4378 | ginstance->transforms_uniform_set = mesh_storage->skeleton_get_3d_uniform_set(ginstance->data->skeleton, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET); |
| 4379 | if (ginstance->data->dirty_dependencies) { |
| 4380 | mesh_storage->skeleton_update_dependency(ginstance->data->skeleton, &ginstance->data->dependency_tracker); |
| 4381 | } |
| 4382 | } else { |
| 4383 | ginstance->transforms_uniform_set = RID(); |
| 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | ginstance->store_transform_cache = store_transform; |
| 4388 | ginstance->can_sdfgi = false; |
| 4389 | |
| 4390 | if (!RendererRD::LightStorage::get_singleton()->lightmap_instance_is_valid(ginstance->lightmap_instance)) { |
| 4391 | if (ginstance->voxel_gi_instances[0].is_null() && (ginstance->data->use_baked_light || ginstance->data->use_dynamic_gi)) { |
| 4392 | ginstance->can_sdfgi = true; |
| 4393 | } |
| 4394 | } |
| 4395 | |
| 4396 | if (ginstance->data->dirty_dependencies) { |
| 4397 | ginstance->data->dependency_tracker.update_end(); |
| 4398 | ginstance->data->dirty_dependencies = false; |
| 4399 | } |
| 4400 | |
| 4401 | ginstance->dirty_list_element.remove_from_list(); |
| 4402 | } |
| 4403 | |
| 4404 | static RD::FramebufferFormatID _get_color_framebuffer_format_for_pipeline(RD::DataFormat p_color_format, bool p_can_be_storage, RD::TextureSamples p_samples, bool p_specular, bool p_velocity, uint32_t p_view_count) { |
| 4405 | const bool multisampling = p_samples > RD::TEXTURE_SAMPLES_1; |
| 4406 | RD::AttachmentFormat attachment; |
| 4407 | attachment.samples = p_samples; |
| 4408 | |
| 4409 | RD::AttachmentFormat unused_attachment; |
| 4410 | unused_attachment.usage_flags = RD::AttachmentFormat::UNUSED_ATTACHMENT; |
| 4411 | |
| 4412 | thread_local Vector<RD::AttachmentFormat> attachments; |
| 4413 | attachments.clear(); |
| 4414 | |
| 4415 | // Color attachment. |
| 4416 | attachment.format = p_color_format; |
| 4417 | attachment.usage_flags = RenderSceneBuffersRD::get_color_usage_bits(false, multisampling, p_can_be_storage); |
| 4418 | attachments.push_back(attachment); |
| 4419 | |
| 4420 | if (p_specular) { |
| 4421 | attachment.format = RenderForwardClustered::RenderBufferDataForwardClustered::get_specular_format(); |
| 4422 | attachment.usage_flags = RenderForwardClustered::RenderBufferDataForwardClustered::get_specular_usage_bits(false, multisampling, p_can_be_storage); |
| 4423 | attachments.push_back(attachment); |
| 4424 | } else { |
| 4425 | attachments.push_back(unused_attachment); |
| 4426 | } |
| 4427 | |
| 4428 | if (p_velocity) { |
| 4429 | attachment.format = RenderSceneBuffersRD::get_velocity_format(); |
| 4430 | attachment.usage_flags = RenderSceneBuffersRD::get_velocity_usage_bits(false, multisampling, p_can_be_storage); |
| 4431 | attachments.push_back(attachment); |
| 4432 | } else { |
| 4433 | attachments.push_back(unused_attachment); |
| 4434 | } |
| 4435 | |
| 4436 | // Depth attachment. |
| 4437 | attachment.format = RenderSceneBuffersRD::get_depth_format(false, multisampling, p_can_be_storage); |
| 4438 | attachment.usage_flags = RenderSceneBuffersRD::get_depth_usage_bits(false, multisampling, p_can_be_storage); |
| 4439 | attachments.push_back(attachment); |
| 4440 | |
| 4441 | thread_local Vector<RD::FramebufferPass> passes; |
| 4442 | passes.resize(1); |
| 4443 | passes.ptrw()[0].color_attachments.resize(attachments.size() - 1); |
| 4444 | |
| 4445 | int *color_attachments = passes.ptrw()[0].color_attachments.ptrw(); |
| 4446 | for (int64_t i = 0; i < attachments.size() - 1; i++) { |
| 4447 | color_attachments[i] = (attachments[i].usage_flags == RD::AttachmentFormat::UNUSED_ATTACHMENT) ? RD::ATTACHMENT_UNUSED : i; |
| 4448 | } |
| 4449 | |
| 4450 | passes.ptrw()[0].depth_attachment = attachments.size() - 1; |
| 4451 | |
| 4452 | return RD::get_singleton()->framebuffer_format_create_multipass(attachments, passes, p_view_count); |
| 4453 | } |
| 4454 | |
| 4455 | static RD::FramebufferFormatID _get_reflection_probe_color_framebuffer_format_for_pipeline(bool p_storage) { |
| 4456 | RD::AttachmentFormat attachment; |
| 4457 | thread_local Vector<RD::AttachmentFormat> attachments; |
| 4458 | attachments.clear(); |
| 4459 | |
| 4460 | attachment.format = RendererRD::LightStorage::get_reflection_probe_color_format(); |
| 4461 | attachment.usage_flags = RendererRD::LightStorage::get_reflection_probe_color_usage_bits(p_storage); |
| 4462 | attachments.push_back(attachment); |
| 4463 | |
| 4464 | attachment.format = RendererRD::LightStorage::get_reflection_probe_depth_format(); |
| 4465 | attachment.usage_flags = RendererRD::LightStorage::get_reflection_probe_depth_usage_bits(); |
| 4466 | attachments.push_back(attachment); |
| 4467 | |
| 4468 | return RD::get_singleton()->framebuffer_format_create(attachments); |
| 4469 | } |
| 4470 | |
| 4471 | static RD::FramebufferFormatID _get_depth_framebuffer_format_for_pipeline(bool p_can_be_storage, RD::TextureSamples p_samples, bool p_normal_roughness, bool p_voxelgi) { |
| 4472 | const bool multisampling = p_samples > RD::TEXTURE_SAMPLES_1; |
| 4473 | RD::AttachmentFormat attachment; |
| 4474 | attachment.samples = p_samples; |
| 4475 | |
| 4476 | thread_local LocalVector<RD::AttachmentFormat> attachments; |
| 4477 | attachments.clear(); |
| 4478 | |
| 4479 | attachment.format = RenderSceneBuffersRD::get_depth_format(false, multisampling, p_can_be_storage); |
| 4480 | attachment.usage_flags = RenderSceneBuffersRD::get_depth_usage_bits(false, multisampling, p_can_be_storage); |
| 4481 | attachments.push_back(attachment); |
| 4482 | |
| 4483 | if (p_normal_roughness) { |
| 4484 | attachment.format = RenderForwardClustered::RenderBufferDataForwardClustered::get_normal_roughness_format(); |
| 4485 | attachment.usage_flags = RenderForwardClustered::RenderBufferDataForwardClustered::get_normal_roughness_usage_bits(false, multisampling, p_can_be_storage); |
| 4486 | attachments.push_back(attachment); |
| 4487 | } |
| 4488 | |
| 4489 | if (p_voxelgi) { |
| 4490 | attachment.format = RenderForwardClustered::RenderBufferDataForwardClustered::get_voxelgi_format(); |
| 4491 | attachment.usage_flags = RenderForwardClustered::RenderBufferDataForwardClustered::get_voxelgi_usage_bits(false, multisampling, p_can_be_storage); |
| 4492 | attachments.push_back(attachment); |
| 4493 | } |
| 4494 | |
| 4495 | thread_local Vector<RD::FramebufferPass> passes; |
| 4496 | passes.resize(1); |
| 4497 | passes.ptrw()[0].color_attachments.resize(attachments.size() - 1); |
| 4498 | |
| 4499 | int *color_attachments = passes.ptrw()[0].color_attachments.ptrw(); |
| 4500 | for (int64_t i = 1; i < attachments.size(); i++) { |
| 4501 | color_attachments[i - 1] = (attachments[i].usage_flags == RD::AttachmentFormat::UNUSED_ATTACHMENT) ? RD::ATTACHMENT_UNUSED : i; |
| 4502 | } |
| 4503 | |
| 4504 | passes.ptrw()[0].depth_attachment = 0; |
| 4505 | |
| 4506 | return RD::get_singleton()->framebuffer_format_create_multipass(Vector<RD::AttachmentFormat>(attachments), passes); |
| 4507 | } |
| 4508 | |
| 4509 | static RD::FramebufferFormatID _get_shadow_cubemap_framebuffer_format_for_pipeline() { |
| 4510 | thread_local LocalVector<RD::AttachmentFormat> attachments; |
| 4511 | attachments.clear(); |
| 4512 | |
| 4513 | RD::AttachmentFormat attachment; |
| 4514 | attachment.format = RendererRD::LightStorage::get_cubemap_depth_format(); |
| 4515 | attachment.usage_flags = RendererRD::LightStorage::get_cubemap_depth_usage_bits(); |
| 4516 | attachments.push_back(attachment); |
| 4517 | |
| 4518 | return RD::get_singleton()->framebuffer_format_create(Vector<RD::AttachmentFormat>(attachments)); |
| 4519 | } |
| 4520 | |
| 4521 | static RD::FramebufferFormatID _get_shadow_atlas_framebuffer_format_for_pipeline(bool p_use_16_bits) { |
| 4522 | thread_local LocalVector<RD::AttachmentFormat> attachments; |
| 4523 | attachments.clear(); |
| 4524 | |
| 4525 | RD::AttachmentFormat attachment; |
| 4526 | attachment.format = RendererRD::LightStorage::get_shadow_atlas_depth_format(p_use_16_bits); |
| 4527 | attachment.usage_flags = RendererRD::LightStorage::get_shadow_atlas_depth_usage_bits(); |
| 4528 | attachments.push_back(attachment); |
| 4529 | |
| 4530 | return RD::get_singleton()->framebuffer_format_create(Vector<RD::AttachmentFormat>(attachments)); |
| 4531 | } |
| 4532 | |
| 4533 | static RD::FramebufferFormatID _get_reflection_probe_depth_framebuffer_format_for_pipeline() { |
| 4534 | thread_local LocalVector<RD::AttachmentFormat> attachments; |
| 4535 | attachments.clear(); |
| 4536 | |
| 4537 | RD::AttachmentFormat attachment; |
| 4538 | attachment.format = RendererRD::LightStorage::get_reflection_probe_depth_format(); |
| 4539 | attachment.usage_flags = RendererRD::LightStorage::get_reflection_probe_depth_usage_bits(); |
| 4540 | attachments.push_back(attachment); |
| 4541 | |
| 4542 | return RD::get_singleton()->framebuffer_format_create(Vector<RD::AttachmentFormat>(attachments)); |
| 4543 | } |
| 4544 | |
| 4545 | void RenderForwardClustered::_mesh_compile_pipeline_for_surface(SceneShaderForwardClustered::ShaderData *p_shader, void *p_mesh_surface, bool p_ubershader, bool p_instanced_surface, RS::PipelineSource p_source, SceneShaderForwardClustered::ShaderData::PipelineKey &r_pipeline_key, Vector<ShaderPipelinePair> *r_pipeline_pairs) { |
| 4546 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 4547 | uint64_t input_mask = p_shader->get_vertex_input_mask(r_pipeline_key.version, r_pipeline_key.color_pass_flags, p_ubershader); |
| 4548 | bool pipeline_motion_vectors = r_pipeline_key.color_pass_flags & SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MOTION_VECTORS; |
| 4549 | bool emulate_point_size = p_shader->uses_point_size && scene_shader.emulate_point_size; |
| 4550 | r_pipeline_key.vertex_format_id = mesh_storage->mesh_surface_get_vertex_format(p_mesh_surface, input_mask, p_instanced_surface, pipeline_motion_vectors, emulate_point_size); |
| 4551 | r_pipeline_key.ubershader = p_ubershader; |
| 4552 | |
| 4553 | p_shader->pipeline_hash_map.compile_pipeline(r_pipeline_key, r_pipeline_key.hash(), p_source, p_ubershader); |
| 4554 | |
| 4555 | if (r_pipeline_pairs != nullptr) { |
| 4556 | r_pipeline_pairs->push_back({ p_shader, r_pipeline_key }); |
| 4557 | } |
| 4558 | } |
| 4559 | |
| 4560 | void RenderForwardClustered::_mesh_compile_pipelines_for_surface(const SurfacePipelineData &p_surface, const GlobalPipelineData &p_global, RS::PipelineSource p_source, Vector<ShaderPipelinePair> *r_pipeline_pairs) { |
| 4561 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 4562 | bool octmap_use_storage = !copy_effects->get_raster_effects().has_flag(RendererRD::CopyEffects::RASTER_EFFECT_OCTMAP); |
| 4563 | |
| 4564 | // Retrieve from the scene shader which groups are currently enabled. |
| 4565 | const bool multiview_enabled = p_global.use_multiview && scene_shader.is_multiview_shader_group_enabled(); |
| 4566 | const RD::DataFormat buffers_color_format = _render_buffers_get_preferred_color_format(); |
| 4567 | const bool buffers_can_be_storage = _render_buffers_can_be_storage(); |
| 4568 | |
| 4569 | // Set the attributes common to all pipelines. |
| 4570 | SceneShaderForwardClustered::ShaderData::PipelineKey pipeline_key; |
| 4571 | pipeline_key.cull_mode = RD::POLYGON_CULL_DISABLED; |
| 4572 | pipeline_key.primitive_type = mesh_storage->mesh_surface_get_primitive(p_surface.mesh_surface); |
| 4573 | pipeline_key.wireframe = false; |
| 4574 | |
| 4575 | // Grab the shader and surface used for most passes. |
| 4576 | const uint32_t multiview_iterations = multiview_enabled ? 2 : 1; |
| 4577 | const uint32_t lightmap_iterations = p_global.use_lightmaps && p_surface.can_use_lightmap ? 2 : 1; |
| 4578 | const uint32_t alpha_iterations = p_surface.uses_transparent ? 2 : 1; |
| 4579 | for (uint32_t multiview = 0; multiview < multiview_iterations; multiview++) { |
| 4580 | for (uint32_t lightmap = 0; lightmap < lightmap_iterations; lightmap++) { |
| 4581 | for (uint32_t alpha = p_surface.uses_opaque ? 0 : 1; alpha < alpha_iterations; alpha++) { |
| 4582 | // Generate all the possible variants used during the color pass. |
| 4583 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_COLOR_PASS; |
| 4584 | pipeline_key.color_pass_flags = 0; |
| 4585 | |
| 4586 | if (lightmap) { |
| 4587 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_LIGHTMAP; |
| 4588 | } |
| 4589 | |
| 4590 | if (alpha) { |
| 4591 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_TRANSPARENT; |
| 4592 | } |
| 4593 | |
| 4594 | if (multiview) { |
| 4595 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MULTIVIEW; |
| 4596 | } else if (p_global.use_reflection_probes) { |
| 4597 | // Reflection probe can't be rendered in multiview. |
| 4598 | pipeline_key.framebuffer_format_id = _get_reflection_probe_color_framebuffer_format_for_pipeline(octmap_use_storage); |
| 4599 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4600 | } |
| 4601 | |
| 4602 | // View count is assumed to be 2 as the configuration is dependent on the viewport. It's likely a safe assumption for stereo rendering. |
| 4603 | uint32_t view_count = multiview ? 2 : 1; |
| 4604 | pipeline_key.framebuffer_format_id = _get_color_framebuffer_format_for_pipeline(buffers_color_format, buffers_can_be_storage, RD::TextureSamples(p_global.texture_samples), false, false, view_count); |
| 4605 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4606 | |
| 4607 | // Generate all the possible variants used during the advanced color passes. |
| 4608 | const uint32_t separate_specular_iterations = p_global.use_separate_specular ? 2 : 1; |
| 4609 | const uint32_t motion_vectors_iterations = p_global.use_motion_vectors ? 2 : 1; |
| 4610 | uint32_t base_color_pass_flags = pipeline_key.color_pass_flags; |
| 4611 | for (uint32_t separate_specular = 0; separate_specular < separate_specular_iterations; separate_specular++) { |
| 4612 | for (uint32_t motion_vectors = 0; motion_vectors < motion_vectors_iterations; motion_vectors++) { |
| 4613 | if (!separate_specular && !motion_vectors) { |
| 4614 | // This case was already generated. |
| 4615 | continue; |
| 4616 | } |
| 4617 | |
| 4618 | pipeline_key.color_pass_flags = base_color_pass_flags; |
| 4619 | |
| 4620 | if (separate_specular) { |
| 4621 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_SEPARATE_SPECULAR; |
| 4622 | } |
| 4623 | |
| 4624 | if (motion_vectors) { |
| 4625 | pipeline_key.color_pass_flags |= SceneShaderForwardClustered::PIPELINE_COLOR_PASS_FLAG_MOTION_VECTORS; |
| 4626 | } |
| 4627 | |
| 4628 | pipeline_key.framebuffer_format_id = _get_color_framebuffer_format_for_pipeline(buffers_color_format, buffers_can_be_storage, RD::TextureSamples(p_global.texture_samples), separate_specular, motion_vectors, view_count); |
| 4629 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4630 | } |
| 4631 | } |
| 4632 | } |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | if (!p_surface.uses_depth) { |
| 4637 | return; |
| 4638 | } |
| 4639 | |
| 4640 | // Generate the depth pipelines if the material supports depth or it must be part of the shadow pass. |
| 4641 | pipeline_key.color_pass_flags = 0; |
| 4642 | |
| 4643 | if (p_global.use_normal_and_roughness) { |
| 4644 | // A lot of different effects rely on normal and roughness being written to during the depth pass. |
| 4645 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS; |
| 4646 | pipeline_key.framebuffer_format_id = _get_depth_framebuffer_format_for_pipeline(buffers_can_be_storage, RD::TextureSamples(p_global.texture_samples), true, false); |
| 4647 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4648 | } |
| 4649 | |
| 4650 | if (p_global.use_voxelgi) { |
| 4651 | // Depth pass with VoxelGI support. |
| 4652 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI; |
| 4653 | pipeline_key.framebuffer_format_id = _get_depth_framebuffer_format_for_pipeline(buffers_can_be_storage, RD::TextureSamples(p_global.texture_samples), true, true); |
| 4654 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4655 | } |
| 4656 | |
| 4657 | if (p_global.use_sdfgi) { |
| 4658 | // Depth pass with SDFGI support. |
| 4659 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_SDF; |
| 4660 | pipeline_key.framebuffer_format_id = _get_depth_framebuffer_format_for_pipeline(buffers_can_be_storage, RD::TextureSamples(p_global.texture_samples), false, false); |
| 4661 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4662 | |
| 4663 | // Depth pass with SDFGI support for an empty framebuffer. |
| 4664 | pipeline_key.framebuffer_format_id = RD::get_singleton()->framebuffer_format_create_empty(); |
| 4665 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4666 | } |
| 4667 | |
| 4668 | // The dedicated depth passes use a different version of the surface and the shader. |
| 4669 | pipeline_key.primitive_type = mesh_storage->mesh_surface_get_primitive(p_surface.mesh_surface_shadow); |
| 4670 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS; |
| 4671 | pipeline_key.framebuffer_format_id = _get_depth_framebuffer_format_for_pipeline(buffers_can_be_storage, RD::TextureSamples(p_global.texture_samples), false, false); |
| 4672 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4673 | |
| 4674 | if (p_global.use_shadow_dual_paraboloid) { |
| 4675 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_DP; |
| 4676 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4677 | } |
| 4678 | |
| 4679 | if (p_global.use_shadow_cubemaps) { |
| 4680 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS; |
| 4681 | pipeline_key.framebuffer_format_id = _get_shadow_cubemap_framebuffer_format_for_pipeline(); |
| 4682 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4683 | } |
| 4684 | |
| 4685 | // Atlas shadowmaps (omni lights) can be in both 16-bit and 32-bit versions. |
| 4686 | const uint32_t use_16_bits_start = p_global.use_32_bit_shadows ? 0 : 1; |
| 4687 | const uint32_t use_16_bits_iterations = p_global.use_16_bit_shadows ? 2 : 1; |
| 4688 | for (uint32_t use_16_bits = use_16_bits_start; use_16_bits < use_16_bits_iterations; use_16_bits++) { |
| 4689 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS; |
| 4690 | pipeline_key.framebuffer_format_id = _get_shadow_atlas_framebuffer_format_for_pipeline(use_16_bits); |
| 4691 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4692 | |
| 4693 | if (p_global.use_shadow_dual_paraboloid) { |
| 4694 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_DP; |
| 4695 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | if (p_global.use_reflection_probes) { |
| 4700 | // Depth pass for reflection probes. Normally this will be redundant as the format is the exact same as the shadow cubemap. |
| 4701 | pipeline_key.version = SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS; |
| 4702 | pipeline_key.framebuffer_format_id = _get_reflection_probe_depth_framebuffer_format_for_pipeline(); |
| 4703 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, true, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 4704 | } |
| 4705 | } |
| 4706 | |
| 4707 | void RenderForwardClustered::_mesh_generate_all_pipelines_for_surface_cache(GeometryInstanceSurfaceDataCache *p_surface_cache, const GlobalPipelineData &p_global) { |
| 4708 | bool uses_alpha_pass = (p_surface_cache->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA) != 0; |
| 4709 | float multiplied_fade_alpha = p_surface_cache->owner->force_alpha * p_surface_cache->owner->parent_fade_alpha; |
| 4710 | bool uses_fade = (multiplied_fade_alpha < FADE_ALPHA_PASS_THRESHOLD) || p_surface_cache->owner->fade_near || p_surface_cache->owner->fade_far; |
| 4711 | SurfacePipelineData surface; |
| 4712 | surface.mesh_surface = p_surface_cache->surface; |
| 4713 | surface.mesh_surface_shadow = p_surface_cache->surface_shadow; |
| 4714 | surface.shader = p_surface_cache->shader; |
| 4715 | surface.shader_shadow = p_surface_cache->shader_shadow; |
| 4716 | surface.instanced = p_surface_cache->owner->mesh_instance.is_valid(); |
| 4717 | surface.uses_opaque = !uses_alpha_pass; |
| 4718 | surface.uses_transparent = uses_alpha_pass || uses_fade; |
| 4719 | surface.uses_depth = (p_surface_cache->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE | GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW)) != 0; |
| 4720 | surface.can_use_lightmap = p_surface_cache->owner->lightmap_instance.is_valid() || p_surface_cache->owner->lightmap_sh; |
| 4721 | _mesh_compile_pipelines_for_surface(surface, p_global, RS::PIPELINE_SOURCE_SURFACE); |
| 4722 | } |
| 4723 | |
| 4724 | void RenderForwardClustered::_update_dirty_geometry_instances() { |
| 4725 | while (geometry_instance_dirty_list.first()) { |
| 4726 | _geometry_instance_update(geometry_instance_dirty_list.first()->self()); |
| 4727 | } |
| 4728 | |
| 4729 | _update_dirty_geometry_pipelines(); |
| 4730 | } |
| 4731 | |
| 4732 | void RenderForwardClustered::_update_dirty_geometry_pipelines() { |
| 4733 | if (global_pipeline_data_required.key != global_pipeline_data_compiled.key) { |
| 4734 | // Go through the entire list of surfaces and compile pipelines for everything again. |
| 4735 | SelfList<GeometryInstanceSurfaceDataCache> *list = geometry_surface_compilation_all_list.first(); |
| 4736 | while (list != nullptr) { |
| 4737 | GeometryInstanceSurfaceDataCache *surface_cache = list->self(); |
| 4738 | _mesh_generate_all_pipelines_for_surface_cache(surface_cache, global_pipeline_data_required); |
| 4739 | |
| 4740 | if (surface_cache->compilation_dirty_element.in_list()) { |
| 4741 | // Remove any elements from the dirty list as they don't need to be processed again. |
| 4742 | geometry_surface_compilation_dirty_list.remove(&surface_cache->compilation_dirty_element); |
| 4743 | } |
| 4744 | |
| 4745 | list = list->next(); |
| 4746 | } |
| 4747 | |
| 4748 | global_pipeline_data_compiled.key = global_pipeline_data_required.key; |
| 4749 | } else { |
| 4750 | // Compile pipelines only for the dirty list. |
| 4751 | if (!geometry_surface_compilation_dirty_list.first()) { |
| 4752 | return; |
| 4753 | } |
| 4754 | |
| 4755 | while (geometry_surface_compilation_dirty_list.first() != nullptr) { |
| 4756 | GeometryInstanceSurfaceDataCache *surface_cache = geometry_surface_compilation_dirty_list.first()->self(); |
| 4757 | _mesh_generate_all_pipelines_for_surface_cache(surface_cache, global_pipeline_data_compiled); |
| 4758 | surface_cache->compilation_dirty_element.remove_from_list(); |
| 4759 | } |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | void RenderForwardClustered::_geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker) { |
| 4764 | switch (p_notification) { |
| 4765 | case Dependency::DEPENDENCY_CHANGED_MATERIAL: |
| 4766 | case Dependency::DEPENDENCY_CHANGED_MESH: |
| 4767 | case Dependency::DEPENDENCY_CHANGED_PARTICLES: |
| 4768 | case Dependency::DEPENDENCY_CHANGED_PARTICLES_INSTANCES: |
| 4769 | case Dependency::DEPENDENCY_CHANGED_MULTIMESH: |
| 4770 | case Dependency::DEPENDENCY_CHANGED_SKELETON_DATA: { |
| 4771 | static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty(); |
| 4772 | static_cast<GeometryInstanceForwardClustered *>(p_tracker->userdata)->data->dirty_dependencies = true; |
| 4773 | } break; |
| 4774 | case Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES: { |
| 4775 | GeometryInstanceForwardClustered *ginstance = static_cast<GeometryInstanceForwardClustered *>(p_tracker->userdata); |
| 4776 | if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) { |
| 4777 | ginstance->instance_count = RendererRD::MeshStorage::get_singleton()->multimesh_get_instances_to_draw(ginstance->data->base); |
| 4778 | } |
| 4779 | } break; |
| 4780 | default: { |
| 4781 | //rest of notifications of no interest |
| 4782 | } break; |
| 4783 | } |
| 4784 | } |
| 4785 | void RenderForwardClustered::_geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker) { |
| 4786 | static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty(); |
| 4787 | static_cast<GeometryInstanceForwardClustered *>(p_tracker->userdata)->data->dirty_dependencies = true; |
| 4788 | } |
| 4789 | |
| 4790 | RenderGeometryInstance *RenderForwardClustered::geometry_instance_create(RID p_base) { |
| 4791 | RS::InstanceType type = RSG::utilities->get_base_type(p_base); |
| 4792 | ERR_FAIL_COND_V(!((1 << type) & RS::INSTANCE_GEOMETRY_MASK), nullptr); |
| 4793 | |
| 4794 | GeometryInstanceForwardClustered *ginstance = geometry_instance_alloc.alloc(); |
| 4795 | ginstance->data = memnew(GeometryInstanceForwardClustered::Data); |
| 4796 | |
| 4797 | ginstance->data->base = p_base; |
| 4798 | ginstance->data->base_type = type; |
| 4799 | ginstance->data->dependency_tracker.userdata = ginstance; |
| 4800 | ginstance->data->dependency_tracker.changed_callback = _geometry_instance_dependency_changed; |
| 4801 | ginstance->data->dependency_tracker.deleted_callback = _geometry_instance_dependency_deleted; |
| 4802 | |
| 4803 | ginstance->_mark_dirty(); |
| 4804 | |
| 4805 | return ginstance; |
| 4806 | } |
| 4807 | |
| 4808 | void RenderForwardClustered::GeometryInstanceForwardClustered::set_transform(const Transform3D &p_transform, const AABB &p_aabb, const AABB &p_transformed_aabb) { |
| 4809 | uint64_t frame = RSG::rasterizer->get_frame_number(); |
| 4810 | if (frame != prev_transform_change_frame) { |
| 4811 | prev_transform = transform; |
| 4812 | prev_transform_change_frame = frame; |
| 4813 | transform_status = TransformStatus::MOVED; |
| 4814 | } else if (unlikely(transform_status == TransformStatus::TELEPORTED)) { |
| 4815 | prev_transform = transform; |
| 4816 | } |
| 4817 | |
| 4818 | RenderGeometryInstanceBase::set_transform(p_transform, p_aabb, p_transformed_aabb); |
| 4819 | } |
| 4820 | |
| 4821 | void RenderForwardClustered::GeometryInstanceForwardClustered::reset_motion_vectors() { |
| 4822 | prev_transform = transform; |
| 4823 | transform_status = TransformStatus::TELEPORTED; |
| 4824 | } |
| 4825 | |
| 4826 | void RenderForwardClustered::GeometryInstanceForwardClustered::set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) { |
| 4827 | lightmap_instance = p_lightmap_instance; |
| 4828 | lightmap_uv_scale = p_lightmap_uv_scale; |
| 4829 | lightmap_slice_index = p_lightmap_slice_index; |
| 4830 | |
| 4831 | _mark_dirty(); |
| 4832 | } |
| 4833 | |
| 4834 | void RenderForwardClustered::GeometryInstanceForwardClustered::set_lightmap_capture(const Color *p_sh9) { |
| 4835 | if (p_sh9) { |
| 4836 | if (lightmap_sh == nullptr) { |
| 4837 | lightmap_sh = RenderForwardClustered::get_singleton()->geometry_instance_lightmap_sh.alloc(); |
| 4838 | } |
| 4839 | |
| 4840 | memcpy(lightmap_sh->sh, p_sh9, sizeof(Color) * 9); |
| 4841 | } else { |
| 4842 | if (lightmap_sh != nullptr) { |
| 4843 | RenderForwardClustered::get_singleton()->geometry_instance_lightmap_sh.free(lightmap_sh); |
| 4844 | lightmap_sh = nullptr; |
| 4845 | } |
| 4846 | } |
| 4847 | _mark_dirty(); |
| 4848 | } |
| 4849 | |
| 4850 | void RenderForwardClustered::geometry_instance_free(RenderGeometryInstance *p_geometry_instance) { |
| 4851 | GeometryInstanceForwardClustered *ginstance = static_cast<GeometryInstanceForwardClustered *>(p_geometry_instance); |
| 4852 | ERR_FAIL_NULL(ginstance); |
| 4853 | if (ginstance->lightmap_sh != nullptr) { |
| 4854 | geometry_instance_lightmap_sh.free(ginstance->lightmap_sh); |
| 4855 | } |
| 4856 | GeometryInstanceSurfaceDataCache *surf = ginstance->surface_caches; |
| 4857 | while (surf) { |
| 4858 | GeometryInstanceSurfaceDataCache *next = surf->next; |
| 4859 | geometry_instance_surface_alloc.free(surf); |
| 4860 | surf = next; |
| 4861 | } |
| 4862 | memdelete(ginstance->data); |
| 4863 | geometry_instance_alloc.free(ginstance); |
| 4864 | } |
| 4865 | |
| 4866 | uint32_t RenderForwardClustered::geometry_instance_get_pair_mask() { |
| 4867 | return (1 << RS::INSTANCE_VOXEL_GI); |
| 4868 | } |
| 4869 | |
| 4870 | void RenderForwardClustered::mesh_generate_pipelines(RID p_mesh, bool p_background_compilation) { |
| 4871 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 4872 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 4873 | RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh); |
| 4874 | uint32_t surface_count = 0; |
| 4875 | const RID *materials = mesh_storage->mesh_get_surface_count_and_materials(p_mesh, surface_count); |
| 4876 | Vector<ShaderPipelinePair> pipeline_pairs; |
| 4877 | for (uint32_t i = 0; i < surface_count; i++) { |
| 4878 | if (materials[i].is_null()) { |
| 4879 | continue; |
| 4880 | } |
| 4881 | |
| 4882 | void *mesh_surface = mesh_storage->mesh_get_surface(p_mesh, i); |
| 4883 | void *mesh_surface_shadow = mesh_surface; |
| 4884 | SceneShaderForwardClustered::MaterialData *material = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(materials[i], RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 4885 | if (material == nullptr || !material->shader_data->is_valid()) { |
| 4886 | continue; |
| 4887 | } |
| 4888 | |
| 4889 | SceneShaderForwardClustered::ShaderData *shader = material->shader_data; |
| 4890 | SceneShaderForwardClustered::ShaderData *shader_shadow = shader; |
| 4891 | if (material->shader_data->uses_shared_shadow_material()) { |
| 4892 | SceneShaderForwardClustered::MaterialData *material_shadow = static_cast<SceneShaderForwardClustered::MaterialData *>(material_storage->material_get_data(scene_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 4893 | if (material_shadow != nullptr) { |
| 4894 | shader_shadow = material_shadow->shader_data; |
| 4895 | if (shadow_mesh.is_valid()) { |
| 4896 | mesh_surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, i); |
| 4897 | } |
| 4898 | } |
| 4899 | } |
| 4900 | |
| 4901 | if (!shader->is_valid()) { |
| 4902 | continue; |
| 4903 | } |
| 4904 | |
| 4905 | SurfacePipelineData surface; |
| 4906 | surface.mesh_surface = mesh_surface; |
| 4907 | surface.mesh_surface_shadow = mesh_surface_shadow; |
| 4908 | surface.shader = shader; |
| 4909 | surface.shader_shadow = shader_shadow; |
| 4910 | surface.instanced = mesh_storage->mesh_needs_instance(p_mesh, true); |
| 4911 | surface.uses_opaque = !material->shader_data->uses_alpha_pass(); |
| 4912 | surface.uses_transparent = material->shader_data->uses_alpha_pass(); |
| 4913 | surface.uses_depth = surface.uses_opaque || (surface.uses_transparent && material->shader_data->uses_depth_in_alpha_pass()); |
| 4914 | surface.can_use_lightmap = mesh_storage->mesh_surface_get_format(mesh_surface) & RS::ARRAY_FORMAT_TEX_UV2; |
| 4915 | _mesh_compile_pipelines_for_surface(surface, global_pipeline_data_required, RS::PIPELINE_SOURCE_MESH, &pipeline_pairs); |
| 4916 | } |
| 4917 | |
| 4918 | // Wait for all the pipelines that were compiled. This will force the loader to wait on all ubershader pipelines to be ready. |
| 4919 | if (!p_background_compilation && !pipeline_pairs.is_empty()) { |
| 4920 | for (ShaderPipelinePair pair : pipeline_pairs) { |
| 4921 | pair.first->pipeline_hash_map.wait_for_pipeline(pair.second.hash()); |
| 4922 | } |
| 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | uint32_t RenderForwardClustered::get_pipeline_compilations(RS::PipelineSource p_source) { |
| 4927 | return scene_shader.get_pipeline_compilations(p_source); |
| 4928 | } |
| 4929 | |
| 4930 | void RenderForwardClustered::enable_features(BitField<FeatureBits> p_feature_bits) { |
| 4931 | if (p_feature_bits.has_flag(FEATURE_MULTIVIEW_BIT)) { |
| 4932 | scene_shader.enable_multiview_shader_group(); |
| 4933 | } |
| 4934 | |
| 4935 | if (p_feature_bits.has_flag(FEATURE_ADVANCED_BIT)) { |
| 4936 | scene_shader.enable_advanced_shader_group(p_feature_bits.has_flag(FEATURE_MULTIVIEW_BIT)); |
| 4937 | } |
| 4938 | |
| 4939 | if (p_feature_bits.has_flag(FEATURE_VRS_BIT)) { |
| 4940 | gi.enable_vrs_shader_group(); |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | String RenderForwardClustered::get_name() const { |
| 4945 | return "forward_clustered"; |
| 4946 | } |
| 4947 | |
| 4948 | void RenderForwardClustered::GeometryInstanceForwardClustered::pair_voxel_gi_instances(const RID *p_voxel_gi_instances, uint32_t p_voxel_gi_instance_count) { |
| 4949 | if (p_voxel_gi_instance_count > 0) { |
| 4950 | voxel_gi_instances[0] = p_voxel_gi_instances[0]; |
| 4951 | } else { |
| 4952 | voxel_gi_instances[0] = RID(); |
| 4953 | } |
| 4954 | |
| 4955 | if (p_voxel_gi_instance_count > 1) { |
| 4956 | voxel_gi_instances[1] = p_voxel_gi_instances[1]; |
| 4957 | } else { |
| 4958 | voxel_gi_instances[1] = RID(); |
| 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | void RenderForwardClustered::GeometryInstanceForwardClustered::set_softshadow_projector_pairing(bool p_softshadow, bool p_projector) { |
| 4963 | using_projectors = p_projector; |
| 4964 | using_softshadows = p_softshadow; |
| 4965 | _mark_dirty(); |
| 4966 | } |
| 4967 | |
| 4968 | void RenderForwardClustered::_update_shader_quality_settings() { |
| 4969 | SceneShaderForwardClustered::ShaderSpecialization specialization = {}; |
| 4970 | specialization.decal_use_mipmaps = decals_get_filter() == RS::DECAL_FILTER_NEAREST_MIPMAPS || |
| 4971 | decals_get_filter() == RS::DECAL_FILTER_LINEAR_MIPMAPS || |
| 4972 | decals_get_filter() == RS::DECAL_FILTER_NEAREST_MIPMAPS_ANISOTROPIC || |
| 4973 | decals_get_filter() == RS::DECAL_FILTER_LINEAR_MIPMAPS_ANISOTROPIC; |
| 4974 | ; |
| 4975 | specialization.projector_use_mipmaps = light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS || |
| 4976 | light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS || |
| 4977 | light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS_ANISOTROPIC || |
| 4978 | light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS_ANISOTROPIC; |
| 4979 | |
| 4980 | specialization.soft_shadow_samples = soft_shadow_samples_get(); |
| 4981 | specialization.penumbra_shadow_samples = penumbra_shadow_samples_get(); |
| 4982 | specialization.directional_soft_shadow_samples = directional_soft_shadow_samples_get(); |
| 4983 | specialization.directional_penumbra_shadow_samples = directional_penumbra_shadow_samples_get(); |
| 4984 | specialization.use_lightmap_bicubic_filter = lightmap_filter_bicubic_get(); |
| 4985 | specialization.fog_use_legacy_blending = fog_use_legacy_blending_get(); |
| 4986 | scene_shader.set_default_specialization(specialization); |
| 4987 | |
| 4988 | base_uniforms_changed(); //also need this |
| 4989 | } |
| 4990 | |
| 4991 | RenderForwardClustered::RenderForwardClustered() { |
| 4992 | singleton = this; |
| 4993 | |
| 4994 | /* SCENE SHADER */ |
| 4995 | |
| 4996 | { |
| 4997 | String defines; |
| 4998 | defines += "\n#define MAX_ROUGHNESS_LOD " + itos(get_roughness_layers() - 1) + ".0\n"; |
| 4999 | if (is_using_radiance_octmap_array()) { |
| 5000 | defines += "\n#define USE_RADIANCE_OCTMAP_ARRAY \n"; |
| 5001 | } |
| 5002 | defines += "\n#define SDFGI_OCT_SIZE " + itos(gi.sdfgi_get_lightprobe_octahedron_size()) + "\n"; |
| 5003 | defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(MAX_DIRECTIONAL_LIGHTS) + "\n"; |
| 5004 | |
| 5005 | bool force_vertex_shading = GLOBAL_GET("rendering/shading/overrides/force_vertex_shading"); |
| 5006 | if (force_vertex_shading) { |
| 5007 | defines += "\n#define USE_VERTEX_LIGHTING\n"; |
| 5008 | } |
| 5009 | |
| 5010 | bool specular_occlusion = GLOBAL_GET("rendering/reflections/specular_occlusion/enabled"); |
| 5011 | if (!specular_occlusion) { |
| 5012 | defines += "\n#define SPECULAR_OCCLUSION_DISABLED\n"; |
| 5013 | } |
| 5014 | |
| 5015 | { |
| 5016 | //lightmaps |
| 5017 | scene_state.max_lightmaps = MAX_LIGHTMAPS; |
| 5018 | defines += "\n#define MAX_LIGHTMAP_TEXTURES " + itos(scene_state.max_lightmaps) + "\n"; |
| 5019 | defines += "\n#define MAX_LIGHTMAPS " + itos(scene_state.max_lightmaps) + "\n"; |
| 5020 | |
| 5021 | scene_state.lightmap_buffer = RD::get_singleton()->storage_buffer_create(sizeof(LightmapData) * scene_state.max_lightmaps); |
| 5022 | } |
| 5023 | { |
| 5024 | //captures |
| 5025 | scene_state.max_lightmap_captures = 2048; |
| 5026 | scene_state.lightmap_captures = memnew_arr(LightmapCaptureData, scene_state.max_lightmap_captures); |
| 5027 | scene_state.lightmap_capture_buffer = RD::get_singleton()->storage_buffer_create(sizeof(LightmapCaptureData) * scene_state.max_lightmap_captures); |
| 5028 | } |
| 5029 | { |
| 5030 | defines += "\n#define MATERIAL_UNIFORM_SET " + itos(MATERIAL_UNIFORM_SET) + "\n"; |
| 5031 | } |
| 5032 | #ifdef REAL_T_IS_DOUBLE |
| 5033 | { |
| 5034 | defines += "\n#define USE_DOUBLE_PRECISION \n"; |
| 5035 | } |
| 5036 | #endif |
| 5037 | |
| 5038 | scene_shader.init(defines); |
| 5039 | } |
| 5040 | |
| 5041 | /* shadow sampler */ |
| 5042 | { |
| 5043 | RD::SamplerState sampler; |
| 5044 | sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST; |
| 5045 | sampler.min_filter = RD::SAMPLER_FILTER_NEAREST; |
| 5046 | sampler.enable_compare = true; |
| 5047 | sampler.compare_op = RD::COMPARE_OP_GREATER; |
| 5048 | shadow_sampler = RD::get_singleton()->sampler_create(sampler); |
| 5049 | } |
| 5050 | |
| 5051 | { |
| 5052 | Vector<String> modes; |
| 5053 | modes.push_back("\n"); |
| 5054 | best_fit_normal.shader.initialize(modes); |
| 5055 | best_fit_normal.shader_version = best_fit_normal.shader.version_create(); |
| 5056 | best_fit_normal.pipeline = RD::get_singleton()->compute_pipeline_create(best_fit_normal.shader.version_get_shader(best_fit_normal.shader_version, 0)); |
| 5057 | |
| 5058 | RD::TextureFormat tformat; |
| 5059 | tformat.format = RD::DATA_FORMAT_R8_UNORM; |
| 5060 | tformat.width = 1024; |
| 5061 | tformat.height = 1024; |
| 5062 | tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT; |
| 5063 | tformat.texture_type = RD::TEXTURE_TYPE_2D; |
| 5064 | best_fit_normal.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView()); |
| 5065 | |
| 5066 | RID shader = best_fit_normal.shader.version_get_shader(best_fit_normal.shader_version, 0); |
| 5067 | ERR_FAIL_COND(shader.is_null()); |
| 5068 | |
| 5069 | Vector<RD::Uniform> uniforms; |
| 5070 | |
| 5071 | { |
| 5072 | RD::Uniform u; |
| 5073 | u.binding = 0; |
| 5074 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
| 5075 | u.append_id(best_fit_normal.texture); |
| 5076 | uniforms.push_back(u); |
| 5077 | } |
| 5078 | RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader, 0); |
| 5079 | |
| 5080 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 5081 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, best_fit_normal.pipeline); |
| 5082 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0); |
| 5083 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, tformat.width, tformat.height, 1); |
| 5084 | RD::get_singleton()->compute_list_end(); |
| 5085 | } |
| 5086 | |
| 5087 | /* DFG LUT */ |
| 5088 | { |
| 5089 | Vector<String> modes; |
| 5090 | modes.push_back("\n"); |
| 5091 | dfg_lut.shader.initialize(modes); |
| 5092 | dfg_lut.shader_version = dfg_lut.shader.version_create(); |
| 5093 | dfg_lut.pipeline = RD::get_singleton()->compute_pipeline_create(dfg_lut.shader.version_get_shader(dfg_lut.shader_version, 0)); |
| 5094 | |
| 5095 | RD::TextureFormat tformat; |
| 5096 | tformat.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT; |
| 5097 | tformat.width = 128; |
| 5098 | tformat.height = 128; |
| 5099 | tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT; |
| 5100 | tformat.texture_type = RD::TEXTURE_TYPE_2D; |
| 5101 | dfg_lut.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView()); |
| 5102 | |
| 5103 | RID shader = dfg_lut.shader.version_get_shader(dfg_lut.shader_version, 0); |
| 5104 | ERR_FAIL_COND(shader.is_null()); |
| 5105 | |
| 5106 | Vector<RD::Uniform> uniforms; |
| 5107 | |
| 5108 | { |
| 5109 | RD::Uniform u; |
| 5110 | u.binding = 0; |
| 5111 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
| 5112 | u.append_id(dfg_lut.texture); |
| 5113 | uniforms.push_back(u); |
| 5114 | } |
| 5115 | RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader, 0); |
| 5116 | |
| 5117 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 5118 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, dfg_lut.pipeline); |
| 5119 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0); |
| 5120 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, tformat.width, tformat.height, 1); |
| 5121 | RD::get_singleton()->compute_list_end(); |
| 5122 | } |
| 5123 | |
| 5124 | _update_shader_quality_settings(); |
| 5125 | _update_global_pipeline_data_requirements_from_project(); |
| 5126 | |
| 5127 | taa = memnew(RendererRD::TAA); |
| 5128 | fsr2_effect = memnew(RendererRD::FSR2Effect); |
| 5129 | ss_effects = memnew(RendererRD::SSEffects); |
| 5130 | #ifdef METAL_MFXTEMPORAL_ENABLED |
| 5131 | motion_vectors_store = memnew(RendererRD::MotionVectorsStore); |
| 5132 | mfx_temporal_effect = memnew(RendererRD::MFXTemporalEffect); |
| 5133 | #endif |
| 5134 | } |
| 5135 | |
| 5136 | RenderForwardClustered::~RenderForwardClustered() { |
| 5137 | if (ss_effects != nullptr) { |
| 5138 | memdelete(ss_effects); |
| 5139 | ss_effects = nullptr; |
| 5140 | } |
| 5141 | |
| 5142 | if (taa != nullptr) { |
| 5143 | memdelete(taa); |
| 5144 | taa = nullptr; |
| 5145 | } |
| 5146 | |
| 5147 | if (fsr2_effect) { |
| 5148 | memdelete(fsr2_effect); |
| 5149 | fsr2_effect = nullptr; |
| 5150 | } |
| 5151 | |
| 5152 | #ifdef METAL_MFXTEMPORAL_ENABLED |
| 5153 | if (mfx_temporal_effect) { |
| 5154 | memdelete(mfx_temporal_effect); |
| 5155 | mfx_temporal_effect = nullptr; |
| 5156 | } |
| 5157 | |
| 5158 | if (motion_vectors_store) { |
| 5159 | memdelete(motion_vectors_store); |
| 5160 | motion_vectors_store = nullptr; |
| 5161 | } |
| 5162 | #endif |
| 5163 | |
| 5164 | RD::get_singleton()->free_rid(shadow_sampler); |
| 5165 | RSG::light_storage->directional_shadow_atlas_set_size(0); |
| 5166 | |
| 5167 | RD::get_singleton()->free_rid(best_fit_normal.pipeline); |
| 5168 | RD::get_singleton()->free_rid(best_fit_normal.texture); |
| 5169 | best_fit_normal.shader.version_free(best_fit_normal.shader_version); |
| 5170 | |
| 5171 | RD::get_singleton()->free_rid(dfg_lut.pipeline); |
| 5172 | RD::get_singleton()->free_rid(dfg_lut.texture); |
| 5173 | dfg_lut.shader.version_free(dfg_lut.shader_version); |
| 5174 | |
| 5175 | { |
| 5176 | for (const RID &rid : scene_state.uniform_buffers) { |
| 5177 | RD::get_singleton()->free_rid(rid); |
| 5178 | } |
| 5179 | for (const RID &rid : scene_state.implementation_uniform_buffers) { |
| 5180 | RD::get_singleton()->free_rid(rid); |
| 5181 | } |
| 5182 | RD::get_singleton()->free_rid(scene_state.lightmap_buffer); |
| 5183 | RD::get_singleton()->free_rid(scene_state.lightmap_capture_buffer); |
| 5184 | for (uint32_t i = 0; i < RENDER_LIST_MAX; i++) { |
| 5185 | scene_state.instance_buffer[i].uninit(); |
| 5186 | } |
| 5187 | memdelete_arr(scene_state.lightmap_captures); |
| 5188 | } |
| 5189 | |
| 5190 | while (sdfgi_framebuffer_size_cache.begin()) { |
| 5191 | RD::get_singleton()->free_rid(sdfgi_framebuffer_size_cache.begin()->value); |
| 5192 | sdfgi_framebuffer_size_cache.remove(sdfgi_framebuffer_size_cache.begin()); |
| 5193 | } |
| 5194 | } |