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 bool
vk_format_is_color(VkFormat format)48 vk_format_is_color(VkFormat format)
49 {
50 return vk_format_aspects(format) == VK_IMAGE_ASPECT_COLOR_BIT;
51 }
52
53 static inline bool
vk_format_is_depth_or_stencil(VkFormat format)54 vk_format_is_depth_or_stencil(VkFormat format)
55 {
56 const VkImageAspectFlags aspects = vk_format_aspects(format);
57 return aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
58 }
59
60 static inline bool
vk_format_has_depth(VkFormat format)61 vk_format_has_depth(VkFormat format)
62 {
63 const VkImageAspectFlags aspects = vk_format_aspects(format);
64 return aspects & VK_IMAGE_ASPECT_DEPTH_BIT;
65 }
66
67 static inline bool
vk_format_has_stencil(VkFormat format)68 vk_format_has_stencil(VkFormat format)
69 {
70 const VkImageAspectFlags aspects = vk_format_aspects(format);
71 return aspects & VK_IMAGE_ASPECT_STENCIL_BIT;
72 }
73
74 static inline VkFormat
vk_format_depth_only(VkFormat format)75 vk_format_depth_only(VkFormat format)
76 {
77 assert(vk_format_has_depth(format));
78 switch (format) {
79 case VK_FORMAT_D16_UNORM_S8_UINT:
80 return VK_FORMAT_D16_UNORM;
81 case VK_FORMAT_D24_UNORM_S8_UINT:
82 return VK_FORMAT_X8_D24_UNORM_PACK32;
83 case VK_FORMAT_D32_SFLOAT_S8_UINT:
84 return VK_FORMAT_D32_SFLOAT;
85 default:
86 return format;
87 }
88 }
89
90 static inline VkFormat
vk_format_stencil_only(VkFormat format)91 vk_format_stencil_only(VkFormat format)
92 {
93 assert(vk_format_has_stencil(format));
94 return VK_FORMAT_S8_UINT;
95 }
96
97 void vk_component_mapping_to_pipe_swizzle(VkComponentMapping mapping,
98 unsigned char out_swizzle[4]);
99
100 static inline bool
vk_format_is_int(VkFormat format)101 vk_format_is_int(VkFormat format)
102 {
103 return util_format_is_pure_integer(vk_format_to_pipe_format(format));
104 }
105
106 static inline bool
vk_format_is_sint(VkFormat format)107 vk_format_is_sint(VkFormat format)
108 {
109 return util_format_is_pure_sint(vk_format_to_pipe_format(format));
110 }
111
112 static inline bool
vk_format_is_uint(VkFormat format)113 vk_format_is_uint(VkFormat format)
114 {
115 return util_format_is_pure_uint(vk_format_to_pipe_format(format));
116 }
117
118 static inline bool
vk_format_is_unorm(VkFormat format)119 vk_format_is_unorm(VkFormat format)
120 {
121 return util_format_is_unorm(vk_format_to_pipe_format(format));
122 }
123
124 static inline bool
vk_format_is_snorm(VkFormat format)125 vk_format_is_snorm(VkFormat format)
126 {
127 return util_format_is_snorm(vk_format_to_pipe_format(format));
128 }
129
130 static inline bool
vk_format_is_float(VkFormat format)131 vk_format_is_float(VkFormat format)
132 {
133 return util_format_is_float(vk_format_to_pipe_format(format));
134 }
135
136 static inline bool
vk_format_is_srgb(VkFormat format)137 vk_format_is_srgb(VkFormat format)
138 {
139 return util_format_is_srgb(vk_format_to_pipe_format(format));
140 }
141
142 static inline unsigned
vk_format_get_blocksize(VkFormat format)143 vk_format_get_blocksize(VkFormat format)
144 {
145 return util_format_get_blocksize(vk_format_to_pipe_format(format));
146 }
147
148 static inline unsigned
vk_format_get_blockwidth(VkFormat format)149 vk_format_get_blockwidth(VkFormat format)
150 {
151 return util_format_get_blockwidth(vk_format_to_pipe_format(format));
152 }
153
154 static inline unsigned
vk_format_get_blockheight(VkFormat format)155 vk_format_get_blockheight(VkFormat format)
156 {
157 return util_format_get_blockheight(vk_format_to_pipe_format(format));
158 }
159
160 static inline bool
vk_format_is_compressed(VkFormat format)161 vk_format_is_compressed(VkFormat format)
162 {
163 /* this includes 4:2:2 formats, which are compressed formats for vulkan */
164 return vk_format_get_blockwidth(format) > 1;
165 }
166
167 static inline bool
vk_format_is_block_compressed(VkFormat format)168 vk_format_is_block_compressed(VkFormat format)
169 {
170 return util_format_is_compressed(vk_format_to_pipe_format(format));
171 }
172
173 static inline const struct util_format_description *
vk_format_description(VkFormat format)174 vk_format_description(VkFormat format)
175 {
176 return util_format_description(vk_format_to_pipe_format(format));
177 }
178
179 static inline unsigned
vk_format_get_component_bits(VkFormat format,enum util_format_colorspace colorspace,unsigned component)180 vk_format_get_component_bits(VkFormat format, enum util_format_colorspace colorspace,
181 unsigned component)
182 {
183 return util_format_get_component_bits(vk_format_to_pipe_format(format),
184 colorspace,
185 component);
186 }
187
188 static inline unsigned
vk_format_get_nr_components(VkFormat format)189 vk_format_get_nr_components(VkFormat format)
190 {
191 return util_format_get_nr_components(vk_format_to_pipe_format(format));
192 }
193
194 static inline bool
vk_format_has_alpha(VkFormat format)195 vk_format_has_alpha(VkFormat format)
196 {
197 return util_format_has_alpha(vk_format_to_pipe_format(format));
198 }
199
200 static inline unsigned
vk_format_get_blocksizebits(VkFormat format)201 vk_format_get_blocksizebits(VkFormat format)
202 {
203 return util_format_get_blocksizebits(vk_format_to_pipe_format(format));
204 }
205
206 VkFormat
207 vk_format_get_plane_format(VkFormat format, unsigned plane_id);
208
209 VkFormat
210 vk_format_get_aspect_format(VkFormat format, const VkImageAspectFlags aspect);
211
212 struct vk_format_ycbcr_plane {
213 /* RGBA format for this plane */
214 VkFormat format;
215
216 /* Whether this plane contains chroma channels */
217 bool has_chroma;
218
219 /* For downscaling of YUV planes */
220 uint8_t denominator_scales[2];
221
222 /* How to map sampled ycbcr planes to a single 4 component element.
223 *
224 * We use uint8_t for compactness but it's actually VkComponentSwizzle.
225 */
226 uint8_t ycbcr_swizzle[4];
227 };
228
229 struct vk_format_ycbcr_info {
230 uint8_t n_planes;
231 struct vk_format_ycbcr_plane planes[3];
232 };
233
234 const struct vk_format_ycbcr_info *vk_format_get_ycbcr_info(VkFormat format);
235
236 static inline unsigned
vk_format_get_plane_count(VkFormat format)237 vk_format_get_plane_count(VkFormat format)
238 {
239 const struct vk_format_ycbcr_info *ycbcr_info =
240 vk_format_get_ycbcr_info(format);
241 return ycbcr_info ? ycbcr_info->n_planes : 1;
242 }
243
244 VkClearColorValue
245 vk_swizzle_color_value(VkClearColorValue color,
246 VkComponentMapping swizzle, bool is_int);
247
248 #ifdef __cplusplus
249 }
250 #endif
251
252 #endif
253