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

Initial Support for DRM interface in vulkan backend #5908

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
19 changes: 9 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ ndk-sys = "0.5.0"
#web-sys = { path = "../wasm-bindgen/crates/web-sys" }
#js-sys = { path = "../wasm-bindgen/crates/js-sys" }
#wasm-bindgen = { path = "../wasm-bindgen" }
raw-window-handle = { git = "https://github.com/morr0ne/raw-window-handle", branch = "drm-connector" }

[profile.release]
lto = "thin"
Expand Down
110 changes: 110 additions & 0 deletions wgpu-hal/src/vulkan/instance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{
ffi::{c_void, CStr, CString},
mem::MaybeUninit,
num::NonZeroU32,
slice,
str::FromStr,
sync::Arc,
Expand All @@ -9,6 +11,7 @@ use std::{
use arrayvec::ArrayVec;
use ash::{ext, khr, vk};
use parking_lot::RwLock;
use raw_window_handle::DrmWindowHandle;

unsafe extern "system" fn debug_utils_messenger_callback(
message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,
Expand Down Expand Up @@ -288,6 +291,10 @@ impl super::Instance {
// so that we don't have to conditionally use the functions provided by the 1.1 instance
extensions.push(khr::get_physical_device_properties2::NAME);

extensions.push(ext::acquire_drm_display::NAME);
extensions.push(khr::display::NAME);
extensions.push(ext::direct_mode_display::NAME);

// Only keep available extensions.
extensions.retain(|&ext| {
if instance_extensions
Expand Down Expand Up @@ -540,6 +547,100 @@ impl super::Instance {
Ok(self.create_surface_from_vk_surface_khr(surface))
}

// FIXME: handle errors
fn create_surface_drm(
&self,
fd: i32,
conn: NonZeroU32,
plane: u32,
) -> Result<super::Surface, crate::InstanceError> {
// Get the device major and minor from the fd
let (major, minor) = {
let mut stat = MaybeUninit::<libc::stat>::uninit();

if unsafe { libc::fstat(fd, stat.as_mut_ptr()) } != 0 {
return Err(crate::InstanceError::new(
"Unable to fstat drm device".to_string(),
));
}

let stat = unsafe { stat.assume_init() };

unsafe { (libc::major(stat.st_rdev), libc::minor(stat.st_rdev)) }
};

let mut physical_device = None;

let raw_devices = match unsafe { self.shared.raw.enumerate_physical_devices() } {
Ok(devices) => devices,
Err(err) => {
log::error!("enumerate_adapters: {}", err);
Vec::new()
}
};

// Enumerate through all devices until a matching one is found
for device in raw_devices {
let properties2 = vk::PhysicalDeviceProperties2KHR::default();

let mut drm_props = vk::PhysicalDeviceDrmPropertiesEXT::default();
let mut properties2 = properties2.push_next(&mut drm_props);

unsafe {
self.shared
.raw
.get_physical_device_properties2(device, &mut properties2)
};

if drm_props.primary_major == major.into() && drm_props.primary_minor == minor.into() {
physical_device = Some(device);

break;
Comment on lines +595 to +598
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned before there can be >1 physical device for which this is true.

}
}

let physical_device = physical_device.ok_or(crate::InstanceError::new(
"Failed to find suitable drm device".to_string(),
))?;

let instance =
ext::acquire_drm_display::Instance::new(&self.shared.entry, &self.shared.raw);

let display = unsafe {
instance
.get_drm_display(physical_device, fd, conn.get())
.expect("Failed to get drm display")
};

unsafe {
instance
.acquire_drm_display(physical_device, fd, display)
.expect("Failed to acquire display")
};

let display_instance = khr::display::Instance::new(&self.shared.entry, &self.shared.raw);

let modes = unsafe {
display_instance
.get_display_mode_properties(physical_device, display)
.expect("Failed to get modes")
};

let mode = modes.first().expect("Failed to find a suitable mode");

let create_info = vk::DisplaySurfaceCreateInfoKHR::default()
.display_mode(mode.display_mode)
.image_extent(mode.parameters.visible_region)
.transform(vk::SurfaceTransformFlagsKHR::IDENTITY)
.alpha_mode(vk::DisplayPlaneAlphaFlagsKHR::OPAQUE)
.plane_index(plane);

let surface = unsafe { display_instance.create_display_plane_surface(&create_info, None) }
.expect("Failed to create surface");

Ok(self.create_surface_from_vk_surface_khr(surface))
}

fn create_surface_from_vk_surface_khr(&self, surface: vk::SurfaceKHR) -> super::Surface {
let functor = khr::surface::Instance::new(&self.shared.entry, &self.shared.raw);
super::Surface {
Expand Down Expand Up @@ -867,6 +968,15 @@ impl crate::Instance for super::Instance {
(Rwh::AndroidNdk(handle), _) => {
self.create_surface_android(handle.a_native_window.as_ptr())
}
(
Rwh::Drm(DrmWindowHandle {
plane,
connector_id: Some(connector_id),
..
}),
Rdh::Drm(display),
) => self.create_surface_drm(display.fd, connector_id, plane),
#[cfg(windows)]
(Rwh::Win32(handle), _) => {
let hinstance = handle.hinstance.ok_or_else(|| {
crate::InstanceError::new(String::from(
Expand Down