xref: /aosp_15_r20/external/mesa3d/src/vulkan/util/vk_format.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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(enum 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 VkFormat
vk_format_stencil_only(VkFormat format)97 vk_format_stencil_only(VkFormat format)
98 {
99    assert(vk_format_has_stencil(format));
100    return VK_FORMAT_S8_UINT;
101 }
102 
103 void vk_component_mapping_to_pipe_swizzle(VkComponentMapping mapping,
104                                           unsigned char out_swizzle[4]);
105 
106 static inline bool
vk_format_is_int(VkFormat format)107 vk_format_is_int(VkFormat format)
108 {
109    return util_format_is_pure_integer(vk_format_to_pipe_format(format));
110 }
111 
112 static inline bool
vk_format_is_sint(VkFormat format)113 vk_format_is_sint(VkFormat format)
114 {
115    return util_format_is_pure_sint(vk_format_to_pipe_format(format));
116 }
117 
118 static inline bool
vk_format_is_uint(VkFormat format)119 vk_format_is_uint(VkFormat format)
120 {
121    return util_format_is_pure_uint(vk_format_to_pipe_format(format));
122 }
123 
124 static inline bool
vk_format_is_unorm(VkFormat format)125 vk_format_is_unorm(VkFormat format)
126 {
127    return util_format_is_unorm(vk_format_to_pipe_format(format));
128 }
129 
130 static inline bool
vk_format_is_snorm(VkFormat format)131 vk_format_is_snorm(VkFormat format)
132 {
133    return util_format_is_snorm(vk_format_to_pipe_format(format));
134 }
135 
136 static inline bool
vk_format_is_float(VkFormat format)137 vk_format_is_float(VkFormat format)
138 {
139    return util_format_is_float(vk_format_to_pipe_format(format));
140 }
141 
142 static inline bool
vk_format_is_srgb(VkFormat format)143 vk_format_is_srgb(VkFormat format)
144 {
145    return util_format_is_srgb(vk_format_to_pipe_format(format));
146 }
147 
vk_format_is_alpha(VkFormat format)148 static inline bool vk_format_is_alpha(VkFormat format)
149 {
150    return util_format_is_alpha(vk_format_to_pipe_format(format));
151 }
152 
vk_format_is_alpha_on_msb(VkFormat vk_format)153 static inline bool vk_format_is_alpha_on_msb(VkFormat vk_format)
154 {
155    const struct util_format_description *desc =
156       vk_format_description(vk_format);
157 
158    return (desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
159            desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
160 #if UTIL_ARCH_BIG_ENDIAN
161           desc->swizzle[3] == PIPE_SWIZZLE_X;
162 #else
163           desc->swizzle[3] == PIPE_SWIZZLE_W;
164 #endif
165 }
166 
167 static inline unsigned
vk_format_get_blocksize(VkFormat format)168 vk_format_get_blocksize(VkFormat format)
169 {
170    return util_format_get_blocksize(vk_format_to_pipe_format(format));
171 }
172 
173 static inline unsigned
vk_format_get_blockwidth(VkFormat format)174 vk_format_get_blockwidth(VkFormat format)
175 {
176    return util_format_get_blockwidth(vk_format_to_pipe_format(format));
177 }
178 
179 static inline unsigned
vk_format_get_blockheight(VkFormat format)180 vk_format_get_blockheight(VkFormat format)
181 {
182    return util_format_get_blockheight(vk_format_to_pipe_format(format));
183 }
184 
185 static inline bool
vk_format_is_compressed(VkFormat format)186 vk_format_is_compressed(VkFormat format)
187 {
188    /* this includes 4:2:2 formats, which are compressed formats for vulkan */
189    return vk_format_get_blockwidth(format) > 1;
190 }
191 
192 static inline bool
vk_format_is_block_compressed(VkFormat format)193 vk_format_is_block_compressed(VkFormat format)
194 {
195    return util_format_is_compressed(vk_format_to_pipe_format(format));
196 }
197 
198 static inline unsigned
vk_format_get_component_bits(VkFormat format,enum util_format_colorspace colorspace,unsigned component)199 vk_format_get_component_bits(VkFormat format, enum util_format_colorspace colorspace,
200                              unsigned component)
201 {
202    return util_format_get_component_bits(vk_format_to_pipe_format(format),
203                                          colorspace,
204                                          component);
205 }
206 
207 static inline unsigned
vk_format_get_nr_components(VkFormat format)208 vk_format_get_nr_components(VkFormat format)
209 {
210    return util_format_get_nr_components(vk_format_to_pipe_format(format));
211 }
212 
213 static inline bool
vk_format_has_alpha(VkFormat format)214 vk_format_has_alpha(VkFormat format)
215 {
216    return util_format_has_alpha(vk_format_to_pipe_format(format));
217 }
218 
219 static inline unsigned
vk_format_get_blocksizebits(VkFormat format)220 vk_format_get_blocksizebits(VkFormat format)
221 {
222    return util_format_get_blocksizebits(vk_format_to_pipe_format(format));
223 }
224 
225 VkFormat
226 vk_format_get_plane_format(VkFormat format, unsigned plane_id);
227 
228 VkFormat
229 vk_format_get_aspect_format(VkFormat format, const VkImageAspectFlags aspect);
230 
231 struct vk_format_ycbcr_plane {
232    /* RGBA format for this plane */
233    VkFormat format;
234 
235    /* Whether this plane contains chroma channels */
236    bool has_chroma;
237 
238    /* For downscaling of YUV planes */
239    uint8_t denominator_scales[2];
240 
241    /* How to map sampled ycbcr planes to a single 4 component element.
242     *
243     * We use uint8_t for compactness but it's actually VkComponentSwizzle.
244     */
245    uint8_t ycbcr_swizzle[4];
246 };
247 
248 struct vk_format_ycbcr_info {
249    uint8_t n_planes;
250    struct vk_format_ycbcr_plane planes[3];
251 };
252 
253 const struct vk_format_ycbcr_info *vk_format_get_ycbcr_info(VkFormat format);
254 
255 static inline unsigned
vk_format_get_plane_count(VkFormat format)256 vk_format_get_plane_count(VkFormat format)
257 {
258    const struct vk_format_ycbcr_info *ycbcr_info =
259       vk_format_get_ycbcr_info(format);
260    return ycbcr_info ? ycbcr_info->n_planes : 1;
261 }
262 
263 static inline unsigned
vk_format_get_plane_width(VkFormat format,unsigned plane,unsigned width)264 vk_format_get_plane_width(VkFormat format, unsigned plane, unsigned width)
265 {
266    const struct vk_format_ycbcr_info *ycbcr_info =
267       vk_format_get_ycbcr_info(format);
268    const uint8_t width_scale = ycbcr_info ?
269          ycbcr_info->planes[plane].denominator_scales[0] : 1;
270    return width / width_scale;
271 }
272 
273 static inline unsigned
vk_format_get_plane_height(VkFormat format,unsigned plane,unsigned height)274 vk_format_get_plane_height(VkFormat format, unsigned plane, unsigned height)
275 {
276    const struct vk_format_ycbcr_info *ycbcr_info =
277       vk_format_get_ycbcr_info(format);
278    const uint8_t height_scale = ycbcr_info ?
279          ycbcr_info->planes[plane].denominator_scales[1] : 1;
280    return height / height_scale;
281 }
282 
283 VkClearColorValue
284 vk_swizzle_color_value(VkClearColorValue color,
285                        VkComponentMapping swizzle, bool is_int);
286 
287 VkFormat
288 vk_select_android_external_format(const void *next, VkFormat default_format);
289 
290 #ifdef __cplusplus
291 }
292 #endif
293 
294 #endif
295