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"
34 #include "common/gen_guardband.h"
38 gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer
*cmd_buffer
)
40 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
41 uint32_t count
= cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
;
42 const VkViewport
*viewports
=
43 cmd_buffer
->state
.gfx
.dynamic
.viewport
.viewports
;
44 struct anv_state sf_clip_state
=
45 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, count
* 64, 64);
47 for (uint32_t i
= 0; i
< count
; i
++) {
48 const VkViewport
*vp
= &viewports
[i
];
50 /* The gen7 state struct has just the matrix and guardband fields, the
51 * gen8 struct adds the min/max viewport fields. */
52 struct GENX(SF_CLIP_VIEWPORT
) sfv
= {
53 .ViewportMatrixElementm00
= vp
->width
/ 2,
54 .ViewportMatrixElementm11
= vp
->height
/ 2,
55 .ViewportMatrixElementm22
= vp
->maxDepth
- vp
->minDepth
,
56 .ViewportMatrixElementm30
= vp
->x
+ vp
->width
/ 2,
57 .ViewportMatrixElementm31
= vp
->y
+ vp
->height
/ 2,
58 .ViewportMatrixElementm32
= vp
->minDepth
,
59 .XMinClipGuardband
= -1.0f
,
60 .XMaxClipGuardband
= 1.0f
,
61 .YMinClipGuardband
= -1.0f
,
62 .YMaxClipGuardband
= 1.0f
,
63 .XMinViewPort
= vp
->x
,
64 .XMaxViewPort
= vp
->x
+ vp
->width
- 1,
65 .YMinViewPort
= MIN2(vp
->y
, vp
->y
+ vp
->height
),
66 .YMaxViewPort
= MAX2(vp
->y
, vp
->y
+ vp
->height
) - 1,
70 /* We can only calculate a "real" guardband clip if we know the
71 * framebuffer at the time we emit the packet. Otherwise, we have
72 * fall back to a worst-case guardband of [-1, 1].
74 gen_calculate_guardband_size(fb
->width
, fb
->height
,
75 sfv
.ViewportMatrixElementm00
,
76 sfv
.ViewportMatrixElementm11
,
77 sfv
.ViewportMatrixElementm30
,
78 sfv
.ViewportMatrixElementm31
,
79 &sfv
.XMinClipGuardband
,
80 &sfv
.XMaxClipGuardband
,
81 &sfv
.YMinClipGuardband
,
82 &sfv
.YMaxClipGuardband
);
85 GENX(SF_CLIP_VIEWPORT_pack
)(NULL
, sf_clip_state
.map
+ i
* 64, &sfv
);
88 anv_batch_emit(&cmd_buffer
->batch
,
89 GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
), clip
) {
90 clip
.SFClipViewportPointer
= sf_clip_state
.offset
;
95 gen8_cmd_buffer_emit_depth_viewport(struct anv_cmd_buffer
*cmd_buffer
,
96 bool depth_clamp_enable
)
98 uint32_t count
= cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
;
99 const VkViewport
*viewports
=
100 cmd_buffer
->state
.gfx
.dynamic
.viewport
.viewports
;
101 struct anv_state cc_state
=
102 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, count
* 8, 32);
104 for (uint32_t i
= 0; i
< count
; i
++) {
105 const VkViewport
*vp
= &viewports
[i
];
107 /* From the Vulkan spec:
109 * "It is valid for minDepth to be greater than or equal to
112 float min_depth
= MIN2(vp
->minDepth
, vp
->maxDepth
);
113 float max_depth
= MAX2(vp
->minDepth
, vp
->maxDepth
);
115 struct GENX(CC_VIEWPORT
) cc_viewport
= {
116 .MinimumDepth
= depth_clamp_enable
? min_depth
: 0.0f
,
117 .MaximumDepth
= depth_clamp_enable
? max_depth
: 1.0f
,
120 GENX(CC_VIEWPORT_pack
)(NULL
, cc_state
.map
+ i
* 8, &cc_viewport
);
123 anv_batch_emit(&cmd_buffer
->batch
,
124 GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC
), cc
) {
125 cc
.CCViewportPointer
= cc_state
.offset
;
131 genX(cmd_buffer_enable_pma_fix
)(struct anv_cmd_buffer
*cmd_buffer
, bool enable
)
133 if (cmd_buffer
->state
.pma_fix_enabled
== enable
)
136 cmd_buffer
->state
.pma_fix_enabled
= enable
;
138 /* According to the Broadwell PIPE_CONTROL documentation, software should
139 * emit a PIPE_CONTROL with the CS Stall and Depth Cache Flush bits set
140 * prior to the LRI. If stencil buffer writes are enabled, then a Render
141 * Cache Flush is also necessary.
143 * The Skylake docs say to use a depth stall rather than a command
144 * streamer stall. However, the hardware seems to violently disagree.
145 * A full command streamer stall seems to be needed in both cases.
147 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
148 pc
.DepthCacheFlushEnable
= true;
149 pc
.CommandStreamerStallEnable
= true;
150 pc
.RenderTargetCacheFlushEnable
= true;
152 pc
.TileCacheFlushEnable
= true;
154 /* GEN:BUG:1409600907: "PIPE_CONTROL with Depth Stall Enable bit must
155 * be set with any PIPE_CONTROL with Depth Flush Enable bit set.
157 pc
.DepthStallEnable
= true;
164 anv_pack_struct(&cache_mode
, GENX(CACHE_MODE_0
),
165 .STCPMAOptimizationEnable
= enable
,
166 .STCPMAOptimizationEnableMask
= true);
167 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
168 lri
.RegisterOffset
= GENX(CACHE_MODE_0_num
);
169 lri
.DataDWord
= cache_mode
;
175 anv_pack_struct(&cache_mode
, GENX(CACHE_MODE_1
),
176 .NPPMAFixEnable
= enable
,
177 .NPEarlyZFailsDisable
= enable
,
178 .NPPMAFixEnableMask
= true,
179 .NPEarlyZFailsDisableMask
= true);
180 anv_batch_emit(&cmd_buffer
->batch
, GENX(MI_LOAD_REGISTER_IMM
), lri
) {
181 lri
.RegisterOffset
= GENX(CACHE_MODE_1_num
);
182 lri
.DataDWord
= cache_mode
;
185 #endif /* GEN_GEN == 8 */
187 /* After the LRI, a PIPE_CONTROL with both the Depth Stall and Depth Cache
188 * Flush bits is often necessary. We do it regardless because it's easier.
189 * The render cache flush is also necessary if stencil writes are enabled.
191 * Again, the Skylake docs give a different set of flushes but the BDW
192 * flushes seem to work just as well.
194 anv_batch_emit(&cmd_buffer
->batch
, GENX(PIPE_CONTROL
), pc
) {
195 pc
.DepthStallEnable
= true;
196 pc
.DepthCacheFlushEnable
= true;
197 pc
.RenderTargetCacheFlushEnable
= true;
199 pc
.TileCacheFlushEnable
= true;
205 want_depth_pma_fix(struct anv_cmd_buffer
*cmd_buffer
)
207 assert(GEN_GEN
== 8);
209 /* From the Broadwell PRM Vol. 2c CACHE_MODE_1::NP_PMA_FIX_ENABLE:
211 * SW must set this bit in order to enable this fix when following
212 * expression is TRUE.
214 * 3DSTATE_WM::ForceThreadDispatch != 1 &&
215 * !(3DSTATE_RASTER::ForceSampleCount != NUMRASTSAMPLES_0) &&
216 * (3DSTATE_DEPTH_BUFFER::SURFACE_TYPE != NULL) &&
217 * (3DSTATE_DEPTH_BUFFER::HIZ Enable) &&
218 * !(3DSTATE_WM::EDSC_Mode == EDSC_PREPS) &&
219 * (3DSTATE_PS_EXTRA::PixelShaderValid) &&
220 * !(3DSTATE_WM_HZ_OP::DepthBufferClear ||
221 * 3DSTATE_WM_HZ_OP::DepthBufferResolve ||
222 * 3DSTATE_WM_HZ_OP::Hierarchical Depth Buffer Resolve Enable ||
223 * 3DSTATE_WM_HZ_OP::StencilBufferClear) &&
224 * (3DSTATE_WM_DEPTH_STENCIL::DepthTestEnable) &&
225 * (((3DSTATE_PS_EXTRA::PixelShaderKillsPixels ||
226 * 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||
227 * 3DSTATE_PS_BLEND::AlphaToCoverageEnable ||
228 * 3DSTATE_PS_BLEND::AlphaTestEnable ||
229 * 3DSTATE_WM_CHROMAKEY::ChromaKeyKillEnable) &&
230 * 3DSTATE_WM::ForceKillPix != ForceOff &&
231 * ((3DSTATE_WM_DEPTH_STENCIL::DepthWriteEnable &&
232 * 3DSTATE_DEPTH_BUFFER::DEPTH_WRITE_ENABLE) ||
233 * (3DSTATE_WM_DEPTH_STENCIL::Stencil Buffer Write Enable &&
234 * 3DSTATE_DEPTH_BUFFER::STENCIL_WRITE_ENABLE &&
235 * 3DSTATE_STENCIL_BUFFER::STENCIL_BUFFER_ENABLE))) ||
236 * (3DSTATE_PS_EXTRA:: Pixel Shader Computed Depth mode != PSCDEPTH_OFF))
239 /* These are always true:
240 * 3DSTATE_WM::ForceThreadDispatch != 1 &&
241 * !(3DSTATE_RASTER::ForceSampleCount != NUMRASTSAMPLES_0)
244 /* We only enable the PMA fix if we know for certain that HiZ is enabled.
245 * If we don't know whether HiZ is enabled or not, we disable the PMA fix
246 * and there is no harm.
248 * (3DSTATE_DEPTH_BUFFER::SURFACE_TYPE != NULL) &&
249 * 3DSTATE_DEPTH_BUFFER::HIZ Enable
251 if (!cmd_buffer
->state
.hiz_enabled
)
254 /* 3DSTATE_PS_EXTRA::PixelShaderValid */
255 struct anv_graphics_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.pipeline
;
256 if (!anv_pipeline_has_stage(pipeline
, MESA_SHADER_FRAGMENT
))
259 /* !(3DSTATE_WM::EDSC_Mode == EDSC_PREPS) */
260 const struct brw_wm_prog_data
*wm_prog_data
= get_wm_prog_data(pipeline
);
261 if (wm_prog_data
->early_fragment_tests
)
264 /* We never use anv_pipeline for HiZ ops so this is trivially true:
265 * !(3DSTATE_WM_HZ_OP::DepthBufferClear ||
266 * 3DSTATE_WM_HZ_OP::DepthBufferResolve ||
267 * 3DSTATE_WM_HZ_OP::Hierarchical Depth Buffer Resolve Enable ||
268 * 3DSTATE_WM_HZ_OP::StencilBufferClear)
271 /* 3DSTATE_WM_DEPTH_STENCIL::DepthTestEnable */
272 if (!pipeline
->depth_test_enable
)
275 /* (((3DSTATE_PS_EXTRA::PixelShaderKillsPixels ||
276 * 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||
277 * 3DSTATE_PS_BLEND::AlphaToCoverageEnable ||
278 * 3DSTATE_PS_BLEND::AlphaTestEnable ||
279 * 3DSTATE_WM_CHROMAKEY::ChromaKeyKillEnable) &&
280 * 3DSTATE_WM::ForceKillPix != ForceOff &&
281 * ((3DSTATE_WM_DEPTH_STENCIL::DepthWriteEnable &&
282 * 3DSTATE_DEPTH_BUFFER::DEPTH_WRITE_ENABLE) ||
283 * (3DSTATE_WM_DEPTH_STENCIL::Stencil Buffer Write Enable &&
284 * 3DSTATE_DEPTH_BUFFER::STENCIL_WRITE_ENABLE &&
285 * 3DSTATE_STENCIL_BUFFER::STENCIL_BUFFER_ENABLE))) ||
286 * (3DSTATE_PS_EXTRA:: Pixel Shader Computed Depth mode != PSCDEPTH_OFF))
288 return (pipeline
->kill_pixel
&& (pipeline
->writes_depth
||
289 pipeline
->writes_stencil
)) ||
290 wm_prog_data
->computed_depth_mode
!= PSCDEPTH_OFF
;
294 want_stencil_pma_fix(struct anv_cmd_buffer
*cmd_buffer
)
298 assert(GEN_GEN
== 9);
300 /* From the Skylake PRM Vol. 2c CACHE_MODE_1::STC PMA Optimization Enable:
302 * Clearing this bit will force the STC cache to wait for pending
303 * retirement of pixels at the HZ-read stage and do the STC-test for
304 * Non-promoted, R-computed and Computed depth modes instead of
305 * postponing the STC-test to RCPFE.
307 * STC_TEST_EN = 3DSTATE_STENCIL_BUFFER::STENCIL_BUFFER_ENABLE &&
308 * 3DSTATE_WM_DEPTH_STENCIL::StencilTestEnable
310 * STC_WRITE_EN = 3DSTATE_STENCIL_BUFFER::STENCIL_BUFFER_ENABLE &&
311 * (3DSTATE_WM_DEPTH_STENCIL::Stencil Buffer Write Enable &&
312 * 3DSTATE_DEPTH_BUFFER::STENCIL_WRITE_ENABLE)
314 * COMP_STC_EN = STC_TEST_EN &&
315 * 3DSTATE_PS_EXTRA::PixelShaderComputesStencil
317 * SW parses the pipeline states to generate the following logical
318 * signal indicating if PMA FIX can be enabled.
321 * 3DSTATE_WM::ForceThreadDispatch != 1 &&
322 * !(3DSTATE_RASTER::ForceSampleCount != NUMRASTSAMPLES_0) &&
323 * 3DSTATE_DEPTH_BUFFER::SURFACE_TYPE != NULL &&
324 * 3DSTATE_DEPTH_BUFFER::HIZ Enable &&
325 * !(3DSTATE_WM::EDSC_Mode == 2) &&
326 * 3DSTATE_PS_EXTRA::PixelShaderValid &&
327 * !(3DSTATE_WM_HZ_OP::DepthBufferClear ||
328 * 3DSTATE_WM_HZ_OP::DepthBufferResolve ||
329 * 3DSTATE_WM_HZ_OP::Hierarchical Depth Buffer Resolve Enable ||
330 * 3DSTATE_WM_HZ_OP::StencilBufferClear) &&
331 * (COMP_STC_EN || STC_WRITE_EN) &&
332 * ((3DSTATE_PS_EXTRA::PixelShaderKillsPixels ||
333 * 3DSTATE_WM::ForceKillPix == ON ||
334 * 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||
335 * 3DSTATE_PS_BLEND::AlphaToCoverageEnable ||
336 * 3DSTATE_PS_BLEND::AlphaTestEnable ||
337 * 3DSTATE_WM_CHROMAKEY::ChromaKeyKillEnable) ||
338 * (3DSTATE_PS_EXTRA::Pixel Shader Computed Depth mode != PSCDEPTH_OFF))
341 /* These are always true:
342 * 3DSTATE_WM::ForceThreadDispatch != 1 &&
343 * !(3DSTATE_RASTER::ForceSampleCount != NUMRASTSAMPLES_0)
346 /* We only enable the PMA fix if we know for certain that HiZ is enabled.
347 * If we don't know whether HiZ is enabled or not, we disable the PMA fix
348 * and there is no harm.
350 * (3DSTATE_DEPTH_BUFFER::SURFACE_TYPE != NULL) &&
351 * 3DSTATE_DEPTH_BUFFER::HIZ Enable
353 if (!cmd_buffer
->state
.hiz_enabled
)
356 /* We can't possibly know if HiZ is enabled without the framebuffer */
357 assert(cmd_buffer
->state
.framebuffer
);
359 /* HiZ is enabled so we had better have a depth buffer with HiZ */
360 const struct anv_image_view
*ds_iview
=
361 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
362 assert(ds_iview
&& ds_iview
->image
->planes
[0].aux_usage
== ISL_AUX_USAGE_HIZ
);
364 /* 3DSTATE_PS_EXTRA::PixelShaderValid */
365 struct anv_graphics_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.pipeline
;
366 if (!anv_pipeline_has_stage(pipeline
, MESA_SHADER_FRAGMENT
))
369 /* !(3DSTATE_WM::EDSC_Mode == 2) */
370 const struct brw_wm_prog_data
*wm_prog_data
= get_wm_prog_data(pipeline
);
371 if (wm_prog_data
->early_fragment_tests
)
374 /* We never use anv_pipeline for HiZ ops so this is trivially true:
375 * !(3DSTATE_WM_HZ_OP::DepthBufferClear ||
376 * 3DSTATE_WM_HZ_OP::DepthBufferResolve ||
377 * 3DSTATE_WM_HZ_OP::Hierarchical Depth Buffer Resolve Enable ||
378 * 3DSTATE_WM_HZ_OP::StencilBufferClear)
381 /* 3DSTATE_STENCIL_BUFFER::STENCIL_BUFFER_ENABLE &&
382 * 3DSTATE_WM_DEPTH_STENCIL::StencilTestEnable
384 const bool stc_test_en
=
385 (ds_iview
->image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
386 pipeline
->stencil_test_enable
;
388 /* 3DSTATE_STENCIL_BUFFER::STENCIL_BUFFER_ENABLE &&
389 * (3DSTATE_WM_DEPTH_STENCIL::Stencil Buffer Write Enable &&
390 * 3DSTATE_DEPTH_BUFFER::STENCIL_WRITE_ENABLE)
392 const bool stc_write_en
=
393 (ds_iview
->image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
394 (cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.front
||
395 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.back
) &&
396 pipeline
->writes_stencil
;
398 /* STC_TEST_EN && 3DSTATE_PS_EXTRA::PixelShaderComputesStencil */
399 const bool comp_stc_en
= stc_test_en
&& wm_prog_data
->computed_stencil
;
401 /* COMP_STC_EN || STC_WRITE_EN */
402 if (!(comp_stc_en
|| stc_write_en
))
405 /* (3DSTATE_PS_EXTRA::PixelShaderKillsPixels ||
406 * 3DSTATE_WM::ForceKillPix == ON ||
407 * 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||
408 * 3DSTATE_PS_BLEND::AlphaToCoverageEnable ||
409 * 3DSTATE_PS_BLEND::AlphaTestEnable ||
410 * 3DSTATE_WM_CHROMAKEY::ChromaKeyKillEnable) ||
411 * (3DSTATE_PS_EXTRA::Pixel Shader Computed Depth mode != PSCDEPTH_OFF)
413 return pipeline
->kill_pixel
||
414 wm_prog_data
->computed_depth_mode
!= PSCDEPTH_OFF
;
418 genX(cmd_buffer_flush_dynamic_state
)(struct anv_cmd_buffer
*cmd_buffer
)
420 struct anv_graphics_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.pipeline
;
421 struct anv_dynamic_state
*d
= &cmd_buffer
->state
.gfx
.dynamic
;
423 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
424 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
)) {
425 uint32_t sf_dw
[GENX(3DSTATE_SF_length
)];
426 struct GENX(3DSTATE_SF
) sf
= {
427 GENX(3DSTATE_SF_header
),
430 if (cmd_buffer
->device
->info
.is_cherryview
) {
431 sf
.CHVLineWidth
= d
->line_width
;
433 sf
.LineWidth
= d
->line_width
;
436 sf
.LineWidth
= d
->line_width
,
438 GENX(3DSTATE_SF_pack
)(NULL
, sf_dw
, &sf
);
439 anv_batch_emit_merge(&cmd_buffer
->batch
, sf_dw
, pipeline
->gen8
.sf
);
442 static const uint32_t vk_to_gen_cullmode
[] = {
443 [VK_CULL_MODE_NONE
] = CULLMODE_NONE
,
444 [VK_CULL_MODE_FRONT_BIT
] = CULLMODE_FRONT
,
445 [VK_CULL_MODE_BACK_BIT
] = CULLMODE_BACK
,
446 [VK_CULL_MODE_FRONT_AND_BACK
] = CULLMODE_BOTH
448 static const uint32_t vk_to_gen_front_face
[] = {
449 [VK_FRONT_FACE_COUNTER_CLOCKWISE
] = 1,
450 [VK_FRONT_FACE_CLOCKWISE
] = 0
452 static const uint32_t vk_to_gen_primitive_type
[] = {
453 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST
] = _3DPRIM_POINTLIST
,
454 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST
] = _3DPRIM_LINELIST
,
455 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
] = _3DPRIM_LINESTRIP
,
456 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
] = _3DPRIM_TRILIST
,
457 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
] = _3DPRIM_TRISTRIP
,
458 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
] = _3DPRIM_TRIFAN
,
459 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
] = _3DPRIM_LINELIST_ADJ
,
460 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
] = _3DPRIM_LINESTRIP_ADJ
,
461 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
] = _3DPRIM_TRILIST_ADJ
,
462 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
] = _3DPRIM_TRISTRIP_ADJ
,
465 static const uint32_t vk_to_gen_compare_op
[] = {
466 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
467 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
468 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
469 [VK_COMPARE_OP_LESS_OR_EQUAL
] = PREFILTEROPLEQUAL
,
470 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
471 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
472 [VK_COMPARE_OP_GREATER_OR_EQUAL
] = PREFILTEROPGEQUAL
,
473 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
476 static const uint32_t vk_to_gen_stencil_op
[] = {
477 [VK_STENCIL_OP_KEEP
] = STENCILOP_KEEP
,
478 [VK_STENCIL_OP_ZERO
] = STENCILOP_ZERO
,
479 [VK_STENCIL_OP_REPLACE
] = STENCILOP_REPLACE
,
480 [VK_STENCIL_OP_INCREMENT_AND_CLAMP
] = STENCILOP_INCRSAT
,
481 [VK_STENCIL_OP_DECREMENT_AND_CLAMP
] = STENCILOP_DECRSAT
,
482 [VK_STENCIL_OP_INVERT
] = STENCILOP_INVERT
,
483 [VK_STENCIL_OP_INCREMENT_AND_WRAP
] = STENCILOP_INCR
,
484 [VK_STENCIL_OP_DECREMENT_AND_WRAP
] = STENCILOP_DECR
,
487 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
488 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
|
489 ANV_CMD_DIRTY_DYNAMIC_CULL_MODE
|
490 ANV_CMD_DIRTY_DYNAMIC_FRONT_FACE
)) {
491 uint32_t raster_dw
[GENX(3DSTATE_RASTER_length
)];
492 struct GENX(3DSTATE_RASTER
) raster
= {
493 GENX(3DSTATE_RASTER_header
),
494 .GlobalDepthOffsetConstant
= d
->depth_bias
.bias
,
495 .GlobalDepthOffsetScale
= d
->depth_bias
.slope
,
496 .GlobalDepthOffsetClamp
= d
->depth_bias
.clamp
,
497 .CullMode
= vk_to_gen_cullmode
[d
->cull_mode
],
498 .FrontWinding
= vk_to_gen_front_face
[d
->front_face
],
500 GENX(3DSTATE_RASTER_pack
)(NULL
, raster_dw
, &raster
);
501 anv_batch_emit_merge(&cmd_buffer
->batch
, raster_dw
,
502 pipeline
->gen8
.raster
);
505 /* Stencil reference values moved from COLOR_CALC_STATE in gen8 to
506 * 3DSTATE_WM_DEPTH_STENCIL in gen9. That means the dirty bits gets split
507 * across different state packets for gen8 and gen9. We handle that by
508 * using a big old #if switch here.
511 if (cmd_buffer
->state
.gfx
.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 GENX(COLOR_CALC_STATE_length
) * 4,
517 struct GENX(COLOR_CALC_STATE
) cc
= {
518 .BlendConstantColorRed
= d
->blend_constants
[0],
519 .BlendConstantColorGreen
= d
->blend_constants
[1],
520 .BlendConstantColorBlue
= d
->blend_constants
[2],
521 .BlendConstantColorAlpha
= d
->blend_constants
[3],
522 .StencilReferenceValue
= d
->stencil_reference
.front
& 0xff,
523 .BackfaceStencilReferenceValue
= d
->stencil_reference
.back
& 0xff,
525 GENX(COLOR_CALC_STATE_pack
)(NULL
, cc_state
.map
, &cc
);
527 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), ccp
) {
528 ccp
.ColorCalcStatePointer
= cc_state
.offset
;
529 ccp
.ColorCalcStatePointerValid
= true;
533 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
534 ANV_CMD_DIRTY_RENDER_TARGETS
|
535 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
|
536 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
|
537 ANV_CMD_DIRTY_DYNAMIC_DEPTH_TEST_ENABLE
|
538 ANV_CMD_DIRTY_DYNAMIC_DEPTH_WRITE_ENABLE
|
539 ANV_CMD_DIRTY_DYNAMIC_DEPTH_COMPARE_OP
|
540 ANV_CMD_DIRTY_DYNAMIC_STENCIL_TEST_ENABLE
|
541 ANV_CMD_DIRTY_DYNAMIC_STENCIL_OP
)) {
542 uint32_t wm_depth_stencil_dw
[GENX(3DSTATE_WM_DEPTH_STENCIL_length
)];
544 struct GENX(3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
) = {
545 GENX(3DSTATE_WM_DEPTH_STENCIL_header
),
547 .StencilTestMask
= d
->stencil_compare_mask
.front
& 0xff,
548 .StencilWriteMask
= d
->stencil_write_mask
.front
& 0xff,
550 .BackfaceStencilTestMask
= d
->stencil_compare_mask
.back
& 0xff,
551 .BackfaceStencilWriteMask
= d
->stencil_write_mask
.back
& 0xff,
553 .StencilBufferWriteEnable
=
554 (d
->stencil_write_mask
.front
|| d
->stencil_write_mask
.back
) &&
555 d
->stencil_test_enable
,
557 .DepthTestEnable
= d
->depth_test_enable
,
558 .DepthBufferWriteEnable
= d
->depth_test_enable
&& d
->depth_write_enable
,
559 .DepthTestFunction
= vk_to_gen_compare_op
[d
->depth_compare_op
],
560 .StencilTestEnable
= d
->stencil_test_enable
,
561 .StencilFailOp
= vk_to_gen_stencil_op
[d
->stencil_op
.front
.fail_op
],
562 .StencilPassDepthPassOp
= vk_to_gen_stencil_op
[d
->stencil_op
.front
.pass_op
],
563 .StencilPassDepthFailOp
= vk_to_gen_stencil_op
[d
->stencil_op
.front
.depth_fail_op
],
564 .StencilTestFunction
= vk_to_gen_compare_op
[d
->stencil_op
.front
.compare_op
],
565 .BackfaceStencilFailOp
= vk_to_gen_stencil_op
[d
->stencil_op
.back
.fail_op
],
566 .BackfaceStencilPassDepthPassOp
= vk_to_gen_stencil_op
[d
->stencil_op
.back
.pass_op
],
567 .BackfaceStencilPassDepthFailOp
= vk_to_gen_stencil_op
[d
->stencil_op
.back
.depth_fail_op
],
568 .BackfaceStencilTestFunction
= vk_to_gen_compare_op
[d
->stencil_op
.back
.compare_op
],
570 GENX(3DSTATE_WM_DEPTH_STENCIL_pack
)(NULL
, wm_depth_stencil_dw
,
573 anv_batch_emit_merge(&cmd_buffer
->batch
, wm_depth_stencil_dw
,
574 pipeline
->gen8
.wm_depth_stencil
);
576 genX(cmd_buffer_enable_pma_fix
)(cmd_buffer
,
577 want_depth_pma_fix(cmd_buffer
));
580 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
) {
581 struct anv_state cc_state
=
582 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
583 GENX(COLOR_CALC_STATE_length
) * 4,
585 struct GENX(COLOR_CALC_STATE
) cc
= {
586 .BlendConstantColorRed
= d
->blend_constants
[0],
587 .BlendConstantColorGreen
= d
->blend_constants
[1],
588 .BlendConstantColorBlue
= d
->blend_constants
[2],
589 .BlendConstantColorAlpha
= d
->blend_constants
[3],
591 GENX(COLOR_CALC_STATE_pack
)(NULL
, cc_state
.map
, &cc
);
593 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), ccp
) {
594 ccp
.ColorCalcStatePointer
= cc_state
.offset
;
595 ccp
.ColorCalcStatePointerValid
= true;
599 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
600 ANV_CMD_DIRTY_RENDER_TARGETS
|
601 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
|
602 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
|
603 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
|
604 ANV_CMD_DIRTY_DYNAMIC_DEPTH_TEST_ENABLE
|
605 ANV_CMD_DIRTY_DYNAMIC_DEPTH_WRITE_ENABLE
|
606 ANV_CMD_DIRTY_DYNAMIC_DEPTH_COMPARE_OP
|
607 ANV_CMD_DIRTY_DYNAMIC_STENCIL_TEST_ENABLE
|
608 ANV_CMD_DIRTY_DYNAMIC_STENCIL_OP
)) {
609 uint32_t dwords
[GENX(3DSTATE_WM_DEPTH_STENCIL_length
)];
610 struct GENX(3DSTATE_WM_DEPTH_STENCIL
) wm_depth_stencil
= {
611 GENX(3DSTATE_WM_DEPTH_STENCIL_header
),
613 .StencilTestMask
= d
->stencil_compare_mask
.front
& 0xff,
614 .StencilWriteMask
= d
->stencil_write_mask
.front
& 0xff,
616 .BackfaceStencilTestMask
= d
->stencil_compare_mask
.back
& 0xff,
617 .BackfaceStencilWriteMask
= d
->stencil_write_mask
.back
& 0xff,
619 .StencilReferenceValue
= d
->stencil_reference
.front
& 0xff,
620 .BackfaceStencilReferenceValue
= d
->stencil_reference
.back
& 0xff,
622 .StencilBufferWriteEnable
=
623 (d
->stencil_write_mask
.front
|| d
->stencil_write_mask
.back
) &&
624 d
->stencil_test_enable
,
626 .DepthTestEnable
= d
->depth_test_enable
,
627 .DepthBufferWriteEnable
= d
->depth_test_enable
&& d
->depth_write_enable
,
628 .DepthTestFunction
= vk_to_gen_compare_op
[d
->depth_compare_op
],
629 .StencilTestEnable
= d
->stencil_test_enable
,
630 .StencilFailOp
= vk_to_gen_stencil_op
[d
->stencil_op
.front
.fail_op
],
631 .StencilPassDepthPassOp
= vk_to_gen_stencil_op
[d
->stencil_op
.front
.pass_op
],
632 .StencilPassDepthFailOp
= vk_to_gen_stencil_op
[d
->stencil_op
.front
.depth_fail_op
],
633 .StencilTestFunction
= vk_to_gen_compare_op
[d
->stencil_op
.front
.compare_op
],
634 .BackfaceStencilFailOp
= vk_to_gen_stencil_op
[d
->stencil_op
.back
.fail_op
],
635 .BackfaceStencilPassDepthPassOp
= vk_to_gen_stencil_op
[d
->stencil_op
.back
.pass_op
],
636 .BackfaceStencilPassDepthFailOp
=vk_to_gen_stencil_op
[d
->stencil_op
.back
.depth_fail_op
],
637 .BackfaceStencilTestFunction
= vk_to_gen_compare_op
[d
->stencil_op
.back
.compare_op
],
640 GENX(3DSTATE_WM_DEPTH_STENCIL_pack
)(NULL
, dwords
, &wm_depth_stencil
);
642 anv_batch_emit_merge(&cmd_buffer
->batch
, dwords
,
643 pipeline
->gen9
.wm_depth_stencil
);
645 genX(cmd_buffer_enable_pma_fix
)(cmd_buffer
,
646 want_stencil_pma_fix(cmd_buffer
));
651 if(cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
652 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
|
653 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE
)) {
654 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_DEPTH_BOUNDS
), db
) {
655 db
.DepthBoundsTestValueModifyDisable
= false;
656 db
.DepthBoundsTestEnableModifyDisable
= false;
657 db
.DepthBoundsTestEnable
= d
->depth_bounds_test_enable
;
658 db
.DepthBoundsTestMinValue
= d
->depth_bounds
.min
;
659 db
.DepthBoundsTestMaxValue
= d
->depth_bounds
.max
;
664 if (cmd_buffer
->state
.gfx
.dirty
& ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE
) {
665 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_LINE_STIPPLE
), ls
) {
666 ls
.LineStipplePattern
= d
->line_stipple
.pattern
;
667 ls
.LineStippleInverseRepeatCount
=
668 1.0f
/ MAX2(1, d
->line_stipple
.factor
);
669 ls
.LineStippleRepeatCount
= d
->line_stipple
.factor
;
673 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
674 ANV_CMD_DIRTY_INDEX_BUFFER
)) {
675 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_VF
), vf
) {
676 vf
.IndexedDrawCutIndexEnable
= pipeline
->primitive_restart
;
677 vf
.CutIndex
= cmd_buffer
->state
.restart_index
;
681 if (cmd_buffer
->state
.gfx
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
682 ANV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY
)) {
684 if (anv_pipeline_has_stage(pipeline
, MESA_SHADER_TESS_EVAL
))
685 topology
= d
->primitive_topology
;
687 topology
= vk_to_gen_primitive_type
[d
->primitive_topology
];
689 cmd_buffer
->state
.gfx
.primitive_topology
= topology
;
691 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_VF_TOPOLOGY
), vft
) {
692 vft
.PrimitiveTopologyType
= topology
;
696 cmd_buffer
->state
.gfx
.dirty
= 0;
699 static uint32_t vk_to_gen_index_type(VkIndexType type
)
702 case VK_INDEX_TYPE_UINT8_EXT
:
704 case VK_INDEX_TYPE_UINT16
:
706 case VK_INDEX_TYPE_UINT32
:
709 unreachable("invalid index type");
713 static uint32_t restart_index_for_type(VkIndexType type
)
716 case VK_INDEX_TYPE_UINT8_EXT
:
718 case VK_INDEX_TYPE_UINT16
:
720 case VK_INDEX_TYPE_UINT32
:
723 unreachable("invalid index type");
727 void genX(CmdBindIndexBuffer
)(
728 VkCommandBuffer commandBuffer
,
731 VkIndexType indexType
)
733 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
734 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
736 cmd_buffer
->state
.restart_index
= restart_index_for_type(indexType
);
738 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_INDEX_BUFFER
), ib
) {
739 ib
.IndexFormat
= vk_to_gen_index_type(indexType
);
740 ib
.MOCS
= anv_mocs_for_bo(cmd_buffer
->device
,
742 ib
.BufferStartingAddress
= anv_address_add(buffer
->address
, offset
);
743 ib
.BufferSize
= buffer
->size
- offset
;
746 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_INDEX_BUFFER
;