Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


shadercompiler

This is an old revision of the document!


Shader Compiler issues

“sampler_texname not found” compile error when sharing samplers

Consider this code:

#pragma shader_feature_local_fragment _ _ALBEDO; TEXTURE2D(_Albedo); TEXTURE2D(_Normal); SAMPLER(sampler_Albedo);

half4 Frag(v2f i) {

 #if _ALBEDO
    return SAMPLE_TEXTURE2D(_Albedo, sampler_Albedo, i.uv);
 #endif
 
 return SAMPLE_TEXTURE2D(_Normal, sampler_Albedo, i.uv); // we share the albedo sampler

}

When compiling this code, two variants are created, one for when albedo is on, another for when it's off. This will throw an error saying that sampler_albedo cannot be found. First, the spelling of the error is wrong, as it's all lowercase when the variable is not. Note that this error will only be thrown on some platforms (windows).

The reason for this is because DX9 associated samplers with textures, and Unity as an engine does also. When you set clamp on a texture, really it sets clamp on sampler_<TEXTURENAME> instead. So what happens in this case is when the variant with _ALBEDO not defined is compiled, the shader compiler strips the albedo texture because it's not being used, and assumes that since sampler_Albedo is associated with it, it should be stripped as well, and strips it, even though the sampler is actually being used.

Possible workarounds: - Give the normal map it's own sampler - Use a sampler not associated with a texture (which means the user cannot edit it via the texture settings) - Make sure you always sample the texture associated with the sampler so it doesn't get stripped, in a way that the compiler cannot strip that code as well. Something like:

result *= saturate(1 + SAMPLE_TEXTURE2D_LOD(_Albedo, sampler_Albedo, float2(0,0), 11)); return result;

shadercompiler.1619969640.txt.gz · Last modified: 2021/05/02 15:34 by 69.30.226.234