Search code examples
declarationhlslvertex

How to read vertex declaration from a shader?


How can I read my vertex declaration from a HLSL vertex shader? I mean this information:

struct VS_INPUT
{
    float4 position : POSITION; 
    float2 uv       : TEXCOORD;
    float4 color    : COLOR;
};

I tried IDirect3DDevice9::GetVertexDeclaration() and some other things, but couldn't get it to work. I need this information so I can know before a draw call that my shader handles the mesh it's trying to render.


Solution

  • With the methode SetVertexDeclaration or SetFVF you describe the format of the used Vertexbuffer in the next DrawCall. FVFs are sufficient for the most cases, but if you are using normalmapping for example you need tangents and binormals, which can only be described via a vertexdeclaration.

    A Shader has no vertexdeclaration or something like that. The struct only describes what the input of the vertexshader is expected to be. Therefore there is a mapping between the vertexdata and the shaderinput with the tags POSITION, TEXCOORD0, COLOR0, etc. If you mark something of your vertexdata with the tag "Position" the mapper will fill this data in the variable of the shaderstruct marked with POSITION. If there is no information in the vertexbuffer for a tag the value field is ambiguous. All data which exists but there is no tag in the shaderstruct will be omitted.

    The right way would be that youre looking which vertexdata you have for the next drawcall and choose the right shader, which expects the given input.