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