vk_image.c revision 7ec681f3
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 <vulkan/vulkan_android.h>
27
28#ifndef _WIN32
29#include <drm-uapi/drm_fourcc.h>
30#endif
31
32#include "vk_alloc.h"
33#include "vk_common_entrypoints.h"
34#include "vk_device.h"
35#include "vk_format.h"
36#include "vk_util.h"
37#include "vulkan/wsi/wsi_common.h"
38
39static VkExtent3D
40sanitize_image_extent(const VkImageType imageType,
41                      const VkExtent3D imageExtent)
42{
43   switch (imageType) {
44   case VK_IMAGE_TYPE_1D:
45      return (VkExtent3D) { imageExtent.width, 1, 1 };
46   case VK_IMAGE_TYPE_2D:
47      return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
48   case VK_IMAGE_TYPE_3D:
49      return imageExtent;
50   default:
51      unreachable("invalid image type");
52   }
53}
54
55void
56vk_image_init(struct vk_device *device,
57              struct vk_image *image,
58              const VkImageCreateInfo *pCreateInfo)
59{
60   vk_object_base_init(device, &image->base, VK_OBJECT_TYPE_IMAGE);
61
62   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
63   assert(pCreateInfo->mipLevels > 0);
64   assert(pCreateInfo->arrayLayers > 0);
65   assert(pCreateInfo->samples > 0);
66   assert(pCreateInfo->extent.width > 0);
67   assert(pCreateInfo->extent.height > 0);
68   assert(pCreateInfo->extent.depth > 0);
69
70   if (pCreateInfo->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
71      assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
72   if (pCreateInfo->flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT)
73      assert(pCreateInfo->imageType == VK_IMAGE_TYPE_3D);
74
75   image->create_flags = pCreateInfo->flags;
76   image->image_type = pCreateInfo->imageType;
77   vk_image_set_format(image, pCreateInfo->format);
78   image->extent = sanitize_image_extent(pCreateInfo->imageType,
79                                         pCreateInfo->extent);
80   image->mip_levels = pCreateInfo->mipLevels;
81   image->array_layers = pCreateInfo->arrayLayers;
82   image->samples = pCreateInfo->samples;
83   image->tiling = pCreateInfo->tiling;
84   image->usage = pCreateInfo->usage;
85
86   if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
87      const VkImageStencilUsageCreateInfoEXT *stencil_usage_info =
88         vk_find_struct_const(pCreateInfo->pNext,
89                              IMAGE_STENCIL_USAGE_CREATE_INFO_EXT);
90      image->stencil_usage =
91         stencil_usage_info ? stencil_usage_info->stencilUsage :
92                              pCreateInfo->usage;
93   } else {
94      image->stencil_usage = 0;
95   }
96
97   const VkExternalMemoryImageCreateInfo *ext_mem_info =
98      vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_MEMORY_IMAGE_CREATE_INFO);
99   if (ext_mem_info)
100      image->external_handle_types = ext_mem_info->handleTypes;
101   else
102      image->external_handle_types = 0;
103
104   const struct wsi_image_create_info *wsi_info =
105      vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
106   image->wsi_legacy_scanout = wsi_info && wsi_info->scanout;
107
108#ifndef _WIN32
109   image->drm_format_mod = ((1ULL << 56) - 1) /* DRM_FORMAT_MOD_INVALID */;
110#endif
111
112#ifdef ANDROID
113   const VkExternalFormatANDROID *ext_format =
114      vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_FORMAT_ANDROID);
115   if (ext_format && ext_format->externalFormat != 0) {
116      assert(image->format == VK_FORMAT_UNDEFINED);
117      assert(image->external_handle_types &
118             VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID);
119      image->android_external_format = ext_format->externalFormat;
120   } else {
121      image->android_external_format = 0;
122   }
123#endif
124}
125
126void *
127vk_image_create(struct vk_device *device,
128                const VkImageCreateInfo *pCreateInfo,
129                const VkAllocationCallbacks *alloc,
130                size_t size)
131{
132   struct vk_image *image =
133      vk_zalloc2(&device->alloc, alloc, size, 8,
134                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
135   if (image == NULL)
136      return NULL;
137
138   vk_image_init(device, image, pCreateInfo);
139
140   return image;
141}
142
143void
144vk_image_finish(struct vk_image *image)
145{
146   vk_object_base_finish(&image->base);
147}
148
149void
150vk_image_destroy(struct vk_device *device,
151                 const VkAllocationCallbacks *alloc,
152                 struct vk_image *image)
153{
154   vk_object_free(device, alloc, image);
155}
156
157#ifndef _WIN32
158VKAPI_ATTR VkResult VKAPI_CALL
159vk_common_GetImageDrmFormatModifierPropertiesEXT(UNUSED VkDevice device,
160                                                 VkImage _image,
161                                                 VkImageDrmFormatModifierPropertiesEXT *pProperties)
162{
163   VK_FROM_HANDLE(vk_image, image, _image);
164
165   assert(pProperties->sType ==
166          VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT);
167
168   assert(image->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT);
169   pProperties->drmFormatModifier = image->drm_format_mod;
170
171   return VK_SUCCESS;
172}
173#endif
174
175void
176vk_image_set_format(struct vk_image *image, VkFormat format)
177{
178   image->format = format;
179   image->aspects = vk_format_aspects(format);
180}
181
182VkImageUsageFlags
183vk_image_usage(const struct vk_image *image,
184               VkImageAspectFlags aspect_mask)
185{
186   assert(!(aspect_mask & ~image->aspects));
187
188   /* From the Vulkan 1.2.131 spec:
189    *
190    *    "If the image was has a depth-stencil format and was created with
191    *    a VkImageStencilUsageCreateInfo structure included in the pNext
192    *    chain of VkImageCreateInfo, the usage is calculated based on the
193    *    subresource.aspectMask provided:
194    *
195    *     - If aspectMask includes only VK_IMAGE_ASPECT_STENCIL_BIT, the
196    *       implicit usage is equal to
197    *       VkImageStencilUsageCreateInfo::stencilUsage.
198    *
199    *     - If aspectMask includes only VK_IMAGE_ASPECT_DEPTH_BIT, the
200    *       implicit usage is equal to VkImageCreateInfo::usage.
201    *
202    *     - If both aspects are included in aspectMask, the implicit usage
203    *       is equal to the intersection of VkImageCreateInfo::usage and
204    *       VkImageStencilUsageCreateInfo::stencilUsage.
205    */
206   if (aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT) {
207      return image->stencil_usage;
208   } else if (aspect_mask == (VK_IMAGE_ASPECT_DEPTH_BIT |
209                              VK_IMAGE_ASPECT_STENCIL_BIT)) {
210      return image->usage & image->stencil_usage;
211   } else {
212      /* This also handles the color case */
213      return image->usage;
214   }
215}
216
217#define VK_IMAGE_ASPECT_ANY_COLOR_MASK_MESA ( \
218   VK_IMAGE_ASPECT_COLOR_BIT | \
219   VK_IMAGE_ASPECT_PLANE_0_BIT | \
220   VK_IMAGE_ASPECT_PLANE_1_BIT | \
221   VK_IMAGE_ASPECT_PLANE_2_BIT)
222
223/** Expands the given aspect mask relative to the image
224 *
225 * If the image has color plane aspects VK_IMAGE_ASPECT_COLOR_BIT has been
226 * requested, this returns the aspects of the underlying image.
227 *
228 * For example,
229 *
230 *    VK_IMAGE_ASPECT_COLOR_BIT
231 *
232 * will be converted to
233 *
234 *    VK_IMAGE_ASPECT_PLANE_0_BIT |
235 *    VK_IMAGE_ASPECT_PLANE_1_BIT |
236 *    VK_IMAGE_ASPECT_PLANE_2_BIT
237 *
238 * for an image of format VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM.
239 */
240VkImageAspectFlags
241vk_image_expand_aspect_mask(const struct vk_image *image,
242                            VkImageAspectFlags aspect_mask)
243{
244   if (aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT) {
245      assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_MASK_MESA);
246      return image->aspects;
247   } else {
248      assert(aspect_mask && !(aspect_mask & ~image->aspects));
249      return aspect_mask;
250   }
251}
252
253static VkComponentSwizzle
254remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component)
255{
256   return swizzle == VK_COMPONENT_SWIZZLE_IDENTITY ? component : swizzle;
257}
258
259void
260vk_image_view_init(struct vk_device *device,
261                   struct vk_image_view *image_view,
262                   const VkImageViewCreateInfo *pCreateInfo)
263{
264   vk_object_base_init(device, &image_view->base, VK_OBJECT_TYPE_IMAGE_VIEW);
265
266   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
267   VK_FROM_HANDLE(vk_image, image, pCreateInfo->image);
268
269   image_view->create_flags = pCreateInfo->flags;
270   image_view->image = image;
271   image_view->view_type = pCreateInfo->viewType;
272
273   switch (image_view->view_type) {
274   case VK_IMAGE_VIEW_TYPE_1D:
275   case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
276      assert(image->image_type == VK_IMAGE_TYPE_1D);
277      break;
278   case VK_IMAGE_VIEW_TYPE_2D:
279   case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
280      if (image->create_flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT)
281         assert(image->image_type == VK_IMAGE_TYPE_3D);
282      else
283         assert(image->image_type == VK_IMAGE_TYPE_2D);
284      break;
285   case VK_IMAGE_VIEW_TYPE_3D:
286      assert(image->image_type == VK_IMAGE_TYPE_3D);
287      break;
288   case VK_IMAGE_VIEW_TYPE_CUBE:
289   case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
290      assert(image->image_type == VK_IMAGE_TYPE_2D);
291      assert(image->create_flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT);
292      break;
293   default:
294      unreachable("Invalid image view type");
295   }
296
297   const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
298
299   /* Some drivers may want to create color views of depth/stencil images
300    * to implement certain operations, which is not strictly allowed by the
301    * Vulkan spec, so handle this case separately.
302    */
303   bool is_color_view_of_depth_stencil =
304      vk_format_is_depth_or_stencil(image->format) &&
305      vk_format_is_color(pCreateInfo->format);
306   if (is_color_view_of_depth_stencil) {
307      assert(range->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
308      assert(util_format_get_blocksize(vk_format_to_pipe_format(image->format)) ==
309             util_format_get_blocksize(vk_format_to_pipe_format(pCreateInfo->format)));
310      image_view->aspects = range->aspectMask;
311   } else {
312      image_view->aspects =
313         vk_image_expand_aspect_mask(image, range->aspectMask);
314
315      /* From the Vulkan 1.2.184 spec:
316       *
317       *    "If the image has a multi-planar format and
318       *    subresourceRange.aspectMask is VK_IMAGE_ASPECT_COLOR_BIT, and image
319       *    has been created with a usage value not containing any of the
320       *    VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR,
321       *    VK_IMAGE_USAGE_VIDEO_DECODE_SRC_BIT_KHR,
322       *    VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR,
323       *    VK_IMAGE_USAGE_VIDEO_ENCODE_DST_BIT_KHR,
324       *    VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR, and
325       *    VK_IMAGE_USAGE_VIDEO_ENCODE_DPB_BIT_KHR flags, then the format must
326       *    be identical to the image format, and the sampler to be used with the
327       *    image view must enable sampler Y′CBCR conversion."
328       *
329       * Since no one implements video yet, we can ignore the bits about video
330       * create flags and assume YCbCr formats match.
331       */
332      if ((image->aspects & VK_IMAGE_ASPECT_PLANE_1_BIT) &&
333          (range->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT))
334         assert(pCreateInfo->format == image->format);
335
336      /* From the Vulkan 1.2.184 spec:
337       *
338       *    "Each depth/stencil format is only compatible with itself."
339       */
340      if (image_view->aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
341                                 VK_IMAGE_ASPECT_STENCIL_BIT))
342         assert(pCreateInfo->format == image->format);
343
344      if (!(image->create_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))
345         assert(pCreateInfo->format == image->format);
346   }
347
348   /* Restrict the format to only the planes chosen.
349    *
350    * For combined depth and stencil images, this means the depth-only or
351    * stencil-only format if only one aspect is chosen and the full combined
352    * format if both aspects are chosen.
353    *
354    * For single-plane color images, we just take the format as-is.  For
355    * multi-plane views of multi-plane images, this means we want the full
356    * multi-plane format.  For single-plane views of multi-plane images, we
357    * want a format compatible with the one plane.  Fortunately, this is
358    * already what the client gives us.  The Vulkan 1.2.184 spec says:
359    *
360    *    "If image was created with the VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT and
361    *    the image has a multi-planar format, and if
362    *    subresourceRange.aspectMask is VK_IMAGE_ASPECT_PLANE_0_BIT,
363    *    VK_IMAGE_ASPECT_PLANE_1_BIT, or VK_IMAGE_ASPECT_PLANE_2_BIT, format
364    *    must be compatible with the corresponding plane of the image, and the
365    *    sampler to be used with the image view must not enable sampler Y′CBCR
366    *    conversion."
367    */
368   if (image_view->aspects == VK_IMAGE_ASPECT_STENCIL_BIT) {
369      image_view->format = vk_format_stencil_only(pCreateInfo->format);
370   } else if (image_view->aspects == VK_IMAGE_ASPECT_DEPTH_BIT) {
371      image_view->format = vk_format_depth_only(pCreateInfo->format);
372   } else {
373      image_view->format = pCreateInfo->format;
374   }
375
376   image_view->swizzle = (VkComponentMapping) {
377      .r = remap_swizzle(pCreateInfo->components.r, VK_COMPONENT_SWIZZLE_R),
378      .g = remap_swizzle(pCreateInfo->components.g, VK_COMPONENT_SWIZZLE_G),
379      .b = remap_swizzle(pCreateInfo->components.b, VK_COMPONENT_SWIZZLE_B),
380      .a = remap_swizzle(pCreateInfo->components.a, VK_COMPONENT_SWIZZLE_A),
381   };
382
383   assert(range->layerCount > 0);
384   assert(range->baseMipLevel < image->mip_levels);
385
386   image_view->base_mip_level = range->baseMipLevel;
387   image_view->level_count = vk_image_subresource_level_count(image, range);
388   image_view->base_array_layer = range->baseArrayLayer;
389   image_view->layer_count = vk_image_subresource_layer_count(image, range);
390
391   image_view->extent =
392      vk_image_mip_level_extent(image, image_view->base_mip_level);
393
394   assert(image_view->base_mip_level + image_view->level_count
395          <= image->mip_levels);
396   switch (image->image_type) {
397   default:
398      unreachable("bad VkImageType");
399   case VK_IMAGE_TYPE_1D:
400   case VK_IMAGE_TYPE_2D:
401      assert(image_view->base_array_layer + image_view->layer_count
402             <= image->array_layers);
403      break;
404   case VK_IMAGE_TYPE_3D:
405      assert(image_view->base_array_layer + image_view->layer_count
406             <= image_view->extent.depth);
407      break;
408   }
409
410   /* If we are creating a color view from a depth/stencil image we compute
411    * usage from the underlying depth/stencil aspects.
412    */
413   const VkImageUsageFlags image_usage = is_color_view_of_depth_stencil ?
414      vk_image_usage(image, image->aspects) :
415      vk_image_usage(image, image_view->aspects);
416   const VkImageViewUsageCreateInfo *usage_info =
417      vk_find_struct_const(pCreateInfo, IMAGE_VIEW_USAGE_CREATE_INFO);
418   image_view->usage = usage_info ? usage_info->usage : image_usage;
419   assert(!(image_view->usage & ~image_usage));
420}
421
422void
423vk_image_view_finish(struct vk_image_view *image_view)
424{
425   vk_object_base_finish(&image_view->base);
426}
427
428void *
429vk_image_view_create(struct vk_device *device,
430                     const VkImageViewCreateInfo *pCreateInfo,
431                     const VkAllocationCallbacks *alloc,
432                     size_t size)
433{
434   struct vk_image_view *image_view =
435      vk_zalloc2(&device->alloc, alloc, size, 8,
436                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
437   if (image_view == NULL)
438      return NULL;
439
440   vk_image_view_init(device, image_view, pCreateInfo);
441
442   return image_view;
443}
444
445void
446vk_image_view_destroy(struct vk_device *device,
447                      const VkAllocationCallbacks *alloc,
448                      struct vk_image_view *image_view)
449{
450   vk_object_free(device, alloc, image_view);
451}
452
453bool
454vk_image_layout_is_read_only(VkImageLayout layout,
455                             VkImageAspectFlagBits aspect)
456{
457   assert(util_bitcount(aspect) == 1);
458
459   switch (layout) {
460   case VK_IMAGE_LAYOUT_UNDEFINED:
461   case VK_IMAGE_LAYOUT_PREINITIALIZED:
462      return true; /* These are only used for layout transitions */
463
464   case VK_IMAGE_LAYOUT_GENERAL:
465   case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
466   case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
467   case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
468   case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
469   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
470   case VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL:
471   case VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR:
472      return false;
473
474   case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
475   case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
476   case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
477   case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
478   case VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV:
479   case VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT:
480   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
481   case VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL:
482   case VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL_KHR:
483      return true;
484
485   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
486      return aspect == VK_IMAGE_ASPECT_DEPTH_BIT;
487
488   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
489      return aspect == VK_IMAGE_ASPECT_STENCIL_BIT;
490
491   case VK_IMAGE_LAYOUT_MAX_ENUM:
492      unreachable("Invalid image layout.");
493   }
494
495   unreachable("Invalid image layout.");
496}
497
498VkImageUsageFlags
499vk_image_layout_to_usage_flags(VkImageLayout layout,
500                               VkImageAspectFlagBits aspect)
501{
502   assert(util_bitcount(aspect) == 1);
503
504   switch (layout) {
505   case VK_IMAGE_LAYOUT_UNDEFINED:
506   case VK_IMAGE_LAYOUT_PREINITIALIZED:
507      return 0u;
508
509   case VK_IMAGE_LAYOUT_GENERAL:
510      return ~0u;
511
512   case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
513      assert(aspect & VK_IMAGE_ASPECT_ANY_COLOR_MASK_MESA);
514      return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
515
516   case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
517      assert(aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
518                       VK_IMAGE_ASPECT_STENCIL_BIT));
519      return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
520
521   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
522      assert(aspect & VK_IMAGE_ASPECT_DEPTH_BIT);
523      return vk_image_layout_to_usage_flags(
524         VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
525
526   case VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL:
527      assert(aspect & VK_IMAGE_ASPECT_STENCIL_BIT);
528      return vk_image_layout_to_usage_flags(
529         VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
530
531   case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
532      assert(aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
533                       VK_IMAGE_ASPECT_STENCIL_BIT));
534      return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT |
535             VK_IMAGE_USAGE_SAMPLED_BIT |
536             VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
537
538   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
539      assert(aspect & VK_IMAGE_ASPECT_DEPTH_BIT);
540      return vk_image_layout_to_usage_flags(
541         VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
542
543   case VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL:
544      assert(aspect & VK_IMAGE_ASPECT_STENCIL_BIT);
545      return vk_image_layout_to_usage_flags(
546         VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
547
548   case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
549      return VK_IMAGE_USAGE_SAMPLED_BIT |
550             VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
551
552   case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
553      return VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
554
555   case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
556      return VK_IMAGE_USAGE_TRANSFER_DST_BIT;
557
558   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
559      if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
560         return vk_image_layout_to_usage_flags(
561            VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
562      } else if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
563         return vk_image_layout_to_usage_flags(
564            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
565      } else {
566         assert(!"Must be a depth/stencil aspect");
567         return 0;
568      }
569
570   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
571      if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
572         return vk_image_layout_to_usage_flags(
573            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
574      } else if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
575         return vk_image_layout_to_usage_flags(
576            VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
577      } else {
578         assert(!"Must be a depth/stencil aspect");
579         return 0;
580      }
581
582   case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
583      assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
584      /* This needs to be handled specially by the caller */
585      return 0;
586
587   case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
588      assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
589      return vk_image_layout_to_usage_flags(VK_IMAGE_LAYOUT_GENERAL, aspect);
590
591   case VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV:
592      assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
593      return VK_IMAGE_USAGE_SHADING_RATE_IMAGE_BIT_NV;
594
595   case VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT:
596      assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
597      return VK_IMAGE_USAGE_FRAGMENT_DENSITY_MAP_BIT_EXT;
598
599   case VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR:
600      if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT ||
601          aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
602         return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
603      } else {
604         assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
605         return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
606      }
607
608   case VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL_KHR:
609      return VK_IMAGE_USAGE_SAMPLED_BIT |
610             VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
611
612   case VK_IMAGE_LAYOUT_MAX_ENUM:
613      unreachable("Invalid image layout.");
614   }
615
616   unreachable("Invalid image layout.");
617}
618