vk_image.h 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#ifndef VK_IMAGE_H
24#define VK_IMAGE_H
25
26#include "vk_object.h"
27
28#include "util/u_math.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34struct vk_image {
35   struct vk_object_base base;
36
37   VkImageCreateFlags create_flags;
38   VkImageType image_type;
39   VkFormat format;
40   VkExtent3D extent;
41   uint32_t mip_levels;
42   uint32_t array_layers;
43   VkSampleCountFlagBits samples;
44   VkImageTiling tiling;
45   VkImageUsageFlags usage;
46
47   /* Derived from format */
48   VkImageAspectFlags aspects;
49
50   /* VK_EXT_separate_stencil_usage */
51   VkImageUsageFlags stencil_usage;
52
53   /* VK_KHR_external_memory */
54   VkExternalMemoryHandleTypeFlags external_handle_types;
55
56   /* wsi_image_create_info::scanout */
57   bool wsi_legacy_scanout;
58
59#ifndef _WIN32
60   /* VK_EXT_drm_format_modifier
61    *
62    * Initialized by vk_image_create/init() to DRM_FORMAT_MOD_INVALID.  It's
63    * the job of the driver to parse the VK_EXT_drm_format_modifier extension
64    * structs and choose the actual modifier.
65    *
66    * Must be DRM_FORMAT_MOD_INVALID unless tiling is
67    * VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT.
68    */
69   uint64_t drm_format_mod;
70#endif
71
72#ifdef ANDROID
73   /* VK_ANDROID_external_memory_android_hardware_buffer */
74   uint64_t android_external_format;
75#endif
76};
77VK_DEFINE_NONDISP_HANDLE_CASTS(vk_image, base, VkImage,
78                               VK_OBJECT_TYPE_IMAGE);
79
80void vk_image_init(struct vk_device *device,
81                   struct vk_image *image,
82                   const VkImageCreateInfo *pCreateInfo);
83void vk_image_finish(struct vk_image *image);
84
85void *vk_image_create(struct vk_device *device,
86                      const VkImageCreateInfo *pCreateInfo,
87                      const VkAllocationCallbacks *alloc,
88                      size_t size);
89void vk_image_destroy(struct vk_device *device,
90                      const VkAllocationCallbacks *alloc,
91                      struct vk_image *image);
92
93void vk_image_set_format(struct vk_image *image, VkFormat format);
94
95VkImageUsageFlags vk_image_usage(const struct vk_image *image,
96                                 VkImageAspectFlags aspect_mask);
97
98VkImageAspectFlags vk_image_expand_aspect_mask(const struct vk_image *image,
99                                               VkImageAspectFlags aspect_mask);
100
101static inline VkExtent3D
102vk_image_mip_level_extent(const struct vk_image *image,
103                          uint32_t mip_level)
104{
105   const VkExtent3D extent = {
106      u_minify(image->extent.width,  mip_level),
107      u_minify(image->extent.height, mip_level),
108      u_minify(image->extent.depth,  mip_level),
109   };
110   return extent;
111}
112
113/* This is defined as a macro so that it works for both
114 * VkImageSubresourceRange and VkImageSubresourceLayers
115 */
116#define vk_image_subresource_layer_count(_image, _range) \
117   ((_range)->layerCount == VK_REMAINING_ARRAY_LAYERS ? \
118    (_image)->array_layers - (_range)->baseArrayLayer : (_range)->layerCount)
119
120static inline uint32_t
121vk_image_subresource_level_count(const struct vk_image *image,
122                                 const VkImageSubresourceRange *range)
123{
124   return range->levelCount == VK_REMAINING_MIP_LEVELS ?
125          image->mip_levels - range->baseMipLevel : range->levelCount;
126}
127
128struct vk_image_view {
129   struct vk_object_base base;
130
131   VkImageViewCreateFlags create_flags;
132   struct vk_image *image;
133   VkImageViewType view_type;
134
135   /** Image view format, relative to the selected aspects
136    *
137    * For a depth/stencil image:
138    *
139    *  - If vk_image_view::aspects contains both depth and stencil, this will
140    *    be the full depth/stencil format of the image.
141    *
142    *  - If only one aspect is selected, this will be the depth-only or
143    *    stencil-only format, as per the selected aspect.
144    *
145    * For color images, we have three cases:
146    *
147    *  1. It's a single-plane image in which case this is the unmodified
148    *     format provided to VkImageViewCreateInfo::format.
149    *
150    *  2. It's a YCbCr view of a multi-plane image in which case the
151    *     client will have asked for VK_IMAGE_ASPECT_COLOR_BIT and the
152    *     format provided will be the full planar format.  In this case,
153    *     the format will be the full format containing all the planes.
154    *
155    *  3. It's a single-plane view of a multi-plane image in which case
156    *     the client will have asked for VK_IMAGE_ASPECT_PLANE_N_BIT and
157    *     will have provided a format compatible with that specific
158    *     plane of the multi-planar format.  In this case, the format will be
159    *     the plane-compatible format requested by the client.
160    */
161   VkFormat format;
162
163   /* Component mapping, aka swizzle
164    *
165    * Unlike the swizzle provided via VkImageViewCreateInfo::components, this
166    * will never contain VK_COMPONENT_SWIZZLE_IDENTITY.  It will be resolved
167    * to VK_COMPONENT_SWIZZLE_R/G/B/A, as appropriate.
168    */
169   VkComponentMapping swizzle;
170
171   /** Aspects from the image represented by this view
172    *
173    * For depth/stencil images, this is the aspectMask provided by
174    * VkImageViewCreateinfo::subresourceRange::aspectMask.
175    *
176    * For color images, we have three cases:
177    *
178    *  1. It's a single-plane image in which case this only aspect is
179    *     VK_IMAGE_ASPECT_COLOR_BIT.
180    *
181    *  2. It's a YCbCr view of a multi-plane image in which case the
182    *     client will have asked for VK_IMAGE_ASPECT_COLOR_BIT and the
183    *     format provided will be the full planar format.  In this case,
184    *     aspects will be the full set of plane aspects in the image.
185    *
186    *  3. It's a single-plane view of a multi-plane image in which case
187    *     the client will have asked for VK_IMAGE_ASPECT_PLANE_N_BIT and
188    *     will have provided a format compatible with that specific
189    *     plane of the multi-planar format.  In this case, aspects will be
190    *     VK_IMAGE_ASPECT_PLANE_N_BIT where N is the selected plane.
191    *
192    * This seems almost backwards from the API but ensures that
193    * vk_image_view::aspects is always a subset of vk_image::aspects.
194    */
195   VkImageAspectFlags aspects;
196
197   uint32_t base_mip_level;
198   uint32_t level_count;
199   uint32_t base_array_layer;
200   uint32_t layer_count;
201
202   /* Image extent at LOD 0 */
203   VkExtent3D extent;
204
205   /* VK_KHR_maintenance2 */
206   VkImageUsageFlags usage;
207};
208
209void vk_image_view_init(struct vk_device *device,
210                        struct vk_image_view *image_view,
211                        const VkImageViewCreateInfo *pCreateInfo);
212void vk_image_view_finish(struct vk_image_view *image_view);
213
214void *vk_image_view_create(struct vk_device *device,
215                           const VkImageViewCreateInfo *pCreateInfo,
216                           const VkAllocationCallbacks *alloc,
217                           size_t size);
218void vk_image_view_destroy(struct vk_device *device,
219                           const VkAllocationCallbacks *alloc,
220                           struct vk_image_view *image_view);
221
222bool vk_image_layout_is_read_only(VkImageLayout layout,
223                                  VkImageAspectFlagBits aspect);
224VkImageUsageFlags vk_image_layout_to_usage_flags(VkImageLayout layout,
225                                                 VkImageAspectFlagBits aspect);
226
227#ifdef __cplusplus
228}
229#endif
230
231#endif /* VK_IMAGE_H */
232