Shader semantic SV_POSITION and semantics meaning in general

Vertex shader needs an input structure in order to request the right data for each vertex. In the vertex input structure, semantics are essential, if I have:

struct vIn{
    float4 objPos : POSITION;
    float4 texcoord : TEXCOORD0;
}

this means: fill vertex with vertex obj position, texcoord with uv coords of uv set 0.

In the vertex output, semantics are not used in a strict way. Let’s say for example that I have as vertex output structure:

struct v2f{
    float4 ClipPos : SV_POSITION;
    float4 texcoord : TEXCOORD0;
}

this doesn’t mean that texcoord MUST be filled in with uv coords of uv set 0. I mean, it should, given the variable name and the semantic used, but I can also fill that value with everything that I want the rasterize interpolates from vertex to fragment. Is this right?
On the other way, I DO NEED to output Clip Space position in ClipPos, because of the semantic SV_POSITION. So, in other words, SV_POSITION is the only semantic in vertex output structure that oblige the content of the variable. Is this true? If yes, are there any other shader semantic that oblige the content of the variable when used in the vertex output structure?

It’s the only (common) semantic like that, yes. All others, the actual semantic doesn’t actually matter as internally they’re just getting assigned an interpolation index, and the data being passed is entirely arbitrary. The SV_POSITION is unique in that the value output from the vertex shader isn’t passes as is or interpolated to the fragment shader.

There are some other vertex shader output semantics that are used for things like multi-viewport instancing that exist, but these are going to be handled automatically by Unity via macros or some other compile time code modifications done in the background and aren’t anything you need to worry about.

1 Like

Thanks a lot as always, bgolus. I think this thing about vertex output semantics is one of the least documented things, and that they generate more confusion among those who start studying this subject!