mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 14:01:08 +01:00
Add DRM support to Vulkan backend (#7212)
* Add basic drm support to vulkan backend * Move vulkan drm implementation to its own module * Properly feature gate drm support and add safety docs * Disable drm on wasm targets * Remove old fixme comment from vulkan drm backend * Move cfg check inside drm module * Use expect instead of allow for create_surface_from_drm * Document that drm is not available on apple platforms
This commit is contained in:
1 parent
5b3266db23
commit
7eb69f43b6
6 files changed
+299
-1
No files matched your search
@@ -202,6 +202,72 @@ impl Instance {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new surface from the given drm configuration.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// - All parameters must point to valid DRM values.
|
||||
///
|
||||
/// # Platform Support
|
||||
///
|
||||
/// This function is only available on non-apple Unix-like platforms (Linux, FreeBSD) and
|
||||
/// currently only works with the Vulkan backend.
|
||||
#[cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))]
|
||||
#[cfg_attr(not(vulkan), expect(unused_variables))]
|
||||
pub unsafe fn create_surface_from_drm(
|
||||
&self,
|
||||
fd: i32,
|
||||
plane: u32,
|
||||
connector_id: u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
refresh_rate: u32,
|
||||
) -> Result<Surface, CreateSurfaceError> {
|
||||
profiling::scope!("Instance::create_surface_from_drm");
|
||||
|
||||
let mut errors = HashMap::default();
|
||||
let mut surface_per_backend: HashMap<Backend, Box<dyn hal::DynSurface>> =
|
||||
HashMap::default();
|
||||
|
||||
#[cfg(vulkan)]
|
||||
{
|
||||
let instance = unsafe { self.as_hal::<hal::api::Vulkan>() }
|
||||
.ok_or(CreateSurfaceError::BackendNotEnabled(Backend::Vulkan))?;
|
||||
|
||||
// Safety must be upheld by the caller
|
||||
match unsafe {
|
||||
instance.create_surface_from_drm(
|
||||
fd,
|
||||
plane,
|
||||
connector_id,
|
||||
width,
|
||||
height,
|
||||
refresh_rate,
|
||||
)
|
||||
} {
|
||||
Ok(surface) => {
|
||||
surface_per_backend.insert(Backend::Vulkan, Box::new(surface));
|
||||
}
|
||||
Err(err) => {
|
||||
errors.insert(Backend::Vulkan, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if surface_per_backend.is_empty() {
|
||||
Err(CreateSurfaceError::FailedToCreateSurfaceForAnyBackend(
|
||||
errors,
|
||||
))
|
||||
} else {
|
||||
let surface = Surface {
|
||||
presentation: Mutex::new(rank::SURFACE_PRESENTATION, None),
|
||||
surface_per_backend,
|
||||
};
|
||||
|
||||
Ok(surface)
|
||||
}
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
///
|
||||
/// `layer` must be a valid pointer.
|
||||
@@ -772,6 +838,42 @@ impl Global {
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// Creates a new surface from the given drm configuration.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// - All parameters must point to valid DRM values.
|
||||
///
|
||||
/// # Platform Support
|
||||
///
|
||||
/// This function is only available on non-apple Unix-like platforms (Linux, FreeBSD) and
|
||||
/// currently only works with the Vulkan backend.
|
||||
#[cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))]
|
||||
pub unsafe fn instance_create_surface_from_drm(
|
||||
&self,
|
||||
fd: i32,
|
||||
plane: u32,
|
||||
connector_id: u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
refresh_rate: u32,
|
||||
id_in: Option<SurfaceId>,
|
||||
) -> Result<SurfaceId, CreateSurfaceError> {
|
||||
let surface = unsafe {
|
||||
self.instance.create_surface_from_drm(
|
||||
fd,
|
||||
plane,
|
||||
connector_id,
|
||||
width,
|
||||
height,
|
||||
refresh_rate,
|
||||
)
|
||||
}?;
|
||||
let id = self.surfaces.prepare(id_in).assign(Arc::new(surface));
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
///
|
||||
/// `layer` must be a valid pointer.
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
#![cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))]
|
||||
|
||||
use core::mem::MaybeUninit;
|
||||
use std::{string::ToString, vec::Vec};
|
||||
|
||||
use ash::{ext, khr, vk};
|
||||
|
||||
impl super::Instance {
|
||||
/// Creates a new surface from the given drm configuration.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// - All parameters must point to valid DRM values.
|
||||
pub unsafe fn create_surface_from_drm(
|
||||
&self,
|
||||
fd: i32,
|
||||
plane: u32,
|
||||
connector_id: u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
refresh_rate: u32,
|
||||
) -> Result<super::Surface, crate::InstanceError> {
|
||||
if !self.shared.extensions.contains(&khr::display::NAME) {
|
||||
return Err(crate::InstanceError::new(
|
||||
"Vulkan driver does not support VK_KHR_display".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let drm_stat = {
|
||||
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(),
|
||||
));
|
||||
}
|
||||
|
||||
unsafe { stat.assume_init() }
|
||||
};
|
||||
|
||||
let raw_devices = match unsafe { self.shared.raw.enumerate_physical_devices() } {
|
||||
Ok(devices) => devices,
|
||||
Err(err) => {
|
||||
log::error!("enumerate_adapters: {}", err);
|
||||
Vec::new()
|
||||
}
|
||||
};
|
||||
|
||||
let mut physical_device = None;
|
||||
|
||||
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)
|
||||
};
|
||||
|
||||
/*
|
||||
The makedev call is just bit manipulation to combine major and minor device numbers into a Unix device ID.
|
||||
It doesn't perform any filesystem operations, only bitshifting.
|
||||
See: https://github.com/rust-lang/libc/blob/268e1b3810ac07ed637d9005bc1a54e49218c958/src/unix/linux_like/linux/mod.rs#L6049
|
||||
We use the resulting device IDs to check if the Vulkan raw device from enumerate_physical_devices
|
||||
matches the DRM device referred to by our file descriptor.
|
||||
*/
|
||||
|
||||
let primary_devid =
|
||||
libc::makedev(drm_props.primary_major as _, drm_props.primary_minor as _);
|
||||
let render_devid =
|
||||
libc::makedev(drm_props.render_major as _, drm_props.render_minor as _);
|
||||
|
||||
if primary_devid == drm_stat.st_rdev || render_devid == drm_stat.st_rdev {
|
||||
physical_device = Some(device)
|
||||
}
|
||||
}
|
||||
|
||||
let physical_device = physical_device.ok_or(crate::InstanceError::new(
|
||||
"Failed to find suitable drm device".to_string(),
|
||||
))?;
|
||||
|
||||
let acquire_drm_display_instance =
|
||||
ext::acquire_drm_display::Instance::new(&self.shared.entry, &self.shared.raw);
|
||||
|
||||
let display = unsafe {
|
||||
acquire_drm_display_instance
|
||||
.get_drm_display(physical_device, fd, connector_id)
|
||||
.expect("Failed to get drm display")
|
||||
};
|
||||
|
||||
unsafe {
|
||||
acquire_drm_display_instance
|
||||
.acquire_drm_display(physical_device, fd, display)
|
||||
.expect("Failed to acquire drm 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 display modes")
|
||||
};
|
||||
|
||||
let mut mode = None;
|
||||
|
||||
for current_mode in modes {
|
||||
log::trace!(
|
||||
"Comparing mode {}x{}@{} with {width}x{height}@{refresh_rate}",
|
||||
current_mode.parameters.visible_region.width,
|
||||
current_mode.parameters.visible_region.height,
|
||||
current_mode.parameters.refresh_rate
|
||||
);
|
||||
if current_mode.parameters.refresh_rate == refresh_rate
|
||||
&& current_mode.parameters.visible_region.width == width
|
||||
&& current_mode.parameters.visible_region.height == height
|
||||
{
|
||||
mode = Some(current_mode)
|
||||
}
|
||||
}
|
||||
|
||||
let mode = mode.ok_or(crate::InstanceError::new(
|
||||
"Failed to find suitable display mode".to_string(),
|
||||
))?;
|
||||
|
||||
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 DRM surface");
|
||||
|
||||
Ok(self.create_surface_from_vk_surface_khr(surface))
|
||||
}
|
||||
}
|
||||
@@ -257,6 +257,12 @@ impl super::Instance {
|
||||
// VK_KHR_surface
|
||||
extensions.push(khr::surface::NAME);
|
||||
|
||||
// Extensions needed for drm support
|
||||
extensions.push(khr::display::NAME);
|
||||
extensions.push(ext::physical_device_drm::NAME);
|
||||
extensions.push(khr::get_display_properties2::NAME);
|
||||
extensions.push(ext::acquire_drm_display::NAME);
|
||||
|
||||
// Platform-specific WSI extensions
|
||||
if cfg!(all(
|
||||
unix,
|
||||
@@ -550,7 +556,10 @@ impl super::Instance {
|
||||
Ok(self.create_surface_from_vk_surface_khr(surface))
|
||||
}
|
||||
|
||||
fn create_surface_from_vk_surface_khr(&self, surface: vk::SurfaceKHR) -> super::Surface {
|
||||
pub(super) 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 {
|
||||
raw: surface,
|
||||
|
||||
@@ -30,6 +30,7 @@ mod adapter;
|
||||
mod command;
|
||||
mod conv;
|
||||
mod device;
|
||||
mod drm;
|
||||
mod instance;
|
||||
mod sampler;
|
||||
|
||||
|
||||
@@ -289,6 +289,31 @@ pub enum SurfaceTargetUnsafe {
|
||||
raw_window_handle: raw_window_handle::RawWindowHandle,
|
||||
},
|
||||
|
||||
/// Surface from a DRM device.
|
||||
///
|
||||
/// If the specified DRM configuration is not supported by any of the backends, then the surface
|
||||
/// will not be supported by any adapters.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// - All parameters must point to valid DRM values and remain valid for as long as the resulting [`Surface`] exists.
|
||||
/// - The file descriptor (`fd`), plane, connector, and mode configuration must be valid and compatible.
|
||||
#[cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))]
|
||||
Drm {
|
||||
/// The file descriptor of the DRM device.
|
||||
fd: i32,
|
||||
/// The plane index on which to create the surface.
|
||||
plane: u32,
|
||||
/// The ID of the connector associated with the selected mode.
|
||||
connector_id: u32,
|
||||
/// The display width of the selected mode.
|
||||
width: u32,
|
||||
/// The display height of the selected mode.
|
||||
height: u32,
|
||||
/// The display refresh rate of the selected mode multiplied by 1000 (e.g., 60Hz → 60000).
|
||||
refresh_rate: u32,
|
||||
},
|
||||
|
||||
/// Surface from `CoreAnimationLayer`.
|
||||
///
|
||||
/// # Safety
|
||||
|
||||
@@ -794,6 +794,26 @@ impl dispatch::InstanceInterface for ContextWgpuCore {
|
||||
.instance_create_surface(raw_display_handle, raw_window_handle, None)
|
||||
},
|
||||
|
||||
#[cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))]
|
||||
SurfaceTargetUnsafe::Drm {
|
||||
fd,
|
||||
plane,
|
||||
connector_id,
|
||||
width,
|
||||
height,
|
||||
refresh_rate,
|
||||
} => unsafe {
|
||||
self.0.instance_create_surface_from_drm(
|
||||
fd,
|
||||
plane,
|
||||
connector_id,
|
||||
width,
|
||||
height,
|
||||
refresh_rate,
|
||||
None,
|
||||
)
|
||||
},
|
||||
|
||||
#[cfg(metal)]
|
||||
SurfaceTargetUnsafe::CoreAnimationLayer(layer) => unsafe {
|
||||
self.0.instance_create_surface_metal(layer, None)
|
||||
|
||||
Reference in new issue
Block a user