8

Looking through Sascha Willem's C++ Vulkan Demos hosted on GitHub, I noticed that some Functions returned the Datatype VkBool32.

I was curious to why Khronos didn't use a normal bool when I noticed the Line

typedef uint32_t VkBool32;

in vulkan.h. The uint32_t is defined as

typedef unsigned int uint32_t;

in stdint.h.

My Question is, why does it make Sense to throw away 3 Bytes if a standard Bool would do the Job with just one Byte? My little Recherche showed that there is next to no performance Difference (see Which is faster : if (bool) or if(int)?), and Khronos themselfes said that they wanted to minimize compatibility issues (in this case old C not having a primitive boolean Type) in Order to Focus on modern Code.

(See Trevett's Quote taken from here)

a ground-up redesign, we’re not backwards compatible

Community
  • 1
  • 1
Jan Hohenheim
  • 3,552
  • 2
  • 17
  • 42
  • 6
    It seems like you already answered your own question there – JustSid Feb 18 '16 at 17:01
  • "Khronos themselfes said that they wanted to minimize compatibility issues (in this case old C not having a primitive boolean Type)" – Mohamad Elghawi Feb 18 '16 at 17:05
  • 3
    "a standard Bool would do the Job with just one Byte" - unfounded assumption. You will find more than one compiler which has `sizeof(bool)==sizeof(int)`. – MSalters Feb 18 '16 at 17:25
  • The linked question is x86 specific. There are still (luckily) other architectures which may yield different results and have different sizes of `bool`. The standard does not require `sizeof(bool) == sizeof(char)`. And it is unlikely that it will be a _memory_ issue at all. Also, C **does** have a boolean type (`_Bool` with `stdbool.h` providing `bool` macro). – too honest for this site Feb 18 '16 at 17:25
  • @MSalters: "*You will find more than one compiler which has `sizeof(bool)==sizeof(int)`*" -- Example(s)? The standard doesn't say that `_Bool` is one byte, but it does have a smaller conversion rank than any other standard integer type, which *tends* to suggest it's going to be one byte. Or are you referring to some implementation-defined `bool` type rather than the standard `_Bool` type? – Keith Thompson Feb 18 '16 at 19:57
  • 1
    @Keith I meant the standard-defined `bool` instead of the implementation-defined `_Bool`, but that's what you get for tagging questions as C/C++. Anyway, see footnote 69 in the C++ standard which literally that it can be bigger. – MSalters Feb 18 '16 at 20:53
  • @MSalters: Footnote 69 in which edition? A section number would be more useful. In any case, I agree that `bool`/`_Bool` *may* be bigger than 1 bytes -- but are there any compilers that actually make it bigger? – Keith Thompson Feb 18 '16 at 21:23
  • @KeithThompson: I have encountered several compilers and targets where `sizeof(bool) = sizeof(int) = 4`. I cannot name them off the top of my head, but I could run a few experiments and get back to you. I remember when I found out that `sizeof(bool) = 1` on x86 that it was a bit "novel". – Dietrich Epp Feb 18 '16 at 21:26
  • @MSalters: `_Bool` is standard-defined, just like `bool`. What `bool` is to C++, `_Bool` is to C. – Dietrich Epp Feb 18 '16 at 21:27
  • @DietrichEpp: Yes, but `_Bool` was only introduced in C99 (along with ``, which has `#define _Bool bool`). Pre-C99 code would commonly define its own `bool` type, such as `typedef int bool;` or `typedef enum { false, true } bool;`. But as the comments have now made clear, we're all talking about the built-in type `_Bool` in C or `bool` in C++. – Keith Thompson Feb 18 '16 at 21:35
  • I think if you're using something as new and shiny as Vulkan, along with it's GL3-era hardware requirements, that it's unlikely that you're stuck using C90. Even MSVC has had `` for years now. – Dietrich Epp Feb 18 '16 at 21:44
  • @DietrichEpp: I've just found two myself: g++ 2.8.1 and 2.95.2 on SPARC/Solaris 9. (They're both pretty ancient, though.) – Keith Thompson Feb 18 '16 at 21:44

1 Answers1

13

Try printing sizeof(bool) on your system. Common answers are 4 or 1, and the value is by no means universal. You can get different answers depending on the compiler flags you are using.

Vulkan needs to work the same way on all systems, and it needs to work correctly no matter what compiler flags you are using to compile your program. If Vulkan was compiled with sizeof(bool) == 1 but you compile with sizeof(bool) == 4, the interface will be incorrect. I have personally witnessed this particular error happen.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415