atagen

monolith

public
Star0
Watch0
0fb670484556007c060b1b67f0ddcbf0f32fcca1
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git

Blame: crates/render-vk/src/device.rs

824bf02b6e55atagen2026-10-16 09:15 UTC1
use crate::Error;
824bf02b6e55atagen2026-10-16 09:15 UTC2
use ash::vk;
824bf02b6e55atagen2026-10-16 09:15 UTC3
use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc};
824bf02b6e55atagen2026-10-16 09:15 UTC4
use std::sync::Mutex;
824bf02b6e55atagen2026-10-16 09:15 UTC5
824bf02b6e55atagen2026-10-16 09:15 UTC6
/// Everything that must outlive every Vulkan object the renderer hands out.
824bf02b6e55atagen2026-10-16 09:15 UTC7
/// Fields drop in declaration order, so the allocator frees its memory before
824bf02b6e55atagen2026-10-16 09:15 UTC8
/// the device is destroyed.
824bf02b6e55atagen2026-10-16 09:15 UTC9
pub(crate) struct Shared {
824bf02b6e55atagen2026-10-16 09:15 UTC10
    pub allocator: Mutex<Allocator>,
824bf02b6e55atagen2026-10-16 09:15 UTC11
    pub device: Device,
824bf02b6e55atagen2026-10-16 09:15 UTC12
}
824bf02b6e55atagen2026-10-16 09:15 UTC13
7de49c2aa70batagen2026-10-16 09:27 UTC14
/// Loaders for the extensions that dmabuf import, export and fence export
7de49c2aa70batagen2026-10-16 09:27 UTC15
/// need. Absent when the device lacks any of them.
7de49c2aa70batagen2026-10-16 09:27 UTC16
pub(crate) struct DmabufExt {
7de49c2aa70batagen2026-10-16 09:27 UTC17
    pub memory_fd: ash::khr::external_memory_fd::Device,
7de49c2aa70batagen2026-10-16 09:27 UTC18
    pub fence_fd: ash::khr::external_fence_fd::Device,
7de49c2aa70batagen2026-10-16 09:27 UTC19
    pub modifier: ash::ext::image_drm_format_modifier::Device,
7de49c2aa70batagen2026-10-16 09:27 UTC20
}
7de49c2aa70batagen2026-10-16 09:27 UTC21
7de49c2aa70batagen2026-10-16 09:27 UTC22
const DMABUF_EXTENSIONS: [&std::ffi::CStr; 5] = [
7de49c2aa70batagen2026-10-16 09:27 UTC23
    ash::khr::external_memory_fd::NAME,
7de49c2aa70batagen2026-10-16 09:27 UTC24
    ash::ext::external_memory_dma_buf::NAME,
7de49c2aa70batagen2026-10-16 09:27 UTC25
    ash::ext::image_drm_format_modifier::NAME,
7de49c2aa70batagen2026-10-16 09:27 UTC26
    ash::ext::queue_family_foreign::NAME,
7de49c2aa70batagen2026-10-16 09:27 UTC27
    ash::khr::external_fence_fd::NAME,
7de49c2aa70batagen2026-10-16 09:27 UTC28
];
7de49c2aa70batagen2026-10-16 09:27 UTC29
824bf02b6e55atagen2026-10-16 09:15 UTC30
pub(crate) struct Device {
824bf02b6e55atagen2026-10-16 09:15 UTC31
    pub _entry: ash::Entry,
824bf02b6e55atagen2026-10-16 09:15 UTC32
    pub instance: ash::Instance,
824bf02b6e55atagen2026-10-16 09:15 UTC33
    pub physical: vk::PhysicalDevice,
824bf02b6e55atagen2026-10-16 09:15 UTC34
    pub device: ash::Device,
824bf02b6e55atagen2026-10-16 09:15 UTC35
    pub queue: Mutex<vk::Queue>,
824bf02b6e55atagen2026-10-16 09:15 UTC36
    pub queue_family: u32,
824bf02b6e55atagen2026-10-16 09:15 UTC37
    pub push_descriptor: ash::khr::push_descriptor::Device,
7de49c2aa70batagen2026-10-16 09:27 UTC38
    pub dmabuf: Option<DmabufExt>,
824bf02b6e55atagen2026-10-16 09:15 UTC39
    pub name: String,
824bf02b6e55atagen2026-10-16 09:15 UTC40
}
824bf02b6e55atagen2026-10-16 09:15 UTC41
824bf02b6e55atagen2026-10-16 09:15 UTC42
impl Drop for Device {
824bf02b6e55atagen2026-10-16 09:15 UTC43
    fn drop(&mut self) {
824bf02b6e55atagen2026-10-16 09:15 UTC44
        unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC45
            let _ = self.device.device_wait_idle();
824bf02b6e55atagen2026-10-16 09:15 UTC46
            self.device.destroy_device(None);
824bf02b6e55atagen2026-10-16 09:15 UTC47
            self.instance.destroy_instance(None);
824bf02b6e55atagen2026-10-16 09:15 UTC48
        }
824bf02b6e55atagen2026-10-16 09:15 UTC49
    }
824bf02b6e55atagen2026-10-16 09:15 UTC50
}
824bf02b6e55atagen2026-10-16 09:15 UTC51
824bf02b6e55atagen2026-10-16 09:15 UTC52
impl Shared {
824bf02b6e55atagen2026-10-16 09:15 UTC53
    /// Opens the first suitable device, or the one whose name contains
824bf02b6e55atagen2026-10-16 09:15 UTC54
    /// `MONOLITH_VK_DEVICE` when that is set.
824bf02b6e55atagen2026-10-16 09:15 UTC55
    pub fn new() -> Result<Self, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC56
        let entry = unsafe { ash::Entry::load()? };
824bf02b6e55atagen2026-10-16 09:15 UTC57
        let app = vk::ApplicationInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC58
            .application_name(c"monolith")
824bf02b6e55atagen2026-10-16 09:15 UTC59
            .engine_name(c"monolith")
824bf02b6e55atagen2026-10-16 09:15 UTC60
            .api_version(vk::API_VERSION_1_3);
824bf02b6e55atagen2026-10-16 09:15 UTC61
        let instance = unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC62
            entry.create_instance(
824bf02b6e55atagen2026-10-16 09:15 UTC63
                &vk::InstanceCreateInfo::default().application_info(&app),
824bf02b6e55atagen2026-10-16 09:15 UTC64
                None,
824bf02b6e55atagen2026-10-16 09:15 UTC65
            )?
824bf02b6e55atagen2026-10-16 09:15 UTC66
        };
824bf02b6e55atagen2026-10-16 09:15 UTC67
        let wanted = std::env::var("MONOLITH_VK_DEVICE").ok();
824bf02b6e55atagen2026-10-16 09:15 UTC68
        let picked = unsafe { pick_physical(&instance, wanted.as_deref()) };
824bf02b6e55atagen2026-10-16 09:15 UTC69
        let Some((physical, queue_family, name)) = picked else {
824bf02b6e55atagen2026-10-16 09:15 UTC70
            unsafe { instance.destroy_instance(None) };
824bf02b6e55atagen2026-10-16 09:15 UTC71
            return Err(Error::NoSuitableDevice);
824bf02b6e55atagen2026-10-16 09:15 UTC72
        };
824bf02b6e55atagen2026-10-16 09:15 UTC73
        let priorities = [1.0];
824bf02b6e55atagen2026-10-16 09:15 UTC74
        let queues = [vk::DeviceQueueCreateInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC75
            .queue_family_index(queue_family)
824bf02b6e55atagen2026-10-16 09:15 UTC76
            .queue_priorities(&priorities)];
7de49c2aa70batagen2026-10-16 09:27 UTC77
        let available: Vec<_> = unsafe { instance.enumerate_device_extension_properties(physical) }
7de49c2aa70batagen2026-10-16 09:27 UTC78
            .unwrap_or_default()
7de49c2aa70batagen2026-10-16 09:27 UTC79
            .iter()
7de49c2aa70batagen2026-10-16 09:27 UTC80
            .filter_map(|e| e.extension_name_as_c_str().ok().map(ToOwned::to_owned))
7de49c2aa70batagen2026-10-16 09:27 UTC81
            .collect();
7de49c2aa70batagen2026-10-16 09:27 UTC82
        let has_dmabuf = DMABUF_EXTENSIONS
7de49c2aa70batagen2026-10-16 09:27 UTC83
            .iter()
7de49c2aa70batagen2026-10-16 09:27 UTC84
            .all(|n| available.iter().any(|a| a.as_c_str() == *n));
7de49c2aa70batagen2026-10-16 09:27 UTC85
        let mut extensions = vec![ash::khr::push_descriptor::NAME.as_ptr()];
7de49c2aa70batagen2026-10-16 09:27 UTC86
        if has_dmabuf {
7de49c2aa70batagen2026-10-16 09:27 UTC87
            extensions.extend(DMABUF_EXTENSIONS.iter().map(|n| n.as_ptr()));
7de49c2aa70batagen2026-10-16 09:27 UTC88
        }
824bf02b6e55atagen2026-10-16 09:15 UTC89
        let mut features13 = vk::PhysicalDeviceVulkan13Features::default()
824bf02b6e55atagen2026-10-16 09:15 UTC90
            .dynamic_rendering(true)
824bf02b6e55atagen2026-10-16 09:15 UTC91
            .synchronization2(true);
824bf02b6e55atagen2026-10-16 09:15 UTC92
        let info = vk::DeviceCreateInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC93
            .queue_create_infos(&queues)
824bf02b6e55atagen2026-10-16 09:15 UTC94
            .enabled_extension_names(&extensions)
824bf02b6e55atagen2026-10-16 09:15 UTC95
            .push_next(&mut features13);
824bf02b6e55atagen2026-10-16 09:15 UTC96
        let device = match unsafe { instance.create_device(physical, &info, None) } {
824bf02b6e55atagen2026-10-16 09:15 UTC97
            Ok(d) => d,
824bf02b6e55atagen2026-10-16 09:15 UTC98
            Err(e) => {
824bf02b6e55atagen2026-10-16 09:15 UTC99
                unsafe { instance.destroy_instance(None) };
824bf02b6e55atagen2026-10-16 09:15 UTC100
                return Err(e.into());
824bf02b6e55atagen2026-10-16 09:15 UTC101
            }
824bf02b6e55atagen2026-10-16 09:15 UTC102
        };
824bf02b6e55atagen2026-10-16 09:15 UTC103
        let queue = unsafe { device.get_device_queue(queue_family, 0) };
824bf02b6e55atagen2026-10-16 09:15 UTC104
        let push_descriptor = ash::khr::push_descriptor::Device::new(&instance, &device);
7de49c2aa70batagen2026-10-16 09:27 UTC105
        let dmabuf = has_dmabuf.then(|| DmabufExt {
7de49c2aa70batagen2026-10-16 09:27 UTC106
            memory_fd: ash::khr::external_memory_fd::Device::new(&instance, &device),
7de49c2aa70batagen2026-10-16 09:27 UTC107
            fence_fd: ash::khr::external_fence_fd::Device::new(&instance, &device),
7de49c2aa70batagen2026-10-16 09:27 UTC108
            modifier: ash::ext::image_drm_format_modifier::Device::new(&instance, &device),
7de49c2aa70batagen2026-10-16 09:27 UTC109
        });
824bf02b6e55atagen2026-10-16 09:15 UTC110
        let allocator = Allocator::new(&AllocatorCreateDesc {
824bf02b6e55atagen2026-10-16 09:15 UTC111
            instance: instance.clone(),
824bf02b6e55atagen2026-10-16 09:15 UTC112
            device: device.clone(),
824bf02b6e55atagen2026-10-16 09:15 UTC113
            physical_device: physical,
824bf02b6e55atagen2026-10-16 09:15 UTC114
            debug_settings: Default::default(),
824bf02b6e55atagen2026-10-16 09:15 UTC115
            buffer_device_address: false,
824bf02b6e55atagen2026-10-16 09:15 UTC116
            allocation_sizes: Default::default(),
824bf02b6e55atagen2026-10-16 09:15 UTC117
        });
824bf02b6e55atagen2026-10-16 09:15 UTC118
        let device = Device {
824bf02b6e55atagen2026-10-16 09:15 UTC119
            _entry: entry,
824bf02b6e55atagen2026-10-16 09:15 UTC120
            instance,
824bf02b6e55atagen2026-10-16 09:15 UTC121
            physical,
824bf02b6e55atagen2026-10-16 09:15 UTC122
            device,
824bf02b6e55atagen2026-10-16 09:15 UTC123
            queue: Mutex::new(queue),
824bf02b6e55atagen2026-10-16 09:15 UTC124
            queue_family,
824bf02b6e55atagen2026-10-16 09:15 UTC125
            push_descriptor,
7de49c2aa70batagen2026-10-16 09:27 UTC126
            dmabuf,
824bf02b6e55atagen2026-10-16 09:15 UTC127
            name,
824bf02b6e55atagen2026-10-16 09:15 UTC128
        };
824bf02b6e55atagen2026-10-16 09:15 UTC129
        Ok(Self {
824bf02b6e55atagen2026-10-16 09:15 UTC130
            allocator: Mutex::new(allocator?),
824bf02b6e55atagen2026-10-16 09:15 UTC131
            device,
824bf02b6e55atagen2026-10-16 09:15 UTC132
        })
824bf02b6e55atagen2026-10-16 09:15 UTC133
    }
824bf02b6e55atagen2026-10-16 09:15 UTC134
}
824bf02b6e55atagen2026-10-16 09:15 UTC135
824bf02b6e55atagen2026-10-16 09:15 UTC136
unsafe fn pick_physical(
824bf02b6e55atagen2026-10-16 09:15 UTC137
    instance: &ash::Instance,
824bf02b6e55atagen2026-10-16 09:15 UTC138
    wanted: Option<&str>,
824bf02b6e55atagen2026-10-16 09:15 UTC139
) -> Option<(vk::PhysicalDevice, u32, String)> {
824bf02b6e55atagen2026-10-16 09:15 UTC140
    let devices = unsafe { instance.enumerate_physical_devices() }.ok()?;
824bf02b6e55atagen2026-10-16 09:15 UTC141
    let mut candidates = Vec::new();
824bf02b6e55atagen2026-10-16 09:15 UTC142
    for physical in devices {
824bf02b6e55atagen2026-10-16 09:15 UTC143
        let props = unsafe { instance.get_physical_device_properties(physical) };
824bf02b6e55atagen2026-10-16 09:15 UTC144
        if props.api_version < vk::API_VERSION_1_3 {
824bf02b6e55atagen2026-10-16 09:15 UTC145
            continue;
824bf02b6e55atagen2026-10-16 09:15 UTC146
        }
824bf02b6e55atagen2026-10-16 09:15 UTC147
        let name = props
824bf02b6e55atagen2026-10-16 09:15 UTC148
            .device_name_as_c_str()
824bf02b6e55atagen2026-10-16 09:15 UTC149
            .unwrap_or(c"unknown")
824bf02b6e55atagen2026-10-16 09:15 UTC150
            .to_string_lossy()
824bf02b6e55atagen2026-10-16 09:15 UTC151
            .into_owned();
824bf02b6e55atagen2026-10-16 09:15 UTC152
        if wanted.is_some_and(|w| !name.contains(w)) {
824bf02b6e55atagen2026-10-16 09:15 UTC153
            continue;
824bf02b6e55atagen2026-10-16 09:15 UTC154
        }
824bf02b6e55atagen2026-10-16 09:15 UTC155
        let mut f13 = vk::PhysicalDeviceVulkan13Features::default();
824bf02b6e55atagen2026-10-16 09:15 UTC156
        let mut f2 = vk::PhysicalDeviceFeatures2::default().push_next(&mut f13);
824bf02b6e55atagen2026-10-16 09:15 UTC157
        unsafe { instance.get_physical_device_features2(physical, &mut f2) };
824bf02b6e55atagen2026-10-16 09:15 UTC158
        if f13.dynamic_rendering == vk::FALSE || f13.synchronization2 == vk::FALSE {
824bf02b6e55atagen2026-10-16 09:15 UTC159
            continue;
824bf02b6e55atagen2026-10-16 09:15 UTC160
        }
824bf02b6e55atagen2026-10-16 09:15 UTC161
        let has_push = unsafe { instance.enumerate_device_extension_properties(physical) }
824bf02b6e55atagen2026-10-16 09:15 UTC162
            .unwrap_or_default()
824bf02b6e55atagen2026-10-16 09:15 UTC163
            .iter()
824bf02b6e55atagen2026-10-16 09:15 UTC164
            .any(|e| e.extension_name_as_c_str().ok() == Some(ash::khr::push_descriptor::NAME));
824bf02b6e55atagen2026-10-16 09:15 UTC165
        if !has_push {
824bf02b6e55atagen2026-10-16 09:15 UTC166
            continue;
824bf02b6e55atagen2026-10-16 09:15 UTC167
        }
824bf02b6e55atagen2026-10-16 09:15 UTC168
        let families = unsafe { instance.get_physical_device_queue_family_properties(physical) };
824bf02b6e55atagen2026-10-16 09:15 UTC169
        let Some(family) = families
824bf02b6e55atagen2026-10-16 09:15 UTC170
            .iter()
824bf02b6e55atagen2026-10-16 09:15 UTC171
            .position(|f| f.queue_flags.contains(vk::QueueFlags::GRAPHICS))
824bf02b6e55atagen2026-10-16 09:15 UTC172
        else {
824bf02b6e55atagen2026-10-16 09:15 UTC173
            continue;
824bf02b6e55atagen2026-10-16 09:15 UTC174
        };
824bf02b6e55atagen2026-10-16 09:15 UTC175
        let rank = match props.device_type {
824bf02b6e55atagen2026-10-16 09:15 UTC176
            vk::PhysicalDeviceType::DISCRETE_GPU => 0,
824bf02b6e55atagen2026-10-16 09:15 UTC177
            vk::PhysicalDeviceType::INTEGRATED_GPU => 1,
824bf02b6e55atagen2026-10-16 09:15 UTC178
            vk::PhysicalDeviceType::VIRTUAL_GPU => 2,
824bf02b6e55atagen2026-10-16 09:15 UTC179
            _ => 3,
824bf02b6e55atagen2026-10-16 09:15 UTC180
        };
824bf02b6e55atagen2026-10-16 09:15 UTC181
        candidates.push((rank, physical, family as u32, name));
824bf02b6e55atagen2026-10-16 09:15 UTC182
    }
824bf02b6e55atagen2026-10-16 09:15 UTC183
    candidates.sort_by_key(|c| c.0);
824bf02b6e55atagen2026-10-16 09:15 UTC184
    candidates.into_iter().next().map(|(_, p, f, n)| (p, f, n))
824bf02b6e55atagen2026-10-16 09:15 UTC185
}