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 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 14 | pub(crate) struct Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 15 | pub _entry: ash::Entry, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 16 | pub instance: ash::Instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 17 | #[expect( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 18 | dead_code, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 19 | reason = "dmabuf import queries format properties through it" |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 20 | )] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 21 | pub physical: vk::PhysicalDevice, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 22 | pub device: ash::Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 23 | pub queue: Mutex<vk::Queue>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 24 | pub queue_family: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 25 | pub push_descriptor: ash::khr::push_descriptor::Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 26 | pub name: String, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 27 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 28 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 29 | impl Drop for Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 30 | fn drop(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 31 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 32 | let _ = self.device.device_wait_idle(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 33 | self.device.destroy_device(None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 34 | self.instance.destroy_instance(None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 35 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 36 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 37 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 38 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 39 | impl Shared { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 40 | /// Opens the first suitable device, or the one whose name contains |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 41 | /// `MONOLITH_VK_DEVICE` when that is set. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 42 | pub fn new() -> Result<Self, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 43 | let entry = unsafe { ash::Entry::load()? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 44 | let app = vk::ApplicationInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 45 | .application_name(c"monolith") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 46 | .engine_name(c"monolith") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | .api_version(vk::API_VERSION_1_3); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 48 | let instance = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 49 | entry.create_instance( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 50 | &vk::InstanceCreateInfo::default().application_info(&app), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 51 | None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 52 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 53 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 54 | let wanted = std::env::var("MONOLITH_VK_DEVICE").ok(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 55 | let picked = unsafe { pick_physical(&instance, wanted.as_deref()) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 56 | let Some((physical, queue_family, name)) = picked else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 57 | unsafe { instance.destroy_instance(None) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 58 | return Err(Error::NoSuitableDevice); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 59 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 60 | let priorities = [1.0]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 61 | let queues = [vk::DeviceQueueCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 62 | .queue_family_index(queue_family) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 63 | .queue_priorities(&priorities)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 64 | let extensions = [ash::khr::push_descriptor::NAME.as_ptr()]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 65 | let mut features13 = vk::PhysicalDeviceVulkan13Features::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 66 | .dynamic_rendering(true) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 67 | .synchronization2(true); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 68 | let info = vk::DeviceCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 69 | .queue_create_infos(&queues) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 70 | .enabled_extension_names(&extensions) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 71 | .push_next(&mut features13); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 72 | let device = match unsafe { instance.create_device(physical, &info, None) } { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 73 | Ok(d) => d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 74 | Err(e) => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 75 | unsafe { instance.destroy_instance(None) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 76 | return Err(e.into()); |
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 | let queue = unsafe { device.get_device_queue(queue_family, 0) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 80 | let push_descriptor = ash::khr::push_descriptor::Device::new(&instance, &device); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 81 | let allocator = Allocator::new(&AllocatorCreateDesc { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 82 | instance: instance.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 83 | device: device.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 84 | physical_device: physical, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 85 | debug_settings: Default::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 86 | buffer_device_address: false, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 87 | allocation_sizes: Default::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 88 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 89 | let device = Device { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 90 | _entry: entry, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 91 | instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 92 | physical, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 93 | device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 94 | queue: Mutex::new(queue), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 95 | queue_family, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 96 | push_descriptor, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 97 | name, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 98 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 99 | Ok(Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 100 | allocator: Mutex::new(allocator?), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 101 | device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 102 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 103 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 104 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 105 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 106 | unsafe fn pick_physical( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 107 | instance: &ash::Instance, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 108 | wanted: Option<&str>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 109 | ) -> Option<(vk::PhysicalDevice, u32, String)> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 110 | let devices = unsafe { instance.enumerate_physical_devices() }.ok()?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 111 | let mut candidates = Vec::new(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 112 | for physical in devices { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 113 | let props = unsafe { instance.get_physical_device_properties(physical) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 114 | if props.api_version < vk::API_VERSION_1_3 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 115 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 116 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 117 | let name = props |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 118 | .device_name_as_c_str() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 119 | .unwrap_or(c"unknown") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 120 | .to_string_lossy() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 121 | .into_owned(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 122 | if wanted.is_some_and(|w| !name.contains(w)) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 123 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 124 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 125 | let mut f13 = vk::PhysicalDeviceVulkan13Features::default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 126 | let mut f2 = vk::PhysicalDeviceFeatures2::default().push_next(&mut f13); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 127 | unsafe { instance.get_physical_device_features2(physical, &mut f2) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 128 | if f13.dynamic_rendering == vk::FALSE || f13.synchronization2 == vk::FALSE { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 129 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 130 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 131 | let has_push = unsafe { instance.enumerate_device_extension_properties(physical) } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 132 | .unwrap_or_default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 133 | .iter() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 134 | .any(|e| e.extension_name_as_c_str().ok() == Some(ash::khr::push_descriptor::NAME)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 135 | if !has_push { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 136 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 137 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 138 | let families = unsafe { instance.get_physical_device_queue_family_properties(physical) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 139 | let Some(family) = families |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 140 | .iter() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 141 | .position(|f| f.queue_flags.contains(vk::QueueFlags::GRAPHICS)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 142 | else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 143 | continue; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 144 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 145 | let rank = match props.device_type { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 146 | vk::PhysicalDeviceType::DISCRETE_GPU => 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 147 | vk::PhysicalDeviceType::INTEGRATED_GPU => 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 148 | vk::PhysicalDeviceType::VIRTUAL_GPU => 2, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 149 | _ => 3, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 150 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 151 | candidates.push((rank, physical, family as u32, name)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 152 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 153 | candidates.sort_by_key(|c| c.0); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 154 | candidates.into_iter().next().map(|(_, p, f, n)| (p, f, n)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 155 | } |