2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 #include "anv_private.h"
28 #include "vk_format_info.h"
30 #include "common/gen_l3_config.h"
31 #include "genxml/gen_macros.h"
32 #include "genxml/genX_pack.h"
35 emit_lrm(struct anv_batch
*batch
,
36 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
38 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
39 lrm
.RegisterAddress
= reg
;
40 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
45 emit_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
47 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
48 lri
.RegisterOffset
= reg
;
54 genX(cmd_buffer_emit_state_base_address
)(struct anv_cmd_buffer
*cmd_buffer
)
56 struct anv_device
*device
= cmd_buffer
->device
;
58 /* Emit a render target cache flush.
60 * This isn't documented anywhere in the PRM. However, it seems to be
61 * necessary prior to changing the surface state base adress. Without
62 * this, we get GPU hangs when using multi-level command buffers which
63 * clear depth, reset state base address, and then go render stuff.
65 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
66 pc
.DCFlushEnable
= true;
67 pc
.RenderTargetCacheFlushEnable
= true;
68 pc
.CommandStreamerStallEnable
= true;
71 anv_batch_emit(&cmd_buffer
->batch
, GENX(STATE_BASE_ADDRESS
), sba
) {
72 sba
.GeneralStateBaseAddress
= (struct anv_address
) { NULL
, 0 };
73 sba
.GeneralStateMemoryObjectControlState
= GENX(MOCS
);
74 sba
.GeneralStateBaseAddressModifyEnable
= true;
76 sba
.SurfaceStateBaseAddress
=
77 anv_cmd_buffer_surface_base_address(cmd_buffer
);
78 sba
.SurfaceStateMemoryObjectControlState
= GENX(MOCS
);
79 sba
.SurfaceStateBaseAddressModifyEnable
= true;
81 sba
.DynamicStateBaseAddress
=
82 (struct anv_address
) { &device
->dynamic_state_block_pool
.bo
, 0 };
83 sba
.DynamicStateMemoryObjectControlState
= GENX(MOCS
);
84 sba
.DynamicStateBaseAddressModifyEnable
= true;
86 sba
.IndirectObjectBaseAddress
= (struct anv_address
) { NULL
, 0 };
87 sba
.IndirectObjectMemoryObjectControlState
= GENX(MOCS
);
88 sba
.IndirectObjectBaseAddressModifyEnable
= true;
90 sba
.InstructionBaseAddress
=
91 (struct anv_address
) { &device
->instruction_block_pool
.bo
, 0 };
92 sba
.InstructionMemoryObjectControlState
= GENX(MOCS
);
93 sba
.InstructionBaseAddressModifyEnable
= true;
96 /* Broadwell requires that we specify a buffer size for a bunch of
97 * these fields. However, since we will be growing the BO's live, we
98 * just set them all to the maximum.
100 sba
.GeneralStateBufferSize
= 0xfffff;
101 sba
.GeneralStateBufferSizeModifyEnable
= true;
102 sba
.DynamicStateBufferSize
= 0xfffff;
103 sba
.DynamicStateBufferSizeModifyEnable
= true;
104 sba
.IndirectObjectBufferSize
= 0xfffff;
105 sba
.IndirectObjectBufferSizeModifyEnable
= true;
106 sba
.InstructionBufferSize
= 0xfffff;
107 sba
.InstructionBuffersizeModifyEnable
= true;
111 /* After re-setting the surface state base address, we have to do some
112 * cache flusing so that the sampler engine will pick up the new
113 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
114 * Shared Function > 3D Sampler > State > State Caching (page 96):
116 * Coherency with system memory in the state cache, like the texture
117 * cache is handled partially by software. It is expected that the
118 * command stream or shader will issue Cache Flush operation or
119 * Cache_Flush sampler message to ensure that the L1 cache remains
120 * coherent with system memory.
124 * Whenever the value of the Dynamic_State_Base_Addr,
125 * Surface_State_Base_Addr are altered, the L1 state cache must be
126 * invalidated to ensure the new surface or sampler state is fetched
127 * from system memory.
129 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
130 * which, according the PIPE_CONTROL instruction documentation in the
133 * Setting this bit is independent of any other bit in this packet.
134 * This bit controls the invalidation of the L1 and L2 state caches
135 * at the top of the pipe i.e. at the parsing time.
137 * Unfortunately, experimentation seems to indicate that state cache
138 * invalidation through a PIPE_CONTROL does nothing whatsoever in
139 * regards to surface state and binding tables. In stead, it seems that
140 * invalidating the texture cache is what is actually needed.
142 * XXX: As far as we have been able to determine through
143 * experimentation, shows that flush the texture cache appears to be
144 * sufficient. The theory here is that all of the sampling/rendering
145 * units cache the binding table in the texture cache. However, we have
146 * yet to be able to actually confirm this.
148 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
149 pc
.TextureCacheInvalidationEnable
= true;
150 pc
.ConstantCacheInvalidationEnable
= true;
151 pc
.StateCacheInvalidationEnable
= true;
156 add_surface_state_reloc(struct anv_cmd_buffer
*cmd_buffer
,
157 struct anv_state state
,
158 struct anv_bo
*bo
, uint32_t offset
)
160 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
163 anv_reloc_list_add(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
,
164 state
.offset
+ isl_dev
->ss
.addr_offset
, bo
, offset
);
165 if (result
!= VK_SUCCESS
)
166 anv_batch_set_error(&cmd_buffer
->batch
, result
);
170 add_image_view_relocs(struct anv_cmd_buffer
*cmd_buffer
,
171 const struct anv_image_view
*iview
,
172 enum isl_aux_usage aux_usage
,
173 struct anv_state state
)
175 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
177 add_surface_state_reloc(cmd_buffer
, state
, iview
->bo
, iview
->offset
);
179 if (aux_usage
!= ISL_AUX_USAGE_NONE
) {
180 uint32_t aux_offset
= iview
->offset
+ iview
->image
->aux_surface
.offset
;
182 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
183 * used to store other information. This should be ok, however, because
184 * surface buffer addresses are always 4K page alinged.
186 assert((aux_offset
& 0xfff) == 0);
187 uint32_t *aux_addr_dw
= state
.map
+ isl_dev
->ss
.aux_addr_offset
;
188 aux_offset
+= *aux_addr_dw
& 0xfff;
191 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
192 &cmd_buffer
->pool
->alloc
,
193 state
.offset
+ isl_dev
->ss
.aux_addr_offset
,
194 iview
->bo
, aux_offset
);
195 if (result
!= VK_SUCCESS
)
196 anv_batch_set_error(&cmd_buffer
->batch
, result
);
201 color_is_zero_one(VkClearColorValue value
, enum isl_format format
)
203 if (isl_format_has_int_channel(format
)) {
204 for (unsigned i
= 0; i
< 4; i
++) {
205 if (value
.int32
[i
] != 0 && value
.int32
[i
] != 1)
209 for (unsigned i
= 0; i
< 4; i
++) {
210 if (value
.float32
[i
] != 0.0f
&& value
.float32
[i
] != 1.0f
)
219 color_attachment_compute_aux_usage(struct anv_device
*device
,
220 struct anv_attachment_state
*att_state
,
221 struct anv_image_view
*iview
,
222 VkRect2D render_area
,
223 union isl_color_value
*fast_clear_color
)
225 if (iview
->image
->aux_surface
.isl
.size
== 0) {
226 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
227 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
228 att_state
->fast_clear
= false;
230 } else if (iview
->image
->aux_usage
== ISL_AUX_USAGE_MCS
) {
231 att_state
->aux_usage
= ISL_AUX_USAGE_MCS
;
232 att_state
->input_aux_usage
= ISL_AUX_USAGE_MCS
;
233 att_state
->fast_clear
= false;
237 assert(iview
->image
->aux_surface
.isl
.usage
& ISL_SURF_USAGE_CCS_BIT
);
239 att_state
->clear_color_is_zero_one
=
240 color_is_zero_one(att_state
->clear_value
.color
, iview
->isl
.format
);
242 if (att_state
->pending_clear_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
243 /* Start off assuming fast clears are possible */
244 att_state
->fast_clear
= true;
246 /* Potentially, we could do partial fast-clears but doing so has crazy
247 * alignment restrictions. It's easier to just restrict to full size
248 * fast clears for now.
250 if (render_area
.offset
.x
!= 0 ||
251 render_area
.offset
.y
!= 0 ||
252 render_area
.extent
.width
!= iview
->extent
.width
||
253 render_area
.extent
.height
!= iview
->extent
.height
)
254 att_state
->fast_clear
= false;
257 /* On gen7, we can't do multi-LOD or multi-layer fast-clears. We
258 * technically can, but it comes with crazy restrictions that we
259 * don't want to deal with now.
261 if (iview
->isl
.base_level
> 0 ||
262 iview
->isl
.base_array_layer
> 0 ||
263 iview
->isl
.array_len
> 1)
264 att_state
->fast_clear
= false;
267 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
268 if (GEN_GEN
<= 8 && !att_state
->clear_color_is_zero_one
)
269 att_state
->fast_clear
= false;
271 if (att_state
->fast_clear
) {
272 memcpy(fast_clear_color
->u32
, att_state
->clear_value
.color
.uint32
,
273 sizeof(fast_clear_color
->u32
));
276 att_state
->fast_clear
= false;
280 * TODO: Consider using a heuristic to determine if temporarily enabling
281 * CCS_E for this image view would be beneficial.
283 * While fast-clear resolves and partial resolves are fairly cheap in the
284 * case where you render to most of the pixels, full resolves are not
285 * because they potentially involve reading and writing the entire
286 * framebuffer. If we can't texture with CCS_E, we should leave it off and
287 * limit ourselves to fast clears.
289 if (iview
->image
->aux_usage
== ISL_AUX_USAGE_CCS_E
) {
290 att_state
->aux_usage
= ISL_AUX_USAGE_CCS_E
;
291 att_state
->input_aux_usage
= ISL_AUX_USAGE_CCS_E
;
292 } else if (att_state
->fast_clear
) {
293 att_state
->aux_usage
= ISL_AUX_USAGE_CCS_D
;
295 !isl_format_supports_ccs_e(&device
->info
, iview
->isl
.format
)) {
296 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
298 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
299 * setting is only allowed if Surface Format supported for Fast
300 * Clear. In addition, if the surface is bound to the sampling
301 * engine, Surface Format must be supported for Render Target
302 * Compression for surfaces bound to the sampling engine."
304 * In other words, we can't sample from a fast-cleared image if it
305 * doesn't also support color compression.
307 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
308 } else if (GEN_GEN
>= 8) {
309 /* Broadwell/Skylake can sample from fast-cleared images */
310 att_state
->input_aux_usage
= ISL_AUX_USAGE_CCS_D
;
312 /* Ivy Bridge and Haswell cannot */
313 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
316 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
317 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
322 need_input_attachment_state(const struct anv_render_pass_attachment
*att
)
324 if (!(att
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
327 /* We only allocate input attachment states for color surfaces. Compression
328 * is not yet enabled for depth textures and stencil doesn't allow
329 * compression so we can just use the texture surface state from the view.
331 return vk_format_is_color(att
->format
);
334 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
335 * the initial layout is undefined, the HiZ buffer and depth buffer will
336 * represent the same data at the end of this operation.
339 transition_depth_buffer(struct anv_cmd_buffer
*cmd_buffer
,
340 const struct anv_image
*image
,
341 VkImageLayout initial_layout
,
342 VkImageLayout final_layout
)
346 /* A transition is a no-op if HiZ is not enabled, or if the initial and
347 * final layouts are equal.
349 * The undefined layout indicates that the user doesn't care about the data
350 * that's currently in the buffer. Therefore, a data-preserving resolve
351 * operation is not needed.
353 * The pre-initialized layout is equivalent to the undefined layout for
354 * optimally-tiled images. Anv only exposes support for optimally-tiled
357 if (image
->aux_usage
!= ISL_AUX_USAGE_HIZ
||
358 initial_layout
== final_layout
||
359 initial_layout
== VK_IMAGE_LAYOUT_UNDEFINED
||
360 initial_layout
== VK_IMAGE_LAYOUT_PREINITIALIZED
)
363 const bool hiz_enabled
= ISL_AUX_USAGE_HIZ
==
364 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
, image
->aspects
,
366 const bool enable_hiz
= ISL_AUX_USAGE_HIZ
==
367 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
, image
->aspects
,
370 enum blorp_hiz_op hiz_op
;
371 if (hiz_enabled
&& !enable_hiz
) {
372 hiz_op
= BLORP_HIZ_OP_DEPTH_RESOLVE
;
373 } else if (!hiz_enabled
&& enable_hiz
) {
374 hiz_op
= BLORP_HIZ_OP_HIZ_RESOLVE
;
376 assert(hiz_enabled
== enable_hiz
);
377 /* If the same buffer will be used, no resolves are necessary. */
378 hiz_op
= BLORP_HIZ_OP_NONE
;
381 if (hiz_op
!= BLORP_HIZ_OP_NONE
)
382 anv_gen8_hiz_op_resolve(cmd_buffer
, image
, hiz_op
);
387 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
390 genX(cmd_buffer_setup_attachments
)(struct anv_cmd_buffer
*cmd_buffer
,
391 struct anv_render_pass
*pass
,
392 const VkRenderPassBeginInfo
*begin
)
394 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
395 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
397 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
399 if (pass
->attachment_count
== 0) {
400 state
->attachments
= NULL
;
404 state
->attachments
= vk_alloc(&cmd_buffer
->pool
->alloc
,
405 pass
->attachment_count
*
406 sizeof(state
->attachments
[0]),
407 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
408 if (state
->attachments
== NULL
) {
409 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
410 return anv_batch_set_error(&cmd_buffer
->batch
,
411 VK_ERROR_OUT_OF_HOST_MEMORY
);
414 bool need_null_state
= false;
415 unsigned num_states
= 0;
416 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
417 if (vk_format_is_color(pass
->attachments
[i
].format
)) {
420 /* We need a null state for any depth-stencil-only subpasses.
421 * Importantly, this includes depth/stencil clears so we create one
422 * whenever we have depth or stencil
424 need_null_state
= true;
427 if (need_input_attachment_state(&pass
->attachments
[i
]))
430 num_states
+= need_null_state
;
432 const uint32_t ss_stride
= align_u32(isl_dev
->ss
.size
, isl_dev
->ss
.align
);
433 state
->render_pass_states
=
434 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
435 num_states
* ss_stride
, isl_dev
->ss
.align
);
437 struct anv_state next_state
= state
->render_pass_states
;
438 next_state
.alloc_size
= isl_dev
->ss
.size
;
440 if (need_null_state
) {
441 state
->null_surface_state
= next_state
;
442 next_state
.offset
+= ss_stride
;
443 next_state
.map
+= ss_stride
;
446 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
447 if (vk_format_is_color(pass
->attachments
[i
].format
)) {
448 state
->attachments
[i
].color_rt_state
= next_state
;
449 next_state
.offset
+= ss_stride
;
450 next_state
.map
+= ss_stride
;
453 if (need_input_attachment_state(&pass
->attachments
[i
])) {
454 state
->attachments
[i
].input_att_state
= next_state
;
455 next_state
.offset
+= ss_stride
;
456 next_state
.map
+= ss_stride
;
459 assert(next_state
.offset
== state
->render_pass_states
.offset
+
460 state
->render_pass_states
.alloc_size
);
463 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, begin
->framebuffer
);
464 assert(pass
->attachment_count
== framebuffer
->attachment_count
);
466 if (need_null_state
) {
467 struct GENX(RENDER_SURFACE_STATE
) null_ss
= {
468 .SurfaceType
= SURFTYPE_NULL
,
469 .SurfaceArray
= framebuffer
->layers
> 0,
470 .SurfaceFormat
= ISL_FORMAT_R8G8B8A8_UNORM
,
474 .TiledSurface
= true,
476 .Width
= framebuffer
->width
- 1,
477 .Height
= framebuffer
->height
- 1,
478 .Depth
= framebuffer
->layers
- 1,
479 .RenderTargetViewExtent
= framebuffer
->layers
- 1,
481 GENX(RENDER_SURFACE_STATE_pack
)(NULL
, state
->null_surface_state
.map
,
485 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
486 struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
487 VkImageAspectFlags att_aspects
= vk_format_aspects(att
->format
);
488 VkImageAspectFlags clear_aspects
= 0;
490 if (att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
491 /* color attachment */
492 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
493 clear_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
496 /* depthstencil attachment */
497 if ((att_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
498 att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
499 clear_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
501 if ((att_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
502 att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
503 clear_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
507 state
->attachments
[i
].current_layout
= att
->initial_layout
;
508 state
->attachments
[i
].pending_clear_aspects
= clear_aspects
;
510 state
->attachments
[i
].clear_value
= begin
->pClearValues
[i
];
512 struct anv_image_view
*iview
= framebuffer
->attachments
[i
];
513 anv_assert(iview
->vk_format
== att
->format
);
515 union isl_color_value clear_color
= { .u32
= { 0, } };
516 if (att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
517 color_attachment_compute_aux_usage(cmd_buffer
->device
,
518 &state
->attachments
[i
],
519 iview
, begin
->renderArea
,
522 struct isl_view view
= iview
->isl
;
523 view
.usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
524 view
.swizzle
= anv_swizzle_for_render(view
.swizzle
);
525 isl_surf_fill_state(isl_dev
,
526 state
->attachments
[i
].color_rt_state
.map
,
527 .surf
= &iview
->image
->color_surface
.isl
,
529 .aux_surf
= &iview
->image
->aux_surface
.isl
,
530 .aux_usage
= state
->attachments
[i
].aux_usage
,
531 .clear_color
= clear_color
,
532 .mocs
= cmd_buffer
->device
->default_mocs
);
534 add_image_view_relocs(cmd_buffer
, iview
,
535 state
->attachments
[i
].aux_usage
,
536 state
->attachments
[i
].color_rt_state
);
538 /* This field will be initialized after the first subpass
541 state
->attachments
[i
].aux_usage
= ISL_AUX_USAGE_NONE
;
543 state
->attachments
[i
].input_aux_usage
= ISL_AUX_USAGE_NONE
;
546 if (need_input_attachment_state(&pass
->attachments
[i
])) {
547 struct isl_view view
= iview
->isl
;
548 view
.usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
549 isl_surf_fill_state(isl_dev
,
550 state
->attachments
[i
].input_att_state
.map
,
551 .surf
= &iview
->image
->color_surface
.isl
,
553 .aux_surf
= &iview
->image
->aux_surface
.isl
,
554 .aux_usage
= state
->attachments
[i
].input_aux_usage
,
555 .clear_color
= clear_color
,
556 .mocs
= cmd_buffer
->device
->default_mocs
);
558 add_image_view_relocs(cmd_buffer
, iview
,
559 state
->attachments
[i
].input_aux_usage
,
560 state
->attachments
[i
].input_att_state
);
564 anv_state_flush(cmd_buffer
->device
, state
->render_pass_states
);
571 genX(BeginCommandBuffer
)(
572 VkCommandBuffer commandBuffer
,
573 const VkCommandBufferBeginInfo
* pBeginInfo
)
575 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
577 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
578 * command buffer's state. Otherwise, we must *reset* its state. In both
581 * From the Vulkan 1.0 spec:
583 * If a command buffer is in the executable state and the command buffer
584 * was allocated from a command pool with the
585 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
586 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
587 * as if vkResetCommandBuffer had been called with
588 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
589 * the command buffer in the recording state.
591 anv_cmd_buffer_reset(cmd_buffer
);
593 cmd_buffer
->usage_flags
= pBeginInfo
->flags
;
595 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
||
596 !(cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
));
598 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
600 /* We sometimes store vertex data in the dynamic state buffer for blorp
601 * operations and our dynamic state stream may re-use data from previous
602 * command buffers. In order to prevent stale cache data, we flush the VF
603 * cache. We could do this on every blorp call but that's not really
604 * needed as all of the data will get written by the CPU prior to the GPU
605 * executing anything. The chances are fairly high that they will use
606 * blorp at least once per primary command buffer so it shouldn't be
609 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
)
610 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
612 VkResult result
= VK_SUCCESS
;
613 if (cmd_buffer
->usage_flags
&
614 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
615 cmd_buffer
->state
.pass
=
616 anv_render_pass_from_handle(pBeginInfo
->pInheritanceInfo
->renderPass
);
617 cmd_buffer
->state
.subpass
=
618 &cmd_buffer
->state
.pass
->subpasses
[pBeginInfo
->pInheritanceInfo
->subpass
];
619 cmd_buffer
->state
.framebuffer
= NULL
;
621 result
= genX(cmd_buffer_setup_attachments
)(cmd_buffer
,
622 cmd_buffer
->state
.pass
, NULL
);
624 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
631 genX(EndCommandBuffer
)(
632 VkCommandBuffer commandBuffer
)
634 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
636 if (anv_batch_has_error(&cmd_buffer
->batch
))
637 return cmd_buffer
->batch
.status
;
639 /* We want every command buffer to start with the PMA fix in a known state,
640 * so we disable it at the end of the command buffer.
642 genX(cmd_buffer_enable_pma_fix
)(cmd_buffer
, false);
644 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
646 anv_cmd_buffer_end_batch_buffer(cmd_buffer
);
652 genX(CmdExecuteCommands
)(
653 VkCommandBuffer commandBuffer
,
654 uint32_t commandBufferCount
,
655 const VkCommandBuffer
* pCmdBuffers
)
657 ANV_FROM_HANDLE(anv_cmd_buffer
, primary
, commandBuffer
);
659 assert(primary
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
661 if (anv_batch_has_error(&primary
->batch
))
664 /* The secondary command buffers will assume that the PMA fix is disabled
665 * when they begin executing. Make sure this is true.
667 genX(cmd_buffer_enable_pma_fix
)(primary
, false);
669 /* The secondary command buffer doesn't know which textures etc. have been
670 * flushed prior to their execution. Apply those flushes now.
672 genX(cmd_buffer_apply_pipe_flushes
)(primary
);
674 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
675 ANV_FROM_HANDLE(anv_cmd_buffer
, secondary
, pCmdBuffers
[i
]);
677 assert(secondary
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
678 assert(!anv_batch_has_error(&secondary
->batch
));
680 if (secondary
->usage_flags
&
681 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
682 /* If we're continuing a render pass from the primary, we need to
683 * copy the surface states for the current subpass into the storage
684 * we allocated for them in BeginCommandBuffer.
686 struct anv_bo
*ss_bo
= &primary
->device
->surface_state_block_pool
.bo
;
687 struct anv_state src_state
= primary
->state
.render_pass_states
;
688 struct anv_state dst_state
= secondary
->state
.render_pass_states
;
689 assert(src_state
.alloc_size
== dst_state
.alloc_size
);
691 genX(cmd_buffer_gpu_memcpy
)(primary
, ss_bo
, dst_state
.offset
,
692 ss_bo
, src_state
.offset
,
693 src_state
.alloc_size
);
696 anv_cmd_buffer_add_secondary(primary
, secondary
);
699 /* Each of the secondary command buffers will use its own state base
700 * address. We need to re-emit state base address for the primary after
701 * all of the secondaries are done.
703 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
706 genX(cmd_buffer_emit_state_base_address
)(primary
);
709 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
710 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
711 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
714 * Program the hardware to use the specified L3 configuration.
717 genX(cmd_buffer_config_l3
)(struct anv_cmd_buffer
*cmd_buffer
,
718 const struct gen_l3_config
*cfg
)
721 if (cfg
== cmd_buffer
->state
.current_l3_config
)
724 if (unlikely(INTEL_DEBUG
& DEBUG_L3
)) {
725 fprintf(stderr
, "L3 config transition: ");
726 gen_dump_l3_config(cfg
, stderr
);
729 const bool has_slm
= cfg
->n
[GEN_L3P_SLM
];
731 /* According to the hardware docs, the L3 partitioning can only be changed
732 * while the pipeline is completely drained and the caches are flushed,
733 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
735 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
736 pc
.DCFlushEnable
= true;
737 pc
.PostSyncOperation
= NoWrite
;
738 pc
.CommandStreamerStallEnable
= true;
741 /* ...followed by a second pipelined PIPE_CONTROL that initiates
742 * invalidation of the relevant caches. Note that because RO invalidation
743 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
744 * command is processed by the CS) we cannot combine it with the previous
745 * stalling flush as the hardware documentation suggests, because that
746 * would cause the CS to stall on previous rendering *after* RO
747 * invalidation and wouldn't prevent the RO caches from being polluted by
748 * concurrent rendering before the stall completes. This intentionally
749 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
750 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
751 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
752 * already guarantee that there is no concurrent GPGPU kernel execution
753 * (see SKL HSD 2132585).
755 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
756 pc
.TextureCacheInvalidationEnable
= true;
757 pc
.ConstantCacheInvalidationEnable
= true;
758 pc
.InstructionCacheInvalidateEnable
= true;
759 pc
.StateCacheInvalidationEnable
= true;
760 pc
.PostSyncOperation
= NoWrite
;
763 /* Now send a third stalling flush to make sure that invalidation is
764 * complete when the L3 configuration registers are modified.
766 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
767 pc
.DCFlushEnable
= true;
768 pc
.PostSyncOperation
= NoWrite
;
769 pc
.CommandStreamerStallEnable
= true;
774 assert(!cfg
->n
[GEN_L3P_IS
] && !cfg
->n
[GEN_L3P_C
] && !cfg
->n
[GEN_L3P_T
]);
777 anv_pack_struct(&l3cr
, GENX(L3CNTLREG
),
778 .SLMEnable
= has_slm
,
779 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
780 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
781 .DCAllocation
= cfg
->n
[GEN_L3P_DC
],
782 .AllAllocation
= cfg
->n
[GEN_L3P_ALL
]);
784 /* Set up the L3 partitioning. */
785 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG_num
), l3cr
);
789 const bool has_dc
= cfg
->n
[GEN_L3P_DC
] || cfg
->n
[GEN_L3P_ALL
];
790 const bool has_is
= cfg
->n
[GEN_L3P_IS
] || cfg
->n
[GEN_L3P_RO
] ||
792 const bool has_c
= cfg
->n
[GEN_L3P_C
] || cfg
->n
[GEN_L3P_RO
] ||
794 const bool has_t
= cfg
->n
[GEN_L3P_T
] || cfg
->n
[GEN_L3P_RO
] ||
797 assert(!cfg
->n
[GEN_L3P_ALL
]);
799 /* When enabled SLM only uses a portion of the L3 on half of the banks,
800 * the matching space on the remaining banks has to be allocated to a
801 * client (URB for all validated configurations) set to the
802 * lower-bandwidth 2-bank address hashing mode.
804 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
805 const bool urb_low_bw
= has_slm
&& !devinfo
->is_baytrail
;
806 assert(!urb_low_bw
|| cfg
->n
[GEN_L3P_URB
] == cfg
->n
[GEN_L3P_SLM
]);
808 /* Minimum number of ways that can be allocated to the URB. */
809 MAYBE_UNUSED
const unsigned n0_urb
= devinfo
->is_baytrail
? 32 : 0;
810 assert(cfg
->n
[GEN_L3P_URB
] >= n0_urb
);
812 uint32_t l3sqcr1
, l3cr2
, l3cr3
;
813 anv_pack_struct(&l3sqcr1
, GENX(L3SQCREG1
),
814 .ConvertDC_UC
= !has_dc
,
815 .ConvertIS_UC
= !has_is
,
816 .ConvertC_UC
= !has_c
,
817 .ConvertT_UC
= !has_t
);
819 GEN_IS_HASWELL
? HSW_L3SQCREG1_SQGHPCI_DEFAULT
:
820 devinfo
->is_baytrail
? VLV_L3SQCREG1_SQGHPCI_DEFAULT
:
821 IVB_L3SQCREG1_SQGHPCI_DEFAULT
;
823 anv_pack_struct(&l3cr2
, GENX(L3CNTLREG2
),
824 .SLMEnable
= has_slm
,
825 .URBLowBandwidth
= urb_low_bw
,
826 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
828 .ALLAllocation
= cfg
->n
[GEN_L3P_ALL
],
830 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
831 .DCAllocation
= cfg
->n
[GEN_L3P_DC
]);
833 anv_pack_struct(&l3cr3
, GENX(L3CNTLREG3
),
834 .ISAllocation
= cfg
->n
[GEN_L3P_IS
],
836 .CAllocation
= cfg
->n
[GEN_L3P_C
],
838 .TAllocation
= cfg
->n
[GEN_L3P_T
],
841 /* Set up the L3 partitioning. */
842 emit_lri(&cmd_buffer
->batch
, GENX(L3SQCREG1_num
), l3sqcr1
);
843 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG2_num
), l3cr2
);
844 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG3_num
), l3cr3
);
847 if (cmd_buffer
->device
->instance
->physicalDevice
.cmd_parser_version
>= 4) {
848 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
849 * them disabled to avoid crashing the system hard.
851 uint32_t scratch1
, chicken3
;
852 anv_pack_struct(&scratch1
, GENX(SCRATCH1
),
853 .L3AtomicDisable
= !has_dc
);
854 anv_pack_struct(&chicken3
, GENX(CHICKEN3
),
855 .L3AtomicDisableMask
= true,
856 .L3AtomicDisable
= !has_dc
);
857 emit_lri(&cmd_buffer
->batch
, GENX(SCRATCH1_num
), scratch1
);
858 emit_lri(&cmd_buffer
->batch
, GENX(CHICKEN3_num
), chicken3
);
864 cmd_buffer
->state
.current_l3_config
= cfg
;
868 genX(cmd_buffer_apply_pipe_flushes
)(struct anv_cmd_buffer
*cmd_buffer
)
870 enum anv_pipe_bits bits
= cmd_buffer
->state
.pending_pipe_bits
;
872 /* Flushes are pipelined while invalidations are handled immediately.
873 * Therefore, if we're flushing anything then we need to schedule a stall
874 * before any invalidations can happen.
876 if (bits
& ANV_PIPE_FLUSH_BITS
)
877 bits
|= ANV_PIPE_NEEDS_CS_STALL_BIT
;
879 /* If we're going to do an invalidate and we have a pending CS stall that
880 * has yet to be resolved, we do the CS stall now.
882 if ((bits
& ANV_PIPE_INVALIDATE_BITS
) &&
883 (bits
& ANV_PIPE_NEEDS_CS_STALL_BIT
)) {
884 bits
|= ANV_PIPE_CS_STALL_BIT
;
885 bits
&= ~ANV_PIPE_NEEDS_CS_STALL_BIT
;
888 if (bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
)) {
889 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
890 pipe
.DepthCacheFlushEnable
= bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
891 pipe
.DCFlushEnable
= bits
& ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
892 pipe
.RenderTargetCacheFlushEnable
=
893 bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
895 pipe
.DepthStallEnable
= bits
& ANV_PIPE_DEPTH_STALL_BIT
;
896 pipe
.CommandStreamerStallEnable
= bits
& ANV_PIPE_CS_STALL_BIT
;
897 pipe
.StallAtPixelScoreboard
= bits
& ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
900 * According to the Broadwell documentation, any PIPE_CONTROL with the
901 * "Command Streamer Stall" bit set must also have another bit set,
902 * with five different options:
904 * - Render Target Cache Flush
905 * - Depth Cache Flush
906 * - Stall at Pixel Scoreboard
907 * - Post-Sync Operation
911 * I chose "Stall at Pixel Scoreboard" since that's what we use in
912 * mesa and it seems to work fine. The choice is fairly arbitrary.
914 if ((bits
& ANV_PIPE_CS_STALL_BIT
) &&
915 !(bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_DEPTH_STALL_BIT
|
916 ANV_PIPE_STALL_AT_SCOREBOARD_BIT
)))
917 pipe
.StallAtPixelScoreboard
= true;
920 bits
&= ~(ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
);
923 if (bits
& ANV_PIPE_INVALIDATE_BITS
) {
924 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
925 pipe
.StateCacheInvalidationEnable
=
926 bits
& ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
927 pipe
.ConstantCacheInvalidationEnable
=
928 bits
& ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
929 pipe
.VFCacheInvalidationEnable
=
930 bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
931 pipe
.TextureCacheInvalidationEnable
=
932 bits
& ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
933 pipe
.InstructionCacheInvalidateEnable
=
934 bits
& ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT
;
937 bits
&= ~ANV_PIPE_INVALIDATE_BITS
;
940 cmd_buffer
->state
.pending_pipe_bits
= bits
;
943 void genX(CmdPipelineBarrier
)(
944 VkCommandBuffer commandBuffer
,
945 VkPipelineStageFlags srcStageMask
,
946 VkPipelineStageFlags destStageMask
,
948 uint32_t memoryBarrierCount
,
949 const VkMemoryBarrier
* pMemoryBarriers
,
950 uint32_t bufferMemoryBarrierCount
,
951 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
952 uint32_t imageMemoryBarrierCount
,
953 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
955 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
957 /* XXX: Right now, we're really dumb and just flush whatever categories
958 * the app asks for. One of these days we may make this a bit better
959 * but right now that's all the hardware allows for in most areas.
961 VkAccessFlags src_flags
= 0;
962 VkAccessFlags dst_flags
= 0;
964 for (uint32_t i
= 0; i
< memoryBarrierCount
; i
++) {
965 src_flags
|= pMemoryBarriers
[i
].srcAccessMask
;
966 dst_flags
|= pMemoryBarriers
[i
].dstAccessMask
;
969 for (uint32_t i
= 0; i
< bufferMemoryBarrierCount
; i
++) {
970 src_flags
|= pBufferMemoryBarriers
[i
].srcAccessMask
;
971 dst_flags
|= pBufferMemoryBarriers
[i
].dstAccessMask
;
974 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
975 src_flags
|= pImageMemoryBarriers
[i
].srcAccessMask
;
976 dst_flags
|= pImageMemoryBarriers
[i
].dstAccessMask
;
977 ANV_FROM_HANDLE(anv_image
, image
, pImageMemoryBarriers
[i
].image
);
978 if (pImageMemoryBarriers
[i
].subresourceRange
.aspectMask
&
979 VK_IMAGE_ASPECT_DEPTH_BIT
) {
980 transition_depth_buffer(cmd_buffer
, image
,
981 pImageMemoryBarriers
[i
].oldLayout
,
982 pImageMemoryBarriers
[i
].newLayout
);
986 cmd_buffer
->state
.pending_pipe_bits
|=
987 anv_pipe_flush_bits_for_access_flags(src_flags
) |
988 anv_pipe_invalidate_bits_for_access_flags(dst_flags
);
992 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
994 VkShaderStageFlags stages
= cmd_buffer
->state
.pipeline
->active_stages
;
996 /* In order to avoid thrash, we assume that vertex and fragment stages
997 * always exist. In the rare case where one is missing *and* the other
998 * uses push concstants, this may be suboptimal. However, avoiding stalls
999 * seems more important.
1001 stages
|= VK_SHADER_STAGE_FRAGMENT_BIT
| VK_SHADER_STAGE_VERTEX_BIT
;
1003 if (stages
== cmd_buffer
->state
.push_constant_stages
)
1007 const unsigned push_constant_kb
= 32;
1008 #elif GEN_IS_HASWELL
1009 const unsigned push_constant_kb
= cmd_buffer
->device
->info
.gt
== 3 ? 32 : 16;
1011 const unsigned push_constant_kb
= 16;
1014 const unsigned num_stages
=
1015 _mesa_bitcount(stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
1016 unsigned size_per_stage
= push_constant_kb
/ num_stages
;
1018 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
1019 * units of 2KB. Incidentally, these are the same platforms that have
1020 * 32KB worth of push constant space.
1022 if (push_constant_kb
== 32)
1023 size_per_stage
&= ~1u;
1025 uint32_t kb_used
= 0;
1026 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
1027 unsigned push_size
= (stages
& (1 << i
)) ? size_per_stage
: 0;
1028 anv_batch_emit(&cmd_buffer
->batch
,
1029 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS
), alloc
) {
1030 alloc
._3DCommandSubOpcode
= 18 + i
;
1031 alloc
.ConstantBufferOffset
= (push_size
> 0) ? kb_used
: 0;
1032 alloc
.ConstantBufferSize
= push_size
;
1034 kb_used
+= push_size
;
1037 anv_batch_emit(&cmd_buffer
->batch
,
1038 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS
), alloc
) {
1039 alloc
.ConstantBufferOffset
= kb_used
;
1040 alloc
.ConstantBufferSize
= push_constant_kb
- kb_used
;
1043 cmd_buffer
->state
.push_constant_stages
= stages
;
1045 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
1047 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
1048 * the next 3DPRIMITIVE command after programming the
1049 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
1051 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
1052 * pipeline setup, we need to dirty push constants.
1054 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
1058 emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
1059 gl_shader_stage stage
,
1060 struct anv_state
*bt_state
)
1062 struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1063 struct anv_pipeline
*pipeline
;
1064 uint32_t bias
, state_offset
;
1067 case MESA_SHADER_COMPUTE
:
1068 pipeline
= cmd_buffer
->state
.compute_pipeline
;
1072 pipeline
= cmd_buffer
->state
.pipeline
;
1077 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
1078 *bt_state
= (struct anv_state
) { 0, };
1082 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
1083 if (bias
+ map
->surface_count
== 0) {
1084 *bt_state
= (struct anv_state
) { 0, };
1088 *bt_state
= anv_cmd_buffer_alloc_binding_table(cmd_buffer
,
1089 bias
+ map
->surface_count
,
1091 uint32_t *bt_map
= bt_state
->map
;
1093 if (bt_state
->map
== NULL
)
1094 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1096 if (stage
== MESA_SHADER_COMPUTE
&&
1097 get_cs_prog_data(cmd_buffer
->state
.compute_pipeline
)->uses_num_work_groups
) {
1098 struct anv_bo
*bo
= cmd_buffer
->state
.num_workgroups_bo
;
1099 uint32_t bo_offset
= cmd_buffer
->state
.num_workgroups_offset
;
1101 struct anv_state surface_state
;
1103 anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
1105 const enum isl_format format
=
1106 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
);
1107 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
1108 format
, bo_offset
, 12, 1);
1110 bt_map
[0] = surface_state
.offset
+ state_offset
;
1111 add_surface_state_reloc(cmd_buffer
, surface_state
, bo
, bo_offset
);
1114 if (map
->surface_count
== 0)
1117 if (map
->image_count
> 0) {
1119 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, stage
, images
);
1120 if (result
!= VK_SUCCESS
)
1123 cmd_buffer
->state
.push_constants_dirty
|= 1 << stage
;
1127 for (uint32_t s
= 0; s
< map
->surface_count
; s
++) {
1128 struct anv_pipeline_binding
*binding
= &map
->surface_to_descriptor
[s
];
1130 struct anv_state surface_state
;
1132 if (binding
->set
== ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
) {
1133 /* Color attachment binding */
1134 assert(stage
== MESA_SHADER_FRAGMENT
);
1135 assert(binding
->binding
== 0);
1136 if (binding
->index
< subpass
->color_count
) {
1137 const unsigned att
= subpass
->color_attachments
[binding
->index
].attachment
;
1138 surface_state
= cmd_buffer
->state
.attachments
[att
].color_rt_state
;
1140 surface_state
= cmd_buffer
->state
.null_surface_state
;
1143 bt_map
[bias
+ s
] = surface_state
.offset
+ state_offset
;
1147 struct anv_descriptor_set
*set
=
1148 cmd_buffer
->state
.descriptors
[binding
->set
];
1149 uint32_t offset
= set
->layout
->binding
[binding
->binding
].descriptor_index
;
1150 struct anv_descriptor
*desc
= &set
->descriptors
[offset
+ binding
->index
];
1152 switch (desc
->type
) {
1153 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1154 /* Nothing for us to do here */
1157 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1158 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1159 surface_state
= desc
->aux_usage
== ISL_AUX_USAGE_NONE
?
1160 desc
->image_view
->no_aux_sampler_surface_state
:
1161 desc
->image_view
->sampler_surface_state
;
1162 assert(surface_state
.alloc_size
);
1163 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1164 desc
->aux_usage
, surface_state
);
1166 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1167 assert(stage
== MESA_SHADER_FRAGMENT
);
1168 if (desc
->image_view
->aspect_mask
!= VK_IMAGE_ASPECT_COLOR_BIT
) {
1169 /* For depth and stencil input attachments, we treat it like any
1170 * old texture that a user may have bound.
1172 surface_state
= desc
->aux_usage
== ISL_AUX_USAGE_NONE
?
1173 desc
->image_view
->no_aux_sampler_surface_state
:
1174 desc
->image_view
->sampler_surface_state
;
1175 assert(surface_state
.alloc_size
);
1176 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1177 desc
->aux_usage
, surface_state
);
1179 /* For color input attachments, we create the surface state at
1180 * vkBeginRenderPass time so that we can include aux and clear
1181 * color information.
1183 assert(binding
->input_attachment_index
< subpass
->input_count
);
1184 const unsigned subpass_att
= binding
->input_attachment_index
;
1185 const unsigned att
= subpass
->input_attachments
[subpass_att
].attachment
;
1186 surface_state
= cmd_buffer
->state
.attachments
[att
].input_att_state
;
1190 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
: {
1191 surface_state
= (binding
->write_only
)
1192 ? desc
->image_view
->writeonly_storage_surface_state
1193 : desc
->image_view
->storage_surface_state
;
1194 assert(surface_state
.alloc_size
);
1195 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1196 desc
->image_view
->image
->aux_usage
,
1199 struct brw_image_param
*image_param
=
1200 &cmd_buffer
->state
.push_constants
[stage
]->images
[image
++];
1202 *image_param
= desc
->image_view
->storage_image_param
;
1203 image_param
->surface_idx
= bias
+ s
;
1207 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1208 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1209 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1210 surface_state
= desc
->buffer_view
->surface_state
;
1211 assert(surface_state
.alloc_size
);
1212 add_surface_state_reloc(cmd_buffer
, surface_state
,
1213 desc
->buffer_view
->bo
,
1214 desc
->buffer_view
->offset
);
1217 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1218 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1219 uint32_t dynamic_offset_idx
=
1220 pipeline
->layout
->set
[binding
->set
].dynamic_offset_start
+
1221 set
->layout
->binding
[binding
->binding
].dynamic_offset_index
+
1224 /* Compute the offset within the buffer */
1225 uint64_t offset
= desc
->offset
+
1226 cmd_buffer
->state
.dynamic_offsets
[dynamic_offset_idx
];
1227 /* Clamp to the buffer size */
1228 offset
= MIN2(offset
, desc
->buffer
->size
);
1229 /* Clamp the range to the buffer size */
1230 uint32_t range
= MIN2(desc
->range
, desc
->buffer
->size
- offset
);
1233 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, 64, 64);
1234 enum isl_format format
=
1235 anv_isl_format_for_descriptor_type(desc
->type
);
1237 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
1238 format
, offset
, range
, 1);
1239 add_surface_state_reloc(cmd_buffer
, surface_state
,
1241 desc
->buffer
->offset
+ offset
);
1245 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1246 surface_state
= (binding
->write_only
)
1247 ? desc
->buffer_view
->writeonly_storage_surface_state
1248 : desc
->buffer_view
->storage_surface_state
;
1249 assert(surface_state
.alloc_size
);
1250 add_surface_state_reloc(cmd_buffer
, surface_state
,
1251 desc
->buffer_view
->bo
,
1252 desc
->buffer_view
->offset
);
1254 struct brw_image_param
*image_param
=
1255 &cmd_buffer
->state
.push_constants
[stage
]->images
[image
++];
1257 *image_param
= desc
->buffer_view
->storage_image_param
;
1258 image_param
->surface_idx
= bias
+ s
;
1262 assert(!"Invalid descriptor type");
1266 bt_map
[bias
+ s
] = surface_state
.offset
+ state_offset
;
1268 assert(image
== map
->image_count
);
1271 anv_state_flush(cmd_buffer
->device
, *bt_state
);
1277 emit_samplers(struct anv_cmd_buffer
*cmd_buffer
,
1278 gl_shader_stage stage
,
1279 struct anv_state
*state
)
1281 struct anv_pipeline
*pipeline
;
1283 if (stage
== MESA_SHADER_COMPUTE
)
1284 pipeline
= cmd_buffer
->state
.compute_pipeline
;
1286 pipeline
= cmd_buffer
->state
.pipeline
;
1288 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
1289 *state
= (struct anv_state
) { 0, };
1293 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
1294 if (map
->sampler_count
== 0) {
1295 *state
= (struct anv_state
) { 0, };
1299 uint32_t size
= map
->sampler_count
* 16;
1300 *state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 32);
1302 if (state
->map
== NULL
)
1303 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1305 for (uint32_t s
= 0; s
< map
->sampler_count
; s
++) {
1306 struct anv_pipeline_binding
*binding
= &map
->sampler_to_descriptor
[s
];
1307 struct anv_descriptor_set
*set
=
1308 cmd_buffer
->state
.descriptors
[binding
->set
];
1309 uint32_t offset
= set
->layout
->binding
[binding
->binding
].descriptor_index
;
1310 struct anv_descriptor
*desc
= &set
->descriptors
[offset
+ binding
->index
];
1312 if (desc
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
1313 desc
->type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
1316 struct anv_sampler
*sampler
= desc
->sampler
;
1318 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
1319 * happens to be zero.
1321 if (sampler
== NULL
)
1324 memcpy(state
->map
+ (s
* 16),
1325 sampler
->state
, sizeof(sampler
->state
));
1328 anv_state_flush(cmd_buffer
->device
, *state
);
1334 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
1336 VkShaderStageFlags dirty
= cmd_buffer
->state
.descriptors_dirty
&
1337 cmd_buffer
->state
.pipeline
->active_stages
;
1339 VkResult result
= VK_SUCCESS
;
1340 anv_foreach_stage(s
, dirty
) {
1341 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
1342 if (result
!= VK_SUCCESS
)
1344 result
= emit_binding_table(cmd_buffer
, s
,
1345 &cmd_buffer
->state
.binding_tables
[s
]);
1346 if (result
!= VK_SUCCESS
)
1350 if (result
!= VK_SUCCESS
) {
1351 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1353 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
1354 if (result
!= VK_SUCCESS
)
1357 /* Re-emit state base addresses so we get the new surface state base
1358 * address before we start emitting binding tables etc.
1360 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1362 /* Re-emit all active binding tables */
1363 dirty
|= cmd_buffer
->state
.pipeline
->active_stages
;
1364 anv_foreach_stage(s
, dirty
) {
1365 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
1366 if (result
!= VK_SUCCESS
) {
1367 anv_batch_set_error(&cmd_buffer
->batch
, result
);
1370 result
= emit_binding_table(cmd_buffer
, s
,
1371 &cmd_buffer
->state
.binding_tables
[s
]);
1372 if (result
!= VK_SUCCESS
) {
1373 anv_batch_set_error(&cmd_buffer
->batch
, result
);
1379 cmd_buffer
->state
.descriptors_dirty
&= ~dirty
;
1385 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
1388 static const uint32_t sampler_state_opcodes
[] = {
1389 [MESA_SHADER_VERTEX
] = 43,
1390 [MESA_SHADER_TESS_CTRL
] = 44, /* HS */
1391 [MESA_SHADER_TESS_EVAL
] = 45, /* DS */
1392 [MESA_SHADER_GEOMETRY
] = 46,
1393 [MESA_SHADER_FRAGMENT
] = 47,
1394 [MESA_SHADER_COMPUTE
] = 0,
1397 static const uint32_t binding_table_opcodes
[] = {
1398 [MESA_SHADER_VERTEX
] = 38,
1399 [MESA_SHADER_TESS_CTRL
] = 39,
1400 [MESA_SHADER_TESS_EVAL
] = 40,
1401 [MESA_SHADER_GEOMETRY
] = 41,
1402 [MESA_SHADER_FRAGMENT
] = 42,
1403 [MESA_SHADER_COMPUTE
] = 0,
1406 anv_foreach_stage(s
, stages
) {
1407 if (cmd_buffer
->state
.samplers
[s
].alloc_size
> 0) {
1408 anv_batch_emit(&cmd_buffer
->batch
,
1409 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS
), ssp
) {
1410 ssp
._3DCommandSubOpcode
= sampler_state_opcodes
[s
];
1411 ssp
.PointertoVSSamplerState
= cmd_buffer
->state
.samplers
[s
].offset
;
1415 /* Always emit binding table pointers if we're asked to, since on SKL
1416 * this is what flushes push constants. */
1417 anv_batch_emit(&cmd_buffer
->batch
,
1418 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS
), btp
) {
1419 btp
._3DCommandSubOpcode
= binding_table_opcodes
[s
];
1420 btp
.PointertoVSBindingTable
= cmd_buffer
->state
.binding_tables
[s
].offset
;
1426 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
1428 static const uint32_t push_constant_opcodes
[] = {
1429 [MESA_SHADER_VERTEX
] = 21,
1430 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
1431 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
1432 [MESA_SHADER_GEOMETRY
] = 22,
1433 [MESA_SHADER_FRAGMENT
] = 23,
1434 [MESA_SHADER_COMPUTE
] = 0,
1437 VkShaderStageFlags flushed
= 0;
1439 anv_foreach_stage(stage
, cmd_buffer
->state
.push_constants_dirty
) {
1440 if (stage
== MESA_SHADER_COMPUTE
)
1443 struct anv_state state
= anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
1445 if (state
.offset
== 0) {
1446 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
)
1447 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
];
1449 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
) {
1450 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
],
1451 c
.ConstantBody
= (struct GENX(3DSTATE_CONSTANT_BODY
)) {
1453 .PointerToConstantBuffer2
= { &cmd_buffer
->device
->dynamic_state_block_pool
.bo
, state
.offset
},
1454 .ConstantBuffer2ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
1456 .PointerToConstantBuffer0
= { .offset
= state
.offset
},
1457 .ConstantBuffer0ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
1463 flushed
|= mesa_to_vk_shader_stage(stage
);
1466 cmd_buffer
->state
.push_constants_dirty
&= ~VK_SHADER_STAGE_ALL_GRAPHICS
;
1472 genX(cmd_buffer_flush_state
)(struct anv_cmd_buffer
*cmd_buffer
)
1474 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1477 uint32_t vb_emit
= cmd_buffer
->state
.vb_dirty
& pipeline
->vb_used
;
1479 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
1481 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
1483 genX(flush_pipeline_select_3d
)(cmd_buffer
);
1486 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
1487 const uint32_t num_dwords
= 1 + num_buffers
* 4;
1489 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
1490 GENX(3DSTATE_VERTEX_BUFFERS
));
1492 for_each_bit(vb
, vb_emit
) {
1493 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
1494 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
1496 struct GENX(VERTEX_BUFFER_STATE
) state
= {
1497 .VertexBufferIndex
= vb
,
1500 .MemoryObjectControlState
= GENX(MOCS
),
1502 .BufferAccessType
= pipeline
->instancing_enable
[vb
] ? INSTANCEDATA
: VERTEXDATA
,
1503 .InstanceDataStepRate
= 1,
1504 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
1507 .AddressModifyEnable
= true,
1508 .BufferPitch
= pipeline
->binding_stride
[vb
],
1509 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
1512 .BufferSize
= buffer
->size
- offset
1514 .EndAddress
= { buffer
->bo
, buffer
->offset
+ buffer
->size
- 1},
1518 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
1523 cmd_buffer
->state
.vb_dirty
&= ~vb_emit
;
1525 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
1526 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
1528 /* The exact descriptor layout is pulled from the pipeline, so we need
1529 * to re-emit binding tables on every pipeline change.
1531 cmd_buffer
->state
.descriptors_dirty
|=
1532 cmd_buffer
->state
.pipeline
->active_stages
;
1534 /* If the pipeline changed, we may need to re-allocate push constant
1537 cmd_buffer_alloc_push_constants(cmd_buffer
);
1541 if (cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_VERTEX_BIT
||
1542 cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_VERTEX_BIT
) {
1543 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
1545 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
1546 * stall needs to be sent just prior to any 3DSTATE_VS,
1547 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
1548 * 3DSTATE_BINDING_TABLE_POINTER_VS,
1549 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
1550 * PIPE_CONTROL needs to be sent before any combination of VS
1551 * associated 3DSTATE."
1553 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1554 pc
.DepthStallEnable
= true;
1555 pc
.PostSyncOperation
= WriteImmediateData
;
1557 (struct anv_address
) { &cmd_buffer
->device
->workaround_bo
, 0 };
1562 /* Render targets live in the same binding table as fragment descriptors */
1563 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_RENDER_TARGETS
)
1564 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
1566 /* We emit the binding tables and sampler tables first, then emit push
1567 * constants and then finally emit binding table and sampler table
1568 * pointers. It has to happen in this order, since emitting the binding
1569 * tables may change the push constants (in case of storage images). After
1570 * emitting push constants, on SKL+ we have to emit the corresponding
1571 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
1574 if (cmd_buffer
->state
.descriptors_dirty
)
1575 dirty
= flush_descriptor_sets(cmd_buffer
);
1577 if (cmd_buffer
->state
.push_constants_dirty
) {
1579 /* On Sky Lake and later, the binding table pointers commands are
1580 * what actually flush the changes to push constant state so we need
1581 * to dirty them so they get re-emitted below.
1583 dirty
|= cmd_buffer_flush_push_constants(cmd_buffer
);
1585 cmd_buffer_flush_push_constants(cmd_buffer
);
1590 cmd_buffer_emit_descriptor_pointers(cmd_buffer
, dirty
);
1592 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
1593 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
1595 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
|
1596 ANV_CMD_DIRTY_PIPELINE
)) {
1597 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer
,
1598 pipeline
->depth_clamp_enable
);
1601 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
)
1602 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
1604 genX(cmd_buffer_flush_dynamic_state
)(cmd_buffer
);
1606 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1610 emit_vertex_bo(struct anv_cmd_buffer
*cmd_buffer
,
1611 struct anv_bo
*bo
, uint32_t offset
,
1612 uint32_t size
, uint32_t index
)
1614 uint32_t *p
= anv_batch_emitn(&cmd_buffer
->batch
, 5,
1615 GENX(3DSTATE_VERTEX_BUFFERS
));
1617 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, p
+ 1,
1618 &(struct GENX(VERTEX_BUFFER_STATE
)) {
1619 .VertexBufferIndex
= index
,
1620 .AddressModifyEnable
= true,
1623 .MemoryObjectControlState
= GENX(MOCS
),
1624 .BufferStartingAddress
= { bo
, offset
},
1627 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
1628 .BufferStartingAddress
= { bo
, offset
},
1629 .EndAddress
= { bo
, offset
+ size
},
1635 emit_base_vertex_instance_bo(struct anv_cmd_buffer
*cmd_buffer
,
1636 struct anv_bo
*bo
, uint32_t offset
)
1638 emit_vertex_bo(cmd_buffer
, bo
, offset
, 8, ANV_SVGS_VB_INDEX
);
1642 emit_base_vertex_instance(struct anv_cmd_buffer
*cmd_buffer
,
1643 uint32_t base_vertex
, uint32_t base_instance
)
1645 struct anv_state id_state
=
1646 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 8, 4);
1648 ((uint32_t *)id_state
.map
)[0] = base_vertex
;
1649 ((uint32_t *)id_state
.map
)[1] = base_instance
;
1651 anv_state_flush(cmd_buffer
->device
, id_state
);
1653 emit_base_vertex_instance_bo(cmd_buffer
,
1654 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
, id_state
.offset
);
1658 emit_draw_index(struct anv_cmd_buffer
*cmd_buffer
, uint32_t draw_index
)
1660 struct anv_state state
=
1661 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 4, 4);
1663 ((uint32_t *)state
.map
)[0] = draw_index
;
1665 anv_state_flush(cmd_buffer
->device
, state
);
1667 emit_vertex_bo(cmd_buffer
,
1668 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
,
1669 state
.offset
, 4, ANV_DRAWID_VB_INDEX
);
1673 VkCommandBuffer commandBuffer
,
1674 uint32_t vertexCount
,
1675 uint32_t instanceCount
,
1676 uint32_t firstVertex
,
1677 uint32_t firstInstance
)
1679 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1680 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1681 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1683 if (anv_batch_has_error(&cmd_buffer
->batch
))
1686 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1688 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1689 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
1690 if (vs_prog_data
->uses_drawid
)
1691 emit_draw_index(cmd_buffer
, 0);
1693 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1694 prim
.VertexAccessType
= SEQUENTIAL
;
1695 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1696 prim
.VertexCountPerInstance
= vertexCount
;
1697 prim
.StartVertexLocation
= firstVertex
;
1698 prim
.InstanceCount
= instanceCount
;
1699 prim
.StartInstanceLocation
= firstInstance
;
1700 prim
.BaseVertexLocation
= 0;
1704 void genX(CmdDrawIndexed
)(
1705 VkCommandBuffer commandBuffer
,
1706 uint32_t indexCount
,
1707 uint32_t instanceCount
,
1708 uint32_t firstIndex
,
1709 int32_t vertexOffset
,
1710 uint32_t firstInstance
)
1712 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1713 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1714 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1716 if (anv_batch_has_error(&cmd_buffer
->batch
))
1719 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1721 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1722 emit_base_vertex_instance(cmd_buffer
, vertexOffset
, firstInstance
);
1723 if (vs_prog_data
->uses_drawid
)
1724 emit_draw_index(cmd_buffer
, 0);
1726 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1727 prim
.VertexAccessType
= RANDOM
;
1728 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1729 prim
.VertexCountPerInstance
= indexCount
;
1730 prim
.StartVertexLocation
= firstIndex
;
1731 prim
.InstanceCount
= instanceCount
;
1732 prim
.StartInstanceLocation
= firstInstance
;
1733 prim
.BaseVertexLocation
= vertexOffset
;
1737 /* Auto-Draw / Indirect Registers */
1738 #define GEN7_3DPRIM_END_OFFSET 0x2420
1739 #define GEN7_3DPRIM_START_VERTEX 0x2430
1740 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
1741 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
1742 #define GEN7_3DPRIM_START_INSTANCE 0x243C
1743 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
1745 void genX(CmdDrawIndirect
)(
1746 VkCommandBuffer commandBuffer
,
1748 VkDeviceSize offset
,
1752 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1753 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1754 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1755 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1756 struct anv_bo
*bo
= buffer
->bo
;
1757 uint32_t bo_offset
= buffer
->offset
+ offset
;
1759 if (anv_batch_has_error(&cmd_buffer
->batch
))
1762 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1764 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1765 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 8);
1766 if (vs_prog_data
->uses_drawid
)
1767 emit_draw_index(cmd_buffer
, 0);
1769 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
1770 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
1771 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
1772 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
1773 emit_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
1775 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1776 prim
.IndirectParameterEnable
= true;
1777 prim
.VertexAccessType
= SEQUENTIAL
;
1778 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1782 void genX(CmdDrawIndexedIndirect
)(
1783 VkCommandBuffer commandBuffer
,
1785 VkDeviceSize offset
,
1789 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1790 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1791 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1792 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1793 struct anv_bo
*bo
= buffer
->bo
;
1794 uint32_t bo_offset
= buffer
->offset
+ offset
;
1796 if (anv_batch_has_error(&cmd_buffer
->batch
))
1799 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1801 /* TODO: We need to stomp base vertex to 0 somehow */
1802 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1803 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 12);
1804 if (vs_prog_data
->uses_drawid
)
1805 emit_draw_index(cmd_buffer
, 0);
1807 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
1808 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
1809 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
1810 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
1811 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
1813 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1814 prim
.IndirectParameterEnable
= true;
1815 prim
.VertexAccessType
= RANDOM
;
1816 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1821 flush_compute_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
)
1823 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1824 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
1827 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
1828 if (result
!= VK_SUCCESS
) {
1829 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1831 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
1832 if (result
!= VK_SUCCESS
)
1835 /* Re-emit state base addresses so we get the new surface state base
1836 * address before we start emitting binding tables etc.
1838 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1840 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
1841 if (result
!= VK_SUCCESS
) {
1842 anv_batch_set_error(&cmd_buffer
->batch
, result
);
1847 result
= emit_samplers(cmd_buffer
, MESA_SHADER_COMPUTE
, &samplers
);
1848 if (result
!= VK_SUCCESS
) {
1849 anv_batch_set_error(&cmd_buffer
->batch
, result
);
1853 uint32_t iface_desc_data_dw
[GENX(INTERFACE_DESCRIPTOR_DATA_length
)];
1854 struct GENX(INTERFACE_DESCRIPTOR_DATA
) desc
= {
1855 .BindingTablePointer
= surfaces
.offset
,
1856 .SamplerStatePointer
= samplers
.offset
,
1858 GENX(INTERFACE_DESCRIPTOR_DATA_pack
)(NULL
, iface_desc_data_dw
, &desc
);
1860 struct anv_state state
=
1861 anv_cmd_buffer_merge_dynamic(cmd_buffer
, iface_desc_data_dw
,
1862 pipeline
->interface_descriptor_data
,
1863 GENX(INTERFACE_DESCRIPTOR_DATA_length
),
1866 uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
1867 anv_batch_emit(&cmd_buffer
->batch
,
1868 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
), mid
) {
1869 mid
.InterfaceDescriptorTotalLength
= size
;
1870 mid
.InterfaceDescriptorDataStartAddress
= state
.offset
;
1877 genX(cmd_buffer_flush_compute_state
)(struct anv_cmd_buffer
*cmd_buffer
)
1879 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1880 MAYBE_UNUSED VkResult result
;
1882 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
1884 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
1886 genX(flush_pipeline_select_gpgpu
)(cmd_buffer
);
1888 if (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
) {
1889 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
1891 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
1892 * the only bits that are changed are scoreboard related: Scoreboard
1893 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
1894 * these scoreboard related states, a MEDIA_STATE_FLUSH is
1897 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
1898 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1900 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
1903 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
1904 (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)) {
1905 /* FIXME: figure out descriptors for gen7 */
1906 result
= flush_compute_descriptor_set(cmd_buffer
);
1907 if (result
!= VK_SUCCESS
)
1910 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
1913 if (cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) {
1914 struct anv_state push_state
=
1915 anv_cmd_buffer_cs_push_constants(cmd_buffer
);
1917 if (push_state
.alloc_size
) {
1918 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
), curbe
) {
1919 curbe
.CURBETotalDataLength
= push_state
.alloc_size
;
1920 curbe
.CURBEDataStartAddress
= push_state
.offset
;
1925 cmd_buffer
->state
.compute_dirty
= 0;
1927 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1933 verify_cmd_parser(const struct anv_device
*device
,
1934 int required_version
,
1935 const char *function
)
1937 if (device
->instance
->physicalDevice
.cmd_parser_version
< required_version
) {
1938 return vk_errorf(VK_ERROR_FEATURE_NOT_PRESENT
,
1939 "cmd parser version %d is required for %s",
1940 required_version
, function
);
1948 void genX(CmdDispatch
)(
1949 VkCommandBuffer commandBuffer
,
1954 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1955 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1956 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
1958 if (anv_batch_has_error(&cmd_buffer
->batch
))
1961 if (prog_data
->uses_num_work_groups
) {
1962 struct anv_state state
=
1963 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 12, 4);
1964 uint32_t *sizes
= state
.map
;
1968 anv_state_flush(cmd_buffer
->device
, state
);
1969 cmd_buffer
->state
.num_workgroups_offset
= state
.offset
;
1970 cmd_buffer
->state
.num_workgroups_bo
=
1971 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
;
1974 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
1976 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
), ggw
) {
1977 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
1978 ggw
.ThreadDepthCounterMaximum
= 0;
1979 ggw
.ThreadHeightCounterMaximum
= 0;
1980 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
1981 ggw
.ThreadGroupIDXDimension
= x
;
1982 ggw
.ThreadGroupIDYDimension
= y
;
1983 ggw
.ThreadGroupIDZDimension
= z
;
1984 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
1985 ggw
.BottomExecutionMask
= 0xffffffff;
1988 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
1991 #define GPGPU_DISPATCHDIMX 0x2500
1992 #define GPGPU_DISPATCHDIMY 0x2504
1993 #define GPGPU_DISPATCHDIMZ 0x2508
1995 #define MI_PREDICATE_SRC0 0x2400
1996 #define MI_PREDICATE_SRC1 0x2408
1998 void genX(CmdDispatchIndirect
)(
1999 VkCommandBuffer commandBuffer
,
2001 VkDeviceSize offset
)
2003 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2004 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2005 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
2006 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
2007 struct anv_bo
*bo
= buffer
->bo
;
2008 uint32_t bo_offset
= buffer
->offset
+ offset
;
2009 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2012 /* Linux 4.4 added command parser version 5 which allows the GPGPU
2013 * indirect dispatch registers to be written.
2015 if (verify_cmd_parser(cmd_buffer
->device
, 5,
2016 "vkCmdDispatchIndirect") != VK_SUCCESS
)
2020 if (prog_data
->uses_num_work_groups
) {
2021 cmd_buffer
->state
.num_workgroups_offset
= bo_offset
;
2022 cmd_buffer
->state
.num_workgroups_bo
= bo
;
2025 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
2027 emit_lrm(batch
, GPGPU_DISPATCHDIMX
, bo
, bo_offset
);
2028 emit_lrm(batch
, GPGPU_DISPATCHDIMY
, bo
, bo_offset
+ 4);
2029 emit_lrm(batch
, GPGPU_DISPATCHDIMZ
, bo
, bo_offset
+ 8);
2032 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
2033 emit_lri(batch
, MI_PREDICATE_SRC0
+ 4, 0);
2034 emit_lri(batch
, MI_PREDICATE_SRC1
+ 0, 0);
2035 emit_lri(batch
, MI_PREDICATE_SRC1
+ 4, 0);
2037 /* Load compute_dispatch_indirect_x_size into SRC0 */
2038 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 0);
2040 /* predicate = (compute_dispatch_indirect_x_size == 0); */
2041 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
2042 mip
.LoadOperation
= LOAD_LOAD
;
2043 mip
.CombineOperation
= COMBINE_SET
;
2044 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
2047 /* Load compute_dispatch_indirect_y_size into SRC0 */
2048 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 4);
2050 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
2051 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
2052 mip
.LoadOperation
= LOAD_LOAD
;
2053 mip
.CombineOperation
= COMBINE_OR
;
2054 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
2057 /* Load compute_dispatch_indirect_z_size into SRC0 */
2058 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 8);
2060 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
2061 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
2062 mip
.LoadOperation
= LOAD_LOAD
;
2063 mip
.CombineOperation
= COMBINE_OR
;
2064 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
2067 /* predicate = !predicate; */
2068 #define COMPARE_FALSE 1
2069 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
2070 mip
.LoadOperation
= LOAD_LOADINV
;
2071 mip
.CombineOperation
= COMBINE_OR
;
2072 mip
.CompareOperation
= COMPARE_FALSE
;
2076 anv_batch_emit(batch
, GENX(GPGPU_WALKER
), ggw
) {
2077 ggw
.IndirectParameterEnable
= true;
2078 ggw
.PredicateEnable
= GEN_GEN
<= 7;
2079 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
2080 ggw
.ThreadDepthCounterMaximum
= 0;
2081 ggw
.ThreadHeightCounterMaximum
= 0;
2082 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
2083 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
2084 ggw
.BottomExecutionMask
= 0xffffffff;
2087 anv_batch_emit(batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
2091 genX(flush_pipeline_select
)(struct anv_cmd_buffer
*cmd_buffer
,
2094 if (cmd_buffer
->state
.current_pipeline
== pipeline
)
2097 #if GEN_GEN >= 8 && GEN_GEN < 10
2098 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
2100 * Software must clear the COLOR_CALC_STATE Valid field in
2101 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
2102 * with Pipeline Select set to GPGPU.
2104 * The internal hardware docs recommend the same workaround for Gen9
2107 if (pipeline
== GPGPU
)
2108 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), t
);
2111 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
2112 * PIPELINE_SELECT [DevBWR+]":
2116 * Software must ensure all the write caches are flushed through a
2117 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
2118 * command to invalidate read only caches prior to programming
2119 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
2121 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2122 pc
.RenderTargetCacheFlushEnable
= true;
2123 pc
.DepthCacheFlushEnable
= true;
2124 pc
.DCFlushEnable
= true;
2125 pc
.PostSyncOperation
= NoWrite
;
2126 pc
.CommandStreamerStallEnable
= true;
2129 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2130 pc
.TextureCacheInvalidationEnable
= true;
2131 pc
.ConstantCacheInvalidationEnable
= true;
2132 pc
.StateCacheInvalidationEnable
= true;
2133 pc
.InstructionCacheInvalidateEnable
= true;
2134 pc
.PostSyncOperation
= NoWrite
;
2137 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
2141 ps
.PipelineSelection
= pipeline
;
2144 cmd_buffer
->state
.current_pipeline
= pipeline
;
2148 genX(flush_pipeline_select_3d
)(struct anv_cmd_buffer
*cmd_buffer
)
2150 genX(flush_pipeline_select
)(cmd_buffer
, _3D
);
2154 genX(flush_pipeline_select_gpgpu
)(struct anv_cmd_buffer
*cmd_buffer
)
2156 genX(flush_pipeline_select
)(cmd_buffer
, GPGPU
);
2160 genX(cmd_buffer_emit_gen7_depth_flush
)(struct anv_cmd_buffer
*cmd_buffer
)
2165 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
2167 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
2168 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
2169 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
2170 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
2171 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
2172 * Depth Flush Bit set, followed by another pipelined depth stall
2173 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
2174 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
2175 * via a preceding MI_FLUSH)."
2177 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2178 pipe
.DepthStallEnable
= true;
2180 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2181 pipe
.DepthCacheFlushEnable
= true;
2183 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2184 pipe
.DepthStallEnable
= true;
2189 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
2191 struct anv_device
*device
= cmd_buffer
->device
;
2192 const struct anv_image_view
*iview
=
2193 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2194 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
2196 /* FIXME: Width and Height are wrong */
2198 genX(cmd_buffer_emit_gen7_depth_flush
)(cmd_buffer
);
2200 uint32_t *dw
= anv_batch_emit_dwords(&cmd_buffer
->batch
,
2201 device
->isl_dev
.ds
.size
/ 4);
2205 struct isl_depth_stencil_hiz_emit_info info
= {
2206 .mocs
= device
->default_mocs
,
2210 info
.view
= &iview
->isl
;
2212 if (image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)) {
2213 info
.depth_surf
= &image
->depth_surface
.isl
;
2215 info
.depth_address
=
2216 anv_batch_emit_reloc(&cmd_buffer
->batch
,
2217 dw
+ device
->isl_dev
.ds
.depth_offset
/ 4,
2219 image
->offset
+ image
->depth_surface
.offset
);
2222 cmd_buffer
->state
.subpass
->depth_stencil_attachment
.attachment
;
2223 info
.hiz_usage
= cmd_buffer
->state
.attachments
[ds
].aux_usage
;
2224 if (info
.hiz_usage
== ISL_AUX_USAGE_HIZ
) {
2225 info
.hiz_surf
= &image
->aux_surface
.isl
;
2228 anv_batch_emit_reloc(&cmd_buffer
->batch
,
2229 dw
+ device
->isl_dev
.ds
.hiz_offset
/ 4,
2231 image
->offset
+ image
->aux_surface
.offset
);
2233 info
.depth_clear_value
= ANV_HZ_FC_VAL
;
2237 if (image
&& (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
)) {
2238 info
.stencil_surf
= &image
->stencil_surface
.isl
;
2240 info
.stencil_address
=
2241 anv_batch_emit_reloc(&cmd_buffer
->batch
,
2242 dw
+ device
->isl_dev
.ds
.stencil_offset
/ 4,
2244 image
->offset
+ image
->stencil_surface
.offset
);
2247 isl_emit_depth_stencil_hiz_s(&device
->isl_dev
, dw
, &info
);
2249 cmd_buffer
->state
.hiz_enabled
= info
.hiz_usage
== ISL_AUX_USAGE_HIZ
;
2254 * @brief Perform any layout transitions required at the beginning and/or end
2255 * of the current subpass for depth buffers.
2257 * TODO: Consider preprocessing the attachment reference array at render pass
2258 * create time to determine if no layout transition is needed at the
2259 * beginning and/or end of each subpass.
2261 * @param cmd_buffer The command buffer the transition is happening within.
2262 * @param subpass_end If true, marks that the transition is happening at the
2263 * end of the subpass.
2266 cmd_buffer_subpass_transition_layouts(struct anv_cmd_buffer
* const cmd_buffer
,
2267 const bool subpass_end
)
2269 /* We need a non-NULL command buffer. */
2272 const struct anv_cmd_state
* const cmd_state
= &cmd_buffer
->state
;
2273 const struct anv_subpass
* const subpass
= cmd_state
->subpass
;
2275 /* This function must be called within a subpass. */
2278 /* If there are attachment references, the array shouldn't be NULL.
2280 if (subpass
->attachment_count
> 0)
2281 assert(subpass
->attachments
);
2283 /* Iterate over the array of attachment references. */
2284 for (const VkAttachmentReference
*att_ref
= subpass
->attachments
;
2285 att_ref
< subpass
->attachments
+ subpass
->attachment_count
; att_ref
++) {
2287 /* If the attachment is unused, we can't perform a layout transition. */
2288 if (att_ref
->attachment
== VK_ATTACHMENT_UNUSED
)
2291 /* This attachment index shouldn't go out of bounds. */
2292 assert(att_ref
->attachment
< cmd_state
->pass
->attachment_count
);
2294 const struct anv_render_pass_attachment
* const att_desc
=
2295 &cmd_state
->pass
->attachments
[att_ref
->attachment
];
2296 struct anv_attachment_state
* const att_state
=
2297 &cmd_buffer
->state
.attachments
[att_ref
->attachment
];
2299 /* The attachment should not be used in a subpass after its last. */
2300 assert(att_desc
->last_subpass_idx
>= anv_get_subpass_id(cmd_state
));
2302 if (subpass_end
&& anv_get_subpass_id(cmd_state
) <
2303 att_desc
->last_subpass_idx
) {
2304 /* We're calling this function on a buffer twice in one subpass and
2305 * this is not the last use of the buffer. The layout should not have
2306 * changed from the first call and no transition is necessary.
2308 assert(att_ref
->layout
== att_state
->current_layout
);
2312 /* Get the appropriate target layout for this attachment. */
2313 const VkImageLayout target_layout
= subpass_end
?
2314 att_desc
->final_layout
: att_ref
->layout
;
2316 /* The attachment index must be less than the number of attachments
2317 * within the framebuffer.
2319 assert(att_ref
->attachment
< cmd_state
->framebuffer
->attachment_count
);
2321 const struct anv_image
* const image
=
2322 cmd_state
->framebuffer
->attachments
[att_ref
->attachment
]->image
;
2324 /* Perform the layout transition. */
2325 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
2326 transition_depth_buffer(cmd_buffer
, image
,
2327 att_state
->current_layout
, target_layout
);
2328 att_state
->aux_usage
=
2329 anv_layout_to_aux_usage(&cmd_buffer
->device
->info
, image
,
2330 image
->aspects
, target_layout
);
2333 att_state
->current_layout
= target_layout
;
2338 genX(cmd_buffer_set_subpass
)(struct anv_cmd_buffer
*cmd_buffer
,
2339 struct anv_subpass
*subpass
)
2341 cmd_buffer
->state
.subpass
= subpass
;
2343 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
2345 /* Perform transitions to the subpass layout before any writes have
2348 cmd_buffer_subpass_transition_layouts(cmd_buffer
, false);
2350 cmd_buffer_emit_depth_stencil(cmd_buffer
);
2352 anv_cmd_buffer_clear_subpass(cmd_buffer
);
2355 void genX(CmdBeginRenderPass
)(
2356 VkCommandBuffer commandBuffer
,
2357 const VkRenderPassBeginInfo
* pRenderPassBegin
,
2358 VkSubpassContents contents
)
2360 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2361 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
2362 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
2364 cmd_buffer
->state
.framebuffer
= framebuffer
;
2365 cmd_buffer
->state
.pass
= pass
;
2366 cmd_buffer
->state
.render_area
= pRenderPassBegin
->renderArea
;
2368 genX(cmd_buffer_setup_attachments
)(cmd_buffer
, pass
, pRenderPassBegin
);
2370 /* If we failed to setup the attachments we should not try to go further */
2371 if (result
!= VK_SUCCESS
) {
2372 assert(anv_batch_has_error(&cmd_buffer
->batch
));
2376 genX(flush_pipeline_select_3d
)(cmd_buffer
);
2378 genX(cmd_buffer_set_subpass
)(cmd_buffer
, pass
->subpasses
);
2380 cmd_buffer
->state
.pending_pipe_bits
|=
2381 cmd_buffer
->state
.pass
->subpass_flushes
[0];
2384 void genX(CmdNextSubpass
)(
2385 VkCommandBuffer commandBuffer
,
2386 VkSubpassContents contents
)
2388 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2390 if (anv_batch_has_error(&cmd_buffer
->batch
))
2393 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
2395 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
2397 /* Perform transitions to the final layout after all writes have occurred.
2399 cmd_buffer_subpass_transition_layouts(cmd_buffer
, true);
2401 genX(cmd_buffer_set_subpass
)(cmd_buffer
, cmd_buffer
->state
.subpass
+ 1);
2403 uint32_t subpass_id
= anv_get_subpass_id(&cmd_buffer
->state
);
2404 cmd_buffer
->state
.pending_pipe_bits
|=
2405 cmd_buffer
->state
.pass
->subpass_flushes
[subpass_id
];
2408 void genX(CmdEndRenderPass
)(
2409 VkCommandBuffer commandBuffer
)
2411 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2413 if (anv_batch_has_error(&cmd_buffer
->batch
))
2416 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
2418 /* Perform transitions to the final layout after all writes have occurred.
2420 cmd_buffer_subpass_transition_layouts(cmd_buffer
, true);
2422 cmd_buffer
->state
.pending_pipe_bits
|=
2423 cmd_buffer
->state
.pass
->subpass_flushes
[cmd_buffer
->state
.pass
->subpass_count
];
2425 cmd_buffer
->state
.hiz_enabled
= false;
2428 anv_dump_add_framebuffer(cmd_buffer
, cmd_buffer
->state
.framebuffer
);
2431 /* Remove references to render pass specific state. This enables us to
2432 * detect whether or not we're in a renderpass.
2434 cmd_buffer
->state
.framebuffer
= NULL
;
2435 cmd_buffer
->state
.pass
= NULL
;
2436 cmd_buffer
->state
.subpass
= NULL
;