2

I am trying to create a VkBool32 in my C++ code:

VkBool32 myBool = VK_FALSE;

and push it to GLSL via a push constant:

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

which is recieved by a bool inside a uniform storage class:

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

First tests seem to work and have the intended behaviour. But is this permitted by the Vulkan Spec?

Brotcrunsher
  • 1,964
  • 10
  • 32
  • VkBool32 is just an uint32_t so You are just passing a 32-bit uint which is definitely possible. I don't remember how it looks like from the shader's side and it would be good to look at how the bool variable was converted inside the SPIR-V code. Maybe it's also just a 32-bit uint. I will look into it. – Ekzuzy Jul 30 '17 at 13:28

1 Answers1

4

Using bools for push constants is fine. There is nothing in the specs that prohibits this and I'v been using it in a few examples too.

If you take a look at the human-readable SPIR-V output you'll see that they're converted to 32 bit integers and thus are aligned to 32 bit:

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)

So if you e.g. would pass a struct containing multiple booleans you'd have to properly align (pad) on the CPU side before passing as a push constant.

As for the SPIR-V side of things, the official spec is always a good starting point and also contains details on how push constants are handled and how they differ.

Sascha Willems
  • 5,280
  • 1
  • 14
  • 21
  • Worth mentioning that care must be taken when pushing such a constant from CPU. If CPU struct looks like struct PC { bool calculateNormals; } and one does PC pc = { false }; followed by vkCmdPushConstants(cmd_buf, layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PC), &pc), byte 1, 2 and 3 of the push constant as seen by shader would be undefined. So even though byte 0 is 0, due to garbage in other bytes the 'boolean' might evaluate to true. – dbajgoric Jan 03 '23 at 19:59