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 surfaces. Compression
307 * is not yet enabled for depth textures and stencil doesn't allow
308 * compression so we can just use the texture surface state from the view.
310 return vk_format_is_color(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
].current_layout
= att
->initial_layout
;
484 state
->attachments
[i
].pending_clear_aspects
= clear_aspects
;
486 state
->attachments
[i
].clear_value
= begin
->pClearValues
[i
];
488 struct anv_image_view
*iview
= framebuffer
->attachments
[i
];
489 assert(iview
->vk_format
== att
->format
);
491 union isl_color_value clear_color
= { .u32
= { 0, } };
492 if (att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
493 color_attachment_compute_aux_usage(cmd_buffer
->device
,
494 &state
->attachments
[i
],
495 iview
, begin
->renderArea
,
498 struct isl_view view
= iview
->isl
;
499 view
.usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
500 isl_surf_fill_state(isl_dev
,
501 state
->attachments
[i
].color_rt_state
.map
,
502 .surf
= &iview
->image
->color_surface
.isl
,
504 .aux_surf
= &iview
->image
->aux_surface
.isl
,
505 .aux_usage
= state
->attachments
[i
].aux_usage
,
506 .clear_color
= clear_color
,
507 .mocs
= cmd_buffer
->device
->default_mocs
);
509 add_image_view_relocs(cmd_buffer
, iview
,
510 state
->attachments
[i
].aux_usage
,
511 state
->attachments
[i
].color_rt_state
);
513 if (iview
->image
->aux_usage
== ISL_AUX_USAGE_HIZ
) {
514 state
->attachments
[i
].aux_usage
=
515 layout_to_hiz_usage(att
->initial_layout
);
517 state
->attachments
[i
].aux_usage
= ISL_AUX_USAGE_NONE
;
519 state
->attachments
[i
].input_aux_usage
= ISL_AUX_USAGE_NONE
;
522 if (need_input_attachment_state(&pass
->attachments
[i
])) {
523 struct isl_view view
= iview
->isl
;
524 view
.usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
525 isl_surf_fill_state(isl_dev
,
526 state
->attachments
[i
].input_att_state
.map
,
527 .surf
= &iview
->image
->color_surface
.isl
,
529 .aux_surf
= &iview
->image
->aux_surface
.isl
,
530 .aux_usage
= state
->attachments
[i
].input_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
].input_aux_usage
,
536 state
->attachments
[i
].input_att_state
);
540 if (!cmd_buffer
->device
->info
.has_llc
)
541 anv_state_clflush(state
->render_pass_states
);
546 genX(BeginCommandBuffer
)(
547 VkCommandBuffer commandBuffer
,
548 const VkCommandBufferBeginInfo
* pBeginInfo
)
550 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
552 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
553 * command buffer's state. Otherwise, we must *reset* its state. In both
556 * From the Vulkan 1.0 spec:
558 * If a command buffer is in the executable state and the command buffer
559 * was allocated from a command pool with the
560 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
561 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
562 * as if vkResetCommandBuffer had been called with
563 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
564 * the command buffer in the recording state.
566 anv_cmd_buffer_reset(cmd_buffer
);
568 cmd_buffer
->usage_flags
= pBeginInfo
->flags
;
570 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
||
571 !(cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
));
573 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
575 if (cmd_buffer
->usage_flags
&
576 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
577 cmd_buffer
->state
.pass
=
578 anv_render_pass_from_handle(pBeginInfo
->pInheritanceInfo
->renderPass
);
579 cmd_buffer
->state
.subpass
=
580 &cmd_buffer
->state
.pass
->subpasses
[pBeginInfo
->pInheritanceInfo
->subpass
];
581 cmd_buffer
->state
.framebuffer
= NULL
;
583 genX(cmd_buffer_setup_attachments
)(cmd_buffer
, cmd_buffer
->state
.pass
,
586 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
593 genX(EndCommandBuffer
)(
594 VkCommandBuffer commandBuffer
)
596 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
598 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
600 anv_cmd_buffer_end_batch_buffer(cmd_buffer
);
606 genX(CmdExecuteCommands
)(
607 VkCommandBuffer commandBuffer
,
608 uint32_t commandBufferCount
,
609 const VkCommandBuffer
* pCmdBuffers
)
611 ANV_FROM_HANDLE(anv_cmd_buffer
, primary
, commandBuffer
);
613 assert(primary
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
615 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
616 ANV_FROM_HANDLE(anv_cmd_buffer
, secondary
, pCmdBuffers
[i
]);
618 assert(secondary
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
620 if (secondary
->usage_flags
&
621 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
) {
622 /* If we're continuing a render pass from the primary, we need to
623 * copy the surface states for the current subpass into the storage
624 * we allocated for them in BeginCommandBuffer.
626 struct anv_bo
*ss_bo
= &primary
->device
->surface_state_block_pool
.bo
;
627 struct anv_state src_state
= primary
->state
.render_pass_states
;
628 struct anv_state dst_state
= secondary
->state
.render_pass_states
;
629 assert(src_state
.alloc_size
== dst_state
.alloc_size
);
631 genX(cmd_buffer_gpu_memcpy
)(primary
, ss_bo
, dst_state
.offset
,
632 ss_bo
, src_state
.offset
,
633 src_state
.alloc_size
);
636 anv_cmd_buffer_add_secondary(primary
, secondary
);
639 /* Each of the secondary command buffers will use its own state base
640 * address. We need to re-emit state base address for the primary after
641 * all of the secondaries are done.
643 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
646 genX(cmd_buffer_emit_state_base_address
)(primary
);
649 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
650 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
651 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
654 * Program the hardware to use the specified L3 configuration.
657 genX(cmd_buffer_config_l3
)(struct anv_cmd_buffer
*cmd_buffer
,
658 const struct gen_l3_config
*cfg
)
661 if (cfg
== cmd_buffer
->state
.current_l3_config
)
664 if (unlikely(INTEL_DEBUG
& DEBUG_L3
)) {
665 fprintf(stderr
, "L3 config transition: ");
666 gen_dump_l3_config(cfg
, stderr
);
669 const bool has_slm
= cfg
->n
[GEN_L3P_SLM
];
671 /* According to the hardware docs, the L3 partitioning can only be changed
672 * while the pipeline is completely drained and the caches are flushed,
673 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
675 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
676 pc
.DCFlushEnable
= true;
677 pc
.PostSyncOperation
= NoWrite
;
678 pc
.CommandStreamerStallEnable
= true;
681 /* ...followed by a second pipelined PIPE_CONTROL that initiates
682 * invalidation of the relevant caches. Note that because RO invalidation
683 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
684 * command is processed by the CS) we cannot combine it with the previous
685 * stalling flush as the hardware documentation suggests, because that
686 * would cause the CS to stall on previous rendering *after* RO
687 * invalidation and wouldn't prevent the RO caches from being polluted by
688 * concurrent rendering before the stall completes. This intentionally
689 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
690 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
691 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
692 * already guarantee that there is no concurrent GPGPU kernel execution
693 * (see SKL HSD 2132585).
695 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
696 pc
.TextureCacheInvalidationEnable
= true;
697 pc
.ConstantCacheInvalidationEnable
= true;
698 pc
.InstructionCacheInvalidateEnable
= true;
699 pc
.StateCacheInvalidationEnable
= true;
700 pc
.PostSyncOperation
= NoWrite
;
703 /* Now send a third stalling flush to make sure that invalidation is
704 * complete when the L3 configuration registers are modified.
706 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
707 pc
.DCFlushEnable
= true;
708 pc
.PostSyncOperation
= NoWrite
;
709 pc
.CommandStreamerStallEnable
= true;
714 assert(!cfg
->n
[GEN_L3P_IS
] && !cfg
->n
[GEN_L3P_C
] && !cfg
->n
[GEN_L3P_T
]);
717 anv_pack_struct(&l3cr
, GENX(L3CNTLREG
),
718 .SLMEnable
= has_slm
,
719 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
720 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
721 .DCAllocation
= cfg
->n
[GEN_L3P_DC
],
722 .AllAllocation
= cfg
->n
[GEN_L3P_ALL
]);
724 /* Set up the L3 partitioning. */
725 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG_num
), l3cr
);
729 const bool has_dc
= cfg
->n
[GEN_L3P_DC
] || cfg
->n
[GEN_L3P_ALL
];
730 const bool has_is
= cfg
->n
[GEN_L3P_IS
] || cfg
->n
[GEN_L3P_RO
] ||
732 const bool has_c
= cfg
->n
[GEN_L3P_C
] || cfg
->n
[GEN_L3P_RO
] ||
734 const bool has_t
= cfg
->n
[GEN_L3P_T
] || cfg
->n
[GEN_L3P_RO
] ||
737 assert(!cfg
->n
[GEN_L3P_ALL
]);
739 /* When enabled SLM only uses a portion of the L3 on half of the banks,
740 * the matching space on the remaining banks has to be allocated to a
741 * client (URB for all validated configurations) set to the
742 * lower-bandwidth 2-bank address hashing mode.
744 const struct gen_device_info
*devinfo
= &cmd_buffer
->device
->info
;
745 const bool urb_low_bw
= has_slm
&& !devinfo
->is_baytrail
;
746 assert(!urb_low_bw
|| cfg
->n
[GEN_L3P_URB
] == cfg
->n
[GEN_L3P_SLM
]);
748 /* Minimum number of ways that can be allocated to the URB. */
749 MAYBE_UNUSED
const unsigned n0_urb
= devinfo
->is_baytrail
? 32 : 0;
750 assert(cfg
->n
[GEN_L3P_URB
] >= n0_urb
);
752 uint32_t l3sqcr1
, l3cr2
, l3cr3
;
753 anv_pack_struct(&l3sqcr1
, GENX(L3SQCREG1
),
754 .ConvertDC_UC
= !has_dc
,
755 .ConvertIS_UC
= !has_is
,
756 .ConvertC_UC
= !has_c
,
757 .ConvertT_UC
= !has_t
);
759 GEN_IS_HASWELL
? HSW_L3SQCREG1_SQGHPCI_DEFAULT
:
760 devinfo
->is_baytrail
? VLV_L3SQCREG1_SQGHPCI_DEFAULT
:
761 IVB_L3SQCREG1_SQGHPCI_DEFAULT
;
763 anv_pack_struct(&l3cr2
, GENX(L3CNTLREG2
),
764 .SLMEnable
= has_slm
,
765 .URBLowBandwidth
= urb_low_bw
,
766 .URBAllocation
= cfg
->n
[GEN_L3P_URB
],
768 .ALLAllocation
= cfg
->n
[GEN_L3P_ALL
],
770 .ROAllocation
= cfg
->n
[GEN_L3P_RO
],
771 .DCAllocation
= cfg
->n
[GEN_L3P_DC
]);
773 anv_pack_struct(&l3cr3
, GENX(L3CNTLREG3
),
774 .ISAllocation
= cfg
->n
[GEN_L3P_IS
],
776 .CAllocation
= cfg
->n
[GEN_L3P_C
],
778 .TAllocation
= cfg
->n
[GEN_L3P_T
],
781 /* Set up the L3 partitioning. */
782 emit_lri(&cmd_buffer
->batch
, GENX(L3SQCREG1_num
), l3sqcr1
);
783 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG2_num
), l3cr2
);
784 emit_lri(&cmd_buffer
->batch
, GENX(L3CNTLREG3_num
), l3cr3
);
787 if (cmd_buffer
->device
->instance
->physicalDevice
.cmd_parser_version
>= 4) {
788 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
789 * them disabled to avoid crashing the system hard.
791 uint32_t scratch1
, chicken3
;
792 anv_pack_struct(&scratch1
, GENX(SCRATCH1
),
793 .L3AtomicDisable
= !has_dc
);
794 anv_pack_struct(&chicken3
, GENX(CHICKEN3
),
795 .L3AtomicDisableMask
= true,
796 .L3AtomicDisable
= !has_dc
);
797 emit_lri(&cmd_buffer
->batch
, GENX(SCRATCH1_num
), scratch1
);
798 emit_lri(&cmd_buffer
->batch
, GENX(CHICKEN3_num
), chicken3
);
804 cmd_buffer
->state
.current_l3_config
= cfg
;
808 genX(cmd_buffer_apply_pipe_flushes
)(struct anv_cmd_buffer
*cmd_buffer
)
810 enum anv_pipe_bits bits
= cmd_buffer
->state
.pending_pipe_bits
;
812 /* Flushes are pipelined while invalidations are handled immediately.
813 * Therefore, if we're flushing anything then we need to schedule a stall
814 * before any invalidations can happen.
816 if (bits
& ANV_PIPE_FLUSH_BITS
)
817 bits
|= ANV_PIPE_NEEDS_CS_STALL_BIT
;
819 /* If we're going to do an invalidate and we have a pending CS stall that
820 * has yet to be resolved, we do the CS stall now.
822 if ((bits
& ANV_PIPE_INVALIDATE_BITS
) &&
823 (bits
& ANV_PIPE_NEEDS_CS_STALL_BIT
)) {
824 bits
|= ANV_PIPE_CS_STALL_BIT
;
825 bits
&= ~ANV_PIPE_NEEDS_CS_STALL_BIT
;
828 if (bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
)) {
829 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
830 pipe
.DepthCacheFlushEnable
= bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
831 pipe
.DCFlushEnable
= bits
& ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
832 pipe
.RenderTargetCacheFlushEnable
=
833 bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
835 pipe
.DepthStallEnable
= bits
& ANV_PIPE_DEPTH_STALL_BIT
;
836 pipe
.CommandStreamerStallEnable
= bits
& ANV_PIPE_CS_STALL_BIT
;
837 pipe
.StallAtPixelScoreboard
= bits
& ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
840 * According to the Broadwell documentation, any PIPE_CONTROL with the
841 * "Command Streamer Stall" bit set must also have another bit set,
842 * with five different options:
844 * - Render Target Cache Flush
845 * - Depth Cache Flush
846 * - Stall at Pixel Scoreboard
847 * - Post-Sync Operation
851 * I chose "Stall at Pixel Scoreboard" since that's what we use in
852 * mesa and it seems to work fine. The choice is fairly arbitrary.
854 if ((bits
& ANV_PIPE_CS_STALL_BIT
) &&
855 !(bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_DEPTH_STALL_BIT
|
856 ANV_PIPE_STALL_AT_SCOREBOARD_BIT
)))
857 pipe
.StallAtPixelScoreboard
= true;
860 bits
&= ~(ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
);
863 if (bits
& ANV_PIPE_INVALIDATE_BITS
) {
864 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
865 pipe
.StateCacheInvalidationEnable
=
866 bits
& ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
867 pipe
.ConstantCacheInvalidationEnable
=
868 bits
& ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
869 pipe
.VFCacheInvalidationEnable
=
870 bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
871 pipe
.TextureCacheInvalidationEnable
=
872 bits
& ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
873 pipe
.InstructionCacheInvalidateEnable
=
874 bits
& ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT
;
877 bits
&= ~ANV_PIPE_INVALIDATE_BITS
;
880 cmd_buffer
->state
.pending_pipe_bits
= bits
;
883 void genX(CmdPipelineBarrier
)(
884 VkCommandBuffer commandBuffer
,
885 VkPipelineStageFlags srcStageMask
,
886 VkPipelineStageFlags destStageMask
,
888 uint32_t memoryBarrierCount
,
889 const VkMemoryBarrier
* pMemoryBarriers
,
890 uint32_t bufferMemoryBarrierCount
,
891 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
892 uint32_t imageMemoryBarrierCount
,
893 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
895 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
898 /* XXX: Right now, we're really dumb and just flush whatever categories
899 * the app asks for. One of these days we may make this a bit better
900 * but right now that's all the hardware allows for in most areas.
902 VkAccessFlags src_flags
= 0;
903 VkAccessFlags dst_flags
= 0;
905 for (uint32_t i
= 0; i
< memoryBarrierCount
; i
++) {
906 src_flags
|= pMemoryBarriers
[i
].srcAccessMask
;
907 dst_flags
|= pMemoryBarriers
[i
].dstAccessMask
;
910 for (uint32_t i
= 0; i
< bufferMemoryBarrierCount
; i
++) {
911 src_flags
|= pBufferMemoryBarriers
[i
].srcAccessMask
;
912 dst_flags
|= pBufferMemoryBarriers
[i
].dstAccessMask
;
915 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
916 src_flags
|= pImageMemoryBarriers
[i
].srcAccessMask
;
917 dst_flags
|= pImageMemoryBarriers
[i
].dstAccessMask
;
918 ANV_FROM_HANDLE(anv_image
, image
, pImageMemoryBarriers
[i
].image
);
919 if (pImageMemoryBarriers
[i
].subresourceRange
.aspectMask
&
920 VK_IMAGE_ASPECT_DEPTH_BIT
) {
921 transition_depth_buffer(cmd_buffer
, image
,
922 pImageMemoryBarriers
[i
].oldLayout
,
923 pImageMemoryBarriers
[i
].newLayout
);
927 enum anv_pipe_bits pipe_bits
= 0;
929 for_each_bit(b
, src_flags
) {
930 switch ((VkAccessFlagBits
)(1 << b
)) {
931 case VK_ACCESS_SHADER_WRITE_BIT
:
932 pipe_bits
|= ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
934 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
:
935 pipe_bits
|= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
937 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
:
938 pipe_bits
|= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
940 case VK_ACCESS_TRANSFER_WRITE_BIT
:
941 pipe_bits
|= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
942 pipe_bits
|= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
945 break; /* Nothing to do */
949 for_each_bit(b
, dst_flags
) {
950 switch ((VkAccessFlagBits
)(1 << b
)) {
951 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT
:
952 case VK_ACCESS_INDEX_READ_BIT
:
953 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT
:
954 pipe_bits
|= ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
956 case VK_ACCESS_UNIFORM_READ_BIT
:
957 pipe_bits
|= ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
958 pipe_bits
|= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
960 case VK_ACCESS_SHADER_READ_BIT
:
961 case VK_ACCESS_INPUT_ATTACHMENT_READ_BIT
:
962 case VK_ACCESS_TRANSFER_READ_BIT
:
963 pipe_bits
|= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
966 break; /* Nothing to do */
970 cmd_buffer
->state
.pending_pipe_bits
|= pipe_bits
;
974 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
976 VkShaderStageFlags stages
= cmd_buffer
->state
.pipeline
->active_stages
;
978 /* In order to avoid thrash, we assume that vertex and fragment stages
979 * always exist. In the rare case where one is missing *and* the other
980 * uses push concstants, this may be suboptimal. However, avoiding stalls
981 * seems more important.
983 stages
|= VK_SHADER_STAGE_FRAGMENT_BIT
| VK_SHADER_STAGE_VERTEX_BIT
;
985 if (stages
== cmd_buffer
->state
.push_constant_stages
)
989 const unsigned push_constant_kb
= 32;
991 const unsigned push_constant_kb
= cmd_buffer
->device
->info
.gt
== 3 ? 32 : 16;
993 const unsigned push_constant_kb
= 16;
996 const unsigned num_stages
=
997 _mesa_bitcount(stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
998 unsigned size_per_stage
= push_constant_kb
/ num_stages
;
1000 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
1001 * units of 2KB. Incidentally, these are the same platforms that have
1002 * 32KB worth of push constant space.
1004 if (push_constant_kb
== 32)
1005 size_per_stage
&= ~1u;
1007 uint32_t kb_used
= 0;
1008 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
1009 unsigned push_size
= (stages
& (1 << i
)) ? size_per_stage
: 0;
1010 anv_batch_emit(&cmd_buffer
->batch
,
1011 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS
), alloc
) {
1012 alloc
._3DCommandSubOpcode
= 18 + i
;
1013 alloc
.ConstantBufferOffset
= (push_size
> 0) ? kb_used
: 0;
1014 alloc
.ConstantBufferSize
= push_size
;
1016 kb_used
+= push_size
;
1019 anv_batch_emit(&cmd_buffer
->batch
,
1020 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS
), alloc
) {
1021 alloc
.ConstantBufferOffset
= kb_used
;
1022 alloc
.ConstantBufferSize
= push_constant_kb
- kb_used
;
1025 cmd_buffer
->state
.push_constant_stages
= stages
;
1027 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
1029 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
1030 * the next 3DPRIMITIVE command after programming the
1031 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
1033 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
1034 * pipeline setup, we need to dirty push constants.
1036 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
1040 emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
1041 gl_shader_stage stage
,
1042 struct anv_state
*bt_state
)
1044 struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1045 struct anv_pipeline
*pipeline
;
1046 uint32_t bias
, state_offset
;
1049 case MESA_SHADER_COMPUTE
:
1050 pipeline
= cmd_buffer
->state
.compute_pipeline
;
1054 pipeline
= cmd_buffer
->state
.pipeline
;
1059 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
1060 *bt_state
= (struct anv_state
) { 0, };
1064 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
1065 if (bias
+ map
->surface_count
== 0) {
1066 *bt_state
= (struct anv_state
) { 0, };
1070 *bt_state
= anv_cmd_buffer_alloc_binding_table(cmd_buffer
,
1071 bias
+ map
->surface_count
,
1073 uint32_t *bt_map
= bt_state
->map
;
1075 if (bt_state
->map
== NULL
)
1076 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1078 if (stage
== MESA_SHADER_COMPUTE
&&
1079 get_cs_prog_data(cmd_buffer
->state
.compute_pipeline
)->uses_num_work_groups
) {
1080 struct anv_bo
*bo
= cmd_buffer
->state
.num_workgroups_bo
;
1081 uint32_t bo_offset
= cmd_buffer
->state
.num_workgroups_offset
;
1083 struct anv_state surface_state
;
1085 anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
1087 const enum isl_format format
=
1088 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
);
1089 anv_fill_buffer_surface_state(cmd_buffer
->device
, surface_state
,
1090 format
, bo_offset
, 12, 1);
1092 bt_map
[0] = surface_state
.offset
+ state_offset
;
1093 add_surface_state_reloc(cmd_buffer
, surface_state
, bo
, bo_offset
);
1096 if (map
->surface_count
== 0)
1099 if (map
->image_count
> 0) {
1101 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, stage
, images
);
1102 if (result
!= VK_SUCCESS
)
1105 cmd_buffer
->state
.push_constants_dirty
|= 1 << stage
;
1109 for (uint32_t s
= 0; s
< map
->surface_count
; s
++) {
1110 struct anv_pipeline_binding
*binding
= &map
->surface_to_descriptor
[s
];
1112 struct anv_state surface_state
;
1114 if (binding
->set
== ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
) {
1115 /* Color attachment binding */
1116 assert(stage
== MESA_SHADER_FRAGMENT
);
1117 assert(binding
->binding
== 0);
1118 if (binding
->index
< subpass
->color_count
) {
1119 const unsigned att
= subpass
->color_attachments
[binding
->index
];
1120 surface_state
= cmd_buffer
->state
.attachments
[att
].color_rt_state
;
1122 surface_state
= cmd_buffer
->state
.null_surface_state
;
1125 bt_map
[bias
+ s
] = surface_state
.offset
+ state_offset
;
1129 struct anv_descriptor_set
*set
=
1130 cmd_buffer
->state
.descriptors
[binding
->set
];
1131 uint32_t offset
= set
->layout
->binding
[binding
->binding
].descriptor_index
;
1132 struct anv_descriptor
*desc
= &set
->descriptors
[offset
+ binding
->index
];
1134 switch (desc
->type
) {
1135 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1136 /* Nothing for us to do here */
1139 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1140 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1141 surface_state
= desc
->image_view
->sampler_surface_state
;
1142 assert(surface_state
.alloc_size
);
1143 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1144 desc
->image_view
->image
->aux_usage
,
1148 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1149 assert(stage
== MESA_SHADER_FRAGMENT
);
1150 if (desc
->image_view
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
) {
1151 /* For stencil input attachments, we treat it like any old texture
1152 * that a user may have bound.
1154 surface_state
= desc
->image_view
->sampler_surface_state
;
1155 assert(surface_state
.alloc_size
);
1156 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1157 desc
->image_view
->image
->aux_usage
,
1160 /* For depth and color input attachments, we create the surface
1161 * state at vkBeginRenderPass time so that we can include aux
1162 * and clear color information.
1164 assert(binding
->input_attachment_index
< subpass
->input_count
);
1165 const unsigned subpass_att
= binding
->input_attachment_index
;
1166 const unsigned att
= subpass
->input_attachments
[subpass_att
];
1167 surface_state
= cmd_buffer
->state
.attachments
[att
].input_att_state
;
1171 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
: {
1172 surface_state
= desc
->image_view
->storage_surface_state
;
1173 assert(surface_state
.alloc_size
);
1174 add_image_view_relocs(cmd_buffer
, desc
->image_view
,
1175 desc
->image_view
->image
->aux_usage
,
1178 struct brw_image_param
*image_param
=
1179 &cmd_buffer
->state
.push_constants
[stage
]->images
[image
++];
1181 *image_param
= desc
->image_view
->storage_image_param
;
1182 image_param
->surface_idx
= bias
+ s
;
1186 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1187 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1188 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1189 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1190 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1191 surface_state
= desc
->buffer_view
->surface_state
;
1192 assert(surface_state
.alloc_size
);
1193 add_surface_state_reloc(cmd_buffer
, surface_state
,
1194 desc
->buffer_view
->bo
,
1195 desc
->buffer_view
->offset
);
1198 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1199 surface_state
= desc
->buffer_view
->storage_surface_state
;
1200 assert(surface_state
.alloc_size
);
1201 add_surface_state_reloc(cmd_buffer
, surface_state
,
1202 desc
->buffer_view
->bo
,
1203 desc
->buffer_view
->offset
);
1205 struct brw_image_param
*image_param
=
1206 &cmd_buffer
->state
.push_constants
[stage
]->images
[image
++];
1208 *image_param
= desc
->buffer_view
->storage_image_param
;
1209 image_param
->surface_idx
= bias
+ s
;
1213 assert(!"Invalid descriptor type");
1217 bt_map
[bias
+ s
] = surface_state
.offset
+ state_offset
;
1219 assert(image
== map
->image_count
);
1222 if (!cmd_buffer
->device
->info
.has_llc
)
1223 anv_state_clflush(*bt_state
);
1229 emit_samplers(struct anv_cmd_buffer
*cmd_buffer
,
1230 gl_shader_stage stage
,
1231 struct anv_state
*state
)
1233 struct anv_pipeline
*pipeline
;
1235 if (stage
== MESA_SHADER_COMPUTE
)
1236 pipeline
= cmd_buffer
->state
.compute_pipeline
;
1238 pipeline
= cmd_buffer
->state
.pipeline
;
1240 if (!anv_pipeline_has_stage(pipeline
, stage
)) {
1241 *state
= (struct anv_state
) { 0, };
1245 struct anv_pipeline_bind_map
*map
= &pipeline
->shaders
[stage
]->bind_map
;
1246 if (map
->sampler_count
== 0) {
1247 *state
= (struct anv_state
) { 0, };
1251 uint32_t size
= map
->sampler_count
* 16;
1252 *state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 32);
1254 if (state
->map
== NULL
)
1255 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1257 for (uint32_t s
= 0; s
< map
->sampler_count
; s
++) {
1258 struct anv_pipeline_binding
*binding
= &map
->sampler_to_descriptor
[s
];
1259 struct anv_descriptor_set
*set
=
1260 cmd_buffer
->state
.descriptors
[binding
->set
];
1261 uint32_t offset
= set
->layout
->binding
[binding
->binding
].descriptor_index
;
1262 struct anv_descriptor
*desc
= &set
->descriptors
[offset
+ binding
->index
];
1264 if (desc
->type
!= VK_DESCRIPTOR_TYPE_SAMPLER
&&
1265 desc
->type
!= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
1268 struct anv_sampler
*sampler
= desc
->sampler
;
1270 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
1271 * happens to be zero.
1273 if (sampler
== NULL
)
1276 memcpy(state
->map
+ (s
* 16),
1277 sampler
->state
, sizeof(sampler
->state
));
1280 if (!cmd_buffer
->device
->info
.has_llc
)
1281 anv_state_clflush(*state
);
1287 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
1289 VkShaderStageFlags dirty
= cmd_buffer
->state
.descriptors_dirty
&
1290 cmd_buffer
->state
.pipeline
->active_stages
;
1292 VkResult result
= VK_SUCCESS
;
1293 anv_foreach_stage(s
, dirty
) {
1294 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
1295 if (result
!= VK_SUCCESS
)
1297 result
= emit_binding_table(cmd_buffer
, s
,
1298 &cmd_buffer
->state
.binding_tables
[s
]);
1299 if (result
!= VK_SUCCESS
)
1303 if (result
!= VK_SUCCESS
) {
1304 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1306 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
1307 assert(result
== VK_SUCCESS
);
1309 /* Re-emit state base addresses so we get the new surface state base
1310 * address before we start emitting binding tables etc.
1312 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1314 /* Re-emit all active binding tables */
1315 dirty
|= cmd_buffer
->state
.pipeline
->active_stages
;
1316 anv_foreach_stage(s
, dirty
) {
1317 result
= emit_samplers(cmd_buffer
, s
, &cmd_buffer
->state
.samplers
[s
]);
1318 if (result
!= VK_SUCCESS
)
1320 result
= emit_binding_table(cmd_buffer
, s
,
1321 &cmd_buffer
->state
.binding_tables
[s
]);
1322 if (result
!= VK_SUCCESS
)
1327 cmd_buffer
->state
.descriptors_dirty
&= ~dirty
;
1333 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
1336 static const uint32_t sampler_state_opcodes
[] = {
1337 [MESA_SHADER_VERTEX
] = 43,
1338 [MESA_SHADER_TESS_CTRL
] = 44, /* HS */
1339 [MESA_SHADER_TESS_EVAL
] = 45, /* DS */
1340 [MESA_SHADER_GEOMETRY
] = 46,
1341 [MESA_SHADER_FRAGMENT
] = 47,
1342 [MESA_SHADER_COMPUTE
] = 0,
1345 static const uint32_t binding_table_opcodes
[] = {
1346 [MESA_SHADER_VERTEX
] = 38,
1347 [MESA_SHADER_TESS_CTRL
] = 39,
1348 [MESA_SHADER_TESS_EVAL
] = 40,
1349 [MESA_SHADER_GEOMETRY
] = 41,
1350 [MESA_SHADER_FRAGMENT
] = 42,
1351 [MESA_SHADER_COMPUTE
] = 0,
1354 anv_foreach_stage(s
, stages
) {
1355 if (cmd_buffer
->state
.samplers
[s
].alloc_size
> 0) {
1356 anv_batch_emit(&cmd_buffer
->batch
,
1357 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS
), ssp
) {
1358 ssp
._3DCommandSubOpcode
= sampler_state_opcodes
[s
];
1359 ssp
.PointertoVSSamplerState
= cmd_buffer
->state
.samplers
[s
].offset
;
1363 /* Always emit binding table pointers if we're asked to, since on SKL
1364 * this is what flushes push constants. */
1365 anv_batch_emit(&cmd_buffer
->batch
,
1366 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS
), btp
) {
1367 btp
._3DCommandSubOpcode
= binding_table_opcodes
[s
];
1368 btp
.PointertoVSBindingTable
= cmd_buffer
->state
.binding_tables
[s
].offset
;
1374 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
1376 static const uint32_t push_constant_opcodes
[] = {
1377 [MESA_SHADER_VERTEX
] = 21,
1378 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
1379 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
1380 [MESA_SHADER_GEOMETRY
] = 22,
1381 [MESA_SHADER_FRAGMENT
] = 23,
1382 [MESA_SHADER_COMPUTE
] = 0,
1385 VkShaderStageFlags flushed
= 0;
1387 anv_foreach_stage(stage
, cmd_buffer
->state
.push_constants_dirty
) {
1388 if (stage
== MESA_SHADER_COMPUTE
)
1391 struct anv_state state
= anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
1393 if (state
.offset
== 0) {
1394 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
)
1395 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
];
1397 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
) {
1398 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
],
1399 c
.ConstantBody
= (struct GENX(3DSTATE_CONSTANT_BODY
)) {
1401 .PointerToConstantBuffer2
= { &cmd_buffer
->device
->dynamic_state_block_pool
.bo
, state
.offset
},
1402 .ConstantBuffer2ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
1404 .PointerToConstantBuffer0
= { .offset
= state
.offset
},
1405 .ConstantBuffer0ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
1411 flushed
|= mesa_to_vk_shader_stage(stage
);
1414 cmd_buffer
->state
.push_constants_dirty
&= ~VK_SHADER_STAGE_ALL_GRAPHICS
;
1420 genX(cmd_buffer_flush_state
)(struct anv_cmd_buffer
*cmd_buffer
)
1422 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1425 uint32_t vb_emit
= cmd_buffer
->state
.vb_dirty
& pipeline
->vb_used
;
1427 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
1429 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
1431 genX(flush_pipeline_select_3d
)(cmd_buffer
);
1434 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
1435 const uint32_t num_dwords
= 1 + num_buffers
* 4;
1437 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
1438 GENX(3DSTATE_VERTEX_BUFFERS
));
1440 for_each_bit(vb
, vb_emit
) {
1441 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
1442 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
1444 struct GENX(VERTEX_BUFFER_STATE
) state
= {
1445 .VertexBufferIndex
= vb
,
1448 .MemoryObjectControlState
= GENX(MOCS
),
1450 .BufferAccessType
= pipeline
->instancing_enable
[vb
] ? INSTANCEDATA
: VERTEXDATA
,
1451 .InstanceDataStepRate
= 1,
1452 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
1455 .AddressModifyEnable
= true,
1456 .BufferPitch
= pipeline
->binding_stride
[vb
],
1457 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
1460 .BufferSize
= buffer
->size
- offset
1462 .EndAddress
= { buffer
->bo
, buffer
->offset
+ buffer
->size
- 1},
1466 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
1471 cmd_buffer
->state
.vb_dirty
&= ~vb_emit
;
1473 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
1474 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
1476 /* The exact descriptor layout is pulled from the pipeline, so we need
1477 * to re-emit binding tables on every pipeline change.
1479 cmd_buffer
->state
.descriptors_dirty
|=
1480 cmd_buffer
->state
.pipeline
->active_stages
;
1482 /* If the pipeline changed, we may need to re-allocate push constant
1485 cmd_buffer_alloc_push_constants(cmd_buffer
);
1489 if (cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_VERTEX_BIT
||
1490 cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_VERTEX_BIT
) {
1491 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
1493 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
1494 * stall needs to be sent just prior to any 3DSTATE_VS,
1495 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
1496 * 3DSTATE_BINDING_TABLE_POINTER_VS,
1497 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
1498 * PIPE_CONTROL needs to be sent before any combination of VS
1499 * associated 3DSTATE."
1501 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1502 pc
.DepthStallEnable
= true;
1503 pc
.PostSyncOperation
= WriteImmediateData
;
1505 (struct anv_address
) { &cmd_buffer
->device
->workaround_bo
, 0 };
1510 /* Render targets live in the same binding table as fragment descriptors */
1511 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_RENDER_TARGETS
)
1512 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
1514 /* We emit the binding tables and sampler tables first, then emit push
1515 * constants and then finally emit binding table and sampler table
1516 * pointers. It has to happen in this order, since emitting the binding
1517 * tables may change the push constants (in case of storage images). After
1518 * emitting push constants, on SKL+ we have to emit the corresponding
1519 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
1522 if (cmd_buffer
->state
.descriptors_dirty
)
1523 dirty
= flush_descriptor_sets(cmd_buffer
);
1525 if (cmd_buffer
->state
.push_constants_dirty
) {
1527 /* On Sky Lake and later, the binding table pointers commands are
1528 * what actually flush the changes to push constant state so we need
1529 * to dirty them so they get re-emitted below.
1531 dirty
|= cmd_buffer_flush_push_constants(cmd_buffer
);
1533 cmd_buffer_flush_push_constants(cmd_buffer
);
1538 cmd_buffer_emit_descriptor_pointers(cmd_buffer
, dirty
);
1540 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
1541 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
1543 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
|
1544 ANV_CMD_DIRTY_PIPELINE
)) {
1545 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer
,
1546 pipeline
->depth_clamp_enable
);
1549 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
)
1550 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
1552 genX(cmd_buffer_flush_dynamic_state
)(cmd_buffer
);
1554 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1558 emit_base_vertex_instance_bo(struct anv_cmd_buffer
*cmd_buffer
,
1559 struct anv_bo
*bo
, uint32_t offset
)
1561 uint32_t *p
= anv_batch_emitn(&cmd_buffer
->batch
, 5,
1562 GENX(3DSTATE_VERTEX_BUFFERS
));
1564 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, p
+ 1,
1565 &(struct GENX(VERTEX_BUFFER_STATE
)) {
1566 .VertexBufferIndex
= 32, /* Reserved for this */
1567 .AddressModifyEnable
= true,
1570 .MemoryObjectControlState
= GENX(MOCS
),
1571 .BufferStartingAddress
= { bo
, offset
},
1574 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
1575 .BufferStartingAddress
= { bo
, offset
},
1576 .EndAddress
= { bo
, offset
+ 8 },
1582 emit_base_vertex_instance(struct anv_cmd_buffer
*cmd_buffer
,
1583 uint32_t base_vertex
, uint32_t base_instance
)
1585 struct anv_state id_state
=
1586 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 8, 4);
1588 ((uint32_t *)id_state
.map
)[0] = base_vertex
;
1589 ((uint32_t *)id_state
.map
)[1] = base_instance
;
1591 if (!cmd_buffer
->device
->info
.has_llc
)
1592 anv_state_clflush(id_state
);
1594 emit_base_vertex_instance_bo(cmd_buffer
,
1595 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
, id_state
.offset
);
1599 VkCommandBuffer commandBuffer
,
1600 uint32_t vertexCount
,
1601 uint32_t instanceCount
,
1602 uint32_t firstVertex
,
1603 uint32_t firstInstance
)
1605 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1606 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1607 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1609 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1611 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1612 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
1614 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1615 prim
.VertexAccessType
= SEQUENTIAL
;
1616 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1617 prim
.VertexCountPerInstance
= vertexCount
;
1618 prim
.StartVertexLocation
= firstVertex
;
1619 prim
.InstanceCount
= instanceCount
;
1620 prim
.StartInstanceLocation
= firstInstance
;
1621 prim
.BaseVertexLocation
= 0;
1625 void genX(CmdDrawIndexed
)(
1626 VkCommandBuffer commandBuffer
,
1627 uint32_t indexCount
,
1628 uint32_t instanceCount
,
1629 uint32_t firstIndex
,
1630 int32_t vertexOffset
,
1631 uint32_t firstInstance
)
1633 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1634 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1635 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1637 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1639 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1640 emit_base_vertex_instance(cmd_buffer
, vertexOffset
, firstInstance
);
1642 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1643 prim
.VertexAccessType
= RANDOM
;
1644 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1645 prim
.VertexCountPerInstance
= indexCount
;
1646 prim
.StartVertexLocation
= firstIndex
;
1647 prim
.InstanceCount
= instanceCount
;
1648 prim
.StartInstanceLocation
= firstInstance
;
1649 prim
.BaseVertexLocation
= vertexOffset
;
1653 /* Auto-Draw / Indirect Registers */
1654 #define GEN7_3DPRIM_END_OFFSET 0x2420
1655 #define GEN7_3DPRIM_START_VERTEX 0x2430
1656 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
1657 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
1658 #define GEN7_3DPRIM_START_INSTANCE 0x243C
1659 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
1661 void genX(CmdDrawIndirect
)(
1662 VkCommandBuffer commandBuffer
,
1664 VkDeviceSize offset
,
1668 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1669 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1670 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1671 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1672 struct anv_bo
*bo
= buffer
->bo
;
1673 uint32_t bo_offset
= buffer
->offset
+ offset
;
1675 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1677 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1678 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 8);
1680 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
1681 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
1682 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
1683 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
1684 emit_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
1686 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1687 prim
.IndirectParameterEnable
= true;
1688 prim
.VertexAccessType
= SEQUENTIAL
;
1689 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1693 void genX(CmdDrawIndexedIndirect
)(
1694 VkCommandBuffer commandBuffer
,
1696 VkDeviceSize offset
,
1700 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1701 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1702 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1703 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1704 struct anv_bo
*bo
= buffer
->bo
;
1705 uint32_t bo_offset
= buffer
->offset
+ offset
;
1707 genX(cmd_buffer_flush_state
)(cmd_buffer
);
1709 /* TODO: We need to stomp base vertex to 0 somehow */
1710 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
1711 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 12);
1713 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
1714 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
1715 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
1716 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
1717 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
1719 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
1720 prim
.IndirectParameterEnable
= true;
1721 prim
.VertexAccessType
= RANDOM
;
1722 prim
.PrimitiveTopologyType
= pipeline
->topology
;
1727 flush_compute_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
)
1729 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1730 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
1733 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
1734 if (result
!= VK_SUCCESS
) {
1735 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1736 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
1737 assert(result
== VK_SUCCESS
);
1739 /* Re-emit state base addresses so we get the new surface state base
1740 * address before we start emitting binding tables etc.
1742 genX(cmd_buffer_emit_state_base_address
)(cmd_buffer
);
1744 result
= emit_binding_table(cmd_buffer
, MESA_SHADER_COMPUTE
, &surfaces
);
1745 assert(result
== VK_SUCCESS
);
1748 result
= emit_samplers(cmd_buffer
, MESA_SHADER_COMPUTE
, &samplers
);
1749 assert(result
== VK_SUCCESS
);
1751 uint32_t iface_desc_data_dw
[GENX(INTERFACE_DESCRIPTOR_DATA_length
)];
1752 struct GENX(INTERFACE_DESCRIPTOR_DATA
) desc
= {
1753 .BindingTablePointer
= surfaces
.offset
,
1754 .SamplerStatePointer
= samplers
.offset
,
1756 GENX(INTERFACE_DESCRIPTOR_DATA_pack
)(NULL
, iface_desc_data_dw
, &desc
);
1758 struct anv_state state
=
1759 anv_cmd_buffer_merge_dynamic(cmd_buffer
, iface_desc_data_dw
,
1760 pipeline
->interface_descriptor_data
,
1761 GENX(INTERFACE_DESCRIPTOR_DATA_length
),
1764 uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
1765 anv_batch_emit(&cmd_buffer
->batch
,
1766 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
), mid
) {
1767 mid
.InterfaceDescriptorTotalLength
= size
;
1768 mid
.InterfaceDescriptorDataStartAddress
= state
.offset
;
1775 genX(cmd_buffer_flush_compute_state
)(struct anv_cmd_buffer
*cmd_buffer
)
1777 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1778 MAYBE_UNUSED VkResult result
;
1780 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
1782 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
->urb
.l3_config
);
1784 genX(flush_pipeline_select_gpgpu
)(cmd_buffer
);
1786 if (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
) {
1787 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
1789 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
1790 * the only bits that are changed are scoreboard related: Scoreboard
1791 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
1792 * these scoreboard related states, a MEDIA_STATE_FLUSH is
1795 cmd_buffer
->state
.pending_pipe_bits
|= ANV_PIPE_CS_STALL_BIT
;
1796 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1798 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
1801 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
1802 (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)) {
1803 /* FIXME: figure out descriptors for gen7 */
1804 result
= flush_compute_descriptor_set(cmd_buffer
);
1805 assert(result
== VK_SUCCESS
);
1806 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
1809 if (cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) {
1810 struct anv_state push_state
=
1811 anv_cmd_buffer_cs_push_constants(cmd_buffer
);
1813 if (push_state
.alloc_size
) {
1814 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
), curbe
) {
1815 curbe
.CURBETotalDataLength
= push_state
.alloc_size
;
1816 curbe
.CURBEDataStartAddress
= push_state
.offset
;
1821 cmd_buffer
->state
.compute_dirty
= 0;
1823 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
1829 verify_cmd_parser(const struct anv_device
*device
,
1830 int required_version
,
1831 const char *function
)
1833 if (device
->instance
->physicalDevice
.cmd_parser_version
< required_version
) {
1834 vk_errorf(VK_ERROR_FEATURE_NOT_PRESENT
,
1835 "cmd parser version %d is required for %s",
1836 required_version
, function
);
1845 void genX(CmdDispatch
)(
1846 VkCommandBuffer commandBuffer
,
1851 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1852 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1853 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
1855 if (prog_data
->uses_num_work_groups
) {
1856 struct anv_state state
=
1857 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 12, 4);
1858 uint32_t *sizes
= state
.map
;
1862 if (!cmd_buffer
->device
->info
.has_llc
)
1863 anv_state_clflush(state
);
1864 cmd_buffer
->state
.num_workgroups_offset
= state
.offset
;
1865 cmd_buffer
->state
.num_workgroups_bo
=
1866 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
;
1869 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
1871 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
), ggw
) {
1872 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
1873 ggw
.ThreadDepthCounterMaximum
= 0;
1874 ggw
.ThreadHeightCounterMaximum
= 0;
1875 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
1876 ggw
.ThreadGroupIDXDimension
= x
;
1877 ggw
.ThreadGroupIDYDimension
= y
;
1878 ggw
.ThreadGroupIDZDimension
= z
;
1879 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
1880 ggw
.BottomExecutionMask
= 0xffffffff;
1883 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
1886 #define GPGPU_DISPATCHDIMX 0x2500
1887 #define GPGPU_DISPATCHDIMY 0x2504
1888 #define GPGPU_DISPATCHDIMZ 0x2508
1890 #define MI_PREDICATE_SRC0 0x2400
1891 #define MI_PREDICATE_SRC1 0x2408
1893 void genX(CmdDispatchIndirect
)(
1894 VkCommandBuffer commandBuffer
,
1896 VkDeviceSize offset
)
1898 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1899 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1900 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
1901 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
1902 struct anv_bo
*bo
= buffer
->bo
;
1903 uint32_t bo_offset
= buffer
->offset
+ offset
;
1904 struct anv_batch
*batch
= &cmd_buffer
->batch
;
1907 /* Linux 4.4 added command parser version 5 which allows the GPGPU
1908 * indirect dispatch registers to be written.
1910 if (!verify_cmd_parser(cmd_buffer
->device
, 5, "vkCmdDispatchIndirect"))
1914 if (prog_data
->uses_num_work_groups
) {
1915 cmd_buffer
->state
.num_workgroups_offset
= bo_offset
;
1916 cmd_buffer
->state
.num_workgroups_bo
= bo
;
1919 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
1921 emit_lrm(batch
, GPGPU_DISPATCHDIMX
, bo
, bo_offset
);
1922 emit_lrm(batch
, GPGPU_DISPATCHDIMY
, bo
, bo_offset
+ 4);
1923 emit_lrm(batch
, GPGPU_DISPATCHDIMZ
, bo
, bo_offset
+ 8);
1926 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
1927 emit_lri(batch
, MI_PREDICATE_SRC0
+ 4, 0);
1928 emit_lri(batch
, MI_PREDICATE_SRC1
+ 0, 0);
1929 emit_lri(batch
, MI_PREDICATE_SRC1
+ 4, 0);
1931 /* Load compute_dispatch_indirect_x_size into SRC0 */
1932 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 0);
1934 /* predicate = (compute_dispatch_indirect_x_size == 0); */
1935 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
1936 mip
.LoadOperation
= LOAD_LOAD
;
1937 mip
.CombineOperation
= COMBINE_SET
;
1938 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
1941 /* Load compute_dispatch_indirect_y_size into SRC0 */
1942 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 4);
1944 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
1945 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
1946 mip
.LoadOperation
= LOAD_LOAD
;
1947 mip
.CombineOperation
= COMBINE_OR
;
1948 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
1951 /* Load compute_dispatch_indirect_z_size into SRC0 */
1952 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 8);
1954 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
1955 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
1956 mip
.LoadOperation
= LOAD_LOAD
;
1957 mip
.CombineOperation
= COMBINE_OR
;
1958 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
1961 /* predicate = !predicate; */
1962 #define COMPARE_FALSE 1
1963 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
1964 mip
.LoadOperation
= LOAD_LOADINV
;
1965 mip
.CombineOperation
= COMBINE_OR
;
1966 mip
.CompareOperation
= COMPARE_FALSE
;
1970 anv_batch_emit(batch
, GENX(GPGPU_WALKER
), ggw
) {
1971 ggw
.IndirectParameterEnable
= true;
1972 ggw
.PredicateEnable
= GEN_GEN
<= 7;
1973 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
1974 ggw
.ThreadDepthCounterMaximum
= 0;
1975 ggw
.ThreadHeightCounterMaximum
= 0;
1976 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
1977 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
1978 ggw
.BottomExecutionMask
= 0xffffffff;
1981 anv_batch_emit(batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
1985 flush_pipeline_before_pipeline_select(struct anv_cmd_buffer
*cmd_buffer
,
1988 #if GEN_GEN >= 8 && GEN_GEN < 10
1989 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
1991 * Software must clear the COLOR_CALC_STATE Valid field in
1992 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
1993 * with Pipeline Select set to GPGPU.
1995 * The internal hardware docs recommend the same workaround for Gen9
1998 if (pipeline
== GPGPU
)
1999 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), t
);
2001 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
2002 * PIPELINE_SELECT [DevBWR+]":
2006 * Software must ensure all the write caches are flushed through a
2007 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
2008 * command to invalidate read only caches prior to programming
2009 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
2011 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2012 pc
.RenderTargetCacheFlushEnable
= true;
2013 pc
.DepthCacheFlushEnable
= true;
2014 pc
.DCFlushEnable
= true;
2015 pc
.PostSyncOperation
= NoWrite
;
2016 pc
.CommandStreamerStallEnable
= true;
2019 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2020 pc
.TextureCacheInvalidationEnable
= true;
2021 pc
.ConstantCacheInvalidationEnable
= true;
2022 pc
.StateCacheInvalidationEnable
= true;
2023 pc
.InstructionCacheInvalidateEnable
= true;
2024 pc
.PostSyncOperation
= NoWrite
;
2030 genX(flush_pipeline_select_3d
)(struct anv_cmd_buffer
*cmd_buffer
)
2032 if (cmd_buffer
->state
.current_pipeline
!= _3D
) {
2033 flush_pipeline_before_pipeline_select(cmd_buffer
, _3D
);
2035 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
2039 ps
.PipelineSelection
= _3D
;
2042 cmd_buffer
->state
.current_pipeline
= _3D
;
2047 genX(flush_pipeline_select_gpgpu
)(struct anv_cmd_buffer
*cmd_buffer
)
2049 if (cmd_buffer
->state
.current_pipeline
!= GPGPU
) {
2050 flush_pipeline_before_pipeline_select(cmd_buffer
, GPGPU
);
2052 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
2056 ps
.PipelineSelection
= GPGPU
;
2059 cmd_buffer
->state
.current_pipeline
= GPGPU
;
2064 genX(cmd_buffer_emit_gen7_depth_flush
)(struct anv_cmd_buffer
*cmd_buffer
)
2069 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
2071 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
2072 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
2073 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
2074 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
2075 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
2076 * Depth Flush Bit set, followed by another pipelined depth stall
2077 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
2078 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
2079 * via a preceding MI_FLUSH)."
2081 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2082 pipe
.DepthStallEnable
= true;
2084 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2085 pipe
.DepthCacheFlushEnable
= true;
2087 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
2088 pipe
.DepthStallEnable
= true;
2093 depth_stencil_surface_type(enum isl_surf_dim dim
)
2096 case ISL_SURF_DIM_1D
:
2098 /* From the Sky Lake PRM, 3DSTATAE_DEPTH_BUFFER::SurfaceType
2100 * Programming Notes:
2101 * The Surface Type of the depth buffer must be the same as the
2102 * Surface Type of the render target(s) (defined in
2103 * SURFACE_STATE), unless either the depth buffer or render
2104 * targets are SURFTYPE_NULL (see exception below for SKL). 1D
2105 * surface type not allowed for depth surface and stencil surface.
2108 * If depth/stencil is enabled with 1D render target,
2109 * depth/stencil surface type needs to be set to 2D surface type
2110 * and height set to 1. Depth will use (legacy) TileY and stencil
2111 * will use TileW. For this case only, the Surface Type of the
2112 * depth buffer can be 2D while the Surface Type of the render
2113 * target(s) are 1D, representing an exception to a programming
2120 case ISL_SURF_DIM_2D
:
2122 case ISL_SURF_DIM_3D
:
2124 /* The Sky Lake docs list the value for 3D as "Reserved". However,
2125 * they have the exact same layout as 2D arrays on gen9+, so we can
2133 unreachable("Invalid surface dimension");
2138 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
2140 struct anv_device
*device
= cmd_buffer
->device
;
2141 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
2142 const struct anv_image_view
*iview
=
2143 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2144 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
2145 const bool has_depth
= image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
2146 const uint32_t ds
= cmd_buffer
->state
.subpass
->depth_stencil_attachment
;
2147 const bool has_hiz
= image
!= NULL
&&
2148 cmd_buffer
->state
.attachments
[ds
].aux_usage
== ISL_AUX_USAGE_HIZ
;
2149 const bool has_stencil
=
2150 image
&& (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
2152 /* FIXME: Implement the PMA stall W/A */
2153 /* FIXME: Width and Height are wrong */
2155 genX(cmd_buffer_emit_gen7_depth_flush
)(cmd_buffer
);
2157 /* Emit 3DSTATE_DEPTH_BUFFER */
2159 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
), db
) {
2161 depth_stencil_surface_type(image
->depth_surface
.isl
.dim
);
2162 db
.DepthWriteEnable
= true;
2163 db
.StencilWriteEnable
= has_stencil
;
2164 db
.HierarchicalDepthBufferEnable
= has_hiz
;
2166 db
.SurfaceFormat
= isl_surf_get_depth_format(&device
->isl_dev
,
2167 &image
->depth_surface
.isl
);
2169 db
.SurfaceBaseAddress
= (struct anv_address
) {
2171 .offset
= image
->offset
+ image
->depth_surface
.offset
,
2173 db
.DepthBufferObjectControlState
= GENX(MOCS
);
2175 db
.SurfacePitch
= image
->depth_surface
.isl
.row_pitch
- 1;
2176 db
.Height
= image
->extent
.height
- 1;
2177 db
.Width
= image
->extent
.width
- 1;
2178 db
.LOD
= iview
->isl
.base_level
;
2179 db
.MinimumArrayElement
= iview
->isl
.base_array_layer
;
2181 assert(image
->depth_surface
.isl
.dim
!= ISL_SURF_DIM_3D
);
2183 db
.RenderTargetViewExtent
=
2184 iview
->isl
.array_len
- iview
->isl
.base_array_layer
- 1;
2188 isl_surf_get_array_pitch_el_rows(&image
->depth_surface
.isl
) >> 2;
2192 /* Even when no depth buffer is present, the hardware requires that
2193 * 3DSTATE_DEPTH_BUFFER be programmed correctly. The Broadwell PRM says:
2195 * If a null depth buffer is bound, the driver must instead bind depth as:
2196 * 3DSTATE_DEPTH.SurfaceType = SURFTYPE_2D
2197 * 3DSTATE_DEPTH.Width = 1
2198 * 3DSTATE_DEPTH.Height = 1
2199 * 3DSTATE_DEPTH.SuraceFormat = D16_UNORM
2200 * 3DSTATE_DEPTH.SurfaceBaseAddress = 0
2201 * 3DSTATE_DEPTH.HierarchicalDepthBufferEnable = 0
2202 * 3DSTATE_WM_DEPTH_STENCIL.DepthTestEnable = 0
2203 * 3DSTATE_WM_DEPTH_STENCIL.DepthBufferWriteEnable = 0
2205 * The PRM is wrong, though. The width and height must be programmed to
2206 * actual framebuffer's width and height, even when neither depth buffer
2207 * nor stencil buffer is present. Also, D16_UNORM is not allowed to
2208 * be combined with a stencil buffer so we use D32_FLOAT instead.
2210 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
), db
) {
2213 depth_stencil_surface_type(image
->stencil_surface
.isl
.dim
);
2215 db
.SurfaceType
= SURFTYPE_2D
;
2217 db
.SurfaceFormat
= D32_FLOAT
;
2218 db
.Width
= MAX2(fb
->width
, 1) - 1;
2219 db
.Height
= MAX2(fb
->height
, 1) - 1;
2220 db
.StencilWriteEnable
= has_stencil
;
2225 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_HIER_DEPTH_BUFFER
), hdb
) {
2226 hdb
.HierarchicalDepthBufferObjectControlState
= GENX(MOCS
);
2227 hdb
.SurfacePitch
= image
->aux_surface
.isl
.row_pitch
- 1;
2228 hdb
.SurfaceBaseAddress
= (struct anv_address
) {
2230 .offset
= image
->offset
+ image
->aux_surface
.offset
,
2233 /* From the SKL PRM Vol2a:
2235 * The interpretation of this field is dependent on Surface Type
2237 * - SURFTYPE_1D: distance in pixels between array slices
2238 * - SURFTYPE_2D/CUBE: distance in rows between array slices
2239 * - SURFTYPE_3D: distance in rows between R - slices
2241 * Unfortunately, the docs aren't 100% accurate here. They fail to
2242 * mention that the 1-D rule only applies to linear 1-D images.
2243 * Since depth and HiZ buffers are always tiled, they are treated as
2244 * 2-D images. Prior to Sky Lake, this field is always in rows.
2247 isl_surf_get_array_pitch_sa_rows(&image
->aux_surface
.isl
) >> 2;
2251 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_HIER_DEPTH_BUFFER
), hdb
);
2254 /* Emit 3DSTATE_STENCIL_BUFFER */
2256 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
), sb
) {
2257 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2258 sb
.StencilBufferEnable
= true;
2260 sb
.StencilBufferObjectControlState
= GENX(MOCS
);
2262 sb
.SurfacePitch
= image
->stencil_surface
.isl
.row_pitch
- 1;
2265 sb
.SurfaceQPitch
= isl_surf_get_array_pitch_el_rows(&image
->stencil_surface
.isl
) >> 2;
2267 sb
.SurfaceBaseAddress
= (struct anv_address
) {
2269 .offset
= image
->offset
+ image
->stencil_surface
.offset
,
2273 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
), sb
);
2276 /* From the IVB PRM Vol2P1, 11.5.5.4 3DSTATE_CLEAR_PARAMS:
2278 * 3DSTATE_CLEAR_PARAMS must always be programmed in the along with
2279 * the other Depth/Stencil state commands(i.e. 3DSTATE_DEPTH_BUFFER,
2280 * 3DSTATE_STENCIL_BUFFER, or 3DSTATE_HIER_DEPTH_BUFFER)
2282 * Testing also shows that some variant of this restriction may exist HSW+.
2283 * On BDW+, it is not possible to emit 2 of these packets consecutively when
2284 * both have DepthClearValueValid set. An analysis of such state programming
2285 * on SKL showed that the GPU doesn't register the latter packet's clear
2288 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CLEAR_PARAMS
), cp
) {
2290 cp
.DepthClearValueValid
= true;
2291 cp
.DepthClearValue
= ANV_HZ_FC_VAL
;
2297 genX(cmd_buffer_set_subpass
)(struct anv_cmd_buffer
*cmd_buffer
,
2298 struct anv_subpass
*subpass
)
2300 cmd_buffer
->state
.subpass
= subpass
;
2302 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_RENDER_TARGETS
;
2304 const struct anv_image_view
*iview
=
2305 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2307 if (iview
&& iview
->image
->aux_usage
== ISL_AUX_USAGE_HIZ
) {
2308 const uint32_t ds
= subpass
->depth_stencil_attachment
;
2309 transition_depth_buffer(cmd_buffer
, iview
->image
,
2310 cmd_buffer
->state
.attachments
[ds
].current_layout
,
2311 cmd_buffer
->state
.subpass
->depth_stencil_layout
);
2312 cmd_buffer
->state
.attachments
[ds
].current_layout
=
2313 cmd_buffer
->state
.subpass
->depth_stencil_layout
;
2314 cmd_buffer
->state
.attachments
[ds
].aux_usage
=
2315 layout_to_hiz_usage(cmd_buffer
->state
.subpass
->depth_stencil_layout
);
2318 cmd_buffer_emit_depth_stencil(cmd_buffer
);
2320 anv_cmd_buffer_clear_subpass(cmd_buffer
);
2323 void genX(CmdBeginRenderPass
)(
2324 VkCommandBuffer commandBuffer
,
2325 const VkRenderPassBeginInfo
* pRenderPassBegin
,
2326 VkSubpassContents contents
)
2328 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2329 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
2330 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
2332 cmd_buffer
->state
.framebuffer
= framebuffer
;
2333 cmd_buffer
->state
.pass
= pass
;
2334 cmd_buffer
->state
.render_area
= pRenderPassBegin
->renderArea
;
2335 genX(cmd_buffer_setup_attachments
)(cmd_buffer
, pass
, pRenderPassBegin
);
2337 genX(flush_pipeline_select_3d
)(cmd_buffer
);
2339 genX(cmd_buffer_set_subpass
)(cmd_buffer
, pass
->subpasses
);
2342 void genX(CmdNextSubpass
)(
2343 VkCommandBuffer commandBuffer
,
2344 VkSubpassContents contents
)
2346 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2348 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
2350 const struct anv_image_view
*iview
=
2351 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2353 if (iview
&& iview
->image
->aux_usage
== ISL_AUX_USAGE_HIZ
) {
2354 const uint32_t ds
= cmd_buffer
->state
.subpass
->depth_stencil_attachment
;
2356 if (cmd_buffer
->state
.subpass
- cmd_buffer
->state
.pass
->subpasses
==
2357 cmd_buffer
->state
.pass
->attachments
[ds
].last_subpass_idx
) {
2358 transition_depth_buffer(cmd_buffer
, iview
->image
,
2359 cmd_buffer
->state
.attachments
[ds
].current_layout
,
2360 cmd_buffer
->state
.pass
->attachments
[ds
].final_layout
);
2364 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
2365 genX(cmd_buffer_set_subpass
)(cmd_buffer
, cmd_buffer
->state
.subpass
+ 1);
2368 void genX(CmdEndRenderPass
)(
2369 VkCommandBuffer commandBuffer
)
2371 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2373 const struct anv_image_view
*iview
=
2374 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
2376 if (iview
&& iview
->image
->aux_usage
== ISL_AUX_USAGE_HIZ
) {
2377 const uint32_t ds
= cmd_buffer
->state
.subpass
->depth_stencil_attachment
;
2379 if (cmd_buffer
->state
.subpass
- cmd_buffer
->state
.pass
->subpasses
==
2380 cmd_buffer
->state
.pass
->attachments
[ds
].last_subpass_idx
) {
2381 transition_depth_buffer(cmd_buffer
, iview
->image
,
2382 cmd_buffer
->state
.attachments
[ds
].current_layout
,
2383 cmd_buffer
->state
.pass
->attachments
[ds
].final_layout
);
2387 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
2390 anv_dump_add_framebuffer(cmd_buffer
, cmd_buffer
->state
.framebuffer
);
2395 emit_ps_depth_count(struct anv_cmd_buffer
*cmd_buffer
,
2396 struct anv_bo
*bo
, uint32_t offset
)
2398 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2399 pc
.DestinationAddressType
= DAT_PPGTT
;
2400 pc
.PostSyncOperation
= WritePSDepthCount
;
2401 pc
.DepthStallEnable
= true;
2402 pc
.Address
= (struct anv_address
) { bo
, offset
};
2404 if (GEN_GEN
== 9 && cmd_buffer
->device
->info
.gt
== 4)
2405 pc
.CommandStreamerStallEnable
= true;
2410 emit_query_availability(struct anv_cmd_buffer
*cmd_buffer
,
2411 struct anv_bo
*bo
, uint32_t offset
)
2413 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2414 pc
.DestinationAddressType
= DAT_PPGTT
;
2415 pc
.PostSyncOperation
= WriteImmediateData
;
2416 pc
.Address
= (struct anv_address
) { bo
, offset
};
2417 pc
.ImmediateData
= 1;
2421 void genX(CmdBeginQuery
)(
2422 VkCommandBuffer commandBuffer
,
2423 VkQueryPool queryPool
,
2425 VkQueryControlFlags flags
)
2427 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2428 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
2430 /* Workaround: When meta uses the pipeline with the VS disabled, it seems
2431 * that the pipelining of the depth write breaks. What we see is that
2432 * samples from the render pass clear leaks into the first query
2433 * immediately after the clear. Doing a pipecontrol with a post-sync
2434 * operation and DepthStallEnable seems to work around the issue.
2436 if (cmd_buffer
->state
.need_query_wa
) {
2437 cmd_buffer
->state
.need_query_wa
= false;
2438 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2439 pc
.DepthCacheFlushEnable
= true;
2440 pc
.DepthStallEnable
= true;
2444 switch (pool
->type
) {
2445 case VK_QUERY_TYPE_OCCLUSION
:
2446 emit_ps_depth_count(cmd_buffer
, &pool
->bo
,
2447 query
* sizeof(struct anv_query_pool_slot
));
2450 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
2456 void genX(CmdEndQuery
)(
2457 VkCommandBuffer commandBuffer
,
2458 VkQueryPool queryPool
,
2461 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2462 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
2464 switch (pool
->type
) {
2465 case VK_QUERY_TYPE_OCCLUSION
:
2466 emit_ps_depth_count(cmd_buffer
, &pool
->bo
,
2467 query
* sizeof(struct anv_query_pool_slot
) + 8);
2469 emit_query_availability(cmd_buffer
, &pool
->bo
,
2470 query
* sizeof(struct anv_query_pool_slot
) + 16);
2473 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
2479 #define TIMESTAMP 0x2358
2481 void genX(CmdWriteTimestamp
)(
2482 VkCommandBuffer commandBuffer
,
2483 VkPipelineStageFlagBits pipelineStage
,
2484 VkQueryPool queryPool
,
2487 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2488 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
2489 uint32_t offset
= query
* sizeof(struct anv_query_pool_slot
);
2491 assert(pool
->type
== VK_QUERY_TYPE_TIMESTAMP
);
2493 switch (pipelineStage
) {
2494 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
:
2495 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
2496 srm
.RegisterAddress
= TIMESTAMP
;
2497 srm
.MemoryAddress
= (struct anv_address
) { &pool
->bo
, offset
};
2499 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
2500 srm
.RegisterAddress
= TIMESTAMP
+ 4;
2501 srm
.MemoryAddress
= (struct anv_address
) { &pool
->bo
, offset
+ 4 };
2506 /* Everything else is bottom-of-pipe */
2507 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2508 pc
.DestinationAddressType
= DAT_PPGTT
;
2509 pc
.PostSyncOperation
= WriteTimestamp
;
2510 pc
.Address
= (struct anv_address
) { &pool
->bo
, offset
};
2512 if (GEN_GEN
== 9 && cmd_buffer
->device
->info
.gt
== 4)
2513 pc
.CommandStreamerStallEnable
= true;
2518 emit_query_availability(cmd_buffer
, &pool
->bo
, query
+ 16);
2521 #if GEN_GEN > 7 || GEN_IS_HASWELL
2523 #define alu_opcode(v) __gen_uint((v), 20, 31)
2524 #define alu_operand1(v) __gen_uint((v), 10, 19)
2525 #define alu_operand2(v) __gen_uint((v), 0, 9)
2526 #define alu(opcode, operand1, operand2) \
2527 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
2529 #define OPCODE_NOOP 0x000
2530 #define OPCODE_LOAD 0x080
2531 #define OPCODE_LOADINV 0x480
2532 #define OPCODE_LOAD0 0x081
2533 #define OPCODE_LOAD1 0x481
2534 #define OPCODE_ADD 0x100
2535 #define OPCODE_SUB 0x101
2536 #define OPCODE_AND 0x102
2537 #define OPCODE_OR 0x103
2538 #define OPCODE_XOR 0x104
2539 #define OPCODE_STORE 0x180
2540 #define OPCODE_STOREINV 0x580
2542 #define OPERAND_R0 0x00
2543 #define OPERAND_R1 0x01
2544 #define OPERAND_R2 0x02
2545 #define OPERAND_R3 0x03
2546 #define OPERAND_R4 0x04
2547 #define OPERAND_SRCA 0x20
2548 #define OPERAND_SRCB 0x21
2549 #define OPERAND_ACCU 0x31
2550 #define OPERAND_ZF 0x32
2551 #define OPERAND_CF 0x33
2553 #define CS_GPR(n) (0x2600 + (n) * 8)
2556 emit_load_alu_reg_u64(struct anv_batch
*batch
, uint32_t reg
,
2557 struct anv_bo
*bo
, uint32_t offset
)
2559 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
2560 lrm
.RegisterAddress
= reg
,
2561 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
2563 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
2564 lrm
.RegisterAddress
= reg
+ 4;
2565 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
+ 4 };
2570 store_query_result(struct anv_batch
*batch
, uint32_t reg
,
2571 struct anv_bo
*bo
, uint32_t offset
, VkQueryResultFlags flags
)
2573 anv_batch_emit(batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
2574 srm
.RegisterAddress
= reg
;
2575 srm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
2578 if (flags
& VK_QUERY_RESULT_64_BIT
) {
2579 anv_batch_emit(batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
2580 srm
.RegisterAddress
= reg
+ 4;
2581 srm
.MemoryAddress
= (struct anv_address
) { bo
, offset
+ 4 };
2586 void genX(CmdCopyQueryPoolResults
)(
2587 VkCommandBuffer commandBuffer
,
2588 VkQueryPool queryPool
,
2589 uint32_t firstQuery
,
2590 uint32_t queryCount
,
2591 VkBuffer destBuffer
,
2592 VkDeviceSize destOffset
,
2593 VkDeviceSize destStride
,
2594 VkQueryResultFlags flags
)
2596 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2597 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
2598 ANV_FROM_HANDLE(anv_buffer
, buffer
, destBuffer
);
2599 uint32_t slot_offset
, dst_offset
;
2601 if (flags
& VK_QUERY_RESULT_WAIT_BIT
) {
2602 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
2603 pc
.CommandStreamerStallEnable
= true;
2604 pc
.StallAtPixelScoreboard
= true;
2608 dst_offset
= buffer
->offset
+ destOffset
;
2609 for (uint32_t i
= 0; i
< queryCount
; i
++) {
2611 slot_offset
= (firstQuery
+ i
) * sizeof(struct anv_query_pool_slot
);
2612 switch (pool
->type
) {
2613 case VK_QUERY_TYPE_OCCLUSION
:
2614 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
2615 CS_GPR(0), &pool
->bo
, slot_offset
);
2616 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
2617 CS_GPR(1), &pool
->bo
, slot_offset
+ 8);
2619 /* FIXME: We need to clamp the result for 32 bit. */
2621 uint32_t *dw
= anv_batch_emitn(&cmd_buffer
->batch
, 5, GENX(MI_MATH
));
2622 dw
[1] = alu(OPCODE_LOAD
, OPERAND_SRCA
, OPERAND_R1
);
2623 dw
[2] = alu(OPCODE_LOAD
, OPERAND_SRCB
, OPERAND_R0
);
2624 dw
[3] = alu(OPCODE_SUB
, 0, 0);
2625 dw
[4] = alu(OPCODE_STORE
, OPERAND_R2
, OPERAND_ACCU
);
2628 case VK_QUERY_TYPE_TIMESTAMP
:
2629 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
2630 CS_GPR(2), &pool
->bo
, slot_offset
);
2634 unreachable("unhandled query type");
2637 store_query_result(&cmd_buffer
->batch
,
2638 CS_GPR(2), buffer
->bo
, dst_offset
, flags
);
2640 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
2641 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(0),
2642 &pool
->bo
, slot_offset
+ 16);
2643 if (flags
& VK_QUERY_RESULT_64_BIT
)
2644 store_query_result(&cmd_buffer
->batch
,
2645 CS_GPR(0), buffer
->bo
, dst_offset
+ 8, flags
);
2647 store_query_result(&cmd_buffer
->batch
,
2648 CS_GPR(0), buffer
->bo
, dst_offset
+ 4, flags
);
2651 dst_offset
+= destStride
;
2656 void genX(CmdCopyQueryPoolResults
)(
2657 VkCommandBuffer commandBuffer
,
2658 VkQueryPool queryPool
,
2659 uint32_t firstQuery
,
2660 uint32_t queryCount
,
2661 VkBuffer destBuffer
,
2662 VkDeviceSize destOffset
,
2663 VkDeviceSize destStride
,
2664 VkQueryResultFlags flags
)
2666 anv_finishme("Queries not yet supported on Ivy Bridge");