wgpu_hal/vulkan/
instance.rs

1use alloc::{borrow::ToOwned as _, boxed::Box, ffi::CString, string::String, sync::Arc, vec::Vec};
2use core::{
3    ffi::{c_void, CStr},
4    marker::PhantomData,
5    slice,
6    str::FromStr,
7};
8use std::thread;
9
10use arrayvec::ArrayVec;
11use ash::{ext, khr, vk};
12use parking_lot::RwLock;
13
14unsafe extern "system" fn debug_utils_messenger_callback(
15    message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,
16    message_type: vk::DebugUtilsMessageTypeFlagsEXT,
17    callback_data_ptr: *const vk::DebugUtilsMessengerCallbackDataEXT,
18    user_data: *mut c_void,
19) -> vk::Bool32 {
20    use alloc::borrow::Cow;
21
22    if thread::panicking() {
23        return vk::FALSE;
24    }
25
26    let cd = unsafe { &*callback_data_ptr };
27    let user_data = unsafe { &*user_data.cast::<super::DebugUtilsMessengerUserData>() };
28
29    const VUID_VKCMDENDDEBUGUTILSLABELEXT_COMMANDBUFFER_01912: i32 = 0x56146426;
30    if cd.message_id_number == VUID_VKCMDENDDEBUGUTILSLABELEXT_COMMANDBUFFER_01912 {
31        // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5671
32        // Versions 1.3.240 through 1.3.250 return a spurious error here if
33        // the debug range start and end appear in different command buffers.
34        if let Some(layer_properties) = user_data.validation_layer_properties.as_ref() {
35            if layer_properties.layer_description.as_ref() == c"Khronos Validation Layer"
36                && layer_properties.layer_spec_version >= vk::make_api_version(0, 1, 3, 240)
37                && layer_properties.layer_spec_version <= vk::make_api_version(0, 1, 3, 250)
38            {
39                return vk::FALSE;
40            }
41        }
42    }
43
44    // Silence Vulkan Validation error "VUID-VkSwapchainCreateInfoKHR-pNext-07781"
45    // This happens when a surface is configured with a size outside the allowed extent.
46    // It's a false positive due to the inherent racy-ness of surface resizing.
47    const VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781: i32 = 0x4c8929c1;
48    if cd.message_id_number == VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781 {
49        return vk::FALSE;
50    }
51
52    // Silence Vulkan Validation error "VUID-VkRenderPassBeginInfo-framebuffer-04627"
53    // if the OBS layer is enabled. This is a bug in the OBS layer. As the OBS layer
54    // does not have a version number they increment, there is no way to qualify the
55    // suppression of the error to a specific version of the OBS layer.
56    //
57    // See https://github.com/obsproject/obs-studio/issues/9353
58    const VUID_VKRENDERPASSBEGININFO_FRAMEBUFFER_04627: i32 = 0x45125641;
59    if cd.message_id_number == VUID_VKRENDERPASSBEGININFO_FRAMEBUFFER_04627
60        && user_data.has_obs_layer
61    {
62        return vk::FALSE;
63    }
64
65    // Silence Vulkan Validation error "VUID-vkCmdCopyImageToBuffer-pRegions-00184".
66    // While we aren't sure yet, we suspect this is probably a VVL issue.
67    // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/9276
68    const VUID_VKCMDCOPYIMAGETOBUFFER_PREGIONS_00184: i32 = 0x45ef177c;
69    if cd.message_id_number == VUID_VKCMDCOPYIMAGETOBUFFER_PREGIONS_00184 {
70        return vk::FALSE;
71    }
72
73    // Silence Vulkan Validation error "VUID-StandaloneSpirv-None-10684".
74    //
75    // This is a bug. To prevent massive noise in the tests, lets suppress it for now.
76    // https://github.com/gfx-rs/wgpu/issues/7696
77    const VUID_STANDALONESPIRV_NONE_10684: i32 = 0xb210f7c2_u32 as i32;
78    if cd.message_id_number == VUID_STANDALONESPIRV_NONE_10684 {
79        return vk::FALSE;
80    }
81
82    let level = match message_severity {
83        // We intentionally suppress info messages down to debug
84        // so that users are not innundated with info messages from the runtime.
85        vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE => log::Level::Trace,
86        vk::DebugUtilsMessageSeverityFlagsEXT::INFO => log::Level::Debug,
87        vk::DebugUtilsMessageSeverityFlagsEXT::WARNING => log::Level::Warn,
88        vk::DebugUtilsMessageSeverityFlagsEXT::ERROR => log::Level::Error,
89        _ => log::Level::Warn,
90    };
91
92    let message_id_name =
93        unsafe { cd.message_id_name_as_c_str() }.map_or(Cow::Borrowed(""), CStr::to_string_lossy);
94    let message = unsafe { cd.message_as_c_str() }.map_or(Cow::Borrowed(""), CStr::to_string_lossy);
95
96    let _ = std::panic::catch_unwind(|| {
97        log::log!(
98            level,
99            "{:?} [{} (0x{:x})]\n\t{}",
100            message_type,
101            message_id_name,
102            cd.message_id_number,
103            message,
104        );
105    });
106
107    if cd.queue_label_count != 0 {
108        let labels =
109            unsafe { slice::from_raw_parts(cd.p_queue_labels, cd.queue_label_count as usize) };
110        let names = labels
111            .iter()
112            .flat_map(|dul_obj| unsafe { dul_obj.label_name_as_c_str() }.map(CStr::to_string_lossy))
113            .collect::<Vec<_>>();
114
115        let _ = std::panic::catch_unwind(|| {
116            log::log!(level, "\tqueues: {}", names.join(", "));
117        });
118    }
119
120    if cd.cmd_buf_label_count != 0 {
121        let labels =
122            unsafe { slice::from_raw_parts(cd.p_cmd_buf_labels, cd.cmd_buf_label_count as usize) };
123        let names = labels
124            .iter()
125            .flat_map(|dul_obj| unsafe { dul_obj.label_name_as_c_str() }.map(CStr::to_string_lossy))
126            .collect::<Vec<_>>();
127
128        let _ = std::panic::catch_unwind(|| {
129            log::log!(level, "\tcommand buffers: {}", names.join(", "));
130        });
131    }
132
133    if cd.object_count != 0 {
134        let labels = unsafe { slice::from_raw_parts(cd.p_objects, cd.object_count as usize) };
135        //TODO: use color fields of `vk::DebugUtilsLabelExt`?
136        let names = labels
137            .iter()
138            .map(|obj_info| {
139                let name = unsafe { obj_info.object_name_as_c_str() }
140                    .map_or(Cow::Borrowed("?"), CStr::to_string_lossy);
141
142                format!(
143                    "(type: {:?}, hndl: 0x{:x}, name: {})",
144                    obj_info.object_type, obj_info.object_handle, name
145                )
146            })
147            .collect::<Vec<_>>();
148        let _ = std::panic::catch_unwind(|| {
149            log::log!(level, "\tobjects: {}", names.join(", "));
150        });
151    }
152
153    #[cfg(feature = "validation_canary")]
154    if cfg!(debug_assertions) && level == log::Level::Error {
155        use alloc::string::ToString as _;
156
157        // Set canary and continue
158        crate::VALIDATION_CANARY.add(message.to_string());
159    }
160
161    vk::FALSE
162}
163
164impl super::DebugUtilsCreateInfo {
165    fn to_vk_create_info(&self) -> vk::DebugUtilsMessengerCreateInfoEXT<'_> {
166        let user_data_ptr: *const super::DebugUtilsMessengerUserData = &*self.callback_data;
167        vk::DebugUtilsMessengerCreateInfoEXT::default()
168            .message_severity(self.severity)
169            .message_type(self.message_type)
170            .user_data(user_data_ptr as *mut _)
171            .pfn_user_callback(Some(debug_utils_messenger_callback))
172    }
173}
174
175impl super::InstanceShared {
176    pub fn entry(&self) -> &ash::Entry {
177        &self.entry
178    }
179
180    pub fn raw_instance(&self) -> &ash::Instance {
181        &self.raw
182    }
183
184    pub fn instance_api_version(&self) -> u32 {
185        self.instance_api_version
186    }
187
188    pub fn extensions(&self) -> &[&'static CStr] {
189        &self.extensions[..]
190    }
191}
192
193impl super::Instance {
194    pub fn shared_instance(&self) -> &super::InstanceShared {
195        &self.shared
196    }
197
198    fn enumerate_instance_extension_properties(
199        entry: &ash::Entry,
200        layer_name: Option<&CStr>,
201    ) -> Result<Vec<vk::ExtensionProperties>, crate::InstanceError> {
202        let instance_extensions = {
203            profiling::scope!("vkEnumerateInstanceExtensionProperties");
204            unsafe { entry.enumerate_instance_extension_properties(layer_name) }
205        };
206        instance_extensions.map_err(|e| {
207            crate::InstanceError::with_source(
208                String::from("enumerate_instance_extension_properties() failed"),
209                e,
210            )
211        })
212    }
213
214    /// Return the instance extension names wgpu would like to enable.
215    ///
216    /// Return a vector of the names of instance extensions actually available
217    /// on `entry` that wgpu would like to enable.
218    ///
219    /// The `instance_api_version` argument should be the instance's Vulkan API
220    /// version, as obtained from `vkEnumerateInstanceVersion`. This is the same
221    /// space of values as the `VK_API_VERSION` constants.
222    ///
223    /// Note that wgpu can function without many of these extensions (for
224    /// example, `VK_KHR_wayland_surface` is certainly not going to be available
225    /// everywhere), but if one of these extensions is available at all, wgpu
226    /// assumes that it has been enabled.
227    pub fn desired_extensions(
228        entry: &ash::Entry,
229        _instance_api_version: u32,
230        flags: wgt::InstanceFlags,
231    ) -> Result<Vec<&'static CStr>, crate::InstanceError> {
232        let instance_extensions = Self::enumerate_instance_extension_properties(entry, None)?;
233
234        // Check our extensions against the available extensions
235        let mut extensions: Vec<&'static CStr> = Vec::new();
236
237        // VK_KHR_surface
238        extensions.push(khr::surface::NAME);
239
240        // Platform-specific WSI extensions
241        if cfg!(all(
242            unix,
243            not(target_os = "android"),
244            not(target_os = "macos")
245        )) {
246            // VK_KHR_xlib_surface
247            extensions.push(khr::xlib_surface::NAME);
248            // VK_KHR_xcb_surface
249            extensions.push(khr::xcb_surface::NAME);
250            // VK_KHR_wayland_surface
251            extensions.push(khr::wayland_surface::NAME);
252        }
253        if cfg!(target_os = "android") {
254            // VK_KHR_android_surface
255            extensions.push(khr::android_surface::NAME);
256        }
257        if cfg!(target_os = "windows") {
258            // VK_KHR_win32_surface
259            extensions.push(khr::win32_surface::NAME);
260        }
261        if cfg!(target_os = "macos") {
262            // VK_EXT_metal_surface
263            extensions.push(ext::metal_surface::NAME);
264            extensions.push(khr::portability_enumeration::NAME);
265        }
266        if cfg!(drm) {
267            // VK_EXT_acquire_drm_display -> VK_EXT_direct_mode_display -> VK_KHR_display
268            extensions.push(ext::acquire_drm_display::NAME);
269            extensions.push(ext::direct_mode_display::NAME);
270            extensions.push(khr::display::NAME);
271            extensions.push(khr::get_physical_device_properties2::NAME);
272            extensions.push(khr::get_display_properties2::NAME);
273        }
274
275        if flags.contains(wgt::InstanceFlags::DEBUG) {
276            // VK_EXT_debug_utils
277            extensions.push(ext::debug_utils::NAME);
278        }
279
280        // VK_EXT_swapchain_colorspace
281        // Provides wide color gamut
282        extensions.push(ext::swapchain_colorspace::NAME);
283
284        // VK_KHR_get_physical_device_properties2
285        // Even though the extension was promoted to Vulkan 1.1, we still require the extension
286        // so that we don't have to conditionally use the functions provided by the 1.1 instance
287        extensions.push(khr::get_physical_device_properties2::NAME);
288
289        // Only keep available extensions.
290        extensions.retain(|&ext| {
291            if instance_extensions
292                .iter()
293                .any(|inst_ext| inst_ext.extension_name_as_c_str() == Ok(ext))
294            {
295                true
296            } else {
297                log::debug!("Unable to find extension: {}", ext.to_string_lossy());
298                false
299            }
300        });
301        Ok(extensions)
302    }
303
304    /// # Safety
305    ///
306    /// - `raw_instance` must be created from `entry`
307    /// - `raw_instance` must be created respecting `instance_api_version`, `extensions` and `flags`
308    /// - `extensions` must be a superset of `desired_extensions()` and must be created from the
309    ///   same entry, `instance_api_version`` and flags.
310    /// - `android_sdk_version` is ignored and can be `0` for all platforms besides Android
311    /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `raw_instance`. If
312    ///   `drop_callback` is [`Some`], `raw_instance` must be valid until the callback is called.
313    ///
314    /// If `debug_utils_user_data` is `Some`, then the validation layer is
315    /// available, so create a [`vk::DebugUtilsMessengerEXT`].
316    #[allow(clippy::too_many_arguments)]
317    pub unsafe fn from_raw(
318        entry: ash::Entry,
319        raw_instance: ash::Instance,
320        instance_api_version: u32,
321        android_sdk_version: u32,
322        debug_utils_create_info: Option<super::DebugUtilsCreateInfo>,
323        extensions: Vec<&'static CStr>,
324        flags: wgt::InstanceFlags,
325        memory_budget_thresholds: wgt::MemoryBudgetThresholds,
326        has_nv_optimus: bool,
327        drop_callback: Option<crate::DropCallback>,
328    ) -> Result<Self, crate::InstanceError> {
329        log::debug!("Instance version: 0x{instance_api_version:x}");
330
331        let debug_utils = if let Some(debug_utils_create_info) = debug_utils_create_info {
332            if extensions.contains(&ext::debug_utils::NAME) {
333                log::debug!("Enabling debug utils");
334
335                let extension = ext::debug_utils::Instance::new(&entry, &raw_instance);
336                let vk_info = debug_utils_create_info.to_vk_create_info();
337                let messenger =
338                    unsafe { extension.create_debug_utils_messenger(&vk_info, None) }.unwrap();
339
340                Some(super::DebugUtils {
341                    extension,
342                    messenger,
343                    callback_data: debug_utils_create_info.callback_data,
344                })
345            } else {
346                log::debug!("Debug utils not enabled: extension not listed");
347                None
348            }
349        } else {
350            log::debug!(
351                "Debug utils not enabled: \
352                        debug_utils_user_data not passed to Instance::from_raw"
353            );
354            None
355        };
356
357        let get_physical_device_properties =
358            if extensions.contains(&khr::get_physical_device_properties2::NAME) {
359                log::debug!("Enabling device properties2");
360                Some(khr::get_physical_device_properties2::Instance::new(
361                    &entry,
362                    &raw_instance,
363                ))
364            } else {
365                None
366            };
367
368        let drop_guard = crate::DropGuard::from_option(drop_callback);
369
370        Ok(Self {
371            shared: Arc::new(super::InstanceShared {
372                raw: raw_instance,
373                extensions,
374                drop_guard,
375                flags,
376                memory_budget_thresholds,
377                debug_utils,
378                get_physical_device_properties,
379                entry,
380                has_nv_optimus,
381                instance_api_version,
382                android_sdk_version,
383            }),
384        })
385    }
386
387    fn create_surface_from_xlib(
388        &self,
389        dpy: *mut vk::Display,
390        window: vk::Window,
391    ) -> Result<super::Surface, crate::InstanceError> {
392        if !self.shared.extensions.contains(&khr::xlib_surface::NAME) {
393            return Err(crate::InstanceError::new(String::from(
394                "Vulkan driver does not support VK_KHR_xlib_surface",
395            )));
396        }
397
398        let surface = {
399            let xlib_loader =
400                khr::xlib_surface::Instance::new(&self.shared.entry, &self.shared.raw);
401            let info = vk::XlibSurfaceCreateInfoKHR::default()
402                .flags(vk::XlibSurfaceCreateFlagsKHR::empty())
403                .window(window)
404                .dpy(dpy);
405
406            unsafe { xlib_loader.create_xlib_surface(&info, None) }
407                .expect("XlibSurface::create_xlib_surface() failed")
408        };
409
410        Ok(self.create_surface_from_vk_surface_khr(surface))
411    }
412
413    fn create_surface_from_xcb(
414        &self,
415        connection: *mut vk::xcb_connection_t,
416        window: vk::xcb_window_t,
417    ) -> Result<super::Surface, crate::InstanceError> {
418        if !self.shared.extensions.contains(&khr::xcb_surface::NAME) {
419            return Err(crate::InstanceError::new(String::from(
420                "Vulkan driver does not support VK_KHR_xcb_surface",
421            )));
422        }
423
424        let surface = {
425            let xcb_loader = khr::xcb_surface::Instance::new(&self.shared.entry, &self.shared.raw);
426            let info = vk::XcbSurfaceCreateInfoKHR::default()
427                .flags(vk::XcbSurfaceCreateFlagsKHR::empty())
428                .window(window)
429                .connection(connection);
430
431            unsafe { xcb_loader.create_xcb_surface(&info, None) }
432                .expect("XcbSurface::create_xcb_surface() failed")
433        };
434
435        Ok(self.create_surface_from_vk_surface_khr(surface))
436    }
437
438    fn create_surface_from_wayland(
439        &self,
440        display: *mut vk::wl_display,
441        surface: *mut vk::wl_surface,
442    ) -> Result<super::Surface, crate::InstanceError> {
443        if !self.shared.extensions.contains(&khr::wayland_surface::NAME) {
444            return Err(crate::InstanceError::new(String::from(
445                "Vulkan driver does not support VK_KHR_wayland_surface",
446            )));
447        }
448
449        let surface = {
450            let w_loader =
451                khr::wayland_surface::Instance::new(&self.shared.entry, &self.shared.raw);
452            let info = vk::WaylandSurfaceCreateInfoKHR::default()
453                .flags(vk::WaylandSurfaceCreateFlagsKHR::empty())
454                .display(display)
455                .surface(surface);
456
457            unsafe { w_loader.create_wayland_surface(&info, None) }.expect("WaylandSurface failed")
458        };
459
460        Ok(self.create_surface_from_vk_surface_khr(surface))
461    }
462
463    fn create_surface_android(
464        &self,
465        window: *mut vk::ANativeWindow,
466    ) -> Result<super::Surface, crate::InstanceError> {
467        if !self.shared.extensions.contains(&khr::android_surface::NAME) {
468            return Err(crate::InstanceError::new(String::from(
469                "Vulkan driver does not support VK_KHR_android_surface",
470            )));
471        }
472
473        let surface = {
474            let a_loader =
475                khr::android_surface::Instance::new(&self.shared.entry, &self.shared.raw);
476            let info = vk::AndroidSurfaceCreateInfoKHR::default()
477                .flags(vk::AndroidSurfaceCreateFlagsKHR::empty())
478                .window(window);
479
480            unsafe { a_loader.create_android_surface(&info, None) }.expect("AndroidSurface failed")
481        };
482
483        Ok(self.create_surface_from_vk_surface_khr(surface))
484    }
485
486    fn create_surface_from_hwnd(
487        &self,
488        hinstance: vk::HINSTANCE,
489        hwnd: vk::HWND,
490    ) -> Result<super::Surface, crate::InstanceError> {
491        if !self.shared.extensions.contains(&khr::win32_surface::NAME) {
492            return Err(crate::InstanceError::new(String::from(
493                "Vulkan driver does not support VK_KHR_win32_surface",
494            )));
495        }
496
497        let surface = {
498            let info = vk::Win32SurfaceCreateInfoKHR::default()
499                .flags(vk::Win32SurfaceCreateFlagsKHR::empty())
500                .hinstance(hinstance)
501                .hwnd(hwnd);
502            let win32_loader =
503                khr::win32_surface::Instance::new(&self.shared.entry, &self.shared.raw);
504            unsafe {
505                win32_loader
506                    .create_win32_surface(&info, None)
507                    .expect("Unable to create Win32 surface")
508            }
509        };
510
511        Ok(self.create_surface_from_vk_surface_khr(surface))
512    }
513
514    #[cfg(target_vendor = "apple")]
515    fn create_surface_from_layer(
516        &self,
517        layer: raw_window_metal::Layer,
518    ) -> Result<super::Surface, crate::InstanceError> {
519        if !self.shared.extensions.contains(&ext::metal_surface::NAME) {
520            return Err(crate::InstanceError::new(String::from(
521                "Vulkan driver does not support VK_EXT_metal_surface",
522            )));
523        }
524
525        // NOTE: The layer is retained by Vulkan's `vkCreateMetalSurfaceEXT`,
526        // so no need to retain it beyond the scope of this function.
527        let surface = {
528            let metal_loader =
529                ext::metal_surface::Instance::new(&self.shared.entry, &self.shared.raw);
530            let vk_info = vk::MetalSurfaceCreateInfoEXT::default()
531                .flags(vk::MetalSurfaceCreateFlagsEXT::empty())
532                .layer(layer.as_ptr().as_ptr());
533
534            unsafe { metal_loader.create_metal_surface(&vk_info, None).unwrap() }
535        };
536
537        Ok(self.create_surface_from_vk_surface_khr(surface))
538    }
539
540    pub(super) fn create_surface_from_vk_surface_khr(
541        &self,
542        surface: vk::SurfaceKHR,
543    ) -> super::Surface {
544        let native_surface =
545            crate::vulkan::swapchain::NativeSurface::from_vk_surface_khr(self, surface);
546
547        super::Surface {
548            swapchain: RwLock::new(None),
549            inner: Box::new(native_surface),
550        }
551    }
552
553    /// `Instance::init` but with a callback.
554    /// If you want to add extensions, add the to the `Vec<'static CStr>` not the create info, otherwise
555    /// it will be overwritten
556    ///
557    /// # Safety:
558    /// Same as `init` but additionally
559    /// - Callback must not remove features.
560    /// - Callback must not change anything to what the instance does not support.
561    pub unsafe fn init_with_callback(
562        desc: &crate::InstanceDescriptor<'_>,
563        callback: Option<Box<super::CreateInstanceCallback>>,
564    ) -> Result<Self, crate::InstanceError> {
565        profiling::scope!("Init Vulkan Backend");
566
567        let entry = unsafe {
568            profiling::scope!("Load vk library");
569            // ohos support is already fixed on ash main, but it's unclear when
570            // a new release can happen.
571            #[cfg(target_env = "ohos")]
572            let loaded = ash::Entry::load_from("libvulkan.so");
573            #[cfg(not(target_env = "ohos"))]
574            let loaded = ash::Entry::load();
575            loaded
576        }
577        .map_err(|err| {
578            crate::InstanceError::with_source(String::from("missing Vulkan entry points"), err)
579        })?;
580        let version = {
581            profiling::scope!("vkEnumerateInstanceVersion");
582            unsafe { entry.try_enumerate_instance_version() }
583        };
584        let instance_api_version = match version {
585            // Vulkan 1.1+
586            Ok(Some(version)) => version,
587            Ok(None) => vk::API_VERSION_1_0,
588            Err(err) => {
589                return Err(crate::InstanceError::with_source(
590                    String::from("try_enumerate_instance_version() failed"),
591                    err,
592                ));
593            }
594        };
595
596        let app_name = CString::new(desc.name).unwrap();
597        let app_info = vk::ApplicationInfo::default()
598            .application_name(app_name.as_c_str())
599            .application_version(1)
600            .engine_name(c"wgpu-hal")
601            .engine_version(2)
602            .api_version(
603                // Vulkan 1.0 doesn't like anything but 1.0 passed in here...
604                if instance_api_version < vk::API_VERSION_1_1 {
605                    vk::API_VERSION_1_0
606                } else {
607                    // This is the max Vulkan API version supported by `wgpu-hal`.
608                    //
609                    // If we want to increment this, there are some things that must be done first:
610                    //  - Audit the behavioral differences between the previous and new API versions.
611                    //  - Audit all extensions used by this backend:
612                    //    - If any were promoted in the new API version and the behavior has changed, we must handle the new behavior in addition to the old behavior.
613                    //    - If any were obsoleted in the new API version, we must implement a fallback for the new API version
614                    //    - If any are non-KHR-vendored, we must ensure the new behavior is still correct (since backwards-compatibility is not guaranteed).
615                    vk::API_VERSION_1_3
616                },
617            );
618
619        let mut extensions = Self::desired_extensions(&entry, instance_api_version, desc.flags)?;
620        let mut create_info = vk::InstanceCreateInfo::default();
621
622        if let Some(callback) = callback {
623            callback(super::CreateInstanceCallbackArgs {
624                extensions: &mut extensions,
625                create_info: &mut create_info,
626                entry: &entry,
627                _phantom: PhantomData,
628            });
629        }
630
631        let instance_layers = {
632            profiling::scope!("vkEnumerateInstanceLayerProperties");
633            unsafe { entry.enumerate_instance_layer_properties() }
634        };
635        let instance_layers = instance_layers.map_err(|e| {
636            log::debug!("enumerate_instance_layer_properties: {e:?}");
637            crate::InstanceError::with_source(
638                String::from("enumerate_instance_layer_properties() failed"),
639                e,
640            )
641        })?;
642
643        fn find_layer<'layers>(
644            instance_layers: &'layers [vk::LayerProperties],
645            name: &CStr,
646        ) -> Option<&'layers vk::LayerProperties> {
647            instance_layers
648                .iter()
649                .find(|inst_layer| inst_layer.layer_name_as_c_str() == Ok(name))
650        }
651
652        let validation_layer_name = c"VK_LAYER_KHRONOS_validation";
653        let validation_layer_properties = find_layer(&instance_layers, validation_layer_name);
654
655        // Determine if VK_EXT_validation_features is available, so we can enable
656        // GPU assisted validation and synchronization validation.
657        let validation_features_are_enabled = if validation_layer_properties.is_some() {
658            // Get the all the instance extension properties.
659            let exts =
660                Self::enumerate_instance_extension_properties(&entry, Some(validation_layer_name))?;
661            // Convert all the names of the extensions into an iterator of CStrs.
662            let mut ext_names = exts
663                .iter()
664                .filter_map(|ext| ext.extension_name_as_c_str().ok());
665            // Find the validation features extension.
666            ext_names.any(|ext_name| ext_name == ext::validation_features::NAME)
667        } else {
668            false
669        };
670
671        let should_enable_gpu_based_validation = desc
672            .flags
673            .intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION)
674            && validation_features_are_enabled;
675
676        let has_nv_optimus = find_layer(&instance_layers, c"VK_LAYER_NV_optimus").is_some();
677
678        let has_obs_layer = find_layer(&instance_layers, c"VK_LAYER_OBS_HOOK").is_some();
679
680        let mut layers: Vec<&'static CStr> = Vec::new();
681
682        let has_debug_extension = extensions.contains(&ext::debug_utils::NAME);
683        let mut debug_user_data = has_debug_extension.then(|| {
684            // Put the callback data on the heap, to ensure it will never be
685            // moved.
686            Box::new(super::DebugUtilsMessengerUserData {
687                validation_layer_properties: None,
688                has_obs_layer,
689            })
690        });
691
692        // Request validation layer if asked.
693        if desc.flags.intersects(wgt::InstanceFlags::VALIDATION)
694            || should_enable_gpu_based_validation
695        {
696            if let Some(layer_properties) = validation_layer_properties {
697                layers.push(validation_layer_name);
698
699                if let Some(debug_user_data) = debug_user_data.as_mut() {
700                    debug_user_data.validation_layer_properties =
701                        Some(super::ValidationLayerProperties {
702                            layer_description: layer_properties
703                                .description_as_c_str()
704                                .unwrap()
705                                .to_owned(),
706                            layer_spec_version: layer_properties.spec_version,
707                        });
708                }
709            } else {
710                log::debug!(
711                    "InstanceFlags::VALIDATION requested, but unable to find layer: {}",
712                    validation_layer_name.to_string_lossy()
713                );
714            }
715        }
716        let mut debug_utils = if let Some(callback_data) = debug_user_data {
717            // having ERROR unconditionally because Vk doesn't like empty flags
718            let mut severity = vk::DebugUtilsMessageSeverityFlagsEXT::ERROR;
719            if log::max_level() >= log::LevelFilter::Debug {
720                severity |= vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE;
721            }
722            if log::max_level() >= log::LevelFilter::Info {
723                severity |= vk::DebugUtilsMessageSeverityFlagsEXT::INFO;
724            }
725            if log::max_level() >= log::LevelFilter::Warn {
726                severity |= vk::DebugUtilsMessageSeverityFlagsEXT::WARNING;
727            }
728
729            let message_type = vk::DebugUtilsMessageTypeFlagsEXT::GENERAL
730                | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION
731                | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE;
732
733            let create_info = super::DebugUtilsCreateInfo {
734                severity,
735                message_type,
736                callback_data,
737            };
738
739            Some(create_info)
740        } else {
741            None
742        };
743
744        #[cfg(target_os = "android")]
745        let android_sdk_version = {
746            let properties = android_system_properties::AndroidSystemProperties::new();
747            // See: https://developer.android.com/reference/android/os/Build.VERSION_CODES
748            if let Some(val) = properties.get("ro.build.version.sdk") {
749                match val.parse::<u32>() {
750                    Ok(sdk_ver) => sdk_ver,
751                    Err(err) => {
752                        log::error!(
753                            concat!(
754                                "Couldn't parse Android's ",
755                                "ro.build.version.sdk system property ({}): {}",
756                            ),
757                            val,
758                            err,
759                        );
760                        0
761                    }
762                }
763            } else {
764                log::error!("Couldn't read Android's ro.build.version.sdk system property");
765                0
766            }
767        };
768        #[cfg(not(target_os = "android"))]
769        let android_sdk_version = 0;
770
771        let mut flags = vk::InstanceCreateFlags::empty();
772
773        // Avoid VUID-VkInstanceCreateInfo-flags-06559: Only ask the instance to
774        // enumerate incomplete Vulkan implementations (which we need on Mac) if
775        // we managed to find the extension that provides the flag.
776        if extensions.contains(&khr::portability_enumeration::NAME) {
777            flags |= vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR;
778        }
779        let vk_instance = {
780            let str_pointers = layers
781                .iter()
782                .chain(extensions.iter())
783                .map(|&s: &&'static _| {
784                    // Safe because `layers` and `extensions` entries have static lifetime.
785                    s.as_ptr()
786                })
787                .collect::<Vec<_>>();
788
789            create_info = create_info
790                .flags(flags)
791                .application_info(&app_info)
792                .enabled_layer_names(&str_pointers[..layers.len()])
793                .enabled_extension_names(&str_pointers[layers.len()..]);
794
795            let mut debug_utils_create_info = debug_utils
796                .as_mut()
797                .map(|create_info| create_info.to_vk_create_info());
798            if let Some(debug_utils_create_info) = debug_utils_create_info.as_mut() {
799                create_info = create_info.push_next(debug_utils_create_info);
800            }
801
802            // Enable explicit validation features if available
803            let mut validation_features;
804            let mut validation_feature_list: ArrayVec<_, 3>;
805            if validation_features_are_enabled {
806                validation_feature_list = ArrayVec::new();
807
808                // Always enable synchronization validation
809                validation_feature_list
810                    .push(vk::ValidationFeatureEnableEXT::SYNCHRONIZATION_VALIDATION);
811
812                // Only enable GPU assisted validation if requested.
813                if should_enable_gpu_based_validation {
814                    validation_feature_list.push(vk::ValidationFeatureEnableEXT::GPU_ASSISTED);
815                    validation_feature_list
816                        .push(vk::ValidationFeatureEnableEXT::GPU_ASSISTED_RESERVE_BINDING_SLOT);
817                }
818
819                validation_features = vk::ValidationFeaturesEXT::default()
820                    .enabled_validation_features(&validation_feature_list);
821                create_info = create_info.push_next(&mut validation_features);
822            }
823
824            unsafe {
825                profiling::scope!("vkCreateInstance");
826                entry.create_instance(&create_info, None)
827            }
828            .map_err(|e| {
829                crate::InstanceError::with_source(
830                    String::from("Entry::create_instance() failed"),
831                    e,
832                )
833            })?
834        };
835
836        unsafe {
837            Self::from_raw(
838                entry,
839                vk_instance,
840                instance_api_version,
841                android_sdk_version,
842                debug_utils,
843                extensions,
844                desc.flags,
845                desc.memory_budget_thresholds,
846                has_nv_optimus,
847                None,
848            )
849        }
850    }
851}
852
853impl Drop for super::InstanceShared {
854    fn drop(&mut self) {
855        unsafe {
856            // Keep du alive since destroy_instance may also log
857            let _du = self.debug_utils.take().inspect(|du| {
858                du.extension
859                    .destroy_debug_utils_messenger(du.messenger, None);
860            });
861            if self.drop_guard.is_none() {
862                self.raw.destroy_instance(None);
863            }
864        }
865    }
866}
867
868impl crate::Instance for super::Instance {
869    type A = super::Api;
870
871    unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {
872        unsafe { Self::init_with_callback(desc, None) }
873    }
874
875    unsafe fn create_surface(
876        &self,
877        display_handle: raw_window_handle::RawDisplayHandle,
878        window_handle: raw_window_handle::RawWindowHandle,
879    ) -> Result<super::Surface, crate::InstanceError> {
880        use raw_window_handle::{RawDisplayHandle as Rdh, RawWindowHandle as Rwh};
881
882        // TODO: Replace with ash-window, which also lazy-loads the extension based on handle type
883
884        match (window_handle, display_handle) {
885            (Rwh::Wayland(handle), Rdh::Wayland(display)) => {
886                self.create_surface_from_wayland(display.display.as_ptr(), handle.surface.as_ptr())
887            }
888            (Rwh::Xlib(handle), Rdh::Xlib(display)) => {
889                let display = display.display.expect("Display pointer is not set.");
890                self.create_surface_from_xlib(display.as_ptr(), handle.window)
891            }
892            (Rwh::Xcb(handle), Rdh::Xcb(display)) => {
893                let connection = display.connection.expect("Pointer to X-Server is not set.");
894                self.create_surface_from_xcb(connection.as_ptr(), handle.window.get())
895            }
896            #[cfg(drm)]
897            (Rwh::Drm(handle), Rdh::Drm(display)) => {
898                self.create_surface_from_drm_plane(display.fd, handle.plane)
899            }
900            (Rwh::AndroidNdk(handle), _) => {
901                self.create_surface_android(handle.a_native_window.as_ptr())
902            }
903            (Rwh::Win32(handle), _) => {
904                let hinstance = handle.hinstance.ok_or_else(|| {
905                    crate::InstanceError::new(String::from(
906                        "Vulkan requires raw-window-handle's Win32::hinstance to be set",
907                    ))
908                })?;
909                self.create_surface_from_hwnd(hinstance.get(), handle.hwnd.get())
910            }
911            #[cfg(target_vendor = "apple")]
912            (Rwh::AppKit(handle), _)
913                if self.shared.extensions.contains(&ext::metal_surface::NAME) =>
914            {
915                let layer = unsafe { raw_window_metal::Layer::from_ns_view(handle.ns_view) };
916                self.create_surface_from_layer(layer)
917            }
918            #[cfg(target_vendor = "apple")]
919            (Rwh::UiKit(handle), _)
920                if self.shared.extensions.contains(&ext::metal_surface::NAME) =>
921            {
922                let layer = unsafe { raw_window_metal::Layer::from_ui_view(handle.ui_view) };
923                self.create_surface_from_layer(layer)
924            }
925            (_, _) => Err(crate::InstanceError::new(format!(
926                "window handle {window_handle:?} is not a Vulkan-compatible handle"
927            ))),
928        }
929    }
930
931    unsafe fn enumerate_adapters(
932        &self,
933        _surface_hint: Option<&super::Surface>,
934    ) -> Vec<crate::ExposedAdapter<super::Api>> {
935        use crate::auxil::db;
936
937        let raw_devices = match unsafe { self.shared.raw.enumerate_physical_devices() } {
938            Ok(devices) => devices,
939            Err(err) => {
940                log::error!("enumerate_adapters: {err}");
941                Vec::new()
942            }
943        };
944
945        let mut exposed_adapters = raw_devices
946            .into_iter()
947            .flat_map(|device| self.expose_adapter(device))
948            .collect::<Vec<_>>();
949
950        // Detect if it's an Intel + NVidia configuration with Optimus
951        let has_nvidia_dgpu = exposed_adapters.iter().any(|exposed| {
952            exposed.info.device_type == wgt::DeviceType::DiscreteGpu
953                && exposed.info.vendor == db::nvidia::VENDOR
954        });
955        if cfg!(target_os = "linux") && has_nvidia_dgpu && self.shared.has_nv_optimus {
956            for exposed in exposed_adapters.iter_mut() {
957                if exposed.info.device_type == wgt::DeviceType::IntegratedGpu
958                    && exposed.info.vendor == db::intel::VENDOR
959                {
960                    // Check if mesa driver and version less than 21.2
961                    if let Some(version) = exposed.info.driver_info.split_once("Mesa ").map(|s| {
962                        let mut components = s.1.split('.');
963                        let major = components.next().and_then(|s| u8::from_str(s).ok());
964                        let minor = components.next().and_then(|s| u8::from_str(s).ok());
965                        if let (Some(major), Some(minor)) = (major, minor) {
966                            (major, minor)
967                        } else {
968                            (0, 0)
969                        }
970                    }) {
971                        if version < (21, 2) {
972                            // See https://gitlab.freedesktop.org/mesa/mesa/-/issues/4688
973                            log::debug!(
974                                concat!(
975                                    "Disabling presentation on '{}' (id {:?}) ",
976                                    "due to NV Optimus and Intel Mesa < v21.2"
977                                ),
978                                exposed.info.name,
979                                exposed.adapter.raw
980                            );
981                            exposed.adapter.private_caps.can_present = false;
982                        }
983                    }
984                }
985            }
986        }
987
988        exposed_adapters
989    }
990}
991
992impl crate::Surface for super::Surface {
993    type A = super::Api;
994
995    unsafe fn configure(
996        &self,
997        device: &super::Device,
998        config: &crate::SurfaceConfiguration,
999    ) -> Result<(), crate::SurfaceError> {
1000        // SAFETY: `configure`'s contract guarantees there are no resources derived from the swapchain in use.
1001        let mut swap_chain = self.swapchain.write();
1002
1003        let mut old = swap_chain.take();
1004        if let Some(ref mut old) = old {
1005            unsafe { old.release_resources(device) };
1006        }
1007
1008        let swapchain = unsafe { self.inner.create_swapchain(device, config, old)? };
1009        *swap_chain = Some(swapchain);
1010
1011        Ok(())
1012    }
1013
1014    unsafe fn unconfigure(&self, device: &super::Device) {
1015        if let Some(mut sc) = self.swapchain.write().take() {
1016            // SAFETY: `unconfigure`'s contract guarantees there are no resources derived from the swapchain in use.
1017            unsafe { sc.release_resources(device) };
1018        }
1019    }
1020
1021    unsafe fn acquire_texture(
1022        &self,
1023        timeout: Option<core::time::Duration>,
1024        fence: &super::Fence,
1025    ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> {
1026        let mut swapchain = self.swapchain.write();
1027        let swapchain = swapchain.as_mut().unwrap();
1028
1029        unsafe { swapchain.acquire(timeout, fence) }
1030    }
1031
1032    unsafe fn discard_texture(&self, texture: super::SurfaceTexture) {
1033        unsafe {
1034            self.swapchain
1035                .write()
1036                .as_mut()
1037                .unwrap()
1038                .discard_texture(texture)
1039                .unwrap()
1040        };
1041    }
1042}