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 (img_bsurf
.image
->vk_format
== VK_FORMAT_R32G32B32_UINT
||
200 img_bsurf
.image
->vk_format
== VK_FORMAT_R32G32B32_SINT
||
201 img_bsurf
.image
->vk_format
== VK_FORMAT_R32G32B32_SFLOAT
)) {
202 radv_meta_buffer_to_image_cs(cmd_buffer
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
204 radv_meta_blit2d(cmd_buffer
, NULL
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
207 /* Once we've done the blit, all of the actual information about
208 * the image is embedded in the command buffer so we can just
209 * increment the offset directly in the image effectively
210 * re-binding it to different backing memory.
212 buf_bsurf
.offset
+= buf_extent_el
.width
*
213 buf_extent_el
.height
* buf_bsurf
.bs
;
215 if (image
->type
== VK_IMAGE_TYPE_3D
)
222 /* Restore conditional rendering. */
223 cmd_buffer
->state
.predicating
= old_predicating
;
225 radv_meta_restore(&saved_state
, cmd_buffer
);
228 void radv_CmdCopyBufferToImage(
229 VkCommandBuffer commandBuffer
,
232 VkImageLayout destImageLayout
,
233 uint32_t regionCount
,
234 const VkBufferImageCopy
* pRegions
)
236 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
237 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
238 RADV_FROM_HANDLE(radv_buffer
, src_buffer
, srcBuffer
);
240 meta_copy_buffer_to_image(cmd_buffer
, src_buffer
, dest_image
, destImageLayout
,
241 regionCount
, pRegions
);
245 meta_copy_image_to_buffer(struct radv_cmd_buffer
*cmd_buffer
,
246 struct radv_buffer
* buffer
,
247 struct radv_image
* image
,
248 VkImageLayout layout
,
249 uint32_t regionCount
,
250 const VkBufferImageCopy
* pRegions
)
252 struct radv_meta_saved_state saved_state
;
253 bool old_predicating
;
255 radv_meta_save(&saved_state
, cmd_buffer
,
256 RADV_META_SAVE_COMPUTE_PIPELINE
|
257 RADV_META_SAVE_CONSTANTS
|
258 RADV_META_SAVE_DESCRIPTORS
);
260 /* VK_EXT_conditional_rendering says that copy commands should not be
261 * affected by conditional rendering.
263 old_predicating
= cmd_buffer
->state
.predicating
;
264 cmd_buffer
->state
.predicating
= false;
266 for (unsigned r
= 0; r
< regionCount
; r
++) {
269 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
270 * extent is the size in texels of the source image to copy in width,
271 * height and depth. 1D images use only x and width. 2D images use x, y,
272 * width and height. 3D images use x, y, z, width, height and depth.
275 * Also, convert the offsets and extent from units of texels to units of
276 * blocks - which is the highest resolution accessible in this command.
278 const VkOffset3D img_offset_el
=
279 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
280 const VkExtent3D bufferExtent
= {
281 .width
= pRegions
[r
].bufferRowLength
?
282 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
283 .height
= pRegions
[r
].bufferImageHeight
?
284 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
286 const VkExtent3D buf_extent_el
=
287 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
289 /* Start creating blit rect */
290 const VkExtent3D img_extent_el
=
291 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
292 struct radv_meta_blit2d_rect rect
= {
293 .width
= img_extent_el
.width
,
294 .height
= img_extent_el
.height
,
297 /* Create blit surfaces */
298 struct radv_meta_blit2d_surf img_info
=
299 blit_surf_for_image_level_layer(image
,
301 &pRegions
[r
].imageSubresource
);
303 struct radv_meta_blit2d_buffer buf_info
= {
305 .format
= img_info
.format
,
307 .offset
= pRegions
[r
].bufferOffset
,
308 .pitch
= buf_extent_el
.width
,
311 if (image
->type
== VK_IMAGE_TYPE_3D
)
312 img_info
.layer
= img_offset_el
.z
;
313 /* Loop through each 3D or array slice */
314 unsigned num_slices_3d
= img_extent_el
.depth
;
315 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
316 unsigned slice_3d
= 0;
317 unsigned slice_array
= 0;
318 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
320 rect
.src_x
= img_offset_el
.x
;
321 rect
.src_y
= img_offset_el
.y
;
325 radv_meta_image_to_buffer(cmd_buffer
, &img_info
, &buf_info
, 1, &rect
);
327 buf_info
.offset
+= buf_extent_el
.width
*
328 buf_extent_el
.height
* buf_info
.bs
;
330 if (image
->type
== VK_IMAGE_TYPE_3D
)
337 /* Restore conditional rendering. */
338 cmd_buffer
->state
.predicating
= old_predicating
;
340 radv_meta_restore(&saved_state
, cmd_buffer
);
343 void radv_CmdCopyImageToBuffer(
344 VkCommandBuffer commandBuffer
,
346 VkImageLayout srcImageLayout
,
348 uint32_t regionCount
,
349 const VkBufferImageCopy
* pRegions
)
351 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
352 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
353 RADV_FROM_HANDLE(radv_buffer
, dst_buffer
, destBuffer
);
355 meta_copy_image_to_buffer(cmd_buffer
, dst_buffer
, src_image
,
357 regionCount
, pRegions
);
361 meta_copy_image(struct radv_cmd_buffer
*cmd_buffer
,
362 struct radv_image
*src_image
,
363 VkImageLayout src_image_layout
,
364 struct radv_image
*dest_image
,
365 VkImageLayout dest_image_layout
,
366 uint32_t regionCount
,
367 const VkImageCopy
*pRegions
)
369 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
370 struct radv_meta_saved_state saved_state
;
371 bool old_predicating
;
373 /* From the Vulkan 1.0 spec:
375 * vkCmdCopyImage can be used to copy image data between multisample
376 * images, but both images must have the same number of samples.
378 assert(src_image
->info
.samples
== dest_image
->info
.samples
);
380 radv_meta_save(&saved_state
, cmd_buffer
,
381 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
382 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
383 RADV_META_SAVE_CONSTANTS
|
384 RADV_META_SAVE_DESCRIPTORS
);
386 /* VK_EXT_conditional_rendering says that copy commands should not be
387 * affected by conditional rendering.
389 old_predicating
= cmd_buffer
->state
.predicating
;
390 cmd_buffer
->state
.predicating
= false;
392 for (unsigned r
= 0; r
< regionCount
; r
++) {
393 assert(pRegions
[r
].srcSubresource
.aspectMask
==
394 pRegions
[r
].dstSubresource
.aspectMask
);
396 /* Create blit surfaces */
397 struct radv_meta_blit2d_surf b_src
=
398 blit_surf_for_image_level_layer(src_image
,
400 &pRegions
[r
].srcSubresource
);
402 struct radv_meta_blit2d_surf b_dst
=
403 blit_surf_for_image_level_layer(dest_image
,
405 &pRegions
[r
].dstSubresource
);
407 uint32_t dst_queue_mask
= radv_image_queue_family_mask(dest_image
,
408 cmd_buffer
->queue_family_index
,
409 cmd_buffer
->queue_family_index
);
410 bool dst_compressed
= radv_layout_dcc_compressed(dest_image
, dest_image_layout
, dst_queue_mask
);
411 uint32_t src_queue_mask
= radv_image_queue_family_mask(src_image
,
412 cmd_buffer
->queue_family_index
,
413 cmd_buffer
->queue_family_index
);
414 bool src_compressed
= radv_layout_dcc_compressed(src_image
, src_image_layout
, src_queue_mask
);
416 if (!src_compressed
|| radv_dcc_formats_compatible(b_src
.format
, b_dst
.format
)) {
417 b_src
.format
= b_dst
.format
;
418 } else if (!dst_compressed
) {
419 b_dst
.format
= b_src
.format
;
421 radv_decompress_dcc(cmd_buffer
, dest_image
, &(VkImageSubresourceRange
) {
422 .aspectMask
= pRegions
[r
].dstSubresource
.aspectMask
,
423 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
425 .baseArrayLayer
= pRegions
[r
].dstSubresource
.baseArrayLayer
,
426 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
428 b_dst
.format
= b_src
.format
;
429 b_dst
.current_layout
= VK_IMAGE_LAYOUT_GENERAL
;
434 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
435 * imageExtent is the size in texels of the image to copy in width, height
436 * and depth. 1D images use only x and width. 2D images use x, y, width
437 * and height. 3D images use x, y, z, width, height and depth.
439 * Also, convert the offsets and extent from units of texels to units of
440 * blocks - which is the highest resolution accessible in this command.
442 const VkOffset3D dst_offset_el
=
443 meta_region_offset_el(dest_image
, &pRegions
[r
].dstOffset
);
444 const VkOffset3D src_offset_el
=
445 meta_region_offset_el(src_image
, &pRegions
[r
].srcOffset
);
448 * From Vulkan 1.0.68, "Copying Data Between Images":
449 * "When copying between compressed and uncompressed formats
450 * the extent members represent the texel dimensions of the
451 * source image and not the destination."
452 * However, we must use the destination image type to avoid
453 * clamping depth when copying multiple layers of a 2D image to
456 const VkExtent3D img_extent_el
=
457 meta_region_extent_el(src_image
, dest_image
->type
, &pRegions
[r
].extent
);
459 /* Start creating blit rect */
460 struct radv_meta_blit2d_rect rect
= {
461 .width
= img_extent_el
.width
,
462 .height
= img_extent_el
.height
,
465 if (src_image
->type
== VK_IMAGE_TYPE_3D
)
466 b_src
.layer
= src_offset_el
.z
;
468 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
469 b_dst
.layer
= dst_offset_el
.z
;
471 /* Loop through each 3D or array slice */
472 unsigned num_slices_3d
= img_extent_el
.depth
;
473 unsigned num_slices_array
= pRegions
[r
].dstSubresource
.layerCount
;
474 unsigned slice_3d
= 0;
475 unsigned slice_array
= 0;
476 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
478 /* Finish creating blit rect */
479 rect
.dst_x
= dst_offset_el
.x
;
480 rect
.dst_y
= dst_offset_el
.y
;
481 rect
.src_x
= src_offset_el
.x
;
482 rect
.src_y
= src_offset_el
.y
;
486 radv_meta_image_to_image_cs(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
488 radv_meta_blit2d(cmd_buffer
, &b_src
, NULL
, &b_dst
, 1, &rect
);
492 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
499 /* Restore conditional rendering. */
500 cmd_buffer
->state
.predicating
= old_predicating
;
502 radv_meta_restore(&saved_state
, cmd_buffer
);
505 void radv_CmdCopyImage(
506 VkCommandBuffer commandBuffer
,
508 VkImageLayout srcImageLayout
,
510 VkImageLayout destImageLayout
,
511 uint32_t regionCount
,
512 const VkImageCopy
* pRegions
)
514 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
515 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
516 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
518 meta_copy_image(cmd_buffer
,
519 src_image
, srcImageLayout
,
520 dest_image
, destImageLayout
,
521 regionCount
, pRegions
);