• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #include "tu_formats.h"
8 
9 #include "fdl/fd6_format_table.h"
10 
11 #include "vk_enum_defines.h"
12 #include "vk_util.h"
13 #include "drm-uapi/drm_fourcc.h"
14 
15 #include "tu_device.h"
16 #include "tu_image.h"
17 
18 /* Map non-colorspace-converted YUV formats to RGB pipe formats where we can,
19  * since our hardware doesn't support colorspace conversion.
20  *
21  * Really, we should probably be returning the RGB formats in
22  * vk_format_to_pipe_format, but we don't have all the equivalent pipe formats
23  * for VK RGB formats yet, and we'd have to switch all consumers of that
24  * function at once.
25  */
26 enum pipe_format
tu_vk_format_to_pipe_format(VkFormat vk_format)27 tu_vk_format_to_pipe_format(VkFormat vk_format)
28 {
29    switch (vk_format) {
30    case VK_FORMAT_R10X6_UNORM_PACK16:
31    case VK_FORMAT_R10X6G10X6_UNORM_2PACK16:
32       return PIPE_FORMAT_NONE; /* These fail some CTS tests */
33    case VK_FORMAT_G8B8G8R8_422_UNORM: /* YUYV */
34       return PIPE_FORMAT_R8G8_R8B8_UNORM;
35    case VK_FORMAT_B8G8R8G8_422_UNORM: /* UYVY */
36       return PIPE_FORMAT_G8R8_B8R8_UNORM;
37    case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
38       return PIPE_FORMAT_G8_B8R8_420_UNORM;
39    case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
40       return PIPE_FORMAT_G8_B8_R8_420_UNORM;
41    default:
42       return vk_format_to_pipe_format(vk_format);
43    }
44 }
45 
46 static bool
tu6_format_vtx_supported(enum pipe_format format)47 tu6_format_vtx_supported(enum pipe_format format)
48 {
49    return fd6_vertex_format(format) != FMT6_NONE;
50 }
51 
52 struct tu_native_format
tu6_format_vtx(enum pipe_format format)53 tu6_format_vtx(enum pipe_format format)
54 {
55    struct tu_native_format fmt = {
56       .fmt = fd6_vertex_format(format),
57       .swap = fd6_vertex_swap(format),
58    };
59    assert(tu6_format_vtx_supported(format));
60    return fmt;
61 }
62 
63 static bool
tu6_format_color_supported(enum pipe_format format)64 tu6_format_color_supported(enum pipe_format format)
65 {
66    return fd6_color_format(format, TILE6_LINEAR) != FMT6_NONE;
67 }
68 
69 struct tu_native_format
tu6_format_color(enum pipe_format format,enum a6xx_tile_mode tile_mode)70 tu6_format_color(enum pipe_format format, enum a6xx_tile_mode tile_mode)
71 {
72    struct tu_native_format fmt = {
73       .fmt = fd6_color_format(format, tile_mode),
74       .swap = fd6_color_swap(format, tile_mode),
75    };
76    assert(fmt.fmt != FMT6_NONE);
77    return fmt;
78 }
79 
80 static bool
tu6_format_texture_supported(enum pipe_format format)81 tu6_format_texture_supported(enum pipe_format format)
82 {
83    return fd6_texture_format(format, TILE6_LINEAR) != FMT6_NONE;
84 }
85 
86 struct tu_native_format
tu6_format_texture(enum pipe_format format,enum a6xx_tile_mode tile_mode)87 tu6_format_texture(enum pipe_format format, enum a6xx_tile_mode tile_mode)
88 {
89    struct tu_native_format fmt = {
90       .fmt = fd6_texture_format(format, tile_mode),
91       .swap = fd6_texture_swap(format, tile_mode),
92    };
93    assert(fmt.fmt != FMT6_NONE);
94    return fmt;
95 }
96 
97 enum tu6_ubwc_compat_type {
98    TU6_UBWC_UNKNOWN_COMPAT,
99    TU6_UBWC_R8G8_UNORM,
100    TU6_UBWC_R8G8_INT,
101    TU6_UBWC_R8G8B8A8_UNORM,
102    TU6_UBWC_R8G8B8A8_INT,
103    TU6_UBWC_B8G8R8A8_UNORM,
104    TU6_UBWC_R16G16_UNORM,
105    TU6_UBWC_R16G16_INT,
106    TU6_UBWC_R16G16B16A16_UNORM,
107    TU6_UBWC_R16G16B16A16_INT,
108    TU6_UBWC_R32_INT,
109    TU6_UBWC_R32G32_INT,
110    TU6_UBWC_R32G32B32A32_INT,
111    TU6_UBWC_R32_FLOAT,
112 };
113 
114 static enum tu6_ubwc_compat_type
tu6_ubwc_compat_mode(const struct fd_dev_info * info,VkFormat format)115 tu6_ubwc_compat_mode(const struct fd_dev_info *info, VkFormat format)
116 {
117    switch (format) {
118    case VK_FORMAT_R8G8_UNORM:
119    case VK_FORMAT_R8G8_SRGB:
120       return info->a7xx.ubwc_unorm_snorm_int_compatible ?
121          TU6_UBWC_R8G8_INT : TU6_UBWC_R8G8_UNORM;
122 
123    case VK_FORMAT_R8G8_SNORM:
124       return info->a7xx.ubwc_unorm_snorm_int_compatible ?
125          TU6_UBWC_R8G8_INT : TU6_UBWC_UNKNOWN_COMPAT;
126 
127    case VK_FORMAT_R8G8_UINT:
128    case VK_FORMAT_R8G8_SINT:
129       return TU6_UBWC_R8G8_INT;
130 
131    case VK_FORMAT_R8G8B8A8_UNORM:
132    case VK_FORMAT_R8G8B8A8_SRGB:
133    case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
134    case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
135       return info->a7xx.ubwc_unorm_snorm_int_compatible ?
136          TU6_UBWC_R8G8B8A8_INT : TU6_UBWC_R8G8B8A8_UNORM;
137 
138    case VK_FORMAT_R8G8B8A8_SNORM:
139       return info->a7xx.ubwc_unorm_snorm_int_compatible ?
140          TU6_UBWC_R8G8B8A8_INT : TU6_UBWC_UNKNOWN_COMPAT;
141 
142    case VK_FORMAT_R8G8B8A8_UINT:
143    case VK_FORMAT_R8G8B8A8_SINT:
144    case VK_FORMAT_A8B8G8R8_UINT_PACK32:
145    case VK_FORMAT_A8B8G8R8_SINT_PACK32:
146       return TU6_UBWC_R8G8B8A8_INT;
147 
148    case VK_FORMAT_R16G16_UNORM:
149       return info->a7xx.ubwc_unorm_snorm_int_compatible ?
150          TU6_UBWC_R16G16_INT : TU6_UBWC_R16G16_UNORM;
151 
152    case VK_FORMAT_R16G16_SNORM:
153       return info->a7xx.ubwc_unorm_snorm_int_compatible ?
154          TU6_UBWC_R16G16_INT : TU6_UBWC_UNKNOWN_COMPAT;
155 
156    case VK_FORMAT_R16G16_UINT:
157    case VK_FORMAT_R16G16_SINT:
158       return TU6_UBWC_R16G16_INT;
159 
160    case VK_FORMAT_R16G16B16A16_UNORM:
161       return info->a7xx.ubwc_unorm_snorm_int_compatible ?
162          TU6_UBWC_R16G16B16A16_INT : TU6_UBWC_R16G16B16A16_UNORM;
163 
164    case VK_FORMAT_R16G16B16A16_SNORM:
165       return info->a7xx.ubwc_unorm_snorm_int_compatible ?
166          TU6_UBWC_R16G16B16A16_INT : TU6_UBWC_UNKNOWN_COMPAT;
167 
168    case VK_FORMAT_R16G16B16A16_UINT:
169    case VK_FORMAT_R16G16B16A16_SINT:
170       return TU6_UBWC_R16G16B16A16_INT;
171 
172    case VK_FORMAT_R32_UINT:
173    case VK_FORMAT_R32_SINT:
174       return TU6_UBWC_R32_INT;
175 
176    case VK_FORMAT_R32G32_UINT:
177    case VK_FORMAT_R32G32_SINT:
178       return TU6_UBWC_R32G32_INT;
179 
180    case VK_FORMAT_R32G32B32A32_UINT:
181    case VK_FORMAT_R32G32B32A32_SINT:
182       return TU6_UBWC_R32G32B32A32_INT;
183 
184    case VK_FORMAT_D32_SFLOAT:
185    case VK_FORMAT_R32_SFLOAT:
186       /* TODO: a630 blob allows these, but not a660.  When is it legal? */
187       return TU6_UBWC_UNKNOWN_COMPAT;
188 
189    case VK_FORMAT_B8G8R8A8_UNORM:
190    case VK_FORMAT_B8G8R8A8_SRGB:
191       /* The blob doesn't list these as compatible, but they surely are.
192        * freedreno's happy to cast between them, and zink would really like
193        * to.
194        */
195       return TU6_UBWC_B8G8R8A8_UNORM;
196 
197    default:
198       return TU6_UBWC_UNKNOWN_COMPAT;
199    }
200 }
201 
202 bool
tu6_mutable_format_list_ubwc_compatible(const struct fd_dev_info * info,const VkImageFormatListCreateInfo * fmt_list)203 tu6_mutable_format_list_ubwc_compatible(const struct fd_dev_info *info,
204                                         const VkImageFormatListCreateInfo *fmt_list)
205 {
206    if (!fmt_list || !fmt_list->viewFormatCount)
207       return false;
208 
209    /* We're only looking at format list cross compatibility here, check
210     * ubwc_possible() for the base "is the format UBWC-able at all?"
211     */
212    if (fmt_list->viewFormatCount == 1)
213       return true;
214 
215    enum tu6_ubwc_compat_type type =
216       tu6_ubwc_compat_mode(info, fmt_list->pViewFormats[0]);
217    if (type == TU6_UBWC_UNKNOWN_COMPAT)
218       return false;
219 
220    for (uint32_t i = 1; i < fmt_list->viewFormatCount; i++) {
221       if (tu6_ubwc_compat_mode(info, fmt_list->pViewFormats[i]) != type)
222          return false;
223    }
224 
225    return true;
226 }
227 
228 static void
tu_physical_device_get_format_properties(struct tu_physical_device * physical_device,VkFormat vk_format,VkFormatProperties3 * out_properties)229 tu_physical_device_get_format_properties(
230    struct tu_physical_device *physical_device,
231    VkFormat vk_format,
232    VkFormatProperties3 *out_properties)
233 {
234    VkFormatFeatureFlags2 linear = 0, optimal = 0, buffer = 0;
235    enum pipe_format format = tu_vk_format_to_pipe_format(vk_format);
236    const struct util_format_description *desc = util_format_description(format);
237 
238    bool supported_vtx = tu6_format_vtx_supported(format);
239    bool supported_color = tu6_format_color_supported(format);
240    bool supported_tex = tu6_format_texture_supported(format);
241    bool is_npot = !util_is_power_of_two_or_zero(desc->block.bits);
242 
243    if (format == PIPE_FORMAT_NONE ||
244        !(supported_vtx || supported_color || supported_tex)) {
245       goto end;
246    }
247 
248    /* We don't support BufferToImage/ImageToBuffer for npot formats */
249    if (!is_npot)
250       buffer |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
251 
252    if (supported_vtx)
253       buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
254 
255    if (supported_tex)
256       buffer |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;
257 
258    /* Don't support anything but texel buffers for non-power-of-two formats
259     * with 3 components. We'd need several workarounds for copying and
260     * clearing them because they're not renderable.
261     */
262    if (supported_tex && !is_npot) {
263       optimal |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
264                  VK_FORMAT_FEATURE_TRANSFER_DST_BIT |
265                  VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
266                  VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
267 
268       /* no blit src bit for YUYV/NV12/I420 formats */
269       if (desc->layout != UTIL_FORMAT_LAYOUT_SUBSAMPLED &&
270           desc->layout != UTIL_FORMAT_LAYOUT_PLANAR2 &&
271           desc->layout != UTIL_FORMAT_LAYOUT_PLANAR3) {
272          optimal |= VK_FORMAT_FEATURE_BLIT_SRC_BIT;
273       } else {
274          optimal |= VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT;
275 
276          if (desc->layout != UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
277             optimal |= VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT |
278                        VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT;
279             if (physical_device->info->a6xx.has_separate_chroma_filter)
280                optimal |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT;
281          }
282       }
283 
284       if (!vk_format_is_int(vk_format)) {
285          optimal |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
286 
287          if (physical_device->vk.supported_extensions.EXT_filter_cubic)
288             optimal |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_EXT;
289       }
290 
291       /* We sample on the CPU so we can technically support anything as long
292        * as it's floating point, but this restricts it to "reasonable" formats
293        * to use, which means two channels and not something weird like
294        * luminance-alpha.
295        */
296       if (util_format_is_float(format) &&
297           desc->nr_channels == 2 && desc->swizzle[0] == PIPE_SWIZZLE_X &&
298           desc->swizzle[1] == PIPE_SWIZZLE_Y) {
299          optimal |= VK_FORMAT_FEATURE_FRAGMENT_DENSITY_MAP_BIT_EXT;
300       }
301    }
302 
303    if (supported_color) {
304       assert(supported_tex);
305       optimal |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
306                  VK_FORMAT_FEATURE_BLIT_DST_BIT |
307                  VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT |
308                  VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
309                  VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
310 
311       buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT |
312                 VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
313                 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
314 
315       /* TODO: The blob also exposes these for R16G16_UINT/R16G16_SINT, but we
316        * don't have any tests for those.
317        */
318       if (vk_format == VK_FORMAT_R32_UINT || vk_format == VK_FORMAT_R32_SINT) {
319          optimal |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
320          buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
321       }
322 
323       if (!util_format_is_pure_integer(format))
324          optimal |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
325    }
326 
327    /* For the most part, we can do anything with a linear image that we could
328     * do with a tiled image. However, we can't support sysmem rendering with a
329     * linear depth texture, because we don't know if there's a bit to control
330     * the tiling of the depth buffer in BYPASS mode, and the blob also
331     * disables linear depth rendering, so there's no way to discover it. We
332     * also can't force GMEM mode, because there are other situations where we
333     * have to use sysmem rendering. So follow the blob here, and only enable
334     * DEPTH_STENCIL_ATTACHMENT_BIT for the optimal features.
335     */
336    linear = optimal;
337    if (tu6_pipe2depth(vk_format) != DEPTH6_NONE)
338       optimal |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
339 
340    if (!tiling_possible(vk_format) &&
341        /* We don't actually support tiling for this format, but we need to
342         * fake it as it's required by VK_KHR_sampler_ycbcr_conversion.
343         */
344        vk_format != VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM) {
345       optimal = 0;
346    }
347 
348    if (vk_format == VK_FORMAT_G8B8G8R8_422_UNORM ||
349        vk_format == VK_FORMAT_B8G8R8G8_422_UNORM ||
350        vk_format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM ||
351        vk_format == VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM) {
352       /* Disable buffer texturing of subsampled (422) and planar YUV textures.
353        * The subsampling requirement comes from "If format is a block-compressed
354        * format, then bufferFeatures must not support any features for the
355        * format" plus the specification of subsampled as 2x1 compressed block
356        * format.  I couldn't find the citation for planar, but 1D access of
357        * planar YUV would be really silly.
358        */
359       buffer = 0;
360    }
361 
362    /* We don't support writing into VK_FORMAT_*_PACK16 images/buffers  */
363    if (desc->nr_channels > 2 && desc->block.bits == 16) {
364       buffer &= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;
365       linear &= ~(VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT |
366                   VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT);
367       optimal &= ~(VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT |
368                    VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT);
369    }
370 
371    /* All our depth formats support shadow comparisons. */
372    if (vk_format_has_depth(vk_format) && (optimal & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
373       optimal |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_DEPTH_COMPARISON_BIT;
374       linear |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_DEPTH_COMPARISON_BIT;
375    }
376 
377    /* From the Vulkan 1.3.205 spec, section 19.3 "43.3. Required Format Support":
378     *
379     *    Mandatory format support: depth/stencil with VkImageType
380     *    VK_IMAGE_TYPE_2D
381     *    [...]
382     *    bufferFeatures must not support any features for these formats
383     */
384    if (vk_format_is_depth_or_stencil(vk_format))
385       buffer = 0;
386 
387    /* D32_SFLOAT_S8_UINT is tiled as two images, so no linear format
388     * blob enables some linear features, but its not useful, so don't bother.
389     */
390    if (vk_format == VK_FORMAT_D32_SFLOAT_S8_UINT)
391       linear = 0;
392 
393 end:
394    out_properties->linearTilingFeatures = linear;
395    out_properties->optimalTilingFeatures = optimal;
396    out_properties->bufferFeatures = buffer;
397 }
398 
399 VKAPI_ATTR void VKAPI_CALL
tu_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties2 * pFormatProperties)400 tu_GetPhysicalDeviceFormatProperties2(
401    VkPhysicalDevice physicalDevice,
402    VkFormat format,
403    VkFormatProperties2 *pFormatProperties)
404 {
405    TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
406 
407    VkFormatProperties3 local_props3;
408    VkFormatProperties3 *props3 =
409       vk_find_struct(pFormatProperties->pNext, FORMAT_PROPERTIES_3);
410    if (!props3)
411       props3 = &local_props3;
412 
413    tu_physical_device_get_format_properties(
414       physical_device, format, props3);
415 
416    pFormatProperties->formatProperties = (VkFormatProperties) {
417       .linearTilingFeatures =
418          vk_format_features2_to_features(props3->linearTilingFeatures),
419       .optimalTilingFeatures =
420          vk_format_features2_to_features(props3->optimalTilingFeatures),
421       .bufferFeatures =
422          vk_format_features2_to_features(props3->bufferFeatures),
423    };
424 
425    VkDrmFormatModifierPropertiesListEXT *list =
426       vk_find_struct(pFormatProperties->pNext, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT);
427    if (list) {
428       VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierPropertiesEXT, out,
429                              list->pDrmFormatModifierProperties,
430                              &list->drmFormatModifierCount);
431 
432       if (pFormatProperties->formatProperties.linearTilingFeatures) {
433          vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out, mod_props) {
434             mod_props->drmFormatModifier = DRM_FORMAT_MOD_LINEAR;
435             mod_props->drmFormatModifierPlaneCount = tu6_plane_count(format);
436             mod_props->drmFormatModifierTilingFeatures =
437                pFormatProperties->formatProperties.linearTilingFeatures;
438          }
439       }
440 
441       /* note: ubwc_possible() argument values to be ignored except for format */
442       if (pFormatProperties->formatProperties.optimalTilingFeatures &&
443           tiling_possible(format) &&
444           ubwc_possible(NULL, format, VK_IMAGE_TYPE_2D, 0, 0,
445                         physical_device->info, VK_SAMPLE_COUNT_1_BIT,
446                         false)) {
447          vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out, mod_props) {
448             mod_props->drmFormatModifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
449             mod_props->drmFormatModifierPlaneCount = tu6_plane_count(format);
450             mod_props->drmFormatModifierTilingFeatures =
451                pFormatProperties->formatProperties.optimalTilingFeatures;
452          }
453       }
454    }
455 }
456 
457 static VkResult
tu_image_unsupported_format(VkImageFormatProperties * pImageFormatProperties)458 tu_image_unsupported_format(VkImageFormatProperties *pImageFormatProperties)
459 {
460    *pImageFormatProperties = (VkImageFormatProperties) {
461       .maxExtent = { 0, 0, 0 },
462       .maxMipLevels = 0,
463       .maxArrayLayers = 0,
464       .sampleCounts = 0,
465       .maxResourceSize = 0,
466    };
467 
468    return VK_ERROR_FORMAT_NOT_SUPPORTED;
469 }
470 
471 static VkResult
tu_get_image_format_properties(struct tu_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * info,VkImageFormatProperties * pImageFormatProperties,VkFormatFeatureFlags * p_feature_flags)472 tu_get_image_format_properties(
473    struct tu_physical_device *physical_device,
474    const VkPhysicalDeviceImageFormatInfo2 *info,
475    VkImageFormatProperties *pImageFormatProperties,
476    VkFormatFeatureFlags *p_feature_flags)
477 {
478    VkFormatProperties3 format_props;
479    VkFormatFeatureFlags format_feature_flags;
480    VkExtent3D maxExtent;
481    uint32_t maxMipLevels;
482    uint32_t maxArraySize;
483    BITMASK_ENUM(VkSampleCountFlagBits) sampleCounts = VK_SAMPLE_COUNT_1_BIT;
484 
485    tu_physical_device_get_format_properties(physical_device, info->format,
486                                             &format_props);
487 
488    switch (info->tiling) {
489    case VK_IMAGE_TILING_LINEAR:
490       format_feature_flags = format_props.linearTilingFeatures;
491       break;
492 
493    case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT: {
494       const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *drm_info =
495          vk_find_struct_const(info->pNext, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
496 
497       /* Subsampled format isn't stable yet, so don't allow
498        * importing/exporting with modifiers yet.
499        */
500       if (info->flags & VK_IMAGE_CREATE_SUBSAMPLED_BIT_EXT)
501          return VK_ERROR_FORMAT_NOT_SUPPORTED;
502 
503       switch (drm_info->drmFormatModifier) {
504       case DRM_FORMAT_MOD_QCOM_COMPRESSED:
505          /* falling back to linear/non-UBWC isn't possible with explicit modifier */
506 
507          /* formats which don't support tiling */
508          if (!format_props.optimalTilingFeatures ||
509              !tiling_possible(info->format))
510             return VK_ERROR_FORMAT_NOT_SUPPORTED;
511 
512          if (info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
513             const VkImageFormatListCreateInfo *format_list =
514                vk_find_struct_const(info->pNext,
515                                     IMAGE_FORMAT_LIST_CREATE_INFO);
516             if (!tu6_mutable_format_list_ubwc_compatible(physical_device->info,
517                                                          format_list))
518                return VK_ERROR_FORMAT_NOT_SUPPORTED;
519          }
520 
521          if (!ubwc_possible(NULL, info->format, info->type, info->usage,
522                             info->usage, physical_device->info, sampleCounts,
523                             false)) {
524             return VK_ERROR_FORMAT_NOT_SUPPORTED;
525          }
526 
527          format_feature_flags = format_props.optimalTilingFeatures;
528          break;
529       case DRM_FORMAT_MOD_LINEAR:
530          format_feature_flags = format_props.linearTilingFeatures;
531          break;
532       default:
533          return VK_ERROR_FORMAT_NOT_SUPPORTED;
534       }
535    } break;
536    case VK_IMAGE_TILING_OPTIMAL:
537       format_feature_flags = format_props.optimalTilingFeatures;
538       break;
539    default:
540       unreachable("bad VkPhysicalDeviceImageFormatInfo2");
541    }
542 
543    if (format_feature_flags == 0)
544       return tu_image_unsupported_format(pImageFormatProperties);
545 
546    if (info->type != VK_IMAGE_TYPE_2D &&
547        vk_format_is_depth_or_stencil(info->format))
548       return tu_image_unsupported_format(pImageFormatProperties);
549 
550    switch (info->type) {
551    default:
552       unreachable("bad vkimage type\n");
553    case VK_IMAGE_TYPE_1D:
554       maxExtent.width = 16384;
555       maxExtent.height = 1;
556       maxExtent.depth = 1;
557       maxMipLevels = 15; /* log2(maxWidth) + 1 */
558       maxArraySize = 2048;
559       break;
560    case VK_IMAGE_TYPE_2D:
561       maxExtent.width = 16384;
562       maxExtent.height = 16384;
563       maxExtent.depth = 1;
564       maxMipLevels = 15; /* log2(maxWidth) + 1 */
565       maxArraySize = 2048;
566       break;
567    case VK_IMAGE_TYPE_3D:
568       maxExtent.width = 2048;
569       maxExtent.height = 2048;
570       maxExtent.depth = 2048;
571       maxMipLevels = 12; /* log2(maxWidth) + 1 */
572       maxArraySize = 1;
573       break;
574    }
575 
576    if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
577        info->type == VK_IMAGE_TYPE_2D &&
578        (format_feature_flags &
579         (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
580          VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
581        !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
582        !(info->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
583       sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
584       /* note: most operations support 8 samples (GMEM render/resolve do at least)
585        * but some do not (which ones?), just disable 8 samples completely,
586        * (no 8x msaa matches the blob driver behavior)
587        */
588    }
589 
590    /* From the Vulkan 1.3.206 spec:
591     *
592     * "VK_IMAGE_CREATE_EXTENDED_USAGE_BIT specifies that the image can be
593     * created with usage flags that are not supported for the format the image
594     * is created with but are supported for at least one format a VkImageView
595     * created from the image can have."
596     *
597     * This means we should relax checks that only depend on the
598     * format_feature_flags, to allow the user to create images that may be
599     * e.g. reinterpreted as storage when the original format doesn't allow it.
600     * The user will have to check against the format features anyway.
601     * Otherwise we'd unnecessarily disallow it.
602     */
603 
604    VkImageUsageFlags image_usage = info->usage;
605    if (info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT)
606       image_usage = 0;
607 
608    if (image_usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
609       if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
610          return tu_image_unsupported_format(pImageFormatProperties);
611       }
612    }
613 
614    if (image_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
615       if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
616          return tu_image_unsupported_format(pImageFormatProperties);
617       }
618    }
619 
620    if (image_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
621       if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
622          return tu_image_unsupported_format(pImageFormatProperties);
623       }
624    }
625 
626    if (image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
627       if (!(format_feature_flags &
628             VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
629          return tu_image_unsupported_format(pImageFormatProperties);
630       }
631    }
632 
633    *pImageFormatProperties = (VkImageFormatProperties) {
634       .maxExtent = maxExtent,
635       .maxMipLevels = maxMipLevels,
636       .maxArrayLayers = maxArraySize,
637       .sampleCounts = sampleCounts,
638 
639       /* FINISHME: Accurately calculate
640        * VkImageFormatProperties::maxResourceSize.
641        */
642       .maxResourceSize = UINT32_MAX,
643    };
644 
645    if (p_feature_flags)
646       *p_feature_flags = format_feature_flags;
647 
648    return VK_SUCCESS;
649 }
650 
651 static VkResult
tu_get_external_image_format_properties(const struct tu_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * pImageFormatInfo,VkExternalMemoryHandleTypeFlagBits handleType,VkExternalImageFormatProperties * external_properties)652 tu_get_external_image_format_properties(
653    const struct tu_physical_device *physical_device,
654    const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
655    VkExternalMemoryHandleTypeFlagBits handleType,
656    VkExternalImageFormatProperties *external_properties)
657 {
658    BITMASK_ENUM(VkExternalMemoryFeatureFlagBits) flags = 0;
659    VkExternalMemoryHandleTypeFlags export_flags = 0;
660    VkExternalMemoryHandleTypeFlags compat_flags = 0;
661 
662    /* From the Vulkan 1.1.98 spec:
663     *
664     *    If handleType is not compatible with the format, type, tiling,
665     *    usage, and flags specified in VkPhysicalDeviceImageFormatInfo2,
666     *    then vkGetPhysicalDeviceImageFormatProperties2 returns
667     *    VK_ERROR_FORMAT_NOT_SUPPORTED.
668     */
669 
670    switch (handleType) {
671    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
672    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
673       switch (pImageFormatInfo->type) {
674       case VK_IMAGE_TYPE_2D:
675          flags = VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT |
676                  VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
677                  VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
678          compat_flags = export_flags =
679             VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
680             VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
681          break;
682       default:
683          return vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
684                           "VkExternalMemoryTypeFlagBits(0x%x) unsupported for VkImageType(%d)",
685                           handleType, pImageFormatInfo->type);
686       }
687       break;
688    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
689       flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
690       compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
691       break;
692    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
693       flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
694       compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
695       break;
696    default:
697       return vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
698                        "VkExternalMemoryTypeFlagBits(0x%x) unsupported",
699                        handleType);
700    }
701 
702    if (external_properties) {
703       external_properties->externalMemoryProperties =
704          (VkExternalMemoryProperties) {
705             .externalMemoryFeatures = flags,
706             .exportFromImportedHandleTypes = export_flags,
707             .compatibleHandleTypes = compat_flags,
708          };
709    }
710 
711    return VK_SUCCESS;
712 }
713 
714 VKAPI_ATTR VkResult VKAPI_CALL
tu_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * base_info,VkImageFormatProperties2 * base_props)715 tu_GetPhysicalDeviceImageFormatProperties2(
716    VkPhysicalDevice physicalDevice,
717    const VkPhysicalDeviceImageFormatInfo2 *base_info,
718    VkImageFormatProperties2 *base_props)
719 {
720    TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
721    const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
722    const VkPhysicalDeviceImageViewImageFormatInfoEXT *image_view_info = NULL;
723    VkExternalImageFormatProperties *external_props = NULL;
724    VkFilterCubicImageViewImageFormatPropertiesEXT *cubic_props = NULL;
725    VkFormatFeatureFlags format_feature_flags;
726    VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
727    VkResult result;
728 
729    result = tu_get_image_format_properties(physical_device,
730       base_info, &base_props->imageFormatProperties, &format_feature_flags);
731    if (result != VK_SUCCESS)
732       return result;
733 
734    /* Extract input structs */
735    vk_foreach_struct_const(s, base_info->pNext)
736    {
737       switch (s->sType) {
738       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
739          external_info = (const VkPhysicalDeviceExternalImageFormatInfo *) s;
740          break;
741       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_VIEW_IMAGE_FORMAT_INFO_EXT:
742          image_view_info = (const VkPhysicalDeviceImageViewImageFormatInfoEXT *) s;
743          break;
744       default:
745          break;
746       }
747    }
748 
749    /* Extract output structs */
750    vk_foreach_struct(s, base_props->pNext)
751    {
752       switch (s->sType) {
753       case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
754          external_props = (VkExternalImageFormatProperties *) s;
755          break;
756       case VK_STRUCTURE_TYPE_FILTER_CUBIC_IMAGE_VIEW_IMAGE_FORMAT_PROPERTIES_EXT:
757          cubic_props = (VkFilterCubicImageViewImageFormatPropertiesEXT *) s;
758          break;
759       case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
760          ycbcr_props = (VkSamplerYcbcrConversionImageFormatProperties *) s;
761          break;
762       default:
763          break;
764       }
765    }
766 
767    /* From the Vulkan 1.0.42 spec:
768     *
769     *    If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
770     *    behave as if VkPhysicalDeviceExternalImageFormatInfo was not
771     *    present and VkExternalImageFormatProperties will be ignored.
772     */
773    if (external_info && external_info->handleType != 0) {
774       result = tu_get_external_image_format_properties(
775          physical_device, base_info, external_info->handleType,
776          external_props);
777       if (result != VK_SUCCESS)
778          goto fail;
779    }
780 
781    if (cubic_props) {
782       /* note: blob only allows cubic filtering for 2D and 2D array views
783        * its likely we can enable it for 1D and CUBE, needs testing however
784        */
785       if ((image_view_info->imageViewType == VK_IMAGE_VIEW_TYPE_2D ||
786            image_view_info->imageViewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY) &&
787           (format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_EXT)) {
788          cubic_props->filterCubic = true;
789          cubic_props->filterCubicMinmax = true;
790       } else {
791          cubic_props->filterCubic = false;
792          cubic_props->filterCubicMinmax = false;
793       }
794    }
795 
796    if (ycbcr_props)
797       ycbcr_props->combinedImageSamplerDescriptorCount = 1;
798 
799    return VK_SUCCESS;
800 
801 fail:
802    if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
803       /* From the Vulkan 1.0.42 spec:
804        *
805        *    If the combination of parameters to
806        *    vkGetPhysicalDeviceImageFormatProperties2 is not supported by
807        *    the implementation for use in vkCreateImage, then all members of
808        *    imageFormatProperties will be filled with zero.
809        */
810       base_props->imageFormatProperties = (VkImageFormatProperties) {};
811    }
812 
813    return result;
814 }
815 
816 VKAPI_ATTR void VKAPI_CALL
tu_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)817 tu_GetPhysicalDeviceSparseImageFormatProperties2(
818    VkPhysicalDevice physicalDevice,
819    const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
820    uint32_t *pPropertyCount,
821    VkSparseImageFormatProperties2 *pProperties)
822 {
823    /* Sparse images are not yet supported. */
824    *pPropertyCount = 0;
825 }
826 
827 VKAPI_ATTR void VKAPI_CALL
tu_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)828 tu_GetPhysicalDeviceExternalBufferProperties(
829    VkPhysicalDevice physicalDevice,
830    const VkPhysicalDeviceExternalBufferInfo *pExternalBufferInfo,
831    VkExternalBufferProperties *pExternalBufferProperties)
832 {
833    BITMASK_ENUM(VkExternalMemoryFeatureFlagBits) flags = 0;
834    VkExternalMemoryHandleTypeFlags export_flags = 0;
835    VkExternalMemoryHandleTypeFlags compat_flags = 0;
836    switch (pExternalBufferInfo->handleType) {
837    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
838    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
839       flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
840               VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
841       compat_flags = export_flags =
842          VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
843          VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
844       break;
845    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
846       flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
847       compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
848       break;
849    default:
850       break;
851    }
852    pExternalBufferProperties->externalMemoryProperties =
853       (VkExternalMemoryProperties) {
854          .externalMemoryFeatures = flags,
855          .exportFromImportedHandleTypes = export_flags,
856          .compatibleHandleTypes = compat_flags,
857       };
858 }
859