-2

I'm trying to draw some cubes with Hardware Instancing.


However they don't get displayed. So I debugged my code, and found the source of the problem:

In my VertexShader, the var that has the per instance world matrix has a value of:

{ 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, NaN, NaN, NaN }

The weirdest thing is that it outpus NaN (Not a Number). Here's how I create my instanced buffer:

XMMATRIX trans[4];

trans[0] = XMMatrixTranslation(0.0f, 0.0f, 0.0f);
trans[1] = XMMatrixTranslation(0.5f, 0.5f, 0.5f);
trans[2] = XMMatrixTranslation(-0.5, -0.5, 0.5);
trans[3] = XMMatrixTranslation(0.5, -0.5f, 0.5);

//Store world matrices
for (int i = 0; i < 4; i++)
    XMStoreFloat4x4(&mIV[i].world, trans[i]);

D3D11_BUFFER_DESC instDesc;
ZeroMemory(&instDesc, sizeof(D3D11_BUFFER_DESC));
instDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
instDesc.Usage = D3D11_USAGE_DYNAMIC;
instDesc.ByteWidth = sizeof(XMFLOAT4X4) * 4;
instDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

//Create instanced buffer
HR(mDevice->CreateBuffer(&instDesc, NULL, &mBoxInstB));

Here's how I map the instanced data to my buffer:

D3D11_MAPPED_SUBRESOURCE mapSub;
mContext->Map(mBoxInstB, 0, D3D11_MAP_WRITE_DISCARD, NULL, &mapSub);

VertexI* idata = reinterpret_cast<VertexI*>(mapSub.pData);

idata = mIV;

mContext->Unmap(mBoxInstB, 0);

Thanks for the help! If you need some more info, write it in the comments!

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • How do you transfer the contents of `mIV` to `mBoxInstB`? – MooseBoys Nov 30 '14 at 06:28
  • Have you: a) configured an input layout that contains the instance data in question, b) set that input layout to the vertex shader stage, and c) set the two buffers you're using to draw? Additionally, don't directly set the members of `XMMATRIX`, use one of the `XMMatrix*` methods or use `XMLoadFloat4x4`. – Alex Dec 01 '14 at 21:00
  • a) Yes I did, b) yes, c) yup. Ok I will try that thanks! – Rakete1111 Dec 02 '14 at 17:33
  • @Alex not still the same result ;(. Thanks anyway! – Rakete1111 Dec 02 '14 at 17:41
  • I'm not sure if this is the solution, so I'm putting it in a comment. But I noticed you're using `Map` and `Unmap` without the `D3D11_USAGE_DYNAMIC` flag set. This, IIRC, is undefined behavior. Try changing the usage to dynamic and adding the `D3D11_CPU_ACCESS_WRITE` flag to the `CPUAccessFlags` member of the `D3D11_BUFFER_DESC` structure. – Alex Dec 02 '14 at 23:12
  • Right... how could I forget that! ;D But not, it didn't change anything. It has to be somewhere else... – Rakete1111 Dec 03 '14 at 16:02
  • Why downvote? Could that person please explain what's wrong?? – Rakete1111 Dec 07 '14 at 12:51

1 Answers1

2

idata = mIV doesn't copy any data. What you want is memcpy(mapSub.pData, &mIV[0].world, instDesc.ByteWidth) assuming the world member is the entire contents of mIV[i] and is contiguous in memory.

MooseBoys
  • 6,641
  • 1
  • 19
  • 43
  • Good try, but no, the world matrix is still the same unfortunetely – Rakete1111 Dec 01 '14 at 18:35
  • @BlitzRakete You mention in the original question that the *per-instance* world matrix has bad values. Is this different or calculated indirectly from the values in `mBoxInstB`? Because `cbuffer`s don't vary per instance. How are you determining what the values are at shader execution time? – MooseBoys Dec 02 '14 at 17:44
  • I'm debugging my shaders with the Visual Studio Graphics Debugger. The values of `mBoxInstB` are initialized as seen on my original question. – Rakete1111 Dec 02 '14 at 18:32