Debian Codegraph @

src/asahi/vulkan/hk_image.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * SPDX-License-Identifier: MIT
6 */
7 #include "hk_image.h"
8 #include "asahi/layout/layout.h"
9 #include "drm-uapi/drm_fourcc.h"
10 #include "util/bitscan.h"
11 #include "util/format/u_format.h"
12 #include "util/format/u_formats.h"
13 #include "util/macros.h"
14 #include "util/u_math.h"
15 #include "vulkan/vulkan_core.h"
16
17 #include "agx_bo.h"
18 #include "hk_buffer.h"
19 #include "hk_device.h"
20 #include "hk_device_memory.h"
21 #include "hk_entrypoints.h"
22 #include "hk_physical_device.h"
23
24 #include "vk_format.h"
25
26 /* Minimum alignment encodable for our descriptors. The hardware texture/PBE
27 * descriptors require 16-byte alignment. Our software PBE atomic descriptor
28 * requires 128-byte alignment, but we could relax that one if we wanted.
29 */
30 #define HK_PLANE_ALIGN_B 128
31
32 /* However, exposing the standard sparse block sizes requires using the standard
33 * alignment 65k.
34 */
35 #define HK_SPARSE_ALIGN_B 65536
36
37 static VkFormatFeatureFlags2
38 hk_get_image_plane_format_features(struct hk_physical_device *pdev,
39 VkFormat vk_format, VkImageTiling tiling)
40 {
41 VkFormatFeatureFlags2 features = 0;
42
43 /* This optional format needs hacks for opaque black, so hide for
44 * performance. We might specially enable this for Proton / behind a driconf.
45 */
46 if (vk_format == VK_FORMAT_A8_UNORM_KHR)
47 return 0;
48
49 enum pipe_format p_format = hk_format_to_pipe_format(vk_format);
50 if (p_format == PIPE_FORMAT_NONE)
51 return 0;
52
53 /* NPOT formats only supported for texel buffers */
54 if (!util_is_power_of_two_nonzero(util_format_get_blocksize(p_format)))
55 return 0;
56
57 /* Linear block-compressed images are all sorts of problematic, not sure
58 * if AGX even supports them. Don't try.
59 */
60 if (util_format_is_compressed(p_format) && tiling != VK_IMAGE_TILING_OPTIMAL)
61 return 0;
62
63 if (ail_pixel_format[p_format].texturable) {
64 features |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT;
65 features |= VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
66
67 /* We can sample integer formats but it doesn't make sense to linearly
68 * filter them.
69 */
70 if (!util_format_is_pure_integer(p_format)) {
71 features |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
72 }
73
74 if (vk_format_has_depth(vk_format)) {
75 features |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_DEPTH_COMPARISON_BIT;
76 }
77 }
78
79 if (ail_pixel_format[p_format].renderable) {
80 features |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
81 VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT |
82 VK_FORMAT_FEATURE_2_BLIT_DST_BIT |
83 VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT |
84 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT |
85 VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT;
86 }
87
88 if (vk_format_is_depth_or_stencil(vk_format)) {
89 if (!(p_format == PIPE_FORMAT_Z32_FLOAT ||
90 p_format == PIPE_FORMAT_S8_UINT ||
91 p_format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ||
92 p_format == PIPE_FORMAT_Z16_UNORM) ||
93 tiling == VK_IMAGE_TILING_LINEAR)
94 return 0;
95
96 features |= VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT;
97 }
98
99 /* Our image atomic lowering doesn't bother to handle linear */
100 if ((p_format == PIPE_FORMAT_R32_UINT || p_format == PIPE_FORMAT_R32_SINT) &&
101 tiling == VK_IMAGE_TILING_OPTIMAL) {
102
103 features |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
104 }
105
106 if (features != 0) {
107 features |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT;
108 features |= VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
109 features |= VK_FORMAT_FEATURE_2_HOST_IMAGE_TRANSFER_BIT_EXT;
110 }
111
112 return features;
113 }
114
115 VkFormatFeatureFlags2
116 hk_get_image_format_features(struct hk_physical_device *pdev,
117 VkFormat vk_format, VkImageTiling tiling)
118 {
119 const struct vk_format_ycbcr_info *ycbcr_info =
120 vk_format_get_ycbcr_info(vk_format);
121 if (ycbcr_info == NULL)
122 return hk_get_image_plane_format_features(pdev, vk_format, tiling);
123
124 /* For multi-plane, we get the feature flags of each plane separately,
125 * then take their intersection as the overall format feature flags
126 */
127 VkFormatFeatureFlags2 features = ~0ull;
128 bool cosited_chroma = false;
129 for (uint8_t plane = 0; plane < ycbcr_info->n_planes; plane++) {
130 const struct vk_format_ycbcr_plane *plane_info =
131 &ycbcr_info->planes[plane];
132 features &=
133 hk_get_image_plane_format_features(pdev, plane_info->format, tiling);
134 if (plane_info->denominator_scales[0] > 1 ||
135 plane_info->denominator_scales[1] > 1)
136 cosited_chroma = true;
137 }
138 if (features == 0)
139 return 0;
140
141 /* Uh... We really should be able to sample from YCbCr */
142 assert(features & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT);
143 assert(features & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT);
144
145 /* These aren't allowed for YCbCr formats */
146 features &=
147 ~(VK_FORMAT_FEATURE_2_BLIT_SRC_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT |
148 VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
149 VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT |
150 VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT);
151
152 /* This is supported on all YCbCr formats */
153 features |=
154 VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT;
155
156 if (ycbcr_info->n_planes > 1) {
157 /* DISJOINT_BIT implies that each plane has its own separate binding,
158 * while SEPARATE_RECONSTRUCTION_FILTER_BIT implies that luma and chroma
159 * each have their own, separate filters, so these two bits make sense
160 * for multi-planar formats only.
161 *
162 * For MIDPOINT_CHROMA_SAMPLES_BIT, NVIDIA HW on single-plane interleaved
163 * YCbCr defaults to COSITED_EVEN, which is inaccurate and fails tests.
164 * This can be fixed with a NIR tweak but for now, we only enable this bit
165 * for multi-plane formats. See Issue #9525 on the mesa/main tracker.
166 */
167 features |=
168 VK_FORMAT_FEATURE_DISJOINT_BIT |
169 VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT |
170 VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
171 }
172
173 if (cosited_chroma)
174 features |= VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT;
175
176 return features;
177 }
178
179 static VkFormatFeatureFlags2
180 vk_image_usage_to_format_features(VkImageUsageFlagBits usage_flag)
181 {
182 assert(util_bitcount(usage_flag) == 1);
183 switch (usage_flag) {
184 case VK_IMAGE_USAGE_TRANSFER_SRC_BIT:
185 return VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
186 VK_FORMAT_FEATURE_BLIT_SRC_BIT;
187 case VK_IMAGE_USAGE_TRANSFER_DST_BIT:
188 return VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
189 VK_FORMAT_FEATURE_BLIT_DST_BIT;
190 case VK_IMAGE_USAGE_SAMPLED_BIT:
191 return VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT;
192 case VK_IMAGE_USAGE_STORAGE_BIT:
193 return VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
194 case VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT:
195 return VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT;
196 case VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT:
197 return VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT;
198 default:
199 return 0;
200 }
201 }
202
203 static bool
204 hk_can_compress(const struct agx_device *dev, VkFormat format, unsigned plane,
205 unsigned width, unsigned height, unsigned samples,
206 VkImageCreateFlagBits flags, VkImageUsageFlagBits usage,
207 const void *pNext)
208 {
209 const struct vk_format_ycbcr_info *ycbcr_info =
210 vk_format_get_ycbcr_info(format);
211
212 if (ycbcr_info) {
213 format = ycbcr_info->planes[plane].format;
214 width /= ycbcr_info->planes[plane].denominator_scales[0];
215 height /= ycbcr_info->planes[plane].denominator_scales[0];
216 } else if (format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
217 format = (plane == 0) ? VK_FORMAT_D32_SFLOAT : VK_FORMAT_S8_UINT;
218 }
219
220 /* Allow disabling compression for debugging */
221 if (dev->debug & AGX_DBG_NOCOMPRESS)
222 return false;
223
224 /* TODO: Handle compression with sparse. This should be doable but it's a bit
225 * subtle. Correctness first.
226 */
227 if (flags & (VK_IMAGE_CREATE_SPARSE_ALIASED_BIT |
228 VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
229 VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT)) {
230 perf_debug_dev(dev, "No compression: sparse");
231 return false;
232 }
233
234 /* Image compression is not (yet?) supported with host image copies,
235 * although the vendor driver does support something similar if I recall.
236 * Compression is not supported in hardware for storage images or mutable
237 * formats in general.
238 *
239 * Feedback loops are problematic with compression. The GL driver bans them.
240 * Interestingly, the relevant CTS tests pass on G13G and G14C, but not on
241 * G13D. For now, conservatively ban compression with feedback loops.
242 */
243 if (usage &
244 (VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT | VK_IMAGE_USAGE_STORAGE_BIT |
245 VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT)) {
246
247 perf_debug_dev(
248 dev, "No compression: incompatible usage -%s%s%s",
249 (usage & VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT) ? " host-transfer" : "",
250 (usage & VK_IMAGE_USAGE_STORAGE_BIT) ? " storage" : "",
251 (usage & VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT)
252 ? " feedback-loop"
253 : "");
254 return false;
255 }
256
257 enum pipe_format p_format = hk_format_to_pipe_format(format);
258
259 /* Check for format compatibility if mutability is enabled. */
260 if (flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
261 const struct VkImageFormatListCreateInfo *format_list =
262 (void *)vk_find_struct_const(pNext, IMAGE_FORMAT_LIST_CREATE_INFO);
263
264 if (!format_list || format_list->viewFormatCount == 0)
265 return false;
266
267 for (unsigned i = 0; i < format_list->viewFormatCount; ++i) {
268 if (format_list->pViewFormats[i] == VK_FORMAT_UNDEFINED)
269 continue;
270
271 enum pipe_format view_format =
272 hk_format_to_pipe_format(format_list->pViewFormats[i]);
273
274 if (!ail_formats_compatible(p_format, view_format)) {
275 perf_debug_dev(dev, "No compression: incompatible image view");
276 return false;
277 }
278 }
279 }
280
281 if (!ail_can_compress(p_format, width, height, samples)) {
282 perf_debug_dev(dev, "No compression: invalid layout %s %ux%ux%u",
283 util_format_short_name(p_format), width, height, samples);
284 return false;
285 }
286
287 return true;
288 }
289
290 bool
291 hk_can_compress_format(const struct agx_device *dev, VkFormat format)
292 {
293 /* Check compressability of a sufficiently large image of the same
294 * format, since we don't have dimensions here. This is lossy for
295 * small images, but that's ok.
296 *
297 * Likewise, we do not set flags as flags only disable compression.
298 */
299 return hk_can_compress(dev, format, 0, 64, 64, 1, 0, 0, NULL);
300 }
301
302 VKAPI_ATTR VkResult VKAPI_CALL
303 hk_GetPhysicalDeviceImageFormatProperties2(
304 VkPhysicalDevice physicalDevice,
305 const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
306 VkImageFormatProperties2 *pImageFormatProperties)
307 {
308 VK_FROM_HANDLE(hk_physical_device, pdev, physicalDevice);
309
310 const VkPhysicalDeviceExternalImageFormatInfo *external_info =
311 vk_find_struct_const(pImageFormatInfo->pNext,
312 PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO);
313
314 /* Initialize to zero in case we return VK_ERROR_FORMAT_NOT_SUPPORTED */
315 memset(&pImageFormatProperties->imageFormatProperties, 0,
316 sizeof(pImageFormatProperties->imageFormatProperties));
317
318 const struct vk_format_ycbcr_info *ycbcr_info =
319 vk_format_get_ycbcr_info(pImageFormatInfo->format);
320
321 /* For the purposes of these checks, we don't care about all the extra
322 * YCbCr features and we just want the accumulation of features available
323 * to all planes of the given format.
324 */
325 VkFormatFeatureFlags2 features;
326 if (ycbcr_info == NULL) {
327 features = hk_get_image_plane_format_features(
328 pdev, pImageFormatInfo->format, pImageFormatInfo->tiling);
329 } else {
330 features = ~0ull;
331 assert(ycbcr_info->n_planes > 0);
332 for (uint8_t plane = 0; plane < ycbcr_info->n_planes; plane++) {
333 const VkFormat plane_format = ycbcr_info->planes[plane].format;
334 features &= hk_get_image_plane_format_features(
335 pdev, plane_format, pImageFormatInfo->tiling);
336 }
337 }
338
339 /* Sparse + host-image-copy doesn't make sense. Forbid it. */
340 if (pImageFormatInfo->flags & (VK_IMAGE_CREATE_SPARSE_ALIASED_BIT |
341 VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
342 VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT)) {
343 features &= ~VK_FORMAT_FEATURE_2_HOST_IMAGE_TRANSFER_BIT_EXT;
344
345 /* We also do not support sparse RGB32 uniform texel buffers, as we do not
346 * have residency query support for this interaction and there are no
347 * known use cases.
348 */
349 if (!util_is_power_of_two_nonzero(
350 vk_format_get_blocksize(pImageFormatInfo->format))) {
351
352 return VK_ERROR_FORMAT_NOT_SUPPORTED;
353 }
354 }
355
356 if (pImageFormatInfo->flags & (VK_IMAGE_CREATE_SPARSE_ALIASED_BIT |
357 VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
358 VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) &&
359 (pImageFormatInfo->usage & VK_IMAGE_USAGE_HOST_TRANSFER_BIT))
360 return VK_ERROR_FORMAT_NOT_SUPPORTED;
361
362 if (features == 0)
363 return VK_ERROR_FORMAT_NOT_SUPPORTED;
364
365 if (pImageFormatInfo->tiling == VK_IMAGE_TILING_LINEAR &&
366 pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
367 return VK_ERROR_FORMAT_NOT_SUPPORTED;
368
369 if (pImageFormatInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT &&
370 pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
371 return VK_ERROR_FORMAT_NOT_SUPPORTED;
372
373 if (ycbcr_info && pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
374 return VK_ERROR_FORMAT_NOT_SUPPORTED;
375
376 /* From the Vulkan 1.3.279 spec:
377 *
378 * VUID-VkImageCreateInfo-tiling-04121
379 *
380 * "If tiling is VK_IMAGE_TILING_LINEAR, flags must not contain
381 * VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT"
382 *
383 * VUID-VkImageCreateInfo-imageType-00970
384 *
385 * "If imageType is VK_IMAGE_TYPE_1D, flags must not contain
386 * VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT"
387 */
388 if (pImageFormatInfo->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT &&
389 (pImageFormatInfo->type == VK_IMAGE_TYPE_1D ||
390 pImageFormatInfo->tiling == VK_IMAGE_TILING_LINEAR))
391 return VK_ERROR_FORMAT_NOT_SUPPORTED;
392
393 /* From the Vulkan 1.3.279 spec:
394 *
395 * VUID-VkImageCreateInfo-flags-09403
396 *
397 * "If flags contains VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT, flags
398 * must not include VK_IMAGE_CREATE_SPARSE_ALIASED_BIT,
399 * VK_IMAGE_CREATE_SPARSE_BINDING_BIT, or
400 * VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT"
401 */
402 if ((pImageFormatInfo->flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT) &&
403 (pImageFormatInfo->flags & (VK_IMAGE_CREATE_SPARSE_ALIASED_BIT |
404 VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
405 VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT)))
406 return VK_ERROR_FORMAT_NOT_SUPPORTED;
407
408 /* Multiplane formats are not supported with sparse residency. This has no
409 * known use cases and is forbidden in other APIs.
410 *
411 * Neither is depth/stencil: this is a hardware limitation on G13. Hardware
412 * support is added with G14, but that's not implemented yet. We could
413 * emulate on G13 but it'd be fiddly. Fortunately, vkd3d-proton doesn't need
414 * sparse depth, as RADV has the same limitation!
415 */
416 if ((ycbcr_info ||
417 vk_format_is_depth_or_stencil(pImageFormatInfo->format)) &&
418 (pImageFormatInfo->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT)) {
419 return VK_ERROR_FORMAT_NOT_SUPPORTED;
420 }
421
422 const uint32_t max_dim = 16384;
423 VkExtent3D maxExtent;
424 uint32_t maxArraySize;
425 switch (pImageFormatInfo->type) {
426 case VK_IMAGE_TYPE_1D:
427 maxExtent = (VkExtent3D){max_dim, 1, 1};
428 maxArraySize = 2048;
429 break;
430 case VK_IMAGE_TYPE_2D:
431 maxExtent = (VkExtent3D){max_dim, max_dim, 1};
432 maxArraySize = 2048;
433 break;
434 case VK_IMAGE_TYPE_3D:
435 maxExtent = (VkExtent3D){max_dim, max_dim, max_dim};
436 maxArraySize = 1;
437 break;
438 default:
439 UNREACHABLE("Invalid image type");
440 }
441 if (pImageFormatInfo->tiling == VK_IMAGE_TILING_LINEAR)
442 maxArraySize = 1;
443
444 assert(util_is_power_of_two_nonzero(max_dim));
445 uint32_t maxMipLevels = util_logbase2(max_dim) + 1;
446 if (ycbcr_info != NULL || pImageFormatInfo->tiling == VK_IMAGE_TILING_LINEAR)
447 maxMipLevels = 1;
448
449 VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
450 if (pImageFormatInfo->tiling == VK_IMAGE_TILING_OPTIMAL &&
451 pImageFormatInfo->type == VK_IMAGE_TYPE_2D && ycbcr_info == NULL &&
452 (features & (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
453 VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
454 !(pImageFormatInfo->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)) {
455
456 sampleCounts =
457 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
458 }
459
460 /* From the Vulkan 1.2.199 spec:
461 *
462 * "VK_IMAGE_CREATE_EXTENDED_USAGE_BIT specifies that the image can be
463 * created with usage flags that are not supported for the format the
464 * image is created with but are supported for at least one format a
465 * VkImageView created from the image can have."
466 *
467 * If VK_IMAGE_CREATE_EXTENDED_USAGE_BIT is set, views can be created with
468 * different usage than the image so we can't always filter on usage.
469 * There is one exception to this below for storage.
470 */
471 const VkImageUsageFlags image_usage = pImageFormatInfo->usage;
472 VkImageUsageFlags view_usage = image_usage;
473 if (pImageFormatInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT)
474 view_usage = 0;
475
476 if (view_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
477 if (!(features & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
478 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))) {
479 return VK_ERROR_FORMAT_NOT_SUPPORTED;
480 }
481 }
482
483 u_foreach_bit(b, view_usage) {
484 VkFormatFeatureFlags2 usage_features =
485 vk_image_usage_to_format_features(1 << b);
486 if (usage_features && !(features & usage_features))
487 return VK_ERROR_FORMAT_NOT_SUPPORTED;
488 }
489
490 const VkExternalMemoryProperties *ext_mem_props = NULL;
491 if (external_info != NULL && external_info->handleType != 0) {
492 bool tiling_has_explicit_layout;
493 switch (pImageFormatInfo->tiling) {
494 case VK_IMAGE_TILING_LINEAR:
495 case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT:
496 tiling_has_explicit_layout = true;
497 break;
498 case VK_IMAGE_TILING_OPTIMAL:
499 tiling_has_explicit_layout = false;
500 break;
501 default:
502 UNREACHABLE("Unsupported VkImageTiling");
503 }
504
505 switch (external_info->handleType) {
506 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
507 /* No special restrictions */
508 if (tiling_has_explicit_layout) {
509 /* With an explicit memory layout, we don't care which type of
510 * fd the image belongs too. Both OPAQUE_FD and DMA_BUF are
511 * interchangeable here.
512 */
513 ext_mem_props = &hk_dma_buf_mem_props;
514 } else {
515 ext_mem_props = &hk_opaque_fd_mem_props;
516 }
517 break;
518
519 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
520 if (!tiling_has_explicit_layout) {
521 return vk_errorf(pdev, VK_ERROR_FORMAT_NOT_SUPPORTED,
522 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT "
523 "requires VK_IMAGE_TILING_LINEAR or "
524 "VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT");
525 }
526 ext_mem_props = &hk_dma_buf_mem_props;
527 break;
528
529 default:
530 /* From the Vulkan 1.3.256 spec:
531 *
532 * "If handleType is not compatible with the [parameters] in
533 * VkPhysicalDeviceImageFormatInfo2, then
534 * vkGetPhysicalDeviceImageFormatProperties2 returns
535 * VK_ERROR_FORMAT_NOT_SUPPORTED."
536 */
537 return vk_errorf(pdev, VK_ERROR_FORMAT_NOT_SUPPORTED,
538 "unsupported VkExternalMemoryTypeFlagBits 0x%x",
539 external_info->handleType);
540 }
541 }
542
543 const unsigned plane_count =
544 vk_format_get_plane_count(pImageFormatInfo->format);
545
546 /* From the Vulkan 1.3.259 spec, VkImageCreateInfo:
547 *
548 * VUID-VkImageCreateInfo-imageCreateFormatFeatures-02260
549 *
550 * "If format is a multi-planar format, and if imageCreateFormatFeatures
551 * (as defined in Image Creation Limits) does not contain
552 * VK_FORMAT_FEATURE_DISJOINT_BIT, then flags must not contain
553 * VK_IMAGE_CREATE_DISJOINT_BIT"
554 *
555 * This is satisfied trivially because we support DISJOINT on all
556 * multi-plane formats. Also,
557 *
558 * VUID-VkImageCreateInfo-format-01577
559 *
560 * "If format is not a multi-planar format, and flags does not include
561 * VK_IMAGE_CREATE_ALIAS_BIT, flags must not contain
562 * VK_IMAGE_CREATE_DISJOINT_BIT"
563 */
564 if (plane_count == 1 &&
565 !(pImageFormatInfo->flags & VK_IMAGE_CREATE_ALIAS_BIT) &&
566 (pImageFormatInfo->flags & VK_IMAGE_CREATE_DISJOINT_BIT))
567 return VK_ERROR_FORMAT_NOT_SUPPORTED;
568
569 if (ycbcr_info &&
570 ((pImageFormatInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) ||
571 (pImageFormatInfo->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT)))
572 return VK_ERROR_FORMAT_NOT_SUPPORTED;
573
574 pImageFormatProperties->imageFormatProperties = (VkImageFormatProperties){
575 .maxExtent = maxExtent,
576 .maxMipLevels = maxMipLevels,
577 .maxArrayLayers = maxArraySize,
578 .sampleCounts = sampleCounts,
579 .maxResourceSize = UINT32_MAX, /* TODO */
580 };
581
582 vk_foreach_struct(s, pImageFormatProperties->pNext) {
583 switch (s->sType) {
584 case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES: {
585 VkExternalImageFormatProperties *p = (void *)s;
586 /* From the Vulkan 1.3.256 spec:
587 *
588 * "If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2
589 * will behave as if VkPhysicalDeviceExternalImageFormatInfo was
590 * not present, and VkExternalImageFormatProperties will be
591 * ignored."
592 *
593 * This is true if and only if ext_mem_props == NULL
594 */
595 if (ext_mem_props != NULL)
596 p->externalMemoryProperties = *ext_mem_props;
597 break;
598 }
599 case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES: {
600 VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = (void *)s;
601 ycbcr_props->combinedImageSamplerDescriptorCount = plane_count;
602 break;
603 }
604 case VK_STRUCTURE_TYPE_HOST_IMAGE_COPY_DEVICE_PERFORMANCE_QUERY_EXT: {
605 VkHostImageCopyDevicePerformanceQueryEXT *hic_props = (void *)s;
606
607 hic_props->optimalDeviceAccess = hic_props->identicalMemoryLayout =
608 !(pImageFormatInfo->tiling == VK_IMAGE_TILING_OPTIMAL &&
609 hk_can_compress_format(&pdev->dev, pImageFormatInfo->format));
610 break;
611 }
612 default:
613 vk_debug_ignored_stype(s->sType);
614 break;
615 }
616 }
617
618 return VK_SUCCESS;
619 }
620
621 static VkSparseImageFormatProperties
622 hk_fill_sparse_image_fmt_props(enum pipe_format format, unsigned samples,
623 VkImageAspectFlags aspects)
624 {
625 /* Apple tile sizes are exactly 16KiB. The Vulkan standard block sizes are
626 * sized to be exactly 64KiB. Fortunately, they correspond directly to the
627 * Apple sizes (except for MSAA 2x), just doubled in each dimensions. Our
628 * sparse binding code gangs together 4 hardware tiles into an API tile. We
629 * just need to derive the correct size here.
630 */
631 unsigned blocksize_B = util_format_get_blocksize(format) * samples;
632 struct ail_tile ail_size = ail_get_max_tile_size(blocksize_B);
633
634 VkExtent3D granularity = {
635 ail_size.width_el * 2 * util_format_get_blockwidth(format),
636 ail_size.height_el * 2 * util_format_get_blockheight(format),
637 1,
638 };
639
640 return (VkSparseImageFormatProperties){
641 .aspectMask = aspects,
642 .flags = VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT,
643 .imageGranularity = granularity,
644 };
645 }
646
647 VKAPI_ATTR void VKAPI_CALL
648 hk_GetPhysicalDeviceSparseImageFormatProperties2(
649 VkPhysicalDevice physicalDevice,
650 const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
651 uint32_t *pPropertyCount, VkSparseImageFormatProperties2 *pProperties)
652 {
653 VkResult result;
654
655 /* Check if the given format info is valid first before returning sparse
656 * props. The easiest way to do this is to just call
657 * hk_GetPhysicalDeviceImageFormatProperties2()
658 */
659 const VkPhysicalDeviceImageFormatInfo2 img_fmt_info = {
660 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
661 .format = pFormatInfo->format,
662 .type = pFormatInfo->type,
663 .tiling = pFormatInfo->tiling,
664 .usage = pFormatInfo->usage,
665 .flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
666 VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
667 };
668
669 VkImageFormatProperties2 img_fmt_props2 = {
670 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
671 .pNext = NULL,
672 };
673
674 result = hk_GetPhysicalDeviceImageFormatProperties2(
675 physicalDevice, &img_fmt_info, &img_fmt_props2);
676 if (result != VK_SUCCESS) {
677 *pPropertyCount = 0;
678 return;
679 }
680
681 const VkImageFormatProperties *props = &img_fmt_props2.imageFormatProperties;
682 if (!(pFormatInfo->samples & props->sampleCounts)) {
683 *pPropertyCount = 0;
684 return;
685 }
686
687 VK_OUTARRAY_MAKE_TYPED(VkSparseImageFormatProperties2, out, pProperties,
688 pPropertyCount);
689
690 VkImageAspectFlags aspects = vk_format_aspects(pFormatInfo->format);
691
692 vk_outarray_append_typed(VkSparseImageFormatProperties2, &out, props)
693 {
694 props->properties = hk_fill_sparse_image_fmt_props(
695 vk_format_to_pipe_format(pFormatInfo->format), pFormatInfo->samples,
696 aspects);
697 }
698 }
699
700 static bool
701 hk_can_compress_create_info(struct hk_device *dev, unsigned plane,
702 const VkImageCreateInfo *info)
703 {
704 return hk_can_compress(&dev->dev, info->format, plane, info->extent.width,
705 info->extent.height, info->samples, info->flags,
706 info->usage, info->pNext);
707 }
708
709 static enum ail_tiling
710 hk_map_tiling(struct hk_device *dev, const VkImageCreateInfo *info,
711 uint64_t modifier)
712 {
713 switch (info->tiling) {
714 case VK_IMAGE_TILING_LINEAR:
715 return AIL_TILING_LINEAR;
716
717 case VK_IMAGE_TILING_OPTIMAL:
718 return AIL_TILING_GPU;
719
720 case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT:
721 return ail_drm_modifier_to_tiling(modifier);
722
723 default:
724 UNREACHABLE("invalid tiling");
725 }
726 }
727
728 static enum ail_tiling
729 hk_map_compression(struct hk_device *dev, const VkImageCreateInfo *info,
730 unsigned plane, uint64_t modifier)
731 {
732 switch (info->tiling) {
733 case VK_IMAGE_TILING_LINEAR:
734 return false;
735
736 case VK_IMAGE_TILING_OPTIMAL:
737 return hk_can_compress_create_info(dev, plane, info);
738
739 case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT:
740 return ail_is_drm_modifier_compressed(modifier);
741
742 default:
743 UNREACHABLE("invalid tiling");
744 }
745 }
746
747 static uint32_t
748 modifier_get_score(uint64_t mod)
749 {
750 switch (mod) {
751 case DRM_FORMAT_MOD_APPLE_GPU_TILED_COMPRESSED:
752 return 10;
753
754 case DRM_FORMAT_MOD_APPLE_GPU_TILED:
755 return 5;
756
757 case DRM_FORMAT_MOD_LINEAR:
758 return 1;
759
760 default:
761 return 0;
762 }
763 }
764
765 static uint64_t
766 choose_drm_format_mod(struct hk_device *dev, uint8_t plane_count,
767 const VkImageCreateInfo *info, uint32_t modifier_count,
768 const uint64_t *modifiers)
769 {
770 uint64_t best_mod = UINT64_MAX;
771 uint32_t best_score = 0;
772 bool can_compress = true;
773
774 for (uint8_t plane = 0; plane < plane_count; plane++) {
775 if (!hk_can_compress_create_info(dev, plane, info))
776 can_compress = false;
777 }
778
779 for (uint32_t i = 0; i < modifier_count; ++i) {
780 if (!can_compress &&
781 modifiers[i] == DRM_FORMAT_MOD_APPLE_GPU_TILED_COMPRESSED)
782 continue;
783
784 uint32_t score = modifier_get_score(modifiers[i]);
785 if (score > best_score) {
786 best_mod = modifiers[i];
787 best_score = score;
788 }
789 }
790
791 if (best_score > 0)
792 return best_mod;
793 else
794 return DRM_FORMAT_MOD_INVALID;
795 }
796
797 static VkResult
798 hk_image_init(struct hk_device *dev, struct hk_image *image,
799 const VkImageCreateInfo *pCreateInfo)
800 {
801 vk_image_init(&dev->vk, &image->vk, pCreateInfo);
802
803 if ((image->vk.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
804 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
805 image->vk.samples > 1) {
806 image->vk.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
807 image->vk.stencil_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
808 }
809
810 if (image->vk.usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)
811 image->vk.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
812 if (image->vk.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)
813 image->vk.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
814
815 image->plane_count = vk_format_get_plane_count(pCreateInfo->format);
816 image->disjoint = image->plane_count > 1 &&
817 (pCreateInfo->flags & VK_IMAGE_CREATE_DISJOINT_BIT);
818
819 /* We do not support interleaved depth/stencil. Instead, we decompose to
820 * a depth plane and a stencil plane.
821 */
822 if (image->vk.format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
823 image->plane_count = 2;
824 }
825
826 if (image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
827 /* Sparse multiplane is not supported. Sparse depth/stencil not supported
828 * on G13 so we're fine there too.
829 */
830 assert(image->plane_count == 1);
831 }
832
833 const struct VkImageDrmFormatModifierExplicitCreateInfoEXT
834 *mod_explicit_info = NULL;
835
836 if (pCreateInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
837 assert(!image->vk.wsi_legacy_scanout);
838 mod_explicit_info = vk_find_struct_const(
839 pCreateInfo->pNext,
840 IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT);
841
842 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
843
844 if (mod_explicit_info) {
845 modifier = mod_explicit_info->drmFormatModifier;
846 } else {
847 const struct VkImageDrmFormatModifierListCreateInfoEXT *mod_list_info =
848 vk_find_struct_const(
849 pCreateInfo->pNext,
850 IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
851
852 modifier = choose_drm_format_mod(dev, image->plane_count, pCreateInfo,
853 mod_list_info->drmFormatModifierCount,
854 mod_list_info->pDrmFormatModifiers);
855 }
856
857 assert(modifier != DRM_FORMAT_MOD_INVALID);
858 assert(image->vk.drm_format_mod == DRM_FORMAT_MOD_INVALID);
859 image->vk.drm_format_mod = modifier;
860 }
861
862 const struct vk_format_ycbcr_info *ycbcr_info =
863 vk_format_get_ycbcr_info(pCreateInfo->format);
864 for (uint8_t plane = 0; plane < image->plane_count; plane++) {
865 VkFormat format =
866 ycbcr_info ? ycbcr_info->planes[plane].format : pCreateInfo->format;
867
868 if (format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
869 format = (plane == 0) ? VK_FORMAT_D32_SFLOAT : VK_FORMAT_S8_UINT;
870 }
871
872 const uint8_t width_scale =
873 ycbcr_info ? ycbcr_info->planes[plane].denominator_scales[0] : 1;
874 const uint8_t height_scale =
875 ycbcr_info ? ycbcr_info->planes[plane].denominator_scales[1] : 1;
876
877 uint32_t linear_stride_B = 0;
878 if (mod_explicit_info &&
879 image->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR)
880 linear_stride_B = mod_explicit_info->pPlaneLayouts[plane].rowPitch;
881
882 bool compressed =
883 hk_map_compression(dev, pCreateInfo, plane, image->vk.drm_format_mod);
884
885 image->planes[plane].layout = (struct ail_layout){
886 .tiling = hk_map_tiling(dev, pCreateInfo, image->vk.drm_format_mod),
887 .compressed = compressed,
888 .mipmapped_z = pCreateInfo->imageType == VK_IMAGE_TYPE_3D,
889 .format = hk_format_to_pipe_format(format),
890
891 .linear_stride_B = linear_stride_B,
892
893 .width_px = pCreateInfo->extent.width / width_scale,
894 .height_px = pCreateInfo->extent.height / height_scale,
895 .depth_px = MAX2(pCreateInfo->extent.depth, pCreateInfo->arrayLayers),
896
897 .levels = pCreateInfo->mipLevels,
898 .sample_count_sa = pCreateInfo->samples,
899 .writeable_image = !compressed,
900
901 /* TODO: Maybe optimize this, our GL driver doesn't bother though */
902 .renderable = true,
903 };
904
905 ail_make_miptree(&image->planes[plane].layout);
906 }
907
908 return VK_SUCCESS;
909 }
910
911 static VkResult
912 hk_image_plane_alloc_vma(struct hk_device *dev, struct hk_image_plane *plane,
913 VkImageCreateFlags create_flags)
914 {
915 const bool sparse_bound = create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT;
916 const bool sparse_resident =
917 create_flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT;
918 assert(sparse_bound || !sparse_resident);
919
920 if (sparse_bound) {
921 size_t size = align(plane->layout.size_B, HK_SPARSE_ALIGN_B);
922 plane->va = agx_va_alloc(&dev->dev, size, AIL_PAGESIZE, 0, 0);
923 plane->addr = plane->va->addr;
924 if (plane->addr == 0) {
925 return vk_errorf(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY,
926 "Sparse VMA allocation failed");
927 }
928
929 /* Bind scratch pages to discard writes, including from lowered software
930 * texture atomics. Reads will use the hardware texture unit sparse
931 * handling to properly handle residency queries.
932 *
933 * In the future we could optimize this out using the PBE sparse support
934 * but that needs more reverse-engineering.
935 */
936 hk_bind_scratch(dev, plane->va, 0, size);
937 }
938
939 if (sparse_resident) {
940 plane->sparse_map =
941 agx_bo_create(&dev->dev, plane->layout.sparse_table_size_B,
942 AIL_PAGESIZE, 0, "Sparse map");
943
944 /* Zero-initialize the sparse map. This ensures all tiles are disabled,
945 * which provides correct behaviour for unmapped tiles.
946 */
947 memset(agx_bo_map(plane->sparse_map), 0,
948 plane->layout.sparse_table_size_B);
949 }
950
951 return VK_SUCCESS;
952 }
953
954 static void
955 hk_image_plane_finish(struct hk_device *dev, struct hk_image_plane *plane,
956 VkImageCreateFlags create_flags,
957 const VkAllocationCallbacks *pAllocator)
958 {
959 if (plane->va) {
960 agx_va_free(&dev->dev, plane->va, true);
961 }
962
963 agx_bo_unreference(&dev->dev, plane->sparse_map);
964 }
965
966 static void
967 hk_image_finish(struct hk_device *dev, struct hk_image *image,
968 const VkAllocationCallbacks *pAllocator)
969 {
970 for (uint8_t plane = 0; plane < image->plane_count; plane++) {
971 hk_image_plane_finish(dev, &image->planes[plane], image->vk.create_flags,
972 pAllocator);
973 }
974
975 vk_image_finish(&image->vk);
976 }
977
978 VKAPI_ATTR VkResult VKAPI_CALL
979 hk_CreateImage(VkDevice _device, const VkImageCreateInfo *pCreateInfo,
980 const VkAllocationCallbacks *pAllocator, VkImage *pImage)
981 {
982 VK_FROM_HANDLE(hk_device, dev, _device);
983 struct hk_physical_device *pdev = hk_device_physical(dev);
984 struct hk_image *image;
985 VkResult result;
986
987 if (wsi_common_is_swapchain_image(pCreateInfo))
988 return wsi_common_create_swapchain_image(&pdev->wsi_device, pCreateInfo,
989 pImage);
990
991 image = vk_zalloc2(&dev->vk.alloc, pAllocator, sizeof(*image), 8,
992 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
993 if (!image)
994 return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
995
996 result = hk_image_init(dev, image, pCreateInfo);
997 if (result != VK_SUCCESS) {
998 vk_free2(&dev->vk.alloc, pAllocator, image);
999 return result;
1000 }
1001
1002 for (uint8_t plane = 0; plane < image->plane_count; plane++) {
1003 result = hk_image_plane_alloc_vma(dev, &image->planes[plane],
1004 image->vk.create_flags);
1005 if (result != VK_SUCCESS) {
1006 hk_image_finish(dev, image, pAllocator);
1007 vk_free2(&dev->vk.alloc, pAllocator, image);
1008 return result;
1009 }
1010 }
1011
1012 *pImage = hk_image_to_handle(image);
1013
1014 return VK_SUCCESS;
1015 }
1016
1017 VKAPI_ATTR void VKAPI_CALL
1018 hk_DestroyImage(VkDevice device, VkImage _image,
1019 const VkAllocationCallbacks *pAllocator)
1020 {
1021 VK_FROM_HANDLE(hk_device, dev, device);
1022 VK_FROM_HANDLE(hk_image, image, _image);
1023
1024 if (!image)
1025 return;
1026
1027 hk_image_finish(dev, image, pAllocator);
1028 vk_free2(&dev->vk.alloc, pAllocator, image);
1029 }
1030
1031 static void
1032 hk_image_plane_add_req(struct hk_image_plane *plane, bool sparse,
1033 uint64_t *size_B, uint32_t *align_B)
1034 {
1035 unsigned plane_align_B = sparse ? HK_SPARSE_ALIGN_B : HK_PLANE_ALIGN_B;
1036 assert(util_is_power_of_two_or_zero64(*align_B));
1037 assert(util_is_power_of_two_or_zero64(plane_align_B));
1038
1039 *align_B = MAX2(*align_B, plane_align_B);
1040 *size_B = align64(*size_B, plane_align_B);
1041 *size_B += plane->layout.size_B;
1042 }
1043
1044 static void
1045 hk_get_image_memory_requirements(struct hk_device *dev, struct hk_image *image,
1046 VkImageAspectFlags aspects,
1047 VkMemoryRequirements2 *pMemoryRequirements)
1048 {
1049 struct hk_physical_device *pdev = hk_device_physical(dev);
1050 uint32_t memory_types = (1 << pdev->mem_type_count) - 1;
1051 bool sparse =
1052 image->vk.create_flags & (VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
1053 VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT);
1054
1055 uint64_t size_B = 0;
1056 uint32_t align_B = 0;
1057 if (image->disjoint) {
1058 uint8_t plane = hk_image_aspects_to_plane(image, aspects);
1059 hk_image_plane_add_req(&image->planes[plane], sparse, &size_B, &align_B);
1060 } else {
1061 for (unsigned plane = 0; plane < image->plane_count; plane++)
1062 hk_image_plane_add_req(&image->planes[plane], sparse, &size_B,
1063 &align_B);
1064 }
1065
1066 /* For sparse binding, we need to pad to the standard alignment so we don't
1067 * clobber over things when we bind memory.
1068 */
1069 if (sparse) {
1070 size_B = align64(size_B, align_B);
1071 }
1072
1073 pMemoryRequirements->memoryRequirements.memoryTypeBits = memory_types;
1074 pMemoryRequirements->memoryRequirements.alignment = align_B;
1075 pMemoryRequirements->memoryRequirements.size = size_B;
1076
1077 vk_foreach_struct_const(ext, pMemoryRequirements->pNext) {
1078 switch (ext->sType) {
1079 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
1080 VkMemoryDedicatedRequirements *dedicated = (void *)ext;
1081 dedicated->prefersDedicatedAllocation = false;
1082 dedicated->requiresDedicatedAllocation = false;
1083 break;
1084 }
1085 default:
1086 vk_debug_ignored_stype(ext->sType);
1087 break;
1088 }
1089 }
1090 }
1091
1092 VKAPI_ATTR void VKAPI_CALL
1093 hk_GetImageMemoryRequirements2(VkDevice device,
1094 const VkImageMemoryRequirementsInfo2 *pInfo,
1095 VkMemoryRequirements2 *pMemoryRequirements)
1096 {
1097 VK_FROM_HANDLE(hk_device, dev, device);
1098 VK_FROM_HANDLE(hk_image, image, pInfo->image);
1099
1100 const VkImagePlaneMemoryRequirementsInfo *plane_info =
1101 vk_find_struct_const(pInfo->pNext, IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO);
1102 const VkImageAspectFlags aspects =
1103 image->disjoint ? plane_info->planeAspect : image->vk.aspects;
1104
1105 hk_get_image_memory_requirements(dev, image, aspects, pMemoryRequirements);
1106 }
1107
1108 VKAPI_ATTR void VKAPI_CALL
1109 hk_GetDeviceImageMemoryRequirements(VkDevice device,
1110 const VkDeviceImageMemoryRequirements *pInfo,
1111 VkMemoryRequirements2 *pMemoryRequirements)
1112 {
1113 VK_FROM_HANDLE(hk_device, dev, device);
1114 ASSERTED VkResult result;
1115 struct hk_image image = {0};
1116
1117 result = hk_image_init(dev, &image, pInfo->pCreateInfo);
1118 assert(result == VK_SUCCESS);
1119
1120 const VkImageAspectFlags aspects =
1121 image.disjoint ? pInfo->planeAspect : image.vk.aspects;
1122
1123 hk_get_image_memory_requirements(dev, &image, aspects, pMemoryRequirements);
1124
1125 hk_image_finish(dev, &image, NULL);
1126 }
1127
1128 static VkSparseImageMemoryRequirements
1129 hk_fill_sparse_image_memory_reqs(const struct ail_layout *layout,
1130 VkImageAspectFlags aspects)
1131 {
1132 VkSparseImageFormatProperties sparse_format_props =
1133 hk_fill_sparse_image_fmt_props(layout->format, layout->sample_count_sa,
1134 aspects);
1135
1136 unsigned tail_level = layout->mip_tail_first_lod;
1137 assert(tail_level <= layout->levels);
1138 VkSparseImageMemoryRequirements sparse_memory_reqs = {
1139 .formatProperties = sparse_format_props,
1140 .imageMipTailFirstLod = layout->mip_tail_first_lod,
1141 .imageMipTailStride = 0,
1142 };
1143
1144 /* imageMipTailSize must be aligned to the sparse block size (65k). This
1145 * requires us to manage the miptail manually, because 16k is the actual
1146 * hardware alignment here so we need to give the illusion of extra
1147 * padding. Annoying!
1148 */
1149 if (tail_level == 0) {
1150 sparse_memory_reqs.imageMipTailSize =
1151 align(layout->size_B, HK_SPARSE_ALIGN_B);
1152
1153 sparse_memory_reqs.imageMipTailOffset = 0;
1154 } else if (tail_level < layout->levels) {
1155 sparse_memory_reqs.imageMipTailSize =
1156 align(layout->mip_tail_stride * layout->depth_px, HK_SPARSE_ALIGN_B);
1157
1158 /* TODO: sparse metadata */
1159 sparse_memory_reqs.imageMipTailOffset = HK_MIP_TAIL_START_OFFSET;
1160 } else {
1161 sparse_memory_reqs.imageMipTailSize = 0;
1162 sparse_memory_reqs.imageMipTailOffset = HK_MIP_TAIL_START_OFFSET;
1163 }
1164
1165 return sparse_memory_reqs;
1166 }
1167
1168 static void
1169 hk_get_image_sparse_memory_requirements(
1170 struct hk_device *dev, struct hk_image *image, VkImageAspectFlags aspects,
1171 uint32_t *pSparseMemoryRequirementCount,
1172 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1173 {
1174 VK_OUTARRAY_MAKE_TYPED(VkSparseImageMemoryRequirements2, out,
1175 pSparseMemoryRequirements,
1176 pSparseMemoryRequirementCount);
1177
1178 /* From the Vulkan 1.3.279 spec:
1179 *
1180 * "The sparse image must have been created using the
1181 * VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT flag to retrieve valid sparse
1182 * image memory requirements."
1183 */
1184 if (!(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT))
1185 return;
1186
1187 /* We don't support multiplane sparse for now */
1188 if (image->plane_count > 1)
1189 return;
1190
1191 vk_outarray_append_typed(VkSparseImageMemoryRequirements2, &out, reqs)
1192 {
1193 reqs->memoryRequirements =
1194 hk_fill_sparse_image_memory_reqs(&image->planes[0].layout, aspects);
1195 };
1196 }
1197
1198 VKAPI_ATTR void VKAPI_CALL
1199 hk_GetImageSparseMemoryRequirements2(
1200 VkDevice device, const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1201 uint32_t *pSparseMemoryRequirementCount,
1202 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1203 {
1204 VK_FROM_HANDLE(hk_device, dev, device);
1205 VK_FROM_HANDLE(hk_image, image, pInfo->image);
1206
1207 const VkImageAspectFlags aspects = image->vk.aspects;
1208
1209 hk_get_image_sparse_memory_requirements(dev, image, aspects,
1210 pSparseMemoryRequirementCount,
1211 pSparseMemoryRequirements);
1212 }
1213
1214 VKAPI_ATTR void VKAPI_CALL
1215 hk_GetDeviceImageSparseMemoryRequirements(
1216 VkDevice device, const VkDeviceImageMemoryRequirements *pInfo,
1217 uint32_t *pSparseMemoryRequirementCount,
1218 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1219 {
1220 VK_FROM_HANDLE(hk_device, dev, device);
1221 ASSERTED VkResult result;
1222 struct hk_image image = {0};
1223
1224 result = hk_image_init(dev, &image, pInfo->pCreateInfo);
1225 assert(result == VK_SUCCESS);
1226
1227 const VkImageAspectFlags aspects =
1228 image.disjoint ? pInfo->planeAspect : image.vk.aspects;
1229
1230 hk_get_image_sparse_memory_requirements(dev, &image, aspects,
1231 pSparseMemoryRequirementCount,
1232 pSparseMemoryRequirements);
1233
1234 hk_image_finish(dev, &image, NULL);
1235 }
1236
1237 static void
1238 hk_get_image_subresource_layout(UNUSED struct hk_device *dev,
1239 struct hk_image *image,
1240 const VkImageSubresource2KHR *pSubresource,
1241 VkSubresourceLayout2KHR *pLayout)
1242 {
1243 const VkImageSubresource *isr = &pSubresource->imageSubresource;
1244
1245 const uint8_t p = hk_image_aspects_to_plane(image, isr->aspectMask);
1246 const struct hk_image_plane *plane = &image->planes[p];
1247
1248 uint64_t offset_B = 0;
1249 if (!image->disjoint) {
1250 uint32_t align_B = 0;
1251 /* TODO: sparse? */
1252 for (unsigned plane = 0; plane < p; plane++)
1253 hk_image_plane_add_req(&image->planes[plane], false, &offset_B,
1254 &align_B);
1255 }
1256 offset_B +=
1257 ail_get_layer_level_B(&plane->layout, isr->arrayLayer, isr->mipLevel);
1258
1259 bool is_3d = image->vk.image_type == VK_IMAGE_TYPE_3D;
1260
1261 pLayout->subresourceLayout = (VkSubresourceLayout){
1262 .offset = offset_B,
1263 .size = ail_get_level_size_B(&plane->layout, isr->mipLevel),
1264
1265 /* From the spec:
1266 *
1267 * It is legal to call vkGetImageSubresourceLayout2KHR with a image
1268 * created with tiling equal to VK_IMAGE_TILING_OPTIMAL, but the
1269 * members of VkSubresourceLayout2KHR::subresourceLayout will have
1270 * undefined values in this case.
1271 *
1272 * So don't collapse with mips.
1273 */
1274 .rowPitch = isr->mipLevel
1275 ? 0
1276 : ail_get_wsi_stride_B(&plane->layout, isr->mipLevel),
1277 .arrayPitch = is_3d ? 0 : plane->layout.layer_stride_B,
1278 .depthPitch = is_3d ? plane->layout.layer_stride_B : 0,
1279 };
1280
1281 VkSubresourceHostMemcpySizeEXT *memcpy_size =
1282 vk_find_struct(pLayout, SUBRESOURCE_HOST_MEMCPY_SIZE_EXT);
1283 if (memcpy_size) {
1284 memcpy_size->size = pLayout->subresourceLayout.size;
1285 }
1286 }
1287
1288 VKAPI_ATTR void VKAPI_CALL
1289 hk_GetImageSubresourceLayout2KHR(VkDevice device, VkImage _image,
1290 const VkImageSubresource2KHR *pSubresource,
1291 VkSubresourceLayout2KHR *pLayout)
1292 {
1293 VK_FROM_HANDLE(hk_device, dev, device);
1294 VK_FROM_HANDLE(hk_image, image, _image);
1295
1296 hk_get_image_subresource_layout(dev, image, pSubresource, pLayout);
1297 }
1298
1299 VKAPI_ATTR void VKAPI_CALL
1300 hk_GetDeviceImageSubresourceLayoutKHR(
1301 VkDevice device, const VkDeviceImageSubresourceInfoKHR *pInfo,
1302 VkSubresourceLayout2KHR *pLayout)
1303 {
1304 VK_FROM_HANDLE(hk_device, dev, device);
1305 ASSERTED VkResult result;
1306 struct hk_image image = {0};
1307
1308 result = hk_image_init(dev, &image, pInfo->pCreateInfo);
1309 assert(result == VK_SUCCESS);
1310
1311 hk_get_image_subresource_layout(dev, &image, pInfo->pSubresource, pLayout);
1312
1313 hk_image_finish(dev, &image, NULL);
1314 }
1315
1316 static void
1317 hk_image_plane_bind(struct hk_device *dev, struct hk_image_plane *plane,
1318 struct hk_device_memory *mem, uint64_t *offset_B)
1319 {
1320 *offset_B = align64(*offset_B, HK_PLANE_ALIGN_B);
1321
1322 if (plane->va) {
1323 #if 0
1324 agx_bo_bind_vma(dev->ws_dev,
1325 mem->bo,
1326 plane->addr,
1327 plane->va,
1328 *offset_B,
1329 plane->nil.pte_kind);
1330 #endif
1331 UNREACHABLE("todo");
1332 } else {
1333 plane->addr = mem->bo->va->addr + *offset_B;
1334 plane->map = agx_bo_map(mem->bo) + *offset_B;
1335 plane->rem = mem->bo->size - (*offset_B);
1336 }
1337
1338 *offset_B += plane->layout.size_B;
1339 }
1340
1341 VKAPI_ATTR VkResult VKAPI_CALL
1342 hk_BindImageMemory2(VkDevice device, uint32_t bindInfoCount,
1343 const VkBindImageMemoryInfo *pBindInfos)
1344 {
1345 VK_FROM_HANDLE(hk_device, dev, device);
1346 for (uint32_t i = 0; i < bindInfoCount; ++i) {
1347 VK_FROM_HANDLE(hk_device_memory, mem, pBindInfos[i].memory);
1348 VK_FROM_HANDLE(hk_image, image, pBindInfos[i].image);
1349
1350 /* Ignore this struct on Android, we cannot access swapchain structures
1351 * there. */
1352 #ifdef HK_USE_WSI_PLATFORM
1353 if (!mem) {
1354 const VkBindImageMemorySwapchainInfoKHR *swapchain_info =
1355 vk_find_struct_const(pBindInfos[i].pNext,
1356 BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR);
1357 assert(swapchain_info && swapchain_info->swapchain != VK_NULL_HANDLE);
1358 mem = hk_device_memory_from_handle(wsi_common_get_memory(
1359 swapchain_info->swapchain, swapchain_info->imageIndex));
1360 }
1361 #endif
1362
1363 assert(mem);
1364 uint64_t offset_B = pBindInfos[i].memoryOffset;
1365 if (image->disjoint) {
1366 const VkBindImagePlaneMemoryInfo *plane_info = vk_find_struct_const(
1367 pBindInfos[i].pNext, BIND_IMAGE_PLANE_MEMORY_INFO);
1368 uint8_t plane =
1369 hk_image_aspects_to_plane(image, plane_info->planeAspect);
1370 hk_image_plane_bind(dev, &image->planes[plane], mem, &offset_B);
1371 } else {
1372 for (unsigned plane = 0; plane < image->plane_count; plane++) {
1373 hk_image_plane_bind(dev, &image->planes[plane], mem, &offset_B);
1374 }
1375 }
1376
1377 const VkBindMemoryStatusKHR *status =
1378 vk_find_struct_const(pBindInfos[i].pNext, BIND_MEMORY_STATUS_KHR);
1379 if (status != NULL && status->pResult != NULL)
1380 *status->pResult = VK_SUCCESS;
1381 }
1382
1383 return VK_SUCCESS;
1384 }
1385
1386 static uint32_t
1387 hk_plane_index(VkFormat format, VkImageAspectFlags aspect_mask)
1388 {
1389 switch (aspect_mask) {
1390 default:
1391 assert(aspect_mask != VK_IMAGE_ASPECT_MEMORY_PLANE_3_BIT_EXT);
1392 return 0;
1393 case VK_IMAGE_ASPECT_PLANE_1_BIT:
1394 case VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT:
1395 return 1;
1396 case VK_IMAGE_ASPECT_PLANE_2_BIT:
1397 case VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT:
1398 return 2;
1399 case VK_IMAGE_ASPECT_STENCIL_BIT:
1400 return format == VK_FORMAT_D32_SFLOAT_S8_UINT;
1401 }
1402 }
1403
1404 static void
1405 hk_copy_memory_to_image(struct hk_device *device, struct hk_image *dst_image,
1406 const VkMemoryToImageCopyEXT *info, bool copy_memcpy)
1407 {
1408 unsigned plane =
1409 hk_plane_index(dst_image->vk.format, info->imageSubresource.aspectMask);
1410 const struct ail_layout *layout = &dst_image->planes[plane].layout;
1411
1412 VkOffset3D offset = info->imageOffset;
1413 VkExtent3D extent = info->imageExtent;
1414 uint32_t src_width = info->memoryRowLength ?: extent.width;
1415 uint32_t src_height = info->memoryImageHeight ?: extent.height;
1416
1417 uint32_t blocksize_B = util_format_get_blocksize(layout->format);
1418
1419 /* Align width and height to block */
1420 src_width =
1421 DIV_ROUND_UP(src_width, util_format_get_blockwidth(layout->format));
1422 src_height =
1423 DIV_ROUND_UP(src_height, util_format_get_blockheight(layout->format));
1424
1425 uint32_t src_pitch = src_width * blocksize_B;
1426
1427 unsigned start_layer = (dst_image->vk.image_type == VK_IMAGE_TYPE_3D)
1428 ? offset.z
1429 : info->imageSubresource.baseArrayLayer;
1430 uint32_t layers =
1431 MAX2(extent.depth, vk_image_subresource_layer_count(
1432 &dst_image->vk, &info->imageSubresource));
1433
1434 unsigned level = info->imageSubresource.mipLevel;
1435 uint32_t image_offset = ail_get_layer_level_B(layout, start_layer, level);
1436 uint32_t dst_layer_stride = layout->layer_stride_B;
1437 uint32_t src_layer_stride = copy_memcpy
1438 ? ail_get_level_size_B(layout, level)
1439 : (src_width * src_height * blocksize_B);
1440 bool tiled = ail_is_level_twiddled_uncompressed(
1441 layout, info->imageSubresource.mipLevel);
1442
1443 const char *src = (const char *)info->pHostPointer;
1444 char *dst = (char *)dst_image->planes[plane].map + image_offset;
1445 for (unsigned layer = 0; layer < layers;
1446 layer++, src += src_layer_stride, dst += dst_layer_stride) {
1447 if (copy_memcpy) {
1448 memcpy(dst, src, ail_get_level_size_B(layout, level));
1449 } else if (!tiled) {
1450 uint32_t dst_pitch = ail_get_linear_stride_B(layout, level);
1451 /*TODO:comp*/
1452 for (unsigned y = 0; y < extent.height; y++) {
1453 memcpy(dst + dst_pitch * (y + offset.y) + offset.x * blocksize_B,
1454 src + src_pitch * y, extent.width * blocksize_B);
1455 }
1456 } else {
1457 ail_tile(dst, (void *)src, layout, level, src_pitch, offset.x,
1458 offset.y, extent.width, extent.height);
1459 }
1460 }
1461 }
1462
1463 VKAPI_ATTR VkResult VKAPI_CALL
1464 hk_CopyMemoryToImageEXT(VkDevice _device,
1465 const VkCopyMemoryToImageInfoEXT *info)
1466 {
1467 VK_FROM_HANDLE(hk_device, device, _device);
1468 VK_FROM_HANDLE(hk_image, dst_image, info->dstImage);
1469
1470 for (unsigned i = 0; i < info->regionCount; i++) {
1471 hk_copy_memory_to_image(device, dst_image, &info->pRegions[i],
1472 info->flags & VK_HOST_IMAGE_COPY_MEMCPY_EXT);
1473 }
1474
1475 return VK_SUCCESS;
1476 }
1477
1478 static void
1479 hk_copy_image_to_memory(struct hk_device *device, struct hk_image *src_image,
1480 const VkImageToMemoryCopyEXT *info, bool copy_memcpy)
1481 {
1482 unsigned plane =
1483 hk_plane_index(src_image->vk.format, info->imageSubresource.aspectMask);
1484 const struct ail_layout *layout = &src_image->planes[plane].layout;
1485
1486 VkOffset3D offset = info->imageOffset;
1487 VkExtent3D extent = info->imageExtent;
1488 uint32_t dst_width = info->memoryRowLength ?: extent.width;
1489 uint32_t dst_height = info->memoryImageHeight ?: extent.height;
1490
1491 #if 0
1492 copy_compressed(src_image->vk.format, &offset, &extent, &dst_width,
1493 &dst_height);
1494 #endif
1495
1496 uint32_t blocksize_B = util_format_get_blocksize(layout->format);
1497
1498 /* Align width and height to block */
1499 dst_width =
1500 DIV_ROUND_UP(dst_width, util_format_get_blockwidth(layout->format));
1501 dst_height =
1502 DIV_ROUND_UP(dst_height, util_format_get_blockheight(layout->format));
1503
1504 uint32_t dst_pitch = dst_width * blocksize_B;
1505
1506 unsigned start_layer = (src_image->vk.image_type == VK_IMAGE_TYPE_3D)
1507 ? offset.z
1508 : info->imageSubresource.baseArrayLayer;
1509 uint32_t layers =
1510 MAX2(extent.depth, vk_image_subresource_layer_count(
1511 &src_image->vk, &info->imageSubresource));
1512 unsigned level = info->imageSubresource.mipLevel;
1513
1514 uint32_t image_offset = ail_get_layer_level_B(layout, start_layer, level);
1515 uint32_t src_layer_stride = layout->layer_stride_B;
1516 uint32_t dst_layer_stride = copy_memcpy
1517 ? ail_get_level_size_B(layout, level)
1518 : (dst_width * dst_height * blocksize_B);
1519
1520 bool tiled = ail_is_level_twiddled_uncompressed(
1521 layout, info->imageSubresource.mipLevel);
1522
1523 const char *src = (const char *)src_image->planes[plane].map + image_offset;
1524 char *dst = (char *)info->pHostPointer;
1525 for (unsigned layer = 0; layer < layers;
1526 layer++, src += src_layer_stride, dst += dst_layer_stride) {
1527
1528 if (copy_memcpy) {
1529 memcpy(dst, src, dst_layer_stride);
1530 } else if (!tiled) {
1531 /* TODO: comp */
1532 uint32_t src_pitch = ail_get_linear_stride_B(layout, level);
1533 for (unsigned y = 0; y < extent.height; y++) {
1534 memcpy(dst + dst_pitch * y,
1535 src + src_pitch * (y + offset.y) + offset.x * blocksize_B,
1536 extent.width * blocksize_B);
1537 }
1538 } else {
1539 ail_detile((void *)src, dst, layout, info->imageSubresource.mipLevel,
1540 dst_pitch, offset.x, offset.y, extent.width, extent.height);
1541 }
1542 }
1543 }
1544
1545 VKAPI_ATTR VkResult VKAPI_CALL
1546 hk_CopyImageToMemoryEXT(VkDevice _device,
1547 const VkCopyImageToMemoryInfoEXT *info)
1548 {
1549 VK_FROM_HANDLE(hk_device, device, _device);
1550 VK_FROM_HANDLE(hk_image, image, info->srcImage);
1551
1552 for (unsigned i = 0; i < info->regionCount; i++) {
1553 hk_copy_image_to_memory(device, image, &info->pRegions[i],
1554 info->flags & VK_HOST_IMAGE_COPY_MEMCPY_EXT);
1555 }
1556
1557 return VK_SUCCESS;
1558 }
1559
1560 static void
1561 hk_copy_image_to_image_cpu(struct hk_device *device, struct hk_image *src_image,
1562 struct hk_image *dst_image, const VkImageCopy2 *info,
1563 bool copy_memcpy)
1564 {
1565 unsigned src_plane =
1566 hk_plane_index(src_image->vk.format, info->srcSubresource.aspectMask);
1567 unsigned dst_plane =
1568 hk_plane_index(dst_image->vk.format, info->dstSubresource.aspectMask);
1569
1570 const struct ail_layout *src_layout = &src_image->planes[src_plane].layout;
1571 const struct ail_layout *dst_layout = &dst_image->planes[dst_plane].layout;
1572
1573 VkOffset3D src_offset = info->srcOffset;
1574 VkOffset3D dst_offset = info->dstOffset;
1575 VkExtent3D extent = info->extent;
1576 uint32_t layers_to_copy = MAX2(
1577 info->extent.depth,
1578 vk_image_subresource_layer_count(&src_image->vk, &info->srcSubresource));
1579
1580 /* See comment above. */
1581 #if 0
1582 copy_compressed(src_image->vk.format, &src_offset, &extent, NULL, NULL);
1583 copy_compressed(dst_image->vk.format, &dst_offset, NULL, NULL, NULL);
1584 #endif
1585
1586 unsigned src_start_layer = (src_image->vk.image_type == VK_IMAGE_TYPE_3D)
1587 ? src_offset.z
1588 : info->srcSubresource.baseArrayLayer;
1589 unsigned dst_start_layer = (dst_image->vk.image_type == VK_IMAGE_TYPE_3D)
1590 ? dst_offset.z
1591 : info->dstSubresource.baseArrayLayer;
1592
1593 uint32_t src_layer_stride = src_layout->layer_stride_B;
1594 uint32_t dst_layer_stride = dst_layout->layer_stride_B;
1595
1596 uint32_t dst_block_B = util_format_get_blocksize(dst_layout->format);
1597 uint32_t src_block_B = util_format_get_blocksize(src_layout->format);
1598
1599 uint32_t src_image_offset = ail_get_layer_level_B(
1600 src_layout, src_start_layer, info->srcSubresource.mipLevel);
1601 uint32_t dst_image_offset = ail_get_layer_level_B(
1602 dst_layout, dst_start_layer, info->dstSubresource.mipLevel);
1603
1604 bool src_tiled = ail_is_level_twiddled_uncompressed(
1605 src_layout, info->srcSubresource.mipLevel);
1606 bool dst_tiled = ail_is_level_twiddled_uncompressed(
1607 dst_layout, info->dstSubresource.mipLevel);
1608
1609 const char *src =
1610 (const char *)src_image->planes[src_plane].map + src_image_offset;
1611 char *dst = (char *)dst_image->planes[dst_plane].map + dst_image_offset;
1612 for (unsigned layer = 0; layer < layers_to_copy;
1613 layer++, src += src_layer_stride, dst += dst_layer_stride) {
1614
1615 if (copy_memcpy) {
1616 uint32_t src_size =
1617 ail_get_level_size_B(src_layout, info->srcSubresource.mipLevel);
1618 uint32_t dst_size =
1619 ail_get_level_size_B(dst_layout, info->dstSubresource.mipLevel);
1620
1621 assert(src_size == dst_size);
1622 memcpy(dst, src, src_size);
1623 } else if (!src_tiled && !dst_tiled) {
1624 /* TODO comp */
1625 uint32_t src_pitch =
1626 ail_get_linear_stride_B(src_layout, info->srcSubresource.mipLevel);
1627
1628 uint32_t dst_pitch =
1629 ail_get_linear_stride_B(dst_layout, info->dstSubresource.mipLevel);
1630
1631 for (unsigned y = 0; y < extent.height; y++) {
1632 memcpy(dst + dst_pitch * (y + dst_offset.y) +
1633 dst_offset.x * dst_block_B,
1634 src + src_pitch * (y + src_offset.y) +
1635 src_offset.x * src_block_B,
1636 extent.width * src_block_B);
1637 }
1638 } else if (!src_tiled) {
1639 UNREACHABLE("todo");
1640 #if 0
1641 fdl6_memcpy_linear_to_tiled(
1642 dst_offset.x, dst_offset.y, extent.width, extent.height, dst,
1643 src + src_pitch * src_offset.y + src_offset.x * src_layout->cpp,
1644 dst_layout, info->dstSubresource.mipLevel, src_pitch,
1645 &device->physical_device->ubwc_config);
1646 #endif
1647 } else if (!dst_tiled) {
1648 UNREACHABLE("todo");
1649 #if 0
1650 fdl6_memcpy_tiled_to_linear(
1651 src_offset.x, src_offset.y, extent.width, extent.height,
1652 dst + dst_pitch * dst_offset.y + dst_offset.x * dst_layout->cpp,
1653 src, src_layout, info->dstSubresource.mipLevel, dst_pitch,
1654 &device->physical_device->ubwc_config);
1655 #endif
1656 } else {
1657 unsigned src_level = info->srcSubresource.mipLevel;
1658 unsigned dst_level = info->dstSubresource.mipLevel;
1659 uint32_t block_width = src_layout->tilesize_el[src_level].width_el;
1660 uint32_t block_height = src_layout->tilesize_el[src_level].height_el;
1661
1662 /* Twiddled images have a single "tile" sized to the entire image, so
1663 * break it up so we'll fit.
1664 */
1665 if (src_layout->tiling == AIL_TILING_TWIDDLED) {
1666 block_width = block_height = MIN2(block_width, 32);
1667 }
1668
1669 uint32_t temp_pitch = block_width * src_block_B;
1670 size_t temp_tile_size = temp_pitch * (src_offset.y + extent.height);
1671
1672 /* Work tile-by-tile, holding the unswizzled tile in a temporary
1673 * buffer.
1674 */
1675 char *temp_tile = malloc(temp_tile_size);
1676
1677 for (unsigned by = src_offset.y / block_height;
1678 by * block_height < src_offset.y + extent.height; by++) {
1679 uint32_t src_y_start = MAX2(src_offset.y, by * block_height);
1680 uint32_t dst_y_start = src_y_start - src_offset.y + dst_offset.y;
1681 uint32_t height =
1682 MIN2((by + 1) * block_height, src_offset.y + extent.height) -
1683 src_y_start;
1684 for (unsigned bx = src_offset.x / block_width;
1685 bx * block_width < src_offset.x + extent.width; bx++) {
1686 uint32_t src_x_start = MAX2(src_offset.x, bx * block_width);
1687 uint32_t dst_x_start = src_x_start - src_offset.x + dst_offset.x;
1688 uint32_t width =
1689 MIN2((bx + 1) * block_width, src_offset.x + extent.width) -
1690 src_x_start;
1691
1692 ail_detile((void *)src, temp_tile, src_layout, src_level,
1693 temp_pitch, src_x_start, src_y_start, width, height);
1694 ail_tile(dst, temp_tile, dst_layout, dst_level, temp_pitch,
1695 dst_x_start, dst_y_start, width, height);
1696 }
1697 }
1698
1699 free(temp_tile);
1700 }
1701 }
1702 }
1703
1704 VKAPI_ATTR VkResult VKAPI_CALL
1705 hk_CopyImageToImageEXT(VkDevice _device,
1706 const VkCopyImageToImageInfoEXT *pCopyImageToImageInfo)
1707 {
1708 VK_FROM_HANDLE(hk_device, device, _device);
1709 VK_FROM_HANDLE(hk_image, src_image, pCopyImageToImageInfo->srcImage);
1710 VK_FROM_HANDLE(hk_image, dst_image, pCopyImageToImageInfo->dstImage);
1711 bool copy_memcpy =
1712 pCopyImageToImageInfo->flags & VK_HOST_IMAGE_COPY_MEMCPY_EXT;
1713
1714 for (uint32_t i = 0; i < pCopyImageToImageInfo->regionCount; ++i) {
1715 if (src_image->vk.format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
1716 VkImageCopy2 info = pCopyImageToImageInfo->pRegions[i];
1717 u_foreach_bit(b, info.dstSubresource.aspectMask) {
1718 info.srcSubresource.aspectMask = BITFIELD_BIT(b);
1719 info.dstSubresource.aspectMask = BITFIELD_BIT(b);
1720 hk_copy_image_to_image_cpu(device, src_image, dst_image, &info,
1721 copy_memcpy);
1722 }
1723 continue;
1724 }
1725
1726 hk_copy_image_to_image_cpu(device, src_image, dst_image,
1727 pCopyImageToImageInfo->pRegions + i,
1728 copy_memcpy);
1729 }
1730
1731 return VK_SUCCESS;
1732 }
1733
1734 VKAPI_ATTR VkResult VKAPI_CALL
1735 hk_TransitionImageLayoutEXT(
1736 VkDevice device, uint32_t transitionCount,
1737 const VkHostImageLayoutTransitionInfoEXT *transitions)
1738 {
1739 /* We don't do anything with layouts so this should be a no-op */
1740 return VK_SUCCESS;
1741 }