Drawing instanced meshes

Started by
1 comment, last by DXnut 17 years, 9 months ago
I tried to create hardware instancing, where the stream 1 data contains the instancing data, but when I draw the mesh (pMesh->DrawSubset(i)) it is drawn all over the screen. I am doing this for the draw:
pDevice->SetStreamSourceFreq(0, D3DSTREAMSOURCE_INDEXEDDATA | numInstances);
pDevice->SetStreamSourceFreq(1, D3DSTREAMSOURCE_INSTANCEDATA | 1ul);

// set declaration to include mesh vertex + stream 1 data
pDevice->SetVertexDeclaration(mHW_STREAM_Stream1.mVDecl);

for all subsets
pMesh->DrawSubset(i);


I suspect that this is not possible for a mesh object, since it will do an internal SetVertexDeclaration() call, and my stream 1 buffer will never make it to the shader (so the shader is getting zeroes or garbage for the SRT matrix data). So, I will have to revert to drawing it manually with DrawIndexedPrimitive for all of my subsets using the attribute buffer to draw each subset. Is this true? It will be a more work to draw the subsets manually this way, but at least I am optimizing the mesh first to make it easier. Thanks.
--------------------------Most of what I know came from Frank D. Luna's DirectX books
Advertisement
Have you checked the instancing demo/sample in the SDK? That should probably be your text-book example of what can/can't be done. If it avoids ID3DXMesh then, yeah, probably not compatible.

Also, re-implementing DrawSubset() isn't actually that difficult. In all truth, re-implementing much of ID3DXMesh wouldn't take more than a couple of days provided you were happy with some simple limitations (and/or knew what you wanted to use it for). Even the nice tricks like cloning and optimizing can be done via other D3DX calls ....

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Thanks. I am basing my changes on that SDK sample. Since they just create a simple 8 vertex box manually, I did not know if it would work with a mesh, but since I am adding this functionality to my mesh modeling program I figured I would try it. I got it to work with the Constants technique now, which has no changes to the vertex buffer. But for the Hardware and Stream techniques, that use stream 1 data, I think I am going to have to do it manually. Actually, it won't take me more than an hour or two to do it. I just wanted to know if it was possible to just use the DrawSubset functionality of the ID3DXMesh interface to do it, and I was missing something.

--------------------------Most of what I know came from Frank D. Luna's DirectX books

This topic is closed to new replies.

Advertisement