0

Bellow 是我遇到问题的缓冲区的 cpu 端声明:

struct PSMaterialTransform {
        float Alpha;
        bool WrapUV;
        XMFLOAT2 padding;
    } static m_PSMaterialTransformDesc;

下面是缓冲区的 gpu 端声明:

cbuffer MaterialTransform: register(b1) {
float Alpha;
bool WrapUV;
float2 padding;
};

这就是我创建所有常量缓冲区的方式:

template<typename T>
bool Seraph::ShaderManager<T>::Create(ID3D11Device* pDevice) {
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(D3D11_BUFFER_DESC));

bd.ByteWidth = sizeof(T);
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

hr = pDevice->CreateBuffer(&bd, nullptr, &m_pBuffer);
if (FAILED(hr))
    return Log("Failed to create constant buffer.");
return true;
}

最后,这是我用来更新常量缓冲区的代码:

template<typename T>
bool Seraph::ShaderManager<T>::SetPS(ID3D11DeviceContext* pDeviceContext, T& Data, int ID) {
D3D11_MAPPED_SUBRESOURCE ms;
ZeroMemory(&ms, sizeof(D3D11_MAPPED_SUBRESOURCE));

hr = pDeviceContext->Map(m_pBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &ms);
if (FAILED(hr))
    return Log("Failed to update constant buffer.");

*(T*)ms.pData = Data;

pDeviceContext->Unmap(m_pBuffer, 0);
pDeviceContext->PSSetConstantBuffers(ID, 1, &m_pBuffer);

return true;
}

The variable ID represents the slot where the buffer is declared, I've already checked 3 times and the ID is 1 for MaterialTransform, but doesn't matter what value I send to the gpu, the boolean value WrapUV is always TRUE, even though the floating point Alpha seems to update normally.

4

1 回答 1

1

HLSL 中 bool 的大小为 4 个字节,因此您的 CPU 结构应该类似于

struct PSMaterialTransform 
{
        float Alpha;
        int32_t WrapUV; //or uint32_t
        XMFLOAT2 padding;
} 

如果你愿意,你可以像这样使用 typedef/using alias:

typedef int32_t bool32; //or
using bool32 = int32_t;

然后你可以改写bool32 WrapUV;

于 2021-09-12T16:29:41.020 回答