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/detect_os.h"
29 #include "util/u_math.h"
30
31 #if DETECT_OS_ANDROID
32 enum android_buffer_type {
33 ANDROID_BUFFER_NONE = 0,
34 ANDROID_BUFFER_NATIVE,
35 ANDROID_BUFFER_HARDWARE,
36 };
37 #endif
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 struct vk_image {
44 struct vk_object_base base;
45
46 VkImageCreateFlags create_flags;
47 VkImageType image_type;
48
49 /* format is from VkImageCreateInfo::format or
50 * VkExternalFormatANDROID::externalFormat. This works because only one of
51 * them can be defined and the runtime uses VkFormat for external formats.
52 */
53 VkFormat format;
54
55 VkExtent3D extent;
56 uint32_t mip_levels;
57 uint32_t array_layers;
58 VkSampleCountFlagBits samples;
59 VkImageTiling tiling;
60 VkImageUsageFlags usage;
61 VkSharingMode sharing_mode;
62
63 /* Derived from format */
64 VkImageAspectFlags aspects;
65
66 /* VK_EXT_separate_stencil_usage */
67 VkImageUsageFlags stencil_usage;
68
69 /* VK_KHR_external_memory */
70 VkExternalMemoryHandleTypeFlags external_handle_types;
71
72 /* VK_EXT_image_compression_control */
73 VkImageCompressionFlagsEXT compr_flags;
74
75 /* wsi_image_create_info::scanout */
76 bool wsi_legacy_scanout;
77
78 #if DETECT_OS_LINUX || DETECT_OS_BSD
79 /* VK_EXT_drm_format_modifier
80 *
81 * Initialized by vk_image_create/init() to DRM_FORMAT_MOD_INVALID. It's
82 * the job of the driver to parse the VK_EXT_drm_format_modifier extension
83 * structs and choose the actual modifier.
84 *
85 * Must be DRM_FORMAT_MOD_INVALID unless tiling is
86 * VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT.
87 */
88 uint64_t drm_format_mod;
89 #endif
90
91 #if DETECT_OS_ANDROID
92 enum android_buffer_type android_buffer_type;
93 VkDeviceMemory anb_memory;
94
95 /* AHARDWAREBUFFER_FORMAT for this image or 0
96 *
97 * A default is provided by the Vulkan runtime code based on the VkFormat
98 * but it may be overridden by the driver as needed.
99 */
100 uint32_t ahb_format;
101 #endif
102 };
103 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_image, base, VkImage,
104 VK_OBJECT_TYPE_IMAGE);
105
106 void vk_image_init(struct vk_device *device,
107 struct vk_image *image,
108 const VkImageCreateInfo *pCreateInfo);
109 void vk_image_finish(struct vk_image *image);
110
111 void *vk_image_create(struct vk_device *device,
112 const VkImageCreateInfo *pCreateInfo,
113 const VkAllocationCallbacks *alloc,
114 size_t size);
115 void vk_image_destroy(struct vk_device *device,
116 const VkAllocationCallbacks *alloc,
117 struct vk_image *image);
118
119 VkResult
120 vk_image_create_get_format_list(struct vk_device *device,
121 const VkImageCreateInfo *pCreateInfo,
122 const VkAllocationCallbacks *pAllocator,
123 VkFormat **formats,
124 uint32_t *format_count);
125
126 void vk_image_set_format(struct vk_image *image, VkFormat format);
127
128 VkImageUsageFlags vk_image_usage(const struct vk_image *image,
129 VkImageAspectFlags aspect_mask);
130
131 VkImageAspectFlags vk_image_expand_aspect_mask(const struct vk_image *image,
132 VkImageAspectFlags aspect_mask);
133
134 static inline VkExtent3D
vk_image_mip_level_extent(const struct vk_image * image,uint32_t mip_level)135 vk_image_mip_level_extent(const struct vk_image *image,
136 uint32_t mip_level)
137 {
138 const VkExtent3D extent = {
139 u_minify(image->extent.width, mip_level),
140 u_minify(image->extent.height, mip_level),
141 u_minify(image->extent.depth, mip_level),
142 };
143 return extent;
144 }
145
146 /* This is defined as a macro so that it works for both
147 * VkImageSubresourceRange and VkImageSubresourceLayers
148 */
149 #define vk_image_subresource_layer_count(_image, _range) \
150 ((_range)->layerCount == VK_REMAINING_ARRAY_LAYERS ? \
151 (_image)->array_layers - (_range)->baseArrayLayer : (_range)->layerCount)
152
153 static inline uint32_t
vk_image_subresource_level_count(const struct vk_image * image,const VkImageSubresourceRange * range)154 vk_image_subresource_level_count(const struct vk_image *image,
155 const VkImageSubresourceRange *range)
156 {
157 return range->levelCount == VK_REMAINING_MIP_LEVELS ?
158 image->mip_levels - range->baseMipLevel : range->levelCount;
159 }
160
161 static inline VkExtent3D
vk_image_sanitize_extent(const struct vk_image * image,const VkExtent3D imageExtent)162 vk_image_sanitize_extent(const struct vk_image *image,
163 const VkExtent3D imageExtent)
164 {
165 switch (image->image_type) {
166 case VK_IMAGE_TYPE_1D:
167 return (VkExtent3D) { imageExtent.width, 1, 1 };
168 case VK_IMAGE_TYPE_2D:
169 return (VkExtent3D) { imageExtent.width, imageExtent.height, 1 };
170 case VK_IMAGE_TYPE_3D:
171 return imageExtent;
172 default:
173 unreachable("invalid image type");
174 }
175 }
176
177 VkExtent3D
178 vk_image_extent_to_elements(const struct vk_image *image, VkExtent3D extent);
179
180 static inline VkOffset3D
vk_image_sanitize_offset(const struct vk_image * image,const VkOffset3D imageOffset)181 vk_image_sanitize_offset(const struct vk_image *image,
182 const VkOffset3D imageOffset)
183 {
184 switch (image->image_type) {
185 case VK_IMAGE_TYPE_1D:
186 return (VkOffset3D) { imageOffset.x, 0, 0 };
187 case VK_IMAGE_TYPE_2D:
188 return (VkOffset3D) { imageOffset.x, imageOffset.y, 0 };
189 case VK_IMAGE_TYPE_3D:
190 return imageOffset;
191 default:
192 unreachable("invalid image type");
193 }
194 }
195
196 VkOffset3D
197 vk_image_offset_to_elements(const struct vk_image *image, VkOffset3D offset);
198
199 struct vk_image_buffer_layout {
200 /**
201 * VkBufferImageCopy2::bufferRowLength or
202 * VkBufferImageCopy2::extent::width as needed.
203 */
204 uint32_t row_length;
205
206 /**
207 * VkBufferImageCopy2::bufferImageHeight or
208 * VkBufferImageCopy2::extent::height as needed.
209 */
210 uint32_t image_height;
211
212 /** Size of a single element (pixel or compressed block) in bytes */
213 uint32_t element_size_B;
214
215 /** Row stride in bytes */
216 uint32_t row_stride_B;
217
218 /** Image (or layer) stride in bytes
219 *
220 * For 1D or 2D array images, this is the stride in bytes between array
221 * slices. For 3D images, this is the stride in bytes between fixed-Z
222 * slices.
223 */
224 uint64_t image_stride_B;
225 };
226
227 static inline VkDeviceSize
vk_image_buffer_range(const struct vk_image * image,const struct vk_image_buffer_layout * buf_layout,const VkExtent3D * elem_extent,const VkImageSubresourceLayers * subres)228 vk_image_buffer_range(const struct vk_image *image,
229 const struct vk_image_buffer_layout *buf_layout,
230 const VkExtent3D *elem_extent,
231 const VkImageSubresourceLayers *subres)
232 {
233 uint32_t depth_or_layer_count =
234 MAX2(elem_extent->depth, vk_image_subresource_layer_count(image, subres));
235
236 /* Depth, layer_count and height must be at least one, and we rely on that
237 * for the rest of the buffer range calculation. */
238 assert(depth_or_layer_count > 0);
239 assert(elem_extent->height > 0);
240
241 return (VkDeviceSize)buf_layout->image_stride_B * (depth_or_layer_count - 1) +
242 (VkDeviceSize)buf_layout->row_stride_B * (elem_extent->height - 1) +
243 (VkDeviceSize)buf_layout->element_size_B * elem_extent->width;
244 }
245
246 struct vk_image_buffer_layout
247 vk_image_buffer_copy_layout(const struct vk_image *image,
248 const VkBufferImageCopy2* region);
249
250 struct vk_image_buffer_layout
251 vk_memory_to_image_copy_layout(const struct vk_image *image,
252 const VkMemoryToImageCopyEXT* region);
253
254 struct vk_image_buffer_layout
255 vk_image_to_memory_copy_layout(const struct vk_image *image,
256 const VkImageToMemoryCopyEXT* region);
257
258 struct vk_image_view {
259 struct vk_object_base base;
260
261 VkImageViewCreateFlags create_flags;
262 struct vk_image *image;
263 VkImageViewType view_type;
264
265 /** VkImageViewCreateInfo::format or vk_image::format */
266 VkFormat format;
267
268 /** Image view format, relative to the selected aspects
269 *
270 * For a depth/stencil image:
271 *
272 * - If vk_image_view::aspects contains both depth and stencil, this will
273 * be the full depth/stencil format of the image.
274 *
275 * - If only one aspect is selected, this will be the depth-only or
276 * stencil-only format, as per the selected aspect.
277 *
278 * For color images, we have three cases:
279 *
280 * 1. It's a single-plane image in which case this is the unmodified
281 * format provided to VkImageViewCreateInfo::format or
282 * vk_image::format.
283 *
284 * 2. It's a YCbCr view of a multi-plane image in which case the
285 * client will have asked for VK_IMAGE_ASPECT_COLOR_BIT and the
286 * format provided will be the full planar format. In this case,
287 * the format will be the full format containing all the planes.
288 *
289 * 3. It's a single-plane view of a multi-plane image in which case
290 * the client will have asked for VK_IMAGE_ASPECT_PLANE_N_BIT and
291 * will have provided a format compatible with that specific
292 * plane of the multi-planar format. In this case, the format will be
293 * the plane-compatible format requested by the client.
294 */
295 VkFormat view_format;
296
297 /* Component mapping, aka swizzle
298 *
299 * Unlike the swizzle provided via VkImageViewCreateInfo::components, this
300 * will never contain VK_COMPONENT_SWIZZLE_IDENTITY. It will be resolved
301 * to VK_COMPONENT_SWIZZLE_R/G/B/A, as appropriate.
302 */
303 VkComponentMapping swizzle;
304
305 /** Aspects from the image represented by this view
306 *
307 * For depth/stencil images, this is the aspectMask provided by
308 * VkImageViewCreateinfo::subresourceRange::aspectMask.
309 *
310 * For color images, we have three cases:
311 *
312 * 1. It's a single-plane image in which case this only aspect is
313 * VK_IMAGE_ASPECT_COLOR_BIT.
314 *
315 * 2. It's a YCbCr view of a multi-plane image in which case the
316 * client will have asked for VK_IMAGE_ASPECT_COLOR_BIT and the
317 * format provided will be the full planar format. In this case,
318 * aspects will be the full set of plane aspects in the image.
319 *
320 * 3. It's a single-plane view of a multi-plane image in which case
321 * the client will have asked for VK_IMAGE_ASPECT_PLANE_N_BIT and
322 * will have provided a format compatible with that specific
323 * plane of the multi-planar format. In this case, aspects will be
324 * VK_IMAGE_ASPECT_PLANE_N_BIT where N is the selected plane.
325 *
326 * This seems almost backwards from the API but ensures that
327 * vk_image_view::aspects is always a subset of vk_image::aspects.
328 */
329 VkImageAspectFlags aspects;
330
331 uint32_t base_mip_level;
332 uint32_t level_count;
333 uint32_t base_array_layer;
334 uint32_t layer_count;
335
336 /* VK_EXT_sliced_view_of_3d */
337 struct {
338 /* VkImageViewSlicedCreateInfoEXT::sliceOffset
339 *
340 * This field will be 0 for 1D and 2D images, 2D views of 3D images, or
341 * when no VkImageViewSlicedCreateInfoEXT is provided.
342 */
343 uint32_t z_slice_offset;
344
345 /* VkImageViewSlicedCreateInfoEXT::sliceCount
346 *
347 * This field will be 1 for 1D and 2D images or 2D views of 3D images.
348 * For 3D views, it will be VkImageViewSlicedCreateInfoEXT::sliceCount
349 * or image view depth (see vk_image_view::extent) when no
350 * VkImageViewSlicedCreateInfoEXT is provided.
351 */
352 uint32_t z_slice_count;
353 } storage;
354
355 /* VK_EXT_image_view_min_lod */
356 float min_lod;
357
358 /* Image extent at LOD 0 */
359 VkExtent3D extent;
360
361 /* VK_KHR_maintenance2 */
362 VkImageUsageFlags usage;
363 };
364 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_image_view, base, VkImageView,
365 VK_OBJECT_TYPE_IMAGE_VIEW);
366
367 void vk_image_view_init(struct vk_device *device,
368 struct vk_image_view *image_view,
369 bool driver_internal,
370 const VkImageViewCreateInfo *pCreateInfo);
371 void vk_image_view_finish(struct vk_image_view *image_view);
372
373 void *vk_image_view_create(struct vk_device *device,
374 bool driver_internal,
375 const VkImageViewCreateInfo *pCreateInfo,
376 const VkAllocationCallbacks *alloc,
377 size_t size);
378 void vk_image_view_destroy(struct vk_device *device,
379 const VkAllocationCallbacks *alloc,
380 struct vk_image_view *image_view);
381
382 static inline VkImageSubresourceRange
vk_image_view_subresource_range(const struct vk_image_view * view)383 vk_image_view_subresource_range(const struct vk_image_view *view)
384 {
385 VkImageSubresourceRange range = {
386 .aspectMask = view->aspects,
387 .baseMipLevel = view->base_mip_level,
388 .levelCount = view->level_count,
389 .baseArrayLayer = view->base_array_layer,
390 .layerCount = view->layer_count,
391 };
392
393 return range;
394 }
395
396 bool vk_image_layout_is_read_only(VkImageLayout layout,
397 VkImageAspectFlagBits aspect);
398 bool vk_image_layout_is_depth_only(VkImageLayout layout);
399
400 VkImageUsageFlags vk_image_layout_to_usage_flags(VkImageLayout layout,
401 VkImageAspectFlagBits aspect);
402
403 VkImageLayout vk_att_ref_stencil_layout(const VkAttachmentReference2 *att_ref,
404 const VkAttachmentDescription2 *attachments);
405 VkImageLayout vk_att_desc_stencil_layout(const VkAttachmentDescription2 *att_desc,
406 bool final);
407
408 #if DETECT_OS_ANDROID
409 static inline bool
vk_image_is_android_native_buffer(struct vk_image * image)410 vk_image_is_android_native_buffer(struct vk_image *image)
411 {
412 return image->android_buffer_type == ANDROID_BUFFER_NATIVE;
413 }
414 #else
415 static inline bool
vk_image_is_android_native_buffer(struct vk_image * image)416 vk_image_is_android_native_buffer(struct vk_image *image)
417 {
418 return false;
419 }
420 #endif /* DETECT_OS_ANDROID */
421
422 #if DETECT_OS_ANDROID && ANDROID_API_LEVEL >= 26
423 static inline bool
vk_image_is_android_hardware_buffer(struct vk_image * image)424 vk_image_is_android_hardware_buffer(struct vk_image *image)
425 {
426 return image->android_buffer_type == ANDROID_BUFFER_HARDWARE;
427 }
428 #else
429 static inline bool
vk_image_is_android_hardware_buffer(struct vk_image * image)430 vk_image_is_android_hardware_buffer(struct vk_image *image)
431 {
432 return false;
433 }
434 #endif
435
436 #ifdef __cplusplus
437 }
438 #endif
439
440 #endif /* VK_IMAGE_H */
441