Skip to content

Commit

Permalink
Take Raw{Display,Window}Handle directly instead of through trait
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Sep 21, 2022
1 parent d97153f commit 0cca1a9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 33 deletions.
4 changes: 3 additions & 1 deletion ash-window/Changelog.md
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions ash-window/README.md
Expand Up @@ -17,17 +17,18 @@ 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)?;
```

[`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
Expand Down
16 changes: 11 additions & 5 deletions ash-window/examples/winit.rs
Expand Up @@ -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},
Expand All @@ -19,9 +20,8 @@ fn main() -> Result<(), Box<dyn Error>> {

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)
Expand All @@ -34,7 +34,13 @@ fn main() -> Result<(), Box<dyn Error>> {
.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);

Expand Down
31 changes: 13 additions & 18 deletions ash-window/src/lib.rs
Expand Up @@ -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<T: HasRawDisplayHandle + HasRawWindowHandle> 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.
///
Expand All @@ -31,13 +19,20 @@ impl<T: HasRawDisplayHandle + HasRawWindowHandle> 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<vk::SurfaceKHR> {
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)
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion examples/Cargo.toml
Expand Up @@ -5,8 +5,9 @@ authors = ["maik klein <maikklein@googlemail.com>"]
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.
Expand Down
18 changes: 13 additions & 5 deletions examples/src/lib.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))]
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 0cca1a9

Please sign in to comment.