i965/fs: use SSA values directly
[mesa.git] / src / mesa / drivers / dri / i965 / brw_sf_emit.c
index 56f7c986e7844c4dd58d66a109b7d8e9513f1bc9..b3ee5c116275b9edc24630189569c51c0d852add 100644 (file)
@@ -1,8 +1,8 @@
 /*
  Copyright (C) Intel Corp.  2006.  All Rights Reserved.
- Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
+ Intel funded Tungsten Graphics to
  develop this 3D driver.
+
  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:
+
  The above copyright notice and this permission notice (including the
  next paragraph) shall be included in all copies or substantial
  portions of the Software.
+
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
  **********************************************************************/
  /*
   * Authors:
-  *   Keith Whitwell <keith@tungstengraphics.com>
+  *   Keith Whitwell <keithw@vmware.com>
   */
-   
+
 
 #include "main/glheader.h"
 #include "main/macros.h"
 #include "brw_sf.h"
 
 
-static struct brw_reg get_vert_attr(struct brw_sf_compile *c,
-                                   struct brw_reg vert,
-                                   GLuint attr)
+/**
+ * Determine the vue slot corresponding to the given half of the given register.
+ */
+static inline int vert_reg_to_vue_slot(struct brw_sf_compile *c, GLuint reg,
+                                       int half)
 {
-   GLuint off = c->attr_to_idx[attr] / 2;
-   GLuint sub = c->attr_to_idx[attr] % 2;
+   return (reg + c->urb_entry_read_offset) * 2 + half;
+}
+
+/**
+ * Determine the varying corresponding to the given half of the given
+ * register.  half=0 means the first half of a register, half=1 means the
+ * second half.
+ */
+static inline int vert_reg_to_varying(struct brw_sf_compile *c, GLuint reg,
+                                      int half)
+{
+   int vue_slot = vert_reg_to_vue_slot(c, reg, half);
+   return c->vue_map.slot_to_varying[vue_slot];
+}
+
+/**
+ * Determine the register corresponding to the given vue slot
+ */
+static struct brw_reg get_vue_slot(struct brw_sf_compile *c,
+                                   struct brw_reg vert,
+                                   int vue_slot)
+{
+   GLuint off = vue_slot / 2 - c->urb_entry_read_offset;
+   GLuint sub = vue_slot % 2;
 
    return brw_vec4_grf(vert.nr + off, sub * 4);
 }
 
-static GLboolean have_attr(struct brw_sf_compile *c,
-                          GLuint attr)
+/**
+ * Determine the register corresponding to the given varying.
+ */
+static struct brw_reg get_varying(struct brw_sf_compile *c,
+                                  struct brw_reg vert,
+                                  GLuint varying)
+{
+   int vue_slot = c->vue_map.varying_to_slot[varying];
+   assert (vue_slot >= c->urb_entry_read_offset);
+   return get_vue_slot(c, vert, vue_slot);
+}
+
+static bool
+have_attr(struct brw_sf_compile *c, GLuint attr)
 {
    return (c->key.attrs & BITFIELD64_BIT(attr)) ? 1 : 0;
 }
 
-/*********************************************************************** 
+/***********************************************************************
  * Twoside lighting
  */
 static void copy_bfc( struct brw_sf_compile *c,
                      struct brw_reg vert )
 {
-   struct brw_compile *p = &c->func;
+   struct brw_codegen *p = &c->func;
    GLuint i;
 
    for (i = 0; i < 2; i++) {
-      if (have_attr(c, VERT_RESULT_COL0+i) &&
-         have_attr(c, VERT_RESULT_BFC0+i))
-        brw_MOV(p, 
-                get_vert_attr(c, vert, VERT_RESULT_COL0+i), 
-                get_vert_attr(c, vert, VERT_RESULT_BFC0+i));
+      if (have_attr(c, VARYING_SLOT_COL0+i) &&
+         have_attr(c, VARYING_SLOT_BFC0+i))
+        brw_MOV(p,
+                get_varying(c, vert, VARYING_SLOT_COL0+i),
+                get_varying(c, vert, VARYING_SLOT_BFC0+i));
    }
 }
 
 
 static void do_twoside_color( struct brw_sf_compile *c )
 {
-   struct brw_compile *p = &c->func;
-   struct brw_instruction *if_insn;
+   struct brw_codegen *p = &c->func;
    GLuint backface_conditional = c->key.frontface_ccw ? BRW_CONDITIONAL_G : BRW_CONDITIONAL_L;
 
    /* Already done in clip program:
@@ -89,22 +124,21 @@ static void do_twoside_color( struct brw_sf_compile *c )
    if (c->key.primitive == SF_UNFILLED_TRIS)
       return;
 
-   /* XXX: What happens if BFC isn't present?  This could only happen
-    * for user-supplied vertex programs, as t_vp_build.c always does
-    * the right thing.
+   /* If the vertex shader provides backface color, do the selection. The VS
+    * promises to set up the front color if the backface color is provided, but
+    * it may contain junk if never written to.
     */
-   if (!(have_attr(c, VERT_RESULT_COL0) && have_attr(c, VERT_RESULT_BFC0)) &&
-       !(have_attr(c, VERT_RESULT_COL1) && have_attr(c, VERT_RESULT_BFC1)))
+   if (!(have_attr(c, VARYING_SLOT_COL0) && have_attr(c, VARYING_SLOT_BFC0)) &&
+       !(have_attr(c, VARYING_SLOT_COL1) && have_attr(c, VARYING_SLOT_BFC1)))
       return;
-   
+
    /* Need to use BRW_EXECUTE_4 and also do an 4-wide compare in order
     * to get all channels active inside the IF.  In the clipping code
     * we run with NoMask, so it's not an option and we can use
     * BRW_EXECUTE_1 for all comparisions.
     */
-   brw_push_insn_state(p);
    brw_CMP(p, vec4(brw_null_reg()), backface_conditional, c->det, brw_imm_f(0));
-   if_insn = brw_IF(p, BRW_EXECUTE_4); 
+   brw_IF(p, BRW_EXECUTE_4);
    {
       switch (c->nr_verts) {
       case 3: copy_bfc(c, c->vert[2]);
@@ -112,8 +146,7 @@ static void do_twoside_color( struct brw_sf_compile *c )
       case 1: copy_bfc(c, c->vert[0]);
       }
    }
-   brw_ENDIF(p, if_insn);
-   brw_pop_insn_state(p);
+   brw_ENDIF(p);
 }
 
 
@@ -122,22 +155,32 @@ static void do_twoside_color( struct brw_sf_compile *c )
  * Flat shading
  */
 
-#define VERT_RESULT_COLOR_BITS (BITFIELD64_BIT(VERT_RESULT_COL0) | \
-                               BITFIELD64_BIT(VERT_RESULT_COL1))
+static void copy_flatshaded_attributes(struct brw_sf_compile *c,
+                                       struct brw_reg dst,
+                                       struct brw_reg src)
+{
+   struct brw_codegen *p = &c->func;
+   int i;
+
+   for (i = 0; i < c->vue_map.num_slots; i++) {
+      if (c->key.interpolation_mode.mode[i] == INTERP_QUALIFIER_FLAT) {
+         brw_MOV(p,
+                 get_vue_slot(c, dst, i),
+                 get_vue_slot(c, src, i));
+      }
+   }
+}
 
-static void copy_colors( struct brw_sf_compile *c,
-                    struct brw_reg dst,
-                    struct brw_reg src)
+static int count_flatshaded_attributes(struct brw_sf_compile *c)
 {
-   struct brw_compile *p = &c->func;
-   GLuint i;
+   int i;
+   int count = 0;
 
-   for (i = VERT_RESULT_COL0; i <= VERT_RESULT_COL1; i++) {
-      if (have_attr(c,i))
-        brw_MOV(p, 
-                get_vert_attr(c, dst, i), 
-                get_vert_attr(c, src, i));
-   }
+   for (i = 0; i < c->vue_map.num_slots; i++)
+      if (c->key.interpolation_mode.mode[i] == INTERP_QUALIFIER_FLAT)
+         count++;
+
+   return count;
 }
 
 
@@ -148,72 +191,58 @@ static void copy_colors( struct brw_sf_compile *c,
  */
 static void do_flatshade_triangle( struct brw_sf_compile *c )
 {
-   struct brw_compile *p = &c->func;
-   struct intel_context *intel = &p->brw->intel;
-   struct brw_reg ip = brw_ip_reg();
-   GLuint nr = brw_count_bits(c->key.attrs & VERT_RESULT_COLOR_BITS);
+   struct brw_codegen *p = &c->func;
+   GLuint nr;
    GLuint jmpi = 1;
 
-   if (!nr)
-      return;
-
    /* Already done in clip program:
     */
    if (c->key.primitive == SF_UNFILLED_TRIS)
       return;
 
-   if (intel->is_ironlake)
+   if (p->devinfo->gen == 5)
        jmpi = 2;
 
-   brw_push_insn_state(p);
-   
-   brw_MUL(p, c->pv, c->pv, brw_imm_d(jmpi*(nr*2+1)));
-   brw_JMPI(p, ip, ip, c->pv);
+   nr = count_flatshaded_attributes(c);
 
-   copy_colors(c, c->vert[1], c->vert[0]);
-   copy_colors(c, c->vert[2], c->vert[0]);
-   brw_JMPI(p, ip, ip, brw_imm_d(jmpi*(nr*4+1)));
+   brw_MUL(p, c->pv, c->pv, brw_imm_d(jmpi*(nr*2+1)));
+   brw_JMPI(p, c->pv, BRW_PREDICATE_NONE);
 
-   copy_colors(c, c->vert[0], c->vert[1]);
-   copy_colors(c, c->vert[2], c->vert[1]);
-   brw_JMPI(p, ip, ip, brw_imm_d(jmpi*nr*2));
+   copy_flatshaded_attributes(c, c->vert[1], c->vert[0]);
+   copy_flatshaded_attributes(c, c->vert[2], c->vert[0]);
+   brw_JMPI(p, brw_imm_d(jmpi*(nr*4+1)), BRW_PREDICATE_NONE);
 
-   copy_colors(c, c->vert[0], c->vert[2]);
-   copy_colors(c, c->vert[1], c->vert[2]);
+   copy_flatshaded_attributes(c, c->vert[0], c->vert[1]);
+   copy_flatshaded_attributes(c, c->vert[2], c->vert[1]);
+   brw_JMPI(p, brw_imm_d(jmpi*nr*2), BRW_PREDICATE_NONE);
 
-   brw_pop_insn_state(p);
+   copy_flatshaded_attributes(c, c->vert[0], c->vert[2]);
+   copy_flatshaded_attributes(c, c->vert[1], c->vert[2]);
 }
        
 
 static void do_flatshade_line( struct brw_sf_compile *c )
 {
-   struct brw_compile *p = &c->func;
-   struct intel_context *intel = &p->brw->intel;
-   struct brw_reg ip = brw_ip_reg();
-   GLuint nr = brw_count_bits(c->key.attrs & VERT_RESULT_COLOR_BITS);
+   struct brw_codegen *p = &c->func;
+   GLuint nr;
    GLuint jmpi = 1;
 
-   if (!nr)
-      return;
-
-   /* Already done in clip program: 
+   /* Already done in clip program:
     */
    if (c->key.primitive == SF_UNFILLED_TRIS)
       return;
 
-   if (intel->is_ironlake)
+   if (p->devinfo->gen == 5)
        jmpi = 2;
 
-   brw_push_insn_state(p);
-   
-   brw_MUL(p, c->pv, c->pv, brw_imm_d(jmpi*(nr+1)));
-   brw_JMPI(p, ip, ip, c->pv);
-   copy_colors(c, c->vert[1], c->vert[0]);
+   nr = count_flatshaded_attributes(c);
 
-   brw_JMPI(p, ip, ip, brw_imm_ud(jmpi*nr));
-   copy_colors(c, c->vert[0], c->vert[1]);
+   brw_MUL(p, c->pv, c->pv, brw_imm_d(jmpi*(nr+1)));
+   brw_JMPI(p, c->pv, BRW_PREDICATE_NONE);
+   copy_flatshaded_attributes(c, c->vert[1], c->vert[0]);
 
-   brw_pop_insn_state(p);
+   brw_JMPI(p, brw_imm_ud(jmpi*nr), BRW_PREDICATE_NONE);
+   copy_flatshaded_attributes(c, c->vert[0], c->vert[1]);
 }
 
        
@@ -244,7 +273,7 @@ static void alloc_regs( struct brw_sf_compile *c )
    c->inv_w[1] = brw_vec1_grf(2, 3);
    c->z[2]     = brw_vec1_grf(2, 4);
    c->inv_w[2] = brw_vec1_grf(2, 5);
-   
+
    /* The vertices:
     */
    reg = 3;
@@ -263,7 +292,7 @@ static void alloc_regs( struct brw_sf_compile *c )
    /* Note grf allocation:
     */
    c->prog_data.total_grf = reg;
-   
+
 
    /* Outputs of this program - interpolation coefficients for
     * rasterization:
@@ -276,17 +305,13 @@ static void alloc_regs( struct brw_sf_compile *c )
 
 static void copy_z_inv_w( struct brw_sf_compile *c )
 {
-   struct brw_compile *p = &c->func;
+   struct brw_codegen *p = &c->func;
    GLuint i;
 
-   brw_push_insn_state(p);
-       
    /* Copy both scalars with a single MOV:
     */
    for (i = 0; i < c->nr_verts; i++)
       brw_MOV(p, vec2(suboffset(c->vert[i], 2)), vec2(c->z[i]));
-        
-   brw_pop_insn_state(p);
 }
 
 
@@ -295,60 +320,48 @@ static void invert_det( struct brw_sf_compile *c)
    /* Looks like we invert all 8 elements just to get 1/det in
     * position 2 !?!
     */
-   brw_math(&c->func, 
-           c->inv_det, 
-           BRW_MATH_FUNCTION_INV,
-           BRW_MATH_SATURATE_NONE,
-           0, 
-           c->det,
-           BRW_MATH_DATA_SCALAR,
-           BRW_MATH_PRECISION_FULL);
+   gen4_math(&c->func,
+            c->inv_det,
+            BRW_MATH_FUNCTION_INV,
+            0,
+            c->det,
+            BRW_MATH_PRECISION_FULL);
 
 }
 
 
-static GLboolean calculate_masks( struct brw_sf_compile *c,
-                                 GLuint reg,
-                                 GLushort *pc,
-                                 GLushort *pc_persp,
-                                 GLushort *pc_linear)
+static bool
+calculate_masks(struct brw_sf_compile *c,
+                GLuint reg,
+                GLushort *pc,
+                GLushort *pc_persp,
+                GLushort *pc_linear)
 {
-   GLboolean is_last_attr = (reg == c->nr_setup_regs - 1);
-   GLbitfield64 persp_mask;
-   GLbitfield64 linear_mask;
-
-   if (c->key.do_flat_shading || c->key.linear_color)
-      persp_mask = c->key.attrs & ~(FRAG_BIT_WPOS |
-                                    FRAG_BIT_COL0 |
-                                    FRAG_BIT_COL1);
-   else
-      persp_mask = c->key.attrs & ~(FRAG_BIT_WPOS);
-
-   if (c->key.do_flat_shading)
-      linear_mask = c->key.attrs & ~(FRAG_BIT_COL0|FRAG_BIT_COL1);
-   else
-      linear_mask = c->key.attrs;
+   bool is_last_attr = (reg == c->nr_setup_regs - 1);
+   enum glsl_interp_qualifier interp;
 
    *pc_persp = 0;
    *pc_linear = 0;
    *pc = 0xf;
-      
-   if (persp_mask & BITFIELD64_BIT(c->idx_to_attr[reg*2]))
-      *pc_persp = 0xf;
 
-   if (linear_mask & BITFIELD64_BIT(c->idx_to_attr[reg*2]))
+   interp = c->key.interpolation_mode.mode[vert_reg_to_vue_slot(c, reg, 0)];
+   if (interp == INTERP_QUALIFIER_SMOOTH) {
+      *pc_linear = 0xf;
+      *pc_persp = 0xf;
+   } else if (interp == INTERP_QUALIFIER_NOPERSPECTIVE)
       *pc_linear = 0xf;
 
    /* Maybe only processs one attribute on the final round:
     */
-   if (reg*2+1 < c->nr_setup_attrs) {
+   if (vert_reg_to_varying(c, reg, 1) != BRW_VARYING_SLOT_COUNT) {
       *pc |= 0xf0;
 
-      if (persp_mask & BITFIELD64_BIT(c->idx_to_attr[reg*2+1]))
-        *pc_persp |= 0xf0;
-
-      if (linear_mask & BITFIELD64_BIT(c->idx_to_attr[reg*2+1]))
-        *pc_linear |= 0xf0;
+      interp = c->key.interpolation_mode.mode[vert_reg_to_vue_slot(c, reg, 1)];
+      if (interp == INTERP_QUALIFIER_SMOOTH) {
+         *pc_linear |= 0xf0;
+         *pc_persp |= 0xf0;
+      } else if (interp == INTERP_QUALIFIER_NOPERSPECTIVE)
+         *pc_linear |= 0xf0;
    }
 
    return is_last_attr;
@@ -360,34 +373,52 @@ static GLboolean calculate_masks( struct brw_sf_compile *c,
 static uint16_t
 calculate_point_sprite_mask(struct brw_sf_compile *c, GLuint reg)
 {
-   int attr1, attr2;
+   int varying1, varying2;
    uint16_t pc = 0;
 
-   attr1 = c->idx_to_attr[reg * 2];
-   if (attr1 >= VERT_RESULT_TEX0 && attr1 <= VERT_RESULT_TEX7) {
-      if (c->key.point_sprite_coord_replace & (1 << (attr1 - VERT_RESULT_TEX0)))
+   varying1 = vert_reg_to_varying(c, reg, 0);
+   if (varying1 >= VARYING_SLOT_TEX0 && varying1 <= VARYING_SLOT_TEX7) {
+      if (c->key.point_sprite_coord_replace & (1 << (varying1 - VARYING_SLOT_TEX0)))
         pc |= 0x0f;
    }
-
-   if (reg * 2 + 1 < c->nr_setup_attrs) {
-       attr2 = c->idx_to_attr[reg * 2 + 1];
-       if (attr2 >= VERT_RESULT_TEX0 && attr2 <= VERT_RESULT_TEX7) {
-         if (c->key.point_sprite_coord_replace & (1 << (attr2 -
-                                                        VERT_RESULT_TEX0)))
-            pc |= 0xf0;
-       }
+   if (varying1 == BRW_VARYING_SLOT_PNTC)
+      pc |= 0x0f;
+
+   varying2 = vert_reg_to_varying(c, reg, 1);
+   if (varying2 >= VARYING_SLOT_TEX0 && varying2 <= VARYING_SLOT_TEX7) {
+      if (c->key.point_sprite_coord_replace & (1 << (varying2 -
+                                                     VARYING_SLOT_TEX0)))
+         pc |= 0xf0;
    }
+   if (varying2 == BRW_VARYING_SLOT_PNTC)
+      pc |= 0xf0;
 
    return pc;
 }
 
+static void
+set_predicate_control_flag_value(struct brw_codegen *p,
+                                 struct brw_sf_compile *c,
+                                 unsigned value)
+{
+   brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
+
+   if (value != 0xff) {
+      if (value != c->flag_value) {
+         brw_MOV(p, brw_flag_reg(0, 0), brw_imm_uw(value));
+         c->flag_value = value;
+      }
 
+      brw_set_default_predicate_control(p, BRW_PREDICATE_NORMAL);
+   }
+}
 
-void brw_emit_tri_setup( struct brw_sf_compile *c, GLboolean allocate)
+void brw_emit_tri_setup(struct brw_sf_compile *c, bool allocate)
 {
-   struct brw_compile *p = &c->func;
+   struct brw_codegen *p = &c->func;
    GLuint i;
 
+   c->flag_value = 0xff;
    c->nr_verts = 3;
 
    if (allocate)
@@ -396,13 +427,13 @@ void brw_emit_tri_setup( struct brw_sf_compile *c, GLboolean allocate)
    invert_det(c);
    copy_z_inv_w(c);
 
-   if (c->key.do_twoside_color) 
+   if (c->key.do_twoside_color)
       do_twoside_color(c);
 
-   if (c->key.do_flat_shading)
+   if (c->has_flat_shading)
       do_flatshade_triangle(c);
-      
-   
+
+
    for (i = 0; i < c->nr_setup_regs; i++)
    {
       /* Pair of incoming attributes:
@@ -411,22 +442,22 @@ void brw_emit_tri_setup( struct brw_sf_compile *c, GLboolean allocate)
       struct brw_reg a1 = offset(c->vert[1], i);
       struct brw_reg a2 = offset(c->vert[2], i);
       GLushort pc, pc_persp, pc_linear;
-      GLboolean last = calculate_masks(c, i, &pc, &pc_persp, &pc_linear);
+      bool last = calculate_masks(c, i, &pc, &pc_persp, &pc_linear);
 
       if (pc_persp)
       {
-        brw_set_predicate_control_flag_value(p, pc_persp);
+        set_predicate_control_flag_value(p, c, pc_persp);
         brw_MUL(p, a0, a0, c->inv_w[0]);
         brw_MUL(p, a1, a1, c->inv_w[1]);
         brw_MUL(p, a2, a2, c->inv_w[2]);
       }
-      
-      
+
+
       /* Calculate coefficients for interpolated values:
-       */      
+       */
       if (pc_linear)
       {
-        brw_set_predicate_control_flag_value(p, pc_linear);
+        set_predicate_control_flag_value(p, c, pc_linear);
 
         brw_ADD(p, c->a1_sub_a0, a1, negate(a0));
         brw_ADD(p, c->a2_sub_a0, a2, negate(a0));
@@ -445,38 +476,38 @@ void brw_emit_tri_setup( struct brw_sf_compile *c, GLboolean allocate)
       }
 
       {
-        brw_set_predicate_control_flag_value(p, pc); 
+        set_predicate_control_flag_value(p, c, pc);
         /* start point for interpolation
          */
         brw_MOV(p, c->m3C0, a0);
-      
+
         /* Copy m0..m3 to URB.  m0 is implicitly copied from r0 in
          * the send instruction:
-         */     
-        brw_urb_WRITE(p, 
+         */    
+        brw_urb_WRITE(p,
                       brw_null_reg(),
                       0,
                       brw_vec8_grf(0, 0), /* r0, will be copied to m0 */
-                      0,       /* allocate */
-                      1,       /* used */
+                       last ? BRW_URB_WRITE_EOT_COMPLETE
+                       : BRW_URB_WRITE_NO_FLAGS,
                       4,       /* msg len */
                       0,       /* response len */
-                      last,    /* eot */
-                      last,    /* writes complete */
                       i*4,     /* offset */
                       BRW_URB_SWIZZLE_TRANSPOSE); /* XXX: Swizzle control "SF to windower" */
       }
    }
+
+   brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
 }
 
 
 
-void brw_emit_line_setup( struct brw_sf_compile *c, GLboolean allocate)
+void brw_emit_line_setup(struct brw_sf_compile *c, bool allocate)
 {
-   struct brw_compile *p = &c->func;
+   struct brw_codegen *p = &c->func;
    GLuint i;
 
-
+   c->flag_value = 0xff;
    c->nr_verts = 2;
 
    if (allocate)
@@ -485,7 +516,7 @@ void brw_emit_line_setup( struct brw_sf_compile *c, GLboolean allocate)
    invert_det(c);
    copy_z_inv_w(c);
 
-   if (c->key.do_flat_shading)
+   if (c->has_flat_shading)
       do_flatshade_line(c);
 
    for (i = 0; i < c->nr_setup_regs; i++)
@@ -495,11 +526,11 @@ void brw_emit_line_setup( struct brw_sf_compile *c, GLboolean allocate)
       struct brw_reg a0 = offset(c->vert[0], i);
       struct brw_reg a1 = offset(c->vert[1], i);
       GLushort pc, pc_persp, pc_linear;
-      GLboolean last = calculate_masks(c, i, &pc, &pc_persp, &pc_linear);
+      bool last = calculate_masks(c, i, &pc, &pc_persp, &pc_linear);
 
       if (pc_persp)
       {
-        brw_set_predicate_control_flag_value(p, pc_persp);
+        set_predicate_control_flag_value(p, c, pc_persp);
         brw_MUL(p, a0, a0, c->inv_w[0]);
         brw_MUL(p, a1, a1, c->inv_w[1]);
       }
@@ -507,11 +538,11 @@ void brw_emit_line_setup( struct brw_sf_compile *c, GLboolean allocate)
       /* Calculate coefficients for position, color:
        */
       if (pc_linear) {
-        brw_set_predicate_control_flag_value(p, pc_linear); 
+        set_predicate_control_flag_value(p, c, pc_linear);
 
         brw_ADD(p, c->a1_sub_a0, a1, negate(a0));
 
-        brw_MUL(p, c->tmp, c->a1_sub_a0, c->dx0); 
+        brw_MUL(p, c->tmp, c->a1_sub_a0, c->dx0);
         brw_MUL(p, c->m1Cx, c->tmp, c->inv_det);
                
         brw_MUL(p, c->tmp, c->a1_sub_a0, c->dy0);
@@ -519,35 +550,36 @@ void brw_emit_line_setup( struct brw_sf_compile *c, GLboolean allocate)
       }
 
       {
-        brw_set_predicate_control_flag_value(p, pc); 
+        set_predicate_control_flag_value(p, c, pc);
 
         /* start point for interpolation
          */
         brw_MOV(p, c->m3C0, a0);
 
-        /* Copy m0..m3 to URB. 
+        /* Copy m0..m3 to URB.
          */
-        brw_urb_WRITE(p, 
+        brw_urb_WRITE(p,
                       brw_null_reg(),
                       0,
                       brw_vec8_grf(0, 0),
-                      0,       /* allocate */
-                      1,       /* used */
+                       last ? BRW_URB_WRITE_EOT_COMPLETE
+                       : BRW_URB_WRITE_NO_FLAGS,
                       4,       /* msg len */
                       0,       /* response len */
-                      last,    /* eot */
-                      last,    /* writes complete */
                       i*4,     /* urb destination offset */
-                      BRW_URB_SWIZZLE_TRANSPOSE); 
+                      BRW_URB_SWIZZLE_TRANSPOSE);
       }
-   } 
+   }
+
+   brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
 }
 
-void brw_emit_point_sprite_setup( struct brw_sf_compile *c, GLboolean allocate)
+void brw_emit_point_sprite_setup(struct brw_sf_compile *c, bool allocate)
 {
-   struct brw_compile *p = &c->func;
+   struct brw_codegen *p = &c->func;
    GLuint i;
 
+   c->flag_value = 0xff;
    c->nr_verts = 1;
 
    if (allocate)
@@ -558,13 +590,13 @@ void brw_emit_point_sprite_setup( struct brw_sf_compile *c, GLboolean allocate)
    {
       struct brw_reg a0 = offset(c->vert[0], i);
       GLushort pc, pc_persp, pc_linear, pc_coord_replace;
-      GLboolean last = calculate_masks(c, i, &pc, &pc_persp, &pc_linear);
+      bool last = calculate_masks(c, i, &pc, &pc_persp, &pc_linear);
 
       pc_coord_replace = calculate_point_sprite_mask(c, i);
       pc_persp &= ~pc_coord_replace;
 
       if (pc_persp) {
-        brw_set_predicate_control_flag_value(p, pc_persp);
+        set_predicate_control_flag_value(p, c, pc_persp);
         brw_MUL(p, a0, a0, c->inv_w[0]);
       }
 
@@ -574,18 +606,16 @@ void brw_emit_point_sprite_setup( struct brw_sf_compile *c, GLboolean allocate)
        * point.
        */
       if (pc_coord_replace) {
-        brw_set_predicate_control_flag_value(p, pc_coord_replace);
+        set_predicate_control_flag_value(p, c, pc_coord_replace);
         /* Caculate 1.0/PointWidth */
-        brw_math(&c->func,
-                 c->tmp,
-                 BRW_MATH_FUNCTION_INV,
-                 BRW_MATH_SATURATE_NONE,
-                 0,
-                 c->dx0,
-                 BRW_MATH_DATA_SCALAR,
-                 BRW_MATH_PRECISION_FULL);
+        gen4_math(&c->func,
+                  c->tmp,
+                  BRW_MATH_FUNCTION_INV,
+                  0,
+                  c->dx0,
+                  BRW_MATH_PRECISION_FULL);
 
-        brw_set_access_mode(p, BRW_ALIGN_16);
+        brw_set_default_access_mode(p, BRW_ALIGN_16);
 
         /* dA/dx, dA/dy */
         brw_MOV(p, c->m1Cx, brw_imm_f(0.0));
@@ -605,44 +635,45 @@ void brw_emit_point_sprite_setup( struct brw_sf_compile *c, GLboolean allocate)
            brw_MOV(p, brw_writemask(c->m3C0, WRITEMASK_W), brw_imm_f(1.0));
         }
 
-        brw_set_access_mode(p, BRW_ALIGN_1);
+        brw_set_default_access_mode(p, BRW_ALIGN_1);
       }
 
       if (pc & ~pc_coord_replace) {
-        brw_set_predicate_control_flag_value(p, pc & ~pc_coord_replace);
+        set_predicate_control_flag_value(p, c, pc & ~pc_coord_replace);
         brw_MOV(p, c->m1Cx, brw_imm_ud(0));
         brw_MOV(p, c->m2Cy, brw_imm_ud(0));
         brw_MOV(p, c->m3C0, a0); /* constant value */
       }
 
 
-      brw_set_predicate_control_flag_value(p, pc);
+      set_predicate_control_flag_value(p, c, pc);
       /* Copy m0..m3 to URB. */
       brw_urb_WRITE(p,
                    brw_null_reg(),
                    0,
                    brw_vec8_grf(0, 0),
-                   0,  /* allocate */
-                   1,  /* used */
+                    last ? BRW_URB_WRITE_EOT_COMPLETE
+                    : BRW_URB_WRITE_NO_FLAGS,
                    4,  /* msg len */
                    0,  /* response len */
-                   last,       /* eot */
-                   last,       /* writes complete */
                    i*4,        /* urb destination offset */
                    BRW_URB_SWIZZLE_TRANSPOSE);
    }
+
+   brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
 }
 
 /* Points setup - several simplifications as all attributes are
  * constant across the face of the point (point sprites excluded!)
  */
-void brw_emit_point_setup( struct brw_sf_compile *c, GLboolean allocate)
+void brw_emit_point_setup(struct brw_sf_compile *c, bool allocate)
 {
-   struct brw_compile *p = &c->func;
+   struct brw_codegen *p = &c->func;
    GLuint i;
 
+   c->flag_value = 0xff;
    c->nr_verts = 1;
-   
+
    if (allocate)
       alloc_regs(c);
 
@@ -655,14 +686,14 @@ void brw_emit_point_setup( struct brw_sf_compile *c, GLboolean allocate)
    {
       struct brw_reg a0 = offset(c->vert[0], i);
       GLushort pc, pc_persp, pc_linear;
-      GLboolean last = calculate_masks(c, i, &pc, &pc_persp, &pc_linear);
-            
+      bool last = calculate_masks(c, i, &pc, &pc_persp, &pc_linear);
+
       if (pc_persp)
       {                                
         /* This seems odd as the values are all constant, but the
          * fragment shader will be expecting it:
          */
-        brw_set_predicate_control_flag_value(p, pc_persp);
+        set_predicate_control_flag_value(p, c, pc_persp);
         brw_MUL(p, a0, a0, c->inv_w[0]);
       }
 
@@ -672,39 +703,36 @@ void brw_emit_point_setup( struct brw_sf_compile *c, GLboolean allocate)
        * code in the fragment shader.
        */
       {
-        brw_set_predicate_control_flag_value(p, pc); 
+        set_predicate_control_flag_value(p, c, pc);
 
         brw_MOV(p, c->m3C0, a0); /* constant value */
 
-        /* Copy m0..m3 to URB. 
+        /* Copy m0..m3 to URB.
          */
-        brw_urb_WRITE(p, 
+        brw_urb_WRITE(p,
                       brw_null_reg(),
                       0,
                       brw_vec8_grf(0, 0),
-                      0,       /* allocate */
-                      1,       /* used */
+                       last ? BRW_URB_WRITE_EOT_COMPLETE
+                       : BRW_URB_WRITE_NO_FLAGS,
                       4,       /* msg len */
                       0,       /* response len */
-                      last,    /* eot */
-                      last,    /* writes complete */
                       i*4,     /* urb destination offset */
                       BRW_URB_SWIZZLE_TRANSPOSE);
       }
    }
+
+   brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
 }
 
 void brw_emit_anyprim_setup( struct brw_sf_compile *c )
 {
-   struct brw_compile *p = &c->func;
-   struct brw_reg ip = brw_ip_reg();
+   struct brw_codegen *p = &c->func;
    struct brw_reg payload_prim = brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, 1, 0);
-   struct brw_reg payload_attr = get_element_ud(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE, 1, 0), 0); 
+   struct brw_reg payload_attr = get_element_ud(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE, 1, 0), 0);
    struct brw_reg primmask;
-   struct brw_instruction *jmp;
+   int jmp;
    struct brw_reg v1_null_ud = vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD));
-   
-   GLuint saveflag;
 
    c->nr_verts = 3;
    alloc_regs(c);
@@ -714,7 +742,6 @@ void brw_emit_anyprim_setup( struct brw_sf_compile *c )
    brw_MOV(p, primmask, brw_imm_ud(1));
    brw_SHL(p, primmask, primmask, payload_prim);
 
-   brw_set_conditionalmod(p, BRW_CONDITIONAL_Z);
    brw_AND(p, v1_null_ud, primmask, brw_imm_ud((1<<_3DPRIM_TRILIST) |
                                               (1<<_3DPRIM_TRISTRIP) |
                                               (1<<_3DPRIM_TRIFAN) |
@@ -722,51 +749,29 @@ void brw_emit_anyprim_setup( struct brw_sf_compile *c )
                                               (1<<_3DPRIM_POLYGON) |
                                               (1<<_3DPRIM_RECTLIST) |
                                               (1<<_3DPRIM_TRIFAN_NOSTIPPLE)));
-   jmp = brw_JMPI(p, ip, ip, brw_imm_d(0));
-   {
-      saveflag = p->flag_value;
-      brw_push_insn_state(p); 
-      brw_emit_tri_setup( c, GL_FALSE );
-      brw_pop_insn_state(p);
-      p->flag_value = saveflag;
-      /* note - thread killed in subroutine, so must
-       * restore the flag which is changed when building
-       * the subroutine. fix #13240
-       */
-   }
+   brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_Z);
+   jmp = brw_JMPI(p, brw_imm_d(0), BRW_PREDICATE_NORMAL) - p->store;
+   brw_emit_tri_setup(c, false);
    brw_land_fwd_jump(p, jmp);
 
-   brw_set_conditionalmod(p, BRW_CONDITIONAL_Z);
    brw_AND(p, v1_null_ud, primmask, brw_imm_ud((1<<_3DPRIM_LINELIST) |
                                               (1<<_3DPRIM_LINESTRIP) |
                                               (1<<_3DPRIM_LINELOOP) |
                                               (1<<_3DPRIM_LINESTRIP_CONT) |
                                               (1<<_3DPRIM_LINESTRIP_BF) |
                                               (1<<_3DPRIM_LINESTRIP_CONT_BF)));
-   jmp = brw_JMPI(p, ip, ip, brw_imm_d(0));
-   {
-      saveflag = p->flag_value;
-      brw_push_insn_state(p); 
-      brw_emit_line_setup( c, GL_FALSE );
-      brw_pop_insn_state(p);
-      p->flag_value = saveflag;
-      /* note - thread killed in subroutine */
-   }
-   brw_land_fwd_jump(p, jmp); 
+   brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_Z);
+   jmp = brw_JMPI(p, brw_imm_d(0), BRW_PREDICATE_NORMAL) - p->store;
+   brw_emit_line_setup(c, false);
+   brw_land_fwd_jump(p, jmp);
 
-   brw_set_conditionalmod(p, BRW_CONDITIONAL_Z);
    brw_AND(p, v1_null_ud, payload_attr, brw_imm_ud(1<<BRW_SPRITE_POINT_ENABLE));
-   jmp = brw_JMPI(p, ip, ip, brw_imm_d(0));
-   {
-      saveflag = p->flag_value;
-      brw_push_insn_state(p); 
-      brw_emit_point_sprite_setup( c, GL_FALSE );
-      brw_pop_insn_state(p);
-      p->flag_value = saveflag;
-   }
-   brw_land_fwd_jump(p, jmp); 
+   brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_Z);
+   jmp = brw_JMPI(p, brw_imm_d(0), BRW_PREDICATE_NORMAL) - p->store;
+   brw_emit_point_sprite_setup(c, false);
+   brw_land_fwd_jump(p, jmp);
 
-   brw_emit_point_setup( c, GL_FALSE );
+   brw_emit_point_setup( c, false );
 }