Адлюстраванне дынамічнага буфера ў Direct3D11 у праграме Windows Store (Mapping a dynamic buffer in Direct3D11 in a Windows Store App)


問題描述

Адлюстраванне дынамічнага буфера ў 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
};

(by DonnieDonnieJinxi)

參考文件

  1. Mapping a dynamic buffer in Direct3D11 in a Windows Store App (CC BY‑SA 3.0/4.0)

#direct3d11 #windows-runtime #C++






相關問題

Адлюстраванне дынамічнага буфера ў Direct3D11 у праграме Windows Store (Mapping a dynamic buffer in Direct3D11 in a Windows Store App)

未處理的異常@ClearRenderTargetView() (Unhandled exception @ ClearRenderTargetView())

將 3d 模型導入 Direct3D 11 示例 (Importing 3d model to Direct3D 11 Example)

編譯器不允許我使用“DDSTextureLoader.h”和“WICTextureLoader.h” (Compiler won't let me use "DDSTextureLoader.h" and "WICTextureLoader.h")

在我從舊版 DirectX SDK 更改為 Windows SDK (Visual Studio 2015) 後,鏈接器一直在抱怨 (The linker keeps complaining after I changed from legacy DirectX SDK to Windows SDK (Visual Studio 2015)))

鏈接器給出了我在我的應用程序中根本沒有使用過的函數的錯誤 (Linker gives errors about a function that i have not used in my application at all)

Direct3D 11 中輸出圖像的雙線性過濾 (Bilinear filtering of output image in Direct3D 11)

Direct3D 編程新手:11 與 12 (New to Direct3D programming: 11 vs 12)

如何在 Direct3D 11 上使用字體文件渲染文本? (How can I render text using font files on Direct3D 11?)

獲取 D3D 調試信息? (Get D3D debug information?)

為什麼 XMMatrixLookAtLH 會導致調試錯誤? (Why does XMMatrixLookAtLH result in a debug error?)

InterlockedAdd HLSL 潛在優化 (InterlockedAdd HLSL potential optimization)







留言討論