我对HLSL D3D11中的CBuffer感到非常沮丧,因为它没有更新,在应用程序启动时设置了初始值,但是更新是不可行的,使用UpdateSubResource,还尝试了ID3D11DeviceContext::Map和ID3D11DeviceContext::UnMap。

注意:使用D3D11_USAGE_DYNAMIC和D3D11_CPU_ACCESS_WRITE设置CBuffer。

我的ID3D11Buffer(恒定缓冲区)仅在查询其大小时才返回4个字节....听起来像是问题的一部分

struct VS_CBUFFER_DATA
{
    XMFLOAT4X4 world;
    XMFLOAT4X4 view;
    XMFLOAT4X4 projection;

    VS_CBUFFER_DATA()
    {
        XMStoreFloat4x4(&world, DirectX::XMMatrixIdentity());
        XMStoreFloat4x4(&view, DirectX::XMMatrixIdentity());
        XMStoreFloat4x4(&projection, DirectX::XMMatrixIdentity());
    }
};

D3D11_BUFFER_DESC cbufferDesc;
memset(&cbufferDesc, 0, sizeof(cbufferDesc));

cbufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER
cbufferDesc.Usage = D3D11_USAGE_DYNAMIC;
cbufferDesc.ByteWidth = sizeof(m_CBufferData);
cbufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbufferDesc.MiscFlags = 0;
cbufferDesc.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA subdata;
memset(&subdata, 0, sizeof(subdata));

subdata.pSysMem = &m_CBufferData;

if (FAILED(pRendererTemp->CreateBuffer(cbufferDesc
{
    OutputDebugString("Failed to create CBuffer!")
    goto failed;
}

void Cube::UpdateViewProjection(__in const Renderer* pRenderer, __in const    XMFLOAT4X4   &view, __in const XMFLOAT4X4 &proj)
{
    D3D11_MAPPED_SUBRESOURCE mappedSubResource;
    memset(&mappedSubResource, 0, sizeof(mappedSubResource));

    if (SUCCEEDED(pRenderer->GetDevContext()->Map(m_pCBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE,  0, &mappedSubResource)))
    {
        Primitive::UpdateViewProjection(NULL, view, proj);
        VS_CBUFFER_DATA* cbData = (VS_CBUFFER_DATA*)&mappedSubResource.pData;

        memcpy(cbData, &m_CBufferData, sizeof(cbData));
        pRenderer->GetDevContext()->Unmap(m_pCBuffer, 0);
        SetBuffers(pRenderer);
    }
}

// SHADER.vsh
cbuffer cbMatrixBuffer : register(b0)
{
    float4x4 world;
    float4x4 view;
    float4x4 projection;
};

最佳答案

您的问题出在memcpy中。这行在这里:

memcpy(cbData, &m_CBufferData, sizeof(cbData));

正在复制的字节数等于VS_CBUFFER_DATA指针的大小,在32位系统上为4字节(x64为8字节)。该代码应显示为:
memcpy(cbData, &m_CBufferData, sizeof(VS_CBUFFER_DATA));

它将复制48个字节的数据,而不是4/8(无论sizeof(void*)计算结果如何)。

通常,避免使用sizeof查询数组的大小,尤其是避免使用指针(除非您有此需要)。

10-08 11:07