atagen

monolith

public
Star0
Watch0
2454f8b9122d101a822ca97b17c55ca9df53a24e
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git
crates/render-vk/src/device.rsRust293 lines11.2 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
    pub drm_node: Option<u64>,
58
    /// Signalled fences kept for reuse, see `FenceInner`.
59
    pub spare_fences: Mutex<Vec<vk::Fence>>,
60
}
61
62
impl Drop for Device {
63
    fn drop(&mut self) {
64
        unsafe {
65
            let _ = self.device.device_wait_idle();
66
            for f in self
67
                .spare_fences
68
                .get_mut()
69
                .unwrap_or_else(|e| e.into_inner())
70
                .drain(..)
71
            {
72
                self.device.destroy_fence(f, None);
73
            }
74
            self.device.destroy_device(None);
75
            self.instance.destroy_instance(None);
76
        }
77
    }
78
}
79
80
impl Shared {
81
    /// Opens the first suitable device, or the one whose name contains
82
    /// `MONOLITH_VK_DEVICE` when that is set. With `node`, only the device
83
    /// behind that DRM device number qualifies.
84
    pub fn new(node: Option<u64>) -> Result<Self, Error> {
85
        let entry = unsafe { ash::Entry::load()? };
86
        let app = vk::ApplicationInfo::default()
87
            .application_name(c"monolith")
88
            .engine_name(c"monolith")
89
            .api_version(vk::API_VERSION_1_3);
90
        let instance = unsafe {
91
            entry.create_instance(
92
                &vk::InstanceCreateInfo::default().application_info(&app),
93
                None,
94
            )?
95
        };
96
        let wanted = std::env::var("MONOLITH_VK_DEVICE").ok();
97
        let picked = unsafe { pick_physical(&instance, wanted.as_deref(), node) };
98
        let Some((physical, queue_family, name)) = picked else {
99
            unsafe { instance.destroy_instance(None) };
100
            return Err(Error::NoSuitableDevice);
101
        };
102
        let priorities = [1.0];
103
        let available: Vec<_> = unsafe { instance.enumerate_device_extension_properties(physical) }
104
            .unwrap_or_default()
105
            .iter()
106
            .filter_map(|e| e.extension_name_as_c_str().ok().map(ToOwned::to_owned))
107
            .collect();
108
        let drm_node = available
109
            .iter()
110
            .any(|a| a.as_c_str() == ash::ext::physical_device_drm::NAME)
111
            .then(|| unsafe { drm_node(&instance, physical) })
112
            .flatten();
113
        let has_dmabuf = DMABUF_EXTENSIONS
114
            .iter()
115
            .all(|n| available.iter().any(|a| a.as_c_str() == *n));
116
        let mut extensions = vec![ash::khr::push_descriptor::NAME.as_ptr()];
117
        if has_dmabuf {
118
            extensions.extend(DMABUF_EXTENSIONS.iter().map(|n| n.as_ptr()));
119
        }
120
        let global_priority = [
121
            ash::khr::global_priority::NAME,
122
            ash::ext::global_priority::NAME,
123
        ]
124
        .into_iter()
125
        .find(|n| available.iter().any(|a| a.as_c_str() == *n));
126
        let mut priorities_to_try = vec![None];
127
        if let Some(name) = global_priority {
128
            extensions.push(name.as_ptr());
129
            priorities_to_try = vec![
130
                Some(vk::QueueGlobalPriorityKHR::REALTIME),
131
                Some(vk::QueueGlobalPriorityKHR::HIGH),
132
                None,
133
            ];
134
        }
135
        let mut created = Err(vk::Result::ERROR_INITIALIZATION_FAILED);
136
        for priority in priorities_to_try {
137
            let mut features13 = vk::PhysicalDeviceVulkan13Features::default()
138
                .dynamic_rendering(true)
139
                .synchronization2(true);
140
            let mut global = vk::DeviceQueueGlobalPriorityCreateInfoKHR::default()
141
                .global_priority(priority.unwrap_or(vk::QueueGlobalPriorityKHR::MEDIUM));
142
            let mut queue = vk::DeviceQueueCreateInfo::default()
143
                .queue_family_index(queue_family)
144
                .queue_priorities(&priorities);
145
            if priority.is_some() {
146
                queue = queue.push_next(&mut global);
147
            }
148
            let queues = [queue];
149
            let info = vk::DeviceCreateInfo::default()
150
                .queue_create_infos(&queues)
151
                .enabled_extension_names(&extensions)
152
                .push_next(&mut features13);
153
            created = unsafe { instance.create_device(physical, &info, None) };
154
            match &created {
155
                Ok(_) => {
156
                    tracing::info!(?priority, "queue global priority");
157
                    break;
158
                }
159
                Err(e) => tracing::debug!(?priority, "queue global priority refused: {e}"),
160
            }
161
        }
162
        let device = match created {
163
            Ok(d) => d,
164
            Err(e) => {
165
                unsafe { instance.destroy_instance(None) };
166
                return Err(e.into());
167
            }
168
        };
169
        let queue = unsafe { device.get_device_queue(queue_family, 0) };
170
        let push_descriptor = ash::khr::push_descriptor::Device::new(&instance, &device);
171
        let dmabuf = has_dmabuf.then(|| DmabufExt {
172
            memory_fd: ash::khr::external_memory_fd::Device::new(&instance, &device),
173
            fence_fd: ash::khr::external_fence_fd::Device::new(&instance, &device),
174
            modifier: ash::ext::image_drm_format_modifier::Device::new(&instance, &device),
175
        });
176
        let allocator = Allocator::new(&AllocatorCreateDesc {
177
            instance: instance.clone(),
178
            device: device.clone(),
179
            physical_device: physical,
180
            debug_settings: Default::default(),
181
            buffer_device_address: false,
182
            allocation_sizes: Default::default(),
183
        });
184
        let device = Device {
185
            _entry: entry,
186
            instance,
187
            physical,
188
            device,
189
            queue: Mutex::new(queue),
190
            queue_family,
191
            push_descriptor,
192
            dmabuf,
193
            name,
194
            drm_node,
195
            spare_fences: Mutex::new(Vec::new()),
196
        };
197
        Ok(Self {
198
            staging_pool: Mutex::new(Vec::new()),
199
            allocator: Mutex::new(allocator?),
200
            device,
201
        })
202
    }
203
}
204
205
/// Whether `physical` is the device behind DRM device number `node`, as its
206
/// primary or render node.
207
unsafe fn serves_node(instance: &ash::Instance, physical: vk::PhysicalDevice, node: u64) -> bool {
208
    let mut drm = vk::PhysicalDeviceDrmPropertiesEXT::default();
209
    let mut props = vk::PhysicalDeviceProperties2::default().push_next(&mut drm);
210
    unsafe { instance.get_physical_device_properties2(physical, &mut props) };
211
    let (major, minor) = (
212
        rustix::fs::major(node) as i64,
213
        rustix::fs::minor(node) as i64,
214
    );
215
    (drm.has_primary == vk::TRUE && (drm.primary_major, drm.primary_minor) == (major, minor))
216
        || (drm.has_render == vk::TRUE && (drm.render_major, drm.render_minor) == (major, minor))
217
}
218
219
/// The render node of `physical`, or its primary node when it has none.
220
unsafe fn drm_node(instance: &ash::Instance, physical: vk::PhysicalDevice) -> Option<u64> {
221
    let mut drm = vk::PhysicalDeviceDrmPropertiesEXT::default();
222
    let mut props = vk::PhysicalDeviceProperties2::default().push_next(&mut drm);
223
    unsafe { instance.get_physical_device_properties2(physical, &mut props) };
224
    let node = |major: i64, minor: i64| rustix::fs::makedev(major as u32, minor as u32);
225
    if drm.has_render == vk::TRUE {
226
        Some(node(drm.render_major, drm.render_minor))
227
    } else if drm.has_primary == vk::TRUE {
228
        Some(node(drm.primary_major, drm.primary_minor))
229
    } else {
230
        None
231
    }
232
}
233
234
unsafe fn pick_physical(
235
    instance: &ash::Instance,
236
    wanted: Option<&str>,
237
    node: Option<u64>,
238
) -> Option<(vk::PhysicalDevice, u32, String)> {
239
    let devices = unsafe { instance.enumerate_physical_devices() }.ok()?;
240
    let mut candidates = Vec::new();
241
    for physical in devices {
242
        let props = unsafe { instance.get_physical_device_properties(physical) };
243
        if props.api_version < vk::API_VERSION_1_3 {
244
            continue;
245
        }
246
        let name = props
247
            .device_name_as_c_str()
248
            .unwrap_or(c"unknown")
249
            .to_string_lossy()
250
            .into_owned();
251
        if wanted.is_some_and(|w| !name.contains(w)) {
252
            continue;
253
        }
254
        let mut f13 = vk::PhysicalDeviceVulkan13Features::default();
255
        let mut f2 = vk::PhysicalDeviceFeatures2::default().push_next(&mut f13);
256
        unsafe { instance.get_physical_device_features2(physical, &mut f2) };
257
        if f13.dynamic_rendering == vk::FALSE || f13.synchronization2 == vk::FALSE {
258
            continue;
259
        }
260
        let extensions =
261
            unsafe { instance.enumerate_device_extension_properties(physical) }.unwrap_or_default();
262
        let has = |n: &std::ffi::CStr| {
263
            extensions
264
                .iter()
265
                .any(|e| e.extension_name_as_c_str().ok() == Some(n))
266
        };
267
        if !has(ash::khr::push_descriptor::NAME) {
268
            continue;
269
        }
270
        if let Some(node) = node
271
            && !(has(ash::ext::physical_device_drm::NAME)
272
                && unsafe { serves_node(instance, physical, node) })
273
        {
274
            continue;
275
        }
276
        let families = unsafe { instance.get_physical_device_queue_family_properties(physical) };
277
        let Some(family) = families
278
            .iter()
279
            .position(|f| f.queue_flags.contains(vk::QueueFlags::GRAPHICS))
280
        else {
281
            continue;
282
        };
283
        let rank = match props.device_type {
284
            vk::PhysicalDeviceType::DISCRETE_GPU => 0,
285
            vk::PhysicalDeviceType::INTEGRATED_GPU => 1,
286
            vk::PhysicalDeviceType::VIRTUAL_GPU => 2,
287
            _ => 3,
288
        };
289
        candidates.push((rank, physical, family as u32, name));
290
    }
291
    candidates.sort_by_key(|c| c.0);
292
    candidates.into_iter().next().map(|(_, p, f, n)| (p, f, n))
293
}