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 "gen8_pack.h"
33 #include "gen9_pack.h"
36 cmd_buffer_flush_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
38 static const uint32_t push_constant_opcodes
[] = {
39 [MESA_SHADER_VERTEX
] = 21,
40 [MESA_SHADER_TESS_CTRL
] = 25, /* HS */
41 [MESA_SHADER_TESS_EVAL
] = 26, /* DS */
42 [MESA_SHADER_GEOMETRY
] = 22,
43 [MESA_SHADER_FRAGMENT
] = 23,
44 [MESA_SHADER_COMPUTE
] = 0,
47 VkShaderStageFlags flushed
= 0;
49 anv_foreach_stage(stage
, cmd_buffer
->state
.push_constants_dirty
) {
50 if (stage
== MESA_SHADER_COMPUTE
)
53 struct anv_state state
= anv_cmd_buffer_push_constants(cmd_buffer
, stage
);
55 if (state
.offset
== 0)
58 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CONSTANT_VS
),
59 ._3DCommandSubOpcode
= push_constant_opcodes
[stage
],
61 .PointerToConstantBuffer0
= { .offset
= state
.offset
},
62 .ConstantBuffer0ReadLength
= DIV_ROUND_UP(state
.alloc_size
, 32),
65 flushed
|= mesa_to_vk_shader_stage(stage
);
68 cmd_buffer
->state
.push_constants_dirty
&= ~flushed
;
73 emit_viewport_state(struct anv_cmd_buffer
*cmd_buffer
,
74 uint32_t count
, const VkViewport
*viewports
)
76 struct anv_state sf_clip_state
=
77 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, count
* 64, 64);
78 struct anv_state cc_state
=
79 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, count
* 8, 32);
81 for (uint32_t i
= 0; i
< count
; i
++) {
82 const VkViewport
*vp
= &viewports
[i
];
84 /* The gen7 state struct has just the matrix and guardband fields, the
85 * gen8 struct adds the min/max viewport fields. */
86 struct GENX(SF_CLIP_VIEWPORT
) sf_clip_viewport
= {
87 .ViewportMatrixElementm00
= vp
->width
/ 2,
88 .ViewportMatrixElementm11
= vp
->height
/ 2,
89 .ViewportMatrixElementm22
= (vp
->maxDepth
- vp
->minDepth
) / 2,
90 .ViewportMatrixElementm30
= vp
->x
+ vp
->width
/ 2,
91 .ViewportMatrixElementm31
= vp
->y
+ vp
->height
/ 2,
92 .ViewportMatrixElementm32
= (vp
->maxDepth
+ vp
->minDepth
) / 2,
93 .XMinClipGuardband
= -1.0f
,
94 .XMaxClipGuardband
= 1.0f
,
95 .YMinClipGuardband
= -1.0f
,
96 .YMaxClipGuardband
= 1.0f
,
97 .XMinViewPort
= vp
->x
,
98 .XMaxViewPort
= vp
->x
+ vp
->width
- 1,
99 .YMinViewPort
= vp
->y
,
100 .YMaxViewPort
= vp
->y
+ vp
->height
- 1,
103 struct GENX(CC_VIEWPORT
) cc_viewport
= {
104 .MinimumDepth
= vp
->minDepth
,
105 .MaximumDepth
= vp
->maxDepth
108 GENX(SF_CLIP_VIEWPORT_pack
)(NULL
, sf_clip_state
.map
+ i
* 64,
110 GENX(CC_VIEWPORT_pack
)(NULL
, cc_state
.map
+ i
* 32, &cc_viewport
);
113 if (!cmd_buffer
->device
->info
.has_llc
) {
114 anv_state_clflush(sf_clip_state
);
115 anv_state_clflush(cc_state
);
118 anv_batch_emit(&cmd_buffer
->batch
,
119 GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC
),
120 .CCViewportPointer
= cc_state
.offset
);
121 anv_batch_emit(&cmd_buffer
->batch
,
122 GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
),
123 .SFClipViewportPointer
= sf_clip_state
.offset
);
127 gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer
*cmd_buffer
)
129 if (cmd_buffer
->state
.dynamic
.viewport
.count
> 0) {
130 emit_viewport_state(cmd_buffer
, cmd_buffer
->state
.dynamic
.viewport
.count
,
131 cmd_buffer
->state
.dynamic
.viewport
.viewports
);
133 /* If viewport count is 0, this is taken to mean "use the default" */
134 emit_viewport_state(cmd_buffer
, 1,
138 .width
= cmd_buffer
->state
.framebuffer
->width
,
139 .height
= cmd_buffer
->state
.framebuffer
->height
,
148 cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
150 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
153 uint32_t vb_emit
= cmd_buffer
->state
.vb_dirty
& pipeline
->vb_used
;
155 assert((pipeline
->active_stages
& VK_SHADER_STAGE_COMPUTE_BIT
) == 0);
157 if (cmd_buffer
->state
.current_pipeline
!= _3D
) {
158 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
),
162 .PipelineSelection
= _3D
);
163 cmd_buffer
->state
.current_pipeline
= _3D
;
167 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
168 const uint32_t num_dwords
= 1 + num_buffers
* 4;
170 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
171 GENX(3DSTATE_VERTEX_BUFFERS
));
173 for_each_bit(vb
, vb_emit
) {
174 struct anv_buffer
*buffer
= cmd_buffer
->state
.vertex_bindings
[vb
].buffer
;
175 uint32_t offset
= cmd_buffer
->state
.vertex_bindings
[vb
].offset
;
177 struct GENX(VERTEX_BUFFER_STATE
) state
= {
178 .VertexBufferIndex
= vb
,
179 .MemoryObjectControlState
= GENX(MOCS
),
180 .AddressModifyEnable
= true,
181 .BufferPitch
= pipeline
->binding_stride
[vb
],
182 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
183 .BufferSize
= buffer
->size
- offset
186 GENX(VERTEX_BUFFER_STATE_pack
)(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
191 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_PIPELINE
) {
192 /* If somebody compiled a pipeline after starting a command buffer the
193 * scratch bo may have grown since we started this cmd buffer (and
194 * emitted STATE_BASE_ADDRESS). If we're binding that pipeline now,
195 * reemit STATE_BASE_ADDRESS so that we use the bigger scratch bo. */
196 if (cmd_buffer
->state
.scratch_size
< pipeline
->total_scratch
)
197 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
199 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
203 /* On SKL+ the new constants don't take effect until the next corresponding
204 * 3DSTATE_BINDING_TABLE_POINTER_* command is parsed so we need to ensure
205 * that is sent. As it is, we re-emit binding tables but we could hold on
206 * to the offset of the most recent binding table and only re-emit the
207 * 3DSTATE_BINDING_TABLE_POINTER_* command.
209 cmd_buffer
->state
.descriptors_dirty
|=
210 cmd_buffer
->state
.push_constants_dirty
&
211 cmd_buffer
->state
.pipeline
->active_stages
;
214 if (cmd_buffer
->state
.descriptors_dirty
)
215 gen7_cmd_buffer_flush_descriptor_sets(cmd_buffer
);
217 if (cmd_buffer
->state
.push_constants_dirty
)
218 cmd_buffer_flush_push_constants(cmd_buffer
);
220 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
)
221 gen8_cmd_buffer_emit_viewport(cmd_buffer
);
223 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
)
224 gen7_cmd_buffer_emit_scissor(cmd_buffer
);
226 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
227 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
)) {
228 uint32_t sf_dw
[GENX(3DSTATE_SF_length
)];
229 struct GENX(3DSTATE_SF
) sf
= {
230 GENX(3DSTATE_SF_header
),
231 .LineWidth
= cmd_buffer
->state
.dynamic
.line_width
,
233 GENX(3DSTATE_SF_pack
)(NULL
, sf_dw
, &sf
);
235 anv_batch_emit_merge(&cmd_buffer
->batch
, sf_dw
, pipeline
->gen8
.sf
);
238 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
239 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
)){
240 bool enable_bias
= cmd_buffer
->state
.dynamic
.depth_bias
.bias
!= 0.0f
||
241 cmd_buffer
->state
.dynamic
.depth_bias
.slope
!= 0.0f
;
243 uint32_t raster_dw
[GENX(3DSTATE_RASTER_length
)];
244 struct GENX(3DSTATE_RASTER
) raster
= {
245 GENX(3DSTATE_RASTER_header
),
246 .GlobalDepthOffsetEnableSolid
= enable_bias
,
247 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
248 .GlobalDepthOffsetEnablePoint
= enable_bias
,
249 .GlobalDepthOffsetConstant
= cmd_buffer
->state
.dynamic
.depth_bias
.bias
,
250 .GlobalDepthOffsetScale
= cmd_buffer
->state
.dynamic
.depth_bias
.slope
,
251 .GlobalDepthOffsetClamp
= cmd_buffer
->state
.dynamic
.depth_bias
.clamp
253 GENX(3DSTATE_RASTER_pack
)(NULL
, raster_dw
, &raster
);
254 anv_batch_emit_merge(&cmd_buffer
->batch
, raster_dw
,
255 pipeline
->gen8
.raster
);
258 /* Stencil reference values moved from COLOR_CALC_STATE in gen8 to
259 * 3DSTATE_WM_DEPTH_STENCIL in gen9. That means the dirty bits gets split
260 * across different state packets for gen8 and gen9. We handle that by
261 * using a big old #if switch here.
264 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
|
265 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
)) {
266 struct anv_state cc_state
=
267 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
268 GEN8_COLOR_CALC_STATE_length
, 64);
269 struct GEN8_COLOR_CALC_STATE cc
= {
270 .BlendConstantColorRed
= cmd_buffer
->state
.dynamic
.blend_constants
[0],
271 .BlendConstantColorGreen
= cmd_buffer
->state
.dynamic
.blend_constants
[1],
272 .BlendConstantColorBlue
= cmd_buffer
->state
.dynamic
.blend_constants
[2],
273 .BlendConstantColorAlpha
= cmd_buffer
->state
.dynamic
.blend_constants
[3],
274 .StencilReferenceValue
=
275 cmd_buffer
->state
.dynamic
.stencil_reference
.front
,
276 .BackFaceStencilReferenceValue
=
277 cmd_buffer
->state
.dynamic
.stencil_reference
.back
,
279 GEN8_COLOR_CALC_STATE_pack(NULL
, cc_state
.map
, &cc
);
281 if (!cmd_buffer
->device
->info
.has_llc
)
282 anv_state_clflush(cc_state
);
284 anv_batch_emit(&cmd_buffer
->batch
,
285 GEN8_3DSTATE_CC_STATE_POINTERS
,
286 .ColorCalcStatePointer
= cc_state
.offset
,
287 .ColorCalcStatePointerValid
= true);
290 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
291 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
|
292 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
)) {
293 uint32_t wm_depth_stencil_dw
[GEN8_3DSTATE_WM_DEPTH_STENCIL_length
];
295 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
296 GEN8_3DSTATE_WM_DEPTH_STENCIL_header
,
298 /* Is this what we need to do? */
299 .StencilBufferWriteEnable
=
300 cmd_buffer
->state
.dynamic
.stencil_write_mask
.front
!= 0,
303 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.front
& 0xff,
305 cmd_buffer
->state
.dynamic
.stencil_write_mask
.front
& 0xff,
307 .BackfaceStencilTestMask
=
308 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.back
& 0xff,
309 .BackfaceStencilWriteMask
=
310 cmd_buffer
->state
.dynamic
.stencil_write_mask
.back
& 0xff,
312 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, wm_depth_stencil_dw
,
315 anv_batch_emit_merge(&cmd_buffer
->batch
, wm_depth_stencil_dw
,
316 pipeline
->gen8
.wm_depth_stencil
);
319 if (cmd_buffer
->state
.dirty
& ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
) {
320 struct anv_state cc_state
=
321 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
322 GEN9_COLOR_CALC_STATE_length
, 64);
323 struct GEN9_COLOR_CALC_STATE cc
= {
324 .BlendConstantColorRed
= cmd_buffer
->state
.dynamic
.blend_constants
[0],
325 .BlendConstantColorGreen
= cmd_buffer
->state
.dynamic
.blend_constants
[1],
326 .BlendConstantColorBlue
= cmd_buffer
->state
.dynamic
.blend_constants
[2],
327 .BlendConstantColorAlpha
= cmd_buffer
->state
.dynamic
.blend_constants
[3],
329 GEN9_COLOR_CALC_STATE_pack(NULL
, cc_state
.map
, &cc
);
331 if (!cmd_buffer
->device
->info
.has_llc
)
332 anv_state_clflush(cc_state
);
334 anv_batch_emit(&cmd_buffer
->batch
,
335 GEN9_3DSTATE_CC_STATE_POINTERS
,
336 .ColorCalcStatePointer
= cc_state
.offset
,
337 .ColorCalcStatePointerValid
= true);
340 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
341 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
|
342 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
|
343 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
)) {
344 uint32_t dwords
[GEN9_3DSTATE_WM_DEPTH_STENCIL_length
];
345 struct anv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
346 struct GEN9_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
347 GEN9_3DSTATE_WM_DEPTH_STENCIL_header
,
349 .StencilBufferWriteEnable
= d
->stencil_write_mask
.front
!= 0,
351 .StencilTestMask
= d
->stencil_compare_mask
.front
& 0xff,
352 .StencilWriteMask
= d
->stencil_write_mask
.front
& 0xff,
354 .BackfaceStencilTestMask
= d
->stencil_compare_mask
.back
& 0xff,
355 .BackfaceStencilWriteMask
= d
->stencil_write_mask
.back
& 0xff,
357 .StencilReferenceValue
= d
->stencil_reference
.front
,
358 .BackfaceStencilReferenceValue
= d
->stencil_reference
.back
360 GEN9_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, dwords
, &wm_depth_stencil
);
362 anv_batch_emit_merge(&cmd_buffer
->batch
, dwords
,
363 pipeline
->gen9
.wm_depth_stencil
);
367 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
368 ANV_CMD_DIRTY_INDEX_BUFFER
)) {
369 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_VF
),
370 .IndexedDrawCutIndexEnable
= pipeline
->primitive_restart
,
371 .CutIndex
= cmd_buffer
->state
.restart_index
,
375 cmd_buffer
->state
.vb_dirty
&= ~vb_emit
;
376 cmd_buffer
->state
.dirty
= 0;
380 VkCommandBuffer commandBuffer
,
381 uint32_t vertexCount
,
382 uint32_t instanceCount
,
383 uint32_t firstVertex
,
384 uint32_t firstInstance
)
386 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
388 cmd_buffer_flush_state(cmd_buffer
);
390 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
),
391 .VertexAccessType
= SEQUENTIAL
,
392 .VertexCountPerInstance
= vertexCount
,
393 .StartVertexLocation
= firstVertex
,
394 .InstanceCount
= instanceCount
,
395 .StartInstanceLocation
= firstInstance
,
396 .BaseVertexLocation
= 0);
399 void genX(CmdDrawIndexed
)(
400 VkCommandBuffer commandBuffer
,
402 uint32_t instanceCount
,
404 int32_t vertexOffset
,
405 uint32_t firstInstance
)
407 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
409 cmd_buffer_flush_state(cmd_buffer
);
411 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
),
412 .VertexAccessType
= RANDOM
,
413 .VertexCountPerInstance
= indexCount
,
414 .StartVertexLocation
= firstIndex
,
415 .InstanceCount
= instanceCount
,
416 .StartInstanceLocation
= firstInstance
,
417 .BaseVertexLocation
= vertexOffset
);
421 emit_lrm(struct anv_batch
*batch
,
422 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
424 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
),
425 .RegisterAddress
= reg
,
426 .MemoryAddress
= { bo
, offset
});
430 emit_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
432 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_IMM
),
433 .RegisterOffset
= reg
,
437 /* Auto-Draw / Indirect Registers */
438 #define GEN7_3DPRIM_END_OFFSET 0x2420
439 #define GEN7_3DPRIM_START_VERTEX 0x2430
440 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
441 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
442 #define GEN7_3DPRIM_START_INSTANCE 0x243C
443 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
445 void genX(CmdDrawIndirect
)(
446 VkCommandBuffer commandBuffer
,
452 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
453 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
454 struct anv_bo
*bo
= buffer
->bo
;
455 uint32_t bo_offset
= buffer
->offset
+ offset
;
457 cmd_buffer_flush_state(cmd_buffer
);
459 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
460 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
461 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
462 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
463 emit_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
465 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
),
466 .IndirectParameterEnable
= true,
467 .VertexAccessType
= SEQUENTIAL
);
470 void genX(CmdBindIndexBuffer
)(
471 VkCommandBuffer commandBuffer
,
474 VkIndexType indexType
)
476 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
477 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
479 static const uint32_t vk_to_gen_index_type
[] = {
480 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
481 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
484 static const uint32_t restart_index_for_type
[] = {
485 [VK_INDEX_TYPE_UINT16
] = UINT16_MAX
,
486 [VK_INDEX_TYPE_UINT32
] = UINT32_MAX
,
489 cmd_buffer
->state
.restart_index
= restart_index_for_type
[indexType
];
491 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_INDEX_BUFFER
),
492 .IndexFormat
= vk_to_gen_index_type
[indexType
],
493 .MemoryObjectControlState
= GENX(MOCS
),
494 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
495 .BufferSize
= buffer
->size
- offset
);
497 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_INDEX_BUFFER
;
501 flush_compute_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
)
503 struct anv_device
*device
= cmd_buffer
->device
;
504 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
505 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
508 result
= anv_cmd_buffer_emit_samplers(cmd_buffer
,
509 MESA_SHADER_COMPUTE
, &samplers
);
510 if (result
!= VK_SUCCESS
)
512 result
= anv_cmd_buffer_emit_binding_table(cmd_buffer
,
513 MESA_SHADER_COMPUTE
, &surfaces
);
514 if (result
!= VK_SUCCESS
)
517 struct anv_state push_state
= anv_cmd_buffer_cs_push_constants(cmd_buffer
);
519 const struct brw_cs_prog_data
*cs_prog_data
= &pipeline
->cs_prog_data
;
520 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
522 unsigned local_id_dwords
= cs_prog_data
->local_invocation_id_regs
* 8;
523 unsigned push_constant_data_size
=
524 (prog_data
->nr_params
+ local_id_dwords
) * 4;
525 unsigned reg_aligned_constant_size
= ALIGN(push_constant_data_size
, 32);
526 unsigned push_constant_regs
= reg_aligned_constant_size
/ 32;
528 if (push_state
.alloc_size
) {
529 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
),
530 .CURBETotalDataLength
= push_state
.alloc_size
,
531 .CURBEDataStartAddress
= push_state
.offset
);
534 struct anv_state state
=
535 anv_state_pool_emit(&device
->dynamic_state_pool
,
536 GENX(INTERFACE_DESCRIPTOR_DATA
), 64,
537 .KernelStartPointer
= pipeline
->cs_simd
,
538 .KernelStartPointerHigh
= 0,
539 .BindingTablePointer
= surfaces
.offset
,
540 .BindingTableEntryCount
= 0,
541 .SamplerStatePointer
= samplers
.offset
,
543 .ConstantIndirectURBEntryReadLength
= push_constant_regs
,
544 .ConstantURBEntryReadOffset
= 0,
545 .NumberofThreadsinGPGPUThreadGroup
= 0);
547 uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
548 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
),
549 .InterfaceDescriptorTotalLength
= size
,
550 .InterfaceDescriptorDataStartAddress
= state
.offset
);
556 cmd_buffer_flush_compute_state(struct anv_cmd_buffer
*cmd_buffer
)
558 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
561 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
563 if (cmd_buffer
->state
.current_pipeline
!= GPGPU
) {
564 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
),
568 .PipelineSelection
= GPGPU
);
569 cmd_buffer
->state
.current_pipeline
= GPGPU
;
572 if (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)
573 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
575 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
576 (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)) {
577 result
= flush_compute_descriptor_set(cmd_buffer
);
578 assert(result
== VK_SUCCESS
);
579 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
582 cmd_buffer
->state
.compute_dirty
= 0;
585 void genX(CmdDrawIndexedIndirect
)(
586 VkCommandBuffer commandBuffer
,
592 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
593 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
594 struct anv_bo
*bo
= buffer
->bo
;
595 uint32_t bo_offset
= buffer
->offset
+ offset
;
597 cmd_buffer_flush_state(cmd_buffer
);
599 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
600 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
601 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
602 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
603 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
605 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
),
606 .IndirectParameterEnable
= true,
607 .VertexAccessType
= RANDOM
);
610 void genX(CmdDispatch
)(
611 VkCommandBuffer commandBuffer
,
616 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
617 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
618 struct brw_cs_prog_data
*prog_data
= &pipeline
->cs_prog_data
;
620 cmd_buffer_flush_compute_state(cmd_buffer
);
622 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
),
623 .SIMDSize
= prog_data
->simd_size
/ 16,
624 .ThreadDepthCounterMaximum
= 0,
625 .ThreadHeightCounterMaximum
= 0,
626 .ThreadWidthCounterMaximum
= pipeline
->cs_thread_width_max
- 1,
627 .ThreadGroupIDXDimension
= x
,
628 .ThreadGroupIDYDimension
= y
,
629 .ThreadGroupIDZDimension
= z
,
630 .RightExecutionMask
= pipeline
->cs_right_mask
,
631 .BottomExecutionMask
= 0xffffffff);
633 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
));
636 #define GPGPU_DISPATCHDIMX 0x2500
637 #define GPGPU_DISPATCHDIMY 0x2504
638 #define GPGPU_DISPATCHDIMZ 0x2508
640 void genX(CmdDispatchIndirect
)(
641 VkCommandBuffer commandBuffer
,
645 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
646 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
647 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
648 struct brw_cs_prog_data
*prog_data
= &pipeline
->cs_prog_data
;
649 struct anv_bo
*bo
= buffer
->bo
;
650 uint32_t bo_offset
= buffer
->offset
+ offset
;
652 cmd_buffer_flush_compute_state(cmd_buffer
);
654 emit_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMX
, bo
, bo_offset
);
655 emit_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMY
, bo
, bo_offset
+ 4);
656 emit_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMZ
, bo
, bo_offset
+ 8);
658 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
),
659 .IndirectParameterEnable
= true,
660 .SIMDSize
= prog_data
->simd_size
/ 16,
661 .ThreadDepthCounterMaximum
= 0,
662 .ThreadHeightCounterMaximum
= 0,
663 .ThreadWidthCounterMaximum
= pipeline
->cs_thread_width_max
- 1,
664 .RightExecutionMask
= pipeline
->cs_right_mask
,
665 .BottomExecutionMask
= 0xffffffff);
667 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
));
671 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
673 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
674 const struct anv_image_view
*iview
=
675 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
676 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
677 const bool has_depth
= iview
&& iview
->format
->depth_format
;
678 const bool has_stencil
= iview
&& iview
->format
->has_stencil
;
680 /* FIXME: Implement the PMA stall W/A */
681 /* FIXME: Width and Height are wrong */
683 /* Emit 3DSTATE_DEPTH_BUFFER */
685 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
),
686 .SurfaceType
= SURFTYPE_2D
,
687 .DepthWriteEnable
= iview
->format
->depth_format
,
688 .StencilWriteEnable
= has_stencil
,
689 .HierarchicalDepthBufferEnable
= false,
690 .SurfaceFormat
= iview
->format
->depth_format
,
691 .SurfacePitch
= image
->depth_surface
.isl
.row_pitch
- 1,
692 .SurfaceBaseAddress
= {
694 .offset
= image
->depth_surface
.offset
,
696 .Height
= fb
->height
- 1,
697 .Width
= fb
->width
- 1,
700 .MinimumArrayElement
= 0,
701 .DepthBufferObjectControlState
= GENX(MOCS
),
702 .RenderTargetViewExtent
= 1 - 1,
703 .SurfaceQPitch
= isl_surf_get_array_pitch_el_rows(&image
->depth_surface
.isl
) >> 2);
705 /* Even when no depth buffer is present, the hardware requires that
706 * 3DSTATE_DEPTH_BUFFER be programmed correctly. The Broadwell PRM says:
708 * If a null depth buffer is bound, the driver must instead bind depth as:
709 * 3DSTATE_DEPTH.SurfaceType = SURFTYPE_2D
710 * 3DSTATE_DEPTH.Width = 1
711 * 3DSTATE_DEPTH.Height = 1
712 * 3DSTATE_DEPTH.SuraceFormat = D16_UNORM
713 * 3DSTATE_DEPTH.SurfaceBaseAddress = 0
714 * 3DSTATE_DEPTH.HierarchicalDepthBufferEnable = 0
715 * 3DSTATE_WM_DEPTH_STENCIL.DepthTestEnable = 0
716 * 3DSTATE_WM_DEPTH_STENCIL.DepthBufferWriteEnable = 0
718 * The PRM is wrong, though. The width and height must be programmed to
719 * actual framebuffer's width and height, even when neither depth buffer
720 * nor stencil buffer is present.
722 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
),
723 .SurfaceType
= SURFTYPE_2D
,
724 .SurfaceFormat
= D16_UNORM
,
725 .Width
= fb
->width
- 1,
726 .Height
= fb
->height
- 1,
727 .StencilWriteEnable
= has_stencil
);
730 /* Emit 3DSTATE_STENCIL_BUFFER */
732 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
),
733 .StencilBufferEnable
= true,
734 .StencilBufferObjectControlState
= GENX(MOCS
),
736 /* Stencil buffers have strange pitch. The PRM says:
738 * The pitch must be set to 2x the value computed based on width,
739 * as the stencil buffer is stored with two rows interleaved.
741 .SurfacePitch
= 2 * image
->stencil_surface
.isl
.row_pitch
- 1,
743 .SurfaceBaseAddress
= {
745 .offset
= image
->offset
+ image
->stencil_surface
.offset
,
747 .SurfaceQPitch
= isl_surf_get_array_pitch_el_rows(&image
->stencil_surface
.isl
) >> 2);
749 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
));
752 /* Disable hierarchial depth buffers. */
753 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_HIER_DEPTH_BUFFER
));
755 /* Clear the clear params. */
756 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CLEAR_PARAMS
));
760 genX(cmd_buffer_begin_subpass
)(struct anv_cmd_buffer
*cmd_buffer
,
761 struct anv_subpass
*subpass
)
763 cmd_buffer
->state
.subpass
= subpass
;
765 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
767 cmd_buffer_emit_depth_stencil(cmd_buffer
);
770 void genX(CmdBeginRenderPass
)(
771 VkCommandBuffer commandBuffer
,
772 const VkRenderPassBeginInfo
* pRenderPassBegin
,
773 VkSubpassContents contents
)
775 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
776 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
777 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
779 cmd_buffer
->state
.framebuffer
= framebuffer
;
780 cmd_buffer
->state
.pass
= pass
;
782 const VkRect2D
*render_area
= &pRenderPassBegin
->renderArea
;
784 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DRAWING_RECTANGLE
),
785 .ClippedDrawingRectangleYMin
= render_area
->offset
.y
,
786 .ClippedDrawingRectangleXMin
= render_area
->offset
.x
,
787 .ClippedDrawingRectangleYMax
=
788 render_area
->offset
.y
+ render_area
->extent
.height
- 1,
789 .ClippedDrawingRectangleXMax
=
790 render_area
->offset
.x
+ render_area
->extent
.width
- 1,
791 .DrawingRectangleOriginY
= 0,
792 .DrawingRectangleOriginX
= 0);
794 anv_cmd_buffer_clear_attachments(cmd_buffer
, pass
,
795 pRenderPassBegin
->pClearValues
);
797 genX(cmd_buffer_begin_subpass
)(cmd_buffer
, pass
->subpasses
);
800 void genX(CmdNextSubpass
)(
801 VkCommandBuffer commandBuffer
,
802 VkSubpassContents contents
)
804 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
806 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
808 genX(cmd_buffer_begin_subpass
)(cmd_buffer
, cmd_buffer
->state
.subpass
+ 1);
811 void genX(CmdEndRenderPass
)(
812 VkCommandBuffer commandBuffer
)
814 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
816 /* Emit a flushing pipe control at the end of a pass. This is kind of a
817 * hack but it ensures that render targets always actually get written.
818 * Eventually, we should do flushing based on image format transitions
819 * or something of that nature.
821 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
),
822 .PostSyncOperation
= NoWrite
,
823 .RenderTargetCacheFlushEnable
= true,
824 .InstructionCacheInvalidateEnable
= true,
825 .DepthCacheFlushEnable
= true,
826 .VFCacheInvalidationEnable
= true,
827 .TextureCacheInvalidationEnable
= true,
828 .CommandStreamerStallEnable
= true);
832 emit_ps_depth_count(struct anv_batch
*batch
,
833 struct anv_bo
*bo
, uint32_t offset
)
835 anv_batch_emit(batch
, GENX(PIPE_CONTROL
),
836 .DestinationAddressType
= DAT_PPGTT
,
837 .PostSyncOperation
= WritePSDepthCount
,
838 .Address
= { bo
, offset
}); /* FIXME: This is only lower 32 bits */
841 void genX(CmdBeginQuery
)(
842 VkCommandBuffer commandBuffer
,
843 VkQueryPool queryPool
,
845 VkQueryControlFlags flags
)
847 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
848 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
850 switch (pool
->type
) {
851 case VK_QUERY_TYPE_OCCLUSION
:
852 emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
853 entry
* sizeof(struct anv_query_pool_slot
));
856 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
862 void genX(CmdEndQuery
)(
863 VkCommandBuffer commandBuffer
,
864 VkQueryPool queryPool
,
867 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
868 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
870 switch (pool
->type
) {
871 case VK_QUERY_TYPE_OCCLUSION
:
872 emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
873 entry
* sizeof(struct anv_query_pool_slot
) + 8);
876 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
882 #define TIMESTAMP 0x2358
884 void genX(CmdWriteTimestamp
)(
885 VkCommandBuffer commandBuffer
,
886 VkPipelineStageFlagBits pipelineStage
,
887 VkQueryPool queryPool
,
890 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
891 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
893 assert(pool
->type
== VK_QUERY_TYPE_TIMESTAMP
);
895 switch (pipelineStage
) {
896 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
:
897 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
),
898 .RegisterAddress
= TIMESTAMP
,
899 .MemoryAddress
= { &pool
->bo
, entry
* 8 });
900 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
),
901 .RegisterAddress
= TIMESTAMP
+ 4,
902 .MemoryAddress
= { &pool
->bo
, entry
* 8 + 4 });
906 /* Everything else is bottom-of-pipe */
907 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
),
908 .DestinationAddressType
= DAT_PPGTT
,
909 .PostSyncOperation
= WriteTimestamp
,
910 .Address
= /* FIXME: This is only lower 32 bits */
911 { &pool
->bo
, entry
* 8 });
916 #define alu_opcode(v) __gen_field((v), 20, 31)
917 #define alu_operand1(v) __gen_field((v), 10, 19)
918 #define alu_operand2(v) __gen_field((v), 0, 9)
919 #define alu(opcode, operand1, operand2) \
920 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
922 #define OPCODE_NOOP 0x000
923 #define OPCODE_LOAD 0x080
924 #define OPCODE_LOADINV 0x480
925 #define OPCODE_LOAD0 0x081
926 #define OPCODE_LOAD1 0x481
927 #define OPCODE_ADD 0x100
928 #define OPCODE_SUB 0x101
929 #define OPCODE_AND 0x102
930 #define OPCODE_OR 0x103
931 #define OPCODE_XOR 0x104
932 #define OPCODE_STORE 0x180
933 #define OPCODE_STOREINV 0x580
935 #define OPERAND_R0 0x00
936 #define OPERAND_R1 0x01
937 #define OPERAND_R2 0x02
938 #define OPERAND_R3 0x03
939 #define OPERAND_R4 0x04
940 #define OPERAND_SRCA 0x20
941 #define OPERAND_SRCB 0x21
942 #define OPERAND_ACCU 0x31
943 #define OPERAND_ZF 0x32
944 #define OPERAND_CF 0x33
946 #define CS_GPR(n) (0x2600 + (n) * 8)
949 emit_load_alu_reg_u64(struct anv_batch
*batch
, uint32_t reg
,
950 struct anv_bo
*bo
, uint32_t offset
)
952 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
),
953 .RegisterAddress
= reg
,
954 .MemoryAddress
= { bo
, offset
});
955 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
),
956 .RegisterAddress
= reg
+ 4,
957 .MemoryAddress
= { bo
, offset
+ 4 });
960 void genX(CmdCopyQueryPoolResults
)(
961 VkCommandBuffer commandBuffer
,
962 VkQueryPool queryPool
,
966 VkDeviceSize destOffset
,
967 VkDeviceSize destStride
,
968 VkQueryResultFlags flags
)
970 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
971 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
972 ANV_FROM_HANDLE(anv_buffer
, buffer
, destBuffer
);
973 uint32_t slot_offset
, dst_offset
;
975 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
976 /* Where is the availabilty info supposed to go? */
977 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
981 assert(pool
->type
== VK_QUERY_TYPE_OCCLUSION
);
983 /* FIXME: If we're not waiting, should we just do this on the CPU? */
984 if (flags
& VK_QUERY_RESULT_WAIT_BIT
)
985 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
),
986 .CommandStreamerStallEnable
= true,
987 .StallAtPixelScoreboard
= true);
989 dst_offset
= buffer
->offset
+ destOffset
;
990 for (uint32_t i
= 0; i
< queryCount
; i
++) {
992 slot_offset
= (startQuery
+ i
) * sizeof(struct anv_query_pool_slot
);
994 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(0), &pool
->bo
, slot_offset
);
995 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(1), &pool
->bo
, slot_offset
+ 8);
997 /* FIXME: We need to clamp the result for 32 bit. */
999 uint32_t *dw
= anv_batch_emitn(&cmd_buffer
->batch
, 5, GENX(MI_MATH
));
1000 dw
[1] = alu(OPCODE_LOAD
, OPERAND_SRCA
, OPERAND_R1
);
1001 dw
[2] = alu(OPCODE_LOAD
, OPERAND_SRCB
, OPERAND_R0
);
1002 dw
[3] = alu(OPCODE_SUB
, 0, 0);
1003 dw
[4] = alu(OPCODE_STORE
, OPERAND_R2
, OPERAND_ACCU
);
1005 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
),
1006 .RegisterAddress
= CS_GPR(2),
1007 /* FIXME: This is only lower 32 bits */
1008 .MemoryAddress
= { buffer
->bo
, dst_offset
});
1010 if (flags
& VK_QUERY_RESULT_64_BIT
)
1011 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
),
1012 .RegisterAddress
= CS_GPR(2) + 4,
1013 /* FIXME: This is only lower 32 bits */
1014 .MemoryAddress
= { buffer
->bo
, dst_offset
+ 4 });
1016 dst_offset
+= destStride
;