asche/
image.rs

1use std::sync::Arc;
2
3use erupt::vk;
4
5use crate::{context::Context, memory_allocator::MemoryAllocator, Lifetime};
6
7/// Wraps an image.
8#[derive(Debug)]
9pub struct Image<LT: Lifetime> {
10    raw: vk::Image,
11    allocation: vk_alloc::Allocation<LT>,
12    memory_allocator: Arc<MemoryAllocator<LT>>,
13    context: Arc<Context>,
14}
15
16impl<LT: Lifetime> Drop for Image<LT> {
17    fn drop(&mut self) {
18        unsafe {
19            self.context.device.destroy_image(Some(self.raw), None);
20            self.memory_allocator
21                .allocator
22                .deallocate(&self.context.device, &self.allocation)
23                .expect("can't free image allocation");
24        };
25    }
26}
27
28impl<LT: Lifetime> Image<LT> {
29    pub(crate) fn new(
30        raw: vk::Image,
31        allocation: vk_alloc::Allocation<LT>,
32        memory_allocator: Arc<MemoryAllocator<LT>>,
33        context: Arc<Context>,
34    ) -> Self {
35        Self {
36            raw,
37            allocation,
38            memory_allocator,
39            context,
40        }
41    }
42
43    /// The raw Vulkan image handle.
44    #[inline]
45    pub fn raw(&self) -> vk::Image {
46        self.raw
47    }
48}
49
50/// Wraps an image view.
51#[derive(Debug)]
52pub struct ImageView {
53    raw: vk::ImageView,
54    context: Arc<Context>,
55}
56
57impl Drop for ImageView {
58    fn drop(&mut self) {
59        unsafe {
60            self.context.device.destroy_image_view(Some(self.raw), None);
61        };
62    }
63}
64
65impl ImageView {
66    pub(crate) fn new(raw: vk::ImageView, context: Arc<Context>) -> Self {
67        Self { raw, context }
68    }
69
70    /// The raw Vulkan image view handle.
71    #[inline]
72    pub fn raw(&self) -> vk::ImageView {
73        self.raw
74    }
75}
76
77/// Wraps a sampler.
78#[derive(Debug)]
79pub struct Sampler {
80    raw: vk::Sampler,
81    context: Arc<Context>,
82}
83
84impl Drop for Sampler {
85    fn drop(&mut self) {
86        unsafe {
87            self.context.device.destroy_sampler(Some(self.raw), None);
88        };
89    }
90}
91
92impl Sampler {
93    pub(crate) fn new(raw: vk::Sampler, context: Arc<Context>) -> Self {
94        Self { raw, context }
95    }
96
97    /// The raw Vulkan sampler handle.
98    #[inline]
99    pub fn raw(&self) -> vk::Sampler {
100        self.raw
101    }
102}