Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


shadercompiler

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
shadercompiler [2021/05/02 15:34]
69.30.226.234 old revision restored (2021/04/30 04:11)
shadercompiler [2021/05/10 21:17] (current)
uninomiconadmin
Line 1: Line 1:
-Shader Compiler issues+====== Shader Compiler ======
  
- +==== Bug: Samplers are incorrectly stripped when used with stripped Texture2D ====
-**"sampler_texname not found" compile error when sharing samplers**+
  
 Consider this code: Consider this code:
  
 +<code glsl>
 #pragma shader_feature_local_fragment _ _ALBEDO; #pragma shader_feature_local_fragment _ _ALBEDO;
 TEXTURE2D(_Albedo); TEXTURE2D(_Albedo);
Line 19: Line 19:
    return SAMPLE_TEXTURE2D(_Normal, sampler_Albedo, i.uv); // we share the albedo sampler    return SAMPLE_TEXTURE2D(_Normal, sampler_Albedo, i.uv); // we share the albedo sampler
 } }
 +</code>
  
 +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((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)):
 +''"sampler_texname not found" compile error when sharing samplers''
  
-When compiling this codetwo variants are created, one for when albedo is on, another for when it's offThis will throw an error saying that sampler_albedo cannot be found. First, the spelling of the error is wrongas it'all lowercase when the variable is not. Note that this error will only be thrown on some platforms (windows)+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 compiledthe compiler strips the albedo texture because it's not being used.
  
-The reason for this is because DX9 associated samplers with texturesand 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. +**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: Possible workarounds:
-Give the normal map it'own sampler +  * 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) +  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:+  Make sure you always sample the texture associated with the sampler so it doesn't get stripped. Something like:
  
 +<code glsl>
 result *= saturate(1 + SAMPLE_TEXTURE2D_LOD(_Albedo, sampler_Albedo, float2(0,0), 11)); result *= saturate(1 + SAMPLE_TEXTURE2D_LOD(_Albedo, sampler_Albedo, float2(0,0), 11));
 return result; return result;
 +</code>
  
  
shadercompiler.1619969640.txt.gz · Last modified: 2021/05/02 15:34 by 69.30.226.234