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 "anv_private.h"
27 lookup_blorp_shader(struct blorp_context
*blorp
,
28 const void *key
, uint32_t key_size
,
29 uint32_t *kernel_out
, void *prog_data_out
)
31 struct anv_device
*device
= blorp
->driver_ctx
;
33 /* The blorp cache must be a real cache */
34 assert(device
->blorp_shader_cache
.cache
);
36 struct anv_shader_bin
*bin
=
37 anv_pipeline_cache_search(&device
->blorp_shader_cache
, key
, key_size
);
41 /* The cache already has a reference and it's not going anywhere so there
42 * is no need to hold a second reference.
44 anv_shader_bin_unref(device
, bin
);
46 *kernel_out
= bin
->kernel
.offset
;
47 *(const struct brw_stage_prog_data
**)prog_data_out
= bin
->prog_data
;
53 upload_blorp_shader(struct blorp_context
*blorp
,
54 const void *key
, uint32_t key_size
,
55 const void *kernel
, uint32_t kernel_size
,
56 const struct brw_stage_prog_data
*prog_data
,
57 uint32_t prog_data_size
,
58 uint32_t *kernel_out
, void *prog_data_out
)
60 struct anv_device
*device
= blorp
->driver_ctx
;
62 /* The blorp cache must be a real cache */
63 assert(device
->blorp_shader_cache
.cache
);
65 struct anv_pipeline_bind_map bind_map
= {
70 struct anv_shader_bin
*bin
=
71 anv_pipeline_cache_upload_kernel(&device
->blorp_shader_cache
,
72 key
, key_size
, kernel
, kernel_size
,
73 prog_data
, prog_data_size
, &bind_map
);
78 /* The cache already has a reference and it's not going anywhere so there
79 * is no need to hold a second reference.
81 anv_shader_bin_unref(device
, bin
);
83 *kernel_out
= bin
->kernel
.offset
;
84 *(const struct brw_stage_prog_data
**)prog_data_out
= bin
->prog_data
;
90 anv_device_init_blorp(struct anv_device
*device
)
92 anv_pipeline_cache_init(&device
->blorp_shader_cache
, device
, true);
93 blorp_init(&device
->blorp
, device
, &device
->isl_dev
);
94 device
->blorp
.compiler
= device
->instance
->physicalDevice
.compiler
;
95 device
->blorp
.mocs
.tex
= device
->default_mocs
;
96 device
->blorp
.mocs
.rb
= device
->default_mocs
;
97 device
->blorp
.mocs
.vb
= device
->default_mocs
;
98 device
->blorp
.lookup_shader
= lookup_blorp_shader
;
99 device
->blorp
.upload_shader
= upload_blorp_shader
;
100 switch (device
->info
.gen
) {
102 if (device
->info
.is_haswell
) {
103 device
->blorp
.exec
= gen75_blorp_exec
;
105 device
->blorp
.exec
= gen7_blorp_exec
;
109 device
->blorp
.exec
= gen8_blorp_exec
;
112 device
->blorp
.exec
= gen9_blorp_exec
;
115 device
->blorp
.exec
= gen10_blorp_exec
;
118 unreachable("Unknown hardware generation");
123 anv_device_finish_blorp(struct anv_device
*device
)
125 blorp_finish(&device
->blorp
);
126 anv_pipeline_cache_finish(&device
->blorp_shader_cache
);
130 get_blorp_surf_for_anv_buffer(struct anv_device
*device
,
131 struct anv_buffer
*buffer
, uint64_t offset
,
132 uint32_t width
, uint32_t height
,
133 uint32_t row_pitch
, enum isl_format format
,
134 struct blorp_surf
*blorp_surf
,
135 struct isl_surf
*isl_surf
)
137 const struct isl_format_layout
*fmtl
=
138 isl_format_get_layout(format
);
141 /* ASTC is the only format which doesn't support linear layouts.
142 * Create an equivalently sized surface with ISL to get around this.
144 if (fmtl
->txc
== ISL_TXC_ASTC
) {
145 /* Use an equivalently sized format */
146 format
= ISL_FORMAT_R32G32B32A32_UINT
;
147 assert(fmtl
->bpb
== isl_format_get_layout(format
)->bpb
);
149 /* Shrink the dimensions for the new format */
150 width
= DIV_ROUND_UP(width
, fmtl
->bw
);
151 height
= DIV_ROUND_UP(height
, fmtl
->bh
);
154 *blorp_surf
= (struct blorp_surf
) {
157 .buffer
= buffer
->bo
,
158 .offset
= buffer
->offset
+ offset
,
162 ok
= isl_surf_init(&device
->isl_dev
, isl_surf
,
163 .dim
= ISL_SURF_DIM_2D
,
171 .row_pitch
= row_pitch
,
172 .usage
= ISL_SURF_USAGE_TEXTURE_BIT
|
173 ISL_SURF_USAGE_RENDER_TARGET_BIT
,
174 .tiling_flags
= ISL_TILING_LINEAR_BIT
);
178 #define ANV_AUX_USAGE_DEFAULT ((enum isl_aux_usage)0xff)
181 get_blorp_surf_for_anv_image(const struct anv_device
*device
,
182 const struct anv_image
*image
,
183 VkImageAspectFlags aspect
,
184 enum isl_aux_usage aux_usage
,
185 struct blorp_surf
*blorp_surf
)
187 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
189 if (aux_usage
== ANV_AUX_USAGE_DEFAULT
)
190 aux_usage
= image
->planes
[plane
].aux_usage
;
192 if (aspect
== VK_IMAGE_ASPECT_STENCIL_BIT
||
193 aux_usage
== ISL_AUX_USAGE_HIZ
)
194 aux_usage
= ISL_AUX_USAGE_NONE
;
196 const struct anv_surface
*surface
= &image
->planes
[plane
].surface
;
197 *blorp_surf
= (struct blorp_surf
) {
198 .surf
= &surface
->isl
,
200 .buffer
= image
->planes
[plane
].bo
,
201 .offset
= image
->planes
[plane
].bo_offset
+ surface
->offset
,
205 if (aux_usage
!= ISL_AUX_USAGE_NONE
) {
206 const struct anv_surface
*aux_surface
= &image
->planes
[plane
].aux_surface
;
207 blorp_surf
->aux_surf
= &aux_surface
->isl
,
208 blorp_surf
->aux_addr
= (struct blorp_address
) {
209 .buffer
= image
->planes
[plane
].bo
,
210 .offset
= image
->planes
[plane
].bo_offset
+ aux_surface
->offset
,
212 blorp_surf
->aux_usage
= aux_usage
;
216 void anv_CmdCopyImage(
217 VkCommandBuffer commandBuffer
,
219 VkImageLayout srcImageLayout
,
221 VkImageLayout dstImageLayout
,
222 uint32_t regionCount
,
223 const VkImageCopy
* pRegions
)
225 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
226 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
227 ANV_FROM_HANDLE(anv_image
, dst_image
, dstImage
);
229 struct blorp_batch batch
;
230 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
232 for (unsigned r
= 0; r
< regionCount
; r
++) {
233 VkOffset3D srcOffset
=
234 anv_sanitize_image_offset(src_image
->type
, pRegions
[r
].srcOffset
);
235 VkOffset3D dstOffset
=
236 anv_sanitize_image_offset(dst_image
->type
, pRegions
[r
].dstOffset
);
238 anv_sanitize_image_extent(src_image
->type
, pRegions
[r
].extent
);
240 unsigned dst_base_layer
, layer_count
;
241 if (dst_image
->type
== VK_IMAGE_TYPE_3D
) {
242 dst_base_layer
= pRegions
[r
].dstOffset
.z
;
243 layer_count
= pRegions
[r
].extent
.depth
;
245 dst_base_layer
= pRegions
[r
].dstSubresource
.baseArrayLayer
;
247 anv_get_layerCount(dst_image
, &pRegions
[r
].dstSubresource
);
250 unsigned src_base_layer
;
251 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
252 src_base_layer
= pRegions
[r
].srcOffset
.z
;
254 src_base_layer
= pRegions
[r
].srcSubresource
.baseArrayLayer
;
255 assert(layer_count
==
256 anv_get_layerCount(src_image
, &pRegions
[r
].srcSubresource
));
259 VkImageAspectFlags src_mask
= pRegions
[r
].srcSubresource
.aspectMask
,
260 dst_mask
= pRegions
[r
].dstSubresource
.aspectMask
;
262 assert(anv_image_aspects_compatible(src_mask
, dst_mask
));
264 if (_mesa_bitcount(src_mask
) > 1) {
266 anv_foreach_image_aspect_bit(aspect_bit
, src_image
, src_mask
) {
267 struct blorp_surf src_surf
, dst_surf
;
268 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
269 src_image
, 1UL << aspect_bit
,
270 ANV_AUX_USAGE_DEFAULT
, &src_surf
);
271 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
272 dst_image
, 1UL << aspect_bit
,
273 ANV_AUX_USAGE_DEFAULT
, &dst_surf
);
275 for (unsigned i
= 0; i
< layer_count
; i
++) {
276 blorp_copy(&batch
, &src_surf
, pRegions
[r
].srcSubresource
.mipLevel
,
278 &dst_surf
, pRegions
[r
].dstSubresource
.mipLevel
,
280 srcOffset
.x
, srcOffset
.y
,
281 dstOffset
.x
, dstOffset
.y
,
282 extent
.width
, extent
.height
);
286 struct blorp_surf src_surf
, dst_surf
;
287 get_blorp_surf_for_anv_image(cmd_buffer
->device
, src_image
, src_mask
,
288 ANV_AUX_USAGE_DEFAULT
, &src_surf
);
289 get_blorp_surf_for_anv_image(cmd_buffer
->device
, dst_image
, dst_mask
,
290 ANV_AUX_USAGE_DEFAULT
, &dst_surf
);
292 for (unsigned i
= 0; i
< layer_count
; i
++) {
293 blorp_copy(&batch
, &src_surf
, pRegions
[r
].srcSubresource
.mipLevel
,
295 &dst_surf
, pRegions
[r
].dstSubresource
.mipLevel
,
297 srcOffset
.x
, srcOffset
.y
,
298 dstOffset
.x
, dstOffset
.y
,
299 extent
.width
, extent
.height
);
304 blorp_batch_finish(&batch
);
308 copy_buffer_to_image(struct anv_cmd_buffer
*cmd_buffer
,
309 struct anv_buffer
*anv_buffer
,
310 struct anv_image
*anv_image
,
311 uint32_t regionCount
,
312 const VkBufferImageCopy
* pRegions
,
313 bool buffer_to_image
)
315 struct blorp_batch batch
;
316 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
319 struct blorp_surf surf
;
322 } image
, buffer
, *src
, *dst
;
325 buffer
.offset
= (VkOffset3D
) { 0, 0, 0 };
327 if (buffer_to_image
) {
335 for (unsigned r
= 0; r
< regionCount
; r
++) {
336 const VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
338 get_blorp_surf_for_anv_image(cmd_buffer
->device
, anv_image
, aspect
,
339 ANV_AUX_USAGE_DEFAULT
, &image
.surf
);
341 anv_sanitize_image_offset(anv_image
->type
, pRegions
[r
].imageOffset
);
342 image
.level
= pRegions
[r
].imageSubresource
.mipLevel
;
345 anv_sanitize_image_extent(anv_image
->type
, pRegions
[r
].imageExtent
);
346 if (anv_image
->type
!= VK_IMAGE_TYPE_3D
) {
347 image
.offset
.z
= pRegions
[r
].imageSubresource
.baseArrayLayer
;
349 anv_get_layerCount(anv_image
, &pRegions
[r
].imageSubresource
);
352 const enum isl_format buffer_format
=
353 anv_get_isl_format(&cmd_buffer
->device
->info
, anv_image
->vk_format
,
354 aspect
, VK_IMAGE_TILING_LINEAR
);
356 const VkExtent3D bufferImageExtent
= {
357 .width
= pRegions
[r
].bufferRowLength
?
358 pRegions
[r
].bufferRowLength
: extent
.width
,
359 .height
= pRegions
[r
].bufferImageHeight
?
360 pRegions
[r
].bufferImageHeight
: extent
.height
,
363 const struct isl_format_layout
*buffer_fmtl
=
364 isl_format_get_layout(buffer_format
);
366 const uint32_t buffer_row_pitch
=
367 DIV_ROUND_UP(bufferImageExtent
.width
, buffer_fmtl
->bw
) *
368 (buffer_fmtl
->bpb
/ 8);
370 const uint32_t buffer_layer_stride
=
371 DIV_ROUND_UP(bufferImageExtent
.height
, buffer_fmtl
->bh
) *
374 struct isl_surf buffer_isl_surf
;
375 get_blorp_surf_for_anv_buffer(cmd_buffer
->device
,
376 anv_buffer
, pRegions
[r
].bufferOffset
,
377 extent
.width
, extent
.height
,
378 buffer_row_pitch
, buffer_format
,
379 &buffer
.surf
, &buffer_isl_surf
);
381 for (unsigned z
= 0; z
< extent
.depth
; z
++) {
382 blorp_copy(&batch
, &src
->surf
, src
->level
, src
->offset
.z
,
383 &dst
->surf
, dst
->level
, dst
->offset
.z
,
384 src
->offset
.x
, src
->offset
.y
, dst
->offset
.x
, dst
->offset
.y
,
385 extent
.width
, extent
.height
);
388 buffer
.surf
.addr
.offset
+= buffer_layer_stride
;
392 blorp_batch_finish(&batch
);
395 void anv_CmdCopyBufferToImage(
396 VkCommandBuffer commandBuffer
,
399 VkImageLayout dstImageLayout
,
400 uint32_t regionCount
,
401 const VkBufferImageCopy
* pRegions
)
403 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
404 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
405 ANV_FROM_HANDLE(anv_image
, dst_image
, dstImage
);
407 copy_buffer_to_image(cmd_buffer
, src_buffer
, dst_image
,
408 regionCount
, pRegions
, true);
411 void anv_CmdCopyImageToBuffer(
412 VkCommandBuffer commandBuffer
,
414 VkImageLayout srcImageLayout
,
416 uint32_t regionCount
,
417 const VkBufferImageCopy
* pRegions
)
419 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
420 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
421 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
423 copy_buffer_to_image(cmd_buffer
, dst_buffer
, src_image
,
424 regionCount
, pRegions
, false);
428 flip_coords(unsigned *src0
, unsigned *src1
, unsigned *dst0
, unsigned *dst1
)
432 unsigned tmp
= *src0
;
439 unsigned tmp
= *dst0
;
448 void anv_CmdBlitImage(
449 VkCommandBuffer commandBuffer
,
451 VkImageLayout srcImageLayout
,
453 VkImageLayout dstImageLayout
,
454 uint32_t regionCount
,
455 const VkImageBlit
* pRegions
,
459 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
460 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
461 ANV_FROM_HANDLE(anv_image
, dst_image
, dstImage
);
463 struct blorp_surf src
, dst
;
467 case VK_FILTER_NEAREST
:
468 gl_filter
= 0x2600; /* GL_NEAREST */
470 case VK_FILTER_LINEAR
:
471 gl_filter
= 0x2601; /* GL_LINEAR */
474 unreachable("Invalid filter");
477 struct blorp_batch batch
;
478 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
480 for (unsigned r
= 0; r
< regionCount
; r
++) {
481 const VkImageSubresourceLayers
*src_res
= &pRegions
[r
].srcSubresource
;
482 const VkImageSubresourceLayers
*dst_res
= &pRegions
[r
].dstSubresource
;
484 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
485 src_image
, src_res
->aspectMask
,
486 ANV_AUX_USAGE_DEFAULT
, &src
);
487 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
488 dst_image
, dst_res
->aspectMask
,
489 ANV_AUX_USAGE_DEFAULT
, &dst
);
491 struct anv_format_plane src_format
=
492 anv_get_format_plane(&cmd_buffer
->device
->info
, src_image
->vk_format
,
493 src_res
->aspectMask
, src_image
->tiling
);
494 struct anv_format_plane dst_format
=
495 anv_get_format_plane(&cmd_buffer
->device
->info
, dst_image
->vk_format
,
496 dst_res
->aspectMask
, dst_image
->tiling
);
498 unsigned dst_start
, dst_end
;
499 if (dst_image
->type
== VK_IMAGE_TYPE_3D
) {
500 assert(dst_res
->baseArrayLayer
== 0);
501 dst_start
= pRegions
[r
].dstOffsets
[0].z
;
502 dst_end
= pRegions
[r
].dstOffsets
[1].z
;
504 dst_start
= dst_res
->baseArrayLayer
;
505 dst_end
= dst_start
+ anv_get_layerCount(dst_image
, dst_res
);
508 unsigned src_start
, src_end
;
509 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
510 assert(src_res
->baseArrayLayer
== 0);
511 src_start
= pRegions
[r
].srcOffsets
[0].z
;
512 src_end
= pRegions
[r
].srcOffsets
[1].z
;
514 src_start
= src_res
->baseArrayLayer
;
515 src_end
= src_start
+ anv_get_layerCount(src_image
, src_res
);
518 bool flip_z
= flip_coords(&src_start
, &src_end
, &dst_start
, &dst_end
);
519 float src_z_step
= (float)(src_end
+ 1 - src_start
) /
520 (float)(dst_end
+ 1 - dst_start
);
527 unsigned src_x0
= pRegions
[r
].srcOffsets
[0].x
;
528 unsigned src_x1
= pRegions
[r
].srcOffsets
[1].x
;
529 unsigned dst_x0
= pRegions
[r
].dstOffsets
[0].x
;
530 unsigned dst_x1
= pRegions
[r
].dstOffsets
[1].x
;
531 bool flip_x
= flip_coords(&src_x0
, &src_x1
, &dst_x0
, &dst_x1
);
533 unsigned src_y0
= pRegions
[r
].srcOffsets
[0].y
;
534 unsigned src_y1
= pRegions
[r
].srcOffsets
[1].y
;
535 unsigned dst_y0
= pRegions
[r
].dstOffsets
[0].y
;
536 unsigned dst_y1
= pRegions
[r
].dstOffsets
[1].y
;
537 bool flip_y
= flip_coords(&src_y0
, &src_y1
, &dst_y0
, &dst_y1
);
539 const unsigned num_layers
= dst_end
- dst_start
;
540 for (unsigned i
= 0; i
< num_layers
; i
++) {
541 unsigned dst_z
= dst_start
+ i
;
542 unsigned src_z
= src_start
+ i
* src_z_step
;
544 blorp_blit(&batch
, &src
, src_res
->mipLevel
, src_z
,
545 src_format
.isl_format
, src_format
.swizzle
,
546 &dst
, dst_res
->mipLevel
, dst_z
,
547 dst_format
.isl_format
,
548 anv_swizzle_for_render(dst_format
.swizzle
),
549 src_x0
, src_y0
, src_x1
, src_y1
,
550 dst_x0
, dst_y0
, dst_x1
, dst_y1
,
551 gl_filter
, flip_x
, flip_y
);
556 blorp_batch_finish(&batch
);
559 static enum isl_format
560 isl_format_for_size(unsigned size_B
)
563 case 4: return ISL_FORMAT_R32_UINT
;
564 case 8: return ISL_FORMAT_R32G32_UINT
;
565 case 16: return ISL_FORMAT_R32G32B32A32_UINT
;
567 unreachable("Not a power-of-two format size");
572 * Returns the greatest common divisor of a and b that is a power of two.
575 gcd_pow2_u64(uint64_t a
, uint64_t b
)
577 assert(a
> 0 || b
> 0);
579 unsigned a_log2
= ffsll(a
) - 1;
580 unsigned b_log2
= ffsll(b
) - 1;
582 /* If either a or b is 0, then a_log2 or b_log2 till be UINT_MAX in which
583 * case, the MIN2() will take the other one. If both are 0 then we will
584 * hit the assert above.
586 return 1 << MIN2(a_log2
, b_log2
);
589 /* This is maximum possible width/height our HW can handle */
590 #define MAX_SURFACE_DIM (1ull << 14)
592 void anv_CmdCopyBuffer(
593 VkCommandBuffer commandBuffer
,
596 uint32_t regionCount
,
597 const VkBufferCopy
* pRegions
)
599 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
600 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
601 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
603 struct blorp_batch batch
;
604 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
606 for (unsigned r
= 0; r
< regionCount
; r
++) {
607 struct blorp_address src
= {
608 .buffer
= src_buffer
->bo
,
609 .offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
,
611 struct blorp_address dst
= {
612 .buffer
= dst_buffer
->bo
,
613 .offset
= dst_buffer
->offset
+ pRegions
[r
].dstOffset
,
616 blorp_buffer_copy(&batch
, src
, dst
, pRegions
[r
].size
);
619 blorp_batch_finish(&batch
);
622 void anv_CmdUpdateBuffer(
623 VkCommandBuffer commandBuffer
,
625 VkDeviceSize dstOffset
,
626 VkDeviceSize dataSize
,
629 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
630 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
632 struct blorp_batch batch
;
633 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
635 /* We can't quite grab a full block because the state stream needs a
636 * little data at the top to build its linked list.
638 const uint32_t max_update_size
=
639 cmd_buffer
->device
->dynamic_state_pool
.block_size
- 64;
641 assert(max_update_size
< MAX_SURFACE_DIM
* 4);
643 /* We're about to read data that was written from the CPU. Flush the
644 * texture cache so we don't get anything stale.
646 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
649 const uint32_t copy_size
= MIN2(dataSize
, max_update_size
);
651 struct anv_state tmp_data
=
652 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, copy_size
, 64);
654 memcpy(tmp_data
.map
, pData
, copy_size
);
656 anv_state_flush(cmd_buffer
->device
, tmp_data
);
658 struct blorp_address src
= {
659 .buffer
= &cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
660 .offset
= tmp_data
.offset
,
662 struct blorp_address dst
= {
663 .buffer
= dst_buffer
->bo
,
664 .offset
= dst_buffer
->offset
+ dstOffset
,
667 blorp_buffer_copy(&batch
, src
, dst
, copy_size
);
669 dataSize
-= copy_size
;
670 dstOffset
+= copy_size
;
671 pData
= (void *)pData
+ copy_size
;
674 blorp_batch_finish(&batch
);
677 void anv_CmdFillBuffer(
678 VkCommandBuffer commandBuffer
,
680 VkDeviceSize dstOffset
,
681 VkDeviceSize fillSize
,
684 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
685 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
686 struct blorp_surf surf
;
687 struct isl_surf isl_surf
;
689 struct blorp_batch batch
;
690 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
692 fillSize
= anv_buffer_get_range(dst_buffer
, dstOffset
, fillSize
);
694 /* From the Vulkan spec:
696 * "size is the number of bytes to fill, and must be either a multiple
697 * of 4, or VK_WHOLE_SIZE to fill the range from offset to the end of
698 * the buffer. If VK_WHOLE_SIZE is used and the remaining size of the
699 * buffer is not a multiple of 4, then the nearest smaller multiple is
704 /* First, we compute the biggest format that can be used with the
705 * given offsets and size.
708 bs
= gcd_pow2_u64(bs
, dstOffset
);
709 bs
= gcd_pow2_u64(bs
, fillSize
);
710 enum isl_format isl_format
= isl_format_for_size(bs
);
712 union isl_color_value color
= {
713 .u32
= { data
, data
, data
, data
},
716 const uint64_t max_fill_size
= MAX_SURFACE_DIM
* MAX_SURFACE_DIM
* bs
;
717 while (fillSize
>= max_fill_size
) {
718 get_blorp_surf_for_anv_buffer(cmd_buffer
->device
,
719 dst_buffer
, dstOffset
,
720 MAX_SURFACE_DIM
, MAX_SURFACE_DIM
,
721 MAX_SURFACE_DIM
* bs
, isl_format
,
724 blorp_clear(&batch
, &surf
, isl_format
, ISL_SWIZZLE_IDENTITY
,
725 0, 0, 1, 0, 0, MAX_SURFACE_DIM
, MAX_SURFACE_DIM
,
727 fillSize
-= max_fill_size
;
728 dstOffset
+= max_fill_size
;
731 uint64_t height
= fillSize
/ (MAX_SURFACE_DIM
* bs
);
732 assert(height
< MAX_SURFACE_DIM
);
734 const uint64_t rect_fill_size
= height
* MAX_SURFACE_DIM
* bs
;
735 get_blorp_surf_for_anv_buffer(cmd_buffer
->device
,
736 dst_buffer
, dstOffset
,
737 MAX_SURFACE_DIM
, height
,
738 MAX_SURFACE_DIM
* bs
, isl_format
,
741 blorp_clear(&batch
, &surf
, isl_format
, ISL_SWIZZLE_IDENTITY
,
742 0, 0, 1, 0, 0, MAX_SURFACE_DIM
, height
,
744 fillSize
-= rect_fill_size
;
745 dstOffset
+= rect_fill_size
;
749 const uint32_t width
= fillSize
/ bs
;
750 get_blorp_surf_for_anv_buffer(cmd_buffer
->device
,
751 dst_buffer
, dstOffset
,
753 width
* bs
, isl_format
,
756 blorp_clear(&batch
, &surf
, isl_format
, ISL_SWIZZLE_IDENTITY
,
757 0, 0, 1, 0, 0, width
, 1,
761 blorp_batch_finish(&batch
);
764 void anv_CmdClearColorImage(
765 VkCommandBuffer commandBuffer
,
767 VkImageLayout imageLayout
,
768 const VkClearColorValue
* pColor
,
770 const VkImageSubresourceRange
* pRanges
)
772 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
773 ANV_FROM_HANDLE(anv_image
, image
, _image
);
775 static const bool color_write_disable
[4] = { false, false, false, false };
777 struct blorp_batch batch
;
778 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
781 for (unsigned r
= 0; r
< rangeCount
; r
++) {
782 if (pRanges
[r
].aspectMask
== 0)
785 assert(pRanges
[r
].aspectMask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
787 struct blorp_surf surf
;
788 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
789 image
, pRanges
[r
].aspectMask
,
790 ANV_AUX_USAGE_DEFAULT
, &surf
);
792 struct anv_format_plane src_format
=
793 anv_get_format_plane(&cmd_buffer
->device
->info
, image
->vk_format
,
794 VK_IMAGE_ASPECT_COLOR_BIT
, image
->tiling
);
796 unsigned base_layer
= pRanges
[r
].baseArrayLayer
;
797 unsigned layer_count
= anv_get_layerCount(image
, &pRanges
[r
]);
799 for (unsigned i
= 0; i
< anv_get_levelCount(image
, &pRanges
[r
]); i
++) {
800 const unsigned level
= pRanges
[r
].baseMipLevel
+ i
;
801 const unsigned level_width
= anv_minify(image
->extent
.width
, level
);
802 const unsigned level_height
= anv_minify(image
->extent
.height
, level
);
804 if (image
->type
== VK_IMAGE_TYPE_3D
) {
806 layer_count
= anv_minify(image
->extent
.depth
, level
);
809 blorp_clear(&batch
, &surf
,
810 src_format
.isl_format
, src_format
.swizzle
,
811 level
, base_layer
, layer_count
,
812 0, 0, level_width
, level_height
,
813 vk_to_isl_color(*pColor
), color_write_disable
);
817 blorp_batch_finish(&batch
);
820 void anv_CmdClearDepthStencilImage(
821 VkCommandBuffer commandBuffer
,
823 VkImageLayout imageLayout
,
824 const VkClearDepthStencilValue
* pDepthStencil
,
826 const VkImageSubresourceRange
* pRanges
)
828 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
829 ANV_FROM_HANDLE(anv_image
, image
, image_h
);
831 struct blorp_batch batch
;
832 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
834 struct blorp_surf depth
, stencil
;
835 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
836 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
837 image
, VK_IMAGE_ASPECT_DEPTH_BIT
,
838 ISL_AUX_USAGE_NONE
, &depth
);
840 memset(&depth
, 0, sizeof(depth
));
843 if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
844 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
845 image
, VK_IMAGE_ASPECT_STENCIL_BIT
,
846 ISL_AUX_USAGE_NONE
, &stencil
);
848 memset(&stencil
, 0, sizeof(stencil
));
851 for (unsigned r
= 0; r
< rangeCount
; r
++) {
852 if (pRanges
[r
].aspectMask
== 0)
855 bool clear_depth
= pRanges
[r
].aspectMask
& VK_IMAGE_ASPECT_DEPTH_BIT
;
856 bool clear_stencil
= pRanges
[r
].aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
;
858 unsigned base_layer
= pRanges
[r
].baseArrayLayer
;
859 unsigned layer_count
= anv_get_layerCount(image
, &pRanges
[r
]);
861 for (unsigned i
= 0; i
< anv_get_levelCount(image
, &pRanges
[r
]); i
++) {
862 const unsigned level
= pRanges
[r
].baseMipLevel
+ i
;
863 const unsigned level_width
= anv_minify(image
->extent
.width
, level
);
864 const unsigned level_height
= anv_minify(image
->extent
.height
, level
);
866 if (image
->type
== VK_IMAGE_TYPE_3D
)
867 layer_count
= anv_minify(image
->extent
.depth
, level
);
869 blorp_clear_depth_stencil(&batch
, &depth
, &stencil
,
870 level
, base_layer
, layer_count
,
871 0, 0, level_width
, level_height
,
872 clear_depth
, pDepthStencil
->depth
,
873 clear_stencil
? 0xff : 0,
874 pDepthStencil
->stencil
);
878 blorp_batch_finish(&batch
);
882 anv_cmd_buffer_alloc_blorp_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
883 uint32_t num_entries
,
884 uint32_t *state_offset
,
885 struct anv_state
*bt_state
)
887 *bt_state
= anv_cmd_buffer_alloc_binding_table(cmd_buffer
, num_entries
,
889 if (bt_state
->map
== NULL
) {
890 /* We ran out of space. Grab a new binding table block. */
891 VkResult result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
892 if (result
!= VK_SUCCESS
)
895 /* Re-emit state base addresses so we get the new surface state base
896 * address before we start emitting binding tables etc.
898 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
900 *bt_state
= anv_cmd_buffer_alloc_binding_table(cmd_buffer
, num_entries
,
902 assert(bt_state
->map
!= NULL
);
909 binding_table_for_surface_state(struct anv_cmd_buffer
*cmd_buffer
,
910 struct anv_state surface_state
,
913 uint32_t state_offset
;
914 struct anv_state bt_state
;
917 anv_cmd_buffer_alloc_blorp_binding_table(cmd_buffer
, 1, &state_offset
,
919 if (result
!= VK_SUCCESS
)
922 uint32_t *bt_map
= bt_state
.map
;
923 bt_map
[0] = surface_state
.offset
+ state_offset
;
925 *bt_offset
= bt_state
.offset
;
930 clear_color_attachment(struct anv_cmd_buffer
*cmd_buffer
,
931 struct blorp_batch
*batch
,
932 const VkClearAttachment
*attachment
,
933 uint32_t rectCount
, const VkClearRect
*pRects
)
935 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
936 const uint32_t color_att
= attachment
->colorAttachment
;
937 const uint32_t att_idx
= subpass
->color_attachments
[color_att
].attachment
;
939 if (att_idx
== VK_ATTACHMENT_UNUSED
)
942 struct anv_render_pass_attachment
*pass_att
=
943 &cmd_buffer
->state
.pass
->attachments
[att_idx
];
944 struct anv_attachment_state
*att_state
=
945 &cmd_buffer
->state
.attachments
[att_idx
];
947 uint32_t binding_table
;
949 binding_table_for_surface_state(cmd_buffer
, att_state
->color
.state
,
951 if (result
!= VK_SUCCESS
)
954 union isl_color_value clear_color
=
955 vk_to_isl_color(attachment
->clearValue
.color
);
957 /* If multiview is enabled we ignore baseArrayLayer and layerCount */
958 if (subpass
->view_mask
) {
960 for_each_bit(view_idx
, subpass
->view_mask
) {
961 for (uint32_t r
= 0; r
< rectCount
; ++r
) {
962 const VkOffset2D offset
= pRects
[r
].rect
.offset
;
963 const VkExtent2D extent
= pRects
[r
].rect
.extent
;
964 blorp_clear_attachments(batch
, binding_table
,
965 ISL_FORMAT_UNSUPPORTED
, pass_att
->samples
,
968 offset
.x
+ extent
.width
,
969 offset
.y
+ extent
.height
,
970 true, clear_color
, false, 0.0f
, 0, 0);
976 for (uint32_t r
= 0; r
< rectCount
; ++r
) {
977 const VkOffset2D offset
= pRects
[r
].rect
.offset
;
978 const VkExtent2D extent
= pRects
[r
].rect
.extent
;
979 blorp_clear_attachments(batch
, binding_table
,
980 ISL_FORMAT_UNSUPPORTED
, pass_att
->samples
,
981 pRects
[r
].baseArrayLayer
,
982 pRects
[r
].layerCount
,
984 offset
.x
+ extent
.width
, offset
.y
+ extent
.height
,
985 true, clear_color
, false, 0.0f
, 0, 0);
990 clear_depth_stencil_attachment(struct anv_cmd_buffer
*cmd_buffer
,
991 struct blorp_batch
*batch
,
992 const VkClearAttachment
*attachment
,
993 uint32_t rectCount
, const VkClearRect
*pRects
)
995 static const union isl_color_value color_value
= { .u32
= { 0, } };
996 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
997 const uint32_t att_idx
= subpass
->depth_stencil_attachment
.attachment
;
999 if (att_idx
== VK_ATTACHMENT_UNUSED
)
1002 struct anv_render_pass_attachment
*pass_att
=
1003 &cmd_buffer
->state
.pass
->attachments
[att_idx
];
1005 bool clear_depth
= attachment
->aspectMask
& VK_IMAGE_ASPECT_DEPTH_BIT
;
1006 bool clear_stencil
= attachment
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
;
1008 enum isl_format depth_format
= ISL_FORMAT_UNSUPPORTED
;
1010 depth_format
= anv_get_isl_format(&cmd_buffer
->device
->info
,
1012 VK_IMAGE_ASPECT_DEPTH_BIT
,
1013 VK_IMAGE_TILING_OPTIMAL
);
1016 uint32_t binding_table
;
1018 binding_table_for_surface_state(cmd_buffer
,
1019 cmd_buffer
->state
.null_surface_state
,
1021 if (result
!= VK_SUCCESS
)
1024 /* If multiview is enabled we ignore baseArrayLayer and layerCount */
1025 if (subpass
->view_mask
) {
1027 for_each_bit(view_idx
, subpass
->view_mask
) {
1028 for (uint32_t r
= 0; r
< rectCount
; ++r
) {
1029 const VkOffset2D offset
= pRects
[r
].rect
.offset
;
1030 const VkExtent2D extent
= pRects
[r
].rect
.extent
;
1031 VkClearDepthStencilValue value
= attachment
->clearValue
.depthStencil
;
1032 blorp_clear_attachments(batch
, binding_table
,
1033 depth_format
, pass_att
->samples
,
1036 offset
.x
+ extent
.width
,
1037 offset
.y
+ extent
.height
,
1039 clear_depth
, value
.depth
,
1040 clear_stencil
? 0xff : 0, value
.stencil
);
1046 for (uint32_t r
= 0; r
< rectCount
; ++r
) {
1047 const VkOffset2D offset
= pRects
[r
].rect
.offset
;
1048 const VkExtent2D extent
= pRects
[r
].rect
.extent
;
1049 VkClearDepthStencilValue value
= attachment
->clearValue
.depthStencil
;
1050 blorp_clear_attachments(batch
, binding_table
,
1051 depth_format
, pass_att
->samples
,
1052 pRects
[r
].baseArrayLayer
,
1053 pRects
[r
].layerCount
,
1055 offset
.x
+ extent
.width
, offset
.y
+ extent
.height
,
1057 clear_depth
, value
.depth
,
1058 clear_stencil
? 0xff : 0, value
.stencil
);
1062 void anv_CmdClearAttachments(
1063 VkCommandBuffer commandBuffer
,
1064 uint32_t attachmentCount
,
1065 const VkClearAttachment
* pAttachments
,
1067 const VkClearRect
* pRects
)
1069 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1071 /* Because this gets called within a render pass, we tell blorp not to
1072 * trash our depth and stencil buffers.
1074 struct blorp_batch batch
;
1075 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
,
1076 BLORP_BATCH_NO_EMIT_DEPTH_STENCIL
);
1078 for (uint32_t a
= 0; a
< attachmentCount
; ++a
) {
1079 if (pAttachments
[a
].aspectMask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
1080 assert(pAttachments
[a
].aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
1081 clear_color_attachment(cmd_buffer
, &batch
,
1085 clear_depth_stencil_attachment(cmd_buffer
, &batch
,
1091 blorp_batch_finish(&batch
);
1094 enum subpass_stage
{
1097 SUBPASS_STAGE_RESOLVE
,
1101 subpass_needs_clear(const struct anv_cmd_buffer
*cmd_buffer
)
1103 const struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1104 uint32_t ds
= cmd_state
->subpass
->depth_stencil_attachment
.attachment
;
1106 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
1107 uint32_t a
= cmd_state
->subpass
->color_attachments
[i
].attachment
;
1108 if (a
== VK_ATTACHMENT_UNUSED
)
1111 assert(a
< cmd_state
->pass
->attachment_count
);
1112 if (cmd_state
->attachments
[a
].pending_clear_aspects
) {
1117 if (ds
!= VK_ATTACHMENT_UNUSED
) {
1118 assert(ds
< cmd_state
->pass
->attachment_count
);
1119 if (cmd_state
->attachments
[ds
].pending_clear_aspects
)
1127 anv_cmd_buffer_clear_subpass(struct anv_cmd_buffer
*cmd_buffer
)
1129 const struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1130 const VkRect2D render_area
= cmd_buffer
->state
.render_area
;
1133 if (!subpass_needs_clear(cmd_buffer
))
1136 /* Because this gets called within a render pass, we tell blorp not to
1137 * trash our depth and stencil buffers.
1139 struct blorp_batch batch
;
1140 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
,
1141 BLORP_BATCH_NO_EMIT_DEPTH_STENCIL
);
1143 VkClearRect clear_rect
= {
1144 .rect
= cmd_buffer
->state
.render_area
,
1145 .baseArrayLayer
= 0,
1146 .layerCount
= cmd_buffer
->state
.framebuffer
->layers
,
1149 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
1150 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
1151 const uint32_t a
= cmd_state
->subpass
->color_attachments
[i
].attachment
;
1152 if (a
== VK_ATTACHMENT_UNUSED
)
1155 assert(a
< cmd_state
->pass
->attachment_count
);
1156 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[a
];
1158 if (!att_state
->pending_clear_aspects
)
1161 assert(att_state
->pending_clear_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
1163 struct anv_image_view
*iview
= fb
->attachments
[a
];
1164 const struct anv_image
*image
= iview
->image
;
1165 struct blorp_surf surf
;
1166 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
1167 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
1168 att_state
->aux_usage
, &surf
);
1170 if (att_state
->fast_clear
) {
1171 surf
.clear_color
= vk_to_isl_color(att_state
->clear_value
.color
);
1173 /* From the Sky Lake PRM Vol. 7, "Render Target Fast Clear":
1175 * "After Render target fast clear, pipe-control with color cache
1176 * write-flush must be issued before sending any DRAW commands on
1177 * that render target."
1179 * This comment is a bit cryptic and doesn't really tell you what's
1180 * going or what's really needed. It appears that fast clear ops are
1181 * not properly synchronized with other drawing. This means that we
1182 * cannot have a fast clear operation in the pipe at the same time as
1183 * other regular drawing operations. We need to use a PIPE_CONTROL
1184 * to ensure that the contents of the previous draw hit the render
1185 * target before we resolve and then use a second PIPE_CONTROL after
1186 * the resolve to ensure that it is completed before any additional
1189 cmd_buffer
->state
.pending_pipe_bits
|=
1190 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
1192 assert(image
->n_planes
== 1);
1193 blorp_fast_clear(&batch
, &surf
, iview
->planes
[0].isl
.format
,
1194 iview
->planes
[0].isl
.base_level
,
1195 iview
->planes
[0].isl
.base_array_layer
, fb
->layers
,
1196 render_area
.offset
.x
, render_area
.offset
.y
,
1197 render_area
.offset
.x
+ render_area
.extent
.width
,
1198 render_area
.offset
.y
+ render_area
.extent
.height
);
1200 cmd_buffer
->state
.pending_pipe_bits
|=
1201 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
1203 assert(image
->n_planes
== 1);
1204 blorp_clear(&batch
, &surf
, iview
->planes
[0].isl
.format
,
1205 anv_swizzle_for_render(iview
->planes
[0].isl
.swizzle
),
1206 iview
->planes
[0].isl
.base_level
,
1207 iview
->planes
[0].isl
.base_array_layer
, fb
->layers
,
1208 render_area
.offset
.x
, render_area
.offset
.y
,
1209 render_area
.offset
.x
+ render_area
.extent
.width
,
1210 render_area
.offset
.y
+ render_area
.extent
.height
,
1211 vk_to_isl_color(att_state
->clear_value
.color
), NULL
);
1214 att_state
->pending_clear_aspects
= 0;
1217 const uint32_t ds
= cmd_state
->subpass
->depth_stencil_attachment
.attachment
;
1218 assert(ds
== VK_ATTACHMENT_UNUSED
|| ds
< cmd_state
->pass
->attachment_count
);
1220 if (ds
!= VK_ATTACHMENT_UNUSED
&&
1221 cmd_state
->attachments
[ds
].pending_clear_aspects
) {
1223 VkClearAttachment clear_att
= {
1224 .aspectMask
= cmd_state
->attachments
[ds
].pending_clear_aspects
,
1225 .clearValue
= cmd_state
->attachments
[ds
].clear_value
,
1229 const uint8_t gen
= cmd_buffer
->device
->info
.gen
;
1230 bool clear_with_hiz
= gen
>= 8 && cmd_state
->attachments
[ds
].aux_usage
==
1232 const struct anv_image_view
*iview
= fb
->attachments
[ds
];
1234 if (clear_with_hiz
) {
1235 const bool clear_depth
= clear_att
.aspectMask
&
1236 VK_IMAGE_ASPECT_DEPTH_BIT
;
1237 const bool clear_stencil
= clear_att
.aspectMask
&
1238 VK_IMAGE_ASPECT_STENCIL_BIT
;
1240 /* Check against restrictions for depth buffer clearing. A great GPU
1241 * performance benefit isn't expected when using the HZ sequence for
1242 * stencil-only clears. Therefore, we don't emit a HZ op sequence for
1243 * a stencil clear in addition to using the BLORP-fallback for depth.
1246 if (!blorp_can_hiz_clear_depth(gen
, iview
->planes
[0].isl
.format
,
1247 iview
->image
->samples
,
1248 render_area
.offset
.x
,
1249 render_area
.offset
.y
,
1250 render_area
.offset
.x
+
1251 render_area
.extent
.width
,
1252 render_area
.offset
.y
+
1253 render_area
.extent
.height
)) {
1254 clear_with_hiz
= false;
1255 } else if (clear_att
.clearValue
.depthStencil
.depth
!=
1257 /* Don't enable fast depth clears for any color not equal to
1260 clear_with_hiz
= false;
1261 } else if (gen
== 8 &&
1262 anv_can_sample_with_hiz(&cmd_buffer
->device
->info
,
1264 /* Only gen9+ supports returning ANV_HZ_FC_VAL when sampling a
1265 * fast-cleared portion of a HiZ buffer. Testing has revealed
1266 * that Gen8 only supports returning 0.0f. Gens prior to gen8 do
1267 * not support this feature at all.
1269 clear_with_hiz
= false;
1273 if (clear_with_hiz
) {
1274 blorp_gen8_hiz_clear_attachments(&batch
, iview
->image
->samples
,
1275 render_area
.offset
.x
,
1276 render_area
.offset
.y
,
1277 render_area
.offset
.x
+
1278 render_area
.extent
.width
,
1279 render_area
.offset
.y
+
1280 render_area
.extent
.height
,
1281 clear_depth
, clear_stencil
,
1282 clear_att
.clearValue
.
1283 depthStencil
.stencil
);
1285 /* From the SKL PRM, Depth Buffer Clear:
1287 * Depth Buffer Clear Workaround
1288 * Depth buffer clear pass using any of the methods (WM_STATE,
1289 * 3DSTATE_WM or 3DSTATE_WM_HZ_OP) must be followed by a
1290 * PIPE_CONTROL command with DEPTH_STALL bit and Depth FLUSH bits
1291 * “set” before starting to render. DepthStall and DepthFlush are
1292 * not needed between consecutive depth clear passes nor is it
1293 * required if the depth-clear pass was done with “full_surf_clear”
1294 * bit set in the 3DSTATE_WM_HZ_OP.
1297 cmd_buffer
->state
.pending_pipe_bits
|=
1298 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
| ANV_PIPE_DEPTH_STALL_BIT
;
1303 if (!clear_with_hiz
) {
1304 clear_depth_stencil_attachment(cmd_buffer
, &batch
,
1305 &clear_att
, 1, &clear_rect
);
1308 cmd_state
->attachments
[ds
].pending_clear_aspects
= 0;
1311 blorp_batch_finish(&batch
);
1315 resolve_surface(struct blorp_batch
*batch
,
1316 struct blorp_surf
*src_surf
,
1317 uint32_t src_level
, uint32_t src_layer
,
1318 struct blorp_surf
*dst_surf
,
1319 uint32_t dst_level
, uint32_t dst_layer
,
1320 uint32_t src_x
, uint32_t src_y
, uint32_t dst_x
, uint32_t dst_y
,
1321 uint32_t width
, uint32_t height
)
1324 src_surf
, src_level
, src_layer
,
1325 ISL_FORMAT_UNSUPPORTED
, ISL_SWIZZLE_IDENTITY
,
1326 dst_surf
, dst_level
, dst_layer
,
1327 ISL_FORMAT_UNSUPPORTED
, ISL_SWIZZLE_IDENTITY
,
1328 src_x
, src_y
, src_x
+ width
, src_y
+ height
,
1329 dst_x
, dst_y
, dst_x
+ width
, dst_y
+ height
,
1330 0x2600 /* GL_NEAREST */, false, false);
1334 resolve_image(struct anv_device
*device
,
1335 struct blorp_batch
*batch
,
1336 const struct anv_image
*src_image
,
1337 uint32_t src_level
, uint32_t src_layer
,
1338 const struct anv_image
*dst_image
,
1339 uint32_t dst_level
, uint32_t dst_layer
,
1340 VkImageAspectFlags aspect_mask
,
1341 uint32_t src_x
, uint32_t src_y
, uint32_t dst_x
, uint32_t dst_y
,
1342 uint32_t width
, uint32_t height
)
1344 assert(src_image
->type
== VK_IMAGE_TYPE_2D
);
1345 assert(src_image
->samples
> 1);
1346 assert(dst_image
->type
== VK_IMAGE_TYPE_2D
);
1347 assert(dst_image
->samples
== 1);
1348 assert(src_image
->n_planes
== dst_image
->n_planes
);
1350 uint32_t aspect_bit
;
1352 anv_foreach_image_aspect_bit(aspect_bit
, src_image
, aspect_mask
) {
1353 struct blorp_surf src_surf
, dst_surf
;
1354 get_blorp_surf_for_anv_image(device
, src_image
, 1UL << aspect_bit
,
1355 ANV_AUX_USAGE_DEFAULT
, &src_surf
);
1356 get_blorp_surf_for_anv_image(device
, dst_image
, 1UL << aspect_bit
,
1357 ANV_AUX_USAGE_DEFAULT
, &dst_surf
);
1359 assert(!src_image
->format
->can_ycbcr
);
1360 assert(!dst_image
->format
->can_ycbcr
);
1362 resolve_surface(batch
,
1363 &src_surf
, src_level
, src_layer
,
1364 &dst_surf
, dst_level
, dst_layer
,
1365 src_x
, src_y
, dst_x
, dst_y
, width
, height
);
1369 void anv_CmdResolveImage(
1370 VkCommandBuffer commandBuffer
,
1372 VkImageLayout srcImageLayout
,
1374 VkImageLayout dstImageLayout
,
1375 uint32_t regionCount
,
1376 const VkImageResolve
* pRegions
)
1378 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1379 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1380 ANV_FROM_HANDLE(anv_image
, dst_image
, dstImage
);
1382 struct blorp_batch batch
;
1383 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
1385 for (uint32_t r
= 0; r
< regionCount
; r
++) {
1386 assert(pRegions
[r
].srcSubresource
.aspectMask
==
1387 pRegions
[r
].dstSubresource
.aspectMask
);
1388 assert(anv_get_layerCount(src_image
, &pRegions
[r
].srcSubresource
) ==
1389 anv_get_layerCount(dst_image
, &pRegions
[r
].dstSubresource
));
1391 const uint32_t layer_count
=
1392 anv_get_layerCount(dst_image
, &pRegions
[r
].dstSubresource
);
1394 VkImageAspectFlags src_mask
= pRegions
[r
].srcSubresource
.aspectMask
,
1395 dst_mask
= pRegions
[r
].dstSubresource
.aspectMask
;
1397 assert(anv_image_aspects_compatible(src_mask
, dst_mask
));
1399 for (uint32_t layer
= 0; layer
< layer_count
; layer
++) {
1400 resolve_image(cmd_buffer
->device
, &batch
,
1402 pRegions
[r
].srcSubresource
.mipLevel
,
1403 pRegions
[r
].srcSubresource
.baseArrayLayer
+ layer
,
1405 pRegions
[r
].dstSubresource
.mipLevel
,
1406 pRegions
[r
].dstSubresource
.baseArrayLayer
+ layer
,
1407 pRegions
[r
].dstSubresource
.aspectMask
,
1408 pRegions
[r
].srcOffset
.x
, pRegions
[r
].srcOffset
.y
,
1409 pRegions
[r
].dstOffset
.x
, pRegions
[r
].dstOffset
.y
,
1410 pRegions
[r
].extent
.width
, pRegions
[r
].extent
.height
);
1414 blorp_batch_finish(&batch
);
1417 static enum isl_aux_usage
1418 fast_clear_aux_usage(const struct anv_image
*image
,
1419 VkImageAspectFlagBits aspect
)
1421 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
1422 if (image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_NONE
)
1423 return ISL_AUX_USAGE_CCS_D
;
1425 return image
->planes
[plane
].aux_usage
;
1429 anv_image_fast_clear(struct anv_cmd_buffer
*cmd_buffer
,
1430 const struct anv_image
*image
,
1431 VkImageAspectFlagBits aspect
,
1432 const uint32_t base_level
, const uint32_t level_count
,
1433 const uint32_t base_layer
, uint32_t layer_count
)
1435 assert(image
->type
== VK_IMAGE_TYPE_3D
|| image
->extent
.depth
== 1);
1437 if (image
->type
== VK_IMAGE_TYPE_3D
) {
1438 assert(base_layer
== 0);
1439 assert(layer_count
== anv_minify(image
->extent
.depth
, base_level
));
1442 struct blorp_batch batch
;
1443 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
1445 struct blorp_surf surf
;
1446 get_blorp_surf_for_anv_image(cmd_buffer
->device
, image
, aspect
,
1447 fast_clear_aux_usage(image
, aspect
),
1450 /* From the Sky Lake PRM Vol. 7, "Render Target Fast Clear":
1452 * "After Render target fast clear, pipe-control with color cache
1453 * write-flush must be issued before sending any DRAW commands on
1454 * that render target."
1456 * This comment is a bit cryptic and doesn't really tell you what's going
1457 * or what's really needed. It appears that fast clear ops are not
1458 * properly synchronized with other drawing. This means that we cannot
1459 * have a fast clear operation in the pipe at the same time as other
1460 * regular drawing operations. We need to use a PIPE_CONTROL to ensure
1461 * that the contents of the previous draw hit the render target before we
1462 * resolve and then use a second PIPE_CONTROL after the resolve to ensure
1463 * that it is completed before any additional drawing occurs.
1465 cmd_buffer
->state
.pending_pipe_bits
|=
1466 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
1468 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
1469 uint32_t width_div
= image
->format
->planes
[plane
].denominator_scales
[0];
1470 uint32_t height_div
= image
->format
->planes
[plane
].denominator_scales
[1];
1472 for (uint32_t l
= 0; l
< level_count
; l
++) {
1473 const uint32_t level
= base_level
+ l
;
1475 const VkExtent3D extent
= {
1476 .width
= anv_minify(image
->extent
.width
, level
),
1477 .height
= anv_minify(image
->extent
.height
, level
),
1478 .depth
= anv_minify(image
->extent
.depth
, level
),
1481 if (image
->type
== VK_IMAGE_TYPE_3D
)
1482 layer_count
= extent
.depth
;
1484 assert(level
< anv_image_aux_levels(image
, aspect
));
1485 assert(base_layer
+ layer_count
<= anv_image_aux_layers(image
, aspect
, level
));
1486 blorp_fast_clear(&batch
, &surf
, surf
.surf
->format
,
1487 level
, base_layer
, layer_count
,
1489 extent
.width
/ width_div
,
1490 extent
.height
/ height_div
);
1493 cmd_buffer
->state
.pending_pipe_bits
|=
1494 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
1498 anv_cmd_buffer_resolve_subpass(struct anv_cmd_buffer
*cmd_buffer
)
1500 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
1501 struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1503 if (subpass
->has_resolve
) {
1504 struct blorp_batch batch
;
1505 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
1507 /* We are about to do some MSAA resolves. We need to flush so that the
1508 * result of writes to the MSAA color attachments show up in the sampler
1509 * when we blit to the single-sampled resolve target.
1511 cmd_buffer
->state
.pending_pipe_bits
|=
1512 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
|
1513 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
1515 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
1516 uint32_t src_att
= subpass
->color_attachments
[i
].attachment
;
1517 uint32_t dst_att
= subpass
->resolve_attachments
[i
].attachment
;
1519 if (dst_att
== VK_ATTACHMENT_UNUSED
)
1522 assert(src_att
< cmd_buffer
->state
.pass
->attachment_count
);
1523 assert(dst_att
< cmd_buffer
->state
.pass
->attachment_count
);
1525 if (cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
) {
1526 /* From the Vulkan 1.0 spec:
1528 * If the first use of an attachment in a render pass is as a
1529 * resolve attachment, then the loadOp is effectively ignored
1530 * as the resolve is guaranteed to overwrite all pixels in the
1533 cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
= 0;
1536 struct anv_image_view
*src_iview
= fb
->attachments
[src_att
];
1537 struct anv_image_view
*dst_iview
= fb
->attachments
[dst_att
];
1539 enum isl_aux_usage src_aux_usage
=
1540 cmd_buffer
->state
.attachments
[src_att
].aux_usage
;
1541 enum isl_aux_usage dst_aux_usage
=
1542 cmd_buffer
->state
.attachments
[dst_att
].aux_usage
;
1544 const VkRect2D render_area
= cmd_buffer
->state
.render_area
;
1546 assert(src_iview
->aspect_mask
== VK_IMAGE_ASPECT_COLOR_BIT
&&
1547 dst_iview
->aspect_mask
== VK_IMAGE_ASPECT_COLOR_BIT
);
1549 struct blorp_surf src_surf
, dst_surf
;
1550 get_blorp_surf_for_anv_image(cmd_buffer
->device
, src_iview
->image
,
1551 VK_IMAGE_ASPECT_COLOR_BIT
,
1552 src_aux_usage
, &src_surf
);
1553 get_blorp_surf_for_anv_image(cmd_buffer
->device
, dst_iview
->image
,
1554 VK_IMAGE_ASPECT_COLOR_BIT
,
1555 dst_aux_usage
, &dst_surf
);
1557 assert(!src_iview
->image
->format
->can_ycbcr
);
1558 assert(!dst_iview
->image
->format
->can_ycbcr
);
1560 resolve_surface(&batch
,
1562 src_iview
->planes
[0].isl
.base_level
,
1563 src_iview
->planes
[0].isl
.base_array_layer
,
1565 dst_iview
->planes
[0].isl
.base_level
,
1566 dst_iview
->planes
[0].isl
.base_array_layer
,
1567 render_area
.offset
.x
, render_area
.offset
.y
,
1568 render_area
.offset
.x
, render_area
.offset
.y
,
1569 render_area
.extent
.width
, render_area
.extent
.height
);
1572 blorp_batch_finish(&batch
);
1577 anv_image_copy_to_shadow(struct anv_cmd_buffer
*cmd_buffer
,
1578 const struct anv_image
*image
,
1579 uint32_t base_level
, uint32_t level_count
,
1580 uint32_t base_layer
, uint32_t layer_count
)
1582 struct blorp_batch batch
;
1583 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
1585 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
&& image
->n_planes
== 1);
1587 struct blorp_surf surf
;
1588 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
1589 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
1590 ISL_AUX_USAGE_NONE
, &surf
);
1592 struct blorp_surf shadow_surf
= {
1593 .surf
= &image
->planes
[0].shadow_surface
.isl
,
1595 .buffer
= image
->planes
[0].bo
,
1596 .offset
= image
->planes
[0].bo_offset
+
1597 image
->planes
[0].shadow_surface
.offset
,
1601 for (uint32_t l
= 0; l
< level_count
; l
++) {
1602 const uint32_t level
= base_level
+ l
;
1604 const VkExtent3D extent
= {
1605 .width
= anv_minify(image
->extent
.width
, level
),
1606 .height
= anv_minify(image
->extent
.height
, level
),
1607 .depth
= anv_minify(image
->extent
.depth
, level
),
1610 if (image
->type
== VK_IMAGE_TYPE_3D
)
1611 layer_count
= extent
.depth
;
1613 for (uint32_t a
= 0; a
< layer_count
; a
++) {
1614 const uint32_t layer
= base_layer
+ a
;
1616 blorp_copy(&batch
, &surf
, level
, layer
,
1617 &shadow_surf
, level
, layer
,
1618 0, 0, 0, 0, extent
.width
, extent
.height
);
1622 blorp_batch_finish(&batch
);
1626 anv_gen8_hiz_op_resolve(struct anv_cmd_buffer
*cmd_buffer
,
1627 const struct anv_image
*image
,
1628 enum blorp_hiz_op op
)
1632 assert(anv_image_aspect_to_plane(image
->aspects
,
1633 VK_IMAGE_ASPECT_DEPTH_BIT
) == 0);
1635 /* Don't resolve depth buffers without an auxiliary HiZ buffer and
1636 * don't perform such a resolve on gens that don't support it.
1638 if (cmd_buffer
->device
->info
.gen
< 8 ||
1639 image
->planes
[0].aux_usage
!= ISL_AUX_USAGE_HIZ
)
1642 assert(op
== BLORP_HIZ_OP_HIZ_RESOLVE
||
1643 op
== BLORP_HIZ_OP_DEPTH_RESOLVE
);
1645 struct blorp_batch batch
;
1646 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
, 0);
1648 struct blorp_surf surf
;
1649 get_blorp_surf_for_anv_image(cmd_buffer
->device
,
1650 image
, VK_IMAGE_ASPECT_DEPTH_BIT
,
1651 ISL_AUX_USAGE_NONE
, &surf
);
1653 /* Manually add the aux HiZ surf */
1654 surf
.aux_surf
= &image
->planes
[0].aux_surface
.isl
,
1655 surf
.aux_addr
= (struct blorp_address
) {
1656 .buffer
= image
->planes
[0].bo
,
1657 .offset
= image
->planes
[0].bo_offset
+
1658 image
->planes
[0].aux_surface
.offset
,
1660 surf
.aux_usage
= ISL_AUX_USAGE_HIZ
;
1662 surf
.clear_color
.f32
[0] = ANV_HZ_FC_VAL
;
1664 blorp_hiz_op(&batch
, &surf
, 0, 0, 1, op
);
1665 blorp_batch_finish(&batch
);
1669 anv_ccs_resolve(struct anv_cmd_buffer
* const cmd_buffer
,
1670 const struct anv_state surface_state
,
1671 const struct anv_image
* const image
,
1672 VkImageAspectFlagBits aspect
,
1673 const uint8_t level
, const uint32_t layer_count
,
1674 const enum blorp_fast_clear_op op
)
1676 assert(cmd_buffer
&& image
);
1678 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
1680 /* The resolved subresource range must have a CCS buffer. */
1681 assert(level
< anv_image_aux_levels(image
, aspect
));
1682 assert(layer_count
<= anv_image_aux_layers(image
, aspect
, level
));
1683 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
&& image
->samples
== 1);
1685 /* Create a binding table for this surface state. */
1686 uint32_t binding_table
;
1688 binding_table_for_surface_state(cmd_buffer
, surface_state
,
1690 if (result
!= VK_SUCCESS
)
1693 struct blorp_batch batch
;
1694 blorp_batch_init(&cmd_buffer
->device
->blorp
, &batch
, cmd_buffer
,
1695 BLORP_BATCH_PREDICATE_ENABLE
);
1697 struct blorp_surf surf
;
1698 get_blorp_surf_for_anv_image(cmd_buffer
->device
, image
, aspect
,
1699 fast_clear_aux_usage(image
, aspect
),
1702 blorp_ccs_resolve_attachment(&batch
, binding_table
, &surf
, level
,
1704 image
->planes
[plane
].surface
.isl
.format
,
1707 blorp_batch_finish(&batch
);