Skip to content

Commit

Permalink
replace no-dmabuf feature with auto check
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed Jun 22, 2024
1 parent d225250 commit 072540f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
100 changes: 61 additions & 39 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -135,17 +135,14 @@ pub struct WlxGraphics {
pub shared_shaders: RwLock<HashMap<&'static str, Arc<ShaderModule>>>,
}

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<Arc<vulkano::VulkanLibrary>> = OnceLock::new();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -393,39 +405,38 @@ 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) {
if missing {
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()
Expand Down Expand Up @@ -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"))?;
Expand Down Expand Up @@ -508,7 +528,7 @@ impl WlxGraphics {
Arc<vulkano::swapchain::Surface>,
)> {
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();
Expand All @@ -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);
Expand Down Expand Up @@ -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(),
)
}
Expand Down

0 comments on commit 072540f

Please sign in to comment.