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 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
)
118 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
119 struct radv_meta_saved_state saved_state
;
120 bool old_predicating
;
122 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
123 * VK_SAMPLE_COUNT_1_BIT."
125 assert(image
->info
.samples
== 1);
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
);
133 /* VK_EXT_conditional_rendering says that copy commands should not be
134 * affected by conditional rendering.
136 old_predicating
= cmd_buffer
->state
.predicating
;
137 cmd_buffer
->state
.predicating
= false;
139 for (unsigned r
= 0; r
< regionCount
; r
++) {
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.
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.
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
,
159 const VkExtent3D buf_extent_el
=
160 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
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
,
170 /* Create blit surfaces */
171 struct radv_meta_blit2d_surf img_bsurf
=
172 blit_surf_for_image_level_layer(image
,
174 &pRegions
[r
].imageSubresource
);
176 struct radv_meta_blit2d_buffer buf_bsurf
= {
178 .format
= img_bsurf
.format
,
180 .offset
= pRegions
[r
].bufferOffset
,
181 .pitch
= buf_extent_el
.width
,
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
) {
193 rect
.dst_x
= img_offset_el
.x
;
194 rect
.dst_y
= img_offset_el
.y
;
199 radv_meta_buffer_to_image_cs(cmd_buffer
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
201 radv_meta_blit2d(cmd_buffer
, NULL
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
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.
208 buf_bsurf
.offset
+= buf_extent_el
.width
*
209 buf_extent_el
.height
* buf_bsurf
.bs
;
211 if (image
->type
== VK_IMAGE_TYPE_3D
)
218 /* Restore conditional rendering. */
219 cmd_buffer
->state
.predicating
= old_predicating
;
221 radv_meta_restore(&saved_state
, cmd_buffer
);
224 void radv_CmdCopyBufferToImage(
225 VkCommandBuffer commandBuffer
,
228 VkImageLayout destImageLayout
,
229 uint32_t regionCount
,
230 const VkBufferImageCopy
* pRegions
)
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
);
236 meta_copy_buffer_to_image(cmd_buffer
, src_buffer
, dest_image
, destImageLayout
,
237 regionCount
, pRegions
);
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
)
248 struct radv_meta_saved_state saved_state
;
249 bool old_predicating
;
251 radv_meta_save(&saved_state
, cmd_buffer
,
252 RADV_META_SAVE_COMPUTE_PIPELINE
|
253 RADV_META_SAVE_CONSTANTS
|
254 RADV_META_SAVE_DESCRIPTORS
);
256 /* VK_EXT_conditional_rendering says that copy commands should not be
257 * affected by conditional rendering.
259 old_predicating
= cmd_buffer
->state
.predicating
;
260 cmd_buffer
->state
.predicating
= false;
262 for (unsigned r
= 0; r
< regionCount
; r
++) {
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.
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.
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
,
282 const VkExtent3D buf_extent_el
=
283 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
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
,
293 /* Create blit surfaces */
294 struct radv_meta_blit2d_surf img_info
=
295 blit_surf_for_image_level_layer(image
,
297 &pRegions
[r
].imageSubresource
);
299 struct radv_meta_blit2d_buffer buf_info
= {
301 .format
= img_info
.format
,
303 .offset
= pRegions
[r
].bufferOffset
,
304 .pitch
= buf_extent_el
.width
,
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
) {
316 rect
.src_x
= img_offset_el
.x
;
317 rect
.src_y
= img_offset_el
.y
;
321 radv_meta_image_to_buffer(cmd_buffer
, &img_info
, &buf_info
, 1, &rect
);
323 buf_info
.offset
+= buf_extent_el
.width
*
324 buf_extent_el
.height
* buf_info
.bs
;
326 if (image
->type
== VK_IMAGE_TYPE_3D
)
333 /* Restore conditional rendering. */
334 cmd_buffer
->state
.predicating
= old_predicating
;
336 radv_meta_restore(&saved_state
, cmd_buffer
);
339 void radv_CmdCopyImageToBuffer(
340 VkCommandBuffer commandBuffer
,
342 VkImageLayout srcImageLayout
,
344 uint32_t regionCount
,
345 const VkBufferImageCopy
* pRegions
)
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
);
351 meta_copy_image_to_buffer(cmd_buffer
, dst_buffer
, src_image
,
353 regionCount
, pRegions
);
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
)
365 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
366 struct radv_meta_saved_state saved_state
;
367 bool old_predicating
;
369 /* From the Vulkan 1.0 spec:
371 * vkCmdCopyImage can be used to copy image data between multisample
372 * images, but both images must have the same number of samples.
374 assert(src_image
->info
.samples
== dest_image
->info
.samples
);
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
);
382 /* VK_EXT_conditional_rendering says that copy commands should not be
383 * affected by conditional rendering.
385 old_predicating
= cmd_buffer
->state
.predicating
;
386 cmd_buffer
->state
.predicating
= false;
388 for (unsigned r
= 0; r
< regionCount
; r
++) {
389 assert(pRegions
[r
].srcSubresource
.aspectMask
==
390 pRegions
[r
].dstSubresource
.aspectMask
);
392 /* Create blit surfaces */
393 struct radv_meta_blit2d_surf b_src
=
394 blit_surf_for_image_level_layer(src_image
,
396 &pRegions
[r
].srcSubresource
);
398 struct radv_meta_blit2d_surf b_dst
=
399 blit_surf_for_image_level_layer(dest_image
,
401 &pRegions
[r
].dstSubresource
);
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
);
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
;
417 radv_decompress_dcc(cmd_buffer
, dest_image
, &(VkImageSubresourceRange
) {
418 .aspectMask
= pRegions
[r
].dstSubresource
.aspectMask
,
419 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
421 .baseArrayLayer
= pRegions
[r
].dstSubresource
.baseArrayLayer
,
422 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
424 b_dst
.format
= b_src
.format
;
425 b_dst
.current_layout
= VK_IMAGE_LAYOUT_GENERAL
;
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.
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.
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
);
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
452 const VkExtent3D img_extent_el
=
453 meta_region_extent_el(src_image
, dest_image
->type
, &pRegions
[r
].extent
);
455 /* Start creating blit rect */
456 struct radv_meta_blit2d_rect rect
= {
457 .width
= img_extent_el
.width
,
458 .height
= img_extent_el
.height
,
461 if (src_image
->type
== VK_IMAGE_TYPE_3D
)
462 b_src
.layer
= src_offset_el
.z
;
464 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
465 b_dst
.layer
= dst_offset_el
.z
;
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
) {
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
;
482 radv_meta_image_to_image_cs(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
484 radv_meta_blit2d(cmd_buffer
, &b_src
, NULL
, &b_dst
, 1, &rect
);
488 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
495 /* Restore conditional rendering. */
496 cmd_buffer
->state
.predicating
= old_predicating
;
498 radv_meta_restore(&saved_state
, cmd_buffer
);
501 void radv_CmdCopyImage(
502 VkCommandBuffer commandBuffer
,
504 VkImageLayout srcImageLayout
,
506 VkImageLayout destImageLayout
,
507 uint32_t regionCount
,
508 const VkImageCopy
* pRegions
)
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
);
514 meta_copy_image(cmd_buffer
,
515 src_image
, srcImageLayout
,
516 dest_image
, destImageLayout
,
517 regionCount
, pRegions
);