1 /*
2 * Copyright © 2016 Intel Corporation
3 * Copyright © 2019 Google LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef U_FORMAT_VK_H
26 #define U_FORMAT_VK_H
27
28 #include <vulkan/vulkan_core.h>
29 #include "util/format/u_format.h"
30 #include "util/u_math.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 extern const enum pipe_format vk_format_map[];
37
38 enum pipe_format
39 vk_format_to_pipe_format(VkFormat vkformat);
40
41 VkFormat
42 vk_format_from_pipe_format(enum pipe_format format);
43
44 VkImageAspectFlags
45 vk_format_aspects(VkFormat format);
46
47 static inline const struct util_format_description *
vk_format_description(VkFormat format)48 vk_format_description(VkFormat format)
49 {
50 return util_format_description(vk_format_to_pipe_format(format));
51 }
52
53 static inline bool
vk_format_is_color(VkFormat format)54 vk_format_is_color(VkFormat format)
55 {
56 return vk_format_aspects(format) == VK_IMAGE_ASPECT_COLOR_BIT;
57 }
58
59 static inline bool
vk_format_is_depth_or_stencil(VkFormat format)60 vk_format_is_depth_or_stencil(VkFormat format)
61 {
62 const VkImageAspectFlags aspects = vk_format_aspects(format);
63 return aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
64 }
65
66 static inline bool
vk_format_has_depth(VkFormat format)67 vk_format_has_depth(VkFormat format)
68 {
69 const VkImageAspectFlags aspects = vk_format_aspects(format);
70 return aspects & VK_IMAGE_ASPECT_DEPTH_BIT;
71 }
72
73 static inline bool
vk_format_has_stencil(VkFormat format)74 vk_format_has_stencil(VkFormat format)
75 {
76 const VkImageAspectFlags aspects = vk_format_aspects(format);
77 return aspects & VK_IMAGE_ASPECT_STENCIL_BIT;
78 }
79
80 static inline VkFormat
vk_format_depth_only(VkFormat format)81 vk_format_depth_only(VkFormat format)
82 {
83 assert(vk_format_has_depth(format));
84 switch (format) {
85 case VK_FORMAT_D16_UNORM_S8_UINT:
86 return VK_FORMAT_D16_UNORM;
87 case VK_FORMAT_D24_UNORM_S8_UINT:
88 return VK_FORMAT_X8_D24_UNORM_PACK32;
89 case VK_FORMAT_D32_SFLOAT_S8_UINT:
90 return VK_FORMAT_D32_SFLOAT;
91 default:
92 return format;
93 }
94 }
95
96 static inline bool
vk_format_has_float_depth(VkFormat format)97 vk_format_has_float_depth(VkFormat format)
98 {
99 switch (format) {
100 case VK_FORMAT_D32_SFLOAT:
101 case VK_FORMAT_D32_SFLOAT_S8_UINT:
102 return true;
103 default:
104 return false;
105 }
106 }
107
108 static inline VkFormat
vk_format_stencil_only(VkFormat format)109 vk_format_stencil_only(VkFormat format)
110 {
111 assert(vk_format_has_stencil(format));
112 return VK_FORMAT_S8_UINT;
113 }
114
115 void vk_component_mapping_to_pipe_swizzle(VkComponentMapping mapping,
116 unsigned char out_swizzle[4]);
117
118 static inline bool
vk_format_is_int(VkFormat format)119 vk_format_is_int(VkFormat format)
120 {
121 return util_format_is_pure_integer(vk_format_to_pipe_format(format));
122 }
123
124 static inline bool
vk_format_is_sint(VkFormat format)125 vk_format_is_sint(VkFormat format)
126 {
127 return util_format_is_pure_sint(vk_format_to_pipe_format(format));
128 }
129
130 static inline bool
vk_format_is_uint(VkFormat format)131 vk_format_is_uint(VkFormat format)
132 {
133 return util_format_is_pure_uint(vk_format_to_pipe_format(format));
134 }
135
136 static inline bool
vk_format_is_unorm(VkFormat format)137 vk_format_is_unorm(VkFormat format)
138 {
139 return util_format_is_unorm(vk_format_to_pipe_format(format));
140 }
141
142 static inline bool
vk_format_is_snorm(VkFormat format)143 vk_format_is_snorm(VkFormat format)
144 {
145 return util_format_is_snorm(vk_format_to_pipe_format(format));
146 }
147
148 static inline bool
vk_format_is_float(VkFormat format)149 vk_format_is_float(VkFormat format)
150 {
151 return util_format_is_float(vk_format_to_pipe_format(format));
152 }
153
154 static inline bool
vk_format_is_srgb(VkFormat format)155 vk_format_is_srgb(VkFormat format)
156 {
157 return util_format_is_srgb(vk_format_to_pipe_format(format));
158 }
159
vk_format_is_alpha(VkFormat format)160 static inline bool vk_format_is_alpha(VkFormat format)
161 {
162 return util_format_is_alpha(vk_format_to_pipe_format(format));
163 }
164
vk_format_is_alpha_on_msb(VkFormat vk_format)165 static inline bool vk_format_is_alpha_on_msb(VkFormat vk_format)
166 {
167 const struct util_format_description *desc =
168 vk_format_description(vk_format);
169
170 return (desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
171 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
172 #if UTIL_ARCH_BIG_ENDIAN
173 desc->swizzle[3] == PIPE_SWIZZLE_X;
174 #else
175 desc->swizzle[3] == PIPE_SWIZZLE_W;
176 #endif
177 }
178
179 static inline unsigned
vk_format_get_blocksize(VkFormat format)180 vk_format_get_blocksize(VkFormat format)
181 {
182 return util_format_get_blocksize(vk_format_to_pipe_format(format));
183 }
184
185 static inline unsigned
vk_format_get_blockwidth(VkFormat format)186 vk_format_get_blockwidth(VkFormat format)
187 {
188 return util_format_get_blockwidth(vk_format_to_pipe_format(format));
189 }
190
191 static inline unsigned
vk_format_get_blockheight(VkFormat format)192 vk_format_get_blockheight(VkFormat format)
193 {
194 return util_format_get_blockheight(vk_format_to_pipe_format(format));
195 }
196
197 static inline bool
vk_format_is_compressed(VkFormat format)198 vk_format_is_compressed(VkFormat format)
199 {
200 /* this includes 4:2:2 formats, which are compressed formats for vulkan */
201 return vk_format_get_blockwidth(format) > 1;
202 }
203
204 static inline bool
vk_format_is_block_compressed(VkFormat format)205 vk_format_is_block_compressed(VkFormat format)
206 {
207 return util_format_is_compressed(vk_format_to_pipe_format(format));
208 }
209
210 static inline unsigned
vk_format_get_component_bits(VkFormat format,enum util_format_colorspace colorspace,unsigned component)211 vk_format_get_component_bits(VkFormat format, enum util_format_colorspace colorspace,
212 unsigned component)
213 {
214 return util_format_get_component_bits(vk_format_to_pipe_format(format),
215 colorspace,
216 component);
217 }
218
219 static inline unsigned
vk_format_get_nr_components(VkFormat format)220 vk_format_get_nr_components(VkFormat format)
221 {
222 return util_format_get_nr_components(vk_format_to_pipe_format(format));
223 }
224
225 static inline bool
vk_format_has_alpha(VkFormat format)226 vk_format_has_alpha(VkFormat format)
227 {
228 return util_format_has_alpha(vk_format_to_pipe_format(format));
229 }
230
231 static inline unsigned
vk_format_get_blocksizebits(VkFormat format)232 vk_format_get_blocksizebits(VkFormat format)
233 {
234 return util_format_get_blocksizebits(vk_format_to_pipe_format(format));
235 }
236
237 static inline unsigned
vk_format_get_bpc(VkFormat format)238 vk_format_get_bpc(VkFormat format)
239 {
240 const struct util_format_description *desc =
241 vk_format_description(format);
242 unsigned bpc = 0;
243 for (unsigned i = 0; i < desc->nr_channels; i++) {
244 if (desc->channel[i].type == UTIL_FORMAT_TYPE_VOID)
245 continue;
246
247 assert(bpc == 0 || bpc == desc->channel[i].size);
248 bpc = desc->channel[i].size;
249 }
250 return bpc;
251 }
252
253 VkFormat
254 vk_format_get_plane_format(VkFormat format, unsigned plane_id);
255
256 VkFormat
257 vk_format_get_aspect_format(VkFormat format, const VkImageAspectFlags aspect);
258
259 struct vk_format_ycbcr_plane {
260 /* RGBA format for this plane */
261 VkFormat format;
262
263 /* Whether this plane contains chroma channels */
264 bool has_chroma;
265
266 /* For downscaling of YUV planes */
267 uint8_t denominator_scales[2];
268
269 /* How to map sampled ycbcr planes to a single 4 component element.
270 *
271 * We use uint8_t for compactness but it's actually VkComponentSwizzle.
272 */
273 uint8_t ycbcr_swizzle[4];
274 };
275
276 struct vk_format_ycbcr_info {
277 uint8_t n_planes;
278 struct vk_format_ycbcr_plane planes[3];
279 };
280
281 const struct vk_format_ycbcr_info *vk_format_get_ycbcr_info(VkFormat format);
282
283 static inline unsigned
vk_format_get_plane_count(VkFormat format)284 vk_format_get_plane_count(VkFormat format)
285 {
286 const struct vk_format_ycbcr_info *ycbcr_info =
287 vk_format_get_ycbcr_info(format);
288 return ycbcr_info ? ycbcr_info->n_planes : 1;
289 }
290
291 static inline unsigned
vk_format_get_plane_width(VkFormat format,unsigned plane,unsigned width)292 vk_format_get_plane_width(VkFormat format, unsigned plane, unsigned width)
293 {
294 const struct vk_format_ycbcr_info *ycbcr_info =
295 vk_format_get_ycbcr_info(format);
296 const uint8_t width_scale = ycbcr_info ?
297 ycbcr_info->planes[plane].denominator_scales[0] : 1;
298 return width / width_scale;
299 }
300
301 static inline unsigned
vk_format_get_plane_height(VkFormat format,unsigned plane,unsigned height)302 vk_format_get_plane_height(VkFormat format, unsigned plane, unsigned height)
303 {
304 const struct vk_format_ycbcr_info *ycbcr_info =
305 vk_format_get_ycbcr_info(format);
306 const uint8_t height_scale = ycbcr_info ?
307 ycbcr_info->planes[plane].denominator_scales[1] : 1;
308 return height / height_scale;
309 }
310
311 VkClearColorValue
312 vk_swizzle_color_value(VkClearColorValue color,
313 VkComponentMapping swizzle, bool is_int);
314
315 VkFormat
316 vk_select_android_external_format(const void *next, VkFormat default_format);
317
318 #ifdef __cplusplus
319 }
320 #endif
321
322 #endif
323