1 /* Copyright (c) 2017-2020 Hans-Kristian Arntzen
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #pragma once
24
25 #include "vulkan_headers.hpp"
26 #include "texture_format.hpp"
27
28 namespace Vulkan
29 {
format_is_srgb(VkFormat format)30 static inline bool format_is_srgb(VkFormat format)
31 {
32 switch (format)
33 {
34 case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
35 case VK_FORMAT_R8G8B8A8_SRGB:
36 case VK_FORMAT_B8G8R8A8_SRGB:
37 case VK_FORMAT_R8_SRGB:
38 case VK_FORMAT_R8G8_SRGB:
39 case VK_FORMAT_R8G8B8_SRGB:
40 case VK_FORMAT_B8G8R8_SRGB:
41 return true;
42
43 default:
44 return false;
45 }
46 }
47
format_has_depth_aspect(VkFormat format)48 static inline bool format_has_depth_aspect(VkFormat format)
49 {
50 switch (format)
51 {
52 case VK_FORMAT_D16_UNORM:
53 case VK_FORMAT_D16_UNORM_S8_UINT:
54 case VK_FORMAT_D24_UNORM_S8_UINT:
55 case VK_FORMAT_D32_SFLOAT:
56 case VK_FORMAT_X8_D24_UNORM_PACK32:
57 case VK_FORMAT_D32_SFLOAT_S8_UINT:
58 return true;
59
60 default:
61 return false;
62 }
63 }
64
format_has_stencil_aspect(VkFormat format)65 static inline bool format_has_stencil_aspect(VkFormat format)
66 {
67 switch (format)
68 {
69 case VK_FORMAT_D16_UNORM_S8_UINT:
70 case VK_FORMAT_D24_UNORM_S8_UINT:
71 case VK_FORMAT_D32_SFLOAT_S8_UINT:
72 case VK_FORMAT_S8_UINT:
73 return true;
74
75 default:
76 return false;
77 }
78 }
79
format_has_depth_or_stencil_aspect(VkFormat format)80 static inline bool format_has_depth_or_stencil_aspect(VkFormat format)
81 {
82 return format_has_depth_aspect(format) || format_has_stencil_aspect(format);
83 }
84
format_to_aspect_mask(VkFormat format)85 static inline VkImageAspectFlags format_to_aspect_mask(VkFormat format)
86 {
87 switch (format)
88 {
89 case VK_FORMAT_UNDEFINED:
90 return 0;
91
92 case VK_FORMAT_S8_UINT:
93 return VK_IMAGE_ASPECT_STENCIL_BIT;
94
95 case VK_FORMAT_D16_UNORM_S8_UINT:
96 case VK_FORMAT_D24_UNORM_S8_UINT:
97 case VK_FORMAT_D32_SFLOAT_S8_UINT:
98 return VK_IMAGE_ASPECT_STENCIL_BIT | VK_IMAGE_ASPECT_DEPTH_BIT;
99
100 case VK_FORMAT_D16_UNORM:
101 case VK_FORMAT_D32_SFLOAT:
102 case VK_FORMAT_X8_D24_UNORM_PACK32:
103 return VK_IMAGE_ASPECT_DEPTH_BIT;
104
105 default:
106 return VK_IMAGE_ASPECT_COLOR_BIT;
107 }
108 }
109
format_align_dim(VkFormat format,uint32_t & width,uint32_t & height)110 static inline void format_align_dim(VkFormat format, uint32_t &width, uint32_t &height)
111 {
112 uint32_t align_width, align_height;
113 TextureFormatLayout::format_block_dim(format, align_width, align_height);
114 width = ((width + align_width - 1) / align_width) * align_width;
115 height = ((height + align_height - 1) / align_height) * align_height;
116 }
117
format_num_blocks(VkFormat format,uint32_t & width,uint32_t & height)118 static inline void format_num_blocks(VkFormat format, uint32_t &width, uint32_t &height)
119 {
120 uint32_t align_width, align_height;
121 TextureFormatLayout::format_block_dim(format, align_width, align_height);
122 width = (width + align_width - 1) / align_width;
123 height = (height + align_height - 1) / align_height;
124 }
125
format_get_layer_size(VkFormat format,VkImageAspectFlags aspect,unsigned width,unsigned height,unsigned depth)126 static inline VkDeviceSize format_get_layer_size(VkFormat format, VkImageAspectFlags aspect, unsigned width, unsigned height, unsigned depth)
127 {
128 uint32_t blocks_x = width;
129 uint32_t blocks_y = height;
130 format_num_blocks(format, blocks_x, blocks_y);
131 format_align_dim(format, width, height);
132
133 VkDeviceSize size = TextureFormatLayout::format_block_size(format, aspect) * depth * blocks_x * blocks_y;
134 return size;
135 }
136
137 enum class YCbCrFormat
138 {
139 YUV420P_3PLANE,
140 YUV444P_3PLANE,
141 YUV422P_3PLANE,
142 Count
143 };
144
format_ycbcr_num_planes(VkFormat format)145 static inline unsigned format_ycbcr_num_planes(VkFormat format)
146 {
147 switch (format)
148 {
149 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
150 case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
151 case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
152 case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
153 case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
154 case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
155 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
156 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
157 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
158 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
159 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
160 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
161 return 3;
162
163 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
164 case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
165 case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
166 case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
167 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
168 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
169 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
170 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
171 return 2;
172
173 default:
174 return 1;
175 }
176 }
177
format_ycbcr_num_planes(YCbCrFormat format)178 static inline unsigned format_ycbcr_num_planes(YCbCrFormat format)
179 {
180 switch (format)
181 {
182 case YCbCrFormat::YUV420P_3PLANE:
183 case YCbCrFormat::YUV422P_3PLANE:
184 case YCbCrFormat::YUV444P_3PLANE:
185 return 3;
186
187 default:
188 return 0;
189 }
190 }
191
format_ycbcr_downsample_dimensions(VkFormat format,VkImageAspectFlags aspect,uint32_t & width,uint32_t & height)192 static inline void format_ycbcr_downsample_dimensions(VkFormat format, VkImageAspectFlags aspect, uint32_t &width, uint32_t &height)
193 {
194 if (aspect == VK_IMAGE_ASPECT_PLANE_0_BIT)
195 return;
196
197 switch (format)
198 {
199 #define fmt(x, sub0, sub1) \
200 case VK_FORMAT_##x: \
201 width >>= sub0; \
202 height >>= sub1; \
203 break
204
205 fmt(G8_B8_R8_3PLANE_420_UNORM, 1, 1);
206 fmt(G8_B8R8_2PLANE_420_UNORM, 1, 1);
207 fmt(G8_B8_R8_3PLANE_422_UNORM, 1, 0);
208 fmt(G8_B8R8_2PLANE_422_UNORM, 1, 0);
209 fmt(G8_B8_R8_3PLANE_444_UNORM, 0, 0);
210
211 fmt(G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16, 1, 1);
212 fmt(G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16, 1, 0);
213 fmt(G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16, 0, 0);
214 fmt(G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16, 1, 1);
215 fmt(G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16, 1, 0);
216
217 fmt(G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16, 1, 1);
218 fmt(G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16, 1, 0);
219 fmt(G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16, 0, 0);
220 fmt(G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16, 1, 1);
221 fmt(G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16, 1, 0);
222
223 fmt(G16_B16_R16_3PLANE_420_UNORM, 1, 1);
224 fmt(G16_B16_R16_3PLANE_422_UNORM, 1, 0);
225 fmt(G16_B16_R16_3PLANE_444_UNORM, 0, 0);
226 fmt(G16_B16R16_2PLANE_420_UNORM, 1, 1);
227 fmt(G16_B16R16_2PLANE_422_UNORM, 1, 0);
228
229 default:
230 break;
231 }
232 #undef fmt
233 }
234
format_ycbcr_downsample_ratio_log2(YCbCrFormat format,unsigned dim,unsigned plane)235 static inline unsigned format_ycbcr_downsample_ratio_log2(YCbCrFormat format, unsigned dim, unsigned plane)
236 {
237 switch (format)
238 {
239 case YCbCrFormat::YUV420P_3PLANE:
240 return plane > 0 ? 1 : 0;
241 case YCbCrFormat::YUV422P_3PLANE:
242 return plane > 0 && dim == 0 ? 1 : 0;
243
244 default:
245 return 0;
246 }
247 }
248
format_ycbcr_plane_vk_format(YCbCrFormat format,unsigned plane)249 static inline VkFormat format_ycbcr_plane_vk_format(YCbCrFormat format, unsigned plane)
250 {
251 switch (format)
252 {
253 case YCbCrFormat::YUV420P_3PLANE:
254 return VK_FORMAT_R8_UNORM;
255 case YCbCrFormat::YUV422P_3PLANE:
256 return plane > 0 ? VK_FORMAT_R8G8_UNORM : VK_FORMAT_R8_UNORM;
257 case YCbCrFormat::YUV444P_3PLANE:
258 return VK_FORMAT_R8_UNORM;
259
260 default:
261 return VK_FORMAT_UNDEFINED;
262 }
263 }
264
format_ycbcr_planar_vk_format(YCbCrFormat format)265 static inline VkFormat format_ycbcr_planar_vk_format(YCbCrFormat format)
266 {
267 switch (format)
268 {
269 case YCbCrFormat::YUV420P_3PLANE:
270 return VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
271 case YCbCrFormat::YUV422P_3PLANE:
272 return VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM;
273 case YCbCrFormat::YUV444P_3PLANE:
274 return VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM;
275
276 default:
277 return VK_FORMAT_UNDEFINED;
278 }
279 }
280
281 }
282