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 Buffer.
pub struct Buffer {
    buffer: vk::Buffer,
    allocation: Allocation,
    render_device: Arc<RenderDevice>,
}

impl Buffer {
    /// 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::BufferCreateInfo,
        memory_property_flags: vk::MemoryPropertyFlags,
    ) -> Result<Self, GraphicsError> {
        let (buffer, allocation) = unsafe {
            render_device
                .memory()
                .allocate_buffer(create_info, memory_property_flags)?
        };
        Ok(Self {
            buffer,
            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.buffer,
            vk::ObjectType::BUFFER,
            name,
        );
    }

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

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

impl Drop for Buffer {
    fn drop(&mut self) {
        unsafe {
            self.render_device
                .memory()
                .free_buffer(self.buffer, self.allocation.clone());
        }
    }
}

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