~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/vulkan/runtime/vk_image.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
39
 
static VkExtent3D
40
 
sanitize_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
 
 
55
 
void
56
 
vk_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
 
 
126
 
void *
127
 
vk_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
 
 
143
 
void
144
 
vk_image_finish(struct vk_image *image)
145
 
{
146
 
   vk_object_base_finish(&image->base);
147
 
}
148
 
 
149
 
void
150
 
vk_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
158
 
VKAPI_ATTR VkResult VKAPI_CALL
159
 
vk_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
 
 
175
 
void
176
 
vk_image_set_format(struct vk_image *image, VkFormat format)
177
 
{
178
 
   image->format = format;
179
 
   image->aspects = vk_format_aspects(format);
180
 
}
181
 
 
182
 
VkImageUsageFlags
183
 
vk_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
 
 */
240
 
VkImageAspectFlags
241
 
vk_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
 
 
253
 
static VkComponentSwizzle
254
 
remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component)
255
 
{
256
 
   return swizzle == VK_COMPONENT_SWIZZLE_IDENTITY ? component : swizzle;
257
 
}
258
 
 
259
 
void
260
 
vk_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
 
   image_view->format = pCreateInfo->format;
273
 
 
274
 
   switch (image_view->view_type) {
275
 
   case VK_IMAGE_VIEW_TYPE_1D:
276
 
   case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
277
 
      assert(image->image_type == VK_IMAGE_TYPE_1D);
278
 
      break;
279
 
   case VK_IMAGE_VIEW_TYPE_2D:
280
 
   case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
281
 
      if (image->create_flags & (VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT | VK_IMAGE_CREATE_2D_VIEW_COMPATIBLE_BIT_EXT))
282
 
         assert(image->image_type == VK_IMAGE_TYPE_3D);
283
 
      else
284
 
         assert(image->image_type == VK_IMAGE_TYPE_2D);
285
 
      break;
286
 
   case VK_IMAGE_VIEW_TYPE_3D:
287
 
      assert(image->image_type == VK_IMAGE_TYPE_3D);
288
 
      break;
289
 
   case VK_IMAGE_VIEW_TYPE_CUBE:
290
 
   case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
291
 
      assert(image->image_type == VK_IMAGE_TYPE_2D);
292
 
      assert(image->create_flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT);
293
 
      break;
294
 
   default:
295
 
      unreachable("Invalid image view type");
296
 
   }
297
 
 
298
 
   const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
299
 
 
300
 
   /* Some drivers may want to create color views of depth/stencil images
301
 
    * to implement certain operations, which is not strictly allowed by the
302
 
    * Vulkan spec, so handle this case separately.
303
 
    */
304
 
   bool is_color_view_of_depth_stencil =
305
 
      vk_format_is_depth_or_stencil(image->format) &&
306
 
      vk_format_is_color(pCreateInfo->format);
307
 
   if (is_color_view_of_depth_stencil) {
308
 
      assert(range->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
309
 
      assert(util_format_get_blocksize(vk_format_to_pipe_format(image->format)) ==
310
 
             util_format_get_blocksize(vk_format_to_pipe_format(pCreateInfo->format)));
311
 
      image_view->aspects = range->aspectMask;
312
 
   } else {
313
 
      image_view->aspects =
314
 
         vk_image_expand_aspect_mask(image, range->aspectMask);
315
 
 
316
 
      /* From the Vulkan 1.2.184 spec:
317
 
       *
318
 
       *    "If the image has a multi-planar format and
319
 
       *    subresourceRange.aspectMask is VK_IMAGE_ASPECT_COLOR_BIT, and image
320
 
       *    has been created with a usage value not containing any of the
321
 
       *    VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR,
322
 
       *    VK_IMAGE_USAGE_VIDEO_DECODE_SRC_BIT_KHR,
323
 
       *    VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR,
324
 
       *    VK_IMAGE_USAGE_VIDEO_ENCODE_DST_BIT_KHR,
325
 
       *    VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR, and
326
 
       *    VK_IMAGE_USAGE_VIDEO_ENCODE_DPB_BIT_KHR flags, then the format must
327
 
       *    be identical to the image format, and the sampler to be used with the
328
 
       *    image view must enable sampler Y′CBCR conversion."
329
 
       *
330
 
       * Since no one implements video yet, we can ignore the bits about video
331
 
       * create flags and assume YCbCr formats match.
332
 
       */
333
 
      if ((image->aspects & VK_IMAGE_ASPECT_PLANE_1_BIT) &&
334
 
          (range->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT))
335
 
         assert(pCreateInfo->format == image->format);
336
 
 
337
 
      /* From the Vulkan 1.2.184 spec:
338
 
       *
339
 
       *    "Each depth/stencil format is only compatible with itself."
340
 
       */
341
 
      if (image_view->aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
342
 
                                 VK_IMAGE_ASPECT_STENCIL_BIT))
343
 
         assert(pCreateInfo->format == image->format);
344
 
 
345
 
      if (!(image->create_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))
346
 
         assert(pCreateInfo->format == image->format);
347
 
   }
348
 
 
349
 
   /* Restrict the format to only the planes chosen.
350
 
    *
351
 
    * For combined depth and stencil images, this means the depth-only or
352
 
    * stencil-only format if only one aspect is chosen and the full combined
353
 
    * format if both aspects are chosen.
354
 
    *
355
 
    * For single-plane color images, we just take the format as-is.  For
356
 
    * multi-plane views of multi-plane images, this means we want the full
357
 
    * multi-plane format.  For single-plane views of multi-plane images, we
358
 
    * want a format compatible with the one plane.  Fortunately, this is
359
 
    * already what the client gives us.  The Vulkan 1.2.184 spec says:
360
 
    *
361
 
    *    "If image was created with the VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT and
362
 
    *    the image has a multi-planar format, and if
363
 
    *    subresourceRange.aspectMask is VK_IMAGE_ASPECT_PLANE_0_BIT,
364
 
    *    VK_IMAGE_ASPECT_PLANE_1_BIT, or VK_IMAGE_ASPECT_PLANE_2_BIT, format
365
 
    *    must be compatible with the corresponding plane of the image, and the
366
 
    *    sampler to be used with the image view must not enable sampler Y′CBCR
367
 
    *    conversion."
368
 
    */
369
 
   if (image_view->aspects == VK_IMAGE_ASPECT_STENCIL_BIT) {
370
 
      image_view->view_format = vk_format_stencil_only(pCreateInfo->format);
371
 
   } else if (image_view->aspects == VK_IMAGE_ASPECT_DEPTH_BIT) {
372
 
      image_view->view_format = vk_format_depth_only(pCreateInfo->format);
373
 
   } else {
374
 
      image_view->view_format = pCreateInfo->format;
375
 
   }
376
 
 
377
 
   image_view->swizzle = (VkComponentMapping) {
378
 
      .r = remap_swizzle(pCreateInfo->components.r, VK_COMPONENT_SWIZZLE_R),
379
 
      .g = remap_swizzle(pCreateInfo->components.g, VK_COMPONENT_SWIZZLE_G),
380
 
      .b = remap_swizzle(pCreateInfo->components.b, VK_COMPONENT_SWIZZLE_B),
381
 
      .a = remap_swizzle(pCreateInfo->components.a, VK_COMPONENT_SWIZZLE_A),
382
 
   };
383
 
 
384
 
   assert(range->layerCount > 0);
385
 
   assert(range->baseMipLevel < image->mip_levels);
386
 
 
387
 
   image_view->base_mip_level = range->baseMipLevel;
388
 
   image_view->level_count = vk_image_subresource_level_count(image, range);
389
 
   image_view->base_array_layer = range->baseArrayLayer;
390
 
   image_view->layer_count = vk_image_subresource_layer_count(image, range);
391
 
 
392
 
   image_view->extent =
393
 
      vk_image_mip_level_extent(image, image_view->base_mip_level);
394
 
 
395
 
   assert(image_view->base_mip_level + image_view->level_count
396
 
          <= image->mip_levels);
397
 
   switch (image->image_type) {
398
 
   default:
399
 
      unreachable("bad VkImageType");
400
 
   case VK_IMAGE_TYPE_1D:
401
 
   case VK_IMAGE_TYPE_2D:
402
 
      assert(image_view->base_array_layer + image_view->layer_count
403
 
             <= image->array_layers);
404
 
      break;
405
 
   case VK_IMAGE_TYPE_3D:
406
 
      assert(image_view->base_array_layer + image_view->layer_count
407
 
             <= image_view->extent.depth);
408
 
      break;
409
 
   }
410
 
 
411
 
   /* If we are creating a color view from a depth/stencil image we compute
412
 
    * usage from the underlying depth/stencil aspects.
413
 
    */
414
 
   const VkImageUsageFlags image_usage = is_color_view_of_depth_stencil ?
415
 
      vk_image_usage(image, image->aspects) :
416
 
      vk_image_usage(image, image_view->aspects);
417
 
   const VkImageViewUsageCreateInfo *usage_info =
418
 
      vk_find_struct_const(pCreateInfo, IMAGE_VIEW_USAGE_CREATE_INFO);
419
 
   image_view->usage = usage_info ? usage_info->usage : image_usage;
420
 
   assert(!(image_view->usage & ~image_usage));
421
 
}
422
 
 
423
 
void
424
 
vk_image_view_finish(struct vk_image_view *image_view)
425
 
{
426
 
   vk_object_base_finish(&image_view->base);
427
 
}
428
 
 
429
 
void *
430
 
vk_image_view_create(struct vk_device *device,
431
 
                     const VkImageViewCreateInfo *pCreateInfo,
432
 
                     const VkAllocationCallbacks *alloc,
433
 
                     size_t size)
434
 
{
435
 
   struct vk_image_view *image_view =
436
 
      vk_zalloc2(&device->alloc, alloc, size, 8,
437
 
                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
438
 
   if (image_view == NULL)
439
 
      return NULL;
440
 
 
441
 
   vk_image_view_init(device, image_view, pCreateInfo);
442
 
 
443
 
   return image_view;
444
 
}
445
 
 
446
 
void
447
 
vk_image_view_destroy(struct vk_device *device,
448
 
                      const VkAllocationCallbacks *alloc,
449
 
                      struct vk_image_view *image_view)
450
 
{
451
 
   vk_object_free(device, alloc, image_view);
452
 
}
453
 
 
454
 
bool
455
 
vk_image_layout_is_read_only(VkImageLayout layout,
456
 
                             VkImageAspectFlagBits aspect)
457
 
{
458
 
   assert(util_bitcount(aspect) == 1);
459
 
 
460
 
   switch (layout) {
461
 
   case VK_IMAGE_LAYOUT_UNDEFINED:
462
 
   case VK_IMAGE_LAYOUT_PREINITIALIZED:
463
 
      return true; /* These are only used for layout transitions */
464
 
 
465
 
   case VK_IMAGE_LAYOUT_GENERAL:
466
 
   case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
467
 
   case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
468
 
   case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
469
 
   case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
470
 
   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
471
 
   case VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL:
472
 
   case VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR:
473
 
      return false;
474
 
 
475
 
   case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
476
 
   case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
477
 
   case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
478
 
   case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
479
 
   case VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV:
480
 
   case VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT:
481
 
   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
482
 
   case VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL:
483
 
   case VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL_KHR:
484
 
      return true;
485
 
 
486
 
   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
487
 
      return aspect == VK_IMAGE_ASPECT_DEPTH_BIT;
488
 
 
489
 
   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
490
 
      return aspect == VK_IMAGE_ASPECT_STENCIL_BIT;
491
 
 
492
 
   case VK_IMAGE_LAYOUT_MAX_ENUM:
493
 
#ifdef VK_ENABLE_BETA_EXTENSIONS
494
 
   case VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR:
495
 
   case VK_IMAGE_LAYOUT_VIDEO_DECODE_SRC_KHR:
496
 
   case VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR:
497
 
   case VK_IMAGE_LAYOUT_VIDEO_ENCODE_DST_KHR:
498
 
   case VK_IMAGE_LAYOUT_VIDEO_ENCODE_SRC_KHR:
499
 
   case VK_IMAGE_LAYOUT_VIDEO_ENCODE_DPB_KHR:
500
 
#endif
501
 
      unreachable("Invalid image layout.");
502
 
   }
503
 
 
504
 
   unreachable("Invalid image layout.");
505
 
}
506
 
 
507
 
bool
508
 
vk_image_layout_is_depth_only(VkImageLayout layout)
509
 
{
510
 
   switch (layout) {
511
 
   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
512
 
   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
513
 
      return true;
514
 
 
515
 
   default:
516
 
      return false;
517
 
   }
518
 
}
519
 
 
520
 
/* From the Vulkan Specification 1.2.166 - VkAttachmentReference2:
521
 
 *
522
 
 *   "If layout only specifies the layout of the depth aspect of the
523
 
 *    attachment, the layout of the stencil aspect is specified by the
524
 
 *    stencilLayout member of a VkAttachmentReferenceStencilLayout structure
525
 
 *    included in the pNext chain. Otherwise, layout describes the layout for
526
 
 *    all relevant image aspects."
527
 
 */
528
 
VkImageLayout
529
 
vk_att_ref_stencil_layout(const VkAttachmentReference2KHR *att_ref,
530
 
                          const VkAttachmentDescription2 *attachments)
531
 
{
532
 
   /* From VUID-VkAttachmentReference2-attachment-04755:
533
 
    *  "If attachment is not VK_ATTACHMENT_UNUSED, and the format of the
534
 
    *   referenced attachment is a depth/stencil format which includes both
535
 
    *   depth and stencil aspects [...]
536
 
    */
537
 
   if (att_ref->attachment == VK_ATTACHMENT_UNUSED ||
538
 
       !vk_format_has_stencil(attachments[att_ref->attachment].format))
539
 
      return VK_IMAGE_LAYOUT_UNDEFINED;
540
 
 
541
 
   const VkAttachmentReferenceStencilLayoutKHR *stencil_ref =
542
 
      vk_find_struct_const(att_ref->pNext, ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
543
 
 
544
 
   if (stencil_ref)
545
 
      return stencil_ref->stencilLayout;
546
 
 
547
 
   /* From VUID-VkAttachmentReference2-attachment-04755:
548
 
    *  "If attachment is not VK_ATTACHMENT_UNUSED, and the format of the
549
 
    *   referenced attachment is a depth/stencil format which includes both
550
 
    *   depth and stencil aspects, and layout is
551
 
    *   VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL or
552
 
    *   VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL, the pNext chain must include
553
 
    *   a VkAttachmentReferenceStencilLayout structure."
554
 
    */
555
 
   assert(!vk_image_layout_is_depth_only(att_ref->layout));
556
 
 
557
 
   return att_ref->layout;
558
 
}
559
 
 
560
 
/* From the Vulkan Specification 1.2.184:
561
 
 *
562
 
 *   "If the pNext chain includes a VkAttachmentDescriptionStencilLayout
563
 
 *    structure, then the stencilInitialLayout and stencilFinalLayout members
564
 
 *    specify the initial and final layouts of the stencil aspect of a
565
 
 *    depth/stencil format, and initialLayout and finalLayout only apply to the
566
 
 *    depth aspect. For depth-only formats, the
567
 
 *    VkAttachmentDescriptionStencilLayout structure is ignored. For
568
 
 *    stencil-only formats, the initial and final layouts of the stencil aspect
569
 
 *    are taken from the VkAttachmentDescriptionStencilLayout structure if
570
 
 *    present, or initialLayout and finalLayout if not present."
571
 
 *
572
 
 *   "If format is a depth/stencil format, and either initialLayout or
573
 
 *    finalLayout does not specify a layout for the stencil aspect, then the
574
 
 *    application must specify the initial and final layouts of the stencil
575
 
 *    aspect by including a VkAttachmentDescriptionStencilLayout structure in
576
 
 *    the pNext chain."
577
 
 */
578
 
VkImageLayout
579
 
vk_att_desc_stencil_layout(const VkAttachmentDescription2KHR *att_desc,
580
 
                             bool final)
581
 
{
582
 
   if (!vk_format_has_stencil(att_desc->format))
583
 
      return VK_IMAGE_LAYOUT_UNDEFINED;
584
 
 
585
 
   const VkAttachmentDescriptionStencilLayoutKHR *stencil_desc =
586
 
      vk_find_struct_const(att_desc->pNext, ATTACHMENT_DESCRIPTION_STENCIL_LAYOUT_KHR);
587
 
 
588
 
   if (stencil_desc) {
589
 
      return final ?
590
 
         stencil_desc->stencilFinalLayout :
591
 
         stencil_desc->stencilInitialLayout;
592
 
   }
593
 
 
594
 
   const VkImageLayout main_layout =
595
 
      final ? att_desc->finalLayout : att_desc->initialLayout;
596
 
 
597
 
   /* From VUID-VkAttachmentDescription2-format-03302/03303:
598
 
    *  "If format is a depth/stencil format which includes both depth and
599
 
    *   stencil aspects, and initial/finalLayout is
600
 
    *   VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL or
601
 
    *   VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL, the pNext chain must include
602
 
    *   a VkAttachmentDescriptionStencilLayout structure."
603
 
    */
604
 
   assert(!vk_image_layout_is_depth_only(main_layout));
605
 
 
606
 
   return main_layout;
607
 
}
608
 
 
609
 
VkImageUsageFlags
610
 
vk_image_layout_to_usage_flags(VkImageLayout layout,
611
 
                               VkImageAspectFlagBits aspect)
612
 
{
613
 
   assert(util_bitcount(aspect) == 1);
614
 
 
615
 
   switch (layout) {
616
 
   case VK_IMAGE_LAYOUT_UNDEFINED:
617
 
   case VK_IMAGE_LAYOUT_PREINITIALIZED:
618
 
      return 0u;
619
 
 
620
 
   case VK_IMAGE_LAYOUT_GENERAL:
621
 
      return ~0u;
622
 
 
623
 
   case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
624
 
      assert(aspect & VK_IMAGE_ASPECT_ANY_COLOR_MASK_MESA);
625
 
      return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
626
 
 
627
 
   case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
628
 
      assert(aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
629
 
                       VK_IMAGE_ASPECT_STENCIL_BIT));
630
 
      return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
631
 
 
632
 
   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
633
 
      assert(aspect & VK_IMAGE_ASPECT_DEPTH_BIT);
634
 
      return vk_image_layout_to_usage_flags(
635
 
         VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
636
 
 
637
 
   case VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL:
638
 
      assert(aspect & VK_IMAGE_ASPECT_STENCIL_BIT);
639
 
      return vk_image_layout_to_usage_flags(
640
 
         VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
641
 
 
642
 
   case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
643
 
      assert(aspect & (VK_IMAGE_ASPECT_DEPTH_BIT |
644
 
                       VK_IMAGE_ASPECT_STENCIL_BIT));
645
 
      return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT |
646
 
             VK_IMAGE_USAGE_SAMPLED_BIT |
647
 
             VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
648
 
 
649
 
   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
650
 
      assert(aspect & VK_IMAGE_ASPECT_DEPTH_BIT);
651
 
      return vk_image_layout_to_usage_flags(
652
 
         VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
653
 
 
654
 
   case VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL:
655
 
      assert(aspect & VK_IMAGE_ASPECT_STENCIL_BIT);
656
 
      return vk_image_layout_to_usage_flags(
657
 
         VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
658
 
 
659
 
   case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
660
 
      return VK_IMAGE_USAGE_SAMPLED_BIT |
661
 
             VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
662
 
 
663
 
   case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
664
 
      return VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
665
 
 
666
 
   case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
667
 
      return VK_IMAGE_USAGE_TRANSFER_DST_BIT;
668
 
 
669
 
   case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
670
 
      if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
671
 
         return vk_image_layout_to_usage_flags(
672
 
            VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
673
 
      } else if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
674
 
         return vk_image_layout_to_usage_flags(
675
 
            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
676
 
      } else {
677
 
         assert(!"Must be a depth/stencil aspect");
678
 
         return 0;
679
 
      }
680
 
 
681
 
   case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
682
 
      if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
683
 
         return vk_image_layout_to_usage_flags(
684
 
            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, aspect);
685
 
      } else if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
686
 
         return vk_image_layout_to_usage_flags(
687
 
            VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, aspect);
688
 
      } else {
689
 
         assert(!"Must be a depth/stencil aspect");
690
 
         return 0;
691
 
      }
692
 
 
693
 
   case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
694
 
      assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
695
 
      /* This needs to be handled specially by the caller */
696
 
      return 0;
697
 
 
698
 
   case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
699
 
      assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
700
 
      return vk_image_layout_to_usage_flags(VK_IMAGE_LAYOUT_GENERAL, aspect);
701
 
 
702
 
   case VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV:
703
 
      assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
704
 
      return VK_IMAGE_USAGE_SHADING_RATE_IMAGE_BIT_NV;
705
 
 
706
 
   case VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT:
707
 
      assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
708
 
      return VK_IMAGE_USAGE_FRAGMENT_DENSITY_MAP_BIT_EXT;
709
 
 
710
 
   case VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR:
711
 
      if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT ||
712
 
          aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
713
 
         return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
714
 
      } else {
715
 
         assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
716
 
         return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
717
 
      }
718
 
 
719
 
   case VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL_KHR:
720
 
      return VK_IMAGE_USAGE_SAMPLED_BIT |
721
 
             VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
722
 
 
723
 
   case VK_IMAGE_LAYOUT_MAX_ENUM:
724
 
#ifdef VK_ENABLE_BETA_EXTENSIONS
725
 
   case VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR:
726
 
   case VK_IMAGE_LAYOUT_VIDEO_DECODE_SRC_KHR:
727
 
   case VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR:
728
 
   case VK_IMAGE_LAYOUT_VIDEO_ENCODE_DST_KHR:
729
 
   case VK_IMAGE_LAYOUT_VIDEO_ENCODE_SRC_KHR:
730
 
   case VK_IMAGE_LAYOUT_VIDEO_ENCODE_DPB_KHR:
731
 
#endif
732
 
      unreachable("Invalid image layout.");
733
 
   }
734
 
 
735
 
   unreachable("Invalid image layout.");
736
 
}