# Mapping dynamic buffers in Direct3D11 in Windows Store apps

I'm trying to make instanced geometry in Direct3D11, and the ID3D11DeviceContext1->Map() call is failing with the very helpful error of "Invalid Parameter" when I'm attempting to update the instance buffer.

The buffer is declared as a member variable:

Microsoft::WRL::ComPtr<ID3D11Buffer> m_instanceBuffer;

Then I create it (which succeeds):

D3D11_BUFFER_DESC instanceDesc;
ZeroMemory(&instanceDesc, sizeof(D3D11_BUFFER_DESC));
instanceDesc.Usage = D3D11_USAGE_DYNAMIC;
instanceDesc.ByteWidth = sizeof(InstanceData) * MAX_INSTANCE_COUNT;
instanceDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
instanceDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
instanceDesc.MiscFlags = 0;
instanceDesc.StructureByteStride = 0;

DX::ThrowIfFailed(d3dDevice->CreateBuffer(&instanceDesc, NULL, &m_instanceBuffer));


However, when I try to map it:

D3D11_MAPPED_SUBRESOURCE inst;
DX::ThrowIfFailed(d3dContext->Map(m_instanceBuffer.Get(), 0, D3D11_MAP_WRITE, 0, &inst));


The map call fails with E_INVALIDARG. Nothing is NULL incorrectly, and this being one of my first D3D apps I'm currently stumped on what to do next to track it down. I'm thinking I must be creating the buffer incorrectly, but I can't see how. Any input would be appreciated.

• Have you tried turning on the debug layer when you create the device, and see if you get any interesting warning messages? The messages will appear in Visual Studio's Output pane, if you run your app in the debugger. Nov 29, 2012 at 5:45
• Thanks, that pointed out the error immediately. I had read about the debug layer, then forgot about it. Won't again. Nov 29, 2012 at 6:50

@Nathan's debug layer tip pointed this out fast - so, turn on the debug layer when debugging D3D code.

The problem was that the MapType needed to be D3D11_MAP_WRITE_DISCARD (or D3D11_MAP_WRITE_NO_OVERWRITE, but that wasn't appropriate for me) because the buffer was created as BUFFER_DYNAMIC.

I'm sure that was documented somewhere, but I completely missed it.