Shader Compiler

Bug: Samplers are incorrectly stripped when used with stripped Texture2D

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 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:

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