Search Unity

Shader Compiler "Missing" a Texture Sampler

Discussion in 'Shaders' started by space928, Jul 3, 2018.

  1. space928

    space928

    Joined:
    Jun 11, 2015
    Posts:
    21
    This is as weird as it sounds but for some reason in my shader, one of my texture samplers is not being included in the compiled shader with the code relevant to that sampler being omitted as well. I have tried changing the name of the sampler, adding and removing samplers, changing shader model, reformulating the expression, etc... Even if I use a different sampler on the 3 samples I do then that sampler will be omitted from the shader (and yes I have checked the shader assembly in RenderDoc). This is not without proof though so here is an excerpt of the code (I can't post the full thing for various reasons, but if I have time I'll try to make a minimal example):
    Code (CSharp):
    1. Shader "Galactic Studios/Ultra Paint Shader World" {
    2.     Properties {
    3.         [Header(Basic Settings)]
    4.         _Color ("Tint", Color) = (1,1,1,1)
    5.         _MainTex ("Albedo (RGB) Smoothness (A)", 2D) = "white" {}
    6. [...]
    7.         [NoScaleOffset]_Splat("Splat Map (RGB)", 2D) = "black" {}
    8.         [NoScaleOffset]_Splat2("Splat Map 2 (RGB)", 2D) = "black" {}
    9. [...]
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 400
    14.  
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.         #pragma surface surf Standard fullforwardshadows
    18.         #pragma multi_compile HEXPLANAR_SPLAT_ON TRIPLANAR_SPLAT_ON PLANAR_SPLAT_ON
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _MainTex;
    22.         sampler2D _NormalTex;
    23.         sampler2D _Splat;
    24.         sampler2D _Splat1;
    25.         sampler2D _Splat2;
    26.         sampler2D _SplatMask;
    27.         sampler2D _PaintTex;
    28.         sampler2D _PaintNrm;
    29.         sampler2D _MaskNrm;
    30.  
    31.         struct Input {
    32.             float2 uv_MainTex;
    33.             float2 uv_SplatMask;
    34.             float2 uv_PaintTex;
    35.             float3 worldPos;
    36.             float3 worldNormal;
    37.             INTERNAL_DATA
    38.         };
    39.  
    40.         [...]
    41.  
    42.         void surf (Input IN, inout SurfaceOutputStandard o) {
    43.             [...]
    44.             #if HEXPLANAR_SPLAT_ON
    45.                 float3 wn = smoothstep(.3,1,WorldNormalVector (IN, o.Normal));
    46.                 float3 wnr = -wn;
    47.                 wnr = saturate(wnr);
    48.                 fixed spltMap = 0.;
    49.  
    50.                 float3 wp = (IN.worldPos-_AABBPosOffset)/_AABBScale+.5;
    51.                 spltMap += tex2D (_Splat, wp.yz).x*saturate(wn.x);
    52.                 spltMap += tex2D (_Splat, wp.xz).y*saturate(wn.y);
    53.                 spltMap += tex2D (_Splat, wp.xy).z*saturate(wn.z);
    54.                
    55.                 spltMap += tex2D (_Splat2, wp.yz).x*wnr.x;
    56.                 spltMap += tex2D (_Splat2, wp.xz).y*wnr.y;
    57.                 spltMap += tex2D (_Splat2, wp.xy).z*wnr.z;
    58.                
    59.                 /*if(dot(wnr, 1)>0)  //Just some experiments to change how the compiler parsed it to mabe make it work. Not of it helped
    60.                 {
    61.                     fixed spltMaphex = tex2D (_Splat2, wp.yz).x*wnr.x + tex2D (_Splat2, wp.xz).y*wnr.y + tex2D (_Splat2, wp.xy).z*wnr.z;
    62. //                    spltMap += tex2D (_Splat2, wp.yz).x*wnr.x;
    63. //                    spltMap += tex2D (_Splat2, wp.xz).y*wnr.y;
    64. //                    spltMap += tex2D (_Splat2, wp.xy).z*wnr.z;
    65.                     spltMap += spltMaphex;
    66.                 }
    67.                 //spltMap += tex2D (_Splat1, wp.yz).x;
    68.                 //spltMap += tex2D (_Splat1, wp.xz).y;
    69.                 //spltMap += tex2D (_Splat1, wp.xy).z;*/
    70.             #endif
    71.                 [...]
    72.  
    73.             //Output struct
    74.             o.Albedo = finalCol.rgb;
    75.             o.Metallic = _Metallic;
    76.             o.Smoothness = lerp(_Glossiness*baseCol.a, _PaintGloss*paintCol.a, blendMask);
    77.             o.Normal = lerp(nrm, paintNrm, blendMask);
    78.             o.Alpha = baseCol.a;
    79.         }
    80.         ENDCG
    81.     }
    82.     FallBack "Diffuse"
    83.     CustomEditor "UltraPaintShaderEditor"
    84. }
    I'm sorry it's so messy but I've been at this for hours and I'm getting really frustrated with it. I'd appreciate any help (even workarounds) anyone has to offer and hope that maybe @Aras could have a look at it.
    Thanks a million,
    - Thomas
     
  2. space928

    space928

    Joined:
    Jun 11, 2015
    Posts:
    21
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
  4. space928

    space928

    Joined:
    Jun 11, 2015
    Posts:
    21
    Ugh, I feel so stupid, but let this be a lesson to anyone ever questioning the shader compiler. The shader compiler is never wrong. It turns out I was wrong and made a huge logic mistake. But let's look at where I went wrong:
    so the line:
    float3 wn = smoothstep(.3,1,WorldNormalVector (IN, o.Normal));

    Also clamps the normal between 0 and 1 so it can only be positive;
    float3 wnr = -wn;

    Means it can only be negative;
    wnr = saturate(wnr);

    Now that it's only negative, saturating it makes it always 0;
    This means that because I multiply that value by all the other samples:

    spltMap += tex2D (_Splat2, wp.yz).x*wnr.x;
    spltMap += tex2D (_Splat2, wp.xz).y*wnr.y;
    spltMap += tex2D (_Splat2, wp.xy).z*wnr.z;

    It's perfectly reasonable for the shader compiler to strip away the excess samplers. Once again the shader compiler has stumped me.
    But thanks to those who did take the time to look at it with me (even though it was to no avail).
    Lesson learnt,
    - Thomas