working on getting vulkan_minimal_compute to run
[kazan.git] / vulkan-driver / src / image.rs
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))]
4 use api;
5 use constants::IMAGE_ALIGNMENT;
6 use device_memory::DeviceMemoryLayout;
7 use handle::SharedHandle;
8 use std::error;
9 use std::fmt;
10
11 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
12 pub enum SupportedTilings {
13 Any,
14 LinearOnly,
15 }
16
17 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
18 pub enum Tiling {
19 Linear,
20 #[allow(dead_code)]
21 Tiled,
22 }
23
24 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
25 pub enum ImageMultisampleCount {
26 Count1,
27 Count4,
28 }
29
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,
36 pub mip_levels: u32,
37 pub multisample_count: ImageMultisampleCount,
38 pub swapchain_present_tiling: Option<Tiling>,
39 }
40
41 #[derive(Copy, Clone, Debug)]
42 pub struct ImageComputedProperties {
43 pub pixel_size_in_bytes: usize,
44 pub memory_layout: DeviceMemoryLayout,
45 }
46
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()
51 } else {
52 match self.supported_tilings {
53 SupportedTilings::LinearOnly => Tiling::Linear,
54 SupportedTilings::Any => Tiling::Tiled,
55 }
56 }
57 }
58 pub fn computed_properties(&self) -> ImageComputedProperties {
59 match *self {
60 Self {
61 supported_tilings: SupportedTilings::Any,
62 format: api::VK_FORMAT_R8G8B8A8_UNORM,
63 extents,
64 array_layers,
65 mip_levels: 1,
66 multisample_count: ImageMultisampleCount::Count1,
67 swapchain_present_tiling: _,
68 } => {
69 let pixel_size_in_bytes = 4;
70 ImageComputedProperties {
71 pixel_size_in_bytes,
72 memory_layout: DeviceMemoryLayout::calculate(
73 pixel_size_in_bytes
74 .checked_mul(extents.width as usize)
75 .unwrap()
76 .checked_mul(extents.height as usize)
77 .unwrap()
78 .checked_mul(extents.depth as usize)
79 .unwrap()
80 .checked_mul(array_layers as usize)
81 .unwrap(),
82 IMAGE_ALIGNMENT,
83 ),
84 }
85 }
86 _ => unimplemented!("ImageProperties::computed_properties({:?})", self),
87 }
88 }
89 }
90
91 #[derive(Debug)]
92 pub struct ImageMemory {
93 pub device_memory: SharedHandle<api::VkDeviceMemory>,
94 pub offset: usize,
95 }
96
97 #[derive(Debug)]
98 pub struct Image {
99 pub properties: ImageProperties,
100 pub memory: Option<ImageMemory>,
101 }
102
103 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
104 pub enum ImageViewType {
105 Type1D,
106 Type2D,
107 Type3D,
108 Cube,
109 Array1D,
110 Array2D,
111 CubeArray,
112 }
113
114 impl ImageViewType {
115 pub fn from(v: api::VkImageViewType) -> Self {
116 match v {
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,
124 _ => unreachable!(),
125 }
126 }
127 }
128
129 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
130 pub enum ComponentSwizzle {
131 Zero,
132 One,
133 X,
134 Y,
135 Z,
136 W,
137 }
138
139 #[derive(Debug)]
140 pub struct InvalidVkComponentSwizzle;
141
142 impl fmt::Display for InvalidVkComponentSwizzle {
143 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144 write!(f, "invalid VkComponentSwizzle")
145 }
146 }
147
148 impl error::Error for InvalidVkComponentSwizzle {}
149
150 impl ComponentSwizzle {
151 pub fn from(
152 v: api::VkComponentSwizzle,
153 identity: Self,
154 ) -> Result<Self, InvalidVkComponentSwizzle> {
155 match v {
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),
164 }
165 }
166 }
167
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,
174 }
175
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,
182 };
183 pub fn from(v: api::VkComponentMapping) -> Result<Self, InvalidVkComponentSwizzle> {
184 Ok(Self {
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)?,
189 })
190 }
191 }
192
193 impl Default for ComponentMapping {
194 fn default() -> Self {
195 Self::IDENTITY
196 }
197 }
198
199 #[derive(Debug)]
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,
206 }