Repository: godotengine/godot (language: cpp)

Issue:
[4.4beta1] Enabling SDFGI -> Desired set (1) not used by shader
### Tested versions

Reproducable in:

v4.4.beta1.official [d33da79d3]
v4.4.dev7.official [46c8f8c5c]
v4.4.dev6.official [1f47e4c4e]
v4.4.dev5.official [9e6098432]
v4.4.dev4.official [36e6207bb]

Not reproducable in:

v4.4.dev3.official [f4af8201b]
v4.4.dev2.official [97ef3c837]
v4.4.dev1.official [28a72fa43]


### System information

Godot v4.4.beta1 - Windows 11 (build 26100) - Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 2070 SUPER (NVIDIA; 32.0.15.6590) - AMD Ryzen 7 3800X 8-Core Processor (16 threads)

### Issue description

Enabling SDFGI throws errors (and crashes game when run outside editor):

Console output:

```
ERROR: Desired set (1) not used by shader.
   at: (servers/rendering/rendering_device.cpp:3427)
ERROR: Condition "rid.is_null()" is true. Returning: rid
   at: _allocate_from_uniforms (servers/rendering/renderer_rd/uniform_set_cache_rd.h:131)
ERROR: Parameter "uniform_set" is null.
   at: draw_list_bind_uniform_set (servers/rendering/rendering_device.cpp:4513)
```

### Steps to reproduce

1) Create new project
2) Create new scene
3) Add WorldEnvironment -> new Environment and enable SDFGI
4) Add Camera3D.
5) Run.

### Minimal reproduction project (MRP)

[sdfgi.zip](https://github.com/user-attachments/files/18476657/sdfgi.zip)


Discussion / hints:
Can confirm, bisecting (computer died partway through so taking a bit longer)
I can't reproduce this on 4.4.beta 791c87eb7 with the MRP.

<details open>

<summary>PC specifications</summary>

- **CPU:** Intel Core i9-13900K
- **GPU:** NVIDIA GeForce RTX 4090
- **RAM:** 64 GB (2×32 GB DDR5-5800 C30)
- **SSD:** Solidigm P44 Pro 2 TB
- **OS:** Windows 11 23H2
</details>
It's a bit finicky, will try to narrow down reproduction details once I finish bisecting (it's finicky so had to restart)
Trying to bisect but this is very transient and unable to properly pin down what exact conditions trigger it, will try redo the bisect tomorrow and see if I can reproduce it more reliably

I can reproduce it reliably on `beta1`, and on [9630d4e2f]
I think i found a way to reproduce this bug in a reliably way, i'll try bisect.
Bisected to #90400, @DarioSamo

![Image](https://github.com/user-attachments/assets/73c395c0-527e-4ef2-b72a-5cdcb878af6c)

<br>

In my case delete the folders `shader_cache` and `vulkan` in `%AppData%\Roaming\Godot\app_userdata\SDFI` was the trick. After delete this folders run the game once, close and run again (just the game, don't need to close the editor too). You'll get the errors or no error if the version is good. 

Also, in my case when i tested outside the editor, my game didn't crashed with this errors, maybe because the GPU?

![Image](https://github.com/user-attachments/assets/2946da61-fe6d-45d0-9898-8a5583b56104)

<br>

Godot v4.4.dev4 - Windows 10.0.19045 - Multi-window, 2 monitors - Vulkan (Forward+) - dedicated AMD Radeon RX 580 2048SP (Advanced Micro Devices, Inc.; 31.0.21921.1000) - AMD Ryzen 5 3600 6-Core Processor (12 threads)

Fix patch:
diff --git a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp
index 70b3a89c4a68..34d372252c68 100644
--- a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp
+++ b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp
@@ -3764,6 +3764,14 @@ RID RenderForwardClustered::_setup_sdfgi_render_pass_uniform_set(RID p_albedo_te
 		uniforms.push_back(u);
 	}
 
+	if (scene_shader.default_shader_sdfgi_rd.is_null()) {
+		// The variant for SDF from the default material should only be retrieved when SDFGI is required.
+		ERR_FAIL_NULL_V(scene_shader.default_material_shader_ptr, RID());
+		scene_shader.enable_advanced_shader_group();
+		scene_shader.default_shader_sdfgi_rd = scene_shader.default_material_shader_ptr->get_shader_variant(SceneShaderForwardClustered::PIPELINE_VERSION_DEPTH_PASS_WITH_SDF, 0, false);
+		ERR_FAIL_COND_V(scene_shader.default_shader_sdfgi_rd.is_null(), RID());
+	}
+
 	return UniformSetCacheRD::get_singleton()->get_cache_vec(scene_shader.default_shader_sdfgi_rd, RENDER_PASS_UNIFORM_SET, uniforms);
 }
 
diff --git a/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp
index 102d15206fe0..5debecb134d4 100644
--- a/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp
+++ b/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp
@@ -805,7 +805,6 @@ void fragment() {
 
 		MaterialData *md = static_cast<MaterialData *>(material_storage->material_get_data(default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D));
 		default_shader_rd = md->shader_data->get_shader_variant(PIPELINE_VERSION_COLOR_PASS, 0, false);
-		default_shader_sdfgi_rd = md->shader_data->get_shader_variant(PIPELINE_VERSION_DEPTH_PASS_WITH_SDF, 0, false);
 
 		default_material_shader_ptr = md->shader_data;
 		default_material_uniform_set = md->uniform_set;


Test patch: