Skip to content

Commit 099a56e

Browse files
pytorchbotSS-JIA
authored andcommitted
[ET-VK][mac] Allow vulkan binaries to work with Vulkan SDK + fixes to 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]>
1 parent 996d941 commit 099a56e

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

backends/vulkan/runtime/vk_api/Runtime.cpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,52 @@ VkInstance create_instance(const RuntimeConfig& config) {
9292
std::vector<const char*> enabled_layers;
9393
std::vector<const char*> enabled_extensions;
9494

95+
std::vector<const char*> requested_layers;
96+
std::vector<const char*> requested_extensions;
97+
9598
if (config.enable_validation_messages) {
96-
std::vector<const char*> requested_layers{
97-
// "VK_LAYER_LUNARG_api_dump",
98-
"VK_LAYER_KHRONOS_validation",
99-
};
100-
std::vector<const char*> requested_extensions{
99+
requested_layers.emplace_back("VK_LAYER_KHRONOS_validation");
101100
#ifdef VK_EXT_debug_report
102-
VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
101+
requested_extensions.emplace_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
103102
#endif /* VK_EXT_debug_report */
104-
};
105-
106-
find_requested_layers_and_extensions(
107-
enabled_layers,
108-
enabled_extensions,
109-
requested_layers,
110-
requested_extensions);
111103
}
112104

105+
VkInstanceCreateFlags instance_flags = 0;
106+
#ifdef __APPLE__
107+
instance_flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
108+
requested_extensions.emplace_back(
109+
VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
110+
#endif
111+
112+
find_requested_layers_and_extensions(
113+
enabled_layers,
114+
enabled_extensions,
115+
requested_layers,
116+
requested_extensions);
117+
118+
const void* instance_create_next = nullptr;
119+
// VkConfig on Mac platforms does not expose debugPrintf settings for whatever
120+
// reason so it has to be enabled manually.
121+
#if defined(__APPLE__) && defined(VULKAN_DEBUG)
122+
std::vector<VkValidationFeatureEnableEXT> enabled_validation_features{
123+
VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT,
124+
};
125+
VkValidationFeaturesEXT validation_features = {
126+
VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT, // sType
127+
nullptr, // pNext
128+
static_cast<uint32_t>(
129+
enabled_validation_features.size()), // enabledValidationFeatureCount
130+
enabled_validation_features.data(), // pEnabledValidationFeatures
131+
0,
132+
nullptr, // pDisabledValidationFeatures
133+
};
134+
instance_create_next = &validation_features;
135+
#endif /* __APPLE__ && VULKAN_DEBUG */
136+
113137
const VkInstanceCreateInfo instance_create_info{
114138
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
115-
nullptr, // pNext
116-
0u, // flags
139+
instance_create_next, // pNext
140+
instance_flags, // flags
117141
&application_info, // pApplicationInfo
118142
static_cast<uint32_t>(enabled_layers.size()), // enabledLayerCount
119143
enabled_layers.data(), // ppEnabledLayerNames

backends/vulkan/targets.bzl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,31 @@ def define_common_targets(is_fbcode = False):
142142
"fbsource//third-party/swiftshader/lib/linux-x64:libvk_swiftshader_so",
143143
]
144144
else:
145+
link_moltenvk = read_config("etvk", "link_moltenvk", "1") == "1"
146+
mac_deps = default_deps
147+
if link_moltenvk:
148+
mac_deps = [
149+
"//third-party/khronos:moltenVK_static"
150+
]
151+
mac_flags = default_flags
152+
if link_moltenvk:
153+
mac_flags = []
154+
145155
VK_API_DEPS += select({
146156
"DEFAULT": default_deps,
147157
"ovr_config//os:android": android_deps,
148-
"ovr_config//os:macos": [
149-
"//third-party/khronos:moltenVK_static"
150-
],
158+
"ovr_config//os:macos": mac_deps,
151159
})
152160
VK_API_PREPROCESSOR_FLAGS += select({
153161
"DEFAULT": default_flags,
154162
"ovr_config//os:android": android_flags,
155-
"ovr_config//os:macos": []
163+
"ovr_config//os:macos": mac_flags,
156164
})
157165

166+
debug_mode = read_config("etvk", "debug", "0") == "1"
167+
if debug_mode:
168+
VK_API_PREPROCESSOR_FLAGS += ["-DVULKAN_DEBUG"]
169+
158170
runtime.cxx_library(
159171
name = "vulkan_compute_api{}".format(suffix),
160172
compiler_flags = get_vulkan_compiler_flags(),

0 commit comments

Comments
 (0)