Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


Shader Compiler

Bug: Samplers are incorrectly stripped when used with stripped Texture2D

Consider this code:

#pragma shader_feature_local_fragment _ _ALBEDO;
half4 Frag(v2f i)
   #if _ALBEDO
      return SAMPLE_TEXTURE2D(_Albedo, sampler_Albedo, i.uv);
   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 found1) 2): “sampler_texname not found” compile error when sharing samplers

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;
Note: this error will only be thrown on some platforms (windows)
the spelling of the error is wrong, as it's all lowercase when the variable is not
shadercompiler.txt · Last modified: 2021/05/10 21:17 by uninomiconadmin