1 /*
2 * Copyright © 2019 Raspberry Pi
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "v3dv_private.h"
25 #include "vk_util.h"
26 #include "vk_format_info.h"
27
28 #include "broadcom/cle/v3dx_pack.h"
29 #include "drm-uapi/drm_fourcc.h"
30 #include "util/format/u_format.h"
31 #include "vulkan/wsi/wsi_common.h"
32
33 #define SWIZ(x,y,z,w) { \
34 PIPE_SWIZZLE_##x, \
35 PIPE_SWIZZLE_##y, \
36 PIPE_SWIZZLE_##z, \
37 PIPE_SWIZZLE_##w \
38 }
39
40 #define FORMAT(vk, rt, tex, swiz, return_size, supports_filtering) \
41 [VK_FORMAT_##vk] = { \
42 true, \
43 V3D_OUTPUT_IMAGE_FORMAT_##rt, \
44 TEXTURE_DATA_FORMAT_##tex, \
45 swiz, \
46 return_size, \
47 supports_filtering, \
48 }
49
50 #define SWIZ_X001 SWIZ(X, 0, 0, 1)
51 #define SWIZ_XY01 SWIZ(X, Y, 0, 1)
52 #define SWIZ_XYZ1 SWIZ(X, Y, Z, 1)
53 #define SWIZ_XYZW SWIZ(X, Y, Z, W)
54 #define SWIZ_YZWX SWIZ(Y, Z, W, X)
55 #define SWIZ_YZW1 SWIZ(Y, Z, W, 1)
56 #define SWIZ_ZYXW SWIZ(Z, Y, X, W)
57 #define SWIZ_ZYX1 SWIZ(Z, Y, X, 1)
58 #define SWIZ_XXXY SWIZ(X, X, X, Y)
59 #define SWIZ_XXX1 SWIZ(X, X, X, 1)
60 #define SWIZ_XXXX SWIZ(X, X, X, X)
61 #define SWIZ_000X SWIZ(0, 0, 0, X)
62 #define SWIZ_WXYZ SWIZ(W, X, Y, Z)
63
64 /* FIXME: expand format table to describe whether the format is supported
65 * for buffer surfaces (texel buffers, vertex buffers, etc).
66 */
67 static const struct v3dv_format format_table[] = {
68 /* Color, 4 channels */
69 FORMAT(B8G8R8A8_SRGB, SRGB8_ALPHA8, RGBA8, SWIZ_ZYXW, 16, true),
70 FORMAT(B8G8R8A8_UNORM, RGBA8, RGBA8, SWIZ_ZYXW, 16, true),
71
72 FORMAT(R8G8B8A8_SRGB, SRGB8_ALPHA8, RGBA8, SWIZ_XYZW, 16, true),
73 FORMAT(R8G8B8A8_UNORM, RGBA8, RGBA8, SWIZ_XYZW, 16, true),
74 FORMAT(R8G8B8A8_SNORM, NO, RGBA8_SNORM, SWIZ_XYZW, 16, true),
75 FORMAT(R8G8B8A8_SINT, RGBA8I, RGBA8I, SWIZ_XYZW, 16, false),
76 FORMAT(R8G8B8A8_UINT, RGBA8UI, RGBA8UI, SWIZ_XYZW, 16, false),
77
78 FORMAT(R16G16B16A16_SFLOAT, RGBA16F, RGBA16F, SWIZ_XYZW, 16, true),
79 FORMAT(R16G16B16A16_UNORM, NO, RGBA16, SWIZ_XYZW, 32, true),
80 FORMAT(R16G16B16A16_SNORM, NO, RGBA16_SNORM, SWIZ_XYZW, 32, true),
81 FORMAT(R16G16B16A16_SINT, RGBA16I, RGBA16I, SWIZ_XYZW, 16, false),
82 FORMAT(R16G16B16A16_UINT, RGBA16UI, RGBA16UI, SWIZ_XYZW, 16, false),
83
84 FORMAT(R32G32B32A32_SFLOAT, RGBA32F, RGBA32F, SWIZ_XYZW, 32, false),
85 FORMAT(R32G32B32A32_SINT, RGBA32I, RGBA32I, SWIZ_XYZW, 32, false),
86 FORMAT(R32G32B32A32_UINT, RGBA32UI, RGBA32UI, SWIZ_XYZW, 32, false),
87
88 /* Color, 3 channels */
89 FORMAT(R32G32B32_SFLOAT, NO, NO, SWIZ_XYZ1, 0, false),
90 FORMAT(R32G32B32_UINT, NO, NO, SWIZ_XYZ1, 0, false),
91 FORMAT(R32G32B32_SINT, NO, NO, SWIZ_XYZ1, 0, false),
92
93 /* Color, 2 channels */
94 FORMAT(R8G8_UNORM, RG8, RG8, SWIZ_XY01, 16, true),
95 FORMAT(R8G8_SNORM, NO, RG8_SNORM, SWIZ_XY01, 16, true),
96 FORMAT(R8G8_SINT, RG8I, RG8I, SWIZ_XY01, 16, false),
97 FORMAT(R8G8_UINT, RG8UI, RG8UI, SWIZ_XY01, 16, false),
98
99 FORMAT(R16G16_UNORM, NO, RG16, SWIZ_XY01, 32, true),
100 FORMAT(R16G16_SNORM, NO, RG16_SNORM, SWIZ_XY01, 32, true),
101 FORMAT(R16G16_SFLOAT, RG16F, RG16F, SWIZ_XY01, 16, true),
102 FORMAT(R16G16_SINT, RG16I, RG16I, SWIZ_XY01, 16, false),
103 FORMAT(R16G16_UINT, RG16UI, RG16UI, SWIZ_XY01, 16, false),
104
105 FORMAT(R32G32_SFLOAT, RG32F, RG32F, SWIZ_XY01, 32, false),
106 FORMAT(R32G32_SINT, RG32I, RG32I, SWIZ_XY01, 32, false),
107 FORMAT(R32G32_UINT, RG32UI, RG32UI, SWIZ_XY01, 32, false),
108
109 /* Color, 1 channel */
110 FORMAT(R8_UNORM, R8, R8, SWIZ_X001, 16, true),
111 FORMAT(R8_SNORM, NO, R8_SNORM, SWIZ_X001, 16, true),
112 FORMAT(R8_SINT, R8I, R8I, SWIZ_X001, 16, false),
113 FORMAT(R8_UINT, R8UI, R8UI, SWIZ_X001, 16, false),
114
115 FORMAT(R16_UNORM, NO, R16, SWIZ_X001, 32, true),
116 FORMAT(R16_SNORM, NO, R16_SNORM, SWIZ_X001, 32, true),
117 FORMAT(R16_SFLOAT, R16F, R16F, SWIZ_X001, 16, true),
118 FORMAT(R16_SINT, R16I, R16I, SWIZ_X001, 16, false),
119 FORMAT(R16_UINT, R16UI, R16UI, SWIZ_X001, 16, false),
120
121 FORMAT(R32_SFLOAT, R32F, R32F, SWIZ_X001, 32, false),
122 FORMAT(R32_SINT, R32I, R32I, SWIZ_X001, 32, false),
123 FORMAT(R32_UINT, R32UI, R32UI, SWIZ_X001, 32, false),
124
125 /* Color, packed */
126 FORMAT(B4G4R4A4_UNORM_PACK16, ABGR4444, RGBA4, SWIZ_ZYXW, 16, true), /* Swap RB */
127 FORMAT(R5G6B5_UNORM_PACK16, BGR565, RGB565, SWIZ_XYZ1, 16, true),
128 FORMAT(R5G5B5A1_UNORM_PACK16, ABGR1555, RGB5_A1, SWIZ_XYZW, 16, true),
129 FORMAT(A1R5G5B5_UNORM_PACK16, RGBA5551, A1_RGB5, SWIZ_ZYXW, 16, true), /* Swap RB */
130 FORMAT(A8B8G8R8_UNORM_PACK32, RGBA8, RGBA8, SWIZ_XYZW, 16, true), /* RGBA8 UNORM */
131 FORMAT(A8B8G8R8_SNORM_PACK32, NO, RGBA8_SNORM, SWIZ_XYZW, 16, true), /* RGBA8 SNORM */
132 FORMAT(A8B8G8R8_UINT_PACK32, RGBA8UI, RGBA8UI, SWIZ_XYZW, 16, true), /* RGBA8 UINT */
133 FORMAT(A8B8G8R8_SINT_PACK32, RGBA8I, RGBA8I, SWIZ_XYZW, 16, true), /* RGBA8 SINT */
134 FORMAT(A8B8G8R8_SRGB_PACK32, SRGB8_ALPHA8, RGBA8, SWIZ_XYZW, 16, true), /* RGBA8 sRGB */
135 FORMAT(A2B10G10R10_UNORM_PACK32,RGB10_A2, RGB10_A2, SWIZ_XYZW, 16, true),
136 FORMAT(A2B10G10R10_UINT_PACK32, RGB10_A2UI, RGB10_A2UI, SWIZ_XYZW, 16, true),
137 FORMAT(E5B9G9R9_UFLOAT_PACK32, NO, RGB9_E5, SWIZ_XYZ1, 16, true),
138 FORMAT(B10G11R11_UFLOAT_PACK32, R11F_G11F_B10F,R11F_G11F_B10F, SWIZ_XYZ1, 16, true),
139
140 /* Depth */
141 FORMAT(D16_UNORM, D16, DEPTH_COMP16, SWIZ_X001, 32, false),
142 FORMAT(D32_SFLOAT, D32F, DEPTH_COMP32F, SWIZ_X001, 32, false),
143 FORMAT(X8_D24_UNORM_PACK32, D24S8, DEPTH24_X8, SWIZ_X001, 32, false),
144
145 /* Depth + Stencil */
146 FORMAT(D24_UNORM_S8_UINT, D24S8, DEPTH24_X8, SWIZ_X001, 32, false),
147
148 /* Compressed: ETC2 / EAC */
149 FORMAT(ETC2_R8G8B8_UNORM_BLOCK, NO, RGB8_ETC2, SWIZ_XYZ1, 16, true),
150 FORMAT(ETC2_R8G8B8_SRGB_BLOCK, NO, RGB8_ETC2, SWIZ_XYZ1, 16, true),
151 FORMAT(ETC2_R8G8B8A1_UNORM_BLOCK, NO, RGB8_PUNCHTHROUGH_ALPHA1, SWIZ_XYZW, 16, true),
152 FORMAT(ETC2_R8G8B8A1_SRGB_BLOCK, NO, RGB8_PUNCHTHROUGH_ALPHA1, SWIZ_XYZW, 16, true),
153 FORMAT(ETC2_R8G8B8A8_UNORM_BLOCK, NO, RGBA8_ETC2_EAC, SWIZ_XYZW, 16, true),
154 FORMAT(ETC2_R8G8B8A8_SRGB_BLOCK, NO, RGBA8_ETC2_EAC, SWIZ_XYZW, 16, true),
155 FORMAT(EAC_R11_UNORM_BLOCK, NO, R11_EAC, SWIZ_X001, 16, true),
156 FORMAT(EAC_R11_SNORM_BLOCK, NO, SIGNED_R11_EAC, SWIZ_X001, 16, true),
157 FORMAT(EAC_R11G11_UNORM_BLOCK, NO, RG11_EAC, SWIZ_XY01, 16, true),
158 FORMAT(EAC_R11G11_SNORM_BLOCK, NO, SIGNED_RG11_EAC, SWIZ_XY01, 16, true),
159 };
160
161 const struct v3dv_format *
v3dv_get_format(VkFormat format)162 v3dv_get_format(VkFormat format)
163 {
164 if (format < ARRAY_SIZE(format_table) && format_table[format].supported)
165 return &format_table[format];
166 else
167 return NULL;
168 }
169
170 void
v3dv_get_internal_type_bpp_for_output_format(uint32_t format,uint32_t * type,uint32_t * bpp)171 v3dv_get_internal_type_bpp_for_output_format(uint32_t format,
172 uint32_t *type,
173 uint32_t *bpp)
174 {
175 switch (format) {
176 case V3D_OUTPUT_IMAGE_FORMAT_RGBA8:
177 case V3D_OUTPUT_IMAGE_FORMAT_RGB8:
178 case V3D_OUTPUT_IMAGE_FORMAT_RG8:
179 case V3D_OUTPUT_IMAGE_FORMAT_R8:
180 case V3D_OUTPUT_IMAGE_FORMAT_ABGR4444:
181 case V3D_OUTPUT_IMAGE_FORMAT_BGR565:
182 case V3D_OUTPUT_IMAGE_FORMAT_ABGR1555:
183 *type = V3D_INTERNAL_TYPE_8;
184 *bpp = V3D_INTERNAL_BPP_32;
185 break;
186
187 case V3D_OUTPUT_IMAGE_FORMAT_RGBA8I:
188 case V3D_OUTPUT_IMAGE_FORMAT_RG8I:
189 case V3D_OUTPUT_IMAGE_FORMAT_R8I:
190 *type = V3D_INTERNAL_TYPE_8I;
191 *bpp = V3D_INTERNAL_BPP_32;
192 break;
193
194 case V3D_OUTPUT_IMAGE_FORMAT_RGBA8UI:
195 case V3D_OUTPUT_IMAGE_FORMAT_RG8UI:
196 case V3D_OUTPUT_IMAGE_FORMAT_R8UI:
197 *type = V3D_INTERNAL_TYPE_8UI;
198 *bpp = V3D_INTERNAL_BPP_32;
199 break;
200
201 case V3D_OUTPUT_IMAGE_FORMAT_SRGB8_ALPHA8:
202 case V3D_OUTPUT_IMAGE_FORMAT_SRGB:
203 case V3D_OUTPUT_IMAGE_FORMAT_RGB10_A2:
204 case V3D_OUTPUT_IMAGE_FORMAT_R11F_G11F_B10F:
205 case V3D_OUTPUT_IMAGE_FORMAT_RGBA16F:
206 /* Note that sRGB RTs are stored in the tile buffer at 16F,
207 * and the conversion to sRGB happens at tilebuffer load/store.
208 */
209 *type = V3D_INTERNAL_TYPE_16F;
210 *bpp = V3D_INTERNAL_BPP_64;
211 break;
212
213 case V3D_OUTPUT_IMAGE_FORMAT_RG16F:
214 case V3D_OUTPUT_IMAGE_FORMAT_R16F:
215 *type = V3D_INTERNAL_TYPE_16F;
216 /* Use 64bpp to make sure the TLB doesn't throw away the alpha
217 * channel before alpha test happens.
218 */
219 *bpp = V3D_INTERNAL_BPP_64;
220 break;
221
222 case V3D_OUTPUT_IMAGE_FORMAT_RGBA16I:
223 *type = V3D_INTERNAL_TYPE_16I;
224 *bpp = V3D_INTERNAL_BPP_64;
225 break;
226
227 case V3D_OUTPUT_IMAGE_FORMAT_RG16I:
228 case V3D_OUTPUT_IMAGE_FORMAT_R16I:
229 *type = V3D_INTERNAL_TYPE_16I;
230 *bpp = V3D_INTERNAL_BPP_32;
231 break;
232
233 case V3D_OUTPUT_IMAGE_FORMAT_RGB10_A2UI:
234 case V3D_OUTPUT_IMAGE_FORMAT_RGBA16UI:
235 *type = V3D_INTERNAL_TYPE_16UI;
236 *bpp = V3D_INTERNAL_BPP_64;
237 break;
238
239 case V3D_OUTPUT_IMAGE_FORMAT_RG16UI:
240 case V3D_OUTPUT_IMAGE_FORMAT_R16UI:
241 *type = V3D_INTERNAL_TYPE_16UI;
242 *bpp = V3D_INTERNAL_BPP_32;
243 break;
244
245 case V3D_OUTPUT_IMAGE_FORMAT_RGBA32I:
246 *type = V3D_INTERNAL_TYPE_32I;
247 *bpp = V3D_INTERNAL_BPP_128;
248 break;
249
250 case V3D_OUTPUT_IMAGE_FORMAT_RG32I:
251 *type = V3D_INTERNAL_TYPE_32I;
252 *bpp = V3D_INTERNAL_BPP_64;
253 break;
254
255 case V3D_OUTPUT_IMAGE_FORMAT_R32I:
256 *type = V3D_INTERNAL_TYPE_32I;
257 *bpp = V3D_INTERNAL_BPP_32;
258 break;
259
260 case V3D_OUTPUT_IMAGE_FORMAT_RGBA32UI:
261 *type = V3D_INTERNAL_TYPE_32UI;
262 *bpp = V3D_INTERNAL_BPP_128;
263 break;
264
265 case V3D_OUTPUT_IMAGE_FORMAT_RG32UI:
266 *type = V3D_INTERNAL_TYPE_32UI;
267 *bpp = V3D_INTERNAL_BPP_64;
268 break;
269
270 case V3D_OUTPUT_IMAGE_FORMAT_R32UI:
271 *type = V3D_INTERNAL_TYPE_32UI;
272 *bpp = V3D_INTERNAL_BPP_32;
273 break;
274
275 case V3D_OUTPUT_IMAGE_FORMAT_RGBA32F:
276 *type = V3D_INTERNAL_TYPE_32F;
277 *bpp = V3D_INTERNAL_BPP_128;
278 break;
279
280 case V3D_OUTPUT_IMAGE_FORMAT_RG32F:
281 *type = V3D_INTERNAL_TYPE_32F;
282 *bpp = V3D_INTERNAL_BPP_64;
283 break;
284
285 case V3D_OUTPUT_IMAGE_FORMAT_R32F:
286 *type = V3D_INTERNAL_TYPE_32F;
287 *bpp = V3D_INTERNAL_BPP_32;
288 break;
289
290 default:
291 /* Provide some default values, as we'll be called at RB
292 * creation time, even if an RB with this format isn't supported.
293 */
294 *type = V3D_INTERNAL_TYPE_8;
295 *bpp = V3D_INTERNAL_BPP_32;
296 break;
297 }
298 }
299
300 bool
v3dv_format_supports_tlb_resolve(const struct v3dv_format * format)301 v3dv_format_supports_tlb_resolve(const struct v3dv_format *format)
302 {
303 uint32_t type, bpp;
304 v3dv_get_internal_type_bpp_for_output_format(format->rt_type, &type, &bpp);
305 return type == V3D_INTERNAL_TYPE_8 || type == V3D_INTERNAL_TYPE_16F;
306 }
307
308 const uint8_t *
v3dv_get_format_swizzle(VkFormat f)309 v3dv_get_format_swizzle(VkFormat f)
310 {
311 const struct v3dv_format *vf = v3dv_get_format(f);
312 static const uint8_t fallback[] = {0, 1, 2, 3};
313
314 if (!vf)
315 return fallback;
316
317 return vf->swizzle;
318 }
319
320 uint8_t
v3dv_get_tex_return_size(const struct v3dv_format * vf,bool compare_enable)321 v3dv_get_tex_return_size(const struct v3dv_format *vf,
322 bool compare_enable)
323 {
324 if (compare_enable)
325 return 16;
326
327 return vf->return_size;
328 }
329
330 bool
v3dv_tfu_supports_tex_format(const struct v3d_device_info * devinfo,uint32_t tex_format)331 v3dv_tfu_supports_tex_format(const struct v3d_device_info *devinfo,
332 uint32_t tex_format)
333 {
334 assert(devinfo->ver >= 41);
335
336 switch (tex_format) {
337 case TEXTURE_DATA_FORMAT_R8:
338 case TEXTURE_DATA_FORMAT_R8_SNORM:
339 case TEXTURE_DATA_FORMAT_RG8:
340 case TEXTURE_DATA_FORMAT_RG8_SNORM:
341 case TEXTURE_DATA_FORMAT_RGBA8:
342 case TEXTURE_DATA_FORMAT_RGBA8_SNORM:
343 case TEXTURE_DATA_FORMAT_RGB565:
344 case TEXTURE_DATA_FORMAT_RGBA4:
345 case TEXTURE_DATA_FORMAT_RGB5_A1:
346 case TEXTURE_DATA_FORMAT_RGB10_A2:
347 case TEXTURE_DATA_FORMAT_R16:
348 case TEXTURE_DATA_FORMAT_R16_SNORM:
349 case TEXTURE_DATA_FORMAT_RG16:
350 case TEXTURE_DATA_FORMAT_RG16_SNORM:
351 case TEXTURE_DATA_FORMAT_RGBA16:
352 case TEXTURE_DATA_FORMAT_RGBA16_SNORM:
353 case TEXTURE_DATA_FORMAT_R16F:
354 case TEXTURE_DATA_FORMAT_RG16F:
355 case TEXTURE_DATA_FORMAT_RGBA16F:
356 case TEXTURE_DATA_FORMAT_R11F_G11F_B10F:
357 case TEXTURE_DATA_FORMAT_R4:
358 return true;
359 default:
360 return false;
361 }
362 }
363
364
365 static bool
format_supports_blending(const struct v3dv_format * format)366 format_supports_blending(const struct v3dv_format *format)
367 {
368 /* Hardware blending is only supported on render targets that are configured
369 * 4x8-bit unorm, 2x16-bit float or 4x16-bit float.
370 */
371 uint32_t type, bpp;
372 v3dv_get_internal_type_bpp_for_output_format(format->rt_type, &type, &bpp);
373 switch (type) {
374 case V3D_INTERNAL_TYPE_8:
375 return bpp == V3D_INTERNAL_BPP_32;
376 case V3D_INTERNAL_TYPE_16F:
377 return bpp == V3D_INTERNAL_BPP_32 || V3D_INTERNAL_BPP_64;
378 default:
379 return false;
380 }
381 }
382
383 static VkFormatFeatureFlags
image_format_features(VkFormat vk_format,const struct v3dv_format * v3dv_format,VkImageTiling tiling)384 image_format_features(VkFormat vk_format,
385 const struct v3dv_format *v3dv_format,
386 VkImageTiling tiling)
387 {
388 if (!v3dv_format || !v3dv_format->supported)
389 return 0;
390
391 const VkImageAspectFlags aspects = vk_format_aspects(vk_format);
392
393 const VkImageAspectFlags zs_aspects = VK_IMAGE_ASPECT_DEPTH_BIT |
394 VK_IMAGE_ASPECT_STENCIL_BIT;
395 const VkImageAspectFlags supported_aspects = VK_IMAGE_ASPECT_COLOR_BIT |
396 zs_aspects;
397 if ((aspects & supported_aspects) != aspects)
398 return 0;
399
400 /* FIXME: We don't support separate stencil yet */
401 if ((aspects & zs_aspects) == VK_IMAGE_ASPECT_STENCIL_BIT)
402 return 0;
403
404 if (v3dv_format->tex_type == TEXTURE_DATA_FORMAT_NO &&
405 v3dv_format->rt_type == V3D_OUTPUT_IMAGE_FORMAT_NO) {
406 return 0;
407 }
408
409 VkFormatFeatureFlags flags = 0;
410
411 /* Raster format is only supported for 1D textures, so let's just
412 * always require optimal tiling for anything that requires sampling.
413 * Note: even if the user requests optimal for a 1D image, we will still
414 * use raster format since that is what the HW requires.
415 */
416 if (v3dv_format->tex_type != TEXTURE_DATA_FORMAT_NO &&
417 tiling == VK_IMAGE_TILING_OPTIMAL) {
418 flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
419 VK_FORMAT_FEATURE_BLIT_SRC_BIT;
420
421 if (v3dv_format->supports_filtering)
422 flags |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
423 }
424
425 if (v3dv_format->rt_type != V3D_OUTPUT_IMAGE_FORMAT_NO) {
426 if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
427 flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
428 VK_FORMAT_FEATURE_BLIT_DST_BIT;
429 if (format_supports_blending(v3dv_format))
430 flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
431 } else if (aspects & zs_aspects) {
432 flags |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT |
433 VK_FORMAT_FEATURE_BLIT_DST_BIT;
434 }
435 }
436
437 const struct util_format_description *desc =
438 vk_format_description(vk_format);
439 assert(desc);
440
441 if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN && desc->is_array) {
442 flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
443 if (desc->nr_channels == 1 && vk_format_is_int(vk_format))
444 flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
445 } else if (vk_format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 ||
446 vk_format == VK_FORMAT_A2B10G10R10_UINT_PACK32 ||
447 vk_format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
448 /* To comply with shaderStorageImageExtendedFormats */
449 flags |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
450 }
451
452 if (flags) {
453 flags |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
454 VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
455 }
456
457 return flags;
458 }
459
460 static VkFormatFeatureFlags
buffer_format_features(VkFormat vk_format,const struct v3dv_format * v3dv_format)461 buffer_format_features(VkFormat vk_format, const struct v3dv_format *v3dv_format)
462 {
463 if (!v3dv_format || !v3dv_format->supported)
464 return 0;
465
466 if (!v3dv_format->supported)
467 return 0;
468
469 /* We probably only want to support buffer formats that have a
470 * color format specification.
471 */
472 if (!vk_format_is_color(vk_format))
473 return 0;
474
475 const struct util_format_description *desc =
476 vk_format_description(vk_format);
477 assert(desc);
478
479 VkFormatFeatureFlags flags = 0;
480 if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN &&
481 desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
482 desc->is_array) {
483 flags |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
484 if (v3dv_format->tex_type != TEXTURE_DATA_FORMAT_NO) {
485 flags |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
486 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
487 }
488 } else if (vk_format == VK_FORMAT_A2B10G10R10_UNORM_PACK32) {
489 flags |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT |
490 VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
491 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
492 } else if (vk_format == VK_FORMAT_A2B10G10R10_UINT_PACK32 ||
493 vk_format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
494 flags |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
495 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
496 }
497
498 if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN &&
499 desc->is_array &&
500 desc->nr_channels == 1 &&
501 vk_format_is_int(vk_format)) {
502 flags |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
503 }
504
505 return flags;
506 }
507
508 void
v3dv_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties * pFormatProperties)509 v3dv_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
510 VkFormat format,
511 VkFormatProperties* pFormatProperties)
512 {
513 const struct v3dv_format *v3dv_format = v3dv_get_format(format);
514
515 *pFormatProperties = (VkFormatProperties) {
516 .linearTilingFeatures =
517 image_format_features(format, v3dv_format, VK_IMAGE_TILING_LINEAR),
518 .optimalTilingFeatures =
519 image_format_features(format, v3dv_format, VK_IMAGE_TILING_OPTIMAL),
520 .bufferFeatures =
521 buffer_format_features(format, v3dv_format),
522 };
523 }
524
525 void
v3dv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties2 * pFormatProperties)526 v3dv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,
527 VkFormat format,
528 VkFormatProperties2 *pFormatProperties)
529 {
530 v3dv_GetPhysicalDeviceFormatProperties(physicalDevice, format,
531 &pFormatProperties->formatProperties);
532
533 vk_foreach_struct(ext, pFormatProperties->pNext) {
534 switch ((unsigned)ext->sType) {
535 case VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT: {
536 struct VkDrmFormatModifierPropertiesListEXT *list = (void *)ext;
537 VK_OUTARRAY_MAKE(out, list->pDrmFormatModifierProperties,
538 &list->drmFormatModifierCount);
539 if (pFormatProperties->formatProperties.linearTilingFeatures) {
540 vk_outarray_append(&out, mod_props) {
541 mod_props->drmFormatModifier = DRM_FORMAT_MOD_LINEAR;
542 mod_props->drmFormatModifierPlaneCount = 1;
543 }
544 }
545 if (pFormatProperties->formatProperties.optimalTilingFeatures) {
546 vk_outarray_append(&out, mod_props) {
547 mod_props->drmFormatModifier = DRM_FORMAT_MOD_BROADCOM_UIF;
548 mod_props->drmFormatModifierPlaneCount = 1;
549 }
550 }
551 break;
552 }
553 default:
554 v3dv_debug_ignored_stype(ext->sType);
555 break;
556 }
557 }
558 }
559
560 static VkResult
get_image_format_properties(struct v3dv_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * info,VkImageTiling tiling,VkImageFormatProperties * pImageFormatProperties,VkSamplerYcbcrConversionImageFormatProperties * pYcbcrImageFormatProperties)561 get_image_format_properties(
562 struct v3dv_physical_device *physical_device,
563 const VkPhysicalDeviceImageFormatInfo2 *info,
564 VkImageTiling tiling,
565 VkImageFormatProperties *pImageFormatProperties,
566 VkSamplerYcbcrConversionImageFormatProperties *pYcbcrImageFormatProperties)
567 {
568 const struct v3dv_format *v3dv_format = v3dv_get_format(info->format);
569 VkFormatFeatureFlags format_feature_flags =
570 image_format_features(info->format, v3dv_format, tiling);
571 if (!format_feature_flags)
572 goto unsupported;
573
574 if (info->usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
575 if (!(format_feature_flags & VK_FORMAT_FEATURE_TRANSFER_SRC_BIT)) {
576 goto unsupported;
577 }
578
579 /* Sampling of raster depth/stencil images is not supported. Since 1D
580 * images are always raster, even if the user requested optimal tiling,
581 * we can't have them be used as transfer sources, since that includes
582 * using them for blit sources, which might require sampling.
583 */
584 if (info->type == VK_IMAGE_TYPE_1D &&
585 vk_format_is_depth_or_stencil(info->format)) {
586 goto unsupported;
587 }
588 }
589
590 if (info->usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
591 if (!(format_feature_flags & VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
592 goto unsupported;
593 }
594 }
595
596 if (info->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
597 if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
598 goto unsupported;
599 }
600
601 /* Sampling of raster depth/stencil images is not supported. Since 1D
602 * images are always raster, even if the user requested optimal tiling,
603 * we can't allow sampling if the format is depth/stencil.
604 */
605 if (info->type == VK_IMAGE_TYPE_1D &&
606 vk_format_is_depth_or_stencil(info->format)) {
607 goto unsupported;
608 }
609 }
610
611 if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
612 if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
613 goto unsupported;
614 }
615 }
616
617 if (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
618 if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
619 goto unsupported;
620 }
621 }
622
623 if (info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
624 if (!(format_feature_flags &
625 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
626 goto unsupported;
627 }
628 }
629
630 /* FIXME: these are taken from VkPhysicalDeviceLimits, we should just put
631 * these limits available in the physical device and read them from there
632 * wherever we need them.
633 */
634 switch (info->type) {
635 case VK_IMAGE_TYPE_1D:
636 pImageFormatProperties->maxExtent.width = 4096;
637 pImageFormatProperties->maxExtent.height = 1;
638 pImageFormatProperties->maxExtent.depth = 1;
639 pImageFormatProperties->maxArrayLayers = 2048;
640 pImageFormatProperties->maxMipLevels = 13; /* log2(maxWidth) + 1 */
641 break;
642 case VK_IMAGE_TYPE_2D:
643 pImageFormatProperties->maxExtent.width = 4096;
644 pImageFormatProperties->maxExtent.height = 4096;
645 pImageFormatProperties->maxExtent.depth = 1;
646 pImageFormatProperties->maxArrayLayers = 2048;
647 pImageFormatProperties->maxMipLevels = 13; /* log2(maxWidth) + 1 */
648 break;
649 case VK_IMAGE_TYPE_3D:
650 pImageFormatProperties->maxExtent.width = 4096;
651 pImageFormatProperties->maxExtent.height = 4096;
652 pImageFormatProperties->maxExtent.depth = 4096;
653 pImageFormatProperties->maxArrayLayers = 1;
654 pImageFormatProperties->maxMipLevels = 13; /* log2(maxWidth) + 1 */
655 break;
656 default:
657 unreachable("bad VkImageType");
658 }
659
660 /* Our hw doesn't support 1D compressed textures. */
661 if (info->type == VK_IMAGE_TYPE_1D &&
662 vk_format_is_compressed(info->format)) {
663 goto unsupported;
664 }
665
666 /* From the Vulkan 1.0 spec, section 34.1.1. Supported Sample Counts:
667 *
668 * sampleCounts will be set to VK_SAMPLE_COUNT_1_BIT if at least one of the
669 * following conditions is true:
670 *
671 * - tiling is VK_IMAGE_TILING_LINEAR
672 * - type is not VK_IMAGE_TYPE_2D
673 * - flags contains VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
674 * - neither the VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT flag nor the
675 * VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT flag in
676 * VkFormatProperties::optimalTilingFeatures returned by
677 * vkGetPhysicalDeviceFormatProperties is set.
678 */
679 pImageFormatProperties->sampleCounts = VK_SAMPLE_COUNT_1_BIT;
680 if (tiling != VK_IMAGE_TILING_LINEAR &&
681 info->type == VK_IMAGE_TYPE_2D &&
682 !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
683 (format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT ||
684 format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
685 pImageFormatProperties->sampleCounts |= VK_SAMPLE_COUNT_4_BIT;
686 }
687
688 if (tiling == VK_IMAGE_TILING_LINEAR)
689 pImageFormatProperties->maxMipLevels = 1;
690
691 pImageFormatProperties->maxResourceSize = 0xffffffff; /* 32-bit allocation */
692
693 return VK_SUCCESS;
694
695 unsupported:
696 *pImageFormatProperties = (VkImageFormatProperties) {
697 .maxExtent = { 0, 0, 0 },
698 .maxMipLevels = 0,
699 .maxArrayLayers = 0,
700 .sampleCounts = 0,
701 .maxResourceSize = 0,
702 };
703
704 return VK_ERROR_FORMAT_NOT_SUPPORTED;
705 }
706
707 static const VkExternalMemoryProperties prime_fd_props = {
708 .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
709 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
710 .exportFromImportedHandleTypes =
711 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
712 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
713 .compatibleHandleTypes =
714 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
715 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
716 };
717
718 VkResult
v3dv_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,VkImageTiling tiling,VkImageUsageFlags usage,VkImageCreateFlags createFlags,VkImageFormatProperties * pImageFormatProperties)719 v3dv_GetPhysicalDeviceImageFormatProperties(
720 VkPhysicalDevice physicalDevice,
721 VkFormat format,
722 VkImageType type,
723 VkImageTiling tiling,
724 VkImageUsageFlags usage,
725 VkImageCreateFlags createFlags,
726 VkImageFormatProperties *pImageFormatProperties)
727 {
728 V3DV_FROM_HANDLE(v3dv_physical_device, physical_device, physicalDevice);
729
730 const VkPhysicalDeviceImageFormatInfo2 info = {
731 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
732 .pNext = NULL,
733 .format = format,
734 .type = type,
735 .tiling = tiling,
736 .usage = usage,
737 .flags = createFlags,
738 };
739
740 return get_image_format_properties(physical_device, &info, tiling,
741 pImageFormatProperties, NULL);
742 }
743
744 VkResult
v3dv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * base_info,VkImageFormatProperties2 * base_props)745 v3dv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,
746 const VkPhysicalDeviceImageFormatInfo2 *base_info,
747 VkImageFormatProperties2 *base_props)
748 {
749 V3DV_FROM_HANDLE(v3dv_physical_device, physical_device, physicalDevice);
750 const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
751 const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *drm_format_mod_info = NULL;
752 VkExternalImageFormatProperties *external_props = NULL;
753 VkImageTiling tiling = base_info->tiling;
754
755 /* Extract input structs */
756 vk_foreach_struct_const(s, base_info->pNext) {
757 switch (s->sType) {
758 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
759 external_info = (const void *) s;
760 break;
761 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT:
762 drm_format_mod_info = (const void *) s;
763 switch (drm_format_mod_info->drmFormatModifier) {
764 case DRM_FORMAT_MOD_LINEAR:
765 tiling = VK_IMAGE_TILING_LINEAR;
766 break;
767 case DRM_FORMAT_MOD_BROADCOM_UIF:
768 tiling = VK_IMAGE_TILING_OPTIMAL;
769 break;
770 default:
771 assert("Unknown DRM format modifier");
772 }
773 break;
774 default:
775 v3dv_debug_ignored_stype(s->sType);
776 break;
777 }
778 }
779
780 assert(tiling == VK_IMAGE_TILING_OPTIMAL ||
781 tiling == VK_IMAGE_TILING_LINEAR);
782
783 /* Extract output structs */
784 vk_foreach_struct(s, base_props->pNext) {
785 switch (s->sType) {
786 case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
787 external_props = (void *) s;
788 break;
789 default:
790 v3dv_debug_ignored_stype(s->sType);
791 break;
792 }
793 }
794
795 VkResult result =
796 get_image_format_properties(physical_device, base_info, tiling,
797 &base_props->imageFormatProperties, NULL);
798 if (result != VK_SUCCESS)
799 goto done;
800
801 if (external_info && external_info->handleType != 0) {
802 switch (external_info->handleType) {
803 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
804 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
805 if (external_props)
806 external_props->externalMemoryProperties = prime_fd_props;
807 break;
808 default:
809 result = VK_ERROR_FORMAT_NOT_SUPPORTED;
810 break;
811 }
812 }
813
814 done:
815 return result;
816 }
817
818 void
v3dv_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,VkSampleCountFlagBits samples,VkImageUsageFlags usage,VkImageTiling tiling,uint32_t * pPropertyCount,VkSparseImageFormatProperties * pProperties)819 v3dv_GetPhysicalDeviceSparseImageFormatProperties(
820 VkPhysicalDevice physicalDevice,
821 VkFormat format,
822 VkImageType type,
823 VkSampleCountFlagBits samples,
824 VkImageUsageFlags usage,
825 VkImageTiling tiling,
826 uint32_t *pPropertyCount,
827 VkSparseImageFormatProperties *pProperties)
828 {
829 *pPropertyCount = 0;
830 }
831
832 void
v3dv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)833 v3dv_GetPhysicalDeviceSparseImageFormatProperties2(
834 VkPhysicalDevice physicalDevice,
835 const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
836 uint32_t *pPropertyCount,
837 VkSparseImageFormatProperties2 *pProperties)
838 {
839 *pPropertyCount = 0;
840 }
841
842 void
v3dv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)843 v3dv_GetPhysicalDeviceExternalBufferProperties(
844 VkPhysicalDevice physicalDevice,
845 const VkPhysicalDeviceExternalBufferInfo *pExternalBufferInfo,
846 VkExternalBufferProperties *pExternalBufferProperties)
847 {
848 switch (pExternalBufferInfo->handleType) {
849 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
850 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
851 pExternalBufferProperties->externalMemoryProperties = prime_fd_props;
852 return;
853 default: /* Unsupported */
854 pExternalBufferProperties->externalMemoryProperties =
855 (VkExternalMemoryProperties) {
856 .compatibleHandleTypes = pExternalBufferInfo->handleType,
857 };
858 break;
859 }
860 }
861