問題描述
Адлюстраванне дынамічнага буфера ў Direct3D11 у праграме Windows Store (Mapping a dynamic buffer in Direct3D11 in a Windows Store App)
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.
‑‑‑‑‑
參考解法
方法 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).
方法 2:
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
};