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
30 #include "anv_private.h"
32 #include "gen7_pack.h"
33 #include "gen75_pack.h"
36 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
38 static const uint32_t push_constant_opcodes
[] = {
39 [VK_SHADER_STAGE_VERTEX
] = 21,
40 [VK_SHADER_STAGE_TESS_CONTROL
] = 25, /* HS */
41 [VK_SHADER_STAGE_TESS_EVALUATION
] = 26, /* DS */
42 [VK_SHADER_STAGE_GEOMETRY
] = 22,
43 [VK_SHADER_STAGE_FRAGMENT
] = 23,
44 [VK_SHADER_STAGE_COMPUTE
] = 0,
48 VkShaderStageFlags flushed
= 0;
50 for_each_bit(stage
, cmd_buffer
->state
.push_constants_dirty
) {
51 struct anv_state state
= anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
53 if (state
.offset
== 0)
56 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_CONSTANT_VS
,
57 ._3DCommandSubOpcode
= push_constant_opcodes
[stage
],
59 .PointerToConstantBuffer0
= { .offset
= state
.offset
},
60 .ConstantBuffer0ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
63 flushed
|= 1 << stage
;
66 cmd_buffer
->state
.push_constants_dirty
&= ~flushed
;
69 GENX_FUNC(GEN7
, GEN7
) void
70 genX(cmd_buffer_emit_state_base_address
)(struct anv_cmd_buffer
*cmd_buffer
)
72 struct anv_device
*device
= cmd_buffer
->device
;
73 struct anv_bo
*scratch_bo
= NULL
;
75 cmd_buffer
->state
.scratch_size
=
76 anv_block_pool_size(&device
->scratch_block_pool
);
77 if (cmd_buffer
->state
.scratch_size
> 0)
78 scratch_bo
= &device
->scratch_block_pool
.bo
;
80 anv_batch_emit(&cmd_buffer
->batch
, GEN7_STATE_BASE_ADDRESS
,
81 .GeneralStateBaseAddress
= { scratch_bo
, 0 },
82 .GeneralStateMemoryObjectControlState
= GEN7_MOCS
,
83 .GeneralStateBaseAddressModifyEnable
= true,
84 .GeneralStateAccessUpperBound
= { scratch_bo
, scratch_bo
->size
},
85 .GeneralStateAccessUpperBoundModifyEnable
= true,
87 .SurfaceStateBaseAddress
= anv_cmd_buffer_surface_base_address(cmd_buffer
),
88 .SurfaceStateMemoryObjectControlState
= GEN7_MOCS
,
89 .SurfaceStateBaseAddressModifyEnable
= true,
91 .DynamicStateBaseAddress
= { &device
->dynamic_state_block_pool
.bo
, 0 },
92 .DynamicStateMemoryObjectControlState
= GEN7_MOCS
,
93 .DynamicStateBaseAddressModifyEnable
= true,
95 .IndirectObjectBaseAddress
= { NULL
, 0 },
96 .IndirectObjectMemoryObjectControlState
= GEN7_MOCS
,
97 .IndirectObjectBaseAddressModifyEnable
= true,
99 .IndirectObjectAccessUpperBound
= { NULL
, 0xffffffff },
100 .IndirectObjectAccessUpperBoundModifyEnable
= true,
102 .InstructionBaseAddress
= { &device
->instruction_block_pool
.bo
, 0 },
103 .InstructionMemoryObjectControlState
= GEN7_MOCS
,
104 .InstructionBaseAddressModifyEnable
= true,
105 .InstructionAccessUpperBound
= { &device
->instruction_block_pool
.bo
,
106 device
->instruction_block_pool
.bo
.size
},
107 .InstructionAccessUpperBoundModifyEnable
= true);
109 /* After re-setting the surface state base address, we have to do some
110 * cache flusing so that the sampler engine will pick up the new
111 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
112 * Shared Function > 3D Sampler > State > State Caching (page 96):
114 * Coherency with system memory in the state cache, like the texture
115 * cache is handled partially by software. It is expected that the
116 * command stream or shader will issue Cache Flush operation or
117 * Cache_Flush sampler message to ensure that the L1 cache remains
118 * coherent with system memory.
122 * Whenever the value of the Dynamic_State_Base_Addr,
123 * Surface_State_Base_Addr are altered, the L1 state cache must be
124 * invalidated to ensure the new surface or sampler state is fetched
125 * from system memory.
127 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
128 * which, according the PIPE_CONTROL instruction documentation in the
131 * Setting this bit is independent of any other bit in this packet.
132 * This bit controls the invalidation of the L1 and L2 state caches
133 * at the top of the pipe i.e. at the parsing time.
135 * Unfortunately, experimentation seems to indicate that state cache
136 * invalidation through a PIPE_CONTROL does nothing whatsoever in
137 * regards to surface state and binding tables. In stead, it seems that
138 * invalidating the texture cache is what is actually needed.
140 * XXX: As far as we have been able to determine through
141 * experimentation, shows that flush the texture cache appears to be
142 * sufficient. The theory here is that all of the sampling/rendering
143 * units cache the binding table in the texture cache. However, we have
144 * yet to be able to actually confirm this.
146 anv_batch_emit(&cmd_buffer
->batch
, GEN7_PIPE_CONTROL
,
147 .TextureCacheInvalidationEnable
= true);
151 flush_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
, VkShaderStage stage
)
153 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
156 result
= anv_cmd_buffer_emit_samplers(cmd_buffer
, stage
, &samplers
);
157 if (result
!= VK_SUCCESS
)
159 result
= anv_cmd_buffer_emit_binding_table(cmd_buffer
, stage
, &surfaces
);
160 if (result
!= VK_SUCCESS
)
163 static const uint32_t sampler_state_opcodes
[] = {
164 [VK_SHADER_STAGE_VERTEX
] = 43,
165 [VK_SHADER_STAGE_TESS_CONTROL
] = 44, /* HS */
166 [VK_SHADER_STAGE_TESS_EVALUATION
] = 45, /* DS */
167 [VK_SHADER_STAGE_GEOMETRY
] = 46,
168 [VK_SHADER_STAGE_FRAGMENT
] = 47,
169 [VK_SHADER_STAGE_COMPUTE
] = 0,
172 static const uint32_t binding_table_opcodes
[] = {
173 [VK_SHADER_STAGE_VERTEX
] = 38,
174 [VK_SHADER_STAGE_TESS_CONTROL
] = 39,
175 [VK_SHADER_STAGE_TESS_EVALUATION
] = 40,
176 [VK_SHADER_STAGE_GEOMETRY
] = 41,
177 [VK_SHADER_STAGE_FRAGMENT
] = 42,
178 [VK_SHADER_STAGE_COMPUTE
] = 0,
181 if (samplers
.alloc_size
> 0) {
182 anv_batch_emit(&cmd_buffer
->batch
,
183 GEN7_3DSTATE_SAMPLER_STATE_POINTERS_VS
,
184 ._3DCommandSubOpcode
= sampler_state_opcodes
[stage
],
185 .PointertoVSSamplerState
= samplers
.offset
);
188 if (surfaces
.alloc_size
> 0) {
189 anv_batch_emit(&cmd_buffer
->batch
,
190 GEN7_3DSTATE_BINDING_TABLE_POINTERS_VS
,
191 ._3DCommandSubOpcode
= binding_table_opcodes
[stage
],
192 .PointertoVSBindingTable
= surfaces
.offset
);
198 GENX_FUNC(GEN7
, GEN7
) void
199 genX(cmd_buffer_flush_descriptor_sets
)(struct anv_cmd_buffer
*cmd_buffer
)
202 VkShaderStageFlags dirty
= cmd_buffer
->state
.descriptors_dirty
&
203 cmd_buffer
->state
.pipeline
->active_stages
;
205 VkResult result
= VK_SUCCESS
;
206 for_each_bit(s
, dirty
) {
207 result
= flush_descriptor_set(cmd_buffer
, s
);
208 if (result
!= VK_SUCCESS
)
212 if (result
!= VK_SUCCESS
) {
213 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
215 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
216 assert(result
== VK_SUCCESS
);
218 /* Re-emit state base addresses so we get the new surface state base
219 * address before we start emitting binding tables etc.
221 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
223 /* Re-emit all active binding tables */
224 for_each_bit(s
, cmd_buffer
->state
.pipeline
->active_stages
) {
225 result
= flush_descriptor_set(cmd_buffer
, s
);
227 /* It had better succeed this time */
228 assert(result
== VK_SUCCESS
);
232 cmd_buffer
->state
.descriptors_dirty
&= ~cmd_buffer
->state
.pipeline
->active_stages
;
235 static inline int64_t
236 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
247 emit_scissor_state(struct anv_cmd_buffer
*cmd_buffer
,
248 uint32_t count
, const VkRect2D
*scissors
)
250 struct anv_state scissor_state
=
251 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, count
* 32, 32);
253 for (uint32_t i
= 0; i
< count
; i
++) {
254 const VkRect2D
*s
= &scissors
[i
];
256 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
257 * ymax < ymin for empty clips. In case clip x, y, width height are all
258 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
259 * what we want. Just special case empty clips and produce a canonical
261 static const struct GEN7_SCISSOR_RECT empty_scissor
= {
262 .ScissorRectangleYMin
= 1,
263 .ScissorRectangleXMin
= 1,
264 .ScissorRectangleYMax
= 0,
265 .ScissorRectangleXMax
= 0
268 const int max
= 0xffff;
269 struct GEN7_SCISSOR_RECT scissor
= {
270 /* Do this math using int64_t so overflow gets clamped correctly. */
271 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
272 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
273 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
274 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
277 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
278 GEN7_SCISSOR_RECT_pack(NULL
, scissor_state
.map
+ i
* 32,
281 GEN7_SCISSOR_RECT_pack(NULL
, scissor_state
.map
+ i
* 32, &scissor
);
285 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_SCISSOR_STATE_POINTERS
,
286 .ScissorRectPointer
= scissor_state
.offset
);
289 GENX_FUNC(GEN7
, GEN7
) void
290 genX(cmd_buffer_emit_scissor
)(struct anv_cmd_buffer
*cmd_buffer
)
292 if (cmd_buffer
->state
.dynamic
.scissor
.count
> 0) {
293 emit_scissor_state(cmd_buffer
, cmd_buffer
->state
.dynamic
.scissor
.count
,
294 cmd_buffer
->state
.dynamic
.scissor
.scissors
);
296 /* Emit a default scissor based on the currently bound framebuffer */
297 emit_scissor_state(cmd_buffer
, 1,
299 .offset
= { .x
= 0, .y
= 0, },
301 .width
= cmd_buffer
->state
.framebuffer
->width
,
302 .height
= cmd_buffer
->state
.framebuffer
->height
,
308 static const uint32_t vk_to_gen_index_type
[] = {
309 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
310 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
313 static const uint32_t restart_index_for_type
[] = {
314 [VK_INDEX_TYPE_UINT16
] = UINT16_MAX
,
315 [VK_INDEX_TYPE_UINT32
] = UINT32_MAX
,
318 void genX(CmdBindIndexBuffer
)(
319 VkCmdBuffer cmdBuffer
,
322 VkIndexType indexType
)
324 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
325 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
327 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_INDEX_BUFFER
;
329 cmd_buffer
->state
.restart_index
= restart_index_for_type
[indexType
];
330 cmd_buffer
->state
.gen7
.index_buffer
= buffer
;
331 cmd_buffer
->state
.gen7
.index_type
= vk_to_gen_index_type
[indexType
];
332 cmd_buffer
->state
.gen7
.index_offset
= offset
;
336 flush_compute_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
)
338 struct anv_device
*device
= cmd_buffer
->device
;
339 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
340 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
343 result
= anv_cmd_buffer_emit_samplers(cmd_buffer
,
344 VK_SHADER_STAGE_COMPUTE
, &samplers
);
345 if (result
!= VK_SUCCESS
)
347 result
= anv_cmd_buffer_emit_binding_table(cmd_buffer
,
348 VK_SHADER_STAGE_COMPUTE
, &surfaces
);
349 if (result
!= VK_SUCCESS
)
352 struct GEN7_INTERFACE_DESCRIPTOR_DATA desc
= {
353 .KernelStartPointer
= pipeline
->cs_simd
,
354 .BindingTablePointer
= surfaces
.offset
,
355 .SamplerStatePointer
= samplers
.offset
,
356 .NumberofThreadsinGPGPUThreadGroup
= 0 /* FIXME: Really? */
359 uint32_t size
= GEN7_INTERFACE_DESCRIPTOR_DATA_length
* sizeof(uint32_t);
360 struct anv_state state
=
361 anv_state_pool_alloc(&device
->dynamic_state_pool
, size
, 64);
363 GEN7_INTERFACE_DESCRIPTOR_DATA_pack(NULL
, state
.map
, &desc
);
365 anv_batch_emit(&cmd_buffer
->batch
, GEN7_MEDIA_INTERFACE_DESCRIPTOR_LOAD
,
366 .InterfaceDescriptorTotalLength
= size
,
367 .InterfaceDescriptorDataStartAddress
= state
.offset
);
373 cmd_buffer_flush_compute_state(struct anv_cmd_buffer
*cmd_buffer
)
375 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
378 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
380 if (cmd_buffer
->state
.current_pipeline
!= GPGPU
) {
381 anv_batch_emit(&cmd_buffer
->batch
, GEN7_PIPELINE_SELECT
,
382 .PipelineSelection
= GPGPU
);
383 cmd_buffer
->state
.current_pipeline
= GPGPU
;
386 if (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)
387 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
389 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
390 (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)) {
391 /* FIXME: figure out descriptors for gen7 */
392 result
= flush_compute_descriptor_set(cmd_buffer
);
393 assert(result
== VK_SUCCESS
);
394 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE
;
397 cmd_buffer
->state
.compute_dirty
= 0;
401 cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
403 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
406 uint32_t vb_emit
= cmd_buffer
->state
.vb_dirty
& pipeline
->vb_used
;
408 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
410 if (cmd_buffer
->state
.current_pipeline
!= _3D
) {
411 anv_batch_emit(&cmd_buffer
->batch
, GEN7_PIPELINE_SELECT
,
412 .PipelineSelection
= _3D
);
413 cmd_buffer
->state
.current_pipeline
= _3D
;
417 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
418 const uint32_t num_dwords
= 1 + num_buffers
* 4;
420 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
421 GEN7_3DSTATE_VERTEX_BUFFERS
);
423 for_each_bit(vb
, vb_emit
) {
424 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
425 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
427 struct GEN7_VERTEX_BUFFER_STATE state
= {
428 .VertexBufferIndex
= vb
,
429 .BufferAccessType
= pipeline
->instancing_enable
[vb
] ? INSTANCEDATA
: VERTEXDATA
,
430 .VertexBufferMemoryObjectControlState
= GEN7_MOCS
,
431 .AddressModifyEnable
= true,
432 .BufferPitch
= pipeline
->binding_stride
[vb
],
433 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
434 .EndAddress
= { buffer
->bo
, buffer
->offset
+ buffer
->size
- 1},
435 .InstanceDataStepRate
= 1
438 GEN7_VERTEX_BUFFER_STATE_pack(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
443 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
444 /* If somebody compiled a pipeline after starting a command buffer the
445 * scratch bo may have grown since we started this cmd buffer (and
446 * emitted STATE_BASE_ADDRESS). If we're binding that pipeline now,
447 * reemit STATE_BASE_ADDRESS so that we use the bigger scratch bo. */
448 if (cmd_buffer
->state
.scratch_size
< pipeline
->total_scratch
)
449 gen7_cmd_buffer_emit_state_base_address(cmd_buffer
);
451 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
454 if (cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_VERTEX_BIT
||
455 cmd_buffer
->state
.push_constants_dirty
& VK_SHADER_STAGE_VERTEX_BIT
) {
456 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
458 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
459 * stall needs to be sent just prior to any 3DSTATE_VS,
460 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
461 * 3DSTATE_BINDING_TABLE_POINTER_VS,
462 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
463 * PIPE_CONTROL needs to be sent before any combination of VS
464 * associated 3DSTATE."
466 anv_batch_emit(&cmd_buffer
->batch
, GEN7_PIPE_CONTROL
,
467 .DepthStallEnable
= true,
468 .PostSyncOperation
= WriteImmediateData
,
469 .Address
= { &cmd_buffer
->device
->workaround_bo
, 0 });
472 if (cmd_buffer
->state
.descriptors_dirty
)
473 gen7_cmd_buffer_flush_descriptor_sets(cmd_buffer
);
475 if (cmd_buffer
->state
.push_constants_dirty
)
476 cmd_buffer_flush_push_constants(cmd_buffer
);
478 /* We use the gen8 state here because it only contains the additional
479 * min/max fields and, since they occur at the end of the packet and
480 * don't change the stride, they work on gen7 too.
482 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
483 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
485 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
)
486 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
488 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
489 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
|
490 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
)) {
492 bool enable_bias
= cmd_buffer
->state
.dynamic
.depth_bias
.bias
!= 0.0f
||
493 cmd_buffer
->state
.dynamic
.depth_bias
.slope_scaled
!= 0.0f
;
495 uint32_t sf_dw
[GEN7_3DSTATE_SF_length
];
496 struct GEN7_3DSTATE_SF sf
= {
497 GEN7_3DSTATE_SF_header
,
498 .LineWidth
= cmd_buffer
->state
.dynamic
.line_width
,
499 .GlobalDepthOffsetEnableSolid
= enable_bias
,
500 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
501 .GlobalDepthOffsetEnablePoint
= enable_bias
,
502 .GlobalDepthOffsetConstant
= cmd_buffer
->state
.dynamic
.depth_bias
.bias
,
503 .GlobalDepthOffsetScale
= cmd_buffer
->state
.dynamic
.depth_bias
.slope_scaled
,
504 .GlobalDepthOffsetClamp
= cmd_buffer
->state
.dynamic
.depth_bias
.clamp
506 GEN7_3DSTATE_SF_pack(NULL
, sf_dw
, &sf
);
508 anv_batch_emit_merge(&cmd_buffer
->batch
, sf_dw
, pipeline
->gen7
.sf
);
511 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
|
512 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
)) {
513 struct anv_state cc_state
=
514 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
515 GEN7_COLOR_CALC_STATE_length
, 64);
516 struct GEN7_COLOR_CALC_STATE cc
= {
517 .BlendConstantColorRed
= cmd_buffer
->state
.dynamic
.blend_constants
[0],
518 .BlendConstantColorGreen
= cmd_buffer
->state
.dynamic
.blend_constants
[1],
519 .BlendConstantColorBlue
= cmd_buffer
->state
.dynamic
.blend_constants
[2],
520 .BlendConstantColorAlpha
= cmd_buffer
->state
.dynamic
.blend_constants
[3],
521 .StencilReferenceValue
=
522 cmd_buffer
->state
.dynamic
.stencil_reference
.front
,
523 .BackFaceStencilReferenceValue
=
524 cmd_buffer
->state
.dynamic
.stencil_reference
.back
,
526 GEN7_COLOR_CALC_STATE_pack(NULL
, cc_state
.map
, &cc
);
528 anv_batch_emit(&cmd_buffer
->batch
,
529 GEN7_3DSTATE_CC_STATE_POINTERS
,
530 .ColorCalcStatePointer
= cc_state
.offset
);
533 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
534 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
|
535 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
)) {
536 uint32_t depth_stencil_dw
[GEN7_DEPTH_STENCIL_STATE_length
];
538 struct GEN7_DEPTH_STENCIL_STATE depth_stencil
= {
539 /* Is this what we need to do? */
540 .StencilBufferWriteEnable
=
541 cmd_buffer
->state
.dynamic
.stencil_write_mask
.front
!= 0,
544 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.front
& 0xff,
546 cmd_buffer
->state
.dynamic
.stencil_write_mask
.front
& 0xff,
548 .BackfaceStencilTestMask
=
549 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.back
& 0xff,
550 .BackfaceStencilWriteMask
=
551 cmd_buffer
->state
.dynamic
.stencil_write_mask
.back
& 0xff,
553 GEN7_DEPTH_STENCIL_STATE_pack(NULL
, depth_stencil_dw
, &depth_stencil
);
555 struct anv_state ds_state
=
556 anv_cmd_buffer_merge_dynamic(cmd_buffer
, depth_stencil_dw
,
557 pipeline
->gen7
.depth_stencil_state
,
558 GEN7_DEPTH_STENCIL_STATE_length
, 64);
560 anv_batch_emit(&cmd_buffer
->batch
,
561 GEN7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS
,
562 .PointertoDEPTH_STENCIL_STATE
= ds_state
.offset
);
565 if (cmd_buffer
->state
.gen7
.index_buffer
&&
566 cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
567 ANV_CMD_DIRTY_INDEX_BUFFER
)) {
568 struct anv_buffer
*buffer
= cmd_buffer
->state
.gen7
.index_buffer
;
569 uint32_t offset
= cmd_buffer
->state
.gen7
.index_offset
;
571 if (ANV_IS_HASWELL
) {
572 anv_batch_emit(&cmd_buffer
->batch
, GEN75_3DSTATE_VF
,
573 .IndexedDrawCutIndexEnable
= pipeline
->primitive_restart
,
574 .CutIndex
= cmd_buffer
->state
.restart_index
);
577 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_INDEX_BUFFER
,
578 .CutIndexEnable
= pipeline
->primitive_restart
,
579 .IndexFormat
= cmd_buffer
->state
.gen7
.index_type
,
580 .MemoryObjectControlState
= GEN7_MOCS
,
581 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
582 .BufferEndingAddress
= { buffer
->bo
, buffer
->offset
+ buffer
->size
});
585 cmd_buffer
->state
.vb_dirty
&= ~vb_emit
;
586 cmd_buffer
->state
.dirty
= 0;
590 VkCmdBuffer cmdBuffer
,
591 uint32_t vertexCount
,
592 uint32_t instanceCount
,
593 uint32_t firstVertex
,
594 uint32_t firstInstance
)
596 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
597 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
599 cmd_buffer_flush_state(cmd_buffer
);
601 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DPRIMITIVE
,
602 .VertexAccessType
= SEQUENTIAL
,
603 .PrimitiveTopologyType
= pipeline
->topology
,
604 .VertexCountPerInstance
= vertexCount
,
605 .StartVertexLocation
= firstVertex
,
606 .InstanceCount
= instanceCount
,
607 .StartInstanceLocation
= firstInstance
,
608 .BaseVertexLocation
= 0);
611 void genX(CmdDrawIndexed
)(
612 VkCmdBuffer cmdBuffer
,
614 uint32_t instanceCount
,
616 int32_t vertexOffset
,
617 uint32_t firstInstance
)
619 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
620 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
622 cmd_buffer_flush_state(cmd_buffer
);
624 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DPRIMITIVE
,
625 .VertexAccessType
= RANDOM
,
626 .PrimitiveTopologyType
= pipeline
->topology
,
627 .VertexCountPerInstance
= indexCount
,
628 .StartVertexLocation
= firstIndex
,
629 .InstanceCount
= instanceCount
,
630 .StartInstanceLocation
= firstInstance
,
631 .BaseVertexLocation
= vertexOffset
);
635 gen7_batch_lrm(struct anv_batch
*batch
,
636 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
638 anv_batch_emit(batch
, GEN7_MI_LOAD_REGISTER_MEM
,
639 .RegisterAddress
= reg
,
640 .MemoryAddress
= { bo
, offset
});
644 gen7_batch_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
646 anv_batch_emit(batch
, GEN7_MI_LOAD_REGISTER_IMM
,
647 .RegisterOffset
= reg
,
651 /* Auto-Draw / Indirect Registers */
652 #define GEN7_3DPRIM_END_OFFSET 0x2420
653 #define GEN7_3DPRIM_START_VERTEX 0x2430
654 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
655 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
656 #define GEN7_3DPRIM_START_INSTANCE 0x243C
657 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
659 void genX(CmdDrawIndirect
)(
660 VkCmdBuffer cmdBuffer
,
666 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
667 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
668 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
669 struct anv_bo
*bo
= buffer
->bo
;
670 uint32_t bo_offset
= buffer
->offset
+ offset
;
672 cmd_buffer_flush_state(cmd_buffer
);
674 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
675 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
676 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
677 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
678 gen7_batch_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
680 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DPRIMITIVE
,
681 .IndirectParameterEnable
= true,
682 .VertexAccessType
= SEQUENTIAL
,
683 .PrimitiveTopologyType
= pipeline
->topology
);
686 void genX(CmdDrawIndexedIndirect
)(
687 VkCmdBuffer cmdBuffer
,
693 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
694 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
695 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
696 struct anv_bo
*bo
= buffer
->bo
;
697 uint32_t bo_offset
= buffer
->offset
+ offset
;
699 cmd_buffer_flush_state(cmd_buffer
);
701 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
702 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
703 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
704 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
705 gen7_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
707 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DPRIMITIVE
,
708 .IndirectParameterEnable
= true,
709 .VertexAccessType
= RANDOM
,
710 .PrimitiveTopologyType
= pipeline
->topology
);
713 void genX(CmdDispatch
)(
714 VkCmdBuffer cmdBuffer
,
719 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
720 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
721 struct brw_cs_prog_data
*prog_data
= &pipeline
->cs_prog_data
;
723 cmd_buffer_flush_compute_state(cmd_buffer
);
725 anv_batch_emit(&cmd_buffer
->batch
, GEN7_GPGPU_WALKER
,
726 .SIMDSize
= prog_data
->simd_size
/ 16,
727 .ThreadDepthCounterMaximum
= 0,
728 .ThreadHeightCounterMaximum
= 0,
729 .ThreadWidthCounterMaximum
= pipeline
->cs_thread_width_max
,
730 .ThreadGroupIDXDimension
= x
,
731 .ThreadGroupIDYDimension
= y
,
732 .ThreadGroupIDZDimension
= z
,
733 .RightExecutionMask
= pipeline
->cs_right_mask
,
734 .BottomExecutionMask
= 0xffffffff);
736 anv_batch_emit(&cmd_buffer
->batch
, GEN7_MEDIA_STATE_FLUSH
);
739 #define GPGPU_DISPATCHDIMX 0x2500
740 #define GPGPU_DISPATCHDIMY 0x2504
741 #define GPGPU_DISPATCHDIMZ 0x2508
743 void genX(CmdDispatchIndirect
)(
744 VkCmdBuffer cmdBuffer
,
748 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
749 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
750 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
751 struct brw_cs_prog_data
*prog_data
= &pipeline
->cs_prog_data
;
752 struct anv_bo
*bo
= buffer
->bo
;
753 uint32_t bo_offset
= buffer
->offset
+ offset
;
755 cmd_buffer_flush_compute_state(cmd_buffer
);
757 gen7_batch_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMX
, bo
, bo_offset
);
758 gen7_batch_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMY
, bo
, bo_offset
+ 4);
759 gen7_batch_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMZ
, bo
, bo_offset
+ 8);
761 anv_batch_emit(&cmd_buffer
->batch
, GEN7_GPGPU_WALKER
,
762 .IndirectParameterEnable
= true,
763 .SIMDSize
= prog_data
->simd_size
/ 16,
764 .ThreadDepthCounterMaximum
= 0,
765 .ThreadHeightCounterMaximum
= 0,
766 .ThreadWidthCounterMaximum
= pipeline
->cs_thread_width_max
,
767 .RightExecutionMask
= pipeline
->cs_right_mask
,
768 .BottomExecutionMask
= 0xffffffff);
770 anv_batch_emit(&cmd_buffer
->batch
, GEN7_MEDIA_STATE_FLUSH
);
773 void genX(CmdPipelineBarrier
)(
774 VkCmdBuffer cmdBuffer
,
775 VkPipelineStageFlags srcStageMask
,
776 VkPipelineStageFlags destStageMask
,
778 uint32_t memBarrierCount
,
779 const void* const* ppMemBarriers
)
785 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
787 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
788 const struct anv_image_view
*iview
=
789 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
790 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
791 const bool has_depth
= iview
&& iview
->format
->depth_format
;
792 const bool has_stencil
= iview
&& iview
->format
->has_stencil
;
794 /* Emit 3DSTATE_DEPTH_BUFFER */
796 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_DEPTH_BUFFER
,
797 .SurfaceType
= SURFTYPE_2D
,
798 .DepthWriteEnable
= iview
->format
->depth_format
,
799 .StencilWriteEnable
= has_stencil
,
800 .HierarchicalDepthBufferEnable
= false,
801 .SurfaceFormat
= iview
->format
->depth_format
,
802 .SurfacePitch
= image
->depth_surface
.stride
- 1,
803 .SurfaceBaseAddress
= {
805 .offset
= image
->depth_surface
.offset
,
807 .Height
= fb
->height
- 1,
808 .Width
= fb
->width
- 1,
811 .MinimumArrayElement
= 0,
812 .DepthBufferObjectControlState
= GEN7_MOCS
,
813 .RenderTargetViewExtent
= 1 - 1);
815 /* Even when no depth buffer is present, the hardware requires that
816 * 3DSTATE_DEPTH_BUFFER be programmed correctly. The Broadwell PRM says:
818 * If a null depth buffer is bound, the driver must instead bind depth as:
819 * 3DSTATE_DEPTH.SurfaceType = SURFTYPE_2D
820 * 3DSTATE_DEPTH.Width = 1
821 * 3DSTATE_DEPTH.Height = 1
822 * 3DSTATE_DEPTH.SuraceFormat = D16_UNORM
823 * 3DSTATE_DEPTH.SurfaceBaseAddress = 0
824 * 3DSTATE_DEPTH.HierarchicalDepthBufferEnable = 0
825 * 3DSTATE_WM_DEPTH_STENCIL.DepthTestEnable = 0
826 * 3DSTATE_WM_DEPTH_STENCIL.DepthBufferWriteEnable = 0
828 * The PRM is wrong, though. The width and height must be programmed to
829 * actual framebuffer's width and height, even when neither depth buffer
830 * nor stencil buffer is present.
832 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_DEPTH_BUFFER
,
833 .SurfaceType
= SURFTYPE_2D
,
834 .SurfaceFormat
= D16_UNORM
,
835 .Width
= fb
->width
- 1,
836 .Height
= fb
->height
- 1,
837 .StencilWriteEnable
= has_stencil
);
840 /* Emit 3DSTATE_STENCIL_BUFFER */
842 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_STENCIL_BUFFER
,
843 .StencilBufferObjectControlState
= GEN7_MOCS
,
845 /* Stencil buffers have strange pitch. The PRM says:
847 * The pitch must be set to 2x the value computed based on width,
848 * as the stencil buffer is stored with two rows interleaved.
850 .SurfacePitch
= 2 * image
->stencil_surface
.stride
- 1,
852 .SurfaceBaseAddress
= {
854 .offset
= image
->offset
+ image
->stencil_surface
.offset
,
857 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_STENCIL_BUFFER
);
860 /* Disable hierarchial depth buffers. */
861 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_HIER_DEPTH_BUFFER
);
863 /* Clear the clear params. */
864 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_CLEAR_PARAMS
);
867 GENX_FUNC(GEN7
, GEN7
) void
868 genX(cmd_buffer_begin_subpass
)(struct anv_cmd_buffer
*cmd_buffer
,
869 struct anv_subpass
*subpass
)
871 cmd_buffer
->state
.subpass
= subpass
;
872 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
874 cmd_buffer_emit_depth_stencil(cmd_buffer
);
878 begin_render_pass(struct anv_cmd_buffer
*cmd_buffer
,
879 const VkRenderPassBeginInfo
* pRenderPassBegin
)
881 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
882 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
884 cmd_buffer
->state
.framebuffer
= framebuffer
;
885 cmd_buffer
->state
.pass
= pass
;
887 const VkRect2D
*render_area
= &pRenderPassBegin
->renderArea
;
889 anv_batch_emit(&cmd_buffer
->batch
, GEN7_3DSTATE_DRAWING_RECTANGLE
,
890 .ClippedDrawingRectangleYMin
= render_area
->offset
.y
,
891 .ClippedDrawingRectangleXMin
= render_area
->offset
.x
,
892 .ClippedDrawingRectangleYMax
=
893 render_area
->offset
.y
+ render_area
->extent
.height
- 1,
894 .ClippedDrawingRectangleXMax
=
895 render_area
->offset
.x
+ render_area
->extent
.width
- 1,
896 .DrawingRectangleOriginY
= 0,
897 .DrawingRectangleOriginX
= 0);
899 anv_cmd_buffer_clear_attachments(cmd_buffer
, pass
,
900 pRenderPassBegin
->pClearValues
);
903 void genX(CmdBeginRenderPass
)(
904 VkCmdBuffer cmdBuffer
,
905 const VkRenderPassBeginInfo
* pRenderPassBegin
,
906 VkRenderPassContents contents
)
908 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
909 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
911 begin_render_pass(cmd_buffer
, pRenderPassBegin
);
913 gen7_cmd_buffer_begin_subpass(cmd_buffer
, pass
->subpasses
);
916 void genX(CmdNextSubpass
)(
917 VkCmdBuffer cmdBuffer
,
918 VkRenderPassContents contents
)
920 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
922 assert(cmd_buffer
->level
== VK_CMD_BUFFER_LEVEL_PRIMARY
);
924 gen7_cmd_buffer_begin_subpass(cmd_buffer
, cmd_buffer
->state
.subpass
+ 1);
927 void genX(CmdEndRenderPass
)(
928 VkCmdBuffer cmdBuffer
)
930 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
932 /* Emit a flushing pipe control at the end of a pass. This is kind of a
933 * hack but it ensures that render targets always actually get written.
934 * Eventually, we should do flushing based on image format transitions
935 * or something of that nature.
937 anv_batch_emit(&cmd_buffer
->batch
, GEN7_PIPE_CONTROL
,
938 .PostSyncOperation
= NoWrite
,
939 .RenderTargetCacheFlushEnable
= true,
940 .InstructionCacheInvalidateEnable
= true,
941 .DepthCacheFlushEnable
= true,
942 .VFCacheInvalidationEnable
= true,
943 .TextureCacheInvalidationEnable
= true,
944 .CommandStreamerStallEnable
= true);