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 /* XXX: Do we need this on more than just BDW? */
60 /* Emit a render target cache flush.
62 * This isn't documented anywhere in the PRM. However, it seems to be
63 * necessary prior to changing the surface state base adress. Without
64 * this, we get GPU hangs when using multi-level command buffers which
65 * clear depth, reset state base address, and then go render stuff.
67 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
68 pc
.RenderTargetCacheFlushEnable
= true;
72 anv_batch_emit(&cmd_buffer
->batch
, GENX(STATE_BASE_ADDRESS
), sba
) {
73 sba
.GeneralStateBaseAddress
= (struct anv_address
) { NULL
, 0 };
74 sba
.GeneralStateMemoryObjectControlState
= GENX(MOCS
);
75 sba
.GeneralStateBaseAddressModifyEnable
= true;
77 sba
.SurfaceStateBaseAddress
=
78 anv_cmd_buffer_surface_base_address(cmd_buffer
);
79 sba
.SurfaceStateMemoryObjectControlState
= GENX(MOCS
);
80 sba
.SurfaceStateBaseAddressModifyEnable
= true;
82 sba
.DynamicStateBaseAddress
=
83 (struct anv_address
) { &device
->dynamic_state_block_pool
.bo
, 0 };
84 sba
.DynamicStateMemoryObjectControlState
= GENX(MOCS
);
85 sba
.DynamicStateBaseAddressModifyEnable
= true;
87 sba
.IndirectObjectBaseAddress
= (struct anv_address
) { NULL
, 0 };
88 sba
.IndirectObjectMemoryObjectControlState
= GENX(MOCS
);
89 sba
.IndirectObjectBaseAddressModifyEnable
= true;
91 sba
.InstructionBaseAddress
=
92 (struct anv_address
) { &device
->instruction_block_pool
.bo
, 0 };
93 sba
.InstructionMemoryObjectControlState
= GENX(MOCS
);
94 sba
.InstructionBaseAddressModifyEnable
= true;
97 /* Broadwell requires that we specify a buffer size for a bunch of
98 * these fields. However, since we will be growing the BO's live, we
99 * just set them all to the maximum.
101 sba
.GeneralStateBufferSize
= 0xfffff;
102 sba
.GeneralStateBufferSizeModifyEnable
= true;
103 sba
.DynamicStateBufferSize
= 0xfffff;
104 sba
.DynamicStateBufferSizeModifyEnable
= true;
105 sba
.IndirectObjectBufferSize
= 0xfffff;
106 sba
.IndirectObjectBufferSizeModifyEnable
= true;
107 sba
.InstructionBufferSize
= 0xfffff;
108 sba
.InstructionBuffersizeModifyEnable
= true;
112 /* After re-setting the surface state base address, we have to do some
113 * cache flusing so that the sampler engine will pick up the new
114 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
115 * Shared Function > 3D Sampler > State > State Caching (page 96):
117 * Coherency with system memory in the state cache, like the texture
118 * cache is handled partially by software. It is expected that the
119 * command stream or shader will issue Cache Flush operation or
120 * Cache_Flush sampler message to ensure that the L1 cache remains
121 * coherent with system memory.
125 * Whenever the value of the Dynamic_State_Base_Addr,
126 * Surface_State_Base_Addr are altered, the L1 state cache must be
127 * invalidated to ensure the new surface or sampler state is fetched
128 * from system memory.
130 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
131 * which, according the PIPE_CONTROL instruction documentation in the
134 * Setting this bit is independent of any other bit in this packet.
135 * This bit controls the invalidation of the L1 and L2 state caches
136 * at the top of the pipe i.e. at the parsing time.
138 * Unfortunately, experimentation seems to indicate that state cache
139 * invalidation through a PIPE_CONTROL does nothing whatsoever in
140 * regards to surface state and binding tables. In stead, it seems that
141 * invalidating the texture cache is what is actually needed.
143 * XXX: As far as we have been able to determine through
144 * experimentation, shows that flush the texture cache appears to be
145 * sufficient. The theory here is that all of the sampling/rendering
146 * units cache the binding table in the texture cache. However, we have
147 * yet to be able to actually confirm this.
149 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
150 pc
.TextureCacheInvalidationEnable
= true;
155 add_surface_state_reloc(struct anv_cmd_buffer
*cmd_buffer
,
156 struct anv_state state
,
157 struct anv_bo
*bo
, uint32_t offset
)
159 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
161 anv_reloc_list_add(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
,
162 state
.offset
+ isl_dev
->ss
.addr_offset
, bo
, offset
);
166 add_image_view_relocs(struct anv_cmd_buffer
*cmd_buffer
,
167 const struct anv_image_view
*iview
,
168 enum isl_aux_usage aux_usage
,
169 struct anv_state state
)
171 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
173 anv_reloc_list_add(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
,
174 state
.offset
+ isl_dev
->ss
.addr_offset
,
175 iview
->bo
, iview
->offset
);
177 if (aux_usage
!= ISL_AUX_USAGE_NONE
) {
178 uint32_t aux_offset
= iview
->offset
+ iview
->image
->aux_surface
.offset
;
180 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
181 * used to store other information. This should be ok, however, because
182 * surface buffer addresses are always 4K page alinged.
184 assert((aux_offset
& 0xfff) == 0);
185 uint32_t *aux_addr_dw
= state
.map
+ isl_dev
->ss
.aux_addr_offset
;
186 aux_offset
+= *aux_addr_dw
& 0xfff;
188 anv_reloc_list_add(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
,
189 state
.offset
+ isl_dev
->ss
.aux_addr_offset
,
190 iview
->bo
, aux_offset
);
195 color_is_zero_one(VkClearColorValue value
, enum isl_format format
)
197 if (isl_format_has_int_channel(format
)) {
198 for (unsigned i
= 0; i
< 4; i
++) {
199 if (value
.int32
[i
] != 0 && value
.int32
[i
] != 1)
203 for (unsigned i
= 0; i
< 4; i
++) {
204 if (value
.float32
[i
] != 0.0f
&& value
.float32
[i
] != 1.0f
)
213 color_attachment_compute_aux_usage(struct anv_device
*device
,
214 struct anv_attachment_state
*att_state
,
215 struct anv_image_view
*iview
,
216 VkRect2D render_area
,
217 union isl_color_value
*fast_clear_color
)
219 if (iview
->image
->aux_surface
.isl
.size
== 0) {
220 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
221 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
222 att_state
->fast_clear
= false;
226 assert(iview
->image
->aux_surface
.isl
.usage
& ISL_SURF_USAGE_CCS_BIT
);
228 att_state
->clear_color_is_zero_one
=
229 color_is_zero_one(att_state
->clear_value
.color
, iview
->isl
.format
);
231 if (att_state
->pending_clear_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
232 /* Start off assuming fast clears are possible */
233 att_state
->fast_clear
= true;
235 /* Potentially, we could do partial fast-clears but doing so has crazy
236 * alignment restrictions. It's easier to just restrict to full size
237 * fast clears for now.
239 if (render_area
.offset
.x
!= 0 ||
240 render_area
.offset
.y
!= 0 ||
241 render_area
.extent
.width
!= iview
->extent
.width
||
242 render_area
.extent
.height
!= iview
->extent
.height
)
243 att_state
->fast_clear
= false;
246 /* On gen7, we can't do multi-LOD or multi-layer fast-clears. We
247 * technically can, but it comes with crazy restrictions that we
248 * don't want to deal with now.
250 if (iview
->isl
.base_level
> 0 ||
251 iview
->isl
.base_array_layer
> 0 ||
252 iview
->isl
.array_len
> 1)
253 att_state
->fast_clear
= false;
256 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
257 if (GEN_GEN
<= 8 && !att_state
->clear_color_is_zero_one
)
258 att_state
->fast_clear
= false;
260 if (att_state
->fast_clear
) {
261 memcpy(fast_clear_color
->u32
, att_state
->clear_value
.color
.uint32
,
262 sizeof(fast_clear_color
->u32
));
265 att_state
->fast_clear
= false;
268 if (isl_format_supports_lossless_compression(&device
->info
,
269 iview
->isl
.format
)) {
270 att_state
->aux_usage
= ISL_AUX_USAGE_CCS_E
;
271 att_state
->input_aux_usage
= ISL_AUX_USAGE_CCS_E
;
272 } else if (att_state
->fast_clear
) {
273 att_state
->aux_usage
= ISL_AUX_USAGE_CCS_D
;
275 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
277 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
278 * setting is only allowed if Surface Format supported for Fast
279 * Clear. In addition, if the surface is bound to the sampling
280 * engine, Surface Format must be supported for Render Target
281 * Compression for surfaces bound to the sampling engine."
283 * In other words, we can't sample from a fast-cleared image if it
284 * doesn't also support color compression.
286 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
287 } else if (GEN_GEN
== 8) {
288 /* Broadwell can sample from fast-cleared images */
289 att_state
->input_aux_usage
= ISL_AUX_USAGE_CCS_D
;
291 /* Ivy Bridge and Haswell cannot */
292 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
295 att_state
->aux_usage
= ISL_AUX_USAGE_NONE
;
296 att_state
->input_aux_usage
= ISL_AUX_USAGE_NONE
;
301 need_input_attachment_state(const struct anv_render_pass_attachment
*att
)
303 if (!(att
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
306 /* We only allocate input attachment states for color and depth surfaces.
307 * Stencil doesn't allow compression so we can just use the texture surface
308 * state from the view
310 return vk_format_is_color(att
->format
) || vk_format_has_depth(att
->format
);
313 static enum isl_aux_usage
314 layout_to_hiz_usage(VkImageLayout layout
)
317 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
:
318 return ISL_AUX_USAGE_HIZ
;
320 return ISL_AUX_USAGE_NONE
;
324 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
325 * the initial layout is undefined, the HiZ buffer and depth buffer will
326 * represent the same data at the end of this operation.
329 transition_depth_buffer(struct anv_cmd_buffer
*cmd_buffer
,
330 const struct anv_image
*image
,
331 VkImageLayout initial_layout
,
332 VkImageLayout final_layout
)
336 if (image
->aux_usage
!= ISL_AUX_USAGE_HIZ
)
339 const bool hiz_enabled
= layout_to_hiz_usage(initial_layout
) ==
341 const bool enable_hiz
= layout_to_hiz_usage(final_layout
) ==
344 /* We've already initialized the aux HiZ buffer at BindImageMemory time,
345 * so there's no need to perform a HIZ resolve or clear to avoid GPU hangs.
346 * This initial layout indicates that the user doesn't care about the data
347 * that's currently in the buffer, so no resolves are necessary.
349 if (initial_layout
== VK_IMAGE_LAYOUT_UNDEFINED
)
352 if (hiz_enabled
== enable_hiz
) {
353 /* The same buffer will be used, no resolves are necessary */
354 } else if (hiz_enabled
&& !enable_hiz
) {
355 anv_gen8_hiz_op_resolve(cmd_buffer
, image
, BLORP_HIZ_OP_DEPTH_RESOLVE
);
357 assert(!hiz_enabled
&& enable_hiz
);
358 anv_gen8_hiz_op_resolve(cmd_buffer
, image
, BLORP_HIZ_OP_HIZ_RESOLVE
);
364 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
367 genX(cmd_buffer_setup_attachments
)(struct anv_cmd_buffer
*cmd_buffer
,
368 struct anv_render_pass
*pass
,
369 const VkRenderPassBeginInfo
*begin
)
371 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
372 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
374 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
376 if (pass
->attachment_count
== 0) {
377 state
->attachments
= NULL
;
381 state
->attachments
= vk_alloc(&cmd_buffer
->pool
->alloc
,
382 pass
->attachment_count
*
383 sizeof(state
->attachments
[0]),
384 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
385 if (state
->attachments
== NULL
) {
386 /* FIXME: Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
390 bool need_null_state
= false;
391 unsigned num_states
= 0;
392 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
393 if (vk_format_is_color(pass
->attachments
[i
].format
)) {
396 /* We need a null state for any depth-stencil-only subpasses.
397 * Importantly, this includes depth/stencil clears so we create one
398 * whenever we have depth or stencil
400 need_null_state
= true;
403 if (need_input_attachment_state(&pass
->attachments
[i
]))
406 num_states
+= need_null_state
;
408 const uint32_t ss_stride
= align_u32(isl_dev
->ss
.size
, isl_dev
->ss
.align
);
409 state
->render_pass_states
=
410 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
411 num_states
* ss_stride
, isl_dev
->ss
.align
);
413 struct anv_state next_state
= state
->render_pass_states
;
414 next_state
.alloc_size
= isl_dev
->ss
.size
;
416 if (need_null_state
) {
417 state
->null_surface_state
= next_state
;
418 next_state
.offset
+= ss_stride
;
419 next_state
.map
+= ss_stride
;
422 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
423 if (vk_format_is_color(pass
->attachments
[i
].format
)) {
424 state
->attachments
[i
].color_rt_state
= next_state
;
425 next_state
.offset
+= ss_stride
;
426 next_state
.map
+= ss_stride
;
429 if (need_input_attachment_state(&pass
->attachments
[i
])) {
430 state
->attachments
[i
].input_att_state
= next_state
;
431 next_state
.offset
+= ss_stride
;
432 next_state
.map
+= ss_stride
;
435 assert(next_state
.offset
== state
->render_pass_states
.offset
+
436 state
->render_pass_states
.alloc_size
);
439 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, begin
->framebuffer
);
440 assert(pass
->attachment_count
== framebuffer
->attachment_count
);
442 if (need_null_state
) {
443 struct GENX(RENDER_SURFACE_STATE
) null_ss
= {
444 .SurfaceType
= SURFTYPE_NULL
,
445 .SurfaceArray
= framebuffer
->layers
> 0,
446 .SurfaceFormat
= ISL_FORMAT_R8G8B8A8_UNORM
,
450 .TiledSurface
= true,
452 .Width
= framebuffer
->width
- 1,
453 .Height
= framebuffer
->height
- 1,
454 .Depth
= framebuffer
->layers
- 1,
455 .RenderTargetViewExtent
= framebuffer
->layers
- 1,
457 GENX(RENDER_SURFACE_STATE_pack
)(NULL
, state
->null_surface_state
.map
,
461 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
462 struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
463 VkImageAspectFlags att_aspects
= vk_format_aspects(att
->format
);
464 VkImageAspectFlags clear_aspects
= 0;
466 if (att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
467 /* color attachment */
468 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
469 clear_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
472 /* depthstencil attachment */
473 if ((att_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
474 att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
475 clear_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
477 if ((att_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
478 att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
479 clear_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
483 state
->attachments
[i
].pending_clear_aspects
= clear_aspects
;
485 state
->attachments
[i
].clear_value
= begin
->pClearValues
[i
];
487 struct anv_image_view
*iview
= framebuffer
->attachments
[i
];
488 assert(iview
->vk_format
== att
->format
);
490 union isl_color_value clear_color
= { .u32
= { 0, } };
491 if (att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
492 color_attachment_compute_aux_usage(cmd_buffer
->device
,
493 &state
->attachments
[i
],
494 iview
, begin
->renderArea
,
497 struct isl_view view
= iview
->isl
;
498 view
.usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
499 isl_surf_fill_state(isl_dev
,
500 state
->attachments
[i
].color_rt_state
.map
,
501 .surf
= &iview
->image
->color_surface
.isl
,
503 .aux_surf
= &iview
->image
->aux_surface
.isl
,
504 .aux_usage
= state
->attachments
[i
].aux_usage
,
505 .clear_color
= clear_color
,
506 .mocs
= cmd_buffer
->device
->default_mocs
);
508 add_image_view_relocs(cmd_buffer
, iview
,
509 state
->attachments
[i
].aux_usage
,
510 state
->attachments
[i
].color_rt_state
);
512 state
->attachments
[i
].aux_usage
= iview
->image
->aux_usage
;
513 state
->attachments
[i
].input_aux_usage
= ISL_AUX_USAGE_NONE
;
516 if (need_input_attachment_state(&pass
->attachments
[i
])) {
517 const struct isl_surf
*surf
;
518 if (att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
519 surf
= &iview
->image
->color_surface
.isl
;
521 surf
= &iview
->image
->depth_surface
.isl
;
524 struct isl_view view
= iview
->isl
;
525 view
.usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
526 isl_surf_fill_state(isl_dev
,
527 state
->attachments
[i
].input_att_state
.map
,
530 .aux_surf
= &iview
->image
->aux_surface
.isl
,
531 .aux_usage
= state
->attachments
[i
].input_aux_usage
,
532 .clear_color
= clear_color
,
533 .mocs
= cmd_buffer
->device
->default_mocs
);
535 add_image_view_relocs(cmd_buffer
, iview
,
536 state
->attachments
[i
].input_aux_usage
,
537 state
->attachments
[i
].input_att_state
);
541 if (!cmd_buffer
->device
->info
.has_llc
)
542 anv_state_clflush(state
->render_pass_states
);
547 genX(BeginCommandBuffer
)(
548 VkCommandBuffer commandBuffer
,
549 const VkCommandBufferBeginInfo
* pBeginInfo
)
551 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
553 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
554 * command buffer's state. Otherwise, we must *reset* its state. In both
557 * From the Vulkan 1.0 spec:
559 * If a command buffer is in the executable state and the command buffer
560 * was allocated from a command pool with the
561 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
562 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
563 * as if vkResetCommandBuffer had been called with
564 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
565 * the command buffer in the recording state.
567 anv_cmd_buffer_reset(cmd_buffer
);
569 cmd_buffer
->usage_flags
= pBeginInfo
->flags
;
571 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
||
572 !(cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
));
574 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
576 if (cmd_buffer
->usage_flags
&
577 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
578 cmd_buffer
->state
.pass
=
579 anv_render_pass_from_handle(pBeginInfo
->pInheritanceInfo
->renderPass
);
580 cmd_buffer
->state
.subpass
=
581 &cmd_buffer
->state
.pass
->subpasses
[pBeginInfo
->pInheritanceInfo
->subpass
];
582 cmd_buffer
->state
.framebuffer
= NULL
;
584 genX(cmd_buffer_setup_attachments
)(cmd_buffer
, cmd_buffer
->state
.pass
,
587 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
594 genX(EndCommandBuffer
)(
595 VkCommandBuffer commandBuffer
)
597 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
599 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
601 anv_cmd_buffer_end_batch_buffer(cmd_buffer
);
607 genX(CmdExecuteCommands
)(
608 VkCommandBuffer commandBuffer
,
609 uint32_t commandBufferCount
,
610 const VkCommandBuffer
* pCmdBuffers
)
612 ANV_FROM_HANDLE(anv_cmd_buffer
, primary
, commandBuffer
);
614 assert(primary
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
616 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
617 ANV_FROM_HANDLE(anv_cmd_buffer
, secondary
, pCmdBuffers
[i
]);
619 assert(secondary
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
621 if (secondary
->usage_flags
&
622 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
623 /* If we're continuing a render pass from the primary, we need to
624 * copy the surface states for the current subpass into the storage
625 * we allocated for them in BeginCommandBuffer.
627 struct anv_bo
*ss_bo
= &primary
->device
->surface_state_block_pool
.bo
;
628 struct anv_state src_state
= primary
->state
.render_pass_states
;
629 struct anv_state dst_state
= secondary
->state
.render_pass_states
;
630 assert(src_state
.alloc_size
== dst_state
.alloc_size
);
632 genX(cmd_buffer_gpu_memcpy
)(primary
, ss_bo
, dst_state
.offset
,
633 ss_bo
, src_state
.offset
,
634 src_state
.alloc_size
);
637 anv_cmd_buffer_add_secondary(primary
, secondary
);
640 /* Each of the secondary command buffers will use its own state base
641 * address. We need to re-emit state base address for the primary after
642 * all of the secondaries are done.
644 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
647 genX(cmd_buffer_emit_state_base_address
)(primary
);
650 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
651 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
652 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
655 * Program the hardware to use the specified L3 configuration.
658 genX(cmd_buffer_config_l3
)(struct anv_cmd_buffer
*cmd_buffer
,
659 const struct gen_l3_config
*cfg
)
662 if (cfg
== cmd_buffer
->state
.current_l3_config
)
665 if (unlikely(INTEL_DEBUG
& DEBUG_L3
)) {
666 fprintf(stderr
, "L3 config transition: ");
667 gen_dump_l3_config(cfg
, stderr
);
670 const bool has_slm
= cfg
->n
[GEN_L3P_SLM
];
672 /* According to the hardware docs, the L3 partitioning can only be changed
673 * while the pipeline is completely drained and the caches are flushed,
674 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
676 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
677 pc
.DCFlushEnable
= true;
678 pc
.PostSyncOperation
= NoWrite
;
679 pc
.CommandStreamerStallEnable
= true;
682 /* ...followed by a second pipelined PIPE_CONTROL that initiates
683 * invalidation of the relevant caches. Note that because RO invalidation
684 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
685 * command is processed by the CS) we cannot combine it with the previous
686 * stalling flush as the hardware documentation suggests, because that
687 * would cause the CS to stall on previous rendering *after* RO
688 * invalidation and wouldn't prevent the RO caches from being polluted by
689 * concurrent rendering before the stall completes. This intentionally
690 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
691 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
692 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
693 * already guarantee that there is no concurrent GPGPU kernel execution
694 * (see SKL HSD 2132585).
696 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
697 pc
.TextureCacheInvalidationEnable
= true;
698 pc
.ConstantCacheInvalidationEnable
= true;
699 pc
.InstructionCacheInvalidateEnable
= true;
700 pc
.StateCacheInvalidationEnable
= true;
701 pc
.PostSyncOperation
= NoWrite
;
704 /* Now send a third stalling flush to make sure that invalidation is
705 * complete when the L3 configuration registers are modified.
707 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
708 pc
.DCFlushEnable
= true;
709 pc
.PostSyncOperation
= NoWrite
;
710 pc
.CommandStreamerStallEnable
= true;
715 assert(!cfg
->n
[GEN_L3P_IS
] && !cfg
->n
[GEN_L3P_C
] && !cfg
->n
[GEN_L3P_T
]);
718 anv_pack_struct(&l3cr
, GENX(L3CNTLREG
),
719 .SLMEnable
= has_slm
,
720 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
721 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
722 .DCAllocation
= cfg
->n
[GEN_L3P_DC
],
723 .AllAllocation
= cfg
->n
[GEN_L3P_ALL
]);
725 /* Set up the L3 partitioning. */
726 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG_num
), l3cr
);
730 const bool has_dc
= cfg
->n
[GEN_L3P_DC
] || cfg
->n
[GEN_L3P_ALL
];
731 const bool has_is
= cfg
->n
[GEN_L3P_IS
] || cfg
->n
[GEN_L3P_RO
] ||
733 const bool has_c
= cfg
->n
[GEN_L3P_C
] || cfg
->n
[GEN_L3P_RO
] ||
735 const bool has_t
= cfg
->n
[GEN_L3P_T
] || cfg
->n
[GEN_L3P_RO
] ||
738 assert(!cfg
->n
[GEN_L3P_ALL
]);
740 /* When enabled SLM only uses a portion of the L3 on half of the banks,
741 * the matching space on the remaining banks has to be allocated to a
742 * client (URB for all validated configurations) set to the
743 * lower-bandwidth 2-bank address hashing mode.
745 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
746 const bool urb_low_bw
= has_slm
&& !devinfo
->is_baytrail
;
747 assert(!urb_low_bw
|| cfg
->n
[GEN_L3P_URB
] == cfg
->n
[GEN_L3P_SLM
]);
749 /* Minimum number of ways that can be allocated to the URB. */
750 MAYBE_UNUSED
const unsigned n0_urb
= devinfo
->is_baytrail
? 32 : 0;
751 assert(cfg
->n
[GEN_L3P_URB
] >= n0_urb
);
753 uint32_t l3sqcr1
, l3cr2
, l3cr3
;
754 anv_pack_struct(&l3sqcr1
, GENX(L3SQCREG1
),
755 .ConvertDC_UC
= !has_dc
,
756 .ConvertIS_UC
= !has_is
,
757 .ConvertC_UC
= !has_c
,
758 .ConvertT_UC
= !has_t
);
760 GEN_IS_HASWELL
? HSW_L3SQCREG1_SQGHPCI_DEFAULT
:
761 devinfo
->is_baytrail
? VLV_L3SQCREG1_SQGHPCI_DEFAULT
:
762 IVB_L3SQCREG1_SQGHPCI_DEFAULT
;
764 anv_pack_struct(&l3cr2
, GENX(L3CNTLREG2
),
765 .SLMEnable
= has_slm
,
766 .URBLowBandwidth
= urb_low_bw
,
767 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
769 .ALLAllocation
= cfg
->n
[GEN_L3P_ALL
],
771 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
772 .DCAllocation
= cfg
->n
[GEN_L3P_DC
]);
774 anv_pack_struct(&l3cr3
, GENX(L3CNTLREG3
),
775 .ISAllocation
= cfg
->n
[GEN_L3P_IS
],
777 .CAllocation
= cfg
->n
[GEN_L3P_C
],
779 .TAllocation
= cfg
->n
[GEN_L3P_T
],
782 /* Set up the L3 partitioning. */
783 emit_lri(&cmd_buffer
->batch
, GENX(L3SQCREG1_num
), l3sqcr1
);
784 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG2_num
), l3cr2
);
785 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG3_num
), l3cr3
);
788 if (cmd_buffer
->device
->instance
->physicalDevice
.cmd_parser_version
>= 4) {
789 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
790 * them disabled to avoid crashing the system hard.
792 uint32_t scratch1
, chicken3
;
793 anv_pack_struct(&scratch1
, GENX(SCRATCH1
),
794 .L3AtomicDisable
= !has_dc
);
795 anv_pack_struct(&chicken3
, GENX(CHICKEN3
),
796 .L3AtomicDisableMask
= true,
797 .L3AtomicDisable
= !has_dc
);
798 emit_lri(&cmd_buffer
->batch
, GENX(SCRATCH1_num
), scratch1
);
799 emit_lri(&cmd_buffer
->batch
, GENX(CHICKEN3_num
), chicken3
);
805 cmd_buffer
->state
.current_l3_config
= cfg
;
809 genX(cmd_buffer_apply_pipe_flushes
)(struct anv_cmd_buffer
*cmd_buffer
)
811 enum anv_pipe_bits bits
= cmd_buffer
->state
.pending_pipe_bits
;
813 /* Flushes are pipelined while invalidations are handled immediately.
814 * Therefore, if we're flushing anything then we need to schedule a stall
815 * before any invalidations can happen.
817 if (bits
& ANV_PIPE_FLUSH_BITS
)
818 bits
|= ANV_PIPE_NEEDS_CS_STALL_BIT
;
820 /* If we're going to do an invalidate and we have a pending CS stall that
821 * has yet to be resolved, we do the CS stall now.
823 if ((bits
& ANV_PIPE_INVALIDATE_BITS
) &&
824 (bits
& ANV_PIPE_NEEDS_CS_STALL_BIT
)) {
825 bits
|= ANV_PIPE_CS_STALL_BIT
;
826 bits
&= ~ANV_PIPE_NEEDS_CS_STALL_BIT
;
829 if (bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
)) {
830 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
831 pipe
.DepthCacheFlushEnable
= bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
832 pipe
.DCFlushEnable
= bits
& ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
833 pipe
.RenderTargetCacheFlushEnable
=
834 bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
836 pipe
.DepthStallEnable
= bits
& ANV_PIPE_DEPTH_STALL_BIT
;
837 pipe
.CommandStreamerStallEnable
= bits
& ANV_PIPE_CS_STALL_BIT
;
838 pipe
.StallAtPixelScoreboard
= bits
& ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
841 * According to the Broadwell documentation, any PIPE_CONTROL with the
842 * "Command Streamer Stall" bit set must also have another bit set,
843 * with five different options:
845 * - Render Target Cache Flush
846 * - Depth Cache Flush
847 * - Stall at Pixel Scoreboard
848 * - Post-Sync Operation
852 * I chose "Stall at Pixel Scoreboard" since that's what we use in
853 * mesa and it seems to work fine. The choice is fairly arbitrary.
855 if ((bits
& ANV_PIPE_CS_STALL_BIT
) &&
856 !(bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_DEPTH_STALL_BIT
|
857 ANV_PIPE_STALL_AT_SCOREBOARD_BIT
)))
858 pipe
.StallAtPixelScoreboard
= true;
861 bits
&= ~(ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
);
864 if (bits
& ANV_PIPE_INVALIDATE_BITS
) {
865 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
866 pipe
.StateCacheInvalidationEnable
=
867 bits
& ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
868 pipe
.ConstantCacheInvalidationEnable
=
869 bits
& ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
870 pipe
.VFCacheInvalidationEnable
=
871 bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
872 pipe
.TextureCacheInvalidationEnable
=
873 bits
& ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
874 pipe
.InstructionCacheInvalidateEnable
=
875 bits
& ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT
;
878 bits
&= ~ANV_PIPE_INVALIDATE_BITS
;
881 cmd_buffer
->state
.pending_pipe_bits
= bits
;
884 void genX(CmdPipelineBarrier
)(
885 VkCommandBuffer commandBuffer
,
886 VkPipelineStageFlags srcStageMask
,
887 VkPipelineStageFlags destStageMask
,
889 uint32_t memoryBarrierCount
,
890 const VkMemoryBarrier
* pMemoryBarriers
,
891 uint32_t bufferMemoryBarrierCount
,
892 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
893 uint32_t imageMemoryBarrierCount
,
894 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
896 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
899 /* XXX: Right now, we're really dumb and just flush whatever categories
900 * the app asks for. One of these days we may make this a bit better
901 * but right now that's all the hardware allows for in most areas.
903 VkAccessFlags src_flags
= 0;
904 VkAccessFlags dst_flags
= 0;
906 for (uint32_t i
= 0; i
< memoryBarrierCount
; i
++) {
907 src_flags
|= pMemoryBarriers
[i
].srcAccessMask
;
908 dst_flags
|= pMemoryBarriers
[i
].dstAccessMask
;
911 for (uint32_t i
= 0; i
< bufferMemoryBarrierCount
; i
++) {
912 src_flags
|= pBufferMemoryBarriers
[i
].srcAccessMask
;
913 dst_flags
|= pBufferMemoryBarriers
[i
].dstAccessMask
;
916 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
917 src_flags
|= pImageMemoryBarriers
[i
].srcAccessMask
;
918 dst_flags
|= pImageMemoryBarriers
[i
].dstAccessMask
;
921 enum anv_pipe_bits pipe_bits
= 0;
923 for_each_bit(b
, src_flags
) {
924 switch ((VkAccessFlagBits
)(1 << b
)) {
925 case VK_ACCESS_SHADER_WRITE_BIT
:
926 pipe_bits
|= ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
928 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
:
929 pipe_bits
|= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
931 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
:
932 pipe_bits
|= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
934 case VK_ACCESS_TRANSFER_WRITE_BIT
:
935 pipe_bits
|= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
936 pipe_bits
|= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
939 break; /* Nothing to do */
943 for_each_bit(b
, dst_flags
) {
944 switch ((VkAccessFlagBits
)(1 << b
)) {
945 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT
:
946 case VK_ACCESS_INDEX_READ_BIT
:
947 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT
:
948 pipe_bits
|= ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
950 case VK_ACCESS_UNIFORM_READ_BIT
:
951 pipe_bits
|= ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
952 pipe_bits
|= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
954 case VK_ACCESS_SHADER_READ_BIT
:
955 case VK_ACCESS_INPUT_ATTACHMENT_READ_BIT
:
956 case VK_ACCESS_TRANSFER_READ_BIT
:
957 pipe_bits
|= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
960 break; /* Nothing to do */
964 cmd_buffer
->state
.pending_pipe_bits
|= pipe_bits
;
968 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
970 VkShaderStageFlags stages
= cmd_buffer
->state
.pipeline
->active_stages
;
972 /* In order to avoid thrash, we assume that vertex and fragment stages
973 * always exist. In the rare case where one is missing *and* the other
974 * uses push concstants, this may be suboptimal. However, avoiding stalls
975 * seems more important.
977 stages
|= VK_SHADER_STAGE_FRAGMENT_BIT
| VK_SHADER_STAGE_VERTEX_BIT
;
979 if (stages
== cmd_buffer
->state
.push_constant_stages
)
983 const unsigned push_constant_kb
= 32;
985 const unsigned push_constant_kb
= cmd_buffer
->device
->info
.gt
== 3 ? 32 : 16;
987 const unsigned push_constant_kb
= 16;
990 const unsigned num_stages
=
991 _mesa_bitcount(stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
992 unsigned size_per_stage
= push_constant_kb
/ num_stages
;
994 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
995 * units of 2KB. Incidentally, these are the same platforms that have
996 * 32KB worth of push constant space.
998 if (push_constant_kb
== 32)
999 size_per_stage
&= ~1u;
1001 uint32_t kb_used
= 0;
1002 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
1003 unsigned push_size
= (stages
& (1 << i
)) ? size_per_stage
: 0;
1004 anv_batch_emit(&cmd_buffer
->batch
,
1005 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS
), alloc
) {
1006 alloc
._3DCommandSubOpcode
= 18 + i
;
1007 alloc
.ConstantBufferOffset
= (push_size
> 0) ? kb_used
: 0;
1008 alloc
.ConstantBufferSize
= push_size
;
1010 kb_used
+= push_size
;
1013 anv_batch_emit(&cmd_buffer
->batch
,
1014 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS
), alloc
) {
1015 alloc
.ConstantBufferOffset
= kb_used
;
1016 alloc
.ConstantBufferSize
= push_constant_kb
- kb_used
;
1019 cmd_buffer
->state
.push_constant_stages
= stages
;
1021 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
1023 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
1024 * the next 3DPRIMITIVE command after programming the
1025 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
1027 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
1028 * pipeline setup, we need to dirty push constants.
1030 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
1034 emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
1035 gl_shader_stage stage
,
1036 struct anv_state
*bt_state
)
1038 struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1039 struct anv_pipeline
*pipeline
;
1040 uint32_t bias
, state_offset
;
1043 case MESA_SHADER_COMPUTE
:
1044 pipeline
= cmd_buffer
->state
.compute_pipeline
;
1048 pipeline
= cmd_buffer
->state
.pipeline
;
1053 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
1054 *bt_state
= (struct anv_state
) { 0, };
1058 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
1059 if (bias
+ map
->surface_count
== 0) {
1060 *bt_state
= (struct anv_state
) { 0, };
1064 *bt_state
= anv_cmd_buffer_alloc_binding_table(cmd_buffer
,
1065 bias
+ map
->surface_count
,
1067 uint32_t *bt_map
= bt_state
->map
;
1069 if (bt_state
->map
== NULL
)
1070 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1072 if (stage
== MESA_SHADER_COMPUTE
&&
1073 get_cs_prog_data(cmd_buffer
->state
.compute_pipeline
)->uses_num_work_groups
) {
1074 struct anv_bo
*bo
= cmd_buffer
->state
.num_workgroups_bo
;
1075 uint32_t bo_offset
= cmd_buffer
->state
.num_workgroups_offset
;
1077 struct anv_state surface_state
;
1079 anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
1081 const enum isl_format format
=
1082 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
);
1083 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
1084 format
, bo_offset
, 12, 1);
1086 bt_map
[0] = surface_state
.offset
+ state_offset
;
1087 add_surface_state_reloc(cmd_buffer
, surface_state
, bo
, bo_offset
);
1090 if (map
->surface_count
== 0)
1093 if (map
->image_count
> 0) {
1095 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, stage
, images
);
1096 if (result
!= VK_SUCCESS
)
1099 cmd_buffer
->state
.push_constants_dirty
|= 1 << stage
;
1103 for (uint32_t s
= 0; s
< map
->surface_count
; s
++) {
1104 struct anv_pipeline_binding
*binding
= &map
->surface_to_descriptor
[s
];
1106 struct anv_state surface_state
;
1108 if (binding
->set
== ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
) {
1109 /* Color attachment binding */
1110 assert(stage
== MESA_SHADER_FRAGMENT
);
1111 assert(binding
->binding
== 0);
1112 if (binding
->index
< subpass
->color_count
) {
1113 const unsigned att
= subpass
->color_attachments
[binding
->index
];
1114 surface_state
= cmd_buffer
->state
.attachments
[att
].color_rt_state
;
1116 surface_state
= cmd_buffer
->state
.null_surface_state
;
1119 bt_map
[bias
+ s
] = surface_state
.offset
+ state_offset
;
1123 struct anv_descriptor_set
*set
=
1124 cmd_buffer
->state
.descriptors
[binding
->set
];
1125 uint32_t offset
= set
->layout
->binding
[binding
->binding
].descriptor_index
;
1126 struct anv_descriptor
*desc
= &set
->descriptors
[offset
+ binding
->index
];
1128 switch (desc
->type
) {
1129 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1130 /* Nothing for us to do here */
1133 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1134 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1135 surface_state
= desc
->image_view
->sampler_surface_state
;
1136 assert(surface_state
.alloc_size
);
1137 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1138 desc
->image_view
->image
->aux_usage
,
1142 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1143 assert(stage
== MESA_SHADER_FRAGMENT
);
1144 if (desc
->image_view
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
) {
1145 /* For stencil input attachments, we treat it like any old texture
1146 * that a user may have bound.
1148 surface_state
= desc
->image_view
->sampler_surface_state
;
1149 assert(surface_state
.alloc_size
);
1150 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1151 desc
->image_view
->image
->aux_usage
,
1154 /* For depth and color input attachments, we create the surface
1155 * state at vkBeginRenderPass time so that we can include aux
1156 * and clear color information.
1158 assert(binding
->input_attachment_index
< subpass
->input_count
);
1159 const unsigned subpass_att
= binding
->input_attachment_index
;
1160 const unsigned att
= subpass
->input_attachments
[subpass_att
];
1161 surface_state
= cmd_buffer
->state
.attachments
[att
].input_att_state
;
1165 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
: {
1166 surface_state
= desc
->image_view
->storage_surface_state
;
1167 assert(surface_state
.alloc_size
);
1168 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1169 desc
->image_view
->image
->aux_usage
,
1172 struct brw_image_param
*image_param
=
1173 &cmd_buffer
->state
.push_constants
[stage
]->images
[image
++];
1175 *image_param
= desc
->image_view
->storage_image_param
;
1176 image_param
->surface_idx
= bias
+ s
;
1180 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1181 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1182 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1183 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1184 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1185 surface_state
= desc
->buffer_view
->surface_state
;
1186 assert(surface_state
.alloc_size
);
1187 add_surface_state_reloc(cmd_buffer
, surface_state
,
1188 desc
->buffer_view
->bo
,
1189 desc
->buffer_view
->offset
);
1192 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1193 surface_state
= desc
->buffer_view
->storage_surface_state
;
1194 assert(surface_state
.alloc_size
);
1195 add_surface_state_reloc(cmd_buffer
, surface_state
,
1196 desc
->buffer_view
->bo
,
1197 desc
->buffer_view
->offset
);
1199 struct brw_image_param
*image_param
=
1200 &cmd_buffer
->state
.push_constants
[stage
]->images
[image
++];
1202 *image_param
= desc
->buffer_view
->storage_image_param
;
1203 image_param
->surface_idx
= bias
+ s
;
1207 assert(!"Invalid descriptor type");
1211 bt_map
[bias
+ s
] = surface_state
.offset
+ state_offset
;
1213 assert(image
== map
->image_count
);
1216 if (!cmd_buffer
->device
->info
.has_llc
)
1217 anv_state_clflush(*bt_state
);
1223 emit_samplers(struct anv_cmd_buffer
*cmd_buffer
,
1224 gl_shader_stage stage
,
1225 struct anv_state
*state
)
1227 struct anv_pipeline
*pipeline
;
1229 if (stage
== MESA_SHADER_COMPUTE
)
1230 pipeline
= cmd_buffer
->state
.compute_pipeline
;
1232 pipeline
= cmd_buffer
->state
.pipeline
;
1234 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
1235 *state
= (struct anv_state
) { 0, };
1239 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
1240 if (map
->sampler_count
== 0) {
1241 *state
= (struct anv_state
) { 0, };
1245 uint32_t size
= map
->sampler_count
* 16;
1246 *state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 32);
1248 if (state
->map
== NULL
)
1249 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1251 for (uint32_t s
= 0; s
< map
->sampler_count
; s
++) {
1252 struct anv_pipeline_binding
*binding
= &map
->sampler_to_descriptor
[s
];
1253 struct anv_descriptor_set
*set
=
1254 cmd_buffer
->state
.descriptors
[binding
->set
];
1255 uint32_t offset
= set
->layout
->binding
[binding
->binding
].descriptor_index
;
1256 struct anv_descriptor
*desc
= &set
->descriptors
[offset
+ binding
->index
];
1258 if (desc
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
1259 desc
->type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
1262 struct anv_sampler
*sampler
= desc
->sampler
;
1264 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
1265 * happens to be zero.
1267 if (sampler
== NULL
)
1270 memcpy(state
->map
+ (s
* 16),
1271 sampler
->state
, sizeof(sampler
->state
));
1274 if (!cmd_buffer
->device
->info
.has_llc
)
1275 anv_state_clflush(*state
);
1281 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
1283 VkShaderStageFlags dirty
= cmd_buffer
->state
.descriptors_dirty
&
1284 cmd_buffer
->state
.pipeline
->active_stages
;
1286 VkResult result
= VK_SUCCESS
;
1287 anv_foreach_stage(s
, dirty
) {
1288 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
1289 if (result
!= VK_SUCCESS
)
1291 result
= emit_binding_table(cmd_buffer
, s
,
1292 &cmd_buffer
->state
.binding_tables
[s
]);
1293 if (result
!= VK_SUCCESS
)
1297 if (result
!= VK_SUCCESS
) {
1298 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1300 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
1301 assert(result
== VK_SUCCESS
);
1303 /* Re-emit state base addresses so we get the new surface state base
1304 * address before we start emitting binding tables etc.
1306 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1308 /* Re-emit all active binding tables */
1309 dirty
|= cmd_buffer
->state
.pipeline
->active_stages
;
1310 anv_foreach_stage(s
, dirty
) {
1311 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
1312 if (result
!= VK_SUCCESS
)
1314 result
= emit_binding_table(cmd_buffer
, s
,
1315 &cmd_buffer
->state
.binding_tables
[s
]);
1316 if (result
!= VK_SUCCESS
)
1321 cmd_buffer
->state
.descriptors_dirty
&= ~dirty
;
1327 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
1330 static const uint32_t sampler_state_opcodes
[] = {
1331 [MESA_SHADER_VERTEX
] = 43,
1332 [MESA_SHADER_TESS_CTRL
] = 44, /* HS */
1333 [MESA_SHADER_TESS_EVAL
] = 45, /* DS */
1334 [MESA_SHADER_GEOMETRY
] = 46,
1335 [MESA_SHADER_FRAGMENT
] = 47,
1336 [MESA_SHADER_COMPUTE
] = 0,
1339 static const uint32_t binding_table_opcodes
[] = {
1340 [MESA_SHADER_VERTEX
] = 38,
1341 [MESA_SHADER_TESS_CTRL
] = 39,
1342 [MESA_SHADER_TESS_EVAL
] = 40,
1343 [MESA_SHADER_GEOMETRY
] = 41,
1344 [MESA_SHADER_FRAGMENT
] = 42,
1345 [MESA_SHADER_COMPUTE
] = 0,
1348 anv_foreach_stage(s
, stages
) {
1349 if (cmd_buffer
->state
.samplers
[s
].alloc_size
> 0) {
1350 anv_batch_emit(&cmd_buffer
->batch
,
1351 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS
), ssp
) {
1352 ssp
._3DCommandSubOpcode
= sampler_state_opcodes
[s
];
1353 ssp
.PointertoVSSamplerState
= cmd_buffer
->state
.samplers
[s
].offset
;
1357 /* Always emit binding table pointers if we're asked to, since on SKL
1358 * this is what flushes push constants. */
1359 anv_batch_emit(&cmd_buffer
->batch
,
1360 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS
), btp
) {
1361 btp
._3DCommandSubOpcode
= binding_table_opcodes
[s
];
1362 btp
.PointertoVSBindingTable
= cmd_buffer
->state
.binding_tables
[s
].offset
;
1368 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
1370 static const uint32_t push_constant_opcodes
[] = {
1371 [MESA_SHADER_VERTEX
] = 21,
1372 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
1373 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
1374 [MESA_SHADER_GEOMETRY
] = 22,
1375 [MESA_SHADER_FRAGMENT
] = 23,
1376 [MESA_SHADER_COMPUTE
] = 0,
1379 VkShaderStageFlags flushed
= 0;
1381 anv_foreach_stage(stage
, cmd_buffer
->state
.push_constants_dirty
) {
1382 if (stage
== MESA_SHADER_COMPUTE
)
1385 struct anv_state state
= anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
1387 if (state
.offset
== 0) {
1388 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
)
1389 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
];
1391 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
) {
1392 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
],
1393 c
.ConstantBody
= (struct GENX(3DSTATE_CONSTANT_BODY
)) {
1395 .PointerToConstantBuffer2
= { &cmd_buffer
->device
->dynamic_state_block_pool
.bo
, state
.offset
},
1396 .ConstantBuffer2ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
1398 .PointerToConstantBuffer0
= { .offset
= state
.offset
},
1399 .ConstantBuffer0ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
1405 flushed
|= mesa_to_vk_shader_stage(stage
);
1408 cmd_buffer
->state
.push_constants_dirty
&= ~VK_SHADER_STAGE_ALL_GRAPHICS
;
1414 genX(cmd_buffer_flush_state
)(struct anv_cmd_buffer
*cmd_buffer
)
1416 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1419 uint32_t vb_emit
= cmd_buffer
->state
.vb_dirty
& pipeline
->vb_used
;
1421 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
1423 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
1425 genX(flush_pipeline_select_3d
)(cmd_buffer
);
1428 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
1429 const uint32_t num_dwords
= 1 + num_buffers
* 4;
1431 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
1432 GENX(3DSTATE_VERTEX_BUFFERS
));
1434 for_each_bit(vb
, vb_emit
) {
1435 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
1436 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
1438 struct GENX(VERTEX_BUFFER_STATE
) state
= {
1439 .VertexBufferIndex
= vb
,
1442 .MemoryObjectControlState
= GENX(MOCS
),
1444 .BufferAccessType
= pipeline
->instancing_enable
[vb
] ? INSTANCEDATA
: VERTEXDATA
,
1445 .InstanceDataStepRate
= 1,
1446 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
1449 .AddressModifyEnable
= true,
1450 .BufferPitch
= pipeline
->binding_stride
[vb
],
1451 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
1454 .BufferSize
= buffer
->size
- offset
1456 .EndAddress
= { buffer
->bo
, buffer
->offset
+ buffer
->size
- 1},
1460 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
1465 cmd_buffer
->state
.vb_dirty
&= ~vb_emit
;
1467 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
1468 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
1470 /* The exact descriptor layout is pulled from the pipeline, so we need
1471 * to re-emit binding tables on every pipeline change.
1473 cmd_buffer
->state
.descriptors_dirty
|=
1474 cmd_buffer
->state
.pipeline
->active_stages
;
1476 /* If the pipeline changed, we may need to re-allocate push constant
1479 cmd_buffer_alloc_push_constants(cmd_buffer
);
1483 if (cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_VERTEX_BIT
||
1484 cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_VERTEX_BIT
) {
1485 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
1487 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
1488 * stall needs to be sent just prior to any 3DSTATE_VS,
1489 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
1490 * 3DSTATE_BINDING_TABLE_POINTER_VS,
1491 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
1492 * PIPE_CONTROL needs to be sent before any combination of VS
1493 * associated 3DSTATE."
1495 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1496 pc
.DepthStallEnable
= true;
1497 pc
.PostSyncOperation
= WriteImmediateData
;
1499 (struct anv_address
) { &cmd_buffer
->device
->workaround_bo
, 0 };
1504 /* Render targets live in the same binding table as fragment descriptors */
1505 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_RENDER_TARGETS
)
1506 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
1508 /* We emit the binding tables and sampler tables first, then emit push
1509 * constants and then finally emit binding table and sampler table
1510 * pointers. It has to happen in this order, since emitting the binding
1511 * tables may change the push constants (in case of storage images). After
1512 * emitting push constants, on SKL+ we have to emit the corresponding
1513 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
1516 if (cmd_buffer
->state
.descriptors_dirty
)
1517 dirty
= flush_descriptor_sets(cmd_buffer
);
1519 if (cmd_buffer
->state
.push_constants_dirty
) {
1521 /* On Sky Lake and later, the binding table pointers commands are
1522 * what actually flush the changes to push constant state so we need
1523 * to dirty them so they get re-emitted below.
1525 dirty
|= cmd_buffer_flush_push_constants(cmd_buffer
);
1527 cmd_buffer_flush_push_constants(cmd_buffer
);
1532 cmd_buffer_emit_descriptor_pointers(cmd_buffer
, dirty
);
1534 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
1535 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
1537 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
|
1538 ANV_CMD_DIRTY_PIPELINE
)) {
1539 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer
,
1540 pipeline
->depth_clamp_enable
);
1543 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
)
1544 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
1546 genX(cmd_buffer_flush_dynamic_state
)(cmd_buffer
);
1548 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1552 emit_base_vertex_instance_bo(struct anv_cmd_buffer
*cmd_buffer
,
1553 struct anv_bo
*bo
, uint32_t offset
)
1555 uint32_t *p
= anv_batch_emitn(&cmd_buffer
->batch
, 5,
1556 GENX(3DSTATE_VERTEX_BUFFERS
));
1558 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, p
+ 1,
1559 &(struct GENX(VERTEX_BUFFER_STATE
)) {
1560 .VertexBufferIndex
= 32, /* Reserved for this */
1561 .AddressModifyEnable
= true,
1564 .MemoryObjectControlState
= GENX(MOCS
),
1565 .BufferStartingAddress
= { bo
, offset
},
1568 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
1569 .BufferStartingAddress
= { bo
, offset
},
1570 .EndAddress
= { bo
, offset
+ 8 },
1576 emit_base_vertex_instance(struct anv_cmd_buffer
*cmd_buffer
,
1577 uint32_t base_vertex
, uint32_t base_instance
)
1579 struct anv_state id_state
=
1580 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 8, 4);
1582 ((uint32_t *)id_state
.map
)[0] = base_vertex
;
1583 ((uint32_t *)id_state
.map
)[1] = base_instance
;
1585 if (!cmd_buffer
->device
->info
.has_llc
)
1586 anv_state_clflush(id_state
);
1588 emit_base_vertex_instance_bo(cmd_buffer
,
1589 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
, id_state
.offset
);
1593 VkCommandBuffer commandBuffer
,
1594 uint32_t vertexCount
,
1595 uint32_t instanceCount
,
1596 uint32_t firstVertex
,
1597 uint32_t firstInstance
)
1599 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1600 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1601 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1603 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1605 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1606 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
1608 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1609 prim
.VertexAccessType
= SEQUENTIAL
;
1610 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1611 prim
.VertexCountPerInstance
= vertexCount
;
1612 prim
.StartVertexLocation
= firstVertex
;
1613 prim
.InstanceCount
= instanceCount
;
1614 prim
.StartInstanceLocation
= firstInstance
;
1615 prim
.BaseVertexLocation
= 0;
1619 void genX(CmdDrawIndexed
)(
1620 VkCommandBuffer commandBuffer
,
1621 uint32_t indexCount
,
1622 uint32_t instanceCount
,
1623 uint32_t firstIndex
,
1624 int32_t vertexOffset
,
1625 uint32_t firstInstance
)
1627 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1628 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1629 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1631 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1633 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1634 emit_base_vertex_instance(cmd_buffer
, vertexOffset
, firstInstance
);
1636 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1637 prim
.VertexAccessType
= RANDOM
;
1638 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1639 prim
.VertexCountPerInstance
= indexCount
;
1640 prim
.StartVertexLocation
= firstIndex
;
1641 prim
.InstanceCount
= instanceCount
;
1642 prim
.StartInstanceLocation
= firstInstance
;
1643 prim
.BaseVertexLocation
= vertexOffset
;
1647 /* Auto-Draw / Indirect Registers */
1648 #define GEN7_3DPRIM_END_OFFSET 0x2420
1649 #define GEN7_3DPRIM_START_VERTEX 0x2430
1650 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
1651 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
1652 #define GEN7_3DPRIM_START_INSTANCE 0x243C
1653 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
1655 void genX(CmdDrawIndirect
)(
1656 VkCommandBuffer commandBuffer
,
1658 VkDeviceSize offset
,
1662 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1663 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1664 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1665 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1666 struct anv_bo
*bo
= buffer
->bo
;
1667 uint32_t bo_offset
= buffer
->offset
+ offset
;
1669 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1671 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1672 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 8);
1674 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
1675 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
1676 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
1677 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
1678 emit_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
1680 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1681 prim
.IndirectParameterEnable
= true;
1682 prim
.VertexAccessType
= SEQUENTIAL
;
1683 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1687 void genX(CmdDrawIndexedIndirect
)(
1688 VkCommandBuffer commandBuffer
,
1690 VkDeviceSize offset
,
1694 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1695 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1696 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1697 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1698 struct anv_bo
*bo
= buffer
->bo
;
1699 uint32_t bo_offset
= buffer
->offset
+ offset
;
1701 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1703 /* TODO: We need to stomp base vertex to 0 somehow */
1704 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1705 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 12);
1707 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
1708 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
1709 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
1710 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
1711 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
1713 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1714 prim
.IndirectParameterEnable
= true;
1715 prim
.VertexAccessType
= RANDOM
;
1716 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1721 flush_compute_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
)
1723 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1724 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
1727 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
1728 if (result
!= VK_SUCCESS
) {
1729 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1730 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
1731 assert(result
== VK_SUCCESS
);
1733 /* Re-emit state base addresses so we get the new surface state base
1734 * address before we start emitting binding tables etc.
1736 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1738 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
1739 assert(result
== VK_SUCCESS
);
1742 result
= emit_samplers(cmd_buffer
, MESA_SHADER_COMPUTE
, &samplers
);
1743 assert(result
== VK_SUCCESS
);
1745 uint32_t iface_desc_data_dw
[GENX(INTERFACE_DESCRIPTOR_DATA_length
)];
1746 struct GENX(INTERFACE_DESCRIPTOR_DATA
) desc
= {
1747 .BindingTablePointer
= surfaces
.offset
,
1748 .SamplerStatePointer
= samplers
.offset
,
1750 GENX(INTERFACE_DESCRIPTOR_DATA_pack
)(NULL
, iface_desc_data_dw
, &desc
);
1752 struct anv_state state
=
1753 anv_cmd_buffer_merge_dynamic(cmd_buffer
, iface_desc_data_dw
,
1754 pipeline
->interface_descriptor_data
,
1755 GENX(INTERFACE_DESCRIPTOR_DATA_length
),
1758 uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
1759 anv_batch_emit(&cmd_buffer
->batch
,
1760 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
), mid
) {
1761 mid
.InterfaceDescriptorTotalLength
= size
;
1762 mid
.InterfaceDescriptorDataStartAddress
= state
.offset
;
1769 genX(cmd_buffer_flush_compute_state
)(struct anv_cmd_buffer
*cmd_buffer
)
1771 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1772 MAYBE_UNUSED VkResult result
;
1774 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
1776 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
1778 genX(flush_pipeline_select_gpgpu
)(cmd_buffer
);
1780 if (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
) {
1781 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
1783 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
1784 * the only bits that are changed are scoreboard related: Scoreboard
1785 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
1786 * these scoreboard related states, a MEDIA_STATE_FLUSH is
1789 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
1790 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1792 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
1795 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
1796 (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)) {
1797 /* FIXME: figure out descriptors for gen7 */
1798 result
= flush_compute_descriptor_set(cmd_buffer
);
1799 assert(result
== VK_SUCCESS
);
1800 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
1803 if (cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) {
1804 struct anv_state push_state
=
1805 anv_cmd_buffer_cs_push_constants(cmd_buffer
);
1807 if (push_state
.alloc_size
) {
1808 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
), curbe
) {
1809 curbe
.CURBETotalDataLength
= push_state
.alloc_size
;
1810 curbe
.CURBEDataStartAddress
= push_state
.offset
;
1815 cmd_buffer
->state
.compute_dirty
= 0;
1817 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1823 verify_cmd_parser(const struct anv_device
*device
,
1824 int required_version
,
1825 const char *function
)
1827 if (device
->instance
->physicalDevice
.cmd_parser_version
< required_version
) {
1828 vk_errorf(VK_ERROR_FEATURE_NOT_PRESENT
,
1829 "cmd parser version %d is required for %s",
1830 required_version
, function
);
1839 void genX(CmdDispatch
)(
1840 VkCommandBuffer commandBuffer
,
1845 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1846 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1847 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
1849 if (prog_data
->uses_num_work_groups
) {
1850 struct anv_state state
=
1851 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 12, 4);
1852 uint32_t *sizes
= state
.map
;
1856 if (!cmd_buffer
->device
->info
.has_llc
)
1857 anv_state_clflush(state
);
1858 cmd_buffer
->state
.num_workgroups_offset
= state
.offset
;
1859 cmd_buffer
->state
.num_workgroups_bo
=
1860 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
;
1863 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
1865 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
), ggw
) {
1866 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
1867 ggw
.ThreadDepthCounterMaximum
= 0;
1868 ggw
.ThreadHeightCounterMaximum
= 0;
1869 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
1870 ggw
.ThreadGroupIDXDimension
= x
;
1871 ggw
.ThreadGroupIDYDimension
= y
;
1872 ggw
.ThreadGroupIDZDimension
= z
;
1873 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
1874 ggw
.BottomExecutionMask
= 0xffffffff;
1877 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
1880 #define GPGPU_DISPATCHDIMX 0x2500
1881 #define GPGPU_DISPATCHDIMY 0x2504
1882 #define GPGPU_DISPATCHDIMZ 0x2508
1884 #define MI_PREDICATE_SRC0 0x2400
1885 #define MI_PREDICATE_SRC1 0x2408
1887 void genX(CmdDispatchIndirect
)(
1888 VkCommandBuffer commandBuffer
,
1890 VkDeviceSize offset
)
1892 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1893 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1894 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1895 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
1896 struct anv_bo
*bo
= buffer
->bo
;
1897 uint32_t bo_offset
= buffer
->offset
+ offset
;
1898 struct anv_batch
*batch
= &cmd_buffer
->batch
;
1901 /* Linux 4.4 added command parser version 5 which allows the GPGPU
1902 * indirect dispatch registers to be written.
1904 if (!verify_cmd_parser(cmd_buffer
->device
, 5, "vkCmdDispatchIndirect"))
1908 if (prog_data
->uses_num_work_groups
) {
1909 cmd_buffer
->state
.num_workgroups_offset
= bo_offset
;
1910 cmd_buffer
->state
.num_workgroups_bo
= bo
;
1913 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
1915 emit_lrm(batch
, GPGPU_DISPATCHDIMX
, bo
, bo_offset
);
1916 emit_lrm(batch
, GPGPU_DISPATCHDIMY
, bo
, bo_offset
+ 4);
1917 emit_lrm(batch
, GPGPU_DISPATCHDIMZ
, bo
, bo_offset
+ 8);
1920 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
1921 emit_lri(batch
, MI_PREDICATE_SRC0
+ 4, 0);
1922 emit_lri(batch
, MI_PREDICATE_SRC1
+ 0, 0);
1923 emit_lri(batch
, MI_PREDICATE_SRC1
+ 4, 0);
1925 /* Load compute_dispatch_indirect_x_size into SRC0 */
1926 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 0);
1928 /* predicate = (compute_dispatch_indirect_x_size == 0); */
1929 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
1930 mip
.LoadOperation
= LOAD_LOAD
;
1931 mip
.CombineOperation
= COMBINE_SET
;
1932 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
1935 /* Load compute_dispatch_indirect_y_size into SRC0 */
1936 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 4);
1938 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
1939 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
1940 mip
.LoadOperation
= LOAD_LOAD
;
1941 mip
.CombineOperation
= COMBINE_OR
;
1942 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
1945 /* Load compute_dispatch_indirect_z_size into SRC0 */
1946 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 8);
1948 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
1949 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
1950 mip
.LoadOperation
= LOAD_LOAD
;
1951 mip
.CombineOperation
= COMBINE_OR
;
1952 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
1955 /* predicate = !predicate; */
1956 #define COMPARE_FALSE 1
1957 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
1958 mip
.LoadOperation
= LOAD_LOADINV
;
1959 mip
.CombineOperation
= COMBINE_OR
;
1960 mip
.CompareOperation
= COMPARE_FALSE
;
1964 anv_batch_emit(batch
, GENX(GPGPU_WALKER
), ggw
) {
1965 ggw
.IndirectParameterEnable
= true;
1966 ggw
.PredicateEnable
= GEN_GEN
<= 7;
1967 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
1968 ggw
.ThreadDepthCounterMaximum
= 0;
1969 ggw
.ThreadHeightCounterMaximum
= 0;
1970 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
1971 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
1972 ggw
.BottomExecutionMask
= 0xffffffff;
1975 anv_batch_emit(batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
1979 flush_pipeline_before_pipeline_select(struct anv_cmd_buffer
*cmd_buffer
,
1982 #if GEN_GEN >= 8 && GEN_GEN < 10
1983 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
1985 * Software must clear the COLOR_CALC_STATE Valid field in
1986 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
1987 * with Pipeline Select set to GPGPU.
1989 * The internal hardware docs recommend the same workaround for Gen9
1992 if (pipeline
== GPGPU
)
1993 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), t
);
1995 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
1996 * PIPELINE_SELECT [DevBWR+]":
2000 * Software must ensure all the write caches are flushed through a
2001 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
2002 * command to invalidate read only caches prior to programming
2003 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
2005 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2006 pc
.RenderTargetCacheFlushEnable
= true;
2007 pc
.DepthCacheFlushEnable
= true;
2008 pc
.DCFlushEnable
= true;
2009 pc
.PostSyncOperation
= NoWrite
;
2010 pc
.CommandStreamerStallEnable
= true;
2013 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2014 pc
.TextureCacheInvalidationEnable
= true;
2015 pc
.ConstantCacheInvalidationEnable
= true;
2016 pc
.StateCacheInvalidationEnable
= true;
2017 pc
.InstructionCacheInvalidateEnable
= true;
2018 pc
.PostSyncOperation
= NoWrite
;
2024 genX(flush_pipeline_select_3d
)(struct anv_cmd_buffer
*cmd_buffer
)
2026 if (cmd_buffer
->state
.current_pipeline
!= _3D
) {
2027 flush_pipeline_before_pipeline_select(cmd_buffer
, _3D
);
2029 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
2033 ps
.PipelineSelection
= _3D
;
2036 cmd_buffer
->state
.current_pipeline
= _3D
;
2041 genX(flush_pipeline_select_gpgpu
)(struct anv_cmd_buffer
*cmd_buffer
)
2043 if (cmd_buffer
->state
.current_pipeline
!= GPGPU
) {
2044 flush_pipeline_before_pipeline_select(cmd_buffer
, GPGPU
);
2046 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
2050 ps
.PipelineSelection
= GPGPU
;
2053 cmd_buffer
->state
.current_pipeline
= GPGPU
;
2058 genX(cmd_buffer_emit_gen7_depth_flush
)(struct anv_cmd_buffer
*cmd_buffer
)
2063 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
2065 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
2066 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
2067 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
2068 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
2069 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
2070 * Depth Flush Bit set, followed by another pipelined depth stall
2071 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
2072 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
2073 * via a preceding MI_FLUSH)."
2075 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2076 pipe
.DepthStallEnable
= true;
2078 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2079 pipe
.DepthCacheFlushEnable
= true;
2081 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2082 pipe
.DepthStallEnable
= true;
2087 depth_stencil_surface_type(enum isl_surf_dim dim
)
2090 case ISL_SURF_DIM_1D
:
2092 /* From the Sky Lake PRM, 3DSTATAE_DEPTH_BUFFER::SurfaceType
2094 * Programming Notes:
2095 * The Surface Type of the depth buffer must be the same as the
2096 * Surface Type of the render target(s) (defined in
2097 * SURFACE_STATE), unless either the depth buffer or render
2098 * targets are SURFTYPE_NULL (see exception below for SKL). 1D
2099 * surface type not allowed for depth surface and stencil surface.
2102 * If depth/stencil is enabled with 1D render target,
2103 * depth/stencil surface type needs to be set to 2D surface type
2104 * and height set to 1. Depth will use (legacy) TileY and stencil
2105 * will use TileW. For this case only, the Surface Type of the
2106 * depth buffer can be 2D while the Surface Type of the render
2107 * target(s) are 1D, representing an exception to a programming
2114 case ISL_SURF_DIM_2D
:
2116 case ISL_SURF_DIM_3D
:
2118 /* The Sky Lake docs list the value for 3D as "Reserved". However,
2119 * they have the exact same layout as 2D arrays on gen9+, so we can
2127 unreachable("Invalid surface dimension");
2132 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
2134 struct anv_device
*device
= cmd_buffer
->device
;
2135 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
2136 const struct anv_image_view
*iview
=
2137 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2138 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
2139 const bool has_depth
= image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
2140 const uint32_t ds
= cmd_buffer
->state
.subpass
->depth_stencil_attachment
;
2141 const bool has_hiz
= image
!= NULL
&&
2142 cmd_buffer
->state
.attachments
[ds
].aux_usage
== ISL_AUX_USAGE_HIZ
;
2143 const bool has_stencil
=
2144 image
&& (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
2146 /* FIXME: Implement the PMA stall W/A */
2147 /* FIXME: Width and Height are wrong */
2149 genX(cmd_buffer_emit_gen7_depth_flush
)(cmd_buffer
);
2151 /* Emit 3DSTATE_DEPTH_BUFFER */
2153 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
), db
) {
2155 depth_stencil_surface_type(image
->depth_surface
.isl
.dim
);
2156 db
.DepthWriteEnable
= true;
2157 db
.StencilWriteEnable
= has_stencil
;
2158 db
.HierarchicalDepthBufferEnable
= has_hiz
;
2160 db
.SurfaceFormat
= isl_surf_get_depth_format(&device
->isl_dev
,
2161 &image
->depth_surface
.isl
);
2163 db
.SurfaceBaseAddress
= (struct anv_address
) {
2165 .offset
= image
->offset
+ image
->depth_surface
.offset
,
2167 db
.DepthBufferObjectControlState
= GENX(MOCS
);
2169 db
.SurfacePitch
= image
->depth_surface
.isl
.row_pitch
- 1;
2170 db
.Height
= image
->extent
.height
- 1;
2171 db
.Width
= image
->extent
.width
- 1;
2172 db
.LOD
= iview
->isl
.base_level
;
2173 db
.MinimumArrayElement
= iview
->isl
.base_array_layer
;
2175 assert(image
->depth_surface
.isl
.dim
!= ISL_SURF_DIM_3D
);
2177 db
.RenderTargetViewExtent
=
2178 iview
->isl
.array_len
- iview
->isl
.base_array_layer
- 1;
2182 isl_surf_get_array_pitch_el_rows(&image
->depth_surface
.isl
) >> 2;
2186 /* Even when no depth buffer is present, the hardware requires that
2187 * 3DSTATE_DEPTH_BUFFER be programmed correctly. The Broadwell PRM says:
2189 * If a null depth buffer is bound, the driver must instead bind depth as:
2190 * 3DSTATE_DEPTH.SurfaceType = SURFTYPE_2D
2191 * 3DSTATE_DEPTH.Width = 1
2192 * 3DSTATE_DEPTH.Height = 1
2193 * 3DSTATE_DEPTH.SuraceFormat = D16_UNORM
2194 * 3DSTATE_DEPTH.SurfaceBaseAddress = 0
2195 * 3DSTATE_DEPTH.HierarchicalDepthBufferEnable = 0
2196 * 3DSTATE_WM_DEPTH_STENCIL.DepthTestEnable = 0
2197 * 3DSTATE_WM_DEPTH_STENCIL.DepthBufferWriteEnable = 0
2199 * The PRM is wrong, though. The width and height must be programmed to
2200 * actual framebuffer's width and height, even when neither depth buffer
2201 * nor stencil buffer is present. Also, D16_UNORM is not allowed to
2202 * be combined with a stencil buffer so we use D32_FLOAT instead.
2204 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
), db
) {
2207 depth_stencil_surface_type(image
->stencil_surface
.isl
.dim
);
2209 db
.SurfaceType
= SURFTYPE_2D
;
2211 db
.SurfaceFormat
= D32_FLOAT
;
2212 db
.Width
= MAX2(fb
->width
, 1) - 1;
2213 db
.Height
= MAX2(fb
->height
, 1) - 1;
2214 db
.StencilWriteEnable
= has_stencil
;
2219 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_HIER_DEPTH_BUFFER
), hdb
) {
2220 hdb
.HierarchicalDepthBufferObjectControlState
= GENX(MOCS
);
2221 hdb
.SurfacePitch
= image
->aux_surface
.isl
.row_pitch
- 1;
2222 hdb
.SurfaceBaseAddress
= (struct anv_address
) {
2224 .offset
= image
->offset
+ image
->aux_surface
.offset
,
2227 /* From the SKL PRM Vol2a:
2229 * The interpretation of this field is dependent on Surface Type
2231 * - SURFTYPE_1D: distance in pixels between array slices
2232 * - SURFTYPE_2D/CUBE: distance in rows between array slices
2233 * - SURFTYPE_3D: distance in rows between R - slices
2235 * Unfortunately, the docs aren't 100% accurate here. They fail to
2236 * mention that the 1-D rule only applies to linear 1-D images.
2237 * Since depth and HiZ buffers are always tiled, they are treated as
2238 * 2-D images. Prior to Sky Lake, this field is always in rows.
2241 isl_surf_get_array_pitch_sa_rows(&image
->aux_surface
.isl
) >> 2;
2245 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_HIER_DEPTH_BUFFER
), hdb
);
2248 /* Emit 3DSTATE_STENCIL_BUFFER */
2250 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
), sb
) {
2251 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2252 sb
.StencilBufferEnable
= true;
2254 sb
.StencilBufferObjectControlState
= GENX(MOCS
);
2256 sb
.SurfacePitch
= image
->stencil_surface
.isl
.row_pitch
- 1;
2259 sb
.SurfaceQPitch
= isl_surf_get_array_pitch_el_rows(&image
->stencil_surface
.isl
) >> 2;
2261 sb
.SurfaceBaseAddress
= (struct anv_address
) {
2263 .offset
= image
->offset
+ image
->stencil_surface
.offset
,
2267 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
), sb
);
2270 /* From the IVB PRM Vol2P1, 11.5.5.4 3DSTATE_CLEAR_PARAMS:
2272 * 3DSTATE_CLEAR_PARAMS must always be programmed in the along with
2273 * the other Depth/Stencil state commands(i.e. 3DSTATE_DEPTH_BUFFER,
2274 * 3DSTATE_STENCIL_BUFFER, or 3DSTATE_HIER_DEPTH_BUFFER)
2276 * Testing also shows that some variant of this restriction may exist HSW+.
2277 * On BDW+, it is not possible to emit 2 of these packets consecutively when
2278 * both have DepthClearValueValid set. An analysis of such state programming
2279 * on SKL showed that the GPU doesn't register the latter packet's clear
2282 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CLEAR_PARAMS
), cp
) {
2284 cp
.DepthClearValueValid
= true;
2286 cmd_buffer
->state
.subpass
->depth_stencil_attachment
;
2287 cp
.DepthClearValue
=
2288 cmd_buffer
->state
.attachments
[ds
].clear_value
.depthStencil
.depth
;
2294 genX(cmd_buffer_set_subpass
)(struct anv_cmd_buffer
*cmd_buffer
,
2295 struct anv_subpass
*subpass
)
2297 cmd_buffer
->state
.subpass
= subpass
;
2299 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
2301 const struct anv_image_view
*iview
=
2302 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2305 anv_gen8_hiz_op_resolve(cmd_buffer
, iview
->image
,
2306 BLORP_HIZ_OP_HIZ_RESOLVE
);
2309 cmd_buffer_emit_depth_stencil(cmd_buffer
);
2311 anv_cmd_buffer_clear_subpass(cmd_buffer
);
2314 void genX(CmdBeginRenderPass
)(
2315 VkCommandBuffer commandBuffer
,
2316 const VkRenderPassBeginInfo
* pRenderPassBegin
,
2317 VkSubpassContents contents
)
2319 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2320 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
2321 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
2323 cmd_buffer
->state
.framebuffer
= framebuffer
;
2324 cmd_buffer
->state
.pass
= pass
;
2325 cmd_buffer
->state
.render_area
= pRenderPassBegin
->renderArea
;
2326 genX(cmd_buffer_setup_attachments
)(cmd_buffer
, pass
, pRenderPassBegin
);
2328 genX(flush_pipeline_select_3d
)(cmd_buffer
);
2330 genX(cmd_buffer_set_subpass
)(cmd_buffer
, pass
->subpasses
);
2333 void genX(CmdNextSubpass
)(
2334 VkCommandBuffer commandBuffer
,
2335 VkSubpassContents contents
)
2337 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2339 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
2341 const struct anv_image_view
*iview
=
2342 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2345 anv_gen8_hiz_op_resolve(cmd_buffer
, iview
->image
,
2346 BLORP_HIZ_OP_DEPTH_RESOLVE
);
2349 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
2350 genX(cmd_buffer_set_subpass
)(cmd_buffer
, cmd_buffer
->state
.subpass
+ 1);
2353 void genX(CmdEndRenderPass
)(
2354 VkCommandBuffer commandBuffer
)
2356 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2358 const struct anv_image_view
*iview
=
2359 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2362 anv_gen8_hiz_op_resolve(cmd_buffer
, iview
->image
,
2363 BLORP_HIZ_OP_DEPTH_RESOLVE
);
2366 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
2369 anv_dump_add_framebuffer(cmd_buffer
, cmd_buffer
->state
.framebuffer
);
2374 emit_ps_depth_count(struct anv_cmd_buffer
*cmd_buffer
,
2375 struct anv_bo
*bo
, uint32_t offset
)
2377 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2378 pc
.DestinationAddressType
= DAT_PPGTT
;
2379 pc
.PostSyncOperation
= WritePSDepthCount
;
2380 pc
.DepthStallEnable
= true;
2381 pc
.Address
= (struct anv_address
) { bo
, offset
};
2383 if (GEN_GEN
== 9 && cmd_buffer
->device
->info
.gt
== 4)
2384 pc
.CommandStreamerStallEnable
= true;
2389 emit_query_availability(struct anv_cmd_buffer
*cmd_buffer
,
2390 struct anv_bo
*bo
, uint32_t offset
)
2392 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2393 pc
.DestinationAddressType
= DAT_PPGTT
;
2394 pc
.PostSyncOperation
= WriteImmediateData
;
2395 pc
.Address
= (struct anv_address
) { bo
, offset
};
2396 pc
.ImmediateData
= 1;
2400 void genX(CmdBeginQuery
)(
2401 VkCommandBuffer commandBuffer
,
2402 VkQueryPool queryPool
,
2404 VkQueryControlFlags flags
)
2406 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2407 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
2409 /* Workaround: When meta uses the pipeline with the VS disabled, it seems
2410 * that the pipelining of the depth write breaks. What we see is that
2411 * samples from the render pass clear leaks into the first query
2412 * immediately after the clear. Doing a pipecontrol with a post-sync
2413 * operation and DepthStallEnable seems to work around the issue.
2415 if (cmd_buffer
->state
.need_query_wa
) {
2416 cmd_buffer
->state
.need_query_wa
= false;
2417 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2418 pc
.DepthCacheFlushEnable
= true;
2419 pc
.DepthStallEnable
= true;
2423 switch (pool
->type
) {
2424 case VK_QUERY_TYPE_OCCLUSION
:
2425 emit_ps_depth_count(cmd_buffer
, &pool
->bo
,
2426 query
* sizeof(struct anv_query_pool_slot
));
2429 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
2435 void genX(CmdEndQuery
)(
2436 VkCommandBuffer commandBuffer
,
2437 VkQueryPool queryPool
,
2440 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2441 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
2443 switch (pool
->type
) {
2444 case VK_QUERY_TYPE_OCCLUSION
:
2445 emit_ps_depth_count(cmd_buffer
, &pool
->bo
,
2446 query
* sizeof(struct anv_query_pool_slot
) + 8);
2448 emit_query_availability(cmd_buffer
, &pool
->bo
,
2449 query
* sizeof(struct anv_query_pool_slot
) + 16);
2452 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
2458 #define TIMESTAMP 0x2358
2460 void genX(CmdWriteTimestamp
)(
2461 VkCommandBuffer commandBuffer
,
2462 VkPipelineStageFlagBits pipelineStage
,
2463 VkQueryPool queryPool
,
2466 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2467 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
2468 uint32_t offset
= query
* sizeof(struct anv_query_pool_slot
);
2470 assert(pool
->type
== VK_QUERY_TYPE_TIMESTAMP
);
2472 switch (pipelineStage
) {
2473 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
:
2474 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
2475 srm
.RegisterAddress
= TIMESTAMP
;
2476 srm
.MemoryAddress
= (struct anv_address
) { &pool
->bo
, offset
};
2478 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
2479 srm
.RegisterAddress
= TIMESTAMP
+ 4;
2480 srm
.MemoryAddress
= (struct anv_address
) { &pool
->bo
, offset
+ 4 };
2485 /* Everything else is bottom-of-pipe */
2486 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2487 pc
.DestinationAddressType
= DAT_PPGTT
;
2488 pc
.PostSyncOperation
= WriteTimestamp
;
2489 pc
.Address
= (struct anv_address
) { &pool
->bo
, offset
};
2491 if (GEN_GEN
== 9 && cmd_buffer
->device
->info
.gt
== 4)
2492 pc
.CommandStreamerStallEnable
= true;
2497 emit_query_availability(cmd_buffer
, &pool
->bo
, query
+ 16);
2500 #if GEN_GEN > 7 || GEN_IS_HASWELL
2502 #define alu_opcode(v) __gen_uint((v), 20, 31)
2503 #define alu_operand1(v) __gen_uint((v), 10, 19)
2504 #define alu_operand2(v) __gen_uint((v), 0, 9)
2505 #define alu(opcode, operand1, operand2) \
2506 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
2508 #define OPCODE_NOOP 0x000
2509 #define OPCODE_LOAD 0x080
2510 #define OPCODE_LOADINV 0x480
2511 #define OPCODE_LOAD0 0x081
2512 #define OPCODE_LOAD1 0x481
2513 #define OPCODE_ADD 0x100
2514 #define OPCODE_SUB 0x101
2515 #define OPCODE_AND 0x102
2516 #define OPCODE_OR 0x103
2517 #define OPCODE_XOR 0x104
2518 #define OPCODE_STORE 0x180
2519 #define OPCODE_STOREINV 0x580
2521 #define OPERAND_R0 0x00
2522 #define OPERAND_R1 0x01
2523 #define OPERAND_R2 0x02
2524 #define OPERAND_R3 0x03
2525 #define OPERAND_R4 0x04
2526 #define OPERAND_SRCA 0x20
2527 #define OPERAND_SRCB 0x21
2528 #define OPERAND_ACCU 0x31
2529 #define OPERAND_ZF 0x32
2530 #define OPERAND_CF 0x33
2532 #define CS_GPR(n) (0x2600 + (n) * 8)
2535 emit_load_alu_reg_u64(struct anv_batch
*batch
, uint32_t reg
,
2536 struct anv_bo
*bo
, uint32_t offset
)
2538 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
2539 lrm
.RegisterAddress
= reg
,
2540 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
2542 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
2543 lrm
.RegisterAddress
= reg
+ 4;
2544 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
+ 4 };
2549 store_query_result(struct anv_batch
*batch
, uint32_t reg
,
2550 struct anv_bo
*bo
, uint32_t offset
, VkQueryResultFlags flags
)
2552 anv_batch_emit(batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
2553 srm
.RegisterAddress
= reg
;
2554 srm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
2557 if (flags
& VK_QUERY_RESULT_64_BIT
) {
2558 anv_batch_emit(batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
2559 srm
.RegisterAddress
= reg
+ 4;
2560 srm
.MemoryAddress
= (struct anv_address
) { bo
, offset
+ 4 };
2565 void genX(CmdCopyQueryPoolResults
)(
2566 VkCommandBuffer commandBuffer
,
2567 VkQueryPool queryPool
,
2568 uint32_t firstQuery
,
2569 uint32_t queryCount
,
2570 VkBuffer destBuffer
,
2571 VkDeviceSize destOffset
,
2572 VkDeviceSize destStride
,
2573 VkQueryResultFlags flags
)
2575 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2576 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
2577 ANV_FROM_HANDLE(anv_buffer
, buffer
, destBuffer
);
2578 uint32_t slot_offset
, dst_offset
;
2580 if (flags
& VK_QUERY_RESULT_WAIT_BIT
) {
2581 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2582 pc
.CommandStreamerStallEnable
= true;
2583 pc
.StallAtPixelScoreboard
= true;
2587 dst_offset
= buffer
->offset
+ destOffset
;
2588 for (uint32_t i
= 0; i
< queryCount
; i
++) {
2590 slot_offset
= (firstQuery
+ i
) * sizeof(struct anv_query_pool_slot
);
2591 switch (pool
->type
) {
2592 case VK_QUERY_TYPE_OCCLUSION
:
2593 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
2594 CS_GPR(0), &pool
->bo
, slot_offset
);
2595 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
2596 CS_GPR(1), &pool
->bo
, slot_offset
+ 8);
2598 /* FIXME: We need to clamp the result for 32 bit. */
2600 uint32_t *dw
= anv_batch_emitn(&cmd_buffer
->batch
, 5, GENX(MI_MATH
));
2601 dw
[1] = alu(OPCODE_LOAD
, OPERAND_SRCA
, OPERAND_R1
);
2602 dw
[2] = alu(OPCODE_LOAD
, OPERAND_SRCB
, OPERAND_R0
);
2603 dw
[3] = alu(OPCODE_SUB
, 0, 0);
2604 dw
[4] = alu(OPCODE_STORE
, OPERAND_R2
, OPERAND_ACCU
);
2607 case VK_QUERY_TYPE_TIMESTAMP
:
2608 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
2609 CS_GPR(2), &pool
->bo
, slot_offset
);
2613 unreachable("unhandled query type");
2616 store_query_result(&cmd_buffer
->batch
,
2617 CS_GPR(2), buffer
->bo
, dst_offset
, flags
);
2619 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
2620 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(0),
2621 &pool
->bo
, slot_offset
+ 16);
2622 if (flags
& VK_QUERY_RESULT_64_BIT
)
2623 store_query_result(&cmd_buffer
->batch
,
2624 CS_GPR(0), buffer
->bo
, dst_offset
+ 8, flags
);
2626 store_query_result(&cmd_buffer
->batch
,
2627 CS_GPR(0), buffer
->bo
, dst_offset
+ 4, flags
);
2630 dst_offset
+= destStride
;
2635 void genX(CmdCopyQueryPoolResults
)(
2636 VkCommandBuffer commandBuffer
,
2637 VkQueryPool queryPool
,
2638 uint32_t firstQuery
,
2639 uint32_t queryCount
,
2640 VkBuffer destBuffer
,
2641 VkDeviceSize destOffset
,
2642 VkDeviceSize destStride
,
2643 VkQueryResultFlags flags
)
2645 anv_finishme("Queries not yet supported on Ivy Bridge");