Search Unity

_ScreenParams not giving correct size?

Discussion in 'Shaders' started by mgear, Oct 25, 2018.

  1. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    Did _ScreenParams stopped working somewhere?

    Cant remember when i used it last time,
    but now getting these values, instead of correct size:
    upload_2018-10-25_16-53-26.png

    tested on 5.6.6.x and 2017.3.f1
     
  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    upload_2018-10-25_14-7-44.png

    _ScreenParams works great. but values are inaccurate in profiler.

    Test shader:

    Code (CSharp):
    1. Shader "ScreenParams"
    2. {
    3.     SubShader
    4.     {
    5.         Pass
    6.         {
    7.             Cull Off
    8.             CGPROGRAM
    9.             #pragma vertex V
    10.             #pragma fragment P
    11.            
    12.             void V(uint i:SV_VertexID,out half4 c:SV_POSITION) {c=half4((i<<1&2)*2-1.,1-2.*(i&2),1,1);}
    13.            
    14.             float4 P (float4 vertex:SV_POSITION) : SV_TARGET
    15.             {
    16.                 float2 uv = vertex.xy/_ScreenParams.xy;
    17.                 return float4(lerp(1..xxxx,0..xxxx,step(max(uv.x,uv.y),0.5)));
    18.             }
    19.             ENDCG
    20.         }
    21.     }
    22. }
     
    mgear likes this.
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    oh i see.. that does work,
    was expecting to see different values, and i feel like it did display actual pixel width and height for .xy there before?

    from docs:
    Code (CSharp):
    1. _ScreenParams float4 x is the width of the camera’s target texture in pixels
    2. , y is the height of the camera’s target texture in pixels, z is 1.0 + 1.0/width and w is 1.0 + 1.0/height.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    @Przemyslaw_Zaworski 's example shows:
    _ScreenParams (1.3e+03, 7.9e+02, 1, 1)
    And the resolution is 1307x788

    Those first two values are exponential notation.

    1.3e+03 = 1300 ~= 1307
    7.9e+02 = 790 ~= 788

    1 ~= 1.00076511 = 1.0 + 1.0 / 1307
    1 ~= 1.001269 = 1.0 + 1.0 / 788


    The frame debugger's values are sometimes being limited to two significant digits, and rounded up to the smallest significant digit. For values like "0.9999" or "1.001269" that's going to get displayed as just "1".
    In the case of 1307, limiting to two significant digits means using exponential notation for numbers >99, so it is going to be converted into 1.307e+03, and then rounded to 1.3e+03.

    So, really, the documentation isn't wrong, and the frame debugger is showing you those values, just not in the form you would expect.

    This comes down to some asinine reason they're sometimes formating the numerical strings using "g2" (limit to 2 digits), and sometimes "g7" (limit to 7 digits). The rhyme or reason for why escapes me. Curiously, if you copy the values from the frame debugger it'll always use "g7".
     
    Last edited: Oct 26, 2018
    AA-Matt and mgear like this.
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    ohno..i stopped reading as soon as i saw the exp. char there : ] thanks!

    i though i was getting exact 1 for both .zw while using it for scaling billboards:
    Code (CSharp):
    1.                float width = _Size*(_ScreenParams.z-1);
    2.                float height = _Size*(_ScreenParams.w-1);
    but actually size was just too small, so i didnt see anything on screen ><
     
  6. svizcay

    svizcay

    Joined:
    Oct 26, 2015
    Posts:
    28
    btw, when using Graphics.SetRenderTarget() and passing a RenderTexture with a certain resolution, I was expecting _ScreenParams.xy to store the resolution of the current attached render target, but it gives back the resolution of the "game viewport".
     
    Fewes likes this.