tu: ir3: Emit push constants directly
[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 return util_format_is_compressed(vk_format_to_pipe_format(format));
171 }
172
173 static inline bool
174 vk_format_has_depth(VkFormat format)
175 {
176 const struct util_format_description *desc = vk_format_description(format);
177
178 return util_format_has_depth(desc);
179 }
180
181 static inline bool
182 vk_format_has_stencil(VkFormat format)
183 {
184 const struct util_format_description *desc = vk_format_description(format);
185
186 return util_format_has_stencil(desc);
187 }
188
189 static inline bool
190 vk_format_is_depth_or_stencil(VkFormat format)
191 {
192 return vk_format_has_depth(format) || vk_format_has_stencil(format);
193 }
194
195 static inline bool
196 vk_format_is_color(VkFormat format)
197 {
198 return !vk_format_is_depth_or_stencil(format);
199 }
200
201 static inline bool
202 vk_format_has_alpha(VkFormat format)
203 {
204 return util_format_has_alpha(vk_format_to_pipe_format(format));
205 }
206
207 static inline VkFormat
208 vk_format_depth_only(VkFormat format)
209 {
210 switch (format) {
211 case VK_FORMAT_D16_UNORM_S8_UINT:
212 return VK_FORMAT_D16_UNORM;
213 case VK_FORMAT_D24_UNORM_S8_UINT:
214 return VK_FORMAT_X8_D24_UNORM_PACK32;
215 case VK_FORMAT_D32_SFLOAT_S8_UINT:
216 return VK_FORMAT_D32_SFLOAT;
217 default:
218 return format;
219 }
220 }
221
222 static inline bool
223 vk_format_is_int(VkFormat format)
224 {
225 return util_format_is_pure_integer(vk_format_to_pipe_format(format));
226 }
227
228 static inline bool
229 vk_format_is_uint(VkFormat format)
230 {
231 return util_format_is_pure_uint(vk_format_to_pipe_format(format));
232 }
233
234 static inline bool
235 vk_format_is_sint(VkFormat format)
236 {
237 return util_format_is_pure_sint(vk_format_to_pipe_format(format));
238 }
239
240 static inline bool
241 vk_format_is_srgb(VkFormat format)
242 {
243 return util_format_is_srgb(vk_format_to_pipe_format(format));
244 }
245
246 static inline bool
247 vk_format_is_snorm(VkFormat format)
248 {
249 return util_format_is_snorm(vk_format_to_pipe_format(format));
250 }
251
252 static inline bool
253 vk_format_is_float(VkFormat format)
254 {
255 return util_format_is_float(vk_format_to_pipe_format(format));
256 }
257
258 static inline VkFormat
259 vk_format_no_srgb(VkFormat format)
260 {
261 switch (format) {
262 case VK_FORMAT_R8_SRGB:
263 return VK_FORMAT_R8_UNORM;
264 case VK_FORMAT_R8G8_SRGB:
265 return VK_FORMAT_R8G8_UNORM;
266 case VK_FORMAT_R8G8B8_SRGB:
267 return VK_FORMAT_R8G8B8_UNORM;
268 case VK_FORMAT_B8G8R8_SRGB:
269 return VK_FORMAT_B8G8R8_UNORM;
270 case VK_FORMAT_R8G8B8A8_SRGB:
271 return VK_FORMAT_R8G8B8A8_UNORM;
272 case VK_FORMAT_B8G8R8A8_SRGB:
273 return VK_FORMAT_B8G8R8A8_UNORM;
274 case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
275 return VK_FORMAT_A8B8G8R8_UNORM_PACK32;
276 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
277 return VK_FORMAT_BC1_RGB_UNORM_BLOCK;
278 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
279 return VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
280 case VK_FORMAT_BC2_SRGB_BLOCK:
281 return VK_FORMAT_BC2_UNORM_BLOCK;
282 case VK_FORMAT_BC3_SRGB_BLOCK:
283 return VK_FORMAT_BC3_UNORM_BLOCK;
284 case VK_FORMAT_BC7_SRGB_BLOCK:
285 return VK_FORMAT_BC7_UNORM_BLOCK;
286 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
287 return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
288 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
289 return VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
290 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
291 return VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
292 default:
293 assert(!vk_format_is_srgb(format));
294 return format;
295 }
296 }
297
298 static inline VkFormat
299 vk_format_stencil_only(VkFormat format)
300 {
301 return VK_FORMAT_S8_UINT;
302 }
303
304 static inline unsigned
305 vk_format_get_component_bits(VkFormat format,
306 enum util_format_colorspace colorspace,
307 unsigned component)
308 {
309 return util_format_get_component_bits(vk_format_to_pipe_format(format),
310 colorspace, component);
311 }
312
313 static inline unsigned
314 vk_format_get_nr_components(VkFormat format)
315 {
316 return util_format_get_nr_components(vk_format_to_pipe_format(format));
317 }
318
319 #endif /* VK_FORMAT_H */