Skip to content

Commit 3fcc530

Browse files
authored
Fix profiles sample for Apple platforms (#1119)
* profiles sample: Enable VK_KHR_portability_enumeration and MoltenVK's Metal Argument Buffers for Apple platforms * Use VP_LUNARG_desktop_portability_2021_subset profile on portability platforms like macOS * Add VP_LUNARG_desktop_portability_2021_subset guard to support current iOS/macOS build configs * Pass address of extension struct vs. copy in std::any_of() predicate function
1 parent f0989c8 commit 3fcc530

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

samples/tooling/profiles/profiles.cpp

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@
3030
// The Vulkan Profiles library is part of the SDK and has been copied to the sample's folder for convenience
3131
#include "vulkan_profiles.hpp"
3232

33-
// This sample uses the VP_LUNARG_desktop_portability_2021 profile that defines feature sets for common desktop platforms with drivers supporting Vulkan 1.1 on Windows and Linux
34-
#define PROFILE_NAME VP_LUNARG_DESKTOP_PORTABILITY_2021_NAME
35-
#define PROFILE_SPEC_VERSION VP_LUNARG_DESKTOP_PORTABILITY_2021_SPEC_VERSION
33+
// This sample uses the VP_LUNARG_desktop_portability_2021 profile that defines feature sets for common desktop platforms with drivers supporting Vulkan 1.1 on Windows and Linux, and the VP_LUNARG_desktop_portability_2021_subset profile on portability platforms like macOS
34+
#if (defined(VKB_ENABLE_PORTABILITY) && defined(VP_LUNARG_desktop_portability_2021_subset))
35+
# define PROFILE_NAME VP_LUNARG_DESKTOP_PORTABILITY_2021_SUBSET_NAME
36+
# define PROFILE_SPEC_VERSION VP_LUNARG_DESKTOP_PORTABILITY_2021_SUBSET_SPEC_VERSION
37+
#else
38+
# define PROFILE_NAME VP_LUNARG_DESKTOP_PORTABILITY_2021_NAME
39+
# define PROFILE_SPEC_VERSION VP_LUNARG_DESKTOP_PORTABILITY_2021_SPEC_VERSION
40+
#endif
3641

3742
Profiles::Profiles()
3843
{
@@ -145,6 +150,55 @@ std::unique_ptr<vkb::Instance> Profiles::create_instance(bool headless)
145150
}
146151

147152
VkInstanceCreateInfo create_info{};
153+
154+
#if (defined(VKB_ENABLE_PORTABILITY))
155+
uint32_t instance_extension_count;
156+
VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr));
157+
std::vector<VkExtensionProperties> available_instance_extensions(instance_extension_count);
158+
VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, available_instance_extensions.data()));
159+
160+
// If VK_KHR_portability_enumeration is available at runtime, enable the extension and flag for instance creation
161+
if (std::any_of(available_instance_extensions.begin(),
162+
available_instance_extensions.end(),
163+
[](VkExtensionProperties const &extension) { return strcmp(extension.extensionName, VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME) == 0; }))
164+
{
165+
enabled_extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
166+
create_info.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
167+
}
168+
169+
# if defined(PLATFORM__MACOS) && TARGET_OS_OSX
170+
// On macOS use layer setting to configure MoltenVK for using Metal argument buffers (needed for descriptor indexing/scaling)
171+
VkLayerSettingEXT layerSetting{};
172+
const int32_t useMetalArgumentBuffers = 1;
173+
VkLayerSettingsCreateInfoEXT layerSettingsCreateInfo{};
174+
175+
if (std::any_of(available_instance_extensions.begin(),
176+
available_instance_extensions.end(),
177+
[](VkExtensionProperties const &extension) { return strcmp(extension.extensionName, VK_EXT_LAYER_SETTINGS_EXTENSION_NAME) == 0; }))
178+
{
179+
enabled_extensions.push_back(VK_EXT_LAYER_SETTINGS_EXTENSION_NAME);
180+
181+
layerSetting.pLayerName = "MoltenVK";
182+
layerSetting.pSettingName = "MVK_CONFIG_USE_METAL_ARGUMENT_BUFFERS";
183+
layerSetting.type = VK_LAYER_SETTING_TYPE_INT32_EXT;
184+
layerSetting.valueCount = 1;
185+
layerSetting.pValues = &useMetalArgumentBuffers;
186+
187+
layerSettingsCreateInfo.sType = VK_STRUCTURE_TYPE_LAYER_SETTINGS_CREATE_INFO_EXT;
188+
layerSettingsCreateInfo.settingCount = 1;
189+
layerSettingsCreateInfo.pSettings = &layerSetting;
190+
191+
create_info.pNext = &layerSettingsCreateInfo;
192+
}
193+
else
194+
{
195+
// If layer settings is not available at runtime, set macOS environment variable for support of older Vulkan SDKs
196+
// Will not work in batch mode, but is the best we can do short of using the deprecated MoltenVK private config API
197+
setenv("MVK_CONFIG_USE_METAL_ARGUMENT_BUFFERS", "1", 1);
198+
}
199+
# endif
200+
#endif
201+
148202
create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
149203
create_info.ppEnabledExtensionNames = enabled_extensions.data();
150204
create_info.enabledExtensionCount = static_cast<uint32_t>(enabled_extensions.size());

0 commit comments

Comments
 (0)