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
26 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
27 * elements rather than texels. One element equals one texel or one block
28 * if Image is uncompressed or compressed, respectively.
30 static struct VkExtent3D
31 meta_region_extent_el(const struct anv_image
*image
,
32 const struct VkExtent3D
*extent
)
34 const struct isl_format_layout
*isl_layout
=
35 anv_format_for_vk_format(image
->vk_format
)->isl_layout
;
36 return anv_sanitize_image_extent(image
->type
, (VkExtent3D
) {
37 .width
= DIV_ROUND_UP(extent
->width
, isl_layout
->bw
),
38 .height
= DIV_ROUND_UP(extent
->height
, isl_layout
->bh
),
39 .depth
= DIV_ROUND_UP(extent
->depth
, isl_layout
->bd
),
43 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
44 * elements rather than texels. One element equals one texel or one block
45 * if Image is uncompressed or compressed, respectively.
47 static struct VkOffset3D
48 meta_region_offset_el(const struct anv_image
*image
,
49 const struct VkOffset3D
*offset
)
51 const struct isl_format_layout
*isl_layout
= image
->format
->isl_layout
;
52 return anv_sanitize_image_offset(image
->type
, (VkOffset3D
) {
53 .x
= offset
->x
/ isl_layout
->bw
,
54 .y
= offset
->y
/ isl_layout
->bh
,
55 .z
= offset
->z
/ isl_layout
->bd
,
59 static struct anv_meta_blit2d_surf
60 blit_surf_for_image(const struct anv_image
* image
,
61 const struct isl_surf
*img_isl_surf
)
63 return (struct anv_meta_blit2d_surf
) {
65 .tiling
= img_isl_surf
->tiling
,
66 .base_offset
= image
->offset
,
67 .bs
= isl_format_get_layout(img_isl_surf
->format
)->bs
,
68 .pitch
= isl_surf_get_row_pitch(img_isl_surf
),
73 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
74 struct anv_bo
*src
, uint64_t src_offset
,
75 struct anv_bo
*dest
, uint64_t dest_offset
,
76 int width
, int height
, int bs
)
78 struct anv_meta_blit2d_surf b_src
= {
80 .tiling
= ISL_TILING_LINEAR
,
81 .base_offset
= src_offset
,
85 struct anv_meta_blit2d_surf b_dst
= {
87 .tiling
= ISL_TILING_LINEAR
,
88 .base_offset
= dest_offset
,
92 struct anv_meta_blit2d_rect rect
= {
96 anv_meta_blit2d(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
100 meta_copy_buffer_to_image(struct anv_cmd_buffer
*cmd_buffer
,
101 struct anv_buffer
* buffer
,
102 struct anv_image
* image
,
103 uint32_t regionCount
,
104 const VkBufferImageCopy
* pRegions
,
107 struct anv_meta_saved_state saved_state
;
109 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
110 * VK_SAMPLE_COUNT_1_BIT."
112 assert(image
->samples
== 1);
114 anv_meta_begin_blit2d(cmd_buffer
, &saved_state
);
116 for (unsigned r
= 0; r
< regionCount
; r
++) {
119 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
120 * extent is the size in texels of the source image to copy in width,
121 * height and depth. 1D images use only x and width. 2D images use x, y,
122 * width and height. 3D images use x, y, z, width, height and depth.
125 * Also, convert the offsets and extent from units of texels to units of
126 * blocks - which is the highest resolution accessible in this command.
128 const VkOffset3D img_offset_el
=
129 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
130 const VkExtent3D bufferExtent
= {
131 .width
= pRegions
[r
].bufferRowLength
?
132 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
133 .height
= pRegions
[r
].bufferImageHeight
?
134 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
136 const VkExtent3D buf_extent_el
=
137 meta_region_extent_el(image
, &bufferExtent
);
139 /* Start creating blit rect */
140 const VkExtent3D img_extent_el
=
141 meta_region_extent_el(image
, &pRegions
[r
].imageExtent
);
142 struct anv_meta_blit2d_rect rect
= {
143 .width
= img_extent_el
.width
,
144 .height
= img_extent_el
.height
,
147 /* Create blit surfaces */
148 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
149 const struct isl_surf
*img_isl_surf
=
150 &anv_image_get_surface_for_aspect_mask(image
, aspect
)->isl
;
151 struct anv_meta_blit2d_surf img_bsurf
=
152 blit_surf_for_image(image
, img_isl_surf
);
153 struct anv_meta_blit2d_surf buf_bsurf
= {
155 .tiling
= ISL_TILING_LINEAR
,
156 .base_offset
= buffer
->offset
+ pRegions
[r
].bufferOffset
,
157 .bs
= forward
? image
->format
->isl_layout
->bs
: img_bsurf
.bs
,
158 .pitch
= buf_extent_el
.width
* buf_bsurf
.bs
,
161 /* Set direction-dependent variables */
162 struct anv_meta_blit2d_surf
*dst_bsurf
= forward
? &img_bsurf
: &buf_bsurf
;
163 struct anv_meta_blit2d_surf
*src_bsurf
= forward
? &buf_bsurf
: &img_bsurf
;
164 uint32_t *x_offset
= forward
? &rect
.dst_x
: &rect
.src_x
;
165 uint32_t *y_offset
= forward
? &rect
.dst_y
: &rect
.src_y
;
167 /* Loop through each 3D or array slice */
168 unsigned num_slices_3d
= img_extent_el
.depth
;
169 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
170 unsigned slice_3d
= 0;
171 unsigned slice_array
= 0;
172 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
174 /* Finish creating blit rect */
175 isl_surf_get_image_offset_el(img_isl_surf
,
176 pRegions
[r
].imageSubresource
.mipLevel
,
177 pRegions
[r
].imageSubresource
.baseArrayLayer
179 img_offset_el
.z
+ slice_3d
,
182 *x_offset
+= img_offset_el
.x
;
183 *y_offset
+= img_offset_el
.y
;
186 anv_meta_blit2d(cmd_buffer
, src_bsurf
, dst_bsurf
, 1, &rect
);
188 /* Once we've done the blit, all of the actual information about
189 * the image is embedded in the command buffer so we can just
190 * increment the offset directly in the image effectively
191 * re-binding it to different backing memory.
193 buf_bsurf
.base_offset
+= buf_extent_el
.width
*
194 buf_extent_el
.height
* buf_bsurf
.bs
;
196 if (image
->type
== VK_IMAGE_TYPE_3D
)
202 anv_meta_end_blit2d(cmd_buffer
, &saved_state
);
205 void anv_CmdCopyBufferToImage(
206 VkCommandBuffer commandBuffer
,
209 VkImageLayout destImageLayout
,
210 uint32_t regionCount
,
211 const VkBufferImageCopy
* pRegions
)
213 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
214 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
215 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
217 meta_copy_buffer_to_image(cmd_buffer
, src_buffer
, dest_image
,
218 regionCount
, pRegions
, true);
221 void anv_CmdCopyImageToBuffer(
222 VkCommandBuffer commandBuffer
,
224 VkImageLayout srcImageLayout
,
226 uint32_t regionCount
,
227 const VkBufferImageCopy
* pRegions
)
229 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
230 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
231 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, destBuffer
);
233 meta_copy_buffer_to_image(cmd_buffer
, dst_buffer
, src_image
,
234 regionCount
, pRegions
, false);
237 void anv_CmdCopyImage(
238 VkCommandBuffer commandBuffer
,
240 VkImageLayout srcImageLayout
,
242 VkImageLayout destImageLayout
,
243 uint32_t regionCount
,
244 const VkImageCopy
* pRegions
)
246 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
247 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
248 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
249 struct anv_meta_saved_state saved_state
;
251 /* From the Vulkan 1.0 spec:
253 * vkCmdCopyImage can be used to copy image data between multisample
254 * images, but both images must have the same number of samples.
256 assert(src_image
->samples
== dest_image
->samples
);
258 anv_meta_begin_blit2d(cmd_buffer
, &saved_state
);
260 for (unsigned r
= 0; r
< regionCount
; r
++) {
261 assert(pRegions
[r
].srcSubresource
.aspectMask
==
262 pRegions
[r
].dstSubresource
.aspectMask
);
264 VkImageAspectFlags aspect
= pRegions
[r
].srcSubresource
.aspectMask
;
266 /* Create blit surfaces */
267 struct isl_surf
*src_isl_surf
=
268 &anv_image_get_surface_for_aspect_mask(src_image
, aspect
)->isl
;
269 struct isl_surf
*dst_isl_surf
=
270 &anv_image_get_surface_for_aspect_mask(dest_image
, aspect
)->isl
;
271 struct anv_meta_blit2d_surf b_src
=
272 blit_surf_for_image(src_image
, src_isl_surf
);
273 struct anv_meta_blit2d_surf b_dst
=
274 blit_surf_for_image(dest_image
, dst_isl_surf
);
277 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
278 * imageExtent is the size in texels of the image to copy in width, height
279 * and depth. 1D images use only x and width. 2D images use x, y, width
280 * and height. 3D images use x, y, z, width, height and depth.
282 * Also, convert the offsets and extent from units of texels to units of
283 * blocks - which is the highest resolution accessible in this command.
285 const VkOffset3D dst_offset_el
=
286 meta_region_offset_el(dest_image
, &pRegions
[r
].dstOffset
);
287 const VkOffset3D src_offset_el
=
288 meta_region_offset_el(src_image
, &pRegions
[r
].srcOffset
);
289 const VkExtent3D img_extent_el
=
290 meta_region_extent_el(src_image
, &pRegions
[r
].extent
);
292 /* Start creating blit rect */
293 struct anv_meta_blit2d_rect rect
= {
294 .width
= img_extent_el
.width
,
295 .height
= img_extent_el
.height
,
298 /* Loop through each 3D or array slice */
299 unsigned num_slices_3d
= img_extent_el
.depth
;
300 unsigned num_slices_array
= pRegions
[r
].dstSubresource
.layerCount
;
301 unsigned slice_3d
= 0;
302 unsigned slice_array
= 0;
303 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
305 /* Finish creating blit rect */
306 isl_surf_get_image_offset_el(dst_isl_surf
,
307 pRegions
[r
].dstSubresource
.mipLevel
,
308 pRegions
[r
].dstSubresource
.baseArrayLayer
310 dst_offset_el
.z
+ slice_3d
,
313 isl_surf_get_image_offset_el(src_isl_surf
,
314 pRegions
[r
].srcSubresource
.mipLevel
,
315 pRegions
[r
].srcSubresource
.baseArrayLayer
317 src_offset_el
.z
+ slice_3d
,
320 rect
.dst_x
+= dst_offset_el
.x
;
321 rect
.dst_y
+= dst_offset_el
.y
;
322 rect
.src_x
+= src_offset_el
.x
;
323 rect
.src_y
+= src_offset_el
.y
;
326 anv_meta_blit2d(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
328 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
335 anv_meta_end_blit2d(cmd_buffer
, &saved_state
);
338 void anv_CmdCopyBuffer(
339 VkCommandBuffer commandBuffer
,
342 uint32_t regionCount
,
343 const VkBufferCopy
* pRegions
)
345 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
346 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
347 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
349 struct anv_meta_saved_state saved_state
;
351 anv_meta_begin_blit2d(cmd_buffer
, &saved_state
);
353 for (unsigned r
= 0; r
< regionCount
; r
++) {
354 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
355 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].dstOffset
;
356 uint64_t copy_size
= pRegions
[r
].size
;
358 /* First, we compute the biggest format that can be used with the
359 * given offsets and size.
363 int fs
= ffs(src_offset
) - 1;
365 bs
= MIN2(bs
, 1 << fs
);
366 assert(src_offset
% bs
== 0);
368 fs
= ffs(dest_offset
) - 1;
370 bs
= MIN2(bs
, 1 << fs
);
371 assert(dest_offset
% bs
== 0);
373 fs
= ffs(pRegions
[r
].size
) - 1;
375 bs
= MIN2(bs
, 1 << fs
);
376 assert(pRegions
[r
].size
% bs
== 0);
378 /* This is maximum possible width/height our HW can handle */
379 uint64_t max_surface_dim
= 1 << 14;
381 /* First, we make a bunch of max-sized copies */
382 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* bs
;
383 while (copy_size
>= max_copy_size
) {
384 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
385 dest_buffer
->bo
, dest_offset
,
386 max_surface_dim
, max_surface_dim
, bs
);
387 copy_size
-= max_copy_size
;
388 src_offset
+= max_copy_size
;
389 dest_offset
+= max_copy_size
;
392 uint64_t height
= copy_size
/ (max_surface_dim
* bs
);
393 assert(height
< max_surface_dim
);
395 uint64_t rect_copy_size
= height
* max_surface_dim
* bs
;
396 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
397 dest_buffer
->bo
, dest_offset
,
398 max_surface_dim
, height
, bs
);
399 copy_size
-= rect_copy_size
;
400 src_offset
+= rect_copy_size
;
401 dest_offset
+= rect_copy_size
;
404 if (copy_size
!= 0) {
405 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
406 dest_buffer
->bo
, dest_offset
,
407 copy_size
/ bs
, 1, bs
);
411 anv_meta_end_blit2d(cmd_buffer
, &saved_state
);
414 void anv_CmdUpdateBuffer(
415 VkCommandBuffer commandBuffer
,
417 VkDeviceSize dstOffset
,
418 VkDeviceSize dataSize
,
419 const uint32_t* pData
)
421 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
422 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
423 struct anv_meta_saved_state saved_state
;
425 anv_meta_begin_blit2d(cmd_buffer
, &saved_state
);
427 /* We can't quite grab a full block because the state stream needs a
428 * little data at the top to build its linked list.
430 const uint32_t max_update_size
=
431 cmd_buffer
->device
->dynamic_state_block_pool
.block_size
- 64;
433 assert(max_update_size
< (1 << 14) * 4);
436 const uint32_t copy_size
= MIN2(dataSize
, max_update_size
);
438 struct anv_state tmp_data
=
439 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, copy_size
, 64);
441 memcpy(tmp_data
.map
, pData
, copy_size
);
444 if ((copy_size
& 15) == 0 && (dstOffset
& 15) == 0) {
446 } else if ((copy_size
& 7) == 0 && (dstOffset
& 7) == 0) {
449 assert((copy_size
& 3) == 0 && (dstOffset
& 3) == 0);
453 do_buffer_copy(cmd_buffer
,
454 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
,
456 dst_buffer
->bo
, dst_buffer
->offset
+ dstOffset
,
457 copy_size
/ bs
, 1, bs
);
459 dataSize
-= copy_size
;
460 dstOffset
+= copy_size
;
461 pData
= (void *)pData
+ copy_size
;
464 anv_meta_end_blit2d(cmd_buffer
, &saved_state
);