From b4adaa291a893987fc24406e909382165b909808 Mon Sep 17 00:00:00 2001 From: clayjohn Date: Sun, 22 Jun 2025 23:15:57 -0700 Subject: [PATCH] Fix a few improper memory accesses in the clustered forward vertex shader draw_call.instance_index should not be used directly since it doesn't take into account auto-batching scene_data_block.data should not be used directly in the vertex shader since it can change between frames and impact motion vector generation IN_SHADOW_PASS can only be accessed inside functions, so it needs to be a global and not a constant --- .../shaders/forward_clustered/scene_forward_clustered.glsl | 6 +++--- servers/rendering/shader_types.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl index afe3452037a..84726aca33a 100644 --- a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl +++ b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl @@ -599,11 +599,11 @@ void vertex_shader(vec3 vertex_input, vec3 directional_specular = vec3(0.0); for (uint i = 0; i < scene_data.directional_light_count; i++) { - if (!bool(directional_lights.data[i].mask & instances.data[draw_call.instance_index].layer_mask)) { + if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) { continue; // Not masked, skip. } - if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { + if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { continue; // Statically baked light and object uses lightmap, skip. } if (i == 0) { @@ -669,7 +669,7 @@ void vertex_shader(vec3 vertex_input, #endif #ifdef Z_CLIP_SCALE_USED - if (!bool(scene_data_block.data.flags & SCENE_DATA_FLAGS_IN_SHADOW_PASS)) { + if (!bool(scene_data.flags & SCENE_DATA_FLAGS_IN_SHADOW_PASS)) { gl_Position.z = mix(gl_Position.w, gl_Position.z, z_clip_scale); } #endif diff --git a/servers/rendering/shader_types.cpp b/servers/rendering/shader_types.cpp index 5efa66b4e67..bca38384808 100644 --- a/servers/rendering/shader_types.cpp +++ b/servers/rendering/shader_types.cpp @@ -76,12 +76,12 @@ ShaderTypes::ShaderTypes() { shader_modes[RS::SHADER_SPATIAL].functions["global"].built_ins["TIME"] = constt(ShaderLanguage::TYPE_FLOAT); shader_modes[RS::SHADER_SPATIAL].functions["global"].built_ins["EXPOSURE"] = constt(ShaderLanguage::TYPE_FLOAT); + shader_modes[RS::SHADER_SPATIAL].functions["global"].built_ins["IN_SHADOW_PASS"] = constt(ShaderLanguage::TYPE_BOOL); shader_modes[RS::SHADER_SPATIAL].functions["constants"].built_ins["PI"] = constvt(ShaderLanguage::TYPE_FLOAT, { pi_scalar }); shader_modes[RS::SHADER_SPATIAL].functions["constants"].built_ins["TAU"] = constvt(ShaderLanguage::TYPE_FLOAT, { tau_scalar }); shader_modes[RS::SHADER_SPATIAL].functions["constants"].built_ins["E"] = constvt(ShaderLanguage::TYPE_FLOAT, { e_scalar }); shader_modes[RS::SHADER_SPATIAL].functions["constants"].built_ins["OUTPUT_IS_SRGB"] = constt(ShaderLanguage::TYPE_BOOL); shader_modes[RS::SHADER_SPATIAL].functions["constants"].built_ins["CLIP_SPACE_FAR"] = constt(ShaderLanguage::TYPE_FLOAT); - shader_modes[RS::SHADER_SPATIAL].functions["constants"].built_ins["IN_SHADOW_PASS"] = constt(ShaderLanguage::TYPE_BOOL); shader_modes[RS::SHADER_SPATIAL].functions["vertex"].built_ins["VERTEX"] = ShaderLanguage::TYPE_VEC3; shader_modes[RS::SHADER_SPATIAL].functions["vertex"].built_ins["NORMAL"] = ShaderLanguage::TYPE_VEC3;