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
27 meta_image_block_size(const struct anv_image
*image
)
29 if (image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
30 const struct isl_format_layout
*isl_layout
=
31 isl_format_get_layout(image
->color_surface
.isl
.format
);
32 return (VkExtent3D
) { isl_layout
->bw
, isl_layout
->bh
, isl_layout
->bd
};
34 return (VkExtent3D
) { 1, 1, 1 };
38 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
39 * elements rather than texels. One element equals one texel or one block
40 * if Image is uncompressed or compressed, respectively.
42 static struct VkExtent3D
43 meta_region_extent_el(const struct anv_image
*image
,
44 const struct VkExtent3D
*extent
)
46 const VkExtent3D block
= meta_image_block_size(image
);
47 return anv_sanitize_image_extent(image
->type
, (VkExtent3D
) {
48 .width
= DIV_ROUND_UP(extent
->width
, block
.width
),
49 .height
= DIV_ROUND_UP(extent
->height
, block
.height
),
50 .depth
= DIV_ROUND_UP(extent
->depth
, block
.depth
),
54 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
55 * elements rather than texels. One element equals one texel or one block
56 * if Image is uncompressed or compressed, respectively.
58 static struct VkOffset3D
59 meta_region_offset_el(const struct anv_image
*image
,
60 const struct VkOffset3D
*offset
)
62 const VkExtent3D block
= meta_image_block_size(image
);
63 return anv_sanitize_image_offset(image
->type
, (VkOffset3D
) {
64 .x
= offset
->x
/ block
.width
,
65 .y
= offset
->y
/ block
.height
,
66 .z
= offset
->z
/ block
.depth
,
70 static struct anv_meta_blit2d_surf
71 blit_surf_for_image(const struct anv_image
* image
,
72 const struct isl_surf
*img_isl_surf
)
74 return (struct anv_meta_blit2d_surf
) {
76 .tiling
= img_isl_surf
->tiling
,
77 .base_offset
= image
->offset
,
78 .bs
= isl_format_get_layout(img_isl_surf
->format
)->bs
,
79 .pitch
= isl_surf_get_row_pitch(img_isl_surf
),
84 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
85 struct anv_bo
*src
, uint64_t src_offset
,
86 struct anv_bo
*dest
, uint64_t dest_offset
,
87 int width
, int height
, int bs
)
89 struct anv_meta_blit2d_surf b_src
= {
91 .tiling
= ISL_TILING_LINEAR
,
92 .base_offset
= src_offset
,
96 struct anv_meta_blit2d_surf b_dst
= {
98 .tiling
= ISL_TILING_LINEAR
,
99 .base_offset
= dest_offset
,
103 struct anv_meta_blit2d_rect rect
= {
107 anv_meta_blit2d(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
111 meta_copy_buffer_to_image(struct anv_cmd_buffer
*cmd_buffer
,
112 struct anv_buffer
* buffer
,
113 struct anv_image
* image
,
114 uint32_t regionCount
,
115 const VkBufferImageCopy
* pRegions
,
118 struct anv_meta_saved_state saved_state
;
120 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
121 * VK_SAMPLE_COUNT_1_BIT."
123 assert(image
->samples
== 1);
125 anv_meta_begin_blit2d(cmd_buffer
, &saved_state
);
127 for (unsigned r
= 0; r
< regionCount
; r
++) {
130 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
131 * extent is the size in texels of the source image to copy in width,
132 * height and depth. 1D images use only x and width. 2D images use x, y,
133 * width and height. 3D images use x, y, z, width, height and depth.
136 * Also, convert the offsets and extent from units of texels to units of
137 * blocks - which is the highest resolution accessible in this command.
139 const VkOffset3D img_offset_el
=
140 meta_region_offset_el(image
, &pRegions
[r
].imageOffset
);
141 const VkExtent3D bufferExtent
= {
142 .width
= pRegions
[r
].bufferRowLength
?
143 pRegions
[r
].bufferRowLength
: pRegions
[r
].imageExtent
.width
,
144 .height
= pRegions
[r
].bufferImageHeight
?
145 pRegions
[r
].bufferImageHeight
: pRegions
[r
].imageExtent
.height
,
147 const VkExtent3D buf_extent_el
=
148 meta_region_extent_el(image
, &bufferExtent
);
150 /* Start creating blit rect */
151 const VkExtent3D img_extent_el
=
152 meta_region_extent_el(image
, &pRegions
[r
].imageExtent
);
153 struct anv_meta_blit2d_rect rect
= {
154 .width
= img_extent_el
.width
,
155 .height
= img_extent_el
.height
,
158 /* Create blit surfaces */
159 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
160 const struct isl_surf
*img_isl_surf
=
161 &anv_image_get_surface_for_aspect_mask(image
, aspect
)->isl
;
162 struct anv_meta_blit2d_surf img_bsurf
=
163 blit_surf_for_image(image
, img_isl_surf
);
164 enum isl_format buf_format
= anv_get_isl_format(image
->vk_format
, aspect
,
165 VK_IMAGE_TILING_LINEAR
);
166 struct anv_meta_blit2d_surf buf_bsurf
= {
168 .tiling
= ISL_TILING_LINEAR
,
169 .base_offset
= buffer
->offset
+ pRegions
[r
].bufferOffset
,
170 .bs
= isl_format_get_layout(buf_format
)->bs
,
171 .pitch
= buf_extent_el
.width
* buf_bsurf
.bs
,
174 /* Set direction-dependent variables */
175 struct anv_meta_blit2d_surf
*dst_bsurf
= forward
? &img_bsurf
: &buf_bsurf
;
176 struct anv_meta_blit2d_surf
*src_bsurf
= forward
? &buf_bsurf
: &img_bsurf
;
177 uint32_t *x_offset
= forward
? &rect
.dst_x
: &rect
.src_x
;
178 uint32_t *y_offset
= forward
? &rect
.dst_y
: &rect
.src_y
;
180 /* Loop through each 3D or array slice */
181 unsigned num_slices_3d
= img_extent_el
.depth
;
182 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
183 unsigned slice_3d
= 0;
184 unsigned slice_array
= 0;
185 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
187 /* Finish creating blit rect */
188 isl_surf_get_image_offset_el(img_isl_surf
,
189 pRegions
[r
].imageSubresource
.mipLevel
,
190 pRegions
[r
].imageSubresource
.baseArrayLayer
192 img_offset_el
.z
+ slice_3d
,
195 *x_offset
+= img_offset_el
.x
;
196 *y_offset
+= img_offset_el
.y
;
199 anv_meta_blit2d(cmd_buffer
, src_bsurf
, dst_bsurf
, 1, &rect
);
201 /* Once we've done the blit, all of the actual information about
202 * the image is embedded in the command buffer so we can just
203 * increment the offset directly in the image effectively
204 * re-binding it to different backing memory.
206 buf_bsurf
.base_offset
+= buf_extent_el
.width
*
207 buf_extent_el
.height
* buf_bsurf
.bs
;
209 if (image
->type
== VK_IMAGE_TYPE_3D
)
215 anv_meta_end_blit2d(cmd_buffer
, &saved_state
);
218 void anv_CmdCopyBufferToImage(
219 VkCommandBuffer commandBuffer
,
222 VkImageLayout destImageLayout
,
223 uint32_t regionCount
,
224 const VkBufferImageCopy
* pRegions
)
226 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
227 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
228 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
230 meta_copy_buffer_to_image(cmd_buffer
, src_buffer
, dest_image
,
231 regionCount
, pRegions
, true);
234 void anv_CmdCopyImageToBuffer(
235 VkCommandBuffer commandBuffer
,
237 VkImageLayout srcImageLayout
,
239 uint32_t regionCount
,
240 const VkBufferImageCopy
* pRegions
)
242 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
243 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
244 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, destBuffer
);
246 meta_copy_buffer_to_image(cmd_buffer
, dst_buffer
, src_image
,
247 regionCount
, pRegions
, false);
250 void anv_CmdCopyImage(
251 VkCommandBuffer commandBuffer
,
253 VkImageLayout srcImageLayout
,
255 VkImageLayout destImageLayout
,
256 uint32_t regionCount
,
257 const VkImageCopy
* pRegions
)
259 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
260 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
261 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
262 struct anv_meta_saved_state saved_state
;
264 /* From the Vulkan 1.0 spec:
266 * vkCmdCopyImage can be used to copy image data between multisample
267 * images, but both images must have the same number of samples.
269 assert(src_image
->samples
== dest_image
->samples
);
271 anv_meta_begin_blit2d(cmd_buffer
, &saved_state
);
273 for (unsigned r
= 0; r
< regionCount
; r
++) {
274 assert(pRegions
[r
].srcSubresource
.aspectMask
==
275 pRegions
[r
].dstSubresource
.aspectMask
);
277 VkImageAspectFlags aspect
= pRegions
[r
].srcSubresource
.aspectMask
;
279 /* Create blit surfaces */
280 struct isl_surf
*src_isl_surf
=
281 &anv_image_get_surface_for_aspect_mask(src_image
, aspect
)->isl
;
282 struct isl_surf
*dst_isl_surf
=
283 &anv_image_get_surface_for_aspect_mask(dest_image
, aspect
)->isl
;
284 struct anv_meta_blit2d_surf b_src
=
285 blit_surf_for_image(src_image
, src_isl_surf
);
286 struct anv_meta_blit2d_surf b_dst
=
287 blit_surf_for_image(dest_image
, dst_isl_surf
);
290 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
291 * imageExtent is the size in texels of the image to copy in width, height
292 * and depth. 1D images use only x and width. 2D images use x, y, width
293 * and height. 3D images use x, y, z, width, height and depth.
295 * Also, convert the offsets and extent from units of texels to units of
296 * blocks - which is the highest resolution accessible in this command.
298 const VkOffset3D dst_offset_el
=
299 meta_region_offset_el(dest_image
, &pRegions
[r
].dstOffset
);
300 const VkOffset3D src_offset_el
=
301 meta_region_offset_el(src_image
, &pRegions
[r
].srcOffset
);
302 const VkExtent3D img_extent_el
=
303 meta_region_extent_el(src_image
, &pRegions
[r
].extent
);
305 /* Start creating blit rect */
306 struct anv_meta_blit2d_rect rect
= {
307 .width
= img_extent_el
.width
,
308 .height
= img_extent_el
.height
,
311 /* Loop through each 3D or array slice */
312 unsigned num_slices_3d
= img_extent_el
.depth
;
313 unsigned num_slices_array
= pRegions
[r
].dstSubresource
.layerCount
;
314 unsigned slice_3d
= 0;
315 unsigned slice_array
= 0;
316 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
318 /* Finish creating blit rect */
319 isl_surf_get_image_offset_el(dst_isl_surf
,
320 pRegions
[r
].dstSubresource
.mipLevel
,
321 pRegions
[r
].dstSubresource
.baseArrayLayer
323 dst_offset_el
.z
+ slice_3d
,
326 isl_surf_get_image_offset_el(src_isl_surf
,
327 pRegions
[r
].srcSubresource
.mipLevel
,
328 pRegions
[r
].srcSubresource
.baseArrayLayer
330 src_offset_el
.z
+ slice_3d
,
333 rect
.dst_x
+= dst_offset_el
.x
;
334 rect
.dst_y
+= dst_offset_el
.y
;
335 rect
.src_x
+= src_offset_el
.x
;
336 rect
.src_y
+= src_offset_el
.y
;
339 anv_meta_blit2d(cmd_buffer
, &b_src
, &b_dst
, 1, &rect
);
341 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
348 anv_meta_end_blit2d(cmd_buffer
, &saved_state
);
351 void anv_CmdCopyBuffer(
352 VkCommandBuffer commandBuffer
,
355 uint32_t regionCount
,
356 const VkBufferCopy
* pRegions
)
358 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
359 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
360 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
362 struct anv_meta_saved_state saved_state
;
364 anv_meta_begin_blit2d(cmd_buffer
, &saved_state
);
366 for (unsigned r
= 0; r
< regionCount
; r
++) {
367 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
368 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].dstOffset
;
369 uint64_t copy_size
= pRegions
[r
].size
;
371 /* First, we compute the biggest format that can be used with the
372 * given offsets and size.
376 int fs
= ffs(src_offset
) - 1;
378 bs
= MIN2(bs
, 1 << fs
);
379 assert(src_offset
% bs
== 0);
381 fs
= ffs(dest_offset
) - 1;
383 bs
= MIN2(bs
, 1 << fs
);
384 assert(dest_offset
% bs
== 0);
386 fs
= ffs(pRegions
[r
].size
) - 1;
388 bs
= MIN2(bs
, 1 << fs
);
389 assert(pRegions
[r
].size
% bs
== 0);
391 /* This is maximum possible width/height our HW can handle */
392 uint64_t max_surface_dim
= 1 << 14;
394 /* First, we make a bunch of max-sized copies */
395 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* bs
;
396 while (copy_size
>= max_copy_size
) {
397 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
398 dest_buffer
->bo
, dest_offset
,
399 max_surface_dim
, max_surface_dim
, bs
);
400 copy_size
-= max_copy_size
;
401 src_offset
+= max_copy_size
;
402 dest_offset
+= max_copy_size
;
405 uint64_t height
= copy_size
/ (max_surface_dim
* bs
);
406 assert(height
< max_surface_dim
);
408 uint64_t rect_copy_size
= height
* max_surface_dim
* bs
;
409 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
410 dest_buffer
->bo
, dest_offset
,
411 max_surface_dim
, height
, bs
);
412 copy_size
-= rect_copy_size
;
413 src_offset
+= rect_copy_size
;
414 dest_offset
+= rect_copy_size
;
417 if (copy_size
!= 0) {
418 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
419 dest_buffer
->bo
, dest_offset
,
420 copy_size
/ bs
, 1, bs
);
424 anv_meta_end_blit2d(cmd_buffer
, &saved_state
);
427 void anv_CmdUpdateBuffer(
428 VkCommandBuffer commandBuffer
,
430 VkDeviceSize dstOffset
,
431 VkDeviceSize dataSize
,
432 const uint32_t* pData
)
434 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
435 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
436 struct anv_meta_saved_state saved_state
;
438 anv_meta_begin_blit2d(cmd_buffer
, &saved_state
);
440 /* We can't quite grab a full block because the state stream needs a
441 * little data at the top to build its linked list.
443 const uint32_t max_update_size
=
444 cmd_buffer
->device
->dynamic_state_block_pool
.block_size
- 64;
446 assert(max_update_size
< (1 << 14) * 4);
449 const uint32_t copy_size
= MIN2(dataSize
, max_update_size
);
451 struct anv_state tmp_data
=
452 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, copy_size
, 64);
454 memcpy(tmp_data
.map
, pData
, copy_size
);
457 if ((copy_size
& 15) == 0 && (dstOffset
& 15) == 0) {
459 } else if ((copy_size
& 7) == 0 && (dstOffset
& 7) == 0) {
462 assert((copy_size
& 3) == 0 && (dstOffset
& 3) == 0);
466 do_buffer_copy(cmd_buffer
,
467 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
,
469 dst_buffer
->bo
, dst_buffer
->offset
+ dstOffset
,
470 copy_size
/ bs
, 1, bs
);
472 dataSize
-= copy_size
;
473 dstOffset
+= copy_size
;
474 pData
= (void *)pData
+ copy_size
;
477 anv_meta_end_blit2d(cmd_buffer
, &saved_state
);