xref: /aosp_15_r20/external/mesa3d/src/vulkan/runtime/vk_image.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2021 Intel Corporation
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 "vk_image.h"
25 
26 #if DETECT_OS_LINUX || DETECT_OS_BSD
27 #include <drm-uapi/drm_fourcc.h>
28 #endif
29 
30 #include "vk_alloc.h"
31 #include "vk_common_entrypoints.h"
32 #include "vk_device.h"
33 #include "vk_format.h"
34 #include "vk_format_info.h"
35 #include "vk_log.h"
36 #include "vk_physical_device.h"
37 #include "vk_render_pass.h"
38 #include "vk_util.h"
39 #include "vulkan/wsi/wsi_common.h"
40 
41 #if DETECT_OS_ANDROID
42 #include "vk_android.h"
43 #include <vulkan/vulkan_android.h>
44 #endif
45 
46 void
vk_image_init(struct vk_device * device,struct vk_image * image,const VkImageCreateInfo * pCreateInfo)47 vk_image_init(struct vk_device *device,
48               struct vk_image *image,
49               const VkImageCreateInfo *pCreateInfo)
50 {
51    vk_object_base_init(device, &image->base, VK_OBJECT_TYPE_IMAGE);
52 
53    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
54    assert(pCreateInfo->mipLevels > 0);
55    assert(pCreateInfo->arrayLayers > 0);
56    assert(pCreateInfo->samples > 0);
57    assert(pCreateInfo->extent.width > 0);
58    assert(pCreateInfo->extent.height > 0);
59    assert(pCreateInfo->extent.depth > 0);
60 
61    if (pCreateInfo->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
62       assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
63    if (pCreateInfo->flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT)
64       assert(pCreateInfo->imageType == VK_IMAGE_TYPE_3D);
65 
66    image->create_flags = pCreateInfo->flags;
67    image->image_type = pCreateInfo->imageType;
68    vk_image_set_format(image, pCreateInfo->format);
69    image->extent = vk_image_sanitize_extent(image, pCreateInfo->extent);
70    image->mip_levels = pCreateInfo->mipLevels;
71    image->array_layers = pCreateInfo->arrayLayers;
72    image->samples = pCreateInfo->samples;
73    image->tiling = pCreateInfo->tiling;
74    image->usage = pCreateInfo->usage;
75    image->sharing_mode = pCreateInfo->sharingMode;
76 
77    if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
78       const VkImageStencilUsageCreateInfo *stencil_usage_info =
79          vk_find_struct_const(pCreateInfo->pNext,
80                               IMAGE_STENCIL_USAGE_CREATE_INFO);
81       image->stencil_usage =
82          stencil_usage_info ? stencil_usage_info->stencilUsage :
83                               pCreateInfo->usage;
84    } else {
85       image->stencil_usage = 0;
86    }
87 
88    const VkExternalMemoryImageCreateInfo *ext_mem_info =
89       vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_MEMORY_IMAGE_CREATE_INFO);
90    if (ext_mem_info)
91       image->external_handle_types = ext_mem_info->handleTypes;
92    else
93       image->external_handle_types = 0;
94 
95    const struct wsi_image_create_info *wsi_info =
96       vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
97    image->wsi_legacy_scanout = wsi_info && wsi_info->scanout;
98 
99 #if DETECT_OS_LINUX || DETECT_OS_BSD
100    image->drm_format_mod = ((1ULL << 56) - 1) /* DRM_FORMAT_MOD_INVALID */;
101 #endif
102 
103 #if DETECT_OS_ANDROID
104    if (image->external_handle_types &
105              VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)
106       image->android_buffer_type = ANDROID_BUFFER_HARDWARE;
107 
108    const VkNativeBufferANDROID *native_buffer =
109       vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
110 
111    if (native_buffer != NULL) {
112       assert(image->android_buffer_type == ANDROID_BUFFER_NONE);
113       image->android_buffer_type = ANDROID_BUFFER_NATIVE;
114    }
115 
116    const VkExternalFormatANDROID *ext_format =
117       vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_FORMAT_ANDROID);
118    if (ext_format && ext_format->externalFormat != 0) {
119       assert(image->format == VK_FORMAT_UNDEFINED);
120       assert(image->external_handle_types &
121              VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID);
122       vk_image_set_format(image, (VkFormat)ext_format->externalFormat);
123    }
124 
125    image->ahb_format = vk_image_format_to_ahb_format(image->format);
126 #endif
127 
128    const VkImageCompressionControlEXT *compr_info =
129       vk_find_struct_const(pCreateInfo->pNext, IMAGE_COMPRESSION_CONTROL_EXT);
130    if (compr_info)
131       image->compr_flags = compr_info->flags;
132 }
133 
134 void *
vk_image_create(struct vk_device * device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * alloc,size_t size)135 vk_image_create(struct vk_device *device,
136                 const VkImageCreateInfo *pCreateInfo,
137                 const VkAllocationCallbacks *alloc,
138                 size_t size)
139 {
140    struct vk_image *image =
141       vk_zalloc2(&device->alloc, alloc, size, 8,
142                  VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
143    if (image == NULL)
144       return NULL;
145 
146    vk_image_init(device, image, pCreateInfo);
147 
148    return image;
149 }
150 
151 void
vk_image_finish(struct vk_image * image)152 vk_image_finish(struct vk_image *image)
153 {
154    vk_object_base_finish(&image->base);
155 }
156 
157 void
vk_image_destroy(struct vk_device * device,const VkAllocationCallbacks * alloc,struct vk_image * image)158 vk_image_destroy(struct vk_device *device,
159                  const VkAllocationCallbacks *alloc,
160                  struct vk_image *image)
161 {
162 #if DETECT_OS_ANDROID
163    if (image->anb_memory) {
164       device->dispatch_table.FreeMemory(
165          (VkDevice)device, image->anb_memory, alloc);
166    }
167 #endif
168    vk_object_free(device, alloc, image);
169 }
170 
171 #if DETECT_OS_LINUX || DETECT_OS_BSD
172 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_GetImageDrmFormatModifierPropertiesEXT(UNUSED VkDevice device,VkImage _image,VkImageDrmFormatModifierPropertiesEXT * pProperties)173 vk_common_GetImageDrmFormatModifierPropertiesEXT(UNUSED VkDevice device,
174                                                  VkImage _image,
175                                                  VkImageDrmFormatModifierPropertiesEXT *pProperties)
176 {
177    VK_FROM_HANDLE(vk_image, image, _image);
178 
179    assert(pProperties->sType ==
180           VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT);
181 
182    assert(image->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT);
183    pProperties->drmFormatModifier = image->drm_format_mod;
184 
185    return VK_SUCCESS;
186 }
187 #endif
188 
189 VKAPI_ATTR void VKAPI_CALL
vk_common_GetImageSubresourceLayout(VkDevice _device,VkImage _image,const VkImageSubresource * pSubresource,VkSubresourceLayout * pLayout)190 vk_common_GetImageSubresourceLayout(VkDevice _device, VkImage _image,
191                                     const VkImageSubresource *pSubresource,
192                                     VkSubresourceLayout *pLayout)
193 {
194    VK_FROM_HANDLE(vk_device, device, _device);
195 
196    const VkImageSubresource2KHR subresource = {
197       .sType = VK_STRUCTURE_TYPE_IMAGE_SUBRESOURCE_2_KHR,
198       .imageSubresource = *pSubresource,
199    };
200 
201    VkSubresourceLayout2KHR layout = {
202       .sType = VK_STRUCTURE_TYPE_SUBRESOURCE_LAYOUT_2_KHR
203    };
204 
205    device->dispatch_table.GetImageSubresourceLayout2KHR(_device, _image,
206                                                         &subresource, &layout);
207 
208    *pLayout = layout.subresourceLayout;
209 }
210 
211 void
vk_image_set_format(struct vk_image * image,VkFormat format)212 vk_image_set_format(struct vk_image *image, VkFormat format)
213 {
214    image->format = format;
215    image->aspects = vk_format_aspects(format);
216 }
217 
218 VkImageUsageFlags
vk_image_usage(const struct vk_image * image,VkImageAspectFlags aspect_mask)219 vk_image_usage(const struct vk_image *image,
220                VkImageAspectFlags aspect_mask)
221 {
222    /* From the Vulkan 1.2.131 spec:
223     *
224     *    "If the image was has a depth-stencil format and was created with
225     *    a VkImageStencilUsageCreateInfo structure included in the pNext
226     *    chain of VkImageCreateInfo, the usage is calculated based on the
227     *    subresource.aspectMask provided:
228     *
229     *     - If aspectMask includes only VK_IMAGE_ASPECT_STENCIL_BIT, the
230     *       implicit usage is equal to
231     *       VkImageStencilUsageCreateInfo::stencilUsage.
232     *
233     *     - If aspectMask includes only VK_IMAGE_ASPECT_DEPTH_BIT, the
234     *       implicit usage is equal to VkImageCreateInfo::usage.
235     *
236     *     - If both aspects are included in aspectMask, the implicit usage
237     *       is equal to the intersection of VkImageCreateInfo::usage and
238     *       VkImageStencilUsageCreateInfo::stencilUsage.
239     */
240    if (aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT) {
241       return image->stencil_usage;
242    } else if (aspect_mask == (VK_IMAGE_ASPECT_DEPTH_BIT |
243                               VK_IMAGE_ASPECT_STENCIL_BIT)) {
244       return image->usage & image->stencil_usage;
245    } else {
246       /* This also handles the color case */
247       return image->usage;
248    }
249 }
250 
251 #define VK_IMAGE_ASPECT_ANY_COLOR_MASK_MESA ( \
252    VK_IMAGE_ASPECT_COLOR_BIT | \
253    VK_IMAGE_ASPECT_PLANE_0_BIT | \
254    VK_IMAGE_ASPECT_PLANE_1_BIT | \
255    VK_IMAGE_ASPECT_PLANE_2_BIT)
256 
257 /** Expands the given aspect mask relative to the image
258  *
259  * If the image has color plane aspects VK_IMAGE_ASPECT_COLOR_BIT has been
260  * requested, this returns the aspects of the underlying image.
261  *
262  * For example,
263  *
264  *    VK_IMAGE_ASPECT_COLOR_BIT
265  *
266  * will be converted to
267  *
268  *    VK_IMAGE_ASPECT_PLANE_0_BIT |
269  *    VK_IMAGE_ASPECT_PLANE_1_BIT |
270  *    VK_IMAGE_ASPECT_PLANE_2_BIT
271  *
272  * for an image of format VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM.
273  */
274 VkImageAspectFlags
vk_image_expand_aspect_mask(const struct vk_image * image,VkImageAspectFlags aspect_mask)275 vk_image_expand_aspect_mask(const struct vk_image *image,
276                             VkImageAspectFlags aspect_mask)
277 {
278    if (aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT) {
279       assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_MASK_MESA);
280       return image->aspects;
281    } else {
282       assert(aspect_mask && !(aspect_mask & ~image->aspects));
283       return aspect_mask;
284    }
285 }
286 
287 VkExtent3D
vk_image_extent_to_elements(const struct vk_image * image,VkExtent3D extent)288 vk_image_extent_to_elements(const struct vk_image *image, VkExtent3D extent)
289 {
290    const struct util_format_description *fmt =
291       vk_format_description(image->format);
292 
293    extent = vk_image_sanitize_extent(image, extent);
294    extent.width = DIV_ROUND_UP(extent.width, fmt->block.width);
295    extent.height = DIV_ROUND_UP(extent.height, fmt->block.height);
296    extent.depth = DIV_ROUND_UP(extent.depth, fmt->block.depth);
297 
298    return extent;
299 }
300 
301 VkOffset3D
vk_image_offset_to_elements(const struct vk_image * image,VkOffset3D offset)302 vk_image_offset_to_elements(const struct vk_image *image, VkOffset3D offset)
303 {
304    const struct util_format_description *fmt =
305       vk_format_description(image->format);
306 
307    offset = vk_image_sanitize_offset(image, offset);
308 
309    assert(offset.x % fmt->block.width == 0);
310    assert(offset.y % fmt->block.height == 0);
311    assert(offset.z % fmt->block.depth == 0);
312 
313    offset.x /= fmt->block.width;
314    offset.y /= fmt->block.height;
315    offset.z /= fmt->block.depth;
316 
317    return offset;
318 }
319 
320 struct vk_image_buffer_layout
vk_image_buffer_copy_layout(const struct vk_image * image,const VkBufferImageCopy2 * region)321 vk_image_buffer_copy_layout(const struct vk_image *image,
322                             const VkBufferImageCopy2* region)
323 {
324    VkExtent3D extent = vk_image_sanitize_extent(image, region->imageExtent);
325 
326    const uint32_t row_length = region->bufferRowLength ?
327                                region->bufferRowLength : extent.width;
328    const uint32_t image_height = region->bufferImageHeight ?
329                                  region->bufferImageHeight : extent.height;
330 
331    const VkImageAspectFlags aspect = region->imageSubresource.aspectMask;
332    VkFormat format = vk_format_get_aspect_format(image->format, aspect);
333    const struct util_format_description *fmt = vk_format_description(format);
334 
335    assert(fmt->block.bits % 8 == 0);
336    const uint32_t element_size_B = fmt->block.bits / 8;
337 
338    const uint32_t row_stride_B =
339       DIV_ROUND_UP(row_length, fmt->block.width) * element_size_B;
340    const uint64_t image_stride_B =
341       DIV_ROUND_UP(image_height, fmt->block.height) * (uint64_t)row_stride_B;
342 
343    return (struct vk_image_buffer_layout) {
344       .row_length = row_length,
345       .image_height = image_height,
346       .element_size_B = element_size_B,
347       .row_stride_B = row_stride_B,
348       .image_stride_B = image_stride_B,
349    };
350 }
351 
352 struct vk_image_buffer_layout
vk_memory_to_image_copy_layout(const struct vk_image * image,const VkMemoryToImageCopyEXT * region)353 vk_memory_to_image_copy_layout(const struct vk_image *image,
354                                const VkMemoryToImageCopyEXT* region)
355 {
356    const VkBufferImageCopy2 bic = {
357       .bufferOffset = 0,
358       .bufferRowLength = region->memoryRowLength,
359       .bufferImageHeight = region->memoryImageHeight,
360       .imageSubresource = region->imageSubresource,
361       .imageOffset = region->imageOffset,
362       .imageExtent = region->imageExtent,
363    };
364    return vk_image_buffer_copy_layout(image, &bic);
365 }
366 
367 struct vk_image_buffer_layout
vk_image_to_memory_copy_layout(const struct vk_image * image,const VkImageToMemoryCopyEXT * region)368 vk_image_to_memory_copy_layout(const struct vk_image *image,
369                                const VkImageToMemoryCopyEXT* region)
370 {
371    const VkBufferImageCopy2 bic = {
372       .bufferOffset = 0,
373       .bufferRowLength = region->memoryRowLength,
374       .bufferImageHeight = region->memoryImageHeight,
375       .imageSubresource = region->imageSubresource,
376       .imageOffset = region->imageOffset,
377       .imageExtent = region->imageExtent,
378    };
379    return vk_image_buffer_copy_layout(image, &bic);
380 }
381 
382 static VkComponentSwizzle
remap_swizzle(VkComponentSwizzle swizzle,VkComponentSwizzle component)383 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component)
384 {
385    return swizzle == VK_COMPONENT_SWIZZLE_IDENTITY ? component : swizzle;
386 }
387 
388 void
vk_image_view_init(struct vk_device * device,struct vk_image_view * image_view,bool driver_internal,const VkImageViewCreateInfo * pCreateInfo)389 vk_image_view_init(struct vk_device *device,
390                    struct vk_image_view *image_view,
391                    bool driver_internal,
392                    const VkImageViewCreateInfo *pCreateInfo)
393 {
394    vk_object_base_init(device, &image_view->base, VK_OBJECT_TYPE_IMAGE_VIEW);
395 
396    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
397    VK_FROM_HANDLE(vk_image, image, pCreateInfo->image);
398 
399    image_view->create_flags = pCreateInfo->flags;
400    image_view->image = image;
401    image_view->view_type = pCreateInfo->viewType;
402 
403    image_view->format = pCreateInfo->format;
404    if (image_view->format == VK_FORMAT_UNDEFINED)
405       image_view->format = image->format;
406 
407    if (!driver_internal) {
408       switch (image_view->view_type) {
409       case VK_IMAGE_VIEW_TYPE_1D:
410       case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
411          assert(image->image_type == VK_IMAGE_TYPE_1D);
412          break;
413       case VK_IMAGE_VIEW_TYPE_2D:
414       case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
415          if (image->create_flags & (VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT |
416                                     VK_IMAGE_CREATE_2D_VIEW_COMPATIBLE_BIT_EXT))
417             assert(image->image_type == VK_IMAGE_TYPE_3D);
418          else
419             assert(image->image_type == VK_IMAGE_TYPE_2D);
420          break;
421       case VK_IMAGE_VIEW_TYPE_3D:
422          assert(image->image_type == VK_IMAGE_TYPE_3D);
423          break;
424       case VK_IMAGE_VIEW_TYPE_CUBE:
425       case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
426          assert(image->image_type == VK_IMAGE_TYPE_2D);
427          assert(image->create_flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT);
428          break;
429       default:
430          unreachable("Invalid image view type");
431       }
432    }
433 
434    const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
435 
436    if (driver_internal) {
437       image_view->aspects = range->aspectMask;
438       image_view->view_format = image_view->format;
439    } else {
440       image_view->aspects =
441          vk_image_expand_aspect_mask(image, range->aspectMask);
442 
443       assert(!(image_view->aspects & ~image->aspects));
444 
445       /* From the Vulkan 1.2.184 spec:
446        *
447        *    "If the image has a multi-planar format and
448        *    subresourceRange.aspectMask is VK_IMAGE_ASPECT_COLOR_BIT, and image
449        *    has been created with a usage value not containing any of the
450        *    VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR,
451        *    VK_IMAGE_USAGE_VIDEO_DECODE_SRC_BIT_KHR,
452        *    VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR,
453        *    VK_IMAGE_USAGE_VIDEO_ENCODE_DST_BIT_KHR,
454        *    VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR, and
455        *    VK_IMAGE_USAGE_VIDEO_ENCODE_DPB_BIT_KHR flags, then the format must
456        *    be identical to the image format, and the sampler to be used with the
457        *    image view must enable sampler Y′CBCR conversion."
458        *
459        * Since no one implements video yet, we can ignore the bits about video
460        * create flags and assume YCbCr formats match.
461        */
462       if ((image->aspects & VK_IMAGE_ASPECT_PLANE_1_BIT) &&
463           (range->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT))
464          assert(image_view->format == image->format);
465 
466       /* From the Vulkan 1.2.184 spec:
467        *
468        *    "Each depth/stencil format is only compatible with itself."
469        */
470       if (image_view->aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
471                                  VK_IMAGE_ASPECT_STENCIL_BIT))
472          assert(image_view->format == image->format);
473 
474       if (!(image->create_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))
475          assert(image_view->format == image->format);
476 
477       /* Restrict the format to only the planes chosen.
478        *
479        * For combined depth and stencil images, this means the depth-only or
480        * stencil-only format if only one aspect is chosen and the full
481        * combined format if both aspects are chosen.
482        *
483        * For single-plane color images, we just take the format as-is.  For
484        * multi-plane views of multi-plane images, this means we want the full
485        * multi-plane format.  For single-plane views of multi-plane images, we
486        * want a format compatible with the one plane.  Fortunately, this is
487        * already what the client gives us.  The Vulkan 1.2.184 spec says:
488        *
489        *    "If image was created with the VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
490        *    and the image has a multi-planar format, and if
491        *    subresourceRange.aspectMask is VK_IMAGE_ASPECT_PLANE_0_BIT,
492        *    VK_IMAGE_ASPECT_PLANE_1_BIT, or VK_IMAGE_ASPECT_PLANE_2_BIT,
493        *    format must be compatible with the corresponding plane of the
494        *    image, and the sampler to be used with the image view must not
495        *    enable sampler Y′CBCR conversion."
496        */
497       if (image_view->aspects == VK_IMAGE_ASPECT_STENCIL_BIT) {
498          image_view->view_format = vk_format_stencil_only(image_view->format);
499       } else if (image_view->aspects == VK_IMAGE_ASPECT_DEPTH_BIT) {
500          image_view->view_format = vk_format_depth_only(image_view->format);
501       } else {
502          image_view->view_format = image_view->format;
503       }
504    }
505 
506    image_view->swizzle = (VkComponentMapping) {
507       .r = remap_swizzle(pCreateInfo->components.r, VK_COMPONENT_SWIZZLE_R),
508       .g = remap_swizzle(pCreateInfo->components.g, VK_COMPONENT_SWIZZLE_G),
509       .b = remap_swizzle(pCreateInfo->components.b, VK_COMPONENT_SWIZZLE_B),
510       .a = remap_swizzle(pCreateInfo->components.a, VK_COMPONENT_SWIZZLE_A),
511    };
512 
513    assert(range->layerCount > 0);
514    assert(range->baseMipLevel < image->mip_levels);
515 
516    image_view->base_mip_level = range->baseMipLevel;
517    image_view->level_count = vk_image_subresource_level_count(image, range);
518    image_view->base_array_layer = range->baseArrayLayer;
519    image_view->layer_count = vk_image_subresource_layer_count(image, range);
520 
521    const VkImageViewMinLodCreateInfoEXT *min_lod_info =
522       vk_find_struct_const(pCreateInfo, IMAGE_VIEW_MIN_LOD_CREATE_INFO_EXT);
523    image_view->min_lod = min_lod_info ? min_lod_info->minLod : 0.0f;
524 
525    /* From the Vulkan 1.3.215 spec:
526     *
527     *    VUID-VkImageViewMinLodCreateInfoEXT-minLod-06456
528     *
529     *    "minLod must be less or equal to the index of the last mipmap level
530     *    accessible to the view."
531     */
532    assert(image_view->min_lod <= image_view->base_mip_level +
533                                  image_view->level_count - 1);
534 
535    image_view->extent =
536       vk_image_mip_level_extent(image, image_view->base_mip_level);
537 
538    if (vk_format_is_compressed(image->format) &&
539        !vk_format_is_compressed(image_view->format)) {
540       const struct util_format_description *fmt =
541          vk_format_description(image_view->format);
542 
543       /* Non-compressed view of compressed image only works for single MIP
544        * views.
545        */
546       assert(image_view->level_count == 1);
547       image_view->extent.width =
548          DIV_ROUND_UP(image_view->extent.width, fmt->block.width);
549       image_view->extent.height =
550          DIV_ROUND_UP(image_view->extent.height, fmt->block.height);
551       image_view->extent.depth =
552          DIV_ROUND_UP(image_view->extent.depth, fmt->block.depth);
553    }
554 
555    /* By default storage uses the same as the image properties, but it can be
556     * overriden with VkImageViewSlicedCreateInfoEXT.
557     */
558    image_view->storage.z_slice_offset = 0;
559    image_view->storage.z_slice_count = image_view->extent.depth;
560 
561    const VkImageViewSlicedCreateInfoEXT *sliced_info =
562       vk_find_struct_const(pCreateInfo, IMAGE_VIEW_SLICED_CREATE_INFO_EXT);
563    assert(image_view->base_mip_level + image_view->level_count
564           <= image->mip_levels);
565    switch (image->image_type) {
566    default:
567       unreachable("bad VkImageType");
568    case VK_IMAGE_TYPE_1D:
569    case VK_IMAGE_TYPE_2D:
570       assert(image_view->base_array_layer + image_view->layer_count
571              <= image->array_layers);
572       break;
573    case VK_IMAGE_TYPE_3D:
574       if (sliced_info && image_view->view_type == VK_IMAGE_VIEW_TYPE_3D) {
575          unsigned total = image_view->extent.depth;
576          image_view->storage.z_slice_offset = sliced_info->sliceOffset;
577          assert(image_view->storage.z_slice_offset < total);
578          if (sliced_info->sliceCount == VK_REMAINING_3D_SLICES_EXT) {
579             image_view->storage.z_slice_count = total - image_view->storage.z_slice_offset;
580          } else {
581             image_view->storage.z_slice_count = sliced_info->sliceCount;
582          }
583       } else if (image_view->view_type != VK_IMAGE_VIEW_TYPE_3D) {
584          image_view->storage.z_slice_offset = image_view->base_array_layer;
585          image_view->storage.z_slice_count = image_view->layer_count;
586       }
587       assert(image_view->storage.z_slice_offset + image_view->storage.z_slice_count
588              <= image->extent.depth);
589       assert(image_view->base_array_layer + image_view->layer_count
590              <= image_view->extent.depth);
591       break;
592    }
593 
594    /* If we are creating a color view from a depth/stencil image we compute
595     * usage from the underlying depth/stencil aspects.
596     */
597    const VkImageUsageFlags image_usage =
598       vk_image_usage(image, image_view->aspects);
599    const VkImageViewUsageCreateInfo *usage_info =
600       vk_find_struct_const(pCreateInfo, IMAGE_VIEW_USAGE_CREATE_INFO);
601    image_view->usage = usage_info ? usage_info->usage : image_usage;
602    assert(driver_internal || !(image_view->usage & ~image_usage));
603 }
604 
605 void
vk_image_view_finish(struct vk_image_view * image_view)606 vk_image_view_finish(struct vk_image_view *image_view)
607 {
608    vk_object_base_finish(&image_view->base);
609 }
610 
611 void *
vk_image_view_create(struct vk_device * device,bool driver_internal,const VkImageViewCreateInfo * pCreateInfo,const VkAllocationCallbacks * alloc,size_t size)612 vk_image_view_create(struct vk_device *device,
613                      bool driver_internal,
614                      const VkImageViewCreateInfo *pCreateInfo,
615                      const VkAllocationCallbacks *alloc,
616                      size_t size)
617 {
618    struct vk_image_view *image_view =
619       vk_zalloc2(&device->alloc, alloc, size, 8,
620                  VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
621    if (image_view == NULL)
622       return NULL;
623 
624    vk_image_view_init(device, image_view, driver_internal, pCreateInfo);
625 
626    return image_view;
627 }
628 
629 void
vk_image_view_destroy(struct vk_device * device,const VkAllocationCallbacks * alloc,struct vk_image_view * image_view)630 vk_image_view_destroy(struct vk_device *device,
631                       const VkAllocationCallbacks *alloc,
632                       struct vk_image_view *image_view)
633 {
634    vk_object_free(device, alloc, image_view);
635 }
636 
637 bool
vk_image_layout_is_read_only(VkImageLayout layout,VkImageAspectFlagBits aspect)638 vk_image_layout_is_read_only(VkImageLayout layout,
639                              VkImageAspectFlagBits aspect)
640 {
641    assert(util_bitcount(aspect) == 1);
642 
643    switch (layout) {
644    case VK_IMAGE_LAYOUT_UNDEFINED:
645    case VK_IMAGE_LAYOUT_PREINITIALIZED:
646       return true; /* These are only used for layout transitions */
647 
648    case VK_IMAGE_LAYOUT_GENERAL:
649    case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
650    case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
651    case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
652    case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
653    case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
654    case VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL:
655    case VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL:
656    case VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT:
657    case VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ_KHR:
658       return false;
659 
660    case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
661    case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
662    case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
663    case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
664    case VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR:
665    case VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT:
666    case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
667    case VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL:
668    case VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL:
669       return true;
670 
671    case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
672       return aspect == VK_IMAGE_ASPECT_DEPTH_BIT;
673 
674    case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
675       return aspect == VK_IMAGE_ASPECT_STENCIL_BIT;
676 
677    case VK_IMAGE_LAYOUT_MAX_ENUM:
678    case VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR:
679    case VK_IMAGE_LAYOUT_VIDEO_DECODE_SRC_KHR:
680    case VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR:
681    case VK_IMAGE_LAYOUT_VIDEO_ENCODE_DST_KHR:
682    case VK_IMAGE_LAYOUT_VIDEO_ENCODE_SRC_KHR:
683    case VK_IMAGE_LAYOUT_VIDEO_ENCODE_DPB_KHR:
684       unreachable("Invalid image layout.");
685    }
686 
687    unreachable("Invalid image layout.");
688 }
689 
690 bool
vk_image_layout_is_depth_only(VkImageLayout layout)691 vk_image_layout_is_depth_only(VkImageLayout layout)
692 {
693    switch (layout) {
694    case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
695    case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
696       return true;
697 
698    default:
699       return false;
700    }
701 }
702 
703 static VkResult
vk_image_create_get_format_list_uncompressed(struct vk_device * device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkFormat ** formats,uint32_t * format_count)704 vk_image_create_get_format_list_uncompressed(struct vk_device *device,
705                                              const VkImageCreateInfo *pCreateInfo,
706                                              const VkAllocationCallbacks *pAllocator,
707                                              VkFormat **formats,
708                                              uint32_t *format_count)
709 {
710    const struct vk_format_class_info *class =
711       vk_format_get_class_info(pCreateInfo->format);
712 
713    *formats = NULL;
714    *format_count = 0;
715 
716    if (class->format_count < 2)
717       return VK_SUCCESS;
718 
719    *formats = vk_alloc2(&device->alloc, pAllocator,
720                         sizeof(VkFormat) * class->format_count,
721                         alignof(VkFormat), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
722    if (*formats == NULL)
723       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
724 
725    memcpy(*formats, class->formats, sizeof(VkFormat) * class->format_count);
726    *format_count = class->format_count;
727 
728    return VK_SUCCESS;
729 }
730 
731 static VkResult
vk_image_create_get_format_list_compressed(struct vk_device * device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkFormat ** formats,uint32_t * format_count)732 vk_image_create_get_format_list_compressed(struct vk_device *device,
733                                            const VkImageCreateInfo *pCreateInfo,
734                                            const VkAllocationCallbacks *pAllocator,
735                                            VkFormat **formats,
736                                            uint32_t *format_count)
737 {
738    if ((pCreateInfo->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) == 0) {
739       return vk_image_create_get_format_list_uncompressed(device,
740                                                           pCreateInfo,
741                                                           pAllocator,
742                                                           formats,
743                                                           format_count);
744    }
745 
746    const struct vk_format_class_info *class =
747       vk_format_get_class_info(pCreateInfo->format);
748    const struct vk_format_class_info *uncompr_class = NULL;
749 
750    switch (vk_format_get_blocksizebits(pCreateInfo->format)) {
751    case 64:
752       uncompr_class = vk_format_class_get_info(MESA_VK_FORMAT_CLASS_64_BIT);
753       break;
754    case 128:
755       uncompr_class = vk_format_class_get_info(MESA_VK_FORMAT_CLASS_128_BIT);
756       break;
757    }
758 
759    if (!uncompr_class)
760       return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
761 
762    uint32_t fmt_count = class->format_count + uncompr_class->format_count;
763 
764    *formats = vk_alloc2(&device->alloc, pAllocator,
765                         sizeof(VkFormat) * fmt_count,
766                         alignof(VkFormat), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
767    if (*formats == NULL)
768       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
769 
770    memcpy(*formats, class->formats, sizeof(VkFormat) * class->format_count);
771    memcpy(*formats + class->format_count, uncompr_class->formats,
772           sizeof(VkFormat) * uncompr_class->format_count);
773    *format_count = class->format_count + uncompr_class->format_count;
774 
775    return VK_SUCCESS;
776 }
777 
778 /* Get a list of compatible formats when VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
779  * or VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT is set. This list is
780  * either retrieved from a VkImageFormatListCreateInfo passed to the creation
781  * chain, or forged from the default compatible list specified in the
782  * "formats-compatibility-classes" section of the spec.
783  *
784  * The value returned in *formats must be freed with
785  * vk_free2(&device->alloc, pAllocator), and should not live past the
786  * vkCreateImage() call (allocated in the COMMAND scope).
787  */
788 VkResult
vk_image_create_get_format_list(struct vk_device * device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkFormat ** formats,uint32_t * format_count)789 vk_image_create_get_format_list(struct vk_device *device,
790                                 const VkImageCreateInfo *pCreateInfo,
791                                 const VkAllocationCallbacks *pAllocator,
792                                 VkFormat **formats,
793                                 uint32_t *format_count)
794 {
795    *formats = NULL;
796    *format_count = 0;
797 
798    if (!(pCreateInfo->flags &
799          (VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT |
800           VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT))) {
801       return VK_SUCCESS;
802    }
803 
804    /* "Each depth/stencil format is only compatible with itself." */
805    if (vk_format_is_depth_or_stencil(pCreateInfo->format))
806       return VK_SUCCESS;
807 
808    const VkImageFormatListCreateInfo *format_list = (const VkImageFormatListCreateInfo *)
809       vk_find_struct_const(pCreateInfo->pNext, IMAGE_FORMAT_LIST_CREATE_INFO);
810 
811    if (format_list) {
812       if (!format_list->viewFormatCount)
813          return VK_SUCCESS;
814 
815       *formats = vk_alloc2(&device->alloc, pAllocator,
816                            sizeof(VkFormat) * format_list->viewFormatCount,
817                            alignof(VkFormat), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
818       if (*formats == NULL)
819          return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
820 
821       memcpy(*formats, format_list->pViewFormats, sizeof(VkFormat) * format_list->viewFormatCount);
822       *format_count = format_list->viewFormatCount;
823       return VK_SUCCESS;
824    }
825 
826    if (vk_format_is_compressed(pCreateInfo->format))
827       return vk_image_create_get_format_list_compressed(device,
828                                                         pCreateInfo,
829                                                         pAllocator,
830                                                         formats,
831                                                         format_count);
832 
833    return vk_image_create_get_format_list_uncompressed(device,
834                                                        pCreateInfo,
835                                                        pAllocator,
836                                                        formats,
837                                                        format_count);
838 }
839 
840 /* From the Vulkan Specification 1.2.166 - VkAttachmentReference2:
841  *
842  *   "If layout only specifies the layout of the depth aspect of the
843  *    attachment, the layout of the stencil aspect is specified by the
844  *    stencilLayout member of a VkAttachmentReferenceStencilLayout structure
845  *    included in the pNext chain. Otherwise, layout describes the layout for
846  *    all relevant image aspects."
847  */
848 VkImageLayout
vk_att_ref_stencil_layout(const VkAttachmentReference2 * att_ref,const VkAttachmentDescription2 * attachments)849 vk_att_ref_stencil_layout(const VkAttachmentReference2 *att_ref,
850                           const VkAttachmentDescription2 *attachments)
851 {
852    /* From VUID-VkAttachmentReference2-attachment-04755:
853     *  "If attachment is not VK_ATTACHMENT_UNUSED, and the format of the
854     *   referenced attachment is a depth/stencil format which includes both
855     *   depth and stencil aspects [...]
856     */
857    if (att_ref->attachment == VK_ATTACHMENT_UNUSED ||
858        !vk_format_has_stencil(attachments[att_ref->attachment].format))
859       return VK_IMAGE_LAYOUT_UNDEFINED;
860 
861    const VkAttachmentReferenceStencilLayout *stencil_ref =
862       vk_find_struct_const(att_ref->pNext, ATTACHMENT_REFERENCE_STENCIL_LAYOUT);
863 
864    if (stencil_ref)
865       return stencil_ref->stencilLayout;
866 
867    /* From VUID-VkAttachmentReference2-attachment-04755:
868     *  "If attachment is not VK_ATTACHMENT_UNUSED, and the format of the
869     *   referenced attachment is a depth/stencil format which includes both
870     *   depth and stencil aspects, and layout is
871     *   VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL or
872     *   VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL, the pNext chain must include
873     *   a VkAttachmentReferenceStencilLayout structure."
874     */
875    assert(!vk_image_layout_is_depth_only(att_ref->layout));
876 
877    return att_ref->layout;
878 }
879 
880 /* From the Vulkan Specification 1.2.184:
881  *
882  *   "If the pNext chain includes a VkAttachmentDescriptionStencilLayout
883  *    structure, then the stencilInitialLayout and stencilFinalLayout members
884  *    specify the initial and final layouts of the stencil aspect of a
885  *    depth/stencil format, and initialLayout and finalLayout only apply to the
886  *    depth aspect. For depth-only formats, the
887  *    VkAttachmentDescriptionStencilLayout structure is ignored. For
888  *    stencil-only formats, the initial and final layouts of the stencil aspect
889  *    are taken from the VkAttachmentDescriptionStencilLayout structure if
890  *    present, or initialLayout and finalLayout if not present."
891  *
892  *   "If format is a depth/stencil format, and either initialLayout or
893  *    finalLayout does not specify a layout for the stencil aspect, then the
894  *    application must specify the initial and final layouts of the stencil
895  *    aspect by including a VkAttachmentDescriptionStencilLayout structure in
896  *    the pNext chain."
897  */
898 VkImageLayout
vk_att_desc_stencil_layout(const VkAttachmentDescription2 * att_desc,bool final)899 vk_att_desc_stencil_layout(const VkAttachmentDescription2 *att_desc, bool final)
900 {
901    if (!vk_format_has_stencil(att_desc->format))
902       return VK_IMAGE_LAYOUT_UNDEFINED;
903 
904    const VkAttachmentDescriptionStencilLayout *stencil_desc =
905       vk_find_struct_const(att_desc->pNext, ATTACHMENT_DESCRIPTION_STENCIL_LAYOUT);
906 
907    if (stencil_desc) {
908       return final ?
909          stencil_desc->stencilFinalLayout :
910          stencil_desc->stencilInitialLayout;
911    }
912 
913    const VkImageLayout main_layout =
914       final ? att_desc->finalLayout : att_desc->initialLayout;
915 
916    /* From VUID-VkAttachmentDescription2-format-03302/03303:
917     *  "If format is a depth/stencil format which includes both depth and
918     *   stencil aspects, and initial/finalLayout is
919     *   VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL or
920     *   VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL, the pNext chain must include
921     *   a VkAttachmentDescriptionStencilLayout structure."
922     */
923    assert(!vk_image_layout_is_depth_only(main_layout));
924 
925    return main_layout;
926 }
927 
928 VkImageUsageFlags
vk_image_layout_to_usage_flags(VkImageLayout layout,VkImageAspectFlagBits aspect)929 vk_image_layout_to_usage_flags(VkImageLayout layout,
930                                VkImageAspectFlagBits aspect)
931 {
932    assert(util_bitcount(aspect) == 1);
933 
934    switch (layout) {
935    case VK_IMAGE_LAYOUT_UNDEFINED:
936    case VK_IMAGE_LAYOUT_PREINITIALIZED:
937       return 0u;
938 
939    case VK_IMAGE_LAYOUT_GENERAL:
940       return ~0u;
941 
942    case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
943       assert(aspect & VK_IMAGE_ASPECT_ANY_COLOR_MASK_MESA);
944       return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
945 
946    case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
947       assert(aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
948                        VK_IMAGE_ASPECT_STENCIL_BIT));
949       return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
950 
951    case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
952       assert(aspect & VK_IMAGE_ASPECT_DEPTH_BIT);
953       return vk_image_layout_to_usage_flags(
954          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
955 
956    case VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL:
957       assert(aspect & VK_IMAGE_ASPECT_STENCIL_BIT);
958       return vk_image_layout_to_usage_flags(
959          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
960 
961    case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
962       assert(aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
963                        VK_IMAGE_ASPECT_STENCIL_BIT));
964       return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT |
965              VK_IMAGE_USAGE_SAMPLED_BIT |
966              VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
967 
968    case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
969       assert(aspect & VK_IMAGE_ASPECT_DEPTH_BIT);
970       return vk_image_layout_to_usage_flags(
971          VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
972 
973    case VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL:
974       assert(aspect & VK_IMAGE_ASPECT_STENCIL_BIT);
975       return vk_image_layout_to_usage_flags(
976          VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
977 
978    case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
979       return VK_IMAGE_USAGE_SAMPLED_BIT |
980              VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
981 
982    case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
983       return VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
984 
985    case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
986       return VK_IMAGE_USAGE_TRANSFER_DST_BIT;
987 
988    case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
989       if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
990          return vk_image_layout_to_usage_flags(
991             VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
992       } else if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
993          return vk_image_layout_to_usage_flags(
994             VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
995       } else {
996          assert(!"Must be a depth/stencil aspect");
997          return 0;
998       }
999 
1000    case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
1001       if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
1002          return vk_image_layout_to_usage_flags(
1003             VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
1004       } else if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
1005          return vk_image_layout_to_usage_flags(
1006             VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
1007       } else {
1008          assert(!"Must be a depth/stencil aspect");
1009          return 0;
1010       }
1011 
1012    case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
1013       assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
1014       /* This needs to be handled specially by the caller */
1015       return 0;
1016 
1017    case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
1018       assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
1019       return vk_image_layout_to_usage_flags(VK_IMAGE_LAYOUT_GENERAL, aspect);
1020 
1021    case VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR:
1022       assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
1023       return VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR;
1024 
1025    case VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT:
1026       assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
1027       return VK_IMAGE_USAGE_FRAGMENT_DENSITY_MAP_BIT_EXT;
1028 
1029    case VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL:
1030       if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT ||
1031           aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
1032          return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
1033       } else {
1034          assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
1035          return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1036       }
1037 
1038    case VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL:
1039       return VK_IMAGE_USAGE_SAMPLED_BIT |
1040              VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
1041 
1042    case VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT:
1043    case VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ_KHR:
1044       if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT ||
1045           aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
1046          return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT |
1047                 VK_IMAGE_USAGE_SAMPLED_BIT |
1048                 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
1049                 VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT;
1050       } else {
1051          assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
1052          return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1053                 VK_IMAGE_USAGE_SAMPLED_BIT |
1054                 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
1055                 VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT;
1056       }
1057 
1058    case VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR:
1059       return VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR;
1060    case VK_IMAGE_LAYOUT_VIDEO_DECODE_SRC_KHR:
1061       return VK_IMAGE_USAGE_VIDEO_DECODE_SRC_BIT_KHR;
1062    case VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR:
1063       return VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
1064    case VK_IMAGE_LAYOUT_VIDEO_ENCODE_DST_KHR:
1065       return VK_IMAGE_USAGE_VIDEO_ENCODE_DST_BIT_KHR;
1066    case VK_IMAGE_LAYOUT_VIDEO_ENCODE_SRC_KHR:
1067       return VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
1068    case VK_IMAGE_LAYOUT_VIDEO_ENCODE_DPB_KHR:
1069       return VK_IMAGE_USAGE_VIDEO_ENCODE_DPB_BIT_KHR;
1070    case VK_IMAGE_LAYOUT_MAX_ENUM:
1071       unreachable("Invalid image layout.");
1072    }
1073 
1074    unreachable("Invalid image layout.");
1075 }
1076