2 * Copyright © 2016 Intel Corporation
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:
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
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
24 #include "radv_meta.h"
25 #include "vk_format.h"
28 meta_image_block_size(const struct radv_image
*image
)
30 const struct vk_format_description
*desc
= vk_format_description(image
->vk_format
);
31 return (VkExtent3D
) { desc
->block
.width
, desc
->block
.height
, 1 };
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.
38 static struct VkExtent3D
39 meta_region_extent_el(const struct radv_image
*image
,
40 const VkImageType imageType
,
41 const struct VkExtent3D
*extent
)
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
),
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.
55 static struct VkOffset3D
56 meta_region_offset_el(const struct radv_image
*image
,
57 const struct VkOffset3D
*offset
)
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
,
68 vk_format_for_size(int 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
;
78 unreachable("Invalid format block size");
82 static struct radv_meta_blit2d_surf
83 blit_surf_for_image_level_layer(struct radv_image
*image
,
85 const VkImageSubresourceLayers
*subres
,
86 VkImageAspectFlags aspect_mask
)
88 VkFormat format
= radv_get_aspect_format(image
, aspect_mask
);
90 if (!radv_image_has_dcc(image
) &&
91 !(radv_image_is_tc_compat_htile(image
)))
92 format
= vk_format_for_size(vk_format_get_blocksize(format
));
94 format
= vk_format_no_srgb(format
);
96 return (struct radv_meta_blit2d_surf
) {
98 .bs
= vk_format_get_blocksize(format
),
99 .level
= subres
->mipLevel
,
100 .layer
= subres
->baseArrayLayer
,
102 .aspect_mask
= aspect_mask
,
103 .current_layout
= layout
,
108 image_is_renderable(struct radv_device
*device
, struct radv_image
*image
)
110 if (image
->vk_format
== VK_FORMAT_R32G32B32_UINT
||
111 image
->vk_format
== VK_FORMAT_R32G32B32_SINT
||
112 image
->vk_format
== VK_FORMAT_R32G32B32_SFLOAT
)
115 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
116 image
->type
== VK_IMAGE_TYPE_3D
&&
117 vk_format_get_blocksizebits(image
->vk_format
) == 128 &&
118 vk_format_is_compressed(image
->vk_format
))
124 meta_copy_buffer_to_image(struct radv_cmd_buffer
*cmd_buffer
,
125 struct radv_buffer
* buffer
,
126 struct radv_image
* image
,
127 VkImageLayout layout
,
128 uint32_t regionCount
,
129 const VkBufferImageCopy
* pRegions
)
131 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
132 struct radv_meta_saved_state saved_state
;
133 bool old_predicating
;
135 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
136 * VK_SAMPLE_COUNT_1_BIT."
138 assert(image
->info
.samples
== 1);
140 radv_meta_save(&saved_state
, cmd_buffer
,
141 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
142 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
143 RADV_META_SAVE_CONSTANTS
|
144 RADV_META_SAVE_DESCRIPTORS
);
146 /* VK_EXT_conditional_rendering says that copy commands should not be
147 * affected by conditional rendering.
149 old_predicating
= cmd_buffer
->state
.predicating
;
150 cmd_buffer
->state
.predicating
= false;
152 for (unsigned r
= 0; r
< regionCount
; r
++) {
155 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
156 * extent is the size in texels of the source image to copy in width,
157 * height and depth. 1D images use only x and width. 2D images use x, y,
158 * width and height. 3D images use x, y, z, width, height and depth.
161 * Also, convert the offsets and extent from units of texels to units of
162 * blocks - which is the highest resolution accessible in this command.
164 const VkOffset3D img_offset_el
=
165 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
166 const VkExtent3D bufferExtent
= {
167 .width
= pRegions
[r
].bufferRowLength
?
168 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
169 .height
= pRegions
[r
].bufferImageHeight
?
170 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
172 const VkExtent3D buf_extent_el
=
173 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
175 /* Start creating blit rect */
176 const VkExtent3D img_extent_el
=
177 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
178 struct radv_meta_blit2d_rect rect
= {
179 .width
= img_extent_el
.width
,
180 .height
= img_extent_el
.height
,
183 /* Create blit surfaces */
184 struct radv_meta_blit2d_surf img_bsurf
=
185 blit_surf_for_image_level_layer(image
,
187 &pRegions
[r
].imageSubresource
,
188 pRegions
[r
].imageSubresource
.aspectMask
);
190 struct radv_meta_blit2d_buffer buf_bsurf
= {
192 .format
= img_bsurf
.format
,
194 .offset
= pRegions
[r
].bufferOffset
,
195 .pitch
= buf_extent_el
.width
,
198 if (image
->type
== VK_IMAGE_TYPE_3D
)
199 img_bsurf
.layer
= img_offset_el
.z
;
200 /* Loop through each 3D or array slice */
201 unsigned num_slices_3d
= img_extent_el
.depth
;
202 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
203 unsigned slice_3d
= 0;
204 unsigned slice_array
= 0;
205 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
207 rect
.dst_x
= img_offset_el
.x
;
208 rect
.dst_y
= img_offset_el
.y
;
213 !image_is_renderable(cmd_buffer
->device
, img_bsurf
.image
)) {
214 radv_meta_buffer_to_image_cs(cmd_buffer
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
216 radv_meta_blit2d(cmd_buffer
, NULL
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
219 /* Once we've done the blit, all of the actual information about
220 * the image is embedded in the command buffer so we can just
221 * increment the offset directly in the image effectively
222 * re-binding it to different backing memory.
224 buf_bsurf
.offset
+= buf_extent_el
.width
*
225 buf_extent_el
.height
* buf_bsurf
.bs
;
227 if (image
->type
== VK_IMAGE_TYPE_3D
)
234 /* Restore conditional rendering. */
235 cmd_buffer
->state
.predicating
= old_predicating
;
237 radv_meta_restore(&saved_state
, cmd_buffer
);
240 void radv_CmdCopyBufferToImage(
241 VkCommandBuffer commandBuffer
,
244 VkImageLayout destImageLayout
,
245 uint32_t regionCount
,
246 const VkBufferImageCopy
* pRegions
)
248 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
249 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
250 RADV_FROM_HANDLE(radv_buffer
, src_buffer
, srcBuffer
);
252 meta_copy_buffer_to_image(cmd_buffer
, src_buffer
, dest_image
, destImageLayout
,
253 regionCount
, pRegions
);
257 meta_copy_image_to_buffer(struct radv_cmd_buffer
*cmd_buffer
,
258 struct radv_buffer
* buffer
,
259 struct radv_image
* image
,
260 VkImageLayout layout
,
261 uint32_t regionCount
,
262 const VkBufferImageCopy
* pRegions
)
264 struct radv_meta_saved_state saved_state
;
265 bool old_predicating
;
267 radv_meta_save(&saved_state
, cmd_buffer
,
268 RADV_META_SAVE_COMPUTE_PIPELINE
|
269 RADV_META_SAVE_CONSTANTS
|
270 RADV_META_SAVE_DESCRIPTORS
);
272 /* VK_EXT_conditional_rendering says that copy commands should not be
273 * affected by conditional rendering.
275 old_predicating
= cmd_buffer
->state
.predicating
;
276 cmd_buffer
->state
.predicating
= false;
278 for (unsigned r
= 0; r
< regionCount
; r
++) {
281 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
282 * extent is the size in texels of the source image to copy in width,
283 * height and depth. 1D images use only x and width. 2D images use x, y,
284 * width and height. 3D images use x, y, z, width, height and depth.
287 * Also, convert the offsets and extent from units of texels to units of
288 * blocks - which is the highest resolution accessible in this command.
290 const VkOffset3D img_offset_el
=
291 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
292 const VkExtent3D bufferExtent
= {
293 .width
= pRegions
[r
].bufferRowLength
?
294 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
295 .height
= pRegions
[r
].bufferImageHeight
?
296 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
298 const VkExtent3D buf_extent_el
=
299 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
301 /* Start creating blit rect */
302 const VkExtent3D img_extent_el
=
303 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
304 struct radv_meta_blit2d_rect rect
= {
305 .width
= img_extent_el
.width
,
306 .height
= img_extent_el
.height
,
309 /* Create blit surfaces */
310 struct radv_meta_blit2d_surf img_info
=
311 blit_surf_for_image_level_layer(image
,
313 &pRegions
[r
].imageSubresource
,
314 pRegions
[r
].imageSubresource
.aspectMask
);
316 struct radv_meta_blit2d_buffer buf_info
= {
318 .format
= img_info
.format
,
320 .offset
= pRegions
[r
].bufferOffset
,
321 .pitch
= buf_extent_el
.width
,
324 if (image
->type
== VK_IMAGE_TYPE_3D
)
325 img_info
.layer
= img_offset_el
.z
;
326 /* Loop through each 3D or array slice */
327 unsigned num_slices_3d
= img_extent_el
.depth
;
328 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
329 unsigned slice_3d
= 0;
330 unsigned slice_array
= 0;
331 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
333 rect
.src_x
= img_offset_el
.x
;
334 rect
.src_y
= img_offset_el
.y
;
338 radv_meta_image_to_buffer(cmd_buffer
, &img_info
, &buf_info
, 1, &rect
);
340 buf_info
.offset
+= buf_extent_el
.width
*
341 buf_extent_el
.height
* buf_info
.bs
;
343 if (image
->type
== VK_IMAGE_TYPE_3D
)
350 /* Restore conditional rendering. */
351 cmd_buffer
->state
.predicating
= old_predicating
;
353 radv_meta_restore(&saved_state
, cmd_buffer
);
356 void radv_CmdCopyImageToBuffer(
357 VkCommandBuffer commandBuffer
,
359 VkImageLayout srcImageLayout
,
361 uint32_t regionCount
,
362 const VkBufferImageCopy
* pRegions
)
364 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
365 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
366 RADV_FROM_HANDLE(radv_buffer
, dst_buffer
, destBuffer
);
368 meta_copy_image_to_buffer(cmd_buffer
, dst_buffer
, src_image
,
370 regionCount
, pRegions
);
374 meta_copy_image(struct radv_cmd_buffer
*cmd_buffer
,
375 struct radv_image
*src_image
,
376 VkImageLayout src_image_layout
,
377 struct radv_image
*dest_image
,
378 VkImageLayout dest_image_layout
,
379 uint32_t regionCount
,
380 const VkImageCopy
*pRegions
)
382 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
383 struct radv_meta_saved_state saved_state
;
384 bool old_predicating
;
386 /* From the Vulkan 1.0 spec:
388 * vkCmdCopyImage can be used to copy image data between multisample
389 * images, but both images must have the same number of samples.
391 assert(src_image
->info
.samples
== dest_image
->info
.samples
);
393 radv_meta_save(&saved_state
, cmd_buffer
,
394 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
395 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
396 RADV_META_SAVE_CONSTANTS
|
397 RADV_META_SAVE_DESCRIPTORS
);
399 /* VK_EXT_conditional_rendering says that copy commands should not be
400 * affected by conditional rendering.
402 old_predicating
= cmd_buffer
->state
.predicating
;
403 cmd_buffer
->state
.predicating
= false;
405 for (unsigned r
= 0; r
< regionCount
; r
++) {
406 VkImageAspectFlags src_aspects
[3] = {VK_IMAGE_ASPECT_PLANE_0_BIT
, VK_IMAGE_ASPECT_PLANE_1_BIT
, VK_IMAGE_ASPECT_PLANE_2_BIT
};
407 VkImageAspectFlags dst_aspects
[3] = {VK_IMAGE_ASPECT_PLANE_0_BIT
, VK_IMAGE_ASPECT_PLANE_1_BIT
, VK_IMAGE_ASPECT_PLANE_2_BIT
};
408 unsigned aspect_count
= pRegions
[r
].srcSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
? src_image
->plane_count
: 1;
409 if (pRegions
[r
].srcSubresource
.aspectMask
!= VK_IMAGE_ASPECT_COLOR_BIT
)
410 src_aspects
[0] = pRegions
[r
].srcSubresource
.aspectMask
;
411 if (pRegions
[r
].dstSubresource
.aspectMask
!= VK_IMAGE_ASPECT_COLOR_BIT
)
412 dst_aspects
[0] = pRegions
[r
].dstSubresource
.aspectMask
;
414 for (unsigned a
= 0; a
< aspect_count
; ++a
) {
415 /* Create blit surfaces */
416 struct radv_meta_blit2d_surf b_src
=
417 blit_surf_for_image_level_layer(src_image
,
419 &pRegions
[r
].srcSubresource
,
422 struct radv_meta_blit2d_surf b_dst
=
423 blit_surf_for_image_level_layer(dest_image
,
425 &pRegions
[r
].dstSubresource
,
428 uint32_t dst_queue_mask
= radv_image_queue_family_mask(dest_image
,
429 cmd_buffer
->queue_family_index
,
430 cmd_buffer
->queue_family_index
);
431 bool dst_compressed
= radv_layout_dcc_compressed(dest_image
, dest_image_layout
, dst_queue_mask
);
432 uint32_t src_queue_mask
= radv_image_queue_family_mask(src_image
,
433 cmd_buffer
->queue_family_index
,
434 cmd_buffer
->queue_family_index
);
435 bool src_compressed
= radv_layout_dcc_compressed(src_image
, src_image_layout
, src_queue_mask
);
437 if (!src_compressed
|| radv_dcc_formats_compatible(b_src
.format
, b_dst
.format
)) {
438 b_src
.format
= b_dst
.format
;
439 } else if (!dst_compressed
) {
440 b_dst
.format
= b_src
.format
;
442 radv_decompress_dcc(cmd_buffer
, dest_image
, &(VkImageSubresourceRange
) {
443 .aspectMask
= dst_aspects
[a
],
444 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
446 .baseArrayLayer
= pRegions
[r
].dstSubresource
.baseArrayLayer
,
447 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
449 b_dst
.format
= b_src
.format
;
450 b_dst
.current_layout
= VK_IMAGE_LAYOUT_GENERAL
;
455 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
456 * imageExtent is the size in texels of the image to copy in width, height
457 * and depth. 1D images use only x and width. 2D images use x, y, width
458 * and height. 3D images use x, y, z, width, height and depth.
460 * Also, convert the offsets and extent from units of texels to units of
461 * blocks - which is the highest resolution accessible in this command.
463 const VkOffset3D dst_offset_el
=
464 meta_region_offset_el(dest_image
, &pRegions
[r
].dstOffset
);
465 const VkOffset3D src_offset_el
=
466 meta_region_offset_el(src_image
, &pRegions
[r
].srcOffset
);
469 * From Vulkan 1.0.68, "Copying Data Between Images":
470 * "When copying between compressed and uncompressed formats
471 * the extent members represent the texel dimensions of the
472 * source image and not the destination."
473 * However, we must use the destination image type to avoid
474 * clamping depth when copying multiple layers of a 2D image to
477 const VkExtent3D img_extent_el
=
478 meta_region_extent_el(src_image
, dest_image
->type
, &pRegions
[r
].extent
);
480 /* Start creating blit rect */
481 struct radv_meta_blit2d_rect rect
= {
482 .width
= img_extent_el
.width
,
483 .height
= img_extent_el
.height
,
486 if (src_image
->type
== VK_IMAGE_TYPE_3D
)
487 b_src
.layer
= src_offset_el
.z
;
489 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
490 b_dst
.layer
= dst_offset_el
.z
;
492 /* Loop through each 3D or array slice */
493 unsigned num_slices_3d
= img_extent_el
.depth
;
494 unsigned num_slices_array
= pRegions
[r
].dstSubresource
.layerCount
;
495 unsigned slice_3d
= 0;
496 unsigned slice_array
= 0;
497 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
499 /* Finish creating blit rect */
500 rect
.dst_x
= dst_offset_el
.x
;
501 rect
.dst_y
= dst_offset_el
.y
;
502 rect
.src_x
= src_offset_el
.x
;
503 rect
.src_y
= src_offset_el
.y
;
507 !image_is_renderable(cmd_buffer
->device
, b_dst
.image
)) {
508 radv_meta_image_to_image_cs(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
510 radv_meta_blit2d(cmd_buffer
, &b_src
, NULL
, &b_dst
, 1, &rect
);
515 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
523 /* Restore conditional rendering. */
524 cmd_buffer
->state
.predicating
= old_predicating
;
526 radv_meta_restore(&saved_state
, cmd_buffer
);
529 void radv_CmdCopyImage(
530 VkCommandBuffer commandBuffer
,
532 VkImageLayout srcImageLayout
,
534 VkImageLayout destImageLayout
,
535 uint32_t regionCount
,
536 const VkImageCopy
* pRegions
)
538 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
539 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
540 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
542 meta_copy_image(cmd_buffer
,
543 src_image
, srcImageLayout
,
544 dest_image
, destImageLayout
,
545 regionCount
, pRegions
);