Debian Codegraph @

src/panfrost/vulkan/panvk_host_copy.c

raw
1 /*
2 * Copyright © 2025 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "panvk_device.h"
7 #include "panvk_device_memory.h"
8 #include "panvk_entrypoints.h"
9 #include "pan_tiling.h"
10 #include "panvk_image.h"
11
12 #include "vk_format.h"
13 #include "vk_object.h"
14 #include "vk_util.h"
15
16 #include "util/cache_ops.h"
17
18 struct image_params {
19 struct panvk_image *img;
20 struct pan_kmod_bo *bo;
21 void *ptr;
22 VkOffset3D offset;
23 VkImageSubresourceLayers subres;
24 };
25
26 struct memory_params {
27 void *ptr;
28 struct vk_image_buffer_layout layout;
29 };
30
31 static void
32 panvk_interleaved_copy(void *dst, void *src, unsigned size_bl,
33 unsigned block_size_B, enum pan_interleave_zs interleave,
34 bool is_store)
35 {
36 switch (interleave) {
37 case PAN_INTERLEAVE_NONE:
38 if (is_store)
39 memcpy(dst, src, size_bl * block_size_B);
40 else
41 memcpy(src, dst, size_bl * block_size_B);
42 break;
43 case PAN_INTERLEAVE_DEPTH:
44 assert(block_size_B == 4);
45 for (unsigned i = 0; i < size_bl; i++)
46 pan_access_image_pixel(dst + i * 4, src + i * 4, 4, interleave,
47 is_store);
48 break;
49 case PAN_INTERLEAVE_STENCIL:
50 assert(block_size_B == 4);
51 for (unsigned i = 0; i < size_bl; i++)
52 pan_access_image_pixel(dst + i * 4, src + i, 4, interleave, is_store);
53 break;
54 }
55 }
56
57 /* Copy either memory->image or image->memory. The direction is controlled by
58 * the memory_to_img argument. */
59 static void
60 panvk_copy_image_to_from_memory(struct image_params img,
61 struct memory_params mem,
62 VkExtent3D extent, VkHostImageCopyFlags flags,
63 bool memory_to_img)
64 {
65 /* AFBC should be disabled on images used for host image copy */
66 assert(img.img->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR ||
67 img.img->vk.drm_format_mod == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
68 bool linear = img.img->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR;
69
70 /* We don't have to care about the multisample layout for image/memory
71 * copies. From the Vulkan 1.4.317 spec:
72 *
73 * VUID-VkCopyImageToMemoryInfo-srcImage-07973 srcImage must have a sample
74 * count equal to VK_SAMPLE_COUNT_1_BIT
75 *
76 * VUID-VkCopyMemoryToImageInfo-dstImage-07973 dstImage must have a sample
77 * count equal to VK_SAMPLE_COUNT_1_BIT
78 */
79 assert(img.img->vk.samples == VK_SAMPLE_COUNT_1_BIT);
80
81 /* From the Vulkan 1.4.317 spec:
82 *
83 * VUID-VkImageToMemoryCopy-aspectMask-09103 The aspectMask member of
84 * imageSubresource must only have a single bit set
85 */
86 assert(util_bitcount(img.subres.aspectMask) == 1);
87 unsigned plane_idx =
88 panvk_plane_index(img.img, img.subres.aspectMask);
89 assert(plane_idx < PANVK_MAX_PLANES);
90 struct panvk_image_plane *plane = &img.img->planes[plane_idx];
91 const struct pan_image_layout *plane_layout = &plane->plane.layout;
92 const struct pan_image_slice_layout *slice_layout =
93 &plane_layout->slices[img.subres.mipLevel];
94
95 /* D24S8 is a special case because the aspects are interleaved in a single
96 * plane */
97 VkFormat vkfmt = panvk_image_is_interleaved_depth_stencil(img.img)
98 ? img.img->vk.format
99 : vk_format_get_aspect_format(img.img->vk.format,
100 img.subres.aspectMask);
101 enum pipe_format pfmt = vk_format_to_pipe_format(vkfmt);
102 const struct util_format_description *fmt = util_format_description(pfmt);
103
104 enum pan_interleave_zs interleave = pan_get_interleave_zs(
105 pfmt, img.subres.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT,
106 img.subres.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
107
108 unsigned block_width_px = fmt->block.width;
109 unsigned block_height_px = fmt->block.height;
110 assert(fmt->block.bits % 8 == 0);
111 unsigned block_size_B = fmt->block.bits / 8;
112 /* With stencil interleave, the memory element size will be smaller than the
113 * image block size */
114 if (interleave != PAN_INTERLEAVE_STENCIL)
115 assert(mem.layout.element_size_B == block_size_B);
116
117 unsigned row_size_bl = DIV_ROUND_UP(extent.width, block_width_px);
118
119 unsigned layer_count =
120 vk_image_subresource_layer_count(&img.img->vk, &img.subres);
121
122 void *img_base_ptr = img.ptr + plane->mem_offset + slice_layout->offset_B;
123
124 for (unsigned layer = 0; layer < layer_count; layer++) {
125 unsigned img_layer = layer + img.subres.baseArrayLayer;
126 void *img_layer_ptr = img_base_ptr +
127 img_layer * plane_layout->array_stride_B;
128 void *mem_layer_ptr = mem.ptr + layer * mem.layout.image_stride_B;
129
130 if (flags & VK_HOST_IMAGE_COPY_MEMCPY_BIT) {
131 /* For depth/stencil interleave, we can't use a plain memcpy, but we
132 * can still get some performance benefit by skipping (de)tiling and
133 * strided copy logic. */
134 panvk_interleaved_copy(img_layer_ptr, mem_layer_ptr,
135 slice_layout->size_B / block_size_B,
136 block_size_B,interleave, memory_to_img);
137 continue;
138 }
139
140 for (unsigned z = 0; z < extent.depth; z++) {
141 unsigned img_z = z + img.offset.z;
142 void *img_depth_ptr = img_layer_ptr +
143 img_z * slice_layout->tiled_or_linear.surface_stride_B;
144 /* There is no distinction between array and 3D images in the memory
145 * layout, image_stride_B applies to both */
146 void *mem_depth_ptr = mem_layer_ptr + z * mem.layout.image_stride_B;
147
148 if (linear) {
149 for (unsigned y = 0; y < extent.height; y += block_height_px) {
150 unsigned img_y_bl = (y + img.offset.y) / block_height_px;
151 unsigned mem_y_bl = y / block_height_px;
152 void *img_row_ptr = img_depth_ptr +
153 img_y_bl * slice_layout->tiled_or_linear.row_stride_B;
154 void *mem_row_ptr = mem_depth_ptr +
155 mem_y_bl * mem.layout.row_stride_B;
156
157 unsigned img_x_bl = img.offset.x / block_width_px;
158 void *img_block_ptr = img_row_ptr + img_x_bl * block_size_B;
159
160 panvk_interleaved_copy(img_block_ptr, mem_row_ptr,
161 row_size_bl, block_size_B, interleave,
162 memory_to_img);
163 }
164 } else {
165 if (memory_to_img)
166 pan_store_tiled_image(
167 img_depth_ptr, mem_depth_ptr,
168 img.offset.x, img.offset.y, extent.width, extent.height,
169 slice_layout->tiled_or_linear.row_stride_B,
170 mem.layout.row_stride_B,
171 pfmt, interleave);
172 else
173 pan_load_tiled_image(
174 mem_depth_ptr, img_depth_ptr,
175 img.offset.x, img.offset.y, extent.width, extent.height,
176 mem.layout.row_stride_B,
177 slice_layout->tiled_or_linear.row_stride_B,
178 pfmt, interleave);
179 }
180 }
181 }
182 }
183
184 static void
185 img_to_from_mem_with_ds_split(struct image_params img, struct memory_params mem,
186 VkExtent3D extent, VkHostImageCopyFlags flags,
187 bool memory_to_img)
188 {
189 if (img.subres.aspectMask ==
190 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) &&
191 !panvk_image_is_interleaved_depth_stencil(img.img)) {
192 img.subres.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
193 panvk_copy_image_to_from_memory(img, mem, extent, flags, memory_to_img);
194 img.subres.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
195 panvk_copy_image_to_from_memory(img, mem, extent, flags, memory_to_img);
196 } else {
197 panvk_copy_image_to_from_memory(img, mem, extent, flags, memory_to_img);
198 }
199 }
200
201 static void
202 panvk_copy_memory_to_image(struct panvk_image *dst, struct pan_kmod_bo *dst_bo,
203 void *dst_cpu, const VkMemoryToImageCopy *region,
204 VkHostImageCopyFlags flags)
205 {
206 struct memory_params src_params = {
207 /* Casting away const, but we don't write to it so it's fine */
208 .ptr = (void *) region->pHostPointer,
209 .layout = vk_memory_to_image_copy_layout(&dst->vk, region),
210 };
211 struct image_params dst_params = {
212 .img = dst,
213 .bo = dst_bo,
214 .ptr = dst_cpu,
215 .offset = region->imageOffset,
216 .subres = region->imageSubresource,
217 };
218
219 img_to_from_mem_with_ds_split(dst_params, src_params, region->imageExtent,
220 flags, true);
221 }
222
223 static VkResult
224 mmap_plane(struct panvk_image *img, uint8_t p, int prot,
225 void *plane_ptrs[static const PANVK_MAX_PLANES])
226 {
227 assert(p < PANVK_MAX_PLANES);
228
229 if (plane_ptrs[p])
230 return VK_SUCCESS;
231
232 plane_ptrs[p] =
233 pan_kmod_bo_mmap(img->planes[p].mem->bo, prot, MAP_SHARED, NULL);
234
235 if (plane_ptrs[p] == MAP_FAILED) {
236 plane_ptrs[p] = NULL;
237 return panvk_errorf(img->vk.base.device, VK_ERROR_MEMORY_MAP_FAILED,
238 "Failed to CPU map image");
239 }
240
241 /* In case of a multi-planar and !disjoint image (or disjoint but with some
242 * planes pointing to the same memory object), we propagate the BO mapping to
243 * all relevant entries, so we don't have to mmap the same BO to different
244 * location if another plane is copied. */
245 for (uint8_t i = 0; i < PANVK_MAX_PLANES; i++) {
246 if (p != i && img->planes[p].mem == img->planes[i].mem)
247 plane_ptrs[i] = plane_ptrs[p];
248 }
249
250 return VK_SUCCESS;
251 }
252
253 static void
254 munmap_planes(struct panvk_image *img,
255 void *plane_ptrs[static const PANVK_MAX_PLANES])
256 {
257 for (uint8_t i = 0; i < PANVK_MAX_PLANES; i++) {
258 if (!plane_ptrs[i])
259 continue;
260
261 /* No need to call pan_kmod_flush_bo_map_syncs() even if we've written to
262 * the image. This will be done just before the next submit. */
263 ASSERTED int ret =
264 os_munmap(plane_ptrs[i], pan_kmod_bo_size(img->planes[i].mem->bo));
265 assert(!ret);
266
267 /* Make sure we reset all mapping entries pointing to the same virtual
268 * address so we don't end up with double-munmap() cases. */
269 for (uint8_t j = i; j < PANVK_MAX_PLANES; j++) {
270 if (plane_ptrs[i] == plane_ptrs[j])
271 plane_ptrs[j] = NULL;
272 }
273
274 plane_ptrs[i] = NULL;
275 }
276 }
277
278 static void
279 image_sync_subres(struct panvk_image *img, void *plane_cpu_ptr,
280 const VkImageSubresourceLayers *subres,
281 enum pan_kmod_bo_sync_type type)
282 {
283 unsigned plane_idx =
284 panvk_plane_index(img, subres->aspectMask);
285 assert(plane_idx < PANVK_MAX_PLANES);
286 struct panvk_image_plane *plane = &img->planes[plane_idx];
287 const struct pan_image_layout *plane_layout = &plane->plane.layout;
288 const struct pan_image_slice_layout *slice_layout =
289 &plane_layout->slices[subres->mipLevel];
290 unsigned layer_count = vk_image_subresource_layer_count(&img->vk, subres);
291 uint32_t base = plane->mem_offset + slice_layout->offset_B;
292
293 for (unsigned layer = 0; layer < layer_count; layer++) {
294 unsigned img_layer = layer + subres->baseArrayLayer;
295 uint64_t offset = base + (img_layer * plane_layout->array_stride_B);
296
297 pan_kmod_queue_bo_map_sync(plane->mem->bo, offset, plane_cpu_ptr + offset,
298 slice_layout->size_B, type);
299 }
300 }
301
302 VKAPI_ATTR VkResult VKAPI_CALL
303 panvk_CopyMemoryToImage(VkDevice device, const VkCopyMemoryToImageInfo *info)
304 {
305 VK_FROM_HANDLE(panvk_image, dst, info->dstImage);
306 void *dst_cpu[PANVK_MAX_PLANES] = {NULL};
307 VkResult result = VK_SUCCESS;
308
309 for (unsigned i = 0; i < info->regionCount; i++) {
310 uint8_t p =
311 panvk_plane_index(dst, info->pRegions[i].imageSubresource.aspectMask);
312
313 result = mmap_plane(dst, p, PROT_WRITE, dst_cpu);
314 if (result != VK_SUCCESS)
315 goto out_unmap;
316
317 panvk_copy_memory_to_image(dst, dst->planes[p].mem->bo, dst_cpu[p],
318 &info->pRegions[i], info->flags);
319 image_sync_subres(dst, dst_cpu[p], &info->pRegions[i].imageSubresource,
320 PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH);
321 }
322
323 out_unmap:
324 munmap_planes(dst, dst_cpu);
325 return result;
326 }
327
328 static void
329 panvk_copy_image_to_memory(struct panvk_image *src,
330 struct pan_kmod_bo *src_bo,
331 void *src_cpu,
332 const VkImageToMemoryCopy *region,
333 VkHostImageCopyFlags flags)
334 {
335 struct memory_params dst_params = {
336 .ptr = region->pHostPointer,
337 .layout = vk_image_to_memory_copy_layout(&src->vk, region),
338 };
339 struct image_params src_params = {
340 .img = src,
341 .bo = src_bo,
342 .ptr = src_cpu,
343 .offset = region->imageOffset,
344 .subres = region->imageSubresource,
345 };
346
347 img_to_from_mem_with_ds_split(src_params, dst_params, region->imageExtent,
348 flags, false);
349 }
350
351 VKAPI_ATTR VkResult VKAPI_CALL
352 panvk_CopyImageToMemory(VkDevice device, const VkCopyImageToMemoryInfo *info)
353 {
354 VK_FROM_HANDLE(panvk_device, dev, device);
355 VK_FROM_HANDLE(panvk_image, src, info->srcImage);
356 void *src_cpu[PANVK_MAX_PLANES] = {NULL};
357 VkResult result = VK_SUCCESS;
358
359 for (unsigned i = 0; i < info->regionCount; i++) {
360 uint8_t p =
361 panvk_plane_index(src, info->pRegions[i].imageSubresource.aspectMask);
362
363 result = mmap_plane(src, p, PROT_READ, src_cpu);
364 if (result != VK_SUCCESS)
365 goto out_unmap;
366
367 image_sync_subres(src, src_cpu[p], &info->pRegions[i].imageSubresource,
368 PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE);
369 }
370
371 pan_kmod_flush_bo_map_syncs(dev->kmod.dev);
372
373 for (unsigned i = 0; i < info->regionCount; i++) {
374 uint8_t p =
375 panvk_plane_index(src, info->pRegions[i].imageSubresource.aspectMask);
376
377 panvk_copy_image_to_memory(src, src->planes[p].mem->bo, src_cpu[p],
378 &info->pRegions[i], info->flags);
379 }
380
381 out_unmap:
382 munmap_planes(src, src_cpu);
383 return result;
384 }
385
386 static void
387 panvk_copy_image_to_image(struct panvk_image *dst,
388 struct pan_kmod_bo *dst_bo, void *dst_cpu,
389 struct panvk_image *src,
390 struct pan_kmod_bo *src_bo, void *src_cpu,
391 const VkImageCopy2 *region,
392 VkHostImageCopyFlags flags)
393 {
394 /* AFBC should be disabled on images used for host image copy */
395 assert(src->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR ||
396 src->vk.drm_format_mod == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
397 assert(dst->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR ||
398 dst->vk.drm_format_mod == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
399 bool src_linear = src->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR;
400 bool dst_linear = dst->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR;
401
402 VkImageSubresourceLayers src_subres = region->srcSubresource;
403 VkImageSubresourceLayers dst_subres = region->dstSubresource;
404
405 unsigned src_plane_idx = panvk_plane_index(src, src_subres.aspectMask);
406 unsigned dst_plane_idx = panvk_plane_index(dst, dst_subres.aspectMask);
407 assert(src_plane_idx < PANVK_MAX_PLANES);
408 assert(dst_plane_idx < PANVK_MAX_PLANES);
409 struct panvk_image_plane *src_plane = &src->planes[src_plane_idx];
410 struct panvk_image_plane *dst_plane = &dst->planes[dst_plane_idx];
411 const struct pan_image_layout *src_plane_layout = &dst_plane->plane.layout;
412 const struct pan_image_layout *dst_plane_layout = &src_plane->plane.layout;
413 const struct pan_image_slice_layout *src_slice_layout =
414 &src_plane_layout->slices[src_subres.mipLevel];
415 const struct pan_image_slice_layout *dst_slice_layout =
416 &dst_plane_layout->slices[dst_subres.mipLevel];
417
418 VkFormat src_vkfmt =
419 vk_format_get_aspect_format(src->vk.format, src_subres.aspectMask);
420 VkFormat dst_vkfmt =
421 vk_format_get_aspect_format(dst->vk.format, dst_subres.aspectMask);
422 enum pipe_format src_pfmt = vk_format_to_pipe_format(src_vkfmt);
423 enum pipe_format dst_pfmt = vk_format_to_pipe_format(dst_vkfmt);
424 const struct util_format_description *src_fmt =
425 util_format_description(src_pfmt);
426 ASSERTED const struct util_format_description *dst_fmt =
427 util_format_description(dst_pfmt);
428
429 unsigned block_width_px = src_fmt->block.width;
430 unsigned block_height_px = src_fmt->block.height;
431 assert(src_fmt->block.bits % 8 == 0);
432 unsigned block_size_B = src_fmt->block.bits / 8;
433
434 /* This doesn't actually seem to be a requirement in the spec, but that's
435 * probably unintentional */
436 assert(dst_fmt->block.width == block_width_px);
437 assert(dst_fmt->block.height == block_height_px);
438 assert(dst_fmt->block.bits == src_fmt->block.bits);
439
440 unsigned row_size_bl = DIV_ROUND_UP(region->extent.width, block_width_px);
441 unsigned row_size_B = row_size_bl * block_size_B;
442
443 unsigned src_layer_count =
444 vk_image_subresource_layer_count(&src->vk, &src_subres);
445 ASSERTED unsigned dst_layer_count =
446 vk_image_subresource_layer_count(&dst->vk, &dst_subres);
447 /* This also is not explicitly required in the spec */
448 assert(src_layer_count == dst_layer_count);
449 unsigned layer_count = src_layer_count;
450
451 unsigned sample_count = src->vk.samples;
452 /* This also is not explicitly required in the spec */
453 assert(dst->vk.samples == sample_count);
454 /* Multisampled images are implemented as 3D */
455 unsigned depth = sample_count > 1 ? sample_count : region->extent.depth;
456
457 void *src_base_ptr =
458 src_cpu + src_plane->mem_offset + src_slice_layout->offset_B;
459 void *dst_base_ptr =
460 dst_cpu + dst_plane->mem_offset + dst_slice_layout->offset_B;
461
462 for (unsigned layer = 0; layer < layer_count; layer++) {
463 unsigned src_layer = layer + src_subres.baseArrayLayer;
464 unsigned dst_layer = layer + dst_subres.baseArrayLayer;
465 void *src_layer_ptr = src_base_ptr +
466 src_layer * src_slice_layout->tiled_or_linear.surface_stride_B;
467 void *dst_layer_ptr = dst_base_ptr +
468 dst_layer * dst_slice_layout->tiled_or_linear.surface_stride_B;
469
470 if (flags & VK_HOST_IMAGE_COPY_MEMCPY_BIT) {
471 assert(src_slice_layout->size_B == dst_slice_layout->size_B);
472 memcpy(dst_layer_ptr, src_layer_ptr, src_slice_layout->size_B);
473 continue;
474 }
475
476 for (unsigned z = 0; z < depth; z++) {
477 unsigned src_z = z + region->srcOffset.z;
478 unsigned dst_z = z + region->dstOffset.z;
479 void *src_depth_ptr = src_layer_ptr +
480 src_z * src_slice_layout->tiled_or_linear.surface_stride_B;
481 void *dst_depth_ptr = dst_layer_ptr +
482 dst_z * dst_slice_layout->tiled_or_linear.surface_stride_B;
483
484 if (src_linear && dst_linear) {
485 for (unsigned y = 0; y < region->extent.height;
486 y += block_height_px) {
487 unsigned src_y_bl = (y + region->srcOffset.y) / block_height_px;
488 unsigned dst_y_bl = (y + region->dstOffset.y) / block_height_px;
489 unsigned src_x_bl = region->srcOffset.x / block_width_px;
490 unsigned dst_x_bl = region->dstOffset.x / block_width_px;
491 void *src_row_ptr = src_depth_ptr +
492 src_y_bl * src_slice_layout->tiled_or_linear.row_stride_B +
493 src_x_bl * block_size_B;
494 void *dst_row_ptr = dst_depth_ptr +
495 dst_y_bl * dst_slice_layout->tiled_or_linear.row_stride_B +
496 dst_x_bl * block_size_B;
497
498 memcpy(dst_row_ptr, src_row_ptr, row_size_B);
499 }
500 } else if (src_linear && !dst_linear) {
501 unsigned src_y_bl = region->srcOffset.y / block_height_px;
502 unsigned src_x_bl = region->srcOffset.x / block_width_px;
503 void *src_row_ptr = src_depth_ptr +
504 src_y_bl * src_slice_layout->tiled_or_linear.row_stride_B +
505 src_x_bl * block_size_B;
506 pan_store_tiled_image(
507 dst_depth_ptr, src_row_ptr,
508 region->dstOffset.x, region->dstOffset.y,
509 region->extent.width, region->extent.height,
510 dst_slice_layout->tiled_or_linear.row_stride_B,
511 src_slice_layout->tiled_or_linear.row_stride_B,
512 src_pfmt, PAN_INTERLEAVE_NONE);
513 } else if (!src_linear && dst_linear) {
514 unsigned dst_y_bl = region->dstOffset.y / block_height_px;
515 unsigned dst_x_bl = region->dstOffset.x / block_width_px;
516 void *dst_row_ptr = dst_depth_ptr +
517 dst_y_bl * dst_slice_layout->tiled_or_linear.row_stride_B +
518 dst_x_bl * block_size_B;
519 pan_load_tiled_image(
520 dst_row_ptr, src_depth_ptr,
521 region->srcOffset.x, region->srcOffset.y,
522 region->extent.width, region->extent.height,
523 dst_slice_layout->tiled_or_linear.row_stride_B,
524 src_slice_layout->tiled_or_linear.row_stride_B,
525 dst_pfmt, PAN_INTERLEAVE_NONE);
526 } else {
527 pan_copy_tiled_image(
528 dst_depth_ptr, src_depth_ptr, region->dstOffset.x,
529 region->dstOffset.y, region->srcOffset.x, region->srcOffset.y,
530 region->extent.width, region->extent.height,
531 dst_slice_layout->tiled_or_linear.row_stride_B,
532 src_slice_layout->tiled_or_linear.row_stride_B, src_pfmt);
533 }
534 }
535 }
536 }
537
538 VKAPI_ATTR VkResult VKAPI_CALL
539 panvk_CopyImageToImage(VkDevice device, const VkCopyImageToImageInfo *info)
540 {
541 VkResult result = VK_SUCCESS;
542
543 VK_FROM_HANDLE(panvk_image, dst, info->dstImage);
544 VK_FROM_HANDLE(panvk_image, src, info->srcImage);
545 struct panvk_device *dev = to_panvk_device(dst->vk.base.device);
546 void *src_cpu[PANVK_MAX_PLANES] = {NULL};
547 void *dst_cpu[PANVK_MAX_PLANES] = {NULL};
548
549 for (unsigned i = 0; i < info->regionCount; i++) {
550 u_foreach_bit(a, info->pRegions[i].srcSubresource.aspectMask) {
551 VkImageSubresourceLayers subres = info->pRegions[i].srcSubresource;
552 subres.aspectMask = 1 << a;
553 uint8_t src_p = panvk_plane_index(src, subres.aspectMask);
554
555
556 result = mmap_plane(src, src_p, PROT_READ, src_cpu);
557 if (result != VK_SUCCESS)
558 goto out_unmap;
559
560 image_sync_subres(src, src_cpu[src_p], &subres,
561 PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE);
562 }
563
564 u_foreach_bit(a, info->pRegions[i].dstSubresource.aspectMask) {
565 uint8_t dst_p = panvk_plane_index(dst, 1 << a);
566
567 result = mmap_plane(dst, dst_p, PROT_WRITE, dst_cpu);
568 if (result != VK_SUCCESS)
569 goto out_unmap;
570 }
571 }
572
573 /* Flush invalidates before reading the source. */
574 pan_kmod_flush_bo_map_syncs(dev->kmod.dev);
575
576 for (unsigned i = 0; i < info->regionCount; i++) {
577 bool depth_and_stencil =
578 info->pRegions[i].srcSubresource.aspectMask ==
579 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
580
581 if (depth_and_stencil) {
582 VkImageCopy2 region = info->pRegions[i];
583 uint8_t src_p, dst_p;
584
585 assert(info->pRegions[i].srcSubresource.aspectMask ==
586 info->pRegions[i].dstSubresource.aspectMask);
587 region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
588 region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
589 src_p = panvk_plane_index(src, VK_IMAGE_ASPECT_DEPTH_BIT);
590 dst_p = panvk_plane_index(dst, VK_IMAGE_ASPECT_DEPTH_BIT);
591 panvk_copy_image_to_image(
592 dst, dst->planes[dst_p].mem->bo, dst_cpu[dst_p], src,
593 src->planes[src_p].mem->bo, src_cpu[src_p], &region, info->flags);
594 image_sync_subres(dst, dst_cpu[dst_p], &region.dstSubresource,
595 PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH);
596
597 region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
598 region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
599 src_p = panvk_plane_index(src, VK_IMAGE_ASPECT_STENCIL_BIT);
600 dst_p = panvk_plane_index(dst, VK_IMAGE_ASPECT_STENCIL_BIT);
601 panvk_copy_image_to_image(
602 dst, dst->planes[dst_p].mem->bo, dst_cpu[dst_p], src,
603 src->planes[src_p].mem->bo, src_cpu[src_p], &region, info->flags);
604 image_sync_subres(dst, dst_cpu[dst_p], &region.dstSubresource,
605 PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH);
606 } else {
607 assert(
608 util_bitcount(info->pRegions[i].srcSubresource.aspectMask) == 1 &&
609 util_bitcount(info->pRegions[i].dstSubresource.aspectMask) == 1);
610
611 uint8_t src_p =
612 panvk_plane_index(src, info->pRegions[i].srcSubresource.aspectMask);
613 uint8_t dst_p =
614 panvk_plane_index(dst, info->pRegions[i].dstSubresource.aspectMask);
615
616 panvk_copy_image_to_image(dst, dst->planes[dst_p].mem->bo,
617 dst_cpu[dst_p], src,
618 src->planes[src_p].mem->bo, src_cpu[src_p],
619 &info->pRegions[i], info->flags);
620 image_sync_subres(dst, dst_cpu[dst_p],
621 &info->pRegions[i].dstSubresource,
622 PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH);
623 }
624 }
625
626 out_unmap:
627 munmap_planes(src, src_cpu);
628 munmap_planes(dst, dst_cpu);
629 return result;
630 }
631
632 VKAPI_ATTR VkResult VKAPI_CALL
633 panvk_TransitionImageLayout(VkDevice device, uint32_t transitionCount,
634 const VkHostImageLayoutTransitionInfo *transitions)
635 {
636 /* We don't use image layouts, this is a no-op */
637 return VK_SUCCESS;
638 }