Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SV_POSITION Semantic doubt

Discussion in 'Shaders' started by beatdesign, Jun 22, 2020.

  1. beatdesign

    beatdesign

    Joined:
    Apr 3, 2015
    Posts:
    137
    Hi,
    l'et's say we are taking into account a very simple shader, an unlit shader that simply displays vertices from object-space to screen-space.
    Vertex operation will then use UnityObjectToClipPos() and will have as output semantic SV_POSITION, right? That's quite simple, and every basic standard shader starts from here. But I can't figure out this dubt:

    From Microsoft shader Semantic guide (https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics), we read that SV_POSITION semantic is:
    Now, pixel location should be 2D coords in Screen Space, right?

    From built in shader functions Unity guide (https://docs.unity3d.com/Manual/SL-BuiltinFunctions.html) we can read that UnityObjectToClipPos() does this:
    Now, clip space homogeneus coordinates are 4D coords inside the camera view frustom.

    So.. we have a vertex shader that should have an output in Screen Space coordinates, but calculates it with a function that returns a value in homogeneous clip space coordinates.
    Where I am wrong?

    Thank you
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The issue is the HLSL documentation is describing what the SV_Position is when used as an input to the pixel shader stage. It doesn’t describe what it is during the vertex shader output. SV_Position is unique in that the value the vertex shader outputs is not the value the pixel shader receives. It’s a little odd that it doesn’t mention anywhere on that page that the vertex shader (or more specifically, last shader stage prior to rasterization, which happens just before the fragment / pixel shader) needs to output a homogeneous clip space position. During rasterization the GPU transforms that into window space, aka pixel position, which is what the pixel shader then receives.
     
    beatdesign likes this.
  3. beatdesign

    beatdesign

    Joined:
    Apr 3, 2015
    Posts:
    137
    Thank you!