Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ash-window: Add get_present_support() helper #774

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ash-window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ rust-version = "1.64.0"
ash = { path = "../ash", version = "0.37", default-features = false }
raw-window-handle = "0.5"

[target.'cfg(unix)'.dependencies]
x11-dl = { version="2.21.0", optional=true }
x11 = { version="2.21.0", features=["xlib"], optional=true }
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
xcb = "1.2.2"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
raw-window-metal = "0.3"

[dev-dependencies]
winit = "0.28.0"
ash = { path = "../ash", version = "0.37", default-features = false, features = ["linked"] }

[features]
default = [ "x11-dl" ]
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved

[[example]]
name = "winit"
required-features = ["ash/linked"]
99 changes: 99 additions & 0 deletions ash-window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,102 @@ pub fn enumerate_required_extensions(

Ok(extensions)
}

/// Query whether a `queue_family` of the given `physical_device` supports presenting to any
/// surface that might be created. This function can be used to find a suitable
/// [`vk::PhysicalDevice`] and queue family for rendering before a single surface is created.
///
/// This function can be a more useful alternative for [`vkGetPhysicalDeviceSurfaceSupportKHR`],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to just have:

Suggested change
/// This function can be a more useful alternative for [`vkGetPhysicalDeviceSurfaceSupportKHR`],
/// This function can be a more useful alternative for [`khr::Surface::get_physical_device_surface_support()`],

We don't generally use Vulkan symbol naming in Ash.

/// which requires having an actual surface available before choosing a physical device.
///
/// For more information see [the vulkan spec on WSI integration][_querying_for_wsi_support].
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
///
/// [`vkGetPhysicalDeviceSurfaceSupportKHR`]: khr::Surface::get_physical_device_surface_support()
/// [_querying_for_wsi_support]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#_querying_for_wsi_support
pub fn get_present_support(
entry: &Entry,
instance: &Instance,
physical_device: vk::PhysicalDevice,
queue_family_index: u32,
display_handle: RawDisplayHandle,
) -> VkResult<bool> {
match display_handle {
RawDisplayHandle::Android(_) | RawDisplayHandle::UiKit(_) | RawDisplayHandle::AppKit(_) => {
// https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#platformQuerySupport_android
// https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#platformQuerySupport_ios
// https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#platformQuerySupport_macos
// On Android, iOS and macOS, every queue family supports presenting to any surface
Ok(true)
}
RawDisplayHandle::Wayland(h) => unsafe {
// https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#platformQuerySupport_walyand
let ext = khr::WaylandSurface::new(entry, instance);
Ok(ext.get_physical_device_wayland_presentation_support(
physical_device,
queue_family_index,
h.display,
))
},
RawDisplayHandle::Windows(_) => unsafe {
// https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#platformQuerySupport_win32
let ext = khr::Win32Surface::new(entry, instance);
Ok(ext.get_physical_device_win32_presentation_support(
physical_device,
queue_family_index,
))
},
#[cfg(unix)]
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
RawDisplayHandle::Xcb(h) => unsafe {
// https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#platformQuerySupport_xcb
let ext = khr::XcbSurface::new(entry, instance);

let xcb = xcb::Connection::from_raw_conn(h.connection.cast());
let setup = xcb.get_setup();
let screen = setup.roots().nth(h.screen as usize).unwrap();
let visual = screen.root_visual();
xcb.into_raw_conn();
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved

let res = ext.get_physical_device_xcb_presentation_support(
physical_device,
queue_family_index,
h.connection,
visual,
);

Ok(res)
},
#[cfg(unix)]
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
RawDisplayHandle::Xlib(h) => unsafe {
// https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#platformQuerySupport_xlib
let ext = khr::XlibSurface::new(entry, instance);

#[cfg(not(any(feature = "x11-dl", feature = "x11")))]
compile_error!("Exactly one of x11-dl or x11 must be enabled on unix");
#[cfg(all(feature = "x11-dl", feature = "x11"))]
compile_error!("Exactly one of x11-dl or x11 must be enabled on unix");
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved

let visual_id;
#[cfg(feature = "x11-dl")]
{
let xlib = x11_dl::xlib::Xlib::open().unwrap();
let default_visual = (xlib.XDefaultVisual)(h.display.cast(), h.screen);
visual_id = (xlib.XVisualIDFromVisual)(default_visual);
}
#[cfg(feature = "x11")]
{
let default_visual = x11::xlib::XDefaultVisual(h.display.cast(), h.screen);
visual_id = x11::xlib::XVisualIDFromVisual(default_visual);
}

Ok(ext.get_physical_device_xlib_presentation_support(
physical_device,
queue_family_index,
h.display,
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
visual_id as _,
))
},
// All other platforms mentioned in the vulkan spec don't
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
// currently have an implementation in ash-window.
_ => Err(vk::Result::ERROR_EXTENSION_NOT_PRESENT),
}
}
2 changes: 1 addition & 1 deletion ash/src/extensions/khr/wayland_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl WaylandSurface {
&self,
physical_device: vk::PhysicalDevice,
queue_family_index: u32,
wl_display: &mut vk::wl_display,
wl_display: *mut vk::wl_display,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should write these down as breaking changes in the CHANGELOG (maybe via a separate PR so that it shows up properly in the commit history), though if someone passes exactly a &mut vk::wl_display it'll automatically coerce to the raw pointer.

) -> bool {
let b = (self.fp.get_physical_device_wayland_presentation_support_khr)(
physical_device,
Expand Down
2 changes: 1 addition & 1 deletion ash/src/extensions/khr/xcb_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl XcbSurface {
&self,
physical_device: vk::PhysicalDevice,
queue_family_index: u32,
connection: &mut vk::xcb_connection_t,
connection: *mut vk::xcb_connection_t,
visual_id: vk::xcb_visualid_t,
) -> bool {
let b = (self.fp.get_physical_device_xcb_presentation_support_khr)(
Expand Down