1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // Copyright 2018 Jacob Lifshay
3 #![cfg_attr(feature = "cargo-clippy", allow(clippy::unneeded_field_pattern))]
5 use constants::IMAGE_ALIGNMENT;
6 use device_memory::DeviceMemoryLayout;
7 use handle::SharedHandle;
11 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
12 pub enum SupportedTilings {
17 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
24 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
25 pub enum ImageMultisampleCount {
30 #[derive(Copy, Clone, Debug)]
31 pub struct ImageProperties {
32 pub supported_tilings: SupportedTilings,
33 pub format: api::VkFormat,
34 pub extents: api::VkExtent3D,
35 pub array_layers: u32,
37 pub multisample_count: ImageMultisampleCount,
38 pub swapchain_present_tiling: Option<Tiling>,
41 #[derive(Copy, Clone, Debug)]
42 pub struct ImageComputedProperties {
43 pub pixel_size_in_bytes: usize,
44 pub memory_layout: DeviceMemoryLayout,
47 impl ImageProperties {
48 pub fn get_tiling(&self, image_layout: api::VkImageLayout) -> Tiling {
49 if image_layout == api::VK_IMAGE_LAYOUT_PRESENT_SRC_KHR {
50 self.swapchain_present_tiling.unwrap()
52 match self.supported_tilings {
53 SupportedTilings::LinearOnly => Tiling::Linear,
54 SupportedTilings::Any => Tiling::Tiled,
58 pub fn computed_properties(&self) -> ImageComputedProperties {
61 supported_tilings: SupportedTilings::Any,
62 format: api::VK_FORMAT_R8G8B8A8_UNORM,
66 multisample_count: ImageMultisampleCount::Count1,
67 swapchain_present_tiling: _,
69 let pixel_size_in_bytes = 4;
70 ImageComputedProperties {
72 memory_layout: DeviceMemoryLayout::calculate(
74 .checked_mul(extents.width as usize)
76 .checked_mul(extents.height as usize)
78 .checked_mul(extents.depth as usize)
80 .checked_mul(array_layers as usize)
86 _ => unimplemented!("ImageProperties::computed_properties({:?})", self),
92 pub struct ImageMemory {
93 pub device_memory: SharedHandle<api::VkDeviceMemory>,
99 pub properties: ImageProperties,
100 pub memory: Option<ImageMemory>,
103 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
104 pub enum ImageViewType {
115 pub fn from(v: api::VkImageViewType) -> Self {
117 api::VK_IMAGE_VIEW_TYPE_1D => ImageViewType::Type1D,
118 api::VK_IMAGE_VIEW_TYPE_2D => ImageViewType::Type2D,
119 api::VK_IMAGE_VIEW_TYPE_3D => ImageViewType::Type3D,
120 api::VK_IMAGE_VIEW_TYPE_CUBE => ImageViewType::Cube,
121 api::VK_IMAGE_VIEW_TYPE_1D_ARRAY => ImageViewType::Array1D,
122 api::VK_IMAGE_VIEW_TYPE_2D_ARRAY => ImageViewType::Array2D,
123 api::VK_IMAGE_VIEW_TYPE_CUBE_ARRAY => ImageViewType::CubeArray,
129 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
130 pub enum ComponentSwizzle {
140 pub struct InvalidVkComponentSwizzle;
142 impl fmt::Display for InvalidVkComponentSwizzle {
143 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144 write!(f, "invalid VkComponentSwizzle")
148 impl error::Error for InvalidVkComponentSwizzle {}
150 impl ComponentSwizzle {
152 v: api::VkComponentSwizzle,
154 ) -> Result<Self, InvalidVkComponentSwizzle> {
156 api::VK_COMPONENT_SWIZZLE_IDENTITY => Ok(identity),
157 api::VK_COMPONENT_SWIZZLE_ZERO => Ok(ComponentSwizzle::Zero),
158 api::VK_COMPONENT_SWIZZLE_ONE => Ok(ComponentSwizzle::One),
159 api::VK_COMPONENT_SWIZZLE_R => Ok(ComponentSwizzle::X),
160 api::VK_COMPONENT_SWIZZLE_G => Ok(ComponentSwizzle::Y),
161 api::VK_COMPONENT_SWIZZLE_B => Ok(ComponentSwizzle::Z),
162 api::VK_COMPONENT_SWIZZLE_A => Ok(ComponentSwizzle::W),
163 _ => Err(InvalidVkComponentSwizzle),
168 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
169 pub struct ComponentMapping {
170 pub x: ComponentSwizzle,
171 pub y: ComponentSwizzle,
172 pub z: ComponentSwizzle,
173 pub w: ComponentSwizzle,
176 impl ComponentMapping {
177 pub const IDENTITY: ComponentMapping = ComponentMapping {
178 x: ComponentSwizzle::X,
179 y: ComponentSwizzle::Y,
180 z: ComponentSwizzle::Z,
181 w: ComponentSwizzle::W,
183 pub fn from(v: api::VkComponentMapping) -> Result<Self, InvalidVkComponentSwizzle> {
185 x: ComponentSwizzle::from(v.r, ComponentSwizzle::X)?,
186 y: ComponentSwizzle::from(v.g, ComponentSwizzle::Y)?,
187 z: ComponentSwizzle::from(v.b, ComponentSwizzle::Z)?,
188 w: ComponentSwizzle::from(v.a, ComponentSwizzle::W)?,
193 impl Default for ComponentMapping {
194 fn default() -> Self {
200 pub struct ImageView {
201 pub image: SharedHandle<api::VkImage>,
202 pub view_type: ImageViewType,
203 pub format: api::VkFormat,
204 pub component_mapping: ComponentMapping,
205 pub subresource_range: api::VkImageSubresourceRange,