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