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
)
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
);
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
));
97 format
= vk_format_no_srgb(format
);
99 return (struct radv_meta_blit2d_surf
) {
101 .bs
= vk_format_get_blocksize(format
),
102 .level
= subres
->mipLevel
,
103 .layer
= subres
->baseArrayLayer
,
105 .aspect_mask
= subres
->aspectMask
,
106 .current_layout
= layout
,
111 image_is_renderable(struct radv_device
*device
, struct radv_image
*image
)
113 if (image
->vk_format
== VK_FORMAT_R32G32B32_UINT
||
114 image
->vk_format
== VK_FORMAT_R32G32B32_SINT
||
115 image
->vk_format
== VK_FORMAT_R32G32B32_SFLOAT
)
118 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
119 image
->type
== VK_IMAGE_TYPE_3D
&&
120 vk_format_get_blocksizebits(image
->vk_format
) == 128 &&
121 vk_format_is_compressed(image
->vk_format
))
127 meta_copy_buffer_to_image(struct radv_cmd_buffer
*cmd_buffer
,
128 struct radv_buffer
* buffer
,
129 struct radv_image
* image
,
130 VkImageLayout layout
,
131 uint32_t regionCount
,
132 const VkBufferImageCopy
* pRegions
)
134 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
135 struct radv_meta_saved_state saved_state
;
136 bool old_predicating
;
138 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
139 * VK_SAMPLE_COUNT_1_BIT."
141 assert(image
->info
.samples
== 1);
143 radv_meta_save(&saved_state
, cmd_buffer
,
144 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
145 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
146 RADV_META_SAVE_CONSTANTS
|
147 RADV_META_SAVE_DESCRIPTORS
);
149 /* VK_EXT_conditional_rendering says that copy commands should not be
150 * affected by conditional rendering.
152 old_predicating
= cmd_buffer
->state
.predicating
;
153 cmd_buffer
->state
.predicating
= false;
155 for (unsigned r
= 0; r
< regionCount
; r
++) {
158 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
159 * extent is the size in texels of the source image to copy in width,
160 * height and depth. 1D images use only x and width. 2D images use x, y,
161 * width and height. 3D images use x, y, z, width, height and depth.
164 * Also, convert the offsets and extent from units of texels to units of
165 * blocks - which is the highest resolution accessible in this command.
167 const VkOffset3D img_offset_el
=
168 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
169 const VkExtent3D bufferExtent
= {
170 .width
= pRegions
[r
].bufferRowLength
?
171 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
172 .height
= pRegions
[r
].bufferImageHeight
?
173 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
175 const VkExtent3D buf_extent_el
=
176 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
178 /* Start creating blit rect */
179 const VkExtent3D img_extent_el
=
180 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
181 struct radv_meta_blit2d_rect rect
= {
182 .width
= img_extent_el
.width
,
183 .height
= img_extent_el
.height
,
186 /* Create blit surfaces */
187 struct radv_meta_blit2d_surf img_bsurf
=
188 blit_surf_for_image_level_layer(image
,
190 &pRegions
[r
].imageSubresource
);
192 struct radv_meta_blit2d_buffer buf_bsurf
= {
194 .format
= img_bsurf
.format
,
196 .offset
= pRegions
[r
].bufferOffset
,
197 .pitch
= buf_extent_el
.width
,
200 if (image
->type
== VK_IMAGE_TYPE_3D
)
201 img_bsurf
.layer
= img_offset_el
.z
;
202 /* Loop through each 3D or array slice */
203 unsigned num_slices_3d
= img_extent_el
.depth
;
204 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
205 unsigned slice_3d
= 0;
206 unsigned slice_array
= 0;
207 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
209 rect
.dst_x
= img_offset_el
.x
;
210 rect
.dst_y
= img_offset_el
.y
;
215 !image_is_renderable(cmd_buffer
->device
, img_bsurf
.image
)) {
216 radv_meta_buffer_to_image_cs(cmd_buffer
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
218 radv_meta_blit2d(cmd_buffer
, NULL
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
221 /* Once we've done the blit, all of the actual information about
222 * the image is embedded in the command buffer so we can just
223 * increment the offset directly in the image effectively
224 * re-binding it to different backing memory.
226 buf_bsurf
.offset
+= buf_extent_el
.width
*
227 buf_extent_el
.height
* buf_bsurf
.bs
;
229 if (image
->type
== VK_IMAGE_TYPE_3D
)
236 /* Restore conditional rendering. */
237 cmd_buffer
->state
.predicating
= old_predicating
;
239 radv_meta_restore(&saved_state
, cmd_buffer
);
242 void radv_CmdCopyBufferToImage(
243 VkCommandBuffer commandBuffer
,
246 VkImageLayout destImageLayout
,
247 uint32_t regionCount
,
248 const VkBufferImageCopy
* pRegions
)
250 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
251 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
252 RADV_FROM_HANDLE(radv_buffer
, src_buffer
, srcBuffer
);
254 meta_copy_buffer_to_image(cmd_buffer
, src_buffer
, dest_image
, destImageLayout
,
255 regionCount
, pRegions
);
259 meta_copy_image_to_buffer(struct radv_cmd_buffer
*cmd_buffer
,
260 struct radv_buffer
* buffer
,
261 struct radv_image
* image
,
262 VkImageLayout layout
,
263 uint32_t regionCount
,
264 const VkBufferImageCopy
* pRegions
)
266 struct radv_meta_saved_state saved_state
;
267 bool old_predicating
;
269 radv_meta_save(&saved_state
, cmd_buffer
,
270 RADV_META_SAVE_COMPUTE_PIPELINE
|
271 RADV_META_SAVE_CONSTANTS
|
272 RADV_META_SAVE_DESCRIPTORS
);
274 /* VK_EXT_conditional_rendering says that copy commands should not be
275 * affected by conditional rendering.
277 old_predicating
= cmd_buffer
->state
.predicating
;
278 cmd_buffer
->state
.predicating
= false;
280 for (unsigned r
= 0; r
< regionCount
; r
++) {
283 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
284 * extent is the size in texels of the source image to copy in width,
285 * height and depth. 1D images use only x and width. 2D images use x, y,
286 * width and height. 3D images use x, y, z, width, height and depth.
289 * Also, convert the offsets and extent from units of texels to units of
290 * blocks - which is the highest resolution accessible in this command.
292 const VkOffset3D img_offset_el
=
293 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
294 const VkExtent3D bufferExtent
= {
295 .width
= pRegions
[r
].bufferRowLength
?
296 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
297 .height
= pRegions
[r
].bufferImageHeight
?
298 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
300 const VkExtent3D buf_extent_el
=
301 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
303 /* Start creating blit rect */
304 const VkExtent3D img_extent_el
=
305 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
306 struct radv_meta_blit2d_rect rect
= {
307 .width
= img_extent_el
.width
,
308 .height
= img_extent_el
.height
,
311 /* Create blit surfaces */
312 struct radv_meta_blit2d_surf img_info
=
313 blit_surf_for_image_level_layer(image
,
315 &pRegions
[r
].imageSubresource
);
317 struct radv_meta_blit2d_buffer buf_info
= {
319 .format
= img_info
.format
,
321 .offset
= pRegions
[r
].bufferOffset
,
322 .pitch
= buf_extent_el
.width
,
325 if (image
->type
== VK_IMAGE_TYPE_3D
)
326 img_info
.layer
= img_offset_el
.z
;
327 /* Loop through each 3D or array slice */
328 unsigned num_slices_3d
= img_extent_el
.depth
;
329 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
330 unsigned slice_3d
= 0;
331 unsigned slice_array
= 0;
332 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
334 rect
.src_x
= img_offset_el
.x
;
335 rect
.src_y
= img_offset_el
.y
;
339 radv_meta_image_to_buffer(cmd_buffer
, &img_info
, &buf_info
, 1, &rect
);
341 buf_info
.offset
+= buf_extent_el
.width
*
342 buf_extent_el
.height
* buf_info
.bs
;
344 if (image
->type
== VK_IMAGE_TYPE_3D
)
351 /* Restore conditional rendering. */
352 cmd_buffer
->state
.predicating
= old_predicating
;
354 radv_meta_restore(&saved_state
, cmd_buffer
);
357 void radv_CmdCopyImageToBuffer(
358 VkCommandBuffer commandBuffer
,
360 VkImageLayout srcImageLayout
,
362 uint32_t regionCount
,
363 const VkBufferImageCopy
* pRegions
)
365 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
366 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
367 RADV_FROM_HANDLE(radv_buffer
, dst_buffer
, destBuffer
);
369 meta_copy_image_to_buffer(cmd_buffer
, dst_buffer
, src_image
,
371 regionCount
, pRegions
);
375 meta_copy_image(struct radv_cmd_buffer
*cmd_buffer
,
376 struct radv_image
*src_image
,
377 VkImageLayout src_image_layout
,
378 struct radv_image
*dest_image
,
379 VkImageLayout dest_image_layout
,
380 uint32_t regionCount
,
381 const VkImageCopy
*pRegions
)
383 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
384 struct radv_meta_saved_state saved_state
;
385 bool old_predicating
;
387 /* From the Vulkan 1.0 spec:
389 * vkCmdCopyImage can be used to copy image data between multisample
390 * images, but both images must have the same number of samples.
392 assert(src_image
->info
.samples
== dest_image
->info
.samples
);
394 radv_meta_save(&saved_state
, cmd_buffer
,
395 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
396 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
397 RADV_META_SAVE_CONSTANTS
|
398 RADV_META_SAVE_DESCRIPTORS
);
400 /* VK_EXT_conditional_rendering says that copy commands should not be
401 * affected by conditional rendering.
403 old_predicating
= cmd_buffer
->state
.predicating
;
404 cmd_buffer
->state
.predicating
= false;
406 for (unsigned r
= 0; r
< regionCount
; r
++) {
407 assert(pRegions
[r
].srcSubresource
.aspectMask
==
408 pRegions
[r
].dstSubresource
.aspectMask
);
410 /* Create blit surfaces */
411 struct radv_meta_blit2d_surf b_src
=
412 blit_surf_for_image_level_layer(src_image
,
414 &pRegions
[r
].srcSubresource
);
416 struct radv_meta_blit2d_surf b_dst
=
417 blit_surf_for_image_level_layer(dest_image
,
419 &pRegions
[r
].dstSubresource
);
421 uint32_t dst_queue_mask
= radv_image_queue_family_mask(dest_image
,
422 cmd_buffer
->queue_family_index
,
423 cmd_buffer
->queue_family_index
);
424 bool dst_compressed
= radv_layout_dcc_compressed(dest_image
, dest_image_layout
, dst_queue_mask
);
425 uint32_t src_queue_mask
= radv_image_queue_family_mask(src_image
,
426 cmd_buffer
->queue_family_index
,
427 cmd_buffer
->queue_family_index
);
428 bool src_compressed
= radv_layout_dcc_compressed(src_image
, src_image_layout
, src_queue_mask
);
430 if (!src_compressed
|| radv_dcc_formats_compatible(b_src
.format
, b_dst
.format
)) {
431 b_src
.format
= b_dst
.format
;
432 } else if (!dst_compressed
) {
433 b_dst
.format
= b_src
.format
;
435 radv_decompress_dcc(cmd_buffer
, dest_image
, &(VkImageSubresourceRange
) {
436 .aspectMask
= pRegions
[r
].dstSubresource
.aspectMask
,
437 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
439 .baseArrayLayer
= pRegions
[r
].dstSubresource
.baseArrayLayer
,
440 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
442 b_dst
.format
= b_src
.format
;
443 b_dst
.current_layout
= VK_IMAGE_LAYOUT_GENERAL
;
448 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
449 * imageExtent is the size in texels of the image to copy in width, height
450 * and depth. 1D images use only x and width. 2D images use x, y, width
451 * and height. 3D images use x, y, z, width, height and depth.
453 * Also, convert the offsets and extent from units of texels to units of
454 * blocks - which is the highest resolution accessible in this command.
456 const VkOffset3D dst_offset_el
=
457 meta_region_offset_el(dest_image
, &pRegions
[r
].dstOffset
);
458 const VkOffset3D src_offset_el
=
459 meta_region_offset_el(src_image
, &pRegions
[r
].srcOffset
);
462 * From Vulkan 1.0.68, "Copying Data Between Images":
463 * "When copying between compressed and uncompressed formats
464 * the extent members represent the texel dimensions of the
465 * source image and not the destination."
466 * However, we must use the destination image type to avoid
467 * clamping depth when copying multiple layers of a 2D image to
470 const VkExtent3D img_extent_el
=
471 meta_region_extent_el(src_image
, dest_image
->type
, &pRegions
[r
].extent
);
473 /* Start creating blit rect */
474 struct radv_meta_blit2d_rect rect
= {
475 .width
= img_extent_el
.width
,
476 .height
= img_extent_el
.height
,
479 if (src_image
->type
== VK_IMAGE_TYPE_3D
)
480 b_src
.layer
= src_offset_el
.z
;
482 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
483 b_dst
.layer
= dst_offset_el
.z
;
485 /* Loop through each 3D or array slice */
486 unsigned num_slices_3d
= img_extent_el
.depth
;
487 unsigned num_slices_array
= pRegions
[r
].dstSubresource
.layerCount
;
488 unsigned slice_3d
= 0;
489 unsigned slice_array
= 0;
490 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
492 /* Finish creating blit rect */
493 rect
.dst_x
= dst_offset_el
.x
;
494 rect
.dst_y
= dst_offset_el
.y
;
495 rect
.src_x
= src_offset_el
.x
;
496 rect
.src_y
= src_offset_el
.y
;
500 !image_is_renderable(cmd_buffer
->device
, b_dst
.image
)) {
501 radv_meta_image_to_image_cs(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
503 radv_meta_blit2d(cmd_buffer
, &b_src
, NULL
, &b_dst
, 1, &rect
);
508 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
515 /* Restore conditional rendering. */
516 cmd_buffer
->state
.predicating
= old_predicating
;
518 radv_meta_restore(&saved_state
, cmd_buffer
);
521 void radv_CmdCopyImage(
522 VkCommandBuffer commandBuffer
,
524 VkImageLayout srcImageLayout
,
526 VkImageLayout destImageLayout
,
527 uint32_t regionCount
,
528 const VkImageCopy
* pRegions
)
530 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
531 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
532 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
534 meta_copy_image(cmd_buffer
,
535 src_image
, srcImageLayout
,
536 dest_image
, destImageLayout
,
537 regionCount
, pRegions
);