<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Feb 2, 2017 at 11:18 PM, Ben Widawsky <span dir="ltr"><<a href="mailto:ben@bwidawsk.net" target="_blank">ben@bwidawsk.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 17-02-02 13:27:05, Jason Ekstrand wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This improves the performance of Dota 2 on my Sky Lake Skull Canyon<br>
machine by about 2-3%.<br>
<br>
Reviewed-by: Lionel Landwerlin <<a href="mailto:lionel.g.landwerlin@intel.com" target="_blank">lionel.g.landwerlin@intel.com</a><wbr>><br>
---<br>
src/intel/vulkan/anv_private.<wbr>h | 1 +<br>
src/intel/vulkan/gen8_cmd_buff<wbr>er.c | 157 ++++++++++++++++++++++++++++++<wbr>++++++-<br>
src/intel/vulkan/genX_pipeline<wbr>.c | 6 +-<br>
3 files changed, 158 insertions(+), 6 deletions(-)<br>
<br>
diff --git a/src/intel/vulkan/anv_private<wbr>.h b/src/intel/vulkan/anv_private<wbr>.h<br>
index 6efe4ea..9f88aef 100644<br>
--- a/src/intel/vulkan/anv_private<wbr>.h<br>
+++ b/src/intel/vulkan/anv_private<wbr>.h<br>
@@ -1482,6 +1482,7 @@ struct anv_pipeline {<br>
bool writes_depth;<br>
bool depth_test_enable;<br>
bool writes_stencil;<br>
+ bool stencil_test_enable;<br>
bool depth_clamp_enable;<br>
bool kill_pixel;<br>
<br>
diff --git a/src/intel/vulkan/gen8_cmd_bu<wbr>ffer.c b/src/intel/vulkan/gen8_cmd_bu<wbr>ffer.c<br>
index 271ab3f..6e44bd6 100644<br>
--- a/src/intel/vulkan/gen8_cmd_bu<wbr>ffer.c<br>
+++ b/src/intel/vulkan/gen8_cmd_bu<wbr>ffer.c<br>
@@ -157,16 +157,39 @@ __emit_sf_state(struct anv_cmd_buffer *cmd_buffer)<br>
void<br>
genX(cmd_buffer_enable_pma_fix<wbr>)(struct anv_cmd_buffer *cmd_buffer, bool enable)<br>
{<br>
-#if GEN_GEN == 8<br>
if (cmd_buffer->state.pma_fix_ena<wbr>bled == enable)<br>
return;<br>
<br>
+ cmd_buffer->state.pma_fix_ena<wbr>bled = enable;<br>
+<br>
+ /* According to the Broadwell PIPE_CONTROL documentation, software should<br>
+ * emit a PIPE_CONTROL with the CS Stall and Depth Cache Flush bits set<br>
+ * prior to the LRI. If stencil buffer writes are enabled, then a Render<br>
+ * Cache Flush is also necessary.<br>
+ *<br>
+ * The Sky Lake docs say to use a depth stall rather than a command<br>
+ * streamer stall. However, the hardware seems to violently disagree.<br>
+ * A full command streamer stall seems to be needed in both cases.<br>
+ */<br>
anv_batch_emit(&cmd_buffer->b<wbr>atch, GENX(PIPE_CONTROL), pc) {<br>
pc.DepthCacheFlushEnable = true;<br>
pc.CommandStreamerStallEnable = true;<br>
pc.RenderTargetCacheFlushEnabl<wbr>e = true;<br>
}<br>
<br>
+#if GEN_GEN == 9<br>
+<br>
+ uint32_t cache_mode;<br>
+ anv_pack_struct(&cache_mode, GENX(CACHE_MODE_0),<br>
+ .STCPMAOptimizationEnable = enable,<br>
+ .STCPMAOptimizationEnableMask = true);<br>
+ anv_batch_emit(&cmd_buffer->b<wbr>atch, GENX(MI_LOAD_REGISTER_IMM), lri) {<br>
+ lri.RegisterOffset = GENX(CACHE_MODE_0_num);<br>
+ lri.DataDWord = cache_mode;<br>
+ }<br>
+<br>
+#elif GEN_GEN == 8<br>
+<br>
uint32_t cache_mode;<br>
anv_pack_struct(&cache_mode, GENX(CACHE_MODE_1),<br>
.NPPMAFixEnable = enable,<br>
@@ -178,18 +201,20 @@ genX(cmd_buffer_enable_pma_fix<wbr>)(struct anv_cmd_buffer *cmd_buffer, bool enable)<br>
lri.DataDWord = cache_mode;<br>
}<br>
<br>
+#endif /* GEN_GEN == 8 */<br>
+<br>
/* After the LRI, a PIPE_CONTROL with both the Depth Stall and Depth Cache<br>
* Flush bits is often necessary. We do it regardless because it's easier.<br>
* The render cache flush is also necessary if stencil writes are enabled.<br>
+ *<br>
+ * Again, the Sky Lake docs give a different set of flushes but the BDW<br>
+ * flushes seem to work just as well.<br>
*/<br>
anv_batch_emit(&cmd_buffer->b<wbr>atch, GENX(PIPE_CONTROL), pc) {<br>
pc.DepthStallEnable = true;<br>
pc.DepthCacheFlushEnable = true;<br>
pc.RenderTargetCacheFlushEnabl<wbr>e = true;<br>
}<br>
-<br>
- cmd_buffer->state.pma_fix_ena<wbr>bled = enable;<br>
-#endif /* GEN_GEN == 8 */<br>
}<br>
<br>
static inline bool<br>
@@ -283,6 +308,126 @@ want_depth_pma_fix(struct anv_cmd_buffer *cmd_buffer)<br>
wm_prog_data->computed_depth_m<wbr>ode != PSCDEPTH_OFF;<br>
}<br>
<br>
+static inline bool<br>
+want_stencil_pma_fix(struct anv_cmd_buffer *cmd_buffer)<br>
+{<br>
+ assert(GEN_GEN == 9);<br>
+<br>
+ /* From the Sky Lake PRM Vol. 2c CACHE_MODE_1::STC PMA Optimization Enable:<br>
+ *<br>
+ * Clearing this bit will force the STC cache to wait for pending<br>
+ * retirement of pixels at the HZ-read stage and do the STC-test for<br>
+ * Non-promoted, R-computed and Computed depth modes instead of<br>
+ * postponing the STC-test to RCPFE.<br>
+ *<br>
+ * STC_TEST_EN = 3DSTATE_STENCIL_BUFFER::STENCI<wbr>L_BUFFER_ENABLE &&<br>
+ * 3DSTATE_WM_DEPTH_STENCIL::Sten<wbr>cilTestEnable<br>
+ *<br>
+ * STC_WRITE_EN = 3DSTATE_STENCIL_BUFFER::STENCI<wbr>L_BUFFER_ENABLE &&<br>
+ * (3DSTATE_WM_DEPTH_STENCIL::St<wbr>encil Buffer Write Enable &&<br>
+ * 3DSTATE_DEPTH_BUFFER::STENCIL_<wbr>WRITE_ENABLE)<br>
+ *<br>
+ * COMP_STC_EN = STC_TEST_EN &&<br>
+ * 3DSTATE_PS_EXTRA::PixelShaderC<wbr>omputesStencil<br>
+ *<br>
+ * SW parses the pipeline states to generate the following logical<br>
+ * signal indicating if PMA FIX can be enabled.<br>
+ *<br>
+ * STC_PMA_OPT =<br>
+ * 3DSTATE_WM::ForceThreadDispat<wbr>ch != 1 &&<br>
+ * !(3DSTATE_RASTER::ForceSample<wbr>Count != NUMRASTSAMPLES_0) &&<br>
+ * 3DSTATE_DEPTH_BUFFER::<wbr>SURFACE_TYPE != NULL &&<br>
+ * 3DSTATE_DEPTH_BUFFER::HIZ Enable &&<br>
+ * !(3DSTATE_WM::EDSC_Mode == 2) &&<br>
+ * 3DSTATE_PS_EXTRA::PixelShader<wbr>Valid &&<br>
+ * !(3DSTATE_WM_HZ_OP::DepthBuff<wbr>erClear ||<br>
+ * 3DSTATE_WM_HZ_OP::DepthBuffer<wbr>Resolve ||<br>
+ * 3DSTATE_WM_HZ_OP::<wbr>Hierarchical Depth Buffer Resolve Enable ||<br>
+ * 3DSTATE_WM_HZ_OP::StencilBuff<wbr>erClear) &&<br>
+ * (COMP_STC_EN || STC_WRITE_EN) &&<br>
+ * ((3DSTATE_PS_EXTRA::PixelShad<wbr>erKillsPixels ||<br>
+ * 3DSTATE_WM::ForceKillPix == ON ||<br>
+ * 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||<br>
+ * 3DSTATE_PS_BLEND::AlphaToCove<wbr>rageEnable ||<br>
+ * 3DSTATE_PS_BLEND::AlphaTestEn<wbr>able ||<br>
+ * 3DSTATE_WM_CHROMAKEY::ChromaK<wbr>eyKillEnable) ||<br>
+ * (3DSTATE_PS_EXTRA::Pixel Shader Computed Depth mode != PSCDEPTH_OFF))<br>
+ */<br>
+<br>
+ /* These are always true:<br>
+ * 3DSTATE_WM::ForceThreadDispatc<wbr>h != 1 &&<br>
+ * !(3DSTATE_RASTER::ForceSampleC<wbr>ount != NUMRASTSAMPLES_0)<br>
+ */<br>
+<br>
+ /* We only enable the PMA fix if we know for certain that HiZ is enabled.<br>
+ * If we don't know whether HiZ is enabled or not, we disable the PMA fix<br>
+ * and there is no harm.<br>
+ *<br>
+ * (3DSTATE_DEPTH_BUFFER::SURFACE<wbr>_TYPE != NULL) &&<br>
+ * 3DSTATE_DEPTH_BUFFER::HIZ Enable<br>
+ */<br>
+ if (!cmd_buffer->state.hiz_enable<wbr>d)<br>
+ return false;<br>
+<br>
+ /* We can't possibly know if HiZ is enabled without the framebuffer */<br>
+ assert(cmd_buffer->state.fram<wbr>ebuffer);<br>
+<br>
+ /* HiZ is enabled so we had better have a depth buffer with HiZ */<br>
+ const struct anv_image_view *ds_iview =<br>
+ anv_cmd_buffer_get_depth_stenc<wbr>il_view(cmd_buffer);<br>
+ assert(ds_iview && ds_iview->image->aux_usage == ISL_AUX_USAGE_HIZ);<br>
+<br>
+ /* 3DSTATE_PS_EXTRA::PixelShaderV<wbr>alid */<br>
+ struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;<br>
+ if (!anv_pipeline_has_stage(pipel<wbr>ine, MESA_SHADER_FRAGMENT))<br>
+ return false;<br>
+<br>
+ /* !(3DSTATE_WM::EDSC_Mode == 2) */<br>
+ const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);<br>
+ if (wm_prog_data->early_fragment_<wbr>tests)<br>
+ return false;<br>
+<br>
+ /* We never use anv_pipeline for HiZ ops so this is trivially true:<br>
+ * !(3DSTATE_WM_HZ_OP::DepthBuffe<wbr>rClear ||<br>
+ * 3DSTATE_WM_HZ_OP::DepthBufferR<wbr>esolve ||<br>
+ * 3DSTATE_WM_HZ_OP::Hierarchical Depth Buffer Resolve Enable ||<br>
+ * 3DSTATE_WM_HZ_OP::StencilBuffe<wbr>rClear)<br>
+ */<br>
+<br>
+ /* 3DSTATE_STENCIL_BUFFER::STENCI<wbr>L_BUFFER_ENABLE &&<br>
+ * 3DSTATE_WM_DEPTH_STENCIL::Sten<wbr>cilTestEnable<br>
+ */<br>
+ const bool stc_test_en =<br>
+ (ds_iview->image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&<br>
+ pipeline->stencil_test_enable;<br>
+<br>
+ /* 3DSTATE_STENCIL_BUFFER::STENCI<wbr>L_BUFFER_ENABLE &&<br>
+ * (3DSTATE_WM_DEPTH_STENCIL::Ste<wbr>ncil Buffer Write Enable &&<br>
+ * 3DSTATE_DEPTH_BUFFER::STENCIL_<wbr>WRITE_ENABLE)<br>
+ */<br>
+ const bool stc_write_en =<br>
+ (ds_iview->image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&<br>
+ pipeline->writes_stencil;<br>
+<br>
+ /* STC_TEST_EN && 3DSTATE_PS_EXTRA::PixelShaderC<wbr>omputesStencil */<br>
+ const bool comp_stc_en = stc_test_en && wm_prog_data->computed_stencil<wbr>;<br>
+<br>
+ /* COMP_STC_EN || STC_WRITE_EN */<br>
+ if (!(comp_stc_en || stc_write_en))<br>
+ return false;<br>
+<br>
+ /* (3DSTATE_PS_EXTRA::PixelShader<wbr>KillsPixels ||<br>
+ * 3DSTATE_WM::ForceKillPix == ON ||<br>
+ * 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||<br>
+ * 3DSTATE_PS_BLEND::AlphaToCover<wbr>ageEnable ||<br>
+ * 3DSTATE_PS_BLEND::AlphaTestEna<wbr>ble ||<br>
+ * 3DSTATE_WM_CHROMAKEY::ChromaKe<wbr>yKillEnable) ||<br>
+ * (3DSTATE_PS_EXTRA::Pixel Shader Computed Depth mode != PSCDEPTH_OFF)<br>
+ */<br>
+ return pipeline->kill_pixel ||<br>
+ wm_prog_data->computed_depth_m<wbr>ode != PSCDEPTH_OFF;<br>
+}<br>
+<br>
void<br>
genX(cmd_buffer_flush_dynamic_<wbr>state)(struct anv_cmd_buffer *cmd_buffer)<br>
{<br>
@@ -392,6 +537,7 @@ genX(cmd_buffer_flush_dynamic_<wbr>state)(struct anv_cmd_buffer *cmd_buffer)<br>
}<br>
<br>
if (cmd_buffer->state.dirty & (ANV_CMD_DIRTY_PIPELINE |<br>
+ ANV_CMD_DIRTY_RENDER_TARGETS |<br>
ANV_CMD_DIRTY_DYNAMIC_STENCIL_<wbr>COMPARE_MASK |<br>
ANV_CMD_DIRTY_DYNAMIC_STENCIL_<wbr>WRITE_MASK |<br>
ANV_CMD_DIRTY_DYNAMIC_STENCIL_<wbr>REFERENCE)) {<br>
@@ -417,6 +563,9 @@ genX(cmd_buffer_flush_dynamic_<wbr>state)(struct anv_cmd_buffer *cmd_buffer)<br>
<br>
anv_batch_emit_merge(&cmd_buff<wbr>er->batch, dwords,<br>
pipeline->gen9.wm_depth_stenc<wbr>il);<br>
+<br>
+ genX(cmd_buffer_enable_pma_fix<wbr>)(cmd_buffer,<br>
+ want_stencil_pma_fix(cmd_buffe<wbr>r));<br>
}<br>
#endif<br>
<br>
diff --git a/src/intel/vulkan/genX_pipeli<wbr>ne.c b/src/intel/vulkan/genX_pipeli<wbr>ne.c<br>
index 3f701f3..3087037 100644<br>
--- a/src/intel/vulkan/genX_pipeli<wbr>ne.c<br>
+++ b/src/intel/vulkan/genX_pipeli<wbr>ne.c<br>
@@ -742,6 +742,7 @@ emit_ds_state(struct anv_pipeline *pipeline,<br>
* to make sure it's initialized to something useful.<br>
*/<br>
pipeline->writes_stencil = false;<br>
+ pipeline->stencil_test_enable = false;<br>
pipeline->writes_depth = false;<br>
pipeline->depth_test_enable = false;<br>
memset(depth_stencil_dw, 0, sizeof(depth_stencil_dw));<br>
@@ -757,6 +758,7 @@ emit_ds_state(struct anv_pipeline *pipeline,<br>
<br>
<wbr>VkPipelineDepthStencilStateCre<wbr>ateInfo info = *pCreateInfo;<br>
sanitize_ds_state(&info, &pipeline->writes_stencil, ds_aspects);<br>
+ pipeline->stencil_test_enable = info.stencilTestEnable;<br>
pipeline->writes_depth = info.depthWriteEnable;<br>
pipeline->depth_test_enable = info.depthTestEnable;<br>
<br>
@@ -1514,8 +1516,8 @@ compute_kill_pixel(struct anv_pipeline *pipeline,<br>
const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);<br>
<br>
/* This computes the KillPixel portion of the computation for whether or<br>
- * not we want to enable the PMA fix on gen8. It's given by this chunk of<br>
- * the giant formula:<br>
+ * not we want to enable the PMA fix on gen8 or gen9. It's given by this<br>
+ * chunk of the giant formula:<br>
*<br>
* (3DSTATE_PS_EXTRA::PixelShader<wbr>KillsPixels ||<br>
* 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||<br>
</blockquote>
<br>
<br></div></div>
s/Sky Lake/Skylake/g<br></blockquote><div><br></div><div>I can never figure out which it's supposed to be. The PRM says "Skylake" but I thought ARC said "Sky Lake" but, now that I look, it says "Skylake" too. I know I saw some official thing that said "Sky Lake". Oh well. I'll change it.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Honestly, I'm a bit surprised you got 2-3%, that's awesome. </blockquote></div><br></div><div class="gmail_extra">Yeah, I was very pleased. Dota 2 must use *a lot* of stencil.<br></div></div>