diff --git a/ash-window/Changelog.md b/ash-window/Changelog.md index 93438a5ae..26e414deb 100644 --- a/ash-window/Changelog.md +++ b/ash-window/Changelog.md @@ -2,11 +2,13 @@ ## [Unreleased] - ReleaseDate +- Bumped `raw-window-handle` to `0.5.0`, now taking `RawDisplayHandle` and `RawWindowHandle` directly + instead of requiring dynamic dispatch through the `HasRaw{Display,Window}Handle` traits (#645) + ## [0.11.0] - 2022-07-29 ### Changed -- Bumped `raw-window-handle` to `0.5.0` (#645) - Bumped `raw-window-handle` to `0.4.2` (#505) ## [0.10.0] - 2022-03-23 diff --git a/ash-window/README.md b/ash-window/README.md index 08771ca86..f848db42c 100644 --- a/ash-window/README.md +++ b/ash-window/README.md @@ -17,9 +17,9 @@ ash-window = "0.11.0" The library exposes two functions: -- [`enumerate_required_extensions`] returns the required instance extensions needed for surface creation from a specific window handle. +- [`enumerate_required_extensions`] returns the required instance extensions needed for surface creation from a specific display handle. -- [`create_surface`] allows to create a surface from a type implementing [`HasRawWindowHandle`]: +- [`create_surface`] allows to create a surface from a type implementing [`RawDisplayHandle`] and [`RawWindowHandle`]: ```rust ash_window::create_surface(&entry, &instance, &window, None)?; @@ -27,7 +27,8 @@ The library exposes two functions: [`enumerate_required_extensions`]: https://docs.rs/ash-window/latest/ash_window/fn.enumerate_required_extensions.html [`create_surface`]: https://docs.rs/ash-window/latest/ash_window/fn.create_surface.html -[`HasRawWindowHandle`]: https://docs.rs/raw-window-handle/latest/raw_window_handle/trait.HasRawWindowHandle.html +[`RawDisplayHandle`]: https://docs.rs/raw-window-handle/latest/raw_window_handle/enum.RawDisplayHandle.html +[`RawWindowHandle`]: https://docs.rs/raw-window-handle/latest/raw_window_handle/enum.RawWindowHandle.html ## Versions ```toml diff --git a/ash-window/examples/winit.rs b/ash-window/examples/winit.rs index 18ea1ddb1..91b4e042f 100644 --- a/ash-window/examples/winit.rs +++ b/ash-window/examples/winit.rs @@ -6,7 +6,8 @@ //! On instance extensions platform specific extensions need to be enabled. use ash::vk; -use std::{error::Error, ops::Deref}; +use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; +use std::error::Error; use winit::{ dpi::PhysicalSize, event::{Event, VirtualKeyCode, WindowEvent}, @@ -19,9 +20,8 @@ fn main() -> Result<(), Box> { unsafe { let entry = ash::Entry::linked(); - let surface_extensions = ash_window::enumerate_required_extensions( - /* Deref into EventLoopWindowTarget */ event_loop.deref(), - )?; + let surface_extensions = + ash_window::enumerate_required_extensions(event_loop.raw_display_handle())?; let app_desc = vk::ApplicationInfo::default().api_version(vk::make_api_version(0, 1, 0, 0)); let instance_desc = vk::InstanceCreateInfo::default() .application_info(&app_desc) @@ -34,7 +34,13 @@ fn main() -> Result<(), Box> { .build(&event_loop)?; // Create a surface from winit window. - let surface = ash_window::create_surface(&entry, &instance, &window, None)?; + let surface = ash_window::create_surface( + &entry, + &instance, + window.raw_display_handle(), + window.raw_window_handle(), + None, + )?; let surface_fn = ash::extensions::khr::Surface::new(&entry, &instance); println!("surface: {:?}", surface); diff --git a/ash-window/src/lib.rs b/ash-window/src/lib.rs index 109f70be7..5328c82a7 100644 --- a/ash-window/src/lib.rs +++ b/ash-window/src/lib.rs @@ -7,19 +7,7 @@ use ash::{ prelude::*, vk, Entry, Instance, }; -use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, -}; - -pub trait HasRawDisplayAndWindowHandle: HasRawDisplayHandle + HasRawWindowHandle { - fn raw_display_and_window_handle(&self) -> (RawDisplayHandle, RawWindowHandle); -} - -impl HasRawDisplayAndWindowHandle for T { - fn raw_display_and_window_handle(&self) -> (RawDisplayHandle, RawWindowHandle) { - (self.raw_display_handle(), self.raw_window_handle()) - } -} +use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; /// Create a surface from a raw surface handle. /// @@ -31,13 +19,20 @@ impl HasRawDisplayAndWindowHandle f /// In order for the created [`vk::SurfaceKHR`] to be valid for the duration of its /// usage, the [`Instance`] this was called on must be dropped later than the /// resulting [`vk::SurfaceKHR`]. +/// +/// The window represented by `window_handle` must be associated with the display connection +/// in `display_handle`. +/// +/// `window_handle` and `display_handle` must be associated with a valid window and display +/// connection, which must not be destroyed for the lifetime of the returned [`vk::SurfaceKHR`]. pub unsafe fn create_surface( entry: &Entry, instance: &Instance, - handle: &dyn HasRawDisplayAndWindowHandle, + display_handle: RawDisplayHandle, + window_handle: RawWindowHandle, allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { - match handle.raw_display_and_window_handle() { + match (display_handle, window_handle) { (RawDisplayHandle::Windows(_), RawWindowHandle::Win32(window)) => { let surface_desc = vk::Win32SurfaceCreateInfoKHR::default() .hinstance(window.hinstance) @@ -111,15 +106,15 @@ pub unsafe fn create_surface( /// Query the required instance extensions for creating a surface from a display handle. /// -/// This [`HasRawDisplayHandle`] can typically be acquired from a window, but is usually also +/// This [`RawDisplayHandle`] can typically be acquired from a window, but is usually also /// accessible earlier through an "event loop" concept to allow querying required instance /// extensions and creation of a compatible Vulkan instance prior to creating a window. /// /// The returned extensions will include all extension dependencies. pub fn enumerate_required_extensions( - display_handle: &dyn HasRawDisplayHandle, + display_handle: RawDisplayHandle, ) -> VkResult<&'static [*const c_char]> { - let extensions = match display_handle.raw_display_handle() { + let extensions = match display_handle { RawDisplayHandle::Windows(_) => { const WINDOWS_EXTS: [*const c_char; 2] = [ khr::Surface::name().as_ptr(), diff --git a/examples/Cargo.toml b/examples/Cargo.toml index d4d36fc50..bbf21ecbe 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -5,8 +5,9 @@ authors = ["maik klein "] edition = "2018" [dependencies] -winit = "0.27.1" image = "0.24" +raw-window-handle = "0.5" +winit = "0.27.1" # The examples require the validation layers, which means the SDK or # equivalent development packages should be present, so we can link # directly and benefit from the infallible `Entry` constructor. diff --git a/examples/src/lib.rs b/examples/src/lib.rs index c38101c02..ded4401e4 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -5,9 +5,9 @@ use ash::extensions::{ ext::DebugUtils, khr::{Surface, Swapchain}, }; - use ash::{vk, Entry}; pub use ash::{Device, Instance}; +use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; use std::borrow::Cow; use std::cell::RefCell; use std::default::Default; @@ -229,9 +229,10 @@ impl ExampleBase { .map(|raw_name| raw_name.as_ptr()) .collect(); - let mut extension_names = ash_window::enumerate_required_extensions(&window) - .unwrap() - .to_vec(); + let mut extension_names = + ash_window::enumerate_required_extensions(window.raw_display_handle()) + .unwrap() + .to_vec(); extension_names.push(DebugUtils::name().as_ptr()); #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -281,7 +282,14 @@ impl ExampleBase { let debug_call_back = debug_utils_loader .create_debug_utils_messenger(&debug_info, None) .unwrap(); - let surface = ash_window::create_surface(&entry, &instance, &window, None).unwrap(); + let surface = ash_window::create_surface( + &entry, + &instance, + window.raw_display_handle(), + window.raw_window_handle(), + None, + ) + .unwrap(); let pdevices = instance .enumerate_physical_devices() .expect("Physical device error");