Java doesn't have union types, so how does VkClearColorValue work?

Started by bcbradle, April 23, 2017, 03:18:22

Previous topic - Next topic

bcbradle

The vulkan specification states that VkClearColorValue is a union type between float, int, and unsigned int arrays.

Java doesn't have a union type so it can't expose that in the api. The lwjgl VkClearColorValue class offers getters and setters for each of float, int and unsigned int arrays.

How does this class decide which field to use (which field is used natively) if they have all been set?

spasi

The VkClearColorValue class does not decide anything. Like all struct classes, it's a wrapper over a pointer address + byte size. Because it's a union, the offset of all 3 members is zero and the byte size is 4 x 4 bytes (i.e. 16 bytes total, not 48).

How those bytes are interpreted is specified by Vulkan; it depends on the format of the image or attachment. The getters and setters with different types provide type-safety over the raw bytes. You can use those that match the format of the image or attachment that you need to clear to a specific color. Using value.float32(2, 3f) and value.int32(2, 3) will both write 4 bytes at offset 8 in the struct, but obviously different bytes will be written. There is no difference between int32 and uint32 in Java, but because of the properties of two's complement binary representation, you can use a Java int as a uint32_t.