2 * Copyright © 2015 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 #include "anv_private.h"
28 #include "vk_format_info.h"
30 #include "util/fast_idiv_by_const.h"
32 #include "common/gen_aux_map.h"
33 #include "common/gen_l3_config.h"
34 #include "genxml/gen_macros.h"
35 #include "genxml/genX_pack.h"
37 /* We reserve GPR 14 and 15 for conditional rendering */
38 #define GEN_MI_BUILDER_NUM_ALLOC_GPRS 14
39 #define __gen_get_batch_dwords anv_batch_emit_dwords
40 #define __gen_address_offset anv_address_add
41 #include "common/gen_mi_builder.h"
43 static void genX(flush_pipeline_select
)(struct anv_cmd_buffer
*cmd_buffer
,
47 emit_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
49 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
50 lri
.RegisterOffset
= reg
;
56 genX(cmd_buffer_emit_state_base_address
)(struct anv_cmd_buffer
*cmd_buffer
)
58 struct anv_device
*device
= cmd_buffer
->device
;
59 UNUSED
const struct gen_device_info
*devinfo
= &device
->info
;
60 uint32_t mocs
= device
->isl_dev
.mocs
.internal
;
62 /* If we are emitting a new state base address we probably need to re-emit
65 cmd_buffer
->state
.descriptors_dirty
|= ~0;
67 /* Emit a render target cache flush.
69 * This isn't documented anywhere in the PRM. However, it seems to be
70 * necessary prior to changing the surface state base adress. Without
71 * this, we get GPU hangs when using multi-level command buffers which
72 * clear depth, reset state base address, and then go render stuff.
74 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
75 pc
.DCFlushEnable
= true;
76 pc
.RenderTargetCacheFlushEnable
= true;
77 pc
.CommandStreamerStallEnable
= true;
79 pc
.TileCacheFlushEnable
= true;
82 /* GEN:BUG:1606662791:
84 * Software must program PIPE_CONTROL command with "HDC Pipeline
85 * Flush" prior to programming of the below two non-pipeline state :
86 * * STATE_BASE_ADDRESS
87 * * 3DSTATE_BINDING_TABLE_POOL_ALLOC
89 if (devinfo
->revision
== 0 /* A0 */)
90 pc
.HDCPipelineFlushEnable
= true;
95 /* GEN:BUG:1607854226:
97 * Workaround the non pipelined state not applying in MEDIA/GPGPU pipeline
98 * mode by putting the pipeline temporarily in 3D mode.
100 uint32_t gen12_wa_pipeline
= cmd_buffer
->state
.current_pipeline
;
101 genX(flush_pipeline_select_3d
)(cmd_buffer
);
104 anv_batch_emit(&cmd_buffer
->batch
, GENX(STATE_BASE_ADDRESS
), sba
) {
105 sba
.GeneralStateBaseAddress
= (struct anv_address
) { NULL
, 0 };
106 sba
.GeneralStateMOCS
= mocs
;
107 sba
.GeneralStateBaseAddressModifyEnable
= true;
109 sba
.StatelessDataPortAccessMOCS
= mocs
;
111 sba
.SurfaceStateBaseAddress
=
112 anv_cmd_buffer_surface_base_address(cmd_buffer
);
113 sba
.SurfaceStateMOCS
= mocs
;
114 sba
.SurfaceStateBaseAddressModifyEnable
= true;
116 sba
.DynamicStateBaseAddress
=
117 (struct anv_address
) { device
->dynamic_state_pool
.block_pool
.bo
, 0 };
118 sba
.DynamicStateMOCS
= mocs
;
119 sba
.DynamicStateBaseAddressModifyEnable
= true;
121 sba
.IndirectObjectBaseAddress
= (struct anv_address
) { NULL
, 0 };
122 sba
.IndirectObjectMOCS
= mocs
;
123 sba
.IndirectObjectBaseAddressModifyEnable
= true;
125 sba
.InstructionBaseAddress
=
126 (struct anv_address
) { device
->instruction_state_pool
.block_pool
.bo
, 0 };
127 sba
.InstructionMOCS
= mocs
;
128 sba
.InstructionBaseAddressModifyEnable
= true;
131 /* Broadwell requires that we specify a buffer size for a bunch of
132 * these fields. However, since we will be growing the BO's live, we
133 * just set them all to the maximum.
135 sba
.GeneralStateBufferSize
= 0xfffff;
136 sba
.IndirectObjectBufferSize
= 0xfffff;
137 if (device
->physical
->use_softpin
) {
138 /* With softpin, we use fixed addresses so we actually know how big
139 * our base addresses are.
141 sba
.DynamicStateBufferSize
= DYNAMIC_STATE_POOL_SIZE
/ 4096;
142 sba
.InstructionBufferSize
= INSTRUCTION_STATE_POOL_SIZE
/ 4096;
144 sba
.DynamicStateBufferSize
= 0xfffff;
145 sba
.InstructionBufferSize
= 0xfffff;
147 sba
.GeneralStateBufferSizeModifyEnable
= true;
148 sba
.IndirectObjectBufferSizeModifyEnable
= true;
149 sba
.DynamicStateBufferSizeModifyEnable
= true;
150 sba
.InstructionBuffersizeModifyEnable
= true;
152 /* On gen7, we have upper bounds instead. According to the docs,
153 * setting an upper bound of zero means that no bounds checking is
154 * performed so, in theory, we should be able to leave them zero.
155 * However, border color is broken and the GPU bounds-checks anyway.
156 * To avoid this and other potential problems, we may as well set it
159 sba
.GeneralStateAccessUpperBound
=
160 (struct anv_address
) { .bo
= NULL
, .offset
= 0xfffff000 };
161 sba
.GeneralStateAccessUpperBoundModifyEnable
= true;
162 sba
.DynamicStateAccessUpperBound
=
163 (struct anv_address
) { .bo
= NULL
, .offset
= 0xfffff000 };
164 sba
.DynamicStateAccessUpperBoundModifyEnable
= true;
165 sba
.InstructionAccessUpperBound
=
166 (struct anv_address
) { .bo
= NULL
, .offset
= 0xfffff000 };
167 sba
.InstructionAccessUpperBoundModifyEnable
= true;
170 if (cmd_buffer
->device
->physical
->use_softpin
) {
171 sba
.BindlessSurfaceStateBaseAddress
= (struct anv_address
) {
172 .bo
= device
->surface_state_pool
.block_pool
.bo
,
175 sba
.BindlessSurfaceStateSize
= (1 << 20) - 1;
177 sba
.BindlessSurfaceStateBaseAddress
= ANV_NULL_ADDRESS
;
178 sba
.BindlessSurfaceStateSize
= 0;
180 sba
.BindlessSurfaceStateMOCS
= mocs
;
181 sba
.BindlessSurfaceStateBaseAddressModifyEnable
= true;
184 sba
.BindlessSamplerStateBaseAddress
= (struct anv_address
) { NULL
, 0 };
185 sba
.BindlessSamplerStateMOCS
= mocs
;
186 sba
.BindlessSamplerStateBaseAddressModifyEnable
= true;
187 sba
.BindlessSamplerStateBufferSize
= 0;
192 /* GEN:BUG:1607854226:
194 * Put the pipeline back into its current mode.
196 if (gen12_wa_pipeline
!= UINT32_MAX
)
197 genX(flush_pipeline_select
)(cmd_buffer
, gen12_wa_pipeline
);
200 /* After re-setting the surface state base address, we have to do some
201 * cache flusing so that the sampler engine will pick up the new
202 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
203 * Shared Function > 3D Sampler > State > State Caching (page 96):
205 * Coherency with system memory in the state cache, like the texture
206 * cache is handled partially by software. It is expected that the
207 * command stream or shader will issue Cache Flush operation or
208 * Cache_Flush sampler message to ensure that the L1 cache remains
209 * coherent with system memory.
213 * Whenever the value of the Dynamic_State_Base_Addr,
214 * Surface_State_Base_Addr are altered, the L1 state cache must be
215 * invalidated to ensure the new surface or sampler state is fetched
216 * from system memory.
218 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
219 * which, according the PIPE_CONTROL instruction documentation in the
222 * Setting this bit is independent of any other bit in this packet.
223 * This bit controls the invalidation of the L1 and L2 state caches
224 * at the top of the pipe i.e. at the parsing time.
226 * Unfortunately, experimentation seems to indicate that state cache
227 * invalidation through a PIPE_CONTROL does nothing whatsoever in
228 * regards to surface state and binding tables. In stead, it seems that
229 * invalidating the texture cache is what is actually needed.
231 * XXX: As far as we have been able to determine through
232 * experimentation, shows that flush the texture cache appears to be
233 * sufficient. The theory here is that all of the sampling/rendering
234 * units cache the binding table in the texture cache. However, we have
235 * yet to be able to actually confirm this.
237 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
238 pc
.TextureCacheInvalidationEnable
= true;
239 pc
.ConstantCacheInvalidationEnable
= true;
240 pc
.StateCacheInvalidationEnable
= true;
245 add_surface_reloc(struct anv_cmd_buffer
*cmd_buffer
,
246 struct anv_state state
, struct anv_address addr
)
248 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
251 anv_reloc_list_add(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
,
252 state
.offset
+ isl_dev
->ss
.addr_offset
,
253 addr
.bo
, addr
.offset
, NULL
);
254 if (result
!= VK_SUCCESS
)
255 anv_batch_set_error(&cmd_buffer
->batch
, result
);
259 add_surface_state_relocs(struct anv_cmd_buffer
*cmd_buffer
,
260 struct anv_surface_state state
)
262 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
264 assert(!anv_address_is_null(state
.address
));
265 add_surface_reloc(cmd_buffer
, state
.state
, state
.address
);
267 if (!anv_address_is_null(state
.aux_address
)) {
269 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
270 &cmd_buffer
->pool
->alloc
,
271 state
.state
.offset
+ isl_dev
->ss
.aux_addr_offset
,
272 state
.aux_address
.bo
,
273 state
.aux_address
.offset
,
275 if (result
!= VK_SUCCESS
)
276 anv_batch_set_error(&cmd_buffer
->batch
, result
);
279 if (!anv_address_is_null(state
.clear_address
)) {
281 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
282 &cmd_buffer
->pool
->alloc
,
284 isl_dev
->ss
.clear_color_state_offset
,
285 state
.clear_address
.bo
,
286 state
.clear_address
.offset
,
288 if (result
!= VK_SUCCESS
)
289 anv_batch_set_error(&cmd_buffer
->batch
, result
);
294 color_attachment_compute_aux_usage(struct anv_device
* device
,
295 struct anv_cmd_state
* cmd_state
,
296 uint32_t att
, VkRect2D render_area
,
297 union isl_color_value
*fast_clear_color
)
299 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[att
];
300 struct anv_image_view
*iview
= cmd_state
->attachments
[att
].image_view
;
302 assert(iview
->n_planes
== 1);
304 if (iview
->planes
[0].isl
.base_array_layer
>=
305 anv_image_aux_layers(iview
->image
, VK_IMAGE_ASPECT_COLOR_BIT
,
306 iview
->planes
[0].isl
.base_level
)) {
307 /* There is no aux buffer which corresponds to the level and layer(s)
310 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
311 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
312 att_state
->fast_clear
= false;
316 att_state
->aux_usage
=
317 anv_layout_to_aux_usage(&device
->info
, iview
->image
,
318 VK_IMAGE_ASPECT_COLOR_BIT
,
319 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
320 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
);
322 /* If we don't have aux, then we should have returned early in the layer
323 * check above. If we got here, we must have something.
325 assert(att_state
->aux_usage
!= ISL_AUX_USAGE_NONE
);
327 if (att_state
->aux_usage
== ISL_AUX_USAGE_CCS_E
||
328 att_state
->aux_usage
== ISL_AUX_USAGE_MCS
) {
329 att_state
->input_aux_usage
= att_state
->aux_usage
;
331 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
333 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
334 * setting is only allowed if Surface Format supported for Fast
335 * Clear. In addition, if the surface is bound to the sampling
336 * engine, Surface Format must be supported for Render Target
337 * Compression for surfaces bound to the sampling engine."
339 * In other words, we can only sample from a fast-cleared image if it
340 * also supports color compression.
342 if (isl_format_supports_ccs_e(&device
->info
, iview
->planes
[0].isl
.format
) &&
343 isl_format_supports_ccs_d(&device
->info
, iview
->planes
[0].isl
.format
)) {
344 att_state
->input_aux_usage
= ISL_AUX_USAGE_CCS_D
;
346 /* While fast-clear resolves and partial resolves are fairly cheap in the
347 * case where you render to most of the pixels, full resolves are not
348 * because they potentially involve reading and writing the entire
349 * framebuffer. If we can't texture with CCS_E, we should leave it off and
350 * limit ourselves to fast clears.
352 if (cmd_state
->pass
->attachments
[att
].first_subpass_layout
==
353 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
) {
354 anv_perf_warn(device
, iview
->image
,
355 "Not temporarily enabling CCS_E.");
358 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
362 assert(iview
->image
->planes
[0].aux_surface
.isl
.usage
&
363 (ISL_SURF_USAGE_CCS_BIT
| ISL_SURF_USAGE_MCS_BIT
));
365 union isl_color_value clear_color
= {};
366 anv_clear_color_from_att_state(&clear_color
, att_state
, iview
);
368 att_state
->clear_color_is_zero_one
=
369 isl_color_value_is_zero_one(clear_color
, iview
->planes
[0].isl
.format
);
370 att_state
->clear_color_is_zero
=
371 isl_color_value_is_zero(clear_color
, iview
->planes
[0].isl
.format
);
373 if (att_state
->pending_clear_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
374 /* Start by getting the fast clear type. We use the first subpass
375 * layout here because we don't want to fast-clear if the first subpass
376 * to use the attachment can't handle fast-clears.
378 enum anv_fast_clear_type fast_clear_type
=
379 anv_layout_to_fast_clear_type(&device
->info
, iview
->image
,
380 VK_IMAGE_ASPECT_COLOR_BIT
,
381 cmd_state
->pass
->attachments
[att
].first_subpass_layout
);
382 switch (fast_clear_type
) {
383 case ANV_FAST_CLEAR_NONE
:
384 att_state
->fast_clear
= false;
386 case ANV_FAST_CLEAR_DEFAULT_VALUE
:
387 att_state
->fast_clear
= att_state
->clear_color_is_zero
;
389 case ANV_FAST_CLEAR_ANY
:
390 att_state
->fast_clear
= true;
394 /* Potentially, we could do partial fast-clears but doing so has crazy
395 * alignment restrictions. It's easier to just restrict to full size
396 * fast clears for now.
398 if (render_area
.offset
.x
!= 0 ||
399 render_area
.offset
.y
!= 0 ||
400 render_area
.extent
.width
!= iview
->extent
.width
||
401 render_area
.extent
.height
!= iview
->extent
.height
)
402 att_state
->fast_clear
= false;
404 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
405 if (GEN_GEN
<= 8 && !att_state
->clear_color_is_zero_one
)
406 att_state
->fast_clear
= false;
408 /* We only allow fast clears to the first slice of an image (level 0,
409 * layer 0) and only for the entire slice. This guarantees us that, at
410 * any given time, there is only one clear color on any given image at
411 * any given time. At the time of our testing (Jan 17, 2018), there
412 * were no known applications which would benefit from fast-clearing
413 * more than just the first slice.
415 if (att_state
->fast_clear
&&
416 (iview
->planes
[0].isl
.base_level
> 0 ||
417 iview
->planes
[0].isl
.base_array_layer
> 0)) {
418 anv_perf_warn(device
, iview
->image
,
419 "Rendering with multi-lod or multi-layer framebuffer "
420 "with LOAD_OP_LOAD and baseMipLevel > 0 or "
421 "baseArrayLayer > 0. Not fast clearing.");
422 att_state
->fast_clear
= false;
423 } else if (att_state
->fast_clear
&& cmd_state
->framebuffer
->layers
> 1) {
424 anv_perf_warn(device
, iview
->image
,
425 "Rendering to a multi-layer framebuffer with "
426 "LOAD_OP_CLEAR. Only fast-clearing the first slice");
429 if (att_state
->fast_clear
)
430 *fast_clear_color
= clear_color
;
432 att_state
->fast_clear
= false;
437 depth_stencil_attachment_compute_aux_usage(struct anv_device
*device
,
438 struct anv_cmd_state
*cmd_state
,
439 uint32_t att
, VkRect2D render_area
)
441 struct anv_render_pass_attachment
*pass_att
=
442 &cmd_state
->pass
->attachments
[att
];
443 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[att
];
444 struct anv_image_view
*iview
= cmd_state
->attachments
[att
].image_view
;
446 /* These will be initialized after the first subpass transition. */
447 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
448 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
450 /* This is unused for depth/stencil but valgrind complains if it
453 att_state
->clear_color_is_zero_one
= false;
456 /* We don't do any HiZ or depth fast-clears on gen7 yet */
457 att_state
->fast_clear
= false;
461 if (!(att_state
->pending_clear_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)) {
462 /* If we're just clearing stencil, we can always HiZ clear */
463 att_state
->fast_clear
= true;
467 /* Default to false for now */
468 att_state
->fast_clear
= false;
470 /* We must have depth in order to have HiZ */
471 if (!(iview
->image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
))
474 const enum isl_aux_usage first_subpass_aux_usage
=
475 anv_layout_to_aux_usage(&device
->info
, iview
->image
,
476 VK_IMAGE_ASPECT_DEPTH_BIT
,
477 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
,
478 pass_att
->first_subpass_layout
);
479 if (!blorp_can_hiz_clear_depth(&device
->info
,
480 &iview
->image
->planes
[0].surface
.isl
,
481 first_subpass_aux_usage
,
482 iview
->planes
[0].isl
.base_level
,
483 iview
->planes
[0].isl
.base_array_layer
,
484 render_area
.offset
.x
,
485 render_area
.offset
.y
,
486 render_area
.offset
.x
+
487 render_area
.extent
.width
,
488 render_area
.offset
.y
+
489 render_area
.extent
.height
))
492 if (att_state
->clear_value
.depthStencil
.depth
!= ANV_HZ_FC_VAL
)
495 if (GEN_GEN
== 8 && anv_can_sample_with_hiz(&device
->info
, iview
->image
)) {
496 /* Only gen9+ supports returning ANV_HZ_FC_VAL when sampling a
497 * fast-cleared portion of a HiZ buffer. Testing has revealed that Gen8
498 * only supports returning 0.0f. Gens prior to gen8 do not support this
504 /* If we got here, then we can fast clear */
505 att_state
->fast_clear
= true;
509 need_input_attachment_state(const struct anv_render_pass_attachment
*att
)
511 if (!(att
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
514 /* We only allocate input attachment states for color surfaces. Compression
515 * is not yet enabled for depth textures and stencil doesn't allow
516 * compression so we can just use the texture surface state from the view.
518 return vk_format_is_color(att
->format
);
521 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
522 * the initial layout is undefined, the HiZ buffer and depth buffer will
523 * represent the same data at the end of this operation.
526 transition_depth_buffer(struct anv_cmd_buffer
*cmd_buffer
,
527 const struct anv_image
*image
,
528 VkImageLayout initial_layout
,
529 VkImageLayout final_layout
)
531 uint32_t depth_plane
=
532 anv_image_aspect_to_plane(image
->aspects
, VK_IMAGE_ASPECT_DEPTH_BIT
);
533 if (image
->planes
[depth_plane
].aux_usage
== ISL_AUX_USAGE_NONE
)
536 const enum isl_aux_state initial_state
=
537 anv_layout_to_aux_state(&cmd_buffer
->device
->info
, image
,
538 VK_IMAGE_ASPECT_DEPTH_BIT
,
540 const enum isl_aux_state final_state
=
541 anv_layout_to_aux_state(&cmd_buffer
->device
->info
, image
,
542 VK_IMAGE_ASPECT_DEPTH_BIT
,
545 const bool initial_depth_valid
=
546 isl_aux_state_has_valid_primary(initial_state
);
547 const bool initial_hiz_valid
=
548 isl_aux_state_has_valid_aux(initial_state
);
549 const bool final_needs_depth
=
550 isl_aux_state_has_valid_primary(final_state
);
551 const bool final_needs_hiz
=
552 isl_aux_state_has_valid_aux(final_state
);
554 /* Getting into the pass-through state for Depth is tricky and involves
555 * both a resolve and an ambiguate. We don't handle that state right now
556 * as anv_layout_to_aux_state never returns it.
558 assert(final_state
!= ISL_AUX_STATE_PASS_THROUGH
);
560 if (final_needs_depth
&& !initial_depth_valid
) {
561 assert(initial_hiz_valid
);
562 anv_image_hiz_op(cmd_buffer
, image
, VK_IMAGE_ASPECT_DEPTH_BIT
,
563 0, 0, 1, ISL_AUX_OP_FULL_RESOLVE
);
564 } else if (final_needs_hiz
&& !initial_hiz_valid
) {
565 assert(initial_depth_valid
);
566 anv_image_hiz_op(cmd_buffer
, image
, VK_IMAGE_ASPECT_DEPTH_BIT
,
567 0, 0, 1, ISL_AUX_OP_AMBIGUATE
);
572 vk_image_layout_stencil_write_optimal(VkImageLayout layout
)
574 return layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
575 layout
== VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
||
576 layout
== VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR
;
579 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
580 * the initial layout is undefined, the HiZ buffer and depth buffer will
581 * represent the same data at the end of this operation.
584 transition_stencil_buffer(struct anv_cmd_buffer
*cmd_buffer
,
585 const struct anv_image
*image
,
586 uint32_t base_level
, uint32_t level_count
,
587 uint32_t base_layer
, uint32_t layer_count
,
588 VkImageLayout initial_layout
,
589 VkImageLayout final_layout
)
592 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
593 VK_IMAGE_ASPECT_STENCIL_BIT
);
595 /* On gen7, we have to store a texturable version of the stencil buffer in
596 * a shadow whenever VK_IMAGE_USAGE_SAMPLED_BIT is set and copy back and
597 * forth at strategic points. Stencil writes are only allowed in following
600 * - VK_IMAGE_LAYOUT_GENERAL
601 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
602 * - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
603 * - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
604 * - VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR
606 * For general, we have no nice opportunity to transition so we do the copy
607 * to the shadow unconditionally at the end of the subpass. For transfer
608 * destinations, we can update it as part of the transfer op. For the other
609 * layouts, we delay the copy until a transition into some other layout.
611 if (image
->planes
[plane
].shadow_surface
.isl
.size_B
> 0 &&
612 vk_image_layout_stencil_write_optimal(initial_layout
) &&
613 !vk_image_layout_stencil_write_optimal(final_layout
)) {
614 anv_image_copy_to_shadow(cmd_buffer
, image
,
615 VK_IMAGE_ASPECT_STENCIL_BIT
,
616 base_level
, level_count
,
617 base_layer
, layer_count
);
619 #endif /* GEN_GEN == 7 */
622 #define MI_PREDICATE_SRC0 0x2400
623 #define MI_PREDICATE_SRC1 0x2408
624 #define MI_PREDICATE_RESULT 0x2418
627 set_image_compressed_bit(struct anv_cmd_buffer
*cmd_buffer
,
628 const struct anv_image
*image
,
629 VkImageAspectFlagBits aspect
,
631 uint32_t base_layer
, uint32_t layer_count
,
634 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
636 /* We only have compression tracking for CCS_E */
637 if (image
->planes
[plane
].aux_usage
!= ISL_AUX_USAGE_CCS_E
)
640 for (uint32_t a
= 0; a
< layer_count
; a
++) {
641 uint32_t layer
= base_layer
+ a
;
642 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
643 sdi
.Address
= anv_image_get_compression_state_addr(cmd_buffer
->device
,
646 sdi
.ImmediateData
= compressed
? UINT32_MAX
: 0;
652 set_image_fast_clear_state(struct anv_cmd_buffer
*cmd_buffer
,
653 const struct anv_image
*image
,
654 VkImageAspectFlagBits aspect
,
655 enum anv_fast_clear_type fast_clear
)
657 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
658 sdi
.Address
= anv_image_get_fast_clear_type_addr(cmd_buffer
->device
,
660 sdi
.ImmediateData
= fast_clear
;
663 /* Whenever we have fast-clear, we consider that slice to be compressed.
664 * This makes building predicates much easier.
666 if (fast_clear
!= ANV_FAST_CLEAR_NONE
)
667 set_image_compressed_bit(cmd_buffer
, image
, aspect
, 0, 0, 1, true);
670 /* This is only really practical on haswell and above because it requires
671 * MI math in order to get it correct.
673 #if GEN_GEN >= 8 || GEN_IS_HASWELL
675 anv_cmd_compute_resolve_predicate(struct anv_cmd_buffer
*cmd_buffer
,
676 const struct anv_image
*image
,
677 VkImageAspectFlagBits aspect
,
678 uint32_t level
, uint32_t array_layer
,
679 enum isl_aux_op resolve_op
,
680 enum anv_fast_clear_type fast_clear_supported
)
682 struct gen_mi_builder b
;
683 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
685 const struct gen_mi_value fast_clear_type
=
686 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer
->device
,
689 if (resolve_op
== ISL_AUX_OP_FULL_RESOLVE
) {
690 /* In this case, we're doing a full resolve which means we want the
691 * resolve to happen if any compression (including fast-clears) is
694 * In order to simplify the logic a bit, we make the assumption that,
695 * if the first slice has been fast-cleared, it is also marked as
696 * compressed. See also set_image_fast_clear_state.
698 const struct gen_mi_value compression_state
=
699 gen_mi_mem32(anv_image_get_compression_state_addr(cmd_buffer
->device
,
701 level
, array_layer
));
702 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
),
704 gen_mi_store(&b
, compression_state
, gen_mi_imm(0));
706 if (level
== 0 && array_layer
== 0) {
707 /* If the predicate is true, we want to write 0 to the fast clear type
708 * and, if it's false, leave it alone. We can do this by writing
710 * clear_type = clear_type & ~predicate;
712 struct gen_mi_value new_fast_clear_type
=
713 gen_mi_iand(&b
, fast_clear_type
,
714 gen_mi_inot(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
)));
715 gen_mi_store(&b
, fast_clear_type
, new_fast_clear_type
);
717 } else if (level
== 0 && array_layer
== 0) {
718 /* In this case, we are doing a partial resolve to get rid of fast-clear
719 * colors. We don't care about the compression state but we do care
720 * about how much fast clear is allowed by the final layout.
722 assert(resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
);
723 assert(fast_clear_supported
< ANV_FAST_CLEAR_ANY
);
725 /* We need to compute (fast_clear_supported < image->fast_clear) */
726 struct gen_mi_value pred
=
727 gen_mi_ult(&b
, gen_mi_imm(fast_clear_supported
), fast_clear_type
);
728 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
),
729 gen_mi_value_ref(&b
, pred
));
731 /* If the predicate is true, we want to write 0 to the fast clear type
732 * and, if it's false, leave it alone. We can do this by writing
734 * clear_type = clear_type & ~predicate;
736 struct gen_mi_value new_fast_clear_type
=
737 gen_mi_iand(&b
, fast_clear_type
, gen_mi_inot(&b
, pred
));
738 gen_mi_store(&b
, fast_clear_type
, new_fast_clear_type
);
740 /* In this case, we're trying to do a partial resolve on a slice that
741 * doesn't have clear color. There's nothing to do.
743 assert(resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
);
747 /* Set src1 to 0 and use a != condition */
748 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
750 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
751 mip
.LoadOperation
= LOAD_LOADINV
;
752 mip
.CombineOperation
= COMBINE_SET
;
753 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
756 #endif /* GEN_GEN >= 8 || GEN_IS_HASWELL */
760 anv_cmd_simple_resolve_predicate(struct anv_cmd_buffer
*cmd_buffer
,
761 const struct anv_image
*image
,
762 VkImageAspectFlagBits aspect
,
763 uint32_t level
, uint32_t array_layer
,
764 enum isl_aux_op resolve_op
,
765 enum anv_fast_clear_type fast_clear_supported
)
767 struct gen_mi_builder b
;
768 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
770 struct gen_mi_value fast_clear_type_mem
=
771 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer
->device
,
774 /* This only works for partial resolves and only when the clear color is
775 * all or nothing. On the upside, this emits less command streamer code
776 * and works on Ivybridge and Bay Trail.
778 assert(resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
);
779 assert(fast_clear_supported
!= ANV_FAST_CLEAR_ANY
);
781 /* We don't support fast clears on anything other than the first slice. */
782 if (level
> 0 || array_layer
> 0)
785 /* On gen8, we don't have a concept of default clear colors because we
786 * can't sample from CCS surfaces. It's enough to just load the fast clear
787 * state into the predicate register.
789 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
), fast_clear_type_mem
);
790 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
791 gen_mi_store(&b
, fast_clear_type_mem
, gen_mi_imm(0));
793 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
794 mip
.LoadOperation
= LOAD_LOADINV
;
795 mip
.CombineOperation
= COMBINE_SET
;
796 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
799 #endif /* GEN_GEN <= 8 */
802 anv_cmd_predicated_ccs_resolve(struct anv_cmd_buffer
*cmd_buffer
,
803 const struct anv_image
*image
,
804 enum isl_format format
,
805 VkImageAspectFlagBits aspect
,
806 uint32_t level
, uint32_t array_layer
,
807 enum isl_aux_op resolve_op
,
808 enum anv_fast_clear_type fast_clear_supported
)
810 const uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
813 anv_cmd_compute_resolve_predicate(cmd_buffer
, image
,
814 aspect
, level
, array_layer
,
815 resolve_op
, fast_clear_supported
);
816 #else /* GEN_GEN <= 8 */
817 anv_cmd_simple_resolve_predicate(cmd_buffer
, image
,
818 aspect
, level
, array_layer
,
819 resolve_op
, fast_clear_supported
);
822 /* CCS_D only supports full resolves and BLORP will assert on us if we try
823 * to do a partial resolve on a CCS_D surface.
825 if (resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
&&
826 image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_CCS_D
)
827 resolve_op
= ISL_AUX_OP_FULL_RESOLVE
;
829 anv_image_ccs_op(cmd_buffer
, image
, format
, aspect
, level
,
830 array_layer
, 1, resolve_op
, NULL
, true);
834 anv_cmd_predicated_mcs_resolve(struct anv_cmd_buffer
*cmd_buffer
,
835 const struct anv_image
*image
,
836 enum isl_format format
,
837 VkImageAspectFlagBits aspect
,
838 uint32_t array_layer
,
839 enum isl_aux_op resolve_op
,
840 enum anv_fast_clear_type fast_clear_supported
)
842 assert(aspect
== VK_IMAGE_ASPECT_COLOR_BIT
);
843 assert(resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
);
845 #if GEN_GEN >= 8 || GEN_IS_HASWELL
846 anv_cmd_compute_resolve_predicate(cmd_buffer
, image
,
847 aspect
, 0, array_layer
,
848 resolve_op
, fast_clear_supported
);
850 anv_image_mcs_op(cmd_buffer
, image
, format
, aspect
,
851 array_layer
, 1, resolve_op
, NULL
, true);
853 unreachable("MCS resolves are unsupported on Ivybridge and Bay Trail");
858 genX(cmd_buffer_mark_image_written
)(struct anv_cmd_buffer
*cmd_buffer
,
859 const struct anv_image
*image
,
860 VkImageAspectFlagBits aspect
,
861 enum isl_aux_usage aux_usage
,
864 uint32_t layer_count
)
866 /* The aspect must be exactly one of the image aspects. */
867 assert(util_bitcount(aspect
) == 1 && (aspect
& image
->aspects
));
869 /* The only compression types with more than just fast-clears are MCS,
870 * CCS_E, and HiZ. With HiZ we just trust the layout and don't actually
871 * track the current fast-clear and compression state. This leaves us
872 * with just MCS and CCS_E.
874 if (aux_usage
!= ISL_AUX_USAGE_CCS_E
&&
875 aux_usage
!= ISL_AUX_USAGE_MCS
)
878 set_image_compressed_bit(cmd_buffer
, image
, aspect
,
879 level
, base_layer
, layer_count
, true);
883 init_fast_clear_color(struct anv_cmd_buffer
*cmd_buffer
,
884 const struct anv_image
*image
,
885 VkImageAspectFlagBits aspect
)
887 assert(cmd_buffer
&& image
);
888 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
890 set_image_fast_clear_state(cmd_buffer
, image
, aspect
,
891 ANV_FAST_CLEAR_NONE
);
893 /* Initialize the struct fields that are accessed for fast-clears so that
894 * the HW restrictions on the field values are satisfied.
896 struct anv_address addr
=
897 anv_image_get_clear_color_addr(cmd_buffer
->device
, image
, aspect
);
900 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
901 const unsigned num_dwords
= GEN_GEN
>= 10 ?
902 isl_dev
->ss
.clear_color_state_size
/ 4 :
903 isl_dev
->ss
.clear_value_size
/ 4;
904 for (unsigned i
= 0; i
< num_dwords
; i
++) {
905 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
907 sdi
.Address
.offset
+= i
* 4;
908 sdi
.ImmediateData
= 0;
912 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
914 if (GEN_GEN
>= 8 || GEN_IS_HASWELL
) {
915 /* Pre-SKL, the dword containing the clear values also contains
916 * other fields, so we need to initialize those fields to match the
917 * values that would be in a color attachment.
919 sdi
.ImmediateData
= ISL_CHANNEL_SELECT_RED
<< 25 |
920 ISL_CHANNEL_SELECT_GREEN
<< 22 |
921 ISL_CHANNEL_SELECT_BLUE
<< 19 |
922 ISL_CHANNEL_SELECT_ALPHA
<< 16;
923 } else if (GEN_GEN
== 7) {
924 /* On IVB, the dword containing the clear values also contains
925 * other fields that must be zero or can be zero.
927 sdi
.ImmediateData
= 0;
933 /* Copy the fast-clear value dword(s) between a surface state object and an
934 * image's fast clear state buffer.
937 genX(copy_fast_clear_dwords
)(struct anv_cmd_buffer
*cmd_buffer
,
938 struct anv_state surface_state
,
939 const struct anv_image
*image
,
940 VkImageAspectFlagBits aspect
,
941 bool copy_from_surface_state
)
943 assert(cmd_buffer
&& image
);
944 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
946 struct anv_address ss_clear_addr
= {
947 .bo
= cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
,
948 .offset
= surface_state
.offset
+
949 cmd_buffer
->device
->isl_dev
.ss
.clear_value_offset
,
951 const struct anv_address entry_addr
=
952 anv_image_get_clear_color_addr(cmd_buffer
->device
, image
, aspect
);
953 unsigned copy_size
= cmd_buffer
->device
->isl_dev
.ss
.clear_value_size
;
956 /* On gen7, the combination of commands used here(MI_LOAD_REGISTER_MEM
957 * and MI_STORE_REGISTER_MEM) can cause GPU hangs if any rendering is
958 * in-flight when they are issued even if the memory touched is not
959 * currently active for rendering. The weird bit is that it is not the
960 * MI_LOAD/STORE_REGISTER_MEM commands which hang but rather the in-flight
961 * rendering hangs such that the next stalling command after the
962 * MI_LOAD/STORE_REGISTER_MEM commands will catch the hang.
964 * It is unclear exactly why this hang occurs. Both MI commands come with
965 * warnings about the 3D pipeline but that doesn't seem to fully explain
966 * it. My (Jason's) best theory is that it has something to do with the
967 * fact that we're using a GPU state register as our temporary and that
968 * something with reading/writing it is causing problems.
970 * In order to work around this issue, we emit a PIPE_CONTROL with the
971 * command streamer stall bit set.
973 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
974 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
977 struct gen_mi_builder b
;
978 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
980 if (copy_from_surface_state
) {
981 gen_mi_memcpy(&b
, entry_addr
, ss_clear_addr
, copy_size
);
983 gen_mi_memcpy(&b
, ss_clear_addr
, entry_addr
, copy_size
);
985 /* Updating a surface state object may require that the state cache be
986 * invalidated. From the SKL PRM, Shared Functions -> State -> State
989 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
990 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
991 * modified [...], the L1 state cache must be invalidated to ensure
992 * the new surface or sampler state is fetched from system memory.
994 * In testing, SKL doesn't actually seem to need this, but HSW does.
996 cmd_buffer
->state
.pending_pipe_bits
|=
997 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
1001 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
1005 anv_image_init_aux_tt(struct anv_cmd_buffer
*cmd_buffer
,
1006 const struct anv_image
*image
,
1007 VkImageAspectFlagBits aspect
,
1008 uint32_t base_level
, uint32_t level_count
,
1009 uint32_t base_layer
, uint32_t layer_count
)
1011 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
1013 uint64_t base_address
=
1014 anv_address_physical(image
->planes
[plane
].address
);
1016 const struct isl_surf
*isl_surf
= &image
->planes
[plane
].surface
.isl
;
1017 uint64_t format_bits
= gen_aux_map_format_bits_for_isl_surf(isl_surf
);
1019 /* We're about to live-update the AUX-TT. We really don't want anyone else
1020 * trying to read it while we're doing this. We could probably get away
1021 * with not having this stall in some cases if we were really careful but
1022 * it's better to play it safe. Full stall the GPU.
1024 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
1025 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1027 struct gen_mi_builder b
;
1028 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
1030 for (uint32_t a
= 0; a
< layer_count
; a
++) {
1031 const uint32_t layer
= base_layer
+ a
;
1033 uint64_t start_offset_B
= UINT64_MAX
, end_offset_B
= 0;
1034 for (uint32_t l
= 0; l
< level_count
; l
++) {
1035 const uint32_t level
= base_level
+ l
;
1037 uint32_t logical_array_layer
, logical_z_offset_px
;
1038 if (image
->type
== VK_IMAGE_TYPE_3D
) {
1039 logical_array_layer
= 0;
1041 /* If the given miplevel does not have this layer, then any higher
1042 * miplevels won't either because miplevels only get smaller the
1045 assert(layer
< image
->extent
.depth
);
1046 if (layer
>= anv_minify(image
->extent
.depth
, level
))
1048 logical_z_offset_px
= layer
;
1050 assert(layer
< image
->array_size
);
1051 logical_array_layer
= layer
;
1052 logical_z_offset_px
= 0;
1055 uint32_t slice_start_offset_B
, slice_end_offset_B
;
1056 isl_surf_get_image_range_B_tile(isl_surf
, level
,
1057 logical_array_layer
,
1058 logical_z_offset_px
,
1059 &slice_start_offset_B
,
1060 &slice_end_offset_B
);
1062 start_offset_B
= MIN2(start_offset_B
, slice_start_offset_B
);
1063 end_offset_B
= MAX2(end_offset_B
, slice_end_offset_B
);
1066 /* Aux operates 64K at a time */
1067 start_offset_B
= align_down_u64(start_offset_B
, 64 * 1024);
1068 end_offset_B
= align_u64(end_offset_B
, 64 * 1024);
1070 for (uint64_t offset
= start_offset_B
;
1071 offset
< end_offset_B
; offset
+= 64 * 1024) {
1072 uint64_t address
= base_address
+ offset
;
1074 uint64_t aux_entry_addr64
, *aux_entry_map
;
1075 aux_entry_map
= gen_aux_map_get_entry(cmd_buffer
->device
->aux_map_ctx
,
1076 address
, &aux_entry_addr64
);
1078 assert(cmd_buffer
->device
->physical
->use_softpin
);
1079 struct anv_address aux_entry_address
= {
1081 .offset
= aux_entry_addr64
,
1084 const uint64_t old_aux_entry
= READ_ONCE(*aux_entry_map
);
1085 uint64_t new_aux_entry
=
1086 (old_aux_entry
& GEN_AUX_MAP_ADDRESS_MASK
) | format_bits
;
1088 if (isl_aux_usage_has_ccs(image
->planes
[plane
].aux_usage
))
1089 new_aux_entry
|= GEN_AUX_MAP_ENTRY_VALID_BIT
;
1091 gen_mi_store(&b
, gen_mi_mem64(aux_entry_address
),
1092 gen_mi_imm(new_aux_entry
));
1096 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_AUX_TABLE_INVALIDATE_BIT
;
1098 #endif /* GEN_GEN == 12 */
1101 * @brief Transitions a color buffer from one layout to another.
1103 * See section 6.1.1. Image Layout Transitions of the Vulkan 1.0.50 spec for
1106 * @param level_count VK_REMAINING_MIP_LEVELS isn't supported.
1107 * @param layer_count VK_REMAINING_ARRAY_LAYERS isn't supported. For 3D images,
1108 * this represents the maximum layers to transition at each
1109 * specified miplevel.
1112 transition_color_buffer(struct anv_cmd_buffer
*cmd_buffer
,
1113 const struct anv_image
*image
,
1114 VkImageAspectFlagBits aspect
,
1115 const uint32_t base_level
, uint32_t level_count
,
1116 uint32_t base_layer
, uint32_t layer_count
,
1117 VkImageLayout initial_layout
,
1118 VkImageLayout final_layout
)
1120 struct anv_device
*device
= cmd_buffer
->device
;
1121 const struct gen_device_info
*devinfo
= &device
->info
;
1122 /* Validate the inputs. */
1124 assert(image
&& image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
1125 /* These values aren't supported for simplicity's sake. */
1126 assert(level_count
!= VK_REMAINING_MIP_LEVELS
&&
1127 layer_count
!= VK_REMAINING_ARRAY_LAYERS
);
1128 /* Ensure the subresource range is valid. */
1129 UNUSED
uint64_t last_level_num
= base_level
+ level_count
;
1130 const uint32_t max_depth
= anv_minify(image
->extent
.depth
, base_level
);
1131 UNUSED
const uint32_t image_layers
= MAX2(image
->array_size
, max_depth
);
1132 assert((uint64_t)base_layer
+ layer_count
<= image_layers
);
1133 assert(last_level_num
<= image
->levels
);
1134 /* The spec disallows these final layouts. */
1135 assert(final_layout
!= VK_IMAGE_LAYOUT_UNDEFINED
&&
1136 final_layout
!= VK_IMAGE_LAYOUT_PREINITIALIZED
);
1138 /* No work is necessary if the layout stays the same or if this subresource
1139 * range lacks auxiliary data.
1141 if (initial_layout
== final_layout
)
1144 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
1146 if (image
->planes
[plane
].shadow_surface
.isl
.size_B
> 0 &&
1147 final_layout
== VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
) {
1148 /* This surface is a linear compressed image with a tiled shadow surface
1149 * for texturing. The client is about to use it in READ_ONLY_OPTIMAL so
1150 * we need to ensure the shadow copy is up-to-date.
1152 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
1153 assert(image
->planes
[plane
].surface
.isl
.tiling
== ISL_TILING_LINEAR
);
1154 assert(image
->planes
[plane
].shadow_surface
.isl
.tiling
!= ISL_TILING_LINEAR
);
1155 assert(isl_format_is_compressed(image
->planes
[plane
].surface
.isl
.format
));
1157 anv_image_copy_to_shadow(cmd_buffer
, image
,
1158 VK_IMAGE_ASPECT_COLOR_BIT
,
1159 base_level
, level_count
,
1160 base_layer
, layer_count
);
1163 if (base_layer
>= anv_image_aux_layers(image
, aspect
, base_level
))
1166 assert(image
->planes
[plane
].surface
.isl
.tiling
!= ISL_TILING_LINEAR
);
1168 if (initial_layout
== VK_IMAGE_LAYOUT_UNDEFINED
||
1169 initial_layout
== VK_IMAGE_LAYOUT_PREINITIALIZED
) {
1171 if (device
->physical
->has_implicit_ccs
&& devinfo
->has_aux_map
) {
1172 anv_image_init_aux_tt(cmd_buffer
, image
, aspect
,
1173 base_level
, level_count
,
1174 base_layer
, layer_count
);
1177 assert(!(device
->physical
->has_implicit_ccs
&& devinfo
->has_aux_map
));
1180 /* A subresource in the undefined layout may have been aliased and
1181 * populated with any arrangement of bits. Therefore, we must initialize
1182 * the related aux buffer and clear buffer entry with desirable values.
1183 * An initial layout of PREINITIALIZED is the same as UNDEFINED for
1184 * images with VK_IMAGE_TILING_OPTIMAL.
1186 * Initialize the relevant clear buffer entries.
1188 if (base_level
== 0 && base_layer
== 0)
1189 init_fast_clear_color(cmd_buffer
, image
, aspect
);
1191 /* Initialize the aux buffers to enable correct rendering. In order to
1192 * ensure that things such as storage images work correctly, aux buffers
1193 * need to be initialized to valid data.
1195 * Having an aux buffer with invalid data is a problem for two reasons:
1197 * 1) Having an invalid value in the buffer can confuse the hardware.
1198 * For instance, with CCS_E on SKL, a two-bit CCS value of 2 is
1199 * invalid and leads to the hardware doing strange things. It
1200 * doesn't hang as far as we can tell but rendering corruption can
1203 * 2) If this transition is into the GENERAL layout and we then use the
1204 * image as a storage image, then we must have the aux buffer in the
1205 * pass-through state so that, if we then go to texture from the
1206 * image, we get the results of our storage image writes and not the
1207 * fast clear color or other random data.
1209 * For CCS both of the problems above are real demonstrable issues. In
1210 * that case, the only thing we can do is to perform an ambiguate to
1211 * transition the aux surface into the pass-through state.
1213 * For MCS, (2) is never an issue because we don't support multisampled
1214 * storage images. In theory, issue (1) is a problem with MCS but we've
1215 * never seen it in the wild. For 4x and 16x, all bit patters could, in
1216 * theory, be interpreted as something but we don't know that all bit
1217 * patterns are actually valid. For 2x and 8x, you could easily end up
1218 * with the MCS referring to an invalid plane because not all bits of
1219 * the MCS value are actually used. Even though we've never seen issues
1220 * in the wild, it's best to play it safe and initialize the MCS. We
1221 * can use a fast-clear for MCS because we only ever touch from render
1222 * and texture (no image load store).
1224 if (image
->samples
== 1) {
1225 for (uint32_t l
= 0; l
< level_count
; l
++) {
1226 const uint32_t level
= base_level
+ l
;
1228 uint32_t aux_layers
= anv_image_aux_layers(image
, aspect
, level
);
1229 if (base_layer
>= aux_layers
)
1230 break; /* We will only get fewer layers as level increases */
1231 uint32_t level_layer_count
=
1232 MIN2(layer_count
, aux_layers
- base_layer
);
1234 anv_image_ccs_op(cmd_buffer
, image
,
1235 image
->planes
[plane
].surface
.isl
.format
,
1236 aspect
, level
, base_layer
, level_layer_count
,
1237 ISL_AUX_OP_AMBIGUATE
, NULL
, false);
1239 if (image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_CCS_E
) {
1240 set_image_compressed_bit(cmd_buffer
, image
, aspect
,
1241 level
, base_layer
, level_layer_count
,
1246 if (image
->samples
== 4 || image
->samples
== 16) {
1247 anv_perf_warn(cmd_buffer
->device
, image
,
1248 "Doing a potentially unnecessary fast-clear to "
1249 "define an MCS buffer.");
1252 assert(base_level
== 0 && level_count
== 1);
1253 anv_image_mcs_op(cmd_buffer
, image
,
1254 image
->planes
[plane
].surface
.isl
.format
,
1255 aspect
, base_layer
, layer_count
,
1256 ISL_AUX_OP_FAST_CLEAR
, NULL
, false);
1261 const enum isl_aux_usage initial_aux_usage
=
1262 anv_layout_to_aux_usage(devinfo
, image
, aspect
, 0, initial_layout
);
1263 const enum isl_aux_usage final_aux_usage
=
1264 anv_layout_to_aux_usage(devinfo
, image
, aspect
, 0, final_layout
);
1266 /* The current code assumes that there is no mixing of CCS_E and CCS_D.
1267 * We can handle transitions between CCS_D/E to and from NONE. What we
1268 * don't yet handle is switching between CCS_E and CCS_D within a given
1269 * image. Doing so in a performant way requires more detailed aux state
1270 * tracking such as what is done in i965. For now, just assume that we
1271 * only have one type of compression.
1273 assert(initial_aux_usage
== ISL_AUX_USAGE_NONE
||
1274 final_aux_usage
== ISL_AUX_USAGE_NONE
||
1275 initial_aux_usage
== final_aux_usage
);
1277 /* If initial aux usage is NONE, there is nothing to resolve */
1278 if (initial_aux_usage
== ISL_AUX_USAGE_NONE
)
1281 enum isl_aux_op resolve_op
= ISL_AUX_OP_NONE
;
1283 /* If the initial layout supports more fast clear than the final layout
1284 * then we need at least a partial resolve.
1286 const enum anv_fast_clear_type initial_fast_clear
=
1287 anv_layout_to_fast_clear_type(devinfo
, image
, aspect
, initial_layout
);
1288 const enum anv_fast_clear_type final_fast_clear
=
1289 anv_layout_to_fast_clear_type(devinfo
, image
, aspect
, final_layout
);
1290 if (final_fast_clear
< initial_fast_clear
)
1291 resolve_op
= ISL_AUX_OP_PARTIAL_RESOLVE
;
1293 if (initial_aux_usage
== ISL_AUX_USAGE_CCS_E
&&
1294 final_aux_usage
!= ISL_AUX_USAGE_CCS_E
)
1295 resolve_op
= ISL_AUX_OP_FULL_RESOLVE
;
1297 if (resolve_op
== ISL_AUX_OP_NONE
)
1300 /* Perform a resolve to synchronize data between the main and aux buffer.
1301 * Before we begin, we must satisfy the cache flushing requirement specified
1302 * in the Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)":
1304 * Any transition from any value in {Clear, Render, Resolve} to a
1305 * different value in {Clear, Render, Resolve} requires end of pipe
1308 * We perform a flush of the write cache before and after the clear and
1309 * resolve operations to meet this requirement.
1311 * Unlike other drawing, fast clear operations are not properly
1312 * synchronized. The first PIPE_CONTROL here likely ensures that the
1313 * contents of the previous render or clear hit the render target before we
1314 * resolve and the second likely ensures that the resolve is complete before
1315 * we do any more rendering or clearing.
1317 cmd_buffer
->state
.pending_pipe_bits
|=
1318 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
1320 for (uint32_t l
= 0; l
< level_count
; l
++) {
1321 uint32_t level
= base_level
+ l
;
1323 uint32_t aux_layers
= anv_image_aux_layers(image
, aspect
, level
);
1324 if (base_layer
>= aux_layers
)
1325 break; /* We will only get fewer layers as level increases */
1326 uint32_t level_layer_count
=
1327 MIN2(layer_count
, aux_layers
- base_layer
);
1329 for (uint32_t a
= 0; a
< level_layer_count
; a
++) {
1330 uint32_t array_layer
= base_layer
+ a
;
1331 if (image
->samples
== 1) {
1332 anv_cmd_predicated_ccs_resolve(cmd_buffer
, image
,
1333 image
->planes
[plane
].surface
.isl
.format
,
1334 aspect
, level
, array_layer
, resolve_op
,
1337 /* We only support fast-clear on the first layer so partial
1338 * resolves should not be used on other layers as they will use
1339 * the clear color stored in memory that is only valid for layer0.
1341 if (resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
&&
1345 anv_cmd_predicated_mcs_resolve(cmd_buffer
, image
,
1346 image
->planes
[plane
].surface
.isl
.format
,
1347 aspect
, array_layer
, resolve_op
,
1353 cmd_buffer
->state
.pending_pipe_bits
|=
1354 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
1358 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
1361 genX(cmd_buffer_setup_attachments
)(struct anv_cmd_buffer
*cmd_buffer
,
1362 struct anv_render_pass
*pass
,
1363 const VkRenderPassBeginInfo
*begin
)
1365 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
1366 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
1367 struct anv_framebuffer
*framebuffer
= cmd_buffer
->state
.framebuffer
;
1369 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
1371 if (pass
->attachment_count
> 0) {
1372 state
->attachments
= vk_alloc(&cmd_buffer
->pool
->alloc
,
1373 pass
->attachment_count
*
1374 sizeof(state
->attachments
[0]),
1375 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1376 if (state
->attachments
== NULL
) {
1377 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
1378 return anv_batch_set_error(&cmd_buffer
->batch
,
1379 VK_ERROR_OUT_OF_HOST_MEMORY
);
1382 state
->attachments
= NULL
;
1385 /* Reserve one for the NULL state. */
1386 unsigned num_states
= 1;
1387 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
1388 if (vk_format_is_color(pass
->attachments
[i
].format
))
1391 if (need_input_attachment_state(&pass
->attachments
[i
]))
1395 const uint32_t ss_stride
= align_u32(isl_dev
->ss
.size
, isl_dev
->ss
.align
);
1396 state
->render_pass_states
=
1397 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
1398 num_states
* ss_stride
, isl_dev
->ss
.align
);
1400 struct anv_state next_state
= state
->render_pass_states
;
1401 next_state
.alloc_size
= isl_dev
->ss
.size
;
1403 state
->null_surface_state
= next_state
;
1404 next_state
.offset
+= ss_stride
;
1405 next_state
.map
+= ss_stride
;
1407 const VkRenderPassAttachmentBeginInfoKHR
*begin_attachment
=
1408 vk_find_struct_const(begin
, RENDER_PASS_ATTACHMENT_BEGIN_INFO_KHR
);
1410 if (begin
&& !begin_attachment
)
1411 assert(pass
->attachment_count
== framebuffer
->attachment_count
);
1413 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
1414 if (vk_format_is_color(pass
->attachments
[i
].format
)) {
1415 state
->attachments
[i
].color
.state
= next_state
;
1416 next_state
.offset
+= ss_stride
;
1417 next_state
.map
+= ss_stride
;
1420 if (need_input_attachment_state(&pass
->attachments
[i
])) {
1421 state
->attachments
[i
].input
.state
= next_state
;
1422 next_state
.offset
+= ss_stride
;
1423 next_state
.map
+= ss_stride
;
1426 if (begin_attachment
&& begin_attachment
->attachmentCount
!= 0) {
1427 assert(begin_attachment
->attachmentCount
== pass
->attachment_count
);
1428 ANV_FROM_HANDLE(anv_image_view
, iview
, begin_attachment
->pAttachments
[i
]);
1429 cmd_buffer
->state
.attachments
[i
].image_view
= iview
;
1430 } else if (framebuffer
&& i
< framebuffer
->attachment_count
) {
1431 cmd_buffer
->state
.attachments
[i
].image_view
= framebuffer
->attachments
[i
];
1434 assert(next_state
.offset
== state
->render_pass_states
.offset
+
1435 state
->render_pass_states
.alloc_size
);
1438 isl_null_fill_state(isl_dev
, state
->null_surface_state
.map
,
1439 isl_extent3d(framebuffer
->width
,
1440 framebuffer
->height
,
1441 framebuffer
->layers
));
1443 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
1444 struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
1445 VkImageAspectFlags att_aspects
= vk_format_aspects(att
->format
);
1446 VkImageAspectFlags clear_aspects
= 0;
1447 VkImageAspectFlags load_aspects
= 0;
1449 if (att_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
1450 /* color attachment */
1451 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
1452 clear_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
1453 } else if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_LOAD
) {
1454 load_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
1457 /* depthstencil attachment */
1458 if (att_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
1459 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
1460 clear_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
1461 } else if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_LOAD
) {
1462 load_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
1465 if (att_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
1466 if (att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
1467 clear_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
1468 } else if (att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_LOAD
) {
1469 load_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
1474 state
->attachments
[i
].current_layout
= att
->initial_layout
;
1475 state
->attachments
[i
].current_stencil_layout
= att
->stencil_initial_layout
;
1476 state
->attachments
[i
].pending_clear_aspects
= clear_aspects
;
1477 state
->attachments
[i
].pending_load_aspects
= load_aspects
;
1479 state
->attachments
[i
].clear_value
= begin
->pClearValues
[i
];
1481 struct anv_image_view
*iview
= cmd_buffer
->state
.attachments
[i
].image_view
;
1482 anv_assert(iview
->vk_format
== att
->format
);
1484 const uint32_t num_layers
= iview
->planes
[0].isl
.array_len
;
1485 state
->attachments
[i
].pending_clear_views
= (1 << num_layers
) - 1;
1487 union isl_color_value clear_color
= { .u32
= { 0, } };
1488 if (att_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
1489 anv_assert(iview
->n_planes
== 1);
1490 assert(att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
1491 color_attachment_compute_aux_usage(cmd_buffer
->device
,
1492 state
, i
, begin
->renderArea
,
1495 anv_image_fill_surface_state(cmd_buffer
->device
,
1497 VK_IMAGE_ASPECT_COLOR_BIT
,
1498 &iview
->planes
[0].isl
,
1499 ISL_SURF_USAGE_RENDER_TARGET_BIT
,
1500 state
->attachments
[i
].aux_usage
,
1503 &state
->attachments
[i
].color
,
1506 add_surface_state_relocs(cmd_buffer
, state
->attachments
[i
].color
);
1508 depth_stencil_attachment_compute_aux_usage(cmd_buffer
->device
,
1513 if (need_input_attachment_state(&pass
->attachments
[i
])) {
1514 anv_image_fill_surface_state(cmd_buffer
->device
,
1516 VK_IMAGE_ASPECT_COLOR_BIT
,
1517 &iview
->planes
[0].isl
,
1518 ISL_SURF_USAGE_TEXTURE_BIT
,
1519 state
->attachments
[i
].input_aux_usage
,
1522 &state
->attachments
[i
].input
,
1525 add_surface_state_relocs(cmd_buffer
, state
->attachments
[i
].input
);
1534 genX(BeginCommandBuffer
)(
1535 VkCommandBuffer commandBuffer
,
1536 const VkCommandBufferBeginInfo
* pBeginInfo
)
1538 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1540 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
1541 * command buffer's state. Otherwise, we must *reset* its state. In both
1542 * cases we reset it.
1544 * From the Vulkan 1.0 spec:
1546 * If a command buffer is in the executable state and the command buffer
1547 * was allocated from a command pool with the
1548 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
1549 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
1550 * as if vkResetCommandBuffer had been called with
1551 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
1552 * the command buffer in the recording state.
1554 anv_cmd_buffer_reset(cmd_buffer
);
1556 cmd_buffer
->usage_flags
= pBeginInfo
->flags
;
1558 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
||
1559 !(cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
));
1561 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1563 /* We sometimes store vertex data in the dynamic state buffer for blorp
1564 * operations and our dynamic state stream may re-use data from previous
1565 * command buffers. In order to prevent stale cache data, we flush the VF
1566 * cache. We could do this on every blorp call but that's not really
1567 * needed as all of the data will get written by the CPU prior to the GPU
1568 * executing anything. The chances are fairly high that they will use
1569 * blorp at least once per primary command buffer so it shouldn't be
1572 * There is also a workaround on gen8 which requires us to invalidate the
1573 * VF cache occasionally. It's easier if we can assume we start with a
1574 * fresh cache (See also genX(cmd_buffer_set_binding_for_gen8_vb_flush).)
1576 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
1578 /* Re-emit the aux table register in every command buffer. This way we're
1579 * ensured that we have the table even if this command buffer doesn't
1580 * initialize any images.
1582 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_AUX_TABLE_INVALIDATE_BIT
;
1584 /* We send an "Indirect State Pointers Disable" packet at
1585 * EndCommandBuffer, so all push contant packets are ignored during a
1586 * context restore. Documentation says after that command, we need to
1587 * emit push constants again before any rendering operation. So we
1588 * flag them dirty here to make sure they get emitted.
1590 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
1592 VkResult result
= VK_SUCCESS
;
1593 if (cmd_buffer
->usage_flags
&
1594 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
1595 assert(pBeginInfo
->pInheritanceInfo
);
1596 cmd_buffer
->state
.pass
=
1597 anv_render_pass_from_handle(pBeginInfo
->pInheritanceInfo
->renderPass
);
1598 cmd_buffer
->state
.subpass
=
1599 &cmd_buffer
->state
.pass
->subpasses
[pBeginInfo
->pInheritanceInfo
->subpass
];
1601 /* This is optional in the inheritance info. */
1602 cmd_buffer
->state
.framebuffer
=
1603 anv_framebuffer_from_handle(pBeginInfo
->pInheritanceInfo
->framebuffer
);
1605 result
= genX(cmd_buffer_setup_attachments
)(cmd_buffer
,
1606 cmd_buffer
->state
.pass
, NULL
);
1608 /* Record that HiZ is enabled if we can. */
1609 if (cmd_buffer
->state
.framebuffer
) {
1610 const struct anv_image_view
* const iview
=
1611 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
1614 VkImageLayout layout
=
1615 cmd_buffer
->state
.subpass
->depth_stencil_attachment
->layout
;
1617 enum isl_aux_usage aux_usage
=
1618 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, iview
->image
,
1619 VK_IMAGE_ASPECT_DEPTH_BIT
,
1620 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
,
1623 cmd_buffer
->state
.hiz_enabled
= aux_usage
== ISL_AUX_USAGE_HIZ
;
1627 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
1630 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1631 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
) {
1632 const VkCommandBufferInheritanceConditionalRenderingInfoEXT
*conditional_rendering_info
=
1633 vk_find_struct_const(pBeginInfo
->pInheritanceInfo
->pNext
, COMMAND_BUFFER_INHERITANCE_CONDITIONAL_RENDERING_INFO_EXT
);
1635 /* If secondary buffer supports conditional rendering
1636 * we should emit commands as if conditional rendering is enabled.
1638 cmd_buffer
->state
.conditional_render_enabled
=
1639 conditional_rendering_info
&& conditional_rendering_info
->conditionalRenderingEnable
;
1646 /* From the PRM, Volume 2a:
1648 * "Indirect State Pointers Disable
1650 * At the completion of the post-sync operation associated with this pipe
1651 * control packet, the indirect state pointers in the hardware are
1652 * considered invalid; the indirect pointers are not saved in the context.
1653 * If any new indirect state commands are executed in the command stream
1654 * while the pipe control is pending, the new indirect state commands are
1657 * [DevIVB+]: Using Invalidate State Pointer (ISP) only inhibits context
1658 * restoring of Push Constant (3DSTATE_CONSTANT_*) commands. Push Constant
1659 * commands are only considered as Indirect State Pointers. Once ISP is
1660 * issued in a context, SW must initialize by programming push constant
1661 * commands for all the shaders (at least to zero length) before attempting
1662 * any rendering operation for the same context."
1664 * 3DSTATE_CONSTANT_* packets are restored during a context restore,
1665 * even though they point to a BO that has been already unreferenced at
1666 * the end of the previous batch buffer. This has been fine so far since
1667 * we are protected by these scratch page (every address not covered by
1668 * a BO should be pointing to the scratch page). But on CNL, it is
1669 * causing a GPU hang during context restore at the 3DSTATE_CONSTANT_*
1672 * The flag "Indirect State Pointers Disable" in PIPE_CONTROL tells the
1673 * hardware to ignore previous 3DSTATE_CONSTANT_* packets during a
1674 * context restore, so the mentioned hang doesn't happen. However,
1675 * software must program push constant commands for all stages prior to
1676 * rendering anything. So we flag them dirty in BeginCommandBuffer.
1678 * Finally, we also make sure to stall at pixel scoreboard to make sure the
1679 * constants have been loaded into the EUs prior to disable the push constants
1680 * so that it doesn't hang a previous 3DPRIMITIVE.
1683 emit_isp_disable(struct anv_cmd_buffer
*cmd_buffer
)
1685 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1686 pc
.StallAtPixelScoreboard
= true;
1687 pc
.CommandStreamerStallEnable
= true;
1689 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1690 pc
.IndirectStatePointersDisable
= true;
1691 pc
.CommandStreamerStallEnable
= true;
1696 genX(EndCommandBuffer
)(
1697 VkCommandBuffer commandBuffer
)
1699 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1701 if (anv_batch_has_error(&cmd_buffer
->batch
))
1702 return cmd_buffer
->batch
.status
;
1704 /* We want every command buffer to start with the PMA fix in a known state,
1705 * so we disable it at the end of the command buffer.
1707 genX(cmd_buffer_enable_pma_fix
)(cmd_buffer
, false);
1709 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1711 emit_isp_disable(cmd_buffer
);
1713 anv_cmd_buffer_end_batch_buffer(cmd_buffer
);
1719 genX(CmdExecuteCommands
)(
1720 VkCommandBuffer commandBuffer
,
1721 uint32_t commandBufferCount
,
1722 const VkCommandBuffer
* pCmdBuffers
)
1724 ANV_FROM_HANDLE(anv_cmd_buffer
, primary
, commandBuffer
);
1726 assert(primary
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1728 if (anv_batch_has_error(&primary
->batch
))
1731 /* The secondary command buffers will assume that the PMA fix is disabled
1732 * when they begin executing. Make sure this is true.
1734 genX(cmd_buffer_enable_pma_fix
)(primary
, false);
1736 /* The secondary command buffer doesn't know which textures etc. have been
1737 * flushed prior to their execution. Apply those flushes now.
1739 genX(cmd_buffer_apply_pipe_flushes
)(primary
);
1741 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
1742 ANV_FROM_HANDLE(anv_cmd_buffer
, secondary
, pCmdBuffers
[i
]);
1744 assert(secondary
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
1745 assert(!anv_batch_has_error(&secondary
->batch
));
1747 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1748 if (secondary
->state
.conditional_render_enabled
) {
1749 if (!primary
->state
.conditional_render_enabled
) {
1750 /* Secondary buffer is constructed as if it will be executed
1751 * with conditional rendering, we should satisfy this dependency
1752 * regardless of conditional rendering being enabled in primary.
1754 struct gen_mi_builder b
;
1755 gen_mi_builder_init(&b
, &primary
->batch
);
1756 gen_mi_store(&b
, gen_mi_reg64(ANV_PREDICATE_RESULT_REG
),
1757 gen_mi_imm(UINT64_MAX
));
1762 if (secondary
->usage_flags
&
1763 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
1764 /* If we're continuing a render pass from the primary, we need to
1765 * copy the surface states for the current subpass into the storage
1766 * we allocated for them in BeginCommandBuffer.
1768 struct anv_bo
*ss_bo
=
1769 primary
->device
->surface_state_pool
.block_pool
.bo
;
1770 struct anv_state src_state
= primary
->state
.render_pass_states
;
1771 struct anv_state dst_state
= secondary
->state
.render_pass_states
;
1772 assert(src_state
.alloc_size
== dst_state
.alloc_size
);
1774 genX(cmd_buffer_so_memcpy
)(primary
,
1775 (struct anv_address
) {
1777 .offset
= dst_state
.offset
,
1779 (struct anv_address
) {
1781 .offset
= src_state
.offset
,
1783 src_state
.alloc_size
);
1786 anv_cmd_buffer_add_secondary(primary
, secondary
);
1789 /* The secondary isn't counted in our VF cache tracking so we need to
1790 * invalidate the whole thing.
1792 if (GEN_GEN
>= 8 && GEN_GEN
<= 9) {
1793 primary
->state
.pending_pipe_bits
|=
1794 ANV_PIPE_CS_STALL_BIT
| ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
1797 /* The secondary may have selected a different pipeline (3D or compute) and
1798 * may have changed the current L3$ configuration. Reset our tracking
1799 * variables to invalid values to ensure that we re-emit these in the case
1800 * where we do any draws or compute dispatches from the primary after the
1801 * secondary has returned.
1803 primary
->state
.current_pipeline
= UINT32_MAX
;
1804 primary
->state
.current_l3_config
= NULL
;
1805 primary
->state
.current_hash_scale
= 0;
1807 /* Each of the secondary command buffers will use its own state base
1808 * address. We need to re-emit state base address for the primary after
1809 * all of the secondaries are done.
1811 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
1814 genX(cmd_buffer_emit_state_base_address
)(primary
);
1817 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
1818 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
1819 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
1822 * Program the hardware to use the specified L3 configuration.
1825 genX(cmd_buffer_config_l3
)(struct anv_cmd_buffer
*cmd_buffer
,
1826 const struct gen_l3_config
*cfg
)
1829 if (cfg
== cmd_buffer
->state
.current_l3_config
)
1832 if (unlikely(INTEL_DEBUG
& DEBUG_L3
)) {
1833 intel_logd("L3 config transition: ");
1834 gen_dump_l3_config(cfg
, stderr
);
1837 UNUSED
const bool has_slm
= cfg
->n
[GEN_L3P_SLM
];
1839 /* According to the hardware docs, the L3 partitioning can only be changed
1840 * while the pipeline is completely drained and the caches are flushed,
1841 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
1843 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1844 pc
.DCFlushEnable
= true;
1845 pc
.PostSyncOperation
= NoWrite
;
1846 pc
.CommandStreamerStallEnable
= true;
1849 /* ...followed by a second pipelined PIPE_CONTROL that initiates
1850 * invalidation of the relevant caches. Note that because RO invalidation
1851 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
1852 * command is processed by the CS) we cannot combine it with the previous
1853 * stalling flush as the hardware documentation suggests, because that
1854 * would cause the CS to stall on previous rendering *after* RO
1855 * invalidation and wouldn't prevent the RO caches from being polluted by
1856 * concurrent rendering before the stall completes. This intentionally
1857 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
1858 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
1859 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
1860 * already guarantee that there is no concurrent GPGPU kernel execution
1861 * (see SKL HSD 2132585).
1863 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1864 pc
.TextureCacheInvalidationEnable
= true;
1865 pc
.ConstantCacheInvalidationEnable
= true;
1866 pc
.InstructionCacheInvalidateEnable
= true;
1867 pc
.StateCacheInvalidationEnable
= true;
1868 pc
.PostSyncOperation
= NoWrite
;
1871 /* Now send a third stalling flush to make sure that invalidation is
1872 * complete when the L3 configuration registers are modified.
1874 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1875 pc
.DCFlushEnable
= true;
1876 pc
.PostSyncOperation
= NoWrite
;
1877 pc
.CommandStreamerStallEnable
= true;
1882 assert(!cfg
->n
[GEN_L3P_IS
] && !cfg
->n
[GEN_L3P_C
] && !cfg
->n
[GEN_L3P_T
]);
1885 #define L3_ALLOCATION_REG GENX(L3ALLOC)
1886 #define L3_ALLOCATION_REG_num GENX(L3ALLOC_num)
1888 #define L3_ALLOCATION_REG GENX(L3CNTLREG)
1889 #define L3_ALLOCATION_REG_num GENX(L3CNTLREG_num)
1893 anv_pack_struct(&l3cr
, L3_ALLOCATION_REG
,
1895 .SLMEnable
= has_slm
,
1898 /* WA_1406697149: Bit 9 "Error Detection Behavior Control" must be set
1899 * in L3CNTLREG register. The default setting of the bit is not the
1900 * desirable behavior.
1902 .ErrorDetectionBehaviorControl
= true,
1903 .UseFullWays
= true,
1905 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
1906 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
1907 .DCAllocation
= cfg
->n
[GEN_L3P_DC
],
1908 .AllAllocation
= cfg
->n
[GEN_L3P_ALL
]);
1910 /* Set up the L3 partitioning. */
1911 emit_lri(&cmd_buffer
->batch
, L3_ALLOCATION_REG_num
, l3cr
);
1915 const bool has_dc
= cfg
->n
[GEN_L3P_DC
] || cfg
->n
[GEN_L3P_ALL
];
1916 const bool has_is
= cfg
->n
[GEN_L3P_IS
] || cfg
->n
[GEN_L3P_RO
] ||
1917 cfg
->n
[GEN_L3P_ALL
];
1918 const bool has_c
= cfg
->n
[GEN_L3P_C
] || cfg
->n
[GEN_L3P_RO
] ||
1919 cfg
->n
[GEN_L3P_ALL
];
1920 const bool has_t
= cfg
->n
[GEN_L3P_T
] || cfg
->n
[GEN_L3P_RO
] ||
1921 cfg
->n
[GEN_L3P_ALL
];
1923 assert(!cfg
->n
[GEN_L3P_ALL
]);
1925 /* When enabled SLM only uses a portion of the L3 on half of the banks,
1926 * the matching space on the remaining banks has to be allocated to a
1927 * client (URB for all validated configurations) set to the
1928 * lower-bandwidth 2-bank address hashing mode.
1930 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
1931 const bool urb_low_bw
= has_slm
&& !devinfo
->is_baytrail
;
1932 assert(!urb_low_bw
|| cfg
->n
[GEN_L3P_URB
] == cfg
->n
[GEN_L3P_SLM
]);
1934 /* Minimum number of ways that can be allocated to the URB. */
1935 const unsigned n0_urb
= devinfo
->is_baytrail
? 32 : 0;
1936 assert(cfg
->n
[GEN_L3P_URB
] >= n0_urb
);
1938 uint32_t l3sqcr1
, l3cr2
, l3cr3
;
1939 anv_pack_struct(&l3sqcr1
, GENX(L3SQCREG1
),
1940 .ConvertDC_UC
= !has_dc
,
1941 .ConvertIS_UC
= !has_is
,
1942 .ConvertC_UC
= !has_c
,
1943 .ConvertT_UC
= !has_t
);
1945 GEN_IS_HASWELL
? HSW_L3SQCREG1_SQGHPCI_DEFAULT
:
1946 devinfo
->is_baytrail
? VLV_L3SQCREG1_SQGHPCI_DEFAULT
:
1947 IVB_L3SQCREG1_SQGHPCI_DEFAULT
;
1949 anv_pack_struct(&l3cr2
, GENX(L3CNTLREG2
),
1950 .SLMEnable
= has_slm
,
1951 .URBLowBandwidth
= urb_low_bw
,
1952 .URBAllocation
= cfg
->n
[GEN_L3P_URB
] - n0_urb
,
1954 .ALLAllocation
= cfg
->n
[GEN_L3P_ALL
],
1956 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
1957 .DCAllocation
= cfg
->n
[GEN_L3P_DC
]);
1959 anv_pack_struct(&l3cr3
, GENX(L3CNTLREG3
),
1960 .ISAllocation
= cfg
->n
[GEN_L3P_IS
],
1961 .ISLowBandwidth
= 0,
1962 .CAllocation
= cfg
->n
[GEN_L3P_C
],
1964 .TAllocation
= cfg
->n
[GEN_L3P_T
],
1965 .TLowBandwidth
= 0);
1967 /* Set up the L3 partitioning. */
1968 emit_lri(&cmd_buffer
->batch
, GENX(L3SQCREG1_num
), l3sqcr1
);
1969 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG2_num
), l3cr2
);
1970 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG3_num
), l3cr3
);
1973 if (cmd_buffer
->device
->physical
->cmd_parser_version
>= 4) {
1974 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
1975 * them disabled to avoid crashing the system hard.
1977 uint32_t scratch1
, chicken3
;
1978 anv_pack_struct(&scratch1
, GENX(SCRATCH1
),
1979 .L3AtomicDisable
= !has_dc
);
1980 anv_pack_struct(&chicken3
, GENX(CHICKEN3
),
1981 .L3AtomicDisableMask
= true,
1982 .L3AtomicDisable
= !has_dc
);
1983 emit_lri(&cmd_buffer
->batch
, GENX(SCRATCH1_num
), scratch1
);
1984 emit_lri(&cmd_buffer
->batch
, GENX(CHICKEN3_num
), chicken3
);
1990 cmd_buffer
->state
.current_l3_config
= cfg
;
1994 genX(cmd_buffer_apply_pipe_flushes
)(struct anv_cmd_buffer
*cmd_buffer
)
1996 enum anv_pipe_bits bits
= cmd_buffer
->state
.pending_pipe_bits
;
1998 if (cmd_buffer
->device
->physical
->always_flush_cache
)
1999 bits
|= ANV_PIPE_FLUSH_BITS
| ANV_PIPE_INVALIDATE_BITS
;
2001 /* Flushes are pipelined while invalidations are handled immediately.
2002 * Therefore, if we're flushing anything then we need to schedule a stall
2003 * before any invalidations can happen.
2005 if (bits
& ANV_PIPE_FLUSH_BITS
)
2006 bits
|= ANV_PIPE_NEEDS_CS_STALL_BIT
;
2008 /* If we're going to do an invalidate and we have a pending CS stall that
2009 * has yet to be resolved, we do the CS stall now.
2011 if ((bits
& ANV_PIPE_INVALIDATE_BITS
) &&
2012 (bits
& ANV_PIPE_NEEDS_CS_STALL_BIT
)) {
2013 bits
|= ANV_PIPE_CS_STALL_BIT
;
2014 bits
&= ~ANV_PIPE_NEEDS_CS_STALL_BIT
;
2017 if (GEN_GEN
>= 12 &&
2018 ((bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
) ||
2019 (bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
))) {
2020 /* From the PIPE_CONTROL instruction table, bit 28 (Tile Cache Flush
2023 * Unified Cache (Tile Cache Disabled):
2025 * When the Color and Depth (Z) streams are enabled to be cached in
2026 * the DC space of L2, Software must use "Render Target Cache Flush
2027 * Enable" and "Depth Cache Flush Enable" along with "Tile Cache
2028 * Flush" for getting the color and depth (Z) write data to be
2029 * globally observable. In this mode of operation it is not required
2030 * to set "CS Stall" upon setting "Tile Cache Flush" bit.
2032 bits
|= ANV_PIPE_TILE_CACHE_FLUSH_BIT
;
2035 /* GEN:BUG:1409226450, Wait for EU to be idle before pipe control which
2036 * invalidates the instruction cache
2038 if (GEN_GEN
== 12 && (bits
& ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT
))
2039 bits
|= ANV_PIPE_CS_STALL_BIT
| ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
2041 if ((GEN_GEN
>= 8 && GEN_GEN
<= 9) &&
2042 (bits
& ANV_PIPE_CS_STALL_BIT
) &&
2043 (bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
)) {
2044 /* If we are doing a VF cache invalidate AND a CS stall (it must be
2045 * both) then we can reset our vertex cache tracking.
2047 memset(cmd_buffer
->state
.gfx
.vb_dirty_ranges
, 0,
2048 sizeof(cmd_buffer
->state
.gfx
.vb_dirty_ranges
));
2049 memset(&cmd_buffer
->state
.gfx
.ib_dirty_range
, 0,
2050 sizeof(cmd_buffer
->state
.gfx
.ib_dirty_range
));
2053 if (bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
)) {
2054 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2056 pipe
.TileCacheFlushEnable
= bits
& ANV_PIPE_TILE_CACHE_FLUSH_BIT
;
2058 pipe
.DepthCacheFlushEnable
= bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
2059 pipe
.DCFlushEnable
= bits
& ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
2060 pipe
.RenderTargetCacheFlushEnable
=
2061 bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
2063 /* GEN:BUG:1409600907: "PIPE_CONTROL with Depth Stall Enable bit must
2064 * be set with any PIPE_CONTROL with Depth Flush Enable bit set.
2067 pipe
.DepthStallEnable
=
2068 pipe
.DepthCacheFlushEnable
|| (bits
& ANV_PIPE_DEPTH_STALL_BIT
);
2070 pipe
.DepthStallEnable
= bits
& ANV_PIPE_DEPTH_STALL_BIT
;
2073 pipe
.CommandStreamerStallEnable
= bits
& ANV_PIPE_CS_STALL_BIT
;
2074 pipe
.StallAtPixelScoreboard
= bits
& ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
2077 * According to the Broadwell documentation, any PIPE_CONTROL with the
2078 * "Command Streamer Stall" bit set must also have another bit set,
2079 * with five different options:
2081 * - Render Target Cache Flush
2082 * - Depth Cache Flush
2083 * - Stall at Pixel Scoreboard
2084 * - Post-Sync Operation
2088 * I chose "Stall at Pixel Scoreboard" since that's what we use in
2089 * mesa and it seems to work fine. The choice is fairly arbitrary.
2091 if ((bits
& ANV_PIPE_CS_STALL_BIT
) &&
2092 !(bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_DEPTH_STALL_BIT
|
2093 ANV_PIPE_STALL_AT_SCOREBOARD_BIT
)))
2094 pipe
.StallAtPixelScoreboard
= true;
2097 /* If a render target flush was emitted, then we can toggle off the bit
2098 * saying that render target writes are ongoing.
2100 if (bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
)
2101 bits
&= ~(ANV_PIPE_RENDER_TARGET_BUFFER_WRITES
);
2103 bits
&= ~(ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
);
2106 if (bits
& ANV_PIPE_INVALIDATE_BITS
) {
2107 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
2109 * "If the VF Cache Invalidation Enable is set to a 1 in a
2110 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields sets to
2111 * 0, with the VF Cache Invalidation Enable set to 0 needs to be sent
2112 * prior to the PIPE_CONTROL with VF Cache Invalidation Enable set to
2115 * This appears to hang Broadwell, so we restrict it to just gen9.
2117 if (GEN_GEN
== 9 && (bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
))
2118 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
);
2120 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2121 pipe
.StateCacheInvalidationEnable
=
2122 bits
& ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
2123 pipe
.ConstantCacheInvalidationEnable
=
2124 bits
& ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
2125 pipe
.VFCacheInvalidationEnable
=
2126 bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
2127 pipe
.TextureCacheInvalidationEnable
=
2128 bits
& ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
2129 pipe
.InstructionCacheInvalidateEnable
=
2130 bits
& ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT
;
2132 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
2134 * "When VF Cache Invalidate is set “Post Sync Operation” must be
2135 * enabled to “Write Immediate Data” or “Write PS Depth Count” or
2136 * “Write Timestamp”.
2138 if (GEN_GEN
== 9 && pipe
.VFCacheInvalidationEnable
) {
2139 pipe
.PostSyncOperation
= WriteImmediateData
;
2141 (struct anv_address
) { cmd_buffer
->device
->workaround_bo
, 0 };
2146 if ((bits
& ANV_PIPE_AUX_TABLE_INVALIDATE_BIT
) &&
2147 cmd_buffer
->device
->info
.has_aux_map
) {
2148 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
2149 lri
.RegisterOffset
= GENX(GFX_CCS_AUX_INV_num
);
2155 bits
&= ~ANV_PIPE_INVALIDATE_BITS
;
2158 cmd_buffer
->state
.pending_pipe_bits
= bits
;
2161 void genX(CmdPipelineBarrier
)(
2162 VkCommandBuffer commandBuffer
,
2163 VkPipelineStageFlags srcStageMask
,
2164 VkPipelineStageFlags destStageMask
,
2166 uint32_t memoryBarrierCount
,
2167 const VkMemoryBarrier
* pMemoryBarriers
,
2168 uint32_t bufferMemoryBarrierCount
,
2169 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
2170 uint32_t imageMemoryBarrierCount
,
2171 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
2173 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2175 /* XXX: Right now, we're really dumb and just flush whatever categories
2176 * the app asks for. One of these days we may make this a bit better
2177 * but right now that's all the hardware allows for in most areas.
2179 VkAccessFlags src_flags
= 0;
2180 VkAccessFlags dst_flags
= 0;
2182 for (uint32_t i
= 0; i
< memoryBarrierCount
; i
++) {
2183 src_flags
|= pMemoryBarriers
[i
].srcAccessMask
;
2184 dst_flags
|= pMemoryBarriers
[i
].dstAccessMask
;
2187 for (uint32_t i
= 0; i
< bufferMemoryBarrierCount
; i
++) {
2188 src_flags
|= pBufferMemoryBarriers
[i
].srcAccessMask
;
2189 dst_flags
|= pBufferMemoryBarriers
[i
].dstAccessMask
;
2192 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
2193 src_flags
|= pImageMemoryBarriers
[i
].srcAccessMask
;
2194 dst_flags
|= pImageMemoryBarriers
[i
].dstAccessMask
;
2195 ANV_FROM_HANDLE(anv_image
, image
, pImageMemoryBarriers
[i
].image
);
2196 const VkImageSubresourceRange
*range
=
2197 &pImageMemoryBarriers
[i
].subresourceRange
;
2199 uint32_t base_layer
, layer_count
;
2200 if (image
->type
== VK_IMAGE_TYPE_3D
) {
2202 layer_count
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
);
2204 base_layer
= range
->baseArrayLayer
;
2205 layer_count
= anv_get_layerCount(image
, range
);
2208 if (range
->aspectMask
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
2209 transition_depth_buffer(cmd_buffer
, image
,
2210 pImageMemoryBarriers
[i
].oldLayout
,
2211 pImageMemoryBarriers
[i
].newLayout
);
2214 if (range
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
2215 transition_stencil_buffer(cmd_buffer
, image
,
2216 range
->baseMipLevel
,
2217 anv_get_levelCount(image
, range
),
2218 base_layer
, layer_count
,
2219 pImageMemoryBarriers
[i
].oldLayout
,
2220 pImageMemoryBarriers
[i
].newLayout
);
2223 if (range
->aspectMask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
2224 VkImageAspectFlags color_aspects
=
2225 anv_image_expand_aspects(image
, range
->aspectMask
);
2226 uint32_t aspect_bit
;
2227 anv_foreach_image_aspect_bit(aspect_bit
, image
, color_aspects
) {
2228 transition_color_buffer(cmd_buffer
, image
, 1UL << aspect_bit
,
2229 range
->baseMipLevel
,
2230 anv_get_levelCount(image
, range
),
2231 base_layer
, layer_count
,
2232 pImageMemoryBarriers
[i
].oldLayout
,
2233 pImageMemoryBarriers
[i
].newLayout
);
2238 cmd_buffer
->state
.pending_pipe_bits
|=
2239 anv_pipe_flush_bits_for_access_flags(src_flags
) |
2240 anv_pipe_invalidate_bits_for_access_flags(dst_flags
);
2244 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
2246 VkShaderStageFlags stages
=
2247 cmd_buffer
->state
.gfx
.base
.pipeline
->active_stages
;
2249 /* In order to avoid thrash, we assume that vertex and fragment stages
2250 * always exist. In the rare case where one is missing *and* the other
2251 * uses push concstants, this may be suboptimal. However, avoiding stalls
2252 * seems more important.
2254 stages
|= VK_SHADER_STAGE_FRAGMENT_BIT
| VK_SHADER_STAGE_VERTEX_BIT
;
2256 if (stages
== cmd_buffer
->state
.push_constant_stages
)
2260 const unsigned push_constant_kb
= 32;
2261 #elif GEN_IS_HASWELL
2262 const unsigned push_constant_kb
= cmd_buffer
->device
->info
.gt
== 3 ? 32 : 16;
2264 const unsigned push_constant_kb
= 16;
2267 const unsigned num_stages
=
2268 util_bitcount(stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
2269 unsigned size_per_stage
= push_constant_kb
/ num_stages
;
2271 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
2272 * units of 2KB. Incidentally, these are the same platforms that have
2273 * 32KB worth of push constant space.
2275 if (push_constant_kb
== 32)
2276 size_per_stage
&= ~1u;
2278 uint32_t kb_used
= 0;
2279 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
2280 unsigned push_size
= (stages
& (1 << i
)) ? size_per_stage
: 0;
2281 anv_batch_emit(&cmd_buffer
->batch
,
2282 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS
), alloc
) {
2283 alloc
._3DCommandSubOpcode
= 18 + i
;
2284 alloc
.ConstantBufferOffset
= (push_size
> 0) ? kb_used
: 0;
2285 alloc
.ConstantBufferSize
= push_size
;
2287 kb_used
+= push_size
;
2290 anv_batch_emit(&cmd_buffer
->batch
,
2291 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS
), alloc
) {
2292 alloc
.ConstantBufferOffset
= kb_used
;
2293 alloc
.ConstantBufferSize
= push_constant_kb
- kb_used
;
2296 cmd_buffer
->state
.push_constant_stages
= stages
;
2298 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
2300 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
2301 * the next 3DPRIMITIVE command after programming the
2302 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
2304 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
2305 * pipeline setup, we need to dirty push constants.
2307 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
2310 static struct anv_address
2311 anv_descriptor_set_address(struct anv_cmd_buffer
*cmd_buffer
,
2312 struct anv_descriptor_set
*set
)
2315 /* This is a normal descriptor set */
2316 return (struct anv_address
) {
2317 .bo
= set
->pool
->bo
,
2318 .offset
= set
->desc_mem
.offset
,
2321 /* This is a push descriptor set. We have to flag it as used on the GPU
2322 * so that the next time we push descriptors, we grab a new memory.
2324 struct anv_push_descriptor_set
*push_set
=
2325 (struct anv_push_descriptor_set
*)set
;
2326 push_set
->set_used_on_gpu
= true;
2328 return (struct anv_address
) {
2329 .bo
= cmd_buffer
->dynamic_state_stream
.state_pool
->block_pool
.bo
,
2330 .offset
= set
->desc_mem
.offset
,
2336 emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
2337 gl_shader_stage stage
,
2338 struct anv_state
*bt_state
)
2340 struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
2341 struct anv_cmd_pipeline_state
*pipe_state
;
2342 struct anv_pipeline
*pipeline
;
2343 uint32_t state_offset
;
2346 case MESA_SHADER_COMPUTE
:
2347 pipe_state
= &cmd_buffer
->state
.compute
.base
;
2350 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
2353 pipeline
= pipe_state
->pipeline
;
2355 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
2356 *bt_state
= (struct anv_state
) { 0, };
2360 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
2361 if (map
->surface_count
== 0) {
2362 *bt_state
= (struct anv_state
) { 0, };
2366 *bt_state
= anv_cmd_buffer_alloc_binding_table(cmd_buffer
,
2369 uint32_t *bt_map
= bt_state
->map
;
2371 if (bt_state
->map
== NULL
)
2372 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2374 /* We only need to emit relocs if we're not using softpin. If we are using
2375 * softpin then we always keep all user-allocated memory objects resident.
2377 const bool need_client_mem_relocs
=
2378 !cmd_buffer
->device
->physical
->use_softpin
;
2380 for (uint32_t s
= 0; s
< map
->surface_count
; s
++) {
2381 struct anv_pipeline_binding
*binding
= &map
->surface_to_descriptor
[s
];
2383 struct anv_state surface_state
;
2385 switch (binding
->set
) {
2386 case ANV_DESCRIPTOR_SET_NULL
:
2390 case ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
:
2391 /* Color attachment binding */
2392 assert(stage
== MESA_SHADER_FRAGMENT
);
2393 if (binding
->index
< subpass
->color_count
) {
2394 const unsigned att
=
2395 subpass
->color_attachments
[binding
->index
].attachment
;
2397 /* From the Vulkan 1.0.46 spec:
2399 * "If any color or depth/stencil attachments are
2400 * VK_ATTACHMENT_UNUSED, then no writes occur for those
2403 if (att
== VK_ATTACHMENT_UNUSED
) {
2404 surface_state
= cmd_buffer
->state
.null_surface_state
;
2406 surface_state
= cmd_buffer
->state
.attachments
[att
].color
.state
;
2409 surface_state
= cmd_buffer
->state
.null_surface_state
;
2412 bt_map
[s
] = surface_state
.offset
+ state_offset
;
2415 case ANV_DESCRIPTOR_SET_SHADER_CONSTANTS
: {
2416 struct anv_state surface_state
=
2417 anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
2419 struct anv_address constant_data
= {
2420 .bo
= pipeline
->device
->dynamic_state_pool
.block_pool
.bo
,
2421 .offset
= pipeline
->shaders
[stage
]->constant_data
.offset
,
2423 unsigned constant_data_size
=
2424 pipeline
->shaders
[stage
]->constant_data_size
;
2426 const enum isl_format format
=
2427 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
);
2428 anv_fill_buffer_surface_state(cmd_buffer
->device
,
2429 surface_state
, format
,
2430 constant_data
, constant_data_size
, 1);
2432 bt_map
[s
] = surface_state
.offset
+ state_offset
;
2433 add_surface_reloc(cmd_buffer
, surface_state
, constant_data
);
2437 case ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS
: {
2438 /* This is always the first binding for compute shaders */
2439 assert(stage
== MESA_SHADER_COMPUTE
&& s
== 0);
2441 struct anv_state surface_state
=
2442 anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
2444 const enum isl_format format
=
2445 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
);
2446 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
2448 cmd_buffer
->state
.compute
.num_workgroups
,
2450 bt_map
[s
] = surface_state
.offset
+ state_offset
;
2451 if (need_client_mem_relocs
) {
2452 add_surface_reloc(cmd_buffer
, surface_state
,
2453 cmd_buffer
->state
.compute
.num_workgroups
);
2458 case ANV_DESCRIPTOR_SET_DESCRIPTORS
: {
2459 /* This is a descriptor set buffer so the set index is actually
2460 * given by binding->binding. (Yes, that's confusing.)
2462 struct anv_descriptor_set
*set
=
2463 pipe_state
->descriptors
[binding
->index
];
2464 assert(set
->desc_mem
.alloc_size
);
2465 assert(set
->desc_surface_state
.alloc_size
);
2466 bt_map
[s
] = set
->desc_surface_state
.offset
+ state_offset
;
2467 add_surface_reloc(cmd_buffer
, set
->desc_surface_state
,
2468 anv_descriptor_set_address(cmd_buffer
, set
));
2473 assert(binding
->set
< MAX_SETS
);
2474 const struct anv_descriptor
*desc
=
2475 &pipe_state
->descriptors
[binding
->set
]->descriptors
[binding
->index
];
2477 switch (desc
->type
) {
2478 case VK_DESCRIPTOR_TYPE_SAMPLER
:
2479 /* Nothing for us to do here */
2482 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
2483 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
: {
2484 struct anv_surface_state sstate
=
2485 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
2486 desc
->image_view
->planes
[binding
->plane
].general_sampler_surface_state
:
2487 desc
->image_view
->planes
[binding
->plane
].optimal_sampler_surface_state
;
2488 surface_state
= sstate
.state
;
2489 assert(surface_state
.alloc_size
);
2490 if (need_client_mem_relocs
)
2491 add_surface_state_relocs(cmd_buffer
, sstate
);
2494 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
2495 assert(stage
== MESA_SHADER_FRAGMENT
);
2496 if ((desc
->image_view
->aspect_mask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) == 0) {
2497 /* For depth and stencil input attachments, we treat it like any
2498 * old texture that a user may have bound.
2500 assert(desc
->image_view
->n_planes
== 1);
2501 struct anv_surface_state sstate
=
2502 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
2503 desc
->image_view
->planes
[0].general_sampler_surface_state
:
2504 desc
->image_view
->planes
[0].optimal_sampler_surface_state
;
2505 surface_state
= sstate
.state
;
2506 assert(surface_state
.alloc_size
);
2507 if (need_client_mem_relocs
)
2508 add_surface_state_relocs(cmd_buffer
, sstate
);
2510 /* For color input attachments, we create the surface state at
2511 * vkBeginRenderPass time so that we can include aux and clear
2512 * color information.
2514 assert(binding
->input_attachment_index
< subpass
->input_count
);
2515 const unsigned subpass_att
= binding
->input_attachment_index
;
2516 const unsigned att
= subpass
->input_attachments
[subpass_att
].attachment
;
2517 surface_state
= cmd_buffer
->state
.attachments
[att
].input
.state
;
2521 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
: {
2522 struct anv_surface_state sstate
= (binding
->write_only
)
2523 ? desc
->image_view
->planes
[binding
->plane
].writeonly_storage_surface_state
2524 : desc
->image_view
->planes
[binding
->plane
].storage_surface_state
;
2525 surface_state
= sstate
.state
;
2526 assert(surface_state
.alloc_size
);
2527 if (need_client_mem_relocs
)
2528 add_surface_state_relocs(cmd_buffer
, sstate
);
2532 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
2533 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
2534 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
2535 surface_state
= desc
->buffer_view
->surface_state
;
2536 assert(surface_state
.alloc_size
);
2537 if (need_client_mem_relocs
) {
2538 add_surface_reloc(cmd_buffer
, surface_state
,
2539 desc
->buffer_view
->address
);
2543 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
2544 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
2545 /* Compute the offset within the buffer */
2546 struct anv_push_constants
*push
=
2547 &cmd_buffer
->state
.push_constants
[stage
];
2549 uint32_t dynamic_offset
=
2550 push
->dynamic_offsets
[binding
->dynamic_offset_index
];
2551 uint64_t offset
= desc
->offset
+ dynamic_offset
;
2552 /* Clamp to the buffer size */
2553 offset
= MIN2(offset
, desc
->buffer
->size
);
2554 /* Clamp the range to the buffer size */
2555 uint32_t range
= MIN2(desc
->range
, desc
->buffer
->size
- offset
);
2557 struct anv_address address
=
2558 anv_address_add(desc
->buffer
->address
, offset
);
2561 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, 64, 64);
2562 enum isl_format format
=
2563 anv_isl_format_for_descriptor_type(desc
->type
);
2565 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
2566 format
, address
, range
, 1);
2567 if (need_client_mem_relocs
)
2568 add_surface_reloc(cmd_buffer
, surface_state
, address
);
2572 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
2573 surface_state
= (binding
->write_only
)
2574 ? desc
->buffer_view
->writeonly_storage_surface_state
2575 : desc
->buffer_view
->storage_surface_state
;
2576 assert(surface_state
.alloc_size
);
2577 if (need_client_mem_relocs
) {
2578 add_surface_reloc(cmd_buffer
, surface_state
,
2579 desc
->buffer_view
->address
);
2584 assert(!"Invalid descriptor type");
2587 bt_map
[s
] = surface_state
.offset
+ state_offset
;
2597 emit_samplers(struct anv_cmd_buffer
*cmd_buffer
,
2598 gl_shader_stage stage
,
2599 struct anv_state
*state
)
2601 struct anv_cmd_pipeline_state
*pipe_state
=
2602 stage
== MESA_SHADER_COMPUTE
? &cmd_buffer
->state
.compute
.base
:
2603 &cmd_buffer
->state
.gfx
.base
;
2604 struct anv_pipeline
*pipeline
= pipe_state
->pipeline
;
2606 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
2607 *state
= (struct anv_state
) { 0, };
2611 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
2612 if (map
->sampler_count
== 0) {
2613 *state
= (struct anv_state
) { 0, };
2617 uint32_t size
= map
->sampler_count
* 16;
2618 *state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 32);
2620 if (state
->map
== NULL
)
2621 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2623 for (uint32_t s
= 0; s
< map
->sampler_count
; s
++) {
2624 struct anv_pipeline_binding
*binding
= &map
->sampler_to_descriptor
[s
];
2625 const struct anv_descriptor
*desc
=
2626 &pipe_state
->descriptors
[binding
->set
]->descriptors
[binding
->index
];
2628 if (desc
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
2629 desc
->type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
2632 struct anv_sampler
*sampler
= desc
->sampler
;
2634 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
2635 * happens to be zero.
2637 if (sampler
== NULL
)
2640 memcpy(state
->map
+ (s
* 16),
2641 sampler
->state
[binding
->plane
], sizeof(sampler
->state
[0]));
2648 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
,
2649 struct anv_pipeline
*pipeline
)
2651 VkShaderStageFlags dirty
= cmd_buffer
->state
.descriptors_dirty
&
2652 pipeline
->active_stages
;
2654 VkResult result
= VK_SUCCESS
;
2655 anv_foreach_stage(s
, dirty
) {
2656 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
2657 if (result
!= VK_SUCCESS
)
2659 result
= emit_binding_table(cmd_buffer
, s
,
2660 &cmd_buffer
->state
.binding_tables
[s
]);
2661 if (result
!= VK_SUCCESS
)
2665 if (result
!= VK_SUCCESS
) {
2666 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2668 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
2669 if (result
!= VK_SUCCESS
)
2672 /* Re-emit state base addresses so we get the new surface state base
2673 * address before we start emitting binding tables etc.
2675 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
2677 /* Re-emit all active binding tables */
2678 dirty
|= pipeline
->active_stages
;
2679 anv_foreach_stage(s
, dirty
) {
2680 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
2681 if (result
!= VK_SUCCESS
) {
2682 anv_batch_set_error(&cmd_buffer
->batch
, result
);
2685 result
= emit_binding_table(cmd_buffer
, s
,
2686 &cmd_buffer
->state
.binding_tables
[s
]);
2687 if (result
!= VK_SUCCESS
) {
2688 anv_batch_set_error(&cmd_buffer
->batch
, result
);
2694 cmd_buffer
->state
.descriptors_dirty
&= ~dirty
;
2700 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
2703 static const uint32_t sampler_state_opcodes
[] = {
2704 [MESA_SHADER_VERTEX
] = 43,
2705 [MESA_SHADER_TESS_CTRL
] = 44, /* HS */
2706 [MESA_SHADER_TESS_EVAL
] = 45, /* DS */
2707 [MESA_SHADER_GEOMETRY
] = 46,
2708 [MESA_SHADER_FRAGMENT
] = 47,
2709 [MESA_SHADER_COMPUTE
] = 0,
2712 static const uint32_t binding_table_opcodes
[] = {
2713 [MESA_SHADER_VERTEX
] = 38,
2714 [MESA_SHADER_TESS_CTRL
] = 39,
2715 [MESA_SHADER_TESS_EVAL
] = 40,
2716 [MESA_SHADER_GEOMETRY
] = 41,
2717 [MESA_SHADER_FRAGMENT
] = 42,
2718 [MESA_SHADER_COMPUTE
] = 0,
2721 anv_foreach_stage(s
, stages
) {
2722 assert(s
< ARRAY_SIZE(binding_table_opcodes
));
2723 assert(binding_table_opcodes
[s
] > 0);
2725 if (cmd_buffer
->state
.samplers
[s
].alloc_size
> 0) {
2726 anv_batch_emit(&cmd_buffer
->batch
,
2727 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS
), ssp
) {
2728 ssp
._3DCommandSubOpcode
= sampler_state_opcodes
[s
];
2729 ssp
.PointertoVSSamplerState
= cmd_buffer
->state
.samplers
[s
].offset
;
2733 /* Always emit binding table pointers if we're asked to, since on SKL
2734 * this is what flushes push constants. */
2735 anv_batch_emit(&cmd_buffer
->batch
,
2736 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS
), btp
) {
2737 btp
._3DCommandSubOpcode
= binding_table_opcodes
[s
];
2738 btp
.PointertoVSBindingTable
= cmd_buffer
->state
.binding_tables
[s
].offset
;
2743 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2744 static struct anv_address
2745 get_push_range_address(struct anv_cmd_buffer
*cmd_buffer
,
2746 gl_shader_stage stage
,
2747 const struct anv_push_range
*range
)
2749 const struct anv_cmd_graphics_state
*gfx_state
= &cmd_buffer
->state
.gfx
;
2750 switch (range
->set
) {
2751 case ANV_DESCRIPTOR_SET_DESCRIPTORS
: {
2752 /* This is a descriptor set buffer so the set index is
2753 * actually given by binding->binding. (Yes, that's
2756 struct anv_descriptor_set
*set
=
2757 gfx_state
->base
.descriptors
[range
->index
];
2758 return anv_descriptor_set_address(cmd_buffer
, set
);
2762 case ANV_DESCRIPTOR_SET_PUSH_CONSTANTS
: {
2763 struct anv_state state
=
2764 anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
2765 return (struct anv_address
) {
2766 .bo
= cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
2767 .offset
= state
.offset
,
2773 assert(range
->set
< MAX_SETS
);
2774 struct anv_descriptor_set
*set
=
2775 gfx_state
->base
.descriptors
[range
->set
];
2776 const struct anv_descriptor
*desc
=
2777 &set
->descriptors
[range
->index
];
2779 if (desc
->type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
) {
2780 return desc
->buffer_view
->address
;
2782 assert(desc
->type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
);
2783 struct anv_push_constants
*push
=
2784 &cmd_buffer
->state
.push_constants
[stage
];
2785 uint32_t dynamic_offset
=
2786 push
->dynamic_offsets
[range
->dynamic_offset_index
];
2787 return anv_address_add(desc
->buffer
->address
,
2788 desc
->offset
+ dynamic_offset
);
2796 cmd_buffer_emit_push_constant(struct anv_cmd_buffer
*cmd_buffer
,
2797 gl_shader_stage stage
, unsigned buffer_count
)
2799 const struct anv_cmd_graphics_state
*gfx_state
= &cmd_buffer
->state
.gfx
;
2800 const struct anv_pipeline
*pipeline
= gfx_state
->base
.pipeline
;
2802 static const uint32_t push_constant_opcodes
[] = {
2803 [MESA_SHADER_VERTEX
] = 21,
2804 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
2805 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
2806 [MESA_SHADER_GEOMETRY
] = 22,
2807 [MESA_SHADER_FRAGMENT
] = 23,
2808 [MESA_SHADER_COMPUTE
] = 0,
2811 assert(stage
< ARRAY_SIZE(push_constant_opcodes
));
2812 assert(push_constant_opcodes
[stage
] > 0);
2814 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
) {
2815 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
];
2817 if (anv_pipeline_has_stage(pipeline
, stage
)) {
2818 const struct anv_pipeline_bind_map
*bind_map
=
2819 &pipeline
->shaders
[stage
]->bind_map
;
2821 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2822 /* The Skylake PRM contains the following restriction:
2824 * "The driver must ensure The following case does not occur
2825 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2826 * buffer 3 read length equal to zero committed followed by a
2827 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2830 * To avoid this, we program the buffers in the highest slots.
2831 * This way, slot 0 is only used if slot 3 is also used.
2833 assert(buffer_count
<= 4);
2834 const unsigned shift
= 4 - buffer_count
;
2835 for (unsigned i
= 0; i
< buffer_count
; i
++) {
2836 const struct anv_push_range
*range
= &bind_map
->push_ranges
[i
];
2838 /* At this point we only have non-empty ranges */
2839 assert(range
->length
> 0);
2841 /* For Ivy Bridge, make sure we only set the first range (actual
2844 assert((GEN_GEN
>= 8 || GEN_IS_HASWELL
) || i
== 0);
2846 const struct anv_address addr
=
2847 get_push_range_address(cmd_buffer
, stage
, range
);
2848 c
.ConstantBody
.ReadLength
[i
+ shift
] = range
->length
;
2849 c
.ConstantBody
.Buffer
[i
+ shift
] =
2850 anv_address_add(addr
, range
->start
* 32);
2853 /* For Ivy Bridge, push constants are relative to dynamic state
2854 * base address and we only ever push actual push constants.
2856 if (bind_map
->push_ranges
[0].length
> 0) {
2857 assert(bind_map
->push_ranges
[0].set
==
2858 ANV_DESCRIPTOR_SET_PUSH_CONSTANTS
);
2859 struct anv_state state
=
2860 anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
2861 c
.ConstantBody
.ReadLength
[0] = bind_map
->push_ranges
[0].length
;
2862 c
.ConstantBody
.Buffer
[0].bo
= NULL
;
2863 c
.ConstantBody
.Buffer
[0].offset
= state
.offset
;
2865 assert(bind_map
->push_ranges
[1].length
== 0);
2866 assert(bind_map
->push_ranges
[2].length
== 0);
2867 assert(bind_map
->push_ranges
[3].length
== 0);
2875 cmd_buffer_emit_push_constant_all(struct anv_cmd_buffer
*cmd_buffer
,
2876 uint32_t shader_mask
, uint32_t count
)
2879 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_ALL
), c
) {
2880 c
.ShaderUpdateEnable
= shader_mask
;
2885 const struct anv_cmd_graphics_state
*gfx_state
= &cmd_buffer
->state
.gfx
;
2886 const struct anv_pipeline
*pipeline
= gfx_state
->base
.pipeline
;
2888 static const uint32_t push_constant_opcodes
[] = {
2889 [MESA_SHADER_VERTEX
] = 21,
2890 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
2891 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
2892 [MESA_SHADER_GEOMETRY
] = 22,
2893 [MESA_SHADER_FRAGMENT
] = 23,
2894 [MESA_SHADER_COMPUTE
] = 0,
2897 gl_shader_stage stage
= vk_to_mesa_shader_stage(shader_mask
);
2898 assert(stage
< ARRAY_SIZE(push_constant_opcodes
));
2899 assert(push_constant_opcodes
[stage
] > 0);
2901 const struct anv_pipeline_bind_map
*bind_map
=
2902 &pipeline
->shaders
[stage
]->bind_map
;
2905 const uint32_t buffers
= (1 << count
) - 1;
2906 const uint32_t num_dwords
= 2 + 2 * count
;
2908 dw
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
2909 GENX(3DSTATE_CONSTANT_ALL
),
2910 .ShaderUpdateEnable
= shader_mask
,
2911 .PointerBufferMask
= buffers
);
2913 for (int i
= 0; i
< count
; i
++) {
2914 const struct anv_push_range
*range
= &bind_map
->push_ranges
[i
];
2915 const struct anv_address addr
=
2916 get_push_range_address(cmd_buffer
, stage
, range
);
2918 GENX(3DSTATE_CONSTANT_ALL_DATA_pack
)(
2919 &cmd_buffer
->batch
, dw
+ 2 + i
* 2,
2920 &(struct GENX(3DSTATE_CONSTANT_ALL_DATA
)) {
2921 .PointerToConstantBuffer
= anv_address_add(addr
, range
->start
* 32),
2922 .ConstantBufferReadLength
= range
->length
,
2929 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
2930 VkShaderStageFlags dirty_stages
)
2932 VkShaderStageFlags flushed
= 0;
2933 const struct anv_cmd_graphics_state
*gfx_state
= &cmd_buffer
->state
.gfx
;
2934 const struct anv_pipeline
*pipeline
= gfx_state
->base
.pipeline
;
2937 uint32_t nobuffer_stages
= 0;
2940 anv_foreach_stage(stage
, dirty_stages
) {
2941 unsigned buffer_count
= 0;
2942 flushed
|= mesa_to_vk_shader_stage(stage
);
2943 uint32_t max_push_range
= 0;
2945 if (anv_pipeline_has_stage(pipeline
, stage
)) {
2946 const struct anv_pipeline_bind_map
*bind_map
=
2947 &pipeline
->shaders
[stage
]->bind_map
;
2949 for (unsigned i
= 0; i
< 4; i
++) {
2950 const struct anv_push_range
*range
= &bind_map
->push_ranges
[i
];
2951 if (range
->length
> 0) {
2953 if (GEN_GEN
>= 12 && range
->length
> max_push_range
)
2954 max_push_range
= range
->length
;
2960 /* If this stage doesn't have any push constants, emit it later in a
2961 * single CONSTANT_ALL packet.
2963 if (buffer_count
== 0) {
2964 nobuffer_stages
|= 1 << stage
;
2968 /* The Constant Buffer Read Length field from 3DSTATE_CONSTANT_ALL
2969 * contains only 5 bits, so we can only use it for buffers smaller than
2972 if (max_push_range
< 32) {
2973 cmd_buffer_emit_push_constant_all(cmd_buffer
, 1 << stage
,
2979 cmd_buffer_emit_push_constant(cmd_buffer
, stage
, buffer_count
);
2983 if (nobuffer_stages
)
2984 cmd_buffer_emit_push_constant_all(cmd_buffer
, nobuffer_stages
, 0);
2987 cmd_buffer
->state
.push_constants_dirty
&= ~flushed
;
2991 genX(cmd_buffer_flush_state
)(struct anv_cmd_buffer
*cmd_buffer
)
2993 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
2996 uint32_t vb_emit
= cmd_buffer
->state
.gfx
.vb_dirty
& pipeline
->vb_used
;
2997 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_PIPELINE
)
2998 vb_emit
|= pipeline
->vb_used
;
3000 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
3002 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
3004 genX(cmd_buffer_emit_hashing_mode
)(cmd_buffer
, UINT_MAX
, UINT_MAX
, 1);
3006 genX(flush_pipeline_select_3d
)(cmd_buffer
);
3009 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
3010 const uint32_t num_dwords
= 1 + num_buffers
* 4;
3012 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
3013 GENX(3DSTATE_VERTEX_BUFFERS
));
3015 for_each_bit(vb
, vb_emit
) {
3016 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
3017 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
3019 struct GENX(VERTEX_BUFFER_STATE
) state
= {
3020 .VertexBufferIndex
= vb
,
3022 .MOCS
= anv_mocs_for_bo(cmd_buffer
->device
, buffer
->address
.bo
),
3024 .BufferAccessType
= pipeline
->vb
[vb
].instanced
? INSTANCEDATA
: VERTEXDATA
,
3025 .InstanceDataStepRate
= pipeline
->vb
[vb
].instance_divisor
,
3028 .AddressModifyEnable
= true,
3029 .BufferPitch
= pipeline
->vb
[vb
].stride
,
3030 .BufferStartingAddress
= anv_address_add(buffer
->address
, offset
),
3033 .BufferSize
= buffer
->size
- offset
3035 .EndAddress
= anv_address_add(buffer
->address
, buffer
->size
- 1),
3039 #if GEN_GEN >= 8 && GEN_GEN <= 9
3040 genX(cmd_buffer_set_binding_for_gen8_vb_flush
)(cmd_buffer
, vb
,
3041 state
.BufferStartingAddress
,
3045 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
3050 cmd_buffer
->state
.gfx
.vb_dirty
&= ~vb_emit
;
3053 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_XFB_ENABLE
) {
3054 /* We don't need any per-buffer dirty tracking because you're not
3055 * allowed to bind different XFB buffers while XFB is enabled.
3057 for (unsigned idx
= 0; idx
< MAX_XFB_BUFFERS
; idx
++) {
3058 struct anv_xfb_binding
*xfb
= &cmd_buffer
->state
.xfb_bindings
[idx
];
3059 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_SO_BUFFER
), sob
) {
3061 sob
.SOBufferIndex
= idx
;
3063 sob
._3DCommandOpcode
= 0;
3064 sob
._3DCommandSubOpcode
= SO_BUFFER_INDEX_0_CMD
+ idx
;
3067 if (cmd_buffer
->state
.xfb_enabled
&& xfb
->buffer
&& xfb
->size
!= 0) {
3068 sob
.SOBufferEnable
= true;
3069 sob
.MOCS
= cmd_buffer
->device
->isl_dev
.mocs
.internal
,
3070 sob
.StreamOffsetWriteEnable
= false;
3071 sob
.SurfaceBaseAddress
= anv_address_add(xfb
->buffer
->address
,
3073 /* Size is in DWords - 1 */
3074 sob
.SurfaceSize
= xfb
->size
/ 4 - 1;
3079 /* CNL and later require a CS stall after 3DSTATE_SO_BUFFER */
3081 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
3085 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
3086 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
3088 /* If the pipeline changed, we may need to re-allocate push constant
3091 cmd_buffer_alloc_push_constants(cmd_buffer
);
3095 if (cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_VERTEX_BIT
||
3096 cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_VERTEX_BIT
) {
3097 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
3099 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
3100 * stall needs to be sent just prior to any 3DSTATE_VS,
3101 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
3102 * 3DSTATE_BINDING_TABLE_POINTER_VS,
3103 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
3104 * PIPE_CONTROL needs to be sent before any combination of VS
3105 * associated 3DSTATE."
3107 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
3108 pc
.DepthStallEnable
= true;
3109 pc
.PostSyncOperation
= WriteImmediateData
;
3111 (struct anv_address
) { cmd_buffer
->device
->workaround_bo
, 0 };
3116 /* Render targets live in the same binding table as fragment descriptors */
3117 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_RENDER_TARGETS
)
3118 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
3120 /* We emit the binding tables and sampler tables first, then emit push
3121 * constants and then finally emit binding table and sampler table
3122 * pointers. It has to happen in this order, since emitting the binding
3123 * tables may change the push constants (in case of storage images). After
3124 * emitting push constants, on SKL+ we have to emit the corresponding
3125 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
3128 if (cmd_buffer
->state
.descriptors_dirty
)
3129 dirty
= flush_descriptor_sets(cmd_buffer
, pipeline
);
3131 if (dirty
|| cmd_buffer
->state
.push_constants_dirty
) {
3132 /* Because we're pushing UBOs, we have to push whenever either
3133 * descriptors or push constants is dirty.
3135 dirty
|= cmd_buffer
->state
.push_constants_dirty
;
3136 dirty
&= ANV_STAGE_MASK
& VK_SHADER_STAGE_ALL_GRAPHICS
;
3137 cmd_buffer_flush_push_constants(cmd_buffer
, dirty
);
3141 cmd_buffer_emit_descriptor_pointers(cmd_buffer
, dirty
);
3143 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
3144 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
3146 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
|
3147 ANV_CMD_DIRTY_PIPELINE
)) {
3148 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer
,
3149 pipeline
->depth_clamp_enable
);
3152 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_SCISSOR
|
3153 ANV_CMD_DIRTY_RENDER_TARGETS
))
3154 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
3156 genX(cmd_buffer_flush_dynamic_state
)(cmd_buffer
);
3160 emit_vertex_bo(struct anv_cmd_buffer
*cmd_buffer
,
3161 struct anv_address addr
,
3162 uint32_t size
, uint32_t index
)
3164 uint32_t *p
= anv_batch_emitn(&cmd_buffer
->batch
, 5,
3165 GENX(3DSTATE_VERTEX_BUFFERS
));
3167 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, p
+ 1,
3168 &(struct GENX(VERTEX_BUFFER_STATE
)) {
3169 .VertexBufferIndex
= index
,
3170 .AddressModifyEnable
= true,
3172 .MOCS
= addr
.bo
? anv_mocs_for_bo(cmd_buffer
->device
, addr
.bo
) : 0,
3173 .NullVertexBuffer
= size
== 0,
3175 .BufferStartingAddress
= addr
,
3178 .BufferStartingAddress
= addr
,
3179 .EndAddress
= anv_address_add(addr
, size
),
3183 genX(cmd_buffer_set_binding_for_gen8_vb_flush
)(cmd_buffer
,
3188 emit_base_vertex_instance_bo(struct anv_cmd_buffer
*cmd_buffer
,
3189 struct anv_address addr
)
3191 emit_vertex_bo(cmd_buffer
, addr
, addr
.bo
? 8 : 0, ANV_SVGS_VB_INDEX
);
3195 emit_base_vertex_instance(struct anv_cmd_buffer
*cmd_buffer
,
3196 uint32_t base_vertex
, uint32_t base_instance
)
3198 if (base_vertex
== 0 && base_instance
== 0) {
3199 emit_base_vertex_instance_bo(cmd_buffer
, ANV_NULL_ADDRESS
);
3201 struct anv_state id_state
=
3202 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 8, 4);
3204 ((uint32_t *)id_state
.map
)[0] = base_vertex
;
3205 ((uint32_t *)id_state
.map
)[1] = base_instance
;
3207 struct anv_address addr
= {
3208 .bo
= cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
3209 .offset
= id_state
.offset
,
3212 emit_base_vertex_instance_bo(cmd_buffer
, addr
);
3217 emit_draw_index(struct anv_cmd_buffer
*cmd_buffer
, uint32_t draw_index
)
3219 struct anv_state state
=
3220 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 4, 4);
3222 ((uint32_t *)state
.map
)[0] = draw_index
;
3224 struct anv_address addr
= {
3225 .bo
= cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
3226 .offset
= state
.offset
,
3229 emit_vertex_bo(cmd_buffer
, addr
, 4, ANV_DRAWID_VB_INDEX
);
3233 update_dirty_vbs_for_gen8_vb_flush(struct anv_cmd_buffer
*cmd_buffer
,
3234 uint32_t access_type
)
3236 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3237 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3239 uint64_t vb_used
= pipeline
->vb_used
;
3240 if (vs_prog_data
->uses_firstvertex
||
3241 vs_prog_data
->uses_baseinstance
)
3242 vb_used
|= 1ull << ANV_SVGS_VB_INDEX
;
3243 if (vs_prog_data
->uses_drawid
)
3244 vb_used
|= 1ull << ANV_DRAWID_VB_INDEX
;
3246 genX(cmd_buffer_update_dirty_vbs_for_gen8_vb_flush
)(cmd_buffer
,
3247 access_type
== RANDOM
,
3252 VkCommandBuffer commandBuffer
,
3253 uint32_t vertexCount
,
3254 uint32_t instanceCount
,
3255 uint32_t firstVertex
,
3256 uint32_t firstInstance
)
3258 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3259 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3260 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3262 if (anv_batch_has_error(&cmd_buffer
->batch
))
3265 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3267 if (cmd_buffer
->state
.conditional_render_enabled
)
3268 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
3270 if (vs_prog_data
->uses_firstvertex
||
3271 vs_prog_data
->uses_baseinstance
)
3272 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
3273 if (vs_prog_data
->uses_drawid
)
3274 emit_draw_index(cmd_buffer
, 0);
3276 /* Emitting draw index or vertex index BOs may result in needing
3277 * additional VF cache flushes.
3279 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3281 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3282 * different views. We need to multiply instanceCount by the view count.
3284 instanceCount
*= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
3286 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3287 prim
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
3288 prim
.VertexAccessType
= SEQUENTIAL
;
3289 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3290 prim
.VertexCountPerInstance
= vertexCount
;
3291 prim
.StartVertexLocation
= firstVertex
;
3292 prim
.InstanceCount
= instanceCount
;
3293 prim
.StartInstanceLocation
= firstInstance
;
3294 prim
.BaseVertexLocation
= 0;
3297 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer
, SEQUENTIAL
);
3300 void genX(CmdDrawIndexed
)(
3301 VkCommandBuffer commandBuffer
,
3302 uint32_t indexCount
,
3303 uint32_t instanceCount
,
3304 uint32_t firstIndex
,
3305 int32_t vertexOffset
,
3306 uint32_t firstInstance
)
3308 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3309 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3310 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3312 if (anv_batch_has_error(&cmd_buffer
->batch
))
3315 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3317 if (cmd_buffer
->state
.conditional_render_enabled
)
3318 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
3320 if (vs_prog_data
->uses_firstvertex
||
3321 vs_prog_data
->uses_baseinstance
)
3322 emit_base_vertex_instance(cmd_buffer
, vertexOffset
, firstInstance
);
3323 if (vs_prog_data
->uses_drawid
)
3324 emit_draw_index(cmd_buffer
, 0);
3326 /* Emitting draw index or vertex index BOs may result in needing
3327 * additional VF cache flushes.
3329 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3331 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3332 * different views. We need to multiply instanceCount by the view count.
3334 instanceCount
*= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
3336 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3337 prim
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
3338 prim
.VertexAccessType
= RANDOM
;
3339 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3340 prim
.VertexCountPerInstance
= indexCount
;
3341 prim
.StartVertexLocation
= firstIndex
;
3342 prim
.InstanceCount
= instanceCount
;
3343 prim
.StartInstanceLocation
= firstInstance
;
3344 prim
.BaseVertexLocation
= vertexOffset
;
3347 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer
, RANDOM
);
3350 /* Auto-Draw / Indirect Registers */
3351 #define GEN7_3DPRIM_END_OFFSET 0x2420
3352 #define GEN7_3DPRIM_START_VERTEX 0x2430
3353 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3354 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3355 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3356 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3358 void genX(CmdDrawIndirectByteCountEXT
)(
3359 VkCommandBuffer commandBuffer
,
3360 uint32_t instanceCount
,
3361 uint32_t firstInstance
,
3362 VkBuffer counterBuffer
,
3363 VkDeviceSize counterBufferOffset
,
3364 uint32_t counterOffset
,
3365 uint32_t vertexStride
)
3367 #if GEN_IS_HASWELL || GEN_GEN >= 8
3368 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3369 ANV_FROM_HANDLE(anv_buffer
, counter_buffer
, counterBuffer
);
3370 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3371 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3373 /* firstVertex is always zero for this draw function */
3374 const uint32_t firstVertex
= 0;
3376 if (anv_batch_has_error(&cmd_buffer
->batch
))
3379 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3381 if (vs_prog_data
->uses_firstvertex
||
3382 vs_prog_data
->uses_baseinstance
)
3383 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
3384 if (vs_prog_data
->uses_drawid
)
3385 emit_draw_index(cmd_buffer
, 0);
3387 /* Emitting draw index or vertex index BOs may result in needing
3388 * additional VF cache flushes.
3390 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3392 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3393 * different views. We need to multiply instanceCount by the view count.
3395 instanceCount
*= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
3397 struct gen_mi_builder b
;
3398 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3399 struct gen_mi_value count
=
3400 gen_mi_mem32(anv_address_add(counter_buffer
->address
,
3401 counterBufferOffset
));
3403 count
= gen_mi_isub(&b
, count
, gen_mi_imm(counterOffset
));
3404 count
= gen_mi_udiv32_imm(&b
, count
, vertexStride
);
3405 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT
), count
);
3407 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX
),
3408 gen_mi_imm(firstVertex
));
3409 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT
),
3410 gen_mi_imm(instanceCount
));
3411 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE
),
3412 gen_mi_imm(firstInstance
));
3413 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX
), gen_mi_imm(0));
3415 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3416 prim
.IndirectParameterEnable
= true;
3417 prim
.VertexAccessType
= SEQUENTIAL
;
3418 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3421 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer
, SEQUENTIAL
);
3422 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
3426 load_indirect_parameters(struct anv_cmd_buffer
*cmd_buffer
,
3427 struct anv_address addr
,
3430 struct gen_mi_builder b
;
3431 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3433 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT
),
3434 gen_mi_mem32(anv_address_add(addr
, 0)));
3436 struct gen_mi_value instance_count
= gen_mi_mem32(anv_address_add(addr
, 4));
3437 unsigned view_count
= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
3438 if (view_count
> 1) {
3439 #if GEN_IS_HASWELL || GEN_GEN >= 8
3440 instance_count
= gen_mi_imul_imm(&b
, instance_count
, view_count
);
3442 anv_finishme("Multiview + indirect draw requires MI_MATH; "
3443 "MI_MATH is not supported on Ivy Bridge");
3446 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT
), instance_count
);
3448 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX
),
3449 gen_mi_mem32(anv_address_add(addr
, 8)));
3452 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX
),
3453 gen_mi_mem32(anv_address_add(addr
, 12)));
3454 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE
),
3455 gen_mi_mem32(anv_address_add(addr
, 16)));
3457 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE
),
3458 gen_mi_mem32(anv_address_add(addr
, 12)));
3459 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX
), gen_mi_imm(0));
3463 void genX(CmdDrawIndirect
)(
3464 VkCommandBuffer commandBuffer
,
3466 VkDeviceSize offset
,
3470 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3471 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3472 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3473 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3475 if (anv_batch_has_error(&cmd_buffer
->batch
))
3478 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3480 if (cmd_buffer
->state
.conditional_render_enabled
)
3481 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
3483 for (uint32_t i
= 0; i
< drawCount
; i
++) {
3484 struct anv_address draw
= anv_address_add(buffer
->address
, offset
);
3486 if (vs_prog_data
->uses_firstvertex
||
3487 vs_prog_data
->uses_baseinstance
)
3488 emit_base_vertex_instance_bo(cmd_buffer
, anv_address_add(draw
, 8));
3489 if (vs_prog_data
->uses_drawid
)
3490 emit_draw_index(cmd_buffer
, i
);
3492 /* Emitting draw index or vertex index BOs may result in needing
3493 * additional VF cache flushes.
3495 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3497 load_indirect_parameters(cmd_buffer
, draw
, false);
3499 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3500 prim
.IndirectParameterEnable
= true;
3501 prim
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
3502 prim
.VertexAccessType
= SEQUENTIAL
;
3503 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3506 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer
, SEQUENTIAL
);
3512 void genX(CmdDrawIndexedIndirect
)(
3513 VkCommandBuffer commandBuffer
,
3515 VkDeviceSize offset
,
3519 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3520 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3521 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3522 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3524 if (anv_batch_has_error(&cmd_buffer
->batch
))
3527 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3529 if (cmd_buffer
->state
.conditional_render_enabled
)
3530 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
3532 for (uint32_t i
= 0; i
< drawCount
; i
++) {
3533 struct anv_address draw
= anv_address_add(buffer
->address
, offset
);
3535 /* TODO: We need to stomp base vertex to 0 somehow */
3536 if (vs_prog_data
->uses_firstvertex
||
3537 vs_prog_data
->uses_baseinstance
)
3538 emit_base_vertex_instance_bo(cmd_buffer
, anv_address_add(draw
, 12));
3539 if (vs_prog_data
->uses_drawid
)
3540 emit_draw_index(cmd_buffer
, i
);
3542 /* Emitting draw index or vertex index BOs may result in needing
3543 * additional VF cache flushes.
3545 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3547 load_indirect_parameters(cmd_buffer
, draw
, true);
3549 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3550 prim
.IndirectParameterEnable
= true;
3551 prim
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
3552 prim
.VertexAccessType
= RANDOM
;
3553 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3556 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer
, RANDOM
);
3562 #define TMP_DRAW_COUNT_REG 0x2670 /* MI_ALU_REG14 */
3565 prepare_for_draw_count_predicate(struct anv_cmd_buffer
*cmd_buffer
,
3566 struct anv_address count_address
,
3567 const bool conditional_render_enabled
)
3569 struct gen_mi_builder b
;
3570 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3572 if (conditional_render_enabled
) {
3573 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3574 gen_mi_store(&b
, gen_mi_reg64(TMP_DRAW_COUNT_REG
),
3575 gen_mi_mem32(count_address
));
3578 /* Upload the current draw count from the draw parameters buffer to
3579 * MI_PREDICATE_SRC0.
3581 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
),
3582 gen_mi_mem32(count_address
));
3584 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC1
+ 4), gen_mi_imm(0));
3589 emit_draw_count_predicate(struct anv_cmd_buffer
*cmd_buffer
,
3590 uint32_t draw_index
)
3592 struct gen_mi_builder b
;
3593 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3595 /* Upload the index of the current primitive to MI_PREDICATE_SRC1. */
3596 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC1
), gen_mi_imm(draw_index
));
3598 if (draw_index
== 0) {
3599 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
3600 mip
.LoadOperation
= LOAD_LOADINV
;
3601 mip
.CombineOperation
= COMBINE_SET
;
3602 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3605 /* While draw_index < draw_count the predicate's result will be
3606 * (draw_index == draw_count) ^ TRUE = TRUE
3607 * When draw_index == draw_count the result is
3608 * (TRUE) ^ TRUE = FALSE
3609 * After this all results will be:
3610 * (FALSE) ^ FALSE = FALSE
3612 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
3613 mip
.LoadOperation
= LOAD_LOAD
;
3614 mip
.CombineOperation
= COMBINE_XOR
;
3615 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3620 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3622 emit_draw_count_predicate_with_conditional_render(
3623 struct anv_cmd_buffer
*cmd_buffer
,
3624 uint32_t draw_index
)
3626 struct gen_mi_builder b
;
3627 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3629 struct gen_mi_value pred
= gen_mi_ult(&b
, gen_mi_imm(draw_index
),
3630 gen_mi_reg64(TMP_DRAW_COUNT_REG
));
3631 pred
= gen_mi_iand(&b
, pred
, gen_mi_reg64(ANV_PREDICATE_RESULT_REG
));
3634 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_RESULT
), pred
);
3636 /* MI_PREDICATE_RESULT is not whitelisted in i915 command parser
3637 * so we emit MI_PREDICATE to set it.
3640 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
), pred
);
3641 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
3643 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
3644 mip
.LoadOperation
= LOAD_LOADINV
;
3645 mip
.CombineOperation
= COMBINE_SET
;
3646 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3652 void genX(CmdDrawIndirectCount
)(
3653 VkCommandBuffer commandBuffer
,
3655 VkDeviceSize offset
,
3656 VkBuffer _countBuffer
,
3657 VkDeviceSize countBufferOffset
,
3658 uint32_t maxDrawCount
,
3661 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3662 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3663 ANV_FROM_HANDLE(anv_buffer
, count_buffer
, _countBuffer
);
3664 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
3665 struct anv_pipeline
*pipeline
= cmd_state
->gfx
.base
.pipeline
;
3666 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3668 if (anv_batch_has_error(&cmd_buffer
->batch
))
3671 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3673 struct anv_address count_address
=
3674 anv_address_add(count_buffer
->address
, countBufferOffset
);
3676 prepare_for_draw_count_predicate(cmd_buffer
, count_address
,
3677 cmd_state
->conditional_render_enabled
);
3679 for (uint32_t i
= 0; i
< maxDrawCount
; i
++) {
3680 struct anv_address draw
= anv_address_add(buffer
->address
, offset
);
3682 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3683 if (cmd_state
->conditional_render_enabled
) {
3684 emit_draw_count_predicate_with_conditional_render(cmd_buffer
, i
);
3686 emit_draw_count_predicate(cmd_buffer
, i
);
3689 emit_draw_count_predicate(cmd_buffer
, i
);
3692 if (vs_prog_data
->uses_firstvertex
||
3693 vs_prog_data
->uses_baseinstance
)
3694 emit_base_vertex_instance_bo(cmd_buffer
, anv_address_add(draw
, 8));
3695 if (vs_prog_data
->uses_drawid
)
3696 emit_draw_index(cmd_buffer
, i
);
3698 /* Emitting draw index or vertex index BOs may result in needing
3699 * additional VF cache flushes.
3701 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3703 load_indirect_parameters(cmd_buffer
, draw
, false);
3705 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3706 prim
.IndirectParameterEnable
= true;
3707 prim
.PredicateEnable
= true;
3708 prim
.VertexAccessType
= SEQUENTIAL
;
3709 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3712 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer
, SEQUENTIAL
);
3718 void genX(CmdDrawIndexedIndirectCount
)(
3719 VkCommandBuffer commandBuffer
,
3721 VkDeviceSize offset
,
3722 VkBuffer _countBuffer
,
3723 VkDeviceSize countBufferOffset
,
3724 uint32_t maxDrawCount
,
3727 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3728 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3729 ANV_FROM_HANDLE(anv_buffer
, count_buffer
, _countBuffer
);
3730 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
3731 struct anv_pipeline
*pipeline
= cmd_state
->gfx
.base
.pipeline
;
3732 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3734 if (anv_batch_has_error(&cmd_buffer
->batch
))
3737 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3739 struct anv_address count_address
=
3740 anv_address_add(count_buffer
->address
, countBufferOffset
);
3742 prepare_for_draw_count_predicate(cmd_buffer
, count_address
,
3743 cmd_state
->conditional_render_enabled
);
3745 for (uint32_t i
= 0; i
< maxDrawCount
; i
++) {
3746 struct anv_address draw
= anv_address_add(buffer
->address
, offset
);
3748 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3749 if (cmd_state
->conditional_render_enabled
) {
3750 emit_draw_count_predicate_with_conditional_render(cmd_buffer
, i
);
3752 emit_draw_count_predicate(cmd_buffer
, i
);
3755 emit_draw_count_predicate(cmd_buffer
, i
);
3758 /* TODO: We need to stomp base vertex to 0 somehow */
3759 if (vs_prog_data
->uses_firstvertex
||
3760 vs_prog_data
->uses_baseinstance
)
3761 emit_base_vertex_instance_bo(cmd_buffer
, anv_address_add(draw
, 12));
3762 if (vs_prog_data
->uses_drawid
)
3763 emit_draw_index(cmd_buffer
, i
);
3765 /* Emitting draw index or vertex index BOs may result in needing
3766 * additional VF cache flushes.
3768 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3770 load_indirect_parameters(cmd_buffer
, draw
, true);
3772 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3773 prim
.IndirectParameterEnable
= true;
3774 prim
.PredicateEnable
= true;
3775 prim
.VertexAccessType
= RANDOM
;
3776 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3779 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer
, RANDOM
);
3785 void genX(CmdBeginTransformFeedbackEXT
)(
3786 VkCommandBuffer commandBuffer
,
3787 uint32_t firstCounterBuffer
,
3788 uint32_t counterBufferCount
,
3789 const VkBuffer
* pCounterBuffers
,
3790 const VkDeviceSize
* pCounterBufferOffsets
)
3792 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3794 assert(firstCounterBuffer
< MAX_XFB_BUFFERS
);
3795 assert(counterBufferCount
<= MAX_XFB_BUFFERS
);
3796 assert(firstCounterBuffer
+ counterBufferCount
<= MAX_XFB_BUFFERS
);
3798 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3800 * "Ssoftware must ensure that no HW stream output operations can be in
3801 * process or otherwise pending at the point that the MI_LOAD/STORE
3802 * commands are processed. This will likely require a pipeline flush."
3804 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
3805 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3807 for (uint32_t idx
= 0; idx
< MAX_XFB_BUFFERS
; idx
++) {
3808 /* If we have a counter buffer, this is a resume so we need to load the
3809 * value into the streamout offset register. Otherwise, this is a begin
3810 * and we need to reset it to zero.
3812 if (pCounterBuffers
&&
3813 idx
>= firstCounterBuffer
&&
3814 idx
- firstCounterBuffer
< counterBufferCount
&&
3815 pCounterBuffers
[idx
- firstCounterBuffer
] != VK_NULL_HANDLE
) {
3816 uint32_t cb_idx
= idx
- firstCounterBuffer
;
3817 ANV_FROM_HANDLE(anv_buffer
, counter_buffer
, pCounterBuffers
[cb_idx
]);
3818 uint64_t offset
= pCounterBufferOffsets
?
3819 pCounterBufferOffsets
[cb_idx
] : 0;
3821 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
3822 lrm
.RegisterAddress
= GENX(SO_WRITE_OFFSET0_num
) + idx
* 4;
3823 lrm
.MemoryAddress
= anv_address_add(counter_buffer
->address
,
3827 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
3828 lri
.RegisterOffset
= GENX(SO_WRITE_OFFSET0_num
) + idx
* 4;
3834 cmd_buffer
->state
.xfb_enabled
= true;
3835 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_XFB_ENABLE
;
3838 void genX(CmdEndTransformFeedbackEXT
)(
3839 VkCommandBuffer commandBuffer
,
3840 uint32_t firstCounterBuffer
,
3841 uint32_t counterBufferCount
,
3842 const VkBuffer
* pCounterBuffers
,
3843 const VkDeviceSize
* pCounterBufferOffsets
)
3845 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3847 assert(firstCounterBuffer
< MAX_XFB_BUFFERS
);
3848 assert(counterBufferCount
<= MAX_XFB_BUFFERS
);
3849 assert(firstCounterBuffer
+ counterBufferCount
<= MAX_XFB_BUFFERS
);
3851 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3853 * "Ssoftware must ensure that no HW stream output operations can be in
3854 * process or otherwise pending at the point that the MI_LOAD/STORE
3855 * commands are processed. This will likely require a pipeline flush."
3857 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
3858 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3860 for (uint32_t cb_idx
= 0; cb_idx
< counterBufferCount
; cb_idx
++) {
3861 unsigned idx
= firstCounterBuffer
+ cb_idx
;
3863 /* If we have a counter buffer, this is a resume so we need to load the
3864 * value into the streamout offset register. Otherwise, this is a begin
3865 * and we need to reset it to zero.
3867 if (pCounterBuffers
&&
3868 cb_idx
< counterBufferCount
&&
3869 pCounterBuffers
[cb_idx
] != VK_NULL_HANDLE
) {
3870 ANV_FROM_HANDLE(anv_buffer
, counter_buffer
, pCounterBuffers
[cb_idx
]);
3871 uint64_t offset
= pCounterBufferOffsets
?
3872 pCounterBufferOffsets
[cb_idx
] : 0;
3874 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
3875 srm
.MemoryAddress
= anv_address_add(counter_buffer
->address
,
3877 srm
.RegisterAddress
= GENX(SO_WRITE_OFFSET0_num
) + idx
* 4;
3882 cmd_buffer
->state
.xfb_enabled
= false;
3883 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_XFB_ENABLE
;
3887 genX(cmd_buffer_flush_compute_state
)(struct anv_cmd_buffer
*cmd_buffer
)
3889 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
3891 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
3893 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
3895 genX(flush_pipeline_select_gpgpu
)(cmd_buffer
);
3897 if (cmd_buffer
->state
.compute
.pipeline_dirty
) {
3898 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
3900 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
3901 * the only bits that are changed are scoreboard related: Scoreboard
3902 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
3903 * these scoreboard related states, a MEDIA_STATE_FLUSH is
3906 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
3907 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3909 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
3911 /* The workgroup size of the pipeline affects our push constant layout
3912 * so flag push constants as dirty if we change the pipeline.
3914 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
3917 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
3918 cmd_buffer
->state
.compute
.pipeline_dirty
) {
3919 flush_descriptor_sets(cmd_buffer
, pipeline
);
3921 uint32_t iface_desc_data_dw
[GENX(INTERFACE_DESCRIPTOR_DATA_length
)];
3922 struct GENX(INTERFACE_DESCRIPTOR_DATA
) desc
= {
3923 .BindingTablePointer
=
3924 cmd_buffer
->state
.binding_tables
[MESA_SHADER_COMPUTE
].offset
,
3925 .SamplerStatePointer
=
3926 cmd_buffer
->state
.samplers
[MESA_SHADER_COMPUTE
].offset
,
3928 GENX(INTERFACE_DESCRIPTOR_DATA_pack
)(NULL
, iface_desc_data_dw
, &desc
);
3930 struct anv_state state
=
3931 anv_cmd_buffer_merge_dynamic(cmd_buffer
, iface_desc_data_dw
,
3932 pipeline
->interface_descriptor_data
,
3933 GENX(INTERFACE_DESCRIPTOR_DATA_length
),
3936 uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
3937 anv_batch_emit(&cmd_buffer
->batch
,
3938 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
), mid
) {
3939 mid
.InterfaceDescriptorTotalLength
= size
;
3940 mid
.InterfaceDescriptorDataStartAddress
= state
.offset
;
3944 if (cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) {
3945 struct anv_state push_state
=
3946 anv_cmd_buffer_cs_push_constants(cmd_buffer
);
3948 if (push_state
.alloc_size
) {
3949 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
), curbe
) {
3950 curbe
.CURBETotalDataLength
= push_state
.alloc_size
;
3951 curbe
.CURBEDataStartAddress
= push_state
.offset
;
3955 cmd_buffer
->state
.push_constants_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
3958 cmd_buffer
->state
.compute
.pipeline_dirty
= false;
3960 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3966 verify_cmd_parser(const struct anv_device
*device
,
3967 int required_version
,
3968 const char *function
)
3970 if (device
->physical
->cmd_parser_version
< required_version
) {
3971 return vk_errorf(device
, device
->physical
,
3972 VK_ERROR_FEATURE_NOT_PRESENT
,
3973 "cmd parser version %d is required for %s",
3974 required_version
, function
);
3983 anv_cmd_buffer_push_base_group_id(struct anv_cmd_buffer
*cmd_buffer
,
3984 uint32_t baseGroupX
,
3985 uint32_t baseGroupY
,
3986 uint32_t baseGroupZ
)
3988 if (anv_batch_has_error(&cmd_buffer
->batch
))
3991 struct anv_push_constants
*push
=
3992 &cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
3993 if (push
->cs
.base_work_group_id
[0] != baseGroupX
||
3994 push
->cs
.base_work_group_id
[1] != baseGroupY
||
3995 push
->cs
.base_work_group_id
[2] != baseGroupZ
) {
3996 push
->cs
.base_work_group_id
[0] = baseGroupX
;
3997 push
->cs
.base_work_group_id
[1] = baseGroupY
;
3998 push
->cs
.base_work_group_id
[2] = baseGroupZ
;
4000 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
4004 void genX(CmdDispatch
)(
4005 VkCommandBuffer commandBuffer
,
4010 genX(CmdDispatchBase
)(commandBuffer
, 0, 0, 0, x
, y
, z
);
4013 void genX(CmdDispatchBase
)(
4014 VkCommandBuffer commandBuffer
,
4015 uint32_t baseGroupX
,
4016 uint32_t baseGroupY
,
4017 uint32_t baseGroupZ
,
4018 uint32_t groupCountX
,
4019 uint32_t groupCountY
,
4020 uint32_t groupCountZ
)
4022 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4023 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
4024 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
4026 anv_cmd_buffer_push_base_group_id(cmd_buffer
, baseGroupX
,
4027 baseGroupY
, baseGroupZ
);
4029 if (anv_batch_has_error(&cmd_buffer
->batch
))
4032 if (prog_data
->uses_num_work_groups
) {
4033 struct anv_state state
=
4034 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 12, 4);
4035 uint32_t *sizes
= state
.map
;
4036 sizes
[0] = groupCountX
;
4037 sizes
[1] = groupCountY
;
4038 sizes
[2] = groupCountZ
;
4039 cmd_buffer
->state
.compute
.num_workgroups
= (struct anv_address
) {
4040 .bo
= cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
4041 .offset
= state
.offset
,
4044 /* The num_workgroups buffer goes in the binding table */
4045 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
4048 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
4050 if (cmd_buffer
->state
.conditional_render_enabled
)
4051 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
4053 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
), ggw
) {
4054 ggw
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
4055 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
4056 ggw
.ThreadDepthCounterMaximum
= 0;
4057 ggw
.ThreadHeightCounterMaximum
= 0;
4058 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
4059 ggw
.ThreadGroupIDXDimension
= groupCountX
;
4060 ggw
.ThreadGroupIDYDimension
= groupCountY
;
4061 ggw
.ThreadGroupIDZDimension
= groupCountZ
;
4062 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
4063 ggw
.BottomExecutionMask
= 0xffffffff;
4066 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
4069 #define GPGPU_DISPATCHDIMX 0x2500
4070 #define GPGPU_DISPATCHDIMY 0x2504
4071 #define GPGPU_DISPATCHDIMZ 0x2508
4073 void genX(CmdDispatchIndirect
)(
4074 VkCommandBuffer commandBuffer
,
4076 VkDeviceSize offset
)
4078 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4079 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
4080 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
4081 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
4082 struct anv_address addr
= anv_address_add(buffer
->address
, offset
);
4083 struct anv_batch
*batch
= &cmd_buffer
->batch
;
4085 anv_cmd_buffer_push_base_group_id(cmd_buffer
, 0, 0, 0);
4088 /* Linux 4.4 added command parser version 5 which allows the GPGPU
4089 * indirect dispatch registers to be written.
4091 if (verify_cmd_parser(cmd_buffer
->device
, 5,
4092 "vkCmdDispatchIndirect") != VK_SUCCESS
)
4096 if (prog_data
->uses_num_work_groups
) {
4097 cmd_buffer
->state
.compute
.num_workgroups
= addr
;
4099 /* The num_workgroups buffer goes in the binding table */
4100 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
4103 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
4105 struct gen_mi_builder b
;
4106 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
4108 struct gen_mi_value size_x
= gen_mi_mem32(anv_address_add(addr
, 0));
4109 struct gen_mi_value size_y
= gen_mi_mem32(anv_address_add(addr
, 4));
4110 struct gen_mi_value size_z
= gen_mi_mem32(anv_address_add(addr
, 8));
4112 gen_mi_store(&b
, gen_mi_reg32(GPGPU_DISPATCHDIMX
), size_x
);
4113 gen_mi_store(&b
, gen_mi_reg32(GPGPU_DISPATCHDIMY
), size_y
);
4114 gen_mi_store(&b
, gen_mi_reg32(GPGPU_DISPATCHDIMZ
), size_z
);
4117 /* predicate = (compute_dispatch_indirect_x_size == 0); */
4118 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
), size_x
);
4119 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
4120 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
4121 mip
.LoadOperation
= LOAD_LOAD
;
4122 mip
.CombineOperation
= COMBINE_SET
;
4123 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
4126 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
4127 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC0
), size_y
);
4128 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
4129 mip
.LoadOperation
= LOAD_LOAD
;
4130 mip
.CombineOperation
= COMBINE_OR
;
4131 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
4134 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
4135 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC0
), size_z
);
4136 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
4137 mip
.LoadOperation
= LOAD_LOAD
;
4138 mip
.CombineOperation
= COMBINE_OR
;
4139 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
4142 /* predicate = !predicate; */
4143 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
4144 mip
.LoadOperation
= LOAD_LOADINV
;
4145 mip
.CombineOperation
= COMBINE_OR
;
4146 mip
.CompareOperation
= COMPARE_FALSE
;
4150 if (cmd_buffer
->state
.conditional_render_enabled
) {
4151 /* predicate &= !(conditional_rendering_predicate == 0); */
4152 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC0
),
4153 gen_mi_reg32(ANV_PREDICATE_RESULT_REG
));
4154 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
4155 mip
.LoadOperation
= LOAD_LOADINV
;
4156 mip
.CombineOperation
= COMBINE_AND
;
4157 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
4162 #else /* GEN_GEN > 7 */
4163 if (cmd_buffer
->state
.conditional_render_enabled
)
4164 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
4167 anv_batch_emit(batch
, GENX(GPGPU_WALKER
), ggw
) {
4168 ggw
.IndirectParameterEnable
= true;
4169 ggw
.PredicateEnable
= GEN_GEN
<= 7 ||
4170 cmd_buffer
->state
.conditional_render_enabled
;
4171 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
4172 ggw
.ThreadDepthCounterMaximum
= 0;
4173 ggw
.ThreadHeightCounterMaximum
= 0;
4174 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
4175 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
4176 ggw
.BottomExecutionMask
= 0xffffffff;
4179 anv_batch_emit(batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
4183 genX(flush_pipeline_select
)(struct anv_cmd_buffer
*cmd_buffer
,
4186 UNUSED
const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
4188 if (cmd_buffer
->state
.current_pipeline
== pipeline
)
4191 #if GEN_GEN >= 8 && GEN_GEN < 10
4192 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
4194 * Software must clear the COLOR_CALC_STATE Valid field in
4195 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
4196 * with Pipeline Select set to GPGPU.
4198 * The internal hardware docs recommend the same workaround for Gen9
4201 if (pipeline
== GPGPU
)
4202 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), t
);
4206 if (pipeline
== _3D
) {
4207 /* There is a mid-object preemption workaround which requires you to
4208 * re-emit MEDIA_VFE_STATE after switching from GPGPU to 3D. However,
4209 * even without preemption, we have issues with geometry flickering when
4210 * GPGPU and 3D are back-to-back and this seems to fix it. We don't
4213 const uint32_t subslices
=
4214 MAX2(cmd_buffer
->device
->physical
->subslice_total
, 1);
4215 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_VFE_STATE
), vfe
) {
4216 vfe
.MaximumNumberofThreads
=
4217 devinfo
->max_cs_threads
* subslices
- 1;
4218 vfe
.NumberofURBEntries
= 2;
4219 vfe
.URBEntryAllocationSize
= 2;
4222 /* We just emitted a dummy MEDIA_VFE_STATE so now that packet is
4223 * invalid. Set the compute pipeline to dirty to force a re-emit of the
4224 * pipeline in case we get back-to-back dispatch calls with the same
4225 * pipeline and a PIPELINE_SELECT in between.
4227 cmd_buffer
->state
.compute
.pipeline_dirty
= true;
4231 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
4232 * PIPELINE_SELECT [DevBWR+]":
4236 * Software must ensure all the write caches are flushed through a
4237 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
4238 * command to invalidate read only caches prior to programming
4239 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
4241 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
4242 pc
.RenderTargetCacheFlushEnable
= true;
4243 pc
.DepthCacheFlushEnable
= true;
4244 pc
.DCFlushEnable
= true;
4245 pc
.PostSyncOperation
= NoWrite
;
4246 pc
.CommandStreamerStallEnable
= true;
4248 pc
.TileCacheFlushEnable
= true;
4250 /* GEN:BUG:1409600907: "PIPE_CONTROL with Depth Stall Enable bit must be
4251 * set with any PIPE_CONTROL with Depth Flush Enable bit set.
4253 pc
.DepthStallEnable
= true;
4257 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
4258 pc
.TextureCacheInvalidationEnable
= true;
4259 pc
.ConstantCacheInvalidationEnable
= true;
4260 pc
.StateCacheInvalidationEnable
= true;
4261 pc
.InstructionCacheInvalidateEnable
= true;
4262 pc
.PostSyncOperation
= NoWrite
;
4264 pc
.TileCacheFlushEnable
= true;
4268 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
4272 ps
.PipelineSelection
= pipeline
;
4276 if (devinfo
->is_geminilake
) {
4279 * "This chicken bit works around a hardware issue with barrier logic
4280 * encountered when switching between GPGPU and 3D pipelines. To
4281 * workaround the issue, this mode bit should be set after a pipeline
4285 anv_pack_struct(&scec
, GENX(SLICE_COMMON_ECO_CHICKEN1
),
4287 pipeline
== GPGPU
? GLK_BARRIER_MODE_GPGPU
4288 : GLK_BARRIER_MODE_3D_HULL
,
4289 .GLKBarrierModeMask
= 1);
4290 emit_lri(&cmd_buffer
->batch
, GENX(SLICE_COMMON_ECO_CHICKEN1_num
), scec
);
4294 cmd_buffer
->state
.current_pipeline
= pipeline
;
4298 genX(flush_pipeline_select_3d
)(struct anv_cmd_buffer
*cmd_buffer
)
4300 genX(flush_pipeline_select
)(cmd_buffer
, _3D
);
4304 genX(flush_pipeline_select_gpgpu
)(struct anv_cmd_buffer
*cmd_buffer
)
4306 genX(flush_pipeline_select
)(cmd_buffer
, GPGPU
);
4310 genX(cmd_buffer_emit_gen7_depth_flush
)(struct anv_cmd_buffer
*cmd_buffer
)
4315 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
4317 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
4318 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
4319 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
4320 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
4321 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
4322 * Depth Flush Bit set, followed by another pipelined depth stall
4323 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
4324 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
4325 * via a preceding MI_FLUSH)."
4327 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
4328 pipe
.DepthStallEnable
= true;
4330 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
4331 pipe
.DepthCacheFlushEnable
= true;
4333 pipe
.TileCacheFlushEnable
= true;
4336 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
4337 pipe
.DepthStallEnable
= true;
4341 /* From the Skylake PRM, 3DSTATE_VERTEX_BUFFERS:
4343 * "The VF cache needs to be invalidated before binding and then using
4344 * Vertex Buffers that overlap with any previously bound Vertex Buffer
4345 * (at a 64B granularity) since the last invalidation. A VF cache
4346 * invalidate is performed by setting the "VF Cache Invalidation Enable"
4347 * bit in PIPE_CONTROL."
4349 * This is implemented by carefully tracking all vertex and index buffer
4350 * bindings and flushing if the cache ever ends up with a range in the cache
4351 * that would exceed 4 GiB. This is implemented in three parts:
4353 * 1. genX(cmd_buffer_set_binding_for_gen8_vb_flush)() which must be called
4354 * every time a 3DSTATE_VERTEX_BUFFER packet is emitted and informs the
4355 * tracking code of the new binding. If this new binding would cause
4356 * the cache to have a too-large range on the next draw call, a pipeline
4357 * stall and VF cache invalidate are added to pending_pipeline_bits.
4359 * 2. genX(cmd_buffer_apply_pipe_flushes)() resets the cache tracking to
4360 * empty whenever we emit a VF invalidate.
4362 * 3. genX(cmd_buffer_update_dirty_vbs_for_gen8_vb_flush)() must be called
4363 * after every 3DPRIMITIVE and copies the bound range into the dirty
4364 * range for each used buffer. This has to be a separate step because
4365 * we don't always re-bind all buffers and so 1. can't know which
4366 * buffers are actually bound.
4369 genX(cmd_buffer_set_binding_for_gen8_vb_flush
)(struct anv_cmd_buffer
*cmd_buffer
,
4371 struct anv_address vb_address
,
4374 if (GEN_GEN
< 8 || GEN_GEN
> 9 ||
4375 !cmd_buffer
->device
->physical
->use_softpin
)
4378 struct anv_vb_cache_range
*bound
, *dirty
;
4379 if (vb_index
== -1) {
4380 bound
= &cmd_buffer
->state
.gfx
.ib_bound_range
;
4381 dirty
= &cmd_buffer
->state
.gfx
.ib_dirty_range
;
4383 assert(vb_index
>= 0);
4384 assert(vb_index
< ARRAY_SIZE(cmd_buffer
->state
.gfx
.vb_bound_ranges
));
4385 assert(vb_index
< ARRAY_SIZE(cmd_buffer
->state
.gfx
.vb_dirty_ranges
));
4386 bound
= &cmd_buffer
->state
.gfx
.vb_bound_ranges
[vb_index
];
4387 dirty
= &cmd_buffer
->state
.gfx
.vb_dirty_ranges
[vb_index
];
4396 assert(vb_address
.bo
&& (vb_address
.bo
->flags
& EXEC_OBJECT_PINNED
));
4397 bound
->start
= gen_48b_address(anv_address_physical(vb_address
));
4398 bound
->end
= bound
->start
+ vb_size
;
4399 assert(bound
->end
> bound
->start
); /* No overflow */
4401 /* Align everything to a cache line */
4402 bound
->start
&= ~(64ull - 1ull);
4403 bound
->end
= align_u64(bound
->end
, 64);
4405 /* Compute the dirty range */
4406 dirty
->start
= MIN2(dirty
->start
, bound
->start
);
4407 dirty
->end
= MAX2(dirty
->end
, bound
->end
);
4409 /* If our range is larger than 32 bits, we have to flush */
4410 assert(bound
->end
- bound
->start
<= (1ull << 32));
4411 if (dirty
->end
- dirty
->start
> (1ull << 32)) {
4412 cmd_buffer
->state
.pending_pipe_bits
|=
4413 ANV_PIPE_CS_STALL_BIT
| ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
4418 genX(cmd_buffer_update_dirty_vbs_for_gen8_vb_flush
)(struct anv_cmd_buffer
*cmd_buffer
,
4419 uint32_t access_type
,
4422 if (GEN_GEN
< 8 || GEN_GEN
> 9 ||
4423 !cmd_buffer
->device
->physical
->use_softpin
)
4426 if (access_type
== RANDOM
) {
4427 /* We have an index buffer */
4428 struct anv_vb_cache_range
*bound
= &cmd_buffer
->state
.gfx
.ib_bound_range
;
4429 struct anv_vb_cache_range
*dirty
= &cmd_buffer
->state
.gfx
.ib_dirty_range
;
4431 if (bound
->end
> bound
->start
) {
4432 dirty
->start
= MIN2(dirty
->start
, bound
->start
);
4433 dirty
->end
= MAX2(dirty
->end
, bound
->end
);
4437 uint64_t mask
= vb_used
;
4439 int i
= u_bit_scan64(&mask
);
4441 assert(i
< ARRAY_SIZE(cmd_buffer
->state
.gfx
.vb_bound_ranges
));
4442 assert(i
< ARRAY_SIZE(cmd_buffer
->state
.gfx
.vb_dirty_ranges
));
4444 struct anv_vb_cache_range
*bound
, *dirty
;
4445 bound
= &cmd_buffer
->state
.gfx
.vb_bound_ranges
[i
];
4446 dirty
= &cmd_buffer
->state
.gfx
.vb_dirty_ranges
[i
];
4448 if (bound
->end
> bound
->start
) {
4449 dirty
->start
= MIN2(dirty
->start
, bound
->start
);
4450 dirty
->end
= MAX2(dirty
->end
, bound
->end
);
4456 * Update the pixel hashing modes that determine the balancing of PS threads
4457 * across subslices and slices.
4459 * \param width Width bound of the rendering area (already scaled down if \p
4460 * scale is greater than 1).
4461 * \param height Height bound of the rendering area (already scaled down if \p
4462 * scale is greater than 1).
4463 * \param scale The number of framebuffer samples that could potentially be
4464 * affected by an individual channel of the PS thread. This is
4465 * typically one for single-sampled rendering, but for operations
4466 * like CCS resolves and fast clears a single PS invocation may
4467 * update a huge number of pixels, in which case a finer
4468 * balancing is desirable in order to maximally utilize the
4469 * bandwidth available. UINT_MAX can be used as shorthand for
4470 * "finest hashing mode available".
4473 genX(cmd_buffer_emit_hashing_mode
)(struct anv_cmd_buffer
*cmd_buffer
,
4474 unsigned width
, unsigned height
,
4478 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
4479 const unsigned slice_hashing
[] = {
4480 /* Because all Gen9 platforms with more than one slice require
4481 * three-way subslice hashing, a single "normal" 16x16 slice hashing
4482 * block is guaranteed to suffer from substantial imbalance, with one
4483 * subslice receiving twice as much work as the other two in the
4486 * The performance impact of that would be particularly severe when
4487 * three-way hashing is also in use for slice balancing (which is the
4488 * case for all Gen9 GT4 platforms), because one of the slices
4489 * receives one every three 16x16 blocks in either direction, which
4490 * is roughly the periodicity of the underlying subslice imbalance
4491 * pattern ("roughly" because in reality the hardware's
4492 * implementation of three-way hashing doesn't do exact modulo 3
4493 * arithmetic, which somewhat decreases the magnitude of this effect
4494 * in practice). This leads to a systematic subslice imbalance
4495 * within that slice regardless of the size of the primitive. The
4496 * 32x32 hashing mode guarantees that the subslice imbalance within a
4497 * single slice hashing block is minimal, largely eliminating this
4501 /* Finest slice hashing mode available. */
4504 const unsigned subslice_hashing
[] = {
4505 /* 16x16 would provide a slight cache locality benefit especially
4506 * visible in the sampler L1 cache efficiency of low-bandwidth
4507 * non-LLC platforms, but it comes at the cost of greater subslice
4508 * imbalance for primitives of dimensions approximately intermediate
4509 * between 16x4 and 16x16.
4512 /* Finest subslice hashing mode available. */
4515 /* Dimensions of the smallest hashing block of a given hashing mode. If
4516 * the rendering area is smaller than this there can't possibly be any
4517 * benefit from switching to this mode, so we optimize out the
4520 const unsigned min_size
[][2] = {
4524 const unsigned idx
= scale
> 1;
4526 if (cmd_buffer
->state
.current_hash_scale
!= scale
&&
4527 (width
> min_size
[idx
][0] || height
> min_size
[idx
][1])) {
4530 anv_pack_struct(>_mode
, GENX(GT_MODE
),
4531 .SliceHashing
= (devinfo
->num_slices
> 1 ? slice_hashing
[idx
] : 0),
4532 .SliceHashingMask
= (devinfo
->num_slices
> 1 ? -1 : 0),
4533 .SubsliceHashing
= subslice_hashing
[idx
],
4534 .SubsliceHashingMask
= -1);
4536 cmd_buffer
->state
.pending_pipe_bits
|=
4537 ANV_PIPE_CS_STALL_BIT
| ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
4538 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
4540 emit_lri(&cmd_buffer
->batch
, GENX(GT_MODE_num
), gt_mode
);
4542 cmd_buffer
->state
.current_hash_scale
= scale
;
4548 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
4550 struct anv_device
*device
= cmd_buffer
->device
;
4551 const struct anv_image_view
*iview
=
4552 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
4553 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
4555 /* FIXME: Width and Height are wrong */
4557 genX(cmd_buffer_emit_gen7_depth_flush
)(cmd_buffer
);
4559 uint32_t *dw
= anv_batch_emit_dwords(&cmd_buffer
->batch
,
4560 device
->isl_dev
.ds
.size
/ 4);
4564 struct isl_depth_stencil_hiz_emit_info info
= { };
4567 info
.view
= &iview
->planes
[0].isl
;
4569 if (image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)) {
4570 uint32_t depth_plane
=
4571 anv_image_aspect_to_plane(image
->aspects
, VK_IMAGE_ASPECT_DEPTH_BIT
);
4572 const struct anv_surface
*surface
= &image
->planes
[depth_plane
].surface
;
4574 info
.depth_surf
= &surface
->isl
;
4576 info
.depth_address
=
4577 anv_batch_emit_reloc(&cmd_buffer
->batch
,
4578 dw
+ device
->isl_dev
.ds
.depth_offset
/ 4,
4579 image
->planes
[depth_plane
].address
.bo
,
4580 image
->planes
[depth_plane
].address
.offset
+
4583 anv_mocs_for_bo(device
, image
->planes
[depth_plane
].address
.bo
);
4586 cmd_buffer
->state
.subpass
->depth_stencil_attachment
->attachment
;
4587 info
.hiz_usage
= cmd_buffer
->state
.attachments
[ds
].aux_usage
;
4588 if (info
.hiz_usage
== ISL_AUX_USAGE_HIZ
) {
4589 info
.hiz_surf
= &image
->planes
[depth_plane
].aux_surface
.isl
;
4592 anv_batch_emit_reloc(&cmd_buffer
->batch
,
4593 dw
+ device
->isl_dev
.ds
.hiz_offset
/ 4,
4594 image
->planes
[depth_plane
].address
.bo
,
4595 image
->planes
[depth_plane
].address
.offset
+
4596 image
->planes
[depth_plane
].aux_surface
.offset
);
4598 info
.depth_clear_value
= ANV_HZ_FC_VAL
;
4602 if (image
&& (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
)) {
4603 uint32_t stencil_plane
=
4604 anv_image_aspect_to_plane(image
->aspects
, VK_IMAGE_ASPECT_STENCIL_BIT
);
4605 const struct anv_surface
*surface
= &image
->planes
[stencil_plane
].surface
;
4607 info
.stencil_surf
= &surface
->isl
;
4609 info
.stencil_address
=
4610 anv_batch_emit_reloc(&cmd_buffer
->batch
,
4611 dw
+ device
->isl_dev
.ds
.stencil_offset
/ 4,
4612 image
->planes
[stencil_plane
].address
.bo
,
4613 image
->planes
[stencil_plane
].address
.offset
+
4616 anv_mocs_for_bo(device
, image
->planes
[stencil_plane
].address
.bo
);
4619 isl_emit_depth_stencil_hiz_s(&device
->isl_dev
, dw
, &info
);
4621 if (GEN_GEN
>= 12) {
4622 /* GEN:BUG:1408224581
4624 * Workaround: Gen12LP Astep only An additional pipe control with
4625 * post-sync = store dword operation would be required.( w/a is to
4626 * have an additional pipe control after the stencil state whenever
4627 * the surface state bits of this state is changing).
4629 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
4630 pc
.PostSyncOperation
= WriteImmediateData
;
4632 (struct anv_address
) { cmd_buffer
->device
->workaround_bo
, 0 };
4635 cmd_buffer
->state
.hiz_enabled
= info
.hiz_usage
== ISL_AUX_USAGE_HIZ
;
4639 * This ANDs the view mask of the current subpass with the pending clear
4640 * views in the attachment to get the mask of views active in the subpass
4641 * that still need to be cleared.
4643 static inline uint32_t
4644 get_multiview_subpass_clear_mask(const struct anv_cmd_state
*cmd_state
,
4645 const struct anv_attachment_state
*att_state
)
4647 return cmd_state
->subpass
->view_mask
& att_state
->pending_clear_views
;
4651 do_first_layer_clear(const struct anv_cmd_state
*cmd_state
,
4652 const struct anv_attachment_state
*att_state
)
4654 if (!cmd_state
->subpass
->view_mask
)
4657 uint32_t pending_clear_mask
=
4658 get_multiview_subpass_clear_mask(cmd_state
, att_state
);
4660 return pending_clear_mask
& 1;
4664 current_subpass_is_last_for_attachment(const struct anv_cmd_state
*cmd_state
,
4667 const uint32_t last_subpass_idx
=
4668 cmd_state
->pass
->attachments
[att_idx
].last_subpass_idx
;
4669 const struct anv_subpass
*last_subpass
=
4670 &cmd_state
->pass
->subpasses
[last_subpass_idx
];
4671 return last_subpass
== cmd_state
->subpass
;
4675 cmd_buffer_begin_subpass(struct anv_cmd_buffer
*cmd_buffer
,
4676 uint32_t subpass_id
)
4678 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
4679 struct anv_subpass
*subpass
= &cmd_state
->pass
->subpasses
[subpass_id
];
4680 cmd_state
->subpass
= subpass
;
4682 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
4684 /* Our implementation of VK_KHR_multiview uses instancing to draw the
4685 * different views. If the client asks for instancing, we need to use the
4686 * Instance Data Step Rate to ensure that we repeat the client's
4687 * per-instance data once for each view. Since this bit is in
4688 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
4692 cmd_buffer
->state
.gfx
.vb_dirty
|= ~0;
4694 /* It is possible to start a render pass with an old pipeline. Because the
4695 * render pass and subpass index are both baked into the pipeline, this is
4696 * highly unlikely. In order to do so, it requires that you have a render
4697 * pass with a single subpass and that you use that render pass twice
4698 * back-to-back and use the same pipeline at the start of the second render
4699 * pass as at the end of the first. In order to avoid unpredictable issues
4700 * with this edge case, we just dirty the pipeline at the start of every
4703 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
4705 /* Accumulate any subpass flushes that need to happen before the subpass */
4706 cmd_buffer
->state
.pending_pipe_bits
|=
4707 cmd_buffer
->state
.pass
->subpass_flushes
[subpass_id
];
4709 VkRect2D render_area
= cmd_buffer
->state
.render_area
;
4710 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
4712 bool is_multiview
= subpass
->view_mask
!= 0;
4714 for (uint32_t i
= 0; i
< subpass
->attachment_count
; ++i
) {
4715 const uint32_t a
= subpass
->attachments
[i
].attachment
;
4716 if (a
== VK_ATTACHMENT_UNUSED
)
4719 assert(a
< cmd_state
->pass
->attachment_count
);
4720 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[a
];
4722 struct anv_image_view
*iview
= cmd_state
->attachments
[a
].image_view
;
4723 const struct anv_image
*image
= iview
->image
;
4725 /* A resolve is necessary before use as an input attachment if the clear
4726 * color or auxiliary buffer usage isn't supported by the sampler.
4728 const bool input_needs_resolve
=
4729 (att_state
->fast_clear
&& !att_state
->clear_color_is_zero_one
) ||
4730 att_state
->input_aux_usage
!= att_state
->aux_usage
;
4732 VkImageLayout target_layout
;
4733 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
&&
4734 !input_needs_resolve
) {
4735 /* Layout transitions before the final only help to enable sampling
4736 * as an input attachment. If the input attachment supports sampling
4737 * using the auxiliary surface, we can skip such transitions by
4738 * making the target layout one that is CCS-aware.
4740 target_layout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
4742 target_layout
= subpass
->attachments
[i
].layout
;
4745 VkImageLayout target_stencil_layout
=
4746 subpass
->attachments
[i
].stencil_layout
;
4748 uint32_t base_layer
, layer_count
;
4749 if (image
->type
== VK_IMAGE_TYPE_3D
) {
4751 layer_count
= anv_minify(iview
->image
->extent
.depth
,
4752 iview
->planes
[0].isl
.base_level
);
4754 base_layer
= iview
->planes
[0].isl
.base_array_layer
;
4755 layer_count
= fb
->layers
;
4758 if (image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
4759 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
4760 transition_color_buffer(cmd_buffer
, image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4761 iview
->planes
[0].isl
.base_level
, 1,
4762 base_layer
, layer_count
,
4763 att_state
->current_layout
, target_layout
);
4766 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
4767 transition_depth_buffer(cmd_buffer
, image
,
4768 att_state
->current_layout
, target_layout
);
4769 att_state
->aux_usage
=
4770 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
,
4771 VK_IMAGE_ASPECT_DEPTH_BIT
,
4772 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
,
4776 if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
4777 transition_stencil_buffer(cmd_buffer
, image
,
4778 iview
->planes
[0].isl
.base_level
, 1,
4779 base_layer
, layer_count
,
4780 att_state
->current_stencil_layout
,
4781 target_stencil_layout
);
4783 att_state
->current_layout
= target_layout
;
4784 att_state
->current_stencil_layout
= target_stencil_layout
;
4786 if (att_state
->pending_clear_aspects
& VK_IMAGE_ASPECT_COLOR_BIT
) {
4787 assert(att_state
->pending_clear_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
4789 /* Multi-planar images are not supported as attachments */
4790 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
4791 assert(image
->n_planes
== 1);
4793 uint32_t base_clear_layer
= iview
->planes
[0].isl
.base_array_layer
;
4794 uint32_t clear_layer_count
= fb
->layers
;
4796 if (att_state
->fast_clear
&&
4797 do_first_layer_clear(cmd_state
, att_state
)) {
4798 /* We only support fast-clears on the first layer */
4799 assert(iview
->planes
[0].isl
.base_level
== 0);
4800 assert(iview
->planes
[0].isl
.base_array_layer
== 0);
4802 union isl_color_value clear_color
= {};
4803 anv_clear_color_from_att_state(&clear_color
, att_state
, iview
);
4804 if (iview
->image
->samples
== 1) {
4805 anv_image_ccs_op(cmd_buffer
, image
,
4806 iview
->planes
[0].isl
.format
,
4807 VK_IMAGE_ASPECT_COLOR_BIT
,
4808 0, 0, 1, ISL_AUX_OP_FAST_CLEAR
,
4812 anv_image_mcs_op(cmd_buffer
, image
,
4813 iview
->planes
[0].isl
.format
,
4814 VK_IMAGE_ASPECT_COLOR_BIT
,
4815 0, 1, ISL_AUX_OP_FAST_CLEAR
,
4820 clear_layer_count
--;
4822 att_state
->pending_clear_views
&= ~1;
4824 if (att_state
->clear_color_is_zero
) {
4825 /* This image has the auxiliary buffer enabled. We can mark the
4826 * subresource as not needing a resolve because the clear color
4827 * will match what's in every RENDER_SURFACE_STATE object when
4828 * it's being used for sampling.
4830 set_image_fast_clear_state(cmd_buffer
, iview
->image
,
4831 VK_IMAGE_ASPECT_COLOR_BIT
,
4832 ANV_FAST_CLEAR_DEFAULT_VALUE
);
4834 set_image_fast_clear_state(cmd_buffer
, iview
->image
,
4835 VK_IMAGE_ASPECT_COLOR_BIT
,
4836 ANV_FAST_CLEAR_ANY
);
4840 /* From the VkFramebufferCreateInfo spec:
4842 * "If the render pass uses multiview, then layers must be one and each
4843 * attachment requires a number of layers that is greater than the
4844 * maximum bit index set in the view mask in the subpasses in which it
4847 * So if multiview is active we ignore the number of layers in the
4848 * framebuffer and instead we honor the view mask from the subpass.
4851 assert(image
->n_planes
== 1);
4852 uint32_t pending_clear_mask
=
4853 get_multiview_subpass_clear_mask(cmd_state
, att_state
);
4856 for_each_bit(layer_idx
, pending_clear_mask
) {
4858 iview
->planes
[0].isl
.base_array_layer
+ layer_idx
;
4860 anv_image_clear_color(cmd_buffer
, image
,
4861 VK_IMAGE_ASPECT_COLOR_BIT
,
4862 att_state
->aux_usage
,
4863 iview
->planes
[0].isl
.format
,
4864 iview
->planes
[0].isl
.swizzle
,
4865 iview
->planes
[0].isl
.base_level
,
4868 vk_to_isl_color(att_state
->clear_value
.color
));
4871 att_state
->pending_clear_views
&= ~pending_clear_mask
;
4872 } else if (clear_layer_count
> 0) {
4873 assert(image
->n_planes
== 1);
4874 anv_image_clear_color(cmd_buffer
, image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4875 att_state
->aux_usage
,
4876 iview
->planes
[0].isl
.format
,
4877 iview
->planes
[0].isl
.swizzle
,
4878 iview
->planes
[0].isl
.base_level
,
4879 base_clear_layer
, clear_layer_count
,
4881 vk_to_isl_color(att_state
->clear_value
.color
));
4883 } else if (att_state
->pending_clear_aspects
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
4884 VK_IMAGE_ASPECT_STENCIL_BIT
)) {
4885 if (att_state
->fast_clear
&& !is_multiview
) {
4886 /* We currently only support HiZ for single-layer images */
4887 if (att_state
->pending_clear_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
4888 assert(iview
->image
->planes
[0].aux_usage
== ISL_AUX_USAGE_HIZ
);
4889 assert(iview
->planes
[0].isl
.base_level
== 0);
4890 assert(iview
->planes
[0].isl
.base_array_layer
== 0);
4891 assert(fb
->layers
== 1);
4894 anv_image_hiz_clear(cmd_buffer
, image
,
4895 att_state
->pending_clear_aspects
,
4896 iview
->planes
[0].isl
.base_level
,
4897 iview
->planes
[0].isl
.base_array_layer
,
4898 fb
->layers
, render_area
,
4899 att_state
->clear_value
.depthStencil
.stencil
);
4900 } else if (is_multiview
) {
4901 uint32_t pending_clear_mask
=
4902 get_multiview_subpass_clear_mask(cmd_state
, att_state
);
4905 for_each_bit(layer_idx
, pending_clear_mask
) {
4907 iview
->planes
[0].isl
.base_array_layer
+ layer_idx
;
4909 anv_image_clear_depth_stencil(cmd_buffer
, image
,
4910 att_state
->pending_clear_aspects
,
4911 att_state
->aux_usage
,
4912 iview
->planes
[0].isl
.base_level
,
4915 att_state
->clear_value
.depthStencil
.depth
,
4916 att_state
->clear_value
.depthStencil
.stencil
);
4919 att_state
->pending_clear_views
&= ~pending_clear_mask
;
4921 anv_image_clear_depth_stencil(cmd_buffer
, image
,
4922 att_state
->pending_clear_aspects
,
4923 att_state
->aux_usage
,
4924 iview
->planes
[0].isl
.base_level
,
4925 iview
->planes
[0].isl
.base_array_layer
,
4926 fb
->layers
, render_area
,
4927 att_state
->clear_value
.depthStencil
.depth
,
4928 att_state
->clear_value
.depthStencil
.stencil
);
4931 assert(att_state
->pending_clear_aspects
== 0);
4935 (att_state
->pending_load_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) &&
4936 image
->planes
[0].aux_usage
!= ISL_AUX_USAGE_NONE
&&
4937 iview
->planes
[0].isl
.base_level
== 0 &&
4938 iview
->planes
[0].isl
.base_array_layer
== 0) {
4939 if (att_state
->aux_usage
!= ISL_AUX_USAGE_NONE
) {
4940 genX(copy_fast_clear_dwords
)(cmd_buffer
, att_state
->color
.state
,
4941 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4942 false /* copy to ss */);
4945 if (need_input_attachment_state(&cmd_state
->pass
->attachments
[a
]) &&
4946 att_state
->input_aux_usage
!= ISL_AUX_USAGE_NONE
) {
4947 genX(copy_fast_clear_dwords
)(cmd_buffer
, att_state
->input
.state
,
4948 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4949 false /* copy to ss */);
4953 if (subpass
->attachments
[i
].usage
==
4954 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
4955 /* We assume that if we're starting a subpass, we're going to do some
4956 * rendering so we may end up with compressed data.
4958 genX(cmd_buffer_mark_image_written
)(cmd_buffer
, iview
->image
,
4959 VK_IMAGE_ASPECT_COLOR_BIT
,
4960 att_state
->aux_usage
,
4961 iview
->planes
[0].isl
.base_level
,
4962 iview
->planes
[0].isl
.base_array_layer
,
4964 } else if (subpass
->attachments
[i
].usage
==
4965 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
4966 /* We may be writing depth or stencil so we need to mark the surface.
4967 * Unfortunately, there's no way to know at this point whether the
4968 * depth or stencil tests used will actually write to the surface.
4970 * Even though stencil may be plane 1, it always shares a base_level
4973 const struct isl_view
*ds_view
= &iview
->planes
[0].isl
;
4974 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
4975 genX(cmd_buffer_mark_image_written
)(cmd_buffer
, image
,
4976 VK_IMAGE_ASPECT_DEPTH_BIT
,
4977 att_state
->aux_usage
,
4978 ds_view
->base_level
,
4979 ds_view
->base_array_layer
,
4982 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
4983 /* Even though stencil may be plane 1, it always shares a
4984 * base_level with depth.
4986 genX(cmd_buffer_mark_image_written
)(cmd_buffer
, image
,
4987 VK_IMAGE_ASPECT_STENCIL_BIT
,
4989 ds_view
->base_level
,
4990 ds_view
->base_array_layer
,
4995 /* If multiview is enabled, then we are only done clearing when we no
4996 * longer have pending layers to clear, or when we have processed the
4997 * last subpass that uses this attachment.
4999 if (!is_multiview
||
5000 att_state
->pending_clear_views
== 0 ||
5001 current_subpass_is_last_for_attachment(cmd_state
, a
)) {
5002 att_state
->pending_clear_aspects
= 0;
5005 att_state
->pending_load_aspects
= 0;
5008 cmd_buffer_emit_depth_stencil(cmd_buffer
);
5011 /* The PIPE_CONTROL command description says:
5013 * "Whenever a Binding Table Index (BTI) used by a Render Taget Message
5014 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
5015 * Target Cache Flush by enabling this bit. When render target flush
5016 * is set due to new association of BTI, PS Scoreboard Stall bit must
5017 * be set in this packet."
5019 cmd_buffer
->state
.pending_pipe_bits
|=
5020 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
|
5021 ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
5025 static enum blorp_filter
5026 vk_to_blorp_resolve_mode(VkResolveModeFlagBitsKHR vk_mode
)
5029 case VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
:
5030 return BLORP_FILTER_SAMPLE_0
;
5031 case VK_RESOLVE_MODE_AVERAGE_BIT_KHR
:
5032 return BLORP_FILTER_AVERAGE
;
5033 case VK_RESOLVE_MODE_MIN_BIT_KHR
:
5034 return BLORP_FILTER_MIN_SAMPLE
;
5035 case VK_RESOLVE_MODE_MAX_BIT_KHR
:
5036 return BLORP_FILTER_MAX_SAMPLE
;
5038 return BLORP_FILTER_NONE
;
5043 cmd_buffer_end_subpass(struct anv_cmd_buffer
*cmd_buffer
)
5045 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
5046 struct anv_subpass
*subpass
= cmd_state
->subpass
;
5047 uint32_t subpass_id
= anv_get_subpass_id(&cmd_buffer
->state
);
5048 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
5050 if (subpass
->has_color_resolve
) {
5051 /* We are about to do some MSAA resolves. We need to flush so that the
5052 * result of writes to the MSAA color attachments show up in the sampler
5053 * when we blit to the single-sampled resolve target.
5055 cmd_buffer
->state
.pending_pipe_bits
|=
5056 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
|
5057 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
5059 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
5060 uint32_t src_att
= subpass
->color_attachments
[i
].attachment
;
5061 uint32_t dst_att
= subpass
->resolve_attachments
[i
].attachment
;
5063 if (dst_att
== VK_ATTACHMENT_UNUSED
)
5066 assert(src_att
< cmd_buffer
->state
.pass
->attachment_count
);
5067 assert(dst_att
< cmd_buffer
->state
.pass
->attachment_count
);
5069 if (cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
) {
5070 /* From the Vulkan 1.0 spec:
5072 * If the first use of an attachment in a render pass is as a
5073 * resolve attachment, then the loadOp is effectively ignored
5074 * as the resolve is guaranteed to overwrite all pixels in the
5077 cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
= 0;
5080 struct anv_image_view
*src_iview
= cmd_state
->attachments
[src_att
].image_view
;
5081 struct anv_image_view
*dst_iview
= cmd_state
->attachments
[dst_att
].image_view
;
5083 const VkRect2D render_area
= cmd_buffer
->state
.render_area
;
5085 enum isl_aux_usage src_aux_usage
=
5086 cmd_buffer
->state
.attachments
[src_att
].aux_usage
;
5087 enum isl_aux_usage dst_aux_usage
=
5088 cmd_buffer
->state
.attachments
[dst_att
].aux_usage
;
5090 assert(src_iview
->aspect_mask
== VK_IMAGE_ASPECT_COLOR_BIT
&&
5091 dst_iview
->aspect_mask
== VK_IMAGE_ASPECT_COLOR_BIT
);
5093 anv_image_msaa_resolve(cmd_buffer
,
5094 src_iview
->image
, src_aux_usage
,
5095 src_iview
->planes
[0].isl
.base_level
,
5096 src_iview
->planes
[0].isl
.base_array_layer
,
5097 dst_iview
->image
, dst_aux_usage
,
5098 dst_iview
->planes
[0].isl
.base_level
,
5099 dst_iview
->planes
[0].isl
.base_array_layer
,
5100 VK_IMAGE_ASPECT_COLOR_BIT
,
5101 render_area
.offset
.x
, render_area
.offset
.y
,
5102 render_area
.offset
.x
, render_area
.offset
.y
,
5103 render_area
.extent
.width
,
5104 render_area
.extent
.height
,
5105 fb
->layers
, BLORP_FILTER_NONE
);
5109 if (subpass
->ds_resolve_attachment
) {
5110 /* We are about to do some MSAA resolves. We need to flush so that the
5111 * result of writes to the MSAA depth attachments show up in the sampler
5112 * when we blit to the single-sampled resolve target.
5114 cmd_buffer
->state
.pending_pipe_bits
|=
5115 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
|
5116 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
5118 uint32_t src_att
= subpass
->depth_stencil_attachment
->attachment
;
5119 uint32_t dst_att
= subpass
->ds_resolve_attachment
->attachment
;
5121 assert(src_att
< cmd_buffer
->state
.pass
->attachment_count
);
5122 assert(dst_att
< cmd_buffer
->state
.pass
->attachment_count
);
5124 if (cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
) {
5125 /* From the Vulkan 1.0 spec:
5127 * If the first use of an attachment in a render pass is as a
5128 * resolve attachment, then the loadOp is effectively ignored
5129 * as the resolve is guaranteed to overwrite all pixels in the
5132 cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
= 0;
5135 struct anv_image_view
*src_iview
= cmd_state
->attachments
[src_att
].image_view
;
5136 struct anv_image_view
*dst_iview
= cmd_state
->attachments
[dst_att
].image_view
;
5138 const VkRect2D render_area
= cmd_buffer
->state
.render_area
;
5140 struct anv_attachment_state
*src_state
=
5141 &cmd_state
->attachments
[src_att
];
5142 struct anv_attachment_state
*dst_state
=
5143 &cmd_state
->attachments
[dst_att
];
5145 if ((src_iview
->image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
5146 subpass
->depth_resolve_mode
!= VK_RESOLVE_MODE_NONE_KHR
) {
5148 /* MSAA resolves sample from the source attachment. Transition the
5149 * depth attachment first to get rid of any HiZ that we may not be
5152 transition_depth_buffer(cmd_buffer
, src_iview
->image
,
5153 src_state
->current_layout
,
5154 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
);
5155 src_state
->aux_usage
=
5156 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, src_iview
->image
,
5157 VK_IMAGE_ASPECT_DEPTH_BIT
,
5158 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
5159 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
);
5160 src_state
->current_layout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
5162 /* MSAA resolves write to the resolve attachment as if it were any
5163 * other transfer op. Transition the resolve attachment accordingly.
5165 VkImageLayout dst_initial_layout
= dst_state
->current_layout
;
5167 /* If our render area is the entire size of the image, we're going to
5168 * blow it all away so we can claim the initial layout is UNDEFINED
5169 * and we'll get a HiZ ambiguate instead of a resolve.
5171 if (dst_iview
->image
->type
!= VK_IMAGE_TYPE_3D
&&
5172 render_area
.offset
.x
== 0 && render_area
.offset
.y
== 0 &&
5173 render_area
.extent
.width
== dst_iview
->extent
.width
&&
5174 render_area
.extent
.height
== dst_iview
->extent
.height
)
5175 dst_initial_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
5177 transition_depth_buffer(cmd_buffer
, dst_iview
->image
,
5179 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
);
5180 dst_state
->aux_usage
=
5181 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, dst_iview
->image
,
5182 VK_IMAGE_ASPECT_DEPTH_BIT
,
5183 VK_IMAGE_USAGE_TRANSFER_DST_BIT
,
5184 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
);
5185 dst_state
->current_layout
= VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
;
5187 enum blorp_filter filter
=
5188 vk_to_blorp_resolve_mode(subpass
->depth_resolve_mode
);
5190 anv_image_msaa_resolve(cmd_buffer
,
5191 src_iview
->image
, src_state
->aux_usage
,
5192 src_iview
->planes
[0].isl
.base_level
,
5193 src_iview
->planes
[0].isl
.base_array_layer
,
5194 dst_iview
->image
, dst_state
->aux_usage
,
5195 dst_iview
->planes
[0].isl
.base_level
,
5196 dst_iview
->planes
[0].isl
.base_array_layer
,
5197 VK_IMAGE_ASPECT_DEPTH_BIT
,
5198 render_area
.offset
.x
, render_area
.offset
.y
,
5199 render_area
.offset
.x
, render_area
.offset
.y
,
5200 render_area
.extent
.width
,
5201 render_area
.extent
.height
,
5202 fb
->layers
, filter
);
5205 if ((src_iview
->image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
5206 subpass
->stencil_resolve_mode
!= VK_RESOLVE_MODE_NONE_KHR
) {
5208 src_state
->current_stencil_layout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
5209 dst_state
->current_stencil_layout
= VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
;
5211 enum isl_aux_usage src_aux_usage
= ISL_AUX_USAGE_NONE
;
5212 enum isl_aux_usage dst_aux_usage
= ISL_AUX_USAGE_NONE
;
5214 enum blorp_filter filter
=
5215 vk_to_blorp_resolve_mode(subpass
->stencil_resolve_mode
);
5217 anv_image_msaa_resolve(cmd_buffer
,
5218 src_iview
->image
, src_aux_usage
,
5219 src_iview
->planes
[0].isl
.base_level
,
5220 src_iview
->planes
[0].isl
.base_array_layer
,
5221 dst_iview
->image
, dst_aux_usage
,
5222 dst_iview
->planes
[0].isl
.base_level
,
5223 dst_iview
->planes
[0].isl
.base_array_layer
,
5224 VK_IMAGE_ASPECT_STENCIL_BIT
,
5225 render_area
.offset
.x
, render_area
.offset
.y
,
5226 render_area
.offset
.x
, render_area
.offset
.y
,
5227 render_area
.extent
.width
,
5228 render_area
.extent
.height
,
5229 fb
->layers
, filter
);
5234 /* On gen7, we have to store a texturable version of the stencil buffer in
5235 * a shadow whenever VK_IMAGE_USAGE_SAMPLED_BIT is set and copy back and
5236 * forth at strategic points. Stencil writes are only allowed in following
5239 * - VK_IMAGE_LAYOUT_GENERAL
5240 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5241 * - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
5242 * - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
5243 * - VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR
5245 * For general, we have no nice opportunity to transition so we do the copy
5246 * to the shadow unconditionally at the end of the subpass. For transfer
5247 * destinations, we can update it as part of the transfer op. For the other
5248 * layouts, we delay the copy until a transition into some other layout.
5250 if (subpass
->depth_stencil_attachment
) {
5251 uint32_t a
= subpass
->depth_stencil_attachment
->attachment
;
5252 assert(a
!= VK_ATTACHMENT_UNUSED
);
5254 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[a
];
5255 struct anv_image_view
*iview
= cmd_state
->attachments
[a
].image_view
;;
5256 const struct anv_image
*image
= iview
->image
;
5258 if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
5259 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
5260 VK_IMAGE_ASPECT_STENCIL_BIT
);
5262 if (image
->planes
[plane
].shadow_surface
.isl
.size_B
> 0 &&
5263 att_state
->current_stencil_layout
== VK_IMAGE_LAYOUT_GENERAL
) {
5264 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
5265 anv_image_copy_to_shadow(cmd_buffer
, image
,
5266 VK_IMAGE_ASPECT_STENCIL_BIT
,
5267 iview
->planes
[plane
].isl
.base_level
, 1,
5268 iview
->planes
[plane
].isl
.base_array_layer
,
5273 #endif /* GEN_GEN == 7 */
5275 for (uint32_t i
= 0; i
< subpass
->attachment_count
; ++i
) {
5276 const uint32_t a
= subpass
->attachments
[i
].attachment
;
5277 if (a
== VK_ATTACHMENT_UNUSED
)
5280 if (cmd_state
->pass
->attachments
[a
].last_subpass_idx
!= subpass_id
)
5283 assert(a
< cmd_state
->pass
->attachment_count
);
5284 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[a
];
5285 struct anv_image_view
*iview
= cmd_state
->attachments
[a
].image_view
;
5286 const struct anv_image
*image
= iview
->image
;
5288 if ((image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) &&
5289 image
->vk_format
!= iview
->vk_format
) {
5290 enum anv_fast_clear_type fast_clear_type
=
5291 anv_layout_to_fast_clear_type(&cmd_buffer
->device
->info
,
5292 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
5293 att_state
->current_layout
);
5295 /* If any clear color was used, flush it down the aux surfaces. If we
5296 * don't do it now using the view's format we might use the clear
5297 * color incorrectly in the following resolves (for example with an
5298 * SRGB view & a UNORM image).
5300 if (fast_clear_type
!= ANV_FAST_CLEAR_NONE
) {
5301 anv_perf_warn(cmd_buffer
->device
, iview
,
5302 "Doing a partial resolve to get rid of clear color at the "
5303 "end of a renderpass due to an image/view format mismatch");
5305 uint32_t base_layer
, layer_count
;
5306 if (image
->type
== VK_IMAGE_TYPE_3D
) {
5308 layer_count
= anv_minify(iview
->image
->extent
.depth
,
5309 iview
->planes
[0].isl
.base_level
);
5311 base_layer
= iview
->planes
[0].isl
.base_array_layer
;
5312 layer_count
= fb
->layers
;
5315 for (uint32_t a
= 0; a
< layer_count
; a
++) {
5316 uint32_t array_layer
= base_layer
+ a
;
5317 if (image
->samples
== 1) {
5318 anv_cmd_predicated_ccs_resolve(cmd_buffer
, image
,
5319 iview
->planes
[0].isl
.format
,
5320 VK_IMAGE_ASPECT_COLOR_BIT
,
5321 iview
->planes
[0].isl
.base_level
,
5323 ISL_AUX_OP_PARTIAL_RESOLVE
,
5324 ANV_FAST_CLEAR_NONE
);
5326 anv_cmd_predicated_mcs_resolve(cmd_buffer
, image
,
5327 iview
->planes
[0].isl
.format
,
5328 VK_IMAGE_ASPECT_COLOR_BIT
,
5330 ISL_AUX_OP_PARTIAL_RESOLVE
,
5331 ANV_FAST_CLEAR_NONE
);
5337 /* Transition the image into the final layout for this render pass */
5338 VkImageLayout target_layout
=
5339 cmd_state
->pass
->attachments
[a
].final_layout
;
5340 VkImageLayout target_stencil_layout
=
5341 cmd_state
->pass
->attachments
[a
].stencil_final_layout
;
5343 uint32_t base_layer
, layer_count
;
5344 if (image
->type
== VK_IMAGE_TYPE_3D
) {
5346 layer_count
= anv_minify(iview
->image
->extent
.depth
,
5347 iview
->planes
[0].isl
.base_level
);
5349 base_layer
= iview
->planes
[0].isl
.base_array_layer
;
5350 layer_count
= fb
->layers
;
5353 if (image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
5354 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
5355 transition_color_buffer(cmd_buffer
, image
, VK_IMAGE_ASPECT_COLOR_BIT
,
5356 iview
->planes
[0].isl
.base_level
, 1,
5357 base_layer
, layer_count
,
5358 att_state
->current_layout
, target_layout
);
5361 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
5362 transition_depth_buffer(cmd_buffer
, image
,
5363 att_state
->current_layout
, target_layout
);
5366 if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
5367 transition_stencil_buffer(cmd_buffer
, image
,
5368 iview
->planes
[0].isl
.base_level
, 1,
5369 base_layer
, layer_count
,
5370 att_state
->current_stencil_layout
,
5371 target_stencil_layout
);
5375 /* Accumulate any subpass flushes that need to happen after the subpass.
5376 * Yes, they do get accumulated twice in the NextSubpass case but since
5377 * genX_CmdNextSubpass just calls end/begin back-to-back, we just end up
5378 * ORing the bits in twice so it's harmless.
5380 cmd_buffer
->state
.pending_pipe_bits
|=
5381 cmd_buffer
->state
.pass
->subpass_flushes
[subpass_id
+ 1];
5384 void genX(CmdBeginRenderPass
)(
5385 VkCommandBuffer commandBuffer
,
5386 const VkRenderPassBeginInfo
* pRenderPassBegin
,
5387 VkSubpassContents contents
)
5389 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5390 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
5391 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
5393 cmd_buffer
->state
.framebuffer
= framebuffer
;
5394 cmd_buffer
->state
.pass
= pass
;
5395 cmd_buffer
->state
.render_area
= pRenderPassBegin
->renderArea
;
5397 genX(cmd_buffer_setup_attachments
)(cmd_buffer
, pass
, pRenderPassBegin
);
5399 /* If we failed to setup the attachments we should not try to go further */
5400 if (result
!= VK_SUCCESS
) {
5401 assert(anv_batch_has_error(&cmd_buffer
->batch
));
5405 genX(flush_pipeline_select_3d
)(cmd_buffer
);
5407 cmd_buffer_begin_subpass(cmd_buffer
, 0);
5410 void genX(CmdBeginRenderPass2
)(
5411 VkCommandBuffer commandBuffer
,
5412 const VkRenderPassBeginInfo
* pRenderPassBeginInfo
,
5413 const VkSubpassBeginInfoKHR
* pSubpassBeginInfo
)
5415 genX(CmdBeginRenderPass
)(commandBuffer
, pRenderPassBeginInfo
,
5416 pSubpassBeginInfo
->contents
);
5419 void genX(CmdNextSubpass
)(
5420 VkCommandBuffer commandBuffer
,
5421 VkSubpassContents contents
)
5423 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5425 if (anv_batch_has_error(&cmd_buffer
->batch
))
5428 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
5430 uint32_t prev_subpass
= anv_get_subpass_id(&cmd_buffer
->state
);
5431 cmd_buffer_end_subpass(cmd_buffer
);
5432 cmd_buffer_begin_subpass(cmd_buffer
, prev_subpass
+ 1);
5435 void genX(CmdNextSubpass2
)(
5436 VkCommandBuffer commandBuffer
,
5437 const VkSubpassBeginInfoKHR
* pSubpassBeginInfo
,
5438 const VkSubpassEndInfoKHR
* pSubpassEndInfo
)
5440 genX(CmdNextSubpass
)(commandBuffer
, pSubpassBeginInfo
->contents
);
5443 void genX(CmdEndRenderPass
)(
5444 VkCommandBuffer commandBuffer
)
5446 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5448 if (anv_batch_has_error(&cmd_buffer
->batch
))
5451 cmd_buffer_end_subpass(cmd_buffer
);
5453 cmd_buffer
->state
.hiz_enabled
= false;
5456 anv_dump_add_attachments(cmd_buffer
);
5459 /* Remove references to render pass specific state. This enables us to
5460 * detect whether or not we're in a renderpass.
5462 cmd_buffer
->state
.framebuffer
= NULL
;
5463 cmd_buffer
->state
.pass
= NULL
;
5464 cmd_buffer
->state
.subpass
= NULL
;
5467 void genX(CmdEndRenderPass2
)(
5468 VkCommandBuffer commandBuffer
,
5469 const VkSubpassEndInfoKHR
* pSubpassEndInfo
)
5471 genX(CmdEndRenderPass
)(commandBuffer
);
5475 genX(cmd_emit_conditional_render_predicate
)(struct anv_cmd_buffer
*cmd_buffer
)
5477 #if GEN_GEN >= 8 || GEN_IS_HASWELL
5478 struct gen_mi_builder b
;
5479 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
5481 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
),
5482 gen_mi_reg32(ANV_PREDICATE_RESULT_REG
));
5483 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
5485 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
5486 mip
.LoadOperation
= LOAD_LOADINV
;
5487 mip
.CombineOperation
= COMBINE_SET
;
5488 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
5493 #if GEN_GEN >= 8 || GEN_IS_HASWELL
5494 void genX(CmdBeginConditionalRenderingEXT
)(
5495 VkCommandBuffer commandBuffer
,
5496 const VkConditionalRenderingBeginInfoEXT
* pConditionalRenderingBegin
)
5498 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5499 ANV_FROM_HANDLE(anv_buffer
, buffer
, pConditionalRenderingBegin
->buffer
);
5500 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
5501 struct anv_address value_address
=
5502 anv_address_add(buffer
->address
, pConditionalRenderingBegin
->offset
);
5504 const bool isInverted
= pConditionalRenderingBegin
->flags
&
5505 VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT
;
5507 cmd_state
->conditional_render_enabled
= true;
5509 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
5511 struct gen_mi_builder b
;
5512 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
5514 /* Section 19.4 of the Vulkan 1.1.85 spec says:
5516 * If the value of the predicate in buffer memory changes
5517 * while conditional rendering is active, the rendering commands
5518 * may be discarded in an implementation-dependent way.
5519 * Some implementations may latch the value of the predicate
5520 * upon beginning conditional rendering while others
5521 * may read it before every rendering command.
5523 * So it's perfectly fine to read a value from the buffer once.
5525 struct gen_mi_value value
= gen_mi_mem32(value_address
);
5527 /* Precompute predicate result, it is necessary to support secondary
5528 * command buffers since it is unknown if conditional rendering is
5529 * inverted when populating them.
5531 gen_mi_store(&b
, gen_mi_reg64(ANV_PREDICATE_RESULT_REG
),
5532 isInverted
? gen_mi_uge(&b
, gen_mi_imm(0), value
) :
5533 gen_mi_ult(&b
, gen_mi_imm(0), value
));
5536 void genX(CmdEndConditionalRenderingEXT
)(
5537 VkCommandBuffer commandBuffer
)
5539 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5540 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
5542 cmd_state
->conditional_render_enabled
= false;
5546 /* Set of stage bits for which are pipelined, i.e. they get queued by the
5547 * command streamer for later execution.
5549 #define ANV_PIPELINE_STAGE_PIPELINED_BITS \
5550 (VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | \
5551 VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | \
5552 VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT | \
5553 VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT | \
5554 VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT | \
5555 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | \
5556 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | \
5557 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | \
5558 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | \
5559 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | \
5560 VK_PIPELINE_STAGE_TRANSFER_BIT | \
5561 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT | \
5562 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT | \
5563 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT)
5565 void genX(CmdSetEvent
)(
5566 VkCommandBuffer commandBuffer
,
5568 VkPipelineStageFlags stageMask
)
5570 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5571 ANV_FROM_HANDLE(anv_event
, event
, _event
);
5573 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
5574 if (stageMask
& ANV_PIPELINE_STAGE_PIPELINED_BITS
) {
5575 pc
.StallAtPixelScoreboard
= true;
5576 pc
.CommandStreamerStallEnable
= true;
5579 pc
.DestinationAddressType
= DAT_PPGTT
,
5580 pc
.PostSyncOperation
= WriteImmediateData
,
5581 pc
.Address
= (struct anv_address
) {
5582 cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
5585 pc
.ImmediateData
= VK_EVENT_SET
;
5589 void genX(CmdResetEvent
)(
5590 VkCommandBuffer commandBuffer
,
5592 VkPipelineStageFlags stageMask
)
5594 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5595 ANV_FROM_HANDLE(anv_event
, event
, _event
);
5597 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
5598 if (stageMask
& ANV_PIPELINE_STAGE_PIPELINED_BITS
) {
5599 pc
.StallAtPixelScoreboard
= true;
5600 pc
.CommandStreamerStallEnable
= true;
5603 pc
.DestinationAddressType
= DAT_PPGTT
;
5604 pc
.PostSyncOperation
= WriteImmediateData
;
5605 pc
.Address
= (struct anv_address
) {
5606 cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
5609 pc
.ImmediateData
= VK_EVENT_RESET
;
5613 void genX(CmdWaitEvents
)(
5614 VkCommandBuffer commandBuffer
,
5615 uint32_t eventCount
,
5616 const VkEvent
* pEvents
,
5617 VkPipelineStageFlags srcStageMask
,
5618 VkPipelineStageFlags destStageMask
,
5619 uint32_t memoryBarrierCount
,
5620 const VkMemoryBarrier
* pMemoryBarriers
,
5621 uint32_t bufferMemoryBarrierCount
,
5622 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
5623 uint32_t imageMemoryBarrierCount
,
5624 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
5627 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5629 for (uint32_t i
= 0; i
< eventCount
; i
++) {
5630 ANV_FROM_HANDLE(anv_event
, event
, pEvents
[i
]);
5632 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_SEMAPHORE_WAIT
), sem
) {
5633 sem
.WaitMode
= PollingMode
,
5634 sem
.CompareOperation
= COMPARE_SAD_EQUAL_SDD
,
5635 sem
.SemaphoreDataDword
= VK_EVENT_SET
,
5636 sem
.SemaphoreAddress
= (struct anv_address
) {
5637 cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
5643 anv_finishme("Implement events on gen7");
5646 genX(CmdPipelineBarrier
)(commandBuffer
, srcStageMask
, destStageMask
,
5647 false, /* byRegion */
5648 memoryBarrierCount
, pMemoryBarriers
,
5649 bufferMemoryBarrierCount
, pBufferMemoryBarriers
,
5650 imageMemoryBarrierCount
, pImageMemoryBarriers
);
5653 VkResult
genX(CmdSetPerformanceOverrideINTEL
)(
5654 VkCommandBuffer commandBuffer
,
5655 const VkPerformanceOverrideInfoINTEL
* pOverrideInfo
)
5657 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5659 switch (pOverrideInfo
->type
) {
5660 case VK_PERFORMANCE_OVERRIDE_TYPE_NULL_HARDWARE_INTEL
: {
5664 anv_pack_struct(&dw
, GENX(CS_DEBUG_MODE2
),
5665 ._3DRenderingInstructionDisable
= pOverrideInfo
->enable
,
5666 .MediaInstructionDisable
= pOverrideInfo
->enable
,
5667 ._3DRenderingInstructionDisableMask
= true,
5668 .MediaInstructionDisableMask
= true);
5669 emit_lri(&cmd_buffer
->batch
, GENX(CS_DEBUG_MODE2_num
), dw
);
5671 anv_pack_struct(&dw
, GENX(INSTPM
),
5672 ._3DRenderingInstructionDisable
= pOverrideInfo
->enable
,
5673 .MediaInstructionDisable
= pOverrideInfo
->enable
,
5674 ._3DRenderingInstructionDisableMask
= true,
5675 .MediaInstructionDisableMask
= true);
5676 emit_lri(&cmd_buffer
->batch
, GENX(INSTPM_num
), dw
);
5681 case VK_PERFORMANCE_OVERRIDE_TYPE_FLUSH_GPU_CACHES_INTEL
:
5682 if (pOverrideInfo
->enable
) {
5683 /* FLUSH ALL THE THINGS! As requested by the MDAPI team. */
5684 cmd_buffer
->state
.pending_pipe_bits
|=
5685 ANV_PIPE_FLUSH_BITS
|
5686 ANV_PIPE_INVALIDATE_BITS
;
5687 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
5692 unreachable("Invalid override");
5698 VkResult
genX(CmdSetPerformanceStreamMarkerINTEL
)(
5699 VkCommandBuffer commandBuffer
,
5700 const VkPerformanceStreamMarkerInfoINTEL
* pMarkerInfo
)
5702 /* TODO: Waiting on the register to write, might depend on generation. */