From 072540fbb5476f89c0b32e840557cc4d47fc842b Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Sat, 22 Jun 2024 20:55:55 +0900 Subject: [PATCH] replace no-dmabuf feature with auto check --- Cargo.toml | 1 - src/graphics.rs | 100 +++++++++++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f816d02..8da8826 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,6 +72,5 @@ x11 = ["dep:xcb", "wlx-capture/xshm", "xkbcommon/x11"] wayland = ["pipewire", "wlx-capture/wlr", "xkbcommon/wayland"] pipewire = ["wlx-capture/pipewire"] uidev = ["dep:winit"] -no-dmabuf = [] xcb = ["dep:xcb"] as-raw-xcb-connection = [] diff --git a/src/graphics.rs b/src/graphics.rs index b83adb3..3535bb3 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -42,8 +42,8 @@ use vulkano::{ allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ - physical::PhysicalDevice, Device, DeviceCreateInfo, DeviceExtensions, DeviceFeatures, Queue, - QueueCreateInfo, QueueFlags, + physical::PhysicalDevice, Device, DeviceCreateInfo, DeviceExtensions, DeviceFeatures, + Queue, QueueCreateInfo, QueueFlags, }, format::Format, image::{ @@ -135,17 +135,14 @@ pub struct WlxGraphics { pub shared_shaders: RwLock>>, } -fn get_device_extensions() -> DeviceExtensions { - #[cfg(not(feature = "no-dmabuf"))] - return DeviceExtensions { +fn get_dmabuf_extensions() -> DeviceExtensions { + DeviceExtensions { khr_external_memory: true, khr_external_memory_fd: true, ext_external_memory_dma_buf: true, ext_image_drm_format_modifier: true, ..DeviceExtensions::empty() - }; - #[cfg(feature = "no-dmabuf")] - return DeviceExtensions::empty(); + } } static VULKAN_LIBRARY: OnceLock> = OnceLock::new(); @@ -258,12 +255,17 @@ impl WlxGraphics { .position(|(_, q)| q.queue_flags.intersects(QueueFlags::GRAPHICS)) .expect("Vulkan device has no graphics queue") as u32; - let mut device_extensions = get_device_extensions(); - if !physical_device + let mut device_extensions = DeviceExtensions::empty(); + let dmabuf_extensions = get_dmabuf_extensions(); + + if physical_device .supported_extensions() - .ext_image_drm_format_modifier + .contains(&dmabuf_extensions) { - device_extensions.ext_image_drm_format_modifier = false; + device_extensions = device_extensions.union(&dmabuf_extensions); + device_extensions.ext_image_drm_format_modifier = physical_device + .supported_extensions() + .ext_image_drm_format_modifier; } let device_extensions_raw = device_extensions @@ -293,8 +295,8 @@ impl WlxGraphics { .queue_create_infos(&queue_create_infos) .enabled_extension_names(&device_extensions_raw); - let mut dynamic_rendering = PhysicalDeviceDynamicRenderingFeatures::default() - .dynamic_rendering(true); + let mut dynamic_rendering = + PhysicalDeviceDynamicRenderingFeatures::default().dynamic_rendering(true); dynamic_rendering.p_next = device_create_info.p_next as _; device_create_info.p_next = (&mut dynamic_rendering) as *const _ as *const c_void; @@ -327,6 +329,15 @@ impl WlxGraphics { ) }; + log::debug!( + " DMA-buf supported: {}", + device.enabled_extensions().ext_external_memory_dma_buf + ); + log::debug!( + " DRM format modifiers supported: {}", + device.enabled_extensions().ext_image_drm_format_modifier + ); + // Drop the CStrings device_extensions_raw .into_iter() @@ -377,6 +388,7 @@ impl WlxGraphics { //#[cfg(debug_assertions)] //let layers = vec!["VK_LAYER_KHRONOS_validation".to_owned()]; //#[cfg(not(debug_assertions))] + let layers = vec![]; log::debug!("Instance exts for runtime: {:?}", &vk_instance_extensions); @@ -393,30 +405,16 @@ impl WlxGraphics { }, )?; - let device_extensions = get_device_extensions(); - log::debug!("Device exts for app: {:?}", &device_extensions); + let dmabuf_extensions = get_dmabuf_extensions(); let (physical_device, my_extensions, queue_family_index) = instance .enumerate_physical_devices()? .filter_map(|p| { - let runtime_extensions = vk_device_extensions_fn(&p); - log::debug!( - "Device exts for {}: {:?}", - p.properties().device_name, - &runtime_extensions - ); - let mut my_extensions = runtime_extensions.union(&device_extensions); - if p.supported_extensions().contains(&my_extensions) { - Some((p, my_extensions)) - } else { - // try without DRM format modifiers - my_extensions.ext_image_drm_format_modifier = false; - if p.supported_extensions().contains(&my_extensions) { - return Some((p, my_extensions)); - } + let mut my_extensions = vk_device_extensions_fn(&p); + if !p.supported_extensions().contains(&my_extensions) { log::debug!( - "Not using {} because it does not implement the following device extensions:", + "Not using {} due to missing extensions:", p.properties().device_name, ); for (ext, missing) in p.supported_extensions().difference(&my_extensions) { @@ -424,8 +422,21 @@ impl WlxGraphics { log::debug!(" {}", ext); } } - None + return None; + } + + if p.supported_extensions().contains(&dmabuf_extensions) { + my_extensions = my_extensions.union(&dmabuf_extensions); + my_extensions.ext_image_drm_format_modifier = + p.supported_extensions().ext_image_drm_format_modifier; } + + log::debug!( + "Device exts for {}: {:?}", + p.properties().device_name, + &my_extensions + ); + Some((p, my_extensions)) }) .filter_map(|(p, my_extensions)| { p.queue_family_properties() @@ -465,6 +476,15 @@ impl WlxGraphics { }, )?; + log::debug!( + " DMA-buf supported: {}", + device.enabled_extensions().ext_external_memory_dma_buf + ); + log::debug!( + " DRM format modifiers supported: {}", + device.enabled_extensions().ext_image_drm_format_modifier + ); + let queue = queues .next() .ok_or_else(|| anyhow::anyhow!("no GPU queues available"))?; @@ -508,7 +528,7 @@ impl WlxGraphics { Arc, )> { use vulkano::swapchain::Surface; - use winit::{event_loop::EventLoop,window::Window}; + use winit::{event_loop::EventLoop, window::Window}; let event_loop = EventLoop::new().unwrap(); let mut vk_instance_extensions = Surface::required_extensions(&event_loop).unwrap(); @@ -524,10 +544,14 @@ impl WlxGraphics { }, )?; - let window = Arc::new(event_loop.create_window(Window::default_attributes()).unwrap()); + let window = Arc::new( + event_loop + .create_window(Window::default_attributes()) + .unwrap(), + ); let surface = Surface::from_window(instance.clone(), window.clone())?; - let mut device_extensions = get_device_extensions(); + let mut device_extensions = DeviceExtensions::empty(); device_extensions.khr_swapchain = true; log::debug!("Device exts for app: {:?}", &device_extensions); @@ -985,9 +1009,7 @@ impl WlxGraphics { (fns.v1_0.queue_submit)( self.queue.handle(), 1, - [SubmitInfo::default() - .command_buffers(&[command_buffer.handle()])] - .as_ptr(), + [SubmitInfo::default().command_buffers(&[command_buffer.handle()])].as_ptr(), fence.handle(), ) }