Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referencing a built-in matrix within the light() function of a spatial shader throws an error; falls back #76556

Closed
ardlak opened this issue Apr 28, 2023 · 2 comments · Fixed by #76977

Comments

@ardlak
Copy link

ardlak commented Apr 28, 2023

Godot version

4.0.2.stable.official

System information

Windows 10, NVIDIA GeForce RTX 2080 SUPER, Driver 516.94, Vulkan backend

Issue description

When referencing a built-in matrix (MODEL_MATRIX, VIEW_MATRIX...) within the light() function of a spatial shader, these errors are thrown per renderer:

Forward+:

-   servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp:171 - Condition "!shader_singleton->shader.version_is_valid(version)" is true.

Mobile:

-   servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp:182 - Condition "!shader_singleton->shader.version_is_valid(version)" is true.

Compatibility (MODEL_MATRIX is referenced):

-  drivers/gles3/shader_gles3.cpp:242 - SceneShaderGLES3: Fragment shader compilation failed:
-   0(521) : error C1503: undefined variable "model_matrix"
-   drivers/gles3/shader_gles3.cpp:342 - Method/function failed.

The shader displays a fallback instead of what's written. This can be avoided by defining a variable that is identical to the matrix and referencing that instead:

shader_type spatial;

varying mat4 mdlMtx;

void vertex() {
	mdlMtx = MODEL_MATRIX;
}

void light() {
	mdlMtx; // works
//	MODEL_MATRIX; // doesn't work
}

Steps to reproduce

In a spatial shader, reference any built-in matrix within the light function:

shader_type spatial;

void light() {
	DIFFUSE_LIGHT += ATTENUATION; // lighting that should be displayed
	MODEL_MATRIX; // breaks the shader, displays a fallback
}

Minimal reproduction project

issue_light_matrices.zip

@clayjohn
Copy link
Member

Looks like the rest of the matrices need to be renamed in the light function. This comes from the uniform for the matrices using a different name from what the shader compiler maps onto.

For example, here is what is done in the GLES3 renderer's fragment function:

#ifdef USE_MULTIVIEW
vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz;
vec3 view = -normalize(vertex_interp - eye_offset);
mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex];
mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex];
#else
vec3 eye_offset = vec3(0.0, 0.0, 0.0);
vec3 view = -normalize(vertex_interp);
mat4 projection_matrix = scene_data.projection_matrix;
mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
#endif
highp mat4 model_matrix = world_transform;

The shader compiler turns MODEL_MATRIX into model_matrix, but the uniform is called world_transform so it needs to be remapped.

@mohsenph69
Copy link

mohsenph69 commented Jun 8, 2023

I Think get some related Issue

When I use this to calculate light direction in world space;
vec3 light_dir = mat3(INV_VIEW_MATRIX) * LIGHT;

In editor it will give me no error but I will give me a huge error in console

   at: _compile_variant (servers/rendering/renderer_rd/shader_rd.cpp:286)
### ERROR: Condition "!shader_singleton->shader.version_is_valid(version)" is true.
   at: set_code (servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp:171)
### ERROR: Condition "!shader" is true. Returning: RID()
   at: uniform_set_create (drivers/vulkan/rendering_device_vulkan.cpp:5361)
### ERROR: Condition "!us" is true.
   at: uniform_set_set_invalidation_callback (drivers/vulkan/rendering_device_vulkan.cpp:5835)

But when I pass INV_VIEW_MATRIX from vertex function with varying everything works
So I will use that until this error will fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants