Search Unity

Bug Metal shader stripping removes fog for some multi_compile combination | Unity 2021.2.8

Discussion in 'Universal Render Pipeline' started by Newcomma, Jan 23, 2022.

  1. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Seeing very odd behaviour with SSAO and FOG and the use of multi compile.

    I have a v basic shader

    Code (CSharp):
    1. Shader "SSAO_FOGtest"
    2. {
    3.   SubShader
    4.     {
    5.         Tags {"RenderPipeline" = "UniversalPipeline" "RenderType"="Opaque" "Queue"="Geometry" }
    6.      
    7.         Pass
    8.         {            
    9.             Name "ForwardLit"
    10.  
    11.             ZTest LEqual
    12.             Blend One Zero
    13.  
    14.             HLSLPROGRAM
    15.  
    16.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE
    17.             #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
    18.             #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
    19.             #pragma multi_compile_fog
    20.  
    21.             #pragma vertex cs
    22.             #pragma fragment fs
    23.  
    24.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    25.  
    26.             struct Attributes
    27.             {
    28.                 float3 positionOS   : POSITION;
    29.                 half3 normalOS      : NORMAL;
    30.                 half4 tangentOS     : TANGENT;
    31.                 float2 uv           : TEXCOORD0;
    32.             };
    33.  
    34.             struct Varyings
    35.             {
    36.                 float4 positionWSAndFogFactor   : TEXCOORD1;
    37.                 float4 positionCS               : SV_POSITION;
    38.             };
    39.  
    40.             Varyings cs(Attributes input)
    41.             {
    42.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS);
    43.              
    44.                 Varyings output;
    45.                 output.positionCS = vertexInput.positionCS;
    46.                 output.positionWSAndFogFactor = float4(vertexInput.positionWS, ComputeFogFactor(vertexInput.positionCS.z));
    47.  
    48.                 return output;
    49.             }
    50.  
    51.             half4 fs(Varyings input) : SV_TARGET
    52.             {
    53.                 half3 col= MixFog(half3(1,1,1), input.positionWSAndFogFactor.w);
    54.                 return half4(col,1);
    55.             }
    56.          
    57.             ENDHLSL
    58.         }
    59.     }
    60. }
    61.  
    And these shader keywords combos

    Code (CSharp):
    1.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE
    2.             #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
    3.             #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
    4.             #pragma multi_compile_fog
    When I compile metal shader variants, no variants with both FOG_EXP2 and _SCREEN_SPACE_OCCLUSION set appear, i.e. they are stripped, even though they don't conflict.

    If I remove
    Code (CSharp):
    1. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE
    or

    Code (CSharp):
    1. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
    Doesn't matter which. Then recompiling the shader, FOG_EXP2 and SSAO appear in variant combinations that are compiled.

    I'm guessing that this is a bug in the Shader Stripping logic, does anyone know if there tend to be issues with Unity incorrectly stripping valid combinations?

    This hit me after upgrading and seeing missing shaders due to variants being stripped that are unexpected to be stripped.

    (Note changing fog stripping settings in project -> graphics doesn't change the behaviour)
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    Hi!
    Which Unity version are you using?
     
    Newcomma likes this.
  3. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Hey, sorry I put it in the title but should've included it in the post! :)

    2021.2.8
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    Are you sure you're not looking into vertex variants? The SSAO keyword is fragment only.
    What do you use to check the variants in the build?
     
    Newcomma likes this.
  5. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Via the unity shader compiler from the inspector and then checking the code output:
     

    Attached Files:

    Last edited: Jan 25, 2022
  6. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    To demo the gif, I made a fresh urp project in 2021, copied the OP shader code into a new shader file, compiled the metal version, and inspected the compiled code.

    Also just re-noting that this behaviour differs from 2020.3 where there are compiled variants mixing the two keywords.
     
  7. Juho_Oravainen

    Juho_Oravainen

    Unity Technologies

    Joined:
    Jun 5, 2014
    Posts:
    42
    Hey! I can verify that there is a bug :) Fortunately it's only affecting 2021.2. The fix is incoming.

    Thanks for reporting this!

    (Edit: Removed broken issue tracker link)
     
    Last edited: Jan 28, 2022
  8. equalsequals

    equalsequals

    Joined:
    Sep 27, 2010
    Posts:
    154
    Hit this exact issue this week. Not isolated to Metal, can confirm for D3D and GLES as well so I assume it doesn't matter which API.
     
  9. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Thanks for the update Juho! I can't follow that issue tracking link however? :(

    Screenshot 2022-01-28 at 12.38.43.png
     
  10. Juho_Oravainen

    Juho_Oravainen

    Unity Technologies

    Joined:
    Jun 5, 2014
    Posts:
    42
    Ah, sorry about the broken link. It appears that the public issue tracker entry was removed as I changed the internal bug case to be a backport instead of a bug in the mainline. Not sure how to fix it :(
    Anyhow, the bug fix is in the processing and hopefully lands asap. I'll poke this thread again when I know the exact version.
     
    Newcomma likes this.
  11. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Nice one! Out of curiosity will the fix land in the URP package, or is it a unity-core fix?
     
  12. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    It's a core fix
     
    Newcomma likes this.
  13. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,794
    Oops, just found this after I submitted the bug report.

    For what it's worth, it doesn't need some fancy keyword setup to replicate, I think just the existence of 3 more keywords triggers it.

    It also has nothing to do with URP. (I'm using built-in)

    In the submitted bug report I replicated it just by using the default unlit shader and:

    Code (CSharp):
    1.             #pragma multi_compile_fog
    2.             #pragma multi_compile _ A_KEYWORD
    3.             #pragma multi_compile _ OMG_A_KEYWORD
    4.             #pragma multi_compile _ ANOTHER_KEYWORD
     
  14. o1o101

    o1o101

    Joined:
    Jan 19, 2014
    Posts:
    639
    Is there any other work around for this besides removing all the multi compiles?
     
  15. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    IIRC the bug only shows up if the last multi_compile directive has more than two keywords, including the possible "_".
    Builtin shortcuts such as "multi_compile_fog" are added after the regular ones, which is why the repro by @AcidArrow is also triggering this.
     
  16. Juho_Oravainen

    Juho_Oravainen

    Unity Technologies

    Joined:
    Jun 5, 2014
    Posts:
    42
    The fix will be in 2021.2.12f1
     
    matt_cauldron likes this.
  17. GurhanH

    GurhanH

    Joined:
    Apr 24, 2017
    Posts:
    539
    @Juho_Oravainen

    There is also an issue with multi_compile_instancing in Unity versions 2021.2.8 and later. Procedural instancing shader variants are not included in builds. Will this also be fixed in 2021.2.12f1?
     
  18. Juho_Oravainen

    Juho_Oravainen

    Unity Technologies

    Joined:
    Jun 5, 2014
    Posts:
    42
    Any issues with shader variants missing from the build that started happening around 2021.2.8 is very likely due this same root cause and will be fixed in 2021.2.12f1
     
    Mixla and GurhanH like this.
  19. Sholms

    Sholms

    Joined:
    Nov 15, 2014
    Posts:
    85
    any date for that update?
     
    chadfranklin47 likes this.
  20. Juho_Oravainen

    Juho_Oravainen

    Unity Technologies

    Joined:
    Jun 5, 2014
    Posts:
    42
    It should be out Feb 16th if the release process goes smoothly.
     
  21. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    115
    Plz be smooth, strongly expect! If god exists plz make unity smooth!
     
    Last edited: Feb 17, 2022
  22. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    304
    I can only assume the process was not smooth? :)
     
    eggsamurai likes this.
  23. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    115
    sigh... definitely.
     
  24. Juho_Oravainen

    Juho_Oravainen

    Unity Technologies

    Joined:
    Jun 5, 2014
    Posts:
    42
    Either some bumps on the release process or maybe I read our release calendar badly... Sorry for the delay.
    In any case, 2021.2.12f1 is out now.
     
  25. matt_cauldron

    matt_cauldron

    Joined:
    Dec 17, 2021
    Posts:
    48
    I forgot to say last month, but thank you for keeping us all updated @Juho-Oravainen :)
     
    aleksandrk and Juho_Oravainen like this.
  26. sh0v0r

    sh0v0r

    Joined:
    Nov 29, 2010
    Posts:
    325