Debian Codegraph @

src/panfrost/vulkan/panvk_image.c

raw
1 /*
2 * Copyright © 2025 Arm Ltd.
3 * Copyright © 2021 Collabora Ltd.
4 *
5 * Derived from tu_image.c which is:
6 * Copyright © 2016 Red Hat.
7 * Copyright © 2016 Bas Nieuwenhuizen
8 * Copyright © 2015 Intel Corporation
9 *
10 * SPDX-License-Identifier: MIT
11 */
12
13 #include "pan_afbc.h"
14 #include "pan_props.h"
15
16 #include "panvk_android.h"
17 #include "panvk_device.h"
18 #include "panvk_device_memory.h"
19 #include "panvk_entrypoints.h"
20 #include "panvk_image.h"
21 #include "panvk_instance.h"
22 #include "panvk_physical_device.h"
23 #include "panvk_sparse.h"
24
25 #include "drm-uapi/drm_fourcc.h"
26 #include "util/u_atomic.h"
27 #include "util/u_debug.h"
28 #include "util/u_drm.h"
29
30 #include "vk_format.h"
31 #include "vk_log.h"
32 #include "vk_object.h"
33 #include "vk_util.h"
34
35 bool
36 panvk_image_can_use_afbc(
37 struct panvk_physical_device *phys_dev, VkFormat fmt,
38 VkImageUsageFlags usage, VkImageType type, VkImageTiling tiling,
39 VkImageCreateFlags flags)
40 {
41 unsigned arch = pan_arch(phys_dev->kmod.dev->props.gpu_id);
42 enum pipe_format pfmt = vk_format_to_pipe_format(fmt);
43
44 /* Disallow AFBC if either of these is true
45 * - PANVK_DEBUG does not have the 'afbc' flag set
46 * - storage image views are requested
47 * - host image copies are requested
48 * - the GPU doesn't support AFBC
49 * - the format is not AFBC-able
50 * - tiling is set to linear
51 * - this is a 1D image
52 * - this is a 3D image on a pre-v7 GPU
53 * - this is a mutable format image on v7- (format re-interpretation is
54 * not possible on Bifrost hardware)
55 * - this is a sparse image
56 *
57 * Some of these checks are redundant with tests provided by the AFBC mod
58 * handler when pan_image_test_props() is called, but we need them because
59 * panvk_image_can_use_afbc() is also called from
60 * GetPhysicalDeviceImageFormatProperties2() and we don't have enough
61 * information to conduct a full image property check in this context.
62 */
63 return !PANVK_DEBUG(NO_AFBC) &&
64 !(usage &
65 (VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_HOST_TRANSFER_BIT)) &&
66 pan_query_afbc(&phys_dev->kmod.dev->props) &&
67 pan_afbc_supports_format(arch, pfmt) &&
68 tiling != VK_IMAGE_TILING_LINEAR && type != VK_IMAGE_TYPE_1D &&
69 (type != VK_IMAGE_TYPE_3D || arch >= 7) &&
70 (!(flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) || arch >= 9) &&
71 (!(flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT));
72 }
73
74 static enum mali_texture_dimension
75 panvk_image_type_to_mali_tex_dim(VkImageType type)
76 {
77 switch (type) {
78 case VK_IMAGE_TYPE_1D:
79 return MALI_TEXTURE_DIMENSION_1D;
80 case VK_IMAGE_TYPE_2D:
81 return MALI_TEXTURE_DIMENSION_2D;
82 case VK_IMAGE_TYPE_3D:
83 return MALI_TEXTURE_DIMENSION_3D;
84 default:
85 UNREACHABLE("Invalid image type");
86 }
87 }
88
89 static struct pan_image_usage
90 get_iusage(struct panvk_image *image, const VkImageCreateInfo *create_info)
91 {
92 const struct wsi_image_create_info *wsi_info =
93 vk_find_struct_const(create_info->pNext, WSI_IMAGE_CREATE_INFO_MESA);
94 struct pan_image_usage iusage = {0};
95
96 if (image->vk.usage &
97 (VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
98 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
99 iusage.bind |= PAN_BIND_SAMPLER_VIEW;
100
101 if (image->vk.usage & VK_IMAGE_USAGE_STORAGE_BIT)
102 iusage.bind |= PAN_BIND_STORAGE_IMAGE;
103
104 if (image->vk.usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
105 iusage.bind |= PAN_BIND_DEPTH_STENCIL;
106
107 if (image->vk.usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
108 iusage.bind |= PAN_BIND_RENDER_TARGET;
109
110 iusage.standard_sparse_mapping_granularity =
111 !!(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT);
112 iusage.host_copy =
113 !!(image->vk.usage & VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT);
114 iusage.legacy_scanout = wsi_info && wsi_info->scanout;
115 iusage.wsi = wsi_info != NULL;
116
117 return iusage;
118 }
119
120 static unsigned
121 get_plane_count(struct panvk_image *image)
122 {
123 bool combined_ds = vk_format_aspects(image->vk.format) ==
124 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
125
126 /* Only depth+stencil images can be made multiplanar behind the scene. */
127 if (!combined_ds)
128 return vk_format_get_plane_count(image->vk.format);
129
130 struct panvk_physical_device *phys_dev =
131 to_panvk_physical_device(image->vk.base.device->physical);
132 unsigned arch = pan_arch(phys_dev->kmod.dev->props.gpu_id);
133
134 /* Z32_S8X24 is not supported on v9+, and we don't want to use it
135 * on v7- anyway, because it's less efficient than the multiplanar
136 * alternative.
137 */
138 if (image->vk.format == VK_FORMAT_D32_SFLOAT_S8_UINT)
139 return 2;
140
141 assert(image->vk.format == VK_FORMAT_D24_UNORM_S8_UINT);
142
143 /* We can do AFBC(S8) on Valhall and we're thus better off using planar
144 * Z24+S8 so we can use AFBC when separateDepthStencilLayouts=true.
145 */
146 return arch >= 9 ? 2 : 1;
147 }
148
149 static enum pipe_format
150 select_depth_plane_pfmt(struct panvk_image *image, uint64_t mod)
151 {
152 switch (image->vk.format) {
153 case VK_FORMAT_D24_UNORM_S8_UINT:
154 /* We only use packed Z24 when AFBC is involved, to simplify copies on on
155 * AFBC resources.
156 */
157 return drm_is_afbc(mod) ? PIPE_FORMAT_Z24_UNORM_PACKED
158 : PIPE_FORMAT_Z24X8_UNORM;
159 case VK_FORMAT_D32_SFLOAT_S8_UINT:
160 return PIPE_FORMAT_Z32_FLOAT;
161 default:
162 UNREACHABLE("Invalid depth+stencil format");
163 }
164 }
165
166 static enum pipe_format
167 select_stencil_plane_pfmt(struct panvk_image *image)
168 {
169 switch (image->vk.format) {
170 case VK_FORMAT_D24_UNORM_S8_UINT:
171 case VK_FORMAT_D32_SFLOAT_S8_UINT:
172 return PIPE_FORMAT_S8_UINT;
173 default:
174 UNREACHABLE("Invalid depth+stencil format");
175 }
176 }
177
178 static enum pipe_format
179 select_plane_pfmt(struct panvk_image *image, uint64_t mod, unsigned plane)
180 {
181 if (panvk_image_is_planar_depth_stencil(image)) {
182 return plane > 0 ? select_stencil_plane_pfmt(image)
183 : select_depth_plane_pfmt(image, mod);
184 }
185
186 VkFormat plane_format = vk_format_get_plane_format(image->vk.format, plane);
187 return vk_format_to_pipe_format(plane_format);
188 }
189
190 static bool
191 panvk_image_can_use_mod(struct panvk_image *image,
192 const struct pan_image_usage *iusage, uint64_t mod,
193 bool optimal_only)
194 {
195 struct panvk_physical_device *phys_dev =
196 to_panvk_physical_device(image->vk.base.device->physical);
197 unsigned arch = pan_arch(phys_dev->kmod.dev->props.gpu_id);
198 const bool forced_linear = PANVK_DEBUG(LINEAR) ||
199 image->vk.tiling == VK_IMAGE_TILING_LINEAR ||
200 image->vk.image_type == VK_IMAGE_TYPE_1D;
201
202 /* If the image is meant to be linear, don't bother testing the
203 * other cases. */
204 if (forced_linear)
205 return mod == DRM_FORMAT_MOD_LINEAR;
206
207 assert(image->vk.tiling == VK_IMAGE_TILING_OPTIMAL ||
208 image->vk.tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT);
209
210 if (drm_is_afbc(mod)) {
211 /* AFBC explicitly disabled. */
212 if (PANVK_DEBUG(NO_AFBC))
213 return false;
214
215 /* Can't do AFBC if store/host copy is requested. */
216 if ((image->vk.usage | image->vk.stencil_usage) &
217 (VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_HOST_TRANSFER_BIT))
218 return false;
219
220 /* Can't do AFBC on v7- if mutable format is requested. */
221 if ((image->vk.create_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) &&
222 arch <= 7)
223 return false;
224
225 /* Disable AFBC on YUV-planar for now. */
226 if (vk_format_get_plane_count(image->vk.format) > 1)
227 return false;
228
229 /* We can't have separate depth/stencil layout transitions with
230 * interleaved ZS, so make sure we disallow AFBC on ZS unless
231 * it's using a planar layout.
232 */
233 if (image->vk.base.device->enabled_features.separateDepthStencilLayouts &&
234 panvk_image_is_interleaved_depth_stencil(image))
235 return false;
236
237 /* Aliased images not supported yet for single <-> multiplanar.
238 * The disjoint flag is what limits this to single <-> multiplanar.
239 * TODO: this can be relaxed once we have multiplanar AFBC. */
240 if ((image->vk.create_flags & VK_IMAGE_CREATE_ALIAS_BIT) &&
241 (image->vk.create_flags & VK_IMAGE_CREATE_DISJOINT_BIT))
242 return false;
243
244 /* On v6 and earlier, we can't reliably resolve directly to AFBC images
245 * (see avoid_direct_resolve_to() in panvk_vX_cmd_draw.c). For MS2SS,
246 * this means we know a priori that the single-sampled image is going to
247 * be a resolve target. It's better to leave it uncompressed than to
248 * eat the separate resolves.
249 */
250 if ((image->vk.create_flags &
251 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT) &&
252 arch < 7)
253 return false;
254 }
255
256 if (mod == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED) {
257 /* Multiplanar YUV with U-interleaving isn't supported by the HW. We
258 * also need to make sure images that can be aliased to planes of
259 * multi-planar images remain compatible with the aliased images, so
260 * don't allow U-interleaving for those either.
261 */
262 if (vk_format_get_plane_count(image->vk.format) > 1 ||
263 vk_image_can_be_aliased_to_yuv_plane(&image->vk))
264 return false;
265
266 /* If we're dealing with a compressed format that requires non-compressed
267 * views we can't use U_INTERLEAVED tiling because the tiling is different
268 * between compressed and non-compressed formats. If we wanted to support
269 * format re-interpretation we would have to specialize the shaders
270 * accessing non-compressed image views (coordinate patching for
271 * sampled/storage image, frag_coord patching for color attachments). Let's
272 * keep things simple for now and make all compressed images that
273 * have VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT set linear. */
274 if (image->vk.create_flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT)
275 return false;
276 }
277
278 /* Defer the rest of the checks to the mod handler. */
279 struct pan_image_props iprops = {
280 .modifier = mod,
281 .dim = panvk_image_type_to_mali_tex_dim(image->vk.image_type),
282 .array_size = image->vk.array_layers,
283 .nr_samples = image->vk.samples,
284 .nr_slices = image->vk.mip_levels,
285 };
286
287 for (uint8_t plane = 0; plane < image->plane_count; plane++) {
288 iprops.format = select_plane_pfmt(image, mod, plane);
289 iprops.extent_px = (struct pan_image_extent){
290 .width = vk_format_get_plane_width(image->vk.format, plane,
291 image->vk.extent.width),
292 .height = vk_format_get_plane_height(image->vk.format, plane,
293 image->vk.extent.height),
294 .depth = image->vk.extent.depth,
295 };
296
297 enum pan_mod_support supported =
298 pan_image_test_props(&phys_dev->kmod.dev->props, &iprops, iusage);
299 if (supported == PAN_MOD_NOT_SUPPORTED ||
300 (supported == PAN_MOD_NOT_OPTIMAL && optimal_only))
301 return false;
302 }
303
304 return true;
305 }
306
307 static uint64_t
308 panvk_image_get_explicit_mod(
309 struct panvk_image *image, const struct pan_image_usage *iusage,
310 const VkImageDrmFormatModifierExplicitCreateInfoEXT *explicit)
311 {
312 uint64_t mod = explicit->drmFormatModifier;
313
314 assert(!vk_format_is_depth_or_stencil(image->vk.format));
315 assert(image->vk.samples == 1);
316 assert(image->vk.array_layers == 1);
317 assert(image->vk.image_type != VK_IMAGE_TYPE_3D);
318 assert(panvk_image_can_use_mod(image, iusage, mod, false));
319
320 return mod;
321 }
322
323 static uint64_t
324 panvk_image_get_mod_from_list(struct panvk_image *image,
325 const struct pan_image_usage *iusage,
326 const uint64_t *mods, uint32_t mod_count)
327 {
328 PAN_SUPPORTED_MODIFIERS(supported_mods);
329
330 for (unsigned pass = 0; pass < 2; pass++) {
331 for (unsigned i = 0; i < ARRAY_SIZE(supported_mods); ++i) {
332 if (!panvk_image_can_use_mod(image, iusage, supported_mods[i],
333 pass == 0))
334 continue;
335
336 if (!mod_count ||
337 drm_find_modifier(supported_mods[i], mods, mod_count))
338 return supported_mods[i];
339 }
340 }
341
342 /* If we reached that point without finding a proper modifier, there's
343 * a serious issue. */
344 assert(!"Invalid modifier");
345 return DRM_FORMAT_MOD_INVALID;
346 }
347
348 static uint64_t
349 panvk_image_get_mod(struct panvk_image *image,
350 const VkImageCreateInfo *pCreateInfo)
351 {
352 struct pan_image_usage iusage = get_iusage(image, pCreateInfo);
353
354 if (pCreateInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
355 const VkImageDrmFormatModifierListCreateInfoEXT *mod_list =
356 vk_find_struct_const(pCreateInfo->pNext,
357 IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
358 const VkImageDrmFormatModifierExplicitCreateInfoEXT *explicit_mod =
359 vk_find_struct_const(
360 pCreateInfo->pNext,
361 IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT);
362
363 if (explicit_mod)
364 return panvk_image_get_explicit_mod(image, &iusage, explicit_mod);
365
366 if (mod_list)
367 return panvk_image_get_mod_from_list(image, &iusage,
368 mod_list->pDrmFormatModifiers,
369 mod_list->drmFormatModifierCount);
370
371 assert(!"Missing modifier info");
372 }
373
374 /* legacy scanout (images without any external modifier info) should default to LINEAR. */
375 if (iusage.legacy_scanout)
376 return DRM_FORMAT_MOD_LINEAR;
377
378 /* Without external dependencies, pick the best modifier that supports the image. */
379 return panvk_image_get_mod_from_list(image, &iusage, NULL, 0);
380 }
381
382 static bool
383 is_disjoint(const struct panvk_image *image)
384 {
385 assert((image->plane_count > 1 &&
386 !vk_format_is_depth_or_stencil(image->vk.format)) ||
387 (image->vk.create_flags & VK_IMAGE_CREATE_ALIAS_BIT) ||
388 !(image->vk.create_flags & VK_IMAGE_CREATE_DISJOINT_BIT));
389 return image->vk.create_flags & VK_IMAGE_CREATE_DISJOINT_BIT;
390 }
391
392 static bool
393 strict_import(struct panvk_image *image, uint32_t plane)
394 {
395 /* We can't do strict imports for AFBC because a Vulkan-based compositor
396 * might be importing buffers from clients that are relying on the old
397 * behavior. The only exception is AFBC(YUV) because support for these
398 * formats was added after we started enforcing WSI pitch. */
399 if (drm_is_afbc(image->vk.drm_format_mod) &&
400 !pan_format_is_yuv(image->planes[plane].image.props.format))
401 return false;
402
403 return true;
404 }
405
406 static VkResult
407 panvk_image_init_layouts(struct panvk_image *image,
408 const VkImageCreateInfo *pCreateInfo)
409 {
410 struct panvk_device *dev = to_panvk_device(image->vk.base.device);
411 struct panvk_physical_device *phys_dev =
412 to_panvk_physical_device(dev->vk.physical);
413 unsigned arch = pan_arch(phys_dev->kmod.dev->props.gpu_id);
414 const VkImageDrmFormatModifierExplicitCreateInfoEXT *explicit_info =
415 vk_find_struct_const(
416 pCreateInfo->pNext,
417 IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT);
418
419 const struct pan_mod_handler *mod_handler =
420 pan_mod_get_handler(arch, image->vk.drm_format_mod);
421 struct pan_image_layout_constraints plane_layout = {
422 .offset_B = 0,
423 };
424 for (uint8_t plane = 0; plane < image->plane_count; plane++) {
425 enum pipe_format pfmt =
426 select_plane_pfmt(image, image->vk.drm_format_mod, plane);
427
428 if (explicit_info) {
429 plane_layout = (struct pan_image_layout_constraints){
430 .offset_B = explicit_info->pPlaneLayouts[plane].offset,
431 .wsi_row_pitch_B = explicit_info->pPlaneLayouts[plane].rowPitch,
432 };
433 }
434
435 image->planes[plane].image = (struct pan_image){
436 .props = {
437 .modifier = image->vk.drm_format_mod,
438 .format = pfmt,
439 .dim = panvk_image_type_to_mali_tex_dim(image->vk.image_type),
440 .extent_px = {
441 .width = vk_format_get_plane_width(image->vk.format, plane,
442 image->vk.extent.width),
443 .height = vk_format_get_plane_height(image->vk.format, plane,
444 image->vk.extent.height),
445 .depth = image->vk.extent.depth,
446 },
447 .array_size = image->vk.array_layers,
448 .nr_samples = image->vk.samples,
449 .nr_slices = image->vk.mip_levels,
450 },
451 .mod_handler = mod_handler,
452 .planes = {&image->planes[plane].plane},
453 };
454
455 plane_layout.strict = strict_import(image, plane);
456 if (!pan_image_layout_init(arch, &image->planes[plane].image, 0,
457 &plane_layout)) {
458 return panvk_error(image->vk.base.device,
459 VK_ERROR_INITIALIZATION_FAILED);
460 }
461
462 if (!is_disjoint(image) && !explicit_info)
463 plane_layout.offset_B += image->planes[plane].plane.layout.data_size_B;
464 }
465
466 return VK_SUCCESS;
467 }
468
469 static void
470 panvk_image_pre_mod_select_meta_adjustments(struct panvk_image *image)
471 {
472 const VkImageAspectFlags aspects = vk_format_aspects(image->vk.format);
473 const VkImageUsageFlags all_usage =
474 image->vk.usage | image->vk.stencil_usage;
475
476 /* We do image blit/resolve with vk_meta, so when an image is flagged as
477 * being a potential transfer source, we also need to add the sampled usage.
478 */
479 if (image->vk.usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)
480 image->vk.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
481 if (image->vk.stencil_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)
482 image->vk.stencil_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
483
484 /* Similarly, image that can be a transfer destination can be attached
485 * as a color or depth-stencil attachment by vk_meta. */
486 if (image->vk.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
487 if (aspects & VK_IMAGE_ASPECT_DEPTH_BIT)
488 image->vk.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
489
490 if (aspects & VK_IMAGE_ASPECT_COLOR_BIT)
491 image->vk.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
492 }
493
494 if (image->vk.stencil_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)
495 image->vk.stencil_usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
496
497 /* vk_meta creates 2D array views of 3D images. */
498 if (all_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT &&
499 image->vk.image_type == VK_IMAGE_TYPE_3D)
500 image->vk.create_flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
501
502 /* Needed for resolve operations. */
503 if (image->vk.usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
504 image->vk.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
505
506 if (image->vk.usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT &&
507 aspects & VK_IMAGE_ASPECT_DEPTH_BIT)
508 image->vk.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
509
510 if (image->vk.stencil_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
511 image->vk.stencil_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
512
513 if ((image->vk.usage &
514 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) &&
515 vk_format_is_compressed(image->vk.format)) {
516 /* We need to be able to create RGBA views of compressed formats for
517 * vk_meta copies.
518 *
519 * FIXME: this might cause LINEAR to be used instead of a better modifier.
520 * See https://gitlab.freedesktop.org/panfrost/mesa/-/issues/271 for
521 * details.
522 */
523 image->vk.create_flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT |
524 VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT;
525 }
526 }
527
528 static void
529 panvk_image_post_mod_select_meta_adjustments(struct panvk_image *image)
530 {
531 const VkImageAspectFlags aspects = vk_format_aspects(image->vk.format);
532
533 /* If the image didn't end up using AFBC, we should add the storage flag
534 * to allow vkmeta to take the compute based copying path. */
535 if ((image->vk.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) &&
536 (aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
537 !drm_is_afbc(image->vk.drm_format_mod)) {
538 image->vk.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
539 }
540 }
541
542 static uint64_t
543 panvk_image_get_total_size(const struct panvk_image *image)
544 {
545 uint64_t size = 0;
546 for (uint8_t plane = 0; plane < image->plane_count; plane++) {
547 const struct pan_image_layout *layout =
548 &image->planes[plane].plane.layout;
549 size = MAX2(size, layout->slices[0].offset_B + layout->data_size_B);
550 }
551 return size;
552 }
553
554 static uint64_t
555 panvk_image_get_sparse_size(const struct panvk_image *image)
556 {
557 struct panvk_device *device = to_panvk_device(image->vk.base.device);
558 uint64_t image_size = panvk_image_get_total_size(image);
559 uint64_t page_size = panvk_get_gpu_page_size(device);
560 return ALIGN_POT(image_size, page_size);
561 }
562
563 VkResult
564 panvk_image_init(struct panvk_image *image,
565 const VkImageCreateInfo *pCreateInfo)
566 {
567 /* Needs to happen early for some panvk_image_ helpers to work. */
568 image->plane_count = get_plane_count(image);
569
570 /* Add any create/usage flags that might be needed for meta operations.
571 * This is run before the modifier selection because some
572 * usage/create_flags influence the modifier selection logic. */
573 panvk_image_pre_mod_select_meta_adjustments(image);
574
575 /* Now that we've patched the create/usage flags, we can proceed with the
576 * modifier selection. */
577 image->vk.drm_format_mod = panvk_image_get_mod(image, pCreateInfo);
578
579 /* Some modifiers like AFBC affect some decisions we make for vkmeta, but
580 * we don't want to outright prevent these modifiers. If those weren't used,
581 * we apply additional flags here. */
582 panvk_image_post_mod_select_meta_adjustments(image);
583
584 return panvk_image_init_layouts(image, pCreateInfo);
585 }
586
587 static void
588 panvk_image_plane_bind_mem(struct panvk_device *dev,
589 struct panvk_image_plane *plane,
590 struct panvk_device_memory *mem, uint64_t offset)
591 {
592 plane->plane.base = mem->addr.dev + offset;
593 plane->mem = mem;
594 plane->mem_offset = offset;
595 }
596
597 static void
598 panvk_image_plane_bind_addr(struct panvk_device *dev,
599 struct panvk_image_plane *plane,
600 uint64_t addr)
601 {
602 plane->plane.base = addr;
603 }
604
605 static void
606 create_ms_images(struct panvk_device *dev, struct panvk_image *img,
607 const VkImageCreateInfo *pCreateInfo,
608 const VkAllocationCallbacks *pAllocator)
609 {
610 struct panvk_physical_device *pdev =
611 to_panvk_physical_device(dev->vk.physical);
612
613 VkPhysicalDeviceImageFormatInfo2 info = {
614 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
615 .format = pCreateInfo->format,
616 .type = pCreateInfo->imageType,
617 .tiling = pCreateInfo->tiling,
618 .usage = pCreateInfo->usage,
619 .flags = pCreateInfo->flags,
620 };
621 VkImageFormatProperties2 properties = {};
622 panvk_GetPhysicalDeviceImageFormatProperties2(
623 vk_physical_device_to_handle(&pdev->vk), &info, &properties);
624
625 VkImageCreateInfo ms_img_info = *pCreateInfo;
626
627 assert(ms_img_info.flags &
628 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT);
629 ms_img_info.flags &=
630 ~VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
631
632 for (uint32_t msaa_idx = 0; msaa_idx < ARRAY_SIZE(img->ms_imgs);
633 ++msaa_idx) {
634 VkSampleCountFlagBits msaa = 1 << (msaa_idx + 1);
635
636 if ((properties.imageFormatProperties.sampleCounts & msaa) == 0) {
637 img->ms_imgs[msaa_idx] = VK_NULL_HANDLE;
638 continue;
639 }
640
641 ms_img_info.samples = msaa;
642
643 panvk_CreateImage(panvk_device_to_handle(dev), &ms_img_info, pAllocator,
644 &img->ms_imgs[msaa_idx]);
645
646 struct panvk_image *res = panvk_image_from_handle(img->ms_imgs[msaa_idx]);
647 assert(res->vk.format == img->vk.format);
648 assert(res->plane_count == img->plane_count);
649 }
650 }
651
652 VKAPI_ATTR VkResult VKAPI_CALL
653 panvk_CreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo,
654 const VkAllocationCallbacks *pAllocator, VkImage *pImage)
655 {
656 VK_FROM_HANDLE(panvk_device, dev, device);
657 struct panvk_physical_device *phys_dev =
658 to_panvk_physical_device(dev->vk.physical);
659 VkResult result;
660
661 if (panvk_android_is_gralloc_image(pCreateInfo)) {
662 return panvk_android_create_gralloc_image(device, pCreateInfo, pAllocator,
663 pImage);
664 }
665
666 if (wsi_common_is_swapchain_image(pCreateInfo)) {
667 return wsi_common_create_swapchain_image(&phys_dev->wsi_device,
668 pCreateInfo,
669 pImage);
670 }
671
672 struct panvk_image *image =
673 vk_image_create(&dev->vk, pCreateInfo, pAllocator, sizeof(*image));
674 if (!image)
675 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
676
677 result = panvk_image_init(image, pCreateInfo);
678 if (result != VK_SUCCESS) {
679 vk_image_destroy(&dev->vk, pAllocator, &image->vk);
680 return result;
681 }
682
683 uint64_t size = panvk_image_get_total_size(image);
684
685 /*
686 * From the Vulkan spec:
687 *
688 * If the size of the resultant image would exceed maxResourceSize, then
689 * vkCreateImage must fail and return VK_ERROR_OUT_OF_DEVICE_MEMORY.
690 */
691 if (size > UINT32_MAX) {
692 result = panvk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
693 goto err_destroy_image;
694 }
695
696 if (image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
697 uint64_t va_range = panvk_image_get_sparse_size(image);
698
699 image->sparse.device_address =
700 panvk_as_alloc(dev, &dev->as.heap, va_range,
701 pan_choose_gpu_va_alignment(dev->kmod.vm, va_range));
702 if (!image->sparse.device_address) {
703 result = panvk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
704 goto err_destroy_image;
705 }
706
707 for (unsigned plane = 0; plane < image->plane_count; plane++) {
708 panvk_image_plane_bind_addr(dev, &image->planes[plane],
709 image->sparse.device_address);
710 }
711
712 if ((image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) ||
713 PANVK_DEBUG(FORCE_BLACKHOLE)) {
714 /* Map last so that we don't have a possibility of getting any more
715 * errors, in which case we'd have to unmap.
716 */
717 result = panvk_map_to_blackhole(dev, image->sparse.device_address,
718 va_range);
719 if (result != VK_SUCCESS) {
720 result = panvk_error(dev, result);
721 goto err_free_va;
722 }
723 }
724 }
725
726 if (pCreateInfo->flags &
727 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT)
728 create_ms_images(dev, image, pCreateInfo, pAllocator);
729
730 *pImage = panvk_image_to_handle(image);
731 return VK_SUCCESS;
732
733 err_free_va:
734 if (image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)
735 panvk_as_free(dev, &dev->as.heap, image->sparse.device_address,
736 panvk_image_get_sparse_size(image));
737
738 err_destroy_image:
739 vk_image_destroy(&dev->vk, pAllocator, &image->vk);
740 return result;
741 }
742
743 VKAPI_ATTR void VKAPI_CALL
744 panvk_DestroyImage(VkDevice _device, VkImage _image,
745 const VkAllocationCallbacks *pAllocator)
746 {
747 VK_FROM_HANDLE(panvk_device, device, _device);
748 VK_FROM_HANDLE(panvk_image, image, _image);
749
750 if (!image)
751 return;
752
753 if (image->vk.create_flags &
754 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT) {
755 for (uint32_t i = 0; i < ARRAY_SIZE(image->ms_imgs); ++i) {
756 panvk_DestroyImage(_device, image->ms_imgs[i], pAllocator);
757 }
758 }
759
760 if (image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
761 uint64_t va_range = panvk_image_get_sparse_size(image);
762
763 struct pan_kmod_vm_op unmap = {
764 .type = PAN_KMOD_VM_OP_TYPE_UNMAP,
765 .va = {
766 .start = image->sparse.device_address,
767 .size = va_range,
768 },
769 };
770 ASSERTED int ret = pan_kmod_vm_bind(
771 device->kmod.vm, PAN_KMOD_VM_OP_MODE_IMMEDIATE, &unmap, 1);
772 assert(!ret);
773
774 panvk_as_free(device, &device->as.heap, image->sparse.device_address,
775 va_range);
776 }
777
778 vk_image_destroy(&device->vk, pAllocator, &image->vk);
779 }
780
781 static void
782 get_image_subresource_layout(const struct panvk_image *image,
783 const VkImageSubresource2 *subres2,
784 VkSubresourceLayout2 *layout2)
785 {
786 const VkImageSubresource *subres = &subres2->imageSubresource;
787 VkSubresourceLayout *layout = &layout2->subresourceLayout;
788 unsigned plane = panvk_plane_index(image, subres->aspectMask);
789 assert(plane < PANVK_MAX_PLANES);
790
791 const struct pan_image_slice_layout *slice_layout =
792 &image->planes[plane].plane.layout.slices[subres->mipLevel];
793
794 layout->offset =
795 slice_layout->offset_B +
796 (subres->arrayLayer * image->planes[plane].plane.layout.array_stride_B);
797 layout->size = slice_layout->size_B;
798 layout->arrayPitch = image->planes[plane].plane.layout.array_stride_B;
799
800 if (drm_is_afbc(image->vk.drm_format_mod)) {
801 /* row/depth pitch expressed in (AFBC superblocks * payload size). */
802 layout->rowPitch = pan_image_get_wsi_row_pitch(
803 &image->planes[plane].image, plane, subres->mipLevel);
804 layout->depthPitch = slice_layout->afbc.surface_stride_B;
805 } else {
806 layout->rowPitch = slice_layout->tiled_or_linear.row_stride_B;
807 layout->depthPitch = slice_layout->tiled_or_linear.surface_stride_B;
808 }
809
810 VkSubresourceHostMemcpySize *memcpy_size =
811 vk_find_struct(layout2->pNext, SUBRESOURCE_HOST_MEMCPY_SIZE);
812 if (memcpy_size) {
813 /* When copying to/from a D24S8 image, we can't use the normal memcpy
814 * path because we need to interleave the depth/stencil components. For
815 * the stencil aspect, the copied data only needs 1 byte/px instead of 4.
816 */
817 if (image->vk.format == VK_FORMAT_D24_UNORM_S8_UINT &&
818 image->plane_count == 1) {
819 switch (subres->aspectMask) {
820 case VK_IMAGE_ASPECT_DEPTH_BIT:
821 memcpy_size->size = slice_layout->size_B;
822 break;
823 case VK_IMAGE_ASPECT_STENCIL_BIT:
824 memcpy_size->size = slice_layout->size_B / 4;
825 break;
826 default:
827 UNREACHABLE("invalid aspect");
828 }
829 } else {
830 memcpy_size->size = slice_layout->size_B;
831 }
832 }
833 }
834
835 VKAPI_ATTR void VKAPI_CALL
836 panvk_GetImageSubresourceLayout2(VkDevice device, VkImage image,
837 const VkImageSubresource2 *pSubresource,
838 VkSubresourceLayout2 *pLayout)
839 {
840 VK_FROM_HANDLE(panvk_image, img, image);
841
842 get_image_subresource_layout(img, pSubresource, pLayout);
843 }
844
845 VKAPI_ATTR void VKAPI_CALL
846 panvk_GetDeviceImageSubresourceLayoutKHR(
847 VkDevice device, const VkDeviceImageSubresourceInfoKHR *pInfo,
848 VkSubresourceLayout2KHR *pLayout)
849 {
850 VK_FROM_HANDLE(panvk_device, dev, device);
851 struct panvk_image image = {0};
852
853 vk_image_init(&dev->vk, &image.vk, pInfo->pCreateInfo);
854 panvk_image_init(&image, pInfo->pCreateInfo);
855 get_image_subresource_layout(&image, pInfo->pSubresource, pLayout);
856 vk_image_finish(&image.vk);
857 }
858
859 static uint64_t
860 panvk_image_get_sparse_binding_granularity(struct panvk_image *image)
861 {
862 struct panvk_device *dev = to_panvk_device(image->vk.base.device);
863
864 assert(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT);
865
866 if (image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT)
867 return panvk_get_sparse_block_desc(image->vk.image_type, image->vk.format).size_B;
868
869 return panvk_get_gpu_page_size(dev);
870 }
871
872 static void
873 append_ms_to_ss_memory_reqs(VkMemoryRequirements2 *pMemoryRequirements,
874 const VkMemoryRequirements2 *append)
875 {
876 pMemoryRequirements->memoryRequirements.alignment =
877 MAX2(pMemoryRequirements->memoryRequirements.alignment,
878 append->memoryRequirements.alignment);
879 /* After the previous images, align this images start properly. */
880 pMemoryRequirements->memoryRequirements.size =
881 align(pMemoryRequirements->memoryRequirements.size,
882 append->memoryRequirements.alignment);
883 pMemoryRequirements->memoryRequirements.size +=
884 append->memoryRequirements.size;
885 pMemoryRequirements->memoryRequirements.memoryTypeBits &=
886 append->memoryRequirements.memoryTypeBits;
887 assert(pMemoryRequirements->memoryRequirements.memoryTypeBits != 0);
888 }
889
890 VKAPI_ATTR void VKAPI_CALL
891 panvk_GetImageMemoryRequirements2(VkDevice device,
892 const VkImageMemoryRequirementsInfo2 *pInfo,
893 VkMemoryRequirements2 *pMemoryRequirements)
894 {
895 VK_FROM_HANDLE(panvk_image, image, pInfo->image);
896 struct panvk_physical_device *phys_dev =
897 to_panvk_physical_device(image->vk.base.device->physical);
898
899 /* For sparse resources alignment specifies binding granularity, rather than
900 * the alignment requirement. It's up to us to satisfy the alignment
901 * requirement when allocating the VA range.
902 */
903 const uint64_t alignment =
904 image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT
905 ? panvk_image_get_sparse_binding_granularity(image)
906 : 4096;
907 const VkImagePlaneMemoryRequirementsInfo *plane_info =
908 vk_find_struct_const(pInfo->pNext, IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO);
909 const bool disjoint = is_disjoint(image);
910 const VkImageAspectFlags aspects =
911 plane_info ? plane_info->planeAspect : image->vk.aspects;
912 uint8_t plane = panvk_plane_index(image, aspects);
913 const uint64_t size_non_sparse =
914 disjoint ? image->planes[plane].plane.layout.data_size_B :
915 panvk_image_get_total_size(image);
916 const uint64_t size =
917 image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT
918 ? align64(size_non_sparse, alignment)
919 : size_non_sparse;
920
921 pMemoryRequirements->memoryRequirements.memoryTypeBits =
922 BITFIELD_MASK(phys_dev->memory.type_count);
923 pMemoryRequirements->memoryRequirements.alignment = alignment;
924 pMemoryRequirements->memoryRequirements.size = size;
925
926 vk_foreach_struct_const(ext, pMemoryRequirements->pNext) {
927 switch (ext->sType) {
928 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
929 VkMemoryDedicatedRequirements *dedicated = (void *)ext;
930 dedicated->requiresDedicatedAllocation =
931 vk_image_is_android_hardware_buffer(&image->vk);
932 dedicated->prefersDedicatedAllocation = dedicated->requiresDedicatedAllocation;
933 break;
934 }
935 default:
936 vk_debug_ignored_stype(ext->sType);
937 break;
938 }
939 }
940
941 if (image->vk.create_flags &
942 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT) {
943 for (uint32_t i = 0; i < ARRAY_SIZE(image->ms_imgs); ++i) {
944 if (image->ms_imgs[i] == VK_NULL_HANDLE)
945 continue;
946
947 VkImageMemoryRequirementsInfo2 info = *pInfo;
948 info.image = image->ms_imgs[i];
949
950 VkMemoryRequirements2 sub_reqs_2 = {
951 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
952 .memoryRequirements = {},
953 };
954 panvk_GetImageMemoryRequirements2(device, &info, &sub_reqs_2);
955 append_ms_to_ss_memory_reqs(pMemoryRequirements, &sub_reqs_2);
956 }
957 }
958 }
959
960 VKAPI_ATTR void VKAPI_CALL
961 panvk_GetDeviceImageMemoryRequirements(VkDevice device,
962 const VkDeviceImageMemoryRequirements *pInfo,
963 VkMemoryRequirements2 *pMemoryRequirements)
964 {
965 VK_FROM_HANDLE(panvk_device, dev, device);
966
967 /* Make a copy so we can turn off the ms2ss flag. */
968 VkDeviceImageMemoryRequirements info = *pInfo;
969 VkImageCreateInfo create_info = *(pInfo->pCreateInfo);
970 create_info.flags &=
971 ~VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
972 info.pCreateInfo = &create_info;
973
974 struct panvk_image image;
975 vk_image_init(&dev->vk, &image.vk, &create_info);
976 panvk_image_init(&image, &create_info);
977
978 VkImageMemoryRequirementsInfo2 info2 = {
979 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
980 .image = panvk_image_to_handle(&image),
981 };
982 panvk_GetImageMemoryRequirements2(device, &info2, pMemoryRequirements);
983 vk_image_finish(&image.vk);
984
985 if (pInfo->pCreateInfo->flags &
986 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT) {
987 for (uint32_t msaa_idx = 0; msaa_idx < ARRAY_SIZE(image.ms_imgs);
988 ++msaa_idx) {
989 /* idx 0 has sample count 2, 1 has sample count 4, ... */
990 create_info.samples = 1 << (msaa_idx + 1);
991 create_info.flags &=
992 ~VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
993
994 VkMemoryRequirements2 msaa_reqs = {
995 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
996 .memoryRequirements = {},
997 };
998 panvk_GetDeviceImageMemoryRequirements(device, &info, &msaa_reqs);
999 append_ms_to_ss_memory_reqs(pMemoryRequirements, &msaa_reqs);
1000 }
1001 }
1002 }
1003
1004 /* See Vulkan spec, 35.4.3. Standard Sparse Image Block Shapes for details */
1005 enum {
1006 STANDARD_SPARSE_BLOCK_SIZE_B = 65536,
1007 };
1008
1009 /* Sparse block extents, in texel blocks, single sample.
1010 * Indexed by log2(texel block size in bytes).
1011 * See Vulkan spec, 35.4.3. Standard Sparse Image Block Shapes for details. */
1012 static struct VkExtent3D standard_sparse_2d_blocks[] = {
1013 /* 1 */ {256, 256, 1},
1014 /* 2 */ {256, 128, 1},
1015 /* 4 */ {128, 128, 1},
1016 /* 8 */ {128, 64, 1},
1017 /* 16 */ {64, 64, 1},
1018 };
1019
1020 struct panvk_sparse_block_desc
1021 panvk_get_sparse_block_desc(VkImageType type, VkFormat format)
1022 {
1023 const struct util_format_description *fmt_desc = vk_format_description(format);
1024
1025 uint32_t texel_block_size_B = fmt_desc->block.bits / 8;
1026
1027 switch (type) {
1028 case VK_IMAGE_TYPE_2D:
1029 case VK_IMAGE_TYPE_3D: {
1030 if (!util_is_power_of_two_nonzero(texel_block_size_B))
1031 break;
1032
1033 uint32_t texel_block_size_B_log2 = util_logbase2(texel_block_size_B);
1034 if (texel_block_size_B_log2 >= ARRAY_SIZE(standard_sparse_2d_blocks))
1035 break;
1036 struct VkExtent3D extent = standard_sparse_2d_blocks[texel_block_size_B_log2];
1037
1038 assert(extent.width * extent.height * extent.depth * texel_block_size_B == STANDARD_SPARSE_BLOCK_SIZE_B);
1039
1040 extent.width *= fmt_desc->block.width;
1041 extent.height *= fmt_desc->block.height;
1042 extent.depth *= fmt_desc->block.depth;
1043
1044 return (struct panvk_sparse_block_desc){
1045 .extent = extent,
1046 .size_B = STANDARD_SPARSE_BLOCK_SIZE_B,
1047 .standard = type == VK_IMAGE_TYPE_2D,
1048 };
1049 }
1050
1051 default:
1052 break;
1053 }
1054
1055 return (struct panvk_sparse_block_desc){};
1056 }
1057
1058 VkSparseImageFormatProperties
1059 panvk_get_sparse_image_fmt_props(VkImageType type, VkFormat format)
1060 {
1061 struct panvk_sparse_block_desc sblock_desc = panvk_get_sparse_block_desc(type, format);
1062 if (!panvk_sparse_block_is_valid(sblock_desc))
1063 return (VkSparseImageFormatProperties){};
1064
1065 VkSparseImageFormatProperties fmt_props = {};
1066 fmt_props.aspectMask = vk_format_aspects(format);
1067 fmt_props.imageGranularity = sblock_desc.extent;
1068 if (!sblock_desc.standard)
1069 fmt_props.flags |= VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT;
1070 return fmt_props;
1071 }
1072
1073 VKAPI_ATTR void VKAPI_CALL
1074 panvk_GetImageSparseMemoryRequirements2(
1075 VkDevice device, const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1076 uint32_t *pSparseMemoryRequirementCount,
1077 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1078 {
1079 VK_FROM_HANDLE(panvk_image, image, pInfo->image);
1080 VK_OUTARRAY_MAKE_TYPED(VkSparseImageMemoryRequirements2, out, pSparseMemoryRequirements,
1081 pSparseMemoryRequirementCount);
1082
1083 if (!(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT))
1084 return;
1085
1086 /* We only support single-plane images right now. See
1087 * https://gitlab.freedesktop.org/panfrost/mesa/-/issues/243 details. */
1088 unsigned plane_idx = 0;
1089
1090 struct panvk_sparse_block_desc sblock = panvk_get_sparse_block_desc(image->vk.image_type, image->vk.format);
1091 assert(panvk_sparse_block_is_valid(sblock));
1092
1093 struct panvk_image_plane *plane = &image->planes[plane_idx];
1094
1095 unsigned mip_tail_first_lod = 0;
1096 uint64_t mip_tail_begin = 0;
1097 for (unsigned level = 0; level < plane->image.props.nr_slices; level++) {
1098 uint64_t offset = plane->plane.layout.slices[level].offset_B;
1099 if (!util_is_aligned(offset, sblock.size_B))
1100 break;
1101 mip_tail_first_lod = level;
1102 mip_tail_begin = offset;
1103 }
1104
1105 uint64_t mip_tail_end = plane->plane.layout.array_stride_B;
1106
1107 vk_outarray_append_typed(VkSparseImageMemoryRequirements2, &out, p) {
1108 p->memoryRequirements = (VkSparseImageMemoryRequirements){
1109 .formatProperties = panvk_get_sparse_image_fmt_props(image->vk.image_type, image->vk.format),
1110 .imageMipTailFirstLod = mip_tail_first_lod,
1111 .imageMipTailSize = mip_tail_end - mip_tail_begin,
1112 .imageMipTailOffset = mip_tail_begin,
1113 .imageMipTailStride = plane->plane.layout.array_stride_B,
1114 };
1115 }
1116 }
1117
1118 VKAPI_ATTR void VKAPI_CALL
1119 panvk_GetDeviceImageSparseMemoryRequirements(VkDevice device,
1120 const VkDeviceImageMemoryRequirements *pInfo,
1121 uint32_t *pSparseMemoryRequirementCount,
1122 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1123 {
1124 VK_FROM_HANDLE(panvk_device, dev, device);
1125
1126 struct panvk_image image;
1127 vk_image_init(&dev->vk, &image.vk, pInfo->pCreateInfo);
1128 panvk_image_init(&image, pInfo->pCreateInfo);
1129
1130 VkImageSparseMemoryRequirementsInfo2 info2 = {
1131 .sType = VK_STRUCTURE_TYPE_IMAGE_SPARSE_MEMORY_REQUIREMENTS_INFO_2,
1132 .image = panvk_image_to_handle(&image),
1133 };
1134 panvk_GetImageSparseMemoryRequirements2(device, &info2,
1135 pSparseMemoryRequirementCount, pSparseMemoryRequirements);
1136 vk_image_finish(&image.vk);
1137 }
1138
1139 static VkResult panvk_image_bind(struct panvk_device *dev,
1140 const VkBindImageMemoryInfo *bind_info);
1141
1142 static void
1143 bind_ms_images(struct panvk_device *dev, const VkBindImageMemoryInfo *bind_info)
1144 {
1145 VK_FROM_HANDLE(panvk_image, image, bind_info->image);
1146
1147 uint64_t total_size = 0;
1148 {
1149 VkImageMemoryRequirementsInfo2 reqs_info = {
1150 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
1151 .image = bind_info->image,
1152 };
1153 VkMemoryRequirements2 reqs2 = {
1154 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
1155 .memoryRequirements = {},
1156 };
1157 panvk_GetImageMemoryRequirements2(panvk_device_to_handle(dev), &reqs_info,
1158 &reqs2);
1159
1160 total_size = reqs2.memoryRequirements.size;
1161 }
1162
1163 uint64_t sub_sz[ARRAY_SIZE(image->ms_imgs)];
1164 uint64_t sub_al[ARRAY_SIZE(image->ms_imgs)];
1165
1166 for (uint32_t i = 0; i < ARRAY_SIZE(image->ms_imgs); ++i) {
1167 if (image->ms_imgs[i] == VK_NULL_HANDLE) {
1168 sub_sz[i] = 0;
1169 sub_al[i] = 1;
1170 continue;
1171 }
1172
1173 VkImageMemoryRequirementsInfo2 reqs_info = {
1174 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
1175 .image = image->ms_imgs[i],
1176 };
1177 VkMemoryRequirements2 reqs2 = {
1178 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
1179 .memoryRequirements = {},
1180 };
1181 panvk_GetImageMemoryRequirements2(panvk_device_to_handle(dev), &reqs_info,
1182 &reqs2);
1183
1184 sub_sz[i] = reqs2.memoryRequirements.size;
1185 sub_al[i] = reqs2.memoryRequirements.alignment;
1186 }
1187
1188 /*
1189 * sub_imgs_aligned_size
1190 * ----------------------------
1191 * [ base, sub_0, sub_1, sub_2, sub_3 ]
1192 * -->-> -->-> ... ------>
1193 * sz a sz a size only
1194 */
1195 uint64_t sub_imgs_aligned_size = 0;
1196 for (uint32_t i = 0; i < ARRAY_SIZE(image->ms_imgs); ++i) {
1197 sub_imgs_aligned_size += sub_sz[i];
1198 if (i < ARRAY_SIZE(image->ms_imgs) - 1) {
1199 sub_imgs_aligned_size = align(sub_imgs_aligned_size, sub_al[i + 1]);
1200 }
1201 }
1202
1203 uint64_t sub_image_offset =
1204 bind_info->memoryOffset + total_size - sub_imgs_aligned_size;
1205
1206 for (uint32_t i = 0; i < ARRAY_SIZE(image->ms_imgs); ++i) {
1207 if (image->ms_imgs[i] == VK_NULL_HANDLE)
1208 continue;
1209
1210 sub_image_offset = align(sub_image_offset, sub_al[i]);
1211
1212 VkBindImageMemoryInfo sub_bind_info = {
1213 .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO,
1214 .image = image->ms_imgs[i],
1215 .memory = bind_info->memory,
1216 .memoryOffset = sub_image_offset,
1217 };
1218
1219 ASSERTED const VkResult res = panvk_image_bind(dev, &sub_bind_info);
1220 assert(res == VK_SUCCESS);
1221
1222 sub_image_offset += sub_sz[i];
1223 }
1224 }
1225
1226 static VkResult
1227 panvk_image_bind(struct panvk_device *dev,
1228 const VkBindImageMemoryInfo *bind_info)
1229 {
1230 VK_FROM_HANDLE(panvk_image, image, bind_info->image);
1231 VK_FROM_HANDLE(panvk_device_memory, mem, bind_info->memory);
1232 uint64_t offset = bind_info->memoryOffset;
1233
1234 assert(!(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT));
1235
1236 if (!mem) {
1237 VkDeviceMemory mem_handle;
1238 #ifdef VK_USE_PLATFORM_ANDROID_KHR
1239 VkResult result =
1240 panvk_android_get_wsi_memory(dev, bind_info, &mem_handle);
1241 if (result != VK_SUCCESS)
1242 return result;
1243 #else
1244 const VkBindImageMemorySwapchainInfoKHR *swapchain_info =
1245 vk_find_struct_const(bind_info->pNext,
1246 BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR);
1247 assert(swapchain_info && swapchain_info->swapchain != VK_NULL_HANDLE);
1248 mem_handle = wsi_common_get_memory(swapchain_info->swapchain,
1249 swapchain_info->imageIndex);
1250 #endif
1251 mem = panvk_device_memory_from_handle(mem_handle);
1252 offset = 0;
1253 }
1254
1255 assert(mem);
1256 if (is_disjoint(image)) {
1257 const VkBindImagePlaneMemoryInfo *plane_info =
1258 vk_find_struct_const(bind_info->pNext, BIND_IMAGE_PLANE_MEMORY_INFO);
1259 const uint8_t plane =
1260 panvk_plane_index(image, plane_info->planeAspect);
1261 panvk_image_plane_bind_mem(dev, &image->planes[plane], mem, offset);
1262 } else {
1263 for (unsigned plane = 0; plane < image->plane_count; plane++)
1264 panvk_image_plane_bind_mem(dev, &image->planes[plane], mem, offset);
1265 }
1266
1267 if (!!(image->vk.create_flags &
1268 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT))
1269 bind_ms_images(dev, bind_info);
1270
1271 return VK_SUCCESS;
1272 }
1273
1274 VKAPI_ATTR VkResult VKAPI_CALL
1275 panvk_BindImageMemory2(VkDevice device, uint32_t bindInfoCount,
1276 const VkBindImageMemoryInfo *pBindInfos)
1277 {
1278 VK_FROM_HANDLE(panvk_device, dev, device);
1279 VkResult result = VK_SUCCESS;
1280
1281 for (uint32_t i = 0; i < bindInfoCount; i++) {
1282 const VkBindMemoryStatus *bind_status =
1283 vk_find_struct_const(&pBindInfos[i], BIND_MEMORY_STATUS);
1284 VkResult bind_result = panvk_image_bind(dev, &pBindInfos[i]);
1285 if (bind_status)
1286 *bind_status->pResult = bind_result;
1287 if (bind_result != VK_SUCCESS)
1288 result = bind_result;
1289 }
1290
1291 return result;
1292 }