atagen

monolith

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