Vertex Index in Vertex Program

Hi,

How can I get a vertex number (or vertex index) inside vertex part of the shader?

You can use VertexID if your target platform supports it.

uint id : SV_VertexID

Otherwise, you’ll have to store the index as part of each vertex (as additional uv set for example).

1 Like

@Michal_1 interesting coz i didnt find this anywhere in the documentation.
So is it a part of appdata_full ?
or should I manually pass it
or is it already there? i can just grab it.
I have no idea because I’m using default Mesh and I dunno how exactly it works (i mean on the code side).

appdata_full probably doesn’t have it. You have to create your own input structure with vertexId and anything else you need. Something like:

struct myAppData
{
    float4 position : POSITION
    float3 normal : NORMAL;
    uint vertexId : SV_VertexID;
    ...
}
1 Like

@Michal_1 wow/ didnt know u can do it. thank you
and SV_VertexID will be automatically set if i use standard mesh right?

There are a lot of pixel shader and vertex shader input semantics not covered by Unity’s documentation, but is in Microsoft’s HLSL documentation.

However not all of those are accessible in Unity, and not all of them are properly converted to (or even have) OpenGL equivalents for desktop and mobile.

@bgolus but isnt it like mesh is giving all this stuff to shader ?
and since i dont have the source for mesh class - i have no idea which data is in shader in the end.

anyways / thank you / ill test it

Most of it comes through as part of the mesh through API calls to Direct3D, yes, but stuff like BINORMAL doesn’t have valid data in it since Unity’s mesh format doesn’t fill in that information since they reconstruct it in-shader. And BLENDINDICES and BLENDWEIGHT I believe are also unfilled because by the time the shader gets the mesh it’s been through a Geometry Shader StreamOut that pre-deforms and strips this data.

1 Like

SV_VertexID (and many others) is system-generated value. It is not part of the source mesh, it is generated by the gpu on the fly.