Debian Codegraph @

src/kosmickrisp/vulkan/kk_cmd_copy.c

raw symbols SCIP
1 /*
2 * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3 * Copyright 2025 LunarG, Inc.
4 * Copyright 2025 Google LLC
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "kk_cmd_buffer.h"
9
10 #include "kk_bo.h"
11 #include "kk_buffer.h"
12 #include "kk_device.h"
13 #include "kk_encoder.h"
14 #include "kk_entrypoints.h"
15 #include "kk_physical_device.h"
16
17 #include "kosmickrisp/bridge/mtl_bridge.h"
18 #include "kosmickrisp/bridge/vk_to_mtl_map.h"
19
20 #include "util/format/u_format.h"
21
22 VKAPI_ATTR void VKAPI_CALL
23 kk_CmdCopyBuffer2(VkCommandBuffer commandBuffer,
24 const VkCopyBufferInfo2 *pCopyBufferInfo)
25 {
26 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
27 VK_FROM_HANDLE(kk_buffer, src, pCopyBufferInfo->srcBuffer);
28 VK_FROM_HANDLE(kk_buffer, dst, pCopyBufferInfo->dstBuffer);
29
30 mtl_blit_encoder *blit = kk_blit_encoder(cmd);
31 for (uint32_t i = 0; i < pCopyBufferInfo->regionCount; i++) {
32 const VkBufferCopy2 *region = &pCopyBufferInfo->pRegions[i];
33 mtl_copy_from_buffer_to_buffer(blit, src->mtl_handle, region->srcOffset,
34 dst->mtl_handle, region->dstOffset,
35 region->size);
36 }
37 }
38
39 struct kk_buffer_image_copy_info {
40 struct mtl_buffer_image_copy mtl_data;
41 size_t buffer_slice_size_B;
42 };
43
44 static struct kk_buffer_image_copy_info
45 vk_buffer_image_copy_to_mtl_buffer_image_copy(
46 const VkBufferImageCopy2 *region, const struct kk_image_plane *plane)
47 {
48 struct kk_buffer_image_copy_info copy;
49 enum pipe_format p_format = plane->layout.format.pipe;
50 if (region->imageSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT) {
51 copy.mtl_data.options = MTL_BLIT_OPTION_DEPTH_FROM_DEPTH_STENCIL;
52 p_format = util_format_get_depth_only(p_format);
53 } else if (region->imageSubresource.aspectMask ==
54 VK_IMAGE_ASPECT_STENCIL_BIT) {
55 copy.mtl_data.options = MTL_BLIT_OPTION_STENCIL_FROM_DEPTH_STENCIL;
56 p_format = PIPE_FORMAT_S8_UINT;
57 } else
58 copy.mtl_data.options = MTL_BLIT_OPTION_NONE;
59
60 const uint32_t buffer_width = region->bufferRowLength
61 ? region->bufferRowLength
62 : region->imageExtent.width;
63 const uint32_t buffer_height = region->bufferImageHeight
64 ? region->bufferImageHeight
65 : region->imageExtent.height;
66
67 const uint32_t buffer_stride_B =
68 util_format_get_stride(p_format, buffer_width);
69 const uint32_t buffer_size_2d_B =
70 util_format_get_2d_size(p_format, buffer_stride_B, buffer_height);
71
72 /* Metal requires this value to be 0 for 2D images, otherwise the number of
73 * bytes between each 2D image of a 3D texture */
74 copy.mtl_data.buffer_2d_image_size_B =
75 plane->layout.depth_px == 1u ? 0u : buffer_size_2d_B;
76 copy.mtl_data.buffer_stride_B = buffer_stride_B;
77 copy.mtl_data.image_size = vk_extent_3d_to_mtl_size(&region->imageExtent);
78 copy.mtl_data.image_origin =
79 vk_offset_3d_to_mtl_origin(&region->imageOffset);
80 copy.mtl_data.image_level = region->imageSubresource.mipLevel;
81 copy.buffer_slice_size_B = buffer_size_2d_B;
82
83 return copy;
84 }
85
86 #define kk_foreach_slice(ndx, image, subresource_member) \
87 for (uint32_t ndx = region->subresource_member.baseArrayLayer; \
88 ndx < (region->subresource_member.baseArrayLayer + \
89 vk_image_subresource_layer_count(&image->vk, \
90 &region->subresource_member)); \
91 ++ndx)
92
93 VKAPI_ATTR void VKAPI_CALL
94 kk_CmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
95 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo)
96 {
97 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
98 VK_FROM_HANDLE(kk_buffer, buffer, pCopyBufferToImageInfo->srcBuffer);
99 VK_FROM_HANDLE(kk_image, image, pCopyBufferToImageInfo->dstImage);
100
101 mtl_blit_encoder *blit = kk_blit_encoder(cmd);
102 for (int r = 0; r < pCopyBufferToImageInfo->regionCount; r++) {
103 const VkBufferImageCopy2 *region = &pCopyBufferToImageInfo->pRegions[r];
104 const uint8_t plane_index = kk_image_memory_aspects_to_plane(
105 image, region->imageSubresource.aspectMask);
106 struct kk_image_plane *plane = &image->planes[plane_index];
107 struct kk_buffer_image_copy_info info =
108 vk_buffer_image_copy_to_mtl_buffer_image_copy(region, plane);
109 info.mtl_data.buffer = buffer->mtl_handle;
110 info.mtl_data.image = plane->mtl_handle;
111 size_t buffer_offset = region->bufferOffset;
112
113 kk_foreach_slice(slice, image, imageSubresource)
114 {
115 info.mtl_data.image_slice = slice;
116 info.mtl_data.buffer_offset_B = buffer_offset;
117 mtl_copy_from_buffer_to_texture(blit, &info.mtl_data);
118 buffer_offset += info.buffer_slice_size_B;
119 }
120 }
121 }
122
123 VKAPI_ATTR void VKAPI_CALL
124 kk_CmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
125 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo)
126 {
127 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
128 VK_FROM_HANDLE(kk_image, image, pCopyImageToBufferInfo->srcImage);
129 VK_FROM_HANDLE(kk_buffer, buffer, pCopyImageToBufferInfo->dstBuffer);
130
131 mtl_blit_encoder *blit = kk_blit_encoder(cmd);
132 for (unsigned r = 0; r < pCopyImageToBufferInfo->regionCount; r++) {
133 const VkBufferImageCopy2 *region = &pCopyImageToBufferInfo->pRegions[r];
134 const uint8_t plane_index = kk_image_memory_aspects_to_plane(
135 image, region->imageSubresource.aspectMask);
136 struct kk_image_plane *plane = &image->planes[plane_index];
137 struct kk_buffer_image_copy_info info =
138 vk_buffer_image_copy_to_mtl_buffer_image_copy(region, plane);
139 info.mtl_data.buffer = buffer->mtl_handle;
140 info.mtl_data.image = plane->mtl_handle;
141 size_t buffer_offset = region->bufferOffset;
142
143 kk_foreach_slice(slice, image, imageSubresource)
144 {
145 info.mtl_data.image_slice = slice;
146 info.mtl_data.buffer_offset_B = buffer_offset;
147 mtl_copy_from_texture_to_buffer(blit, &info.mtl_data);
148 buffer_offset += info.buffer_slice_size_B;
149 }
150 }
151 }
152
153 /* Copies images by doing a texture->buffer->texture transfer. Returns the new
154 * buffer offset for subsequent copies. */
155 static size_t
156 copy_through_buffer(struct kk_cmd_buffer *cmd, struct kk_image *src,
157 uint32_t src_index, struct kk_image *dst,
158 uint32_t dst_index, mtl_buffer *buffer,
159 size_t buffer_offset, const VkImageCopy2 *region)
160 {
161 struct kk_image_plane *src_plane = &src->planes[src_index];
162 struct kk_image_plane *dst_plane = &dst->planes[dst_index];
163 enum pipe_format src_format = src_plane->layout.format.pipe;
164 enum pipe_format dst_format = dst_plane->layout.format.pipe;
165 bool is_src_compressed = util_format_is_compressed(src_format);
166 bool is_dst_compressed = util_format_is_compressed(dst_format);
167 /* We shouldn't do any depth/stencil through this path */
168 assert(!util_format_is_depth_or_stencil(src_format) ||
169 !util_format_is_depth_or_stencil(dst_format));
170 mtl_blit_encoder *blit = kk_blit_encoder(cmd);
171
172 uint32_t mip_level = region->srcSubresource.mipLevel;
173 const uint32_t mip_width = u_minify(src_plane->layout.width_px, mip_level);
174 const uint32_t mip_height = u_minify(src_plane->layout.height_px, mip_level);
175 const uint32_t stride_B = util_format_get_stride(src_format, mip_width);
176 const uint32_t size_2d_B =
177 util_format_get_2d_size(src_format, stride_B, mip_height);
178 const uint32_t buffer_stride_B =
179 util_format_get_stride(src_format, region->extent.width);
180 const uint32_t buffer_size_2d_B = util_format_get_2d_size(
181 src_format, buffer_stride_B, region->extent.height);
182
183 struct kk_buffer_image_copy_info info;
184
185 /* Metal requires this value to be 0 for 2D images, otherwise the number
186 * of bytes between each 2D image of a 3D texture */
187 info.mtl_data.buffer_2d_image_size_B =
188 src_plane->layout.depth_px == 1u ? 0u : size_2d_B;
189 info.mtl_data.buffer_stride_B = buffer_stride_B;
190 info.mtl_data.image_level = mip_level;
191 info.mtl_data.buffer = buffer;
192 info.mtl_data.options = MTL_BLIT_OPTION_NONE;
193 info.buffer_slice_size_B = buffer_size_2d_B;
194 struct mtl_size src_size = vk_extent_3d_to_mtl_size(&region->extent);
195 struct mtl_size dst_size = vk_extent_3d_to_mtl_size(&region->extent);
196 /* Need to adjust size to block dimensions */
197 if (is_src_compressed) {
198 dst_size.x /= util_format_get_blockwidth(src_format);
199 dst_size.y /= util_format_get_blockheight(src_format);
200 dst_size.z /= util_format_get_blockdepth(src_format);
201 }
202 if (is_dst_compressed) {
203 dst_size.x *= util_format_get_blockwidth(dst_format);
204 dst_size.y *= util_format_get_blockheight(dst_format);
205 dst_size.z *= util_format_get_blockdepth(dst_format);
206 }
207 struct mtl_origin src_origin =
208 vk_offset_3d_to_mtl_origin(&region->srcOffset);
209 struct mtl_origin dst_origin =
210 vk_offset_3d_to_mtl_origin(&region->dstOffset);
211
212 /* Texture->Buffer->Texture */
213 // TODO_KOSMICKRISP We don't handle 3D to 2D array nor vice-versa in this
214 // path. Unsure if it's even needed, can compressed textures be 3D?
215 kk_foreach_slice(slice, src, srcSubresource)
216 {
217 info.mtl_data.image = src_plane->mtl_handle;
218 info.mtl_data.image_size = src_size;
219 info.mtl_data.image_origin = src_origin;
220 info.mtl_data.image_slice = slice;
221 info.mtl_data.buffer_offset_B = buffer_offset;
222 mtl_copy_from_texture_to_buffer(blit, &info.mtl_data);
223
224 info.mtl_data.image = dst_plane->mtl_handle;
225 info.mtl_data.image_size = dst_size;
226 info.mtl_data.image_origin = dst_origin;
227 mtl_copy_from_buffer_to_texture(blit, &info.mtl_data);
228
229 buffer_offset += info.buffer_slice_size_B;
230 }
231
232 return buffer_offset;
233 }
234
235 static bool
236 can_do_image_to_image_copy(struct kk_image *src, uint32_t src_index,
237 struct kk_image *dst, uint32_t dst_index)
238 {
239 struct kk_image_plane *src_plane = &src->planes[src_index];
240 struct kk_image_plane *dst_plane = &dst->planes[dst_index];
241 enum pipe_format src_format = src_plane->layout.format.pipe;
242 enum pipe_format dst_format = dst_plane->layout.format.pipe;
243
244 return src_format == dst_format && src_plane->layout.sample_count_sa ==
245 dst_plane->layout.sample_count_sa;
246 }
247
248 /* Copies images through Metal's texture->texture copy mechanism */
249 static void
250 copy_image(struct kk_cmd_buffer *cmd, struct kk_image *src, uint32_t src_index,
251 struct kk_image *dst, uint32_t dst_index, const VkImageCopy2 *region)
252 {
253 mtl_blit_encoder *blit = kk_blit_encoder(cmd);
254 struct kk_image_plane *src_plane = &src->planes[src_index];
255 struct kk_image_plane *dst_plane = &dst->planes[dst_index];
256
257 /* From the Vulkan 1.3.217 spec:
258 *
259 * "When copying between compressed and uncompressed formats the
260 * extent members represent the texel dimensions of the source image
261 * and not the destination."
262 */
263 const VkExtent3D extent_px =
264 vk_image_sanitize_extent(&src->vk, region->extent);
265
266 size_t src_slice = region->srcSubresource.baseArrayLayer;
267 size_t src_level = region->srcSubresource.mipLevel;
268 struct mtl_origin src_origin =
269 vk_offset_3d_to_mtl_origin(&region->srcOffset);
270 struct mtl_size size = {.x = extent_px.width,
271 .y = extent_px.height,
272 .z = extent_px.depth};
273 size_t dst_slice = region->dstSubresource.baseArrayLayer;
274 size_t dst_level = region->dstSubresource.mipLevel;
275 struct mtl_origin dst_origin =
276 vk_offset_3d_to_mtl_origin(&region->dstOffset);
277
278 /* When copying 3D to 2D layered or vice-versa, we need to change the 3D
279 * size to 2D and iterate on the layer count of the 2D image (which is the
280 * same as the depth of the 3D) and adjust origin and slice accordingly */
281 uint32_t layer_count =
282 vk_image_subresource_layer_count(&src->vk, &region->srcSubresource);
283 const uint32_t dst_layer_count =
284 vk_image_subresource_layer_count(&dst->vk, &region->dstSubresource);
285 size_t *src_increase = &src_slice;
286 size_t *dst_increase = &dst_slice;
287
288 if (layer_count < dst_layer_count) { /* 3D to 2D layered */
289 layer_count = dst_layer_count;
290 src_increase = &src_origin.z;
291 size.z = 1u;
292 } else if (dst_layer_count < layer_count) { /* 2D layered to 3D */
293 dst_increase = &dst_origin.z;
294 size.z = 1u;
295 }
296 for (uint32_t l = 0; l < layer_count;
297 ++l, ++(*src_increase), ++(*dst_increase)) {
298 mtl_copy_from_texture_to_texture(
299 blit, src_plane->mtl_handle, src_slice, src_level, src_origin, size,
300 dst_plane->mtl_handle, dst_slice, dst_level, dst_origin);
301 }
302 }
303
304 VKAPI_ATTR void VKAPI_CALL
305 kk_CmdCopyImage2(VkCommandBuffer commandBuffer,
306 const VkCopyImageInfo2 *pCopyImageInfo)
307 {
308 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
309 VK_FROM_HANDLE(kk_image, src, pCopyImageInfo->srcImage);
310 VK_FROM_HANDLE(kk_image, dst, pCopyImageInfo->dstImage);
311
312 size_t buffer_size = 0u;
313
314 /* Copy as much as we can through Metal's image to image copy that only
315 * supports same format and sample count while getting the required buffer
316 * size for image->buffer->image copy. */
317 for (uint32_t i = 0u; i < pCopyImageInfo->regionCount; ++i) {
318 const VkImageCopy2 *region = &pCopyImageInfo->pRegions[i];
319 uint8_t src_index =
320 kk_image_aspects_to_plane(src, region->srcSubresource.aspectMask);
321 uint8_t dst_index =
322 kk_image_aspects_to_plane(dst, region->dstSubresource.aspectMask);
323
324 if (can_do_image_to_image_copy(src, src_index, dst, dst_index))
325 copy_image(cmd, src, src_index, dst, dst_index, region);
326 else {
327 struct kk_image_plane *src_plane = &src->planes[src_index];
328 enum pipe_format src_format = src_plane->layout.format.pipe;
329 const uint32_t buffer_stride_B =
330 util_format_get_stride(src_format, region->extent.width);
331 const uint32_t buffer_size_2d_B = util_format_get_2d_size(
332 src_format, buffer_stride_B, region->extent.height);
333 const uint32_t layer_count =
334 vk_image_subresource_layer_count(&src->vk, &region->srcSubresource);
335 buffer_size += buffer_size_2d_B * layer_count;
336 }
337 }
338
339 /* Copy source image to buffer then to the destination image for those
340 * regions that image to image was not possible. */
341 if (buffer_size) {
342 struct kk_bo *bo = kk_cmd_allocate_buffer(cmd, buffer_size, 8);
343 size_t buffer_offset = 0u;
344 for (uint32_t i = 0u; i < pCopyImageInfo->regionCount; ++i) {
345 const VkImageCopy2 *region = &pCopyImageInfo->pRegions[i];
346 uint8_t src_index =
347 kk_image_aspects_to_plane(src, region->srcSubresource.aspectMask);
348 uint8_t dst_index =
349 kk_image_aspects_to_plane(dst, region->dstSubresource.aspectMask);
350 if (!can_do_image_to_image_copy(src, src_index, dst, dst_index))
351 buffer_offset =
352 copy_through_buffer(cmd, src, src_index, dst, dst_index, bo->map,
353 buffer_offset, region);
354 }
355 }
356 }