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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use {
    crate::graphics::{vulkan_api::RenderDevice, GraphicsError},
    ash::vk,
    ccthw_ash_allocator::Allocation,
    std::sync::Arc,
};

/// RAII Vulkan Image.
pub struct Image {
    image: vk::Image,
    allocation: Allocation,
    render_device: Arc<RenderDevice>,
}

impl Image {
    /// Create a new Vulkan descriptor pool.
    ///
    /// # Safety
    ///
    /// Unsafe because:
    ///   - command pools must be destroyed before the Vulkan device is dropped.
    pub unsafe fn new(
        render_device: Arc<RenderDevice>,
        create_info: &vk::ImageCreateInfo,
        memory_property_flags: vk::MemoryPropertyFlags,
    ) -> Result<Self, GraphicsError> {
        let (image, allocation) = unsafe {
            render_device
                .memory()
                .allocate_image(create_info, memory_property_flags)?
        };
        Ok(Self {
            image,
            allocation,
            render_device,
        })
    }

    /// Set the name which shows up in Vulkan debug logs for this resource.
    pub fn set_debug_name(&self, name: impl Into<String>) {
        self.render_device.set_debug_name(
            self.image,
            vk::ObjectType::IMAGE,
            name,
        );
    }

    /// Get the backing memory allocation for the Image.
    pub fn allocation(&self) -> &Allocation {
        &self.allocation
    }

    /// Get the raw Vulkan command pool handle.
    pub fn raw(&self) -> vk::Image {
        self.image
    }
}

impl Drop for Image {
    fn drop(&mut self) {
        unsafe {
            self.render_device
                .memory()
                .free_image(self.image, self.allocation.clone());
        }
    }
}

impl std::fmt::Debug for Image {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Image")
            .field("image", &self.image)
            .field("allocation", &self.allocation)
            .finish()
    }
}