Repository: godotengine/godot (language: cpp)

Issue:
Shader with instance uniforms fails in compatibility
### Tested versions

v4.4.dev4.official [36e6207bb]


### System information

Godot v4.4.dev4 - Windows 10.0.22631 - Multi-window, 2 monitors - OpenGL 3 (Compatibility) - NVIDIA GeForce RTX 2070 SUPER (NVIDIA; 32.0.15.6094) - 13th Gen Intel(R) Core(TM) i7-13700KF (24 threads)

### Issue description

Attached project uses both a global and 2 instance uniforms.

Actual error is: 
```
E 0:00:01:0046   _display_error_with_code: SceneShaderGLES3: Fragment shader compilation failed:
0(445) : error C1503: undefined variable "instance_offset"
```

Looks like definition is after use.
From the output log (filtered by instance_offset):
```
445: 		vec2 m_d=vec2((((global_shader_uniforms[m_iTime].x) * 0.00050000002375) + (global_shader_uniforms[(instance_offset+1u)].x)), 0.0);
672: uniform highp uint instance_offset;
```



### Steps to reproduce

Run attached project.


### Minimal reproduction project (MRP)

[instance-test.zip](https://github.com/user-attachments/files/18031254/instance-test.zip)



Discussion / hints:
looked into this a little bit and correct me if i'm wrong but i believe the error is coming from one of these function calls
https://github.com/godotengine/godot/blob/eb5103093c1bfd4d527ec1255d28e9bc8d3625b5/drivers/gles3/shader_gles3.cpp#L373-L376
which are OpenGL functions. not too sure how to proceed from here
It's a shader source compile error. Godot generates the shader code from a template. See https://github.com/godotengine/godot/blob/master/drivers/gles3/shaders/scene.glsl

If I move the declaration for instance_offset up, it works. 
I can make a PR for the diff below, but I'm not sure if I moved it to a "good" place (right after scene_data) and/or if the other variables in that block should maybe be moved, too.

```
diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl
index 2022c8ee43..778c4f953d 100644
--- a/drivers/gles3/shaders/scene.glsl
+++ b/drivers/gles3/shaders/scene.glsl
@@ -206,6 +206,8 @@ layout(std140) uniform SceneData { // ubo:2
 }
 scene_data;
 
+uniform highp uint instance_offset;
+
 #ifdef USE_ADDITIVE_LIGHTING
 
 #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
@@ -430,7 +432,6 @@ uniform highp mat4 world_transform;
 uniform highp vec3 compressed_aabb_position;
 uniform highp vec3 compressed_aabb_size;
 uniform highp vec4 uv_scale;
-uniform highp uint instance_offset;
 
 uniform highp uint model_flags;
 
@@ -965,6 +966,8 @@ layout(std140) uniform SceneData { // ubo:2
 }
 scene_data;
 
+uniform highp uint instance_offset;
+
 #ifdef USE_MULTIVIEW
 layout(std140) uniform MultiviewData { // ubo:8
        highp mat4 projection_matrix_view[MAX_VIEWS];
@@ -1202,7 +1205,6 @@ ivec2 multiview_uv(ivec2 uv) {
 uniform highp mat4 world_transform;
 uniform mediump float opaque_prepass_threshold;
 uniform highp uint model_flags;
-uniform highp uint instance_offset;
 
 #if defined(RENDER_MATERIAL)
 layout(location = 0) out vec4 albedo_output_buffer;
```

Fix patch:
diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl
index 2022c8ee436f..1867930b2922 100644
--- a/drivers/gles3/shaders/scene.glsl
+++ b/drivers/gles3/shaders/scene.glsl
@@ -974,6 +974,10 @@ layout(std140) uniform MultiviewData { // ubo:8
 multiview_data;
 #endif
 
+uniform highp mat4 world_transform;
+uniform highp uint instance_offset;
+uniform highp uint model_flags;
+
 /* clang-format off */
 
 #GLOBALS
@@ -1199,10 +1203,7 @@ ivec2 multiview_uv(ivec2 uv) {
 }
 #endif
 
-uniform highp mat4 world_transform;
 uniform mediump float opaque_prepass_threshold;
-uniform highp uint model_flags;
-uniform highp uint instance_offset;
 
 #if defined(RENDER_MATERIAL)
 layout(location = 0) out vec4 albedo_output_buffer;


Test patch: