1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use {crate::graphics::vulkan::raii, anyhow::Result, ash::vk, std::sync::Arc};

/// A RAII wrapper for the Vulkan Logical Device.
pub struct Device {
    pub raw: ash::Device,
    pub ash: Arc<raii::Instance>,
}

impl Device {
    pub fn new(
        instance: Arc<raii::Instance>,
        physical_device: vk::PhysicalDevice,
        create_info: &vk::DeviceCreateInfo,
    ) -> Result<Arc<Self>> {
        let raw = unsafe {
            instance
                .raw
                .create_device(physical_device, create_info, None)?
        };
        Ok(Arc::new(Self { raw, ash: instance }))
    }
}

impl Drop for Device {
    fn drop(&mut self) {
        unsafe { self.raw.destroy_device(None) }
    }
}

impl std::fmt::Debug for Device {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Device")
            .field("raw", &"<raw vulkan device handle>")
            .field("ash", &self.ash)
            .finish()
    }
}

impl std::ops::Deref for Device {
    type Target = ash::Device;

    fn deref(&self) -> &Self::Target {
        &self.raw
    }
}