如何创建具有有效尺寸的常量缓冲区
晚上好,
我正在尝试将 XMFLOAT3X3 发送到常量缓冲区(请参阅下面的代码)。
ZeroMemory(&constDesc, sizeof(constDesc));
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth = sizeof(XMFLOAT3X3);
constDesc.Usage = D3D11_USAGE_DEFAULT;
result = m_pDevice->CreateBuffer(&constDesc,0,&m_pTexTransformCB);
if (FAILED(result)) {
MessageBoxA(NULL,"Error creating constant buffer m_pTexTransformCB", "Error", MB_OK);
return false;
}
但编译器告诉我,XMFLOAT3X3 对于常量缓冲区字节宽度来说是无效的维度:
D3D11: ERROR: ID3D11Device::CreateBuffer: The Dimensions are invalid. For ConstantBuffers, marked with the D3D11_BIND_CONSTANT_BUFFER BindFlag, the ByteWidth (value = 36) must be a multiple of 16 and be less than or equal to 65536. [ STATE_CREATION ERROR #66: CREATEBUFFER_INVALIDDIMENSIONS ]
但是,我对 HLSL 有点陌生,所以我不确定是否将字节宽度设置为 48,即着色器 cbuffer 中的 float3x3将正确注册。我应该如何最好地处理这个问题?
如果您需要更多信息,请发表评论,我将编辑问题。我希望它足够清楚。
Good evening,
I'm trying to send a XMFLOAT3X3 to a constant buffer (see code below).
ZeroMemory(&constDesc, sizeof(constDesc));
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth = sizeof(XMFLOAT3X3);
constDesc.Usage = D3D11_USAGE_DEFAULT;
result = m_pDevice->CreateBuffer(&constDesc,0,&m_pTexTransformCB);
if (FAILED(result)) {
MessageBoxA(NULL,"Error creating constant buffer m_pTexTransformCB", "Error", MB_OK);
return false;
}
But the compiler tells me that XMFLOAT3X3 is an invalid dimension for the constant buffer bytewidth:
D3D11: ERROR: ID3D11Device::CreateBuffer: The Dimensions are invalid. For ConstantBuffers, marked with the D3D11_BIND_CONSTANT_BUFFER BindFlag, the ByteWidth (value = 36) must be a multiple of 16 and be less than or equal to 65536. [ STATE_CREATION ERROR #66: CREATEBUFFER_INVALIDDIMENSIONS ]
However, I'm sort of new to HLSL, so I'm not sure if I set the bytewidth to 48, the float3x3 in the cbuffer of the shader will register properly. How should I handle this best?
If you need any more information, comment and I'll edit the question. I hope it's clear enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您的情况,您只需将 ByteWidth 更改为 48 就可以了。如果您想要将多个值组合在一起,则需要确保数据与 16 字节边界对齐。为此,您只需在定义结构之前添加 __declspec(align(16)) 即可。
这样您就可以使用 sizeof(Data) 并保证您的 ByteWidth 有效。很抱歉之前给您提供了错误的答案,您不需要手动填充。
In your case, you can just change the ByteWidth to 48 and it will be fine. If you want to group more than one value together, you're going to need to make sure that your data is aligned to 16 byte boundaries. To do this you can just add __declspec(align(16)) before defining your structures.
This way you can use sizeof(Data) and be guaranteed that your ByteWidth will be valid. I apologize for giving you an incorrect answer earlier, you do not need to manually pad.
“如果绑定标志为 D3D11_BIND_CONSTANT_BUFFER,则必须将 ByteWidth 值设置为 16 的倍数”
请参阅 MSDN
,特别是言论。
“If the bind flag is D3D11_BIND_CONSTANT_BUFFER, you must set the ByteWidth value in multiples of 16”
See the MSDN
,notably the remarks.