Skip to content

Commit

Permalink
[ET-VK][mac] Allow vulkan binaries to work with Vulkan SDK + fixes to…
Browse files Browse the repository at this point in the history
… enable debugPrintf extension

## Context

Add a buck config option to prevent linking against MoltenVK when building for MacOS, instead opting to use volk. This is useful for seeing debug print statments from GLSL code.

Also implement some fixes to make things work when using system-installed Vulkan SDK, and some fixes to enable using debug Printf extension on Mac.

Specifically, the fixes are as follows:

* On newer versions of the Vulkan SDK, a portability instance extension needs to be explicitly enabled, otherwise creating the Vulkan instance will complain about the driver being incompatible. [source](https://stackoverflow.com/questions/58732459/vk-error-incompatible-driver-with-mac-os-and-vulkan-moltenvk)
* VkConfig on Mac doesn't show options to enable debugprintf, therefore it has to be enabled programmatically.

Differential Revision: [D68650936](https://our.internmc.facebook.com/intern/diff/D68650936/)

ghstack-source-id: 263006772
Pull Request resolved: #7957

Co-authored-by: Stephen Jia <[email protected]>
  • Loading branch information
pytorchbot and SS-JIA authored Jan 27, 2025
1 parent 2170a84 commit 06ef713
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
54 changes: 39 additions & 15 deletions backends/vulkan/runtime/vk_api/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,52 @@ VkInstance create_instance(const RuntimeConfig& config) {
std::vector<const char*> enabled_layers;
std::vector<const char*> enabled_extensions;

std::vector<const char*> requested_layers;
std::vector<const char*> requested_extensions;

if (config.enable_validation_messages) {
std::vector<const char*> requested_layers{
// "VK_LAYER_LUNARG_api_dump",
"VK_LAYER_KHRONOS_validation",
};
std::vector<const char*> requested_extensions{
requested_layers.emplace_back("VK_LAYER_KHRONOS_validation");
#ifdef VK_EXT_debug_report
VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
requested_extensions.emplace_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
#endif /* VK_EXT_debug_report */
};

find_requested_layers_and_extensions(
enabled_layers,
enabled_extensions,
requested_layers,
requested_extensions);
}

VkInstanceCreateFlags instance_flags = 0;
#ifdef __APPLE__
instance_flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
requested_extensions.emplace_back(
VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
#endif

find_requested_layers_and_extensions(
enabled_layers,
enabled_extensions,
requested_layers,
requested_extensions);

const void* instance_create_next = nullptr;
// VkConfig on Mac platforms does not expose debugPrintf settings for whatever
// reason so it has to be enabled manually.
#if defined(__APPLE__) && defined(VULKAN_DEBUG)
std::vector<VkValidationFeatureEnableEXT> enabled_validation_features{
VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT,
};
VkValidationFeaturesEXT validation_features = {
VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT, // sType
nullptr, // pNext
static_cast<uint32_t>(
enabled_validation_features.size()), // enabledValidationFeatureCount
enabled_validation_features.data(), // pEnabledValidationFeatures
0,
nullptr, // pDisabledValidationFeatures
};
instance_create_next = &validation_features;
#endif /* __APPLE__ && VULKAN_DEBUG */

const VkInstanceCreateInfo instance_create_info{
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
nullptr, // pNext
0u, // flags
instance_create_next, // pNext
instance_flags, // flags
&application_info, // pApplicationInfo
static_cast<uint32_t>(enabled_layers.size()), // enabledLayerCount
enabled_layers.data(), // ppEnabledLayerNames
Expand Down
20 changes: 16 additions & 4 deletions backends/vulkan/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,31 @@ def define_common_targets(is_fbcode = False):
"fbsource//third-party/swiftshader/lib/linux-x64:libvk_swiftshader_so",
]
else:
link_moltenvk = read_config("etvk", "link_moltenvk", "1") == "1"
mac_deps = default_deps
if link_moltenvk:
mac_deps = [
"//third-party/khronos:moltenVK_static"
]
mac_flags = default_flags
if link_moltenvk:
mac_flags = []

VK_API_DEPS += select({
"DEFAULT": default_deps,
"ovr_config//os:android": android_deps,
"ovr_config//os:macos": [
"//third-party/khronos:moltenVK_static"
],
"ovr_config//os:macos": mac_deps,
})
VK_API_PREPROCESSOR_FLAGS += select({
"DEFAULT": default_flags,
"ovr_config//os:android": android_flags,
"ovr_config//os:macos": []
"ovr_config//os:macos": mac_flags,
})

debug_mode = read_config("etvk", "debug", "0") == "1"
if debug_mode:
VK_API_PREPROCESSOR_FLAGS += ["-DVULKAN_DEBUG"]

runtime.cxx_library(
name = "vulkan_compute_api{}".format(suffix),
compiler_flags = get_vulkan_compiler_flags(),
Expand Down

0 comments on commit 06ef713

Please sign in to comment.