diff --git a/.deny.toml b/.deny.toml index 6451a35a0..996b03022 100644 --- a/.deny.toml +++ b/.deny.toml @@ -50,6 +50,9 @@ skip = [ # deno uses an old version { name = "bit-vec", version = "0.8.0" }, { name = "bit-set", version = "0.8.0" }, + + # drm uses an older version via drm-sys, and a newer version via rustix + { name = "linux-raw-sys", version = "0.9.4" }, ] wildcards = "deny" allow-wildcard-paths = true diff --git a/.github/workflows/cts.yml b/.github/workflows/cts.yml index 90bb4cd59..76b353d64 100644 --- a/.github/workflows/cts.yml +++ b/.github/workflows/cts.yml @@ -99,7 +99,7 @@ jobs: uses: Swatinem/rust-cache@v2 with: # The version number can be incremented for cache busting. - prefix-key: v2-rust-${{ hashFiles('cts_runner/revision.txt') }} + prefix-key: v3-rust-${{ hashFiles('cts_runner/revision.txt') }} cache-directories: cts # We enable line numbers for panics, but that's it diff --git a/CHANGELOG.md b/CHANGELOG.md index d30efb02f..73fe7de2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,10 @@ Bottom level categories: - Added support for mesh shaders in naga's HLSL writer, completing DX12 support for mesh shaders. By @inner-daemons in [#8752](https://github.com/gfx-rs/wgpu/pull/8752). +#### Vulkan + +- Add support for RawWindowHandle::Drm on unix. By @rectalogic in [#9182](https://github.com/gfx-rs/wgpu/pull/9182). + ### Changes #### General diff --git a/Cargo.lock b/Cargo.lock index 13e31d957..3e57e9573 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1165,6 +1165,47 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" +[[package]] +name = "drm" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a41816e58f47f49acfd956651055ddcf137c4882c2098c30c448817af21183a" +dependencies = [ + "bitflags 2.11.0", + "bytemuck", + "bytemuck_derive", + "drm-ffi", + "drm-fourcc", + "libc", + "rustix 1.1.4", +] + +[[package]] +name = "drm-ffi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51a91c9b32ac4e8105dec255e849e0d66e27d7c34d184364fb93e469db08f690" +dependencies = [ + "drm-sys", + "rustix 1.1.4", +] + +[[package]] +name = "drm-fourcc" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0aafbcdb8afc29c1a7ee5fbe53b5d62f4565b35a042a662ca9fecd0b54dae6f4" + +[[package]] +name = "drm-sys" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8e1361066d91f5ffccff060a3c3be9c3ecde15be2959c1937595f7a82a9f8" +dependencies = [ + "libc", + "linux-raw-sys 0.9.4", +] + [[package]] name = "dtor" version = "0.1.1" @@ -2190,6 +2231,12 @@ version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" +[[package]] +name = "linux-raw-sys" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" + [[package]] name = "linux-raw-sys" version = "0.12.1" @@ -4782,6 +4829,7 @@ dependencies = [ "bytemuck", "cfg-if", "cfg_aliases", + "drm", "env_logger", "glam", "glow", diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index d91b56404..290970f85 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -94,6 +94,7 @@ vulkan = [ "dep:arrayvec", "dep:ash", "dep:bytemuck", + "dep:drm", "dep:gpu-descriptor", "dep:hashbrown", "dep:libc", @@ -251,6 +252,9 @@ wayland-sys = { version = "0.31.3", features = [ "egl", ], optional = true } +[target.'cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))'.dependencies] +drm = { version = "0.15", optional = true } + ######################### ### Platform: Windows ### ######################### diff --git a/wgpu-hal/src/vulkan/drm.rs b/wgpu-hal/src/vulkan/drm.rs index 807260e33..0732a51ef 100644 --- a/wgpu-hal/src/vulkan/drm.rs +++ b/wgpu-hal/src/vulkan/drm.rs @@ -1,10 +1,25 @@ #![cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))] use alloc::{string::ToString, vec::Vec}; -use core::mem::MaybeUninit; +use core::{mem::MaybeUninit, num::NonZeroU32}; +use std::os::fd::{AsFd, BorrowedFd}; use ash::{ext, khr, vk}; +use drm::{ + self, + control::{self, connector, Device as _}, +}; + +struct Card(i32); +impl AsFd for Card { + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.0) } + } +} +impl control::Device for Card {} +impl drm::Device for Card {} + macro_rules! to_u64 { ($expr:expr) => {{ #[allow(trivial_numeric_casts)] @@ -15,6 +30,77 @@ macro_rules! to_u64 { } impl super::Instance { + /// Creates a new surface from the given drm fd and plane, deriving the connector and mode. + /// + /// # Safety + /// + /// - All parameters must point to valid DRM values. + pub fn create_surface_from_drm_plane( + &self, + fd: i32, + plane: u32, + ) -> Result { + let card = Card(fd); + let plane_info = card + .get_plane( + NonZeroU32::new(plane) + .ok_or(crate::InstanceError::new("Invalid drm plane".to_string()))? + .into(), + ) + .map_err(|e| crate::InstanceError::with_source("drm plane not found".to_string(), e))?; + let crtc_handle = plane_info.crtc().ok_or(crate::InstanceError::new( + "No CRTC for drm plane".to_string(), + ))?; + let crtc_info = card + .get_crtc(crtc_handle) + .map_err(|e| crate::InstanceError::with_source("drm CRTC not found".to_string(), e))?; + let mode = crtc_info.mode().ok_or(crate::InstanceError::new( + "No mode for drm CRTC".to_string(), + ))?; + + let mut connector_and_mode = None; + let resources = card.resource_handles().map_err(|e| { + crate::InstanceError::with_source("No drm resource handles found".to_string(), e) + })?; + for connector_handle in resources.connectors() { + let Ok(connector_info) = card.get_connector(*connector_handle, false) else { + continue; + }; + if connector_info.state() != connector::State::Connected { + continue; + } + + if let Some(encoder_handle) = connector_info.current_encoder() { + if let Ok(encoder_info) = card.get_encoder(encoder_handle) { + if encoder_info.crtc() == Some(crtc_handle) { + connector_and_mode = Some((*connector_handle, mode)); + break; + } + } + } + } + + let (connector_handle, mode) = connector_and_mode.ok_or(crate::InstanceError::new( + "Failed to derive drm connector and mode for plane".to_string(), + ))?; + let (width, height) = mode.size(); + // Rate in millihertz + let refresh_rate = (((mode.clock() as f64 * 1000.0) + / (mode.hsync().2 as f64 * mode.vsync().2 as f64)) + * 1000.0) + .round() as u32; + unsafe { + self.create_surface_from_drm( + fd, + plane, + connector_handle.into(), + width as u32, + height as u32, + refresh_rate, + ) + } + } + /// Creates a new surface from the given drm configuration. /// /// # Safety diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index 895111589..0528c966d 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -892,6 +892,10 @@ impl crate::Instance for super::Instance { let connection = display.connection.expect("Pointer to X-Server is not set."); self.create_surface_from_xcb(connection.as_ptr(), handle.window.get()) } + #[cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))] + (Rwh::Drm(handle), Rdh::Drm(display)) => { + self.create_surface_from_drm_plane(display.fd, handle.plane) + } (Rwh::AndroidNdk(handle), _) => { self.create_surface_android(handle.a_native_window.as_ptr()) }