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 16: return VK_FORMAT_R32G32B32A32_UINT
;
77 unreachable("Invalid format block size");
81 static struct radv_meta_blit2d_surf
82 blit_surf_for_image_level_layer(struct radv_image
*image
,
84 const VkImageSubresourceLayers
*subres
)
86 VkFormat format
= image
->vk_format
;
87 if (subres
->aspectMask
& VK_IMAGE_ASPECT_DEPTH_BIT
)
88 format
= vk_format_depth_only(format
);
89 else if (subres
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
)
90 format
= vk_format_stencil_only(format
);
92 if (!radv_image_has_dcc(image
) &&
93 !(radv_image_is_tc_compat_htile(image
)))
94 format
= vk_format_for_size(vk_format_get_blocksize(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
= subres
->aspectMask
,
103 .current_layout
= layout
,
108 meta_copy_buffer_to_image(struct radv_cmd_buffer
*cmd_buffer
,
109 struct radv_buffer
* buffer
,
110 struct radv_image
* image
,
111 VkImageLayout layout
,
112 uint32_t regionCount
,
113 const VkBufferImageCopy
* pRegions
)
115 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
116 struct radv_meta_saved_state saved_state
;
118 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
119 * VK_SAMPLE_COUNT_1_BIT."
121 assert(image
->info
.samples
== 1);
123 radv_meta_save(&saved_state
, cmd_buffer
,
124 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
125 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
126 RADV_META_SAVE_CONSTANTS
|
127 RADV_META_SAVE_DESCRIPTORS
);
129 for (unsigned r
= 0; r
< regionCount
; r
++) {
132 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
133 * extent is the size in texels of the source image to copy in width,
134 * height and depth. 1D images use only x and width. 2D images use x, y,
135 * width and height. 3D images use x, y, z, width, height and depth.
138 * Also, convert the offsets and extent from units of texels to units of
139 * blocks - which is the highest resolution accessible in this command.
141 const VkOffset3D img_offset_el
=
142 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
143 const VkExtent3D bufferExtent
= {
144 .width
= pRegions
[r
].bufferRowLength
?
145 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
146 .height
= pRegions
[r
].bufferImageHeight
?
147 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
149 const VkExtent3D buf_extent_el
=
150 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
152 /* Start creating blit rect */
153 const VkExtent3D img_extent_el
=
154 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
155 struct radv_meta_blit2d_rect rect
= {
156 .width
= img_extent_el
.width
,
157 .height
= img_extent_el
.height
,
160 /* Create blit surfaces */
161 struct radv_meta_blit2d_surf img_bsurf
=
162 blit_surf_for_image_level_layer(image
,
164 &pRegions
[r
].imageSubresource
);
166 struct radv_meta_blit2d_buffer buf_bsurf
= {
168 .format
= img_bsurf
.format
,
170 .offset
= pRegions
[r
].bufferOffset
,
171 .pitch
= buf_extent_el
.width
,
174 if (image
->type
== VK_IMAGE_TYPE_3D
)
175 img_bsurf
.layer
= img_offset_el
.z
;
176 /* Loop through each 3D or array slice */
177 unsigned num_slices_3d
= img_extent_el
.depth
;
178 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
179 unsigned slice_3d
= 0;
180 unsigned slice_array
= 0;
181 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
183 rect
.dst_x
= img_offset_el
.x
;
184 rect
.dst_y
= img_offset_el
.y
;
189 radv_meta_buffer_to_image_cs(cmd_buffer
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
191 radv_meta_blit2d(cmd_buffer
, NULL
, &buf_bsurf
, &img_bsurf
, 1, &rect
);
193 /* Once we've done the blit, all of the actual information about
194 * the image is embedded in the command buffer so we can just
195 * increment the offset directly in the image effectively
196 * re-binding it to different backing memory.
198 buf_bsurf
.offset
+= buf_extent_el
.width
*
199 buf_extent_el
.height
* buf_bsurf
.bs
;
201 if (image
->type
== VK_IMAGE_TYPE_3D
)
208 radv_meta_restore(&saved_state
, cmd_buffer
);
211 void radv_CmdCopyBufferToImage(
212 VkCommandBuffer commandBuffer
,
215 VkImageLayout destImageLayout
,
216 uint32_t regionCount
,
217 const VkBufferImageCopy
* pRegions
)
219 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
220 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
221 RADV_FROM_HANDLE(radv_buffer
, src_buffer
, srcBuffer
);
223 meta_copy_buffer_to_image(cmd_buffer
, src_buffer
, dest_image
, destImageLayout
,
224 regionCount
, pRegions
);
228 meta_copy_image_to_buffer(struct radv_cmd_buffer
*cmd_buffer
,
229 struct radv_buffer
* buffer
,
230 struct radv_image
* image
,
231 VkImageLayout layout
,
232 uint32_t regionCount
,
233 const VkBufferImageCopy
* pRegions
)
235 struct radv_meta_saved_state saved_state
;
237 radv_meta_save(&saved_state
, cmd_buffer
,
238 RADV_META_SAVE_COMPUTE_PIPELINE
|
239 RADV_META_SAVE_CONSTANTS
|
240 RADV_META_SAVE_DESCRIPTORS
);
242 for (unsigned r
= 0; r
< regionCount
; r
++) {
245 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
246 * extent is the size in texels of the source image to copy in width,
247 * height and depth. 1D images use only x and width. 2D images use x, y,
248 * width and height. 3D images use x, y, z, width, height and depth.
251 * Also, convert the offsets and extent from units of texels to units of
252 * blocks - which is the highest resolution accessible in this command.
254 const VkOffset3D img_offset_el
=
255 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
256 const VkExtent3D bufferExtent
= {
257 .width
= pRegions
[r
].bufferRowLength
?
258 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
259 .height
= pRegions
[r
].bufferImageHeight
?
260 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
262 const VkExtent3D buf_extent_el
=
263 meta_region_extent_el(image
, image
->type
, &bufferExtent
);
265 /* Start creating blit rect */
266 const VkExtent3D img_extent_el
=
267 meta_region_extent_el(image
, image
->type
, &pRegions
[r
].imageExtent
);
268 struct radv_meta_blit2d_rect rect
= {
269 .width
= img_extent_el
.width
,
270 .height
= img_extent_el
.height
,
273 /* Create blit surfaces */
274 struct radv_meta_blit2d_surf img_info
=
275 blit_surf_for_image_level_layer(image
,
277 &pRegions
[r
].imageSubresource
);
279 struct radv_meta_blit2d_buffer buf_info
= {
281 .format
= img_info
.format
,
283 .offset
= pRegions
[r
].bufferOffset
,
284 .pitch
= buf_extent_el
.width
,
287 if (image
->type
== VK_IMAGE_TYPE_3D
)
288 img_info
.layer
= img_offset_el
.z
;
289 /* Loop through each 3D or array slice */
290 unsigned num_slices_3d
= img_extent_el
.depth
;
291 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
292 unsigned slice_3d
= 0;
293 unsigned slice_array
= 0;
294 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
296 rect
.src_x
= img_offset_el
.x
;
297 rect
.src_y
= img_offset_el
.y
;
301 radv_meta_image_to_buffer(cmd_buffer
, &img_info
, &buf_info
, 1, &rect
);
303 buf_info
.offset
+= buf_extent_el
.width
*
304 buf_extent_el
.height
* buf_info
.bs
;
306 if (image
->type
== VK_IMAGE_TYPE_3D
)
313 radv_meta_restore(&saved_state
, cmd_buffer
);
316 void radv_CmdCopyImageToBuffer(
317 VkCommandBuffer commandBuffer
,
319 VkImageLayout srcImageLayout
,
321 uint32_t regionCount
,
322 const VkBufferImageCopy
* pRegions
)
324 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
325 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
326 RADV_FROM_HANDLE(radv_buffer
, dst_buffer
, destBuffer
);
328 meta_copy_image_to_buffer(cmd_buffer
, dst_buffer
, src_image
,
330 regionCount
, pRegions
);
334 meta_copy_image(struct radv_cmd_buffer
*cmd_buffer
,
335 struct radv_image
*src_image
,
336 VkImageLayout src_image_layout
,
337 struct radv_image
*dest_image
,
338 VkImageLayout dest_image_layout
,
339 uint32_t regionCount
,
340 const VkImageCopy
*pRegions
)
342 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
343 struct radv_meta_saved_state saved_state
;
345 /* From the Vulkan 1.0 spec:
347 * vkCmdCopyImage can be used to copy image data between multisample
348 * images, but both images must have the same number of samples.
350 assert(src_image
->info
.samples
== dest_image
->info
.samples
);
352 radv_meta_save(&saved_state
, cmd_buffer
,
353 (cs
? RADV_META_SAVE_COMPUTE_PIPELINE
:
354 RADV_META_SAVE_GRAPHICS_PIPELINE
) |
355 RADV_META_SAVE_CONSTANTS
|
356 RADV_META_SAVE_DESCRIPTORS
);
358 for (unsigned r
= 0; r
< regionCount
; r
++) {
359 assert(pRegions
[r
].srcSubresource
.aspectMask
==
360 pRegions
[r
].dstSubresource
.aspectMask
);
362 /* Create blit surfaces */
363 struct radv_meta_blit2d_surf b_src
=
364 blit_surf_for_image_level_layer(src_image
,
366 &pRegions
[r
].srcSubresource
);
368 struct radv_meta_blit2d_surf b_dst
=
369 blit_surf_for_image_level_layer(dest_image
,
371 &pRegions
[r
].dstSubresource
);
373 uint32_t dst_queue_mask
= radv_image_queue_family_mask(dest_image
,
374 cmd_buffer
->queue_family_index
,
375 cmd_buffer
->queue_family_index
);
376 bool dst_compressed
= radv_layout_dcc_compressed(dest_image
, dest_image_layout
, dst_queue_mask
);
377 uint32_t src_queue_mask
= radv_image_queue_family_mask(src_image
,
378 cmd_buffer
->queue_family_index
,
379 cmd_buffer
->queue_family_index
);
380 bool src_compressed
= radv_layout_dcc_compressed(src_image
, src_image_layout
, src_queue_mask
);
382 if (!src_compressed
|| radv_dcc_formats_compatible(b_src
.format
, b_dst
.format
)) {
383 b_src
.format
= b_dst
.format
;
384 } else if (!dst_compressed
) {
385 b_dst
.format
= b_src
.format
;
387 radv_decompress_dcc(cmd_buffer
, dest_image
, &(VkImageSubresourceRange
) {
388 .aspectMask
= pRegions
[r
].dstSubresource
.aspectMask
,
389 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
391 .baseArrayLayer
= pRegions
[r
].dstSubresource
.baseArrayLayer
,
392 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
394 b_dst
.format
= b_src
.format
;
395 b_dst
.current_layout
= VK_IMAGE_LAYOUT_GENERAL
;
400 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
401 * imageExtent is the size in texels of the image to copy in width, height
402 * and depth. 1D images use only x and width. 2D images use x, y, width
403 * and height. 3D images use x, y, z, width, height and depth.
405 * Also, convert the offsets and extent from units of texels to units of
406 * blocks - which is the highest resolution accessible in this command.
408 const VkOffset3D dst_offset_el
=
409 meta_region_offset_el(dest_image
, &pRegions
[r
].dstOffset
);
410 const VkOffset3D src_offset_el
=
411 meta_region_offset_el(src_image
, &pRegions
[r
].srcOffset
);
414 * From Vulkan 1.0.68, "Copying Data Between Images":
415 * "When copying between compressed and uncompressed formats
416 * the extent members represent the texel dimensions of the
417 * source image and not the destination."
418 * However, we must use the destination image type to avoid
419 * clamping depth when copying multiple layers of a 2D image to
422 const VkExtent3D img_extent_el
=
423 meta_region_extent_el(src_image
, dest_image
->type
, &pRegions
[r
].extent
);
425 /* Start creating blit rect */
426 struct radv_meta_blit2d_rect rect
= {
427 .width
= img_extent_el
.width
,
428 .height
= img_extent_el
.height
,
431 if (src_image
->type
== VK_IMAGE_TYPE_3D
)
432 b_src
.layer
= src_offset_el
.z
;
434 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
435 b_dst
.layer
= dst_offset_el
.z
;
437 /* Loop through each 3D or array slice */
438 unsigned num_slices_3d
= img_extent_el
.depth
;
439 unsigned num_slices_array
= pRegions
[r
].dstSubresource
.layerCount
;
440 unsigned slice_3d
= 0;
441 unsigned slice_array
= 0;
442 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
444 /* Finish creating blit rect */
445 rect
.dst_x
= dst_offset_el
.x
;
446 rect
.dst_y
= dst_offset_el
.y
;
447 rect
.src_x
= src_offset_el
.x
;
448 rect
.src_y
= src_offset_el
.y
;
452 radv_meta_image_to_image_cs(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
454 radv_meta_blit2d(cmd_buffer
, &b_src
, NULL
, &b_dst
, 1, &rect
);
458 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
465 radv_meta_restore(&saved_state
, cmd_buffer
);
468 void radv_CmdCopyImage(
469 VkCommandBuffer commandBuffer
,
471 VkImageLayout srcImageLayout
,
473 VkImageLayout destImageLayout
,
474 uint32_t regionCount
,
475 const VkImageCopy
* pRegions
)
477 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
478 RADV_FROM_HANDLE(radv_image
, src_image
, srcImage
);
479 RADV_FROM_HANDLE(radv_image
, dest_image
, destImage
);
481 meta_copy_image(cmd_buffer
,
482 src_image
, srcImageLayout
,
483 dest_image
, destImageLayout
,
484 regionCount
, pRegions
);
487 void radv_blit_to_prime_linear(struct radv_cmd_buffer
*cmd_buffer
,
488 struct radv_image
*image
,
489 struct radv_image
*linear_image
)
491 struct VkImageCopy image_copy
= { 0 };
493 image_copy
.srcSubresource
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
494 image_copy
.srcSubresource
.layerCount
= 1;
496 image_copy
.dstSubresource
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
497 image_copy
.dstSubresource
.layerCount
= 1;
499 image_copy
.extent
.width
= image
->info
.width
;
500 image_copy
.extent
.height
= image
->info
.height
;
501 image_copy
.extent
.depth
= 1;
503 meta_copy_image(cmd_buffer
, image
, VK_IMAGE_LAYOUT_GENERAL
, linear_image
,
504 VK_IMAGE_LAYOUT_GENERAL
,