Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug HLSL preprocessor issue?

Discussion in '2023.1 Beta' started by n3b, May 23, 2023.

  1. n3b

    n3b

    Joined:
    Nov 16, 2014
    Posts:
    56
    Found some weird behavior in ShaderVariables.hlsl:615

    Code (CSharp):
    1. #define unity_ProbesOcclusion       LoadDOTSInstancedData_ProbesOcclusion()
    2. #ifdef PATH1
    3. float4 test(){return asfloat(0xFFFF);}
    4. #define unity_RenderingLayer        test()
    5. #else // PATH2
    6. const float4 unity_RenderingLayer = asfloat(0xFFFF);
    7. #endif
    8. #define unity_MotionVectorsParams   LoadDOTSInstancedData_MotionVectorsParams()
    9. #define unity_WorldTransformParams  LoadDOTSInstancedData_WorldTransformParams()
    10. #define unity_RendererBounds_Min    LoadDOTSInstancedData_RendererBounds_Min()
    11. #define unity_RendererBounds_Max    LoadDOTSInstancedData_RendererBounds_Max()
    I replaced LoadDOTSInstancedData_RenderingLayer() with test(). Whenever PATH1 is taken, all meshes become unlit. PATH2 works fine though.

    Tried the same in a simple shader and its ok too. Maybe happens only in compute?
    Code (CSharp):
    1.             float4 test()
    2.             {
    3.                 return float4(1, 0, 0, 0);
    4.             }
    5.  
    6.             float4 frag2(v2f i) : SV_Target
    7.             {
    8.                 #define TEST test()
    9.                 return float4(TEST.x, 0, 0, 1);
    10.             }
     
    Last edited: May 23, 2023
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,074
    It's highly unlikely that this is an issue with the preprocessor. Open the shader inspector for the shader you're testing things with and check the "Preprocess only" checkbox, then click "Compile and show code". It should show the result of the preprocessing.

    I would suggest making a test that is closer to the config you have in ShaderVariables.hlsl - with an include file, a const float4 and so on. This should help isolate the issue.
     
  3. n3b

    n3b

    Joined:
    Nov 16, 2014
    Posts:
    56
    Thanks for the tip! Indeed preprocessor works fine. The bug is here:
    Code (CSharp):
    1. asuint(asfloat(0xFFFF)) == 0xFFFF
    it returns false (at least on my hardware)
     
  4. n3b

    n3b

    Joined:
    Nov 16, 2014
    Posts:
    56
    yeah, this one does work
    Code (CSharp):
    1. uint4 LoadDOTSInstancedData_RenderingLayer()
    2. {
    3.     return 0x0001;
    4. }
    and this one doesn't
    Code (CSharp):
    1. float4 LoadDOTSInstancedData_RenderingLayer()
    2. {
    3.     return asfloat(0x0001);
    4. }
    considering that this one works too
    Code (CSharp):
    1. const float4 unity_RenderingLayer = asfloat(0xFFFF);
    it seems that asfloat returns different results at compile/run time