bort_vk/
image_dimensions.rs

1use ash::vk;
2
3#[derive(Copy, Clone, Debug, PartialEq, Eq)]
4pub enum ImageDimensions {
5    Dim1d {
6        width: u32,
7        array_layers: u32,
8    },
9    Dim2d {
10        width: u32,
11        height: u32,
12        array_layers: u32,
13    },
14    Dim3d {
15        width: u32,
16        height: u32,
17        depth: u32,
18    },
19}
20
21impl ImageDimensions {
22    pub fn new_from_extent_and_layers(extent_3d: vk::Extent3D, array_layers: u32) -> Self {
23        if array_layers > 1 {
24            if extent_3d.height > 1 {
25                Self::new_2d_array(extent_3d.width, extent_3d.height, array_layers)
26            } else {
27                Self::new_1d_array(extent_3d.width, array_layers)
28            }
29        } else {
30            if extent_3d.depth > 1 {
31                Self::new_3d(extent_3d.width, extent_3d.height, extent_3d.depth)
32            } else if extent_3d.height > 1 {
33                Self::new_2d(extent_3d.width, extent_3d.height)
34            } else {
35                Self::new_1d(extent_3d.width)
36            }
37        }
38    }
39
40    pub fn new_1d(width: u32) -> Self {
41        Self::Dim1d {
42            width,
43            array_layers: 1,
44        }
45    }
46
47    pub fn new_1d_array(width: u32, array_layers: u32) -> Self {
48        Self::Dim1d {
49            width,
50            array_layers,
51        }
52    }
53
54    pub fn new_2d(width: u32, height: u32) -> Self {
55        Self::Dim2d {
56            width,
57            height,
58            array_layers: 1,
59        }
60    }
61
62    pub fn new_2d_array(width: u32, height: u32, array_layers: u32) -> Self {
63        Self::Dim2d {
64            width,
65            height,
66            array_layers,
67        }
68    }
69
70    pub fn new_3d(width: u32, height: u32, depth: u32) -> Self {
71        Self::Dim3d {
72            width,
73            height,
74            depth,
75        }
76    }
77
78    pub fn width(&self) -> u32 {
79        match *self {
80            ImageDimensions::Dim1d { width, .. } => width,
81            ImageDimensions::Dim2d { width, .. } => width,
82            ImageDimensions::Dim3d { width, .. } => width,
83        }
84    }
85
86    pub fn height(&self) -> u32 {
87        match *self {
88            ImageDimensions::Dim1d { .. } => 1,
89            ImageDimensions::Dim2d { height, .. } => height,
90            ImageDimensions::Dim3d { height, .. } => height,
91        }
92    }
93
94    pub fn width_height(&self) -> [u32; 2] {
95        [self.width(), self.height()]
96    }
97
98    pub fn depth(&self) -> u32 {
99        match *self {
100            ImageDimensions::Dim1d { .. } => 1,
101            ImageDimensions::Dim2d { .. } => 1,
102            ImageDimensions::Dim3d { depth, .. } => depth,
103        }
104    }
105
106    pub fn extent_3d(&self) -> vk::Extent3D {
107        vk::Extent3D {
108            width: self.width(),
109            height: self.height(),
110            depth: self.depth(),
111        }
112    }
113
114    pub fn array_layers(&self) -> u32 {
115        match *self {
116            ImageDimensions::Dim1d { array_layers, .. } => array_layers,
117            ImageDimensions::Dim2d { array_layers, .. } => array_layers,
118            ImageDimensions::Dim3d { .. } => 1,
119        }
120    }
121
122    pub fn num_texels(&self) -> u32 {
123        self.width() * self.height() * self.depth() * self.array_layers()
124    }
125
126    pub fn image_type(&self) -> vk::ImageType {
127        match *self {
128            ImageDimensions::Dim1d { .. } => vk::ImageType::TYPE_1D,
129            ImageDimensions::Dim2d { .. } => vk::ImageType::TYPE_2D,
130            ImageDimensions::Dim3d { .. } => vk::ImageType::TYPE_3D,
131        }
132    }
133
134    pub fn default_image_view_type(&self) -> vk::ImageViewType {
135        match self {
136            Self::Dim1d {
137                array_layers: 1, ..
138            } => vk::ImageViewType::TYPE_1D,
139            Self::Dim1d { .. } => vk::ImageViewType::TYPE_1D_ARRAY,
140            Self::Dim2d {
141                array_layers: 1, ..
142            } => vk::ImageViewType::TYPE_2D,
143            Self::Dim2d { .. } => vk::ImageViewType::TYPE_2D_ARRAY,
144            Self::Dim3d { .. } => vk::ImageViewType::TYPE_3D,
145        }
146    }
147
148    pub fn whole_viewport(&self) -> vk::Viewport {
149        vk::Viewport {
150            x: 0.,
151            y: 0.,
152            width: self.width() as f32,
153            height: self.height() as f32,
154            min_depth: 0.,
155            max_depth: 1., // not to be confused with `self.depth()`
156        }
157    }
158}
159
160impl Default for ImageDimensions {
161    fn default() -> Self {
162        Self::Dim1d {
163            width: 1,
164            array_layers: 1,
165        }
166    }
167}