17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Intel Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg#ifndef VK_IMAGE_H
247ec681f3Smrg#define VK_IMAGE_H
257ec681f3Smrg
267ec681f3Smrg#include "vk_object.h"
277ec681f3Smrg
287ec681f3Smrg#include "util/u_math.h"
297ec681f3Smrg
307ec681f3Smrg#ifdef __cplusplus
317ec681f3Smrgextern "C" {
327ec681f3Smrg#endif
337ec681f3Smrg
347ec681f3Smrgstruct vk_image {
357ec681f3Smrg   struct vk_object_base base;
367ec681f3Smrg
377ec681f3Smrg   VkImageCreateFlags create_flags;
387ec681f3Smrg   VkImageType image_type;
397ec681f3Smrg   VkFormat format;
407ec681f3Smrg   VkExtent3D extent;
417ec681f3Smrg   uint32_t mip_levels;
427ec681f3Smrg   uint32_t array_layers;
437ec681f3Smrg   VkSampleCountFlagBits samples;
447ec681f3Smrg   VkImageTiling tiling;
457ec681f3Smrg   VkImageUsageFlags usage;
467ec681f3Smrg
477ec681f3Smrg   /* Derived from format */
487ec681f3Smrg   VkImageAspectFlags aspects;
497ec681f3Smrg
507ec681f3Smrg   /* VK_EXT_separate_stencil_usage */
517ec681f3Smrg   VkImageUsageFlags stencil_usage;
527ec681f3Smrg
537ec681f3Smrg   /* VK_KHR_external_memory */
547ec681f3Smrg   VkExternalMemoryHandleTypeFlags external_handle_types;
557ec681f3Smrg
567ec681f3Smrg   /* wsi_image_create_info::scanout */
577ec681f3Smrg   bool wsi_legacy_scanout;
587ec681f3Smrg
597ec681f3Smrg#ifndef _WIN32
607ec681f3Smrg   /* VK_EXT_drm_format_modifier
617ec681f3Smrg    *
627ec681f3Smrg    * Initialized by vk_image_create/init() to DRM_FORMAT_MOD_INVALID.  It's
637ec681f3Smrg    * the job of the driver to parse the VK_EXT_drm_format_modifier extension
647ec681f3Smrg    * structs and choose the actual modifier.
657ec681f3Smrg    *
667ec681f3Smrg    * Must be DRM_FORMAT_MOD_INVALID unless tiling is
677ec681f3Smrg    * VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT.
687ec681f3Smrg    */
697ec681f3Smrg   uint64_t drm_format_mod;
707ec681f3Smrg#endif
717ec681f3Smrg
727ec681f3Smrg#ifdef ANDROID
737ec681f3Smrg   /* VK_ANDROID_external_memory_android_hardware_buffer */
747ec681f3Smrg   uint64_t android_external_format;
757ec681f3Smrg#endif
767ec681f3Smrg};
777ec681f3SmrgVK_DEFINE_NONDISP_HANDLE_CASTS(vk_image, base, VkImage,
787ec681f3Smrg                               VK_OBJECT_TYPE_IMAGE);
797ec681f3Smrg
807ec681f3Smrgvoid vk_image_init(struct vk_device *device,
817ec681f3Smrg                   struct vk_image *image,
827ec681f3Smrg                   const VkImageCreateInfo *pCreateInfo);
837ec681f3Smrgvoid vk_image_finish(struct vk_image *image);
847ec681f3Smrg
857ec681f3Smrgvoid *vk_image_create(struct vk_device *device,
867ec681f3Smrg                      const VkImageCreateInfo *pCreateInfo,
877ec681f3Smrg                      const VkAllocationCallbacks *alloc,
887ec681f3Smrg                      size_t size);
897ec681f3Smrgvoid vk_image_destroy(struct vk_device *device,
907ec681f3Smrg                      const VkAllocationCallbacks *alloc,
917ec681f3Smrg                      struct vk_image *image);
927ec681f3Smrg
937ec681f3Smrgvoid vk_image_set_format(struct vk_image *image, VkFormat format);
947ec681f3Smrg
957ec681f3SmrgVkImageUsageFlags vk_image_usage(const struct vk_image *image,
967ec681f3Smrg                                 VkImageAspectFlags aspect_mask);
977ec681f3Smrg
987ec681f3SmrgVkImageAspectFlags vk_image_expand_aspect_mask(const struct vk_image *image,
997ec681f3Smrg                                               VkImageAspectFlags aspect_mask);
1007ec681f3Smrg
1017ec681f3Smrgstatic inline VkExtent3D
1027ec681f3Smrgvk_image_mip_level_extent(const struct vk_image *image,
1037ec681f3Smrg                          uint32_t mip_level)
1047ec681f3Smrg{
1057ec681f3Smrg   const VkExtent3D extent = {
1067ec681f3Smrg      u_minify(image->extent.width,  mip_level),
1077ec681f3Smrg      u_minify(image->extent.height, mip_level),
1087ec681f3Smrg      u_minify(image->extent.depth,  mip_level),
1097ec681f3Smrg   };
1107ec681f3Smrg   return extent;
1117ec681f3Smrg}
1127ec681f3Smrg
1137ec681f3Smrg/* This is defined as a macro so that it works for both
1147ec681f3Smrg * VkImageSubresourceRange and VkImageSubresourceLayers
1157ec681f3Smrg */
1167ec681f3Smrg#define vk_image_subresource_layer_count(_image, _range) \
1177ec681f3Smrg   ((_range)->layerCount == VK_REMAINING_ARRAY_LAYERS ? \
1187ec681f3Smrg    (_image)->array_layers - (_range)->baseArrayLayer : (_range)->layerCount)
1197ec681f3Smrg
1207ec681f3Smrgstatic inline uint32_t
1217ec681f3Smrgvk_image_subresource_level_count(const struct vk_image *image,
1227ec681f3Smrg                                 const VkImageSubresourceRange *range)
1237ec681f3Smrg{
1247ec681f3Smrg   return range->levelCount == VK_REMAINING_MIP_LEVELS ?
1257ec681f3Smrg          image->mip_levels - range->baseMipLevel : range->levelCount;
1267ec681f3Smrg}
1277ec681f3Smrg
1287ec681f3Smrgstruct vk_image_view {
1297ec681f3Smrg   struct vk_object_base base;
1307ec681f3Smrg
1317ec681f3Smrg   VkImageViewCreateFlags create_flags;
1327ec681f3Smrg   struct vk_image *image;
1337ec681f3Smrg   VkImageViewType view_type;
1347ec681f3Smrg
1357ec681f3Smrg   /** Image view format, relative to the selected aspects
1367ec681f3Smrg    *
1377ec681f3Smrg    * For a depth/stencil image:
1387ec681f3Smrg    *
1397ec681f3Smrg    *  - If vk_image_view::aspects contains both depth and stencil, this will
1407ec681f3Smrg    *    be the full depth/stencil format of the image.
1417ec681f3Smrg    *
1427ec681f3Smrg    *  - If only one aspect is selected, this will be the depth-only or
1437ec681f3Smrg    *    stencil-only format, as per the selected aspect.
1447ec681f3Smrg    *
1457ec681f3Smrg    * For color images, we have three cases:
1467ec681f3Smrg    *
1477ec681f3Smrg    *  1. It's a single-plane image in which case this is the unmodified
1487ec681f3Smrg    *     format provided to VkImageViewCreateInfo::format.
1497ec681f3Smrg    *
1507ec681f3Smrg    *  2. It's a YCbCr view of a multi-plane image in which case the
1517ec681f3Smrg    *     client will have asked for VK_IMAGE_ASPECT_COLOR_BIT and the
1527ec681f3Smrg    *     format provided will be the full planar format.  In this case,
1537ec681f3Smrg    *     the format will be the full format containing all the planes.
1547ec681f3Smrg    *
1557ec681f3Smrg    *  3. It's a single-plane view of a multi-plane image in which case
1567ec681f3Smrg    *     the client will have asked for VK_IMAGE_ASPECT_PLANE_N_BIT and
1577ec681f3Smrg    *     will have provided a format compatible with that specific
1587ec681f3Smrg    *     plane of the multi-planar format.  In this case, the format will be
1597ec681f3Smrg    *     the plane-compatible format requested by the client.
1607ec681f3Smrg    */
1617ec681f3Smrg   VkFormat format;
1627ec681f3Smrg
1637ec681f3Smrg   /* Component mapping, aka swizzle
1647ec681f3Smrg    *
1657ec681f3Smrg    * Unlike the swizzle provided via VkImageViewCreateInfo::components, this
1667ec681f3Smrg    * will never contain VK_COMPONENT_SWIZZLE_IDENTITY.  It will be resolved
1677ec681f3Smrg    * to VK_COMPONENT_SWIZZLE_R/G/B/A, as appropriate.
1687ec681f3Smrg    */
1697ec681f3Smrg   VkComponentMapping swizzle;
1707ec681f3Smrg
1717ec681f3Smrg   /** Aspects from the image represented by this view
1727ec681f3Smrg    *
1737ec681f3Smrg    * For depth/stencil images, this is the aspectMask provided by
1747ec681f3Smrg    * VkImageViewCreateinfo::subresourceRange::aspectMask.
1757ec681f3Smrg    *
1767ec681f3Smrg    * For color images, we have three cases:
1777ec681f3Smrg    *
1787ec681f3Smrg    *  1. It's a single-plane image in which case this only aspect is
1797ec681f3Smrg    *     VK_IMAGE_ASPECT_COLOR_BIT.
1807ec681f3Smrg    *
1817ec681f3Smrg    *  2. It's a YCbCr view of a multi-plane image in which case the
1827ec681f3Smrg    *     client will have asked for VK_IMAGE_ASPECT_COLOR_BIT and the
1837ec681f3Smrg    *     format provided will be the full planar format.  In this case,
1847ec681f3Smrg    *     aspects will be the full set of plane aspects in the image.
1857ec681f3Smrg    *
1867ec681f3Smrg    *  3. It's a single-plane view of a multi-plane image in which case
1877ec681f3Smrg    *     the client will have asked for VK_IMAGE_ASPECT_PLANE_N_BIT and
1887ec681f3Smrg    *     will have provided a format compatible with that specific
1897ec681f3Smrg    *     plane of the multi-planar format.  In this case, aspects will be
1907ec681f3Smrg    *     VK_IMAGE_ASPECT_PLANE_N_BIT where N is the selected plane.
1917ec681f3Smrg    *
1927ec681f3Smrg    * This seems almost backwards from the API but ensures that
1937ec681f3Smrg    * vk_image_view::aspects is always a subset of vk_image::aspects.
1947ec681f3Smrg    */
1957ec681f3Smrg   VkImageAspectFlags aspects;
1967ec681f3Smrg
1977ec681f3Smrg   uint32_t base_mip_level;
1987ec681f3Smrg   uint32_t level_count;
1997ec681f3Smrg   uint32_t base_array_layer;
2007ec681f3Smrg   uint32_t layer_count;
2017ec681f3Smrg
2027ec681f3Smrg   /* Image extent at LOD 0 */
2037ec681f3Smrg   VkExtent3D extent;
2047ec681f3Smrg
2057ec681f3Smrg   /* VK_KHR_maintenance2 */
2067ec681f3Smrg   VkImageUsageFlags usage;
2077ec681f3Smrg};
2087ec681f3Smrg
2097ec681f3Smrgvoid vk_image_view_init(struct vk_device *device,
2107ec681f3Smrg                        struct vk_image_view *image_view,
2117ec681f3Smrg                        const VkImageViewCreateInfo *pCreateInfo);
2127ec681f3Smrgvoid vk_image_view_finish(struct vk_image_view *image_view);
2137ec681f3Smrg
2147ec681f3Smrgvoid *vk_image_view_create(struct vk_device *device,
2157ec681f3Smrg                           const VkImageViewCreateInfo *pCreateInfo,
2167ec681f3Smrg                           const VkAllocationCallbacks *alloc,
2177ec681f3Smrg                           size_t size);
2187ec681f3Smrgvoid vk_image_view_destroy(struct vk_device *device,
2197ec681f3Smrg                           const VkAllocationCallbacks *alloc,
2207ec681f3Smrg                           struct vk_image_view *image_view);
2217ec681f3Smrg
2227ec681f3Smrgbool vk_image_layout_is_read_only(VkImageLayout layout,
2237ec681f3Smrg                                  VkImageAspectFlagBits aspect);
2247ec681f3SmrgVkImageUsageFlags vk_image_layout_to_usage_flags(VkImageLayout layout,
2257ec681f3Smrg                                                 VkImageAspectFlagBits aspect);
2267ec681f3Smrg
2277ec681f3Smrg#ifdef __cplusplus
2287ec681f3Smrg}
2297ec681f3Smrg#endif
2307ec681f3Smrg
2317ec681f3Smrg#endif /* VK_IMAGE_H */
232