1
\$\begingroup\$

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!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The issue seem to be that you don't copy data over, you're overwriting the idata pointer with the address of mIV:

idata = mIV; // this changes idata to point to mIV, not copy data

Should be something like:

for(size_t i=0; i < (sizeof(mIV) / sizeof(mIV[0])); ++i){
  idata[i] = mIV[i];
}

Or

memcpy(idata, mIV, sizeof(mIV));

Or some other copy function to copy the whole array. I'm assuming mIV is an array and not a pointer, if it is a pointer you need to use

for(size_t i=0; i < 4; ++i){
  idata[i] = mIV[i];
}

Ideally replace 4 with a constant or variable to keep track of the number of instances.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for that! Now I know ;D. Any other copy function still gives the same results unfortunately..... \$\endgroup\$ Commented Dec 7, 2014 at 14:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.