I am new to Direct3D 11 and I am having some trouble understanding how to update constant (and other buffers) on a per-object basis. I some simple code where I am trying to get two Quads to draw to the screen, but at different positions. Here is the code I am using to draw them.
// ------------------------------------------------------------------------------------------------------------------------------
void QuadShape::UpdateBuffers(ID3D11DeviceContext* pContext)
{
// We need to know about our verts + set the constant buffers...
// NOTE: We only really need to do this when the buffer data actually changes...
XMMATRIX translate = XMMatrixTranspose(XMMatrixTranslation(X, Y, 0));
XMStoreFloat4x4(&ConstData.World, translate);
D3D11_MAPPED_SUBRESOURCE mappedResource;
ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
pContext->Map(ConstBuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
memcpy(mappedResource.pData, &ConstData, sizeof(ObjectConstBuffer));
pContext->Unmap(ConstBuf, 0);
}
// ------------------------------------------------------------------------------------------------------------------------------
void QuadShape::Draw(ID3D11DeviceContext* pContext)
{
UpdateBuffers(pContext);
pContext->DrawIndexed(_VertCount, _StartVert, 0);
}
You can see that I am computing a translation matrix based on the object's current X/Y position, and mapping that to the object's constant buffer, denoted as 'ConstBuf'. The problem that I am having is coming from the fact that all of the quads are ending up being drawn at the same position even though I have verified that the matrices computed for each are indeed different, and that the buffers are indeed dynamic.
I am guessing that what is happening is that the mapped resource is just being overwritten with whatever the last matrix is, but I thought that MAP_WRITE_DISCARD was supposed to avoid this. I am confused, how can I use a different constant buffer per object, and get them to show up at a different position?