824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1 | use crate::Error; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 2 | use ash::vk; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 3 | use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 4 | use std::sync::Mutex; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 5 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 6 | /// Everything that must outlive every Vulkan object the renderer hands out. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 7 | /// Fields drop in declaration order, so the allocator frees its memory before |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 8 | /// the device is destroyed. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 9 | pub(crate) struct Shared { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 10 | /// Staging buffers kept for reuse, see `Staging`. |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 11 | pub staging_pool: Mutex<Vec<crate::image::Pooled>>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 12 | pub allocator: Mutex<Allocator>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 13 | pub device: Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 14 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 15 | |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 16 | impl Drop for Shared { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 17 | fn drop(&mut self) { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 18 | let pool = std::mem::take( |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 19 | self.staging_pool |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 20 | .get_mut() |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 21 | .unwrap_or_else(|e| e.into_inner()), |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 22 | ); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 23 | let allocator = self.allocator.get_mut().unwrap_or_else(|e| e.into_inner()); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 24 | for p in pool { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 25 | unsafe { self.device.device.destroy_buffer(p.buffer, None) }; |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 26 | let _ = allocator.free(p.allocation); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 27 | } |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 28 | } |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 29 | } |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 30 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 31 | /// Loaders for the extensions that dmabuf import, export and fence export |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 32 | /// need. Absent when the device lacks any of them. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 33 | pub(crate) struct DmabufExt { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 34 | pub memory_fd: ash::khr::external_memory_fd::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 35 | pub fence_fd: ash::khr::external_fence_fd::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 36 | pub modifier: ash::ext::image_drm_format_modifier::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 37 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 38 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 39 | const DMABUF_EXTENSIONS: [&std::ffi::CStr; 5] = [ |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 40 | ash::khr::external_memory_fd::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 41 | ash::ext::external_memory_dma_buf::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 42 | ash::ext::image_drm_format_modifier::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 43 | ash::ext::queue_family_foreign::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 44 | ash::khr::external_fence_fd::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 45 | ]; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 46 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | pub(crate) struct Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 48 | pub _entry: ash::Entry, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 49 | pub instance: ash::Instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 50 | pub physical: vk::PhysicalDevice, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 51 | pub device: ash::Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 52 | pub queue: Mutex<vk::Queue>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 53 | pub queue_family: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 54 | pub push_descriptor: ash::khr::push_descriptor::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 55 | pub dmabuf: Option<DmabufExt>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 56 | pub name: String, |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 57 | pub drm_node: Option<u64>, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 58 | /// Signalled fences kept for reuse, see `FenceInner`. |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 59 | pub spare_fences: Mutex<Vec<vk::Fence>>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 60 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 61 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 62 | impl Drop for Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 63 | fn drop(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 64 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 65 | let _ = self.device.device_wait_idle(); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 66 | for f in self |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 67 | .spare_fences |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 68 | .get_mut() |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 69 | .unwrap_or_else(|e| e.into_inner()) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 70 | .drain(..) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 71 | { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 72 | self.device.destroy_fence(f, None); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 73 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 74 | self.device.destroy_device(None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 75 | self.instance.destroy_instance(None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 76 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 77 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 78 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 79 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 80 | impl Shared { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 81 | /// Opens the first suitable device, or the one whose name contains |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 82 | /// `MONOLITH_VK_DEVICE` when that is set. With `node`, only the device |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 83 | /// behind that DRM device number qualifies. |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 84 | pub fn new(node: Option<u64>) -> Result<Self, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 85 | let entry = unsafe { ash::Entry::load()? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 86 | let app = vk::ApplicationInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 87 | .application_name(c"monolith") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 88 | .engine_name(c"monolith") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 89 | .api_version(vk::API_VERSION_1_3); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 90 | let wanted = std::env::var("MONOLITH_VK_DEVICE").ok(); |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 91 | let mut filter = node.and_then(icd_filter); |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 92 | let (instance, physical, queue_family, name) = loop { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 93 | if let Some(glob) = filter { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 94 | // SAFETY: the compositor creates its renderer before starting any |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 95 | // other thread, so nothing reads the environment concurrently. |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 96 | unsafe { std::env::set_var(DRIVERS_SELECT, glob) }; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 97 | } |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 98 | let instance = unsafe { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 99 | entry.create_instance( |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 100 | &vk::InstanceCreateInfo::default().application_info(&app), |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 101 | None, |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 102 | ) |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 103 | }; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 104 | if filter.is_some() { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 105 | // SAFETY: as above. Removing it keeps clients from inheriting it. |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 106 | unsafe { std::env::remove_var(DRIVERS_SELECT) }; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 107 | } |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 108 | let instance = match instance { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 109 | Ok(i) => i, |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 110 | Err(_) if filter.take().is_some() => continue, |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 111 | Err(e) => return Err(e.into()), |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 112 | }; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 113 | match unsafe { pick_physical(&instance, wanted.as_deref(), node) } { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 114 | Some((p, f, n)) => break (instance, p, f, n), |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 115 | None => { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 116 | unsafe { instance.destroy_instance(None) }; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 117 | if filter.take().is_none() { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 118 | return Err(Error::NoSuitableDevice); |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 119 | } |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 120 | } |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 121 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 122 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 123 | let priorities = [1.0]; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 124 | let available: Vec<_> = unsafe { instance.enumerate_device_extension_properties(physical) } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 125 | .unwrap_or_default() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 126 | .iter() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 127 | .filter_map(|e| e.extension_name_as_c_str().ok().map(ToOwned::to_owned)) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 128 | .collect(); |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 129 | let drm_node = available |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 130 | .iter() |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 131 | .any(|a| a.as_c_str() == ash::ext::physical_device_drm::NAME) |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 132 | .then(|| unsafe { drm_node(&instance, physical) }) |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 133 | .flatten(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 134 | let has_dmabuf = DMABUF_EXTENSIONS |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 135 | .iter() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 136 | .all(|n| available.iter().any(|a| a.as_c_str() == *n)); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 137 | let mut extensions = vec![ash::khr::push_descriptor::NAME.as_ptr()]; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 138 | if has_dmabuf { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 139 | extensions.extend(DMABUF_EXTENSIONS.iter().map(|n| n.as_ptr())); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 140 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 141 | let global_priority = [ |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 142 | ash::khr::global_priority::NAME, |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 143 | ash::ext::global_priority::NAME, |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 144 | ] |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 145 | .into_iter() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 146 | .find(|n| available.iter().any(|a| a.as_c_str() == *n)); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 147 | let mut priorities_to_try = vec![None]; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 148 | if let Some(name) = global_priority { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 149 | extensions.push(name.as_ptr()); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 150 | priorities_to_try = vec![ |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 151 | Some(vk::QueueGlobalPriorityKHR::REALTIME), |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 152 | Some(vk::QueueGlobalPriorityKHR::HIGH), |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 153 | None, |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 154 | ]; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 155 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 156 | let mut created = Err(vk::Result::ERROR_INITIALIZATION_FAILED); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 157 | for priority in priorities_to_try { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 158 | let mut features13 = vk::PhysicalDeviceVulkan13Features::default() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 159 | .dynamic_rendering(true) |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 160 | .synchronization2(true); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 161 | let mut global = vk::DeviceQueueGlobalPriorityCreateInfoKHR::default() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 162 | .global_priority(priority.unwrap_or(vk::QueueGlobalPriorityKHR::MEDIUM)); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 163 | let mut queue = vk::DeviceQueueCreateInfo::default() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 164 | .queue_family_index(queue_family) |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 165 | .queue_priorities(&priorities); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 166 | if priority.is_some() { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 167 | queue = queue.push_next(&mut global); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 168 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 169 | let queues = [queue]; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 170 | let info = vk::DeviceCreateInfo::default() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 171 | .queue_create_infos(&queues) |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 172 | .enabled_extension_names(&extensions) |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 173 | .push_next(&mut features13); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 174 | created = unsafe { instance.create_device(physical, &info, None) }; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 175 | match &created { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 176 | Ok(_) => { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 177 | tracing::info!(?priority, "queue global priority"); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 178 | break; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 179 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 180 | Err(e) => tracing::debug!(?priority, "queue global priority refused: {e}"), |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 181 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 182 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 183 | let device = match created { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 184 | Ok(d) => d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 185 | Err(e) => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 186 | unsafe { instance.destroy_instance(None) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 187 | return Err(e.into()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 188 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 189 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 190 | let queue = unsafe { device.get_device_queue(queue_family, 0) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 191 | let push_descriptor = ash::khr::push_descriptor::Device::new(&instance, &device); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 192 | let dmabuf = has_dmabuf.then(|| DmabufExt { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 193 | memory_fd: ash::khr::external_memory_fd::Device::new(&instance, &device), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 194 | fence_fd: ash::khr::external_fence_fd::Device::new(&instance, &device), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 195 | modifier: ash::ext::image_drm_format_modifier::Device::new(&instance, &device), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 196 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 197 | let allocator = Allocator::new(&AllocatorCreateDesc { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 198 | instance: instance.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 199 | device: device.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 200 | physical_device: physical, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 201 | debug_settings: Default::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 202 | buffer_device_address: false, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 203 | allocation_sizes: Default::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 204 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 205 | let device = Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 206 | _entry: entry, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 207 | instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 208 | physical, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 209 | device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 210 | queue: Mutex::new(queue), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 211 | queue_family, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 212 | push_descriptor, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 213 | dmabuf, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 214 | name, |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 215 | drm_node, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 216 | spare_fences: Mutex::new(Vec::new()), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 217 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 218 | Ok(Self { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 219 | staging_pool: Mutex::new(Vec::new()), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 220 | allocator: Mutex::new(allocator?), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 221 | device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 222 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 223 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 224 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 225 | |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 226 | /// Whether `physical` is the device behind DRM device number `node`, as its |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 227 | /// primary or render node. |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 228 | unsafe fn serves_node(instance: &ash::Instance, physical: vk::PhysicalDevice, node: u64) -> bool { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 229 | let mut drm = vk::PhysicalDeviceDrmPropertiesEXT::default(); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 230 | let mut props = vk::PhysicalDeviceProperties2::default().push_next(&mut drm); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 231 | unsafe { instance.get_physical_device_properties2(physical, &mut props) }; |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 232 | let (major, minor) = ( |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 233 | rustix::fs::major(node) as i64, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 234 | rustix::fs::minor(node) as i64, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 235 | ); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 236 | (drm.has_primary == vk::TRUE && (drm.primary_major, drm.primary_minor) == (major, minor)) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 237 | || (drm.has_render == vk::TRUE && (drm.render_major, drm.render_minor) == (major, minor)) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 238 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 239 | |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 240 | const DRIVERS_SELECT: &str = "VK_LOADER_DRIVERS_SELECT"; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 241 | |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 242 | /// A loader glob matching the ICD for the kernel driver behind `node`, unless |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 243 | /// the user already picks drivers themselves. The loader keeps every ICD it |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 244 | /// finds loaded for the instance's lifetime, and on NixOS that includes |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 245 | /// lavapipe and its 26 MB of resident libLLVM. |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 246 | fn icd_filter(node: u64) -> Option<&'static str> { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 247 | let chosen = [ |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 248 | DRIVERS_SELECT, |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 249 | "VK_LOADER_DRIVERS_DISABLE", |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 250 | "VK_DRIVER_FILES", |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 251 | "VK_ICD_FILENAMES", |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 252 | "VK_ADD_DRIVER_FILES", |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 253 | ]; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 254 | if chosen.iter().any(|v| std::env::var_os(v).is_some()) { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 255 | return None; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 256 | } |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 257 | let (major, minor) = (rustix::fs::major(node), rustix::fs::minor(node)); |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 258 | let driver = std::fs::read_link(format!("/sys/dev/char/{major}:{minor}/device/driver")).ok()?; |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 259 | match driver.file_name()?.to_str()? { |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 260 | "nvidia" => Some("*nvidia*"), |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 261 | "amdgpu" | "radeon" => Some("*radeon*"), |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 262 | "i915" | "xe" => Some("*intel*"), |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 263 | "nouveau" => Some("*nouveau*"), |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 264 | _ => None, |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 265 | } |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 266 | } |
6118d58e7ed3 | atagen | 2026-10-19 06:08 UTC | 267 | |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 268 | /// The render node of `physical`, or its primary node when it has none. |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 269 | unsafe fn drm_node(instance: &ash::Instance, physical: vk::PhysicalDevice) -> Option<u64> { |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 270 | let mut drm = vk::PhysicalDeviceDrmPropertiesEXT::default(); |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 271 | let mut props = vk::PhysicalDeviceProperties2::default().push_next(&mut drm); |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 272 | unsafe { instance.get_physical_device_properties2(physical, &mut props) }; |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 273 | let node = |major: i64, minor: i64| rustix::fs::makedev(major as u32, minor as u32); |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 274 | if drm.has_render == vk::TRUE { |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 275 | Some(node(drm.render_major, drm.render_minor)) |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 276 | } else if drm.has_primary == vk::TRUE { |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 277 | Some(node(drm.primary_major, drm.primary_minor)) |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 278 | } else { |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 279 | None |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 280 | } |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 281 | } |
2454f8b9122d | atagen | 2026-10-19 06:05 UTC | 282 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 283 | unsafe fn pick_physical( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 284 | instance: &ash::Instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 285 | wanted: Option<&str>, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 286 | node: Option<u64>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 287 | ) -> Option<(vk::PhysicalDevice, u32, String)> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 288 | let devices = unsafe { instance.enumerate_physical_devices() }.ok()?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 289 | let mut candidates = Vec::new(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 290 | for physical in devices { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 291 | let props = unsafe { instance.get_physical_device_properties(physical) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 292 | if props.api_version < vk::API_VERSION_1_3 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 293 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 294 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 295 | let name = props |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 296 | .device_name_as_c_str() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 297 | .unwrap_or(c"unknown") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 298 | .to_string_lossy() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 299 | .into_owned(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 300 | if wanted.is_some_and(|w| !name.contains(w)) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 301 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 302 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 303 | let mut f13 = vk::PhysicalDeviceVulkan13Features::default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 304 | let mut f2 = vk::PhysicalDeviceFeatures2::default().push_next(&mut f13); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 305 | unsafe { instance.get_physical_device_features2(physical, &mut f2) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 306 | if f13.dynamic_rendering == vk::FALSE || f13.synchronization2 == vk::FALSE { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 307 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 308 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 309 | let extensions = |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 310 | unsafe { instance.enumerate_device_extension_properties(physical) }.unwrap_or_default(); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 311 | let has = |n: &std::ffi::CStr| { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 312 | extensions |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 313 | .iter() |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 314 | .any(|e| e.extension_name_as_c_str().ok() == Some(n)) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 315 | }; |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 316 | if !has(ash::khr::push_descriptor::NAME) { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 317 | continue; |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 318 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 319 | if let Some(node) = node |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 320 | && !(has(ash::ext::physical_device_drm::NAME) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 321 | && unsafe { serves_node(instance, physical, node) }) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 322 | { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 323 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 324 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 325 | let families = unsafe { instance.get_physical_device_queue_family_properties(physical) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 326 | let Some(family) = families |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 327 | .iter() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 328 | .position(|f| f.queue_flags.contains(vk::QueueFlags::GRAPHICS)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 329 | else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 330 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 331 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 332 | let rank = match props.device_type { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 333 | vk::PhysicalDeviceType::DISCRETE_GPU => 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 334 | vk::PhysicalDeviceType::INTEGRATED_GPU => 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 335 | vk::PhysicalDeviceType::VIRTUAL_GPU => 2, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 336 | _ => 3, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 337 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 338 | candidates.push((rank, physical, family as u32, name)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 339 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 340 | candidates.sort_by_key(|c| c.0); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 341 | candidates.into_iter().next().map(|(_, p, f, n)| (p, f, n)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 342 | } |