0

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.

Donnie
  • 45,732
  • 10
  • 64
  • 86

2 Answers2

1

Because the buffer was created with D3D11_USAGE_DYNAMIC, it had to be mapped with D3D_MAP_WRITE_DISCARD (or D3D_MAP_WRITE_NO_OVERWRITE, but that was inappropriate for my application).

Donnie
  • 45,732
  • 10
  • 64
  • 86
0

I had the same problem when I tried to create a buffer for a shader. At the createBuffer it would always give me E_INVALIDARG. The problem at my project was, that I forgot to align all the attributes to 16 Bytes. here is an example:

struct TessellationBufferType
{
    float tessellationAmount; //4bytes
    D3DXVECTOR3 cameraPosition; //12bytes
};

and if you don't get 16, add an additional variable (e.g padding) just to align up to 16:

struct LightBufferType
{
    D3DXVECTOR4 ambientColor; //16
    D3DXVECTOR4 diffuseColor; //16
    D3DXVECTOR3 lightDirection; //12
    float padding; //4
};
gordonk
  • 435
  • 3
  • 13
Jinxi
  • 303
  • 1
  • 13
  • Bytes, not bits. Also, you have to align between members if you have, for example, a series of VECTOR3s. – Donnie Feb 28 '13 at 03:22