turnip: enable 422_UNORM formats
[mesa.git] / src / freedreno / vulkan / vk_format.h
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * Based on u_format.h which is:
6 * Copyright 2009-2010 Vmware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 #ifndef VK_FORMAT_H
28 #define VK_FORMAT_H
29
30 #include <assert.h>
31 #include <util/macros.h>
32 #include <util/format/u_format.h>
33 #include <vulkan/util/vk_format.h>
34
35 #include <vulkan/vulkan.h>
36
37 static inline const struct util_format_description *
38 vk_format_description(VkFormat format)
39 {
40 return util_format_description(vk_format_to_pipe_format(format));
41 }
42
43 /**
44 * Return total bits needed for the pixel format per block.
45 */
46 static inline unsigned
47 vk_format_get_blocksizebits(VkFormat format)
48 {
49 return util_format_get_blocksizebits(vk_format_to_pipe_format(format));
50 }
51
52 /**
53 * Return bytes per block (not pixel) for the given format.
54 */
55 static inline unsigned
56 vk_format_get_blocksize(VkFormat format)
57 {
58 return util_format_get_blocksize(vk_format_to_pipe_format(format));
59 }
60
61 static inline unsigned
62 vk_format_get_blockwidth(VkFormat format)
63 {
64 return util_format_get_blockwidth(vk_format_to_pipe_format(format));
65 }
66
67 static inline unsigned
68 vk_format_get_blockheight(VkFormat format)
69 {
70 return util_format_get_blockheight(vk_format_to_pipe_format(format));
71 }
72
73 static inline unsigned
74 vk_format_get_block_count_width(VkFormat format, unsigned width)
75 {
76 return util_format_get_nblocksx(vk_format_to_pipe_format(format), width);
77 }
78
79 static inline unsigned
80 vk_format_get_block_count_height(VkFormat format, unsigned height)
81 {
82 return util_format_get_nblocksy(vk_format_to_pipe_format(format), height);
83 }
84
85 static inline unsigned
86 vk_format_get_block_count(VkFormat format, unsigned width, unsigned height)
87 {
88 return util_format_get_nblocks(vk_format_to_pipe_format(format),
89 width, height);
90 }
91
92 static inline VkImageAspectFlags
93 vk_format_aspects(VkFormat format)
94 {
95 switch (format) {
96 case VK_FORMAT_UNDEFINED:
97 return 0;
98
99 case VK_FORMAT_S8_UINT:
100 return VK_IMAGE_ASPECT_STENCIL_BIT;
101
102 case VK_FORMAT_D16_UNORM_S8_UINT:
103 case VK_FORMAT_D24_UNORM_S8_UINT:
104 case VK_FORMAT_D32_SFLOAT_S8_UINT:
105 return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
106
107 case VK_FORMAT_D16_UNORM:
108 case VK_FORMAT_X8_D24_UNORM_PACK32:
109 case VK_FORMAT_D32_SFLOAT:
110 return VK_IMAGE_ASPECT_DEPTH_BIT;
111
112 default:
113 return VK_IMAGE_ASPECT_COLOR_BIT;
114 }
115 }
116
117 static inline enum pipe_swizzle
118 tu_swizzle_conv(VkComponentSwizzle component,
119 const unsigned char chan[4],
120 VkComponentSwizzle vk_swiz)
121 {
122 int x;
123
124 if (vk_swiz == VK_COMPONENT_SWIZZLE_IDENTITY)
125 vk_swiz = component;
126 switch (vk_swiz) {
127 case VK_COMPONENT_SWIZZLE_ZERO:
128 return PIPE_SWIZZLE_0;
129 case VK_COMPONENT_SWIZZLE_ONE:
130 return PIPE_SWIZZLE_1;
131 case VK_COMPONENT_SWIZZLE_R:
132 for (x = 0; x < 4; x++)
133 if (chan[x] == 0)
134 return x;
135 return PIPE_SWIZZLE_0;
136 case VK_COMPONENT_SWIZZLE_G:
137 for (x = 0; x < 4; x++)
138 if (chan[x] == 1)
139 return x;
140 return PIPE_SWIZZLE_0;
141 case VK_COMPONENT_SWIZZLE_B:
142 for (x = 0; x < 4; x++)
143 if (chan[x] == 2)
144 return x;
145 return PIPE_SWIZZLE_0;
146 case VK_COMPONENT_SWIZZLE_A:
147 for (x = 0; x < 4; x++)
148 if (chan[x] == 3)
149 return x;
150 return PIPE_SWIZZLE_1;
151 default:
152 unreachable("Illegal swizzle");
153 }
154 }
155
156 static inline void
157 vk_format_compose_swizzles(const VkComponentMapping *mapping,
158 const unsigned char swz[4],
159 enum pipe_swizzle dst[4])
160 {
161 dst[0] = tu_swizzle_conv(VK_COMPONENT_SWIZZLE_R, swz, mapping->r);
162 dst[1] = tu_swizzle_conv(VK_COMPONENT_SWIZZLE_G, swz, mapping->g);
163 dst[2] = tu_swizzle_conv(VK_COMPONENT_SWIZZLE_B, swz, mapping->b);
164 dst[3] = tu_swizzle_conv(VK_COMPONENT_SWIZZLE_A, swz, mapping->a);
165 }
166
167 static inline bool
168 vk_format_is_compressed(VkFormat format)
169 {
170 /* this includes 4:2:2 formats, which are compressed formats for vulkan */
171 return vk_format_get_blockwidth(format) > 1;
172 }
173
174 static inline bool
175 vk_format_has_depth(VkFormat format)
176 {
177 const struct util_format_description *desc = vk_format_description(format);
178
179 return util_format_has_depth(desc);
180 }
181
182 static inline bool
183 vk_format_has_stencil(VkFormat format)
184 {
185 const struct util_format_description *desc = vk_format_description(format);
186
187 return util_format_has_stencil(desc);
188 }
189
190 static inline bool
191 vk_format_is_depth_or_stencil(VkFormat format)
192 {
193 return vk_format_has_depth(format) || vk_format_has_stencil(format);
194 }
195
196 static inline bool
197 vk_format_is_color(VkFormat format)
198 {
199 return !vk_format_is_depth_or_stencil(format);
200 }
201
202 static inline bool
203 vk_format_has_alpha(VkFormat format)
204 {
205 return util_format_has_alpha(vk_format_to_pipe_format(format));
206 }
207
208 static inline VkFormat
209 vk_format_depth_only(VkFormat format)
210 {
211 switch (format) {
212 case VK_FORMAT_D16_UNORM_S8_UINT:
213 return VK_FORMAT_D16_UNORM;
214 case VK_FORMAT_D24_UNORM_S8_UINT:
215 return VK_FORMAT_X8_D24_UNORM_PACK32;
216 case VK_FORMAT_D32_SFLOAT_S8_UINT:
217 return VK_FORMAT_D32_SFLOAT;
218 default:
219 return format;
220 }
221 }
222
223 static inline bool
224 vk_format_is_int(VkFormat format)
225 {
226 return util_format_is_pure_integer(vk_format_to_pipe_format(format));
227 }
228
229 static inline bool
230 vk_format_is_uint(VkFormat format)
231 {
232 return util_format_is_pure_uint(vk_format_to_pipe_format(format));
233 }
234
235 static inline bool
236 vk_format_is_sint(VkFormat format)
237 {
238 return util_format_is_pure_sint(vk_format_to_pipe_format(format));
239 }
240
241 static inline bool
242 vk_format_is_srgb(VkFormat format)
243 {
244 return util_format_is_srgb(vk_format_to_pipe_format(format));
245 }
246
247 static inline bool
248 vk_format_is_unorm(VkFormat format)
249 {
250 return util_format_is_unorm(vk_format_to_pipe_format(format));
251 }
252
253 static inline bool
254 vk_format_is_snorm(VkFormat format)
255 {
256 return util_format_is_snorm(vk_format_to_pipe_format(format));
257 }
258
259 static inline bool
260 vk_format_is_float(VkFormat format)
261 {
262 return util_format_is_float(vk_format_to_pipe_format(format));
263 }
264
265 static inline VkFormat
266 vk_format_no_srgb(VkFormat format)
267 {
268 switch (format) {
269 case VK_FORMAT_R8_SRGB:
270 return VK_FORMAT_R8_UNORM;
271 case VK_FORMAT_R8G8_SRGB:
272 return VK_FORMAT_R8G8_UNORM;
273 case VK_FORMAT_R8G8B8_SRGB:
274 return VK_FORMAT_R8G8B8_UNORM;
275 case VK_FORMAT_B8G8R8_SRGB:
276 return VK_FORMAT_B8G8R8_UNORM;
277 case VK_FORMAT_R8G8B8A8_SRGB:
278 return VK_FORMAT_R8G8B8A8_UNORM;
279 case VK_FORMAT_B8G8R8A8_SRGB:
280 return VK_FORMAT_B8G8R8A8_UNORM;
281 case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
282 return VK_FORMAT_A8B8G8R8_UNORM_PACK32;
283 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
284 return VK_FORMAT_BC1_RGB_UNORM_BLOCK;
285 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
286 return VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
287 case VK_FORMAT_BC2_SRGB_BLOCK:
288 return VK_FORMAT_BC2_UNORM_BLOCK;
289 case VK_FORMAT_BC3_SRGB_BLOCK:
290 return VK_FORMAT_BC3_UNORM_BLOCK;
291 case VK_FORMAT_BC7_SRGB_BLOCK:
292 return VK_FORMAT_BC7_UNORM_BLOCK;
293 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
294 return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
295 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
296 return VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
297 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
298 return VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
299 default:
300 assert(!vk_format_is_srgb(format));
301 return format;
302 }
303 }
304
305 static inline VkFormat
306 vk_format_stencil_only(VkFormat format)
307 {
308 return VK_FORMAT_S8_UINT;
309 }
310
311 static inline unsigned
312 vk_format_get_component_bits(VkFormat format,
313 enum util_format_colorspace colorspace,
314 unsigned component)
315 {
316 switch (format) {
317 case VK_FORMAT_G8B8G8R8_422_UNORM:
318 case VK_FORMAT_B8G8R8G8_422_UNORM:
319 /* util_format_get_component_bits doesn't return what we want */
320 return 8;
321 default:
322 break;
323 }
324
325 return util_format_get_component_bits(vk_format_to_pipe_format(format),
326 colorspace, component);
327 }
328
329 static inline unsigned
330 vk_format_get_nr_components(VkFormat format)
331 {
332 return util_format_get_nr_components(vk_format_to_pipe_format(format));
333 }
334
335 #endif /* VK_FORMAT_H */