| 1 | /**************************************************************************/ |
| 2 | /* render_forward_mobile.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_mobile.h" |
| 32 | #include "core/config/project_settings.h" |
| 33 | #include "servers/rendering/renderer_rd/framebuffer_cache_rd.h" |
| 34 | #include "servers/rendering/renderer_rd/storage_rd/light_storage.h" |
| 35 | #include "servers/rendering/renderer_rd/storage_rd/mesh_storage.h" |
| 36 | #include "servers/rendering/renderer_rd/storage_rd/particles_storage.h" |
| 37 | #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h" |
| 38 | #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h" |
| 39 | #include "servers/rendering/rendering_device.h" |
| 40 | #include "servers/rendering/rendering_server_default.h" |
| 41 | |
| 42 | #define PRELOAD_PIPELINES_ON_SURFACE_CACHE_CONSTRUCTION 1 |
| 43 | |
| 44 | using namespace RendererSceneRenderImplementation; |
| 45 | |
| 46 | RendererRD::ForwardID RenderForwardMobile::ForwardIDStorageMobile::allocate_forward_id(RendererRD::ForwardIDType p_type) { |
| 47 | int32_t index = -1; |
| 48 | for (uint32_t i = 0; i < forward_id_allocators[p_type].allocations.size(); i++) { |
| 49 | if (forward_id_allocators[p_type].allocations[i] == false) { |
| 50 | index = i; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (index == -1) { |
| 56 | index = forward_id_allocators[p_type].allocations.size(); |
| 57 | forward_id_allocators[p_type].allocations.push_back(true); |
| 58 | forward_id_allocators[p_type].map.push_back(0xFF); |
| 59 | forward_id_allocators[p_type].last_pass.push_back(0); |
| 60 | } else { |
| 61 | forward_id_allocators[p_type].allocations[index] = true; |
| 62 | } |
| 63 | |
| 64 | return index; |
| 65 | } |
| 66 | void RenderForwardMobile::ForwardIDStorageMobile::free_forward_id(RendererRD::ForwardIDType p_type, RendererRD::ForwardID p_id) { |
| 67 | ERR_FAIL_INDEX(p_id, (RendererRD::ForwardID)forward_id_allocators[p_type].allocations.size()); |
| 68 | forward_id_allocators[p_type].allocations[p_id] = false; |
| 69 | } |
| 70 | |
| 71 | void RenderForwardMobile::ForwardIDStorageMobile::map_forward_id(RendererRD::ForwardIDType p_type, RendererRD::ForwardID p_id, uint32_t p_index, uint64_t p_last_pass) { |
| 72 | forward_id_allocators[p_type].map[p_id] = p_index; |
| 73 | forward_id_allocators[p_type].last_pass[p_id] = p_last_pass; |
| 74 | } |
| 75 | |
| 76 | void RenderForwardMobile::fill_push_constant_instance_indices(SceneState::InstanceData *p_instance_data, const GeometryInstanceForwardMobile *p_instance) { |
| 77 | uint64_t current_frame = RSG::rasterizer->get_frame_number(); |
| 78 | |
| 79 | p_instance_data->omni_lights[0] = 0xFFFFFFFF; |
| 80 | p_instance_data->omni_lights[1] = 0xFFFFFFFF; |
| 81 | |
| 82 | uint32_t idx = 0; |
| 83 | for (uint32_t i = 0; i < p_instance->omni_light_count; i++) { |
| 84 | uint32_t ofs = idx < 4 ? 0 : 1; |
| 85 | uint32_t shift = (idx & 0x3) << 3; |
| 86 | uint32_t mask = ~(0xFF << shift); |
| 87 | |
| 88 | if (forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_OMNI_LIGHT].last_pass[p_instance->omni_lights[i]] == current_frame) { |
| 89 | p_instance_data->omni_lights[ofs] &= mask; |
| 90 | p_instance_data->omni_lights[ofs] |= uint32_t(forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_OMNI_LIGHT].map[p_instance->omni_lights[i]]) << shift; |
| 91 | idx++; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | p_instance_data->spot_lights[0] = 0xFFFFFFFF; |
| 96 | p_instance_data->spot_lights[1] = 0xFFFFFFFF; |
| 97 | |
| 98 | idx = 0; |
| 99 | for (uint32_t i = 0; i < p_instance->spot_light_count; i++) { |
| 100 | uint32_t ofs = idx < 4 ? 0 : 1; |
| 101 | uint32_t shift = (idx & 0x3) << 3; |
| 102 | uint32_t mask = ~(0xFF << shift); |
| 103 | if (forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_SPOT_LIGHT].last_pass[p_instance->spot_lights[i]] == current_frame) { |
| 104 | p_instance_data->spot_lights[ofs] &= mask; |
| 105 | p_instance_data->spot_lights[ofs] |= uint32_t(forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_SPOT_LIGHT].map[p_instance->spot_lights[i]]) << shift; |
| 106 | idx++; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | p_instance_data->decals[0] = 0xFFFFFFFF; |
| 111 | p_instance_data->decals[1] = 0xFFFFFFFF; |
| 112 | |
| 113 | idx = 0; |
| 114 | for (uint32_t i = 0; i < p_instance->decals_count; i++) { |
| 115 | uint32_t ofs = idx < 4 ? 0 : 1; |
| 116 | uint32_t shift = (idx & 0x3) << 3; |
| 117 | uint32_t mask = ~(0xFF << shift); |
| 118 | if (forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_DECAL].last_pass[p_instance->decals[i]] == current_frame) { |
| 119 | p_instance_data->decals[ofs] &= mask; |
| 120 | p_instance_data->decals[ofs] |= uint32_t(forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_DECAL].map[p_instance->decals[i]]) << shift; |
| 121 | idx++; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | p_instance_data->reflection_probes[0] = 0xFFFFFFFF; |
| 126 | p_instance_data->reflection_probes[1] = 0xFFFFFFFF; |
| 127 | |
| 128 | ForwardIDByMapSort sorted_reflection_probes[MAX_RDL_CULL]; |
| 129 | for (uint32_t i = 0; i < p_instance->reflection_probe_count; i++) { |
| 130 | sorted_reflection_probes[i].forward_id = p_instance->reflection_probes[i]; |
| 131 | sorted_reflection_probes[i].map = forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_REFLECTION_PROBE].map[p_instance->reflection_probes[i]]; |
| 132 | } |
| 133 | |
| 134 | SortArray<ForwardIDByMapSort> sort_array; |
| 135 | sort_array.sort(sorted_reflection_probes, p_instance->reflection_probe_count); |
| 136 | |
| 137 | idx = 0; |
| 138 | for (uint32_t i = 0; i < p_instance->reflection_probe_count; i++) { |
| 139 | uint32_t ofs = idx < 4 ? 0 : 1; |
| 140 | uint32_t shift = (idx & 0x3) << 3; |
| 141 | uint32_t mask = ~(0xFF << shift); |
| 142 | if (forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_REFLECTION_PROBE].last_pass[sorted_reflection_probes[i].forward_id] == current_frame) { |
| 143 | p_instance_data->reflection_probes[ofs] &= mask; |
| 144 | p_instance_data->reflection_probes[ofs] |= uint32_t(forward_id_storage_mobile->forward_id_allocators[RendererRD::FORWARD_ID_TYPE_REFLECTION_PROBE].map[sorted_reflection_probes[i].forward_id]) << shift; |
| 145 | idx++; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* Render buffer */ |
| 151 | |
| 152 | void RenderForwardMobile::RenderBufferDataForwardMobile::free_data() { |
| 153 | // this should already be done but JIC.. |
| 154 | if (render_buffers) { |
| 155 | render_buffers->clear_context(RB_SCOPE_MOBILE); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void RenderForwardMobile::RenderBufferDataForwardMobile::configure(RenderSceneBuffersRD *p_render_buffers) { |
| 160 | if (render_buffers) { |
| 161 | // JIC |
| 162 | free_data(); |
| 163 | } |
| 164 | |
| 165 | render_buffers = p_render_buffers; |
| 166 | ERR_FAIL_NULL(render_buffers); // Huh? really? |
| 167 | } |
| 168 | |
| 169 | RID RendererSceneRenderImplementation::RenderForwardMobile::RenderBufferDataForwardMobile::get_motion_vectors_fb() { |
| 170 | ERR_FAIL_NULL_V(render_buffers, RID()); |
| 171 | |
| 172 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 173 | ERR_FAIL_NULL_V(texture_storage, RID()); |
| 174 | |
| 175 | RID velocity = render_buffers->get_velocity_buffer(false); |
| 176 | RID velocity_depth = render_buffers->get_velocity_depth_buffer(); |
| 177 | |
| 178 | if (velocity.is_valid() && velocity_depth.is_valid()) { |
| 179 | Vector<RID> textures; |
| 180 | textures.push_back(velocity); |
| 181 | textures.push_back(velocity_depth); |
| 182 | |
| 183 | Vector<RD::FramebufferPass> passes; |
| 184 | RD::FramebufferPass pass; |
| 185 | pass.color_attachments.push_back(0); |
| 186 | pass.depth_attachment = 1; |
| 187 | passes.push_back(pass); |
| 188 | |
| 189 | uint32_t view_count = render_buffers->get_view_count(); |
| 190 | |
| 191 | return FramebufferCacheRD::get_singleton()->get_cache_multipass(textures, passes, view_count); |
| 192 | } |
| 193 | |
| 194 | return RID(); |
| 195 | } |
| 196 | |
| 197 | RID RenderForwardMobile::RenderBufferDataForwardMobile::get_color_fbs(FramebufferConfigType p_config_type, bool p_resolve_depth) { |
| 198 | ERR_FAIL_NULL_V(render_buffers, RID()); |
| 199 | |
| 200 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 201 | ERR_FAIL_NULL_V(texture_storage, RID()); |
| 202 | |
| 203 | // We use our framebuffer cache here instead of building these in RenderBufferDataForwardMobile::configure |
| 204 | // This approach ensures we only build the framebuffers we actually need for this viewport. |
| 205 | // In the (near) future this means that if we cycle through a texture chain for our render target, we'll also support |
| 206 | // this. |
| 207 | |
| 208 | RS::ViewportMSAA msaa_3d = render_buffers->get_msaa_3d(); |
| 209 | bool use_msaa = msaa_3d != RS::VIEWPORT_MSAA_DISABLED; |
| 210 | |
| 211 | uint32_t view_count = render_buffers->get_view_count(); |
| 212 | |
| 213 | RID vrs_texture; |
| 214 | #ifndef XR_DISABLED |
| 215 | if (render_buffers->get_vrs_mode() == RS::VIEWPORT_VRS_XR) { |
| 216 | Ref<XRInterface> interface = XRServer::get_singleton()->get_primary_interface(); |
| 217 | if (interface.is_valid() && RD::get_singleton()->vrs_get_method() == RD::VRS_METHOD_FRAGMENT_DENSITY_MAP && interface->get_vrs_texture_format() == XRInterface::XR_VRS_TEXTURE_FORMAT_FRAGMENT_DENSITY_MAP) { |
| 218 | vrs_texture = interface->get_vrs_texture(); |
| 219 | } |
| 220 | } |
| 221 | #endif // XR_DISABLED |
| 222 | if (vrs_texture.is_null() && render_buffers->has_texture(RB_SCOPE_VRS, RB_TEXTURE)) { |
| 223 | vrs_texture = render_buffers->get_texture(RB_SCOPE_VRS, RB_TEXTURE); |
| 224 | } |
| 225 | |
| 226 | Vector<RID> textures; |
| 227 | int color_buffer_id = 0; |
| 228 | int depth_buffer_id = 1; |
| 229 | textures.push_back(use_msaa ? render_buffers->get_color_msaa() : render_buffers->get_internal_texture()); // 0 - color buffer. |
| 230 | textures.push_back(use_msaa ? render_buffers->get_depth_msaa() : render_buffers->get_depth_texture()); // 1 - depth buffer. |
| 231 | if (vrs_texture.is_valid()) { |
| 232 | textures.push_back(vrs_texture); // 2 - vrs texture. |
| 233 | } |
| 234 | if (use_msaa) { |
| 235 | color_buffer_id = textures.size(); |
| 236 | textures.push_back(render_buffers->get_internal_texture()); // Color buffer for resolve. |
| 237 | } |
| 238 | if (use_msaa && p_resolve_depth) { |
| 239 | depth_buffer_id = textures.size(); |
| 240 | textures.push_back(render_buffers->get_depth_texture()); // Depth buffer for resolve. |
| 241 | } |
| 242 | |
| 243 | // Now define our subpasses. |
| 244 | Vector<RD::FramebufferPass> passes; |
| 245 | |
| 246 | switch (p_config_type) { |
| 247 | case FB_CONFIG_RENDER_PASS: { |
| 248 | RD::FramebufferPass pass; |
| 249 | pass.color_attachments.push_back(0); |
| 250 | pass.depth_attachment = 1; |
| 251 | |
| 252 | if (use_msaa) { |
| 253 | // Add color resolve. |
| 254 | pass.resolve_attachments.push_back(color_buffer_id); |
| 255 | |
| 256 | if (p_resolve_depth) { |
| 257 | // Add depth resolve. |
| 258 | pass.depth_resolve_attachment = depth_buffer_id; |
| 259 | } |
| 260 | } |
| 261 | passes.push_back(pass); |
| 262 | |
| 263 | return FramebufferCacheRD::get_singleton()->get_cache_multipass(textures, passes, view_count); |
| 264 | } break; |
| 265 | |
| 266 | case FB_CONFIG_RENDER_AND_POST_PASS: { |
| 267 | Size2i target_size = render_buffers->get_target_size(); |
| 268 | Size2i internal_size = render_buffers->get_internal_size(); |
| 269 | |
| 270 | // Can't do our blit pass if resolutions don't match, this should already have been checked. |
| 271 | ERR_FAIL_COND_V(target_size != internal_size, RID()); |
| 272 | |
| 273 | RD::FramebufferPass pass; |
| 274 | pass.color_attachments.push_back(0); |
| 275 | pass.depth_attachment = 1; |
| 276 | |
| 277 | if (use_msaa) { |
| 278 | // Add color resolve. |
| 279 | pass.resolve_attachments.push_back(color_buffer_id); |
| 280 | |
| 281 | if (p_resolve_depth) { |
| 282 | // Add depth resolve. |
| 283 | pass.depth_resolve_attachment = depth_buffer_id; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | passes.push_back(pass); |
| 288 | |
| 289 | // Add blit to 2D pass. |
| 290 | RID render_target = render_buffers->get_render_target(); |
| 291 | ERR_FAIL_COND_V(render_target.is_null(), RID()); |
| 292 | RID target_buffer; |
| 293 | if (view_count > 1 || texture_storage->render_target_get_msaa(render_target) == RS::VIEWPORT_MSAA_DISABLED) { |
| 294 | target_buffer = texture_storage->render_target_get_rd_texture(render_target); |
| 295 | } else { |
| 296 | target_buffer = texture_storage->render_target_get_rd_texture_msaa(render_target); |
| 297 | texture_storage->render_target_set_msaa_needs_resolve(render_target, true); // Make sure this gets resolved. |
| 298 | } |
| 299 | ERR_FAIL_COND_V(target_buffer.is_null(), RID()); |
| 300 | |
| 301 | int target_buffer_id = textures.size(); |
| 302 | textures.push_back(target_buffer); // Target buffer. |
| 303 | |
| 304 | RD::FramebufferPass blit_pass; |
| 305 | blit_pass.input_attachments.push_back(color_buffer_id); // Read from our (resolved) color buffer. |
| 306 | blit_pass.color_attachments.push_back(target_buffer_id); // Write into our target buffer. |
| 307 | // This doesn't need VRS or depth. |
| 308 | passes.push_back(blit_pass); |
| 309 | |
| 310 | return FramebufferCacheRD::get_singleton()->get_cache_multipass(textures, passes, view_count); |
| 311 | } break; |
| 312 | default: |
| 313 | break; |
| 314 | }; |
| 315 | |
| 316 | return RID(); |
| 317 | } |
| 318 | |
| 319 | RID RenderForwardMobile::reflection_probe_create_framebuffer(RID p_color, RID p_depth) { |
| 320 | // Our attachments |
| 321 | Vector<RID> attachments; |
| 322 | attachments.push_back(p_color); // 0 |
| 323 | attachments.push_back(p_depth); // 1 |
| 324 | |
| 325 | return RD::get_singleton()->framebuffer_create(attachments); |
| 326 | } |
| 327 | |
| 328 | void RenderForwardMobile::setup_render_buffer_data(Ref<RenderSceneBuffersRD> p_render_buffers) { |
| 329 | Ref<RenderBufferDataForwardMobile> data; |
| 330 | data.instantiate(); |
| 331 | |
| 332 | p_render_buffers->set_custom_data(RB_SCOPE_MOBILE, data); |
| 333 | } |
| 334 | |
| 335 | void RenderForwardMobile::mesh_generate_pipelines(RID p_mesh, bool p_background_compilation) { |
| 336 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 337 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 338 | RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh); |
| 339 | uint32_t surface_count = 0; |
| 340 | const RID *materials = mesh_storage->mesh_get_surface_count_and_materials(p_mesh, surface_count); |
| 341 | Vector<ShaderPipelinePair> pipeline_pairs; |
| 342 | for (uint32_t i = 0; i < surface_count; i++) { |
| 343 | if (materials[i].is_null()) { |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | void *mesh_surface = mesh_storage->mesh_get_surface(p_mesh, i); |
| 348 | void *mesh_surface_shadow = mesh_surface; |
| 349 | SceneShaderForwardMobile::MaterialData *material = static_cast<SceneShaderForwardMobile::MaterialData *>(material_storage->material_get_data(materials[i], RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 350 | if (material == nullptr || !material->shader_data->is_valid()) { |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | SceneShaderForwardMobile::ShaderData *shader = material->shader_data; |
| 355 | SceneShaderForwardMobile::ShaderData *shader_shadow = shader; |
| 356 | if (material->shader_data->uses_shared_shadow_material()) { |
| 357 | SceneShaderForwardMobile::MaterialData *material_shadow = static_cast<SceneShaderForwardMobile::MaterialData *>(material_storage->material_get_data(scene_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 358 | if (material_shadow != nullptr) { |
| 359 | shader_shadow = material_shadow->shader_data; |
| 360 | if (shadow_mesh.is_valid()) { |
| 361 | mesh_surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, i); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | if (!shader->is_valid()) { |
| 367 | continue; |
| 368 | } |
| 369 | |
| 370 | SurfacePipelineData surface; |
| 371 | surface.mesh_surface = mesh_surface; |
| 372 | surface.mesh_surface_shadow = mesh_surface_shadow; |
| 373 | surface.shader = shader; |
| 374 | surface.shader_shadow = shader_shadow; |
| 375 | surface.instanced = mesh_storage->mesh_needs_instance(p_mesh, true); |
| 376 | surface.uses_opaque = !material->shader_data->uses_alpha_pass(); |
| 377 | surface.uses_transparent = material->shader_data->uses_alpha_pass(); |
| 378 | surface.uses_depth = surface.uses_opaque || (surface.uses_transparent && material->shader_data->uses_depth_in_alpha_pass()); |
| 379 | surface.can_use_lightmap = mesh_storage->mesh_surface_get_format(mesh_surface) & RS::ARRAY_FORMAT_TEX_UV2; |
| 380 | _mesh_compile_pipelines_for_surface(surface, global_pipeline_data_required, RS::PIPELINE_SOURCE_MESH, &pipeline_pairs); |
| 381 | } |
| 382 | |
| 383 | // Try to wait for all the pipelines that were compiled. This will force the loader to wait on all ubershader pipelines to be ready. |
| 384 | if (!p_background_compilation && !pipeline_pairs.is_empty()) { |
| 385 | for (ShaderPipelinePair pair : pipeline_pairs) { |
| 386 | pair.first->pipeline_hash_map.wait_for_pipeline(pair.second.hash()); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | uint32_t RenderForwardMobile::get_pipeline_compilations(RS::PipelineSource p_source) { |
| 392 | return scene_shader.get_pipeline_compilations(p_source); |
| 393 | } |
| 394 | |
| 395 | void RenderForwardMobile::enable_features(BitField<FeatureBits> p_feature_bits) { |
| 396 | if (p_feature_bits.has_flag(FEATURE_FP32_BIT)) { |
| 397 | scene_shader.enable_fp32_shader_group(); |
| 398 | } |
| 399 | |
| 400 | if (p_feature_bits.has_flag(FEATURE_FP16_BIT)) { |
| 401 | scene_shader.enable_fp16_shader_group(); |
| 402 | } |
| 403 | |
| 404 | if (p_feature_bits.has_flag(FEATURE_MULTIVIEW_BIT)) { |
| 405 | scene_shader.enable_multiview_shader_group(); |
| 406 | } |
| 407 | |
| 408 | if (p_feature_bits.has_flag(FEATURE_VRS_BIT)) { |
| 409 | gi.enable_vrs_shader_group(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | String RenderForwardMobile::get_name() const { |
| 414 | return "forward_mobile"; |
| 415 | } |
| 416 | |
| 417 | bool RenderForwardMobile::free(RID p_rid) { |
| 418 | if (RendererSceneRenderRD::free(p_rid)) { |
| 419 | return true; |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | void RenderForwardMobile::update() { |
| 425 | RendererSceneRenderRD::update(); |
| 426 | _update_global_pipeline_data_requirements_from_project(); |
| 427 | _update_global_pipeline_data_requirements_from_light_storage(); |
| 428 | } |
| 429 | |
| 430 | /* Render functions */ |
| 431 | |
| 432 | RD::DataFormat RenderForwardMobile::_render_buffers_get_preferred_color_format() { |
| 433 | // Using 32bit buffers enables AFBC on mobile devices which should have a definite performance improvement (MALI G710 and newer support this on 64bit RTs) |
| 434 | return RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32; |
| 435 | } |
| 436 | |
| 437 | bool RenderForwardMobile::_render_buffers_can_be_storage() { |
| 438 | // Doesn't support storage |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | RID RenderForwardMobile::_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, uint32_t p_pass_offset) { |
| 443 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 444 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 445 | |
| 446 | bool is_multiview = false; |
| 447 | |
| 448 | Ref<RenderBufferDataForwardMobile> rb_data; |
| 449 | Ref<RenderSceneBuffersRD> rb; |
| 450 | if (p_render_data && p_render_data->render_buffers.is_valid()) { |
| 451 | rb = p_render_data->render_buffers; |
| 452 | is_multiview = rb->get_view_count() > 1; |
| 453 | if (rb->has_custom_data(RB_SCOPE_MOBILE)) { |
| 454 | // Our forward mobile custom data buffer will only be available when we're rendering our normal view. |
| 455 | // This will not be available when rendering reflection probes. |
| 456 | rb_data = rb->get_custom_data(RB_SCOPE_MOBILE); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // default render buffer and scene state uniform set |
| 461 | // loaded into set 1 |
| 462 | |
| 463 | thread_local LocalVector<RD::Uniform> uniforms; |
| 464 | uniforms.clear(); |
| 465 | |
| 466 | { |
| 467 | RD::Uniform u; |
| 468 | u.binding = 0; |
| 469 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER_DYNAMIC; |
| 470 | // Negative on purpose. We've created multiple uniform_buffers by calling prepare_for_upload() |
| 471 | // many times in a row, now we must reference those. |
| 472 | // We use 0u - p_pass_offset instead of -p_pass_offset to make MSVC warnings shut up. |
| 473 | // See the "Tricks" section of MultiUmaBuffer documentation. |
| 474 | u.append_id(scene_state.uniform_buffers._get(uint32_t(0u - p_pass_offset))); |
| 475 | uniforms.push_back(u); |
| 476 | } |
| 477 | |
| 478 | { |
| 479 | RD::Uniform u; |
| 480 | u.binding = 1; |
| 481 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC; |
| 482 | if (scene_state.instance_buffer[p_render_list].get_size(0u) == 0u) { |
| 483 | // Any buffer will do since it's not used, so just create one. |
| 484 | // We can't use scene_shader.default_vec4_xform_buffer because it's not dynamic. |
| 485 | scene_state.instance_buffer[p_render_list].set_storage_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData)); |
| 486 | scene_state.instance_buffer[p_render_list].prepare_for_upload(); |
| 487 | } |
| 488 | RID instance_buffer = scene_state.instance_buffer[p_render_list]._get(0u); |
| 489 | u.append_id(instance_buffer); |
| 490 | uniforms.push_back(u); |
| 491 | } |
| 492 | |
| 493 | { |
| 494 | RID radiance_texture; |
| 495 | if (p_radiance_texture.is_valid()) { |
| 496 | radiance_texture = p_radiance_texture; |
| 497 | } else { |
| 498 | 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); |
| 499 | } |
| 500 | RD::Uniform u; |
| 501 | u.binding = 2; |
| 502 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 503 | u.append_id(radiance_texture); |
| 504 | uniforms.push_back(u); |
| 505 | } |
| 506 | |
| 507 | { |
| 508 | 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(); |
| 509 | RD::Uniform u; |
| 510 | u.binding = 3; |
| 511 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 512 | if (ref_texture.is_valid()) { |
| 513 | u.append_id(ref_texture); |
| 514 | } else { |
| 515 | u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK)); |
| 516 | } |
| 517 | uniforms.push_back(u); |
| 518 | } |
| 519 | |
| 520 | { |
| 521 | RD::Uniform u; |
| 522 | u.binding = 4; |
| 523 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 524 | RID texture; |
| 525 | if (p_render_data && p_render_data->shadow_atlas.is_valid()) { |
| 526 | texture = light_storage->shadow_atlas_get_texture(p_render_data->shadow_atlas); |
| 527 | } |
| 528 | if (!texture.is_valid()) { |
| 529 | texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH); |
| 530 | } |
| 531 | u.append_id(texture); |
| 532 | uniforms.push_back(u); |
| 533 | } |
| 534 | { |
| 535 | RD::Uniform u; |
| 536 | u.binding = 5; |
| 537 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 538 | if (p_use_directional_shadow_atlas && light_storage->directional_shadow_get_texture().is_valid()) { |
| 539 | u.append_id(light_storage->directional_shadow_get_texture()); |
| 540 | } else { |
| 541 | u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH)); |
| 542 | } |
| 543 | uniforms.push_back(u); |
| 544 | } |
| 545 | |
| 546 | /* we have limited ability to keep textures like this so we're moving this to a set we change before drawing geometry and just pushing the needed texture in */ |
| 547 | { |
| 548 | Vector<RID> textures; |
| 549 | textures.resize(scene_state.max_lightmaps * 2); |
| 550 | |
| 551 | RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE); |
| 552 | for (uint32_t i = 0; i < scene_state.max_lightmaps * 2; i++) { |
| 553 | uint32_t current_lightmap_index = i < scene_state.max_lightmaps ? i : i - scene_state.max_lightmaps; |
| 554 | |
| 555 | if (p_render_data && current_lightmap_index < p_render_data->lightmaps->size()) { |
| 556 | RID base = light_storage->lightmap_instance_get_lightmap((*p_render_data->lightmaps)[current_lightmap_index]); |
| 557 | RID texture; |
| 558 | |
| 559 | if (i < scene_state.max_lightmaps) { |
| 560 | // Lightmap |
| 561 | texture = light_storage->lightmap_get_texture(base); |
| 562 | } else { |
| 563 | // Shadowmask |
| 564 | texture = light_storage->shadowmask_get_texture(base); |
| 565 | } |
| 566 | |
| 567 | if (texture.is_valid()) { |
| 568 | RID rd_texture = texture_storage->texture_get_rd_texture(texture); |
| 569 | textures.write[i] = rd_texture; |
| 570 | continue; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | textures.write[i] = default_tex; |
| 575 | } |
| 576 | RD::Uniform u(RD::UNIFORM_TYPE_TEXTURE, 6, textures); |
| 577 | uniforms.push_back(u); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | { |
| 582 | RD::Uniform u; |
| 583 | u.binding = 7; |
| 584 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 585 | u.ids.resize(MAX_VOXEL_GI_INSTANCESS); |
| 586 | RID default_tex = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE); |
| 587 | for (int i = 0; i < MAX_VOXEL_GI_INSTANCESS; i++) { |
| 588 | if (i < (int)p_voxel_gi_instances.size()) { |
| 589 | RID tex = gi.voxel_gi_instance_get_texture(p_voxel_gi_instances[i]); |
| 590 | if (!tex.is_valid()) { |
| 591 | tex = default_tex; |
| 592 | } |
| 593 | u.ids.write[i] = tex; |
| 594 | } else { |
| 595 | u.ids.write[i] = default_tex; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | uniforms.push_back(u); |
| 600 | } |
| 601 | */ |
| 602 | |
| 603 | { |
| 604 | RD::Uniform u; |
| 605 | u.binding = 9; |
| 606 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 607 | RID texture; |
| 608 | if (rb.is_valid() && rb->has_texture(RB_SCOPE_BUFFERS, RB_TEX_BACK_DEPTH)) { |
| 609 | texture = rb->get_texture(RB_SCOPE_BUFFERS, RB_TEX_BACK_DEPTH); |
| 610 | } else if (is_multiview) { |
| 611 | texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_DEPTH); |
| 612 | } else { |
| 613 | texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_DEPTH); |
| 614 | } |
| 615 | u.append_id(texture); |
| 616 | uniforms.push_back(u); |
| 617 | } |
| 618 | { |
| 619 | RD::Uniform u; |
| 620 | u.binding = 10; |
| 621 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 622 | RID texture = rb_data.is_valid() ? rb->get_back_buffer_texture() : RID(); |
| 623 | if (texture.is_null()) { |
| 624 | if (is_multiview) { |
| 625 | texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_DEPTH); |
| 626 | } else { |
| 627 | texture = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK); |
| 628 | } |
| 629 | } |
| 630 | u.append_id(texture); |
| 631 | uniforms.push_back(u); |
| 632 | } |
| 633 | |
| 634 | { |
| 635 | RD::Uniform u; |
| 636 | u.binding = 11; |
| 637 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 638 | RID sampler; |
| 639 | switch (decals_get_filter()) { |
| 640 | case RS::DECAL_FILTER_NEAREST: { |
| 641 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 642 | } break; |
| 643 | case RS::DECAL_FILTER_LINEAR: { |
| 644 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 645 | } break; |
| 646 | case RS::DECAL_FILTER_NEAREST_MIPMAPS: { |
| 647 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 648 | } break; |
| 649 | case RS::DECAL_FILTER_LINEAR_MIPMAPS: { |
| 650 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 651 | } break; |
| 652 | case RS::DECAL_FILTER_NEAREST_MIPMAPS_ANISOTROPIC: { |
| 653 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 654 | } break; |
| 655 | case RS::DECAL_FILTER_LINEAR_MIPMAPS_ANISOTROPIC: { |
| 656 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 657 | } break; |
| 658 | } |
| 659 | |
| 660 | u.append_id(sampler); |
| 661 | uniforms.push_back(u); |
| 662 | } |
| 663 | |
| 664 | { |
| 665 | RD::Uniform u; |
| 666 | u.binding = 12; |
| 667 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 668 | RID sampler; |
| 669 | switch (light_projectors_get_filter()) { |
| 670 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST: { |
| 671 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 672 | } break; |
| 673 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR: { |
| 674 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 675 | } break; |
| 676 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS: { |
| 677 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 678 | } break; |
| 679 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS: { |
| 680 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 681 | } break; |
| 682 | case RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS_ANISOTROPIC: { |
| 683 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 684 | } break; |
| 685 | case RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS_ANISOTROPIC: { |
| 686 | sampler = p_samplers.get_sampler(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 687 | } break; |
| 688 | } |
| 689 | |
| 690 | u.append_id(sampler); |
| 691 | uniforms.push_back(u); |
| 692 | } |
| 693 | |
| 694 | p_samplers.append_uniforms(uniforms, 13); |
| 695 | |
| 696 | return UniformSetCacheRD::get_singleton()->get_cache_vec(scene_shader.default_shader_rd, RENDER_PASS_UNIFORM_SET, uniforms); |
| 697 | } |
| 698 | |
| 699 | void RenderForwardMobile::_setup_lightmaps(const RenderDataRD *p_render_data, const PagedArray<RID> &p_lightmaps, const Transform3D &p_cam_transform) { |
| 700 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 701 | |
| 702 | // This probably needs to change... |
| 703 | scene_state.lightmaps_used = 0; |
| 704 | for (int i = 0; i < (int)p_lightmaps.size(); i++) { |
| 705 | if (i >= (int)scene_state.max_lightmaps) { |
| 706 | break; |
| 707 | } |
| 708 | |
| 709 | RID lightmap = light_storage->lightmap_instance_get_lightmap(p_lightmaps[i]); |
| 710 | |
| 711 | // Transform (for directional lightmaps). |
| 712 | Basis to_lm = light_storage->lightmap_instance_get_transform(p_lightmaps[i]).basis.inverse() * p_cam_transform.basis; |
| 713 | to_lm = to_lm.inverse().transposed(); //will transform normals |
| 714 | RendererRD::MaterialStorage::store_transform_3x3(to_lm, scene_state.lightmaps[i].normal_xform); |
| 715 | |
| 716 | // Light texture size. |
| 717 | Vector2i lightmap_size = light_storage->lightmap_get_light_texture_size(lightmap); |
| 718 | scene_state.lightmaps[i].texture_size[0] = lightmap_size[0]; |
| 719 | scene_state.lightmaps[i].texture_size[1] = lightmap_size[1]; |
| 720 | |
| 721 | // Exposure. |
| 722 | scene_state.lightmaps[i].exposure_normalization = 1.0; |
| 723 | scene_state.lightmaps[i].flags = light_storage->lightmap_get_shadowmask_mode(lightmap); |
| 724 | if (p_render_data->camera_attributes.is_valid()) { |
| 725 | float baked_exposure = light_storage->lightmap_get_baked_exposure_normalization(lightmap); |
| 726 | float enf = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes); |
| 727 | scene_state.lightmaps[i].exposure_normalization = enf / baked_exposure; |
| 728 | } |
| 729 | |
| 730 | scene_state.lightmap_ids[i] = p_lightmaps[i]; |
| 731 | scene_state.lightmap_has_sh[i] = light_storage->lightmap_uses_spherical_harmonics(lightmap); |
| 732 | |
| 733 | scene_state.lightmaps_used++; |
| 734 | } |
| 735 | if (scene_state.lightmaps_used > 0) { |
| 736 | RD::get_singleton()->buffer_update(scene_state.lightmap_buffer, 0, sizeof(LightmapData) * scene_state.lightmaps_used, scene_state.lightmaps); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void RenderForwardMobile::_pre_opaque_render(RenderDataRD *p_render_data) { |
| 741 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 742 | |
| 743 | p_render_data->cube_shadows.clear(); |
| 744 | p_render_data->shadows.clear(); |
| 745 | p_render_data->directional_shadows.clear(); |
| 746 | |
| 747 | float lod_distance_multiplier = p_render_data->scene_data->cam_projection.get_lod_multiplier(); |
| 748 | { |
| 749 | for (int i = 0; i < p_render_data->render_shadow_count; i++) { |
| 750 | RID li = p_render_data->render_shadows[i].light; |
| 751 | RID base = light_storage->light_instance_get_base_light(li); |
| 752 | |
| 753 | if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) { |
| 754 | p_render_data->directional_shadows.push_back(i); |
| 755 | } else if (light_storage->light_get_type(base) == RS::LIGHT_OMNI && light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) { |
| 756 | p_render_data->cube_shadows.push_back(i); |
| 757 | } else { |
| 758 | p_render_data->shadows.push_back(i); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | //cube shadows are rendered in their own way |
| 763 | for (const int &index : p_render_data->cube_shadows) { |
| 764 | _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, p_render_data->scene_data->cam_transform); |
| 765 | } |
| 766 | |
| 767 | if (p_render_data->directional_shadows.size()) { |
| 768 | //open the pass for directional shadows |
| 769 | light_storage->update_directional_shadow_atlas(); |
| 770 | RD::get_singleton()->draw_list_begin(light_storage->direction_shadow_get_fb(), RD::DRAW_CLEAR_DEPTH, Vector<Color>(), 0.0f); |
| 771 | RD::get_singleton()->draw_list_end(); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | bool render_shadows = p_render_data->directional_shadows.size() || p_render_data->shadows.size(); |
| 776 | |
| 777 | //prepare shadow rendering |
| 778 | if (render_shadows) { |
| 779 | RENDER_TIMESTAMP("Render Shadows"); |
| 780 | |
| 781 | _render_shadow_begin(); |
| 782 | |
| 783 | //render directional shadows |
| 784 | for (uint32_t i = 0; i < p_render_data->directional_shadows.size(); i++) { |
| 785 | _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, p_render_data->scene_data->cam_transform); |
| 786 | } |
| 787 | //render positional shadows |
| 788 | for (uint32_t i = 0; i < p_render_data->shadows.size(); i++) { |
| 789 | _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, p_render_data->scene_data->cam_transform); |
| 790 | } |
| 791 | |
| 792 | _render_shadow_process(); |
| 793 | |
| 794 | _render_shadow_end(); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color &p_default_bg_color) { |
| 799 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 800 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 801 | |
| 802 | ERR_FAIL_NULL(p_render_data); |
| 803 | |
| 804 | Ref<RenderSceneBuffersRD> rb = p_render_data->render_buffers; |
| 805 | ERR_FAIL_COND(rb.is_null()); |
| 806 | |
| 807 | Ref<RenderBufferDataForwardMobile> rb_data; |
| 808 | if (rb->has_custom_data(RB_SCOPE_MOBILE)) { |
| 809 | // Our forward mobile custom data buffer will only be available when we're rendering our normal view. |
| 810 | // This will not be available when rendering reflection probes. |
| 811 | rb_data = rb->get_custom_data(RB_SCOPE_MOBILE); |
| 812 | } |
| 813 | bool is_reflection_probe = p_render_data->reflection_probe.is_valid(); |
| 814 | |
| 815 | RENDER_TIMESTAMP("Prepare 3D Scene"); |
| 816 | |
| 817 | _update_vrs(rb); |
| 818 | |
| 819 | if (rb->has_texture(RB_SCOPE_VRS, RB_TEXTURE)) { |
| 820 | global_pipeline_data_required.use_vrs = true; |
| 821 | } |
| 822 | |
| 823 | RENDER_TIMESTAMP("Setup 3D Scene"); |
| 824 | |
| 825 | bool has_depth_texture_override = false; |
| 826 | bool supports_depth_resolve = RenderingDevice::get_singleton()->has_feature(RD::SUPPORTS_FRAMEBUFFER_DEPTH_RESOLVE); |
| 827 | |
| 828 | RID render_target = rb->get_render_target(); |
| 829 | if (render_target.is_valid()) { |
| 830 | p_render_data->scene_data->calculate_motion_vectors = texture_storage->render_target_get_override_velocity(render_target).is_valid(); |
| 831 | has_depth_texture_override = texture_storage->render_target_get_override_depth(render_target).is_valid(); |
| 832 | } else { |
| 833 | p_render_data->scene_data->calculate_motion_vectors = false; |
| 834 | } |
| 835 | |
| 836 | p_render_data->scene_data->directional_light_count = 0; |
| 837 | p_render_data->scene_data->opaque_prepass_threshold = 0.0; |
| 838 | |
| 839 | // We can only use our full subpass approach if we're: |
| 840 | // - not reading from SCREEN_TEXTURE/DEPTH_TEXTURE |
| 841 | // - not using ssr/sss (currently not supported) |
| 842 | // - not using glow or other post effects (can't do 4th subpass) |
| 843 | // - rendering to a half sized render buffer (can't do 4th subpass) |
| 844 | // We'll need to restrict how far we're going with subpasses based on this. |
| 845 | |
| 846 | Size2i screen_size; |
| 847 | RID framebuffer; |
| 848 | bool reverse_cull = p_render_data->scene_data->cam_transform.basis.determinant() < 0; |
| 849 | bool merge_transparent_pass = true; // If true: we can do our transparent pass in the same pass as our opaque pass. |
| 850 | bool using_subpass_post_process = true; // If true: we can do our post processing in a subpass |
| 851 | RendererRD::MaterialStorage::Samplers samplers; |
| 852 | bool hdr_render_target = false; |
| 853 | |
| 854 | RS::ViewportMSAA msaa = rb->get_msaa_3d(); |
| 855 | bool use_msaa = msaa != RS::VIEWPORT_MSAA_DISABLED; |
| 856 | bool resolve_depth_buffer = (use_msaa && has_depth_texture_override); // We'll check more conditions later. |
| 857 | |
| 858 | bool ce_has_post_opaque = _has_compositor_effect(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_POST_OPAQUE, p_render_data); |
| 859 | bool ce_has_pre_transparent = _has_compositor_effect(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_TRANSPARENT, p_render_data); |
| 860 | bool ce_has_post_transparent = _has_compositor_effect(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_POST_TRANSPARENT, p_render_data); |
| 861 | |
| 862 | if (ce_has_post_opaque) { |
| 863 | // As we're doing opaque and sky in subpasses we don't support this *yet* |
| 864 | WARN_PRINT_ONCE("Post opaque rendering effect callback is not supported in the mobile renderer"); |
| 865 | } |
| 866 | |
| 867 | // Using RenderingEffects limits our ability to do subpasses.. |
| 868 | if (ce_has_pre_transparent) { |
| 869 | merge_transparent_pass = false; |
| 870 | using_subpass_post_process = false; |
| 871 | } |
| 872 | if (ce_has_post_transparent) { |
| 873 | using_subpass_post_process = false; |
| 874 | } |
| 875 | |
| 876 | if (use_msaa && _compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_DEPTH, RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_ANY)) { |
| 877 | // We'll be able to do this when we finish PR #78598 |
| 878 | WARN_PRINT_ONCE("Depth buffer resolve for rendering effect callback is not supported in the mobile renderer"); |
| 879 | } |
| 880 | |
| 881 | // We don't need to check resolve color flag, it will be resolved for transparent passes regardless. |
| 882 | |
| 883 | bool using_shadows = true; |
| 884 | |
| 885 | if (p_render_data->reflection_probe.is_valid()) { |
| 886 | if (!RSG::light_storage->reflection_probe_renders_shadows(light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe))) { |
| 887 | using_shadows = false; |
| 888 | } |
| 889 | } else { |
| 890 | //do not render reflections when rendering a reflection probe |
| 891 | 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); |
| 892 | } |
| 893 | |
| 894 | // Update light and decal buffer first so we know what lights and decals are safe to pair with. |
| 895 | uint32_t directional_light_count = 0; |
| 896 | uint32_t positional_light_count = 0; |
| 897 | 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); |
| 898 | texture_storage->update_decal_buffer(*p_render_data->decals, p_render_data->scene_data->cam_transform); |
| 899 | |
| 900 | p_render_data->directional_light_count = directional_light_count; |
| 901 | |
| 902 | // Lightmaps need to be set up before _fill_render_list as it depends on them. |
| 903 | _setup_lightmaps(p_render_data, *p_render_data->lightmaps, p_render_data->scene_data->cam_transform); |
| 904 | |
| 905 | // fill our render lists early so we can find out if we use various features |
| 906 | _fill_render_list(RENDER_LIST_OPAQUE, p_render_data, PASS_MODE_COLOR); |
| 907 | if (scene_state.used_opaque_stencil) { |
| 908 | render_list[RENDER_LIST_OPAQUE].sort_by_key_and_stencil(); |
| 909 | } else { |
| 910 | render_list[RENDER_LIST_OPAQUE].sort_by_key(); |
| 911 | } |
| 912 | render_list[RENDER_LIST_ALPHA].sort_by_reverse_depth_and_priority(); |
| 913 | |
| 914 | _fill_instance_data(RENDER_LIST_OPAQUE); |
| 915 | _fill_instance_data(RENDER_LIST_ALPHA); |
| 916 | |
| 917 | if (p_render_data->render_info) { |
| 918 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME] = p_render_data->instances->size(); |
| 919 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] = p_render_data->instances->size(); |
| 920 | } |
| 921 | |
| 922 | if (is_reflection_probe) { |
| 923 | uint32_t resolution = light_storage->reflection_probe_instance_get_resolution(p_render_data->reflection_probe); |
| 924 | screen_size.x = resolution; |
| 925 | screen_size.y = resolution; |
| 926 | |
| 927 | framebuffer = light_storage->reflection_probe_instance_get_framebuffer(p_render_data->reflection_probe, p_render_data->reflection_probe_pass); |
| 928 | |
| 929 | if (light_storage->reflection_probe_is_interior(light_storage->reflection_probe_instance_get_probe(p_render_data->reflection_probe))) { |
| 930 | p_render_data->environment = RID(); //no environment on interiors |
| 931 | } |
| 932 | |
| 933 | reverse_cull = true; |
| 934 | merge_transparent_pass = true; // we ignore our screen/depth texture here |
| 935 | using_subpass_post_process = false; // not applicable at all for reflection probes. |
| 936 | samplers = RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default(); |
| 937 | |
| 938 | // Indicate pipelines for reflection probes are required. |
| 939 | global_pipeline_data_required.use_reflection_probes = true; |
| 940 | } else if (rb_data.is_valid()) { |
| 941 | // setup rendering to render buffer |
| 942 | screen_size = p_render_data->render_buffers->get_internal_size(); |
| 943 | |
| 944 | if (rb->get_scaling_3d_mode() != RS::VIEWPORT_SCALING_3D_MODE_OFF) { |
| 945 | // can't do blit subpass because we're scaling |
| 946 | using_subpass_post_process = false; |
| 947 | } else if (p_render_data->environment.is_valid() && (environment_get_glow_enabled(p_render_data->environment) || RSG::camera_attributes->camera_attributes_uses_auto_exposure(p_render_data->camera_attributes) || RSG::camera_attributes->camera_attributes_uses_dof(p_render_data->camera_attributes) || environment_get_background(p_render_data->environment) == RS::ENV_BG_CANVAS)) { |
| 948 | // can't do blit subpass because we're using post processes |
| 949 | using_subpass_post_process = false; |
| 950 | } |
| 951 | |
| 952 | if (rb->get_screen_space_aa() != RS::VIEWPORT_SCREEN_SPACE_AA_DISABLED) { |
| 953 | // Can't do blit subpass because we're using screen space AA. |
| 954 | using_subpass_post_process = false; |
| 955 | } |
| 956 | |
| 957 | if (use_msaa && p_render_data->environment.is_valid() && RSG::camera_attributes->camera_attributes_uses_dof(p_render_data->camera_attributes)) { |
| 958 | // Need to resolve depth texture for DOF when using MSAA. |
| 959 | scene_state.used_depth_texture = true; |
| 960 | resolve_depth_buffer = true; |
| 961 | } |
| 962 | |
| 963 | if (scene_state.used_screen_texture || scene_state.used_depth_texture) { |
| 964 | // can't use our last two subpasses because we're reading from screen texture or depth texture |
| 965 | merge_transparent_pass = false; |
| 966 | using_subpass_post_process = false; |
| 967 | } |
| 968 | |
| 969 | if (use_msaa && (global_surface_data.depth_texture_used || scene_state.used_depth_texture)) { |
| 970 | resolve_depth_buffer = true; |
| 971 | } |
| 972 | |
| 973 | if (using_subpass_post_process) { |
| 974 | // We can do all in one go. |
| 975 | framebuffer = rb_data->get_color_fbs(RenderBufferDataForwardMobile::FB_CONFIG_RENDER_AND_POST_PASS, resolve_depth_buffer && supports_depth_resolve); |
| 976 | global_pipeline_data_required.use_subpass_post_pass = true; |
| 977 | } else { |
| 978 | // We separate things out. |
| 979 | framebuffer = rb_data->get_color_fbs(RenderBufferDataForwardMobile::FB_CONFIG_RENDER_PASS, resolve_depth_buffer && supports_depth_resolve); |
| 980 | global_pipeline_data_required.use_separate_post_pass = true; |
| 981 | } |
| 982 | samplers = rb->get_samplers(); |
| 983 | |
| 984 | hdr_render_target = RendererRD::TextureStorage::get_singleton()->render_target_is_using_hdr(rb->get_render_target()); |
| 985 | if (hdr_render_target) { |
| 986 | global_pipeline_data_required.use_hdr_render_target = true; |
| 987 | } else { |
| 988 | global_pipeline_data_required.use_ldr_render_target = true; |
| 989 | } |
| 990 | } else { |
| 991 | ERR_FAIL(); //bug? |
| 992 | } |
| 993 | |
| 994 | if (p_render_data->scene_data->view_count > 1) { |
| 995 | global_pipeline_data_required.use_multiview = true; |
| 996 | } |
| 997 | |
| 998 | if (scene_state.used_lightmap) { |
| 999 | global_pipeline_data_required.use_lightmaps = true; |
| 1000 | } |
| 1001 | |
| 1002 | if (global_surface_data.screen_texture_used || global_surface_data.depth_texture_used) { |
| 1003 | if (rb_data.is_valid()) { |
| 1004 | // Just called to create the framebuffer since we know we will need it later. |
| 1005 | rb_data->get_color_fbs(RenderBufferDataForwardMobile::FB_CONFIG_RENDER_PASS); |
| 1006 | } |
| 1007 | global_pipeline_data_required.use_separate_post_pass = true; |
| 1008 | } |
| 1009 | |
| 1010 | _update_dirty_geometry_pipelines(); |
| 1011 | |
| 1012 | p_render_data->scene_data->emissive_exposure_normalization = -1.0; |
| 1013 | |
| 1014 | RD::get_singleton()->draw_command_begin_label("Render Setup"); |
| 1015 | |
| 1016 | _setup_environment(p_render_data, is_reflection_probe, screen_size, screen_size, p_default_bg_color, false); |
| 1017 | |
| 1018 | // May have changed due to the above (light buffer enlarged, as an example). |
| 1019 | _update_render_base_uniform_set(); |
| 1020 | |
| 1021 | RD::get_singleton()->draw_command_end_label(); // Render Setup |
| 1022 | |
| 1023 | // setup environment |
| 1024 | RID radiance_texture; |
| 1025 | bool draw_sky = false; |
| 1026 | bool draw_sky_fog_only = false; |
| 1027 | // We invert luminance_multiplier for sky so that we can combine it with exposure value. |
| 1028 | float inverse_luminance_multiplier = 1.0 / rb->get_luminance_multiplier(); |
| 1029 | float sky_luminance_multiplier = 1.0 / 2.0; // Hardcoded since sky always uses LDR in the mobile renderer |
| 1030 | float sky_brightness_multiplier = 1.0; |
| 1031 | |
| 1032 | Color clear_color = p_default_bg_color; |
| 1033 | bool load_color = false; |
| 1034 | bool copy_canvas = false; |
| 1035 | bool use_ambient_cubemap = false; |
| 1036 | bool use_reflection_cubemap = false; |
| 1037 | |
| 1038 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { |
| 1039 | clear_color = Color(0, 0, 0, 1); //in overdraw mode, BG should always be black |
| 1040 | } else if (is_environment(p_render_data->environment)) { |
| 1041 | RS::EnvironmentAmbientSource ambient_source = environment_get_ambient_source(p_render_data->environment); |
| 1042 | RS::EnvironmentBG bg_mode = environment_get_background(p_render_data->environment); |
| 1043 | float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment); |
| 1044 | bg_energy_multiplier *= environment_get_bg_intensity(p_render_data->environment); |
| 1045 | RS::EnvironmentReflectionSource reflection_source = environment_get_reflection_source(p_render_data->environment); |
| 1046 | use_ambient_cubemap = (ambient_source == RS::ENV_AMBIENT_SOURCE_BG && bg_mode == RS::ENV_BG_SKY) || ambient_source == RS::ENV_AMBIENT_SOURCE_SKY; |
| 1047 | use_reflection_cubemap = (reflection_source == RS::ENV_REFLECTION_SOURCE_BG && bg_mode == RS::ENV_BG_SKY) || reflection_source == RS::ENV_REFLECTION_SOURCE_SKY; |
| 1048 | |
| 1049 | if (p_render_data->camera_attributes.is_valid()) { |
| 1050 | bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes); |
| 1051 | } |
| 1052 | |
| 1053 | switch (bg_mode) { |
| 1054 | case RS::ENV_BG_CLEAR_COLOR: { |
| 1055 | clear_color = p_default_bg_color; |
| 1056 | clear_color.r *= bg_energy_multiplier; |
| 1057 | clear_color.g *= bg_energy_multiplier; |
| 1058 | clear_color.b *= bg_energy_multiplier; |
| 1059 | if (!p_render_data->transparent_bg && environment_get_fog_enabled(p_render_data->environment)) { |
| 1060 | draw_sky_fog_only = true; |
| 1061 | RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear())); |
| 1062 | } |
| 1063 | } break; |
| 1064 | case RS::ENV_BG_COLOR: { |
| 1065 | clear_color = environment_get_bg_color(p_render_data->environment); |
| 1066 | clear_color.r *= bg_energy_multiplier; |
| 1067 | clear_color.g *= bg_energy_multiplier; |
| 1068 | clear_color.b *= bg_energy_multiplier; |
| 1069 | if (!p_render_data->transparent_bg && environment_get_fog_enabled(p_render_data->environment)) { |
| 1070 | draw_sky_fog_only = true; |
| 1071 | RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear())); |
| 1072 | } |
| 1073 | } break; |
| 1074 | case RS::ENV_BG_SKY: { |
| 1075 | draw_sky = !p_render_data->transparent_bg; |
| 1076 | } break; |
| 1077 | case RS::ENV_BG_CANVAS: { |
| 1078 | if (rb_data.is_valid()) { |
| 1079 | copy_canvas = true; |
| 1080 | } |
| 1081 | } break; |
| 1082 | case RS::ENV_BG_KEEP: { |
| 1083 | load_color = true; |
| 1084 | } break; |
| 1085 | case RS::ENV_BG_CAMERA_FEED: { |
| 1086 | } break; |
| 1087 | default: { |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | // setup sky if used for ambient, reflections, or background |
| 1092 | 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 || ambient_source == RS::ENV_AMBIENT_SOURCE_SKY) { |
| 1093 | RENDER_TIMESTAMP("Setup Sky"); |
| 1094 | RD::get_singleton()->draw_command_begin_label("Setup Sky"); |
| 1095 | |
| 1096 | sky.setup_sky(p_render_data, screen_size); |
| 1097 | |
| 1098 | sky_brightness_multiplier *= bg_energy_multiplier; |
| 1099 | |
| 1100 | RID sky_rid = environment_get_sky(p_render_data->environment); |
| 1101 | if (sky_rid.is_valid()) { |
| 1102 | sky.update_radiance_buffers(rb, p_render_data->environment, p_render_data->scene_data->cam_transform.origin, time, sky_luminance_multiplier, sky_brightness_multiplier); |
| 1103 | radiance_texture = sky.sky_get_radiance_texture_rd(sky_rid); |
| 1104 | } else { |
| 1105 | // do not try to draw sky if invalid |
| 1106 | draw_sky = false; |
| 1107 | } |
| 1108 | |
| 1109 | if (draw_sky || draw_sky_fog_only) { |
| 1110 | // update sky half/quarter res buffers (if required) |
| 1111 | sky.update_res_buffers(rb, p_render_data->environment, time, sky_luminance_multiplier, sky_brightness_multiplier); |
| 1112 | } |
| 1113 | RD::get_singleton()->draw_command_end_label(); // Setup Sky |
| 1114 | } |
| 1115 | } else { |
| 1116 | clear_color = p_default_bg_color; |
| 1117 | } |
| 1118 | |
| 1119 | // We don't have access to any rendered buffers but we may be able to effect mesh data... |
| 1120 | _process_compositor_effects(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_OPAQUE, p_render_data); |
| 1121 | |
| 1122 | _pre_opaque_render(p_render_data); |
| 1123 | |
| 1124 | SceneShaderForwardMobile::ShaderSpecialization base_specialization = scene_shader.default_specialization; |
| 1125 | |
| 1126 | { |
| 1127 | base_specialization.use_directional_soft_shadows = p_render_data->directional_light_count > 0 ? p_render_data->directional_light_soft_shadows : false; |
| 1128 | base_specialization.directional_lights = SceneShaderForwardMobile::shader_count_for(p_render_data->directional_light_count); |
| 1129 | base_specialization.directional_light_blend_splits = light_storage->get_directional_light_blend_splits(p_render_data->directional_light_count); |
| 1130 | |
| 1131 | if (!is_environment(p_render_data->environment) || !environment_get_fog_enabled(p_render_data->environment)) { |
| 1132 | base_specialization.disable_fog = true; |
| 1133 | base_specialization.use_fog_aerial_perspective = false; |
| 1134 | base_specialization.use_fog_sun_scatter = false; |
| 1135 | base_specialization.use_fog_height_density = false; |
| 1136 | base_specialization.use_depth_fog = false; |
| 1137 | } else { |
| 1138 | base_specialization.disable_fog = false; |
| 1139 | base_specialization.use_fog_aerial_perspective = environment_get_fog_aerial_perspective(p_render_data->environment) > 0.0; |
| 1140 | base_specialization.use_fog_sun_scatter = environment_get_fog_sun_scatter(p_render_data->environment) > 0.001; |
| 1141 | base_specialization.use_fog_height_density = std::abs(environment_get_fog_height_density(p_render_data->environment)) >= 0.0001; |
| 1142 | 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; |
| 1143 | } |
| 1144 | |
| 1145 | base_specialization.scene_use_ambient_cubemap = use_ambient_cubemap; |
| 1146 | base_specialization.scene_use_reflection_cubemap = use_reflection_cubemap; |
| 1147 | base_specialization.scene_roughness_limiter_enabled = p_render_data->render_buffers.is_valid() && screen_space_roughness_limiter_is_active(); |
| 1148 | base_specialization.luminance_multiplier = p_render_data->render_buffers.is_valid() ? p_render_data->render_buffers->get_luminance_multiplier() : 1.0; |
| 1149 | } |
| 1150 | |
| 1151 | { |
| 1152 | RDD::BreadcrumbMarker breadcrumb; |
| 1153 | if (rb_data.is_valid()) { |
| 1154 | RD::get_singleton()->draw_command_begin_label("Render 3D Pass"); |
| 1155 | breadcrumb = RDD::BreadcrumbMarker::OPAQUE_PASS; |
| 1156 | } else { |
| 1157 | RD::get_singleton()->draw_command_begin_label("Render Reflection Probe Pass"); |
| 1158 | breadcrumb = RDD::BreadcrumbMarker::REFLECTION_PROBES; |
| 1159 | } |
| 1160 | |
| 1161 | if (rb_data.is_valid() && p_render_data->scene_data->calculate_motion_vectors) { |
| 1162 | RID mv_fb = rb_data->get_motion_vectors_fb(); |
| 1163 | |
| 1164 | if (mv_fb.is_valid()) { |
| 1165 | RENDER_TIMESTAMP("Render Motion Vectors"); |
| 1166 | |
| 1167 | Vector<Color> mv_pass_clear; |
| 1168 | mv_pass_clear.push_back(Color(0, 0, 0, 0)); |
| 1169 | |
| 1170 | RD::get_singleton()->draw_command_begin_label("Render Motion Vectors"); |
| 1171 | |
| 1172 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_OPAQUE, nullptr, RID(), samplers); |
| 1173 | 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_MOTION_VECTORS, rp_uniform_set, base_specialization); |
| 1174 | _render_list_with_draw_list(&render_list_params, mv_fb, RD::DRAW_CLEAR_ALL, mv_pass_clear); |
| 1175 | |
| 1176 | RD::get_singleton()->draw_command_end_label(); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | // opaque pass |
| 1181 | RD::get_singleton()->draw_command_begin_label("Render Opaque"); |
| 1182 | |
| 1183 | p_render_data->scene_data->directional_light_count = p_render_data->directional_light_count; |
| 1184 | |
| 1185 | // Shadow pass can change the base uniform set samplers. |
| 1186 | _update_render_base_uniform_set(); |
| 1187 | |
| 1188 | _setup_environment(p_render_data, is_reflection_probe, screen_size, screen_size, p_default_bg_color, p_render_data->render_buffers.is_valid()); |
| 1189 | |
| 1190 | if (merge_transparent_pass && using_subpass_post_process) { |
| 1191 | RENDER_TIMESTAMP("Render Opaque + Transparent + Tonemap"); |
| 1192 | } else if (merge_transparent_pass) { |
| 1193 | RENDER_TIMESTAMP("Render Opaque + Transparent"); |
| 1194 | } else { |
| 1195 | RENDER_TIMESTAMP("Render Opaque"); |
| 1196 | } |
| 1197 | |
| 1198 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_OPAQUE, p_render_data, radiance_texture, samplers, true); |
| 1199 | |
| 1200 | // Set clear colors. |
| 1201 | Vector<Color> c; |
| 1202 | if (!load_color) { |
| 1203 | Color cc = clear_color.srgb_to_linear() * inverse_luminance_multiplier; |
| 1204 | if (rb_data.is_valid()) { |
| 1205 | cc.a = 0; // For transparent viewport backgrounds. |
| 1206 | } |
| 1207 | |
| 1208 | c.push_back(cc); // Our render buffer. |
| 1209 | if (rb_data.is_valid()) { |
| 1210 | if (use_msaa) { |
| 1211 | c.push_back(clear_color.srgb_to_linear() * inverse_luminance_multiplier); // Our resolve buffer. |
| 1212 | } |
| 1213 | if (using_subpass_post_process) { |
| 1214 | c.push_back(Color()); // Our 2D buffer we're copying into. |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, load_color ? RD::DRAW_CLEAR_DEPTH : (RD::DRAW_CLEAR_COLOR_0 | RD::DRAW_CLEAR_DEPTH), c, 0.0f, 0, p_render_data->render_region, breadcrumb); |
| 1220 | RD::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(framebuffer); |
| 1221 | |
| 1222 | if (copy_canvas) { |
| 1223 | if (p_render_data->scene_data->view_count > 1) { |
| 1224 | WARN_PRINT_ONCE("Canvas background is not supported in multiview!"); |
| 1225 | } else { |
| 1226 | RID texture = RendererRD::TextureStorage::get_singleton()->render_target_get_rd_texture(rb->get_render_target()); |
| 1227 | bool convert_to_linear = !hdr_render_target; |
| 1228 | |
| 1229 | copy_effects->copy_to_drawlist(draw_list, fb_format, texture, convert_to_linear, 2.0f); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | if (render_list[RENDER_LIST_OPAQUE].elements.size() > 0) { |
| 1234 | 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, rp_uniform_set, base_specialization, 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); |
| 1235 | render_list_params.framebuffer_format = fb_format; |
| 1236 | render_list_params.subpass = RD::get_singleton()->draw_list_get_current_pass(); // Should now always be 0. |
| 1237 | |
| 1238 | _render_list(draw_list, fb_format, &render_list_params, 0, render_list_params.element_count); |
| 1239 | } |
| 1240 | |
| 1241 | RD::get_singleton()->draw_command_end_label(); //Render Opaque |
| 1242 | |
| 1243 | if (draw_sky || draw_sky_fog_only) { |
| 1244 | RD::get_singleton()->draw_command_begin_label("Draw Sky"); |
| 1245 | |
| 1246 | // Note, sky.setup should have been called up above and setup stuff we need. |
| 1247 | |
| 1248 | sky.draw_sky(draw_list, rb, p_render_data->environment, framebuffer, time, inverse_luminance_multiplier, sky_brightness_multiplier); |
| 1249 | |
| 1250 | RD::get_singleton()->draw_command_end_label(); // Draw Sky |
| 1251 | } |
| 1252 | |
| 1253 | if (merge_transparent_pass) { |
| 1254 | if (render_list[RENDER_LIST_ALPHA].element_info.size() > 0) { |
| 1255 | // transparent pass |
| 1256 | |
| 1257 | RD::get_singleton()->draw_command_begin_label("Render Transparent"); |
| 1258 | |
| 1259 | rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_ALPHA, p_render_data, radiance_texture, samplers, true); |
| 1260 | |
| 1261 | 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, rp_uniform_set, base_specialization, 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); |
| 1262 | render_list_params.framebuffer_format = fb_format; |
| 1263 | render_list_params.subpass = RD::get_singleton()->draw_list_get_current_pass(); // Should now always be 0. |
| 1264 | |
| 1265 | _render_list(draw_list, fb_format, &render_list_params, 0, render_list_params.element_count); |
| 1266 | |
| 1267 | RD::get_singleton()->draw_command_end_label(); // Render Transparent Subpass |
| 1268 | } |
| 1269 | |
| 1270 | // blit to tonemap |
| 1271 | if (rb_data.is_valid() && using_subpass_post_process) { |
| 1272 | _post_process_subpass(p_render_data->render_buffers->get_internal_texture(), framebuffer, p_render_data); |
| 1273 | } |
| 1274 | |
| 1275 | RD::get_singleton()->draw_command_end_label(); // Render 3D Pass / Render Reflection Probe Pass |
| 1276 | |
| 1277 | RD::get_singleton()->draw_list_end(); |
| 1278 | |
| 1279 | // note, if MSAA is used we should get an automatic resolve of the color buffer here. |
| 1280 | |
| 1281 | if (use_msaa && has_depth_texture_override && !supports_depth_resolve) { |
| 1282 | // We don't have a fallback for this, See PR #111322 |
| 1283 | WARN_PRINT_ONCE("MSAA Depth buffer resolve is not supported on this platform."); |
| 1284 | } |
| 1285 | } else { |
| 1286 | // We're done with our subpasses so end our container pass |
| 1287 | // note, if MSAA is used we should get an automatic resolve of the color buffer here. |
| 1288 | |
| 1289 | RD::get_singleton()->draw_list_end(); |
| 1290 | |
| 1291 | RD::get_singleton()->draw_command_end_label(); // Render 3D Pass / Render Reflection Probe Pass |
| 1292 | |
| 1293 | // rendering effects |
| 1294 | if (ce_has_pre_transparent) { |
| 1295 | _process_compositor_effects(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_PRE_TRANSPARENT, p_render_data); |
| 1296 | } |
| 1297 | |
| 1298 | if (scene_state.used_screen_texture || global_surface_data.screen_texture_used) { |
| 1299 | _render_buffers_ensure_screen_texture(p_render_data); |
| 1300 | |
| 1301 | if (scene_state.used_screen_texture) { |
| 1302 | // Copy screen texture to backbuffer so we can read from it |
| 1303 | _render_buffers_copy_screen_texture(p_render_data); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | if (use_msaa && has_depth_texture_override && !supports_depth_resolve) { |
| 1308 | // We don't have a fallback for this, See PR #111322 |
| 1309 | WARN_PRINT_ONCE("MSAA Depth buffer resolve is not supported on this platform."); |
| 1310 | } |
| 1311 | |
| 1312 | if (scene_state.used_depth_texture || global_surface_data.depth_texture_used) { |
| 1313 | _render_buffers_ensure_depth_texture(p_render_data); |
| 1314 | |
| 1315 | if (scene_state.used_depth_texture) { |
| 1316 | // Copy depth texture to backbuffer so we can read from it. |
| 1317 | _render_buffers_copy_depth_texture(p_render_data, use_msaa && !supports_depth_resolve); // Note, once fallback for has_depth_texture_override works, we also don't need to do our resolve here. |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | if (render_list[RENDER_LIST_ALPHA].element_info.size() > 0) { |
| 1322 | RD::get_singleton()->draw_command_begin_label("Render Transparent Pass"); |
| 1323 | RENDER_TIMESTAMP("Render Transparent"); |
| 1324 | |
| 1325 | rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_ALPHA, p_render_data, radiance_texture, samplers, true); |
| 1326 | |
| 1327 | // this may be needed if we re-introduced steps that change info, not sure which do so in the previous implementation |
| 1328 | //_setup_environment(p_render_data, is_reflection_probe, screen_size, screen_size, p_default_bg_color, false); |
| 1329 | |
| 1330 | 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, rp_uniform_set, base_specialization, 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); |
| 1331 | render_list_params.framebuffer_format = fb_format; |
| 1332 | render_list_params.subpass = RD::get_singleton()->draw_list_get_current_pass(); // Should now always be 0. |
| 1333 | |
| 1334 | draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, p_render_data->render_region, breadcrumb); |
| 1335 | _render_list(draw_list, fb_format, &render_list_params, 0, render_list_params.element_count); |
| 1336 | RD::get_singleton()->draw_list_end(); |
| 1337 | |
| 1338 | RD::get_singleton()->draw_command_end_label(); // Render Transparent Pass |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | if (rb_data.is_valid() && !using_subpass_post_process) { |
| 1344 | RD::get_singleton()->draw_command_begin_label("Post Process Pass"); |
| 1345 | |
| 1346 | if (ce_has_post_transparent) { |
| 1347 | _process_compositor_effects(RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_POST_TRANSPARENT, p_render_data); |
| 1348 | } |
| 1349 | |
| 1350 | // If we need extra effects we do this in its own pass |
| 1351 | RENDER_TIMESTAMP("Tonemap"); |
| 1352 | |
| 1353 | _render_buffers_post_process_and_tonemap(p_render_data, use_msaa); |
| 1354 | |
| 1355 | RD::get_singleton()->draw_command_end_label(); // Post process pass |
| 1356 | } |
| 1357 | |
| 1358 | if (rb_data.is_valid()) { |
| 1359 | _disable_clear_request(p_render_data); |
| 1360 | } |
| 1361 | |
| 1362 | _render_buffers_debug_draw(p_render_data); |
| 1363 | } |
| 1364 | |
| 1365 | /* these are being called from RendererSceneRenderRD::_pre_opaque_render */ |
| 1366 | |
| 1367 | void RenderForwardMobile::_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 Transform3D &p_main_cam_transform) { |
| 1368 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 1369 | |
| 1370 | ERR_FAIL_COND(!light_storage->owns_light_instance(p_light)); |
| 1371 | |
| 1372 | RID base = light_storage->light_instance_get_base_light(p_light); |
| 1373 | |
| 1374 | Rect2i atlas_rect; |
| 1375 | uint32_t atlas_size = 1; |
| 1376 | RID atlas_fb; |
| 1377 | |
| 1378 | bool using_dual_paraboloid = false; |
| 1379 | bool using_dual_paraboloid_flip = false; |
| 1380 | Vector2i dual_paraboloid_offset; |
| 1381 | RID render_fb; |
| 1382 | RID render_texture; |
| 1383 | float zfar; |
| 1384 | |
| 1385 | bool use_pancake = false; |
| 1386 | bool render_cubemap = false; |
| 1387 | bool finalize_cubemap = false; |
| 1388 | |
| 1389 | bool flip_y = false; |
| 1390 | |
| 1391 | Projection light_projection; |
| 1392 | Transform3D light_transform; |
| 1393 | |
| 1394 | if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) { |
| 1395 | //set pssm stuff |
| 1396 | uint64_t last_scene_shadow_pass = light_storage->light_instance_get_shadow_pass(p_light); |
| 1397 | if (last_scene_shadow_pass != get_scene_pass()) { |
| 1398 | light_storage->light_instance_set_directional_rect(p_light, light_storage->get_directional_shadow_rect()); |
| 1399 | light_storage->directional_shadow_increase_current_light(); |
| 1400 | light_storage->light_instance_set_shadow_pass(p_light, get_scene_pass()); |
| 1401 | } |
| 1402 | |
| 1403 | use_pancake = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE) > 0; |
| 1404 | light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass); |
| 1405 | light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass); |
| 1406 | |
| 1407 | atlas_rect = light_storage->light_instance_get_directional_rect(p_light); |
| 1408 | |
| 1409 | if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) { |
| 1410 | atlas_rect.size.width /= 2; |
| 1411 | atlas_rect.size.height /= 2; |
| 1412 | |
| 1413 | if (p_pass == 1) { |
| 1414 | atlas_rect.position.x += atlas_rect.size.width; |
| 1415 | } else if (p_pass == 2) { |
| 1416 | atlas_rect.position.y += atlas_rect.size.height; |
| 1417 | } else if (p_pass == 3) { |
| 1418 | atlas_rect.position += atlas_rect.size; |
| 1419 | } |
| 1420 | } else if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) { |
| 1421 | atlas_rect.size.height /= 2; |
| 1422 | |
| 1423 | if (p_pass == 0) { |
| 1424 | } else { |
| 1425 | atlas_rect.position.y += atlas_rect.size.height; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | float directional_shadow_size = light_storage->directional_shadow_get_size(); |
| 1430 | Rect2 atlas_rect_norm = atlas_rect; |
| 1431 | atlas_rect_norm.position /= directional_shadow_size; |
| 1432 | atlas_rect_norm.size /= directional_shadow_size; |
| 1433 | light_storage->light_instance_set_directional_shadow_atlas_rect(p_light, p_pass, atlas_rect_norm); |
| 1434 | |
| 1435 | zfar = RSG::light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE); |
| 1436 | |
| 1437 | render_fb = light_storage->direction_shadow_get_fb(); |
| 1438 | render_texture = RID(); |
| 1439 | flip_y = true; |
| 1440 | |
| 1441 | } else { |
| 1442 | //set from shadow atlas |
| 1443 | |
| 1444 | ERR_FAIL_COND(!light_storage->owns_shadow_atlas(p_shadow_atlas)); |
| 1445 | ERR_FAIL_COND(!light_storage->shadow_atlas_owns_light_instance(p_shadow_atlas, p_light)); |
| 1446 | |
| 1447 | RSG::light_storage->shadow_atlas_update(p_shadow_atlas); |
| 1448 | |
| 1449 | uint32_t key = light_storage->shadow_atlas_get_light_instance_key(p_shadow_atlas, p_light); |
| 1450 | |
| 1451 | uint32_t quadrant = (key >> RendererRD::LightStorage::QUADRANT_SHIFT) & 0x3; |
| 1452 | uint32_t shadow = key & RendererRD::LightStorage::SHADOW_INDEX_MASK; |
| 1453 | uint32_t subdivision = light_storage->shadow_atlas_get_quadrant_subdivision(p_shadow_atlas, quadrant); |
| 1454 | |
| 1455 | ERR_FAIL_INDEX((int)shadow, light_storage->shadow_atlas_get_quadrant_shadow_size(p_shadow_atlas, quadrant)); |
| 1456 | |
| 1457 | uint32_t shadow_atlas_size = light_storage->shadow_atlas_get_size(p_shadow_atlas); |
| 1458 | uint32_t quadrant_size = shadow_atlas_size >> 1; |
| 1459 | |
| 1460 | atlas_rect.position.x = (quadrant & 1) * quadrant_size; |
| 1461 | atlas_rect.position.y = (quadrant >> 1) * quadrant_size; |
| 1462 | |
| 1463 | uint32_t shadow_size = (quadrant_size / subdivision); |
| 1464 | atlas_rect.position.x += (shadow % subdivision) * shadow_size; |
| 1465 | atlas_rect.position.y += (shadow / subdivision) * shadow_size; |
| 1466 | |
| 1467 | atlas_rect.size.width = shadow_size; |
| 1468 | atlas_rect.size.height = shadow_size; |
| 1469 | |
| 1470 | zfar = light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE); |
| 1471 | |
| 1472 | if (light_storage->light_get_type(base) == RS::LIGHT_OMNI) { |
| 1473 | bool wrap = (shadow + 1) % subdivision == 0; |
| 1474 | dual_paraboloid_offset = wrap ? Vector2i(1 - subdivision, 1) : Vector2i(1, 0); |
| 1475 | |
| 1476 | if (light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) { |
| 1477 | render_texture = light_storage->get_cubemap(shadow_size / 2); |
| 1478 | render_fb = light_storage->get_cubemap_fb(shadow_size / 2, p_pass); |
| 1479 | |
| 1480 | light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass); |
| 1481 | light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass); |
| 1482 | render_cubemap = true; |
| 1483 | finalize_cubemap = p_pass == 5; |
| 1484 | atlas_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas); |
| 1485 | |
| 1486 | atlas_size = shadow_atlas_size; |
| 1487 | |
| 1488 | if (p_pass == 0) { |
| 1489 | _render_shadow_begin(); |
| 1490 | } |
| 1491 | |
| 1492 | } else { |
| 1493 | atlas_rect.position.x += 1; |
| 1494 | atlas_rect.position.y += 1; |
| 1495 | atlas_rect.size.x -= 2; |
| 1496 | atlas_rect.size.y -= 2; |
| 1497 | |
| 1498 | atlas_rect.position += p_pass * atlas_rect.size * dual_paraboloid_offset; |
| 1499 | |
| 1500 | light_projection = light_storage->light_instance_get_shadow_camera(p_light, 0); |
| 1501 | light_transform = light_storage->light_instance_get_shadow_transform(p_light, 0); |
| 1502 | |
| 1503 | using_dual_paraboloid = true; |
| 1504 | using_dual_paraboloid_flip = p_pass == 1; |
| 1505 | render_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas); |
| 1506 | flip_y = true; |
| 1507 | } |
| 1508 | |
| 1509 | } else if (light_storage->light_get_type(base) == RS::LIGHT_SPOT) { |
| 1510 | light_projection = light_storage->light_instance_get_shadow_camera(p_light, 0); |
| 1511 | light_transform = light_storage->light_instance_get_shadow_transform(p_light, 0); |
| 1512 | |
| 1513 | render_fb = light_storage->shadow_atlas_get_fb(p_shadow_atlas); |
| 1514 | |
| 1515 | flip_y = true; |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | if (render_cubemap) { |
| 1520 | //rendering to cubemap |
| 1521 | _render_shadow_append(render_fb, p_instances, light_projection, light_transform, zfar, 0, 0, false, false, use_pancake, p_lod_distance_multiplier, p_screen_mesh_lod_threshold, Rect2(), false, true, true, true, p_render_info, p_main_cam_transform); |
| 1522 | if (finalize_cubemap) { |
| 1523 | _render_shadow_process(); |
| 1524 | _render_shadow_end(); |
| 1525 | |
| 1526 | // reblit |
| 1527 | Rect2 atlas_rect_norm = atlas_rect; |
| 1528 | atlas_rect_norm.position /= float(atlas_size); |
| 1529 | atlas_rect_norm.size /= float(atlas_size); |
| 1530 | copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), zfar, false); |
| 1531 | atlas_rect_norm.position += Vector2(dual_paraboloid_offset) * atlas_rect_norm.size; |
| 1532 | copy_effects->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect_norm, atlas_rect.size, light_projection.get_z_near(), zfar, true); |
| 1533 | |
| 1534 | //restore transform so it can be properly used |
| 1535 | light_storage->light_instance_set_shadow_transform(p_light, Projection(), light_storage->light_instance_get_base_transform(p_light), zfar, 0, 0, 0); |
| 1536 | } |
| 1537 | |
| 1538 | } else { |
| 1539 | //render shadow |
| 1540 | _render_shadow_append(render_fb, p_instances, light_projection, light_transform, zfar, 0, 0, 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_main_cam_transform); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | void RenderForwardMobile::_render_shadow_begin() { |
| 1545 | scene_state.shadow_passes.clear(); |
| 1546 | RD::get_singleton()->draw_command_begin_label("Shadow Setup"); |
| 1547 | _update_render_base_uniform_set(); |
| 1548 | |
| 1549 | render_list[RENDER_LIST_SECONDARY].clear(); |
| 1550 | } |
| 1551 | |
| 1552 | void RenderForwardMobile::_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_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 Transform3D &p_main_cam_transform) { |
| 1553 | SceneState::ShadowPass shadow_pass; |
| 1554 | |
| 1555 | if (p_render_info) { |
| 1556 | p_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME] = p_instances.size(); |
| 1557 | p_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] = p_instances.size(); |
| 1558 | } |
| 1559 | |
| 1560 | RenderSceneDataRD scene_data; |
| 1561 | scene_data.flip_y = !p_flip_y; // Q: Why is this inverted? Do we assume flip in shadow logic? |
| 1562 | scene_data.cam_projection = p_projection; |
| 1563 | scene_data.cam_transform = p_transform; |
| 1564 | scene_data.view_projection[0] = p_projection; |
| 1565 | scene_data.z_near = 0.0; |
| 1566 | scene_data.z_far = p_zfar; |
| 1567 | scene_data.lod_distance_multiplier = p_lod_distance_multiplier; |
| 1568 | scene_data.dual_paraboloid_side = p_use_dp_flip ? -1 : 1; |
| 1569 | scene_data.opaque_prepass_threshold = 0.1; |
| 1570 | scene_data.time = time; |
| 1571 | scene_data.time_step = time_step; |
| 1572 | scene_data.main_cam_transform = p_main_cam_transform; |
| 1573 | |
| 1574 | RenderDataRD render_data; |
| 1575 | render_data.scene_data = &scene_data; |
| 1576 | render_data.instances = &p_instances; |
| 1577 | render_data.render_info = p_render_info; |
| 1578 | |
| 1579 | Size2i screen_size = RD::get_singleton()->framebuffer_get_size(p_framebuffer); |
| 1580 | Size2i viewport_size = p_rect.size; |
| 1581 | if (viewport_size == Size2()) { |
| 1582 | viewport_size = screen_size; |
| 1583 | } |
| 1584 | _setup_environment(&render_data, true, screen_size, viewport_size, Color(), false, p_use_pancake); |
| 1585 | |
| 1586 | if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) { |
| 1587 | scene_data.screen_mesh_lod_threshold = 0.0; |
| 1588 | } else { |
| 1589 | scene_data.screen_mesh_lod_threshold = p_screen_mesh_lod_threshold; |
| 1590 | } |
| 1591 | |
| 1592 | PassMode pass_mode = p_use_dp ? PASS_MODE_SHADOW_DP : PASS_MODE_SHADOW; |
| 1593 | |
| 1594 | uint32_t render_list_from = render_list[RENDER_LIST_SECONDARY].elements.size(); |
| 1595 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode, true); |
| 1596 | uint32_t render_list_size = render_list[RENDER_LIST_SECONDARY].elements.size() - render_list_from; |
| 1597 | render_list[RENDER_LIST_SECONDARY].sort_by_key_range(render_list_from, render_list_size); |
| 1598 | _fill_instance_data(RENDER_LIST_SECONDARY, render_list_from, render_list_size, false); |
| 1599 | |
| 1600 | { |
| 1601 | //regular forward for now |
| 1602 | bool flip_cull = p_use_dp_flip; |
| 1603 | if (p_flip_y) { |
| 1604 | flip_cull = !flip_cull; |
| 1605 | } |
| 1606 | |
| 1607 | shadow_pass.element_from = render_list_from; |
| 1608 | shadow_pass.element_count = render_list_size; |
| 1609 | shadow_pass.flip_cull = flip_cull; |
| 1610 | shadow_pass.pass_mode = pass_mode; |
| 1611 | |
| 1612 | shadow_pass.rp_uniform_set = RID(); //will be filled later when instance buffer is complete |
| 1613 | shadow_pass.screen_mesh_lod_threshold = scene_data.screen_mesh_lod_threshold; |
| 1614 | shadow_pass.lod_distance_multiplier = scene_data.lod_distance_multiplier; |
| 1615 | |
| 1616 | shadow_pass.framebuffer = p_framebuffer; |
| 1617 | shadow_pass.clear_depth = p_begin || p_clear_region; |
| 1618 | shadow_pass.rect = p_rect; |
| 1619 | |
| 1620 | scene_state.shadow_passes.push_back(shadow_pass); |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | void RenderForwardMobile::_render_shadow_process() { |
| 1625 | RenderingDevice *rd = RenderingDevice::get_singleton(); |
| 1626 | if (scene_state.instance_buffer[RENDER_LIST_SECONDARY].get_size(0u) > 0u) { |
| 1627 | rd->buffer_flush(scene_state.instance_buffer[RENDER_LIST_SECONDARY]._get(0u)); |
| 1628 | } |
| 1629 | |
| 1630 | //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) |
| 1631 | |
| 1632 | for (uint32_t i = 0; i < scene_state.shadow_passes.size(); i++) { |
| 1633 | //render passes need to be configured after instance buffer is done, since they need the latest version |
| 1634 | SceneState::ShadowPass &shadow_pass = scene_state.shadow_passes[i]; |
| 1635 | shadow_pass.rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default(), false, scene_state.shadow_passes.size() - 1u - i); |
| 1636 | } |
| 1637 | |
| 1638 | RD::get_singleton()->draw_command_end_label(); |
| 1639 | } |
| 1640 | |
| 1641 | void RenderForwardMobile::_render_shadow_end() { |
| 1642 | RD::get_singleton()->draw_command_begin_label("Shadow Render"); |
| 1643 | |
| 1644 | for (SceneState::ShadowPass &shadow_pass : scene_state.shadow_passes) { |
| 1645 | 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, shadow_pass.rp_uniform_set, scene_shader.default_specialization, false, Vector2(), shadow_pass.lod_distance_multiplier, shadow_pass.screen_mesh_lod_threshold, 1, shadow_pass.element_from); |
| 1646 | _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); |
| 1647 | } |
| 1648 | |
| 1649 | RD::get_singleton()->draw_command_end_label(); |
| 1650 | } |
| 1651 | |
| 1652 | /* */ |
| 1653 | |
| 1654 | void RenderForwardMobile::_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) { |
| 1655 | RENDER_TIMESTAMP("Setup Rendering 3D Material"); |
| 1656 | |
| 1657 | RD::get_singleton()->draw_command_begin_label("Render 3D Material"); |
| 1658 | |
| 1659 | _update_render_base_uniform_set(); |
| 1660 | |
| 1661 | RenderSceneDataRD scene_data; |
| 1662 | scene_data.cam_projection = p_cam_projection; |
| 1663 | scene_data.cam_transform = p_cam_transform; |
| 1664 | scene_data.view_projection[0] = p_cam_projection; |
| 1665 | scene_data.dual_paraboloid_side = 0; |
| 1666 | scene_data.material_uv2_mode = false; |
| 1667 | scene_data.opaque_prepass_threshold = 0.0f; |
| 1668 | scene_data.emissive_exposure_normalization = p_exposure_normalization; |
| 1669 | scene_data.time = time; |
| 1670 | scene_data.time_step = time_step; |
| 1671 | scene_data.main_cam_transform = p_cam_transform; |
| 1672 | |
| 1673 | RenderDataRD render_data; |
| 1674 | render_data.scene_data = &scene_data; |
| 1675 | render_data.instances = &p_instances; |
| 1676 | |
| 1677 | Size2i screen_size = RD::get_singleton()->framebuffer_get_size(p_framebuffer); |
| 1678 | Size2i viewport_size = p_region.size; |
| 1679 | if (viewport_size == Size2()) { |
| 1680 | viewport_size = screen_size; |
| 1681 | } |
| 1682 | _setup_environment(&render_data, true, screen_size, viewport_size, Color()); |
| 1683 | |
| 1684 | PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL; |
| 1685 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode); |
| 1686 | render_list[RENDER_LIST_SECONDARY].sort_by_key(); |
| 1687 | _fill_instance_data(RENDER_LIST_SECONDARY); |
| 1688 | |
| 1689 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default()); |
| 1690 | |
| 1691 | RENDER_TIMESTAMP("Render 3D Material"); |
| 1692 | |
| 1693 | { |
| 1694 | 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, rp_uniform_set, scene_shader.default_specialization); |
| 1695 | //regular forward for now |
| 1696 | Vector<Color> clear = { |
| 1697 | Color(0, 0, 0, 0), |
| 1698 | Color(0, 0, 0, 0), |
| 1699 | Color(0, 0, 0, 0), |
| 1700 | Color(0, 0, 0, 0), |
| 1701 | Color(0, 0, 0, 0) |
| 1702 | }; |
| 1703 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer, RD::DRAW_CLEAR_ALL, clear, 0.0f, 0, p_region); |
| 1704 | _render_list(draw_list, RD::get_singleton()->framebuffer_get_format(p_framebuffer), &render_list_params, 0, render_list_params.element_count); |
| 1705 | RD::get_singleton()->draw_list_end(); |
| 1706 | } |
| 1707 | |
| 1708 | RD::get_singleton()->draw_command_end_label(); |
| 1709 | } |
| 1710 | |
| 1711 | void RenderForwardMobile::_render_uv2(const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) { |
| 1712 | RENDER_TIMESTAMP("Setup Rendering UV2"); |
| 1713 | |
| 1714 | RD::get_singleton()->draw_command_begin_label("Render UV2"); |
| 1715 | |
| 1716 | _update_render_base_uniform_set(); |
| 1717 | |
| 1718 | RenderSceneDataRD scene_data; |
| 1719 | scene_data.dual_paraboloid_side = 0; |
| 1720 | scene_data.material_uv2_mode = true; |
| 1721 | scene_data.emissive_exposure_normalization = -1.0; |
| 1722 | |
| 1723 | RenderDataRD render_data; |
| 1724 | render_data.scene_data = &scene_data; |
| 1725 | render_data.instances = &p_instances; |
| 1726 | |
| 1727 | Size2i screen_size = RD::get_singleton()->framebuffer_get_size(p_framebuffer); |
| 1728 | Size2i viewport_size = p_region.size; |
| 1729 | if (viewport_size == Size2()) { |
| 1730 | viewport_size = screen_size; |
| 1731 | } |
| 1732 | _setup_environment(&render_data, true, screen_size, viewport_size, Color()); |
| 1733 | |
| 1734 | PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL; |
| 1735 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode); |
| 1736 | render_list[RENDER_LIST_SECONDARY].sort_by_key(); |
| 1737 | _fill_instance_data(RENDER_LIST_SECONDARY); |
| 1738 | |
| 1739 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default()); |
| 1740 | |
| 1741 | RENDER_TIMESTAMP("Render 3D Material"); |
| 1742 | |
| 1743 | { |
| 1744 | 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, rp_uniform_set, scene_shader.default_specialization, false); |
| 1745 | //regular forward for now |
| 1746 | Vector<Color> clear = { |
| 1747 | Color(0, 0, 0, 0), |
| 1748 | Color(0, 0, 0, 0), |
| 1749 | Color(0, 0, 0, 0), |
| 1750 | Color(0, 0, 0, 0), |
| 1751 | Color(0, 0, 0, 0) |
| 1752 | }; |
| 1753 | |
| 1754 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer, RD::DRAW_CLEAR_ALL, clear, 0.0f, 0, p_region); |
| 1755 | |
| 1756 | const int uv_offset_count = 9; |
| 1757 | static const Vector2 uv_offsets[uv_offset_count] = { |
| 1758 | Vector2(-1, 1), |
| 1759 | Vector2(1, 1), |
| 1760 | Vector2(1, -1), |
| 1761 | Vector2(-1, -1), |
| 1762 | Vector2(-1, 0), |
| 1763 | Vector2(1, 0), |
| 1764 | Vector2(0, -1), |
| 1765 | Vector2(0, 1), |
| 1766 | Vector2(0, 0), |
| 1767 | |
| 1768 | }; |
| 1769 | |
| 1770 | for (int i = 0; i < uv_offset_count; i++) { |
| 1771 | Vector2 ofs = uv_offsets[i]; |
| 1772 | ofs.x /= p_region.size.width; |
| 1773 | ofs.y /= p_region.size.height; |
| 1774 | render_list_params.uv_offset = ofs; |
| 1775 | _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 |
| 1776 | } |
| 1777 | render_list_params.uv_offset = Vector2(); |
| 1778 | _render_list(draw_list, RD::get_singleton()->framebuffer_get_format(p_framebuffer), &render_list_params, 0, render_list_params.element_count); //second regular triangles |
| 1779 | |
| 1780 | RD::get_singleton()->draw_list_end(); |
| 1781 | } |
| 1782 | |
| 1783 | RD::get_singleton()->draw_command_end_label(); |
| 1784 | } |
| 1785 | |
| 1786 | void RenderForwardMobile::_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) { |
| 1787 | // we don't do SDFGI in low end.. |
| 1788 | } |
| 1789 | |
| 1790 | void RenderForwardMobile::_render_particle_collider_heightfield(RID p_fb, const Transform3D &p_cam_transform, const Projection &p_cam_projection, const PagedArray<RenderGeometryInstance *> &p_instances) { |
| 1791 | RENDER_TIMESTAMP("Setup GPUParticlesCollisionHeightField3D"); |
| 1792 | |
| 1793 | RD::get_singleton()->draw_command_begin_label("Render Collider Heightfield"); |
| 1794 | |
| 1795 | _update_render_base_uniform_set(); |
| 1796 | |
| 1797 | RenderSceneDataRD scene_data; |
| 1798 | scene_data.flip_y = true; |
| 1799 | scene_data.cam_projection = p_cam_projection; |
| 1800 | scene_data.cam_transform = p_cam_transform; |
| 1801 | scene_data.view_projection[0] = p_cam_projection; |
| 1802 | scene_data.z_near = 0.0; |
| 1803 | scene_data.z_far = p_cam_projection.get_z_far(); |
| 1804 | scene_data.dual_paraboloid_side = 0; |
| 1805 | scene_data.opaque_prepass_threshold = 0.0; |
| 1806 | scene_data.time = time; |
| 1807 | scene_data.time_step = time_step; |
| 1808 | scene_data.main_cam_transform = p_cam_transform; |
| 1809 | |
| 1810 | RenderDataRD render_data; |
| 1811 | render_data.scene_data = &scene_data; |
| 1812 | render_data.instances = &p_instances; |
| 1813 | |
| 1814 | Size2i screen_size = RD::get_singleton()->framebuffer_get_size(p_fb); |
| 1815 | _setup_environment(&render_data, true, screen_size, screen_size, Color(), false, false); |
| 1816 | |
| 1817 | PassMode pass_mode = PASS_MODE_SHADOW; |
| 1818 | |
| 1819 | _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode); |
| 1820 | render_list[RENDER_LIST_SECONDARY].sort_by_key(); |
| 1821 | _fill_instance_data(RENDER_LIST_SECONDARY); |
| 1822 | |
| 1823 | RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_SECONDARY, nullptr, RID(), RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default()); |
| 1824 | |
| 1825 | RENDER_TIMESTAMP("Render Collider Heightfield"); |
| 1826 | |
| 1827 | { |
| 1828 | //regular forward for now |
| 1829 | 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, rp_uniform_set, scene_shader.default_specialization); |
| 1830 | _render_list_with_draw_list(&render_list_params, p_fb); |
| 1831 | } |
| 1832 | RD::get_singleton()->draw_command_end_label(); |
| 1833 | } |
| 1834 | |
| 1835 | void RenderForwardMobile::base_uniforms_changed() { |
| 1836 | if (!render_base_uniform_set.is_null() && RD::get_singleton()->uniform_set_is_valid(render_base_uniform_set)) { |
| 1837 | RD::get_singleton()->free_rid(render_base_uniform_set); |
| 1838 | } |
| 1839 | render_base_uniform_set = RID(); |
| 1840 | } |
| 1841 | |
| 1842 | void RenderForwardMobile::_update_render_base_uniform_set() { |
| 1843 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 1844 | |
| 1845 | 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())) { |
| 1846 | if (render_base_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(render_base_uniform_set)) { |
| 1847 | RD::get_singleton()->free_rid(render_base_uniform_set); |
| 1848 | } |
| 1849 | |
| 1850 | lightmap_texture_array_version = light_storage->lightmap_array_get_version(); |
| 1851 | |
| 1852 | thread_local LocalVector<RD::Uniform> uniforms; |
| 1853 | uniforms.clear(); |
| 1854 | |
| 1855 | { |
| 1856 | RD::Uniform u; |
| 1857 | u.binding = 2; |
| 1858 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 1859 | u.append_id(scene_shader.shadow_sampler); |
| 1860 | u.immutable_sampler = true; |
| 1861 | uniforms.push_back(u); |
| 1862 | } |
| 1863 | |
| 1864 | { |
| 1865 | RD::Uniform u; |
| 1866 | u.binding = 3; |
| 1867 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1868 | u.append_id(RendererRD::LightStorage::get_singleton()->get_omni_light_buffer()); |
| 1869 | uniforms.push_back(u); |
| 1870 | } |
| 1871 | { |
| 1872 | RD::Uniform u; |
| 1873 | u.binding = 4; |
| 1874 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1875 | u.append_id(RendererRD::LightStorage::get_singleton()->get_spot_light_buffer()); |
| 1876 | uniforms.push_back(u); |
| 1877 | } |
| 1878 | |
| 1879 | { |
| 1880 | RD::Uniform u; |
| 1881 | u.binding = 5; |
| 1882 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1883 | u.append_id(RendererRD::LightStorage::get_singleton()->get_reflection_probe_buffer()); |
| 1884 | uniforms.push_back(u); |
| 1885 | } |
| 1886 | { |
| 1887 | RD::Uniform u; |
| 1888 | u.binding = 6; |
| 1889 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 1890 | u.append_id(RendererRD::LightStorage::get_singleton()->get_directional_light_buffer()); |
| 1891 | uniforms.push_back(u); |
| 1892 | } |
| 1893 | { |
| 1894 | RD::Uniform u; |
| 1895 | u.binding = 7; |
| 1896 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1897 | u.append_id(scene_state.lightmap_buffer); |
| 1898 | uniforms.push_back(u); |
| 1899 | } |
| 1900 | { |
| 1901 | RD::Uniform u; |
| 1902 | u.binding = 8; |
| 1903 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1904 | u.append_id(scene_state.lightmap_capture_buffer); |
| 1905 | uniforms.push_back(u); |
| 1906 | } |
| 1907 | { |
| 1908 | RD::Uniform u; |
| 1909 | u.binding = 9; |
| 1910 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 1911 | RID decal_atlas = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture(); |
| 1912 | u.append_id(decal_atlas); |
| 1913 | uniforms.push_back(u); |
| 1914 | } |
| 1915 | { |
| 1916 | RD::Uniform u; |
| 1917 | u.binding = 10; |
| 1918 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 1919 | RID decal_atlas = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture_srgb(); |
| 1920 | u.append_id(decal_atlas); |
| 1921 | uniforms.push_back(u); |
| 1922 | } |
| 1923 | { |
| 1924 | RD::Uniform u; |
| 1925 | u.binding = 11; |
| 1926 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1927 | u.append_id(RendererRD::TextureStorage::get_singleton()->get_decal_buffer()); |
| 1928 | uniforms.push_back(u); |
| 1929 | } |
| 1930 | |
| 1931 | { |
| 1932 | RD::Uniform u; |
| 1933 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1934 | u.binding = 12; |
| 1935 | u.append_id(RendererRD::MaterialStorage::get_singleton()->global_shader_uniforms_get_storage_buffer()); |
| 1936 | uniforms.push_back(u); |
| 1937 | } |
| 1938 | |
| 1939 | { |
| 1940 | RD::Uniform u; |
| 1941 | u.binding = 13; |
| 1942 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 1943 | 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)); |
| 1944 | uniforms.push_back(u); |
| 1945 | } |
| 1946 | |
| 1947 | render_base_uniform_set = UniformSetCacheRD::get_singleton()->get_cache_vec(scene_shader.default_shader_rd, SCENE_UNIFORM_SET, uniforms); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | RID RenderForwardMobile::_render_buffers_get_normal_texture(Ref<RenderSceneBuffersRD> p_render_buffers) { |
| 1952 | return RID(); |
| 1953 | } |
| 1954 | |
| 1955 | RID RenderForwardMobile::_render_buffers_get_velocity_texture(Ref<RenderSceneBuffersRD> p_render_buffers) { |
| 1956 | return RID(); |
| 1957 | } |
| 1958 | |
| 1959 | void RenderForwardMobile::SceneState::grow_instance_buffer(RenderListType p_render_list, uint32_t p_req_element_count, bool p_append) { |
| 1960 | if (p_req_element_count > 0) { |
| 1961 | if (instance_buffer[p_render_list].get_size(0u) < p_req_element_count * sizeof(SceneState::InstanceData)) { |
| 1962 | instance_buffer[p_render_list].uninit(); |
| 1963 | uint32_t new_size = nearest_power_of_2_templated(MAX(uint64_t(INSTANCE_DATA_BUFFER_MIN_SIZE), p_req_element_count)); |
| 1964 | instance_buffer[p_render_list].set_storage_size(0u, new_size * sizeof(SceneState::InstanceData)); |
| 1965 | curr_gpu_ptr[p_render_list] = nullptr; |
| 1966 | } |
| 1967 | |
| 1968 | const bool must_remap = instance_buffer[p_render_list].prepare_for_map(p_append); |
| 1969 | if (must_remap) { |
| 1970 | curr_gpu_ptr[p_render_list] = nullptr; |
| 1971 | } |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | void RenderForwardMobile::_fill_instance_data(RenderListType p_render_list, uint32_t p_offset, int32_t p_max_elements, bool p_update_buffer) { |
| 1976 | RenderList *rl = &render_list[p_render_list]; |
| 1977 | uint32_t element_total = p_max_elements >= 0 ? uint32_t(p_max_elements) : rl->elements.size(); |
| 1978 | |
| 1979 | rl->element_info.resize(p_offset + element_total); |
| 1980 | |
| 1981 | uint64_t frame = RSG::rasterizer->get_frame_number(); |
| 1982 | |
| 1983 | scene_state.grow_instance_buffer(p_render_list, p_offset + element_total, p_offset != 0u); |
| 1984 | if (!scene_state.curr_gpu_ptr[p_render_list] && element_total > 0u) { |
| 1985 | // The old buffer was replaced for another larger one. We must start copying from scratch. |
| 1986 | element_total += p_offset; |
| 1987 | p_offset = 0u; |
| 1988 | scene_state.curr_gpu_ptr[p_render_list] = reinterpret_cast<SceneState::InstanceData *>(scene_state.instance_buffer[p_render_list].map_raw_for_upload(0u)); |
| 1989 | } |
| 1990 | for (uint32_t i = 0; i < element_total; i++) { |
| 1991 | GeometryInstanceSurfaceDataCache *surface = rl->elements[i + p_offset]; |
| 1992 | GeometryInstanceForwardMobile *inst = surface->owner; |
| 1993 | |
| 1994 | SceneState::InstanceData instance_data; |
| 1995 | |
| 1996 | if (inst->prev_transform_dirty && frame > inst->prev_transform_change_frame + 1 && inst->prev_transform_change_frame) { |
| 1997 | inst->prev_transform = inst->transform; |
| 1998 | inst->prev_transform_dirty = false; |
| 1999 | } |
| 2000 | |
| 2001 | if (inst->store_transform_cache) { |
| 2002 | RendererRD::MaterialStorage::store_transform_transposed_3x4(inst->transform, instance_data.transform); |
| 2003 | RendererRD::MaterialStorage::store_transform_transposed_3x4(inst->prev_transform, instance_data.prev_transform); |
| 2004 | |
| 2005 | #ifdef REAL_T_IS_DOUBLE |
| 2006 | // Split the origin into two components, the float approximation and the missing precision. |
| 2007 | // In the shader we will combine these back together to restore the lost precision. |
| 2008 | RendererRD::MaterialStorage::split_double(inst->transform.origin.x, &instance_data.transform[3], &instance_data.model_precision[0]); |
| 2009 | RendererRD::MaterialStorage::split_double(inst->transform.origin.y, &instance_data.transform[7], &instance_data.model_precision[1]); |
| 2010 | RendererRD::MaterialStorage::split_double(inst->transform.origin.z, &instance_data.transform[11], &instance_data.model_precision[2]); |
| 2011 | RendererRD::MaterialStorage::split_double(inst->prev_transform.origin.x, &instance_data.prev_transform[3], &instance_data.prev_model_precision[0]); |
| 2012 | RendererRD::MaterialStorage::split_double(inst->prev_transform.origin.y, &instance_data.prev_transform[7], &instance_data.prev_model_precision[1]); |
| 2013 | RendererRD::MaterialStorage::split_double(inst->prev_transform.origin.z, &instance_data.prev_transform[11], &instance_data.prev_model_precision[2]); |
| 2014 | #endif |
| 2015 | } else { |
| 2016 | RendererRD::MaterialStorage::store_transform_transposed_3x4(Transform3D(), instance_data.transform); |
| 2017 | RendererRD::MaterialStorage::store_transform_transposed_3x4(Transform3D(), instance_data.prev_transform); |
| 2018 | } |
| 2019 | |
| 2020 | instance_data.flags = inst->flags_cache; |
| 2021 | instance_data.gi_offset = inst->gi_offset_cache; |
| 2022 | instance_data.layer_mask = inst->layer_mask; |
| 2023 | instance_data.instance_uniforms_ofs = uint32_t(inst->shader_uniforms_offset); |
| 2024 | instance_data.set_lightmap_uv_scale(inst->lightmap_uv_scale); |
| 2025 | |
| 2026 | AABB surface_aabb = AABB(Vector3(0.0, 0.0, 0.0), Vector3(1.0, 1.0, 1.0)); |
| 2027 | uint64_t format = RendererRD::MeshStorage::get_singleton()->mesh_surface_get_format(surface->surface); |
| 2028 | Vector4 uv_scale = Vector4(0.0, 0.0, 0.0, 0.0); |
| 2029 | |
| 2030 | if (format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) { |
| 2031 | surface_aabb = RendererRD::MeshStorage::get_singleton()->mesh_surface_get_aabb(surface->surface); |
| 2032 | uv_scale = RendererRD::MeshStorage::get_singleton()->mesh_surface_get_uv_scale(surface->surface); |
| 2033 | } |
| 2034 | |
| 2035 | fill_push_constant_instance_indices(&instance_data, inst); |
| 2036 | |
| 2037 | instance_data.set_compressed_aabb(surface_aabb); |
| 2038 | instance_data.set_uv_scale(uv_scale); |
| 2039 | |
| 2040 | scene_state.curr_gpu_ptr[p_render_list][i + p_offset] = instance_data; |
| 2041 | |
| 2042 | RenderElementInfo &element_info = rl->element_info[p_offset + i]; |
| 2043 | |
| 2044 | // Sets lod_index and uses_lightmap at once. |
| 2045 | element_info.value = uint32_t(surface->sort.sort_key1 & 0x1FF); |
| 2046 | } |
| 2047 | |
| 2048 | if (p_update_buffer && element_total > 0u) { |
| 2049 | RenderingDevice::get_singleton()->buffer_flush(scene_state.instance_buffer[p_render_list]._get(0u)); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | _FORCE_INLINE_ static uint32_t _indices_to_primitives(RS::PrimitiveType p_primitive, uint32_t p_indices) { |
| 2054 | static const uint32_t divisor[RS::PRIMITIVE_MAX] = { 1, 2, 1, 3, 1 }; |
| 2055 | static const uint32_t subtractor[RS::PRIMITIVE_MAX] = { 0, 0, 1, 0, 2 }; |
| 2056 | return (p_indices - subtractor[p_primitive]) / divisor[p_primitive]; |
| 2057 | } |
| 2058 | |
| 2059 | void RenderForwardMobile::_fill_render_list(RenderListType p_render_list, const RenderDataRD *p_render_data, PassMode p_pass_mode, bool p_append) { |
| 2060 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 2061 | |
| 2062 | if (p_render_list == RENDER_LIST_OPAQUE) { |
| 2063 | scene_state.used_screen_texture = false; |
| 2064 | scene_state.used_depth_texture = false; |
| 2065 | scene_state.used_lightmap = false; |
| 2066 | } |
| 2067 | uint32_t lightmap_captures_used = 0; |
| 2068 | |
| 2069 | Plane near_plane(-p_render_data->scene_data->cam_transform.basis.get_column(Vector3::AXIS_Z), p_render_data->scene_data->cam_transform.origin); |
| 2070 | near_plane.d += p_render_data->scene_data->cam_projection.get_z_near(); |
| 2071 | float z_max = p_render_data->scene_data->cam_projection.get_z_far() - p_render_data->scene_data->cam_projection.get_z_near(); |
| 2072 | |
| 2073 | RenderList *rl = &render_list[p_render_list]; |
| 2074 | |
| 2075 | // Parse any updates on our geometry, updates surface caches and such |
| 2076 | _update_dirty_geometry_instances(); |
| 2077 | |
| 2078 | if (!p_append) { |
| 2079 | rl->clear(); |
| 2080 | if (p_render_list == RENDER_LIST_OPAQUE) { |
| 2081 | render_list[RENDER_LIST_ALPHA].clear(); //opaque fills alpha too |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | //fill list |
| 2086 | |
| 2087 | for (int i = 0; i < (int)p_render_data->instances->size(); i++) { |
| 2088 | GeometryInstanceForwardMobile *inst = static_cast<GeometryInstanceForwardMobile *>((*p_render_data->instances)[i]); |
| 2089 | |
| 2090 | Vector3 center = inst->transform.origin; |
| 2091 | if (p_render_data->scene_data->cam_orthogonal) { |
| 2092 | if (inst->use_aabb_center) { |
| 2093 | center = inst->transformed_aabb.get_support(-near_plane.normal); |
| 2094 | } |
| 2095 | inst->depth = near_plane.distance_to(center) - inst->sorting_offset; |
| 2096 | } else { |
| 2097 | if (inst->use_aabb_center) { |
| 2098 | center = inst->transformed_aabb.position + (inst->transformed_aabb.size * 0.5); |
| 2099 | } |
| 2100 | inst->depth = p_render_data->scene_data->cam_transform.origin.distance_to(center) - inst->sorting_offset; |
| 2101 | } |
| 2102 | uint32_t depth_layer = CLAMP(int(inst->depth * 16 / z_max), 0, 15); |
| 2103 | |
| 2104 | uint32_t flags = inst->base_flags; //fill flags if appropriate |
| 2105 | |
| 2106 | if (inst->non_uniform_scale) { |
| 2107 | flags |= INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE; |
| 2108 | } |
| 2109 | |
| 2110 | bool uses_lightmap = false; |
| 2111 | // bool uses_gi = false; |
| 2112 | |
| 2113 | if (p_render_list == RENDER_LIST_OPAQUE) { |
| 2114 | if (inst->lightmap_instance.is_valid()) { |
| 2115 | int32_t lightmap_cull_index = -1; |
| 2116 | for (uint32_t j = 0; j < scene_state.lightmaps_used; j++) { |
| 2117 | if (scene_state.lightmap_ids[j] == inst->lightmap_instance) { |
| 2118 | lightmap_cull_index = j; |
| 2119 | break; |
| 2120 | } |
| 2121 | } |
| 2122 | if (lightmap_cull_index >= 0) { |
| 2123 | inst->gi_offset_cache = inst->lightmap_slice_index << 16; |
| 2124 | inst->gi_offset_cache |= lightmap_cull_index; |
| 2125 | flags |= INSTANCE_DATA_FLAG_USE_LIGHTMAP; |
| 2126 | if (scene_state.lightmap_has_sh[lightmap_cull_index]) { |
| 2127 | flags |= INSTANCE_DATA_FLAG_USE_SH_LIGHTMAP; |
| 2128 | } |
| 2129 | uses_lightmap = true; |
| 2130 | } else { |
| 2131 | inst->gi_offset_cache = 0xFFFFFFFF; |
| 2132 | } |
| 2133 | |
| 2134 | } else if (inst->lightmap_sh) { |
| 2135 | if (lightmap_captures_used < scene_state.max_lightmap_captures) { |
| 2136 | const Color *src_capture = inst->lightmap_sh->sh; |
| 2137 | LightmapCaptureData &lcd = scene_state.lightmap_captures[lightmap_captures_used]; |
| 2138 | for (int j = 0; j < 9; j++) { |
| 2139 | lcd.sh[j * 4 + 0] = src_capture[j].r; |
| 2140 | lcd.sh[j * 4 + 1] = src_capture[j].g; |
| 2141 | lcd.sh[j * 4 + 2] = src_capture[j].b; |
| 2142 | lcd.sh[j * 4 + 3] = src_capture[j].a; |
| 2143 | } |
| 2144 | flags |= INSTANCE_DATA_FLAG_USE_LIGHTMAP_CAPTURE; |
| 2145 | inst->gi_offset_cache = lightmap_captures_used; |
| 2146 | lightmap_captures_used++; |
| 2147 | uses_lightmap = true; |
| 2148 | } |
| 2149 | } |
| 2150 | } |
| 2151 | inst->flags_cache = flags; |
| 2152 | |
| 2153 | GeometryInstanceSurfaceDataCache *surf = inst->surface_caches; |
| 2154 | |
| 2155 | float lod_distance = 0.0; |
| 2156 | |
| 2157 | if (p_render_data->scene_data->cam_orthogonal) { |
| 2158 | lod_distance = 1.0; |
| 2159 | } else { |
| 2160 | Vector3 aabb_min = inst->transformed_aabb.position; |
| 2161 | Vector3 aabb_max = inst->transformed_aabb.position + inst->transformed_aabb.size; |
| 2162 | Vector3 camera_position = p_render_data->scene_data->main_cam_transform.origin; |
| 2163 | Vector3 surface_distance = Vector3(0.0, 0.0, 0.0).max(aabb_min - camera_position).max(camera_position - aabb_max); |
| 2164 | |
| 2165 | lod_distance = surface_distance.length(); |
| 2166 | } |
| 2167 | |
| 2168 | while (surf) { |
| 2169 | surf->sort.uses_lightmap = 0; |
| 2170 | |
| 2171 | // LOD |
| 2172 | |
| 2173 | if (p_render_data->scene_data->screen_mesh_lod_threshold > 0.0 && mesh_storage->mesh_surface_has_lod(surf->surface)) { |
| 2174 | uint32_t indices = 0; |
| 2175 | 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); |
| 2176 | if (p_render_data->render_info) { |
| 2177 | indices = _indices_to_primitives(surf->primitive, indices); |
| 2178 | if (p_render_list == RENDER_LIST_OPAQUE) { //opaque |
| 2179 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices; |
| 2180 | } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow |
| 2181 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices; |
| 2182 | } |
| 2183 | } |
| 2184 | } else { |
| 2185 | surf->sort.lod_index = 0; |
| 2186 | if (p_render_data->render_info) { |
| 2187 | uint32_t to_draw = mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface); |
| 2188 | to_draw = _indices_to_primitives(surf->primitive, to_draw); |
| 2189 | to_draw *= inst->instance_count; |
| 2190 | if (p_render_list == RENDER_LIST_OPAQUE) { //opaque |
| 2191 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw; |
| 2192 | } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow |
| 2193 | p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw; |
| 2194 | } |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | // ADD Element |
| 2199 | if (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT || p_pass_mode == PASS_MODE_MOTION_VECTORS) { |
| 2200 | #ifdef DEBUG_ENABLED |
| 2201 | bool force_alpha = unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW); |
| 2202 | #else |
| 2203 | bool force_alpha = false; |
| 2204 | #endif |
| 2205 | if (!force_alpha && (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE)) { |
| 2206 | rl->add_element(surf); |
| 2207 | } |
| 2208 | if (force_alpha || (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA)) { |
| 2209 | render_list[RENDER_LIST_ALPHA].add_element(surf); |
| 2210 | } |
| 2211 | |
| 2212 | if (uses_lightmap) { |
| 2213 | surf->sort.uses_lightmap = 1; // This needs to become our lightmap index but we'll do that in a separate PR. |
| 2214 | scene_state.used_lightmap = true; |
| 2215 | } |
| 2216 | |
| 2217 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_SCREEN_TEXTURE) { |
| 2218 | scene_state.used_screen_texture = true; |
| 2219 | } |
| 2220 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_DEPTH_TEXTURE) { |
| 2221 | scene_state.used_depth_texture = true; |
| 2222 | } |
| 2223 | if ((surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_STENCIL) && !force_alpha && (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE))) { |
| 2224 | scene_state.used_opaque_stencil = true; |
| 2225 | } |
| 2226 | |
| 2227 | } else if (p_pass_mode == PASS_MODE_SHADOW || p_pass_mode == PASS_MODE_SHADOW_DP) { |
| 2228 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW) { |
| 2229 | rl->add_element(surf); |
| 2230 | } |
| 2231 | } else if (p_pass_mode == PASS_MODE_DEPTH_MATERIAL) { |
| 2232 | if (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE | GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA)) { |
| 2233 | rl->add_element(surf); |
| 2234 | } |
| 2235 | } else { |
| 2236 | if (surf->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE)) { |
| 2237 | rl->add_element(surf); |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | surf->sort.depth_layer = depth_layer; |
| 2242 | |
| 2243 | surf = surf->next; |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | if (p_render_list == RENDER_LIST_OPAQUE && lightmap_captures_used) { |
| 2248 | RD::get_singleton()->buffer_update(scene_state.lightmap_capture_buffer, 0, sizeof(LightmapCaptureData) * lightmap_captures_used, scene_state.lightmap_captures); |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | void RenderForwardMobile::_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_pancake_shadows) { |
| 2253 | RID env = is_environment(p_render_data->environment) ? p_render_data->environment : RID(); |
| 2254 | RID reflection_probe_instance = p_render_data->reflection_probe.is_valid() ? RendererRD::LightStorage::get_singleton()->reflection_probe_instance_get_probe(p_render_data->reflection_probe) : RID(); |
| 2255 | |
| 2256 | // May do this earlier in RenderSceneRenderRD::render_scene |
| 2257 | if (scene_state.uniform_buffers.get_size(0u) == 0u) { |
| 2258 | scene_state.uniform_buffers.set_uniform_size(0u, p_render_data->scene_data->get_uniform_buffer_size_bytes()); |
| 2259 | } |
| 2260 | |
| 2261 | float luminance_multiplier = p_render_data->render_buffers.is_valid() ? p_render_data->render_buffers->get_luminance_multiplier() : 1.0; |
| 2262 | |
| 2263 | // Start a new setup. |
| 2264 | scene_state.uniform_buffers.prepare_for_upload(); |
| 2265 | p_render_data->scene_data->update_ubo(scene_state.uniform_buffers.get_for_upload(0u), 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, false); |
| 2266 | } |
| 2267 | |
| 2268 | /// RENDERING /// |
| 2269 | |
| 2270 | void RenderForwardMobile::_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) { |
| 2271 | //use template for faster performance (pass mode comparisons are inlined) |
| 2272 | |
| 2273 | switch (p_params->pass_mode) { |
| 2274 | case PASS_MODE_COLOR: { |
| 2275 | _render_list_template<PASS_MODE_COLOR>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 2276 | } break; |
| 2277 | case PASS_MODE_COLOR_TRANSPARENT: { |
| 2278 | _render_list_template<PASS_MODE_COLOR_TRANSPARENT>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 2279 | } break; |
| 2280 | case PASS_MODE_SHADOW: { |
| 2281 | _render_list_template<PASS_MODE_SHADOW>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 2282 | } break; |
| 2283 | case PASS_MODE_SHADOW_DP: { |
| 2284 | _render_list_template<PASS_MODE_SHADOW_DP>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 2285 | } break; |
| 2286 | case PASS_MODE_DEPTH_MATERIAL: { |
| 2287 | _render_list_template<PASS_MODE_DEPTH_MATERIAL>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 2288 | } break; |
| 2289 | case PASS_MODE_MOTION_VECTORS: { |
| 2290 | _render_list_template<PASS_MODE_MOTION_VECTORS>(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); |
| 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | void RenderForwardMobile::_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) { |
| 2296 | RD::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(p_framebuffer); |
| 2297 | p_params->framebuffer_format = fb_format; |
| 2298 | |
| 2299 | 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); |
| 2300 | _render_list(draw_list, fb_format, p_params, 0, p_params->element_count); |
| 2301 | RD::get_singleton()->draw_list_end(); |
| 2302 | } |
| 2303 | |
| 2304 | template <RenderForwardMobile::PassMode p_pass_mode> |
| 2305 | void RenderForwardMobile::_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) { |
| 2306 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 2307 | RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton(); |
| 2308 | |
| 2309 | RD::DrawListID draw_list = p_draw_list; |
| 2310 | RD::FramebufferFormatID framebuffer_format = p_framebuffer_Format; |
| 2311 | |
| 2312 | //global scope bindings |
| 2313 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, render_base_uniform_set, SCENE_UNIFORM_SET); |
| 2314 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_params->render_pass_uniform_set, RENDER_PASS_UNIFORM_SET); |
| 2315 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, scene_shader.default_vec4_xform_uniform_set, TRANSFORMS_UNIFORM_SET); |
| 2316 | |
| 2317 | RID material_uniform_set; |
| 2318 | RID prev_material_uniform_set; |
| 2319 | |
| 2320 | RID prev_vertex_array_rd; |
| 2321 | RID prev_index_array_rd; |
| 2322 | RID prev_xforms_uniform_set; |
| 2323 | bool should_request_redraw = false; |
| 2324 | |
| 2325 | void *mesh_surface; |
| 2326 | SceneShaderForwardMobile::ShaderData *shader = nullptr; |
| 2327 | SceneShaderForwardMobile::ShaderData *prev_shader = nullptr; |
| 2328 | SceneShaderForwardMobile::ShaderData::PipelineKey pipeline_key; |
| 2329 | uint32_t pipeline_hash = 0; |
| 2330 | uint32_t prev_pipeline_hash = 0; |
| 2331 | |
| 2332 | bool shadow_pass = (p_params->pass_mode == PASS_MODE_SHADOW) || (p_params->pass_mode == PASS_MODE_SHADOW_DP); |
| 2333 | |
| 2334 | for (uint32_t i = p_from_element; i < p_to_element; i++) { |
| 2335 | const GeometryInstanceSurfaceDataCache *surf = p_params->elements[i]; |
| 2336 | const RenderElementInfo &element_info = p_params->element_info[i]; |
| 2337 | const GeometryInstanceForwardMobile *inst = surf->owner; |
| 2338 | |
| 2339 | if (inst->instance_count == 0) { |
| 2340 | continue; |
| 2341 | } |
| 2342 | |
| 2343 | SceneShaderForwardMobile::ShaderSpecialization pipeline_specialization = p_params->base_specialization; |
| 2344 | pipeline_specialization.multimesh = bool(inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH); |
| 2345 | pipeline_specialization.multimesh_format_2d = bool(inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D); |
| 2346 | pipeline_specialization.multimesh_has_color = bool(inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR); |
| 2347 | pipeline_specialization.multimesh_has_custom_data = bool(inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA); |
| 2348 | |
| 2349 | SceneState::PushConstant push_constant; |
| 2350 | push_constant.base_index = i + p_params->element_offset; |
| 2351 | |
| 2352 | if constexpr (p_pass_mode == PASS_MODE_DEPTH_MATERIAL) { |
| 2353 | push_constant.uv_offset = Math::make_half_float(p_params->uv_offset.y) << 16; |
| 2354 | push_constant.uv_offset |= Math::make_half_float(p_params->uv_offset.x); |
| 2355 | } else { |
| 2356 | push_constant.uv_offset = 0; |
| 2357 | } |
| 2358 | |
| 2359 | if (shadow_pass) { |
| 2360 | material_uniform_set = surf->material_uniform_set_shadow; |
| 2361 | shader = surf->shader_shadow; |
| 2362 | mesh_surface = surf->surface_shadow; |
| 2363 | |
| 2364 | } else { |
| 2365 | pipeline_specialization.use_light_projector = inst->use_projector; |
| 2366 | pipeline_specialization.use_light_soft_shadows = inst->use_soft_shadow; |
| 2367 | pipeline_specialization.omni_lights = SceneShaderForwardMobile::shader_count_for(inst->omni_light_count); |
| 2368 | pipeline_specialization.spot_lights = SceneShaderForwardMobile::shader_count_for(inst->spot_light_count); |
| 2369 | pipeline_specialization.reflection_probes = SceneShaderForwardMobile::shader_count_for(inst->reflection_probe_count); |
| 2370 | pipeline_specialization.decals = inst->decals_count > 0; |
| 2371 | |
| 2372 | #ifdef DEBUG_ENABLED |
| 2373 | if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_LIGHTING)) { |
| 2374 | material_uniform_set = scene_shader.default_material_uniform_set; |
| 2375 | shader = scene_shader.default_material_shader_ptr; |
| 2376 | } else if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW)) { |
| 2377 | material_uniform_set = scene_shader.overdraw_material_uniform_set; |
| 2378 | shader = scene_shader.overdraw_material_shader_ptr; |
| 2379 | } else if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_PSSM_SPLITS)) { |
| 2380 | material_uniform_set = scene_shader.debug_shadow_splits_material_uniform_set; |
| 2381 | shader = scene_shader.debug_shadow_splits_material_shader_ptr; |
| 2382 | } else { |
| 2383 | #endif |
| 2384 | material_uniform_set = surf->material_uniform_set; |
| 2385 | shader = surf->shader; |
| 2386 | surf->material->set_as_used(); |
| 2387 | #ifdef DEBUG_ENABLED |
| 2388 | } |
| 2389 | #endif |
| 2390 | mesh_surface = surf->surface; |
| 2391 | } |
| 2392 | |
| 2393 | if (!mesh_surface) { |
| 2394 | continue; |
| 2395 | } |
| 2396 | |
| 2397 | //request a redraw if one of the shaders uses TIME |
| 2398 | if (shader->uses_time) { |
| 2399 | should_request_redraw = true; |
| 2400 | } |
| 2401 | |
| 2402 | //find cull variant |
| 2403 | SceneShaderForwardMobile::ShaderData::CullVariant cull_variant; |
| 2404 | |
| 2405 | if (p_params->pass_mode == PASS_MODE_DEPTH_MATERIAL || ((p_params->pass_mode == PASS_MODE_SHADOW || p_params->pass_mode == PASS_MODE_SHADOW_DP) && surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_DOUBLE_SIDED_SHADOWS)) { |
| 2406 | cull_variant = SceneShaderForwardMobile::ShaderData::CULL_VARIANT_DOUBLE_SIDED; |
| 2407 | } else { |
| 2408 | bool mirror = surf->owner->mirror; |
| 2409 | if (p_params->reverse_cull) { |
| 2410 | mirror = !mirror; |
| 2411 | } |
| 2412 | cull_variant = mirror ? SceneShaderForwardMobile::ShaderData::CULL_VARIANT_REVERSED : SceneShaderForwardMobile::ShaderData::CULL_VARIANT_NORMAL; |
| 2413 | } |
| 2414 | |
| 2415 | pipeline_key.primitive_type = surf->primitive; |
| 2416 | RID xforms_uniform_set = surf->owner->transforms_uniform_set; |
| 2417 | |
| 2418 | switch (p_params->pass_mode) { |
| 2419 | case PASS_MODE_COLOR: |
| 2420 | case PASS_MODE_COLOR_TRANSPARENT: { |
| 2421 | if (element_info.uses_lightmap) { |
| 2422 | pipeline_key.version = p_params->view_count > 1 ? SceneShaderForwardMobile::SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW : SceneShaderForwardMobile::SHADER_VERSION_LIGHTMAP_COLOR_PASS; |
| 2423 | } else { |
| 2424 | pipeline_key.version = p_params->view_count > 1 ? SceneShaderForwardMobile::SHADER_VERSION_COLOR_PASS_MULTIVIEW : SceneShaderForwardMobile::SHADER_VERSION_COLOR_PASS; |
| 2425 | } |
| 2426 | } break; |
| 2427 | case PASS_MODE_SHADOW: { |
| 2428 | pipeline_key.version = p_params->view_count > 1 ? SceneShaderForwardMobile::SHADER_VERSION_SHADOW_PASS_MULTIVIEW : SceneShaderForwardMobile::SHADER_VERSION_SHADOW_PASS; |
| 2429 | } break; |
| 2430 | case PASS_MODE_SHADOW_DP: { |
| 2431 | ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for shadow DP pass"); |
| 2432 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_SHADOW_PASS_DP; |
| 2433 | } break; |
| 2434 | case PASS_MODE_DEPTH_MATERIAL: { |
| 2435 | ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for material pass"); |
| 2436 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL; |
| 2437 | } break; |
| 2438 | case PASS_MODE_MOTION_VECTORS: { |
| 2439 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_MOTION_VECTORS_MULTIVIEW; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | pipeline_key.framebuffer_format_id = framebuffer_format; |
| 2444 | pipeline_key.wireframe = p_params->force_wireframe; |
| 2445 | pipeline_key.render_pass = p_params->subpass; |
| 2446 | pipeline_key.ubershader = 0; |
| 2447 | |
| 2448 | bool emulate_point_size = shader->uses_point_size && scene_shader.emulate_point_size; |
| 2449 | |
| 2450 | const RD::PolygonCullMode cull_mode = shader->get_cull_mode_from_cull_variant(cull_variant); |
| 2451 | RD::VertexFormatID vertex_format = -1; |
| 2452 | RID pipeline_rd; |
| 2453 | RID vertex_array_rd; |
| 2454 | RID index_array_rd; |
| 2455 | const uint32_t ubershader_iterations = 2; |
| 2456 | bool pipeline_valid = false; |
| 2457 | while (pipeline_key.ubershader < ubershader_iterations) { |
| 2458 | // Skeleton and blend shape. |
| 2459 | uint64_t input_mask = shader->get_vertex_input_mask(pipeline_key.version, pipeline_key.ubershader); |
| 2460 | if (surf->owner->mesh_instance.is_valid()) { |
| 2461 | mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, input_mask, p_pass_mode == PASS_MODE_MOTION_VECTORS, emulate_point_size, vertex_array_rd, vertex_format); |
| 2462 | } else { |
| 2463 | mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, input_mask, p_pass_mode == PASS_MODE_MOTION_VECTORS, emulate_point_size, vertex_array_rd, vertex_format); |
| 2464 | } |
| 2465 | |
| 2466 | pipeline_key.vertex_format_id = vertex_format; |
| 2467 | |
| 2468 | if (pipeline_key.ubershader) { |
| 2469 | pipeline_key.shader_specialization = {}; |
| 2470 | pipeline_key.cull_mode = RD::POLYGON_CULL_DISABLED; |
| 2471 | } else { |
| 2472 | pipeline_key.shader_specialization = pipeline_specialization; |
| 2473 | pipeline_key.cull_mode = cull_mode; |
| 2474 | } |
| 2475 | |
| 2476 | pipeline_hash = pipeline_key.hash(); |
| 2477 | |
| 2478 | if (shader != prev_shader || pipeline_hash != prev_pipeline_hash) { |
| 2479 | RS::PipelineSource pipeline_source = pipeline_key.ubershader ? RS::PIPELINE_SOURCE_DRAW : RS::PIPELINE_SOURCE_SPECIALIZATION; |
| 2480 | pipeline_rd = shader->pipeline_hash_map.get_pipeline(pipeline_key, pipeline_hash, pipeline_key.ubershader, pipeline_source); |
| 2481 | |
| 2482 | if (pipeline_rd.is_valid()) { |
| 2483 | pipeline_valid = true; |
| 2484 | prev_shader = shader; |
| 2485 | prev_pipeline_hash = pipeline_hash; |
| 2486 | break; |
| 2487 | } else { |
| 2488 | pipeline_key.ubershader++; |
| 2489 | } |
| 2490 | } else { |
| 2491 | // The same pipeline is bound already. |
| 2492 | pipeline_valid = true; |
| 2493 | break; |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | if (pipeline_valid) { |
| 2498 | if (!emulate_point_size) { |
| 2499 | index_array_rd = mesh_storage->mesh_surface_get_index_array(mesh_surface, element_info.lod_index); |
| 2500 | } else { |
| 2501 | index_array_rd = RID(); |
| 2502 | } |
| 2503 | |
| 2504 | if (prev_vertex_array_rd != vertex_array_rd) { |
| 2505 | RD::get_singleton()->draw_list_bind_vertex_array(draw_list, vertex_array_rd); |
| 2506 | prev_vertex_array_rd = vertex_array_rd; |
| 2507 | } |
| 2508 | |
| 2509 | if (prev_index_array_rd != index_array_rd) { |
| 2510 | if (index_array_rd.is_valid()) { |
| 2511 | RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array_rd); |
| 2512 | } |
| 2513 | prev_index_array_rd = index_array_rd; |
| 2514 | } |
| 2515 | |
| 2516 | if (!pipeline_rd.is_null()) { |
| 2517 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline_rd); |
| 2518 | } |
| 2519 | |
| 2520 | if (xforms_uniform_set.is_valid() && prev_xforms_uniform_set != xforms_uniform_set) { |
| 2521 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, xforms_uniform_set, TRANSFORMS_UNIFORM_SET); |
| 2522 | prev_xforms_uniform_set = xforms_uniform_set; |
| 2523 | } |
| 2524 | |
| 2525 | if (material_uniform_set != prev_material_uniform_set) { |
| 2526 | // Update uniform set. |
| 2527 | if (material_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(material_uniform_set)) { // Material may not have a uniform set. |
| 2528 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, material_uniform_set, MATERIAL_UNIFORM_SET); |
| 2529 | } |
| 2530 | |
| 2531 | prev_material_uniform_set = material_uniform_set; |
| 2532 | } |
| 2533 | |
| 2534 | if (surf->owner->base_flags & INSTANCE_DATA_FLAG_PARTICLES) { |
| 2535 | 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); |
| 2536 | } else if (surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH) { |
| 2537 | 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); |
| 2538 | } else { |
| 2539 | push_constant.multimesh_motion_vectors_current_offset = 0; |
| 2540 | push_constant.multimesh_motion_vectors_previous_offset = 0; |
| 2541 | } |
| 2542 | |
| 2543 | size_t push_constant_size = 0; |
| 2544 | if (pipeline_key.ubershader) { |
| 2545 | push_constant_size = sizeof(SceneState::PushConstant); |
| 2546 | push_constant.ubershader.specialization = pipeline_specialization; |
| 2547 | push_constant.ubershader.constants = {}; |
| 2548 | push_constant.ubershader.constants.cull_mode = cull_mode; |
| 2549 | } else { |
| 2550 | push_constant_size = sizeof(SceneState::PushConstant) - sizeof(SceneState::PushConstantUbershader); |
| 2551 | } |
| 2552 | |
| 2553 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, push_constant_size); |
| 2554 | |
| 2555 | uint32_t instance_count = surf->owner->instance_count > 1 ? surf->owner->instance_count : 1; |
| 2556 | if (surf->flags & GeometryInstanceSurfaceDataCache::FLAG_USES_PARTICLE_TRAILS) { |
| 2557 | instance_count /= surf->owner->trail_steps; |
| 2558 | } |
| 2559 | |
| 2560 | bool indirect = bool(surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH_INDIRECT); |
| 2561 | |
| 2562 | if (emulate_point_size) { |
| 2563 | if (indirect) { |
| 2564 | WARN_PRINT("Indirect draws are not supported when emulating point size."); |
| 2565 | } |
| 2566 | RD::get_singleton()->draw_list_draw(draw_list, false, mesh_storage->mesh_surface_get_vertex_count(mesh_surface), instance_count * 6); |
| 2567 | } else if (indirect) { |
| 2568 | 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); |
| 2569 | } else { |
| 2570 | RD::get_singleton()->draw_list_draw(draw_list, index_array_rd.is_valid(), instance_count); |
| 2571 | } |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | // Make the actual redraw request |
| 2576 | if (should_request_redraw) { |
| 2577 | RenderingServerDefault::redraw_request(); |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | /* Geometry instance */ |
| 2582 | |
| 2583 | RenderGeometryInstance *RenderForwardMobile::geometry_instance_create(RID p_base) { |
| 2584 | RS::InstanceType type = RSG::utilities->get_base_type(p_base); |
| 2585 | ERR_FAIL_COND_V(!((1 << type) & RS::INSTANCE_GEOMETRY_MASK), nullptr); |
| 2586 | |
| 2587 | GeometryInstanceForwardMobile *ginstance = geometry_instance_alloc.alloc(); |
| 2588 | ginstance->data = memnew(GeometryInstanceForwardMobile::Data); |
| 2589 | |
| 2590 | ginstance->data->base = p_base; |
| 2591 | ginstance->data->base_type = type; |
| 2592 | ginstance->data->dependency_tracker.userdata = ginstance; |
| 2593 | ginstance->data->dependency_tracker.changed_callback = _geometry_instance_dependency_changed; |
| 2594 | ginstance->data->dependency_tracker.deleted_callback = _geometry_instance_dependency_deleted; |
| 2595 | |
| 2596 | ginstance->_mark_dirty(); |
| 2597 | |
| 2598 | return ginstance; |
| 2599 | } |
| 2600 | |
| 2601 | void RendererSceneRenderImplementation::RenderForwardMobile::GeometryInstanceForwardMobile::set_transform(const Transform3D &p_transform, const AABB &p_aabb, const AABB &p_transformed_aabb) { |
| 2602 | uint64_t frame = RSG::rasterizer->get_frame_number(); |
| 2603 | if (frame != prev_transform_change_frame) { |
| 2604 | prev_transform = transform; |
| 2605 | prev_transform_change_frame = frame; |
| 2606 | prev_transform_dirty = true; |
| 2607 | } |
| 2608 | |
| 2609 | RenderGeometryInstanceBase::set_transform(p_transform, p_aabb, p_transformed_aabb); |
| 2610 | } |
| 2611 | |
| 2612 | void RenderForwardMobile::GeometryInstanceForwardMobile::set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) { |
| 2613 | lightmap_instance = p_lightmap_instance; |
| 2614 | lightmap_uv_scale = p_lightmap_uv_scale; |
| 2615 | lightmap_slice_index = p_lightmap_slice_index; |
| 2616 | |
| 2617 | _mark_dirty(); |
| 2618 | } |
| 2619 | |
| 2620 | void RenderForwardMobile::GeometryInstanceForwardMobile::set_lightmap_capture(const Color *p_sh9) { |
| 2621 | if (p_sh9) { |
| 2622 | if (lightmap_sh == nullptr) { |
| 2623 | lightmap_sh = RenderForwardMobile::get_singleton()->geometry_instance_lightmap_sh.alloc(); |
| 2624 | } |
| 2625 | |
| 2626 | memcpy(lightmap_sh->sh, p_sh9, sizeof(Color) * 9); |
| 2627 | } else { |
| 2628 | if (lightmap_sh != nullptr) { |
| 2629 | RenderForwardMobile::get_singleton()->geometry_instance_lightmap_sh.free(lightmap_sh); |
| 2630 | lightmap_sh = nullptr; |
| 2631 | } |
| 2632 | } |
| 2633 | _mark_dirty(); |
| 2634 | } |
| 2635 | |
| 2636 | void RenderForwardMobile::geometry_instance_free(RenderGeometryInstance *p_geometry_instance) { |
| 2637 | GeometryInstanceForwardMobile *ginstance = static_cast<GeometryInstanceForwardMobile *>(p_geometry_instance); |
| 2638 | ERR_FAIL_NULL(ginstance); |
| 2639 | if (ginstance->lightmap_sh != nullptr) { |
| 2640 | geometry_instance_lightmap_sh.free(ginstance->lightmap_sh); |
| 2641 | } |
| 2642 | GeometryInstanceSurfaceDataCache *surf = ginstance->surface_caches; |
| 2643 | while (surf) { |
| 2644 | GeometryInstanceSurfaceDataCache *next = surf->next; |
| 2645 | geometry_instance_surface_alloc.free(surf); |
| 2646 | surf = next; |
| 2647 | } |
| 2648 | memdelete(ginstance->data); |
| 2649 | geometry_instance_alloc.free(ginstance); |
| 2650 | } |
| 2651 | |
| 2652 | uint32_t RenderForwardMobile::geometry_instance_get_pair_mask() { |
| 2653 | return ((1 << RS::INSTANCE_LIGHT) + (1 << RS::INSTANCE_REFLECTION_PROBE) + (1 << RS::INSTANCE_DECAL)); |
| 2654 | } |
| 2655 | |
| 2656 | uint32_t RenderForwardMobile::get_max_lights_total() { |
| 2657 | return (uint32_t)get_singleton()->get_max_elements(); |
| 2658 | } |
| 2659 | |
| 2660 | uint32_t RenderForwardMobile::get_max_lights_per_mesh() { |
| 2661 | return (uint32_t)MAX_RDL_CULL; |
| 2662 | } |
| 2663 | |
| 2664 | void RenderForwardMobile::GeometryInstanceForwardMobile::clear_light_instances() { |
| 2665 | omni_light_count = 0; |
| 2666 | spot_light_count = 0; |
| 2667 | } |
| 2668 | |
| 2669 | void RenderForwardMobile::GeometryInstanceForwardMobile::pair_light_instance( |
| 2670 | const RID p_light_instance, RS::LightType light_type, uint32_t placement_idx) { |
| 2671 | if (placement_idx < (uint32_t)MAX_RDL_CULL) { |
| 2672 | RendererRD::ForwardID light_id = RendererRD::LightStorage::get_singleton()->light_instance_get_forward_id(p_light_instance); |
| 2673 | switch (light_type) { |
| 2674 | case RS::LIGHT_OMNI: { |
| 2675 | omni_lights[placement_idx] = light_id; |
| 2676 | if (placement_idx >= omni_light_count) { |
| 2677 | omni_light_count = placement_idx + 1; |
| 2678 | } |
| 2679 | } break; |
| 2680 | case RS::LIGHT_SPOT: { |
| 2681 | spot_lights[placement_idx] = light_id; |
| 2682 | if (placement_idx >= spot_light_count) { |
| 2683 | spot_light_count = placement_idx + 1; |
| 2684 | } |
| 2685 | } break; |
| 2686 | default: |
| 2687 | break; |
| 2688 | } |
| 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | void RenderForwardMobile::GeometryInstanceForwardMobile::pair_reflection_probe_instances(const RID *p_reflection_probe_instances, uint32_t p_reflection_probe_instance_count) { |
| 2693 | reflection_probe_count = p_reflection_probe_instance_count < (uint32_t)MAX_RDL_CULL ? p_reflection_probe_instance_count : (uint32_t)MAX_RDL_CULL; |
| 2694 | for (uint32_t i = 0; i < reflection_probe_count; i++) { |
| 2695 | reflection_probes[i] = RendererRD::LightStorage::get_singleton()->reflection_probe_instance_get_forward_id(p_reflection_probe_instances[i]); |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | void RenderForwardMobile::GeometryInstanceForwardMobile::pair_decal_instances(const RID *p_decal_instances, uint32_t p_decal_instance_count) { |
| 2700 | decals_count = p_decal_instance_count < (uint32_t)MAX_RDL_CULL ? p_decal_instance_count : (uint32_t)MAX_RDL_CULL; |
| 2701 | for (uint32_t i = 0; i < decals_count; i++) { |
| 2702 | decals[i] = RendererRD::TextureStorage::get_singleton()->decal_instance_get_forward_id(p_decal_instances[i]); |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | void RenderForwardMobile::GeometryInstanceForwardMobile::set_softshadow_projector_pairing(bool p_softshadow, bool p_projector) { |
| 2707 | use_projector = p_projector; |
| 2708 | use_soft_shadow = p_softshadow; |
| 2709 | } |
| 2710 | |
| 2711 | void RenderForwardMobile::GeometryInstanceForwardMobile::_mark_dirty() { |
| 2712 | if (dirty_list_element.in_list()) { |
| 2713 | return; |
| 2714 | } |
| 2715 | |
| 2716 | //clear surface caches |
| 2717 | GeometryInstanceSurfaceDataCache *surf = surface_caches; |
| 2718 | |
| 2719 | while (surf) { |
| 2720 | GeometryInstanceSurfaceDataCache *next = surf->next; |
| 2721 | RenderForwardMobile::get_singleton()->geometry_instance_surface_alloc.free(surf); |
| 2722 | surf = next; |
| 2723 | } |
| 2724 | |
| 2725 | surface_caches = nullptr; |
| 2726 | |
| 2727 | RenderForwardMobile::get_singleton()->geometry_instance_dirty_list.add(&dirty_list_element); |
| 2728 | } |
| 2729 | |
| 2730 | void RenderForwardMobile::_update_global_pipeline_data_requirements_from_project() { |
| 2731 | const int msaa_2d_mode = GLOBAL_GET("rendering/anti_aliasing/quality/msaa_2d"); |
| 2732 | const int msaa_3d_mode = GLOBAL_GET("rendering/anti_aliasing/quality/msaa_3d"); |
| 2733 | const bool directional_shadow_16_bits = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/16_bits"); |
| 2734 | const bool positional_shadow_16_bits = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_16_bits"); |
| 2735 | global_pipeline_data_required.use_16_bit_shadows = directional_shadow_16_bits || positional_shadow_16_bits; |
| 2736 | global_pipeline_data_required.use_32_bit_shadows = !directional_shadow_16_bits || !positional_shadow_16_bits; |
| 2737 | global_pipeline_data_required.target_samples = RenderSceneBuffersRD::msaa_to_samples(RS::ViewportMSAA(msaa_2d_mode)); |
| 2738 | global_pipeline_data_required.texture_samples = RenderSceneBuffersRD::msaa_to_samples(RS::ViewportMSAA(msaa_3d_mode)); |
| 2739 | } |
| 2740 | |
| 2741 | void RenderForwardMobile::_update_global_pipeline_data_requirements_from_light_storage() { |
| 2742 | RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton(); |
| 2743 | global_pipeline_data_required.use_shadow_cubemaps = light_storage->get_shadow_cubemaps_used(); |
| 2744 | global_pipeline_data_required.use_shadow_dual_paraboloid = light_storage->get_shadow_dual_paraboloid_used(); |
| 2745 | } |
| 2746 | |
| 2747 | void RenderForwardMobile::_geometry_instance_add_surface_with_material(GeometryInstanceForwardMobile *ginstance, uint32_t p_surface, SceneShaderForwardMobile::MaterialData *p_material, uint32_t p_material_id, uint32_t p_shader_id, RID p_mesh) { |
| 2748 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 2749 | uint32_t flags = 0; |
| 2750 | |
| 2751 | if (p_material->shader_data->uses_sss) { |
| 2752 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SUBSURFACE_SCATTERING; |
| 2753 | } |
| 2754 | |
| 2755 | if (p_material->shader_data->uses_screen_texture) { |
| 2756 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SCREEN_TEXTURE; |
| 2757 | global_surface_data.screen_texture_used = true; |
| 2758 | } |
| 2759 | |
| 2760 | if (p_material->shader_data->uses_depth_texture) { |
| 2761 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_DEPTH_TEXTURE; |
| 2762 | global_surface_data.depth_texture_used = true; |
| 2763 | } |
| 2764 | |
| 2765 | if (p_material->shader_data->uses_normal_texture) { |
| 2766 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_NORMAL_TEXTURE; |
| 2767 | } |
| 2768 | |
| 2769 | if (ginstance->data->cast_double_sided_shadows) { |
| 2770 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_DOUBLE_SIDED_SHADOWS; |
| 2771 | } |
| 2772 | |
| 2773 | if (p_material->shader_data->stencil_enabled) { |
| 2774 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_STENCIL; |
| 2775 | } |
| 2776 | |
| 2777 | if (p_material->shader_data->uses_alpha_pass()) { |
| 2778 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA; |
| 2779 | if (p_material->shader_data->uses_depth_in_alpha_pass()) { |
| 2780 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH; |
| 2781 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW; |
| 2782 | } |
| 2783 | } else { |
| 2784 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE; |
| 2785 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH; |
| 2786 | flags |= GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW; |
| 2787 | } |
| 2788 | |
| 2789 | if (p_material->shader_data->uses_particle_trails) { |
| 2790 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_PARTICLE_TRAILS; |
| 2791 | } |
| 2792 | |
| 2793 | if (p_material->shader_data->stencil_enabled) { |
| 2794 | if (p_material->shader_data->stencil_flags & SceneShaderForwardMobile::ShaderData::STENCIL_FLAG_READ) { |
| 2795 | // Stencil materials which read from the stencil buffer must be in the alpha pass. |
| 2796 | // This is critical to preserve compatibility once we'll have the compositor. |
| 2797 | if (!(flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA)) { |
| 2798 | String shader_path = p_material->shader_data->path.is_empty() ? "" : "(" + p_material->shader_data->path + ")"; |
| 2799 | 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)); |
| 2800 | } |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | SceneShaderForwardMobile::MaterialData *material_shadow = nullptr; |
| 2805 | void *surface_shadow = nullptr; |
| 2806 | if (p_material->shader_data->uses_shared_shadow_material()) { |
| 2807 | flags |= GeometryInstanceSurfaceDataCache::FLAG_USES_SHARED_SHADOW_MATERIAL; |
| 2808 | material_shadow = static_cast<SceneShaderForwardMobile::MaterialData *>(RendererRD::MaterialStorage::get_singleton()->material_get_data(scene_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 2809 | |
| 2810 | RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh); |
| 2811 | |
| 2812 | if (shadow_mesh.is_valid()) { |
| 2813 | surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, p_surface); |
| 2814 | } |
| 2815 | |
| 2816 | } else { |
| 2817 | material_shadow = p_material; |
| 2818 | } |
| 2819 | |
| 2820 | GeometryInstanceSurfaceDataCache *sdcache = geometry_instance_surface_alloc.alloc(); |
| 2821 | |
| 2822 | sdcache->flags = flags; |
| 2823 | |
| 2824 | sdcache->shader = p_material->shader_data; |
| 2825 | sdcache->material = p_material; |
| 2826 | sdcache->material_uniform_set = p_material->uniform_set; |
| 2827 | sdcache->surface = mesh_storage->mesh_get_surface(p_mesh, p_surface); |
| 2828 | sdcache->primitive = mesh_storage->mesh_surface_get_primitive(sdcache->surface); |
| 2829 | sdcache->surface_index = p_surface; |
| 2830 | |
| 2831 | if (ginstance->data->dirty_dependencies) { |
| 2832 | RSG::utilities->base_update_dependency(p_mesh, &ginstance->data->dependency_tracker); |
| 2833 | } |
| 2834 | |
| 2835 | //shadow |
| 2836 | sdcache->shader_shadow = material_shadow->shader_data; |
| 2837 | sdcache->material_uniform_set_shadow = material_shadow->uniform_set; |
| 2838 | |
| 2839 | sdcache->surface_shadow = surface_shadow ? surface_shadow : sdcache->surface; |
| 2840 | |
| 2841 | sdcache->owner = ginstance; |
| 2842 | |
| 2843 | sdcache->next = ginstance->surface_caches; |
| 2844 | ginstance->surface_caches = sdcache; |
| 2845 | |
| 2846 | //sortkey |
| 2847 | |
| 2848 | sdcache->sort.sort_key1 = 0; |
| 2849 | sdcache->sort.sort_key2 = 0; |
| 2850 | |
| 2851 | sdcache->sort.surface_index = p_surface; |
| 2852 | sdcache->sort.material_id_hi = (p_material_id & 0xFF000000) >> 24; |
| 2853 | sdcache->sort.material_id_lo = (p_material_id & 0x00FFFFFF); |
| 2854 | sdcache->sort.shader_id = p_shader_id; |
| 2855 | sdcache->sort.geometry_id = p_mesh.get_local_index(); |
| 2856 | sdcache->sort.priority = p_material->priority; |
| 2857 | |
| 2858 | uint64_t format = RendererRD::MeshStorage::get_singleton()->mesh_surface_get_format(sdcache->surface); |
| 2859 | if (p_material->shader_data->uses_tangent && !(format & RS::ARRAY_FORMAT_TANGENT)) { |
| 2860 | String shader_path = p_material->shader_data->path.is_empty() ? "" : "(" + p_material->shader_data->path + ")"; |
| 2861 | String mesh_path = mesh_storage->mesh_get_path(p_mesh).is_empty() ? "" : "(" + mesh_storage->mesh_get_path(p_mesh) + ")"; |
| 2862 | 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)); |
| 2863 | } |
| 2864 | |
| 2865 | #if PRELOAD_PIPELINES_ON_SURFACE_CACHE_CONSTRUCTION |
| 2866 | if (!sdcache->compilation_dirty_element.in_list()) { |
| 2867 | geometry_surface_compilation_dirty_list.add(&sdcache->compilation_dirty_element); |
| 2868 | } |
| 2869 | |
| 2870 | if (!sdcache->compilation_all_element.in_list()) { |
| 2871 | geometry_surface_compilation_all_list.add(&sdcache->compilation_all_element); |
| 2872 | } |
| 2873 | #endif |
| 2874 | } |
| 2875 | |
| 2876 | void RenderForwardMobile::_geometry_instance_add_surface_with_material_chain(GeometryInstanceForwardMobile *ginstance, uint32_t p_surface, SceneShaderForwardMobile::MaterialData *p_material, RID p_mat_src, RID p_mesh) { |
| 2877 | SceneShaderForwardMobile::MaterialData *material = p_material; |
| 2878 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 2879 | |
| 2880 | _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); |
| 2881 | |
| 2882 | while (material->next_pass.is_valid()) { |
| 2883 | RID next_pass = material->next_pass; |
| 2884 | material = static_cast<SceneShaderForwardMobile::MaterialData *>(material_storage->material_get_data(next_pass, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 2885 | if (!material || !material->shader_data->is_valid()) { |
| 2886 | break; |
| 2887 | } |
| 2888 | if (ginstance->data->dirty_dependencies) { |
| 2889 | material_storage->material_update_dependency(next_pass, &ginstance->data->dependency_tracker); |
| 2890 | } |
| 2891 | _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); |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | void RenderForwardMobile::_geometry_instance_add_surface(GeometryInstanceForwardMobile *ginstance, uint32_t p_surface, RID p_material, RID p_mesh) { |
| 2896 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 2897 | RID m_src; |
| 2898 | |
| 2899 | m_src = ginstance->data->material_override.is_valid() ? ginstance->data->material_override : p_material; |
| 2900 | |
| 2901 | SceneShaderForwardMobile::MaterialData *material = nullptr; |
| 2902 | |
| 2903 | if (m_src.is_valid()) { |
| 2904 | material = static_cast<SceneShaderForwardMobile::MaterialData *>(material_storage->material_get_data(m_src, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 2905 | if (!material || !material->shader_data->is_valid()) { |
| 2906 | material = nullptr; |
| 2907 | } |
| 2908 | } |
| 2909 | |
| 2910 | if (material) { |
| 2911 | if (ginstance->data->dirty_dependencies) { |
| 2912 | material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker); |
| 2913 | } |
| 2914 | } else { |
| 2915 | material = static_cast<SceneShaderForwardMobile::MaterialData *>(material_storage->material_get_data(scene_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 2916 | m_src = scene_shader.default_material; |
| 2917 | } |
| 2918 | |
| 2919 | ERR_FAIL_NULL(material); |
| 2920 | |
| 2921 | _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material, m_src, p_mesh); |
| 2922 | |
| 2923 | if (ginstance->data->material_overlay.is_valid()) { |
| 2924 | m_src = ginstance->data->material_overlay; |
| 2925 | |
| 2926 | material = static_cast<SceneShaderForwardMobile::MaterialData *>(material_storage->material_get_data(m_src, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 2927 | if (material && material->shader_data->is_valid()) { |
| 2928 | if (ginstance->data->dirty_dependencies) { |
| 2929 | material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker); |
| 2930 | } |
| 2931 | |
| 2932 | _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material, m_src, p_mesh); |
| 2933 | } |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | void RenderForwardMobile::_geometry_instance_update(RenderGeometryInstance *p_geometry_instance) { |
| 2938 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 2939 | RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton(); |
| 2940 | GeometryInstanceForwardMobile *ginstance = static_cast<GeometryInstanceForwardMobile *>(p_geometry_instance); |
| 2941 | |
| 2942 | if (ginstance->data->dirty_dependencies) { |
| 2943 | ginstance->data->dependency_tracker.update_begin(); |
| 2944 | } |
| 2945 | |
| 2946 | //add geometry for drawing |
| 2947 | switch (ginstance->data->base_type) { |
| 2948 | case RS::INSTANCE_MESH: { |
| 2949 | const RID *materials = nullptr; |
| 2950 | uint32_t surface_count; |
| 2951 | RID mesh = ginstance->data->base; |
| 2952 | |
| 2953 | materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count); |
| 2954 | if (materials) { |
| 2955 | //if no materials, no surfaces. |
| 2956 | const RID *inst_materials = ginstance->data->surface_materials.ptr(); |
| 2957 | uint32_t surf_mat_count = ginstance->data->surface_materials.size(); |
| 2958 | |
| 2959 | for (uint32_t j = 0; j < surface_count; j++) { |
| 2960 | RID material = (j < surf_mat_count && inst_materials[j].is_valid()) ? inst_materials[j] : materials[j]; |
| 2961 | _geometry_instance_add_surface(ginstance, j, material, mesh); |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | ginstance->instance_count = 1; |
| 2966 | |
| 2967 | } break; |
| 2968 | |
| 2969 | case RS::INSTANCE_MULTIMESH: { |
| 2970 | RID mesh = mesh_storage->multimesh_get_mesh(ginstance->data->base); |
| 2971 | if (mesh.is_valid()) { |
| 2972 | const RID *materials = nullptr; |
| 2973 | uint32_t surface_count; |
| 2974 | |
| 2975 | materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count); |
| 2976 | if (materials) { |
| 2977 | for (uint32_t j = 0; j < surface_count; j++) { |
| 2978 | _geometry_instance_add_surface(ginstance, j, materials[j], mesh); |
| 2979 | } |
| 2980 | } |
| 2981 | |
| 2982 | ginstance->instance_count = mesh_storage->multimesh_get_instances_to_draw(ginstance->data->base); |
| 2983 | } |
| 2984 | |
| 2985 | } break; |
| 2986 | #if 0 |
| 2987 | case RS::INSTANCE_IMMEDIATE: { |
| 2988 | RasterizerStorageGLES3::Immediate *immediate = storage->immediate_owner.get_or_null(inst->base); |
| 2989 | ERR_CONTINUE(!immediate); |
| 2990 | |
| 2991 | _add_geometry(immediate, inst, nullptr, -1, p_depth_pass, p_shadow_pass); |
| 2992 | |
| 2993 | } break; |
| 2994 | #endif |
| 2995 | case RS::INSTANCE_PARTICLES: { |
| 2996 | int draw_passes = particles_storage->particles_get_draw_passes(ginstance->data->base); |
| 2997 | |
| 2998 | for (int j = 0; j < draw_passes; j++) { |
| 2999 | RID mesh = particles_storage->particles_get_draw_pass_mesh(ginstance->data->base, j); |
| 3000 | if (!mesh.is_valid()) { |
| 3001 | continue; |
| 3002 | } |
| 3003 | |
| 3004 | const RID *materials = nullptr; |
| 3005 | uint32_t surface_count; |
| 3006 | |
| 3007 | materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count); |
| 3008 | if (materials) { |
| 3009 | for (uint32_t k = 0; k < surface_count; k++) { |
| 3010 | _geometry_instance_add_surface(ginstance, k, materials[k], mesh); |
| 3011 | } |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | ginstance->instance_count = particles_storage->particles_get_amount(ginstance->data->base, ginstance->trail_steps); |
| 3016 | |
| 3017 | } break; |
| 3018 | |
| 3019 | default: { |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | //Fill push constant |
| 3024 | |
| 3025 | bool store_transform = true; |
| 3026 | ginstance->base_flags = 0; |
| 3027 | |
| 3028 | if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) { |
| 3029 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH; |
| 3030 | |
| 3031 | if (mesh_storage->multimesh_get_transform_format(ginstance->data->base) == RS::MULTIMESH_TRANSFORM_2D) { |
| 3032 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D; |
| 3033 | } |
| 3034 | if (mesh_storage->multimesh_uses_colors(ginstance->data->base)) { |
| 3035 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR; |
| 3036 | } |
| 3037 | if (mesh_storage->multimesh_uses_custom_data(ginstance->data->base)) { |
| 3038 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA; |
| 3039 | } |
| 3040 | if (mesh_storage->multimesh_uses_indirect(ginstance->data->base)) { |
| 3041 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_INDIRECT; |
| 3042 | } |
| 3043 | |
| 3044 | ginstance->transforms_uniform_set = mesh_storage->multimesh_get_3d_uniform_set(ginstance->data->base, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET); |
| 3045 | |
| 3046 | } else if (ginstance->data->base_type == RS::INSTANCE_PARTICLES) { |
| 3047 | ginstance->base_flags |= INSTANCE_DATA_FLAG_PARTICLES; |
| 3048 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH; |
| 3049 | if (false) { // 2D particles |
| 3050 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D; |
| 3051 | } |
| 3052 | |
| 3053 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR; |
| 3054 | ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA; |
| 3055 | |
| 3056 | //for particles, stride is the trail size |
| 3057 | ginstance->base_flags |= (ginstance->trail_steps << INSTANCE_DATA_FLAGS_PARTICLE_TRAIL_SHIFT); |
| 3058 | |
| 3059 | if (!particles_storage->particles_is_using_local_coords(ginstance->data->base)) { |
| 3060 | store_transform = false; |
| 3061 | } |
| 3062 | ginstance->transforms_uniform_set = particles_storage->particles_get_instance_buffer_uniform_set(ginstance->data->base, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET); |
| 3063 | |
| 3064 | if (particles_storage->particles_get_frame_counter(ginstance->data->base) == 0) { |
| 3065 | // Particles haven't been cleared or updated, update once now to ensure they are ready to render. |
| 3066 | particles_storage->update_particles(); |
| 3067 | } |
| 3068 | |
| 3069 | if (ginstance->data->dirty_dependencies) { |
| 3070 | particles_storage->particles_update_dependency(ginstance->data->base, &ginstance->data->dependency_tracker); |
| 3071 | } |
| 3072 | } else if (ginstance->data->base_type == RS::INSTANCE_MESH) { |
| 3073 | if (mesh_storage->skeleton_is_valid(ginstance->data->skeleton)) { |
| 3074 | ginstance->transforms_uniform_set = mesh_storage->skeleton_get_3d_uniform_set(ginstance->data->skeleton, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET); |
| 3075 | if (ginstance->data->dirty_dependencies) { |
| 3076 | mesh_storage->skeleton_update_dependency(ginstance->data->skeleton, &ginstance->data->dependency_tracker); |
| 3077 | } |
| 3078 | } else { |
| 3079 | ginstance->transforms_uniform_set = RID(); |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | ginstance->store_transform_cache = store_transform; |
| 3084 | |
| 3085 | if (ginstance->data->dirty_dependencies) { |
| 3086 | ginstance->data->dependency_tracker.update_end(); |
| 3087 | ginstance->data->dirty_dependencies = false; |
| 3088 | } |
| 3089 | |
| 3090 | ginstance->dirty_list_element.remove_from_list(); |
| 3091 | } |
| 3092 | |
| 3093 | static RD::FramebufferFormatID _get_color_framebuffer_format_for_pipeline(RD::DataFormat p_color_format, bool p_can_be_storage, RD::TextureSamples p_samples, RD::TextureSamples p_target_samples, bool p_vrs, bool p_post_pass, bool p_hdr, uint32_t p_view_count) { |
| 3094 | const bool multisampling = p_samples > RD::TEXTURE_SAMPLES_1; |
| 3095 | RD::AttachmentFormat attachment; |
| 3096 | |
| 3097 | RD::AttachmentFormat unused_attachment; |
| 3098 | unused_attachment.usage_flags = RD::AttachmentFormat::UNUSED_ATTACHMENT; |
| 3099 | |
| 3100 | thread_local Vector<RD::AttachmentFormat> attachments; |
| 3101 | attachments.clear(); |
| 3102 | |
| 3103 | // Color attachment. |
| 3104 | attachment.samples = p_samples; |
| 3105 | attachment.format = p_color_format; |
| 3106 | attachment.usage_flags = RenderSceneBuffersRD::get_color_usage_bits(false, multisampling, p_can_be_storage); |
| 3107 | attachments.push_back(attachment); |
| 3108 | |
| 3109 | // Depth attachment. |
| 3110 | attachment.samples = p_samples; |
| 3111 | attachment.format = RenderSceneBuffersRD::get_depth_format(false, multisampling, p_can_be_storage); |
| 3112 | attachment.usage_flags = RenderSceneBuffersRD::get_depth_usage_bits(false, multisampling, p_can_be_storage); |
| 3113 | attachments.push_back(attachment); |
| 3114 | |
| 3115 | if (p_vrs) { |
| 3116 | // VRS attachment. |
| 3117 | attachment.samples = RD::TEXTURE_SAMPLES_1; |
| 3118 | attachment.format = RenderSceneBuffersRD::get_vrs_format(); |
| 3119 | attachment.usage_flags = RenderSceneBuffersRD::get_vrs_usage_bits(); |
| 3120 | attachments.push_back(attachment); |
| 3121 | } |
| 3122 | |
| 3123 | if (multisampling) { |
| 3124 | // Resolve attachment. |
| 3125 | attachment.samples = RD::TEXTURE_SAMPLES_1; |
| 3126 | attachment.format = p_color_format; |
| 3127 | attachment.usage_flags = RenderSceneBuffersRD::get_color_usage_bits(true, false, p_can_be_storage); |
| 3128 | attachments.push_back(attachment); |
| 3129 | } |
| 3130 | |
| 3131 | RD::FramebufferPass pass; |
| 3132 | thread_local Vector<RD::FramebufferPass> passes; |
| 3133 | passes.clear(); |
| 3134 | pass.color_attachments.clear(); |
| 3135 | pass.color_attachments.push_back(0); |
| 3136 | pass.depth_attachment = 1; |
| 3137 | |
| 3138 | if (multisampling) { |
| 3139 | pass.resolve_attachments.push_back(attachments.size() - 1); |
| 3140 | } |
| 3141 | |
| 3142 | passes.push_back(pass); |
| 3143 | |
| 3144 | if (p_post_pass) { |
| 3145 | attachment.format = RendererRD::TextureStorage::render_target_get_color_format(p_hdr, false); |
| 3146 | |
| 3147 | if (p_view_count > 1 || p_target_samples == RD::TEXTURE_SAMPLES_1) { |
| 3148 | attachment.samples = RD::TEXTURE_SAMPLES_1; |
| 3149 | attachment.usage_flags = RendererRD::TextureStorage::render_target_get_color_usage_bits(false); |
| 3150 | } else { |
| 3151 | attachment.samples = p_target_samples; |
| 3152 | attachment.usage_flags = RendererRD::TextureStorage::render_target_get_color_usage_bits(true); |
| 3153 | } |
| 3154 | |
| 3155 | attachments.push_back(attachment); |
| 3156 | |
| 3157 | RD::FramebufferPass blit_pass; |
| 3158 | blit_pass.input_attachments.push_back(multisampling ? (attachments.size() - 2) : 0); |
| 3159 | blit_pass.color_attachments.push_back(attachments.size() - 1); |
| 3160 | passes.push_back(blit_pass); |
| 3161 | } |
| 3162 | |
| 3163 | int32_t vrs_attachment = p_vrs ? 2 : -1; |
| 3164 | return RD::get_singleton()->framebuffer_format_create_multipass(attachments, passes, p_view_count, vrs_attachment); |
| 3165 | } |
| 3166 | |
| 3167 | static RD::FramebufferFormatID _get_reflection_probe_color_framebuffer_format_for_pipeline(bool p_storage) { |
| 3168 | RD::AttachmentFormat attachment; |
| 3169 | thread_local Vector<RD::AttachmentFormat> attachments; |
| 3170 | attachments.clear(); |
| 3171 | |
| 3172 | attachment.format = RendererRD::LightStorage::get_reflection_probe_color_format(); |
| 3173 | attachment.usage_flags = RendererRD::LightStorage::get_reflection_probe_color_usage_bits(p_storage); |
| 3174 | attachments.push_back(attachment); |
| 3175 | |
| 3176 | attachment.format = RendererRD::LightStorage::get_reflection_probe_depth_format(); |
| 3177 | attachment.usage_flags = RendererRD::LightStorage::get_reflection_probe_depth_usage_bits(); |
| 3178 | attachments.push_back(attachment); |
| 3179 | |
| 3180 | return RD::get_singleton()->framebuffer_format_create(attachments); |
| 3181 | } |
| 3182 | |
| 3183 | static RD::FramebufferFormatID _get_shadow_cubemap_framebuffer_format_for_pipeline() { |
| 3184 | thread_local LocalVector<RD::AttachmentFormat> attachments; |
| 3185 | attachments.clear(); |
| 3186 | |
| 3187 | RD::AttachmentFormat attachment; |
| 3188 | attachment.format = RendererRD::LightStorage::get_cubemap_depth_format(); |
| 3189 | attachment.usage_flags = RendererRD::LightStorage::get_cubemap_depth_usage_bits(); |
| 3190 | attachments.push_back(attachment); |
| 3191 | |
| 3192 | return RD::get_singleton()->framebuffer_format_create(Vector<RD::AttachmentFormat>(attachments)); |
| 3193 | } |
| 3194 | |
| 3195 | static RD::FramebufferFormatID _get_shadow_atlas_framebuffer_format_for_pipeline(bool p_use_16_bits) { |
| 3196 | thread_local LocalVector<RD::AttachmentFormat> attachments; |
| 3197 | attachments.clear(); |
| 3198 | |
| 3199 | RD::AttachmentFormat attachment; |
| 3200 | attachment.format = RendererRD::LightStorage::get_shadow_atlas_depth_format(p_use_16_bits); |
| 3201 | attachment.usage_flags = RendererRD::LightStorage::get_shadow_atlas_depth_usage_bits(); |
| 3202 | attachments.push_back(attachment); |
| 3203 | |
| 3204 | return RD::get_singleton()->framebuffer_format_create(Vector<RD::AttachmentFormat>(attachments)); |
| 3205 | } |
| 3206 | |
| 3207 | void RenderForwardMobile::_mesh_compile_pipeline_for_surface(SceneShaderForwardMobile::ShaderData *p_shader, void *p_mesh_surface, bool p_instanced_surface, RS::PipelineSource p_source, SceneShaderForwardMobile::ShaderData::PipelineKey &r_pipeline_key, Vector<ShaderPipelinePair> *r_pipeline_pairs) { |
| 3208 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 3209 | uint64_t input_mask = p_shader->get_vertex_input_mask(r_pipeline_key.version, true); |
| 3210 | bool emulate_point_size = p_shader->uses_point_size && scene_shader.emulate_point_size; |
| 3211 | r_pipeline_key.vertex_format_id = mesh_storage->mesh_surface_get_vertex_format(p_mesh_surface, input_mask, p_instanced_surface, false, emulate_point_size); |
| 3212 | r_pipeline_key.ubershader = true; |
| 3213 | p_shader->pipeline_hash_map.compile_pipeline(r_pipeline_key, r_pipeline_key.hash(), p_source, r_pipeline_key.ubershader); |
| 3214 | |
| 3215 | if (r_pipeline_pairs != nullptr) { |
| 3216 | r_pipeline_pairs->push_back({ p_shader, r_pipeline_key }); |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | void RenderForwardMobile::_mesh_compile_pipelines_for_surface(const SurfacePipelineData &p_surface, const GlobalPipelineData &p_global, RS::PipelineSource p_source, Vector<ShaderPipelinePair> *r_pipeline_pairs) { |
| 3221 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 3222 | bool octmap_use_storage = !copy_effects->get_raster_effects().has_flag(RendererRD::CopyEffects::RASTER_EFFECT_OCTMAP); |
| 3223 | |
| 3224 | // Set the attributes common to all pipelines. |
| 3225 | SceneShaderForwardMobile::ShaderData::PipelineKey pipeline_key; |
| 3226 | pipeline_key.cull_mode = RD::POLYGON_CULL_DISABLED; |
| 3227 | pipeline_key.primitive_type = mesh_storage->mesh_surface_get_primitive(p_surface.mesh_surface); |
| 3228 | pipeline_key.wireframe = false; |
| 3229 | |
| 3230 | const bool multiview_enabled = p_global.use_multiview && scene_shader.is_multiview_shader_group_enabled(); |
| 3231 | const bool buffers_can_be_storage = _render_buffers_can_be_storage(); |
| 3232 | const uint32_t vrs_iterations = p_global.use_vrs ? 2 : 1; |
| 3233 | |
| 3234 | const uint32_t post_pass_start = p_global.use_separate_post_pass ? 0 : 1; |
| 3235 | const uint32_t post_pass_iterations = p_global.use_subpass_post_pass ? 2 : (post_pass_start + 1); |
| 3236 | |
| 3237 | const uint32_t hdr_start = p_global.use_ldr_render_target ? 0 : 1; |
| 3238 | const uint32_t hdr_target_iterations = p_global.use_hdr_render_target ? 2 : 1; |
| 3239 | |
| 3240 | for (uint32_t use_vrs = 0; use_vrs < vrs_iterations; use_vrs++) { |
| 3241 | for (uint32_t use_post_pass = post_pass_start; use_post_pass < post_pass_iterations; use_post_pass++) { |
| 3242 | const uint32_t hdr_iterations = use_post_pass ? hdr_target_iterations : (hdr_start + 1); |
| 3243 | for (uint32_t use_hdr = hdr_start; use_hdr < hdr_iterations; use_hdr++) { |
| 3244 | const RD::DataFormat buffers_color_format = use_hdr ? RD::DATA_FORMAT_R16G16B16A16_SFLOAT : _render_buffers_get_preferred_color_format(); |
| 3245 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_COLOR_PASS; |
| 3246 | pipeline_key.framebuffer_format_id = _get_color_framebuffer_format_for_pipeline(buffers_color_format, buffers_can_be_storage, RD::TextureSamples(p_global.texture_samples), RD::TextureSamples(p_global.target_samples), use_vrs, use_post_pass, use_hdr, 1); |
| 3247 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3248 | |
| 3249 | if (p_global.use_lightmaps && p_surface.can_use_lightmap) { |
| 3250 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_LIGHTMAP_COLOR_PASS; |
| 3251 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3252 | } |
| 3253 | |
| 3254 | if (multiview_enabled) { |
| 3255 | // View count is assumed to be 2 as the configuration is dependent on the viewport. It's likely a safe assumption for stereo rendering. |
| 3256 | const uint32_t view_count = 2; |
| 3257 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_COLOR_PASS_MULTIVIEW; |
| 3258 | pipeline_key.framebuffer_format_id = _get_color_framebuffer_format_for_pipeline(buffers_color_format, buffers_can_be_storage, RD::TextureSamples(p_global.texture_samples), RD::TextureSamples(p_global.target_samples), use_vrs, use_post_pass, use_hdr, view_count); |
| 3259 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3260 | |
| 3261 | if (p_global.use_lightmaps && p_surface.can_use_lightmap) { |
| 3262 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW; |
| 3263 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3264 | } |
| 3265 | } |
| 3266 | } |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | if (p_global.use_reflection_probes) { |
| 3271 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_COLOR_PASS; |
| 3272 | pipeline_key.framebuffer_format_id = _get_reflection_probe_color_framebuffer_format_for_pipeline(octmap_use_storage); |
| 3273 | _mesh_compile_pipeline_for_surface(p_surface.shader, p_surface.mesh_surface, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3274 | } |
| 3275 | |
| 3276 | if (!p_surface.uses_depth) { |
| 3277 | return; |
| 3278 | } |
| 3279 | |
| 3280 | if (p_global.use_shadow_cubemaps) { |
| 3281 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_SHADOW_PASS; |
| 3282 | pipeline_key.framebuffer_format_id = _get_shadow_cubemap_framebuffer_format_for_pipeline(); |
| 3283 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3284 | } |
| 3285 | |
| 3286 | const uint32_t use_16_bits_start = p_global.use_32_bit_shadows ? 0 : 1; |
| 3287 | const uint32_t use_16_bits_iterations = p_global.use_16_bit_shadows ? 2 : 1; |
| 3288 | for (uint32_t use_16_bits = use_16_bits_start; use_16_bits < use_16_bits_iterations; use_16_bits++) { |
| 3289 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_SHADOW_PASS; |
| 3290 | pipeline_key.framebuffer_format_id = _get_shadow_atlas_framebuffer_format_for_pipeline(use_16_bits); |
| 3291 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3292 | |
| 3293 | if (p_global.use_shadow_dual_paraboloid) { |
| 3294 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_SHADOW_PASS_DP; |
| 3295 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3296 | } |
| 3297 | |
| 3298 | if (multiview_enabled) { |
| 3299 | pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_SHADOW_PASS_MULTIVIEW; |
| 3300 | _mesh_compile_pipeline_for_surface(p_surface.shader_shadow, p_surface.mesh_surface_shadow, p_surface.instanced, p_source, pipeline_key, r_pipeline_pairs); |
| 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | void RenderForwardMobile::_mesh_generate_all_pipelines_for_surface_cache(GeometryInstanceSurfaceDataCache *p_surface_cache, const GlobalPipelineData &p_global) { |
| 3306 | bool uses_alpha_pass = (p_surface_cache->flags & GeometryInstanceSurfaceDataCache::FLAG_PASS_ALPHA) != 0; |
| 3307 | SurfacePipelineData surface; |
| 3308 | surface.mesh_surface = p_surface_cache->surface; |
| 3309 | surface.mesh_surface_shadow = p_surface_cache->surface_shadow; |
| 3310 | surface.shader = p_surface_cache->shader; |
| 3311 | surface.shader_shadow = p_surface_cache->shader_shadow; |
| 3312 | surface.instanced = p_surface_cache->owner->mesh_instance.is_valid(); |
| 3313 | surface.uses_opaque = !uses_alpha_pass; |
| 3314 | surface.uses_transparent = uses_alpha_pass; |
| 3315 | surface.uses_depth = (p_surface_cache->flags & (GeometryInstanceSurfaceDataCache::FLAG_PASS_DEPTH | GeometryInstanceSurfaceDataCache::FLAG_PASS_OPAQUE | GeometryInstanceSurfaceDataCache::FLAG_PASS_SHADOW)) != 0; |
| 3316 | surface.can_use_lightmap = p_surface_cache->owner->lightmap_instance.is_valid() || p_surface_cache->owner->lightmap_sh; |
| 3317 | _mesh_compile_pipelines_for_surface(surface, p_global, RS::PIPELINE_SOURCE_SURFACE); |
| 3318 | } |
| 3319 | |
| 3320 | void RenderForwardMobile::_update_dirty_geometry_instances() { |
| 3321 | while (geometry_instance_dirty_list.first()) { |
| 3322 | _geometry_instance_update(geometry_instance_dirty_list.first()->self()); |
| 3323 | } |
| 3324 | |
| 3325 | _update_dirty_geometry_pipelines(); |
| 3326 | } |
| 3327 | |
| 3328 | void RenderForwardMobile::_update_dirty_geometry_pipelines() { |
| 3329 | if (global_pipeline_data_required.key != global_pipeline_data_compiled.key) { |
| 3330 | // Go through the entire list of surfaces and compile pipelines for everything again. |
| 3331 | SelfList<GeometryInstanceSurfaceDataCache> *list = geometry_surface_compilation_all_list.first(); |
| 3332 | while (list != nullptr) { |
| 3333 | GeometryInstanceSurfaceDataCache *surface_cache = list->self(); |
| 3334 | _mesh_generate_all_pipelines_for_surface_cache(surface_cache, global_pipeline_data_required); |
| 3335 | |
| 3336 | if (surface_cache->compilation_dirty_element.in_list()) { |
| 3337 | // Remove any elements from the dirty list as they don't need to be processed again. |
| 3338 | geometry_surface_compilation_dirty_list.remove(&surface_cache->compilation_dirty_element); |
| 3339 | } |
| 3340 | |
| 3341 | list = list->next(); |
| 3342 | } |
| 3343 | |
| 3344 | global_pipeline_data_compiled.key = global_pipeline_data_required.key; |
| 3345 | } else { |
| 3346 | // Compile pipelines only for the dirty list. |
| 3347 | if (!geometry_surface_compilation_dirty_list.first()) { |
| 3348 | return; |
| 3349 | } |
| 3350 | |
| 3351 | while (geometry_surface_compilation_dirty_list.first() != nullptr) { |
| 3352 | GeometryInstanceSurfaceDataCache *surface_cache = geometry_surface_compilation_dirty_list.first()->self(); |
| 3353 | _mesh_generate_all_pipelines_for_surface_cache(surface_cache, global_pipeline_data_compiled); |
| 3354 | surface_cache->compilation_dirty_element.remove_from_list(); |
| 3355 | } |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | void RenderForwardMobile::_geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker) { |
| 3360 | switch (p_notification) { |
| 3361 | case Dependency::DEPENDENCY_CHANGED_MATERIAL: |
| 3362 | case Dependency::DEPENDENCY_CHANGED_MESH: |
| 3363 | case Dependency::DEPENDENCY_CHANGED_PARTICLES: |
| 3364 | case Dependency::DEPENDENCY_CHANGED_MULTIMESH: |
| 3365 | case Dependency::DEPENDENCY_CHANGED_SKELETON_DATA: { |
| 3366 | static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty(); |
| 3367 | static_cast<GeometryInstanceForwardMobile *>(p_tracker->userdata)->data->dirty_dependencies = true; |
| 3368 | } break; |
| 3369 | case Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES: { |
| 3370 | GeometryInstanceForwardMobile *ginstance = static_cast<GeometryInstanceForwardMobile *>(p_tracker->userdata); |
| 3371 | if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) { |
| 3372 | ginstance->instance_count = RendererRD::MeshStorage::get_singleton()->multimesh_get_instances_to_draw(ginstance->data->base); |
| 3373 | } |
| 3374 | } break; |
| 3375 | default: { |
| 3376 | //rest of notifications of no interest |
| 3377 | } break; |
| 3378 | } |
| 3379 | } |
| 3380 | void RenderForwardMobile::_geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker) { |
| 3381 | static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty(); |
| 3382 | static_cast<GeometryInstanceForwardMobile *>(p_tracker->userdata)->data->dirty_dependencies = true; |
| 3383 | } |
| 3384 | |
| 3385 | /* misc */ |
| 3386 | |
| 3387 | bool RenderForwardMobile::is_dynamic_gi_supported() const { |
| 3388 | return false; |
| 3389 | } |
| 3390 | |
| 3391 | bool RenderForwardMobile::is_volumetric_supported() const { |
| 3392 | return false; |
| 3393 | } |
| 3394 | |
| 3395 | uint32_t RenderForwardMobile::get_max_elements() const { |
| 3396 | return 256; |
| 3397 | } |
| 3398 | |
| 3399 | RenderForwardMobile *RenderForwardMobile::singleton = nullptr; |
| 3400 | |
| 3401 | void RenderForwardMobile::_update_shader_quality_settings() { |
| 3402 | SceneShaderForwardMobile::ShaderSpecialization specialization = {}; |
| 3403 | specialization.soft_shadow_samples = soft_shadow_samples_get(); |
| 3404 | specialization.penumbra_shadow_samples = penumbra_shadow_samples_get(); |
| 3405 | specialization.directional_soft_shadow_samples = directional_soft_shadow_samples_get(); |
| 3406 | specialization.directional_penumbra_shadow_samples = directional_penumbra_shadow_samples_get(); |
| 3407 | specialization.decal_use_mipmaps = |
| 3408 | decals_get_filter() == RS::DECAL_FILTER_NEAREST_MIPMAPS || |
| 3409 | decals_get_filter() == RS::DECAL_FILTER_LINEAR_MIPMAPS || |
| 3410 | decals_get_filter() == RS::DECAL_FILTER_NEAREST_MIPMAPS_ANISOTROPIC || |
| 3411 | decals_get_filter() == RS::DECAL_FILTER_LINEAR_MIPMAPS_ANISOTROPIC; |
| 3412 | |
| 3413 | specialization.projector_use_mipmaps = |
| 3414 | light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS || |
| 3415 | light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS || |
| 3416 | light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_NEAREST_MIPMAPS_ANISOTROPIC || |
| 3417 | light_projectors_get_filter() == RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS_ANISOTROPIC; |
| 3418 | |
| 3419 | specialization.use_lightmap_bicubic_filter = lightmap_filter_bicubic_get(); |
| 3420 | specialization.use_material_debanding = material_use_debanding_get(); |
| 3421 | specialization.luminance_multiplier = 2.0f; |
| 3422 | scene_shader.set_default_specialization(specialization); |
| 3423 | |
| 3424 | base_uniforms_changed(); //also need this |
| 3425 | } |
| 3426 | |
| 3427 | RenderForwardMobile::RenderForwardMobile() { |
| 3428 | singleton = this; |
| 3429 | |
| 3430 | sky.set_texture_format(_render_buffers_get_preferred_color_format()); |
| 3431 | |
| 3432 | String defines; |
| 3433 | |
| 3434 | defines += "\n#define MAX_ROUGHNESS_LOD " + itos(get_roughness_layers() - 1) + ".0\n"; |
| 3435 | if (is_using_radiance_octmap_array()) { |
| 3436 | defines += "\n#define USE_RADIANCE_OCTMAP_ARRAY \n"; |
| 3437 | } |
| 3438 | // defines += "\n#define SDFGI_OCT_SIZE " + itos(gi.sdfgi_get_lightprobe_octahedron_size()) + "\n"; |
| 3439 | defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(MAX_DIRECTIONAL_LIGHTS) + "\n"; |
| 3440 | |
| 3441 | bool force_vertex_shading = GLOBAL_GET("rendering/shading/overrides/force_vertex_shading"); |
| 3442 | if (force_vertex_shading) { |
| 3443 | defines += "\n#define USE_VERTEX_LIGHTING\n"; |
| 3444 | } |
| 3445 | |
| 3446 | bool specular_occlusion = GLOBAL_GET("rendering/reflections/specular_occlusion/enabled"); |
| 3447 | if (!specular_occlusion) { |
| 3448 | defines += "\n#define SPECULAR_OCCLUSION_DISABLED\n"; |
| 3449 | } |
| 3450 | |
| 3451 | { |
| 3452 | //lightmaps |
| 3453 | scene_state.max_lightmaps = MAX_LIGHTMAPS; |
| 3454 | defines += "\n#define MAX_LIGHTMAP_TEXTURES " + itos(scene_state.max_lightmaps) + "\n"; |
| 3455 | defines += "\n#define MAX_LIGHTMAPS " + itos(scene_state.max_lightmaps) + "\n"; |
| 3456 | |
| 3457 | scene_state.lightmap_buffer = RD::get_singleton()->storage_buffer_create(sizeof(LightmapData) * scene_state.max_lightmaps); |
| 3458 | } |
| 3459 | { |
| 3460 | //captures |
| 3461 | scene_state.max_lightmap_captures = 2048; |
| 3462 | scene_state.lightmap_captures = memnew_arr(LightmapCaptureData, scene_state.max_lightmap_captures); |
| 3463 | scene_state.lightmap_capture_buffer = RD::get_singleton()->storage_buffer_create(sizeof(LightmapCaptureData) * scene_state.max_lightmap_captures); |
| 3464 | } |
| 3465 | { |
| 3466 | defines += "\n#define MATERIAL_UNIFORM_SET " + itos(MATERIAL_UNIFORM_SET) + "\n"; |
| 3467 | } |
| 3468 | #ifdef REAL_T_IS_DOUBLE |
| 3469 | { |
| 3470 | defines += "\n#define USE_DOUBLE_PRECISION \n"; |
| 3471 | } |
| 3472 | #endif |
| 3473 | |
| 3474 | scene_shader.init(defines); |
| 3475 | |
| 3476 | _update_shader_quality_settings(); |
| 3477 | _update_global_pipeline_data_requirements_from_project(); |
| 3478 | |
| 3479 | // Only update these from the project setting at init time. |
| 3480 | const bool root_hdr_render_target = GLOBAL_GET("rendering/viewport/hdr_2d"); |
| 3481 | global_pipeline_data_required.use_hdr_render_target = root_hdr_render_target; |
| 3482 | global_pipeline_data_required.use_ldr_render_target = !root_hdr_render_target; |
| 3483 | } |
| 3484 | |
| 3485 | RenderForwardMobile::~RenderForwardMobile() { |
| 3486 | RSG::light_storage->directional_shadow_atlas_set_size(0); |
| 3487 | |
| 3488 | { |
| 3489 | scene_state.uniform_buffers.uninit(); |
| 3490 | for (uint32_t i = 0; i < RENDER_LIST_MAX; i++) { |
| 3491 | scene_state.instance_buffer[i].uninit(); |
| 3492 | } |
| 3493 | RD::get_singleton()->free_rid(scene_state.lightmap_buffer); |
| 3494 | RD::get_singleton()->free_rid(scene_state.lightmap_capture_buffer); |
| 3495 | memdelete_arr(scene_state.lightmap_captures); |
| 3496 | } |
| 3497 | } |