Missing something about vertex position in clip space

Ok so I’m trying to wrap my head around shaders. One very simple thing that I’ve read over and over again is that to get a vertex position in screen space you do

mul(UNITY_MATRIX_MVP, v.vertex);

and you should get a float4 with x and y ranging from -1 to 1.

When I do this I’m getting a value that ranges from 0 to screen width/height;

EX:

Shader "Unlit/VertexPositionShader"
{
    Properties
    {
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };
           
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                return o;
            }
           
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col;
                if (i.vertex.x > _ScreenParams.x/2)
                    col = 0;//black
                else
                    col = 1;//white
                return col;
            }
            ENDCG
        }
    }
}

The Result is
2614885--183440--ShaderIssue.png

Am I missing something here?

There’s some weirdness that happens with the SV_POSITION output from the vertex shader, specifically that value doesn’t necessarily get to the fragment shader unmodified. Most likely you’re getting the VPOS, which is the pixel coordinate the rasterization calculates from the SV_POSITION. If you want the clip space position you’ll want to add another interpolant to be passed from the vertex shader to the fragment shader, like float4 clipPos : TEXCOORD0;

Also, even then you’ll need to do clipPos.xy / clipPos.w to get it into -1 to 1 ranges. You should download the built in shader source and look at the functions for getting screen space positions like in the particle shaders for getting the depth texture UV.

Thanks a lot for the info. I will definitely take a look at some of the built in shader source code to get a better idea. Good to know I’m not totally missing something :smile: