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"
31 #include "common/gen_l3_config.h"
32 #include "genxml/gen_macros.h"
33 #include "genxml/genX_pack.h"
36 emit_lrm(struct anv_batch
*batch
,
37 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
39 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
40 lrm
.RegisterAddress
= reg
;
41 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
46 emit_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
48 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
49 lri
.RegisterOffset
= reg
;
54 #if GEN_IS_HASWELL || GEN_GEN >= 8
56 emit_lrr(struct anv_batch
*batch
, uint32_t dst
, uint32_t src
)
58 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_REG
), lrr
) {
59 lrr
.SourceRegisterAddress
= src
;
60 lrr
.DestinationRegisterAddress
= dst
;
66 genX(cmd_buffer_emit_state_base_address
)(struct anv_cmd_buffer
*cmd_buffer
)
68 struct anv_device
*device
= cmd_buffer
->device
;
70 /* Emit a render target cache flush.
72 * This isn't documented anywhere in the PRM. However, it seems to be
73 * necessary prior to changing the surface state base adress. Without
74 * this, we get GPU hangs when using multi-level command buffers which
75 * clear depth, reset state base address, and then go render stuff.
77 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
78 pc
.DCFlushEnable
= true;
79 pc
.RenderTargetCacheFlushEnable
= true;
80 pc
.CommandStreamerStallEnable
= true;
83 anv_batch_emit(&cmd_buffer
->batch
, GENX(STATE_BASE_ADDRESS
), sba
) {
84 sba
.GeneralStateBaseAddress
= (struct anv_address
) { NULL
, 0 };
85 sba
.GeneralStateMemoryObjectControlState
= GENX(MOCS
);
86 sba
.GeneralStateBaseAddressModifyEnable
= true;
88 sba
.SurfaceStateBaseAddress
=
89 anv_cmd_buffer_surface_base_address(cmd_buffer
);
90 sba
.SurfaceStateMemoryObjectControlState
= GENX(MOCS
);
91 sba
.SurfaceStateBaseAddressModifyEnable
= true;
93 sba
.DynamicStateBaseAddress
=
94 (struct anv_address
) { &device
->dynamic_state_pool
.block_pool
.bo
, 0 };
95 sba
.DynamicStateMemoryObjectControlState
= GENX(MOCS
);
96 sba
.DynamicStateBaseAddressModifyEnable
= true;
98 sba
.IndirectObjectBaseAddress
= (struct anv_address
) { NULL
, 0 };
99 sba
.IndirectObjectMemoryObjectControlState
= GENX(MOCS
);
100 sba
.IndirectObjectBaseAddressModifyEnable
= true;
102 sba
.InstructionBaseAddress
=
103 (struct anv_address
) { &device
->instruction_state_pool
.block_pool
.bo
, 0 };
104 sba
.InstructionMemoryObjectControlState
= GENX(MOCS
);
105 sba
.InstructionBaseAddressModifyEnable
= true;
108 /* Broadwell requires that we specify a buffer size for a bunch of
109 * these fields. However, since we will be growing the BO's live, we
110 * just set them all to the maximum.
112 sba
.GeneralStateBufferSize
= 0xfffff;
113 sba
.GeneralStateBufferSizeModifyEnable
= true;
114 sba
.DynamicStateBufferSize
= 0xfffff;
115 sba
.DynamicStateBufferSizeModifyEnable
= true;
116 sba
.IndirectObjectBufferSize
= 0xfffff;
117 sba
.IndirectObjectBufferSizeModifyEnable
= true;
118 sba
.InstructionBufferSize
= 0xfffff;
119 sba
.InstructionBuffersizeModifyEnable
= true;
123 /* After re-setting the surface state base address, we have to do some
124 * cache flusing so that the sampler engine will pick up the new
125 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
126 * Shared Function > 3D Sampler > State > State Caching (page 96):
128 * Coherency with system memory in the state cache, like the texture
129 * cache is handled partially by software. It is expected that the
130 * command stream or shader will issue Cache Flush operation or
131 * Cache_Flush sampler message to ensure that the L1 cache remains
132 * coherent with system memory.
136 * Whenever the value of the Dynamic_State_Base_Addr,
137 * Surface_State_Base_Addr are altered, the L1 state cache must be
138 * invalidated to ensure the new surface or sampler state is fetched
139 * from system memory.
141 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
142 * which, according the PIPE_CONTROL instruction documentation in the
145 * Setting this bit is independent of any other bit in this packet.
146 * This bit controls the invalidation of the L1 and L2 state caches
147 * at the top of the pipe i.e. at the parsing time.
149 * Unfortunately, experimentation seems to indicate that state cache
150 * invalidation through a PIPE_CONTROL does nothing whatsoever in
151 * regards to surface state and binding tables. In stead, it seems that
152 * invalidating the texture cache is what is actually needed.
154 * XXX: As far as we have been able to determine through
155 * experimentation, shows that flush the texture cache appears to be
156 * sufficient. The theory here is that all of the sampling/rendering
157 * units cache the binding table in the texture cache. However, we have
158 * yet to be able to actually confirm this.
160 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
161 pc
.TextureCacheInvalidationEnable
= true;
162 pc
.ConstantCacheInvalidationEnable
= true;
163 pc
.StateCacheInvalidationEnable
= true;
168 add_surface_state_reloc(struct anv_cmd_buffer
*cmd_buffer
,
169 struct anv_state state
,
170 struct anv_bo
*bo
, uint32_t offset
)
172 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
175 anv_reloc_list_add(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
,
176 state
.offset
+ isl_dev
->ss
.addr_offset
, bo
, offset
);
177 if (result
!= VK_SUCCESS
)
178 anv_batch_set_error(&cmd_buffer
->batch
, result
);
182 add_image_relocs(struct anv_cmd_buffer
*cmd_buffer
,
183 const struct anv_image
*image
,
184 const uint32_t plane
,
185 struct anv_surface_state state
)
187 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
189 add_surface_state_reloc(cmd_buffer
, state
.state
,
190 image
->planes
[plane
].bo
, state
.address
);
192 if (state
.aux_address
) {
194 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
195 &cmd_buffer
->pool
->alloc
,
196 state
.state
.offset
+ isl_dev
->ss
.aux_addr_offset
,
197 image
->planes
[plane
].bo
,
199 if (result
!= VK_SUCCESS
)
200 anv_batch_set_error(&cmd_buffer
->batch
, result
);
205 add_image_view_relocs(struct anv_cmd_buffer
*cmd_buffer
,
206 const struct anv_image_view
*image_view
,
207 const uint32_t plane
,
208 struct anv_surface_state state
)
210 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
211 const struct anv_image
*image
= image_view
->image
;
212 uint32_t image_plane
= image_view
->planes
[plane
].image_plane
;
214 add_surface_state_reloc(cmd_buffer
, state
.state
,
215 image
->planes
[image_plane
].bo
, state
.address
);
217 if (state
.aux_address
) {
219 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
220 &cmd_buffer
->pool
->alloc
,
221 state
.state
.offset
+ isl_dev
->ss
.aux_addr_offset
,
222 image
->planes
[image_plane
].bo
, state
.aux_address
);
223 if (result
!= VK_SUCCESS
)
224 anv_batch_set_error(&cmd_buffer
->batch
, result
);
229 color_is_zero_one(VkClearColorValue value
, enum isl_format format
)
231 if (isl_format_has_int_channel(format
)) {
232 for (unsigned i
= 0; i
< 4; i
++) {
233 if (value
.int32
[i
] != 0 && value
.int32
[i
] != 1)
237 for (unsigned i
= 0; i
< 4; i
++) {
238 if (value
.float32
[i
] != 0.0f
&& value
.float32
[i
] != 1.0f
)
247 color_attachment_compute_aux_usage(struct anv_device
* device
,
248 struct anv_cmd_state
* cmd_state
,
249 uint32_t att
, VkRect2D render_area
,
250 union isl_color_value
*fast_clear_color
)
252 struct anv_attachment_state
*att_state
= &cmd_state
->attachments
[att
];
253 struct anv_image_view
*iview
= cmd_state
->framebuffer
->attachments
[att
];
255 assert(iview
->n_planes
== 1);
257 if (iview
->planes
[0].isl
.base_array_layer
>=
258 anv_image_aux_layers(iview
->image
, VK_IMAGE_ASPECT_COLOR_BIT
,
259 iview
->planes
[0].isl
.base_level
)) {
260 /* There is no aux buffer which corresponds to the level and layer(s)
263 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
264 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
265 att_state
->fast_clear
= false;
267 } else if (iview
->image
->planes
[0].aux_usage
== ISL_AUX_USAGE_MCS
) {
268 att_state
->aux_usage
= ISL_AUX_USAGE_MCS
;
269 att_state
->input_aux_usage
= ISL_AUX_USAGE_MCS
;
270 att_state
->fast_clear
= false;
272 } else if (iview
->image
->planes
[0].aux_usage
== ISL_AUX_USAGE_CCS_E
) {
273 att_state
->aux_usage
= ISL_AUX_USAGE_CCS_E
;
274 att_state
->input_aux_usage
= ISL_AUX_USAGE_CCS_E
;
276 att_state
->aux_usage
= ISL_AUX_USAGE_CCS_D
;
277 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
279 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
280 * setting is only allowed if Surface Format supported for Fast
281 * Clear. In addition, if the surface is bound to the sampling
282 * engine, Surface Format must be supported for Render Target
283 * Compression for surfaces bound to the sampling engine."
285 * In other words, we can only sample from a fast-cleared image if it
286 * also supports color compression.
288 if (isl_format_supports_ccs_e(&device
->info
, iview
->planes
[0].isl
.format
)) {
289 att_state
->input_aux_usage
= ISL_AUX_USAGE_CCS_D
;
291 /* While fast-clear resolves and partial resolves are fairly cheap in the
292 * case where you render to most of the pixels, full resolves are not
293 * because they potentially involve reading and writing the entire
294 * framebuffer. If we can't texture with CCS_E, we should leave it off and
295 * limit ourselves to fast clears.
297 if (cmd_state
->pass
->attachments
[att
].first_subpass_layout
==
298 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
) {
299 anv_perf_warn(device
->instance
, iview
->image
,
300 "Not temporarily enabling CCS_E.");
303 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
307 assert(iview
->image
->planes
[0].aux_surface
.isl
.usage
& ISL_SURF_USAGE_CCS_BIT
);
309 att_state
->clear_color_is_zero_one
=
310 color_is_zero_one(att_state
->clear_value
.color
, iview
->planes
[0].isl
.format
);
311 att_state
->clear_color_is_zero
=
312 att_state
->clear_value
.color
.uint32
[0] == 0 &&
313 att_state
->clear_value
.color
.uint32
[1] == 0 &&
314 att_state
->clear_value
.color
.uint32
[2] == 0 &&
315 att_state
->clear_value
.color
.uint32
[3] == 0;
317 if (att_state
->pending_clear_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
318 /* Start off assuming fast clears are possible */
319 att_state
->fast_clear
= true;
321 /* Potentially, we could do partial fast-clears but doing so has crazy
322 * alignment restrictions. It's easier to just restrict to full size
323 * fast clears for now.
325 if (render_area
.offset
.x
!= 0 ||
326 render_area
.offset
.y
!= 0 ||
327 render_area
.extent
.width
!= iview
->extent
.width
||
328 render_area
.extent
.height
!= iview
->extent
.height
)
329 att_state
->fast_clear
= false;
331 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
332 if (GEN_GEN
<= 8 && !att_state
->clear_color_is_zero_one
)
333 att_state
->fast_clear
= false;
335 /* We allow fast clears when all aux layers of the miplevel are targeted.
336 * See add_fast_clear_state_buffer() for more information. Also, because
337 * we only either do a fast clear or a normal clear and not both, this
338 * complies with the gen7 restriction of not fast-clearing multiple
341 if (cmd_state
->framebuffer
->layers
!=
342 anv_image_aux_layers(iview
->image
, VK_IMAGE_ASPECT_COLOR_BIT
,
343 iview
->planes
[0].isl
.base_level
)) {
344 att_state
->fast_clear
= false;
346 anv_perf_warn(device
->instance
, iview
->image
,
347 "Not fast-clearing the first layer in "
348 "a multi-layer fast clear.");
352 /* We only allow fast clears in the GENERAL layout if the auxiliary
353 * buffer is always enabled and the fast-clear value is all 0's. See
354 * add_fast_clear_state_buffer() for more information.
356 if (cmd_state
->pass
->attachments
[att
].first_subpass_layout
==
357 VK_IMAGE_LAYOUT_GENERAL
&&
358 (!att_state
->clear_color_is_zero
||
359 iview
->image
->planes
[0].aux_usage
== ISL_AUX_USAGE_NONE
)) {
360 att_state
->fast_clear
= false;
363 if (att_state
->fast_clear
) {
364 memcpy(fast_clear_color
->u32
, att_state
->clear_value
.color
.uint32
,
365 sizeof(fast_clear_color
->u32
));
368 att_state
->fast_clear
= false;
373 need_input_attachment_state(const struct anv_render_pass_attachment
*att
)
375 if (!(att
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
378 /* We only allocate input attachment states for color surfaces. Compression
379 * is not yet enabled for depth textures and stencil doesn't allow
380 * compression so we can just use the texture surface state from the view.
382 return vk_format_is_color(att
->format
);
385 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
386 * the initial layout is undefined, the HiZ buffer and depth buffer will
387 * represent the same data at the end of this operation.
390 transition_depth_buffer(struct anv_cmd_buffer
*cmd_buffer
,
391 const struct anv_image
*image
,
392 VkImageLayout initial_layout
,
393 VkImageLayout final_layout
)
397 /* A transition is a no-op if HiZ is not enabled, or if the initial and
398 * final layouts are equal.
400 * The undefined layout indicates that the user doesn't care about the data
401 * that's currently in the buffer. Therefore, a data-preserving resolve
402 * operation is not needed.
404 if (image
->planes
[0].aux_usage
!= ISL_AUX_USAGE_HIZ
|| initial_layout
== final_layout
)
407 const bool hiz_enabled
= ISL_AUX_USAGE_HIZ
==
408 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
,
409 VK_IMAGE_ASPECT_DEPTH_BIT
, initial_layout
);
410 const bool enable_hiz
= ISL_AUX_USAGE_HIZ
==
411 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
,
412 VK_IMAGE_ASPECT_DEPTH_BIT
, final_layout
);
414 enum blorp_hiz_op hiz_op
;
415 if (hiz_enabled
&& !enable_hiz
) {
416 hiz_op
= BLORP_HIZ_OP_DEPTH_RESOLVE
;
417 } else if (!hiz_enabled
&& enable_hiz
) {
418 hiz_op
= BLORP_HIZ_OP_HIZ_RESOLVE
;
420 assert(hiz_enabled
== enable_hiz
);
421 /* If the same buffer will be used, no resolves are necessary. */
422 hiz_op
= BLORP_HIZ_OP_NONE
;
425 if (hiz_op
!= BLORP_HIZ_OP_NONE
)
426 anv_gen8_hiz_op_resolve(cmd_buffer
, image
, hiz_op
);
429 enum fast_clear_state_field
{
430 FAST_CLEAR_STATE_FIELD_CLEAR_COLOR
,
431 FAST_CLEAR_STATE_FIELD_NEEDS_RESOLVE
,
434 static inline struct anv_address
435 get_fast_clear_state_address(const struct anv_device
*device
,
436 const struct anv_image
*image
,
437 VkImageAspectFlagBits aspect
,
439 enum fast_clear_state_field field
)
441 assert(device
&& image
);
442 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
);
443 assert(level
< anv_image_aux_levels(image
, aspect
));
445 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
447 /* Refer to the definition of anv_image for the memory layout. */
448 uint32_t offset
= image
->planes
[plane
].fast_clear_state_offset
;
450 offset
+= anv_fast_clear_state_entry_size(device
) * level
;
453 case FAST_CLEAR_STATE_FIELD_NEEDS_RESOLVE
:
454 offset
+= device
->isl_dev
.ss
.clear_value_size
;
456 case FAST_CLEAR_STATE_FIELD_CLEAR_COLOR
:
460 assert(offset
< image
->planes
[plane
].surface
.offset
+ image
->planes
[plane
].size
);
462 return (struct anv_address
) {
463 .bo
= image
->planes
[plane
].bo
,
468 #define MI_PREDICATE_SRC0 0x2400
469 #define MI_PREDICATE_SRC1 0x2408
471 /* Manages the state of an color image subresource to ensure resolves are
472 * performed properly.
475 genX(set_image_needs_resolve
)(struct anv_cmd_buffer
*cmd_buffer
,
476 const struct anv_image
*image
,
477 VkImageAspectFlagBits aspect
,
478 unsigned level
, bool needs_resolve
)
480 assert(cmd_buffer
&& image
);
481 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
);
482 assert(level
< anv_image_aux_levels(image
, aspect
));
484 const struct anv_address resolve_flag_addr
=
485 get_fast_clear_state_address(cmd_buffer
->device
, image
, aspect
, level
,
486 FAST_CLEAR_STATE_FIELD_NEEDS_RESOLVE
);
488 /* The HW docs say that there is no way to guarantee the completion of
489 * the following command. We use it nevertheless because it shows no
490 * issues in testing is currently being used in the GL driver.
492 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
493 sdi
.Address
= resolve_flag_addr
;
494 sdi
.ImmediateData
= needs_resolve
;
499 genX(load_needs_resolve_predicate
)(struct anv_cmd_buffer
*cmd_buffer
,
500 const struct anv_image
*image
,
501 VkImageAspectFlagBits aspect
,
504 assert(cmd_buffer
&& image
);
505 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
);
506 assert(level
< anv_image_aux_levels(image
, aspect
));
508 const struct anv_address resolve_flag_addr
=
509 get_fast_clear_state_address(cmd_buffer
->device
, image
, aspect
, level
,
510 FAST_CLEAR_STATE_FIELD_NEEDS_RESOLVE
);
512 /* Make the pending predicated resolve a no-op if one is not needed.
513 * predicate = do_resolve = resolve_flag != 0;
515 emit_lri(&cmd_buffer
->batch
, MI_PREDICATE_SRC1
, 0);
516 emit_lri(&cmd_buffer
->batch
, MI_PREDICATE_SRC1
+ 4, 0);
517 emit_lri(&cmd_buffer
->batch
, MI_PREDICATE_SRC0
, 0);
518 emit_lrm(&cmd_buffer
->batch
, MI_PREDICATE_SRC0
+ 4,
519 resolve_flag_addr
.bo
, resolve_flag_addr
.offset
);
520 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_PREDICATE
), mip
) {
521 mip
.LoadOperation
= LOAD_LOADINV
;
522 mip
.CombineOperation
= COMBINE_SET
;
523 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
528 init_fast_clear_state_entry(struct anv_cmd_buffer
*cmd_buffer
,
529 const struct anv_image
*image
,
530 VkImageAspectFlagBits aspect
,
533 assert(cmd_buffer
&& image
);
534 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
);
535 assert(level
< anv_image_aux_levels(image
, aspect
));
537 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
538 enum isl_aux_usage aux_usage
= image
->planes
[plane
].aux_usage
;
540 /* The resolve flag should updated to signify that fast-clear/compression
541 * data needs to be removed when leaving the undefined layout. Such data
542 * may need to be removed if it would cause accesses to the color buffer
543 * to return incorrect data. The fast clear data in CCS_D buffers should
544 * be removed because CCS_D isn't enabled all the time.
546 genX(set_image_needs_resolve
)(cmd_buffer
, image
, aspect
, level
,
547 aux_usage
== ISL_AUX_USAGE_NONE
);
549 /* The fast clear value dword(s) will be copied into a surface state object.
550 * Ensure that the restrictions of the fields in the dword(s) are followed.
552 * CCS buffers on SKL+ can have any value set for the clear colors.
554 if (image
->samples
== 1 && GEN_GEN
>= 9)
557 /* Other combinations of auxiliary buffers and platforms require specific
558 * values in the clear value dword(s).
561 for (; i
< cmd_buffer
->device
->isl_dev
.ss
.clear_value_size
; i
+= 4) {
562 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_DATA_IMM
), sdi
) {
564 get_fast_clear_state_address(cmd_buffer
->device
, image
, aspect
, level
,
565 FAST_CLEAR_STATE_FIELD_CLEAR_COLOR
);
568 /* MCS buffers on SKL+ can only have 1/0 clear colors. */
569 assert(aux_usage
== ISL_AUX_USAGE_MCS
);
570 sdi
.ImmediateData
= 0;
571 } else if (GEN_VERSIONx10
>= 75) {
572 /* Pre-SKL, the dword containing the clear values also contains
573 * other fields, so we need to initialize those fields to match the
574 * values that would be in a color attachment.
577 sdi
.ImmediateData
= ISL_CHANNEL_SELECT_RED
<< 25 |
578 ISL_CHANNEL_SELECT_GREEN
<< 22 |
579 ISL_CHANNEL_SELECT_BLUE
<< 19 |
580 ISL_CHANNEL_SELECT_ALPHA
<< 16;
581 } else if (GEN_VERSIONx10
== 70) {
582 /* On IVB, the dword containing the clear values also contains
583 * other fields that must be zero or can be zero.
586 sdi
.ImmediateData
= 0;
592 /* Copy the fast-clear value dword(s) between a surface state object and an
593 * image's fast clear state buffer.
596 genX(copy_fast_clear_dwords
)(struct anv_cmd_buffer
*cmd_buffer
,
597 struct anv_state surface_state
,
598 const struct anv_image
*image
,
599 VkImageAspectFlagBits aspect
,
601 bool copy_from_surface_state
)
603 assert(cmd_buffer
&& image
);
604 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
);
605 assert(level
< anv_image_aux_levels(image
, aspect
));
607 struct anv_bo
*ss_bo
=
608 &cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
;
609 uint32_t ss_clear_offset
= surface_state
.offset
+
610 cmd_buffer
->device
->isl_dev
.ss
.clear_value_offset
;
611 const struct anv_address entry_addr
=
612 get_fast_clear_state_address(cmd_buffer
->device
, image
, aspect
, level
,
613 FAST_CLEAR_STATE_FIELD_CLEAR_COLOR
);
614 unsigned copy_size
= cmd_buffer
->device
->isl_dev
.ss
.clear_value_size
;
616 if (copy_from_surface_state
) {
617 genX(cmd_buffer_mi_memcpy
)(cmd_buffer
, entry_addr
.bo
, entry_addr
.offset
,
618 ss_bo
, ss_clear_offset
, copy_size
);
620 genX(cmd_buffer_mi_memcpy
)(cmd_buffer
, ss_bo
, ss_clear_offset
,
621 entry_addr
.bo
, entry_addr
.offset
, copy_size
);
623 /* Updating a surface state object may require that the state cache be
624 * invalidated. From the SKL PRM, Shared Functions -> State -> State
627 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
628 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
629 * modified [...], the L1 state cache must be invalidated to ensure
630 * the new surface or sampler state is fetched from system memory.
632 * In testing, SKL doesn't actually seem to need this, but HSW does.
634 cmd_buffer
->state
.pending_pipe_bits
|=
635 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
640 * @brief Transitions a color buffer from one layout to another.
642 * See section 6.1.1. Image Layout Transitions of the Vulkan 1.0.50 spec for
645 * @param level_count VK_REMAINING_MIP_LEVELS isn't supported.
646 * @param layer_count VK_REMAINING_ARRAY_LAYERS isn't supported. For 3D images,
647 * this represents the maximum layers to transition at each
648 * specified miplevel.
651 transition_color_buffer(struct anv_cmd_buffer
*cmd_buffer
,
652 const struct anv_image
*image
,
653 VkImageAspectFlagBits aspect
,
654 const uint32_t base_level
, uint32_t level_count
,
655 uint32_t base_layer
, uint32_t layer_count
,
656 VkImageLayout initial_layout
,
657 VkImageLayout final_layout
)
659 /* Validate the inputs. */
661 assert(image
&& image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
);
662 /* These values aren't supported for simplicity's sake. */
663 assert(level_count
!= VK_REMAINING_MIP_LEVELS
&&
664 layer_count
!= VK_REMAINING_ARRAY_LAYERS
);
665 /* Ensure the subresource range is valid. */
666 uint64_t last_level_num
= base_level
+ level_count
;
667 const uint32_t max_depth
= anv_minify(image
->extent
.depth
, base_level
);
668 UNUSED
const uint32_t image_layers
= MAX2(image
->array_size
, max_depth
);
669 assert((uint64_t)base_layer
+ layer_count
<= image_layers
);
670 assert(last_level_num
<= image
->levels
);
671 /* The spec disallows these final layouts. */
672 assert(final_layout
!= VK_IMAGE_LAYOUT_UNDEFINED
&&
673 final_layout
!= VK_IMAGE_LAYOUT_PREINITIALIZED
);
675 /* No work is necessary if the layout stays the same or if this subresource
676 * range lacks auxiliary data.
678 if (initial_layout
== final_layout
)
681 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
683 if (image
->planes
[plane
].shadow_surface
.isl
.size
> 0 &&
684 final_layout
== VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
) {
685 /* This surface is a linear compressed image with a tiled shadow surface
686 * for texturing. The client is about to use it in READ_ONLY_OPTIMAL so
687 * we need to ensure the shadow copy is up-to-date.
689 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
690 assert(image
->planes
[plane
].surface
.isl
.tiling
== ISL_TILING_LINEAR
);
691 assert(image
->planes
[plane
].shadow_surface
.isl
.tiling
!= ISL_TILING_LINEAR
);
692 assert(isl_format_is_compressed(image
->planes
[plane
].surface
.isl
.format
));
694 anv_image_copy_to_shadow(cmd_buffer
, image
,
695 base_level
, level_count
,
696 base_layer
, layer_count
);
699 if (base_layer
>= anv_image_aux_layers(image
, aspect
, base_level
))
702 /* A transition of a 3D subresource works on all slices at a time. */
703 if (image
->type
== VK_IMAGE_TYPE_3D
) {
705 layer_count
= anv_minify(image
->extent
.depth
, base_level
);
708 /* We're interested in the subresource range subset that has aux data. */
709 level_count
= MIN2(level_count
, anv_image_aux_levels(image
, aspect
) - base_level
);
710 layer_count
= MIN2(layer_count
,
711 anv_image_aux_layers(image
, aspect
, base_level
) - base_layer
);
712 last_level_num
= base_level
+ level_count
;
714 /* Record whether or not the layout is undefined. Pre-initialized images
715 * with auxiliary buffers have a non-linear layout and are thus undefined.
717 assert(image
->tiling
== VK_IMAGE_TILING_OPTIMAL
);
718 const bool undef_layout
= initial_layout
== VK_IMAGE_LAYOUT_UNDEFINED
||
719 initial_layout
== VK_IMAGE_LAYOUT_PREINITIALIZED
;
721 /* Do preparatory work before the resolve operation or return early if no
722 * resolve is actually needed.
725 /* A subresource in the undefined layout may have been aliased and
726 * populated with any arrangement of bits. Therefore, we must initialize
727 * the related aux buffer and clear buffer entry with desirable values.
729 * Initialize the relevant clear buffer entries.
731 for (unsigned level
= base_level
; level
< last_level_num
; level
++)
732 init_fast_clear_state_entry(cmd_buffer
, image
, aspect
, level
);
734 /* Initialize the aux buffers to enable correct rendering. This operation
735 * requires up to two steps: one to rid the aux buffer of data that may
736 * cause GPU hangs, and another to ensure that writes done without aux
737 * will be visible to reads done with aux.
739 * Having an aux buffer with invalid data is possible for CCS buffers
740 * SKL+ and for MCS buffers with certain sample counts (2x and 8x). One
741 * easy way to get to a valid state is to fast-clear the specified range.
743 * Even for MCS buffers that have sample counts that don't require
744 * certain bits to be reserved (4x and 8x), we're unsure if the hardware
745 * will be okay with the sample mappings given by the undefined buffer.
746 * We don't have any data to show that this is a problem, but we want to
747 * avoid causing difficult-to-debug problems.
749 if ((GEN_GEN
>= 9 && image
->samples
== 1) || image
->samples
> 1) {
750 if (image
->samples
== 4 || image
->samples
== 16) {
751 anv_perf_warn(cmd_buffer
->device
->instance
, image
,
752 "Doing a potentially unnecessary fast-clear to "
753 "define an MCS buffer.");
756 anv_image_fast_clear(cmd_buffer
, image
, aspect
,
757 base_level
, level_count
,
758 base_layer
, layer_count
);
760 /* At this point, some elements of the CCS buffer may have the fast-clear
761 * bit-arrangement. As the user writes to a subresource, we need to have
762 * the associated CCS elements enter the ambiguated state. This enables
763 * reads (implicit or explicit) to reflect the user-written data instead
764 * of the clear color. The only time such elements will not change their
765 * state as described above, is in a final layout that doesn't have CCS
766 * enabled. In this case, we must force the associated CCS buffers of the
767 * specified range to enter the ambiguated state in advance.
769 if (image
->samples
== 1 &&
770 image
->planes
[plane
].aux_usage
!= ISL_AUX_USAGE_CCS_E
&&
771 final_layout
!= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
) {
772 /* The CCS_D buffer may not be enabled in the final layout. Continue
773 * executing this function to perform a resolve.
775 anv_perf_warn(cmd_buffer
->device
->instance
, image
,
776 "Performing an additional resolve for CCS_D layout "
777 "transition. Consider always leaving it on or "
778 "performing an ambiguation pass.");
780 /* Writes in the final layout will be aware of the auxiliary buffer.
781 * In addition, the clear buffer entries and the auxiliary buffers
782 * have been populated with values that will result in correct
787 } else if (initial_layout
!= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
) {
788 /* Resolves are only necessary if the subresource may contain blocks
789 * fast-cleared to values unsupported in other layouts. This only occurs
790 * if the initial layout is COLOR_ATTACHMENT_OPTIMAL.
793 } else if (image
->samples
> 1) {
794 /* MCS buffers don't need resolving. */
798 /* Perform a resolve to synchronize data between the main and aux buffer.
799 * Before we begin, we must satisfy the cache flushing requirement specified
800 * in the Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)":
802 * Any transition from any value in {Clear, Render, Resolve} to a
803 * different value in {Clear, Render, Resolve} requires end of pipe
806 * We perform a flush of the write cache before and after the clear and
807 * resolve operations to meet this requirement.
809 * Unlike other drawing, fast clear operations are not properly
810 * synchronized. The first PIPE_CONTROL here likely ensures that the
811 * contents of the previous render or clear hit the render target before we
812 * resolve and the second likely ensures that the resolve is complete before
813 * we do any more rendering or clearing.
815 cmd_buffer
->state
.pending_pipe_bits
|=
816 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
818 for (uint32_t level
= base_level
; level
< last_level_num
; level
++) {
820 /* The number of layers changes at each 3D miplevel. */
821 if (image
->type
== VK_IMAGE_TYPE_3D
) {
822 layer_count
= MIN2(layer_count
, anv_image_aux_layers(image
, aspect
, level
));
825 genX(load_needs_resolve_predicate
)(cmd_buffer
, image
, aspect
, level
);
827 enum isl_aux_usage aux_usage
=
828 image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_NONE
?
829 ISL_AUX_USAGE_CCS_D
: image
->planes
[plane
].aux_usage
;
831 /* Create a surface state with the right clear color and perform the
834 struct anv_surface_state surface_state
;
835 surface_state
.state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
836 anv_image_fill_surface_state(cmd_buffer
->device
,
837 image
, VK_IMAGE_ASPECT_COLOR_BIT
,
839 .format
= image
->planes
[plane
].surface
.isl
.format
,
840 .swizzle
= ISL_SWIZZLE_IDENTITY
,
843 .base_array_layer
= base_layer
,
844 .array_len
= layer_count
,
846 ISL_SURF_USAGE_RENDER_TARGET_BIT
,
848 &surface_state
, NULL
);
849 add_image_relocs(cmd_buffer
, image
, 0, surface_state
);
850 genX(copy_fast_clear_dwords
)(cmd_buffer
, surface_state
.state
, image
,
851 aspect
, level
, false /* copy to ss */);
852 anv_ccs_resolve(cmd_buffer
, surface_state
.state
, image
,
853 aspect
, level
, layer_count
,
854 image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_CCS_E
?
855 BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL
:
856 BLORP_FAST_CLEAR_OP_RESOLVE_FULL
);
858 genX(set_image_needs_resolve
)(cmd_buffer
, image
, aspect
, level
, false);
861 cmd_buffer
->state
.pending_pipe_bits
|=
862 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
| ANV_PIPE_CS_STALL_BIT
;
866 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
869 genX(cmd_buffer_setup_attachments
)(struct anv_cmd_buffer
*cmd_buffer
,
870 struct anv_render_pass
*pass
,
871 const VkRenderPassBeginInfo
*begin
)
873 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
874 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
876 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
878 if (pass
->attachment_count
> 0) {
879 state
->attachments
= vk_alloc(&cmd_buffer
->pool
->alloc
,
880 pass
->attachment_count
*
881 sizeof(state
->attachments
[0]),
882 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
883 if (state
->attachments
== NULL
) {
884 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
885 return anv_batch_set_error(&cmd_buffer
->batch
,
886 VK_ERROR_OUT_OF_HOST_MEMORY
);
889 state
->attachments
= NULL
;
892 /* Reserve one for the NULL state. */
893 unsigned num_states
= 1;
894 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
895 if (vk_format_is_color(pass
->attachments
[i
].format
))
898 if (need_input_attachment_state(&pass
->attachments
[i
]))
902 const uint32_t ss_stride
= align_u32(isl_dev
->ss
.size
, isl_dev
->ss
.align
);
903 state
->render_pass_states
=
904 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
905 num_states
* ss_stride
, isl_dev
->ss
.align
);
907 struct anv_state next_state
= state
->render_pass_states
;
908 next_state
.alloc_size
= isl_dev
->ss
.size
;
910 state
->null_surface_state
= next_state
;
911 next_state
.offset
+= ss_stride
;
912 next_state
.map
+= ss_stride
;
914 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
915 if (vk_format_is_color(pass
->attachments
[i
].format
)) {
916 state
->attachments
[i
].color
.state
= next_state
;
917 next_state
.offset
+= ss_stride
;
918 next_state
.map
+= ss_stride
;
921 if (need_input_attachment_state(&pass
->attachments
[i
])) {
922 state
->attachments
[i
].input
.state
= next_state
;
923 next_state
.offset
+= ss_stride
;
924 next_state
.map
+= ss_stride
;
927 assert(next_state
.offset
== state
->render_pass_states
.offset
+
928 state
->render_pass_states
.alloc_size
);
931 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, begin
->framebuffer
);
932 assert(pass
->attachment_count
== framebuffer
->attachment_count
);
934 isl_null_fill_state(isl_dev
, state
->null_surface_state
.map
,
935 isl_extent3d(framebuffer
->width
,
937 framebuffer
->layers
));
939 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
940 struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
941 VkImageAspectFlags att_aspects
= vk_format_aspects(att
->format
);
942 VkImageAspectFlags clear_aspects
= 0;
944 if (att_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) {
945 /* color attachment */
946 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
947 clear_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
950 /* depthstencil attachment */
951 if ((att_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
952 att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
953 clear_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
955 if ((att_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
956 att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
957 clear_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
961 state
->attachments
[i
].current_layout
= att
->initial_layout
;
962 state
->attachments
[i
].pending_clear_aspects
= clear_aspects
;
964 state
->attachments
[i
].clear_value
= begin
->pClearValues
[i
];
966 struct anv_image_view
*iview
= framebuffer
->attachments
[i
];
967 anv_assert(iview
->vk_format
== att
->format
);
968 anv_assert(iview
->n_planes
== 1);
970 union isl_color_value clear_color
= { .u32
= { 0, } };
971 if (att_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) {
972 assert(att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
973 color_attachment_compute_aux_usage(cmd_buffer
->device
,
974 state
, i
, begin
->renderArea
,
977 anv_image_fill_surface_state(cmd_buffer
->device
,
979 VK_IMAGE_ASPECT_COLOR_BIT
,
980 &iview
->planes
[0].isl
,
981 ISL_SURF_USAGE_RENDER_TARGET_BIT
,
982 state
->attachments
[i
].aux_usage
,
985 &state
->attachments
[i
].color
,
988 add_image_view_relocs(cmd_buffer
, iview
, 0,
989 state
->attachments
[i
].color
);
991 /* This field will be initialized after the first subpass
994 state
->attachments
[i
].aux_usage
= ISL_AUX_USAGE_NONE
;
996 state
->attachments
[i
].input_aux_usage
= ISL_AUX_USAGE_NONE
;
999 if (need_input_attachment_state(&pass
->attachments
[i
])) {
1000 anv_image_fill_surface_state(cmd_buffer
->device
,
1002 VK_IMAGE_ASPECT_COLOR_BIT
,
1003 &iview
->planes
[0].isl
,
1004 ISL_SURF_USAGE_TEXTURE_BIT
,
1005 state
->attachments
[i
].input_aux_usage
,
1008 &state
->attachments
[i
].input
,
1011 add_image_view_relocs(cmd_buffer
, iview
, 0,
1012 state
->attachments
[i
].input
);
1021 genX(BeginCommandBuffer
)(
1022 VkCommandBuffer commandBuffer
,
1023 const VkCommandBufferBeginInfo
* pBeginInfo
)
1025 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1027 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
1028 * command buffer's state. Otherwise, we must *reset* its state. In both
1029 * cases we reset it.
1031 * From the Vulkan 1.0 spec:
1033 * If a command buffer is in the executable state and the command buffer
1034 * was allocated from a command pool with the
1035 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
1036 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
1037 * as if vkResetCommandBuffer had been called with
1038 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
1039 * the command buffer in the recording state.
1041 anv_cmd_buffer_reset(cmd_buffer
);
1043 cmd_buffer
->usage_flags
= pBeginInfo
->flags
;
1045 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
||
1046 !(cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
));
1048 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1050 /* We sometimes store vertex data in the dynamic state buffer for blorp
1051 * operations and our dynamic state stream may re-use data from previous
1052 * command buffers. In order to prevent stale cache data, we flush the VF
1053 * cache. We could do this on every blorp call but that's not really
1054 * needed as all of the data will get written by the CPU prior to the GPU
1055 * executing anything. The chances are fairly high that they will use
1056 * blorp at least once per primary command buffer so it shouldn't be
1059 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
)
1060 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
1062 VkResult result
= VK_SUCCESS
;
1063 if (cmd_buffer
->usage_flags
&
1064 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
1065 assert(pBeginInfo
->pInheritanceInfo
);
1066 cmd_buffer
->state
.pass
=
1067 anv_render_pass_from_handle(pBeginInfo
->pInheritanceInfo
->renderPass
);
1068 cmd_buffer
->state
.subpass
=
1069 &cmd_buffer
->state
.pass
->subpasses
[pBeginInfo
->pInheritanceInfo
->subpass
];
1070 cmd_buffer
->state
.framebuffer
= NULL
;
1072 result
= genX(cmd_buffer_setup_attachments
)(cmd_buffer
,
1073 cmd_buffer
->state
.pass
, NULL
);
1075 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
1082 genX(EndCommandBuffer
)(
1083 VkCommandBuffer commandBuffer
)
1085 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1087 if (anv_batch_has_error(&cmd_buffer
->batch
))
1088 return cmd_buffer
->batch
.status
;
1090 /* We want every command buffer to start with the PMA fix in a known state,
1091 * so we disable it at the end of the command buffer.
1093 genX(cmd_buffer_enable_pma_fix
)(cmd_buffer
, false);
1095 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1097 anv_cmd_buffer_end_batch_buffer(cmd_buffer
);
1103 genX(CmdExecuteCommands
)(
1104 VkCommandBuffer commandBuffer
,
1105 uint32_t commandBufferCount
,
1106 const VkCommandBuffer
* pCmdBuffers
)
1108 ANV_FROM_HANDLE(anv_cmd_buffer
, primary
, commandBuffer
);
1110 assert(primary
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1112 if (anv_batch_has_error(&primary
->batch
))
1115 /* The secondary command buffers will assume that the PMA fix is disabled
1116 * when they begin executing. Make sure this is true.
1118 genX(cmd_buffer_enable_pma_fix
)(primary
, false);
1120 /* The secondary command buffer doesn't know which textures etc. have been
1121 * flushed prior to their execution. Apply those flushes now.
1123 genX(cmd_buffer_apply_pipe_flushes
)(primary
);
1125 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
1126 ANV_FROM_HANDLE(anv_cmd_buffer
, secondary
, pCmdBuffers
[i
]);
1128 assert(secondary
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
1129 assert(!anv_batch_has_error(&secondary
->batch
));
1131 if (secondary
->usage_flags
&
1132 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
1133 /* If we're continuing a render pass from the primary, we need to
1134 * copy the surface states for the current subpass into the storage
1135 * we allocated for them in BeginCommandBuffer.
1137 struct anv_bo
*ss_bo
=
1138 &primary
->device
->surface_state_pool
.block_pool
.bo
;
1139 struct anv_state src_state
= primary
->state
.render_pass_states
;
1140 struct anv_state dst_state
= secondary
->state
.render_pass_states
;
1141 assert(src_state
.alloc_size
== dst_state
.alloc_size
);
1143 genX(cmd_buffer_so_memcpy
)(primary
, ss_bo
, dst_state
.offset
,
1144 ss_bo
, src_state
.offset
,
1145 src_state
.alloc_size
);
1148 anv_cmd_buffer_add_secondary(primary
, secondary
);
1151 /* Each of the secondary command buffers will use its own state base
1152 * address. We need to re-emit state base address for the primary after
1153 * all of the secondaries are done.
1155 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
1158 genX(cmd_buffer_emit_state_base_address
)(primary
);
1161 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
1162 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
1163 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
1166 * Program the hardware to use the specified L3 configuration.
1169 genX(cmd_buffer_config_l3
)(struct anv_cmd_buffer
*cmd_buffer
,
1170 const struct gen_l3_config
*cfg
)
1173 if (cfg
== cmd_buffer
->state
.current_l3_config
)
1176 if (unlikely(INTEL_DEBUG
& DEBUG_L3
)) {
1177 fprintf(stderr
, "L3 config transition: ");
1178 gen_dump_l3_config(cfg
, stderr
);
1181 const bool has_slm
= cfg
->n
[GEN_L3P_SLM
];
1183 /* According to the hardware docs, the L3 partitioning can only be changed
1184 * while the pipeline is completely drained and the caches are flushed,
1185 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
1187 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1188 pc
.DCFlushEnable
= true;
1189 pc
.PostSyncOperation
= NoWrite
;
1190 pc
.CommandStreamerStallEnable
= true;
1193 /* ...followed by a second pipelined PIPE_CONTROL that initiates
1194 * invalidation of the relevant caches. Note that because RO invalidation
1195 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
1196 * command is processed by the CS) we cannot combine it with the previous
1197 * stalling flush as the hardware documentation suggests, because that
1198 * would cause the CS to stall on previous rendering *after* RO
1199 * invalidation and wouldn't prevent the RO caches from being polluted by
1200 * concurrent rendering before the stall completes. This intentionally
1201 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
1202 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
1203 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
1204 * already guarantee that there is no concurrent GPGPU kernel execution
1205 * (see SKL HSD 2132585).
1207 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1208 pc
.TextureCacheInvalidationEnable
= true;
1209 pc
.ConstantCacheInvalidationEnable
= true;
1210 pc
.InstructionCacheInvalidateEnable
= true;
1211 pc
.StateCacheInvalidationEnable
= true;
1212 pc
.PostSyncOperation
= NoWrite
;
1215 /* Now send a third stalling flush to make sure that invalidation is
1216 * complete when the L3 configuration registers are modified.
1218 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1219 pc
.DCFlushEnable
= true;
1220 pc
.PostSyncOperation
= NoWrite
;
1221 pc
.CommandStreamerStallEnable
= true;
1226 assert(!cfg
->n
[GEN_L3P_IS
] && !cfg
->n
[GEN_L3P_C
] && !cfg
->n
[GEN_L3P_T
]);
1229 anv_pack_struct(&l3cr
, GENX(L3CNTLREG
),
1230 .SLMEnable
= has_slm
,
1231 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
1232 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
1233 .DCAllocation
= cfg
->n
[GEN_L3P_DC
],
1234 .AllAllocation
= cfg
->n
[GEN_L3P_ALL
]);
1236 /* Set up the L3 partitioning. */
1237 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG_num
), l3cr
);
1241 const bool has_dc
= cfg
->n
[GEN_L3P_DC
] || cfg
->n
[GEN_L3P_ALL
];
1242 const bool has_is
= cfg
->n
[GEN_L3P_IS
] || cfg
->n
[GEN_L3P_RO
] ||
1243 cfg
->n
[GEN_L3P_ALL
];
1244 const bool has_c
= cfg
->n
[GEN_L3P_C
] || cfg
->n
[GEN_L3P_RO
] ||
1245 cfg
->n
[GEN_L3P_ALL
];
1246 const bool has_t
= cfg
->n
[GEN_L3P_T
] || cfg
->n
[GEN_L3P_RO
] ||
1247 cfg
->n
[GEN_L3P_ALL
];
1249 assert(!cfg
->n
[GEN_L3P_ALL
]);
1251 /* When enabled SLM only uses a portion of the L3 on half of the banks,
1252 * the matching space on the remaining banks has to be allocated to a
1253 * client (URB for all validated configurations) set to the
1254 * lower-bandwidth 2-bank address hashing mode.
1256 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
1257 const bool urb_low_bw
= has_slm
&& !devinfo
->is_baytrail
;
1258 assert(!urb_low_bw
|| cfg
->n
[GEN_L3P_URB
] == cfg
->n
[GEN_L3P_SLM
]);
1260 /* Minimum number of ways that can be allocated to the URB. */
1261 MAYBE_UNUSED
const unsigned n0_urb
= devinfo
->is_baytrail
? 32 : 0;
1262 assert(cfg
->n
[GEN_L3P_URB
] >= n0_urb
);
1264 uint32_t l3sqcr1
, l3cr2
, l3cr3
;
1265 anv_pack_struct(&l3sqcr1
, GENX(L3SQCREG1
),
1266 .ConvertDC_UC
= !has_dc
,
1267 .ConvertIS_UC
= !has_is
,
1268 .ConvertC_UC
= !has_c
,
1269 .ConvertT_UC
= !has_t
);
1271 GEN_IS_HASWELL
? HSW_L3SQCREG1_SQGHPCI_DEFAULT
:
1272 devinfo
->is_baytrail
? VLV_L3SQCREG1_SQGHPCI_DEFAULT
:
1273 IVB_L3SQCREG1_SQGHPCI_DEFAULT
;
1275 anv_pack_struct(&l3cr2
, GENX(L3CNTLREG2
),
1276 .SLMEnable
= has_slm
,
1277 .URBLowBandwidth
= urb_low_bw
,
1278 .URBAllocation
= cfg
->n
[GEN_L3P_URB
] - n0_urb
,
1280 .ALLAllocation
= cfg
->n
[GEN_L3P_ALL
],
1282 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
1283 .DCAllocation
= cfg
->n
[GEN_L3P_DC
]);
1285 anv_pack_struct(&l3cr3
, GENX(L3CNTLREG3
),
1286 .ISAllocation
= cfg
->n
[GEN_L3P_IS
],
1287 .ISLowBandwidth
= 0,
1288 .CAllocation
= cfg
->n
[GEN_L3P_C
],
1290 .TAllocation
= cfg
->n
[GEN_L3P_T
],
1291 .TLowBandwidth
= 0);
1293 /* Set up the L3 partitioning. */
1294 emit_lri(&cmd_buffer
->batch
, GENX(L3SQCREG1_num
), l3sqcr1
);
1295 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG2_num
), l3cr2
);
1296 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG3_num
), l3cr3
);
1299 if (cmd_buffer
->device
->instance
->physicalDevice
.cmd_parser_version
>= 4) {
1300 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
1301 * them disabled to avoid crashing the system hard.
1303 uint32_t scratch1
, chicken3
;
1304 anv_pack_struct(&scratch1
, GENX(SCRATCH1
),
1305 .L3AtomicDisable
= !has_dc
);
1306 anv_pack_struct(&chicken3
, GENX(CHICKEN3
),
1307 .L3AtomicDisableMask
= true,
1308 .L3AtomicDisable
= !has_dc
);
1309 emit_lri(&cmd_buffer
->batch
, GENX(SCRATCH1_num
), scratch1
);
1310 emit_lri(&cmd_buffer
->batch
, GENX(CHICKEN3_num
), chicken3
);
1316 cmd_buffer
->state
.current_l3_config
= cfg
;
1320 genX(cmd_buffer_apply_pipe_flushes
)(struct anv_cmd_buffer
*cmd_buffer
)
1322 enum anv_pipe_bits bits
= cmd_buffer
->state
.pending_pipe_bits
;
1324 /* Flushes are pipelined while invalidations are handled immediately.
1325 * Therefore, if we're flushing anything then we need to schedule a stall
1326 * before any invalidations can happen.
1328 if (bits
& ANV_PIPE_FLUSH_BITS
)
1329 bits
|= ANV_PIPE_NEEDS_CS_STALL_BIT
;
1331 /* If we're going to do an invalidate and we have a pending CS stall that
1332 * has yet to be resolved, we do the CS stall now.
1334 if ((bits
& ANV_PIPE_INVALIDATE_BITS
) &&
1335 (bits
& ANV_PIPE_NEEDS_CS_STALL_BIT
)) {
1336 bits
|= ANV_PIPE_CS_STALL_BIT
;
1337 bits
&= ~ANV_PIPE_NEEDS_CS_STALL_BIT
;
1340 if (bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
)) {
1341 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
1342 pipe
.DepthCacheFlushEnable
= bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
1343 pipe
.DCFlushEnable
= bits
& ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
1344 pipe
.RenderTargetCacheFlushEnable
=
1345 bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
1347 pipe
.DepthStallEnable
= bits
& ANV_PIPE_DEPTH_STALL_BIT
;
1348 pipe
.CommandStreamerStallEnable
= bits
& ANV_PIPE_CS_STALL_BIT
;
1349 pipe
.StallAtPixelScoreboard
= bits
& ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
1352 * According to the Broadwell documentation, any PIPE_CONTROL with the
1353 * "Command Streamer Stall" bit set must also have another bit set,
1354 * with five different options:
1356 * - Render Target Cache Flush
1357 * - Depth Cache Flush
1358 * - Stall at Pixel Scoreboard
1359 * - Post-Sync Operation
1363 * I chose "Stall at Pixel Scoreboard" since that's what we use in
1364 * mesa and it seems to work fine. The choice is fairly arbitrary.
1366 if ((bits
& ANV_PIPE_CS_STALL_BIT
) &&
1367 !(bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_DEPTH_STALL_BIT
|
1368 ANV_PIPE_STALL_AT_SCOREBOARD_BIT
)))
1369 pipe
.StallAtPixelScoreboard
= true;
1372 bits
&= ~(ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
);
1375 if (bits
& ANV_PIPE_INVALIDATE_BITS
) {
1376 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
1377 pipe
.StateCacheInvalidationEnable
=
1378 bits
& ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
1379 pipe
.ConstantCacheInvalidationEnable
=
1380 bits
& ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
1381 pipe
.VFCacheInvalidationEnable
=
1382 bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
1383 pipe
.TextureCacheInvalidationEnable
=
1384 bits
& ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
1385 pipe
.InstructionCacheInvalidateEnable
=
1386 bits
& ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT
;
1389 bits
&= ~ANV_PIPE_INVALIDATE_BITS
;
1392 cmd_buffer
->state
.pending_pipe_bits
= bits
;
1395 void genX(CmdPipelineBarrier
)(
1396 VkCommandBuffer commandBuffer
,
1397 VkPipelineStageFlags srcStageMask
,
1398 VkPipelineStageFlags destStageMask
,
1400 uint32_t memoryBarrierCount
,
1401 const VkMemoryBarrier
* pMemoryBarriers
,
1402 uint32_t bufferMemoryBarrierCount
,
1403 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
1404 uint32_t imageMemoryBarrierCount
,
1405 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
1407 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1409 /* XXX: Right now, we're really dumb and just flush whatever categories
1410 * the app asks for. One of these days we may make this a bit better
1411 * but right now that's all the hardware allows for in most areas.
1413 VkAccessFlags src_flags
= 0;
1414 VkAccessFlags dst_flags
= 0;
1416 for (uint32_t i
= 0; i
< memoryBarrierCount
; i
++) {
1417 src_flags
|= pMemoryBarriers
[i
].srcAccessMask
;
1418 dst_flags
|= pMemoryBarriers
[i
].dstAccessMask
;
1421 for (uint32_t i
= 0; i
< bufferMemoryBarrierCount
; i
++) {
1422 src_flags
|= pBufferMemoryBarriers
[i
].srcAccessMask
;
1423 dst_flags
|= pBufferMemoryBarriers
[i
].dstAccessMask
;
1426 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
1427 src_flags
|= pImageMemoryBarriers
[i
].srcAccessMask
;
1428 dst_flags
|= pImageMemoryBarriers
[i
].dstAccessMask
;
1429 ANV_FROM_HANDLE(anv_image
, image
, pImageMemoryBarriers
[i
].image
);
1430 const VkImageSubresourceRange
*range
=
1431 &pImageMemoryBarriers
[i
].subresourceRange
;
1433 if (range
->aspectMask
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
1434 transition_depth_buffer(cmd_buffer
, image
,
1435 pImageMemoryBarriers
[i
].oldLayout
,
1436 pImageMemoryBarriers
[i
].newLayout
);
1437 } else if (range
->aspectMask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) {
1438 VkImageAspectFlags color_aspects
=
1439 anv_image_expand_aspects(image
, range
->aspectMask
);
1440 uint32_t aspect_bit
;
1442 anv_foreach_image_aspect_bit(aspect_bit
, image
, color_aspects
) {
1443 transition_color_buffer(cmd_buffer
, image
, 1UL << aspect_bit
,
1444 range
->baseMipLevel
,
1445 anv_get_levelCount(image
, range
),
1446 range
->baseArrayLayer
,
1447 anv_get_layerCount(image
, range
),
1448 pImageMemoryBarriers
[i
].oldLayout
,
1449 pImageMemoryBarriers
[i
].newLayout
);
1454 cmd_buffer
->state
.pending_pipe_bits
|=
1455 anv_pipe_flush_bits_for_access_flags(src_flags
) |
1456 anv_pipe_invalidate_bits_for_access_flags(dst_flags
);
1460 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
1462 VkShaderStageFlags stages
= cmd_buffer
->state
.pipeline
->active_stages
;
1464 /* In order to avoid thrash, we assume that vertex and fragment stages
1465 * always exist. In the rare case where one is missing *and* the other
1466 * uses push concstants, this may be suboptimal. However, avoiding stalls
1467 * seems more important.
1469 stages
|= VK_SHADER_STAGE_FRAGMENT_BIT
| VK_SHADER_STAGE_VERTEX_BIT
;
1471 if (stages
== cmd_buffer
->state
.push_constant_stages
)
1475 const unsigned push_constant_kb
= 32;
1476 #elif GEN_IS_HASWELL
1477 const unsigned push_constant_kb
= cmd_buffer
->device
->info
.gt
== 3 ? 32 : 16;
1479 const unsigned push_constant_kb
= 16;
1482 const unsigned num_stages
=
1483 _mesa_bitcount(stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
1484 unsigned size_per_stage
= push_constant_kb
/ num_stages
;
1486 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
1487 * units of 2KB. Incidentally, these are the same platforms that have
1488 * 32KB worth of push constant space.
1490 if (push_constant_kb
== 32)
1491 size_per_stage
&= ~1u;
1493 uint32_t kb_used
= 0;
1494 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
1495 unsigned push_size
= (stages
& (1 << i
)) ? size_per_stage
: 0;
1496 anv_batch_emit(&cmd_buffer
->batch
,
1497 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS
), alloc
) {
1498 alloc
._3DCommandSubOpcode
= 18 + i
;
1499 alloc
.ConstantBufferOffset
= (push_size
> 0) ? kb_used
: 0;
1500 alloc
.ConstantBufferSize
= push_size
;
1502 kb_used
+= push_size
;
1505 anv_batch_emit(&cmd_buffer
->batch
,
1506 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS
), alloc
) {
1507 alloc
.ConstantBufferOffset
= kb_used
;
1508 alloc
.ConstantBufferSize
= push_constant_kb
- kb_used
;
1511 cmd_buffer
->state
.push_constant_stages
= stages
;
1513 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
1515 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
1516 * the next 3DPRIMITIVE command after programming the
1517 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
1519 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
1520 * pipeline setup, we need to dirty push constants.
1522 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
1526 emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
1527 gl_shader_stage stage
,
1528 struct anv_state
*bt_state
)
1530 struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1531 struct anv_pipeline
*pipeline
;
1532 uint32_t bias
, state_offset
;
1535 case MESA_SHADER_COMPUTE
:
1536 pipeline
= cmd_buffer
->state
.compute_pipeline
;
1540 pipeline
= cmd_buffer
->state
.pipeline
;
1545 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
1546 *bt_state
= (struct anv_state
) { 0, };
1550 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
1551 if (bias
+ map
->surface_count
== 0) {
1552 *bt_state
= (struct anv_state
) { 0, };
1556 *bt_state
= anv_cmd_buffer_alloc_binding_table(cmd_buffer
,
1557 bias
+ map
->surface_count
,
1559 uint32_t *bt_map
= bt_state
->map
;
1561 if (bt_state
->map
== NULL
)
1562 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1564 if (stage
== MESA_SHADER_COMPUTE
&&
1565 get_cs_prog_data(cmd_buffer
->state
.compute_pipeline
)->uses_num_work_groups
) {
1566 struct anv_bo
*bo
= cmd_buffer
->state
.num_workgroups_bo
;
1567 uint32_t bo_offset
= cmd_buffer
->state
.num_workgroups_offset
;
1569 struct anv_state surface_state
;
1571 anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
1573 const enum isl_format format
=
1574 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
);
1575 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
1576 format
, bo_offset
, 12, 1);
1578 bt_map
[0] = surface_state
.offset
+ state_offset
;
1579 add_surface_state_reloc(cmd_buffer
, surface_state
, bo
, bo_offset
);
1582 if (map
->surface_count
== 0)
1585 if (map
->image_count
> 0) {
1587 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, stage
, images
);
1588 if (result
!= VK_SUCCESS
)
1591 cmd_buffer
->state
.push_constants_dirty
|= 1 << stage
;
1595 for (uint32_t s
= 0; s
< map
->surface_count
; s
++) {
1596 struct anv_pipeline_binding
*binding
= &map
->surface_to_descriptor
[s
];
1598 struct anv_state surface_state
;
1600 if (binding
->set
== ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
) {
1601 /* Color attachment binding */
1602 assert(stage
== MESA_SHADER_FRAGMENT
);
1603 assert(binding
->binding
== 0);
1604 if (binding
->index
< subpass
->color_count
) {
1605 const unsigned att
=
1606 subpass
->color_attachments
[binding
->index
].attachment
;
1608 /* From the Vulkan 1.0.46 spec:
1610 * "If any color or depth/stencil attachments are
1611 * VK_ATTACHMENT_UNUSED, then no writes occur for those
1614 if (att
== VK_ATTACHMENT_UNUSED
) {
1615 surface_state
= cmd_buffer
->state
.null_surface_state
;
1617 surface_state
= cmd_buffer
->state
.attachments
[att
].color
.state
;
1620 surface_state
= cmd_buffer
->state
.null_surface_state
;
1623 bt_map
[bias
+ s
] = surface_state
.offset
+ state_offset
;
1627 struct anv_descriptor_set
*set
=
1628 cmd_buffer
->state
.descriptors
[binding
->set
];
1629 uint32_t offset
= set
->layout
->binding
[binding
->binding
].descriptor_index
;
1630 struct anv_descriptor
*desc
= &set
->descriptors
[offset
+ binding
->index
];
1632 switch (desc
->type
) {
1633 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1634 /* Nothing for us to do here */
1637 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1638 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
: {
1639 struct anv_surface_state sstate
=
1640 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
1641 desc
->image_view
->planes
[binding
->plane
].general_sampler_surface_state
:
1642 desc
->image_view
->planes
[binding
->plane
].optimal_sampler_surface_state
;
1643 surface_state
= sstate
.state
;
1644 assert(surface_state
.alloc_size
);
1645 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1646 binding
->plane
, sstate
);
1649 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1650 assert(stage
== MESA_SHADER_FRAGMENT
);
1651 if ((desc
->image_view
->aspect_mask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) == 0) {
1652 /* For depth and stencil input attachments, we treat it like any
1653 * old texture that a user may have bound.
1655 struct anv_surface_state sstate
=
1656 (desc
->layout
== VK_IMAGE_LAYOUT_GENERAL
) ?
1657 desc
->image_view
->planes
[binding
->plane
].general_sampler_surface_state
:
1658 desc
->image_view
->planes
[binding
->plane
].optimal_sampler_surface_state
;
1659 surface_state
= sstate
.state
;
1660 assert(surface_state
.alloc_size
);
1661 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1662 binding
->plane
, sstate
);
1664 /* For color input attachments, we create the surface state at
1665 * vkBeginRenderPass time so that we can include aux and clear
1666 * color information.
1668 assert(binding
->input_attachment_index
< subpass
->input_count
);
1669 const unsigned subpass_att
= binding
->input_attachment_index
;
1670 const unsigned att
= subpass
->input_attachments
[subpass_att
].attachment
;
1671 surface_state
= cmd_buffer
->state
.attachments
[att
].input
.state
;
1675 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
: {
1676 struct anv_surface_state sstate
= (binding
->write_only
)
1677 ? desc
->image_view
->planes
[binding
->plane
].writeonly_storage_surface_state
1678 : desc
->image_view
->planes
[binding
->plane
].storage_surface_state
;
1679 surface_state
= sstate
.state
;
1680 assert(surface_state
.alloc_size
);
1681 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1682 binding
->plane
, sstate
);
1684 struct brw_image_param
*image_param
=
1685 &cmd_buffer
->state
.push_constants
[stage
]->images
[image
++];
1687 *image_param
= desc
->image_view
->planes
[binding
->plane
].storage_image_param
;
1688 image_param
->surface_idx
= bias
+ s
;
1692 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1693 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1694 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1695 surface_state
= desc
->buffer_view
->surface_state
;
1696 assert(surface_state
.alloc_size
);
1697 add_surface_state_reloc(cmd_buffer
, surface_state
,
1698 desc
->buffer_view
->bo
,
1699 desc
->buffer_view
->offset
);
1702 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1703 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1704 uint32_t dynamic_offset_idx
=
1705 pipeline
->layout
->set
[binding
->set
].dynamic_offset_start
+
1706 set
->layout
->binding
[binding
->binding
].dynamic_offset_index
+
1709 /* Compute the offset within the buffer */
1710 uint64_t offset
= desc
->offset
+
1711 cmd_buffer
->state
.dynamic_offsets
[dynamic_offset_idx
];
1712 /* Clamp to the buffer size */
1713 offset
= MIN2(offset
, desc
->buffer
->size
);
1714 /* Clamp the range to the buffer size */
1715 uint32_t range
= MIN2(desc
->range
, desc
->buffer
->size
- offset
);
1718 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, 64, 64);
1719 enum isl_format format
=
1720 anv_isl_format_for_descriptor_type(desc
->type
);
1722 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
1723 format
, offset
, range
, 1);
1724 add_surface_state_reloc(cmd_buffer
, surface_state
,
1726 desc
->buffer
->offset
+ offset
);
1730 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1731 surface_state
= (binding
->write_only
)
1732 ? desc
->buffer_view
->writeonly_storage_surface_state
1733 : desc
->buffer_view
->storage_surface_state
;
1734 assert(surface_state
.alloc_size
);
1735 add_surface_state_reloc(cmd_buffer
, surface_state
,
1736 desc
->buffer_view
->bo
,
1737 desc
->buffer_view
->offset
);
1739 struct brw_image_param
*image_param
=
1740 &cmd_buffer
->state
.push_constants
[stage
]->images
[image
++];
1742 *image_param
= desc
->buffer_view
->storage_image_param
;
1743 image_param
->surface_idx
= bias
+ s
;
1747 assert(!"Invalid descriptor type");
1751 bt_map
[bias
+ s
] = surface_state
.offset
+ state_offset
;
1753 assert(image
== map
->image_count
);
1756 anv_state_flush(cmd_buffer
->device
, *bt_state
);
1762 emit_samplers(struct anv_cmd_buffer
*cmd_buffer
,
1763 gl_shader_stage stage
,
1764 struct anv_state
*state
)
1766 struct anv_pipeline
*pipeline
;
1768 if (stage
== MESA_SHADER_COMPUTE
)
1769 pipeline
= cmd_buffer
->state
.compute_pipeline
;
1771 pipeline
= cmd_buffer
->state
.pipeline
;
1773 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
1774 *state
= (struct anv_state
) { 0, };
1778 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
1779 if (map
->sampler_count
== 0) {
1780 *state
= (struct anv_state
) { 0, };
1784 uint32_t size
= map
->sampler_count
* 16;
1785 *state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 32);
1787 if (state
->map
== NULL
)
1788 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1790 for (uint32_t s
= 0; s
< map
->sampler_count
; s
++) {
1791 struct anv_pipeline_binding
*binding
= &map
->sampler_to_descriptor
[s
];
1792 struct anv_descriptor_set
*set
=
1793 cmd_buffer
->state
.descriptors
[binding
->set
];
1794 uint32_t offset
= set
->layout
->binding
[binding
->binding
].descriptor_index
;
1795 struct anv_descriptor
*desc
= &set
->descriptors
[offset
+ binding
->index
];
1797 if (desc
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
1798 desc
->type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
1801 struct anv_sampler
*sampler
= desc
->sampler
;
1803 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
1804 * happens to be zero.
1806 if (sampler
== NULL
)
1809 memcpy(state
->map
+ (s
* 16),
1810 sampler
->state
[binding
->plane
], sizeof(sampler
->state
[0]));
1813 anv_state_flush(cmd_buffer
->device
, *state
);
1819 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
1821 VkShaderStageFlags dirty
= cmd_buffer
->state
.descriptors_dirty
&
1822 cmd_buffer
->state
.pipeline
->active_stages
;
1824 VkResult result
= VK_SUCCESS
;
1825 anv_foreach_stage(s
, dirty
) {
1826 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
1827 if (result
!= VK_SUCCESS
)
1829 result
= emit_binding_table(cmd_buffer
, s
,
1830 &cmd_buffer
->state
.binding_tables
[s
]);
1831 if (result
!= VK_SUCCESS
)
1835 if (result
!= VK_SUCCESS
) {
1836 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1838 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
1839 if (result
!= VK_SUCCESS
)
1842 /* Re-emit state base addresses so we get the new surface state base
1843 * address before we start emitting binding tables etc.
1845 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1847 /* Re-emit all active binding tables */
1848 dirty
|= cmd_buffer
->state
.pipeline
->active_stages
;
1849 anv_foreach_stage(s
, dirty
) {
1850 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
1851 if (result
!= VK_SUCCESS
) {
1852 anv_batch_set_error(&cmd_buffer
->batch
, result
);
1855 result
= emit_binding_table(cmd_buffer
, s
,
1856 &cmd_buffer
->state
.binding_tables
[s
]);
1857 if (result
!= VK_SUCCESS
) {
1858 anv_batch_set_error(&cmd_buffer
->batch
, result
);
1864 cmd_buffer
->state
.descriptors_dirty
&= ~dirty
;
1870 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
1873 static const uint32_t sampler_state_opcodes
[] = {
1874 [MESA_SHADER_VERTEX
] = 43,
1875 [MESA_SHADER_TESS_CTRL
] = 44, /* HS */
1876 [MESA_SHADER_TESS_EVAL
] = 45, /* DS */
1877 [MESA_SHADER_GEOMETRY
] = 46,
1878 [MESA_SHADER_FRAGMENT
] = 47,
1879 [MESA_SHADER_COMPUTE
] = 0,
1882 static const uint32_t binding_table_opcodes
[] = {
1883 [MESA_SHADER_VERTEX
] = 38,
1884 [MESA_SHADER_TESS_CTRL
] = 39,
1885 [MESA_SHADER_TESS_EVAL
] = 40,
1886 [MESA_SHADER_GEOMETRY
] = 41,
1887 [MESA_SHADER_FRAGMENT
] = 42,
1888 [MESA_SHADER_COMPUTE
] = 0,
1891 anv_foreach_stage(s
, stages
) {
1892 if (cmd_buffer
->state
.samplers
[s
].alloc_size
> 0) {
1893 anv_batch_emit(&cmd_buffer
->batch
,
1894 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS
), ssp
) {
1895 ssp
._3DCommandSubOpcode
= sampler_state_opcodes
[s
];
1896 ssp
.PointertoVSSamplerState
= cmd_buffer
->state
.samplers
[s
].offset
;
1900 /* Always emit binding table pointers if we're asked to, since on SKL
1901 * this is what flushes push constants. */
1902 anv_batch_emit(&cmd_buffer
->batch
,
1903 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS
), btp
) {
1904 btp
._3DCommandSubOpcode
= binding_table_opcodes
[s
];
1905 btp
.PointertoVSBindingTable
= cmd_buffer
->state
.binding_tables
[s
].offset
;
1911 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
1913 static const uint32_t push_constant_opcodes
[] = {
1914 [MESA_SHADER_VERTEX
] = 21,
1915 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
1916 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
1917 [MESA_SHADER_GEOMETRY
] = 22,
1918 [MESA_SHADER_FRAGMENT
] = 23,
1919 [MESA_SHADER_COMPUTE
] = 0,
1922 VkShaderStageFlags flushed
= 0;
1924 anv_foreach_stage(stage
, cmd_buffer
->state
.push_constants_dirty
) {
1925 if (stage
== MESA_SHADER_COMPUTE
)
1928 struct anv_state state
= anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
1930 if (state
.offset
== 0) {
1931 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
)
1932 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
];
1934 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
) {
1935 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
],
1936 c
.ConstantBody
= (struct GENX(3DSTATE_CONSTANT_BODY
)) {
1938 .Buffer
[2] = { &cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
, state
.offset
},
1939 .ReadLength
[2] = DIV_ROUND_UP(state
.alloc_size
, 32),
1941 .Buffer
[0] = { .offset
= state
.offset
},
1942 .ReadLength
[0] = DIV_ROUND_UP(state
.alloc_size
, 32),
1948 flushed
|= mesa_to_vk_shader_stage(stage
);
1951 cmd_buffer
->state
.push_constants_dirty
&= ~VK_SHADER_STAGE_ALL_GRAPHICS
;
1957 genX(cmd_buffer_flush_state
)(struct anv_cmd_buffer
*cmd_buffer
)
1959 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1962 uint32_t vb_emit
= cmd_buffer
->state
.vb_dirty
& pipeline
->vb_used
;
1964 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
1966 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
1968 genX(flush_pipeline_select_3d
)(cmd_buffer
);
1971 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
1972 const uint32_t num_dwords
= 1 + num_buffers
* 4;
1974 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
1975 GENX(3DSTATE_VERTEX_BUFFERS
));
1977 for_each_bit(vb
, vb_emit
) {
1978 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
1979 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
1981 struct GENX(VERTEX_BUFFER_STATE
) state
= {
1982 .VertexBufferIndex
= vb
,
1985 .MemoryObjectControlState
= GENX(MOCS
),
1987 .BufferAccessType
= pipeline
->instancing_enable
[vb
] ? INSTANCEDATA
: VERTEXDATA
,
1988 /* Our implementation of VK_KHR_multiview uses instancing to draw
1989 * the different views. If the client asks for instancing, we
1990 * need to use the Instance Data Step Rate to ensure that we
1991 * repeat the client's per-instance data once for each view.
1993 .InstanceDataStepRate
= anv_subpass_view_count(pipeline
->subpass
),
1994 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
1997 .AddressModifyEnable
= true,
1998 .BufferPitch
= pipeline
->binding_stride
[vb
],
1999 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2002 .BufferSize
= buffer
->size
- offset
2004 .EndAddress
= { buffer
->bo
, buffer
->offset
+ buffer
->size
- 1},
2008 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
2013 cmd_buffer
->state
.vb_dirty
&= ~vb_emit
;
2015 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
2016 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
2018 /* The exact descriptor layout is pulled from the pipeline, so we need
2019 * to re-emit binding tables on every pipeline change.
2021 cmd_buffer
->state
.descriptors_dirty
|=
2022 cmd_buffer
->state
.pipeline
->active_stages
;
2024 /* If the pipeline changed, we may need to re-allocate push constant
2027 cmd_buffer_alloc_push_constants(cmd_buffer
);
2031 if (cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_VERTEX_BIT
||
2032 cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_VERTEX_BIT
) {
2033 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
2035 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
2036 * stall needs to be sent just prior to any 3DSTATE_VS,
2037 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
2038 * 3DSTATE_BINDING_TABLE_POINTER_VS,
2039 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
2040 * PIPE_CONTROL needs to be sent before any combination of VS
2041 * associated 3DSTATE."
2043 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2044 pc
.DepthStallEnable
= true;
2045 pc
.PostSyncOperation
= WriteImmediateData
;
2047 (struct anv_address
) { &cmd_buffer
->device
->workaround_bo
, 0 };
2052 /* Render targets live in the same binding table as fragment descriptors */
2053 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_RENDER_TARGETS
)
2054 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
2056 /* We emit the binding tables and sampler tables first, then emit push
2057 * constants and then finally emit binding table and sampler table
2058 * pointers. It has to happen in this order, since emitting the binding
2059 * tables may change the push constants (in case of storage images). After
2060 * emitting push constants, on SKL+ we have to emit the corresponding
2061 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
2064 if (cmd_buffer
->state
.descriptors_dirty
)
2065 dirty
= flush_descriptor_sets(cmd_buffer
);
2067 if (cmd_buffer
->state
.push_constants_dirty
) {
2069 /* On Sky Lake and later, the binding table pointers commands are
2070 * what actually flush the changes to push constant state so we need
2071 * to dirty them so they get re-emitted below.
2073 dirty
|= cmd_buffer_flush_push_constants(cmd_buffer
);
2075 cmd_buffer_flush_push_constants(cmd_buffer
);
2080 cmd_buffer_emit_descriptor_pointers(cmd_buffer
, dirty
);
2082 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
2083 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
2085 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
|
2086 ANV_CMD_DIRTY_PIPELINE
)) {
2087 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer
,
2088 pipeline
->depth_clamp_enable
);
2091 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
)
2092 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
2094 genX(cmd_buffer_flush_dynamic_state
)(cmd_buffer
);
2096 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
2100 emit_vertex_bo(struct anv_cmd_buffer
*cmd_buffer
,
2101 struct anv_bo
*bo
, uint32_t offset
,
2102 uint32_t size
, uint32_t index
)
2104 uint32_t *p
= anv_batch_emitn(&cmd_buffer
->batch
, 5,
2105 GENX(3DSTATE_VERTEX_BUFFERS
));
2107 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, p
+ 1,
2108 &(struct GENX(VERTEX_BUFFER_STATE
)) {
2109 .VertexBufferIndex
= index
,
2110 .AddressModifyEnable
= true,
2113 .MemoryObjectControlState
= GENX(MOCS
),
2114 .BufferStartingAddress
= { bo
, offset
},
2117 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
2118 .BufferStartingAddress
= { bo
, offset
},
2119 .EndAddress
= { bo
, offset
+ size
},
2125 emit_base_vertex_instance_bo(struct anv_cmd_buffer
*cmd_buffer
,
2126 struct anv_bo
*bo
, uint32_t offset
)
2128 emit_vertex_bo(cmd_buffer
, bo
, offset
, 8, ANV_SVGS_VB_INDEX
);
2132 emit_base_vertex_instance(struct anv_cmd_buffer
*cmd_buffer
,
2133 uint32_t base_vertex
, uint32_t base_instance
)
2135 struct anv_state id_state
=
2136 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 8, 4);
2138 ((uint32_t *)id_state
.map
)[0] = base_vertex
;
2139 ((uint32_t *)id_state
.map
)[1] = base_instance
;
2141 anv_state_flush(cmd_buffer
->device
, id_state
);
2143 emit_base_vertex_instance_bo(cmd_buffer
,
2144 &cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
, id_state
.offset
);
2148 emit_draw_index(struct anv_cmd_buffer
*cmd_buffer
, uint32_t draw_index
)
2150 struct anv_state state
=
2151 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 4, 4);
2153 ((uint32_t *)state
.map
)[0] = draw_index
;
2155 anv_state_flush(cmd_buffer
->device
, state
);
2157 emit_vertex_bo(cmd_buffer
,
2158 &cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
,
2159 state
.offset
, 4, ANV_DRAWID_VB_INDEX
);
2163 VkCommandBuffer commandBuffer
,
2164 uint32_t vertexCount
,
2165 uint32_t instanceCount
,
2166 uint32_t firstVertex
,
2167 uint32_t firstInstance
)
2169 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2170 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
2171 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
2173 if (anv_batch_has_error(&cmd_buffer
->batch
))
2176 genX(cmd_buffer_flush_state
)(cmd_buffer
);
2178 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
2179 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
2180 if (vs_prog_data
->uses_drawid
)
2181 emit_draw_index(cmd_buffer
, 0);
2183 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2184 * different views. We need to multiply instanceCount by the view count.
2186 instanceCount
*= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
2188 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
2189 prim
.VertexAccessType
= SEQUENTIAL
;
2190 prim
.PrimitiveTopologyType
= pipeline
->topology
;
2191 prim
.VertexCountPerInstance
= vertexCount
;
2192 prim
.StartVertexLocation
= firstVertex
;
2193 prim
.InstanceCount
= instanceCount
;
2194 prim
.StartInstanceLocation
= firstInstance
;
2195 prim
.BaseVertexLocation
= 0;
2199 void genX(CmdDrawIndexed
)(
2200 VkCommandBuffer commandBuffer
,
2201 uint32_t indexCount
,
2202 uint32_t instanceCount
,
2203 uint32_t firstIndex
,
2204 int32_t vertexOffset
,
2205 uint32_t firstInstance
)
2207 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2208 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
2209 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
2211 if (anv_batch_has_error(&cmd_buffer
->batch
))
2214 genX(cmd_buffer_flush_state
)(cmd_buffer
);
2216 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
2217 emit_base_vertex_instance(cmd_buffer
, vertexOffset
, firstInstance
);
2218 if (vs_prog_data
->uses_drawid
)
2219 emit_draw_index(cmd_buffer
, 0);
2221 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2222 * different views. We need to multiply instanceCount by the view count.
2224 instanceCount
*= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
2226 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
2227 prim
.VertexAccessType
= RANDOM
;
2228 prim
.PrimitiveTopologyType
= pipeline
->topology
;
2229 prim
.VertexCountPerInstance
= indexCount
;
2230 prim
.StartVertexLocation
= firstIndex
;
2231 prim
.InstanceCount
= instanceCount
;
2232 prim
.StartInstanceLocation
= firstInstance
;
2233 prim
.BaseVertexLocation
= vertexOffset
;
2237 /* Auto-Draw / Indirect Registers */
2238 #define GEN7_3DPRIM_END_OFFSET 0x2420
2239 #define GEN7_3DPRIM_START_VERTEX 0x2430
2240 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2241 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2242 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2243 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2245 /* MI_MATH only exists on Haswell+ */
2246 #if GEN_IS_HASWELL || GEN_GEN >= 8
2249 mi_alu(uint32_t opcode
, uint32_t op1
, uint32_t op2
)
2251 struct GENX(MI_MATH_ALU_INSTRUCTION
) instr
= {
2252 .ALUOpcode
= opcode
,
2258 GENX(MI_MATH_ALU_INSTRUCTION_pack
)(NULL
, &dw
, &instr
);
2263 #define CS_GPR(n) (0x2600 + (n) * 8)
2265 /* Emit dwords to multiply GPR0 by N */
2267 build_alu_multiply_gpr0(uint32_t *dw
, unsigned *dw_count
, uint32_t N
)
2269 VK_OUTARRAY_MAKE(out
, dw
, dw_count
);
2271 #define append_alu(opcode, operand1, operand2) \
2272 vk_outarray_append(&out, alu_dw) *alu_dw = mi_alu(opcode, operand1, operand2)
2275 unsigned top_bit
= 31 - __builtin_clz(N
);
2276 for (int i
= top_bit
- 1; i
>= 0; i
--) {
2277 /* We get our initial data in GPR0 and we write the final data out to
2278 * GPR0 but we use GPR1 as our scratch register.
2280 unsigned src_reg
= i
== top_bit
- 1 ? MI_ALU_REG0
: MI_ALU_REG1
;
2281 unsigned dst_reg
= i
== 0 ? MI_ALU_REG0
: MI_ALU_REG1
;
2283 /* Shift the current value left by 1 */
2284 append_alu(MI_ALU_LOAD
, MI_ALU_SRCA
, src_reg
);
2285 append_alu(MI_ALU_LOAD
, MI_ALU_SRCB
, src_reg
);
2286 append_alu(MI_ALU_ADD
, 0, 0);
2289 /* Store ACCU to R1 and add R0 to R1 */
2290 append_alu(MI_ALU_STORE
, MI_ALU_REG1
, MI_ALU_ACCU
);
2291 append_alu(MI_ALU_LOAD
, MI_ALU_SRCA
, MI_ALU_REG0
);
2292 append_alu(MI_ALU_LOAD
, MI_ALU_SRCB
, MI_ALU_REG1
);
2293 append_alu(MI_ALU_ADD
, 0, 0);
2296 append_alu(MI_ALU_STORE
, dst_reg
, MI_ALU_ACCU
);
2303 emit_mul_gpr0(struct anv_batch
*batch
, uint32_t N
)
2305 uint32_t num_dwords
;
2306 build_alu_multiply_gpr0(NULL
, &num_dwords
, N
);
2308 uint32_t *dw
= anv_batch_emitn(batch
, 1 + num_dwords
, GENX(MI_MATH
));
2309 build_alu_multiply_gpr0(dw
+ 1, &num_dwords
, N
);
2312 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
2315 load_indirect_parameters(struct anv_cmd_buffer
*cmd_buffer
,
2316 struct anv_buffer
*buffer
, uint64_t offset
,
2319 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2320 struct anv_bo
*bo
= buffer
->bo
;
2321 uint32_t bo_offset
= buffer
->offset
+ offset
;
2323 emit_lrm(batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
2325 unsigned view_count
= anv_subpass_view_count(cmd_buffer
->state
.subpass
);
2326 if (view_count
> 1) {
2327 #if GEN_IS_HASWELL || GEN_GEN >= 8
2328 emit_lrm(batch
, CS_GPR(0), bo
, bo_offset
+ 4);
2329 emit_mul_gpr0(batch
, view_count
);
2330 emit_lrr(batch
, GEN7_3DPRIM_INSTANCE_COUNT
, CS_GPR(0));
2332 anv_finishme("Multiview + indirect draw requires MI_MATH\n"
2333 "MI_MATH is not supported on Ivy Bridge");
2334 emit_lrm(batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
2337 emit_lrm(batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
2340 emit_lrm(batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
2343 emit_lrm(batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
2344 emit_lrm(batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
2346 emit_lrm(batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
2347 emit_lri(batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
2351 void genX(CmdDrawIndirect
)(
2352 VkCommandBuffer commandBuffer
,
2354 VkDeviceSize offset
,
2358 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2359 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2360 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
2361 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
2363 if (anv_batch_has_error(&cmd_buffer
->batch
))
2366 genX(cmd_buffer_flush_state
)(cmd_buffer
);
2368 for (uint32_t i
= 0; i
< drawCount
; i
++) {
2369 struct anv_bo
*bo
= buffer
->bo
;
2370 uint32_t bo_offset
= buffer
->offset
+ offset
;
2372 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
2373 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 8);
2374 if (vs_prog_data
->uses_drawid
)
2375 emit_draw_index(cmd_buffer
, i
);
2377 load_indirect_parameters(cmd_buffer
, buffer
, offset
, false);
2379 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
2380 prim
.IndirectParameterEnable
= true;
2381 prim
.VertexAccessType
= SEQUENTIAL
;
2382 prim
.PrimitiveTopologyType
= pipeline
->topology
;
2389 void genX(CmdDrawIndexedIndirect
)(
2390 VkCommandBuffer commandBuffer
,
2392 VkDeviceSize offset
,
2396 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2397 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2398 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
2399 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
2401 if (anv_batch_has_error(&cmd_buffer
->batch
))
2404 genX(cmd_buffer_flush_state
)(cmd_buffer
);
2406 for (uint32_t i
= 0; i
< drawCount
; i
++) {
2407 struct anv_bo
*bo
= buffer
->bo
;
2408 uint32_t bo_offset
= buffer
->offset
+ offset
;
2410 /* TODO: We need to stomp base vertex to 0 somehow */
2411 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
2412 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 12);
2413 if (vs_prog_data
->uses_drawid
)
2414 emit_draw_index(cmd_buffer
, i
);
2416 load_indirect_parameters(cmd_buffer
, buffer
, offset
, true);
2418 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
2419 prim
.IndirectParameterEnable
= true;
2420 prim
.VertexAccessType
= RANDOM
;
2421 prim
.PrimitiveTopologyType
= pipeline
->topology
;
2429 flush_compute_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
)
2431 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
2432 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
2435 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
2436 if (result
!= VK_SUCCESS
) {
2437 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2439 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
2440 if (result
!= VK_SUCCESS
)
2443 /* Re-emit state base addresses so we get the new surface state base
2444 * address before we start emitting binding tables etc.
2446 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
2448 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
2449 if (result
!= VK_SUCCESS
) {
2450 anv_batch_set_error(&cmd_buffer
->batch
, result
);
2455 result
= emit_samplers(cmd_buffer
, MESA_SHADER_COMPUTE
, &samplers
);
2456 if (result
!= VK_SUCCESS
) {
2457 anv_batch_set_error(&cmd_buffer
->batch
, result
);
2461 uint32_t iface_desc_data_dw
[GENX(INTERFACE_DESCRIPTOR_DATA_length
)];
2462 struct GENX(INTERFACE_DESCRIPTOR_DATA
) desc
= {
2463 .BindingTablePointer
= surfaces
.offset
,
2464 .SamplerStatePointer
= samplers
.offset
,
2466 GENX(INTERFACE_DESCRIPTOR_DATA_pack
)(NULL
, iface_desc_data_dw
, &desc
);
2468 struct anv_state state
=
2469 anv_cmd_buffer_merge_dynamic(cmd_buffer
, iface_desc_data_dw
,
2470 pipeline
->interface_descriptor_data
,
2471 GENX(INTERFACE_DESCRIPTOR_DATA_length
),
2474 uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
2475 anv_batch_emit(&cmd_buffer
->batch
,
2476 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
), mid
) {
2477 mid
.InterfaceDescriptorTotalLength
= size
;
2478 mid
.InterfaceDescriptorDataStartAddress
= state
.offset
;
2485 genX(cmd_buffer_flush_compute_state
)(struct anv_cmd_buffer
*cmd_buffer
)
2487 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
2488 MAYBE_UNUSED VkResult result
;
2490 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
2492 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
2494 genX(flush_pipeline_select_gpgpu
)(cmd_buffer
);
2496 if (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
) {
2497 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
2499 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
2500 * the only bits that are changed are scoreboard related: Scoreboard
2501 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
2502 * these scoreboard related states, a MEDIA_STATE_FLUSH is
2505 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
2506 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
2508 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
2511 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
2512 (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)) {
2513 /* FIXME: figure out descriptors for gen7 */
2514 result
= flush_compute_descriptor_set(cmd_buffer
);
2515 if (result
!= VK_SUCCESS
)
2518 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
2521 if (cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) {
2522 struct anv_state push_state
=
2523 anv_cmd_buffer_cs_push_constants(cmd_buffer
);
2525 if (push_state
.alloc_size
) {
2526 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
), curbe
) {
2527 curbe
.CURBETotalDataLength
= push_state
.alloc_size
;
2528 curbe
.CURBEDataStartAddress
= push_state
.offset
;
2533 cmd_buffer
->state
.compute_dirty
= 0;
2535 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
2541 verify_cmd_parser(const struct anv_device
*device
,
2542 int required_version
,
2543 const char *function
)
2545 if (device
->instance
->physicalDevice
.cmd_parser_version
< required_version
) {
2546 return vk_errorf(device
->instance
, device
->instance
,
2547 VK_ERROR_FEATURE_NOT_PRESENT
,
2548 "cmd parser version %d is required for %s",
2549 required_version
, function
);
2557 void genX(CmdDispatch
)(
2558 VkCommandBuffer commandBuffer
,
2563 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2564 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
2565 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
2567 if (anv_batch_has_error(&cmd_buffer
->batch
))
2570 if (prog_data
->uses_num_work_groups
) {
2571 struct anv_state state
=
2572 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 12, 4);
2573 uint32_t *sizes
= state
.map
;
2577 anv_state_flush(cmd_buffer
->device
, state
);
2578 cmd_buffer
->state
.num_workgroups_offset
= state
.offset
;
2579 cmd_buffer
->state
.num_workgroups_bo
=
2580 &cmd_buffer
->device
->dynamic_state_pool
.block_pool
.bo
;
2583 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
2585 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
), ggw
) {
2586 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
2587 ggw
.ThreadDepthCounterMaximum
= 0;
2588 ggw
.ThreadHeightCounterMaximum
= 0;
2589 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
2590 ggw
.ThreadGroupIDXDimension
= x
;
2591 ggw
.ThreadGroupIDYDimension
= y
;
2592 ggw
.ThreadGroupIDZDimension
= z
;
2593 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
2594 ggw
.BottomExecutionMask
= 0xffffffff;
2597 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
2600 #define GPGPU_DISPATCHDIMX 0x2500
2601 #define GPGPU_DISPATCHDIMY 0x2504
2602 #define GPGPU_DISPATCHDIMZ 0x2508
2604 void genX(CmdDispatchIndirect
)(
2605 VkCommandBuffer commandBuffer
,
2607 VkDeviceSize offset
)
2609 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2610 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2611 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
2612 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
2613 struct anv_bo
*bo
= buffer
->bo
;
2614 uint32_t bo_offset
= buffer
->offset
+ offset
;
2615 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2618 /* Linux 4.4 added command parser version 5 which allows the GPGPU
2619 * indirect dispatch registers to be written.
2621 if (verify_cmd_parser(cmd_buffer
->device
, 5,
2622 "vkCmdDispatchIndirect") != VK_SUCCESS
)
2626 if (prog_data
->uses_num_work_groups
) {
2627 cmd_buffer
->state
.num_workgroups_offset
= bo_offset
;
2628 cmd_buffer
->state
.num_workgroups_bo
= bo
;
2631 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
2633 emit_lrm(batch
, GPGPU_DISPATCHDIMX
, bo
, bo_offset
);
2634 emit_lrm(batch
, GPGPU_DISPATCHDIMY
, bo
, bo_offset
+ 4);
2635 emit_lrm(batch
, GPGPU_DISPATCHDIMZ
, bo
, bo_offset
+ 8);
2638 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
2639 emit_lri(batch
, MI_PREDICATE_SRC0
+ 4, 0);
2640 emit_lri(batch
, MI_PREDICATE_SRC1
+ 0, 0);
2641 emit_lri(batch
, MI_PREDICATE_SRC1
+ 4, 0);
2643 /* Load compute_dispatch_indirect_x_size into SRC0 */
2644 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 0);
2646 /* predicate = (compute_dispatch_indirect_x_size == 0); */
2647 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
2648 mip
.LoadOperation
= LOAD_LOAD
;
2649 mip
.CombineOperation
= COMBINE_SET
;
2650 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
2653 /* Load compute_dispatch_indirect_y_size into SRC0 */
2654 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 4);
2656 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
2657 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
2658 mip
.LoadOperation
= LOAD_LOAD
;
2659 mip
.CombineOperation
= COMBINE_OR
;
2660 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
2663 /* Load compute_dispatch_indirect_z_size into SRC0 */
2664 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 8);
2666 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
2667 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
2668 mip
.LoadOperation
= LOAD_LOAD
;
2669 mip
.CombineOperation
= COMBINE_OR
;
2670 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
2673 /* predicate = !predicate; */
2674 #define COMPARE_FALSE 1
2675 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
2676 mip
.LoadOperation
= LOAD_LOADINV
;
2677 mip
.CombineOperation
= COMBINE_OR
;
2678 mip
.CompareOperation
= COMPARE_FALSE
;
2682 anv_batch_emit(batch
, GENX(GPGPU_WALKER
), ggw
) {
2683 ggw
.IndirectParameterEnable
= true;
2684 ggw
.PredicateEnable
= GEN_GEN
<= 7;
2685 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
2686 ggw
.ThreadDepthCounterMaximum
= 0;
2687 ggw
.ThreadHeightCounterMaximum
= 0;
2688 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
2689 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
2690 ggw
.BottomExecutionMask
= 0xffffffff;
2693 anv_batch_emit(batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
2697 genX(flush_pipeline_select
)(struct anv_cmd_buffer
*cmd_buffer
,
2700 if (cmd_buffer
->state
.current_pipeline
== pipeline
)
2703 #if GEN_GEN >= 8 && GEN_GEN < 10
2704 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
2706 * Software must clear the COLOR_CALC_STATE Valid field in
2707 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
2708 * with Pipeline Select set to GPGPU.
2710 * The internal hardware docs recommend the same workaround for Gen9
2713 if (pipeline
== GPGPU
)
2714 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), t
);
2717 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
2718 * PIPELINE_SELECT [DevBWR+]":
2722 * Software must ensure all the write caches are flushed through a
2723 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
2724 * command to invalidate read only caches prior to programming
2725 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
2727 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2728 pc
.RenderTargetCacheFlushEnable
= true;
2729 pc
.DepthCacheFlushEnable
= true;
2730 pc
.DCFlushEnable
= true;
2731 pc
.PostSyncOperation
= NoWrite
;
2732 pc
.CommandStreamerStallEnable
= true;
2735 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2736 pc
.TextureCacheInvalidationEnable
= true;
2737 pc
.ConstantCacheInvalidationEnable
= true;
2738 pc
.StateCacheInvalidationEnable
= true;
2739 pc
.InstructionCacheInvalidateEnable
= true;
2740 pc
.PostSyncOperation
= NoWrite
;
2743 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
2747 ps
.PipelineSelection
= pipeline
;
2750 cmd_buffer
->state
.current_pipeline
= pipeline
;
2754 genX(flush_pipeline_select_3d
)(struct anv_cmd_buffer
*cmd_buffer
)
2756 genX(flush_pipeline_select
)(cmd_buffer
, _3D
);
2760 genX(flush_pipeline_select_gpgpu
)(struct anv_cmd_buffer
*cmd_buffer
)
2762 genX(flush_pipeline_select
)(cmd_buffer
, GPGPU
);
2766 genX(cmd_buffer_emit_gen7_depth_flush
)(struct anv_cmd_buffer
*cmd_buffer
)
2771 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
2773 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
2774 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
2775 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
2776 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
2777 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
2778 * Depth Flush Bit set, followed by another pipelined depth stall
2779 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
2780 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
2781 * via a preceding MI_FLUSH)."
2783 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2784 pipe
.DepthStallEnable
= true;
2786 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2787 pipe
.DepthCacheFlushEnable
= true;
2789 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2790 pipe
.DepthStallEnable
= true;
2795 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
2797 struct anv_device
*device
= cmd_buffer
->device
;
2798 const struct anv_image_view
*iview
=
2799 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2800 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
2802 /* FIXME: Width and Height are wrong */
2804 genX(cmd_buffer_emit_gen7_depth_flush
)(cmd_buffer
);
2806 uint32_t *dw
= anv_batch_emit_dwords(&cmd_buffer
->batch
,
2807 device
->isl_dev
.ds
.size
/ 4);
2811 struct isl_depth_stencil_hiz_emit_info info
= {
2812 .mocs
= device
->default_mocs
,
2816 info
.view
= &iview
->planes
[0].isl
;
2818 if (image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)) {
2819 uint32_t depth_plane
=
2820 anv_image_aspect_to_plane(image
->aspects
, VK_IMAGE_ASPECT_DEPTH_BIT
);
2821 const struct anv_surface
*surface
= &image
->planes
[depth_plane
].surface
;
2823 info
.depth_surf
= &surface
->isl
;
2825 info
.depth_address
=
2826 anv_batch_emit_reloc(&cmd_buffer
->batch
,
2827 dw
+ device
->isl_dev
.ds
.depth_offset
/ 4,
2828 image
->planes
[depth_plane
].bo
,
2829 image
->planes
[depth_plane
].bo_offset
+
2833 cmd_buffer
->state
.subpass
->depth_stencil_attachment
.attachment
;
2834 info
.hiz_usage
= cmd_buffer
->state
.attachments
[ds
].aux_usage
;
2835 if (info
.hiz_usage
== ISL_AUX_USAGE_HIZ
) {
2836 info
.hiz_surf
= &image
->planes
[depth_plane
].aux_surface
.isl
;
2839 anv_batch_emit_reloc(&cmd_buffer
->batch
,
2840 dw
+ device
->isl_dev
.ds
.hiz_offset
/ 4,
2841 image
->planes
[depth_plane
].bo
,
2842 image
->planes
[depth_plane
].bo_offset
+
2843 image
->planes
[depth_plane
].aux_surface
.offset
);
2845 info
.depth_clear_value
= ANV_HZ_FC_VAL
;
2849 if (image
&& (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
)) {
2850 uint32_t stencil_plane
=
2851 anv_image_aspect_to_plane(image
->aspects
, VK_IMAGE_ASPECT_STENCIL_BIT
);
2852 const struct anv_surface
*surface
= &image
->planes
[stencil_plane
].surface
;
2854 info
.stencil_surf
= &surface
->isl
;
2856 info
.stencil_address
=
2857 anv_batch_emit_reloc(&cmd_buffer
->batch
,
2858 dw
+ device
->isl_dev
.ds
.stencil_offset
/ 4,
2859 image
->planes
[stencil_plane
].bo
,
2860 image
->planes
[stencil_plane
].bo_offset
+ surface
->offset
);
2863 isl_emit_depth_stencil_hiz_s(&device
->isl_dev
, dw
, &info
);
2865 cmd_buffer
->state
.hiz_enabled
= info
.hiz_usage
== ISL_AUX_USAGE_HIZ
;
2870 * @brief Perform any layout transitions required at the beginning and/or end
2871 * of the current subpass for depth buffers.
2873 * TODO: Consider preprocessing the attachment reference array at render pass
2874 * create time to determine if no layout transition is needed at the
2875 * beginning and/or end of each subpass.
2877 * @param cmd_buffer The command buffer the transition is happening within.
2878 * @param subpass_end If true, marks that the transition is happening at the
2879 * end of the subpass.
2882 cmd_buffer_subpass_transition_layouts(struct anv_cmd_buffer
* const cmd_buffer
,
2883 const bool subpass_end
)
2885 /* We need a non-NULL command buffer. */
2888 const struct anv_cmd_state
* const cmd_state
= &cmd_buffer
->state
;
2889 const struct anv_subpass
* const subpass
= cmd_state
->subpass
;
2891 /* This function must be called within a subpass. */
2894 /* If there are attachment references, the array shouldn't be NULL.
2896 if (subpass
->attachment_count
> 0)
2897 assert(subpass
->attachments
);
2899 /* Iterate over the array of attachment references. */
2900 for (const VkAttachmentReference
*att_ref
= subpass
->attachments
;
2901 att_ref
< subpass
->attachments
+ subpass
->attachment_count
; att_ref
++) {
2903 /* If the attachment is unused, we can't perform a layout transition. */
2904 if (att_ref
->attachment
== VK_ATTACHMENT_UNUSED
)
2907 /* This attachment index shouldn't go out of bounds. */
2908 assert(att_ref
->attachment
< cmd_state
->pass
->attachment_count
);
2910 const struct anv_render_pass_attachment
* const att_desc
=
2911 &cmd_state
->pass
->attachments
[att_ref
->attachment
];
2912 struct anv_attachment_state
* const att_state
=
2913 &cmd_buffer
->state
.attachments
[att_ref
->attachment
];
2915 /* The attachment should not be used in a subpass after its last. */
2916 assert(att_desc
->last_subpass_idx
>= anv_get_subpass_id(cmd_state
));
2918 if (subpass_end
&& anv_get_subpass_id(cmd_state
) <
2919 att_desc
->last_subpass_idx
) {
2920 /* We're calling this function on a buffer twice in one subpass and
2921 * this is not the last use of the buffer. The layout should not have
2922 * changed from the first call and no transition is necessary.
2924 assert(att_state
->current_layout
== att_ref
->layout
||
2925 att_state
->current_layout
==
2926 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
);
2930 /* The attachment index must be less than the number of attachments
2931 * within the framebuffer.
2933 assert(att_ref
->attachment
< cmd_state
->framebuffer
->attachment_count
);
2935 const struct anv_image_view
* const iview
=
2936 cmd_state
->framebuffer
->attachments
[att_ref
->attachment
];
2937 const struct anv_image
* const image
= iview
->image
;
2939 /* Get the appropriate target layout for this attachment. */
2940 VkImageLayout target_layout
;
2942 /* A resolve is necessary before use as an input attachment if the clear
2943 * color or auxiliary buffer usage isn't supported by the sampler.
2945 const bool input_needs_resolve
=
2946 (att_state
->fast_clear
&& !att_state
->clear_color_is_zero_one
) ||
2947 att_state
->input_aux_usage
!= att_state
->aux_usage
;
2949 target_layout
= att_desc
->final_layout
;
2950 } else if (iview
->aspect_mask
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
&&
2951 !input_needs_resolve
) {
2952 /* Layout transitions before the final only help to enable sampling as
2953 * an input attachment. If the input attachment supports sampling
2954 * using the auxiliary surface, we can skip such transitions by making
2955 * the target layout one that is CCS-aware.
2957 target_layout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
2959 target_layout
= att_ref
->layout
;
2962 /* Perform the layout transition. */
2963 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
2964 transition_depth_buffer(cmd_buffer
, image
,
2965 att_state
->current_layout
, target_layout
);
2966 att_state
->aux_usage
=
2967 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
,
2968 VK_IMAGE_ASPECT_DEPTH_BIT
, target_layout
);
2969 } else if (image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) {
2970 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
2971 transition_color_buffer(cmd_buffer
, image
, VK_IMAGE_ASPECT_COLOR_BIT
,
2972 iview
->planes
[0].isl
.base_level
, 1,
2973 iview
->planes
[0].isl
.base_array_layer
,
2974 iview
->planes
[0].isl
.array_len
,
2975 att_state
->current_layout
, target_layout
);
2978 att_state
->current_layout
= target_layout
;
2982 /* Update the clear value dword(s) in surface state objects or the fast clear
2983 * state buffer entry for the color attachments used in this subpass.
2986 cmd_buffer_subpass_sync_fast_clear_values(struct anv_cmd_buffer
*cmd_buffer
)
2988 assert(cmd_buffer
&& cmd_buffer
->state
.subpass
);
2990 const struct anv_cmd_state
*state
= &cmd_buffer
->state
;
2992 /* Iterate through every color attachment used in this subpass. */
2993 for (uint32_t i
= 0; i
< state
->subpass
->color_count
; ++i
) {
2995 /* The attachment should be one of the attachments described in the
2996 * render pass and used in the subpass.
2998 const uint32_t a
= state
->subpass
->color_attachments
[i
].attachment
;
2999 if (a
== VK_ATTACHMENT_UNUSED
)
3002 assert(a
< state
->pass
->attachment_count
);
3004 /* Store some information regarding this attachment. */
3005 const struct anv_attachment_state
*att_state
= &state
->attachments
[a
];
3006 const struct anv_image_view
*iview
= state
->framebuffer
->attachments
[a
];
3007 const struct anv_render_pass_attachment
*rp_att
=
3008 &state
->pass
->attachments
[a
];
3010 if (att_state
->aux_usage
== ISL_AUX_USAGE_NONE
)
3013 /* The fast clear state entry must be updated if a fast clear is going to
3014 * happen. The surface state must be updated if the clear value from a
3015 * prior fast clear may be needed.
3017 if (att_state
->pending_clear_aspects
&& att_state
->fast_clear
) {
3018 /* Update the fast clear state entry. */
3019 genX(copy_fast_clear_dwords
)(cmd_buffer
, att_state
->color
.state
,
3021 VK_IMAGE_ASPECT_COLOR_BIT
,
3022 iview
->planes
[0].isl
.base_level
,
3023 true /* copy from ss */);
3025 /* Fast-clears impact whether or not a resolve will be necessary. */
3026 if (iview
->image
->planes
[0].aux_usage
== ISL_AUX_USAGE_CCS_E
&&
3027 att_state
->clear_color_is_zero
) {
3028 /* This image always has the auxiliary buffer enabled. We can mark
3029 * the subresource as not needing a resolve because the clear color
3030 * will match what's in every RENDER_SURFACE_STATE object when it's
3031 * being used for sampling.
3033 genX(set_image_needs_resolve
)(cmd_buffer
, iview
->image
,
3034 VK_IMAGE_ASPECT_COLOR_BIT
,
3035 iview
->planes
[0].isl
.base_level
,
3038 genX(set_image_needs_resolve
)(cmd_buffer
, iview
->image
,
3039 VK_IMAGE_ASPECT_COLOR_BIT
,
3040 iview
->planes
[0].isl
.base_level
,
3043 } else if (rp_att
->load_op
== VK_ATTACHMENT_LOAD_OP_LOAD
) {
3044 /* The attachment may have been fast-cleared in a previous render
3045 * pass and the value is needed now. Update the surface state(s).
3047 * TODO: Do this only once per render pass instead of every subpass.
3049 genX(copy_fast_clear_dwords
)(cmd_buffer
, att_state
->color
.state
,
3051 VK_IMAGE_ASPECT_COLOR_BIT
,
3052 iview
->planes
[0].isl
.base_level
,
3053 false /* copy to ss */);
3055 if (need_input_attachment_state(rp_att
) &&
3056 att_state
->input_aux_usage
!= ISL_AUX_USAGE_NONE
) {
3057 genX(copy_fast_clear_dwords
)(cmd_buffer
, att_state
->input
.state
,
3059 VK_IMAGE_ASPECT_COLOR_BIT
,
3060 iview
->planes
[0].isl
.base_level
,
3061 false /* copy to ss */);
3069 genX(cmd_buffer_set_subpass
)(struct anv_cmd_buffer
*cmd_buffer
,
3070 struct anv_subpass
*subpass
)
3072 cmd_buffer
->state
.subpass
= subpass
;
3074 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
3076 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3077 * different views. If the client asks for instancing, we need to use the
3078 * Instance Data Step Rate to ensure that we repeat the client's
3079 * per-instance data once for each view. Since this bit is in
3080 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
3084 cmd_buffer
->state
.vb_dirty
|= ~0;
3086 /* Perform transitions to the subpass layout before any writes have
3089 cmd_buffer_subpass_transition_layouts(cmd_buffer
, false);
3091 /* Update clear values *after* performing automatic layout transitions.
3092 * This ensures that transitions from the UNDEFINED layout have had a chance
3093 * to populate the clear value buffer with the correct values for the
3094 * LOAD_OP_LOAD loadOp and that the fast-clears will update the buffer
3095 * without the aforementioned layout transition overwriting the fast-clear
3098 cmd_buffer_subpass_sync_fast_clear_values(cmd_buffer
);
3100 cmd_buffer_emit_depth_stencil(cmd_buffer
);
3102 anv_cmd_buffer_clear_subpass(cmd_buffer
);
3105 void genX(CmdBeginRenderPass
)(
3106 VkCommandBuffer commandBuffer
,
3107 const VkRenderPassBeginInfo
* pRenderPassBegin
,
3108 VkSubpassContents contents
)
3110 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3111 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
3112 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
3114 cmd_buffer
->state
.framebuffer
= framebuffer
;
3115 cmd_buffer
->state
.pass
= pass
;
3116 cmd_buffer
->state
.render_area
= pRenderPassBegin
->renderArea
;
3118 genX(cmd_buffer_setup_attachments
)(cmd_buffer
, pass
, pRenderPassBegin
);
3120 /* If we failed to setup the attachments we should not try to go further */
3121 if (result
!= VK_SUCCESS
) {
3122 assert(anv_batch_has_error(&cmd_buffer
->batch
));
3126 genX(flush_pipeline_select_3d
)(cmd_buffer
);
3128 genX(cmd_buffer_set_subpass
)(cmd_buffer
, pass
->subpasses
);
3130 cmd_buffer
->state
.pending_pipe_bits
|=
3131 cmd_buffer
->state
.pass
->subpass_flushes
[0];
3134 void genX(CmdNextSubpass
)(
3135 VkCommandBuffer commandBuffer
,
3136 VkSubpassContents contents
)
3138 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3140 if (anv_batch_has_error(&cmd_buffer
->batch
))
3143 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
3145 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
3147 /* Perform transitions to the final layout after all writes have occurred.
3149 cmd_buffer_subpass_transition_layouts(cmd_buffer
, true);
3151 genX(cmd_buffer_set_subpass
)(cmd_buffer
, cmd_buffer
->state
.subpass
+ 1);
3153 uint32_t subpass_id
= anv_get_subpass_id(&cmd_buffer
->state
);
3154 cmd_buffer
->state
.pending_pipe_bits
|=
3155 cmd_buffer
->state
.pass
->subpass_flushes
[subpass_id
];
3158 void genX(CmdEndRenderPass
)(
3159 VkCommandBuffer commandBuffer
)
3161 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3163 if (anv_batch_has_error(&cmd_buffer
->batch
))
3166 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
3168 /* Perform transitions to the final layout after all writes have occurred.
3170 cmd_buffer_subpass_transition_layouts(cmd_buffer
, true);
3172 cmd_buffer
->state
.pending_pipe_bits
|=
3173 cmd_buffer
->state
.pass
->subpass_flushes
[cmd_buffer
->state
.pass
->subpass_count
];
3175 cmd_buffer
->state
.hiz_enabled
= false;
3178 anv_dump_add_framebuffer(cmd_buffer
, cmd_buffer
->state
.framebuffer
);
3181 /* Remove references to render pass specific state. This enables us to
3182 * detect whether or not we're in a renderpass.
3184 cmd_buffer
->state
.framebuffer
= NULL
;
3185 cmd_buffer
->state
.pass
= NULL
;
3186 cmd_buffer
->state
.subpass
= NULL
;