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 DEALINGS
24 * 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 <vulkan/util/vk_format.h>
33 #include <vulkan/vulkan.h>
34
35 static inline const struct util_format_description *
vk_format_description(VkFormat format)36 vk_format_description(VkFormat format)
37 {
38 return util_format_description(vk_format_to_pipe_format(format));
39 }
40
41 /**
42 * Return total bits needed for the pixel format per block.
43 */
44 static inline unsigned
vk_format_get_blocksizebits(VkFormat format)45 vk_format_get_blocksizebits(VkFormat format)
46 {
47 return util_format_get_blocksizebits(vk_format_to_pipe_format(format));
48 }
49
50 /**
51 * Return bytes per block (not pixel) for the given format.
52 */
53 static inline unsigned
vk_format_get_blocksize(VkFormat format)54 vk_format_get_blocksize(VkFormat format)
55 {
56 return util_format_get_blocksize(vk_format_to_pipe_format(format));
57 }
58
59 static inline unsigned
vk_format_get_blockwidth(VkFormat format)60 vk_format_get_blockwidth(VkFormat format)
61 {
62 return util_format_get_blockwidth(vk_format_to_pipe_format(format));
63 }
64
65 static inline unsigned
vk_format_get_blockheight(VkFormat format)66 vk_format_get_blockheight(VkFormat format)
67 {
68 return util_format_get_blockheight(vk_format_to_pipe_format(format));
69 }
70
71 /**
72 * Return the index of the first non-void channel
73 * -1 if no non-void channels
74 */
75 static inline int
vk_format_get_first_non_void_channel(VkFormat format)76 vk_format_get_first_non_void_channel(VkFormat format)
77 {
78 return util_format_get_first_non_void_channel(vk_format_to_pipe_format(format));
79 }
80
81 static inline enum pipe_swizzle
radv_swizzle_conv(VkComponentSwizzle component,const unsigned char chan[4],VkComponentSwizzle vk_swiz)82 radv_swizzle_conv(VkComponentSwizzle component, const unsigned char chan[4],
83 VkComponentSwizzle vk_swiz)
84 {
85 if (vk_swiz == VK_COMPONENT_SWIZZLE_IDENTITY)
86 vk_swiz = component;
87 switch (vk_swiz) {
88 case VK_COMPONENT_SWIZZLE_ZERO:
89 return PIPE_SWIZZLE_0;
90 case VK_COMPONENT_SWIZZLE_ONE:
91 return PIPE_SWIZZLE_1;
92 case VK_COMPONENT_SWIZZLE_R:
93 case VK_COMPONENT_SWIZZLE_G:
94 case VK_COMPONENT_SWIZZLE_B:
95 case VK_COMPONENT_SWIZZLE_A:
96 return (enum pipe_swizzle)chan[vk_swiz - VK_COMPONENT_SWIZZLE_R];
97 default:
98 unreachable("Illegal swizzle");
99 }
100 }
101
102 static inline void
vk_format_compose_swizzles(const VkComponentMapping * mapping,const unsigned char swz[4],enum pipe_swizzle dst[4])103 vk_format_compose_swizzles(const VkComponentMapping *mapping, const unsigned char swz[4],
104 enum pipe_swizzle dst[4])
105 {
106 dst[0] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_R, swz, mapping->r);
107 dst[1] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_G, swz, mapping->g);
108 dst[2] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_B, swz, mapping->b);
109 dst[3] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_A, swz, mapping->a);
110 }
111
112 static inline bool
vk_format_is_compressed(VkFormat format)113 vk_format_is_compressed(VkFormat format)
114 {
115 return util_format_is_compressed(vk_format_to_pipe_format(format));
116 }
117
118 static inline bool
vk_format_is_subsampled(VkFormat format)119 vk_format_is_subsampled(VkFormat format)
120 {
121 return util_format_is_subsampled_422(vk_format_to_pipe_format(format));
122 }
123
124 static inline bool
vk_format_is_int(VkFormat format)125 vk_format_is_int(VkFormat format)
126 {
127 return util_format_is_pure_integer(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_sint(VkFormat format)137 vk_format_is_sint(VkFormat format)
138 {
139 return util_format_is_pure_sint(vk_format_to_pipe_format(format));
140 }
141
142 static inline bool
vk_format_is_unorm(VkFormat format)143 vk_format_is_unorm(VkFormat format)
144 {
145 return util_format_is_unorm(vk_format_to_pipe_format(format));
146 }
147
148 static inline bool
vk_format_is_srgb(VkFormat format)149 vk_format_is_srgb(VkFormat format)
150 {
151 return util_format_is_srgb(vk_format_to_pipe_format(format));
152 }
153
154 static inline VkFormat
vk_format_no_srgb(VkFormat format)155 vk_format_no_srgb(VkFormat format)
156 {
157 switch (format) {
158 case VK_FORMAT_R8_SRGB:
159 return VK_FORMAT_R8_UNORM;
160 case VK_FORMAT_R8G8_SRGB:
161 return VK_FORMAT_R8G8_UNORM;
162 case VK_FORMAT_R8G8B8_SRGB:
163 return VK_FORMAT_R8G8B8_UNORM;
164 case VK_FORMAT_B8G8R8_SRGB:
165 return VK_FORMAT_B8G8R8_UNORM;
166 case VK_FORMAT_R8G8B8A8_SRGB:
167 return VK_FORMAT_R8G8B8A8_UNORM;
168 case VK_FORMAT_B8G8R8A8_SRGB:
169 return VK_FORMAT_B8G8R8A8_UNORM;
170 case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
171 return VK_FORMAT_A8B8G8R8_UNORM_PACK32;
172 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
173 return VK_FORMAT_BC1_RGB_UNORM_BLOCK;
174 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
175 return VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
176 case VK_FORMAT_BC2_SRGB_BLOCK:
177 return VK_FORMAT_BC2_UNORM_BLOCK;
178 case VK_FORMAT_BC3_SRGB_BLOCK:
179 return VK_FORMAT_BC3_UNORM_BLOCK;
180 case VK_FORMAT_BC7_SRGB_BLOCK:
181 return VK_FORMAT_BC7_UNORM_BLOCK;
182 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
183 return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
184 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
185 return VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
186 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
187 return VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
188 default:
189 assert(!vk_format_is_srgb(format));
190 return format;
191 }
192 }
193
194 static inline unsigned
vk_format_get_component_bits(VkFormat format,enum util_format_colorspace colorspace,unsigned component)195 vk_format_get_component_bits(VkFormat format, enum util_format_colorspace colorspace,
196 unsigned component)
197 {
198 const struct util_format_description *desc = vk_format_description(format);
199 enum util_format_colorspace desc_colorspace;
200
201 assert(format);
202 if (!format) {
203 return 0;
204 }
205
206 assert(component < 4);
207
208 /* Treat RGB and SRGB as equivalent. */
209 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
210 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
211 }
212 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
213 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
214 } else {
215 desc_colorspace = desc->colorspace;
216 }
217
218 if (desc_colorspace != colorspace) {
219 return 0;
220 }
221
222 switch (desc->swizzle[component]) {
223 case PIPE_SWIZZLE_X:
224 return desc->channel[0].size;
225 case PIPE_SWIZZLE_Y:
226 return desc->channel[1].size;
227 case PIPE_SWIZZLE_Z:
228 return desc->channel[2].size;
229 case PIPE_SWIZZLE_W:
230 return desc->channel[3].size;
231 default:
232 return 0;
233 }
234 }
235
236 static inline VkFormat
vk_to_non_srgb_format(VkFormat format)237 vk_to_non_srgb_format(VkFormat format)
238 {
239 switch (format) {
240 case VK_FORMAT_R8_SRGB:
241 return VK_FORMAT_R8_UNORM;
242 case VK_FORMAT_R8G8_SRGB:
243 return VK_FORMAT_R8G8_UNORM;
244 case VK_FORMAT_R8G8B8_SRGB:
245 return VK_FORMAT_R8G8B8_UNORM;
246 case VK_FORMAT_B8G8R8_SRGB:
247 return VK_FORMAT_B8G8R8_UNORM;
248 case VK_FORMAT_R8G8B8A8_SRGB:
249 return VK_FORMAT_R8G8B8A8_UNORM;
250 case VK_FORMAT_B8G8R8A8_SRGB:
251 return VK_FORMAT_B8G8R8A8_UNORM;
252 case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
253 return VK_FORMAT_A8B8G8R8_UNORM_PACK32;
254 default:
255 return format;
256 }
257 }
258
259 static inline unsigned
vk_format_get_nr_components(VkFormat format)260 vk_format_get_nr_components(VkFormat format)
261 {
262 return util_format_get_nr_components(vk_format_to_pipe_format(format));
263 }
264
265 static inline unsigned
vk_format_get_plane_count(VkFormat format)266 vk_format_get_plane_count(VkFormat format)
267 {
268 return util_format_get_num_planes(vk_format_to_pipe_format(format));
269 }
270
271 static inline unsigned
vk_format_get_plane_width(VkFormat format,unsigned plane,unsigned width)272 vk_format_get_plane_width(VkFormat format, unsigned plane, unsigned width)
273 {
274 return util_format_get_plane_width(vk_format_to_pipe_format(format), plane, width);
275 }
276
277 static inline unsigned
vk_format_get_plane_height(VkFormat format,unsigned plane,unsigned height)278 vk_format_get_plane_height(VkFormat format, unsigned plane, unsigned height)
279 {
280 return util_format_get_plane_height(vk_format_to_pipe_format(format), plane, height);
281 }
282
283 static inline VkFormat
vk_format_get_plane_format(VkFormat format,unsigned plane_id)284 vk_format_get_plane_format(VkFormat format, unsigned plane_id)
285 {
286 assert(plane_id < vk_format_get_plane_count(format));
287
288 switch (format) {
289 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
290 case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
291 case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
292 return VK_FORMAT_R8_UNORM;
293 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
294 case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
295 return plane_id ? VK_FORMAT_R8G8_UNORM : VK_FORMAT_R8_UNORM;
296 case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
297 case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
298 case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
299 return VK_FORMAT_R16_UNORM;
300 case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
301 case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
302 return plane_id ? VK_FORMAT_R16G16_UNORM : VK_FORMAT_R16_UNORM;
303 default:
304 assert(vk_format_get_plane_count(format) == 1);
305 return format;
306 }
307 }
308
309 #endif /* VK_FORMAT_H */
310