<div dir="ltr">Commit <span style="font-family:courier new,monospace">17c7ead7</span> exposed a bug in how uniform loading happens in the presence of discard.  It manifested itself in an application as randomly incorrect pixels on the borders of conditional areas.<br>
<br>We believe it is due to how discards jump to the end of the shader incorrectly for some channels.  The current implementation checks each 2x2 subspan to preserve derivatives.  When uniform loading via samplers was turned on, it uses a full execution mask, as stated in <span style="font-family:courier new,monospace">lower_uniform_pull_constant_loads()</span>, and only populates four channels of the destination (see <span style="font-family:courier new,monospace">generate_uniform_pull_constant_load_gen7()</span>).  We think that is happening incorrectly when the first subspan has been jumped over.<br>
<br>A possible fix is to only jump to the end of the shader if all relevant channels are disabled, i.e. all 8 or 16, depending on dispatch.  This preserves the original speedup noted in commit <span style="font-family:courier new,monospace">beafced2</span>.  There are other more heavyweight fixes (i.e. don't use sampler for uniforms if discard in use), but this seems like a good fix.  I've appended it below as a patch.  It changes the shader accordingly:<br>
<br><span style="font-family:courier new,monospace">before    : 0x000000d0: (-f0.1.any4h) halt(8) 17 2      null                            { align1 WE_all 1Q };<br>after(8)  : 0x000000d0: (-f0.1.any8h) halt(8) 17 2      null                            { align1 WE_all 1Q };<br>
after(16) : 0x00000250: (-f0.1.any16h) halt(16) 17 2    null                            { align1 WE_all 1H };</span><br><br>I attached a test case to the bugzilla entry below.<br><br>Thanks for any review or feedback.  Curious if anyone sees a better fix.<br>
<br>-C<br><br><br>From 871671c4ab8ff00e85b434865e8855fc356efa8f Mon Sep 17 00:00:00 2001<br>From: Cody Northrop <<a href="mailto:cody@lunarg.com">cody@lunarg.com</a>><br>Date: Thu, 12 Jun 2014 09:07:18 -0600<br>Subject: [PATCH] i965/fs: Update discard jump to preserve uniform loads via<br>
 sampler.<br><br>The series that implemented this optimization was done before<br>the changes to use samplers for uniform loads.  Uniform sampler<br>loads use special execution masks and only populate four<br>channels, so we can't jump over those or corruption ensues.<br>
Use a more conservative jump mask which only jumps to the end<br>if all relevant channels are disabled.<br><br>No change was observed in GLbenchmark 2.7, so the optimization<br>is preserved.<br><br>Signed-off-by: Cody Northrop <<a href="mailto:cody@lunarg.com">cody@lunarg.com</a>><br>
Reviewed-by: Mike Stroyan <<a href="mailto:mike@lunarg.com">mike@lunarg.com</a>><br>Bugzilla: <a href="https://bugs.freedesktop.org/show_bug.cgi?id=79948">https://bugs.freedesktop.org/show_bug.cgi?id=79948</a><br>---<br>
 src/mesa/drivers/dri/i965/brw_fs_visitor.cpp | 10 +++++++++-<br> 1 file changed, 9 insertions(+), 1 deletion(-)<br><br>diff --git a/src/mesa/drivers/dri/i965/brw_fs_visitor.cpp b/src/mesa/drivers/dri/i965/brw_fs_visitor.cpp<br>
index 8858852..fe05715 100644<br>--- a/src/mesa/drivers/dri/i965/brw_fs_visitor.cpp<br>+++ b/src/mesa/drivers/dri/i965/brw_fs_visitor.cpp<br>@@ -1907,7 +1907,15 @@ fs_visitor::visit(ir_discard *ir)<br>        */<br>       fs_inst *discard_jump = emit(FS_OPCODE_DISCARD_JUMP);<br>
       discard_jump->flag_subreg = 1;<br>-      discard_jump->predicate = BRW_PREDICATE_ALIGN1_ANY4H;<br>+<br>+      /* Uniforms are now loaded using samplers with a routine that has<br>+       * its own execution mask, so we can only jump if all relevant<br>
+       * channels are dead.  This is more conservative than the previous<br>+       * four channel checking, but still preserves speedups.<br>+       */<br>+      discard_jump->predicate = (8 == dispatch_width)<br>+                                ? BRW_PREDICATE_ALIGN1_ANY8H<br>
+                                : BRW_PREDICATE_ALIGN1_ANY16H;<br>       discard_jump->predicate_inverse = true;<br>    }<br> }<br>-- <br>1.8.3.2<br><br><br></div>