是否允许使用 VkBool32 作为推送常量?
Is it allowed to use a VkBool32 as a push constant?
我正在尝试在我的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方面,官方规范始终是一个很好的起点,并且还包含有关如何处理推送常量及其差异的详细信息。
相关文章:
- 通过多个头文件使用常量变量
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 如何使用数据对象上的常量指针初始化类
- 在C++中使用非常量引用作为常量
- 为什么C++常量模板化向量在使用之前没有初始化?
- 使用自动推导的 lambda 参数作为常量表达式
- C4204:使用的非标准扩展:非常量聚合初始值设定项
- 在定义的函数中使用常量时出错
- 使用双指针传递 2D 常量数组
- C++/QT:使用指向私有成员的常量指针作为只读数据共享
- 是否可以使用 new 指定具有宏常量的动态分配数组的元素?
- C++ 在类中使用常量引用文本时 O2 内存泄漏
- 在 embarcadero / RAD 工作室中的 .dfm 文件中使用常量
- C++17 使用驱动类常量作为基类构造函数的参数来初始化基类构造函数
- 有没有办法声明一个公共静态常量,该常量将使用 constexpr 在源文件中定义(有什么区别)?
- 替换常量:何时使用静态 constexpr 和内联 constexpr
- 结构中的静态常量,使用构造函数
- 对非常量成员使用 constexpr 是否有效
- 根据编译时常量,使用相同的标识符 #define 或类型定义是否被认为是可接受的做法?