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

GL_IMAGE_SAMPLER_NAME_NOT_FOUND_IN_SHADER: image-sampler name not found in shader #163

Open
AlexanderArvidsson opened this issue Dec 29, 2024 · 3 comments

Comments

@AlexanderArvidsson
Copy link

AlexanderArvidsson commented Dec 29, 2024

When using multiple samplers in a fragment shader, the program runs fine, but I get errors in my terminal:

[sg][error][id:8] c/sokol_gfx.h:8809:0: 
	GL_IMAGE_SAMPLER_NAME_NOT_FOUND_IN_SHADER: image-sampler name not found in shader (gl)

[sg][info][id:8] c/sokol_gfx.h:8810:0: 
	tex_position_smp_position

[sg][error][id:8] c/sokol_gfx.h:8809:0: 
	GL_IMAGE_SAMPLER_NAME_NOT_FOUND_IN_SHADER: image-sampler name not found in shader (gl)

[sg][info][id:8] c/sokol_gfx.h:8810:0: 
	tex_normal_smp_normal

[sg][error][id:8] c/sokol_gfx.h:8809:0: 
	GL_IMAGE_SAMPLER_NAME_NOT_FOUND_IN_SHADER: image-sampler name not found in shader (gl)

[sg][info][id:8] c/sokol_gfx.h:8810:0: 
	tex_depth_smp_depth

Here's my uniforms in my shader (for deferred rendering in a fullscreen quad):

layout(binding=0) uniform texture2D tex_color;
layout(binding=0) uniform sampler smp_color;

layout(binding=1) uniform texture2D tex_position;
layout(binding=1) uniform sampler smp_position;

layout(binding=2) uniform texture2D tex_normal;
layout(binding=2) uniform sampler smp_normal;

@image_sample_type tex_depth unfilterable_float
@sampler_type smp_depth nonfiltering
layout(binding=3) uniform texture2D tex_depth;
layout(binding=3) uniform sampler smp_depth;

Here's my shader pipeline (in Odin):

	g_mem.fullscreen.shader = sg.make_shader(fullscreen_shader_desc(sg.query_backend()))
	g_mem.fullscreen.pip = sg.make_pipeline(
		{
			shader = g_mem.fullscreen.shader,
			layout = {
				attrs = {
					ATTR_fullscreen_position = {format = .FLOAT4},
					ATTR_fullscreen_texcoord0 = {format = .FLOAT2},
				},
			},
			index_type = .UINT16,
			cull_mode = .BACK,
			depth = {compare = .LESS_EQUAL, write_enabled = true},
		},
	)

Here's the generated code form shdc (Odin):

    /*
    layout(binding = 16) uniform sampler2D tex_color_smp_color;
    layout(binding = 17) uniform sampler2D tex_position_smp_position;
    layout(binding = 18) uniform sampler2D tex_normal_smp_normal;
    layout(binding = 19) uniform sampler2D tex_depth_smp_depth;
    */

    case .GLCORE:
        desc.vertex_func.source = transmute(cstring)&vs_source_glsl430
        desc.vertex_func.entry = "main"
        desc.fragment_func.source = transmute(cstring)&fs_source_glsl430
        desc.fragment_func.entry = "main"
        desc.attrs[0].glsl_name = "position"
        desc.attrs[1].glsl_name = "texcoord0"
        desc.uniform_blocks[0].stage = .VERTEX
        desc.uniform_blocks[0].layout = .STD140
        desc.uniform_blocks[0].size = 80
        desc.uniform_blocks[0].glsl_uniforms[0].type = .FLOAT4
        desc.uniform_blocks[0].glsl_uniforms[0].array_count = 5
        desc.uniform_blocks[0].glsl_uniforms[0].glsl_name = "vs_fullscreen_params"
        desc.uniform_blocks[1].stage = .FRAGMENT
        desc.uniform_blocks[1].layout = .STD140
        desc.uniform_blocks[1].size = 16
        desc.uniform_blocks[1].glsl_uniforms[0].type = .FLOAT4
        desc.uniform_blocks[1].glsl_uniforms[0].array_count = 1
        desc.uniform_blocks[1].glsl_uniforms[0].glsl_name = "fs_fullscreen_params"
        desc.images[0].stage = .FRAGMENT
        desc.images[0].multisampled = false
        desc.images[0].image_type = ._2D
        desc.images[0].sample_type = .FLOAT
        desc.images[1].stage = .FRAGMENT
        desc.images[1].multisampled = false
        desc.images[1].image_type = ._2D
        desc.images[1].sample_type = .FLOAT
        desc.images[2].stage = .FRAGMENT
        desc.images[2].multisampled = false
        desc.images[2].image_type = ._2D
        desc.images[2].sample_type = .FLOAT
        desc.images[3].stage = .FRAGMENT
        desc.images[3].multisampled = false
        desc.images[3].image_type = ._2D
        desc.images[3].sample_type = .UNFILTERABLE_FLOAT
        desc.samplers[0].stage = .FRAGMENT
        desc.samplers[0].sampler_type = .FILTERING
        desc.samplers[1].stage = .FRAGMENT
        desc.samplers[1].sampler_type = .FILTERING
        desc.samplers[2].stage = .FRAGMENT
        desc.samplers[2].sampler_type = .FILTERING
        desc.samplers[3].stage = .FRAGMENT
        desc.samplers[3].sampler_type = .NONFILTERING
        desc.image_sampler_pairs[0].stage = .FRAGMENT
        desc.image_sampler_pairs[0].image_slot = 0
        desc.image_sampler_pairs[0].sampler_slot = 0
        desc.image_sampler_pairs[0].glsl_name = "tex_color_smp_color"
        desc.image_sampler_pairs[1].stage = .FRAGMENT
        desc.image_sampler_pairs[1].image_slot = 1
        desc.image_sampler_pairs[1].sampler_slot = 1
        desc.image_sampler_pairs[1].glsl_name = "tex_position_smp_position"
        desc.image_sampler_pairs[2].stage = .FRAGMENT
        desc.image_sampler_pairs[2].image_slot = 2
        desc.image_sampler_pairs[2].sampler_slot = 2
        desc.image_sampler_pairs[2].glsl_name = "tex_normal_smp_normal"
        desc.image_sampler_pairs[3].stage = .FRAGMENT
        desc.image_sampler_pairs[3].image_slot = 3
        desc.image_sampler_pairs[3].sampler_slot = 3
        desc.image_sampler_pairs[3].glsl_name = "tex_depth_smp_depth"

This error only shows up from the _sg_gl_create_shader function in the form of an error, but the program runs fine after that and I can read from the texture using the samplers correctly.

No error shows up on Metal on my MacOS.

I can't find many examples with multiple samplers so I could be doing something wrong here, please let me know!

@floooh
Copy link
Owner

floooh commented Dec 29, 2024

Hmm, that means the glGetUniformLocation() call here returned -1:

https://github.com/floooh/sokol/blob/c1cc713a48669fb78c8fadc1a3cb9dd6c3bb97d3/sokol_gfx.h#L8803C24-L8811

GLSL compilers like to remove resource bindings that are not actually used in the shader code. Can you paste the GLSL code passed into sokol-gfx? It's in the sokol-shdc output file as comment above the 'shader code arrays', e.g.:

https://github.com/floooh/sokol-odin/blob/2c47c1fdcc6f86fad1f2b3e03559e35bb50df6cc/examples/cube/shader.odin#L70-L81

I'm actually not sure if this situation should be logged as an error by sokol-gfx, it's at best a warning. It shouldn't be completely silent though, because sometimes it is actually something that needs to be fixed.

@AlexanderArvidsson
Copy link
Author

AlexanderArvidsson commented Dec 30, 2024

You're absolutely right! Since I wasn't done with my shader, I found that I could force the compiler to not omit my variables if I set my frag_color out attribute multiple times:

       frag_color = vec4(vec3(depth), 1);
       frag_color = position;
       frag_color = vec4(normal, 1);
       frag_color = color;

That way, I didn't get compile errors. But that must have still lead it to be excluded by GLSL, but not MSL. Understandable, it was a lazy hack to get things moving without having to comment in and out every other change.
Now that I've gotten a lot more work done on my shader and I'm actually using my samplers, the errors are gone.

May I suggest adding a hint about this in the error message? Since the program technically works like it should even when this error is logged, I think it should be a warning with a hint what could be wrong (i.e. unused sampler)!

@floooh
Copy link
Owner

floooh commented Dec 30, 2024

May I suggest adding a hint about this in the error message?

Yeah, I'll keep the issue open until I get around to improving the error message.

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

No branches or pull requests

2 participants