From 9406685ab91a44751a4c103dbfddc1f5f631a713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Jen=C3=9F?= Date: Fri, 27 May 2022 11:05:15 +0200 Subject: [PATCH] examples: Fix validation errors on macOS and iOS (#623) * Error: vkCreateDevice: VK_KHR_portability_subset must be enabled [...] * Error: Attempting to create a VkDevice from a VkPhysicalDevice which is from a portability driver without the VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR bit in the VkInstanceCreateInfo flags being set and the VK_KHR_portability_enumeration extension enabled. --- examples/src/lib.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 25a577d..1e8a137 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -15,6 +15,11 @@ use std::ffi::CStr; use std::ops::Drop; use std::os::raw::c_char; +#[cfg(any(target_os = "macos", target_os = "ios"))] +use ash::vk::{ + KhrGetPhysicalDeviceProperties2Fn, KhrPortabilityEnumerationFn, KhrPortabilitySubsetFn, +}; + use winit::{ event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event_loop::{ControlFlow, EventLoop}, @@ -233,6 +238,13 @@ impl ExampleBase { .to_vec(); extension_names.push(DebugUtils::name().as_ptr()); + #[cfg(any(target_os = "macos", target_os = "ios"))] + { + extension_names.push(KhrPortabilityEnumerationFn::name().as_ptr()); + // Enabling this extension is a requirement when using `VK_KHR_portability_subset` + extension_names.push(KhrGetPhysicalDeviceProperties2Fn::name().as_ptr()); + } + let appinfo = vk::ApplicationInfo::builder() .application_name(app_name) .application_version(0) @@ -240,10 +252,17 @@ impl ExampleBase { .engine_version(0) .api_version(vk::make_api_version(0, 1, 0, 0)); + let create_flags = if cfg!(any(target_os = "macos", target_os = "ios")) { + vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR + } else { + vk::InstanceCreateFlags::default() + }; + let create_info = vk::InstanceCreateInfo::builder() .application_info(&appinfo) .enabled_layer_names(&layers_names_raw) - .enabled_extension_names(&extension_names); + .enabled_extension_names(&extension_names) + .flags(create_flags); let instance: Instance = entry .create_instance(&create_info, None) @@ -297,7 +316,11 @@ impl ExampleBase { }) .expect("Couldn't find suitable device."); let queue_family_index = queue_family_index as u32; - let device_extension_names_raw = [Swapchain::name().as_ptr()]; + let device_extension_names_raw = [ + Swapchain::name().as_ptr(), + #[cfg(any(target_os = "macos", target_os = "ios"))] + KhrPortabilitySubsetFn::name().as_ptr(), + ]; let features = vk::PhysicalDeviceFeatures { shader_clip_distance: 1, ..Default::default()