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"
29 #include "genxml/gen_macros.h"
30 #include "genxml/genX_pack.h"
33 emit_lrm(struct anv_batch
*batch
,
34 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
36 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
37 lrm
.RegisterAddress
= reg
;
38 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
43 emit_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
45 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
46 lri
.RegisterOffset
= reg
;
52 genX(cmd_buffer_emit_state_base_address
)(struct anv_cmd_buffer
*cmd_buffer
)
54 struct anv_device
*device
= cmd_buffer
->device
;
56 /* XXX: Do we need this on more than just BDW? */
58 /* Emit a render target cache flush.
60 * This isn't documented anywhere in the PRM. However, it seems to be
61 * necessary prior to changing the surface state base adress. Without
62 * this, we get GPU hangs when using multi-level command buffers which
63 * clear depth, reset state base address, and then go render stuff.
65 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
66 pc
.RenderTargetCacheFlushEnable
= true;
70 anv_batch_emit(&cmd_buffer
->batch
, GENX(STATE_BASE_ADDRESS
), sba
) {
71 sba
.GeneralStateBaseAddress
= (struct anv_address
) { NULL
, 0 };
72 sba
.GeneralStateMemoryObjectControlState
= GENX(MOCS
);
73 sba
.GeneralStateBaseAddressModifyEnable
= true;
75 sba
.SurfaceStateBaseAddress
=
76 anv_cmd_buffer_surface_base_address(cmd_buffer
);
77 sba
.SurfaceStateMemoryObjectControlState
= GENX(MOCS
);
78 sba
.SurfaceStateBaseAddressModifyEnable
= true;
80 sba
.DynamicStateBaseAddress
=
81 (struct anv_address
) { &device
->dynamic_state_block_pool
.bo
, 0 };
82 sba
.DynamicStateMemoryObjectControlState
= GENX(MOCS
),
83 sba
.DynamicStateBaseAddressModifyEnable
= true,
85 sba
.IndirectObjectBaseAddress
= (struct anv_address
) { NULL
, 0 };
86 sba
.IndirectObjectMemoryObjectControlState
= GENX(MOCS
);
87 sba
.IndirectObjectBaseAddressModifyEnable
= true;
89 sba
.InstructionBaseAddress
=
90 (struct anv_address
) { &device
->instruction_block_pool
.bo
, 0 };
91 sba
.InstructionMemoryObjectControlState
= GENX(MOCS
);
92 sba
.InstructionBaseAddressModifyEnable
= true;
95 /* Broadwell requires that we specify a buffer size for a bunch of
96 * these fields. However, since we will be growing the BO's live, we
97 * just set them all to the maximum.
99 sba
.GeneralStateBufferSize
= 0xfffff;
100 sba
.GeneralStateBufferSizeModifyEnable
= true;
101 sba
.DynamicStateBufferSize
= 0xfffff;
102 sba
.DynamicStateBufferSizeModifyEnable
= true;
103 sba
.IndirectObjectBufferSize
= 0xfffff;
104 sba
.IndirectObjectBufferSizeModifyEnable
= true;
105 sba
.InstructionBufferSize
= 0xfffff;
106 sba
.InstructionBuffersizeModifyEnable
= true;
110 /* After re-setting the surface state base address, we have to do some
111 * cache flusing so that the sampler engine will pick up the new
112 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
113 * Shared Function > 3D Sampler > State > State Caching (page 96):
115 * Coherency with system memory in the state cache, like the texture
116 * cache is handled partially by software. It is expected that the
117 * command stream or shader will issue Cache Flush operation or
118 * Cache_Flush sampler message to ensure that the L1 cache remains
119 * coherent with system memory.
123 * Whenever the value of the Dynamic_State_Base_Addr,
124 * Surface_State_Base_Addr are altered, the L1 state cache must be
125 * invalidated to ensure the new surface or sampler state is fetched
126 * from system memory.
128 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
129 * which, according the PIPE_CONTROL instruction documentation in the
132 * Setting this bit is independent of any other bit in this packet.
133 * This bit controls the invalidation of the L1 and L2 state caches
134 * at the top of the pipe i.e. at the parsing time.
136 * Unfortunately, experimentation seems to indicate that state cache
137 * invalidation through a PIPE_CONTROL does nothing whatsoever in
138 * regards to surface state and binding tables. In stead, it seems that
139 * invalidating the texture cache is what is actually needed.
141 * XXX: As far as we have been able to determine through
142 * experimentation, shows that flush the texture cache appears to be
143 * sufficient. The theory here is that all of the sampling/rendering
144 * units cache the binding table in the texture cache. However, we have
145 * yet to be able to actually confirm this.
147 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
148 pc
.TextureCacheInvalidationEnable
= true;
153 genX(cmd_buffer_apply_pipe_flushes
)(struct anv_cmd_buffer
*cmd_buffer
)
155 enum anv_pipe_bits bits
= cmd_buffer
->state
.pending_pipe_bits
;
157 /* Flushes are pipelined while invalidations are handled immediately.
158 * Therefore, if we're flushing anything then we need to schedule a stall
159 * before any invalidations can happen.
161 if (bits
& ANV_PIPE_FLUSH_BITS
)
162 bits
|= ANV_PIPE_NEEDS_CS_STALL_BIT
;
164 /* If we're going to do an invalidate and we have a pending CS stall that
165 * has yet to be resolved, we do the CS stall now.
167 if ((bits
& ANV_PIPE_INVALIDATE_BITS
) &&
168 (bits
& ANV_PIPE_NEEDS_CS_STALL_BIT
)) {
169 bits
|= ANV_PIPE_CS_STALL_BIT
;
170 bits
&= ~ANV_PIPE_NEEDS_CS_STALL_BIT
;
173 if (bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
)) {
174 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
175 pipe
.DepthCacheFlushEnable
= bits
& ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
176 pipe
.DCFlushEnable
= bits
& ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
177 pipe
.RenderTargetCacheFlushEnable
=
178 bits
& ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
180 pipe
.DepthStallEnable
= bits
& ANV_PIPE_DEPTH_STALL_BIT
;
181 pipe
.CommandStreamerStallEnable
= bits
& ANV_PIPE_CS_STALL_BIT
;
182 pipe
.StallAtPixelScoreboard
= bits
& ANV_PIPE_STALL_AT_SCOREBOARD_BIT
;
185 * According to the Broadwell documentation, any PIPE_CONTROL with the
186 * "Command Streamer Stall" bit set must also have another bit set,
187 * with five different options:
189 * - Render Target Cache Flush
190 * - Depth Cache Flush
191 * - Stall at Pixel Scoreboard
192 * - Post-Sync Operation
196 * I chose "Stall at Pixel Scoreboard" since that's what we use in
197 * mesa and it seems to work fine. The choice is fairly arbitrary.
199 if ((bits
& ANV_PIPE_CS_STALL_BIT
) &&
200 !(bits
& (ANV_PIPE_FLUSH_BITS
| ANV_PIPE_DEPTH_STALL_BIT
|
201 ANV_PIPE_STALL_AT_SCOREBOARD_BIT
)))
202 pipe
.StallAtPixelScoreboard
= true;
205 bits
&= ~(ANV_PIPE_FLUSH_BITS
| ANV_PIPE_CS_STALL_BIT
);
208 if (bits
& ANV_PIPE_INVALIDATE_BITS
) {
209 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pipe
) {
210 pipe
.StateCacheInvalidationEnable
=
211 bits
& ANV_PIPE_STATE_CACHE_INVALIDATE_BIT
;
212 pipe
.ConstantCacheInvalidationEnable
=
213 bits
& ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
214 pipe
.VFCacheInvalidationEnable
=
215 bits
& ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
216 pipe
.TextureCacheInvalidationEnable
=
217 bits
& ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
218 pipe
.InstructionCacheInvalidateEnable
=
219 bits
& ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT
;
222 bits
&= ~ANV_PIPE_INVALIDATE_BITS
;
225 cmd_buffer
->state
.pending_pipe_bits
= bits
;
228 void genX(CmdPipelineBarrier
)(
229 VkCommandBuffer commandBuffer
,
230 VkPipelineStageFlags srcStageMask
,
231 VkPipelineStageFlags destStageMask
,
233 uint32_t memoryBarrierCount
,
234 const VkMemoryBarrier
* pMemoryBarriers
,
235 uint32_t bufferMemoryBarrierCount
,
236 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
237 uint32_t imageMemoryBarrierCount
,
238 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
240 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
243 /* XXX: Right now, we're really dumb and just flush whatever categories
244 * the app asks for. One of these days we may make this a bit better
245 * but right now that's all the hardware allows for in most areas.
247 VkAccessFlags src_flags
= 0;
248 VkAccessFlags dst_flags
= 0;
250 for (uint32_t i
= 0; i
< memoryBarrierCount
; i
++) {
251 src_flags
|= pMemoryBarriers
[i
].srcAccessMask
;
252 dst_flags
|= pMemoryBarriers
[i
].dstAccessMask
;
255 for (uint32_t i
= 0; i
< bufferMemoryBarrierCount
; i
++) {
256 src_flags
|= pBufferMemoryBarriers
[i
].srcAccessMask
;
257 dst_flags
|= pBufferMemoryBarriers
[i
].dstAccessMask
;
260 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
261 src_flags
|= pImageMemoryBarriers
[i
].srcAccessMask
;
262 dst_flags
|= pImageMemoryBarriers
[i
].dstAccessMask
;
265 enum anv_pipe_bits pipe_bits
= 0;
267 for_each_bit(b
, src_flags
) {
268 switch ((VkAccessFlagBits
)(1 << b
)) {
269 case VK_ACCESS_SHADER_WRITE_BIT
:
270 pipe_bits
|= ANV_PIPE_DATA_CACHE_FLUSH_BIT
;
272 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
:
273 pipe_bits
|= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
275 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
:
276 pipe_bits
|= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
278 case VK_ACCESS_TRANSFER_WRITE_BIT
:
279 pipe_bits
|= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT
;
280 pipe_bits
|= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT
;
283 break; /* Nothing to do */
287 for_each_bit(b
, dst_flags
) {
288 switch ((VkAccessFlagBits
)(1 << b
)) {
289 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT
:
290 case VK_ACCESS_INDEX_READ_BIT
:
291 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT
:
292 pipe_bits
|= ANV_PIPE_VF_CACHE_INVALIDATE_BIT
;
294 case VK_ACCESS_UNIFORM_READ_BIT
:
295 pipe_bits
|= ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT
;
296 pipe_bits
|= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
298 case VK_ACCESS_SHADER_READ_BIT
:
299 case VK_ACCESS_COLOR_ATTACHMENT_READ_BIT
:
300 case VK_ACCESS_TRANSFER_READ_BIT
:
301 pipe_bits
|= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT
;
304 break; /* Nothing to do */
308 cmd_buffer
->state
.pending_pipe_bits
|= pipe_bits
;
312 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
314 VkShaderStageFlags stages
= cmd_buffer
->state
.pipeline
->active_stages
;
316 /* In order to avoid thrash, we assume that vertex and fragment stages
317 * always exist. In the rare case where one is missing *and* the other
318 * uses push concstants, this may be suboptimal. However, avoiding stalls
319 * seems more important.
321 stages
|= VK_SHADER_STAGE_FRAGMENT_BIT
| VK_SHADER_STAGE_VERTEX_BIT
;
323 if (stages
== cmd_buffer
->state
.push_constant_stages
)
327 const unsigned push_constant_kb
= 32;
329 const unsigned push_constant_kb
= cmd_buffer
->device
->info
.gt
== 3 ? 32 : 16;
331 const unsigned push_constant_kb
= 16;
334 const unsigned num_stages
=
335 _mesa_bitcount(stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
336 unsigned size_per_stage
= push_constant_kb
/ num_stages
;
338 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
339 * units of 2KB. Incidentally, these are the same platforms that have
340 * 32KB worth of push constant space.
342 if (push_constant_kb
== 32)
343 size_per_stage
&= ~1u;
345 uint32_t kb_used
= 0;
346 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
347 unsigned push_size
= (stages
& (1 << i
)) ? size_per_stage
: 0;
348 anv_batch_emit(&cmd_buffer
->batch
,
349 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS
), alloc
) {
350 alloc
._3DCommandSubOpcode
= 18 + i
;
351 alloc
.ConstantBufferOffset
= (push_size
> 0) ? kb_used
: 0;
352 alloc
.ConstantBufferSize
= push_size
;
354 kb_used
+= push_size
;
357 anv_batch_emit(&cmd_buffer
->batch
,
358 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS
), alloc
) {
359 alloc
.ConstantBufferOffset
= kb_used
;
360 alloc
.ConstantBufferSize
= push_constant_kb
- kb_used
;
363 cmd_buffer
->state
.push_constant_stages
= stages
;
365 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
367 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
368 * the next 3DPRIMITIVE command after programming the
369 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
371 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
372 * pipeline setup, we need to dirty push constants.
374 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
378 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
381 static const uint32_t sampler_state_opcodes
[] = {
382 [MESA_SHADER_VERTEX
] = 43,
383 [MESA_SHADER_TESS_CTRL
] = 44, /* HS */
384 [MESA_SHADER_TESS_EVAL
] = 45, /* DS */
385 [MESA_SHADER_GEOMETRY
] = 46,
386 [MESA_SHADER_FRAGMENT
] = 47,
387 [MESA_SHADER_COMPUTE
] = 0,
390 static const uint32_t binding_table_opcodes
[] = {
391 [MESA_SHADER_VERTEX
] = 38,
392 [MESA_SHADER_TESS_CTRL
] = 39,
393 [MESA_SHADER_TESS_EVAL
] = 40,
394 [MESA_SHADER_GEOMETRY
] = 41,
395 [MESA_SHADER_FRAGMENT
] = 42,
396 [MESA_SHADER_COMPUTE
] = 0,
399 anv_foreach_stage(s
, stages
) {
400 if (cmd_buffer
->state
.samplers
[s
].alloc_size
> 0) {
401 anv_batch_emit(&cmd_buffer
->batch
,
402 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS
), ssp
) {
403 ssp
._3DCommandSubOpcode
= sampler_state_opcodes
[s
];
404 ssp
.PointertoVSSamplerState
= cmd_buffer
->state
.samplers
[s
].offset
;
408 /* Always emit binding table pointers if we're asked to, since on SKL
409 * this is what flushes push constants. */
410 anv_batch_emit(&cmd_buffer
->batch
,
411 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS
), btp
) {
412 btp
._3DCommandSubOpcode
= binding_table_opcodes
[s
];
413 btp
.PointertoVSBindingTable
= cmd_buffer
->state
.binding_tables
[s
].offset
;
419 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
421 static const uint32_t push_constant_opcodes
[] = {
422 [MESA_SHADER_VERTEX
] = 21,
423 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
424 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
425 [MESA_SHADER_GEOMETRY
] = 22,
426 [MESA_SHADER_FRAGMENT
] = 23,
427 [MESA_SHADER_COMPUTE
] = 0,
430 VkShaderStageFlags flushed
= 0;
432 anv_foreach_stage(stage
, cmd_buffer
->state
.push_constants_dirty
) {
433 if (stage
== MESA_SHADER_COMPUTE
)
436 struct anv_state state
= anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
438 if (state
.offset
== 0) {
439 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
)
440 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
];
442 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
), c
) {
443 c
._3DCommandSubOpcode
= push_constant_opcodes
[stage
],
444 c
.ConstantBody
= (struct GENX(3DSTATE_CONSTANT_BODY
)) {
446 .PointerToConstantBuffer2
= { &cmd_buffer
->device
->dynamic_state_block_pool
.bo
, state
.offset
},
447 .ConstantBuffer2ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
449 .PointerToConstantBuffer0
= { .offset
= state
.offset
},
450 .ConstantBuffer0ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
456 flushed
|= mesa_to_vk_shader_stage(stage
);
459 cmd_buffer
->state
.push_constants_dirty
&= ~VK_SHADER_STAGE_ALL_GRAPHICS
;
465 genX(cmd_buffer_flush_state
)(struct anv_cmd_buffer
*cmd_buffer
)
467 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
470 uint32_t vb_emit
= cmd_buffer
->state
.vb_dirty
& pipeline
->vb_used
;
472 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
474 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
);
476 genX(flush_pipeline_select_3d
)(cmd_buffer
);
479 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
480 const uint32_t num_dwords
= 1 + num_buffers
* 4;
482 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
483 GENX(3DSTATE_VERTEX_BUFFERS
));
485 for_each_bit(vb
, vb_emit
) {
486 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
487 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
489 struct GENX(VERTEX_BUFFER_STATE
) state
= {
490 .VertexBufferIndex
= vb
,
493 .MemoryObjectControlState
= GENX(MOCS
),
495 .BufferAccessType
= pipeline
->instancing_enable
[vb
] ? INSTANCEDATA
: VERTEXDATA
,
496 .InstanceDataStepRate
= 1,
497 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
500 .AddressModifyEnable
= true,
501 .BufferPitch
= pipeline
->binding_stride
[vb
],
502 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
505 .BufferSize
= buffer
->size
- offset
507 .EndAddress
= { buffer
->bo
, buffer
->offset
+ buffer
->size
- 1},
511 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
516 cmd_buffer
->state
.vb_dirty
&= ~vb_emit
;
518 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
519 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
521 /* The exact descriptor layout is pulled from the pipeline, so we need
522 * to re-emit binding tables on every pipeline change.
524 cmd_buffer
->state
.descriptors_dirty
|=
525 cmd_buffer
->state
.pipeline
->active_stages
;
527 /* If the pipeline changed, we may need to re-allocate push constant
530 cmd_buffer_alloc_push_constants(cmd_buffer
);
534 if (cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_VERTEX_BIT
||
535 cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_VERTEX_BIT
) {
536 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
538 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
539 * stall needs to be sent just prior to any 3DSTATE_VS,
540 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
541 * 3DSTATE_BINDING_TABLE_POINTER_VS,
542 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
543 * PIPE_CONTROL needs to be sent before any combination of VS
544 * associated 3DSTATE."
546 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
547 pc
.DepthStallEnable
= true;
548 pc
.PostSyncOperation
= WriteImmediateData
;
550 (struct anv_address
) { &cmd_buffer
->device
->workaround_bo
, 0 };
555 /* We emit the binding tables and sampler tables first, then emit push
556 * constants and then finally emit binding table and sampler table
557 * pointers. It has to happen in this order, since emitting the binding
558 * tables may change the push constants (in case of storage images). After
559 * emitting push constants, on SKL+ we have to emit the corresponding
560 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
563 if (cmd_buffer
->state
.descriptors_dirty
)
564 dirty
= anv_cmd_buffer_flush_descriptor_sets(cmd_buffer
);
566 if (cmd_buffer
->state
.push_constants_dirty
) {
568 /* On Sky Lake and later, the binding table pointers commands are
569 * what actually flush the changes to push constant state so we need
570 * to dirty them so they get re-emitted below.
572 dirty
|= cmd_buffer_flush_push_constants(cmd_buffer
);
574 cmd_buffer_flush_push_constants(cmd_buffer
);
579 cmd_buffer_emit_descriptor_pointers(cmd_buffer
, dirty
);
581 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
582 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
584 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
|
585 ANV_CMD_DIRTY_PIPELINE
)) {
586 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer
,
587 pipeline
->depth_clamp_enable
);
590 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
)
591 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
593 genX(cmd_buffer_flush_dynamic_state
)(cmd_buffer
);
595 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
599 emit_base_vertex_instance_bo(struct anv_cmd_buffer
*cmd_buffer
,
600 struct anv_bo
*bo
, uint32_t offset
)
602 uint32_t *p
= anv_batch_emitn(&cmd_buffer
->batch
, 5,
603 GENX(3DSTATE_VERTEX_BUFFERS
));
605 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, p
+ 1,
606 &(struct GENX(VERTEX_BUFFER_STATE
)) {
607 .VertexBufferIndex
= 32, /* Reserved for this */
608 .AddressModifyEnable
= true,
611 .MemoryObjectControlState
= GENX(MOCS
),
612 .BufferStartingAddress
= { bo
, offset
},
615 .VertexBufferMemoryObjectControlState
= GENX(MOCS
),
616 .BufferStartingAddress
= { bo
, offset
},
617 .EndAddress
= { bo
, offset
+ 8 },
623 emit_base_vertex_instance(struct anv_cmd_buffer
*cmd_buffer
,
624 uint32_t base_vertex
, uint32_t base_instance
)
626 struct anv_state id_state
=
627 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 8, 4);
629 ((uint32_t *)id_state
.map
)[0] = base_vertex
;
630 ((uint32_t *)id_state
.map
)[1] = base_instance
;
632 if (!cmd_buffer
->device
->info
.has_llc
)
633 anv_state_clflush(id_state
);
635 emit_base_vertex_instance_bo(cmd_buffer
,
636 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
, id_state
.offset
);
640 VkCommandBuffer commandBuffer
,
641 uint32_t vertexCount
,
642 uint32_t instanceCount
,
643 uint32_t firstVertex
,
644 uint32_t firstInstance
)
646 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
647 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
648 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
650 genX(cmd_buffer_flush_state
)(cmd_buffer
);
652 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
653 emit_base_vertex_instance(cmd_buffer
, firstVertex
, firstInstance
);
655 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
656 prim
.VertexAccessType
= SEQUENTIAL
;
657 prim
.PrimitiveTopologyType
= pipeline
->topology
;
658 prim
.VertexCountPerInstance
= vertexCount
;
659 prim
.StartVertexLocation
= firstVertex
;
660 prim
.InstanceCount
= instanceCount
;
661 prim
.StartInstanceLocation
= firstInstance
;
662 prim
.BaseVertexLocation
= 0;
666 void genX(CmdDrawIndexed
)(
667 VkCommandBuffer commandBuffer
,
669 uint32_t instanceCount
,
671 int32_t vertexOffset
,
672 uint32_t firstInstance
)
674 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
675 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
676 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
678 genX(cmd_buffer_flush_state
)(cmd_buffer
);
680 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
681 emit_base_vertex_instance(cmd_buffer
, vertexOffset
, firstInstance
);
683 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
684 prim
.VertexAccessType
= RANDOM
;
685 prim
.PrimitiveTopologyType
= pipeline
->topology
;
686 prim
.VertexCountPerInstance
= indexCount
;
687 prim
.StartVertexLocation
= firstIndex
;
688 prim
.InstanceCount
= instanceCount
;
689 prim
.StartInstanceLocation
= firstInstance
;
690 prim
.BaseVertexLocation
= vertexOffset
;
694 /* Auto-Draw / Indirect Registers */
695 #define GEN7_3DPRIM_END_OFFSET 0x2420
696 #define GEN7_3DPRIM_START_VERTEX 0x2430
697 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
698 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
699 #define GEN7_3DPRIM_START_INSTANCE 0x243C
700 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
702 void genX(CmdDrawIndirect
)(
703 VkCommandBuffer commandBuffer
,
709 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
710 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
711 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
712 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
713 struct anv_bo
*bo
= buffer
->bo
;
714 uint32_t bo_offset
= buffer
->offset
+ offset
;
716 genX(cmd_buffer_flush_state
)(cmd_buffer
);
718 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
719 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 8);
721 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
722 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
723 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
724 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
725 emit_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
727 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
728 prim
.IndirectParameterEnable
= true;
729 prim
.VertexAccessType
= SEQUENTIAL
;
730 prim
.PrimitiveTopologyType
= pipeline
->topology
;
734 void genX(CmdDrawIndexedIndirect
)(
735 VkCommandBuffer commandBuffer
,
741 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
742 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
743 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
744 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
745 struct anv_bo
*bo
= buffer
->bo
;
746 uint32_t bo_offset
= buffer
->offset
+ offset
;
748 genX(cmd_buffer_flush_state
)(cmd_buffer
);
750 /* TODO: We need to stomp base vertex to 0 somehow */
751 if (vs_prog_data
->uses_basevertex
|| vs_prog_data
->uses_baseinstance
)
752 emit_base_vertex_instance_bo(cmd_buffer
, bo
, bo_offset
+ 12);
754 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
755 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
756 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
757 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
758 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
760 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
), prim
) {
761 prim
.IndirectParameterEnable
= true;
762 prim
.VertexAccessType
= RANDOM
;
763 prim
.PrimitiveTopologyType
= pipeline
->topology
;
770 verify_cmd_parser(const struct anv_device
*device
,
771 int required_version
,
772 const char *function
)
774 if (device
->instance
->physicalDevice
.cmd_parser_version
< required_version
) {
775 vk_errorf(VK_ERROR_FEATURE_NOT_PRESENT
,
776 "cmd parser version %d is required for %s",
777 required_version
, function
);
786 void genX(CmdDispatch
)(
787 VkCommandBuffer commandBuffer
,
792 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
793 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
794 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
796 if (prog_data
->uses_num_work_groups
) {
797 struct anv_state state
=
798 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, 12, 4);
799 uint32_t *sizes
= state
.map
;
803 if (!cmd_buffer
->device
->info
.has_llc
)
804 anv_state_clflush(state
);
805 cmd_buffer
->state
.num_workgroups_offset
= state
.offset
;
806 cmd_buffer
->state
.num_workgroups_bo
=
807 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
;
810 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
812 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
), ggw
) {
813 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
814 ggw
.ThreadDepthCounterMaximum
= 0;
815 ggw
.ThreadHeightCounterMaximum
= 0;
816 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
817 ggw
.ThreadGroupIDXDimension
= x
;
818 ggw
.ThreadGroupIDYDimension
= y
;
819 ggw
.ThreadGroupIDZDimension
= z
;
820 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
821 ggw
.BottomExecutionMask
= 0xffffffff;
824 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
827 #define GPGPU_DISPATCHDIMX 0x2500
828 #define GPGPU_DISPATCHDIMY 0x2504
829 #define GPGPU_DISPATCHDIMZ 0x2508
831 #define MI_PREDICATE_SRC0 0x2400
832 #define MI_PREDICATE_SRC1 0x2408
834 void genX(CmdDispatchIndirect
)(
835 VkCommandBuffer commandBuffer
,
839 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
840 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
841 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
842 const struct brw_cs_prog_data
*prog_data
= get_cs_prog_data(pipeline
);
843 struct anv_bo
*bo
= buffer
->bo
;
844 uint32_t bo_offset
= buffer
->offset
+ offset
;
845 struct anv_batch
*batch
= &cmd_buffer
->batch
;
848 /* Linux 4.4 added command parser version 5 which allows the GPGPU
849 * indirect dispatch registers to be written.
851 if (!verify_cmd_parser(cmd_buffer
->device
, 5, "vkCmdDispatchIndirect"))
855 if (prog_data
->uses_num_work_groups
) {
856 cmd_buffer
->state
.num_workgroups_offset
= bo_offset
;
857 cmd_buffer
->state
.num_workgroups_bo
= bo
;
860 genX(cmd_buffer_flush_compute_state
)(cmd_buffer
);
862 emit_lrm(batch
, GPGPU_DISPATCHDIMX
, bo
, bo_offset
);
863 emit_lrm(batch
, GPGPU_DISPATCHDIMY
, bo
, bo_offset
+ 4);
864 emit_lrm(batch
, GPGPU_DISPATCHDIMZ
, bo
, bo_offset
+ 8);
867 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
868 emit_lri(batch
, MI_PREDICATE_SRC0
+ 4, 0);
869 emit_lri(batch
, MI_PREDICATE_SRC1
+ 0, 0);
870 emit_lri(batch
, MI_PREDICATE_SRC1
+ 4, 0);
872 /* Load compute_dispatch_indirect_x_size into SRC0 */
873 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 0);
875 /* predicate = (compute_dispatch_indirect_x_size == 0); */
876 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
877 mip
.LoadOperation
= LOAD_LOAD
;
878 mip
.CombineOperation
= COMBINE_SET
;
879 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
882 /* Load compute_dispatch_indirect_y_size into SRC0 */
883 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 4);
885 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
886 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
887 mip
.LoadOperation
= LOAD_LOAD
;
888 mip
.CombineOperation
= COMBINE_OR
;
889 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
892 /* Load compute_dispatch_indirect_z_size into SRC0 */
893 emit_lrm(batch
, MI_PREDICATE_SRC0
, bo
, bo_offset
+ 8);
895 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
896 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
897 mip
.LoadOperation
= LOAD_LOAD
;
898 mip
.CombineOperation
= COMBINE_OR
;
899 mip
.CompareOperation
= COMPARE_SRCS_EQUAL
;
902 /* predicate = !predicate; */
903 #define COMPARE_FALSE 1
904 anv_batch_emit(batch
, GENX(MI_PREDICATE
), mip
) {
905 mip
.LoadOperation
= LOAD_LOADINV
;
906 mip
.CombineOperation
= COMBINE_OR
;
907 mip
.CompareOperation
= COMPARE_FALSE
;
911 anv_batch_emit(batch
, GENX(GPGPU_WALKER
), ggw
) {
912 ggw
.IndirectParameterEnable
= true;
913 ggw
.PredicateEnable
= GEN_GEN
<= 7;
914 ggw
.SIMDSize
= prog_data
->simd_size
/ 16;
915 ggw
.ThreadDepthCounterMaximum
= 0;
916 ggw
.ThreadHeightCounterMaximum
= 0;
917 ggw
.ThreadWidthCounterMaximum
= prog_data
->threads
- 1;
918 ggw
.RightExecutionMask
= pipeline
->cs_right_mask
;
919 ggw
.BottomExecutionMask
= 0xffffffff;
922 anv_batch_emit(batch
, GENX(MEDIA_STATE_FLUSH
), msf
);
926 flush_pipeline_before_pipeline_select(struct anv_cmd_buffer
*cmd_buffer
,
929 #if GEN_GEN >= 8 && GEN_GEN < 10
930 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
932 * Software must clear the COLOR_CALC_STATE Valid field in
933 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
934 * with Pipeline Select set to GPGPU.
936 * The internal hardware docs recommend the same workaround for Gen9
939 if (pipeline
== GPGPU
)
940 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), t
);
942 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
943 * PIPELINE_SELECT [DevBWR+]":
947 * Software must ensure all the write caches are flushed through a
948 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
949 * command to invalidate read only caches prior to programming
950 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
952 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
953 pc
.RenderTargetCacheFlushEnable
= true;
954 pc
.DepthCacheFlushEnable
= true;
955 pc
.DCFlushEnable
= true;
956 pc
.PostSyncOperation
= NoWrite
;
957 pc
.CommandStreamerStallEnable
= true;
960 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
961 pc
.TextureCacheInvalidationEnable
= true;
962 pc
.ConstantCacheInvalidationEnable
= true;
963 pc
.StateCacheInvalidationEnable
= true;
964 pc
.InstructionCacheInvalidateEnable
= true;
965 pc
.PostSyncOperation
= NoWrite
;
971 genX(flush_pipeline_select_3d
)(struct anv_cmd_buffer
*cmd_buffer
)
973 if (cmd_buffer
->state
.current_pipeline
!= _3D
) {
974 flush_pipeline_before_pipeline_select(cmd_buffer
, _3D
);
976 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
980 ps
.PipelineSelection
= _3D
;
983 cmd_buffer
->state
.current_pipeline
= _3D
;
988 genX(flush_pipeline_select_gpgpu
)(struct anv_cmd_buffer
*cmd_buffer
)
990 if (cmd_buffer
->state
.current_pipeline
!= GPGPU
) {
991 flush_pipeline_before_pipeline_select(cmd_buffer
, GPGPU
);
993 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
), ps
) {
997 ps
.PipelineSelection
= GPGPU
;
1000 cmd_buffer
->state
.current_pipeline
= GPGPU
;
1005 genX(cmd_buffer_alloc_null_surface_state
)(struct anv_cmd_buffer
*cmd_buffer
,
1006 struct anv_framebuffer
*fb
)
1008 struct anv_state state
=
1009 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, 64, 64);
1011 struct GENX(RENDER_SURFACE_STATE
) null_ss
= {
1012 .SurfaceType
= SURFTYPE_NULL
,
1013 .SurfaceArray
= fb
->layers
> 0,
1014 .SurfaceFormat
= ISL_FORMAT_R8G8B8A8_UNORM
,
1018 .TiledSurface
= true,
1020 .Width
= fb
->width
- 1,
1021 .Height
= fb
->height
- 1,
1022 .Depth
= fb
->layers
- 1,
1023 .RenderTargetViewExtent
= fb
->layers
- 1,
1026 GENX(RENDER_SURFACE_STATE_pack
)(NULL
, state
.map
, &null_ss
);
1028 if (!cmd_buffer
->device
->info
.has_llc
)
1029 anv_state_clflush(state
);
1035 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
1037 struct anv_device
*device
= cmd_buffer
->device
;
1038 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
1039 const struct anv_image_view
*iview
=
1040 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
1041 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
1042 const bool has_depth
= image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
1043 const bool has_stencil
=
1044 image
&& (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
1046 /* FIXME: Implement the PMA stall W/A */
1047 /* FIXME: Width and Height are wrong */
1049 /* Emit 3DSTATE_DEPTH_BUFFER */
1051 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
), db
) {
1052 db
.SurfaceType
= SURFTYPE_2D
;
1053 db
.DepthWriteEnable
= true;
1054 db
.StencilWriteEnable
= has_stencil
;
1055 db
.HierarchicalDepthBufferEnable
= false;
1057 db
.SurfaceFormat
= isl_surf_get_depth_format(&device
->isl_dev
,
1058 &image
->depth_surface
.isl
);
1060 db
.SurfaceBaseAddress
= (struct anv_address
) {
1062 .offset
= image
->offset
+ image
->depth_surface
.offset
,
1064 db
.DepthBufferObjectControlState
= GENX(MOCS
),
1066 db
.SurfacePitch
= image
->depth_surface
.isl
.row_pitch
- 1;
1067 db
.Height
= image
->extent
.height
- 1;
1068 db
.Width
= image
->extent
.width
- 1;
1069 db
.LOD
= iview
->base_mip
;
1070 db
.Depth
= image
->array_size
- 1; /* FIXME: 3-D */
1071 db
.MinimumArrayElement
= iview
->base_layer
;
1075 isl_surf_get_array_pitch_el_rows(&image
->depth_surface
.isl
) >> 2,
1077 db
.RenderTargetViewExtent
= 1 - 1;
1080 /* Even when no depth buffer is present, the hardware requires that
1081 * 3DSTATE_DEPTH_BUFFER be programmed correctly. The Broadwell PRM says:
1083 * If a null depth buffer is bound, the driver must instead bind depth as:
1084 * 3DSTATE_DEPTH.SurfaceType = SURFTYPE_2D
1085 * 3DSTATE_DEPTH.Width = 1
1086 * 3DSTATE_DEPTH.Height = 1
1087 * 3DSTATE_DEPTH.SuraceFormat = D16_UNORM
1088 * 3DSTATE_DEPTH.SurfaceBaseAddress = 0
1089 * 3DSTATE_DEPTH.HierarchicalDepthBufferEnable = 0
1090 * 3DSTATE_WM_DEPTH_STENCIL.DepthTestEnable = 0
1091 * 3DSTATE_WM_DEPTH_STENCIL.DepthBufferWriteEnable = 0
1093 * The PRM is wrong, though. The width and height must be programmed to
1094 * actual framebuffer's width and height, even when neither depth buffer
1095 * nor stencil buffer is present. Also, D16_UNORM is not allowed to
1096 * be combined with a stencil buffer so we use D32_FLOAT instead.
1098 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
), db
) {
1099 db
.SurfaceType
= SURFTYPE_2D
;
1100 db
.SurfaceFormat
= D32_FLOAT
;
1101 db
.Width
= fb
->width
- 1;
1102 db
.Height
= fb
->height
- 1;
1103 db
.StencilWriteEnable
= has_stencil
;
1107 /* Emit 3DSTATE_STENCIL_BUFFER */
1109 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
), sb
) {
1110 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1111 sb
.StencilBufferEnable
= true,
1113 sb
.StencilBufferObjectControlState
= GENX(MOCS
),
1115 sb
.SurfacePitch
= image
->stencil_surface
.isl
.row_pitch
- 1,
1118 sb
.SurfaceQPitch
= isl_surf_get_array_pitch_el_rows(&image
->stencil_surface
.isl
) >> 2,
1120 sb
.SurfaceBaseAddress
= (struct anv_address
) {
1122 .offset
= image
->offset
+ image
->stencil_surface
.offset
,
1126 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
), sb
);
1129 /* Disable hierarchial depth buffers. */
1130 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_HIER_DEPTH_BUFFER
), hz
);
1132 /* Clear the clear params. */
1133 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CLEAR_PARAMS
), cp
);
1137 * @see anv_cmd_buffer_set_subpass()
1140 genX(cmd_buffer_set_subpass
)(struct anv_cmd_buffer
*cmd_buffer
,
1141 struct anv_subpass
*subpass
)
1143 cmd_buffer
->state
.subpass
= subpass
;
1145 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
1147 cmd_buffer_emit_depth_stencil(cmd_buffer
);
1150 void genX(CmdBeginRenderPass
)(
1151 VkCommandBuffer commandBuffer
,
1152 const VkRenderPassBeginInfo
* pRenderPassBegin
,
1153 VkSubpassContents contents
)
1155 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1156 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
1157 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
1159 cmd_buffer
->state
.framebuffer
= framebuffer
;
1160 cmd_buffer
->state
.pass
= pass
;
1161 cmd_buffer
->state
.render_area
= pRenderPassBegin
->renderArea
;
1162 anv_cmd_state_setup_attachments(cmd_buffer
, pRenderPassBegin
);
1164 genX(flush_pipeline_select_3d
)(cmd_buffer
);
1166 genX(cmd_buffer_set_subpass
)(cmd_buffer
, pass
->subpasses
);
1167 anv_cmd_buffer_clear_subpass(cmd_buffer
);
1170 void genX(CmdNextSubpass
)(
1171 VkCommandBuffer commandBuffer
,
1172 VkSubpassContents contents
)
1174 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1176 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1178 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
1179 genX(cmd_buffer_set_subpass
)(cmd_buffer
, cmd_buffer
->state
.subpass
+ 1);
1180 anv_cmd_buffer_clear_subpass(cmd_buffer
);
1183 void genX(CmdEndRenderPass
)(
1184 VkCommandBuffer commandBuffer
)
1186 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1188 anv_cmd_buffer_resolve_subpass(cmd_buffer
);
1191 anv_dump_add_framebuffer(cmd_buffer
, cmd_buffer
->state
.framebuffer
);
1196 emit_ps_depth_count(struct anv_batch
*batch
,
1197 struct anv_bo
*bo
, uint32_t offset
)
1199 anv_batch_emit(batch
, GENX(PIPE_CONTROL
), pc
) {
1200 pc
.DestinationAddressType
= DAT_PPGTT
;
1201 pc
.PostSyncOperation
= WritePSDepthCount
;
1202 pc
.DepthStallEnable
= true;
1203 pc
.Address
= (struct anv_address
) { bo
, offset
};
1208 emit_query_availability(struct anv_batch
*batch
,
1209 struct anv_bo
*bo
, uint32_t offset
)
1211 anv_batch_emit(batch
, GENX(PIPE_CONTROL
), pc
) {
1212 pc
.DestinationAddressType
= DAT_PPGTT
;
1213 pc
.PostSyncOperation
= WriteImmediateData
;
1214 pc
.Address
= (struct anv_address
) { bo
, offset
};
1215 pc
.ImmediateData
= 1;
1219 void genX(CmdBeginQuery
)(
1220 VkCommandBuffer commandBuffer
,
1221 VkQueryPool queryPool
,
1223 VkQueryControlFlags flags
)
1225 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1226 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
1228 /* Workaround: When meta uses the pipeline with the VS disabled, it seems
1229 * that the pipelining of the depth write breaks. What we see is that
1230 * samples from the render pass clear leaks into the first query
1231 * immediately after the clear. Doing a pipecontrol with a post-sync
1232 * operation and DepthStallEnable seems to work around the issue.
1234 if (cmd_buffer
->state
.need_query_wa
) {
1235 cmd_buffer
->state
.need_query_wa
= false;
1236 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1237 pc
.DepthCacheFlushEnable
= true;
1238 pc
.DepthStallEnable
= true;
1242 switch (pool
->type
) {
1243 case VK_QUERY_TYPE_OCCLUSION
:
1244 emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
1245 query
* sizeof(struct anv_query_pool_slot
));
1248 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
1254 void genX(CmdEndQuery
)(
1255 VkCommandBuffer commandBuffer
,
1256 VkQueryPool queryPool
,
1259 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1260 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
1262 switch (pool
->type
) {
1263 case VK_QUERY_TYPE_OCCLUSION
:
1264 emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
1265 query
* sizeof(struct anv_query_pool_slot
) + 8);
1267 emit_query_availability(&cmd_buffer
->batch
, &pool
->bo
,
1268 query
* sizeof(struct anv_query_pool_slot
) + 16);
1271 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
1277 #define TIMESTAMP 0x2358
1279 void genX(CmdWriteTimestamp
)(
1280 VkCommandBuffer commandBuffer
,
1281 VkPipelineStageFlagBits pipelineStage
,
1282 VkQueryPool queryPool
,
1285 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1286 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
1287 uint32_t offset
= query
* sizeof(struct anv_query_pool_slot
);
1289 assert(pool
->type
== VK_QUERY_TYPE_TIMESTAMP
);
1291 switch (pipelineStage
) {
1292 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
:
1293 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
1294 srm
.RegisterAddress
= TIMESTAMP
;
1295 srm
.MemoryAddress
= (struct anv_address
) { &pool
->bo
, offset
};
1297 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
1298 srm
.RegisterAddress
= TIMESTAMP
+ 4;
1299 srm
.MemoryAddress
= (struct anv_address
) { &pool
->bo
, offset
+ 4 };
1304 /* Everything else is bottom-of-pipe */
1305 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1306 pc
.DestinationAddressType
= DAT_PPGTT
,
1307 pc
.PostSyncOperation
= WriteTimestamp
,
1308 pc
.Address
= (struct anv_address
) { &pool
->bo
, offset
};
1313 emit_query_availability(&cmd_buffer
->batch
, &pool
->bo
, query
+ 16);
1316 #if GEN_GEN > 7 || GEN_IS_HASWELL
1318 #define alu_opcode(v) __gen_uint((v), 20, 31)
1319 #define alu_operand1(v) __gen_uint((v), 10, 19)
1320 #define alu_operand2(v) __gen_uint((v), 0, 9)
1321 #define alu(opcode, operand1, operand2) \
1322 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
1324 #define OPCODE_NOOP 0x000
1325 #define OPCODE_LOAD 0x080
1326 #define OPCODE_LOADINV 0x480
1327 #define OPCODE_LOAD0 0x081
1328 #define OPCODE_LOAD1 0x481
1329 #define OPCODE_ADD 0x100
1330 #define OPCODE_SUB 0x101
1331 #define OPCODE_AND 0x102
1332 #define OPCODE_OR 0x103
1333 #define OPCODE_XOR 0x104
1334 #define OPCODE_STORE 0x180
1335 #define OPCODE_STOREINV 0x580
1337 #define OPERAND_R0 0x00
1338 #define OPERAND_R1 0x01
1339 #define OPERAND_R2 0x02
1340 #define OPERAND_R3 0x03
1341 #define OPERAND_R4 0x04
1342 #define OPERAND_SRCA 0x20
1343 #define OPERAND_SRCB 0x21
1344 #define OPERAND_ACCU 0x31
1345 #define OPERAND_ZF 0x32
1346 #define OPERAND_CF 0x33
1348 #define CS_GPR(n) (0x2600 + (n) * 8)
1351 emit_load_alu_reg_u64(struct anv_batch
*batch
, uint32_t reg
,
1352 struct anv_bo
*bo
, uint32_t offset
)
1354 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
1355 lrm
.RegisterAddress
= reg
,
1356 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
1358 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
), lrm
) {
1359 lrm
.RegisterAddress
= reg
+ 4;
1360 lrm
.MemoryAddress
= (struct anv_address
) { bo
, offset
+ 4 };
1365 store_query_result(struct anv_batch
*batch
, uint32_t reg
,
1366 struct anv_bo
*bo
, uint32_t offset
, VkQueryResultFlags flags
)
1368 anv_batch_emit(batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
1369 srm
.RegisterAddress
= reg
;
1370 srm
.MemoryAddress
= (struct anv_address
) { bo
, offset
};
1373 if (flags
& VK_QUERY_RESULT_64_BIT
) {
1374 anv_batch_emit(batch
, GENX(MI_STORE_REGISTER_MEM
), srm
) {
1375 srm
.RegisterAddress
= reg
+ 4;
1376 srm
.MemoryAddress
= (struct anv_address
) { bo
, offset
+ 4 };
1381 void genX(CmdCopyQueryPoolResults
)(
1382 VkCommandBuffer commandBuffer
,
1383 VkQueryPool queryPool
,
1384 uint32_t firstQuery
,
1385 uint32_t queryCount
,
1386 VkBuffer destBuffer
,
1387 VkDeviceSize destOffset
,
1388 VkDeviceSize destStride
,
1389 VkQueryResultFlags flags
)
1391 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1392 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
1393 ANV_FROM_HANDLE(anv_buffer
, buffer
, destBuffer
);
1394 uint32_t slot_offset
, dst_offset
;
1396 if (flags
& VK_QUERY_RESULT_WAIT_BIT
) {
1397 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
1398 pc
.CommandStreamerStallEnable
= true;
1399 pc
.StallAtPixelScoreboard
= true;
1403 dst_offset
= buffer
->offset
+ destOffset
;
1404 for (uint32_t i
= 0; i
< queryCount
; i
++) {
1406 slot_offset
= (firstQuery
+ i
) * sizeof(struct anv_query_pool_slot
);
1407 switch (pool
->type
) {
1408 case VK_QUERY_TYPE_OCCLUSION
:
1409 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
1410 CS_GPR(0), &pool
->bo
, slot_offset
);
1411 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
1412 CS_GPR(1), &pool
->bo
, slot_offset
+ 8);
1414 /* FIXME: We need to clamp the result for 32 bit. */
1416 uint32_t *dw
= anv_batch_emitn(&cmd_buffer
->batch
, 5, GENX(MI_MATH
));
1417 dw
[1] = alu(OPCODE_LOAD
, OPERAND_SRCA
, OPERAND_R1
);
1418 dw
[2] = alu(OPCODE_LOAD
, OPERAND_SRCB
, OPERAND_R0
);
1419 dw
[3] = alu(OPCODE_SUB
, 0, 0);
1420 dw
[4] = alu(OPCODE_STORE
, OPERAND_R2
, OPERAND_ACCU
);
1423 case VK_QUERY_TYPE_TIMESTAMP
:
1424 emit_load_alu_reg_u64(&cmd_buffer
->batch
,
1425 CS_GPR(2), &pool
->bo
, slot_offset
);
1429 unreachable("unhandled query type");
1432 store_query_result(&cmd_buffer
->batch
,
1433 CS_GPR(2), buffer
->bo
, dst_offset
, flags
);
1435 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
1436 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(0),
1437 &pool
->bo
, slot_offset
+ 16);
1438 if (flags
& VK_QUERY_RESULT_64_BIT
)
1439 store_query_result(&cmd_buffer
->batch
,
1440 CS_GPR(0), buffer
->bo
, dst_offset
+ 8, flags
);
1442 store_query_result(&cmd_buffer
->batch
,
1443 CS_GPR(0), buffer
->bo
, dst_offset
+ 4, flags
);
1446 dst_offset
+= destStride
;
1451 void genX(CmdCopyQueryPoolResults
)(
1452 VkCommandBuffer commandBuffer
,
1453 VkQueryPool queryPool
,
1454 uint32_t firstQuery
,
1455 uint32_t queryCount
,
1456 VkBuffer destBuffer
,
1457 VkDeviceSize destOffset
,
1458 VkDeviceSize destStride
,
1459 VkQueryResultFlags flags
)
1461 anv_finishme("Queries not yet supported on Ivy Bridge");