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
) * sizeof(gl_constant_value
);
525 unsigned reg_aligned_constant_size
= ALIGN(push_constant_data_size
, 32);
526 unsigned push_constant_regs
= reg_aligned_constant_size
/ 32;
528 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
),
529 .CURBETotalDataLength
= push_state
.alloc_size
,
530 .CURBEDataStartAddress
= push_state
.offset
);
532 struct anv_state state
=
533 anv_state_pool_emit(&device
->dynamic_state_pool
,
534 GENX(INTERFACE_DESCRIPTOR_DATA
), 64,
535 .KernelStartPointer
= pipeline
->cs_simd
,
536 .KernelStartPointerHigh
= 0,
537 .BindingTablePointer
= surfaces
.offset
,
538 .BindingTableEntryCount
= 0,
539 .SamplerStatePointer
= samplers
.offset
,
541 .ConstantIndirectURBEntryReadLength
= push_constant_regs
,
542 .ConstantURBEntryReadOffset
= 0,
543 .NumberofThreadsinGPGPUThreadGroup
= 0);
545 uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
546 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
),
547 .InterfaceDescriptorTotalLength
= size
,
548 .InterfaceDescriptorDataStartAddress
= state
.offset
);
554 cmd_buffer_flush_compute_state(struct anv_cmd_buffer
*cmd_buffer
)
556 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
559 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
561 if (cmd_buffer
->state
.current_pipeline
!= GPGPU
) {
562 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPELINE_SELECT
),
566 .PipelineSelection
= GPGPU
);
567 cmd_buffer
->state
.current_pipeline
= GPGPU
;
570 if (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)
571 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
573 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
574 (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)) {
575 result
= flush_compute_descriptor_set(cmd_buffer
);
576 assert(result
== VK_SUCCESS
);
577 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
580 cmd_buffer
->state
.compute_dirty
= 0;
583 void genX(CmdDrawIndexedIndirect
)(
584 VkCommandBuffer commandBuffer
,
590 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
591 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
592 struct anv_bo
*bo
= buffer
->bo
;
593 uint32_t bo_offset
= buffer
->offset
+ offset
;
595 cmd_buffer_flush_state(cmd_buffer
);
597 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
598 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
599 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
600 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
601 emit_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
603 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DPRIMITIVE
),
604 .IndirectParameterEnable
= true,
605 .VertexAccessType
= RANDOM
);
608 void genX(CmdDispatch
)(
609 VkCommandBuffer commandBuffer
,
614 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
615 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
616 struct brw_cs_prog_data
*prog_data
= &pipeline
->cs_prog_data
;
618 cmd_buffer_flush_compute_state(cmd_buffer
);
620 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
),
621 .SIMDSize
= prog_data
->simd_size
/ 16,
622 .ThreadDepthCounterMaximum
= 0,
623 .ThreadHeightCounterMaximum
= 0,
624 .ThreadWidthCounterMaximum
= pipeline
->cs_thread_width_max
- 1,
625 .ThreadGroupIDXDimension
= x
,
626 .ThreadGroupIDYDimension
= y
,
627 .ThreadGroupIDZDimension
= z
,
628 .RightExecutionMask
= pipeline
->cs_right_mask
,
629 .BottomExecutionMask
= 0xffffffff);
631 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
));
634 #define GPGPU_DISPATCHDIMX 0x2500
635 #define GPGPU_DISPATCHDIMY 0x2504
636 #define GPGPU_DISPATCHDIMZ 0x2508
638 void genX(CmdDispatchIndirect
)(
639 VkCommandBuffer commandBuffer
,
643 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
644 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
645 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
646 struct brw_cs_prog_data
*prog_data
= &pipeline
->cs_prog_data
;
647 struct anv_bo
*bo
= buffer
->bo
;
648 uint32_t bo_offset
= buffer
->offset
+ offset
;
650 cmd_buffer_flush_compute_state(cmd_buffer
);
652 emit_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMX
, bo
, bo_offset
);
653 emit_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMY
, bo
, bo_offset
+ 4);
654 emit_lrm(&cmd_buffer
->batch
, GPGPU_DISPATCHDIMZ
, bo
, bo_offset
+ 8);
656 anv_batch_emit(&cmd_buffer
->batch
, GENX(GPGPU_WALKER
),
657 .IndirectParameterEnable
= true,
658 .SIMDSize
= prog_data
->simd_size
/ 16,
659 .ThreadDepthCounterMaximum
= 0,
660 .ThreadHeightCounterMaximum
= 0,
661 .ThreadWidthCounterMaximum
= pipeline
->cs_thread_width_max
- 1,
662 .RightExecutionMask
= pipeline
->cs_right_mask
,
663 .BottomExecutionMask
= 0xffffffff);
665 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_STATE_FLUSH
));
669 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
)
671 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
672 const struct anv_image_view
*iview
=
673 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
674 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
675 const bool has_depth
= iview
&& iview
->format
->depth_format
;
676 const bool has_stencil
= iview
&& iview
->format
->has_stencil
;
678 /* FIXME: Implement the PMA stall W/A */
679 /* FIXME: Width and Height are wrong */
681 /* Emit 3DSTATE_DEPTH_BUFFER */
683 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
),
684 .SurfaceType
= SURFTYPE_2D
,
685 .DepthWriteEnable
= iview
->format
->depth_format
,
686 .StencilWriteEnable
= has_stencil
,
687 .HierarchicalDepthBufferEnable
= false,
688 .SurfaceFormat
= iview
->format
->depth_format
,
689 .SurfacePitch
= image
->depth_surface
.isl
.row_pitch
- 1,
690 .SurfaceBaseAddress
= {
692 .offset
= image
->depth_surface
.offset
,
694 .Height
= fb
->height
- 1,
695 .Width
= fb
->width
- 1,
698 .MinimumArrayElement
= 0,
699 .DepthBufferObjectControlState
= GENX(MOCS
),
700 .RenderTargetViewExtent
= 1 - 1,
701 .SurfaceQPitch
= isl_surf_get_array_pitch_el_rows(&image
->depth_surface
.isl
) >> 2);
703 /* Even when no depth buffer is present, the hardware requires that
704 * 3DSTATE_DEPTH_BUFFER be programmed correctly. The Broadwell PRM says:
706 * If a null depth buffer is bound, the driver must instead bind depth as:
707 * 3DSTATE_DEPTH.SurfaceType = SURFTYPE_2D
708 * 3DSTATE_DEPTH.Width = 1
709 * 3DSTATE_DEPTH.Height = 1
710 * 3DSTATE_DEPTH.SuraceFormat = D16_UNORM
711 * 3DSTATE_DEPTH.SurfaceBaseAddress = 0
712 * 3DSTATE_DEPTH.HierarchicalDepthBufferEnable = 0
713 * 3DSTATE_WM_DEPTH_STENCIL.DepthTestEnable = 0
714 * 3DSTATE_WM_DEPTH_STENCIL.DepthBufferWriteEnable = 0
716 * The PRM is wrong, though. The width and height must be programmed to
717 * actual framebuffer's width and height, even when neither depth buffer
718 * nor stencil buffer is present.
720 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BUFFER
),
721 .SurfaceType
= SURFTYPE_2D
,
722 .SurfaceFormat
= D16_UNORM
,
723 .Width
= fb
->width
- 1,
724 .Height
= fb
->height
- 1,
725 .StencilWriteEnable
= has_stencil
);
728 /* Emit 3DSTATE_STENCIL_BUFFER */
730 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
),
731 .StencilBufferEnable
= true,
732 .StencilBufferObjectControlState
= GENX(MOCS
),
734 /* Stencil buffers have strange pitch. The PRM says:
736 * The pitch must be set to 2x the value computed based on width,
737 * as the stencil buffer is stored with two rows interleaved.
739 .SurfacePitch
= 2 * image
->stencil_surface
.isl
.row_pitch
- 1,
741 .SurfaceBaseAddress
= {
743 .offset
= image
->offset
+ image
->stencil_surface
.offset
,
745 .SurfaceQPitch
= isl_surf_get_array_pitch_el_rows(&image
->stencil_surface
.isl
) >> 2);
747 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_STENCIL_BUFFER
));
750 /* Disable hierarchial depth buffers. */
751 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_HIER_DEPTH_BUFFER
));
753 /* Clear the clear params. */
754 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CLEAR_PARAMS
));
758 genX(cmd_buffer_begin_subpass
)(struct anv_cmd_buffer
*cmd_buffer
,
759 struct anv_subpass
*subpass
)
761 cmd_buffer
->state
.subpass
= subpass
;
763 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
765 cmd_buffer_emit_depth_stencil(cmd_buffer
);
768 void genX(CmdBeginRenderPass
)(
769 VkCommandBuffer commandBuffer
,
770 const VkRenderPassBeginInfo
* pRenderPassBegin
,
771 VkSubpassContents contents
)
773 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
774 ANV_FROM_HANDLE(anv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
775 ANV_FROM_HANDLE(anv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
777 cmd_buffer
->state
.framebuffer
= framebuffer
;
778 cmd_buffer
->state
.pass
= pass
;
780 const VkRect2D
*render_area
= &pRenderPassBegin
->renderArea
;
782 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DRAWING_RECTANGLE
),
783 .ClippedDrawingRectangleYMin
= render_area
->offset
.y
,
784 .ClippedDrawingRectangleXMin
= render_area
->offset
.x
,
785 .ClippedDrawingRectangleYMax
=
786 render_area
->offset
.y
+ render_area
->extent
.height
- 1,
787 .ClippedDrawingRectangleXMax
=
788 render_area
->offset
.x
+ render_area
->extent
.width
- 1,
789 .DrawingRectangleOriginY
= 0,
790 .DrawingRectangleOriginX
= 0);
792 anv_cmd_buffer_clear_attachments(cmd_buffer
, pass
,
793 pRenderPassBegin
->pClearValues
);
795 genX(cmd_buffer_begin_subpass
)(cmd_buffer
, pass
->subpasses
);
798 void genX(CmdNextSubpass
)(
799 VkCommandBuffer commandBuffer
,
800 VkSubpassContents contents
)
802 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
804 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
806 genX(cmd_buffer_begin_subpass
)(cmd_buffer
, cmd_buffer
->state
.subpass
+ 1);
809 void genX(CmdEndRenderPass
)(
810 VkCommandBuffer commandBuffer
)
812 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
814 /* Emit a flushing pipe control at the end of a pass. This is kind of a
815 * hack but it ensures that render targets always actually get written.
816 * Eventually, we should do flushing based on image format transitions
817 * or something of that nature.
819 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
),
820 .PostSyncOperation
= NoWrite
,
821 .RenderTargetCacheFlushEnable
= true,
822 .InstructionCacheInvalidateEnable
= true,
823 .DepthCacheFlushEnable
= true,
824 .VFCacheInvalidationEnable
= true,
825 .TextureCacheInvalidationEnable
= true,
826 .CommandStreamerStallEnable
= true);
830 emit_ps_depth_count(struct anv_batch
*batch
,
831 struct anv_bo
*bo
, uint32_t offset
)
833 anv_batch_emit(batch
, GENX(PIPE_CONTROL
),
834 .DestinationAddressType
= DAT_PPGTT
,
835 .PostSyncOperation
= WritePSDepthCount
,
836 .Address
= { bo
, offset
}); /* FIXME: This is only lower 32 bits */
839 void genX(CmdBeginQuery
)(
840 VkCommandBuffer commandBuffer
,
841 VkQueryPool queryPool
,
843 VkQueryControlFlags flags
)
845 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
846 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
848 switch (pool
->type
) {
849 case VK_QUERY_TYPE_OCCLUSION
:
850 emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
851 entry
* sizeof(struct anv_query_pool_slot
));
854 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
860 void genX(CmdEndQuery
)(
861 VkCommandBuffer commandBuffer
,
862 VkQueryPool queryPool
,
865 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
866 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
868 switch (pool
->type
) {
869 case VK_QUERY_TYPE_OCCLUSION
:
870 emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
871 entry
* sizeof(struct anv_query_pool_slot
) + 8);
874 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
880 #define TIMESTAMP 0x2358
882 void genX(CmdWriteTimestamp
)(
883 VkCommandBuffer commandBuffer
,
884 VkPipelineStageFlagBits pipelineStage
,
885 VkQueryPool queryPool
,
888 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
889 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
891 assert(pool
->type
== VK_QUERY_TYPE_TIMESTAMP
);
893 switch (pipelineStage
) {
894 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
:
895 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
),
896 .RegisterAddress
= TIMESTAMP
,
897 .MemoryAddress
= { &pool
->bo
, entry
* 8 });
898 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
),
899 .RegisterAddress
= TIMESTAMP
+ 4,
900 .MemoryAddress
= { &pool
->bo
, entry
* 8 + 4 });
904 /* Everything else is bottom-of-pipe */
905 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
),
906 .DestinationAddressType
= DAT_PPGTT
,
907 .PostSyncOperation
= WriteTimestamp
,
908 .Address
= /* FIXME: This is only lower 32 bits */
909 { &pool
->bo
, entry
* 8 });
914 #define alu_opcode(v) __gen_field((v), 20, 31)
915 #define alu_operand1(v) __gen_field((v), 10, 19)
916 #define alu_operand2(v) __gen_field((v), 0, 9)
917 #define alu(opcode, operand1, operand2) \
918 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
920 #define OPCODE_NOOP 0x000
921 #define OPCODE_LOAD 0x080
922 #define OPCODE_LOADINV 0x480
923 #define OPCODE_LOAD0 0x081
924 #define OPCODE_LOAD1 0x481
925 #define OPCODE_ADD 0x100
926 #define OPCODE_SUB 0x101
927 #define OPCODE_AND 0x102
928 #define OPCODE_OR 0x103
929 #define OPCODE_XOR 0x104
930 #define OPCODE_STORE 0x180
931 #define OPCODE_STOREINV 0x580
933 #define OPERAND_R0 0x00
934 #define OPERAND_R1 0x01
935 #define OPERAND_R2 0x02
936 #define OPERAND_R3 0x03
937 #define OPERAND_R4 0x04
938 #define OPERAND_SRCA 0x20
939 #define OPERAND_SRCB 0x21
940 #define OPERAND_ACCU 0x31
941 #define OPERAND_ZF 0x32
942 #define OPERAND_CF 0x33
944 #define CS_GPR(n) (0x2600 + (n) * 8)
947 emit_load_alu_reg_u64(struct anv_batch
*batch
, uint32_t reg
,
948 struct anv_bo
*bo
, uint32_t offset
)
950 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
),
951 .RegisterAddress
= reg
,
952 .MemoryAddress
= { bo
, offset
});
953 anv_batch_emit(batch
, GENX(MI_LOAD_REGISTER_MEM
),
954 .RegisterAddress
= reg
+ 4,
955 .MemoryAddress
= { bo
, offset
+ 4 });
958 void genX(CmdCopyQueryPoolResults
)(
959 VkCommandBuffer commandBuffer
,
960 VkQueryPool queryPool
,
964 VkDeviceSize destOffset
,
965 VkDeviceSize destStride
,
966 VkQueryResultFlags flags
)
968 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
969 ANV_FROM_HANDLE(anv_query_pool
, pool
, queryPool
);
970 ANV_FROM_HANDLE(anv_buffer
, buffer
, destBuffer
);
971 uint32_t slot_offset
, dst_offset
;
973 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
974 /* Where is the availabilty info supposed to go? */
975 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
979 assert(pool
->type
== VK_QUERY_TYPE_OCCLUSION
);
981 /* FIXME: If we're not waiting, should we just do this on the CPU? */
982 if (flags
& VK_QUERY_RESULT_WAIT_BIT
)
983 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
),
984 .CommandStreamerStallEnable
= true,
985 .StallAtPixelScoreboard
= true);
987 dst_offset
= buffer
->offset
+ destOffset
;
988 for (uint32_t i
= 0; i
< queryCount
; i
++) {
990 slot_offset
= (startQuery
+ i
) * sizeof(struct anv_query_pool_slot
);
992 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(0), &pool
->bo
, slot_offset
);
993 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(1), &pool
->bo
, slot_offset
+ 8);
995 /* FIXME: We need to clamp the result for 32 bit. */
997 uint32_t *dw
= anv_batch_emitn(&cmd_buffer
->batch
, 5, GENX(MI_MATH
));
998 dw
[1] = alu(OPCODE_LOAD
, OPERAND_SRCA
, OPERAND_R1
);
999 dw
[2] = alu(OPCODE_LOAD
, OPERAND_SRCB
, OPERAND_R0
);
1000 dw
[3] = alu(OPCODE_SUB
, 0, 0);
1001 dw
[4] = alu(OPCODE_STORE
, OPERAND_R2
, OPERAND_ACCU
);
1003 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
),
1004 .RegisterAddress
= CS_GPR(2),
1005 /* FIXME: This is only lower 32 bits */
1006 .MemoryAddress
= { buffer
->bo
, dst_offset
});
1008 if (flags
& VK_QUERY_RESULT_64_BIT
)
1009 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_STORE_REGISTER_MEM
),
1010 .RegisterAddress
= CS_GPR(2) + 4,
1011 /* FIXME: This is only lower 32 bits */
1012 .MemoryAddress
= { buffer
->bo
, dst_offset
+ 4 });
1014 dst_offset
+= destStride
;