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"
44 emit_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
46 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
47 lri
.RegisterOffset
= reg
;
53 genX(cmd_buffer_emit_state_base_address
)(struct anv_cmd_buffer
*cmd_buffer
)
55 struct anv_device
*device
= cmd_buffer
->device
;
56 uint32_t mocs
= device
->isl_dev
.mocs
.internal
;
58 /* If we are emitting a new state base address we probably need to re-emit
61 cmd_buffer
->state
.descriptors_dirty
|= ~0;
63 /* Emit a render target cache flush.
65 * This isn't documented anywhere in the PRM. However, it seems to be
66 * necessary prior to changing the surface state base adress. Without
67 * this, we get GPU hangs when using multi-level command buffers which
68 * clear depth, reset state base address, and then go render stuff.
70 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
71 pc
.DCFlushEnable
= true;
72 pc
.RenderTargetCacheFlushEnable
= true;
73 pc
.CommandStreamerStallEnable
= true;
75 pc
.TileCacheFlushEnable
= true;
79 anv_batch_emit(&cmd_buffer
->batch
, GENX(STATE_BASE_ADDRESS
), sba
) {
80 sba
.GeneralStateBaseAddress
= (struct anv_address
) { NULL
, 0 };
81 sba
.GeneralStateMOCS
= mocs
;
82 sba
.GeneralStateBaseAddressModifyEnable
= true;
84 sba
.StatelessDataPortAccessMOCS
= mocs
;
86 sba
.SurfaceStateBaseAddress
=
87 anv_cmd_buffer_surface_base_address(cmd_buffer
);
88 sba
.SurfaceStateMOCS
= mocs
;
89 sba
.SurfaceStateBaseAddressModifyEnable
= true;
91 sba
.DynamicStateBaseAddress
=
92 (struct anv_address
) { device
->dynamic_state_pool
.block_pool
.bo
, 0 };
93 sba
.DynamicStateMOCS
= mocs
;
94 sba
.DynamicStateBaseAddressModifyEnable
= true;
96 sba
.IndirectObjectBaseAddress
= (struct anv_address
) { NULL
, 0 };
97 sba
.IndirectObjectMOCS
= mocs
;
98 sba
.IndirectObjectBaseAddressModifyEnable
= true;
100 sba
.InstructionBaseAddress
=
101 (struct anv_address
) { device
->instruction_state_pool
.block_pool
.bo
, 0 };
102 sba
.InstructionMOCS
= mocs
;
103 sba
.InstructionBaseAddressModifyEnable
= true;
106 /* Broadwell requires that we specify a buffer size for a bunch of
107 * these fields. However, since we will be growing the BO's live, we
108 * just set them all to the maximum.
110 sba
.GeneralStateBufferSize
= 0xfffff;
111 sba
.GeneralStateBufferSizeModifyEnable
= true;
112 sba
.DynamicStateBufferSize
= 0xfffff;
113 sba
.DynamicStateBufferSizeModifyEnable
= true;
114 sba
.IndirectObjectBufferSize
= 0xfffff;
115 sba
.IndirectObjectBufferSizeModifyEnable
= true;
116 sba
.InstructionBufferSize
= 0xfffff;
117 sba
.InstructionBuffersizeModifyEnable
= true;
119 /* On gen7, we have upper bounds instead. According to the docs,
120 * setting an upper bound of zero means that no bounds checking is
121 * performed so, in theory, we should be able to leave them zero.
122 * However, border color is broken and the GPU bounds-checks anyway.
123 * To avoid this and other potential problems, we may as well set it
126 sba
.GeneralStateAccessUpperBound
=
127 (struct anv_address
) { .bo
= NULL
, .offset
= 0xfffff000 };
128 sba
.GeneralStateAccessUpperBoundModifyEnable
= true;
129 sba
.DynamicStateAccessUpperBound
=
130 (struct anv_address
) { .bo
= NULL
, .offset
= 0xfffff000 };
131 sba
.DynamicStateAccessUpperBoundModifyEnable
= true;
132 sba
.InstructionAccessUpperBound
=
133 (struct anv_address
) { .bo
= NULL
, .offset
= 0xfffff000 };
134 sba
.InstructionAccessUpperBoundModifyEnable
= true;
137 if (cmd_buffer
->device
->instance
->physicalDevice
.use_softpin
) {
138 sba
.BindlessSurfaceStateBaseAddress
= (struct anv_address
) {
139 .bo
= device
->surface_state_pool
.block_pool
.bo
,
142 sba
.BindlessSurfaceStateSize
= (1 << 20) - 1;
144 sba
.BindlessSurfaceStateBaseAddress
= ANV_NULL_ADDRESS
;
145 sba
.BindlessSurfaceStateSize
= 0;
147 sba
.BindlessSurfaceStateMOCS
= mocs
;
148 sba
.BindlessSurfaceStateBaseAddressModifyEnable
= true;
151 sba
.BindlessSamplerStateBaseAddress
= (struct anv_address
) { NULL
, 0 };
152 sba
.BindlessSamplerStateMOCS
= mocs
;
153 sba
.BindlessSamplerStateBaseAddressModifyEnable
= true;
154 sba
.BindlessSamplerStateBufferSize
= 0;
158 /* After re-setting the surface state base address, we have to do some
159 * cache flusing so that the sampler engine will pick up the new
160 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
161 * Shared Function > 3D Sampler > State > State Caching (page 96):
163 * Coherency with system memory in the state cache, like the texture
164 * cache is handled partially by software. It is expected that the
165 * command stream or shader will issue Cache Flush operation or
166 * Cache_Flush sampler message to ensure that the L1 cache remains
167 * coherent with system memory.
171 * Whenever the value of the Dynamic_State_Base_Addr,
172 * Surface_State_Base_Addr are altered, the L1 state cache must be
173 * invalidated to ensure the new surface or sampler state is fetched
174 * from system memory.
176 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
177 * which, according the PIPE_CONTROL instruction documentation in the
180 * Setting this bit is independent of any other bit in this packet.
181 * This bit controls the invalidation of the L1 and L2 state caches
182 * at the top of the pipe i.e. at the parsing time.
184 * Unfortunately, experimentation seems to indicate that state cache
185 * invalidation through a PIPE_CONTROL does nothing whatsoever in
186 * regards to surface state and binding tables. In stead, it seems that
187 * invalidating the texture cache is what is actually needed.
189 * XXX: As far as we have been able to determine through
190 * experimentation, shows that flush the texture cache appears to be
191 * sufficient. The theory here is that all of the sampling/rendering
192 * units cache the binding table in the texture cache. However, we have
193 * yet to be able to actually confirm this.
195 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
196 pc
.TextureCacheInvalidationEnable
= true;
197 pc
.ConstantCacheInvalidationEnable
= true;
198 pc
.StateCacheInvalidationEnable
= true;
203 add_surface_reloc(struct anv_cmd_buffer
*cmd_buffer
,
204 struct anv_state state
, struct anv_address addr
)
206 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
209 anv_reloc_list_add(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
,
210 state
.offset
+ isl_dev
->ss
.addr_offset
,
211 addr
.bo
, addr
.offset
, NULL
);
212 if (result
!= VK_SUCCESS
)
213 anv_batch_set_error(&cmd_buffer
->batch
, result
);
217 add_surface_state_relocs(struct anv_cmd_buffer
*cmd_buffer
,
218 struct anv_surface_state state
)
220 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
222 assert(!anv_address_is_null(state
.address
));
223 add_surface_reloc(cmd_buffer
, state
.state
, state
.address
);
225 if (!anv_address_is_null(state
.aux_address
)) {
227 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
228 &cmd_buffer
->pool
->alloc
,
229 state
.state
.offset
+ isl_dev
->ss
.aux_addr_offset
,
230 state
.aux_address
.bo
,
231 state
.aux_address
.offset
,
233 if (result
!= VK_SUCCESS
)
234 anv_batch_set_error(&cmd_buffer
->batch
, result
);
237 if (!anv_address_is_null(state
.clear_address
)) {
239 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
240 &cmd_buffer
->pool
->alloc
,
242 isl_dev
->ss
.clear_color_state_offset
,
243 state
.clear_address
.bo
,
244 state
.clear_address
.offset
,
246 if (result
!= VK_SUCCESS
)
247 anv_batch_set_error(&cmd_buffer
->batch
, result
);
252 color_attachment_compute_aux_usage(struct anv_device
* device
,
253 struct anv_cmd_state
* cmd_state
,
254 uint32_t att
, VkRect2D render_area
,
255 union isl_color_value
*fast_clear_color
)
257 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[att
];
258 struct anv_image_view
*iview
= cmd_state
->attachments
[att
].image_view
;
260 assert(iview
->n_planes
== 1);
262 if (iview
->planes
[0].isl
.base_array_layer
>=
263 anv_image_aux_layers(iview
->image
, VK_IMAGE_ASPECT_COLOR_BIT
,
264 iview
->planes
[0].isl
.base_level
)) {
265 /* There is no aux buffer which corresponds to the level and layer(s)
268 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
269 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
270 att_state
->fast_clear
= false;
274 att_state
->aux_usage
=
275 anv_layout_to_aux_usage(&device
->info
, iview
->image
,
276 VK_IMAGE_ASPECT_COLOR_BIT
,
277 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
);
279 /* If we don't have aux, then we should have returned early in the layer
280 * check above. If we got here, we must have something.
282 assert(att_state
->aux_usage
!= ISL_AUX_USAGE_NONE
);
284 if (att_state
->aux_usage
== ISL_AUX_USAGE_CCS_E
||
285 att_state
->aux_usage
== ISL_AUX_USAGE_MCS
) {
286 att_state
->input_aux_usage
= att_state
->aux_usage
;
288 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
290 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
291 * setting is only allowed if Surface Format supported for Fast
292 * Clear. In addition, if the surface is bound to the sampling
293 * engine, Surface Format must be supported for Render Target
294 * Compression for surfaces bound to the sampling engine."
296 * In other words, we can only sample from a fast-cleared image if it
297 * also supports color compression.
299 if (isl_format_supports_ccs_e(&device
->info
, iview
->planes
[0].isl
.format
) &&
300 isl_format_supports_ccs_d(&device
->info
, iview
->planes
[0].isl
.format
)) {
301 att_state
->input_aux_usage
= ISL_AUX_USAGE_CCS_D
;
303 /* While fast-clear resolves and partial resolves are fairly cheap in the
304 * case where you render to most of the pixels, full resolves are not
305 * because they potentially involve reading and writing the entire
306 * framebuffer. If we can't texture with CCS_E, we should leave it off and
307 * limit ourselves to fast clears.
309 if (cmd_state
->pass
->attachments
[att
].first_subpass_layout
==
310 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
) {
311 anv_perf_warn(device
->instance
, iview
->image
,
312 "Not temporarily enabling CCS_E.");
315 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
319 assert(iview
->image
->planes
[0].aux_surface
.isl
.usage
&
320 (ISL_SURF_USAGE_CCS_BIT
| ISL_SURF_USAGE_MCS_BIT
));
322 union isl_color_value clear_color
= {};
323 anv_clear_color_from_att_state(&clear_color
, att_state
, iview
);
325 att_state
->clear_color_is_zero_one
=
326 isl_color_value_is_zero_one(clear_color
, iview
->planes
[0].isl
.format
);
327 att_state
->clear_color_is_zero
=
328 isl_color_value_is_zero(clear_color
, iview
->planes
[0].isl
.format
);
330 if (att_state
->pending_clear_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
331 /* Start by getting the fast clear type. We use the first subpass
332 * layout here because we don't want to fast-clear if the first subpass
333 * to use the attachment can't handle fast-clears.
335 enum anv_fast_clear_type fast_clear_type
=
336 anv_layout_to_fast_clear_type(&device
->info
, iview
->image
,
337 VK_IMAGE_ASPECT_COLOR_BIT
,
338 cmd_state
->pass
->attachments
[att
].first_subpass_layout
);
339 switch (fast_clear_type
) {
340 case ANV_FAST_CLEAR_NONE
:
341 att_state
->fast_clear
= false;
343 case ANV_FAST_CLEAR_DEFAULT_VALUE
:
344 att_state
->fast_clear
= att_state
->clear_color_is_zero
;
346 case ANV_FAST_CLEAR_ANY
:
347 att_state
->fast_clear
= true;
351 /* Potentially, we could do partial fast-clears but doing so has crazy
352 * alignment restrictions. It's easier to just restrict to full size
353 * fast clears for now.
355 if (render_area
.offset
.x
!= 0 ||
356 render_area
.offset
.y
!= 0 ||
357 render_area
.extent
.width
!= iview
->extent
.width
||
358 render_area
.extent
.height
!= iview
->extent
.height
)
359 att_state
->fast_clear
= false;
361 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
362 if (GEN_GEN
<= 8 && !att_state
->clear_color_is_zero_one
)
363 att_state
->fast_clear
= false;
365 /* We only allow fast clears to the first slice of an image (level 0,
366 * layer 0) and only for the entire slice. This guarantees us that, at
367 * any given time, there is only one clear color on any given image at
368 * any given time. At the time of our testing (Jan 17, 2018), there
369 * were no known applications which would benefit from fast-clearing
370 * more than just the first slice.
372 if (att_state
->fast_clear
&&
373 (iview
->planes
[0].isl
.base_level
> 0 ||
374 iview
->planes
[0].isl
.base_array_layer
> 0)) {
375 anv_perf_warn(device
->instance
, iview
->image
,
376 "Rendering with multi-lod or multi-layer framebuffer "
377 "with LOAD_OP_LOAD and baseMipLevel > 0 or "
378 "baseArrayLayer > 0. Not fast clearing.");
379 att_state
->fast_clear
= false;
380 } else if (att_state
->fast_clear
&& cmd_state
->framebuffer
->layers
> 1) {
381 anv_perf_warn(device
->instance
, iview
->image
,
382 "Rendering to a multi-layer framebuffer with "
383 "LOAD_OP_CLEAR. Only fast-clearing the first slice");
386 if (att_state
->fast_clear
)
387 *fast_clear_color
= clear_color
;
389 att_state
->fast_clear
= false;
394 depth_stencil_attachment_compute_aux_usage(struct anv_device
*device
,
395 struct anv_cmd_state
*cmd_state
,
396 uint32_t att
, VkRect2D render_area
)
398 struct anv_render_pass_attachment
*pass_att
=
399 &cmd_state
->pass
->attachments
[att
];
400 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[att
];
401 struct anv_image_view
*iview
= cmd_state
->attachments
[att
].image_view
;
403 /* These will be initialized after the first subpass transition. */
404 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
405 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
408 /* We don't do any HiZ or depth fast-clears on gen7 yet */
409 att_state
->fast_clear
= false;
413 if (!(att_state
->pending_clear_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)) {
414 /* If we're just clearing stencil, we can always HiZ clear */
415 att_state
->fast_clear
= true;
419 /* Default to false for now */
420 att_state
->fast_clear
= false;
422 /* We must have depth in order to have HiZ */
423 if (!(iview
->image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
))
426 const enum isl_aux_usage first_subpass_aux_usage
=
427 anv_layout_to_aux_usage(&device
->info
, iview
->image
,
428 VK_IMAGE_ASPECT_DEPTH_BIT
,
429 pass_att
->first_subpass_layout
);
430 if (!blorp_can_hiz_clear_depth(&device
->info
,
431 &iview
->image
->planes
[0].surface
.isl
,
432 first_subpass_aux_usage
,
433 iview
->planes
[0].isl
.base_level
,
434 iview
->planes
[0].isl
.base_array_layer
,
435 render_area
.offset
.x
,
436 render_area
.offset
.y
,
437 render_area
.offset
.x
+
438 render_area
.extent
.width
,
439 render_area
.offset
.y
+
440 render_area
.extent
.height
))
443 if (att_state
->clear_value
.depthStencil
.depth
!= ANV_HZ_FC_VAL
)
446 if (GEN_GEN
== 8 && anv_can_sample_with_hiz(&device
->info
, iview
->image
)) {
447 /* Only gen9+ supports returning ANV_HZ_FC_VAL when sampling a
448 * fast-cleared portion of a HiZ buffer. Testing has revealed that Gen8
449 * only supports returning 0.0f. Gens prior to gen8 do not support this
455 /* If we got here, then we can fast clear */
456 att_state
->fast_clear
= true;
460 need_input_attachment_state(const struct anv_render_pass_attachment
*att
)
462 if (!(att
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
465 /* We only allocate input attachment states for color surfaces. Compression
466 * is not yet enabled for depth textures and stencil doesn't allow
467 * compression so we can just use the texture surface state from the view.
469 return vk_format_is_color(att
->format
);
472 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
473 * the initial layout is undefined, the HiZ buffer and depth buffer will
474 * represent the same data at the end of this operation.
477 transition_depth_buffer(struct anv_cmd_buffer
*cmd_buffer
,
478 const struct anv_image
*image
,
479 VkImageLayout initial_layout
,
480 VkImageLayout final_layout
)
482 const bool hiz_enabled
= ISL_AUX_USAGE_HIZ
==
483 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
,
484 VK_IMAGE_ASPECT_DEPTH_BIT
, initial_layout
);
485 const bool enable_hiz
= ISL_AUX_USAGE_HIZ
==
486 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
,
487 VK_IMAGE_ASPECT_DEPTH_BIT
, final_layout
);
489 enum isl_aux_op hiz_op
;
490 if (hiz_enabled
&& !enable_hiz
) {
491 hiz_op
= ISL_AUX_OP_FULL_RESOLVE
;
492 } else if (!hiz_enabled
&& enable_hiz
) {
493 hiz_op
= ISL_AUX_OP_AMBIGUATE
;
495 assert(hiz_enabled
== enable_hiz
);
496 /* If the same buffer will be used, no resolves are necessary. */
497 hiz_op
= ISL_AUX_OP_NONE
;
500 if (hiz_op
!= ISL_AUX_OP_NONE
)
501 anv_image_hiz_op(cmd_buffer
, image
, VK_IMAGE_ASPECT_DEPTH_BIT
,
506 vk_image_layout_stencil_write_optimal(VkImageLayout layout
)
508 return layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
509 layout
== VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
||
510 layout
== VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR
;
513 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
514 * the initial layout is undefined, the HiZ buffer and depth buffer will
515 * represent the same data at the end of this operation.
518 transition_stencil_buffer(struct anv_cmd_buffer
*cmd_buffer
,
519 const struct anv_image
*image
,
520 uint32_t base_level
, uint32_t level_count
,
521 uint32_t base_layer
, uint32_t layer_count
,
522 VkImageLayout initial_layout
,
523 VkImageLayout final_layout
)
526 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
527 VK_IMAGE_ASPECT_STENCIL_BIT
);
529 /* On gen7, we have to store a texturable version of the stencil buffer in
530 * a shadow whenever VK_IMAGE_USAGE_SAMPLED_BIT is set and copy back and
531 * forth at strategic points. Stencil writes are only allowed in following
534 * - VK_IMAGE_LAYOUT_GENERAL
535 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
536 * - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
537 * - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
538 * - VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR
540 * For general, we have no nice opportunity to transition so we do the copy
541 * to the shadow unconditionally at the end of the subpass. For transfer
542 * destinations, we can update it as part of the transfer op. For the other
543 * layouts, we delay the copy until a transition into some other layout.
545 if (image
->planes
[plane
].shadow_surface
.isl
.size_B
> 0 &&
546 vk_image_layout_stencil_write_optimal(initial_layout
) &&
547 !vk_image_layout_stencil_write_optimal(final_layout
)) {
548 anv_image_copy_to_shadow(cmd_buffer
, image
,
549 VK_IMAGE_ASPECT_STENCIL_BIT
,
550 base_level
, level_count
,
551 base_layer
, layer_count
);
553 #endif /* GEN_GEN == 7 */
556 #define MI_PREDICATE_SRC0 0x2400
557 #define MI_PREDICATE_SRC1 0x2408
558 #define MI_PREDICATE_RESULT 0x2418
561 set_image_compressed_bit(struct anv_cmd_buffer
*cmd_buffer
,
562 const struct anv_image
*image
,
563 VkImageAspectFlagBits aspect
,
565 uint32_t base_layer
, uint32_t layer_count
,
568 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
570 /* We only have compression tracking for CCS_E */
571 if (image
->planes
[plane
].aux_usage
!= ISL_AUX_USAGE_CCS_E
)
574 for (uint32_t a
= 0; a
< layer_count
; a
++) {
575 uint32_t layer
= base_layer
+ a
;
576 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
577 sdi
.Address
= anv_image_get_compression_state_addr(cmd_buffer
->device
,
580 sdi
.ImmediateData
= compressed
? UINT32_MAX
: 0;
586 set_image_fast_clear_state(struct anv_cmd_buffer
*cmd_buffer
,
587 const struct anv_image
*image
,
588 VkImageAspectFlagBits aspect
,
589 enum anv_fast_clear_type fast_clear
)
591 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
592 sdi
.Address
= anv_image_get_fast_clear_type_addr(cmd_buffer
->device
,
594 sdi
.ImmediateData
= fast_clear
;
597 /* Whenever we have fast-clear, we consider that slice to be compressed.
598 * This makes building predicates much easier.
600 if (fast_clear
!= ANV_FAST_CLEAR_NONE
)
601 set_image_compressed_bit(cmd_buffer
, image
, aspect
, 0, 0, 1, true);
604 /* This is only really practical on haswell and above because it requires
605 * MI math in order to get it correct.
607 #if GEN_GEN >= 8 || GEN_IS_HASWELL
609 anv_cmd_compute_resolve_predicate(struct anv_cmd_buffer
*cmd_buffer
,
610 const struct anv_image
*image
,
611 VkImageAspectFlagBits aspect
,
612 uint32_t level
, uint32_t array_layer
,
613 enum isl_aux_op resolve_op
,
614 enum anv_fast_clear_type fast_clear_supported
)
616 struct gen_mi_builder b
;
617 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
619 const struct gen_mi_value fast_clear_type
=
620 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer
->device
,
623 if (resolve_op
== ISL_AUX_OP_FULL_RESOLVE
) {
624 /* In this case, we're doing a full resolve which means we want the
625 * resolve to happen if any compression (including fast-clears) is
628 * In order to simplify the logic a bit, we make the assumption that,
629 * if the first slice has been fast-cleared, it is also marked as
630 * compressed. See also set_image_fast_clear_state.
632 const struct gen_mi_value compression_state
=
633 gen_mi_mem32(anv_image_get_compression_state_addr(cmd_buffer
->device
,
635 level
, array_layer
));
636 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
),
638 gen_mi_store(&b
, compression_state
, gen_mi_imm(0));
640 if (level
== 0 && array_layer
== 0) {
641 /* If the predicate is true, we want to write 0 to the fast clear type
642 * and, if it's false, leave it alone. We can do this by writing
644 * clear_type = clear_type & ~predicate;
646 struct gen_mi_value new_fast_clear_type
=
647 gen_mi_iand(&b
, fast_clear_type
,
648 gen_mi_inot(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
)));
649 gen_mi_store(&b
, fast_clear_type
, new_fast_clear_type
);
651 } else if (level
== 0 && array_layer
== 0) {
652 /* In this case, we are doing a partial resolve to get rid of fast-clear
653 * colors. We don't care about the compression state but we do care
654 * about how much fast clear is allowed by the final layout.
656 assert(resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
);
657 assert(fast_clear_supported
< ANV_FAST_CLEAR_ANY
);
659 /* We need to compute (fast_clear_supported < image->fast_clear) */
660 struct gen_mi_value pred
=
661 gen_mi_ult(&b
, gen_mi_imm(fast_clear_supported
), fast_clear_type
);
662 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
),
663 gen_mi_value_ref(&b
, pred
));
665 /* If the predicate is true, we want to write 0 to the fast clear type
666 * and, if it's false, leave it alone. We can do this by writing
668 * clear_type = clear_type & ~predicate;
670 struct gen_mi_value new_fast_clear_type
=
671 gen_mi_iand(&b
, fast_clear_type
, gen_mi_inot(&b
, pred
));
672 gen_mi_store(&b
, fast_clear_type
, new_fast_clear_type
);
674 /* In this case, we're trying to do a partial resolve on a slice that
675 * doesn't have clear color. There's nothing to do.
677 assert(resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
);
681 /* Set src1 to 0 and use a != condition */
682 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
684 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
685 mip
.LoadOperation
= LOAD_LOADINV
;
686 mip
.CombineOperation
= COMBINE_SET
;
687 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
690 #endif /* GEN_GEN >= 8 || GEN_IS_HASWELL */
694 anv_cmd_simple_resolve_predicate(struct anv_cmd_buffer
*cmd_buffer
,
695 const struct anv_image
*image
,
696 VkImageAspectFlagBits aspect
,
697 uint32_t level
, uint32_t array_layer
,
698 enum isl_aux_op resolve_op
,
699 enum anv_fast_clear_type fast_clear_supported
)
701 struct gen_mi_builder b
;
702 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
704 struct gen_mi_value fast_clear_type_mem
=
705 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer
->device
,
708 /* This only works for partial resolves and only when the clear color is
709 * all or nothing. On the upside, this emits less command streamer code
710 * and works on Ivybridge and Bay Trail.
712 assert(resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
);
713 assert(fast_clear_supported
!= ANV_FAST_CLEAR_ANY
);
715 /* We don't support fast clears on anything other than the first slice. */
716 if (level
> 0 || array_layer
> 0)
719 /* On gen8, we don't have a concept of default clear colors because we
720 * can't sample from CCS surfaces. It's enough to just load the fast clear
721 * state into the predicate register.
723 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
), fast_clear_type_mem
);
724 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
725 gen_mi_store(&b
, fast_clear_type_mem
, gen_mi_imm(0));
727 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
728 mip
.LoadOperation
= LOAD_LOADINV
;
729 mip
.CombineOperation
= COMBINE_SET
;
730 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
733 #endif /* GEN_GEN <= 8 */
736 anv_cmd_predicated_ccs_resolve(struct anv_cmd_buffer
*cmd_buffer
,
737 const struct anv_image
*image
,
738 enum isl_format format
,
739 VkImageAspectFlagBits aspect
,
740 uint32_t level
, uint32_t array_layer
,
741 enum isl_aux_op resolve_op
,
742 enum anv_fast_clear_type fast_clear_supported
)
744 const uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
747 anv_cmd_compute_resolve_predicate(cmd_buffer
, image
,
748 aspect
, level
, array_layer
,
749 resolve_op
, fast_clear_supported
);
750 #else /* GEN_GEN <= 8 */
751 anv_cmd_simple_resolve_predicate(cmd_buffer
, image
,
752 aspect
, level
, array_layer
,
753 resolve_op
, fast_clear_supported
);
756 /* CCS_D only supports full resolves and BLORP will assert on us if we try
757 * to do a partial resolve on a CCS_D surface.
759 if (resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
&&
760 image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_NONE
)
761 resolve_op
= ISL_AUX_OP_FULL_RESOLVE
;
763 anv_image_ccs_op(cmd_buffer
, image
, format
, aspect
, level
,
764 array_layer
, 1, resolve_op
, NULL
, true);
768 anv_cmd_predicated_mcs_resolve(struct anv_cmd_buffer
*cmd_buffer
,
769 const struct anv_image
*image
,
770 enum isl_format format
,
771 VkImageAspectFlagBits aspect
,
772 uint32_t array_layer
,
773 enum isl_aux_op resolve_op
,
774 enum anv_fast_clear_type fast_clear_supported
)
776 assert(aspect
== VK_IMAGE_ASPECT_COLOR_BIT
);
777 assert(resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
);
779 #if GEN_GEN >= 8 || GEN_IS_HASWELL
780 anv_cmd_compute_resolve_predicate(cmd_buffer
, image
,
781 aspect
, 0, array_layer
,
782 resolve_op
, fast_clear_supported
);
784 anv_image_mcs_op(cmd_buffer
, image
, format
, aspect
,
785 array_layer
, 1, resolve_op
, NULL
, true);
787 unreachable("MCS resolves are unsupported on Ivybridge and Bay Trail");
792 genX(cmd_buffer_mark_image_written
)(struct anv_cmd_buffer
*cmd_buffer
,
793 const struct anv_image
*image
,
794 VkImageAspectFlagBits aspect
,
795 enum isl_aux_usage aux_usage
,
798 uint32_t layer_count
)
800 /* The aspect must be exactly one of the image aspects. */
801 assert(util_bitcount(aspect
) == 1 && (aspect
& image
->aspects
));
803 /* The only compression types with more than just fast-clears are MCS,
804 * CCS_E, and HiZ. With HiZ we just trust the layout and don't actually
805 * track the current fast-clear and compression state. This leaves us
806 * with just MCS and CCS_E.
808 if (aux_usage
!= ISL_AUX_USAGE_CCS_E
&&
809 aux_usage
!= ISL_AUX_USAGE_MCS
)
812 set_image_compressed_bit(cmd_buffer
, image
, aspect
,
813 level
, base_layer
, layer_count
, true);
817 init_fast_clear_color(struct anv_cmd_buffer
*cmd_buffer
,
818 const struct anv_image
*image
,
819 VkImageAspectFlagBits aspect
)
821 assert(cmd_buffer
&& image
);
822 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
824 set_image_fast_clear_state(cmd_buffer
, image
, aspect
,
825 ANV_FAST_CLEAR_NONE
);
827 /* Initialize the struct fields that are accessed for fast-clears so that
828 * the HW restrictions on the field values are satisfied.
830 struct anv_address addr
=
831 anv_image_get_clear_color_addr(cmd_buffer
->device
, image
, aspect
);
834 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
835 const unsigned num_dwords
= GEN_GEN
>= 10 ?
836 isl_dev
->ss
.clear_color_state_size
/ 4 :
837 isl_dev
->ss
.clear_value_size
/ 4;
838 for (unsigned i
= 0; i
< num_dwords
; i
++) {
839 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
841 sdi
.Address
.offset
+= i
* 4;
842 sdi
.ImmediateData
= 0;
846 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
848 if (GEN_GEN
>= 8 || GEN_IS_HASWELL
) {
849 /* Pre-SKL, the dword containing the clear values also contains
850 * other fields, so we need to initialize those fields to match the
851 * values that would be in a color attachment.
853 sdi
.ImmediateData
= ISL_CHANNEL_SELECT_RED
<< 25 |
854 ISL_CHANNEL_SELECT_GREEN
<< 22 |
855 ISL_CHANNEL_SELECT_BLUE
<< 19 |
856 ISL_CHANNEL_SELECT_ALPHA
<< 16;
857 } else if (GEN_GEN
== 7) {
858 /* On IVB, the dword containing the clear values also contains
859 * other fields that must be zero or can be zero.
861 sdi
.ImmediateData
= 0;
867 /* Copy the fast-clear value dword(s) between a surface state object and an
868 * image's fast clear state buffer.
871 genX(copy_fast_clear_dwords
)(struct anv_cmd_buffer
*cmd_buffer
,
872 struct anv_state surface_state
,
873 const struct anv_image
*image
,
874 VkImageAspectFlagBits aspect
,
875 bool copy_from_surface_state
)
877 assert(cmd_buffer
&& image
);
878 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
880 struct anv_address ss_clear_addr
= {
881 .bo
= cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
,
882 .offset
= surface_state
.offset
+
883 cmd_buffer
->device
->isl_dev
.ss
.clear_value_offset
,
885 const struct anv_address entry_addr
=
886 anv_image_get_clear_color_addr(cmd_buffer
->device
, image
, aspect
);
887 unsigned copy_size
= cmd_buffer
->device
->isl_dev
.ss
.clear_value_size
;
890 /* On gen7, the combination of commands used here(MI_LOAD_REGISTER_MEM
891 * and MI_STORE_REGISTER_MEM) can cause GPU hangs if any rendering is
892 * in-flight when they are issued even if the memory touched is not
893 * currently active for rendering. The weird bit is that it is not the
894 * MI_LOAD/STORE_REGISTER_MEM commands which hang but rather the in-flight
895 * rendering hangs such that the next stalling command after the
896 * MI_LOAD/STORE_REGISTER_MEM commands will catch the hang.
898 * It is unclear exactly why this hang occurs. Both MI commands come with
899 * warnings about the 3D pipeline but that doesn't seem to fully explain
900 * it. My (Jason's) best theory is that it has something to do with the
901 * fact that we're using a GPU state register as our temporary and that
902 * something with reading/writing it is causing problems.
904 * In order to work around this issue, we emit a PIPE_CONTROL with the
905 * command streamer stall bit set.
907 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
908 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
911 struct gen_mi_builder b
;
912 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
914 if (copy_from_surface_state
) {
915 gen_mi_memcpy(&b
, entry_addr
, ss_clear_addr
, copy_size
);
917 gen_mi_memcpy(&b
, ss_clear_addr
, entry_addr
, copy_size
);
919 /* Updating a surface state object may require that the state cache be
920 * invalidated. From the SKL PRM, Shared Functions -> State -> State
923 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
924 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
925 * modified [...], the L1 state cache must be invalidated to ensure
926 * the new surface or sampler state is fetched from system memory.
928 * In testing, SKL doesn't actually seem to need this, but HSW does.
930 cmd_buffer
->state
.pending_pipe_bits
|=
931 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
936 * @brief Transitions a color buffer from one layout to another.
938 * See section 6.1.1. Image Layout Transitions of the Vulkan 1.0.50 spec for
941 * @param level_count VK_REMAINING_MIP_LEVELS isn't supported.
942 * @param layer_count VK_REMAINING_ARRAY_LAYERS isn't supported. For 3D images,
943 * this represents the maximum layers to transition at each
944 * specified miplevel.
947 transition_color_buffer(struct anv_cmd_buffer
*cmd_buffer
,
948 const struct anv_image
*image
,
949 VkImageAspectFlagBits aspect
,
950 const uint32_t base_level
, uint32_t level_count
,
951 uint32_t base_layer
, uint32_t layer_count
,
952 VkImageLayout initial_layout
,
953 VkImageLayout final_layout
)
955 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
956 /* Validate the inputs. */
958 assert(image
&& image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
959 /* These values aren't supported for simplicity's sake. */
960 assert(level_count
!= VK_REMAINING_MIP_LEVELS
&&
961 layer_count
!= VK_REMAINING_ARRAY_LAYERS
);
962 /* Ensure the subresource range is valid. */
963 UNUSED
uint64_t last_level_num
= base_level
+ level_count
;
964 const uint32_t max_depth
= anv_minify(image
->extent
.depth
, base_level
);
965 UNUSED
const uint32_t image_layers
= MAX2(image
->array_size
, max_depth
);
966 assert((uint64_t)base_layer
+ layer_count
<= image_layers
);
967 assert(last_level_num
<= image
->levels
);
968 /* The spec disallows these final layouts. */
969 assert(final_layout
!= VK_IMAGE_LAYOUT_UNDEFINED
&&
970 final_layout
!= VK_IMAGE_LAYOUT_PREINITIALIZED
);
972 /* No work is necessary if the layout stays the same or if this subresource
973 * range lacks auxiliary data.
975 if (initial_layout
== final_layout
)
978 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
980 if (image
->planes
[plane
].shadow_surface
.isl
.size_B
> 0 &&
981 final_layout
== VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
) {
982 /* This surface is a linear compressed image with a tiled shadow surface
983 * for texturing. The client is about to use it in READ_ONLY_OPTIMAL so
984 * we need to ensure the shadow copy is up-to-date.
986 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
987 assert(image
->planes
[plane
].surface
.isl
.tiling
== ISL_TILING_LINEAR
);
988 assert(image
->planes
[plane
].shadow_surface
.isl
.tiling
!= ISL_TILING_LINEAR
);
989 assert(isl_format_is_compressed(image
->planes
[plane
].surface
.isl
.format
));
991 anv_image_copy_to_shadow(cmd_buffer
, image
,
992 VK_IMAGE_ASPECT_COLOR_BIT
,
993 base_level
, level_count
,
994 base_layer
, layer_count
);
997 if (base_layer
>= anv_image_aux_layers(image
, aspect
, base_level
))
1000 assert(image
->tiling
== VK_IMAGE_TILING_OPTIMAL
);
1002 if (initial_layout
== VK_IMAGE_LAYOUT_UNDEFINED
||
1003 initial_layout
== VK_IMAGE_LAYOUT_PREINITIALIZED
) {
1004 /* A subresource in the undefined layout may have been aliased and
1005 * populated with any arrangement of bits. Therefore, we must initialize
1006 * the related aux buffer and clear buffer entry with desirable values.
1007 * An initial layout of PREINITIALIZED is the same as UNDEFINED for
1008 * images with VK_IMAGE_TILING_OPTIMAL.
1010 * Initialize the relevant clear buffer entries.
1012 if (base_level
== 0 && base_layer
== 0)
1013 init_fast_clear_color(cmd_buffer
, image
, aspect
);
1015 /* Initialize the aux buffers to enable correct rendering. In order to
1016 * ensure that things such as storage images work correctly, aux buffers
1017 * need to be initialized to valid data.
1019 * Having an aux buffer with invalid data is a problem for two reasons:
1021 * 1) Having an invalid value in the buffer can confuse the hardware.
1022 * For instance, with CCS_E on SKL, a two-bit CCS value of 2 is
1023 * invalid and leads to the hardware doing strange things. It
1024 * doesn't hang as far as we can tell but rendering corruption can
1027 * 2) If this transition is into the GENERAL layout and we then use the
1028 * image as a storage image, then we must have the aux buffer in the
1029 * pass-through state so that, if we then go to texture from the
1030 * image, we get the results of our storage image writes and not the
1031 * fast clear color or other random data.
1033 * For CCS both of the problems above are real demonstrable issues. In
1034 * that case, the only thing we can do is to perform an ambiguate to
1035 * transition the aux surface into the pass-through state.
1037 * For MCS, (2) is never an issue because we don't support multisampled
1038 * storage images. In theory, issue (1) is a problem with MCS but we've
1039 * never seen it in the wild. For 4x and 16x, all bit patters could, in
1040 * theory, be interpreted as something but we don't know that all bit
1041 * patterns are actually valid. For 2x and 8x, you could easily end up
1042 * with the MCS referring to an invalid plane because not all bits of
1043 * the MCS value are actually used. Even though we've never seen issues
1044 * in the wild, it's best to play it safe and initialize the MCS. We
1045 * can use a fast-clear for MCS because we only ever touch from render
1046 * and texture (no image load store).
1048 if (image
->samples
== 1) {
1049 for (uint32_t l
= 0; l
< level_count
; l
++) {
1050 const uint32_t level
= base_level
+ l
;
1052 uint32_t aux_layers
= anv_image_aux_layers(image
, aspect
, level
);
1053 if (base_layer
>= aux_layers
)
1054 break; /* We will only get fewer layers as level increases */
1055 uint32_t level_layer_count
=
1056 MIN2(layer_count
, aux_layers
- base_layer
);
1058 anv_image_ccs_op(cmd_buffer
, image
,
1059 image
->planes
[plane
].surface
.isl
.format
,
1060 aspect
, level
, base_layer
, level_layer_count
,
1061 ISL_AUX_OP_AMBIGUATE
, NULL
, false);
1063 if (image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_CCS_E
) {
1064 set_image_compressed_bit(cmd_buffer
, image
, aspect
,
1065 level
, base_layer
, level_layer_count
,
1070 if (image
->samples
== 4 || image
->samples
== 16) {
1071 anv_perf_warn(cmd_buffer
->device
->instance
, image
,
1072 "Doing a potentially unnecessary fast-clear to "
1073 "define an MCS buffer.");
1076 assert(base_level
== 0 && level_count
== 1);
1077 anv_image_mcs_op(cmd_buffer
, image
,
1078 image
->planes
[plane
].surface
.isl
.format
,
1079 aspect
, base_layer
, layer_count
,
1080 ISL_AUX_OP_FAST_CLEAR
, NULL
, false);
1085 const enum isl_aux_usage initial_aux_usage
=
1086 anv_layout_to_aux_usage(devinfo
, image
, aspect
, initial_layout
);
1087 const enum isl_aux_usage final_aux_usage
=
1088 anv_layout_to_aux_usage(devinfo
, image
, aspect
, final_layout
);
1090 /* The current code assumes that there is no mixing of CCS_E and CCS_D.
1091 * We can handle transitions between CCS_D/E to and from NONE. What we
1092 * don't yet handle is switching between CCS_E and CCS_D within a given
1093 * image. Doing so in a performant way requires more detailed aux state
1094 * tracking such as what is done in i965. For now, just assume that we
1095 * only have one type of compression.
1097 assert(initial_aux_usage
== ISL_AUX_USAGE_NONE
||
1098 final_aux_usage
== ISL_AUX_USAGE_NONE
||
1099 initial_aux_usage
== final_aux_usage
);
1101 /* If initial aux usage is NONE, there is nothing to resolve */
1102 if (initial_aux_usage
== ISL_AUX_USAGE_NONE
)
1105 enum isl_aux_op resolve_op
= ISL_AUX_OP_NONE
;
1107 /* If the initial layout supports more fast clear than the final layout
1108 * then we need at least a partial resolve.
1110 const enum anv_fast_clear_type initial_fast_clear
=
1111 anv_layout_to_fast_clear_type(devinfo
, image
, aspect
, initial_layout
);
1112 const enum anv_fast_clear_type final_fast_clear
=
1113 anv_layout_to_fast_clear_type(devinfo
, image
, aspect
, final_layout
);
1114 if (final_fast_clear
< initial_fast_clear
)
1115 resolve_op
= ISL_AUX_OP_PARTIAL_RESOLVE
;
1117 if (initial_aux_usage
== ISL_AUX_USAGE_CCS_E
&&
1118 final_aux_usage
!= ISL_AUX_USAGE_CCS_E
)
1119 resolve_op
= ISL_AUX_OP_FULL_RESOLVE
;
1121 if (resolve_op
== ISL_AUX_OP_NONE
)
1124 /* Perform a resolve to synchronize data between the main and aux buffer.
1125 * Before we begin, we must satisfy the cache flushing requirement specified
1126 * in the Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)":
1128 * Any transition from any value in {Clear, Render, Resolve} to a
1129 * different value in {Clear, Render, Resolve} requires end of pipe
1132 * We perform a flush of the write cache before and after the clear and
1133 * resolve operations to meet this requirement.
1135 * Unlike other drawing, fast clear operations are not properly
1136 * synchronized. The first PIPE_CONTROL here likely ensures that the
1137 * contents of the previous render or clear hit the render target before we
1138 * resolve and the second likely ensures that the resolve is complete before
1139 * we do any more rendering or clearing.
1141 cmd_buffer
->state
.pending_pipe_bits
|=
1142 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
1144 for (uint32_t l
= 0; l
< level_count
; l
++) {
1145 uint32_t level
= base_level
+ l
;
1147 uint32_t aux_layers
= anv_image_aux_layers(image
, aspect
, level
);
1148 if (base_layer
>= aux_layers
)
1149 break; /* We will only get fewer layers as level increases */
1150 uint32_t level_layer_count
=
1151 MIN2(layer_count
, aux_layers
- base_layer
);
1153 for (uint32_t a
= 0; a
< level_layer_count
; a
++) {
1154 uint32_t array_layer
= base_layer
+ a
;
1155 if (image
->samples
== 1) {
1156 anv_cmd_predicated_ccs_resolve(cmd_buffer
, image
,
1157 image
->planes
[plane
].surface
.isl
.format
,
1158 aspect
, level
, array_layer
, resolve_op
,
1161 /* We only support fast-clear on the first layer so partial
1162 * resolves should not be used on other layers as they will use
1163 * the clear color stored in memory that is only valid for layer0.
1165 if (resolve_op
== ISL_AUX_OP_PARTIAL_RESOLVE
&&
1169 anv_cmd_predicated_mcs_resolve(cmd_buffer
, image
,
1170 image
->planes
[plane
].surface
.isl
.format
,
1171 aspect
, array_layer
, resolve_op
,
1177 cmd_buffer
->state
.pending_pipe_bits
|=
1178 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
1182 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
1185 genX(cmd_buffer_setup_attachments
)(struct anv_cmd_buffer
*cmd_buffer
,
1186 struct anv_render_pass
*pass
,
1187 const VkRenderPassBeginInfo
*begin
)
1189 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
1190 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
1191 struct anv_framebuffer
*framebuffer
= cmd_buffer
->state
.framebuffer
;
1193 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
1195 if (pass
->attachment_count
> 0) {
1196 state
->attachments
= vk_alloc(&cmd_buffer
->pool
->alloc
,
1197 pass
->attachment_count
*
1198 sizeof(state
->attachments
[0]),
1199 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1200 if (state
->attachments
== NULL
) {
1201 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
1202 return anv_batch_set_error(&cmd_buffer
->batch
,
1203 VK_ERROR_OUT_OF_HOST_MEMORY
);
1206 state
->attachments
= NULL
;
1209 /* Reserve one for the NULL state. */
1210 unsigned num_states
= 1;
1211 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
1212 if (vk_format_is_color(pass
->attachments
[i
].format
))
1215 if (need_input_attachment_state(&pass
->attachments
[i
]))
1219 const uint32_t ss_stride
= align_u32(isl_dev
->ss
.size
, isl_dev
->ss
.align
);
1220 state
->render_pass_states
=
1221 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
1222 num_states
* ss_stride
, isl_dev
->ss
.align
);
1224 struct anv_state next_state
= state
->render_pass_states
;
1225 next_state
.alloc_size
= isl_dev
->ss
.size
;
1227 state
->null_surface_state
= next_state
;
1228 next_state
.offset
+= ss_stride
;
1229 next_state
.map
+= ss_stride
;
1231 const VkRenderPassAttachmentBeginInfoKHR
*begin_attachment
=
1232 vk_find_struct_const(begin
, RENDER_PASS_ATTACHMENT_BEGIN_INFO_KHR
);
1234 if (begin
&& !begin_attachment
)
1235 assert(pass
->attachment_count
== framebuffer
->attachment_count
);
1237 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
1238 if (vk_format_is_color(pass
->attachments
[i
].format
)) {
1239 state
->attachments
[i
].color
.state
= next_state
;
1240 next_state
.offset
+= ss_stride
;
1241 next_state
.map
+= ss_stride
;
1244 if (need_input_attachment_state(&pass
->attachments
[i
])) {
1245 state
->attachments
[i
].input
.state
= next_state
;
1246 next_state
.offset
+= ss_stride
;
1247 next_state
.map
+= ss_stride
;
1250 if (begin_attachment
&& begin_attachment
->attachmentCount
!= 0) {
1251 assert(begin_attachment
->attachmentCount
== pass
->attachment_count
);
1252 ANV_FROM_HANDLE(anv_image_view
, iview
, begin_attachment
->pAttachments
[i
]);
1253 cmd_buffer
->state
.attachments
[i
].image_view
= iview
;
1254 } else if (framebuffer
&& i
< framebuffer
->attachment_count
) {
1255 cmd_buffer
->state
.attachments
[i
].image_view
= framebuffer
->attachments
[i
];
1258 assert(next_state
.offset
== state
->render_pass_states
.offset
+
1259 state
->render_pass_states
.alloc_size
);
1262 isl_null_fill_state(isl_dev
, state
->null_surface_state
.map
,
1263 isl_extent3d(framebuffer
->width
,
1264 framebuffer
->height
,
1265 framebuffer
->layers
));
1267 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
1268 struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
1269 VkImageAspectFlags att_aspects
= vk_format_aspects(att
->format
);
1270 VkImageAspectFlags clear_aspects
= 0;
1271 VkImageAspectFlags load_aspects
= 0;
1273 if (att_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
1274 /* color attachment */
1275 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
1276 clear_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
1277 } else if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_LOAD
) {
1278 load_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
1281 /* depthstencil attachment */
1282 if (att_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
1283 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
1284 clear_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
1285 } else if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_LOAD
) {
1286 load_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
1289 if (att_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
1290 if (att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
1291 clear_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
1292 } else if (att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_LOAD
) {
1293 load_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
1298 state
->attachments
[i
].current_layout
= att
->initial_layout
;
1299 state
->attachments
[i
].current_stencil_layout
= att
->stencil_initial_layout
;
1300 state
->attachments
[i
].pending_clear_aspects
= clear_aspects
;
1301 state
->attachments
[i
].pending_load_aspects
= load_aspects
;
1303 state
->attachments
[i
].clear_value
= begin
->pClearValues
[i
];
1305 struct anv_image_view
*iview
= cmd_buffer
->state
.attachments
[i
].image_view
;
1306 anv_assert(iview
->vk_format
== att
->format
);
1308 const uint32_t num_layers
= iview
->planes
[0].isl
.array_len
;
1309 state
->attachments
[i
].pending_clear_views
= (1 << num_layers
) - 1;
1311 union isl_color_value clear_color
= { .u32
= { 0, } };
1312 if (att_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
1313 anv_assert(iview
->n_planes
== 1);
1314 assert(att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
1315 color_attachment_compute_aux_usage(cmd_buffer
->device
,
1316 state
, i
, begin
->renderArea
,
1319 anv_image_fill_surface_state(cmd_buffer
->device
,
1321 VK_IMAGE_ASPECT_COLOR_BIT
,
1322 &iview
->planes
[0].isl
,
1323 ISL_SURF_USAGE_RENDER_TARGET_BIT
,
1324 state
->attachments
[i
].aux_usage
,
1327 &state
->attachments
[i
].color
,
1330 add_surface_state_relocs(cmd_buffer
, state
->attachments
[i
].color
);
1332 depth_stencil_attachment_compute_aux_usage(cmd_buffer
->device
,
1337 if (need_input_attachment_state(&pass
->attachments
[i
])) {
1338 anv_image_fill_surface_state(cmd_buffer
->device
,
1340 VK_IMAGE_ASPECT_COLOR_BIT
,
1341 &iview
->planes
[0].isl
,
1342 ISL_SURF_USAGE_TEXTURE_BIT
,
1343 state
->attachments
[i
].input_aux_usage
,
1346 &state
->attachments
[i
].input
,
1349 add_surface_state_relocs(cmd_buffer
, state
->attachments
[i
].input
);
1358 genX(BeginCommandBuffer
)(
1359 VkCommandBuffer commandBuffer
,
1360 const VkCommandBufferBeginInfo
* pBeginInfo
)
1362 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1364 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
1365 * command buffer's state. Otherwise, we must *reset* its state. In both
1366 * cases we reset it.
1368 * From the Vulkan 1.0 spec:
1370 * If a command buffer is in the executable state and the command buffer
1371 * was allocated from a command pool with the
1372 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
1373 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
1374 * as if vkResetCommandBuffer had been called with
1375 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
1376 * the command buffer in the recording state.
1378 anv_cmd_buffer_reset(cmd_buffer
);
1380 cmd_buffer
->usage_flags
= pBeginInfo
->flags
;
1382 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
||
1383 !(cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
));
1385 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1387 /* We sometimes store vertex data in the dynamic state buffer for blorp
1388 * operations and our dynamic state stream may re-use data from previous
1389 * command buffers. In order to prevent stale cache data, we flush the VF
1390 * cache. We could do this on every blorp call but that's not really
1391 * needed as all of the data will get written by the CPU prior to the GPU
1392 * executing anything. The chances are fairly high that they will use
1393 * blorp at least once per primary command buffer so it shouldn't be
1396 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
)
1397 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
1399 /* We send an "Indirect State Pointers Disable" packet at
1400 * EndCommandBuffer, so all push contant packets are ignored during a
1401 * context restore. Documentation says after that command, we need to
1402 * emit push constants again before any rendering operation. So we
1403 * flag them dirty here to make sure they get emitted.
1405 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
1407 VkResult result
= VK_SUCCESS
;
1408 if (cmd_buffer
->usage_flags
&
1409 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
1410 assert(pBeginInfo
->pInheritanceInfo
);
1411 cmd_buffer
->state
.pass
=
1412 anv_render_pass_from_handle(pBeginInfo
->pInheritanceInfo
->renderPass
);
1413 cmd_buffer
->state
.subpass
=
1414 &cmd_buffer
->state
.pass
->subpasses
[pBeginInfo
->pInheritanceInfo
->subpass
];
1416 /* This is optional in the inheritance info. */
1417 cmd_buffer
->state
.framebuffer
=
1418 anv_framebuffer_from_handle(pBeginInfo
->pInheritanceInfo
->framebuffer
);
1420 result
= genX(cmd_buffer_setup_attachments
)(cmd_buffer
,
1421 cmd_buffer
->state
.pass
, NULL
);
1423 /* Record that HiZ is enabled if we can. */
1424 if (cmd_buffer
->state
.framebuffer
) {
1425 const struct anv_image_view
* const iview
=
1426 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
1429 VkImageLayout layout
=
1430 cmd_buffer
->state
.subpass
->depth_stencil_attachment
->layout
;
1432 enum isl_aux_usage aux_usage
=
1433 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, iview
->image
,
1434 VK_IMAGE_ASPECT_DEPTH_BIT
, layout
);
1436 cmd_buffer
->state
.hiz_enabled
= aux_usage
== ISL_AUX_USAGE_HIZ
;
1440 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
1443 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1444 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
) {
1445 const VkCommandBufferInheritanceConditionalRenderingInfoEXT
*conditional_rendering_info
=
1446 vk_find_struct_const(pBeginInfo
->pInheritanceInfo
->pNext
, COMMAND_BUFFER_INHERITANCE_CONDITIONAL_RENDERING_INFO_EXT
);
1448 /* If secondary buffer supports conditional rendering
1449 * we should emit commands as if conditional rendering is enabled.
1451 cmd_buffer
->state
.conditional_render_enabled
=
1452 conditional_rendering_info
&& conditional_rendering_info
->conditionalRenderingEnable
;
1459 /* From the PRM, Volume 2a:
1461 * "Indirect State Pointers Disable
1463 * At the completion of the post-sync operation associated with this pipe
1464 * control packet, the indirect state pointers in the hardware are
1465 * considered invalid; the indirect pointers are not saved in the context.
1466 * If any new indirect state commands are executed in the command stream
1467 * while the pipe control is pending, the new indirect state commands are
1470 * [DevIVB+]: Using Invalidate State Pointer (ISP) only inhibits context
1471 * restoring of Push Constant (3DSTATE_CONSTANT_*) commands. Push Constant
1472 * commands are only considered as Indirect State Pointers. Once ISP is
1473 * issued in a context, SW must initialize by programming push constant
1474 * commands for all the shaders (at least to zero length) before attempting
1475 * any rendering operation for the same context."
1477 * 3DSTATE_CONSTANT_* packets are restored during a context restore,
1478 * even though they point to a BO that has been already unreferenced at
1479 * the end of the previous batch buffer. This has been fine so far since
1480 * we are protected by these scratch page (every address not covered by
1481 * a BO should be pointing to the scratch page). But on CNL, it is
1482 * causing a GPU hang during context restore at the 3DSTATE_CONSTANT_*
1485 * The flag "Indirect State Pointers Disable" in PIPE_CONTROL tells the
1486 * hardware to ignore previous 3DSTATE_CONSTANT_* packets during a
1487 * context restore, so the mentioned hang doesn't happen. However,
1488 * software must program push constant commands for all stages prior to
1489 * rendering anything. So we flag them dirty in BeginCommandBuffer.
1491 * Finally, we also make sure to stall at pixel scoreboard to make sure the
1492 * constants have been loaded into the EUs prior to disable the push constants
1493 * so that it doesn't hang a previous 3DPRIMITIVE.
1496 emit_isp_disable(struct anv_cmd_buffer
*cmd_buffer
)
1498 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1499 pc
.StallAtPixelScoreboard
= true;
1500 pc
.CommandStreamerStallEnable
= true;
1502 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1503 pc
.IndirectStatePointersDisable
= true;
1504 pc
.CommandStreamerStallEnable
= true;
1509 genX(EndCommandBuffer
)(
1510 VkCommandBuffer commandBuffer
)
1512 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1514 if (anv_batch_has_error(&cmd_buffer
->batch
))
1515 return cmd_buffer
->batch
.status
;
1517 /* We want every command buffer to start with the PMA fix in a known state,
1518 * so we disable it at the end of the command buffer.
1520 genX(cmd_buffer_enable_pma_fix
)(cmd_buffer
, false);
1522 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1524 emit_isp_disable(cmd_buffer
);
1526 anv_cmd_buffer_end_batch_buffer(cmd_buffer
);
1532 genX(CmdExecuteCommands
)(
1533 VkCommandBuffer commandBuffer
,
1534 uint32_t commandBufferCount
,
1535 const VkCommandBuffer
* pCmdBuffers
)
1537 ANV_FROM_HANDLE(anv_cmd_buffer
, primary
, commandBuffer
);
1539 assert(primary
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1541 if (anv_batch_has_error(&primary
->batch
))
1544 /* The secondary command buffers will assume that the PMA fix is disabled
1545 * when they begin executing. Make sure this is true.
1547 genX(cmd_buffer_enable_pma_fix
)(primary
, false);
1549 /* The secondary command buffer doesn't know which textures etc. have been
1550 * flushed prior to their execution. Apply those flushes now.
1552 genX(cmd_buffer_apply_pipe_flushes
)(primary
);
1554 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
1555 ANV_FROM_HANDLE(anv_cmd_buffer
, secondary
, pCmdBuffers
[i
]);
1557 assert(secondary
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
1558 assert(!anv_batch_has_error(&secondary
->batch
));
1560 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1561 if (secondary
->state
.conditional_render_enabled
) {
1562 if (!primary
->state
.conditional_render_enabled
) {
1563 /* Secondary buffer is constructed as if it will be executed
1564 * with conditional rendering, we should satisfy this dependency
1565 * regardless of conditional rendering being enabled in primary.
1567 struct gen_mi_builder b
;
1568 gen_mi_builder_init(&b
, &primary
->batch
);
1569 gen_mi_store(&b
, gen_mi_reg64(ANV_PREDICATE_RESULT_REG
),
1570 gen_mi_imm(UINT64_MAX
));
1575 if (secondary
->usage_flags
&
1576 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
1577 /* If we're continuing a render pass from the primary, we need to
1578 * copy the surface states for the current subpass into the storage
1579 * we allocated for them in BeginCommandBuffer.
1581 struct anv_bo
*ss_bo
=
1582 primary
->device
->surface_state_pool
.block_pool
.bo
;
1583 struct anv_state src_state
= primary
->state
.render_pass_states
;
1584 struct anv_state dst_state
= secondary
->state
.render_pass_states
;
1585 assert(src_state
.alloc_size
== dst_state
.alloc_size
);
1587 genX(cmd_buffer_so_memcpy
)(primary
,
1588 (struct anv_address
) {
1590 .offset
= dst_state
.offset
,
1592 (struct anv_address
) {
1594 .offset
= src_state
.offset
,
1596 src_state
.alloc_size
);
1599 anv_cmd_buffer_add_secondary(primary
, secondary
);
1602 /* The secondary may have selected a different pipeline (3D or compute) and
1603 * may have changed the current L3$ configuration. Reset our tracking
1604 * variables to invalid values to ensure that we re-emit these in the case
1605 * where we do any draws or compute dispatches from the primary after the
1606 * secondary has returned.
1608 primary
->state
.current_pipeline
= UINT32_MAX
;
1609 primary
->state
.current_l3_config
= NULL
;
1610 primary
->state
.current_hash_scale
= 0;
1612 /* Each of the secondary command buffers will use its own state base
1613 * address. We need to re-emit state base address for the primary after
1614 * all of the secondaries are done.
1616 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
1619 genX(cmd_buffer_emit_state_base_address
)(primary
);
1622 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
1623 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
1624 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
1627 * Program the hardware to use the specified L3 configuration.
1630 genX(cmd_buffer_config_l3
)(struct anv_cmd_buffer
*cmd_buffer
,
1631 const struct gen_l3_config
*cfg
)
1634 if (cfg
== cmd_buffer
->state
.current_l3_config
)
1637 if (unlikely(INTEL_DEBUG
& DEBUG_L3
)) {
1638 intel_logd("L3 config transition: ");
1639 gen_dump_l3_config(cfg
, stderr
);
1642 UNUSED
const bool has_slm
= cfg
->n
[GEN_L3P_SLM
];
1644 /* According to the hardware docs, the L3 partitioning can only be changed
1645 * while the pipeline is completely drained and the caches are flushed,
1646 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
1648 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1649 pc
.DCFlushEnable
= true;
1650 pc
.PostSyncOperation
= NoWrite
;
1651 pc
.CommandStreamerStallEnable
= true;
1654 /* ...followed by a second pipelined PIPE_CONTROL that initiates
1655 * invalidation of the relevant caches. Note that because RO invalidation
1656 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
1657 * command is processed by the CS) we cannot combine it with the previous
1658 * stalling flush as the hardware documentation suggests, because that
1659 * would cause the CS to stall on previous rendering *after* RO
1660 * invalidation and wouldn't prevent the RO caches from being polluted by
1661 * concurrent rendering before the stall completes. This intentionally
1662 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
1663 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
1664 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
1665 * already guarantee that there is no concurrent GPGPU kernel execution
1666 * (see SKL HSD 2132585).
1668 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1669 pc
.TextureCacheInvalidationEnable
= true;
1670 pc
.ConstantCacheInvalidationEnable
= true;
1671 pc
.InstructionCacheInvalidateEnable
= true;
1672 pc
.StateCacheInvalidationEnable
= true;
1673 pc
.PostSyncOperation
= NoWrite
;
1676 /* Now send a third stalling flush to make sure that invalidation is
1677 * complete when the L3 configuration registers are modified.
1679 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1680 pc
.DCFlushEnable
= true;
1681 pc
.PostSyncOperation
= NoWrite
;
1682 pc
.CommandStreamerStallEnable
= true;
1687 assert(!cfg
->n
[GEN_L3P_IS
] && !cfg
->n
[GEN_L3P_C
] && !cfg
->n
[GEN_L3P_T
]);
1690 #define L3_ALLOCATION_REG GENX(L3ALLOC)
1691 #define L3_ALLOCATION_REG_num GENX(L3ALLOC_num)
1693 #define L3_ALLOCATION_REG GENX(L3CNTLREG)
1694 #define L3_ALLOCATION_REG_num GENX(L3CNTLREG_num)
1698 anv_pack_struct(&l3cr
, L3_ALLOCATION_REG
,
1700 .SLMEnable
= has_slm
,
1703 /* WA_1406697149: Bit 9 "Error Detection Behavior Control" must be set
1704 * in L3CNTLREG register. The default setting of the bit is not the
1705 * desirable behavior.
1707 .ErrorDetectionBehaviorControl
= true,
1708 .UseFullWays
= true,
1710 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
1711 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
1712 .DCAllocation
= cfg
->n
[GEN_L3P_DC
],
1713 .AllAllocation
= cfg
->n
[GEN_L3P_ALL
]);
1715 /* Set up the L3 partitioning. */
1716 emit_lri(&cmd_buffer
->batch
, L3_ALLOCATION_REG_num
, l3cr
);
1720 const bool has_dc
= cfg
->n
[GEN_L3P_DC
] || cfg
->n
[GEN_L3P_ALL
];
1721 const bool has_is
= cfg
->n
[GEN_L3P_IS
] || cfg
->n
[GEN_L3P_RO
] ||
1722 cfg
->n
[GEN_L3P_ALL
];
1723 const bool has_c
= cfg
->n
[GEN_L3P_C
] || cfg
->n
[GEN_L3P_RO
] ||
1724 cfg
->n
[GEN_L3P_ALL
];
1725 const bool has_t
= cfg
->n
[GEN_L3P_T
] || cfg
->n
[GEN_L3P_RO
] ||
1726 cfg
->n
[GEN_L3P_ALL
];
1728 assert(!cfg
->n
[GEN_L3P_ALL
]);
1730 /* When enabled SLM only uses a portion of the L3 on half of the banks,
1731 * the matching space on the remaining banks has to be allocated to a
1732 * client (URB for all validated configurations) set to the
1733 * lower-bandwidth 2-bank address hashing mode.
1735 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
1736 const bool urb_low_bw
= has_slm
&& !devinfo
->is_baytrail
;
1737 assert(!urb_low_bw
|| cfg
->n
[GEN_L3P_URB
] == cfg
->n
[GEN_L3P_SLM
]);
1739 /* Minimum number of ways that can be allocated to the URB. */
1740 const unsigned n0_urb
= devinfo
->is_baytrail
? 32 : 0;
1741 assert(cfg
->n
[GEN_L3P_URB
] >= n0_urb
);
1743 uint32_t l3sqcr1
, l3cr2
, l3cr3
;
1744 anv_pack_struct(&l3sqcr1
, GENX(L3SQCREG1
),
1745 .ConvertDC_UC
= !has_dc
,
1746 .ConvertIS_UC
= !has_is
,
1747 .ConvertC_UC
= !has_c
,
1748 .ConvertT_UC
= !has_t
);
1750 GEN_IS_HASWELL
? HSW_L3SQCREG1_SQGHPCI_DEFAULT
:
1751 devinfo
->is_baytrail
? VLV_L3SQCREG1_SQGHPCI_DEFAULT
:
1752 IVB_L3SQCREG1_SQGHPCI_DEFAULT
;
1754 anv_pack_struct(&l3cr2
, GENX(L3CNTLREG2
),
1755 .SLMEnable
= has_slm
,
1756 .URBLowBandwidth
= urb_low_bw
,
1757 .URBAllocation
= cfg
->n
[GEN_L3P_URB
] - n0_urb
,
1759 .ALLAllocation
= cfg
->n
[GEN_L3P_ALL
],
1761 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
1762 .DCAllocation
= cfg
->n
[GEN_L3P_DC
]);
1764 anv_pack_struct(&l3cr3
, GENX(L3CNTLREG3
),
1765 .ISAllocation
= cfg
->n
[GEN_L3P_IS
],
1766 .ISLowBandwidth
= 0,
1767 .CAllocation
= cfg
->n
[GEN_L3P_C
],
1769 .TAllocation
= cfg
->n
[GEN_L3P_T
],
1770 .TLowBandwidth
= 0);
1772 /* Set up the L3 partitioning. */
1773 emit_lri(&cmd_buffer
->batch
, GENX(L3SQCREG1_num
), l3sqcr1
);
1774 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG2_num
), l3cr2
);
1775 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG3_num
), l3cr3
);
1778 if (cmd_buffer
->device
->instance
->physicalDevice
.cmd_parser_version
>= 4) {
1779 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
1780 * them disabled to avoid crashing the system hard.
1782 uint32_t scratch1
, chicken3
;
1783 anv_pack_struct(&scratch1
, GENX(SCRATCH1
),
1784 .L3AtomicDisable
= !has_dc
);
1785 anv_pack_struct(&chicken3
, GENX(CHICKEN3
),
1786 .L3AtomicDisableMask
= true,
1787 .L3AtomicDisable
= !has_dc
);
1788 emit_lri(&cmd_buffer
->batch
, GENX(SCRATCH1_num
), scratch1
);
1789 emit_lri(&cmd_buffer
->batch
, GENX(CHICKEN3_num
), chicken3
);
1795 cmd_buffer
->state
.current_l3_config
= cfg
;
1799 genX(cmd_buffer_apply_pipe_flushes
)(struct anv_cmd_buffer
*cmd_buffer
)
1801 enum anv_pipe_bits bits
= cmd_buffer
->state
.pending_pipe_bits
;
1803 /* Flushes are pipelined while invalidations are handled immediately.
1804 * Therefore, if we're flushing anything then we need to schedule a stall
1805 * before any invalidations can happen.
1807 if (bits
& ANV_PIPE_FLUSH_BITS
)
1808 bits
|= ANV_PIPE_NEEDS_CS_STALL_BIT
;
1810 /* If we're going to do an invalidate and we have a pending CS stall that
1811 * has yet to be resolved, we do the CS stall now.
1813 if ((bits
& ANV_PIPE_INVALIDATE_BITS
) &&
1814 (bits
& ANV_PIPE_NEEDS_CS_STALL_BIT
)) {
1815 bits
|= ANV_PIPE_CS_STALL_BIT
;
1816 bits
&= ~ANV_PIPE_NEEDS_CS_STALL_BIT
;
1819 if (GEN_GEN
>= 12 &&
1820 ((bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
) ||
1821 (bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
))) {
1822 /* From the PIPE_CONTROL instruction table, bit 28 (Tile Cache Flush
1825 * Unified Cache (Tile Cache Disabled):
1827 * When the Color and Depth (Z) streams are enabled to be cached in
1828 * the DC space of L2, Software must use "Render Target Cache Flush
1829 * Enable" and "Depth Cache Flush Enable" along with "Tile Cache
1830 * Flush" for getting the color and depth (Z) write data to be
1831 * globally observable. In this mode of operation it is not required
1832 * to set "CS Stall" upon setting "Tile Cache Flush" bit.
1834 bits
|= ANV_PIPE_TILE_CACHE_FLUSH_BIT
;
1837 if (bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
)) {
1838 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
1840 pipe
.TileCacheFlushEnable
= bits
& ANV_PIPE_TILE_CACHE_FLUSH_BIT
;
1842 pipe
.DepthCacheFlushEnable
= bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
1843 pipe
.DCFlushEnable
= bits
& ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
1844 pipe
.RenderTargetCacheFlushEnable
=
1845 bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
1847 pipe
.DepthStallEnable
= bits
& ANV_PIPE_DEPTH_STALL_BIT
;
1848 pipe
.CommandStreamerStallEnable
= bits
& ANV_PIPE_CS_STALL_BIT
;
1849 pipe
.StallAtPixelScoreboard
= bits
& ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
1852 * According to the Broadwell documentation, any PIPE_CONTROL with the
1853 * "Command Streamer Stall" bit set must also have another bit set,
1854 * with five different options:
1856 * - Render Target Cache Flush
1857 * - Depth Cache Flush
1858 * - Stall at Pixel Scoreboard
1859 * - Post-Sync Operation
1863 * I chose "Stall at Pixel Scoreboard" since that's what we use in
1864 * mesa and it seems to work fine. The choice is fairly arbitrary.
1866 if ((bits
& ANV_PIPE_CS_STALL_BIT
) &&
1867 !(bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_DEPTH_STALL_BIT
|
1868 ANV_PIPE_STALL_AT_SCOREBOARD_BIT
)))
1869 pipe
.StallAtPixelScoreboard
= true;
1872 /* If a render target flush was emitted, then we can toggle off the bit
1873 * saying that render target writes are ongoing.
1875 if (bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
)
1876 bits
&= ~(ANV_PIPE_RENDER_TARGET_BUFFER_WRITES
);
1878 bits
&= ~(ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
);
1881 if (bits
& ANV_PIPE_INVALIDATE_BITS
) {
1882 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
1884 * "If the VF Cache Invalidation Enable is set to a 1 in a
1885 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields sets to
1886 * 0, with the VF Cache Invalidation Enable set to 0 needs to be sent
1887 * prior to the PIPE_CONTROL with VF Cache Invalidation Enable set to
1890 * This appears to hang Broadwell, so we restrict it to just gen9.
1892 if (GEN_GEN
== 9 && (bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
))
1893 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
);
1895 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
1896 pipe
.StateCacheInvalidationEnable
=
1897 bits
& ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
1898 pipe
.ConstantCacheInvalidationEnable
=
1899 bits
& ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
1900 pipe
.VFCacheInvalidationEnable
=
1901 bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
1902 pipe
.TextureCacheInvalidationEnable
=
1903 bits
& ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
1904 pipe
.InstructionCacheInvalidateEnable
=
1905 bits
& ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT
;
1907 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
1909 * "When VF Cache Invalidate is set “Post Sync Operation” must be
1910 * enabled to “Write Immediate Data” or “Write PS Depth Count” or
1911 * “Write Timestamp”.
1913 if (GEN_GEN
== 9 && pipe
.VFCacheInvalidationEnable
) {
1914 pipe
.PostSyncOperation
= WriteImmediateData
;
1916 (struct anv_address
) { cmd_buffer
->device
->workaround_bo
, 0 };
1920 bits
&= ~ANV_PIPE_INVALIDATE_BITS
;
1923 cmd_buffer
->state
.pending_pipe_bits
= bits
;
1926 void genX(CmdPipelineBarrier
)(
1927 VkCommandBuffer commandBuffer
,
1928 VkPipelineStageFlags srcStageMask
,
1929 VkPipelineStageFlags destStageMask
,
1931 uint32_t memoryBarrierCount
,
1932 const VkMemoryBarrier
* pMemoryBarriers
,
1933 uint32_t bufferMemoryBarrierCount
,
1934 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
1935 uint32_t imageMemoryBarrierCount
,
1936 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
1938 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1940 /* XXX: Right now, we're really dumb and just flush whatever categories
1941 * the app asks for. One of these days we may make this a bit better
1942 * but right now that's all the hardware allows for in most areas.
1944 VkAccessFlags src_flags
= 0;
1945 VkAccessFlags dst_flags
= 0;
1947 for (uint32_t i
= 0; i
< memoryBarrierCount
; i
++) {
1948 src_flags
|= pMemoryBarriers
[i
].srcAccessMask
;
1949 dst_flags
|= pMemoryBarriers
[i
].dstAccessMask
;
1952 for (uint32_t i
= 0; i
< bufferMemoryBarrierCount
; i
++) {
1953 src_flags
|= pBufferMemoryBarriers
[i
].srcAccessMask
;
1954 dst_flags
|= pBufferMemoryBarriers
[i
].dstAccessMask
;
1957 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
1958 src_flags
|= pImageMemoryBarriers
[i
].srcAccessMask
;
1959 dst_flags
|= pImageMemoryBarriers
[i
].dstAccessMask
;
1960 ANV_FROM_HANDLE(anv_image
, image
, pImageMemoryBarriers
[i
].image
);
1961 const VkImageSubresourceRange
*range
=
1962 &pImageMemoryBarriers
[i
].subresourceRange
;
1964 uint32_t base_layer
, layer_count
;
1965 if (image
->type
== VK_IMAGE_TYPE_3D
) {
1967 layer_count
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
);
1969 base_layer
= range
->baseArrayLayer
;
1970 layer_count
= anv_get_layerCount(image
, range
);
1973 if (range
->aspectMask
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
1974 transition_depth_buffer(cmd_buffer
, image
,
1975 pImageMemoryBarriers
[i
].oldLayout
,
1976 pImageMemoryBarriers
[i
].newLayout
);
1979 if (range
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
1980 transition_stencil_buffer(cmd_buffer
, image
,
1981 range
->baseMipLevel
,
1982 anv_get_levelCount(image
, range
),
1983 base_layer
, layer_count
,
1984 pImageMemoryBarriers
[i
].oldLayout
,
1985 pImageMemoryBarriers
[i
].newLayout
);
1988 if (range
->aspectMask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
1989 VkImageAspectFlags color_aspects
=
1990 anv_image_expand_aspects(image
, range
->aspectMask
);
1991 uint32_t aspect_bit
;
1992 anv_foreach_image_aspect_bit(aspect_bit
, image
, color_aspects
) {
1993 transition_color_buffer(cmd_buffer
, image
, 1UL << aspect_bit
,
1994 range
->baseMipLevel
,
1995 anv_get_levelCount(image
, range
),
1996 base_layer
, layer_count
,
1997 pImageMemoryBarriers
[i
].oldLayout
,
1998 pImageMemoryBarriers
[i
].newLayout
);
2003 cmd_buffer
->state
.pending_pipe_bits
|=
2004 anv_pipe_flush_bits_for_access_flags(src_flags
) |
2005 anv_pipe_invalidate_bits_for_access_flags(dst_flags
);
2009 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
2011 VkShaderStageFlags stages
=
2012 cmd_buffer
->state
.gfx
.base
.pipeline
->active_stages
;
2014 /* In order to avoid thrash, we assume that vertex and fragment stages
2015 * always exist. In the rare case where one is missing *and* the other
2016 * uses push concstants, this may be suboptimal. However, avoiding stalls
2017 * seems more important.
2019 stages
|= VK_SHADER_STAGE_FRAGMENT_BIT
| VK_SHADER_STAGE_VERTEX_BIT
;
2021 if (stages
== cmd_buffer
->state
.push_constant_stages
)
2025 const unsigned push_constant_kb
= 32;
2026 #elif GEN_IS_HASWELL
2027 const unsigned push_constant_kb
= cmd_buffer
->device
->info
.gt
== 3 ? 32 : 16;
2029 const unsigned push_constant_kb
= 16;
2032 const unsigned num_stages
=
2033 util_bitcount(stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
2034 unsigned size_per_stage
= push_constant_kb
/ num_stages
;
2036 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
2037 * units of 2KB. Incidentally, these are the same platforms that have
2038 * 32KB worth of push constant space.
2040 if (push_constant_kb
== 32)
2041 size_per_stage
&= ~1u;
2043 uint32_t kb_used
= 0;
2044 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
2045 unsigned push_size
= (stages
& (1 << i
)) ? size_per_stage
: 0;
2046 anv_batch_emit(&cmd_buffer
->batch
,
2047 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS
), alloc
) {
2048 alloc
._3DCommandSubOpcode
= 18 + i
;
2049 alloc
.ConstantBufferOffset
= (push_size
> 0) ? kb_used
: 0;
2050 alloc
.ConstantBufferSize
= push_size
;
2052 kb_used
+= push_size
;
2055 anv_batch_emit(&cmd_buffer
->batch
,
2056 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS
), alloc
) {
2057 alloc
.ConstantBufferOffset
= kb_used
;
2058 alloc
.ConstantBufferSize
= push_constant_kb
- kb_used
;
2061 cmd_buffer
->state
.push_constant_stages
= stages
;
2063 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
2065 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
2066 * the next 3DPRIMITIVE command after programming the
2067 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
2069 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
2070 * pipeline setup, we need to dirty push constants.
2072 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
2075 static struct anv_address
2076 anv_descriptor_set_address(struct anv_cmd_buffer
*cmd_buffer
,
2077 struct anv_descriptor_set
*set
)
2080 /* This is a normal descriptor set */
2081 return (struct anv_address
) {
2082 .bo
= set
->pool
->bo
,
2083 .offset
= set
->desc_mem
.offset
,
2086 /* This is a push descriptor set. We have to flag it as used on the GPU
2087 * so that the next time we push descriptors, we grab a new memory.
2089 struct anv_push_descriptor_set
*push_set
=
2090 (struct anv_push_descriptor_set
*)set
;
2091 push_set
->set_used_on_gpu
= true;
2093 return (struct anv_address
) {
2094 .bo
= cmd_buffer
->dynamic_state_stream
.state_pool
->block_pool
.bo
,
2095 .offset
= set
->desc_mem
.offset
,
2101 emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
2102 gl_shader_stage stage
,
2103 struct anv_state
*bt_state
)
2105 struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
2106 struct anv_cmd_pipeline_state
*pipe_state
;
2107 struct anv_pipeline
*pipeline
;
2108 uint32_t state_offset
;
2111 case MESA_SHADER_COMPUTE
:
2112 pipe_state
= &cmd_buffer
->state
.compute
.base
;
2115 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
2118 pipeline
= pipe_state
->pipeline
;
2120 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
2121 *bt_state
= (struct anv_state
) { 0, };
2125 struct anv_shader_bin
*bin
= pipeline
->shaders
[stage
];
2126 struct anv_pipeline_bind_map
*map
= &bin
->bind_map
;
2127 if (map
->surface_count
== 0) {
2128 *bt_state
= (struct anv_state
) { 0, };
2132 *bt_state
= anv_cmd_buffer_alloc_binding_table(cmd_buffer
,
2135 uint32_t *bt_map
= bt_state
->map
;
2137 if (bt_state
->map
== NULL
)
2138 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2140 /* We only need to emit relocs if we're not using softpin. If we are using
2141 * softpin then we always keep all user-allocated memory objects resident.
2143 const bool need_client_mem_relocs
=
2144 !cmd_buffer
->device
->instance
->physicalDevice
.use_softpin
;
2146 for (uint32_t s
= 0; s
< map
->surface_count
; s
++) {
2147 struct anv_pipeline_binding
*binding
= &map
->surface_to_descriptor
[s
];
2149 struct anv_state surface_state
;
2151 if (binding
->set
== ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
) {
2152 /* Color attachment binding */
2153 assert(stage
== MESA_SHADER_FRAGMENT
);
2154 if (binding
->index
< subpass
->color_count
) {
2155 const unsigned att
=
2156 subpass
->color_attachments
[binding
->index
].attachment
;
2158 /* From the Vulkan 1.0.46 spec:
2160 * "If any color or depth/stencil attachments are
2161 * VK_ATTACHMENT_UNUSED, then no writes occur for those
2164 if (att
== VK_ATTACHMENT_UNUSED
) {
2165 surface_state
= cmd_buffer
->state
.null_surface_state
;
2167 surface_state
= cmd_buffer
->state
.attachments
[att
].color
.state
;
2170 surface_state
= cmd_buffer
->state
.null_surface_state
;
2173 bt_map
[s
] = surface_state
.offset
+ state_offset
;
2175 } else if (binding
->set
== ANV_DESCRIPTOR_SET_SHADER_CONSTANTS
) {
2176 struct anv_state surface_state
=
2177 anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
2179 struct anv_address constant_data
= {
2180 .bo
= pipeline
->device
->dynamic_state_pool
.block_pool
.bo
,
2181 .offset
= pipeline
->shaders
[stage
]->constant_data
.offset
,
2183 unsigned constant_data_size
=
2184 pipeline
->shaders
[stage
]->constant_data_size
;
2186 const enum isl_format format
=
2187 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
);
2188 anv_fill_buffer_surface_state(cmd_buffer
->device
,
2189 surface_state
, format
,
2190 constant_data
, constant_data_size
, 1);
2192 bt_map
[s
] = surface_state
.offset
+ state_offset
;
2193 add_surface_reloc(cmd_buffer
, surface_state
, constant_data
);
2195 } else if (binding
->set
== ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS
) {
2196 /* This is always the first binding for compute shaders */
2197 assert(stage
== MESA_SHADER_COMPUTE
&& s
== 0);
2198 if (!get_cs_prog_data(pipeline
)->uses_num_work_groups
)
2201 struct anv_state surface_state
=
2202 anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
2204 const enum isl_format format
=
2205 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
);
2206 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
2208 cmd_buffer
->state
.compute
.num_workgroups
,
2210 bt_map
[s
] = surface_state
.offset
+ state_offset
;
2211 if (need_client_mem_relocs
) {
2212 add_surface_reloc(cmd_buffer
, surface_state
,
2213 cmd_buffer
->state
.compute
.num_workgroups
);
2216 } else if (binding
->set
== ANV_DESCRIPTOR_SET_DESCRIPTORS
) {
2217 /* This is a descriptor set buffer so the set index is actually
2218 * given by binding->binding. (Yes, that's confusing.)
2220 struct anv_descriptor_set
*set
=
2221 pipe_state
->descriptors
[binding
->index
];
2222 assert(set
->desc_mem
.alloc_size
);
2223 assert(set
->desc_surface_state
.alloc_size
);
2224 bt_map
[s
] = set
->desc_surface_state
.offset
+ state_offset
;
2225 add_surface_reloc(cmd_buffer
, set
->desc_surface_state
,
2226 anv_descriptor_set_address(cmd_buffer
, set
));
2230 const struct anv_descriptor
*desc
=
2231 &pipe_state
->descriptors
[binding
->set
]->descriptors
[binding
->index
];
2233 switch (desc
->type
) {
2234 case VK_DESCRIPTOR_TYPE_SAMPLER
:
2235 /* Nothing for us to do here */
2238 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
2239 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
: {
2240 struct anv_surface_state sstate
=
2241 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
2242 desc
->image_view
->planes
[binding
->plane
].general_sampler_surface_state
:
2243 desc
->image_view
->planes
[binding
->plane
].optimal_sampler_surface_state
;
2244 surface_state
= sstate
.state
;
2245 assert(surface_state
.alloc_size
);
2246 if (need_client_mem_relocs
)
2247 add_surface_state_relocs(cmd_buffer
, sstate
);
2250 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
2251 assert(stage
== MESA_SHADER_FRAGMENT
);
2252 if ((desc
->image_view
->aspect_mask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) == 0) {
2253 /* For depth and stencil input attachments, we treat it like any
2254 * old texture that a user may have bound.
2256 assert(desc
->image_view
->n_planes
== 1);
2257 struct anv_surface_state sstate
=
2258 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
2259 desc
->image_view
->planes
[0].general_sampler_surface_state
:
2260 desc
->image_view
->planes
[0].optimal_sampler_surface_state
;
2261 surface_state
= sstate
.state
;
2262 assert(surface_state
.alloc_size
);
2263 if (need_client_mem_relocs
)
2264 add_surface_state_relocs(cmd_buffer
, sstate
);
2266 /* For color input attachments, we create the surface state at
2267 * vkBeginRenderPass time so that we can include aux and clear
2268 * color information.
2270 assert(binding
->input_attachment_index
< subpass
->input_count
);
2271 const unsigned subpass_att
= binding
->input_attachment_index
;
2272 const unsigned att
= subpass
->input_attachments
[subpass_att
].attachment
;
2273 surface_state
= cmd_buffer
->state
.attachments
[att
].input
.state
;
2277 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
: {
2278 struct anv_surface_state sstate
= (binding
->write_only
)
2279 ? desc
->image_view
->planes
[binding
->plane
].writeonly_storage_surface_state
2280 : desc
->image_view
->planes
[binding
->plane
].storage_surface_state
;
2281 surface_state
= sstate
.state
;
2282 assert(surface_state
.alloc_size
);
2283 if (need_client_mem_relocs
)
2284 add_surface_state_relocs(cmd_buffer
, sstate
);
2288 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
2289 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
2290 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
2291 surface_state
= desc
->buffer_view
->surface_state
;
2292 assert(surface_state
.alloc_size
);
2293 if (need_client_mem_relocs
) {
2294 add_surface_reloc(cmd_buffer
, surface_state
,
2295 desc
->buffer_view
->address
);
2299 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
2300 /* If the shader never does any UBO pulls (this is a fairly common
2301 * case) then we don't need to fill out those binding table entries.
2302 * The real cost savings here is that we don't have to build the
2303 * surface state for them which is surprisingly expensive when it's
2306 if (!bin
->prog_data
->has_ubo_pull
)
2310 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
2311 /* Compute the offset within the buffer */
2312 uint32_t dynamic_offset
=
2313 pipe_state
->dynamic_offsets
[binding
->dynamic_offset_index
];
2314 uint64_t offset
= desc
->offset
+ dynamic_offset
;
2315 /* Clamp to the buffer size */
2316 offset
= MIN2(offset
, desc
->buffer
->size
);
2317 /* Clamp the range to the buffer size */
2318 uint32_t range
= MIN2(desc
->range
, desc
->buffer
->size
- offset
);
2320 struct anv_address address
=
2321 anv_address_add(desc
->buffer
->address
, offset
);
2324 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, 64, 64);
2325 enum isl_format format
=
2326 anv_isl_format_for_descriptor_type(desc
->type
);
2328 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
2329 format
, address
, range
, 1);
2330 if (need_client_mem_relocs
)
2331 add_surface_reloc(cmd_buffer
, surface_state
, address
);
2335 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
2336 surface_state
= (binding
->write_only
)
2337 ? desc
->buffer_view
->writeonly_storage_surface_state
2338 : desc
->buffer_view
->storage_surface_state
;
2339 assert(surface_state
.alloc_size
);
2340 if (need_client_mem_relocs
) {
2341 add_surface_reloc(cmd_buffer
, surface_state
,
2342 desc
->buffer_view
->address
);
2347 assert(!"Invalid descriptor type");
2351 bt_map
[s
] = surface_state
.offset
+ state_offset
;
2358 emit_samplers(struct anv_cmd_buffer
*cmd_buffer
,
2359 gl_shader_stage stage
,
2360 struct anv_state
*state
)
2362 struct anv_cmd_pipeline_state
*pipe_state
=
2363 stage
== MESA_SHADER_COMPUTE
? &cmd_buffer
->state
.compute
.base
:
2364 &cmd_buffer
->state
.gfx
.base
;
2365 struct anv_pipeline
*pipeline
= pipe_state
->pipeline
;
2367 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
2368 *state
= (struct anv_state
) { 0, };
2372 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
2373 if (map
->sampler_count
== 0) {
2374 *state
= (struct anv_state
) { 0, };
2378 uint32_t size
= map
->sampler_count
* 16;
2379 *state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 32);
2381 if (state
->map
== NULL
)
2382 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2384 for (uint32_t s
= 0; s
< map
->sampler_count
; s
++) {
2385 struct anv_pipeline_binding
*binding
= &map
->sampler_to_descriptor
[s
];
2386 const struct anv_descriptor
*desc
=
2387 &pipe_state
->descriptors
[binding
->set
]->descriptors
[binding
->index
];
2389 if (desc
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
2390 desc
->type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
2393 struct anv_sampler
*sampler
= desc
->sampler
;
2395 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
2396 * happens to be zero.
2398 if (sampler
== NULL
)
2401 memcpy(state
->map
+ (s
* 16),
2402 sampler
->state
[binding
->plane
], sizeof(sampler
->state
[0]));
2409 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
2411 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
2413 VkShaderStageFlags dirty
= cmd_buffer
->state
.descriptors_dirty
&
2414 pipeline
->active_stages
;
2416 VkResult result
= VK_SUCCESS
;
2417 anv_foreach_stage(s
, dirty
) {
2418 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
2419 if (result
!= VK_SUCCESS
)
2421 result
= emit_binding_table(cmd_buffer
, s
,
2422 &cmd_buffer
->state
.binding_tables
[s
]);
2423 if (result
!= VK_SUCCESS
)
2427 if (result
!= VK_SUCCESS
) {
2428 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2430 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
2431 if (result
!= VK_SUCCESS
)
2434 /* Re-emit state base addresses so we get the new surface state base
2435 * address before we start emitting binding tables etc.
2437 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
2439 /* Re-emit all active binding tables */
2440 dirty
|= pipeline
->active_stages
;
2441 anv_foreach_stage(s
, dirty
) {
2442 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
2443 if (result
!= VK_SUCCESS
) {
2444 anv_batch_set_error(&cmd_buffer
->batch
, result
);
2447 result
= emit_binding_table(cmd_buffer
, s
,
2448 &cmd_buffer
->state
.binding_tables
[s
]);
2449 if (result
!= VK_SUCCESS
) {
2450 anv_batch_set_error(&cmd_buffer
->batch
, result
);
2456 cmd_buffer
->state
.descriptors_dirty
&= ~dirty
;
2462 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
2465 static const uint32_t sampler_state_opcodes
[] = {
2466 [MESA_SHADER_VERTEX
] = 43,
2467 [MESA_SHADER_TESS_CTRL
] = 44, /* HS */
2468 [MESA_SHADER_TESS_EVAL
] = 45, /* DS */
2469 [MESA_SHADER_GEOMETRY
] = 46,
2470 [MESA_SHADER_FRAGMENT
] = 47,
2471 [MESA_SHADER_COMPUTE
] = 0,
2474 static const uint32_t binding_table_opcodes
[] = {
2475 [MESA_SHADER_VERTEX
] = 38,
2476 [MESA_SHADER_TESS_CTRL
] = 39,
2477 [MESA_SHADER_TESS_EVAL
] = 40,
2478 [MESA_SHADER_GEOMETRY
] = 41,
2479 [MESA_SHADER_FRAGMENT
] = 42,
2480 [MESA_SHADER_COMPUTE
] = 0,
2483 anv_foreach_stage(s
, stages
) {
2484 assert(s
< ARRAY_SIZE(binding_table_opcodes
));
2485 assert(binding_table_opcodes
[s
] > 0);
2487 if (cmd_buffer
->state
.samplers
[s
].alloc_size
> 0) {
2488 anv_batch_emit(&cmd_buffer
->batch
,
2489 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS
), ssp
) {
2490 ssp
._3DCommandSubOpcode
= sampler_state_opcodes
[s
];
2491 ssp
.PointertoVSSamplerState
= cmd_buffer
->state
.samplers
[s
].offset
;
2495 /* Always emit binding table pointers if we're asked to, since on SKL
2496 * this is what flushes push constants. */
2497 anv_batch_emit(&cmd_buffer
->batch
,
2498 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS
), btp
) {
2499 btp
._3DCommandSubOpcode
= binding_table_opcodes
[s
];
2500 btp
.PointertoVSBindingTable
= cmd_buffer
->state
.binding_tables
[s
].offset
;
2506 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
2507 VkShaderStageFlags dirty_stages
)
2509 const struct anv_cmd_graphics_state
*gfx_state
= &cmd_buffer
->state
.gfx
;
2510 const struct anv_pipeline
*pipeline
= gfx_state
->base
.pipeline
;
2512 static const uint32_t push_constant_opcodes
[] = {
2513 [MESA_SHADER_VERTEX
] = 21,
2514 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
2515 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
2516 [MESA_SHADER_GEOMETRY
] = 22,
2517 [MESA_SHADER_FRAGMENT
] = 23,
2518 [MESA_SHADER_COMPUTE
] = 0,
2521 VkShaderStageFlags flushed
= 0;
2523 anv_foreach_stage(stage
, dirty_stages
) {
2524 assert(stage
< ARRAY_SIZE(push_constant_opcodes
));
2525 assert(push_constant_opcodes
[stage
] > 0);
2527 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
) {
2528 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
];
2530 if (anv_pipeline_has_stage(pipeline
, stage
)) {
2531 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2532 const struct brw_stage_prog_data
*prog_data
=
2533 pipeline
->shaders
[stage
]->prog_data
;
2534 const struct anv_pipeline_bind_map
*bind_map
=
2535 &pipeline
->shaders
[stage
]->bind_map
;
2537 /* The Skylake PRM contains the following restriction:
2539 * "The driver must ensure The following case does not occur
2540 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2541 * buffer 3 read length equal to zero committed followed by a
2542 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2545 * To avoid this, we program the buffers in the highest slots.
2546 * This way, slot 0 is only used if slot 3 is also used.
2550 for (int i
= 3; i
>= 0; i
--) {
2551 const struct brw_ubo_range
*range
= &prog_data
->ubo_ranges
[i
];
2552 if (range
->length
== 0)
2555 const unsigned surface
=
2556 prog_data
->binding_table
.ubo_start
+ range
->block
;
2558 assert(surface
<= bind_map
->surface_count
);
2559 const struct anv_pipeline_binding
*binding
=
2560 &bind_map
->surface_to_descriptor
[surface
];
2562 struct anv_address read_addr
;
2564 if (binding
->set
== ANV_DESCRIPTOR_SET_SHADER_CONSTANTS
) {
2565 struct anv_address constant_data
= {
2566 .bo
= pipeline
->device
->dynamic_state_pool
.block_pool
.bo
,
2567 .offset
= pipeline
->shaders
[stage
]->constant_data
.offset
,
2569 unsigned constant_data_size
=
2570 pipeline
->shaders
[stage
]->constant_data_size
;
2572 read_len
= MIN2(range
->length
,
2573 DIV_ROUND_UP(constant_data_size
, 32) - range
->start
);
2574 read_addr
= anv_address_add(constant_data
,
2576 } else if (binding
->set
== ANV_DESCRIPTOR_SET_DESCRIPTORS
) {
2577 /* This is a descriptor set buffer so the set index is
2578 * actually given by binding->binding. (Yes, that's
2581 struct anv_descriptor_set
*set
=
2582 gfx_state
->base
.descriptors
[binding
->index
];
2583 struct anv_address desc_buffer_addr
=
2584 anv_descriptor_set_address(cmd_buffer
, set
);
2585 const unsigned desc_buffer_size
= set
->desc_mem
.alloc_size
;
2587 read_len
= MIN2(range
->length
,
2588 DIV_ROUND_UP(desc_buffer_size
, 32) - range
->start
);
2589 read_addr
= anv_address_add(desc_buffer_addr
,
2592 struct anv_descriptor_set
*set
=
2593 gfx_state
->base
.descriptors
[binding
->set
];
2594 const struct anv_descriptor
*desc
=
2595 &set
->descriptors
[binding
->index
];
2597 if (desc
->type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
) {
2598 read_len
= MIN2(range
->length
,
2599 DIV_ROUND_UP(desc
->buffer_view
->range
, 32) - range
->start
);
2600 read_addr
= anv_address_add(desc
->buffer_view
->address
,
2603 assert(desc
->type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
);
2605 uint32_t dynamic_offset
=
2606 gfx_state
->base
.dynamic_offsets
[binding
->dynamic_offset_index
];
2607 uint32_t buf_offset
=
2608 MIN2(desc
->offset
+ dynamic_offset
, desc
->buffer
->size
);
2609 uint32_t buf_range
=
2610 MIN2(desc
->range
, desc
->buffer
->size
- buf_offset
);
2612 read_len
= MIN2(range
->length
,
2613 DIV_ROUND_UP(buf_range
, 32) - range
->start
);
2614 read_addr
= anv_address_add(desc
->buffer
->address
,
2615 buf_offset
+ range
->start
* 32);
2620 c
.ConstantBody
.Buffer
[n
] = read_addr
;
2621 c
.ConstantBody
.ReadLength
[n
] = read_len
;
2626 struct anv_state state
=
2627 anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
2629 if (state
.alloc_size
> 0) {
2630 c
.ConstantBody
.Buffer
[n
] = (struct anv_address
) {
2631 .bo
= cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
2632 .offset
= state
.offset
,
2634 c
.ConstantBody
.ReadLength
[n
] =
2635 DIV_ROUND_UP(state
.alloc_size
, 32);
2638 /* For Ivy Bridge, the push constants packets have a different
2639 * rule that would require us to iterate in the other direction
2640 * and possibly mess around with dynamic state base address.
2641 * Don't bother; just emit regular push constants at n = 0.
2643 struct anv_state state
=
2644 anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
2646 if (state
.alloc_size
> 0) {
2647 c
.ConstantBody
.Buffer
[0].offset
= state
.offset
,
2648 c
.ConstantBody
.ReadLength
[0] =
2649 DIV_ROUND_UP(state
.alloc_size
, 32);
2655 flushed
|= mesa_to_vk_shader_stage(stage
);
2658 cmd_buffer
->state
.push_constants_dirty
&= ~flushed
;
2663 genX(cmd_buffer_aux_map_state
)(struct anv_cmd_buffer
*cmd_buffer
)
2665 void *aux_map_ctx
= cmd_buffer
->device
->aux_map_ctx
;
2668 uint32_t aux_map_state_num
= gen_aux_map_get_state_num(aux_map_ctx
);
2669 if (cmd_buffer
->state
.last_aux_map_state
!= aux_map_state_num
) {
2670 /* If the aux-map state number increased, then we need to rewrite the
2671 * register. Rewriting the register is used to both set the aux-map
2672 * translation table address, and also to invalidate any previously
2673 * cached translations.
2675 uint64_t base_addr
= gen_aux_map_get_base(aux_map_ctx
);
2676 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
2677 lri
.RegisterOffset
= GENX(GFX_AUX_TABLE_BASE_ADDR_num
);
2678 lri
.DataDWord
= base_addr
& 0xffffffff;
2680 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
2681 lri
.RegisterOffset
= GENX(GFX_AUX_TABLE_BASE_ADDR_num
) + 4;
2682 lri
.DataDWord
= base_addr
>> 32;
2684 cmd_buffer
->state
.last_aux_map_state
= aux_map_state_num
;
2690 genX(cmd_buffer_flush_state
)(struct anv_cmd_buffer
*cmd_buffer
)
2692 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
2695 uint32_t vb_emit
= cmd_buffer
->state
.gfx
.vb_dirty
& pipeline
->vb_used
;
2696 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_PIPELINE
)
2697 vb_emit
|= pipeline
->vb_used
;
2699 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
2701 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
2703 genX(cmd_buffer_emit_hashing_mode
)(cmd_buffer
, UINT_MAX
, UINT_MAX
, 1);
2705 genX(flush_pipeline_select_3d
)(cmd_buffer
);
2708 genX(cmd_buffer_aux_map_state
)(cmd_buffer
);
2712 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
2713 const uint32_t num_dwords
= 1 + num_buffers
* 4;
2715 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
2716 GENX(3DSTATE_VERTEX_BUFFERS
));
2718 for_each_bit(vb
, vb_emit
) {
2719 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
2720 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
2722 struct GENX(VERTEX_BUFFER_STATE
) state
= {
2723 .VertexBufferIndex
= vb
,
2725 .MOCS
= anv_mocs_for_bo(cmd_buffer
->device
, buffer
->address
.bo
),
2727 .BufferAccessType
= pipeline
->vb
[vb
].instanced
? INSTANCEDATA
: VERTEXDATA
,
2728 .InstanceDataStepRate
= pipeline
->vb
[vb
].instance_divisor
,
2731 .AddressModifyEnable
= true,
2732 .BufferPitch
= pipeline
->vb
[vb
].stride
,
2733 .BufferStartingAddress
= anv_address_add(buffer
->address
, offset
),
2736 .BufferSize
= buffer
->size
- offset
2738 .EndAddress
= anv_address_add(buffer
->address
, buffer
->size
- 1),
2742 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
2747 cmd_buffer
->state
.gfx
.vb_dirty
&= ~vb_emit
;
2750 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_XFB_ENABLE
) {
2751 /* We don't need any per-buffer dirty tracking because you're not
2752 * allowed to bind different XFB buffers while XFB is enabled.
2754 for (unsigned idx
= 0; idx
< MAX_XFB_BUFFERS
; idx
++) {
2755 struct anv_xfb_binding
*xfb
= &cmd_buffer
->state
.xfb_bindings
[idx
];
2756 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_SO_BUFFER
), sob
) {
2758 sob
.SOBufferIndex
= idx
;
2760 sob
._3DCommandOpcode
= 0;
2761 sob
._3DCommandSubOpcode
= SO_BUFFER_INDEX_0_CMD
+ idx
;
2764 if (cmd_buffer
->state
.xfb_enabled
&& xfb
->buffer
&& xfb
->size
!= 0) {
2765 sob
.SOBufferEnable
= true;
2766 sob
.MOCS
= cmd_buffer
->device
->isl_dev
.mocs
.internal
,
2767 sob
.StreamOffsetWriteEnable
= false;
2768 sob
.SurfaceBaseAddress
= anv_address_add(xfb
->buffer
->address
,
2770 /* Size is in DWords - 1 */
2771 sob
.SurfaceSize
= xfb
->size
/ 4 - 1;
2776 /* CNL and later require a CS stall after 3DSTATE_SO_BUFFER */
2778 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
2782 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
2783 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
2785 /* The exact descriptor layout is pulled from the pipeline, so we need
2786 * to re-emit binding tables on every pipeline change.
2788 cmd_buffer
->state
.descriptors_dirty
|= pipeline
->active_stages
;
2790 /* If the pipeline changed, we may need to re-allocate push constant
2793 cmd_buffer_alloc_push_constants(cmd_buffer
);
2797 if (cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_VERTEX_BIT
||
2798 cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_VERTEX_BIT
) {
2799 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
2801 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
2802 * stall needs to be sent just prior to any 3DSTATE_VS,
2803 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
2804 * 3DSTATE_BINDING_TABLE_POINTER_VS,
2805 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
2806 * PIPE_CONTROL needs to be sent before any combination of VS
2807 * associated 3DSTATE."
2809 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2810 pc
.DepthStallEnable
= true;
2811 pc
.PostSyncOperation
= WriteImmediateData
;
2813 (struct anv_address
) { cmd_buffer
->device
->workaround_bo
, 0 };
2818 /* Render targets live in the same binding table as fragment descriptors */
2819 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_RENDER_TARGETS
)
2820 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
2822 /* We emit the binding tables and sampler tables first, then emit push
2823 * constants and then finally emit binding table and sampler table
2824 * pointers. It has to happen in this order, since emitting the binding
2825 * tables may change the push constants (in case of storage images). After
2826 * emitting push constants, on SKL+ we have to emit the corresponding
2827 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
2830 if (cmd_buffer
->state
.descriptors_dirty
)
2831 dirty
= flush_descriptor_sets(cmd_buffer
);
2833 if (dirty
|| cmd_buffer
->state
.push_constants_dirty
) {
2834 /* Because we're pushing UBOs, we have to push whenever either
2835 * descriptors or push constants is dirty.
2837 dirty
|= cmd_buffer
->state
.push_constants_dirty
;
2838 dirty
&= ANV_STAGE_MASK
& VK_SHADER_STAGE_ALL_GRAPHICS
;
2839 cmd_buffer_flush_push_constants(cmd_buffer
, dirty
);
2843 cmd_buffer_emit_descriptor_pointers(cmd_buffer
, dirty
);
2845 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
2846 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
2848 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
|
2849 ANV_CMD_DIRTY_PIPELINE
)) {
2850 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer
,
2851 pipeline
->depth_clamp_enable
);
2854 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_SCISSOR
|
2855 ANV_CMD_DIRTY_RENDER_TARGETS
))
2856 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
2858 genX(cmd_buffer_flush_dynamic_state
)(cmd_buffer
);
2860 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
2864 emit_vertex_bo(struct anv_cmd_buffer
*cmd_buffer
,
2865 struct anv_address addr
,
2866 uint32_t size
, uint32_t index
)
2868 uint32_t *p
= anv_batch_emitn(&cmd_buffer
->batch
, 5,
2869 GENX(3DSTATE_VERTEX_BUFFERS
));
2871 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, p
+ 1,
2872 &(struct GENX(VERTEX_BUFFER_STATE
)) {
2873 .VertexBufferIndex
= index
,
2874 .AddressModifyEnable
= true,
2876 .MOCS
= anv_mocs_for_bo(cmd_buffer
->device
, addr
.bo
),
2878 .BufferStartingAddress
= addr
,
2881 .BufferStartingAddress
= addr
,
2882 .EndAddress
= anv_address_add(addr
, size
),
2888 emit_base_vertex_instance_bo(struct anv_cmd_buffer
*cmd_buffer
,
2889 struct anv_address addr
)
2891 emit_vertex_bo(cmd_buffer
, addr
, 8, ANV_SVGS_VB_INDEX
);
2895 emit_base_vertex_instance(struct anv_cmd_buffer
*cmd_buffer
,
2896 uint32_t base_vertex
, uint32_t base_instance
)
2898 struct anv_state id_state
=
2899 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 8, 4);
2901 ((uint32_t *)id_state
.map
)[0] = base_vertex
;
2902 ((uint32_t *)id_state
.map
)[1] = base_instance
;
2904 struct anv_address addr
= {
2905 .bo
= cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
2906 .offset
= id_state
.offset
,
2909 emit_base_vertex_instance_bo(cmd_buffer
, addr
);
2913 emit_draw_index(struct anv_cmd_buffer
*cmd_buffer
, uint32_t draw_index
)
2915 struct anv_state state
=
2916 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 4, 4);
2918 ((uint32_t *)state
.map
)[0] = draw_index
;
2920 struct anv_address addr
= {
2921 .bo
= cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
2922 .offset
= state
.offset
,
2925 emit_vertex_bo(cmd_buffer
, addr
, 4, ANV_DRAWID_VB_INDEX
);
2929 VkCommandBuffer commandBuffer
,
2930 uint32_t vertexCount
,
2931 uint32_t instanceCount
,
2932 uint32_t firstVertex
,
2933 uint32_t firstInstance
)
2935 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2936 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
2937 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
2939 if (anv_batch_has_error(&cmd_buffer
->batch
))
2942 genX(cmd_buffer_flush_state
)(cmd_buffer
);
2944 if (cmd_buffer
->state
.conditional_render_enabled
)
2945 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
2947 if (vs_prog_data
->uses_firstvertex
||
2948 vs_prog_data
->uses_baseinstance
)
2949 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
2950 if (vs_prog_data
->uses_drawid
)
2951 emit_draw_index(cmd_buffer
, 0);
2953 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2954 * different views. We need to multiply instanceCount by the view count.
2956 instanceCount
*= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
2958 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
2959 prim
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
2960 prim
.VertexAccessType
= SEQUENTIAL
;
2961 prim
.PrimitiveTopologyType
= pipeline
->topology
;
2962 prim
.VertexCountPerInstance
= vertexCount
;
2963 prim
.StartVertexLocation
= firstVertex
;
2964 prim
.InstanceCount
= instanceCount
;
2965 prim
.StartInstanceLocation
= firstInstance
;
2966 prim
.BaseVertexLocation
= 0;
2970 void genX(CmdDrawIndexed
)(
2971 VkCommandBuffer commandBuffer
,
2972 uint32_t indexCount
,
2973 uint32_t instanceCount
,
2974 uint32_t firstIndex
,
2975 int32_t vertexOffset
,
2976 uint32_t firstInstance
)
2978 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2979 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
2980 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
2982 if (anv_batch_has_error(&cmd_buffer
->batch
))
2985 genX(cmd_buffer_flush_state
)(cmd_buffer
);
2987 if (cmd_buffer
->state
.conditional_render_enabled
)
2988 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
2990 if (vs_prog_data
->uses_firstvertex
||
2991 vs_prog_data
->uses_baseinstance
)
2992 emit_base_vertex_instance(cmd_buffer
, vertexOffset
, firstInstance
);
2993 if (vs_prog_data
->uses_drawid
)
2994 emit_draw_index(cmd_buffer
, 0);
2996 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2997 * different views. We need to multiply instanceCount by the view count.
2999 instanceCount
*= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
3001 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3002 prim
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
3003 prim
.VertexAccessType
= RANDOM
;
3004 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3005 prim
.VertexCountPerInstance
= indexCount
;
3006 prim
.StartVertexLocation
= firstIndex
;
3007 prim
.InstanceCount
= instanceCount
;
3008 prim
.StartInstanceLocation
= firstInstance
;
3009 prim
.BaseVertexLocation
= vertexOffset
;
3013 /* Auto-Draw / Indirect Registers */
3014 #define GEN7_3DPRIM_END_OFFSET 0x2420
3015 #define GEN7_3DPRIM_START_VERTEX 0x2430
3016 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3017 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3018 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3019 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3021 void genX(CmdDrawIndirectByteCountEXT
)(
3022 VkCommandBuffer commandBuffer
,
3023 uint32_t instanceCount
,
3024 uint32_t firstInstance
,
3025 VkBuffer counterBuffer
,
3026 VkDeviceSize counterBufferOffset
,
3027 uint32_t counterOffset
,
3028 uint32_t vertexStride
)
3030 #if GEN_IS_HASWELL || GEN_GEN >= 8
3031 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3032 ANV_FROM_HANDLE(anv_buffer
, counter_buffer
, counterBuffer
);
3033 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3034 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3036 /* firstVertex is always zero for this draw function */
3037 const uint32_t firstVertex
= 0;
3039 if (anv_batch_has_error(&cmd_buffer
->batch
))
3042 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3044 if (vs_prog_data
->uses_firstvertex
||
3045 vs_prog_data
->uses_baseinstance
)
3046 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
3047 if (vs_prog_data
->uses_drawid
)
3048 emit_draw_index(cmd_buffer
, 0);
3050 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3051 * different views. We need to multiply instanceCount by the view count.
3053 instanceCount
*= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
3055 struct gen_mi_builder b
;
3056 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3057 struct gen_mi_value count
=
3058 gen_mi_mem32(anv_address_add(counter_buffer
->address
,
3059 counterBufferOffset
));
3061 count
= gen_mi_isub(&b
, count
, gen_mi_imm(counterOffset
));
3062 count
= gen_mi_udiv32_imm(&b
, count
, vertexStride
);
3063 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT
), count
);
3065 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX
),
3066 gen_mi_imm(firstVertex
));
3067 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT
),
3068 gen_mi_imm(instanceCount
));
3069 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE
),
3070 gen_mi_imm(firstInstance
));
3071 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX
), gen_mi_imm(0));
3073 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3074 prim
.IndirectParameterEnable
= true;
3075 prim
.VertexAccessType
= SEQUENTIAL
;
3076 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3078 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
3082 load_indirect_parameters(struct anv_cmd_buffer
*cmd_buffer
,
3083 struct anv_address addr
,
3086 struct gen_mi_builder b
;
3087 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3089 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT
),
3090 gen_mi_mem32(anv_address_add(addr
, 0)));
3092 struct gen_mi_value instance_count
= gen_mi_mem32(anv_address_add(addr
, 4));
3093 unsigned view_count
= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
3094 if (view_count
> 1) {
3095 #if GEN_IS_HASWELL || GEN_GEN >= 8
3096 instance_count
= gen_mi_imul_imm(&b
, instance_count
, view_count
);
3098 anv_finishme("Multiview + indirect draw requires MI_MATH; "
3099 "MI_MATH is not supported on Ivy Bridge");
3102 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT
), instance_count
);
3104 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX
),
3105 gen_mi_mem32(anv_address_add(addr
, 8)));
3108 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX
),
3109 gen_mi_mem32(anv_address_add(addr
, 12)));
3110 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE
),
3111 gen_mi_mem32(anv_address_add(addr
, 16)));
3113 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE
),
3114 gen_mi_mem32(anv_address_add(addr
, 12)));
3115 gen_mi_store(&b
, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX
), gen_mi_imm(0));
3119 void genX(CmdDrawIndirect
)(
3120 VkCommandBuffer commandBuffer
,
3122 VkDeviceSize offset
,
3126 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3127 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3128 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3129 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3131 if (anv_batch_has_error(&cmd_buffer
->batch
))
3134 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3136 if (cmd_buffer
->state
.conditional_render_enabled
)
3137 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
3139 for (uint32_t i
= 0; i
< drawCount
; i
++) {
3140 struct anv_address draw
= anv_address_add(buffer
->address
, offset
);
3142 if (vs_prog_data
->uses_firstvertex
||
3143 vs_prog_data
->uses_baseinstance
)
3144 emit_base_vertex_instance_bo(cmd_buffer
, anv_address_add(draw
, 8));
3145 if (vs_prog_data
->uses_drawid
)
3146 emit_draw_index(cmd_buffer
, i
);
3148 load_indirect_parameters(cmd_buffer
, draw
, false);
3150 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3151 prim
.IndirectParameterEnable
= true;
3152 prim
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
3153 prim
.VertexAccessType
= SEQUENTIAL
;
3154 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3161 void genX(CmdDrawIndexedIndirect
)(
3162 VkCommandBuffer commandBuffer
,
3164 VkDeviceSize offset
,
3168 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3169 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3170 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
3171 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3173 if (anv_batch_has_error(&cmd_buffer
->batch
))
3176 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3178 if (cmd_buffer
->state
.conditional_render_enabled
)
3179 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
3181 for (uint32_t i
= 0; i
< drawCount
; i
++) {
3182 struct anv_address draw
= anv_address_add(buffer
->address
, offset
);
3184 /* TODO: We need to stomp base vertex to 0 somehow */
3185 if (vs_prog_data
->uses_firstvertex
||
3186 vs_prog_data
->uses_baseinstance
)
3187 emit_base_vertex_instance_bo(cmd_buffer
, anv_address_add(draw
, 12));
3188 if (vs_prog_data
->uses_drawid
)
3189 emit_draw_index(cmd_buffer
, i
);
3191 load_indirect_parameters(cmd_buffer
, draw
, true);
3193 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3194 prim
.IndirectParameterEnable
= true;
3195 prim
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
3196 prim
.VertexAccessType
= RANDOM
;
3197 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3204 #define TMP_DRAW_COUNT_REG 0x2670 /* MI_ALU_REG14 */
3207 prepare_for_draw_count_predicate(struct anv_cmd_buffer
*cmd_buffer
,
3208 struct anv_address count_address
,
3209 const bool conditional_render_enabled
)
3211 struct gen_mi_builder b
;
3212 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3214 if (conditional_render_enabled
) {
3215 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3216 gen_mi_store(&b
, gen_mi_reg64(TMP_DRAW_COUNT_REG
),
3217 gen_mi_mem32(count_address
));
3220 /* Upload the current draw count from the draw parameters buffer to
3221 * MI_PREDICATE_SRC0.
3223 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
),
3224 gen_mi_mem32(count_address
));
3226 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC1
+ 4), gen_mi_imm(0));
3231 emit_draw_count_predicate(struct anv_cmd_buffer
*cmd_buffer
,
3232 uint32_t draw_index
)
3234 struct gen_mi_builder b
;
3235 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3237 /* Upload the index of the current primitive to MI_PREDICATE_SRC1. */
3238 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC1
), gen_mi_imm(draw_index
));
3240 if (draw_index
== 0) {
3241 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
3242 mip
.LoadOperation
= LOAD_LOADINV
;
3243 mip
.CombineOperation
= COMBINE_SET
;
3244 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3247 /* While draw_index < draw_count the predicate's result will be
3248 * (draw_index == draw_count) ^ TRUE = TRUE
3249 * When draw_index == draw_count the result is
3250 * (TRUE) ^ TRUE = FALSE
3251 * After this all results will be:
3252 * (FALSE) ^ FALSE = FALSE
3254 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
3255 mip
.LoadOperation
= LOAD_LOAD
;
3256 mip
.CombineOperation
= COMBINE_XOR
;
3257 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3262 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3264 emit_draw_count_predicate_with_conditional_render(
3265 struct anv_cmd_buffer
*cmd_buffer
,
3266 uint32_t draw_index
)
3268 struct gen_mi_builder b
;
3269 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3271 struct gen_mi_value pred
= gen_mi_ult(&b
, gen_mi_imm(draw_index
),
3272 gen_mi_reg64(TMP_DRAW_COUNT_REG
));
3273 pred
= gen_mi_iand(&b
, pred
, gen_mi_reg64(ANV_PREDICATE_RESULT_REG
));
3276 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_RESULT
), pred
);
3278 /* MI_PREDICATE_RESULT is not whitelisted in i915 command parser
3279 * so we emit MI_PREDICATE to set it.
3282 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
), pred
);
3283 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
3285 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
3286 mip
.LoadOperation
= LOAD_LOADINV
;
3287 mip
.CombineOperation
= COMBINE_SET
;
3288 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3294 void genX(CmdDrawIndirectCountKHR
)(
3295 VkCommandBuffer commandBuffer
,
3297 VkDeviceSize offset
,
3298 VkBuffer _countBuffer
,
3299 VkDeviceSize countBufferOffset
,
3300 uint32_t maxDrawCount
,
3303 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3304 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3305 ANV_FROM_HANDLE(anv_buffer
, count_buffer
, _countBuffer
);
3306 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
3307 struct anv_pipeline
*pipeline
= cmd_state
->gfx
.base
.pipeline
;
3308 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3310 if (anv_batch_has_error(&cmd_buffer
->batch
))
3313 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3315 struct anv_address count_address
=
3316 anv_address_add(count_buffer
->address
, countBufferOffset
);
3318 prepare_for_draw_count_predicate(cmd_buffer
, count_address
,
3319 cmd_state
->conditional_render_enabled
);
3321 for (uint32_t i
= 0; i
< maxDrawCount
; i
++) {
3322 struct anv_address draw
= anv_address_add(buffer
->address
, offset
);
3324 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3325 if (cmd_state
->conditional_render_enabled
) {
3326 emit_draw_count_predicate_with_conditional_render(cmd_buffer
, i
);
3328 emit_draw_count_predicate(cmd_buffer
, i
);
3331 emit_draw_count_predicate(cmd_buffer
, i
);
3334 if (vs_prog_data
->uses_firstvertex
||
3335 vs_prog_data
->uses_baseinstance
)
3336 emit_base_vertex_instance_bo(cmd_buffer
, anv_address_add(draw
, 8));
3337 if (vs_prog_data
->uses_drawid
)
3338 emit_draw_index(cmd_buffer
, i
);
3340 load_indirect_parameters(cmd_buffer
, draw
, false);
3342 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3343 prim
.IndirectParameterEnable
= true;
3344 prim
.PredicateEnable
= true;
3345 prim
.VertexAccessType
= SEQUENTIAL
;
3346 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3353 void genX(CmdDrawIndexedIndirectCountKHR
)(
3354 VkCommandBuffer commandBuffer
,
3356 VkDeviceSize offset
,
3357 VkBuffer _countBuffer
,
3358 VkDeviceSize countBufferOffset
,
3359 uint32_t maxDrawCount
,
3362 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3363 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3364 ANV_FROM_HANDLE(anv_buffer
, count_buffer
, _countBuffer
);
3365 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
3366 struct anv_pipeline
*pipeline
= cmd_state
->gfx
.base
.pipeline
;
3367 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
3369 if (anv_batch_has_error(&cmd_buffer
->batch
))
3372 genX(cmd_buffer_flush_state
)(cmd_buffer
);
3374 struct anv_address count_address
=
3375 anv_address_add(count_buffer
->address
, countBufferOffset
);
3377 prepare_for_draw_count_predicate(cmd_buffer
, count_address
,
3378 cmd_state
->conditional_render_enabled
);
3380 for (uint32_t i
= 0; i
< maxDrawCount
; i
++) {
3381 struct anv_address draw
= anv_address_add(buffer
->address
, offset
);
3383 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3384 if (cmd_state
->conditional_render_enabled
) {
3385 emit_draw_count_predicate_with_conditional_render(cmd_buffer
, i
);
3387 emit_draw_count_predicate(cmd_buffer
, i
);
3390 emit_draw_count_predicate(cmd_buffer
, i
);
3393 /* TODO: We need to stomp base vertex to 0 somehow */
3394 if (vs_prog_data
->uses_firstvertex
||
3395 vs_prog_data
->uses_baseinstance
)
3396 emit_base_vertex_instance_bo(cmd_buffer
, anv_address_add(draw
, 12));
3397 if (vs_prog_data
->uses_drawid
)
3398 emit_draw_index(cmd_buffer
, i
);
3400 load_indirect_parameters(cmd_buffer
, draw
, true);
3402 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
3403 prim
.IndirectParameterEnable
= true;
3404 prim
.PredicateEnable
= true;
3405 prim
.VertexAccessType
= RANDOM
;
3406 prim
.PrimitiveTopologyType
= pipeline
->topology
;
3413 void genX(CmdBeginTransformFeedbackEXT
)(
3414 VkCommandBuffer commandBuffer
,
3415 uint32_t firstCounterBuffer
,
3416 uint32_t counterBufferCount
,
3417 const VkBuffer
* pCounterBuffers
,
3418 const VkDeviceSize
* pCounterBufferOffsets
)
3420 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3422 assert(firstCounterBuffer
< MAX_XFB_BUFFERS
);
3423 assert(counterBufferCount
<= MAX_XFB_BUFFERS
);
3424 assert(firstCounterBuffer
+ counterBufferCount
<= MAX_XFB_BUFFERS
);
3426 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3428 * "Ssoftware must ensure that no HW stream output operations can be in
3429 * process or otherwise pending at the point that the MI_LOAD/STORE
3430 * commands are processed. This will likely require a pipeline flush."
3432 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
3433 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3435 for (uint32_t idx
= 0; idx
< MAX_XFB_BUFFERS
; idx
++) {
3436 /* If we have a counter buffer, this is a resume so we need to load the
3437 * value into the streamout offset register. Otherwise, this is a begin
3438 * and we need to reset it to zero.
3440 if (pCounterBuffers
&&
3441 idx
>= firstCounterBuffer
&&
3442 idx
- firstCounterBuffer
< counterBufferCount
&&
3443 pCounterBuffers
[idx
- firstCounterBuffer
] != VK_NULL_HANDLE
) {
3444 uint32_t cb_idx
= idx
- firstCounterBuffer
;
3445 ANV_FROM_HANDLE(anv_buffer
, counter_buffer
, pCounterBuffers
[cb_idx
]);
3446 uint64_t offset
= pCounterBufferOffsets
?
3447 pCounterBufferOffsets
[cb_idx
] : 0;
3449 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
3450 lrm
.RegisterAddress
= GENX(SO_WRITE_OFFSET0_num
) + idx
* 4;
3451 lrm
.MemoryAddress
= anv_address_add(counter_buffer
->address
,
3455 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
3456 lri
.RegisterOffset
= GENX(SO_WRITE_OFFSET0_num
) + idx
* 4;
3462 cmd_buffer
->state
.xfb_enabled
= true;
3463 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_XFB_ENABLE
;
3466 void genX(CmdEndTransformFeedbackEXT
)(
3467 VkCommandBuffer commandBuffer
,
3468 uint32_t firstCounterBuffer
,
3469 uint32_t counterBufferCount
,
3470 const VkBuffer
* pCounterBuffers
,
3471 const VkDeviceSize
* pCounterBufferOffsets
)
3473 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3475 assert(firstCounterBuffer
< MAX_XFB_BUFFERS
);
3476 assert(counterBufferCount
<= MAX_XFB_BUFFERS
);
3477 assert(firstCounterBuffer
+ counterBufferCount
<= MAX_XFB_BUFFERS
);
3479 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3481 * "Ssoftware must ensure that no HW stream output operations can be in
3482 * process or otherwise pending at the point that the MI_LOAD/STORE
3483 * commands are processed. This will likely require a pipeline flush."
3485 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
3486 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3488 for (uint32_t cb_idx
= 0; cb_idx
< counterBufferCount
; cb_idx
++) {
3489 unsigned idx
= firstCounterBuffer
+ cb_idx
;
3491 /* If we have a counter buffer, this is a resume so we need to load the
3492 * value into the streamout offset register. Otherwise, this is a begin
3493 * and we need to reset it to zero.
3495 if (pCounterBuffers
&&
3496 cb_idx
< counterBufferCount
&&
3497 pCounterBuffers
[cb_idx
] != VK_NULL_HANDLE
) {
3498 ANV_FROM_HANDLE(anv_buffer
, counter_buffer
, pCounterBuffers
[cb_idx
]);
3499 uint64_t offset
= pCounterBufferOffsets
?
3500 pCounterBufferOffsets
[cb_idx
] : 0;
3502 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
3503 srm
.MemoryAddress
= anv_address_add(counter_buffer
->address
,
3505 srm
.RegisterAddress
= GENX(SO_WRITE_OFFSET0_num
) + idx
* 4;
3510 cmd_buffer
->state
.xfb_enabled
= false;
3511 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_XFB_ENABLE
;
3515 flush_compute_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
)
3517 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
3518 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
3521 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
3522 if (result
!= VK_SUCCESS
) {
3523 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
3525 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
3526 if (result
!= VK_SUCCESS
)
3529 /* Re-emit state base addresses so we get the new surface state base
3530 * address before we start emitting binding tables etc.
3532 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
3534 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
3535 if (result
!= VK_SUCCESS
) {
3536 anv_batch_set_error(&cmd_buffer
->batch
, result
);
3541 result
= emit_samplers(cmd_buffer
, MESA_SHADER_COMPUTE
, &samplers
);
3542 if (result
!= VK_SUCCESS
) {
3543 anv_batch_set_error(&cmd_buffer
->batch
, result
);
3547 uint32_t iface_desc_data_dw
[GENX(INTERFACE_DESCRIPTOR_DATA_length
)];
3548 struct GENX(INTERFACE_DESCRIPTOR_DATA
) desc
= {
3549 .BindingTablePointer
= surfaces
.offset
,
3550 .SamplerStatePointer
= samplers
.offset
,
3552 GENX(INTERFACE_DESCRIPTOR_DATA_pack
)(NULL
, iface_desc_data_dw
, &desc
);
3554 struct anv_state state
=
3555 anv_cmd_buffer_merge_dynamic(cmd_buffer
, iface_desc_data_dw
,
3556 pipeline
->interface_descriptor_data
,
3557 GENX(INTERFACE_DESCRIPTOR_DATA_length
),
3560 uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
3561 anv_batch_emit(&cmd_buffer
->batch
,
3562 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
), mid
) {
3563 mid
.InterfaceDescriptorTotalLength
= size
;
3564 mid
.InterfaceDescriptorDataStartAddress
= state
.offset
;
3571 genX(cmd_buffer_flush_compute_state
)(struct anv_cmd_buffer
*cmd_buffer
)
3573 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
3576 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
3578 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
3580 genX(flush_pipeline_select_gpgpu
)(cmd_buffer
);
3583 genX(cmd_buffer_aux_map_state
)(cmd_buffer
);
3586 if (cmd_buffer
->state
.compute
.pipeline_dirty
) {
3587 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
3589 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
3590 * the only bits that are changed are scoreboard related: Scoreboard
3591 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
3592 * these scoreboard related states, a MEDIA_STATE_FLUSH is
3595 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
3596 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3598 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
3601 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
3602 cmd_buffer
->state
.compute
.pipeline_dirty
) {
3603 /* FIXME: figure out descriptors for gen7 */
3604 result
= flush_compute_descriptor_set(cmd_buffer
);
3605 if (result
!= VK_SUCCESS
)
3608 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
3611 if (cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) {
3612 struct anv_state push_state
=
3613 anv_cmd_buffer_cs_push_constants(cmd_buffer
);
3615 if (push_state
.alloc_size
) {
3616 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
), curbe
) {
3617 curbe
.CURBETotalDataLength
= push_state
.alloc_size
;
3618 curbe
.CURBEDataStartAddress
= push_state
.offset
;
3622 cmd_buffer
->state
.push_constants_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
3625 cmd_buffer
->state
.compute
.pipeline_dirty
= false;
3627 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
3633 verify_cmd_parser(const struct anv_device
*device
,
3634 int required_version
,
3635 const char *function
)
3637 if (device
->instance
->physicalDevice
.cmd_parser_version
< required_version
) {
3638 return vk_errorf(device
->instance
, device
->instance
,
3639 VK_ERROR_FEATURE_NOT_PRESENT
,
3640 "cmd parser version %d is required for %s",
3641 required_version
, function
);
3650 anv_cmd_buffer_push_base_group_id(struct anv_cmd_buffer
*cmd_buffer
,
3651 uint32_t baseGroupX
,
3652 uint32_t baseGroupY
,
3653 uint32_t baseGroupZ
)
3655 if (anv_batch_has_error(&cmd_buffer
->batch
))
3658 struct anv_push_constants
*push
=
3659 &cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
3660 if (push
->base_work_group_id
[0] != baseGroupX
||
3661 push
->base_work_group_id
[1] != baseGroupY
||
3662 push
->base_work_group_id
[2] != baseGroupZ
) {
3663 push
->base_work_group_id
[0] = baseGroupX
;
3664 push
->base_work_group_id
[1] = baseGroupY
;
3665 push
->base_work_group_id
[2] = baseGroupZ
;
3667 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
3671 void genX(CmdDispatch
)(
3672 VkCommandBuffer commandBuffer
,
3677 genX(CmdDispatchBase
)(commandBuffer
, 0, 0, 0, x
, y
, z
);
3680 void genX(CmdDispatchBase
)(
3681 VkCommandBuffer commandBuffer
,
3682 uint32_t baseGroupX
,
3683 uint32_t baseGroupY
,
3684 uint32_t baseGroupZ
,
3685 uint32_t groupCountX
,
3686 uint32_t groupCountY
,
3687 uint32_t groupCountZ
)
3689 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3690 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
3691 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
3693 anv_cmd_buffer_push_base_group_id(cmd_buffer
, baseGroupX
,
3694 baseGroupY
, baseGroupZ
);
3696 if (anv_batch_has_error(&cmd_buffer
->batch
))
3699 if (prog_data
->uses_num_work_groups
) {
3700 struct anv_state state
=
3701 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 12, 4);
3702 uint32_t *sizes
= state
.map
;
3703 sizes
[0] = groupCountX
;
3704 sizes
[1] = groupCountY
;
3705 sizes
[2] = groupCountZ
;
3706 cmd_buffer
->state
.compute
.num_workgroups
= (struct anv_address
) {
3707 .bo
= cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
3708 .offset
= state
.offset
,
3712 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
3714 if (cmd_buffer
->state
.conditional_render_enabled
)
3715 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
3717 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
), ggw
) {
3718 ggw
.PredicateEnable
= cmd_buffer
->state
.conditional_render_enabled
;
3719 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
3720 ggw
.ThreadDepthCounterMaximum
= 0;
3721 ggw
.ThreadHeightCounterMaximum
= 0;
3722 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
3723 ggw
.ThreadGroupIDXDimension
= groupCountX
;
3724 ggw
.ThreadGroupIDYDimension
= groupCountY
;
3725 ggw
.ThreadGroupIDZDimension
= groupCountZ
;
3726 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
3727 ggw
.BottomExecutionMask
= 0xffffffff;
3730 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
3733 #define GPGPU_DISPATCHDIMX 0x2500
3734 #define GPGPU_DISPATCHDIMY 0x2504
3735 #define GPGPU_DISPATCHDIMZ 0x2508
3737 void genX(CmdDispatchIndirect
)(
3738 VkCommandBuffer commandBuffer
,
3740 VkDeviceSize offset
)
3742 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3743 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3744 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
3745 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
3746 struct anv_address addr
= anv_address_add(buffer
->address
, offset
);
3747 struct anv_batch
*batch
= &cmd_buffer
->batch
;
3749 anv_cmd_buffer_push_base_group_id(cmd_buffer
, 0, 0, 0);
3752 /* Linux 4.4 added command parser version 5 which allows the GPGPU
3753 * indirect dispatch registers to be written.
3755 if (verify_cmd_parser(cmd_buffer
->device
, 5,
3756 "vkCmdDispatchIndirect") != VK_SUCCESS
)
3760 if (prog_data
->uses_num_work_groups
)
3761 cmd_buffer
->state
.compute
.num_workgroups
= addr
;
3763 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
3765 struct gen_mi_builder b
;
3766 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
3768 struct gen_mi_value size_x
= gen_mi_mem32(anv_address_add(addr
, 0));
3769 struct gen_mi_value size_y
= gen_mi_mem32(anv_address_add(addr
, 4));
3770 struct gen_mi_value size_z
= gen_mi_mem32(anv_address_add(addr
, 8));
3772 gen_mi_store(&b
, gen_mi_reg32(GPGPU_DISPATCHDIMX
), size_x
);
3773 gen_mi_store(&b
, gen_mi_reg32(GPGPU_DISPATCHDIMY
), size_y
);
3774 gen_mi_store(&b
, gen_mi_reg32(GPGPU_DISPATCHDIMZ
), size_z
);
3777 /* predicate = (compute_dispatch_indirect_x_size == 0); */
3778 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
), size_x
);
3779 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
3780 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
3781 mip
.LoadOperation
= LOAD_LOAD
;
3782 mip
.CombineOperation
= COMBINE_SET
;
3783 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3786 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
3787 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC0
), size_y
);
3788 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
3789 mip
.LoadOperation
= LOAD_LOAD
;
3790 mip
.CombineOperation
= COMBINE_OR
;
3791 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3794 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
3795 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC0
), size_z
);
3796 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
3797 mip
.LoadOperation
= LOAD_LOAD
;
3798 mip
.CombineOperation
= COMBINE_OR
;
3799 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3802 /* predicate = !predicate; */
3803 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
3804 mip
.LoadOperation
= LOAD_LOADINV
;
3805 mip
.CombineOperation
= COMBINE_OR
;
3806 mip
.CompareOperation
= COMPARE_FALSE
;
3810 if (cmd_buffer
->state
.conditional_render_enabled
) {
3811 /* predicate &= !(conditional_rendering_predicate == 0); */
3812 gen_mi_store(&b
, gen_mi_reg32(MI_PREDICATE_SRC0
),
3813 gen_mi_reg32(ANV_PREDICATE_RESULT_REG
));
3814 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
3815 mip
.LoadOperation
= LOAD_LOADINV
;
3816 mip
.CombineOperation
= COMBINE_AND
;
3817 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
3822 #else /* GEN_GEN > 7 */
3823 if (cmd_buffer
->state
.conditional_render_enabled
)
3824 genX(cmd_emit_conditional_render_predicate
)(cmd_buffer
);
3827 anv_batch_emit(batch
, GENX(GPGPU_WALKER
), ggw
) {
3828 ggw
.IndirectParameterEnable
= true;
3829 ggw
.PredicateEnable
= GEN_GEN
<= 7 ||
3830 cmd_buffer
->state
.conditional_render_enabled
;
3831 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
3832 ggw
.ThreadDepthCounterMaximum
= 0;
3833 ggw
.ThreadHeightCounterMaximum
= 0;
3834 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
3835 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
3836 ggw
.BottomExecutionMask
= 0xffffffff;
3839 anv_batch_emit(batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
3843 genX(flush_pipeline_select
)(struct anv_cmd_buffer
*cmd_buffer
,
3846 UNUSED
const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
3848 if (cmd_buffer
->state
.current_pipeline
== pipeline
)
3851 #if GEN_GEN >= 8 && GEN_GEN < 10
3852 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
3854 * Software must clear the COLOR_CALC_STATE Valid field in
3855 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
3856 * with Pipeline Select set to GPGPU.
3858 * The internal hardware docs recommend the same workaround for Gen9
3861 if (pipeline
== GPGPU
)
3862 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), t
);
3866 if (pipeline
== _3D
) {
3867 /* There is a mid-object preemption workaround which requires you to
3868 * re-emit MEDIA_VFE_STATE after switching from GPGPU to 3D. However,
3869 * even without preemption, we have issues with geometry flickering when
3870 * GPGPU and 3D are back-to-back and this seems to fix it. We don't
3873 const uint32_t subslices
=
3874 MAX2(cmd_buffer
->device
->instance
->physicalDevice
.subslice_total
, 1);
3875 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_VFE_STATE
), vfe
) {
3876 vfe
.MaximumNumberofThreads
=
3877 devinfo
->max_cs_threads
* subslices
- 1;
3878 vfe
.NumberofURBEntries
= 2;
3879 vfe
.URBEntryAllocationSize
= 2;
3884 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
3885 * PIPELINE_SELECT [DevBWR+]":
3889 * Software must ensure all the write caches are flushed through a
3890 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
3891 * command to invalidate read only caches prior to programming
3892 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
3894 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
3895 pc
.RenderTargetCacheFlushEnable
= true;
3896 pc
.DepthCacheFlushEnable
= true;
3897 pc
.DCFlushEnable
= true;
3898 pc
.PostSyncOperation
= NoWrite
;
3899 pc
.CommandStreamerStallEnable
= true;
3901 pc
.TileCacheFlushEnable
= true;
3905 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
3906 pc
.TextureCacheInvalidationEnable
= true;
3907 pc
.ConstantCacheInvalidationEnable
= true;
3908 pc
.StateCacheInvalidationEnable
= true;
3909 pc
.InstructionCacheInvalidateEnable
= true;
3910 pc
.PostSyncOperation
= NoWrite
;
3912 pc
.TileCacheFlushEnable
= true;
3916 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
3920 ps
.PipelineSelection
= pipeline
;
3924 if (devinfo
->is_geminilake
) {
3927 * "This chicken bit works around a hardware issue with barrier logic
3928 * encountered when switching between GPGPU and 3D pipelines. To
3929 * workaround the issue, this mode bit should be set after a pipeline
3933 anv_pack_struct(&scec
, GENX(SLICE_COMMON_ECO_CHICKEN1
),
3935 pipeline
== GPGPU
? GLK_BARRIER_MODE_GPGPU
3936 : GLK_BARRIER_MODE_3D_HULL
,
3937 .GLKBarrierModeMask
= 1);
3938 emit_lri(&cmd_buffer
->batch
, GENX(SLICE_COMMON_ECO_CHICKEN1_num
), scec
);
3942 cmd_buffer
->state
.current_pipeline
= pipeline
;
3946 genX(flush_pipeline_select_3d
)(struct anv_cmd_buffer
*cmd_buffer
)
3948 genX(flush_pipeline_select
)(cmd_buffer
, _3D
);
3952 genX(flush_pipeline_select_gpgpu
)(struct anv_cmd_buffer
*cmd_buffer
)
3954 genX(flush_pipeline_select
)(cmd_buffer
, GPGPU
);
3958 genX(cmd_buffer_emit_gen7_depth_flush
)(struct anv_cmd_buffer
*cmd_buffer
)
3963 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
3965 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
3966 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
3967 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
3968 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
3969 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
3970 * Depth Flush Bit set, followed by another pipelined depth stall
3971 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
3972 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
3973 * via a preceding MI_FLUSH)."
3975 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
3976 pipe
.DepthStallEnable
= true;
3978 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
3979 pipe
.DepthCacheFlushEnable
= true;
3981 pipe
.TileCacheFlushEnable
= true;
3984 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
3985 pipe
.DepthStallEnable
= true;
3990 * Update the pixel hashing modes that determine the balancing of PS threads
3991 * across subslices and slices.
3993 * \param width Width bound of the rendering area (already scaled down if \p
3994 * scale is greater than 1).
3995 * \param height Height bound of the rendering area (already scaled down if \p
3996 * scale is greater than 1).
3997 * \param scale The number of framebuffer samples that could potentially be
3998 * affected by an individual channel of the PS thread. This is
3999 * typically one for single-sampled rendering, but for operations
4000 * like CCS resolves and fast clears a single PS invocation may
4001 * update a huge number of pixels, in which case a finer
4002 * balancing is desirable in order to maximally utilize the
4003 * bandwidth available. UINT_MAX can be used as shorthand for
4004 * "finest hashing mode available".
4007 genX(cmd_buffer_emit_hashing_mode
)(struct anv_cmd_buffer
*cmd_buffer
,
4008 unsigned width
, unsigned height
,
4012 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
4013 const unsigned slice_hashing
[] = {
4014 /* Because all Gen9 platforms with more than one slice require
4015 * three-way subslice hashing, a single "normal" 16x16 slice hashing
4016 * block is guaranteed to suffer from substantial imbalance, with one
4017 * subslice receiving twice as much work as the other two in the
4020 * The performance impact of that would be particularly severe when
4021 * three-way hashing is also in use for slice balancing (which is the
4022 * case for all Gen9 GT4 platforms), because one of the slices
4023 * receives one every three 16x16 blocks in either direction, which
4024 * is roughly the periodicity of the underlying subslice imbalance
4025 * pattern ("roughly" because in reality the hardware's
4026 * implementation of three-way hashing doesn't do exact modulo 3
4027 * arithmetic, which somewhat decreases the magnitude of this effect
4028 * in practice). This leads to a systematic subslice imbalance
4029 * within that slice regardless of the size of the primitive. The
4030 * 32x32 hashing mode guarantees that the subslice imbalance within a
4031 * single slice hashing block is minimal, largely eliminating this
4035 /* Finest slice hashing mode available. */
4038 const unsigned subslice_hashing
[] = {
4039 /* 16x16 would provide a slight cache locality benefit especially
4040 * visible in the sampler L1 cache efficiency of low-bandwidth
4041 * non-LLC platforms, but it comes at the cost of greater subslice
4042 * imbalance for primitives of dimensions approximately intermediate
4043 * between 16x4 and 16x16.
4046 /* Finest subslice hashing mode available. */
4049 /* Dimensions of the smallest hashing block of a given hashing mode. If
4050 * the rendering area is smaller than this there can't possibly be any
4051 * benefit from switching to this mode, so we optimize out the
4054 const unsigned min_size
[][2] = {
4058 const unsigned idx
= scale
> 1;
4060 if (cmd_buffer
->state
.current_hash_scale
!= scale
&&
4061 (width
> min_size
[idx
][0] || height
> min_size
[idx
][1])) {
4064 anv_pack_struct(>_mode
, GENX(GT_MODE
),
4065 .SliceHashing
= (devinfo
->num_slices
> 1 ? slice_hashing
[idx
] : 0),
4066 .SliceHashingMask
= (devinfo
->num_slices
> 1 ? -1 : 0),
4067 .SubsliceHashing
= subslice_hashing
[idx
],
4068 .SubsliceHashingMask
= -1);
4070 cmd_buffer
->state
.pending_pipe_bits
|=
4071 ANV_PIPE_CS_STALL_BIT
| ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
4072 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
4074 emit_lri(&cmd_buffer
->batch
, GENX(GT_MODE_num
), gt_mode
);
4076 cmd_buffer
->state
.current_hash_scale
= scale
;
4082 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
4084 struct anv_device
*device
= cmd_buffer
->device
;
4085 const struct anv_image_view
*iview
=
4086 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
4087 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
4089 /* FIXME: Width and Height are wrong */
4091 genX(cmd_buffer_emit_gen7_depth_flush
)(cmd_buffer
);
4093 uint32_t *dw
= anv_batch_emit_dwords(&cmd_buffer
->batch
,
4094 device
->isl_dev
.ds
.size
/ 4);
4098 struct isl_depth_stencil_hiz_emit_info info
= { };
4101 info
.view
= &iview
->planes
[0].isl
;
4103 if (image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)) {
4104 uint32_t depth_plane
=
4105 anv_image_aspect_to_plane(image
->aspects
, VK_IMAGE_ASPECT_DEPTH_BIT
);
4106 const struct anv_surface
*surface
= &image
->planes
[depth_plane
].surface
;
4108 info
.depth_surf
= &surface
->isl
;
4110 info
.depth_address
=
4111 anv_batch_emit_reloc(&cmd_buffer
->batch
,
4112 dw
+ device
->isl_dev
.ds
.depth_offset
/ 4,
4113 image
->planes
[depth_plane
].address
.bo
,
4114 image
->planes
[depth_plane
].address
.offset
+
4117 anv_mocs_for_bo(device
, image
->planes
[depth_plane
].address
.bo
);
4120 cmd_buffer
->state
.subpass
->depth_stencil_attachment
->attachment
;
4121 info
.hiz_usage
= cmd_buffer
->state
.attachments
[ds
].aux_usage
;
4122 if (info
.hiz_usage
== ISL_AUX_USAGE_HIZ
) {
4123 info
.hiz_surf
= &image
->planes
[depth_plane
].aux_surface
.isl
;
4126 anv_batch_emit_reloc(&cmd_buffer
->batch
,
4127 dw
+ device
->isl_dev
.ds
.hiz_offset
/ 4,
4128 image
->planes
[depth_plane
].address
.bo
,
4129 image
->planes
[depth_plane
].address
.offset
+
4130 image
->planes
[depth_plane
].aux_surface
.offset
);
4132 info
.depth_clear_value
= ANV_HZ_FC_VAL
;
4136 if (image
&& (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
)) {
4137 uint32_t stencil_plane
=
4138 anv_image_aspect_to_plane(image
->aspects
, VK_IMAGE_ASPECT_STENCIL_BIT
);
4139 const struct anv_surface
*surface
= &image
->planes
[stencil_plane
].surface
;
4141 info
.stencil_surf
= &surface
->isl
;
4143 info
.stencil_address
=
4144 anv_batch_emit_reloc(&cmd_buffer
->batch
,
4145 dw
+ device
->isl_dev
.ds
.stencil_offset
/ 4,
4146 image
->planes
[stencil_plane
].address
.bo
,
4147 image
->planes
[stencil_plane
].address
.offset
+
4150 anv_mocs_for_bo(device
, image
->planes
[stencil_plane
].address
.bo
);
4153 isl_emit_depth_stencil_hiz_s(&device
->isl_dev
, dw
, &info
);
4155 cmd_buffer
->state
.hiz_enabled
= info
.hiz_usage
== ISL_AUX_USAGE_HIZ
;
4159 * This ANDs the view mask of the current subpass with the pending clear
4160 * views in the attachment to get the mask of views active in the subpass
4161 * that still need to be cleared.
4163 static inline uint32_t
4164 get_multiview_subpass_clear_mask(const struct anv_cmd_state
*cmd_state
,
4165 const struct anv_attachment_state
*att_state
)
4167 return cmd_state
->subpass
->view_mask
& att_state
->pending_clear_views
;
4171 do_first_layer_clear(const struct anv_cmd_state
*cmd_state
,
4172 const struct anv_attachment_state
*att_state
)
4174 if (!cmd_state
->subpass
->view_mask
)
4177 uint32_t pending_clear_mask
=
4178 get_multiview_subpass_clear_mask(cmd_state
, att_state
);
4180 return pending_clear_mask
& 1;
4184 current_subpass_is_last_for_attachment(const struct anv_cmd_state
*cmd_state
,
4187 const uint32_t last_subpass_idx
=
4188 cmd_state
->pass
->attachments
[att_idx
].last_subpass_idx
;
4189 const struct anv_subpass
*last_subpass
=
4190 &cmd_state
->pass
->subpasses
[last_subpass_idx
];
4191 return last_subpass
== cmd_state
->subpass
;
4195 cmd_buffer_begin_subpass(struct anv_cmd_buffer
*cmd_buffer
,
4196 uint32_t subpass_id
)
4198 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
4199 struct anv_subpass
*subpass
= &cmd_state
->pass
->subpasses
[subpass_id
];
4200 cmd_state
->subpass
= subpass
;
4202 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
4204 /* Our implementation of VK_KHR_multiview uses instancing to draw the
4205 * different views. If the client asks for instancing, we need to use the
4206 * Instance Data Step Rate to ensure that we repeat the client's
4207 * per-instance data once for each view. Since this bit is in
4208 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
4212 cmd_buffer
->state
.gfx
.vb_dirty
|= ~0;
4214 /* It is possible to start a render pass with an old pipeline. Because the
4215 * render pass and subpass index are both baked into the pipeline, this is
4216 * highly unlikely. In order to do so, it requires that you have a render
4217 * pass with a single subpass and that you use that render pass twice
4218 * back-to-back and use the same pipeline at the start of the second render
4219 * pass as at the end of the first. In order to avoid unpredictable issues
4220 * with this edge case, we just dirty the pipeline at the start of every
4223 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
4225 /* Accumulate any subpass flushes that need to happen before the subpass */
4226 cmd_buffer
->state
.pending_pipe_bits
|=
4227 cmd_buffer
->state
.pass
->subpass_flushes
[subpass_id
];
4229 VkRect2D render_area
= cmd_buffer
->state
.render_area
;
4230 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
4232 bool is_multiview
= subpass
->view_mask
!= 0;
4234 for (uint32_t i
= 0; i
< subpass
->attachment_count
; ++i
) {
4235 const uint32_t a
= subpass
->attachments
[i
].attachment
;
4236 if (a
== VK_ATTACHMENT_UNUSED
)
4239 assert(a
< cmd_state
->pass
->attachment_count
);
4240 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[a
];
4242 struct anv_image_view
*iview
= cmd_state
->attachments
[a
].image_view
;
4243 const struct anv_image
*image
= iview
->image
;
4245 /* A resolve is necessary before use as an input attachment if the clear
4246 * color or auxiliary buffer usage isn't supported by the sampler.
4248 const bool input_needs_resolve
=
4249 (att_state
->fast_clear
&& !att_state
->clear_color_is_zero_one
) ||
4250 att_state
->input_aux_usage
!= att_state
->aux_usage
;
4252 VkImageLayout target_layout
, target_stencil_layout
;
4253 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
&&
4254 !input_needs_resolve
) {
4255 /* Layout transitions before the final only help to enable sampling
4256 * as an input attachment. If the input attachment supports sampling
4257 * using the auxiliary surface, we can skip such transitions by
4258 * making the target layout one that is CCS-aware.
4260 target_layout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
4262 target_layout
= subpass
->attachments
[i
].layout
;
4263 target_stencil_layout
= subpass
->attachments
[i
].stencil_layout
;
4266 uint32_t base_layer
, layer_count
;
4267 if (image
->type
== VK_IMAGE_TYPE_3D
) {
4269 layer_count
= anv_minify(iview
->image
->extent
.depth
,
4270 iview
->planes
[0].isl
.base_level
);
4272 base_layer
= iview
->planes
[0].isl
.base_array_layer
;
4273 layer_count
= fb
->layers
;
4276 if (image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
4277 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
4278 transition_color_buffer(cmd_buffer
, image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4279 iview
->planes
[0].isl
.base_level
, 1,
4280 base_layer
, layer_count
,
4281 att_state
->current_layout
, target_layout
);
4284 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
4285 transition_depth_buffer(cmd_buffer
, image
,
4286 att_state
->current_layout
, target_layout
);
4287 att_state
->aux_usage
=
4288 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
,
4289 VK_IMAGE_ASPECT_DEPTH_BIT
, target_layout
);
4292 if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
4293 transition_stencil_buffer(cmd_buffer
, image
,
4294 iview
->planes
[0].isl
.base_level
, 1,
4295 base_layer
, layer_count
,
4296 att_state
->current_stencil_layout
,
4297 target_stencil_layout
);
4299 att_state
->current_layout
= target_layout
;
4300 att_state
->current_stencil_layout
= target_stencil_layout
;
4302 if (att_state
->pending_clear_aspects
& VK_IMAGE_ASPECT_COLOR_BIT
) {
4303 assert(att_state
->pending_clear_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
4305 /* Multi-planar images are not supported as attachments */
4306 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
4307 assert(image
->n_planes
== 1);
4309 uint32_t base_clear_layer
= iview
->planes
[0].isl
.base_array_layer
;
4310 uint32_t clear_layer_count
= fb
->layers
;
4312 if (att_state
->fast_clear
&&
4313 do_first_layer_clear(cmd_state
, att_state
)) {
4314 /* We only support fast-clears on the first layer */
4315 assert(iview
->planes
[0].isl
.base_level
== 0);
4316 assert(iview
->planes
[0].isl
.base_array_layer
== 0);
4318 union isl_color_value clear_color
= {};
4319 anv_clear_color_from_att_state(&clear_color
, att_state
, iview
);
4320 if (iview
->image
->samples
== 1) {
4321 anv_image_ccs_op(cmd_buffer
, image
,
4322 iview
->planes
[0].isl
.format
,
4323 VK_IMAGE_ASPECT_COLOR_BIT
,
4324 0, 0, 1, ISL_AUX_OP_FAST_CLEAR
,
4328 anv_image_mcs_op(cmd_buffer
, image
,
4329 iview
->planes
[0].isl
.format
,
4330 VK_IMAGE_ASPECT_COLOR_BIT
,
4331 0, 1, ISL_AUX_OP_FAST_CLEAR
,
4336 clear_layer_count
--;
4338 att_state
->pending_clear_views
&= ~1;
4340 if (att_state
->clear_color_is_zero
) {
4341 /* This image has the auxiliary buffer enabled. We can mark the
4342 * subresource as not needing a resolve because the clear color
4343 * will match what's in every RENDER_SURFACE_STATE object when
4344 * it's being used for sampling.
4346 set_image_fast_clear_state(cmd_buffer
, iview
->image
,
4347 VK_IMAGE_ASPECT_COLOR_BIT
,
4348 ANV_FAST_CLEAR_DEFAULT_VALUE
);
4350 set_image_fast_clear_state(cmd_buffer
, iview
->image
,
4351 VK_IMAGE_ASPECT_COLOR_BIT
,
4352 ANV_FAST_CLEAR_ANY
);
4356 /* From the VkFramebufferCreateInfo spec:
4358 * "If the render pass uses multiview, then layers must be one and each
4359 * attachment requires a number of layers that is greater than the
4360 * maximum bit index set in the view mask in the subpasses in which it
4363 * So if multiview is active we ignore the number of layers in the
4364 * framebuffer and instead we honor the view mask from the subpass.
4367 assert(image
->n_planes
== 1);
4368 uint32_t pending_clear_mask
=
4369 get_multiview_subpass_clear_mask(cmd_state
, att_state
);
4372 for_each_bit(layer_idx
, pending_clear_mask
) {
4374 iview
->planes
[0].isl
.base_array_layer
+ layer_idx
;
4376 anv_image_clear_color(cmd_buffer
, image
,
4377 VK_IMAGE_ASPECT_COLOR_BIT
,
4378 att_state
->aux_usage
,
4379 iview
->planes
[0].isl
.format
,
4380 iview
->planes
[0].isl
.swizzle
,
4381 iview
->planes
[0].isl
.base_level
,
4384 vk_to_isl_color(att_state
->clear_value
.color
));
4387 att_state
->pending_clear_views
&= ~pending_clear_mask
;
4388 } else if (clear_layer_count
> 0) {
4389 assert(image
->n_planes
== 1);
4390 anv_image_clear_color(cmd_buffer
, image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4391 att_state
->aux_usage
,
4392 iview
->planes
[0].isl
.format
,
4393 iview
->planes
[0].isl
.swizzle
,
4394 iview
->planes
[0].isl
.base_level
,
4395 base_clear_layer
, clear_layer_count
,
4397 vk_to_isl_color(att_state
->clear_value
.color
));
4399 } else if (att_state
->pending_clear_aspects
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
4400 VK_IMAGE_ASPECT_STENCIL_BIT
)) {
4401 if (att_state
->fast_clear
&& !is_multiview
) {
4402 /* We currently only support HiZ for single-layer images */
4403 if (att_state
->pending_clear_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
4404 assert(iview
->image
->planes
[0].aux_usage
== ISL_AUX_USAGE_HIZ
);
4405 assert(iview
->planes
[0].isl
.base_level
== 0);
4406 assert(iview
->planes
[0].isl
.base_array_layer
== 0);
4407 assert(fb
->layers
== 1);
4410 anv_image_hiz_clear(cmd_buffer
, image
,
4411 att_state
->pending_clear_aspects
,
4412 iview
->planes
[0].isl
.base_level
,
4413 iview
->planes
[0].isl
.base_array_layer
,
4414 fb
->layers
, render_area
,
4415 att_state
->clear_value
.depthStencil
.stencil
);
4416 } else if (is_multiview
) {
4417 uint32_t pending_clear_mask
=
4418 get_multiview_subpass_clear_mask(cmd_state
, att_state
);
4421 for_each_bit(layer_idx
, pending_clear_mask
) {
4423 iview
->planes
[0].isl
.base_array_layer
+ layer_idx
;
4425 anv_image_clear_depth_stencil(cmd_buffer
, image
,
4426 att_state
->pending_clear_aspects
,
4427 att_state
->aux_usage
,
4428 iview
->planes
[0].isl
.base_level
,
4431 att_state
->clear_value
.depthStencil
.depth
,
4432 att_state
->clear_value
.depthStencil
.stencil
);
4435 att_state
->pending_clear_views
&= ~pending_clear_mask
;
4437 anv_image_clear_depth_stencil(cmd_buffer
, image
,
4438 att_state
->pending_clear_aspects
,
4439 att_state
->aux_usage
,
4440 iview
->planes
[0].isl
.base_level
,
4441 iview
->planes
[0].isl
.base_array_layer
,
4442 fb
->layers
, render_area
,
4443 att_state
->clear_value
.depthStencil
.depth
,
4444 att_state
->clear_value
.depthStencil
.stencil
);
4447 assert(att_state
->pending_clear_aspects
== 0);
4451 (att_state
->pending_load_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) &&
4452 image
->planes
[0].aux_surface
.isl
.size_B
> 0 &&
4453 iview
->planes
[0].isl
.base_level
== 0 &&
4454 iview
->planes
[0].isl
.base_array_layer
== 0) {
4455 if (att_state
->aux_usage
!= ISL_AUX_USAGE_NONE
) {
4456 genX(copy_fast_clear_dwords
)(cmd_buffer
, att_state
->color
.state
,
4457 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4458 false /* copy to ss */);
4461 if (need_input_attachment_state(&cmd_state
->pass
->attachments
[a
]) &&
4462 att_state
->input_aux_usage
!= ISL_AUX_USAGE_NONE
) {
4463 genX(copy_fast_clear_dwords
)(cmd_buffer
, att_state
->input
.state
,
4464 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4465 false /* copy to ss */);
4469 if (subpass
->attachments
[i
].usage
==
4470 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
4471 /* We assume that if we're starting a subpass, we're going to do some
4472 * rendering so we may end up with compressed data.
4474 genX(cmd_buffer_mark_image_written
)(cmd_buffer
, iview
->image
,
4475 VK_IMAGE_ASPECT_COLOR_BIT
,
4476 att_state
->aux_usage
,
4477 iview
->planes
[0].isl
.base_level
,
4478 iview
->planes
[0].isl
.base_array_layer
,
4480 } else if (subpass
->attachments
[i
].usage
==
4481 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
4482 /* We may be writing depth or stencil so we need to mark the surface.
4483 * Unfortunately, there's no way to know at this point whether the
4484 * depth or stencil tests used will actually write to the surface.
4486 * Even though stencil may be plane 1, it always shares a base_level
4489 const struct isl_view
*ds_view
= &iview
->planes
[0].isl
;
4490 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
4491 genX(cmd_buffer_mark_image_written
)(cmd_buffer
, image
,
4492 VK_IMAGE_ASPECT_DEPTH_BIT
,
4493 att_state
->aux_usage
,
4494 ds_view
->base_level
,
4495 ds_view
->base_array_layer
,
4498 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
4499 /* Even though stencil may be plane 1, it always shares a
4500 * base_level with depth.
4502 genX(cmd_buffer_mark_image_written
)(cmd_buffer
, image
,
4503 VK_IMAGE_ASPECT_STENCIL_BIT
,
4505 ds_view
->base_level
,
4506 ds_view
->base_array_layer
,
4511 /* If multiview is enabled, then we are only done clearing when we no
4512 * longer have pending layers to clear, or when we have processed the
4513 * last subpass that uses this attachment.
4515 if (!is_multiview
||
4516 att_state
->pending_clear_views
== 0 ||
4517 current_subpass_is_last_for_attachment(cmd_state
, a
)) {
4518 att_state
->pending_clear_aspects
= 0;
4521 att_state
->pending_load_aspects
= 0;
4524 cmd_buffer_emit_depth_stencil(cmd_buffer
);
4527 /* The PIPE_CONTROL command description says:
4529 * "Whenever a Binding Table Index (BTI) used by a Render Taget Message
4530 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
4531 * Target Cache Flush by enabling this bit. When render target flush
4532 * is set due to new association of BTI, PS Scoreboard Stall bit must
4533 * be set in this packet."
4535 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
4536 pc
.RenderTargetCacheFlushEnable
= true;
4537 pc
.StallAtPixelScoreboard
= true;
4539 pc
.TileCacheFlushEnable
= true;
4545 static enum blorp_filter
4546 vk_to_blorp_resolve_mode(VkResolveModeFlagBitsKHR vk_mode
)
4549 case VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
:
4550 return BLORP_FILTER_SAMPLE_0
;
4551 case VK_RESOLVE_MODE_AVERAGE_BIT_KHR
:
4552 return BLORP_FILTER_AVERAGE
;
4553 case VK_RESOLVE_MODE_MIN_BIT_KHR
:
4554 return BLORP_FILTER_MIN_SAMPLE
;
4555 case VK_RESOLVE_MODE_MAX_BIT_KHR
:
4556 return BLORP_FILTER_MAX_SAMPLE
;
4558 return BLORP_FILTER_NONE
;
4563 cmd_buffer_end_subpass(struct anv_cmd_buffer
*cmd_buffer
)
4565 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
4566 struct anv_subpass
*subpass
= cmd_state
->subpass
;
4567 uint32_t subpass_id
= anv_get_subpass_id(&cmd_buffer
->state
);
4568 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
4570 if (subpass
->has_color_resolve
) {
4571 /* We are about to do some MSAA resolves. We need to flush so that the
4572 * result of writes to the MSAA color attachments show up in the sampler
4573 * when we blit to the single-sampled resolve target.
4575 cmd_buffer
->state
.pending_pipe_bits
|=
4576 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
|
4577 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
4579 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
4580 uint32_t src_att
= subpass
->color_attachments
[i
].attachment
;
4581 uint32_t dst_att
= subpass
->resolve_attachments
[i
].attachment
;
4583 if (dst_att
== VK_ATTACHMENT_UNUSED
)
4586 assert(src_att
< cmd_buffer
->state
.pass
->attachment_count
);
4587 assert(dst_att
< cmd_buffer
->state
.pass
->attachment_count
);
4589 if (cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
) {
4590 /* From the Vulkan 1.0 spec:
4592 * If the first use of an attachment in a render pass is as a
4593 * resolve attachment, then the loadOp is effectively ignored
4594 * as the resolve is guaranteed to overwrite all pixels in the
4597 cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
= 0;
4600 struct anv_image_view
*src_iview
= cmd_state
->attachments
[src_att
].image_view
;
4601 struct anv_image_view
*dst_iview
= cmd_state
->attachments
[dst_att
].image_view
;
4603 const VkRect2D render_area
= cmd_buffer
->state
.render_area
;
4605 enum isl_aux_usage src_aux_usage
=
4606 cmd_buffer
->state
.attachments
[src_att
].aux_usage
;
4607 enum isl_aux_usage dst_aux_usage
=
4608 cmd_buffer
->state
.attachments
[dst_att
].aux_usage
;
4610 assert(src_iview
->aspect_mask
== VK_IMAGE_ASPECT_COLOR_BIT
&&
4611 dst_iview
->aspect_mask
== VK_IMAGE_ASPECT_COLOR_BIT
);
4613 anv_image_msaa_resolve(cmd_buffer
,
4614 src_iview
->image
, src_aux_usage
,
4615 src_iview
->planes
[0].isl
.base_level
,
4616 src_iview
->planes
[0].isl
.base_array_layer
,
4617 dst_iview
->image
, dst_aux_usage
,
4618 dst_iview
->planes
[0].isl
.base_level
,
4619 dst_iview
->planes
[0].isl
.base_array_layer
,
4620 VK_IMAGE_ASPECT_COLOR_BIT
,
4621 render_area
.offset
.x
, render_area
.offset
.y
,
4622 render_area
.offset
.x
, render_area
.offset
.y
,
4623 render_area
.extent
.width
,
4624 render_area
.extent
.height
,
4625 fb
->layers
, BLORP_FILTER_NONE
);
4629 if (subpass
->ds_resolve_attachment
) {
4630 /* We are about to do some MSAA resolves. We need to flush so that the
4631 * result of writes to the MSAA depth attachments show up in the sampler
4632 * when we blit to the single-sampled resolve target.
4634 cmd_buffer
->state
.pending_pipe_bits
|=
4635 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
|
4636 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
4638 uint32_t src_att
= subpass
->depth_stencil_attachment
->attachment
;
4639 uint32_t dst_att
= subpass
->ds_resolve_attachment
->attachment
;
4641 assert(src_att
< cmd_buffer
->state
.pass
->attachment_count
);
4642 assert(dst_att
< cmd_buffer
->state
.pass
->attachment_count
);
4644 if (cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
) {
4645 /* From the Vulkan 1.0 spec:
4647 * If the first use of an attachment in a render pass is as a
4648 * resolve attachment, then the loadOp is effectively ignored
4649 * as the resolve is guaranteed to overwrite all pixels in the
4652 cmd_buffer
->state
.attachments
[dst_att
].pending_clear_aspects
= 0;
4655 struct anv_image_view
*src_iview
= cmd_state
->attachments
[src_att
].image_view
;
4656 struct anv_image_view
*dst_iview
= cmd_state
->attachments
[dst_att
].image_view
;
4658 const VkRect2D render_area
= cmd_buffer
->state
.render_area
;
4660 struct anv_attachment_state
*src_state
=
4661 &cmd_state
->attachments
[src_att
];
4662 struct anv_attachment_state
*dst_state
=
4663 &cmd_state
->attachments
[dst_att
];
4665 if ((src_iview
->image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
4666 subpass
->depth_resolve_mode
!= VK_RESOLVE_MODE_NONE_KHR
) {
4668 /* MSAA resolves sample from the source attachment. Transition the
4669 * depth attachment first to get rid of any HiZ that we may not be
4672 transition_depth_buffer(cmd_buffer
, src_iview
->image
,
4673 src_state
->current_layout
,
4674 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
);
4675 src_state
->aux_usage
=
4676 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, src_iview
->image
,
4677 VK_IMAGE_ASPECT_DEPTH_BIT
,
4678 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
);
4679 src_state
->current_layout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
4681 /* MSAA resolves write to the resolve attachment as if it were any
4682 * other transfer op. Transition the resolve attachment accordingly.
4684 VkImageLayout dst_initial_layout
= dst_state
->current_layout
;
4686 /* If our render area is the entire size of the image, we're going to
4687 * blow it all away so we can claim the initial layout is UNDEFINED
4688 * and we'll get a HiZ ambiguate instead of a resolve.
4690 if (dst_iview
->image
->type
!= VK_IMAGE_TYPE_3D
&&
4691 render_area
.offset
.x
== 0 && render_area
.offset
.y
== 0 &&
4692 render_area
.extent
.width
== dst_iview
->extent
.width
&&
4693 render_area
.extent
.height
== dst_iview
->extent
.height
)
4694 dst_initial_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
4696 transition_depth_buffer(cmd_buffer
, dst_iview
->image
,
4698 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
);
4699 dst_state
->aux_usage
=
4700 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, dst_iview
->image
,
4701 VK_IMAGE_ASPECT_DEPTH_BIT
,
4702 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
);
4703 dst_state
->current_layout
= VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
;
4705 enum blorp_filter filter
=
4706 vk_to_blorp_resolve_mode(subpass
->depth_resolve_mode
);
4708 anv_image_msaa_resolve(cmd_buffer
,
4709 src_iview
->image
, src_state
->aux_usage
,
4710 src_iview
->planes
[0].isl
.base_level
,
4711 src_iview
->planes
[0].isl
.base_array_layer
,
4712 dst_iview
->image
, dst_state
->aux_usage
,
4713 dst_iview
->planes
[0].isl
.base_level
,
4714 dst_iview
->planes
[0].isl
.base_array_layer
,
4715 VK_IMAGE_ASPECT_DEPTH_BIT
,
4716 render_area
.offset
.x
, render_area
.offset
.y
,
4717 render_area
.offset
.x
, render_area
.offset
.y
,
4718 render_area
.extent
.width
,
4719 render_area
.extent
.height
,
4720 fb
->layers
, filter
);
4723 if ((src_iview
->image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
4724 subpass
->stencil_resolve_mode
!= VK_RESOLVE_MODE_NONE_KHR
) {
4726 src_state
->current_stencil_layout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
4727 dst_state
->current_stencil_layout
= VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
;
4729 enum isl_aux_usage src_aux_usage
= ISL_AUX_USAGE_NONE
;
4730 enum isl_aux_usage dst_aux_usage
= ISL_AUX_USAGE_NONE
;
4732 enum blorp_filter filter
=
4733 vk_to_blorp_resolve_mode(subpass
->stencil_resolve_mode
);
4735 anv_image_msaa_resolve(cmd_buffer
,
4736 src_iview
->image
, src_aux_usage
,
4737 src_iview
->planes
[0].isl
.base_level
,
4738 src_iview
->planes
[0].isl
.base_array_layer
,
4739 dst_iview
->image
, dst_aux_usage
,
4740 dst_iview
->planes
[0].isl
.base_level
,
4741 dst_iview
->planes
[0].isl
.base_array_layer
,
4742 VK_IMAGE_ASPECT_STENCIL_BIT
,
4743 render_area
.offset
.x
, render_area
.offset
.y
,
4744 render_area
.offset
.x
, render_area
.offset
.y
,
4745 render_area
.extent
.width
,
4746 render_area
.extent
.height
,
4747 fb
->layers
, filter
);
4752 /* On gen7, we have to store a texturable version of the stencil buffer in
4753 * a shadow whenever VK_IMAGE_USAGE_SAMPLED_BIT is set and copy back and
4754 * forth at strategic points. Stencil writes are only allowed in following
4757 * - VK_IMAGE_LAYOUT_GENERAL
4758 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
4759 * - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
4760 * - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
4761 * - VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR
4763 * For general, we have no nice opportunity to transition so we do the copy
4764 * to the shadow unconditionally at the end of the subpass. For transfer
4765 * destinations, we can update it as part of the transfer op. For the other
4766 * layouts, we delay the copy until a transition into some other layout.
4768 if (subpass
->depth_stencil_attachment
) {
4769 uint32_t a
= subpass
->depth_stencil_attachment
->attachment
;
4770 assert(a
!= VK_ATTACHMENT_UNUSED
);
4772 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[a
];
4773 struct anv_image_view
*iview
= cmd_state
->attachments
[a
].image_view
;;
4774 const struct anv_image
*image
= iview
->image
;
4776 if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
4777 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
4778 VK_IMAGE_ASPECT_STENCIL_BIT
);
4780 if (image
->planes
[plane
].shadow_surface
.isl
.size_B
> 0 &&
4781 att_state
->current_stencil_layout
== VK_IMAGE_LAYOUT_GENERAL
) {
4782 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
4783 anv_image_copy_to_shadow(cmd_buffer
, image
,
4784 VK_IMAGE_ASPECT_STENCIL_BIT
,
4785 iview
->planes
[plane
].isl
.base_level
, 1,
4786 iview
->planes
[plane
].isl
.base_array_layer
,
4791 #endif /* GEN_GEN == 7 */
4793 for (uint32_t i
= 0; i
< subpass
->attachment_count
; ++i
) {
4794 const uint32_t a
= subpass
->attachments
[i
].attachment
;
4795 if (a
== VK_ATTACHMENT_UNUSED
)
4798 if (cmd_state
->pass
->attachments
[a
].last_subpass_idx
!= subpass_id
)
4801 assert(a
< cmd_state
->pass
->attachment_count
);
4802 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[a
];
4803 struct anv_image_view
*iview
= cmd_state
->attachments
[a
].image_view
;
4804 const struct anv_image
*image
= iview
->image
;
4806 if ((image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) &&
4807 image
->vk_format
!= iview
->vk_format
) {
4808 enum anv_fast_clear_type fast_clear_type
=
4809 anv_layout_to_fast_clear_type(&cmd_buffer
->device
->info
,
4810 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4811 att_state
->current_layout
);
4813 /* If any clear color was used, flush it down the aux surfaces. If we
4814 * don't do it now using the view's format we might use the clear
4815 * color incorrectly in the following resolves (for example with an
4816 * SRGB view & a UNORM image).
4818 if (fast_clear_type
!= ANV_FAST_CLEAR_NONE
) {
4819 anv_perf_warn(cmd_buffer
->device
->instance
, iview
,
4820 "Doing a partial resolve to get rid of clear color at the "
4821 "end of a renderpass due to an image/view format mismatch");
4823 uint32_t base_layer
, layer_count
;
4824 if (image
->type
== VK_IMAGE_TYPE_3D
) {
4826 layer_count
= anv_minify(iview
->image
->extent
.depth
,
4827 iview
->planes
[0].isl
.base_level
);
4829 base_layer
= iview
->planes
[0].isl
.base_array_layer
;
4830 layer_count
= fb
->layers
;
4833 for (uint32_t a
= 0; a
< layer_count
; a
++) {
4834 uint32_t array_layer
= base_layer
+ a
;
4835 if (image
->samples
== 1) {
4836 anv_cmd_predicated_ccs_resolve(cmd_buffer
, image
,
4837 iview
->planes
[0].isl
.format
,
4838 VK_IMAGE_ASPECT_COLOR_BIT
,
4839 iview
->planes
[0].isl
.base_level
,
4841 ISL_AUX_OP_PARTIAL_RESOLVE
,
4842 ANV_FAST_CLEAR_NONE
);
4844 anv_cmd_predicated_mcs_resolve(cmd_buffer
, image
,
4845 iview
->planes
[0].isl
.format
,
4846 VK_IMAGE_ASPECT_COLOR_BIT
,
4848 ISL_AUX_OP_PARTIAL_RESOLVE
,
4849 ANV_FAST_CLEAR_NONE
);
4855 /* Transition the image into the final layout for this render pass */
4856 VkImageLayout target_layout
=
4857 cmd_state
->pass
->attachments
[a
].final_layout
;
4858 VkImageLayout target_stencil_layout
=
4859 cmd_state
->pass
->attachments
[a
].stencil_final_layout
;
4861 uint32_t base_layer
, layer_count
;
4862 if (image
->type
== VK_IMAGE_TYPE_3D
) {
4864 layer_count
= anv_minify(iview
->image
->extent
.depth
,
4865 iview
->planes
[0].isl
.base_level
);
4867 base_layer
= iview
->planes
[0].isl
.base_array_layer
;
4868 layer_count
= fb
->layers
;
4871 if (image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
4872 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
4873 transition_color_buffer(cmd_buffer
, image
, VK_IMAGE_ASPECT_COLOR_BIT
,
4874 iview
->planes
[0].isl
.base_level
, 1,
4875 base_layer
, layer_count
,
4876 att_state
->current_layout
, target_layout
);
4879 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
4880 transition_depth_buffer(cmd_buffer
, image
,
4881 att_state
->current_layout
, target_layout
);
4884 if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
4885 transition_stencil_buffer(cmd_buffer
, image
,
4886 iview
->planes
[0].isl
.base_level
, 1,
4887 base_layer
, layer_count
,
4888 att_state
->current_stencil_layout
,
4889 target_stencil_layout
);
4893 /* Accumulate any subpass flushes that need to happen after the subpass.
4894 * Yes, they do get accumulated twice in the NextSubpass case but since
4895 * genX_CmdNextSubpass just calls end/begin back-to-back, we just end up
4896 * ORing the bits in twice so it's harmless.
4898 cmd_buffer
->state
.pending_pipe_bits
|=
4899 cmd_buffer
->state
.pass
->subpass_flushes
[subpass_id
+ 1];
4902 void genX(CmdBeginRenderPass
)(
4903 VkCommandBuffer commandBuffer
,
4904 const VkRenderPassBeginInfo
* pRenderPassBegin
,
4905 VkSubpassContents contents
)
4907 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4908 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
4909 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
4911 cmd_buffer
->state
.framebuffer
= framebuffer
;
4912 cmd_buffer
->state
.pass
= pass
;
4913 cmd_buffer
->state
.render_area
= pRenderPassBegin
->renderArea
;
4915 genX(cmd_buffer_setup_attachments
)(cmd_buffer
, pass
, pRenderPassBegin
);
4917 /* If we failed to setup the attachments we should not try to go further */
4918 if (result
!= VK_SUCCESS
) {
4919 assert(anv_batch_has_error(&cmd_buffer
->batch
));
4923 genX(flush_pipeline_select_3d
)(cmd_buffer
);
4925 cmd_buffer_begin_subpass(cmd_buffer
, 0);
4928 void genX(CmdBeginRenderPass2KHR
)(
4929 VkCommandBuffer commandBuffer
,
4930 const VkRenderPassBeginInfo
* pRenderPassBeginInfo
,
4931 const VkSubpassBeginInfoKHR
* pSubpassBeginInfo
)
4933 genX(CmdBeginRenderPass
)(commandBuffer
, pRenderPassBeginInfo
,
4934 pSubpassBeginInfo
->contents
);
4937 void genX(CmdNextSubpass
)(
4938 VkCommandBuffer commandBuffer
,
4939 VkSubpassContents contents
)
4941 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4943 if (anv_batch_has_error(&cmd_buffer
->batch
))
4946 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
4948 uint32_t prev_subpass
= anv_get_subpass_id(&cmd_buffer
->state
);
4949 cmd_buffer_end_subpass(cmd_buffer
);
4950 cmd_buffer_begin_subpass(cmd_buffer
, prev_subpass
+ 1);
4953 void genX(CmdNextSubpass2KHR
)(
4954 VkCommandBuffer commandBuffer
,
4955 const VkSubpassBeginInfoKHR
* pSubpassBeginInfo
,
4956 const VkSubpassEndInfoKHR
* pSubpassEndInfo
)
4958 genX(CmdNextSubpass
)(commandBuffer
, pSubpassBeginInfo
->contents
);
4961 void genX(CmdEndRenderPass
)(
4962 VkCommandBuffer commandBuffer
)
4964 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4966 if (anv_batch_has_error(&cmd_buffer
->batch
))
4969 cmd_buffer_end_subpass(cmd_buffer
);
4971 cmd_buffer
->state
.hiz_enabled
= false;
4974 anv_dump_add_attachments(cmd_buffer
);
4977 /* Remove references to render pass specific state. This enables us to
4978 * detect whether or not we're in a renderpass.
4980 cmd_buffer
->state
.framebuffer
= NULL
;
4981 cmd_buffer
->state
.pass
= NULL
;
4982 cmd_buffer
->state
.subpass
= NULL
;
4985 void genX(CmdEndRenderPass2KHR
)(
4986 VkCommandBuffer commandBuffer
,
4987 const VkSubpassEndInfoKHR
* pSubpassEndInfo
)
4989 genX(CmdEndRenderPass
)(commandBuffer
);
4993 genX(cmd_emit_conditional_render_predicate
)(struct anv_cmd_buffer
*cmd_buffer
)
4995 #if GEN_GEN >= 8 || GEN_IS_HASWELL
4996 struct gen_mi_builder b
;
4997 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
4999 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC0
),
5000 gen_mi_reg32(ANV_PREDICATE_RESULT_REG
));
5001 gen_mi_store(&b
, gen_mi_reg64(MI_PREDICATE_SRC1
), gen_mi_imm(0));
5003 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
5004 mip
.LoadOperation
= LOAD_LOADINV
;
5005 mip
.CombineOperation
= COMBINE_SET
;
5006 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
5011 #if GEN_GEN >= 8 || GEN_IS_HASWELL
5012 void genX(CmdBeginConditionalRenderingEXT
)(
5013 VkCommandBuffer commandBuffer
,
5014 const VkConditionalRenderingBeginInfoEXT
* pConditionalRenderingBegin
)
5016 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5017 ANV_FROM_HANDLE(anv_buffer
, buffer
, pConditionalRenderingBegin
->buffer
);
5018 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
5019 struct anv_address value_address
=
5020 anv_address_add(buffer
->address
, pConditionalRenderingBegin
->offset
);
5022 const bool isInverted
= pConditionalRenderingBegin
->flags
&
5023 VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT
;
5025 cmd_state
->conditional_render_enabled
= true;
5027 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
5029 struct gen_mi_builder b
;
5030 gen_mi_builder_init(&b
, &cmd_buffer
->batch
);
5032 /* Section 19.4 of the Vulkan 1.1.85 spec says:
5034 * If the value of the predicate in buffer memory changes
5035 * while conditional rendering is active, the rendering commands
5036 * may be discarded in an implementation-dependent way.
5037 * Some implementations may latch the value of the predicate
5038 * upon beginning conditional rendering while others
5039 * may read it before every rendering command.
5041 * So it's perfectly fine to read a value from the buffer once.
5043 struct gen_mi_value value
= gen_mi_mem32(value_address
);
5045 /* Precompute predicate result, it is necessary to support secondary
5046 * command buffers since it is unknown if conditional rendering is
5047 * inverted when populating them.
5049 gen_mi_store(&b
, gen_mi_reg64(ANV_PREDICATE_RESULT_REG
),
5050 isInverted
? gen_mi_uge(&b
, gen_mi_imm(0), value
) :
5051 gen_mi_ult(&b
, gen_mi_imm(0), value
));
5054 void genX(CmdEndConditionalRenderingEXT
)(
5055 VkCommandBuffer commandBuffer
)
5057 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5058 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
5060 cmd_state
->conditional_render_enabled
= false;
5064 /* Set of stage bits for which are pipelined, i.e. they get queued by the
5065 * command streamer for later execution.
5067 #define ANV_PIPELINE_STAGE_PIPELINED_BITS \
5068 (VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | \
5069 VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | \
5070 VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT | \
5071 VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT | \
5072 VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT | \
5073 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | \
5074 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | \
5075 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | \
5076 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | \
5077 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | \
5078 VK_PIPELINE_STAGE_TRANSFER_BIT | \
5079 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT | \
5080 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT | \
5081 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT)
5083 void genX(CmdSetEvent
)(
5084 VkCommandBuffer commandBuffer
,
5086 VkPipelineStageFlags stageMask
)
5088 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5089 ANV_FROM_HANDLE(anv_event
, event
, _event
);
5091 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
5092 if (stageMask
& ANV_PIPELINE_STAGE_PIPELINED_BITS
) {
5093 pc
.StallAtPixelScoreboard
= true;
5094 pc
.CommandStreamerStallEnable
= true;
5097 pc
.DestinationAddressType
= DAT_PPGTT
,
5098 pc
.PostSyncOperation
= WriteImmediateData
,
5099 pc
.Address
= (struct anv_address
) {
5100 cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
5103 pc
.ImmediateData
= VK_EVENT_SET
;
5107 void genX(CmdResetEvent
)(
5108 VkCommandBuffer commandBuffer
,
5110 VkPipelineStageFlags stageMask
)
5112 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5113 ANV_FROM_HANDLE(anv_event
, event
, _event
);
5115 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
5116 if (stageMask
& ANV_PIPELINE_STAGE_PIPELINED_BITS
) {
5117 pc
.StallAtPixelScoreboard
= true;
5118 pc
.CommandStreamerStallEnable
= true;
5121 pc
.DestinationAddressType
= DAT_PPGTT
;
5122 pc
.PostSyncOperation
= WriteImmediateData
;
5123 pc
.Address
= (struct anv_address
) {
5124 cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
5127 pc
.ImmediateData
= VK_EVENT_RESET
;
5131 void genX(CmdWaitEvents
)(
5132 VkCommandBuffer commandBuffer
,
5133 uint32_t eventCount
,
5134 const VkEvent
* pEvents
,
5135 VkPipelineStageFlags srcStageMask
,
5136 VkPipelineStageFlags destStageMask
,
5137 uint32_t memoryBarrierCount
,
5138 const VkMemoryBarrier
* pMemoryBarriers
,
5139 uint32_t bufferMemoryBarrierCount
,
5140 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
5141 uint32_t imageMemoryBarrierCount
,
5142 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
5145 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5147 for (uint32_t i
= 0; i
< eventCount
; i
++) {
5148 ANV_FROM_HANDLE(anv_event
, event
, pEvents
[i
]);
5150 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_SEMAPHORE_WAIT
), sem
) {
5151 sem
.WaitMode
= PollingMode
,
5152 sem
.CompareOperation
= COMPARE_SAD_EQUAL_SDD
,
5153 sem
.SemaphoreDataDword
= VK_EVENT_SET
,
5154 sem
.SemaphoreAddress
= (struct anv_address
) {
5155 cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
5161 anv_finishme("Implement events on gen7");
5164 genX(CmdPipelineBarrier
)(commandBuffer
, srcStageMask
, destStageMask
,
5165 false, /* byRegion */
5166 memoryBarrierCount
, pMemoryBarriers
,
5167 bufferMemoryBarrierCount
, pBufferMemoryBarriers
,
5168 imageMemoryBarrierCount
, pImageMemoryBarriers
);
5171 VkResult
genX(CmdSetPerformanceOverrideINTEL
)(
5172 VkCommandBuffer commandBuffer
,
5173 const VkPerformanceOverrideInfoINTEL
* pOverrideInfo
)
5175 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5177 switch (pOverrideInfo
->type
) {
5178 case VK_PERFORMANCE_OVERRIDE_TYPE_NULL_HARDWARE_INTEL
: {
5182 anv_pack_struct(&dw
, GENX(CS_DEBUG_MODE2
),
5183 ._3DRenderingInstructionDisable
= pOverrideInfo
->enable
,
5184 .MediaInstructionDisable
= pOverrideInfo
->enable
,
5185 ._3DRenderingInstructionDisableMask
= true,
5186 .MediaInstructionDisableMask
= true);
5187 emit_lri(&cmd_buffer
->batch
, GENX(CS_DEBUG_MODE2_num
), dw
);
5189 anv_pack_struct(&dw
, GENX(INSTPM
),
5190 ._3DRenderingInstructionDisable
= pOverrideInfo
->enable
,
5191 .MediaInstructionDisable
= pOverrideInfo
->enable
,
5192 ._3DRenderingInstructionDisableMask
= true,
5193 .MediaInstructionDisableMask
= true);
5194 emit_lri(&cmd_buffer
->batch
, GENX(INSTPM_num
), dw
);
5199 case VK_PERFORMANCE_OVERRIDE_TYPE_FLUSH_GPU_CACHES_INTEL
:
5200 if (pOverrideInfo
->enable
) {
5201 /* FLUSH ALL THE THINGS! As requested by the MDAPI team. */
5202 cmd_buffer
->state
.pending_pipe_bits
|=
5203 ANV_PIPE_FLUSH_BITS
|
5204 ANV_PIPE_INVALIDATE_BITS
;
5205 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
5210 unreachable("Invalid override");
5216 VkResult
genX(CmdSetPerformanceStreamMarkerINTEL
)(
5217 VkCommandBuffer commandBuffer
,
5218 const VkPerformanceStreamMarkerInfoINTEL
* pMarkerInfo
)
5220 /* TODO: Waiting on the register to write, might depend on generation. */