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 return (struct radv_meta_blit2d_surf
) {
99 .bs
= vk_format_get_blocksize(format
),
100 .level
= subres
->mipLevel
,
101 .layer
= subres
->baseArrayLayer
,
103 .aspect_mask
= subres
->aspectMask
,
104 .current_layout
= layout
,
109 meta_copy_buffer_to_image(struct radv_cmd_buffer
*cmd_buffer
,
110 struct radv_buffer
* buffer
,
111 struct radv_image
* image
,
112 VkImageLayout layout
,
113 uint32_t regionCount
,
114 const VkBufferImageCopy
* pRegions
)
116 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
117 struct radv_meta_saved_state saved_state
;
119 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
120 * VK_SAMPLE_COUNT_1_BIT."
122 assert(image
->info
.samples
== 1);
124 radv_meta_save(&saved_state
, cmd_buffer
,
125 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
126 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
127 RADV_META_SAVE_CONSTANTS
|
128 RADV_META_SAVE_DESCRIPTORS
);
130 for (unsigned r
= 0; r
< regionCount
; r
++) {
133 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
134 * extent is the size in texels of the source image to copy in width,
135 * height and depth. 1D images use only x and width. 2D images use x, y,
136 * width and height. 3D images use x, y, z, width, height and depth.
139 * Also, convert the offsets and extent from units of texels to units of
140 * blocks - which is the highest resolution accessible in this command.
142 const VkOffset3D img_offset_el
=
143 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
144 const VkExtent3D bufferExtent
= {
145 .width
= pRegions
[r
].bufferRowLength
?
146 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
147 .height
= pRegions
[r
].bufferImageHeight
?
148 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
150 const VkExtent3D buf_extent_el
=
151 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
153 /* Start creating blit rect */
154 const VkExtent3D img_extent_el
=
155 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
156 struct radv_meta_blit2d_rect rect
= {
157 .width
= img_extent_el
.width
,
158 .height
= img_extent_el
.height
,
161 /* Create blit surfaces */
162 struct radv_meta_blit2d_surf img_bsurf
=
163 blit_surf_for_image_level_layer(image
,
165 &pRegions
[r
].imageSubresource
);
167 struct radv_meta_blit2d_buffer buf_bsurf
= {
169 .format
= img_bsurf
.format
,
171 .offset
= pRegions
[r
].bufferOffset
,
172 .pitch
= buf_extent_el
.width
,
175 if (image
->type
== VK_IMAGE_TYPE_3D
)
176 img_bsurf
.layer
= img_offset_el
.z
;
177 /* Loop through each 3D or array slice */
178 unsigned num_slices_3d
= img_extent_el
.depth
;
179 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
180 unsigned slice_3d
= 0;
181 unsigned slice_array
= 0;
182 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
184 rect
.dst_x
= img_offset_el
.x
;
185 rect
.dst_y
= img_offset_el
.y
;
190 radv_meta_buffer_to_image_cs(cmd_buffer
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
192 radv_meta_blit2d(cmd_buffer
, NULL
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
194 /* Once we've done the blit, all of the actual information about
195 * the image is embedded in the command buffer so we can just
196 * increment the offset directly in the image effectively
197 * re-binding it to different backing memory.
199 buf_bsurf
.offset
+= buf_extent_el
.width
*
200 buf_extent_el
.height
* buf_bsurf
.bs
;
202 if (image
->type
== VK_IMAGE_TYPE_3D
)
209 radv_meta_restore(&saved_state
, cmd_buffer
);
212 void radv_CmdCopyBufferToImage(
213 VkCommandBuffer commandBuffer
,
216 VkImageLayout destImageLayout
,
217 uint32_t regionCount
,
218 const VkBufferImageCopy
* pRegions
)
220 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
221 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
222 RADV_FROM_HANDLE(radv_buffer
, src_buffer
, srcBuffer
);
224 meta_copy_buffer_to_image(cmd_buffer
, src_buffer
, dest_image
, destImageLayout
,
225 regionCount
, pRegions
);
229 meta_copy_image_to_buffer(struct radv_cmd_buffer
*cmd_buffer
,
230 struct radv_buffer
* buffer
,
231 struct radv_image
* image
,
232 VkImageLayout layout
,
233 uint32_t regionCount
,
234 const VkBufferImageCopy
* pRegions
)
236 struct radv_meta_saved_state saved_state
;
238 radv_meta_save(&saved_state
, cmd_buffer
,
239 RADV_META_SAVE_COMPUTE_PIPELINE
|
240 RADV_META_SAVE_CONSTANTS
|
241 RADV_META_SAVE_DESCRIPTORS
);
243 for (unsigned r
= 0; r
< regionCount
; r
++) {
246 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
247 * extent is the size in texels of the source image to copy in width,
248 * height and depth. 1D images use only x and width. 2D images use x, y,
249 * width and height. 3D images use x, y, z, width, height and depth.
252 * Also, convert the offsets and extent from units of texels to units of
253 * blocks - which is the highest resolution accessible in this command.
255 const VkOffset3D img_offset_el
=
256 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
257 const VkExtent3D bufferExtent
= {
258 .width
= pRegions
[r
].bufferRowLength
?
259 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
260 .height
= pRegions
[r
].bufferImageHeight
?
261 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
263 const VkExtent3D buf_extent_el
=
264 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
266 /* Start creating blit rect */
267 const VkExtent3D img_extent_el
=
268 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
269 struct radv_meta_blit2d_rect rect
= {
270 .width
= img_extent_el
.width
,
271 .height
= img_extent_el
.height
,
274 /* Create blit surfaces */
275 struct radv_meta_blit2d_surf img_info
=
276 blit_surf_for_image_level_layer(image
,
278 &pRegions
[r
].imageSubresource
);
280 struct radv_meta_blit2d_buffer buf_info
= {
282 .format
= img_info
.format
,
284 .offset
= pRegions
[r
].bufferOffset
,
285 .pitch
= buf_extent_el
.width
,
288 if (image
->type
== VK_IMAGE_TYPE_3D
)
289 img_info
.layer
= img_offset_el
.z
;
290 /* Loop through each 3D or array slice */
291 unsigned num_slices_3d
= img_extent_el
.depth
;
292 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
293 unsigned slice_3d
= 0;
294 unsigned slice_array
= 0;
295 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
297 rect
.src_x
= img_offset_el
.x
;
298 rect
.src_y
= img_offset_el
.y
;
302 radv_meta_image_to_buffer(cmd_buffer
, &img_info
, &buf_info
, 1, &rect
);
304 buf_info
.offset
+= buf_extent_el
.width
*
305 buf_extent_el
.height
* buf_info
.bs
;
307 if (image
->type
== VK_IMAGE_TYPE_3D
)
314 radv_meta_restore(&saved_state
, cmd_buffer
);
317 void radv_CmdCopyImageToBuffer(
318 VkCommandBuffer commandBuffer
,
320 VkImageLayout srcImageLayout
,
322 uint32_t regionCount
,
323 const VkBufferImageCopy
* pRegions
)
325 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
326 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
327 RADV_FROM_HANDLE(radv_buffer
, dst_buffer
, destBuffer
);
329 meta_copy_image_to_buffer(cmd_buffer
, dst_buffer
, src_image
,
331 regionCount
, pRegions
);
335 meta_copy_image(struct radv_cmd_buffer
*cmd_buffer
,
336 struct radv_image
*src_image
,
337 VkImageLayout src_image_layout
,
338 struct radv_image
*dest_image
,
339 VkImageLayout dest_image_layout
,
340 uint32_t regionCount
,
341 const VkImageCopy
*pRegions
)
343 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
344 struct radv_meta_saved_state saved_state
;
346 /* From the Vulkan 1.0 spec:
348 * vkCmdCopyImage can be used to copy image data between multisample
349 * images, but both images must have the same number of samples.
351 assert(src_image
->info
.samples
== dest_image
->info
.samples
);
353 radv_meta_save(&saved_state
, cmd_buffer
,
354 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
355 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
356 RADV_META_SAVE_CONSTANTS
|
357 RADV_META_SAVE_DESCRIPTORS
);
359 for (unsigned r
= 0; r
< regionCount
; r
++) {
360 assert(pRegions
[r
].srcSubresource
.aspectMask
==
361 pRegions
[r
].dstSubresource
.aspectMask
);
363 /* Create blit surfaces */
364 struct radv_meta_blit2d_surf b_src
=
365 blit_surf_for_image_level_layer(src_image
,
367 &pRegions
[r
].srcSubresource
);
369 struct radv_meta_blit2d_surf b_dst
=
370 blit_surf_for_image_level_layer(dest_image
,
372 &pRegions
[r
].dstSubresource
);
374 uint32_t dst_queue_mask
= radv_image_queue_family_mask(dest_image
,
375 cmd_buffer
->queue_family_index
,
376 cmd_buffer
->queue_family_index
);
377 bool dst_compressed
= radv_layout_dcc_compressed(dest_image
, dest_image_layout
, dst_queue_mask
);
378 uint32_t src_queue_mask
= radv_image_queue_family_mask(src_image
,
379 cmd_buffer
->queue_family_index
,
380 cmd_buffer
->queue_family_index
);
381 bool src_compressed
= radv_layout_dcc_compressed(src_image
, src_image_layout
, src_queue_mask
);
383 if (!src_compressed
|| radv_dcc_formats_compatible(b_src
.format
, b_dst
.format
)) {
384 b_src
.format
= b_dst
.format
;
385 } else if (!dst_compressed
) {
386 b_dst
.format
= b_src
.format
;
388 radv_decompress_dcc(cmd_buffer
, dest_image
, &(VkImageSubresourceRange
) {
389 .aspectMask
= pRegions
[r
].dstSubresource
.aspectMask
,
390 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
392 .baseArrayLayer
= pRegions
[r
].dstSubresource
.baseArrayLayer
,
393 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
395 b_dst
.format
= b_src
.format
;
396 b_dst
.current_layout
= VK_IMAGE_LAYOUT_GENERAL
;
401 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
402 * imageExtent is the size in texels of the image to copy in width, height
403 * and depth. 1D images use only x and width. 2D images use x, y, width
404 * and height. 3D images use x, y, z, width, height and depth.
406 * Also, convert the offsets and extent from units of texels to units of
407 * blocks - which is the highest resolution accessible in this command.
409 const VkOffset3D dst_offset_el
=
410 meta_region_offset_el(dest_image
, &pRegions
[r
].dstOffset
);
411 const VkOffset3D src_offset_el
=
412 meta_region_offset_el(src_image
, &pRegions
[r
].srcOffset
);
415 * From Vulkan 1.0.68, "Copying Data Between Images":
416 * "When copying between compressed and uncompressed formats
417 * the extent members represent the texel dimensions of the
418 * source image and not the destination."
419 * However, we must use the destination image type to avoid
420 * clamping depth when copying multiple layers of a 2D image to
423 const VkExtent3D img_extent_el
=
424 meta_region_extent_el(src_image
, dest_image
->type
, &pRegions
[r
].extent
);
426 /* Start creating blit rect */
427 struct radv_meta_blit2d_rect rect
= {
428 .width
= img_extent_el
.width
,
429 .height
= img_extent_el
.height
,
432 if (src_image
->type
== VK_IMAGE_TYPE_3D
)
433 b_src
.layer
= src_offset_el
.z
;
435 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
436 b_dst
.layer
= dst_offset_el
.z
;
438 /* Loop through each 3D or array slice */
439 unsigned num_slices_3d
= img_extent_el
.depth
;
440 unsigned num_slices_array
= pRegions
[r
].dstSubresource
.layerCount
;
441 unsigned slice_3d
= 0;
442 unsigned slice_array
= 0;
443 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
445 /* Finish creating blit rect */
446 rect
.dst_x
= dst_offset_el
.x
;
447 rect
.dst_y
= dst_offset_el
.y
;
448 rect
.src_x
= src_offset_el
.x
;
449 rect
.src_y
= src_offset_el
.y
;
453 radv_meta_image_to_image_cs(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
455 radv_meta_blit2d(cmd_buffer
, &b_src
, NULL
, &b_dst
, 1, &rect
);
459 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
466 radv_meta_restore(&saved_state
, cmd_buffer
);
469 void radv_CmdCopyImage(
470 VkCommandBuffer commandBuffer
,
472 VkImageLayout srcImageLayout
,
474 VkImageLayout destImageLayout
,
475 uint32_t regionCount
,
476 const VkImageCopy
* pRegions
)
478 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
479 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
480 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
482 meta_copy_image(cmd_buffer
,
483 src_image
, srcImageLayout
,
484 dest_image
, destImageLayout
,
485 regionCount
, pRegions
);
488 void radv_blit_to_prime_linear(struct radv_cmd_buffer
*cmd_buffer
,
489 struct radv_image
*image
,
490 struct radv_image
*linear_image
)
492 struct VkImageCopy image_copy
= { 0 };
494 image_copy
.srcSubresource
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
495 image_copy
.srcSubresource
.layerCount
= 1;
497 image_copy
.dstSubresource
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
498 image_copy
.dstSubresource
.layerCount
= 1;
500 image_copy
.extent
.width
= image
->info
.width
;
501 image_copy
.extent
.height
= image
->info
.height
;
502 image_copy
.extent
.depth
= 1;
504 meta_copy_image(cmd_buffer
, image
, VK_IMAGE_LAYOUT_GENERAL
, linear_image
,
505 VK_IMAGE_LAYOUT_GENERAL
,