1f18886d2c5f56ec0923938a13e70e02bf125a26
[mesa.git] / src / amd / vulkan / radv_meta_copy.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "radv_meta.h"
25 #include "vk_format.h"
26
27 static VkExtent3D
28 meta_image_block_size(const struct radv_image *image)
29 {
30 const struct vk_format_description *desc = vk_format_description(image->vk_format);
31 return (VkExtent3D) { desc->block.width, desc->block.height, 1 };
32 }
33
34 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
35 * elements rather than texels. One element equals one texel or one block
36 * if Image is uncompressed or compressed, respectively.
37 */
38 static struct VkExtent3D
39 meta_region_extent_el(const struct radv_image *image,
40 const VkImageType imageType,
41 const struct VkExtent3D *extent)
42 {
43 const VkExtent3D block = meta_image_block_size(image);
44 return radv_sanitize_image_extent(imageType, (VkExtent3D) {
45 .width = DIV_ROUND_UP(extent->width , block.width),
46 .height = DIV_ROUND_UP(extent->height, block.height),
47 .depth = DIV_ROUND_UP(extent->depth , block.depth),
48 });
49 }
50
51 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
52 * elements rather than texels. One element equals one texel or one block
53 * if Image is uncompressed or compressed, respectively.
54 */
55 static struct VkOffset3D
56 meta_region_offset_el(const struct radv_image *image,
57 const struct VkOffset3D *offset)
58 {
59 const VkExtent3D block = meta_image_block_size(image);
60 return radv_sanitize_image_offset(image->type, (VkOffset3D) {
61 .x = offset->x / block.width,
62 .y = offset->y / block.height,
63 .z = offset->z / block.depth,
64 });
65 }
66
67 static VkFormat
68 vk_format_for_size(int bs)
69 {
70 switch (bs) {
71 case 1: return VK_FORMAT_R8_UINT;
72 case 2: return VK_FORMAT_R8G8_UINT;
73 case 4: return VK_FORMAT_R8G8B8A8_UINT;
74 case 8: return VK_FORMAT_R16G16B16A16_UINT;
75 case 12: return VK_FORMAT_R32G32B32_UINT;
76 case 16: return VK_FORMAT_R32G32B32A32_UINT;
77 default:
78 unreachable("Invalid format block size");
79 }
80 }
81
82 static struct radv_meta_blit2d_surf
83 blit_surf_for_image_level_layer(struct radv_image *image,
84 VkImageLayout layout,
85 const VkImageSubresourceLayers *subres)
86 {
87 VkFormat format = image->vk_format;
88 if (subres->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT)
89 format = vk_format_depth_only(format);
90 else if (subres->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
91 format = vk_format_stencil_only(format);
92
93 if (!radv_image_has_dcc(image) &&
94 !(radv_image_is_tc_compat_htile(image)))
95 format = vk_format_for_size(vk_format_get_blocksize(format));
96
97 return (struct radv_meta_blit2d_surf) {
98 .format = format,
99 .bs = vk_format_get_blocksize(format),
100 .level = subres->mipLevel,
101 .layer = subres->baseArrayLayer,
102 .image = image,
103 .aspect_mask = subres->aspectMask,
104 .current_layout = layout,
105 };
106 }
107
108 static void
109 meta_copy_buffer_to_image(struct radv_cmd_buffer *cmd_buffer,
110 struct radv_buffer* buffer,
111 struct radv_image* image,
112 VkImageLayout layout,
113 uint32_t regionCount,
114 const VkBufferImageCopy* pRegions)
115 {
116 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
117 struct radv_meta_saved_state saved_state;
118
119 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
120 * VK_SAMPLE_COUNT_1_BIT."
121 */
122 assert(image->info.samples == 1);
123
124 radv_meta_save(&saved_state, cmd_buffer,
125 (cs ? RADV_META_SAVE_COMPUTE_PIPELINE :
126 RADV_META_SAVE_GRAPHICS_PIPELINE) |
127 RADV_META_SAVE_CONSTANTS |
128 RADV_META_SAVE_DESCRIPTORS);
129
130 for (unsigned r = 0; r < regionCount; r++) {
131
132 /**
133 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
134 * extent is the size in texels of the source image to copy in width,
135 * height and depth. 1D images use only x and width. 2D images use x, y,
136 * width and height. 3D images use x, y, z, width, height and depth.
137 *
138 *
139 * Also, convert the offsets and extent from units of texels to units of
140 * blocks - which is the highest resolution accessible in this command.
141 */
142 const VkOffset3D img_offset_el =
143 meta_region_offset_el(image, &pRegions[r].imageOffset);
144 const VkExtent3D bufferExtent = {
145 .width = pRegions[r].bufferRowLength ?
146 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
147 .height = pRegions[r].bufferImageHeight ?
148 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
149 };
150 const VkExtent3D buf_extent_el =
151 meta_region_extent_el(image, image->type, &bufferExtent);
152
153 /* Start creating blit rect */
154 const VkExtent3D img_extent_el =
155 meta_region_extent_el(image, image->type, &pRegions[r].imageExtent);
156 struct radv_meta_blit2d_rect rect = {
157 .width = img_extent_el.width,
158 .height = img_extent_el.height,
159 };
160
161 /* Create blit surfaces */
162 struct radv_meta_blit2d_surf img_bsurf =
163 blit_surf_for_image_level_layer(image,
164 layout,
165 &pRegions[r].imageSubresource);
166
167 struct radv_meta_blit2d_buffer buf_bsurf = {
168 .bs = img_bsurf.bs,
169 .format = img_bsurf.format,
170 .buffer = buffer,
171 .offset = pRegions[r].bufferOffset,
172 .pitch = buf_extent_el.width,
173 };
174
175 if (image->type == VK_IMAGE_TYPE_3D)
176 img_bsurf.layer = img_offset_el.z;
177 /* Loop through each 3D or array slice */
178 unsigned num_slices_3d = img_extent_el.depth;
179 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
180 unsigned slice_3d = 0;
181 unsigned slice_array = 0;
182 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
183
184 rect.dst_x = img_offset_el.x;
185 rect.dst_y = img_offset_el.y;
186
187
188 /* Perform Blit */
189 if (cs)
190 radv_meta_buffer_to_image_cs(cmd_buffer, &buf_bsurf, &img_bsurf, 1, &rect);
191 else
192 radv_meta_blit2d(cmd_buffer, NULL, &buf_bsurf, &img_bsurf, 1, &rect);
193
194 /* Once we've done the blit, all of the actual information about
195 * the image is embedded in the command buffer so we can just
196 * increment the offset directly in the image effectively
197 * re-binding it to different backing memory.
198 */
199 buf_bsurf.offset += buf_extent_el.width *
200 buf_extent_el.height * buf_bsurf.bs;
201 img_bsurf.layer++;
202 if (image->type == VK_IMAGE_TYPE_3D)
203 slice_3d++;
204 else
205 slice_array++;
206 }
207 }
208
209 radv_meta_restore(&saved_state, cmd_buffer);
210 }
211
212 void radv_CmdCopyBufferToImage(
213 VkCommandBuffer commandBuffer,
214 VkBuffer srcBuffer,
215 VkImage destImage,
216 VkImageLayout destImageLayout,
217 uint32_t regionCount,
218 const VkBufferImageCopy* pRegions)
219 {
220 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
221 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
222 RADV_FROM_HANDLE(radv_buffer, src_buffer, srcBuffer);
223
224 meta_copy_buffer_to_image(cmd_buffer, src_buffer, dest_image, destImageLayout,
225 regionCount, pRegions);
226 }
227
228 static void
229 meta_copy_image_to_buffer(struct radv_cmd_buffer *cmd_buffer,
230 struct radv_buffer* buffer,
231 struct radv_image* image,
232 VkImageLayout layout,
233 uint32_t regionCount,
234 const VkBufferImageCopy* pRegions)
235 {
236 struct radv_meta_saved_state saved_state;
237
238 radv_meta_save(&saved_state, cmd_buffer,
239 RADV_META_SAVE_COMPUTE_PIPELINE |
240 RADV_META_SAVE_CONSTANTS |
241 RADV_META_SAVE_DESCRIPTORS);
242
243 for (unsigned r = 0; r < regionCount; r++) {
244
245 /**
246 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
247 * extent is the size in texels of the source image to copy in width,
248 * height and depth. 1D images use only x and width. 2D images use x, y,
249 * width and height. 3D images use x, y, z, width, height and depth.
250 *
251 *
252 * Also, convert the offsets and extent from units of texels to units of
253 * blocks - which is the highest resolution accessible in this command.
254 */
255 const VkOffset3D img_offset_el =
256 meta_region_offset_el(image, &pRegions[r].imageOffset);
257 const VkExtent3D bufferExtent = {
258 .width = pRegions[r].bufferRowLength ?
259 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
260 .height = pRegions[r].bufferImageHeight ?
261 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
262 };
263 const VkExtent3D buf_extent_el =
264 meta_region_extent_el(image, image->type, &bufferExtent);
265
266 /* Start creating blit rect */
267 const VkExtent3D img_extent_el =
268 meta_region_extent_el(image, image->type, &pRegions[r].imageExtent);
269 struct radv_meta_blit2d_rect rect = {
270 .width = img_extent_el.width,
271 .height = img_extent_el.height,
272 };
273
274 /* Create blit surfaces */
275 struct radv_meta_blit2d_surf img_info =
276 blit_surf_for_image_level_layer(image,
277 layout,
278 &pRegions[r].imageSubresource);
279
280 struct radv_meta_blit2d_buffer buf_info = {
281 .bs = img_info.bs,
282 .format = img_info.format,
283 .buffer = buffer,
284 .offset = pRegions[r].bufferOffset,
285 .pitch = buf_extent_el.width,
286 };
287
288 if (image->type == VK_IMAGE_TYPE_3D)
289 img_info.layer = img_offset_el.z;
290 /* Loop through each 3D or array slice */
291 unsigned num_slices_3d = img_extent_el.depth;
292 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
293 unsigned slice_3d = 0;
294 unsigned slice_array = 0;
295 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
296
297 rect.src_x = img_offset_el.x;
298 rect.src_y = img_offset_el.y;
299
300
301 /* Perform Blit */
302 radv_meta_image_to_buffer(cmd_buffer, &img_info, &buf_info, 1, &rect);
303
304 buf_info.offset += buf_extent_el.width *
305 buf_extent_el.height * buf_info.bs;
306 img_info.layer++;
307 if (image->type == VK_IMAGE_TYPE_3D)
308 slice_3d++;
309 else
310 slice_array++;
311 }
312 }
313
314 radv_meta_restore(&saved_state, cmd_buffer);
315 }
316
317 void radv_CmdCopyImageToBuffer(
318 VkCommandBuffer commandBuffer,
319 VkImage srcImage,
320 VkImageLayout srcImageLayout,
321 VkBuffer destBuffer,
322 uint32_t regionCount,
323 const VkBufferImageCopy* pRegions)
324 {
325 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
326 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
327 RADV_FROM_HANDLE(radv_buffer, dst_buffer, destBuffer);
328
329 meta_copy_image_to_buffer(cmd_buffer, dst_buffer, src_image,
330 srcImageLayout,
331 regionCount, pRegions);
332 }
333
334 static void
335 meta_copy_image(struct radv_cmd_buffer *cmd_buffer,
336 struct radv_image *src_image,
337 VkImageLayout src_image_layout,
338 struct radv_image *dest_image,
339 VkImageLayout dest_image_layout,
340 uint32_t regionCount,
341 const VkImageCopy *pRegions)
342 {
343 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
344 struct radv_meta_saved_state saved_state;
345
346 /* From the Vulkan 1.0 spec:
347 *
348 * vkCmdCopyImage can be used to copy image data between multisample
349 * images, but both images must have the same number of samples.
350 */
351 assert(src_image->info.samples == dest_image->info.samples);
352
353 radv_meta_save(&saved_state, cmd_buffer,
354 (cs ? RADV_META_SAVE_COMPUTE_PIPELINE :
355 RADV_META_SAVE_GRAPHICS_PIPELINE) |
356 RADV_META_SAVE_CONSTANTS |
357 RADV_META_SAVE_DESCRIPTORS);
358
359 for (unsigned r = 0; r < regionCount; r++) {
360 assert(pRegions[r].srcSubresource.aspectMask ==
361 pRegions[r].dstSubresource.aspectMask);
362
363 /* Create blit surfaces */
364 struct radv_meta_blit2d_surf b_src =
365 blit_surf_for_image_level_layer(src_image,
366 src_image_layout,
367 &pRegions[r].srcSubresource);
368
369 struct radv_meta_blit2d_surf b_dst =
370 blit_surf_for_image_level_layer(dest_image,
371 dest_image_layout,
372 &pRegions[r].dstSubresource);
373
374 uint32_t dst_queue_mask = radv_image_queue_family_mask(dest_image,
375 cmd_buffer->queue_family_index,
376 cmd_buffer->queue_family_index);
377 bool dst_compressed = radv_layout_dcc_compressed(dest_image, dest_image_layout, dst_queue_mask);
378 uint32_t src_queue_mask = radv_image_queue_family_mask(src_image,
379 cmd_buffer->queue_family_index,
380 cmd_buffer->queue_family_index);
381 bool src_compressed = radv_layout_dcc_compressed(src_image, src_image_layout, src_queue_mask);
382
383 if (!src_compressed || radv_dcc_formats_compatible(b_src.format, b_dst.format)) {
384 b_src.format = b_dst.format;
385 } else if (!dst_compressed) {
386 b_dst.format = b_src.format;
387 } else {
388 radv_decompress_dcc(cmd_buffer, dest_image, &(VkImageSubresourceRange) {
389 .aspectMask = pRegions[r].dstSubresource.aspectMask,
390 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
391 .levelCount = 1,
392 .baseArrayLayer = pRegions[r].dstSubresource.baseArrayLayer,
393 .layerCount = pRegions[r].dstSubresource.layerCount,
394 });
395 b_dst.format = b_src.format;
396 b_dst.current_layout = VK_IMAGE_LAYOUT_GENERAL;
397 }
398
399
400 /**
401 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
402 * imageExtent is the size in texels of the image to copy in width, height
403 * and depth. 1D images use only x and width. 2D images use x, y, width
404 * and height. 3D images use x, y, z, width, height and depth.
405 *
406 * Also, convert the offsets and extent from units of texels to units of
407 * blocks - which is the highest resolution accessible in this command.
408 */
409 const VkOffset3D dst_offset_el =
410 meta_region_offset_el(dest_image, &pRegions[r].dstOffset);
411 const VkOffset3D src_offset_el =
412 meta_region_offset_el(src_image, &pRegions[r].srcOffset);
413
414 /*
415 * From Vulkan 1.0.68, "Copying Data Between Images":
416 * "When copying between compressed and uncompressed formats
417 * the extent members represent the texel dimensions of the
418 * source image and not the destination."
419 * However, we must use the destination image type to avoid
420 * clamping depth when copying multiple layers of a 2D image to
421 * a 3D image.
422 */
423 const VkExtent3D img_extent_el =
424 meta_region_extent_el(src_image, dest_image->type, &pRegions[r].extent);
425
426 /* Start creating blit rect */
427 struct radv_meta_blit2d_rect rect = {
428 .width = img_extent_el.width,
429 .height = img_extent_el.height,
430 };
431
432 if (src_image->type == VK_IMAGE_TYPE_3D)
433 b_src.layer = src_offset_el.z;
434
435 if (dest_image->type == VK_IMAGE_TYPE_3D)
436 b_dst.layer = dst_offset_el.z;
437
438 /* Loop through each 3D or array slice */
439 unsigned num_slices_3d = img_extent_el.depth;
440 unsigned num_slices_array = pRegions[r].dstSubresource.layerCount;
441 unsigned slice_3d = 0;
442 unsigned slice_array = 0;
443 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
444
445 /* Finish creating blit rect */
446 rect.dst_x = dst_offset_el.x;
447 rect.dst_y = dst_offset_el.y;
448 rect.src_x = src_offset_el.x;
449 rect.src_y = src_offset_el.y;
450
451 /* Perform Blit */
452 if (cs)
453 radv_meta_image_to_image_cs(cmd_buffer, &b_src, &b_dst, 1, &rect);
454 else
455 radv_meta_blit2d(cmd_buffer, &b_src, NULL, &b_dst, 1, &rect);
456
457 b_src.layer++;
458 b_dst.layer++;
459 if (dest_image->type == VK_IMAGE_TYPE_3D)
460 slice_3d++;
461 else
462 slice_array++;
463 }
464 }
465
466 radv_meta_restore(&saved_state, cmd_buffer);
467 }
468
469 void radv_CmdCopyImage(
470 VkCommandBuffer commandBuffer,
471 VkImage srcImage,
472 VkImageLayout srcImageLayout,
473 VkImage destImage,
474 VkImageLayout destImageLayout,
475 uint32_t regionCount,
476 const VkImageCopy* pRegions)
477 {
478 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
479 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
480 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
481
482 meta_copy_image(cmd_buffer,
483 src_image, srcImageLayout,
484 dest_image, destImageLayout,
485 regionCount, pRegions);
486 }
487
488 void radv_blit_to_prime_linear(struct radv_cmd_buffer *cmd_buffer,
489 struct radv_image *image,
490 struct radv_image *linear_image)
491 {
492 struct VkImageCopy image_copy = { 0 };
493
494 image_copy.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
495 image_copy.srcSubresource.layerCount = 1;
496
497 image_copy.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
498 image_copy.dstSubresource.layerCount = 1;
499
500 image_copy.extent.width = image->info.width;
501 image_copy.extent.height = image->info.height;
502 image_copy.extent.depth = 1;
503
504 meta_copy_image(cmd_buffer, image, VK_IMAGE_LAYOUT_GENERAL, linear_image,
505 VK_IMAGE_LAYOUT_GENERAL,
506 1, &image_copy);
507 }