Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Can't make SRP compatible shader that uses _ScreenParams

Discussion in 'Shaders' started by Shaggy21, May 19, 2024.

  1. Shaggy21

    Shaggy21

    Joined:
    Oct 2, 2014
    Posts:
    26
    Hey,
    I need to get the screen space position in a shader i wrote. I basically copied the logic from the old CG shaders:



    CBUFFER_START(UnityPerMaterial)
    uniform float4 _ScreenParams;
    uniform float4 _ProjectionParams;
    CBUFFER_END

    ...

    struct v2f
    {
    float2 uv : TEXCOORD0;
    float4 vertex : SV_POSITION;
    float4 screenPos : TEXCOORD1;
    };
    v2f vert (appdata v)
    {
    v2f o;
    o.vertex = UnityObjectToClipPosition(v.vertex, unity_ObjectToWorld, unity_MatrixVP);
    o.screenPos = ComputeScreenPos(o.vertex, _ScreenParams, _ProjectionParams);
    o.uv = v.uv;
    return o;
    }
    ...


    And this works, but then when I look at this shader from the inspector it says it is SRP incompatible because I did not add Properties for _ScreenParams and _ProjectionParams.

    However when I tried:


    Properties {
    ...
    [HideInInspector] _ScreenParams("_ScreenParams", Vector) = (0, 0, 0, 0)
    [HideInInspector] _ProjectionParams("_ProjectionParams", Vector) = (0, 0, 0, 0)
    }


    This seems to override Unity's builtin _ScreenParams and _ProjectionParams, and actually set 0 for all values.
    So it seems like a bug where we have to lose SRP compatibility to get Screen space positions. Is there a proper solution for this?
     
  2. Shaggy21

    Shaggy21

    Joined:
    Oct 2, 2014
    Posts:
    26
    My bad, I'm an idiot, I should've simply placed these buffers in


    CBUFFER_START(UnityPerFrame)
    float4x4 unity_MatrixVP;
    uniform float4 _ScreenParams;
    uniform float4 _ProjectionParams;
    CBUFFER_END


    And remove them from Properties. This is both SRP compatible and gets the required values (and obviously the proper buffer to get these values in since they don't change per instance)