Search Unity

Question Strange compilation error URP Lit shader

Discussion in 'Universal Render Pipeline' started by GiorgioK, Apr 17, 2023.

  1. GiorgioK

    GiorgioK

    Joined:
    Oct 20, 2021
    Posts:
    34
    I am using Unity 2021.3.21f1 and URP 12.1.10
    I slightly modified the standard URP Lit shader. I managed to fix many compilation errors, but I can not fix one error for several days. Moreover, this error does not occur during normal compilation in the Uity editor - IT OCCURRS ONLY WHEN BUILDING THE WHOLE PROJECT.
    The error is like this:
    Shader error in 'GK/URP_Lit': Fragment program 'DepthNormalsFragment': Unrecognized sampler 'sampler_basemap' - does not match any texture and is not a recognized inline name (should contain filter and wrap modes). at line 266 (on d3d11)

    As you can see, the error is in the 'GK/URP_Lit' shader and is related to the 'DepthNormals' pass, specifically in the fragment program 'DepthNormalsFragment', which is located in the GK_URP_LitDepthNormalsPass.hlsl file at the very bottom.

    This is all clear. But there are questions.
    1) There is no 'sampler_basemap' sampler in this shader, there is only such 'sampler_BaseMap'.
    2) If line 93 :
    float3 normalTS = SampleNormal( uv, TEXTURE2D_ARGS(_NormalMap, sampler_BaseMap), _NormalMul );
    replace with this
    float3 normalTS = float3(0.0, 0.0, 1.0);
    then the ERROR DOES NOT occur, and a little higher in the DepthNormalsFragment() function there is line 77
    Alpha( SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff );
    it also uses sampler_BaseMap and there are no errors!
    3) Another 'ForwardLit' pass also uses the sampler_BaseMap sampler for _BaseMap, _NormalMap, _MetallicMap, _RoughnessMap textures and everything is fine - no errors.

    You can, of course, create your own sampler for _NormalMap and use it, but I want to figure out why sampler_BaseMap works for _NormalMap for one pass, but does not work for another pass.

    Also noticed one not clear moment.
    For the ForwardLit pass in the Varyings structure (file GK_URP_LitForwardPass.hlsl) for uv, TEXCOORD0 is used:
    float2 uv : TEXCOORD0;
    And for the DepthNormals pass in the Varyings structure (GK_URP_LitDepthNormalsPass.hlsl file) for uv, TEXCOORD1 is used:
    float2 uv : TEXCOORD1;
    Why?
     

    Attached Files:

  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,025
    Hi!
    This can happen if the compiler decides that the texture for this sampler is not used in the pass.
     
  3. GiorgioK

    GiorgioK

    Joined:
    Oct 20, 2021
    Posts:
    34
    At first I also thought that the compiler for some reason for the DepthNormals pass does not use the _BaseMap texture, and therefore the sampler_BaseMap, which is why this error occurs.
    But why is there an error like this:
    Code (CSharp):
    1. Alpha( SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff );
    2. float3 normalTS = SampleNormal( input.uv, TEXTURE2D_ARGS(_NormalMap, sampler_BaseMap), _NormalMul );
    and there is no error:
    Code (CSharp):
    1. Alpha( SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff );
    2. float3 normalTS = float3(0.0, 0.0, 1.0);
    I wrote about this in the first post in question number 2. Both variants use sampler_BaseMap.
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,025
    I think the second snippet doesn't use neither _BaseMap nor the sampler_BaseMap, so there's no errors.
     
  5. GiorgioK

    GiorgioK

    Joined:
    Oct 20, 2021
    Posts:
    34
    Maybe. We don't know exactly how the shader compiler works, so, as I wrote above, I will create another sampler_NormalMap sampler and use it.
    Thanks for the comments.