Skip to content

Commit 2052ba5

Browse files
vk_compute_demo: replace usage guards with #ifndef fallback defines
The #if defined(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME) guards fixed the compile error on old Vulkan SDK headers but silently removed the runtime portability detection on those headers — breaking macOS / MoltenVK on any machine with an SDK older than 1.3.x. The correct fix is to define local fallback constants when the header doesn't provide them. Both values are fixed by the Vulkan spec, so hardcoding them as fallbacks is safe across all SDK versions. The runtime detection and flag assignment are now always compiled and always run; on old-header Linux the strcmp never matches at runtime (the extension isn't present there), so hasPortabilityEnum stays false and behaviour is unchanged. Also removes the (void)hasPortabilityEnum bandaid that the usage guards had introduced.
1 parent ca0518d commit 2052ba5

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

examples/shader-coverage-bvh-traversal/vk_compute_demo.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
#include <cstring>
88

9+
// VK_KHR_portability_enumeration was introduced in Vulkan SDK 1.3.x headers.
10+
// Define string and flag fallbacks so the runtime detection below compiles
11+
// against any SDK version; both values are fixed by the Vulkan spec.
12+
#ifndef VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME
13+
#define VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME "VK_KHR_portability_enumeration"
14+
#endif
15+
#ifndef VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR
16+
#define VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR ((VkInstanceCreateFlagBits)0x00000001)
17+
#endif
18+
919
namespace vkdemo
1020
{
1121

@@ -18,10 +28,6 @@ void Context::init(bool requireInt64Atomics)
1828
std::vector<VkExtensionProperties> props(propertyCount);
1929
vkEnumerateInstanceExtensionProperties(nullptr, &propertyCount, props.data());
2030
bool hasPortabilityEnum = false;
21-
// VK_KHR_portability_enumeration is only defined by Vulkan headers >= 1.3.x.
22-
// Guard the references so older headers (e.g. on some Linux CI runners) still
23-
// compile; the runtime detection runs wherever the extension is available.
24-
#if defined(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME)
2531
for (auto& p : props)
2632
{
2733
if (std::strcmp(p.extensionName, VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME) == 0)
@@ -31,7 +37,6 @@ void Context::init(bool requireInt64Atomics)
3137
break;
3238
}
3339
}
34-
#endif
3540

3641
VkApplicationInfo appInfo = {};
3742
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
@@ -50,11 +55,8 @@ void Context::init(bool requireInt64Atomics)
5055
ci.pApplicationInfo = &appInfo;
5156
ci.enabledExtensionCount = (uint32_t)instanceExts.size();
5257
ci.ppEnabledExtensionNames = instanceExts.data();
53-
#if defined(VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR)
5458
if (hasPortabilityEnum)
5559
ci.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
56-
#endif
57-
(void)hasPortabilityEnum; // unused when the portability headers are absent
5860
check(vkCreateInstance(&ci, nullptr, &instance), "vkCreateInstance");
5961

6062
// Pick a physical device with a compute-capable queue family. When 64-bit

examples/shader-coverage-image-pipeline/vk_compute_demo.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
#include <cstring>
88

9+
// VK_KHR_portability_enumeration was introduced in Vulkan SDK 1.3.x headers.
10+
// Define string and flag fallbacks so the runtime detection below compiles
11+
// against any SDK version; both values are fixed by the Vulkan spec.
12+
#ifndef VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME
13+
#define VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME "VK_KHR_portability_enumeration"
14+
#endif
15+
#ifndef VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR
16+
#define VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR ((VkInstanceCreateFlagBits)0x00000001)
17+
#endif
18+
919
namespace vkdemo
1020
{
1121

@@ -18,10 +28,6 @@ void Context::init(bool requireInt64Atomics)
1828
std::vector<VkExtensionProperties> props(propertyCount);
1929
vkEnumerateInstanceExtensionProperties(nullptr, &propertyCount, props.data());
2030
bool hasPortabilityEnum = false;
21-
// VK_KHR_portability_enumeration is only defined by Vulkan headers >= 1.3.x.
22-
// Guard the references so older headers (e.g. on some Linux CI runners) still
23-
// compile; the runtime detection runs wherever the extension is available.
24-
#if defined(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME)
2531
for (auto& p : props)
2632
{
2733
if (std::strcmp(p.extensionName, VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME) == 0)
@@ -31,7 +37,6 @@ void Context::init(bool requireInt64Atomics)
3137
break;
3238
}
3339
}
34-
#endif
3540

3641
VkApplicationInfo appInfo = {};
3742
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
@@ -50,11 +55,8 @@ void Context::init(bool requireInt64Atomics)
5055
ci.pApplicationInfo = &appInfo;
5156
ci.enabledExtensionCount = (uint32_t)instanceExts.size();
5257
ci.ppEnabledExtensionNames = instanceExts.data();
53-
#if defined(VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR)
5458
if (hasPortabilityEnum)
5559
ci.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
56-
#endif
57-
(void)hasPortabilityEnum; // unused when the portability headers are absent
5860
check(vkCreateInstance(&ci, nullptr, &instance), "vkCreateInstance");
5961

6062
// Pick a physical device with a compute-capable queue family. When 64-bit

0 commit comments

Comments
 (0)