是否允许使用 VkBool32 作为推送常量?

Is it allowed to use a VkBool32 as a push constant?

本文关键字:常量 许使用 VkBool32 是否      更新时间:2023-10-16

我正在尝试在我的C++代码中创建一个 VkBool32:

VkBool32 myBool = VK_FALSE;

并通过推送常量将其推送到 GLSL:

vkCmdPushConstants(..., sizeof(myBool), &myBool);

由统一存储类内的布尔值接收:

layout(push_constant) uniform PushConstants
{
bool myBool;
} pushConts;

第一次测试似乎有效并具有预期的行为。但这是否是 Vulkan 规范允许的?

对推送常量使用布尔值是可以的。规范中没有任何内容禁止这样做,我也一直在一些示例中使用它。

如果您查看人类可读的 SPIR-V 输出,您会发现它们被转换为 32 位整数,因此与 32 位对齐:

GLSL

layout (push_constant) uniform PushConsts {
bool calculateNormals;
} pushConsts;

SPIR-V

430(PushConsts):             TypeStruct 40(int)
431:             TypePointer PushConstant 430(PushConsts)
432(pushConsts):    431(ptr) Variable PushConstant
433:             TypePointer PushConstant 40(int)

因此,例如,如果您要传递一个包含多个布尔值的结构,则必须在作为推送常量传递之前在 CPU 端正确对齐(pad(。

至于SPIR-V方面,官方规范始终是一个很好的起点,并且还包含有关如何处理推送常量及其差异的详细信息。