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 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 10 | pub allocator: Mutex<Allocator>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 11 | pub device: Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 12 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 13 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 14 | /// Loaders for the extensions that dmabuf import, export and fence export |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 15 | /// need. Absent when the device lacks any of them. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 16 | pub(crate) struct DmabufExt { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 17 | pub memory_fd: ash::khr::external_memory_fd::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 18 | pub fence_fd: ash::khr::external_fence_fd::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 19 | pub modifier: ash::ext::image_drm_format_modifier::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 20 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 21 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 22 | const DMABUF_EXTENSIONS: [&std::ffi::CStr; 5] = [ |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 23 | ash::khr::external_memory_fd::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 24 | ash::ext::external_memory_dma_buf::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 25 | ash::ext::image_drm_format_modifier::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 26 | ash::ext::queue_family_foreign::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 27 | ash::khr::external_fence_fd::NAME, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 28 | ]; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 29 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 30 | pub(crate) struct Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 31 | pub _entry: ash::Entry, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 32 | pub instance: ash::Instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 33 | pub physical: vk::PhysicalDevice, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 34 | pub device: ash::Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 35 | pub queue: Mutex<vk::Queue>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 36 | pub queue_family: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 37 | pub push_descriptor: ash::khr::push_descriptor::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 38 | pub dmabuf: Option<DmabufExt>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 39 | pub name: String, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 40 | /// Signalled fences kept for reuse, see `FenceInner`. |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 41 | pub spare_fences: Mutex<Vec<vk::Fence>>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 42 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 43 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 44 | impl Drop for Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 45 | fn drop(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 46 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | let _ = self.device.device_wait_idle(); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 48 | for f in self |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 49 | .spare_fences |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 50 | .get_mut() |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 51 | .unwrap_or_else(|e| e.into_inner()) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 52 | .drain(..) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 53 | { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 54 | self.device.destroy_fence(f, None); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 55 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 56 | self.device.destroy_device(None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 57 | self.instance.destroy_instance(None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 58 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 59 | } |
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 Shared { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 63 | /// Opens the first suitable device, or the one whose name contains |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 64 | /// `MONOLITH_VK_DEVICE` when that is set. With `node`, only the device |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 65 | /// behind that DRM device number qualifies. |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 66 | pub fn new(node: Option<u64>) -> Result<Self, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 67 | let entry = unsafe { ash::Entry::load()? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 68 | let app = vk::ApplicationInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 69 | .application_name(c"monolith") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 70 | .engine_name(c"monolith") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 71 | .api_version(vk::API_VERSION_1_3); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 72 | let instance = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 73 | entry.create_instance( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 74 | &vk::InstanceCreateInfo::default().application_info(&app), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 75 | 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 | let wanted = std::env::var("MONOLITH_VK_DEVICE").ok(); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 79 | let picked = unsafe { pick_physical(&instance, wanted.as_deref(), node) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 80 | let Some((physical, queue_family, name)) = picked else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 81 | unsafe { instance.destroy_instance(None) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 82 | return Err(Error::NoSuitableDevice); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 83 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 84 | let priorities = [1.0]; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 85 | let available: Vec<_> = unsafe { instance.enumerate_device_extension_properties(physical) } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 86 | .unwrap_or_default() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 87 | .iter() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 88 | .filter_map(|e| e.extension_name_as_c_str().ok().map(ToOwned::to_owned)) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 89 | .collect(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 90 | let has_dmabuf = DMABUF_EXTENSIONS |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 91 | .iter() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 92 | .all(|n| available.iter().any(|a| a.as_c_str() == *n)); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 93 | let mut extensions = vec![ash::khr::push_descriptor::NAME.as_ptr()]; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 94 | if has_dmabuf { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 95 | extensions.extend(DMABUF_EXTENSIONS.iter().map(|n| n.as_ptr())); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 96 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 97 | let global_priority = [ |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 98 | ash::khr::global_priority::NAME, |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 99 | ash::ext::global_priority::NAME, |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 100 | ] |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 101 | .into_iter() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 102 | .find(|n| available.iter().any(|a| a.as_c_str() == *n)); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 103 | let mut priorities_to_try = vec![None]; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 104 | if let Some(name) = global_priority { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 105 | extensions.push(name.as_ptr()); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 106 | priorities_to_try = vec![ |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 107 | Some(vk::QueueGlobalPriorityKHR::REALTIME), |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 108 | Some(vk::QueueGlobalPriorityKHR::HIGH), |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 109 | None, |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 110 | ]; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 111 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 112 | let mut created = Err(vk::Result::ERROR_INITIALIZATION_FAILED); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 113 | for priority in priorities_to_try { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 114 | let mut features13 = vk::PhysicalDeviceVulkan13Features::default() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 115 | .dynamic_rendering(true) |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 116 | .synchronization2(true); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 117 | let mut global = vk::DeviceQueueGlobalPriorityCreateInfoKHR::default() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 118 | .global_priority(priority.unwrap_or(vk::QueueGlobalPriorityKHR::MEDIUM)); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 119 | let mut queue = vk::DeviceQueueCreateInfo::default() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 120 | .queue_family_index(queue_family) |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 121 | .queue_priorities(&priorities); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 122 | if priority.is_some() { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 123 | queue = queue.push_next(&mut global); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 124 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 125 | let queues = [queue]; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 126 | let info = vk::DeviceCreateInfo::default() |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 127 | .queue_create_infos(&queues) |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 128 | .enabled_extension_names(&extensions) |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 129 | .push_next(&mut features13); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 130 | created = unsafe { instance.create_device(physical, &info, None) }; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 131 | match &created { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 132 | Ok(_) => { |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 133 | tracing::info!(?priority, "queue global priority"); |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 134 | break; |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 135 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 136 | Err(e) => tracing::debug!(?priority, "queue global priority refused: {e}"), |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 137 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 138 | } |
796c963a4b55 | atagen | 2026-10-16 22:46 UTC | 139 | let device = match created { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 140 | Ok(d) => d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 141 | Err(e) => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 142 | unsafe { instance.destroy_instance(None) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 143 | return Err(e.into()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 144 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 145 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 146 | let queue = unsafe { device.get_device_queue(queue_family, 0) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 147 | let push_descriptor = ash::khr::push_descriptor::Device::new(&instance, &device); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 148 | let dmabuf = has_dmabuf.then(|| DmabufExt { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 149 | memory_fd: ash::khr::external_memory_fd::Device::new(&instance, &device), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 150 | fence_fd: ash::khr::external_fence_fd::Device::new(&instance, &device), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 151 | modifier: ash::ext::image_drm_format_modifier::Device::new(&instance, &device), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 152 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 153 | let allocator = Allocator::new(&AllocatorCreateDesc { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 154 | instance: instance.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 155 | device: device.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 156 | physical_device: physical, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 157 | debug_settings: Default::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 158 | buffer_device_address: false, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 159 | allocation_sizes: Default::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 160 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 161 | let device = Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 162 | _entry: entry, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 163 | instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 164 | physical, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 165 | device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 166 | queue: Mutex::new(queue), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 167 | queue_family, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 168 | push_descriptor, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 169 | dmabuf, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 170 | name, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 171 | spare_fences: Mutex::new(Vec::new()), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 172 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 173 | Ok(Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 174 | allocator: Mutex::new(allocator?), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 175 | device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 176 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 177 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 178 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 179 | |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 180 | /// Whether `physical` is the device behind DRM device number `node`, as its |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 181 | /// primary or render node. |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 182 | unsafe fn serves_node(instance: &ash::Instance, physical: vk::PhysicalDevice, node: u64) -> bool { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 183 | let mut drm = vk::PhysicalDeviceDrmPropertiesEXT::default(); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 184 | let mut props = vk::PhysicalDeviceProperties2::default().push_next(&mut drm); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 185 | unsafe { instance.get_physical_device_properties2(physical, &mut props) }; |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 186 | let (major, minor) = ( |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 187 | rustix::fs::major(node) as i64, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 188 | rustix::fs::minor(node) as i64, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 189 | ); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 190 | (drm.has_primary == vk::TRUE && (drm.primary_major, drm.primary_minor) == (major, minor)) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 191 | || (drm.has_render == vk::TRUE && (drm.render_major, drm.render_minor) == (major, minor)) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 192 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 193 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 194 | unsafe fn pick_physical( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 195 | instance: &ash::Instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 196 | wanted: Option<&str>, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 197 | node: Option<u64>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 198 | ) -> Option<(vk::PhysicalDevice, u32, String)> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 199 | let devices = unsafe { instance.enumerate_physical_devices() }.ok()?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 200 | let mut candidates = Vec::new(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 201 | for physical in devices { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 202 | let props = unsafe { instance.get_physical_device_properties(physical) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 203 | if props.api_version < vk::API_VERSION_1_3 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 204 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 205 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 206 | let name = props |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 207 | .device_name_as_c_str() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 208 | .unwrap_or(c"unknown") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 209 | .to_string_lossy() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 210 | .into_owned(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 211 | if wanted.is_some_and(|w| !name.contains(w)) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 212 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 213 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 214 | let mut f13 = vk::PhysicalDeviceVulkan13Features::default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 215 | let mut f2 = vk::PhysicalDeviceFeatures2::default().push_next(&mut f13); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 216 | unsafe { instance.get_physical_device_features2(physical, &mut f2) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 217 | if f13.dynamic_rendering == vk::FALSE || f13.synchronization2 == vk::FALSE { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 218 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 219 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 220 | let extensions = |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 221 | unsafe { instance.enumerate_device_extension_properties(physical) }.unwrap_or_default(); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 222 | let has = |n: &std::ffi::CStr| { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 223 | extensions |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 224 | .iter() |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 225 | .any(|e| e.extension_name_as_c_str().ok() == Some(n)) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 226 | }; |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 227 | if !has(ash::khr::push_descriptor::NAME) { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 228 | continue; |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 229 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 230 | if let Some(node) = node |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 231 | && !(has(ash::ext::physical_device_drm::NAME) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 232 | && unsafe { serves_node(instance, physical, node) }) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 233 | { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 234 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 235 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 236 | let families = unsafe { instance.get_physical_device_queue_family_properties(physical) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 237 | let Some(family) = families |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 238 | .iter() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 239 | .position(|f| f.queue_flags.contains(vk::QueueFlags::GRAPHICS)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 240 | else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 241 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 242 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 243 | let rank = match props.device_type { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 244 | vk::PhysicalDeviceType::DISCRETE_GPU => 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 245 | vk::PhysicalDeviceType::INTEGRATED_GPU => 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 246 | vk::PhysicalDeviceType::VIRTUAL_GPU => 2, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 247 | _ => 3, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 248 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 249 | candidates.push((rank, physical, family as u32, name)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 250 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 251 | candidates.sort_by_key(|c| c.0); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 252 | candidates.into_iter().next().map(|(_, p, f, n)| (p, f, n)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 253 | } |