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 "genxml/gen_macros.h"
33 #include "genxml/genX_pack.h"
35 #if GEN_GEN == 7 && !GEN_IS_HASWELL
37 gen7_cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer
*cmd_buffer
,
40 static const uint32_t sampler_state_opcodes
[] = {
41 [MESA_SHADER_VERTEX
] = 43,
42 [MESA_SHADER_TESS_CTRL
] = 44, /* HS */
43 [MESA_SHADER_TESS_EVAL
] = 45, /* DS */
44 [MESA_SHADER_GEOMETRY
] = 46,
45 [MESA_SHADER_FRAGMENT
] = 47,
46 [MESA_SHADER_COMPUTE
] = 0,
49 static const uint32_t binding_table_opcodes
[] = {
50 [MESA_SHADER_VERTEX
] = 38,
51 [MESA_SHADER_TESS_CTRL
] = 39,
52 [MESA_SHADER_TESS_EVAL
] = 40,
53 [MESA_SHADER_GEOMETRY
] = 41,
54 [MESA_SHADER_FRAGMENT
] = 42,
55 [MESA_SHADER_COMPUTE
] = 0,
58 anv_foreach_stage(s
, stages
) {
59 if (cmd_buffer
->state
.samplers
[s
].alloc_size
> 0) {
60 anv_batch_emit(&cmd_buffer
->batch
,
61 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS
), ssp
) {
62 ssp
._3DCommandSubOpcode
= sampler_state_opcodes
[s
];
63 ssp
.PointertoVSSamplerState
= cmd_buffer
->state
.samplers
[s
].offset
;
67 /* Always emit binding table pointers if we're asked to, since on SKL
68 * this is what flushes push constants. */
69 anv_batch_emit(&cmd_buffer
->batch
,
70 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS
), btp
) {
71 btp
._3DCommandSubOpcode
= binding_table_opcodes
[s
];
72 btp
.PointertoVSBindingTable
= cmd_buffer
->state
.binding_tables
[s
].offset
;
78 gen7_cmd_buffer_flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
80 VkShaderStageFlags dirty
= cmd_buffer
->state
.descriptors_dirty
&
81 cmd_buffer
->state
.pipeline
->active_stages
;
83 VkResult result
= VK_SUCCESS
;
84 anv_foreach_stage(s
, dirty
) {
85 result
= anv_cmd_buffer_emit_samplers(cmd_buffer
, s
,
86 &cmd_buffer
->state
.samplers
[s
]);
87 if (result
!= VK_SUCCESS
)
89 result
= anv_cmd_buffer_emit_binding_table(cmd_buffer
, s
,
90 &cmd_buffer
->state
.binding_tables
[s
]);
91 if (result
!= VK_SUCCESS
)
95 if (result
!= VK_SUCCESS
) {
96 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
98 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
99 assert(result
== VK_SUCCESS
);
101 /* Re-emit state base addresses so we get the new surface state base
102 * address before we start emitting binding tables etc.
104 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
106 /* Re-emit all active binding tables */
107 dirty
|= cmd_buffer
->state
.pipeline
->active_stages
;
108 anv_foreach_stage(s
, dirty
) {
109 result
= anv_cmd_buffer_emit_samplers(cmd_buffer
, s
,
110 &cmd_buffer
->state
.samplers
[s
]);
111 if (result
!= VK_SUCCESS
)
113 result
= anv_cmd_buffer_emit_binding_table(cmd_buffer
, s
,
114 &cmd_buffer
->state
.binding_tables
[s
]);
115 if (result
!= VK_SUCCESS
)
120 cmd_buffer
->state
.descriptors_dirty
&= ~dirty
;
124 #endif /* GEN_GEN == 7 && !GEN_IS_HASWELL */
126 static inline int64_t
127 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
137 #if GEN_GEN == 7 && !GEN_IS_HASWELL
139 gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer
*cmd_buffer
)
141 uint32_t count
= cmd_buffer
->state
.dynamic
.scissor
.count
;
142 const VkRect2D
*scissors
= cmd_buffer
->state
.dynamic
.scissor
.scissors
;
143 struct anv_state scissor_state
=
144 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, count
* 8, 32);
146 for (uint32_t i
= 0; i
< count
; i
++) {
147 const VkRect2D
*s
= &scissors
[i
];
149 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
150 * ymax < ymin for empty clips. In case clip x, y, width height are all
151 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
152 * what we want. Just special case empty clips and produce a canonical
154 static const struct GEN7_SCISSOR_RECT empty_scissor
= {
155 .ScissorRectangleYMin
= 1,
156 .ScissorRectangleXMin
= 1,
157 .ScissorRectangleYMax
= 0,
158 .ScissorRectangleXMax
= 0
161 const int max
= 0xffff;
162 struct GEN7_SCISSOR_RECT scissor
= {
163 /* Do this math using int64_t so overflow gets clamped correctly. */
164 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
165 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
166 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
167 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
170 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
171 GEN7_SCISSOR_RECT_pack(NULL
, scissor_state
.map
+ i
* 8,
174 GEN7_SCISSOR_RECT_pack(NULL
, scissor_state
.map
+ i
* 8, &scissor
);
178 anv_batch_emit(&cmd_buffer
->batch
,
179 GEN7_3DSTATE_SCISSOR_STATE_POINTERS
, ssp
) {
180 ssp
.ScissorRectPointer
= scissor_state
.offset
;
183 if (!cmd_buffer
->device
->info
.has_llc
)
184 anv_state_clflush(scissor_state
);
188 static const uint32_t vk_to_gen_index_type
[] = {
189 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
190 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
193 static const uint32_t restart_index_for_type
[] = {
194 [VK_INDEX_TYPE_UINT16
] = UINT16_MAX
,
195 [VK_INDEX_TYPE_UINT32
] = UINT32_MAX
,
198 void genX(CmdBindIndexBuffer
)(
199 VkCommandBuffer commandBuffer
,
202 VkIndexType indexType
)
204 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
205 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
207 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_INDEX_BUFFER
;
209 cmd_buffer
->state
.restart_index
= restart_index_for_type
[indexType
];
210 cmd_buffer
->state
.gen7
.index_buffer
= buffer
;
211 cmd_buffer
->state
.gen7
.index_type
= vk_to_gen_index_type
[indexType
];
212 cmd_buffer
->state
.gen7
.index_offset
= offset
;
216 flush_compute_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
)
218 struct anv_device
*device
= cmd_buffer
->device
;
219 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
220 struct anv_state surfaces
= { 0, }, samplers
= { 0, };
223 result
= anv_cmd_buffer_emit_samplers(cmd_buffer
,
224 MESA_SHADER_COMPUTE
, &samplers
);
225 if (result
!= VK_SUCCESS
)
227 result
= anv_cmd_buffer_emit_binding_table(cmd_buffer
,
228 MESA_SHADER_COMPUTE
, &surfaces
);
229 if (result
!= VK_SUCCESS
)
232 struct anv_state push_state
= anv_cmd_buffer_cs_push_constants(cmd_buffer
);
234 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
235 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
237 if (push_state
.alloc_size
) {
238 anv_batch_emit(&cmd_buffer
->batch
, GENX(MEDIA_CURBE_LOAD
), curbe
) {
239 curbe
.CURBETotalDataLength
= push_state
.alloc_size
;
240 curbe
.CURBEDataStartAddress
= push_state
.offset
;
244 assert(prog_data
->total_shared
<= 64 * 1024);
245 uint32_t slm_size
= 0;
246 if (prog_data
->total_shared
> 0) {
247 /* slm_size is in 4k increments, but must be a power of 2. */
249 while (slm_size
< prog_data
->total_shared
)
251 slm_size
/= 4 * 1024;
254 struct anv_state state
=
255 anv_state_pool_emit(&device
->dynamic_state_pool
,
256 GENX(INTERFACE_DESCRIPTOR_DATA
), 64,
257 .KernelStartPointer
= pipeline
->cs_simd
,
258 .BindingTablePointer
= surfaces
.offset
,
259 .SamplerStatePointer
= samplers
.offset
,
260 .ConstantURBEntryReadLength
=
261 cs_prog_data
->push
.per_thread
.regs
,
263 .CrossThreadConstantDataReadLength
=
264 cs_prog_data
->push
.cross_thread
.regs
,
266 .ConstantURBEntryReadOffset
= 0,
268 .BarrierEnable
= cs_prog_data
->uses_barrier
,
269 .SharedLocalMemorySize
= slm_size
,
270 .NumberofThreadsinGPGPUThreadGroup
=
271 cs_prog_data
->threads
);
273 const uint32_t size
= GENX(INTERFACE_DESCRIPTOR_DATA_length
) * sizeof(uint32_t);
274 anv_batch_emit(&cmd_buffer
->batch
,
275 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD
), idl
) {
276 idl
.InterfaceDescriptorTotalLength
= size
;
277 idl
.InterfaceDescriptorDataStartAddress
= state
.offset
;
284 genX(cmd_buffer_flush_compute_state
)(struct anv_cmd_buffer
*cmd_buffer
)
286 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
287 MAYBE_UNUSED VkResult result
;
289 assert(pipeline
->active_stages
== VK_SHADER_STAGE_COMPUTE_BIT
);
291 genX(cmd_buffer_config_l3
)(cmd_buffer
, pipeline
);
293 genX(flush_pipeline_select_gpgpu
)(cmd_buffer
);
295 if (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)
296 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
298 if ((cmd_buffer
->state
.descriptors_dirty
& VK_SHADER_STAGE_COMPUTE_BIT
) ||
299 (cmd_buffer
->state
.compute_dirty
& ANV_CMD_DIRTY_PIPELINE
)) {
300 /* FIXME: figure out descriptors for gen7 */
301 result
= flush_compute_descriptor_set(cmd_buffer
);
302 assert(result
== VK_SUCCESS
);
303 cmd_buffer
->state
.descriptors_dirty
&= ~VK_SHADER_STAGE_COMPUTE_BIT
;
306 cmd_buffer
->state
.compute_dirty
= 0;
308 genX(cmd_buffer_apply_pipe_flushes
)(cmd_buffer
);
312 genX(cmd_buffer_flush_dynamic_state
)(struct anv_cmd_buffer
*cmd_buffer
)
314 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
316 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
317 ANV_CMD_DIRTY_RENDER_TARGETS
|
318 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
|
319 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
)) {
321 const struct anv_image_view
*iview
=
322 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
323 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
324 const bool has_depth
=
325 image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
326 const uint32_t depth_format
= has_depth
?
327 isl_surf_get_depth_format(&cmd_buffer
->device
->isl_dev
,
328 &image
->depth_surface
.isl
) : D16_UNORM
;
330 uint32_t sf_dw
[GENX(3DSTATE_SF_length
)];
331 struct GENX(3DSTATE_SF
) sf
= {
332 GENX(3DSTATE_SF_header
),
333 .DepthBufferSurfaceFormat
= depth_format
,
334 .LineWidth
= cmd_buffer
->state
.dynamic
.line_width
,
335 .GlobalDepthOffsetConstant
= cmd_buffer
->state
.dynamic
.depth_bias
.bias
,
336 .GlobalDepthOffsetScale
= cmd_buffer
->state
.dynamic
.depth_bias
.slope
,
337 .GlobalDepthOffsetClamp
= cmd_buffer
->state
.dynamic
.depth_bias
.clamp
339 GENX(3DSTATE_SF_pack
)(NULL
, sf_dw
, &sf
);
341 anv_batch_emit_merge(&cmd_buffer
->batch
, sf_dw
, pipeline
->gen7
.sf
);
344 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
|
345 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
)) {
346 struct anv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
347 struct anv_state cc_state
=
348 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
349 GENX(COLOR_CALC_STATE_length
) * 4,
351 struct GENX(COLOR_CALC_STATE
) cc
= {
352 .BlendConstantColorRed
= cmd_buffer
->state
.dynamic
.blend_constants
[0],
353 .BlendConstantColorGreen
= cmd_buffer
->state
.dynamic
.blend_constants
[1],
354 .BlendConstantColorBlue
= cmd_buffer
->state
.dynamic
.blend_constants
[2],
355 .BlendConstantColorAlpha
= cmd_buffer
->state
.dynamic
.blend_constants
[3],
356 .StencilReferenceValue
= d
->stencil_reference
.front
& 0xff,
357 .BackfaceStencilReferenceValue
= d
->stencil_reference
.back
& 0xff,
359 GENX(COLOR_CALC_STATE_pack
)(NULL
, cc_state
.map
, &cc
);
360 if (!cmd_buffer
->device
->info
.has_llc
)
361 anv_state_clflush(cc_state
);
363 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), ccp
) {
364 ccp
.ColorCalcStatePointer
= cc_state
.offset
;
368 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
369 ANV_CMD_DIRTY_RENDER_TARGETS
|
370 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
|
371 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
)) {
372 uint32_t depth_stencil_dw
[GENX(DEPTH_STENCIL_STATE_length
)];
373 struct anv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
375 struct GENX(DEPTH_STENCIL_STATE
) depth_stencil
= {
376 .StencilTestMask
= d
->stencil_compare_mask
.front
& 0xff,
377 .StencilWriteMask
= d
->stencil_write_mask
.front
& 0xff,
379 .BackfaceStencilTestMask
= d
->stencil_compare_mask
.back
& 0xff,
380 .BackfaceStencilWriteMask
= d
->stencil_write_mask
.back
& 0xff,
382 GENX(DEPTH_STENCIL_STATE_pack
)(NULL
, depth_stencil_dw
, &depth_stencil
);
384 struct anv_state ds_state
=
385 anv_cmd_buffer_merge_dynamic(cmd_buffer
, depth_stencil_dw
,
386 pipeline
->gen7
.depth_stencil_state
,
387 GENX(DEPTH_STENCIL_STATE_length
), 64);
389 anv_batch_emit(&cmd_buffer
->batch
,
390 GENX(3DSTATE_DEPTH_STENCIL_STATE_POINTERS
), dsp
) {
391 dsp
.PointertoDEPTH_STENCIL_STATE
= ds_state
.offset
;
395 if (cmd_buffer
->state
.gen7
.index_buffer
&&
396 cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
397 ANV_CMD_DIRTY_INDEX_BUFFER
)) {
398 struct anv_buffer
*buffer
= cmd_buffer
->state
.gen7
.index_buffer
;
399 uint32_t offset
= cmd_buffer
->state
.gen7
.index_offset
;
402 anv_batch_emit(&cmd_buffer
->batch
, GEN75_3DSTATE_VF
, vf
) {
403 vf
.IndexedDrawCutIndexEnable
= pipeline
->primitive_restart
;
404 vf
.CutIndex
= cmd_buffer
->state
.restart_index
;
408 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_INDEX_BUFFER
), ib
) {
410 ib
.CutIndexEnable
= pipeline
->primitive_restart
;
412 ib
.IndexFormat
= cmd_buffer
->state
.gen7
.index_type
;
413 ib
.MemoryObjectControlState
= GENX(MOCS
);
415 ib
.BufferStartingAddress
=
416 (struct anv_address
) { buffer
->bo
, buffer
->offset
+ offset
};
417 ib
.BufferEndingAddress
=
418 (struct anv_address
) { buffer
->bo
, buffer
->offset
+ buffer
->size
};
422 cmd_buffer
->state
.dirty
= 0;
425 void genX(CmdSetEvent
)(
426 VkCommandBuffer commandBuffer
,
428 VkPipelineStageFlags stageMask
)
433 void genX(CmdResetEvent
)(
434 VkCommandBuffer commandBuffer
,
436 VkPipelineStageFlags stageMask
)
441 void genX(CmdWaitEvents
)(
442 VkCommandBuffer commandBuffer
,
444 const VkEvent
* pEvents
,
445 VkPipelineStageFlags srcStageMask
,
446 VkPipelineStageFlags destStageMask
,
447 uint32_t memoryBarrierCount
,
448 const VkMemoryBarrier
* pMemoryBarriers
,
449 uint32_t bufferMemoryBarrierCount
,
450 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
451 uint32_t imageMemoryBarrierCount
,
452 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
456 genX(CmdPipelineBarrier
)(commandBuffer
, srcStageMask
, destStageMask
,
457 false, /* byRegion */
458 memoryBarrierCount
, pMemoryBarriers
,
459 bufferMemoryBarrierCount
, pBufferMemoryBarriers
,
460 imageMemoryBarrierCount
, pImageMemoryBarriers
);