-
Notifications
You must be signed in to change notification settings - Fork 518
Add flexible definition of 64 bit bitfield constants #2651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Add definitions `VK_USE_64_BIT_FLAGS_ENUMS`, `VK_BEGIN_ENUMERATION`, `VK_DEFINE_ENUM_VALUE` and `VK_END_ENUMERATION` to control definition of 64 bit enum-like constants
|
I'm not clear on how much type safety this would provide. There are relatively few places in the API where a single Vk*FlagBits value is passed around by type, compared to the places where bits are ORed together to generate a mask value (roughly 1:5, I think). Having the underlying enum type does not do much for likely sorts of errors with 32-bit masks today:
or even just
Would there be anything different about compiler behavior using the syntax you're suggesting for 64-bit flags? Also to note that today, I do not think there are any APIs accepting individual 64-bit flag bits - they are all masks of these bits. Of course that could change in the future and I may have missed something in a cursory grep of the XML. But in general, we introduce the 64-bit flag types when an existing 32-bit type has run out of bits, and to date those are all open-ended things like usage / access / creation / format flags, while the places where individual flag bits are passed around are mostly more constrained stuff like external memory handle types or resolve modes. Coincidentally we are currently looking at which flag types are likely to need new 64-bit types in the relatively near term, and they are also all usage / creation / format flags. |
|
Indeed this PR is not about type safety. Ensuring it is the points of Having the possibility of defining 64-bit bitfields as enums is more about consistency with their 32-bit counterpart. It also allows to have a dedicated types for template specialization in C++ (which is not possible for example whith |
|
Concerning the CI failures:
|
|
I also have an alternative version ready: #ifndef VK_BEGIN_ENUMERATION
#if VK_USE_64_BIT_FLAGS_ENUMS
#define VK_BEGIN_ENUMERATION(enum_name, underlying_type) typedef enum enum_name : underlying_type {
#else
#define VK_BEGIN_ENUMERATION(enum_name, underlying_type) typedef underlying_type enum_name; static const enum_name
#endif
#endif
#ifndef VK_END_ENUMERATION
#if VK_USE_64_BIT_FLAGS_ENUMS
#define VK_END_ENUMERATION(enum_name, underlying_type) } enum_name;
#else
#define VK_END_ENUMERATION(enum_name, underlying_type) _##enum_name##_CLOSING = 0;
#endif
#endif
// Flag bits for VkPipelineStageFlagBits2
VK_BEGIN_ENUMERATION(VkPipelineStageFlagBits2, VkFlags64)
VK_PIPELINE_STAGE_2_NONE = 0ULL,
VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT = 0x00000001ULL,
VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT = 0x00000002ULL,
VK_PIPELINE_STAGE_2_VERTEX_INPUT_BIT = 0x00000004ULL,
/// ....
VK_END_ENUMERATION(VkPipelineStageFlagBits2, VkFlags64)It removes macro
|
Currently, due to limitations of C99, 64-bit flags (such as
VkAccessFlagBits2) are defined as alias ofVkFlags64, and globalstatic constvariables. More recent standards (such as C++11 and C23) allow enumerations with an explicit underlying type.This PR proposes to add macros to define 64-bit flags in
vulkan_core.hin a flexible way, depending on the langage capabilities, and the application preferences:Declaration of 64-bit flags would become:
The above definition in C is still quite readable, but unfortunately a bit less than directly using enums or static constants.
Each individual macro of
VK_BEGIN_ENUMERATION,VK_DEFINE_ENUM_VALUEandVK_END_ENUMERATIONcan be defined by the application to suit its needs.An additional macro
VK_USE_64_BIT_FLAGS_ENUMSis defined as a shorcut to decide how the above macros are defined by default (similar to howVK_USE_64_BIT_PTR_DEFINESdecides the default definition ofVK_DEFINE_NON_DISPATCHABLE_HANDLE)This PR should not bring any API breaking change.
The only issue I would see is if a script parses
vulkan_core.hto extract bitfields values, it would probably break.I am open to feedback and suggestions.
Note:
In
appendices/boilerplate.adoc, I used version 338 for when the macros were added to the specification. This will need to be changed if this PR is accepted for a later version.