f4de5528edf904a509a89953b9b852853856ab36
[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 format = vk_format_no_srgb(format);
98
99 return (struct radv_meta_blit2d_surf) {
100 .format = format,
101 .bs = vk_format_get_blocksize(format),
102 .level = subres->mipLevel,
103 .layer = subres->baseArrayLayer,
104 .image = image,
105 .aspect_mask = subres->aspectMask,
106 .current_layout = layout,
107 };
108 }
109
110 static void
111 meta_copy_buffer_to_image(struct radv_cmd_buffer *cmd_buffer,
112 struct radv_buffer* buffer,
113 struct radv_image* image,
114 VkImageLayout layout,
115 uint32_t regionCount,
116 const VkBufferImageCopy* pRegions)
117 {
118 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
119 struct radv_meta_saved_state saved_state;
120 bool old_predicating;
121
122 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
123 * VK_SAMPLE_COUNT_1_BIT."
124 */
125 assert(image->info.samples == 1);
126
127 radv_meta_save(&saved_state, cmd_buffer,
128 (cs ? RADV_META_SAVE_COMPUTE_PIPELINE :
129 RADV_META_SAVE_GRAPHICS_PIPELINE) |
130 RADV_META_SAVE_CONSTANTS |
131 RADV_META_SAVE_DESCRIPTORS);
132
133 /* VK_EXT_conditional_rendering says that copy commands should not be
134 * affected by conditional rendering.
135 */
136 old_predicating = cmd_buffer->state.predicating;
137 cmd_buffer->state.predicating = false;
138
139 for (unsigned r = 0; r < regionCount; r++) {
140
141 /**
142 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
143 * extent is the size in texels of the source image to copy in width,
144 * height and depth. 1D images use only x and width. 2D images use x, y,
145 * width and height. 3D images use x, y, z, width, height and depth.
146 *
147 *
148 * Also, convert the offsets and extent from units of texels to units of
149 * blocks - which is the highest resolution accessible in this command.
150 */
151 const VkOffset3D img_offset_el =
152 meta_region_offset_el(image, &pRegions[r].imageOffset);
153 const VkExtent3D bufferExtent = {
154 .width = pRegions[r].bufferRowLength ?
155 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
156 .height = pRegions[r].bufferImageHeight ?
157 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
158 };
159 const VkExtent3D buf_extent_el =
160 meta_region_extent_el(image, image->type, &bufferExtent);
161
162 /* Start creating blit rect */
163 const VkExtent3D img_extent_el =
164 meta_region_extent_el(image, image->type, &pRegions[r].imageExtent);
165 struct radv_meta_blit2d_rect rect = {
166 .width = img_extent_el.width,
167 .height = img_extent_el.height,
168 };
169
170 /* Create blit surfaces */
171 struct radv_meta_blit2d_surf img_bsurf =
172 blit_surf_for_image_level_layer(image,
173 layout,
174 &pRegions[r].imageSubresource);
175
176 struct radv_meta_blit2d_buffer buf_bsurf = {
177 .bs = img_bsurf.bs,
178 .format = img_bsurf.format,
179 .buffer = buffer,
180 .offset = pRegions[r].bufferOffset,
181 .pitch = buf_extent_el.width,
182 };
183
184 if (image->type == VK_IMAGE_TYPE_3D)
185 img_bsurf.layer = img_offset_el.z;
186 /* Loop through each 3D or array slice */
187 unsigned num_slices_3d = img_extent_el.depth;
188 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
189 unsigned slice_3d = 0;
190 unsigned slice_array = 0;
191 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
192
193 rect.dst_x = img_offset_el.x;
194 rect.dst_y = img_offset_el.y;
195
196
197 /* Perform Blit */
198 if (cs)
199 radv_meta_buffer_to_image_cs(cmd_buffer, &buf_bsurf, &img_bsurf, 1, &rect);
200 else
201 radv_meta_blit2d(cmd_buffer, NULL, &buf_bsurf, &img_bsurf, 1, &rect);
202
203 /* Once we've done the blit, all of the actual information about
204 * the image is embedded in the command buffer so we can just
205 * increment the offset directly in the image effectively
206 * re-binding it to different backing memory.
207 */
208 buf_bsurf.offset += buf_extent_el.width *
209 buf_extent_el.height * buf_bsurf.bs;
210 img_bsurf.layer++;
211 if (image->type == VK_IMAGE_TYPE_3D)
212 slice_3d++;
213 else
214 slice_array++;
215 }
216 }
217
218 /* Restore conditional rendering. */
219 cmd_buffer->state.predicating = old_predicating;
220
221 radv_meta_restore(&saved_state, cmd_buffer);
222 }
223
224 void radv_CmdCopyBufferToImage(
225 VkCommandBuffer commandBuffer,
226 VkBuffer srcBuffer,
227 VkImage destImage,
228 VkImageLayout destImageLayout,
229 uint32_t regionCount,
230 const VkBufferImageCopy* pRegions)
231 {
232 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
233 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
234 RADV_FROM_HANDLE(radv_buffer, src_buffer, srcBuffer);
235
236 meta_copy_buffer_to_image(cmd_buffer, src_buffer, dest_image, destImageLayout,
237 regionCount, pRegions);
238 }
239
240 static void
241 meta_copy_image_to_buffer(struct radv_cmd_buffer *cmd_buffer,
242 struct radv_buffer* buffer,
243 struct radv_image* image,
244 VkImageLayout layout,
245 uint32_t regionCount,
246 const VkBufferImageCopy* pRegions)
247 {
248 struct radv_meta_saved_state saved_state;
249 bool old_predicating;
250
251 radv_meta_save(&saved_state, cmd_buffer,
252 RADV_META_SAVE_COMPUTE_PIPELINE |
253 RADV_META_SAVE_CONSTANTS |
254 RADV_META_SAVE_DESCRIPTORS);
255
256 /* VK_EXT_conditional_rendering says that copy commands should not be
257 * affected by conditional rendering.
258 */
259 old_predicating = cmd_buffer->state.predicating;
260 cmd_buffer->state.predicating = false;
261
262 for (unsigned r = 0; r < regionCount; r++) {
263
264 /**
265 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
266 * extent is the size in texels of the source image to copy in width,
267 * height and depth. 1D images use only x and width. 2D images use x, y,
268 * width and height. 3D images use x, y, z, width, height and depth.
269 *
270 *
271 * Also, convert the offsets and extent from units of texels to units of
272 * blocks - which is the highest resolution accessible in this command.
273 */
274 const VkOffset3D img_offset_el =
275 meta_region_offset_el(image, &pRegions[r].imageOffset);
276 const VkExtent3D bufferExtent = {
277 .width = pRegions[r].bufferRowLength ?
278 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
279 .height = pRegions[r].bufferImageHeight ?
280 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
281 };
282 const VkExtent3D buf_extent_el =
283 meta_region_extent_el(image, image->type, &bufferExtent);
284
285 /* Start creating blit rect */
286 const VkExtent3D img_extent_el =
287 meta_region_extent_el(image, image->type, &pRegions[r].imageExtent);
288 struct radv_meta_blit2d_rect rect = {
289 .width = img_extent_el.width,
290 .height = img_extent_el.height,
291 };
292
293 /* Create blit surfaces */
294 struct radv_meta_blit2d_surf img_info =
295 blit_surf_for_image_level_layer(image,
296 layout,
297 &pRegions[r].imageSubresource);
298
299 struct radv_meta_blit2d_buffer buf_info = {
300 .bs = img_info.bs,
301 .format = img_info.format,
302 .buffer = buffer,
303 .offset = pRegions[r].bufferOffset,
304 .pitch = buf_extent_el.width,
305 };
306
307 if (image->type == VK_IMAGE_TYPE_3D)
308 img_info.layer = img_offset_el.z;
309 /* Loop through each 3D or array slice */
310 unsigned num_slices_3d = img_extent_el.depth;
311 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
312 unsigned slice_3d = 0;
313 unsigned slice_array = 0;
314 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
315
316 rect.src_x = img_offset_el.x;
317 rect.src_y = img_offset_el.y;
318
319
320 /* Perform Blit */
321 radv_meta_image_to_buffer(cmd_buffer, &img_info, &buf_info, 1, &rect);
322
323 buf_info.offset += buf_extent_el.width *
324 buf_extent_el.height * buf_info.bs;
325 img_info.layer++;
326 if (image->type == VK_IMAGE_TYPE_3D)
327 slice_3d++;
328 else
329 slice_array++;
330 }
331 }
332
333 /* Restore conditional rendering. */
334 cmd_buffer->state.predicating = old_predicating;
335
336 radv_meta_restore(&saved_state, cmd_buffer);
337 }
338
339 void radv_CmdCopyImageToBuffer(
340 VkCommandBuffer commandBuffer,
341 VkImage srcImage,
342 VkImageLayout srcImageLayout,
343 VkBuffer destBuffer,
344 uint32_t regionCount,
345 const VkBufferImageCopy* pRegions)
346 {
347 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
348 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
349 RADV_FROM_HANDLE(radv_buffer, dst_buffer, destBuffer);
350
351 meta_copy_image_to_buffer(cmd_buffer, dst_buffer, src_image,
352 srcImageLayout,
353 regionCount, pRegions);
354 }
355
356 static void
357 meta_copy_image(struct radv_cmd_buffer *cmd_buffer,
358 struct radv_image *src_image,
359 VkImageLayout src_image_layout,
360 struct radv_image *dest_image,
361 VkImageLayout dest_image_layout,
362 uint32_t regionCount,
363 const VkImageCopy *pRegions)
364 {
365 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
366 struct radv_meta_saved_state saved_state;
367 bool old_predicating;
368
369 /* From the Vulkan 1.0 spec:
370 *
371 * vkCmdCopyImage can be used to copy image data between multisample
372 * images, but both images must have the same number of samples.
373 */
374 assert(src_image->info.samples == dest_image->info.samples);
375
376 radv_meta_save(&saved_state, cmd_buffer,
377 (cs ? RADV_META_SAVE_COMPUTE_PIPELINE :
378 RADV_META_SAVE_GRAPHICS_PIPELINE) |
379 RADV_META_SAVE_CONSTANTS |
380 RADV_META_SAVE_DESCRIPTORS);
381
382 /* VK_EXT_conditional_rendering says that copy commands should not be
383 * affected by conditional rendering.
384 */
385 old_predicating = cmd_buffer->state.predicating;
386 cmd_buffer->state.predicating = false;
387
388 for (unsigned r = 0; r < regionCount; r++) {
389 assert(pRegions[r].srcSubresource.aspectMask ==
390 pRegions[r].dstSubresource.aspectMask);
391
392 /* Create blit surfaces */
393 struct radv_meta_blit2d_surf b_src =
394 blit_surf_for_image_level_layer(src_image,
395 src_image_layout,
396 &pRegions[r].srcSubresource);
397
398 struct radv_meta_blit2d_surf b_dst =
399 blit_surf_for_image_level_layer(dest_image,
400 dest_image_layout,
401 &pRegions[r].dstSubresource);
402
403 uint32_t dst_queue_mask = radv_image_queue_family_mask(dest_image,
404 cmd_buffer->queue_family_index,
405 cmd_buffer->queue_family_index);
406 bool dst_compressed = radv_layout_dcc_compressed(dest_image, dest_image_layout, dst_queue_mask);
407 uint32_t src_queue_mask = radv_image_queue_family_mask(src_image,
408 cmd_buffer->queue_family_index,
409 cmd_buffer->queue_family_index);
410 bool src_compressed = radv_layout_dcc_compressed(src_image, src_image_layout, src_queue_mask);
411
412 if (!src_compressed || radv_dcc_formats_compatible(b_src.format, b_dst.format)) {
413 b_src.format = b_dst.format;
414 } else if (!dst_compressed) {
415 b_dst.format = b_src.format;
416 } else {
417 radv_decompress_dcc(cmd_buffer, dest_image, &(VkImageSubresourceRange) {
418 .aspectMask = pRegions[r].dstSubresource.aspectMask,
419 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
420 .levelCount = 1,
421 .baseArrayLayer = pRegions[r].dstSubresource.baseArrayLayer,
422 .layerCount = pRegions[r].dstSubresource.layerCount,
423 });
424 b_dst.format = b_src.format;
425 b_dst.current_layout = VK_IMAGE_LAYOUT_GENERAL;
426 }
427
428
429 /**
430 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
431 * imageExtent is the size in texels of the image to copy in width, height
432 * and depth. 1D images use only x and width. 2D images use x, y, width
433 * and height. 3D images use x, y, z, width, height and depth.
434 *
435 * Also, convert the offsets and extent from units of texels to units of
436 * blocks - which is the highest resolution accessible in this command.
437 */
438 const VkOffset3D dst_offset_el =
439 meta_region_offset_el(dest_image, &pRegions[r].dstOffset);
440 const VkOffset3D src_offset_el =
441 meta_region_offset_el(src_image, &pRegions[r].srcOffset);
442
443 /*
444 * From Vulkan 1.0.68, "Copying Data Between Images":
445 * "When copying between compressed and uncompressed formats
446 * the extent members represent the texel dimensions of the
447 * source image and not the destination."
448 * However, we must use the destination image type to avoid
449 * clamping depth when copying multiple layers of a 2D image to
450 * a 3D image.
451 */
452 const VkExtent3D img_extent_el =
453 meta_region_extent_el(src_image, dest_image->type, &pRegions[r].extent);
454
455 /* Start creating blit rect */
456 struct radv_meta_blit2d_rect rect = {
457 .width = img_extent_el.width,
458 .height = img_extent_el.height,
459 };
460
461 if (src_image->type == VK_IMAGE_TYPE_3D)
462 b_src.layer = src_offset_el.z;
463
464 if (dest_image->type == VK_IMAGE_TYPE_3D)
465 b_dst.layer = dst_offset_el.z;
466
467 /* Loop through each 3D or array slice */
468 unsigned num_slices_3d = img_extent_el.depth;
469 unsigned num_slices_array = pRegions[r].dstSubresource.layerCount;
470 unsigned slice_3d = 0;
471 unsigned slice_array = 0;
472 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
473
474 /* Finish creating blit rect */
475 rect.dst_x = dst_offset_el.x;
476 rect.dst_y = dst_offset_el.y;
477 rect.src_x = src_offset_el.x;
478 rect.src_y = src_offset_el.y;
479
480 /* Perform Blit */
481 if (cs)
482 radv_meta_image_to_image_cs(cmd_buffer, &b_src, &b_dst, 1, &rect);
483 else
484 radv_meta_blit2d(cmd_buffer, &b_src, NULL, &b_dst, 1, &rect);
485
486 b_src.layer++;
487 b_dst.layer++;
488 if (dest_image->type == VK_IMAGE_TYPE_3D)
489 slice_3d++;
490 else
491 slice_array++;
492 }
493 }
494
495 /* Restore conditional rendering. */
496 cmd_buffer->state.predicating = old_predicating;
497
498 radv_meta_restore(&saved_state, cmd_buffer);
499 }
500
501 void radv_CmdCopyImage(
502 VkCommandBuffer commandBuffer,
503 VkImage srcImage,
504 VkImageLayout srcImageLayout,
505 VkImage destImage,
506 VkImageLayout destImageLayout,
507 uint32_t regionCount,
508 const VkImageCopy* pRegions)
509 {
510 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
511 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
512 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
513
514 meta_copy_image(cmd_buffer,
515 src_image, srcImageLayout,
516 dest_image, destImageLayout,
517 regionCount, pRegions);
518 }