Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


shadercompiler

This is an old revision of the document!


Shader Compiler

"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, 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 1) 2)

DX9 associates samplers with textures, and so Unity does too. When you set a Unity Texture to Clamp, it sets '-clamp on sampler_<TEXTURENAME>. When the variant with _ALBEDO not defined is compiled, the compiler strips the albedo texture because it's not being used.

However, Unity assumes that since sampler_Albedo is associated with the _Albedo texture, it should be stripped as well, even though the sampler is also being used by the _Normal.

Possible workarounds:

  • Give the _Normal texture its 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. Something like:
result *= saturate(1 + SAMPLE_TEXTURE2D_LOD(_Albedo, sampler_Albedo, float2(0,0), 11));
return result;
1)
Note: this error will only be thrown on some platforms (windows
2)
the spelling of the error is wrong, as it's all lowercase when the variable is not
shadercompiler.1619757597.txt.gz · Last modified: 2021/04/30 04:39 by uninomiconadmin