I don't understand how semantics are used in shaders. While reading Unity's shader tutorials, I come across this

struct v2f {
    float worldPos: TEXCOORD0;
    half3 worldNormal: TEXCOORD1;
    float4 pos : SV_POSITION;

and also this

       struct v2f {
            float3 worldPos : TEXCOORD0;
            half3 tspace0 : TEXCOORD1;
            half3 tspace1 : TEXCOORD2;
            half3 tspace2 : TEXCOORD3;
            float2 uv : TEXCOORD4;
            float4 pos : SV_POSITION;

Microsoft's documentation says

A semantic is a string attached to a shader input or output that conveys information about the intended use of a parameter.

I know there is some kind of interpolation of data because vertices and pixels aren't one to one, thus I think semantics are used for telling the graphics library what to do about the data. But here it uses TEXCOORDX for things like world position and tangent space vectors. They don't seem to have anything to do with UV coordinates. So how are these semantics actully used?

  • \$\begingroup\$ "from some answers on Unity's forum they say Unity only supports two UV channels" this does not sound accurate based on my experience. :/ Have you tested this claim? \$\endgroup\$ – DMGregory Mar 8 '19 at 1:39
  • \$\begingroup\$ I'm editing my question, actually, I don't care about that, yet. I need to rephrase something. \$\endgroup\$ – Mingheng Wang Mar 8 '19 at 1:40

Keep in mind that the struct in HLSL is just a language convenience. It doesn't really mean anything in the hardware. You can use HLSL structs to group parameters to shaders and outputs from shaders. They can be used in constant buffers (either globally in the case of legacy Direct3D 9, or in constant buffers in Direct3D 10+ where there's actually some context to the grouping as a cbuffer).

These particular semantics are associated with the outputs of vertex shaders and the inputs of pixel shaders. You must have a SV_Position (or the older POSITION) as that's used to drive the rasterization and invocation of pixel shaders. The rest are just 'interpolators' that calculate smooth values between each vertex on the triangle when drawing. That includes NORMAL, TEXCOORD0, etc. They are often used by shaders with this convention, but there's nothing special about these values in most cases.

See Microsoft Docs

| improve this answer | |
  • \$\begingroup\$ Does the rasterizer interpolate all types of data with the same algorithm? For example, if I assign normal to data marked with "TEXCOORD0", will I get the correct result? \$\endgroup\$ – Mingheng Wang Mar 12 '19 at 12:21
  • \$\begingroup\$ In modern GPUs, they are all "general purpose" units that do the same interpolation. \$\endgroup\$ – Chuck Walbourn Mar 12 '19 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.