Problems with vertex declarations

Started by
8 comments, last by Rompa 16 years, 10 months ago
I am trying to create/use a vertex declaration for use in a vertex shader, but I am having problems getting the declaration validated. My original declaration is as follows:

	D3DVERTEXELEMENT9 dwDecl[] =
	{
		{0, sizeof( float ) * 0, D3DDECLTYPE_FLOAT3,		D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, sizeof( float ) * 3, D3DDECLTYPE_FLOAT3,		D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		{0, sizeof( float ) * 6, D3DDECLTYPE_FLOAT3,		D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 1},
		{0, sizeof( float ) * 9, D3DDECLTYPE_FLOAT3,		D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 2},
		{0, sizeof( float ) * 12, D3DDECLTYPE_FLOAT3,		D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 3},
		{0, sizeof( float ) * 15, D3DDECLTYPE_D3DCOLOR,       D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
		D3DDECL_END()
	};
and here is the corresponding input struct in the vertex shader:

struct a2vConnector {
	float3 objCoord : POSITION;
	float3 texCoord : TEXCOORD;
	float3 objTangent : TANGENT0;
	float3 objBinormal : BINORMAL0;
	float3 objNormal : NORMAL;
	float4 color : COLOR;
};
To simplify things, I even tried to reduce the struct to just the POSITION variable and the declaration to just the D3DDECLUSAGE_POSITION, but it still is not getting validated, which leads me to believe that the problem lies somewhere else besides the syntax of my struct and declaration. I am also a bit confused about what exactly usage indices (the last parameter in each element of the vertex declaration) are, and the documentation online isnt very descriptive either, so additional help on this would be appreciated. Any ideas?
Advertisement
this is the struct i use to define my vertices:
const D3DVERTEXELEMENT9 CModelDeclFull[6] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 },
{ 0, 44, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0 },
D3DDECL_END()
};

The last parameter is used for example to define additional texture channels: assume you want 2 uv, one for diffuse and the other for lightmap/ambient occlusion, you should write something like this:

{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 0, 32, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
D3DDECL_END()

the second uv channel has usage index 1.

Hope this helps :)
i assume your variables in your shader look something like this?

struct a2vConnector {	float3 objCoord : POSITION;	float3 objNormal : NORMAL;	float2 texCoord : TEXCOORD;	float3 objTangent : TANGENT;	float3 objBinormal : BINORMAL;};


using your declaration with the above struct still returns 0 when i call

cgD3D9ValidateVertexDeclaration( cgVertexProgram, CModelDeclFull );

(forgot to mention i am using Cg)
Does the Direct3D Debug Runtime give you any insight into why it's failing to validate?

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

I'm new to the D3D debug runtime, but after turning the debug output level to max, it didnt give me any sort of messages...the

cgD3D9ValidateVertexDeclaration( cgVertexProgram, CModelDeclFull );

simply returns 0.
Quote:Original post by SubMatrix
I'm new to the D3D debug runtime, but after turning the debug output level to max, it didnt give me any sort of messages...
Sorry for the question, but I assume you hit the 'Debug Runtime' radio button in that control panel as well? If you're using the D3D Debug Runtime then you should at least get some "Direct3D is starting up" kind of messages in your debugger's Output window.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

yeah im using the debug version...

what i meant to say was that im not getting any messages related to that call in particular, but i AM getting other warnings and misc info messages for other parts of the program.
Ah, ok.

I'd try setting the usage indices on your tanget/binormal/normal to 0. Usage indices are for when you have multiple pieces of data for a given usage in a vertex - for example, two positions that you tween between in the vertex shader - the indices are used to differentiate between them (POSITION0 versus POSITION1, etc).

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Quote:Original post by SubMatrix
i assume your variables in your shader look something like this?

struct a2vConnector {	float3 objCoord : POSITION;	float3 objNormal : NORMAL;	float2 texCoord : TEXCOORD;	float3 objTangent : TANGENT;	float3 objBinormal : BINORMAL;};



This is the structure I have in the vertex shader:

struct vertexInput {
half3 Position : POSITION;
half3 Normal : NORMAL;
half2 UV : TEXCOORD0;
half3 tangent : TANGENT;
half3 binormal : BINORMAL;
};
You only use one tangent, one binormal (bitangent!) and one normal so your "usage index" should be zeros not 1, 2 and 3 respectively.
Did you mean to make the texcoord a float3?
Anyway, what does your vertex data structure actually look like, the one you use to populate the VB with?

This topic is closed to new replies.

Advertisement