[solved] Instancing and HLSL, C++

Started by
2 comments, last by MJP 16 years, 6 months ago
Hi, I'm adding streaming instancing support to my graphics engine and I have a couple of problems. I'm basing my code on the DirectX Hardware instancing demo from the SDK. The first problem is my vertex buffer, my buffer has vertex position, normals, colour, specular and texture coorinates.

struct MYVERTEX
{
	float x, y, z;
	float nx,ny,nz;
	DWORD Col;
	DWORD Spec;
	float tu, tv;

} ;


the example from the sdk has Position, normal and texture coordinates. I'm trying to create the vertex declaration for my vertex buffer..

D3DVERTEXELEMENT9 g_VertexElemHardware[] = 
{
    { 0, 0,     D3DDECLTYPE_FLOAT3,     D3DDECLMETHOD_DEFAULT,  D3DDECLUSAGE_POSITION,  0 },// Size 12
    { 0, 12,	D3DDECLTYPE_FLOAT3,     D3DDECLMETHOD_DEFAULT,  D3DDECLUSAGE_NORMAL,    0 },// Size 12
    { 0, 24,    D3DDECLTYPE_D3DCOLOR,   D3DDECLMETHOD_DEFAULT,  D3DDECLUSAGE_COLOR,     0 },// Size 4
    { 0, 28,    D3DDECLTYPE_D3DCOLOR,   D3DDECLMETHOD_DEFAULT,  D3DDECLUSAGE_COLOR,     1 },// Size 4
    { 0, 32,	D3DDECLTYPE_FLOAT2,     D3DDECLMETHOD_DEFAULT,  D3DDECLUSAGE_TEXCOORD,  0 },// Size 12
    { 1, 0,     D3DDECLTYPE_D3DCOLOR,   D3DDECLMETHOD_DEFAULT,  D3DDECLUSAGE_COLOR,     0 },// Size 4
    { 1, 4,     D3DDECLTYPE_D3DCOLOR,   D3DDECLMETHOD_DEFAULT,  D3DDECLUSAGE_COLOR,     1 },// Size 4
    D3DDECL_END()
};
	hr = m_pD3DDevice->CreateVertexDeclaration( g_VertexElemHardware, &g_pVertexDeclHardware );
	if (hr == S_OK)
		Instancing = true;
	else
		Instancing = false;



my first problem is that CreateVertexDeclaration fails and I don't know why, the second problem is with the .fx file.

void VS_HWInstancing( 			float4 vPos : POSITION,
					float3 vNormal : NORMAL,
					float4 difCol : COLOR0,
					float4 SpecCol : COLOR0,
					float2 vTex0 : TEXCOORD0,
					float4 vColor : COLOR0,
					float4 vBoxInstance : COLOR1,
					out float4 oPos : POSITION,
					out float4 oColor : COLOR0,
					out float2 oTex0 : TEXCOORD0 )
this also fails to compile? what am I doing wrong? [Edited by - darrenmarklines on October 11, 2007 7:03:13 AM]
Advertisement
When a DirectX function fails, and you're running the debug runtimes, it outputs a detailed explaination of why it failed to VS's Output window.

Turn on the DirectX Debug Runtimes (Start -> Programs -> DirectX -> Utilities -> DirectX Control Panel).

That should provide you with the details of why the call failed, which will aid you in finding what has gone wrong.

This should allow you to easily detect both why the .fx file won't compile, and why CreateVertexDeclaration fails.

For FX files, you can also use FXC.exe to compile the file from the command line, and get the compilation errors.

If you have trouble getting this debug info, or using it, add a reply and someone will help you with it.
Sirob Yes.» - status: Work-O-Rama.
that really helped, thanks.

The CreateVertexDeclaration was failing because of lots of D3DDECLUSAGE_COLOR parameters, you need to pass D3DDECLUSAGE_COLOR, 0 for the first, D3DDECLUSAGE_COLOR, 1 for the 2nd etc.

the same thing applies to the .fx file, COLOR0 for the 1st, COLOR1 the 2nd etc.

I now have 1000 instances of my mesh at hardly any perfomance cost. Now to try and get those colour paramters implemented. Anyone know how to deal with specular colour in a shader?
Quote:Original post by darrenmarklines
Anyone know how to deal with specular colour in a shader?


Are you talking about the lighting equations for specular the specular component? Those are detailed in this article. Shouldn't be too hard for you to implement them in HLSL.

This topic is closed to new replies.

Advertisement