   Hardware Instancing in DX
  I use hardware instancing in DX to create 100 trees, but only one tree appears. The instace has only position. The vertex format and declaration of tree mesh is:Quote:struct TreeVertex{D3DXVECTOR3 pos;D3DXVECTOR3 normal;D3DXVECTOR2 levelIndex;D3DXVECTOR3 offset;D3DXVECTOR3 texCoorType;}; D3DVERTEXELEMENT9 treeVertexDecl[] ={{ 0, 0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION, 0 },{ 0, 3 * 4,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL,0 },{ 0, 6 * 4,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0 },{ 0, 8 * 4,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,1 },{ 0, 11* 4,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,2 },D3DDECL_END()};The instance declaration is:Quote:struct TreeInstance {D3DXVECTOR3 worldPos;};static D3DVERTEXELEMENT9 declTreeInstance[]  ={{ 0, 0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION, 0 },{ 0, 3 * 4,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL,0 },{ 0, 6 * 4,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0 },{ 0, 8 * 4,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,1 },{ 0, 11* 4,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,2 },{ 1, 0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,3 },D3DDECL_END()};The setting code is :Quote:m_pD3DDevice->CreateVertexDeclaration( declTreeInstance, &m_pTreeDecl );m_pD3DDevice->SetVertexDeclaration(m_pTreeDecl);m_pD3DDevice->SetStreamSource(0, m_pTreeVB, 0, D3DXGetDeclVertexSize(declTreeInstance,0));m_pD3DDevice->SetStreamSourceFreq(0, D3DSTREAMSOURCE_INDEXEDDATA | 100);m_pD3DDevice->SetStreamSource(1, m_pTreeInstanceVB, 0, D3DXGetDeclVertexSize(declTreeInstance,1));m_pD3DDevice->SetStreamSourceFreq(1, D3DSTREAMSOURCE_INSTANCEDATA | 1UL);m_pD3DDevice->SetIndices(m_pTreeIB);where is wrong?   
  Do you draw 100x the triangle count of your tree? Does your shader use the additional position?   ;  In VS, vertex declaration is:Quote:struct VsInstancingInput{float3 Pos: POSITION;float3 Normal: NORMAL;float2 levelIndex  : TEXCOORD0;float3 offset : TEXCOORD1;float3 texCoorType: TEXCOORD2; float3 worldPos: TEXCOORD3;};struct VSInstancingOutput{float4 Pos: POSITION;float3 texCoorType : TEXCOORD0;};and in VS,I add postion of every instance to original tree position:Quote:output.Pos = output.Pos + float4(i.worldPos,0);;  And how many triangles do you draw?   ;Quote:Original post by SchrompfAnd how many triangles do you draw?I create one mesh,and draw all the triangles in the mesh:Quote:m_pTree->GetTreeMesh()->DrawSubset(0);m_pTree->GetTreeMesh()->DrawSubset(1);;  You probably need to draw them on your own, e.g. with IDirect3DDevice9::DrawIndexedPrimitive(). Because I don't know what setup D3DXMesh::DrawSubset() does internally. Maybe it overrides your vertex declaration or something similar.   ;Quote:Original post by SchrompfYou probably need to draw them on your own, e.g. with IDirect3DDevice9::DrawIndexedPrimitive(). Because I don't know what setup D3DXMesh::DrawSubset() does internally. Maybe it overrides your vertex declaration or something similar.Yeah, it's right. yesterday, I draw with DrawIndexedPrimitive and it works.Today, I changed to DrawSubset() it fails and I forgot this change. Thank you.   