i965: Start adding the VS visitor and codegen.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vs_emit.c
index 7e43324a1f9c8e7b707d12b872682664f44d7a0f..a06a2bbec528e34a78f301919fd4a363dcc45423 100644 (file)
@@ -140,9 +140,13 @@ clear_current_const(struct brw_vs_compile *c)
 static void brw_vs_alloc_regs( struct brw_vs_compile *c )
 {
    struct intel_context *intel = &c->func.brw->intel;
-   GLuint i, reg = 0, mrf;
+   GLuint i, reg = 0, mrf, j;
    int attributes_in_vue;
    int first_reladdr_output;
+   int max_constant;
+   int constant = 0;
+   int vert_result_reoder[VERT_RESULT_MAX];
+   int bfc = 0;
 
    /* Determine whether to use a real constant buffer or use a block
     * of GRF registers for constants.  The later is faster but only
@@ -181,62 +185,81 @@ static void brw_vs_alloc_regs( struct brw_vs_compile *c )
 
    }
 
-   /* Vertex program parameters from curbe:
+   /* Assign some (probably all) of the vertex program constants to
+    * the push constant buffer/CURBE.
+    *
+    * There's an obvious limit to the numer of push constants equal to
+    * the number of register available, and that number is smaller
+    * than the minimum maximum number of vertex program parameters, so
+    * support for pull constants is required if we overflow.
+    * Additionally, on gen6 the number of push constants is even
+    * lower.
+    *
+    * When there's relative addressing, we don't know what range of
+    * Mesa IR registers can be accessed.  And generally, when relative
+    * addressing is used we also have too many constants to load them
+    * all as push constants.  So, we'll just support relative
+    * addressing out of the pull constant buffers, and try to load as
+    * many statically-accessed constants into the push constant buffer
+    * as we can.
     */
-   if (c->vp->use_const_buffer) {
-      int max_constant = BRW_MAX_GRF - 20 - c->vp->program.Base.NumTemporaries;
-      int constant = 0;
-
-      /* We've got more constants than we can load with the push
-       * mechanism.  This is often correlated with reladdr loads where
-       * we should probably be using a pull mechanism anyway to avoid
-       * excessive reading.  However, the pull mechanism is slow in
-       * general.  So, we try to allocate as many non-reladdr-loaded
-       * constants through the push buffer as we can before giving up.
-       */
-      memset(c->constant_map, -1, c->vp->program.Base.Parameters->NumParameters);
-      for (i = 0;
-          i < c->vp->program.Base.NumInstructions && constant < max_constant;
-          i++) {
-        struct prog_instruction *inst = &c->vp->program.Base.Instructions[i];
-        int arg;
-
-        for (arg = 0; arg < 3 && constant < max_constant; arg++) {
-           if ((inst->SrcReg[arg].File != PROGRAM_STATE_VAR &&
-                inst->SrcReg[arg].File != PROGRAM_CONSTANT &&
-                inst->SrcReg[arg].File != PROGRAM_UNIFORM &&
-                inst->SrcReg[arg].File != PROGRAM_ENV_PARAM &&
-                inst->SrcReg[arg].File != PROGRAM_LOCAL_PARAM) ||
-               inst->SrcReg[arg].RelAddr)
-              continue;
-
-           if (c->constant_map[inst->SrcReg[arg].Index] == -1) {
-              c->constant_map[inst->SrcReg[arg].Index] = constant++;
-           }
+   if (intel->gen >= 6) {
+      /* We can only load 32 regs of push constants. */
+      max_constant = 32 * 2 - c->key.nr_userclip;
+   } else {
+      max_constant = BRW_MAX_GRF - 20 - c->vp->program.Base.NumTemporaries;
+   }
+
+   /* constant_map maps from ParameterValues[] index to index in the
+    * push constant buffer, or -1 if it's only in the pull constant
+    * buffer.
+    */
+   memset(c->constant_map, -1, c->vp->program.Base.Parameters->NumParameters);
+   for (i = 0;
+       i < c->vp->program.Base.NumInstructions && constant < max_constant;
+       i++) {
+      struct prog_instruction *inst = &c->vp->program.Base.Instructions[i];
+      int arg;
+
+      for (arg = 0; arg < 3 && constant < max_constant; arg++) {
+        if (inst->SrcReg[arg].File != PROGRAM_STATE_VAR &&
+            inst->SrcReg[arg].File != PROGRAM_CONSTANT &&
+            inst->SrcReg[arg].File != PROGRAM_UNIFORM &&
+            inst->SrcReg[arg].File != PROGRAM_ENV_PARAM &&
+            inst->SrcReg[arg].File != PROGRAM_LOCAL_PARAM) {
+           continue;
+        }
+
+        if (inst->SrcReg[arg].RelAddr) {
+           c->vp->use_const_buffer = GL_TRUE;
+           continue;
         }
-      }
 
-      for (i = 0; i < constant; i++) {
-         c->regs[PROGRAM_STATE_VAR][i] = stride( brw_vec4_grf(reg+i/2,
-                                                             (i%2) * 4),
-                                                0, 4, 1);
+        if (c->constant_map[inst->SrcReg[arg].Index] == -1) {
+           c->constant_map[inst->SrcReg[arg].Index] = constant++;
+        }
       }
-      reg += (constant + 1) / 2;
-      c->prog_data.curb_read_length = reg - 1;
-      /* XXX 0 causes a bug elsewhere... */
-      c->prog_data.nr_params = MAX2(constant * 4, 4);
    }
-   else {
-      /* use a section of the GRF for constants */
-      GLuint nr_params = c->vp->program.Base.Parameters->NumParameters;
-      for (i = 0; i < nr_params; i++) {
-         c->regs[PROGRAM_STATE_VAR][i] = stride( brw_vec4_grf(reg+i/2, (i%2) * 4), 0, 4, 1);
-      }
-      reg += (nr_params + 1) / 2;
-      c->prog_data.curb_read_length = reg - 1;
 
-      c->prog_data.nr_params = nr_params * 4;
+   /* If we ran out of push constant space, then we'll also upload all
+    * constants through the pull constant buffer so that they can be
+    * accessed no matter what.  For relative addressing (the common
+    * case) we need them all in place anyway.
+    */
+   if (constant == max_constant)
+      c->vp->use_const_buffer = GL_TRUE;
+
+   for (i = 0; i < constant; i++) {
+      c->regs[PROGRAM_STATE_VAR][i] = stride(brw_vec4_grf(reg + i / 2,
+                                                         (i % 2) * 4),
+                                            0, 4, 1);
    }
+   reg += (constant + 1) / 2;
+   c->prog_data.curb_read_length = reg - 1;
+   c->prog_data.nr_params = constant * 4;
+   /* XXX 0 causes a bug elsewhere... */
+   if (intel->gen < 6 && c->prog_data.nr_params == 0)
+      c->prog_data.nr_params = 4;
 
    /* Allocate input regs:  
     */
@@ -270,7 +293,36 @@ static void brw_vs_alloc_regs( struct brw_vs_compile *c )
       mrf = 4;
 
    first_reladdr_output = get_first_reladdr_output(&c->vp->program);
-   for (i = 0; i < VERT_RESULT_MAX; i++) {
+
+   for (i = 0; i < VERT_RESULT_MAX; i++)
+       vert_result_reoder[i] = i;
+
+   /* adjust attribute order in VUE for BFC0/BFC1 on Gen6+ */
+   if (intel->gen >= 6 && c->key.two_side_color) {
+       if ((c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_COL1)) &&
+           (c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC1))) {
+           assert(c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_COL0));
+           assert(c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC0));
+           bfc = 2;
+       } else if ((c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_COL0)) &&
+           (c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC0)))
+           bfc = 1;
+
+       if (bfc) {
+           for (i = 0; i < bfc; i++) {
+               vert_result_reoder[VERT_RESULT_COL0 + i * 2 + 0] = VERT_RESULT_COL0 + i;
+               vert_result_reoder[VERT_RESULT_COL0 + i * 2 + 1] = VERT_RESULT_BFC0 + i;
+           }
+
+           for (i = VERT_RESULT_COL0 + bfc * 2; i < VERT_RESULT_BFC0 + bfc; i++) {
+               vert_result_reoder[i] = i - bfc;
+           }
+       }
+   }
+
+   for (j = 0; j < VERT_RESULT_MAX; j++) {
+      i = vert_result_reoder[j];
+
       if (c->prog_data.outputs_written & BITFIELD64_BIT(i)) {
         c->nr_outputs++;
          assert(i < Elements(c->regs[PROGRAM_OUTPUT]));
@@ -281,7 +333,6 @@ static void brw_vs_alloc_regs( struct brw_vs_compile *c )
         else if (i == VERT_RESULT_PSIZ) {
            c->regs[PROGRAM_OUTPUT][i] = brw_vec8_grf(reg, 0);
            reg++;
-           mrf++;              /* just a placeholder?  XXX fix later stages & remove this */
         }
         else {
            /* Two restrictions on our compute-to-MRF here.  The
@@ -381,13 +432,28 @@ static void brw_vs_alloc_regs( struct brw_vs_compile *c )
    /* See emit_vertex_write() for where the VUE's overhead on top of the
     * attributes comes from.
     */
-   if (intel->gen >= 6) {
+   if (intel->gen >= 7) {
+      int header_regs = 2;
+      if (c->key.nr_userclip)
+        header_regs += 2;
+
+      /* Each attribute is 16 bytes (1 vec4), so dividing by 4 gives us the
+       * number of 64-byte (512-bit) units.
+       */
+      c->prog_data.urb_entry_size = (attributes_in_vue + header_regs + 3) / 4;
+   } else if (intel->gen == 6) {
       int header_regs = 2;
       if (c->key.nr_userclip)
         header_regs += 2;
 
+      /* Each attribute is 16 bytes (1 vec4), so dividing by 8 gives us the
+       * number of 128-byte (1024-bit) units.
+       */
       c->prog_data.urb_entry_size = (attributes_in_vue + header_regs + 7) / 8;
    } else if (intel->gen == 5)
+      /* Each attribute is 16 bytes (1 vec4), so dividing by 4 gives us the
+       * number of 64-byte (512-bit) units.
+       */
       c->prog_data.urb_entry_size = (attributes_in_vue + 6 + 3) / 4;
    else
       c->prog_data.urb_entry_size = (attributes_in_vue + 2 + 3) / 4;
@@ -574,9 +640,18 @@ static void emit_max( struct brw_compile *p,
                      struct brw_reg arg0,
                      struct brw_reg arg1 )
 {
-   brw_CMP(p, brw_null_reg(), BRW_CONDITIONAL_GE, arg0, arg1);
-   brw_SEL(p, dst, arg0, arg1);
-   brw_set_predicate_control(p, BRW_PREDICATE_NONE);
+   struct intel_context *intel = &p->brw->intel;
+
+   if (intel->gen >= 6) {
+      brw_set_conditionalmod(p, BRW_CONDITIONAL_GE);
+      brw_SEL(p, dst, arg0, arg1);
+      brw_set_conditionalmod(p, BRW_CONDITIONAL_NONE);
+      brw_set_predicate_control(p, BRW_PREDICATE_NONE);
+   } else {
+      brw_CMP(p, brw_null_reg(), BRW_CONDITIONAL_GE, arg0, arg1);
+      brw_SEL(p, dst, arg0, arg1);
+      brw_set_predicate_control(p, BRW_PREDICATE_NONE);
+   }
 }
 
 static void emit_min( struct brw_compile *p, 
@@ -584,9 +659,34 @@ static void emit_min( struct brw_compile *p,
                      struct brw_reg arg0,
                      struct brw_reg arg1 )
 {
-   brw_CMP(p, brw_null_reg(), BRW_CONDITIONAL_L, arg0, arg1);
-   brw_SEL(p, dst, arg0, arg1);
-   brw_set_predicate_control(p, BRW_PREDICATE_NONE);
+   struct intel_context *intel = &p->brw->intel;
+
+   if (intel->gen >= 6) {
+      brw_set_conditionalmod(p, BRW_CONDITIONAL_L);
+      brw_SEL(p, dst, arg0, arg1);
+      brw_set_conditionalmod(p, BRW_CONDITIONAL_NONE);
+      brw_set_predicate_control(p, BRW_PREDICATE_NONE);
+   } else {
+      brw_CMP(p, brw_null_reg(), BRW_CONDITIONAL_L, arg0, arg1);
+      brw_SEL(p, dst, arg0, arg1);
+      brw_set_predicate_control(p, BRW_PREDICATE_NONE);
+   }
+}
+
+static void emit_arl(struct brw_compile *p,
+                    struct brw_reg dst,
+                    struct brw_reg src)
+{
+   struct intel_context *intel = &p->brw->intel;
+
+   if (intel->gen >= 6) {
+      struct brw_reg dst_f = retype(dst, BRW_REGISTER_TYPE_F);
+
+      brw_RNDD(p, dst_f, src);
+      brw_MOV(p, dst, dst_f);
+   } else {
+      brw_RNDD(p, dst, src);
+   }
 }
 
 static void emit_math1_gen4(struct brw_vs_compile *c,
@@ -680,7 +780,7 @@ emit_math1(struct brw_vs_compile *c,
       emit_math1_gen4(c, function, dst, arg0, precision);
 }
 
-static void emit_math2( struct brw_vs_compile *c, 
+static void emit_math2_gen4( struct brw_vs_compile *c, 
                        GLuint function,
                        struct brw_reg dst,
                        struct brw_reg arg0,
@@ -688,14 +788,11 @@ static void emit_math2( struct brw_vs_compile *c,
                        GLuint precision)
 {
    struct brw_compile *p = &c->func;
-   struct intel_context *intel = &p->brw->intel;
    struct brw_reg tmp = dst;
    GLboolean need_tmp = GL_FALSE;
 
-   if (dst.file != BRW_GENERAL_REGISTER_FILE)
-      need_tmp = GL_TRUE;
-
-   if (intel->gen < 6 && dst.dw1.bits.writemask != 0xf)
+   if (dst.file != BRW_GENERAL_REGISTER_FILE ||
+       dst.dw1.bits.writemask != 0xf)
       need_tmp = GL_TRUE;
 
    if (need_tmp) 
@@ -718,6 +815,53 @@ static void emit_math2( struct brw_vs_compile *c,
    }
 }
 
+static void emit_math2_gen6( struct brw_vs_compile *c, 
+                       GLuint function,
+                       struct brw_reg dst,
+                       struct brw_reg arg0,
+                       struct brw_reg arg1,
+                       GLuint precision)
+{
+   struct brw_compile *p = &c->func;
+   struct brw_reg tmp_src0, tmp_src1, tmp_dst;
+
+   tmp_src0 = get_tmp(c);
+   tmp_src1 = get_tmp(c);
+   tmp_dst = get_tmp(c);
+
+   brw_MOV(p, tmp_src0, arg0);
+   brw_MOV(p, tmp_src1, arg1);
+   
+   brw_set_access_mode(p, BRW_ALIGN_1);
+   brw_math2(p,
+           tmp_dst,
+           function,
+           tmp_src0,
+           tmp_src1);
+   brw_set_access_mode(p, BRW_ALIGN_16);
+
+   brw_MOV(p, dst, tmp_dst);
+
+   release_tmp(c, tmp_src0);
+   release_tmp(c, tmp_src1);
+   release_tmp(c, tmp_dst);
+}
+
+static void emit_math2( struct brw_vs_compile *c, 
+                       GLuint function,
+                       struct brw_reg dst,
+                       struct brw_reg arg0,
+                       struct brw_reg arg1,
+                       GLuint precision)
+{
+   struct brw_compile *p = &c->func;
+   struct intel_context *intel = &p->brw->intel;
+
+   if (intel->gen >= 6)
+      emit_math2_gen6(c, function, dst, arg0, arg1, precision);
+   else
+      emit_math2_gen4(c, function, dst, arg0, arg1, precision);
+}
 
 static void emit_exp_noalias( struct brw_vs_compile *c,
                              struct brw_reg dst,
@@ -904,7 +1048,6 @@ static void emit_lit_noalias( struct brw_vs_compile *c,
                              struct brw_reg arg0 )
 {
    struct brw_compile *p = &c->func;
-   struct brw_instruction *if_insn;
    struct brw_reg tmp = dst;
    GLboolean need_tmp = (dst.file != BRW_GENERAL_REGISTER_FILE);
 
@@ -920,7 +1063,7 @@ static void emit_lit_noalias( struct brw_vs_compile *c,
     * BRW_EXECUTE_1 for all comparisions.
     */
    brw_CMP(p, brw_null_reg(), BRW_CONDITIONAL_G, brw_swizzle1(arg0,0), brw_imm_f(0));
-   if_insn = brw_IF(p, BRW_EXECUTE_8);
+   brw_IF(p, BRW_EXECUTE_8);
    {
       brw_MOV(p, brw_writemask(dst, WRITEMASK_Y), brw_swizzle1(arg0,0));
 
@@ -935,8 +1078,7 @@ static void emit_lit_noalias( struct brw_vs_compile *c,
                 brw_swizzle1(arg0, 3),
                 BRW_MATH_PRECISION_PARTIAL);      
    }
-
-   brw_ENDIF(p, if_insn);
+   brw_ENDIF(p);
 
    release_tmp(c, tmp);
 }
@@ -990,8 +1132,6 @@ get_constant(struct brw_vs_compile *c,
 
    assert(argIndex < 3);
 
-   assert(c->func.brw->intel.gen < 6); /* FINISHME */
-
    if (c->current_const[argIndex].index != src->Index) {
       /* Keep track of the last constant loaded in this slot, for reuse. */
       c->current_const[argIndex].index = src->Index;
@@ -1009,7 +1149,7 @@ get_constant(struct brw_vs_compile *c,
    }
 
    /* replicate lower four floats into upper half (to get XYZWXYZW) */
-   const_reg = stride(const_reg, 0, 4, 0);
+   const_reg = stride(const_reg, 0, 4, 1);
    const_reg.subnr = 0;
 
    return const_reg;
@@ -1022,14 +1162,14 @@ get_reladdr_constant(struct brw_vs_compile *c,
 {
    const struct prog_src_register *src = &inst->SrcReg[argIndex];
    struct brw_compile *p = &c->func;
+   struct brw_context *brw = p->brw;
+   struct intel_context *intel = &brw->intel;
    struct brw_reg const_reg = c->current_const[argIndex].reg;
-   struct brw_reg addrReg = c->regs[PROGRAM_ADDRESS][0];
-   struct brw_reg byte_addr_reg = retype(get_tmp(c), BRW_REGISTER_TYPE_D);
+   struct brw_reg addr_reg = c->regs[PROGRAM_ADDRESS][0];
+   uint32_t offset;
 
    assert(argIndex < 3);
 
-   assert(c->func.brw->intel.gen < 6); /* FINISHME */
-
    /* Can't reuse a reladdr constant load. */
    c->current_const[argIndex].index = -1;
 
@@ -1038,15 +1178,21 @@ get_reladdr_constant(struct brw_vs_compile *c,
          src->Index, argIndex, c->current_const[argIndex].reg.nr);
 #endif
 
-   brw_MUL(p, byte_addr_reg, addrReg, brw_imm_ud(16));
+   if (intel->gen >= 6) {
+      offset = src->Index;
+   } else {
+      struct brw_reg byte_addr_reg = retype(get_tmp(c), BRW_REGISTER_TYPE_D);
+      brw_MUL(p, byte_addr_reg, addr_reg, brw_imm_d(16));
+      addr_reg = byte_addr_reg;
+      offset = 16 * src->Index;
+   }
 
    /* fetch the first vec4 */
    brw_dp_READ_4_vs_relative(p,
-                            const_reg,                     /* writeback dest */
-                            byte_addr_reg,                 /* address register */
-                            16 * src->Index,               /* byte offset */
-                            SURF_INDEX_VERT_CONST_BUFFER   /* binding table index */
-                            );
+                            const_reg,
+                            addr_reg,
+                            offset,
+                            SURF_INDEX_VERT_CONST_BUFFER);
 
    return const_reg;
 }
@@ -1213,7 +1359,7 @@ get_src_reg( struct brw_vs_compile *c,
 
         if (component >= 0) {
            params = c->vp->program.Base.Parameters;
-           f = params->ParameterValues[src->Index][component];
+           f = params->ParameterValues[src->Index][component].f;
 
            if (src->Abs)
               f = fabs(f);
@@ -1241,22 +1387,18 @@ get_src_reg( struct brw_vs_compile *c,
    case PROGRAM_UNIFORM:
    case PROGRAM_ENV_PARAM:
    case PROGRAM_LOCAL_PARAM:
-      if (c->vp->use_const_buffer) {
-        if (!relAddr && c->constant_map[index] != -1) {
-           assert(c->regs[PROGRAM_STATE_VAR][c->constant_map[index]].nr != 0);
-           return c->regs[PROGRAM_STATE_VAR][c->constant_map[index]];
-        } else if (relAddr)
+      if (!relAddr && c->constant_map[index] != -1) {
+        /* Take from the push constant buffer if possible. */
+        assert(c->regs[PROGRAM_STATE_VAR][c->constant_map[index]].nr != 0);
+        return c->regs[PROGRAM_STATE_VAR][c->constant_map[index]];
+      } else {
+        /* Must be in the pull constant buffer then .*/
+        assert(c->vp->use_const_buffer);
+        if (relAddr)
            return get_reladdr_constant(c, inst, argIndex);
         else
            return get_constant(c, inst, argIndex);
       }
-      else if (relAddr) {
-         return deref(c, c->regs[PROGRAM_STATE_VAR][0], index, 16);
-      }
-      else {
-         assert(c->regs[PROGRAM_STATE_VAR][index].nr != 0);
-         return c->regs[PROGRAM_STATE_VAR][index];
-      }
    case PROGRAM_ADDRESS:
       assert(index == 0);
       return c->regs[file][index];
@@ -1297,11 +1439,10 @@ static struct brw_reg get_arg( struct brw_vs_compile *c,
                                          GET_SWZ(src->Swizzle, 1),
                                          GET_SWZ(src->Swizzle, 2),
                                          GET_SWZ(src->Swizzle, 3));
-   }
 
-   /* Note this is ok for non-swizzle instructions: 
-    */
-   reg.negate = src->Negate ? 1 : 0;   
+      /* Note this is ok for non-swizzle ARB_vp instructions */
+      reg.negate = src->Negate ? 1 : 0;
+   }
 
    return reg;
 }
@@ -1419,6 +1560,26 @@ static void emit_swz( struct brw_vs_compile *c,
    }
 }
 
+static int
+align_interleaved_urb_mlen(struct brw_context *brw, int mlen)
+{
+   struct intel_context *intel = &brw->intel;
+
+   if (intel->gen >= 6) {
+      /* URB data written (does not include the message header reg) must
+       * be a multiple of 256 bits, or 2 VS registers.  See vol5c.5,
+       * section 5.4.3.2.2: URB_INTERLEAVED.
+       *
+       * URB entries are allocated on a multiple of 1024 bits, so an
+       * extra 128 bits written here to make the end align to 256 is
+       * no problem.
+       */
+      if ((mlen % 2) != 1)
+        mlen++;
+   }
+
+   return mlen;
+}
 
 /**
  * Post-vertex-program processing.  Send the results to the URB.
@@ -1433,6 +1594,7 @@ static void emit_vertex_write( struct brw_vs_compile *c)
    int eot;
    GLuint len_vertex_header = 2;
    int next_mrf, i;
+   int msg_len;
 
    if (c->key.copy_edgeflag) {
       brw_MOV(p, 
@@ -1473,7 +1635,7 @@ static void emit_vertex_write( struct brw_vs_compile *c)
            else
               m = brw_message_reg(4);
 
-           brw_DP4(p, brw_writemask(m, (1 << (i & 7))),pos, c->userplane[i]);
+           brw_DP4(p, brw_writemask(m, (1 << (i & 3))),pos, c->userplane[i]);
         }
       }
    } else if ((c->prog_data.outputs_written &
@@ -1585,6 +1747,8 @@ static void emit_vertex_write( struct brw_vs_compile *c)
         break;
       if (!(c->prog_data.outputs_written & BITFIELD64_BIT(i)))
         continue;
+      if (i == VERT_RESULT_PSIZ)
+        continue;
 
       if (i >= VERT_RESULT_TEX0 &&
          c->regs[PROGRAM_OUTPUT][i].file == BRW_GENERAL_REGISTER_FILE) {
@@ -1597,13 +1761,19 @@ static void emit_vertex_write( struct brw_vs_compile *c)
 
    eot = (c->first_overflow_output == 0);
 
+   /* Message header, plus VUE header, plus the (first set of) outputs. */
+   msg_len = 1 + len_vertex_header + c->nr_outputs;
+   msg_len = align_interleaved_urb_mlen(brw, msg_len);
+   /* Any outputs beyond BRW_MAX_MRF should be past first_overflow_output */
+   msg_len = MIN2(msg_len, (BRW_MAX_MRF - 1)),
+
    brw_urb_WRITE(p, 
                 brw_null_reg(), /* dest */
                 0,             /* starting mrf reg nr */
                 c->r0,         /* src */
                 0,             /* allocate */
                 1,             /* used */
-                MIN2(c->nr_outputs + 1 + len_vertex_header, (BRW_MAX_MRF-1)), /* msg len */
+                msg_len,
                 0,             /* response len */
                 eot,           /* eot */
                 eot,           /* writes complete */
@@ -1630,7 +1800,7 @@ static void emit_vertex_write( struct brw_vs_compile *c)
                     c->r0,          /* src */
                     0,              /* allocate */
                     1,              /* used */
-                    mrf,            /* msg len */
+                    align_interleaved_urb_mlen(brw, mrf),
                     0,              /* response len */
                     1,              /* eot */
                     1,              /* writes complete */
@@ -1651,6 +1821,9 @@ accumulator_contains(struct brw_vs_compile *c, struct brw_reg val)
    if (val.address_mode != BRW_ADDRESS_DIRECT)
       return GL_FALSE;
 
+   if (val.negate || val.abs)
+      return GL_FALSE;
+
    switch (prev_insn->header.opcode) {
    case BRW_OPCODE_MOV:
    case BRW_OPCODE_MAC:
@@ -1708,9 +1881,29 @@ get_predicate(const struct prog_instruction *inst)
    }
 }
 
+static void
+brw_vs_rescale_gl_fixed(struct brw_vs_compile *c)
+{
+   struct brw_compile *p = &c->func;
+   int i;
+
+   for (i = 0; i < VERT_ATTRIB_MAX; i++) {
+      if (!(c->prog_data.inputs_read & (1 << i)))
+        continue;
+
+      if (c->key.gl_fixed_input_size[i] != 0) {
+        struct brw_reg reg = c->regs[PROGRAM_INPUT][i];
+
+        brw_MUL(p,
+                brw_writemask(reg, (1 << c->key.gl_fixed_input_size[i]) - 1),
+                reg, brw_imm_f(1.0 / 65536.0));
+      }
+   }
+}
+
 /* Emit the vertex program instructions here.
  */
-void brw_vs_emit(struct brw_vs_compile *c )
+void brw_old_vs_emit(struct brw_vs_compile *c )
 {
 #define MAX_IF_DEPTH 32
 #define MAX_LOOP_DEPTH 32
@@ -1718,8 +1911,8 @@ void brw_vs_emit(struct brw_vs_compile *c )
    struct brw_context *brw = p->brw;
    struct intel_context *intel = &brw->intel;
    const GLuint nr_insns = c->vp->program.Base.NumInstructions;
-   GLuint insn, if_depth = 0, loop_depth = 0;
-   struct brw_instruction *if_inst[MAX_IF_DEPTH], *loop_inst[MAX_LOOP_DEPTH] = { 0 };
+   GLuint insn, loop_depth = 0;
+   struct brw_instruction *loop_inst[MAX_LOOP_DEPTH] = { 0 };
    int if_depth_in_loop[MAX_LOOP_DEPTH];
    const struct brw_indirect stack_index = brw_indirect(0, 0);   
    GLuint index;
@@ -1767,6 +1960,8 @@ void brw_vs_emit(struct brw_vs_compile *c )
     */
    brw_vs_alloc_regs(c);
 
+   brw_vs_rescale_gl_fixed(c);
+
    if (c->needs_stack)
       brw_MOV(p, get_addr_reg(stack_index), brw_address(c->stack));
 
@@ -1788,9 +1983,22 @@ void brw_vs_emit(struct brw_vs_compile *c )
              const struct prog_src_register *src = &inst->SrcReg[i];
              index = src->Index;
              file = src->File; 
-             if (file == PROGRAM_OUTPUT && c->output_regs[index].used_in_src)
-                 args[i] = c->output_regs[index].reg;
-             else
+             if (file == PROGRAM_OUTPUT && c->output_regs[index].used_in_src) {
+                /* Can't just make get_arg "do the right thing" here because
+                 * other callers of get_arg and get_src_reg don't expect any
+                 * special behavior for the c->output_regs[index].used_in_src
+                 * case.
+                 */
+                args[i] = c->output_regs[index].reg;
+                args[i].dw1.bits.swizzle =
+                   BRW_SWIZZLE4(GET_SWZ(src->Swizzle, 0),
+                                GET_SWZ(src->Swizzle, 1),
+                                GET_SWZ(src->Swizzle, 2),
+                                GET_SWZ(src->Swizzle, 3));
+
+                /* Note this is ok for non-swizzle ARB_vp instructions */
+                args[i].negate = src->Negate ? 1 : 0;
+             } else
                   args[i] = get_arg(c, inst, i);
          }
 
@@ -1801,7 +2009,11 @@ void brw_vs_emit(struct brw_vs_compile *c )
       index = inst->DstReg.Index;
       file = inst->DstReg.File;
       if (file == PROGRAM_OUTPUT && c->output_regs[index].used_in_src)
-         dst = c->output_regs[index].reg;
+        /* Can't just make get_dst "do the right thing" here because other
+         * callers of get_dst don't expect any special behavior for the
+         * c->output_regs[index].used_in_src case.
+         */
+        dst = brw_writemask(c->output_regs[index].reg, inst->DstReg.WriteMask);
       else
          dst = get_dst(c, inst->DstReg);
 
@@ -1812,6 +2024,7 @@ void brw_vs_emit(struct brw_vs_compile *c )
 
       switch (inst->Opcode) {
       case OPCODE_ABS:
+        args[0].negate = false;
         brw_MOV(p, dst, brw_abs(args[0]));
         break;
       case OPCODE_ADD:
@@ -1848,7 +2061,7 @@ void brw_vs_emit(struct brw_vs_compile *c )
         emit_math1(c, BRW_MATH_FUNCTION_EXP, dst, args[0], BRW_MATH_PRECISION_FULL);
         break;
       case OPCODE_ARL:
-        brw_RNDD(p, dst, args[0]);
+        emit_arl(p, dst, args[0]);
         break;
       case OPCODE_FLR:
         brw_RNDD(p, dst, args[0]);
@@ -1895,7 +2108,7 @@ void brw_vs_emit(struct brw_vs_compile *c )
         emit_math1(c, BRW_MATH_FUNCTION_INV, dst, args[0], BRW_MATH_PRECISION_FULL);
         break;
       case OPCODE_RSQ:
-        emit_math1(c, BRW_MATH_FUNCTION_RSQ, dst, args[0], BRW_MATH_PRECISION_FULL);
+        emit_math1(c, BRW_MATH_FUNCTION_RSQ, dst, brw_abs(args[0]), BRW_MATH_PRECISION_FULL);
         break;
 
       case OPCODE_SEQ:
@@ -1938,23 +2151,20 @@ void brw_vs_emit(struct brw_vs_compile *c )
       case OPCODE_XPD:
         emit_xpd(p, dst, args[0], args[1]);
         break;
-      case OPCODE_IF:
-        assert(if_depth < MAX_IF_DEPTH);
-        if_inst[if_depth] = brw_IF(p, BRW_EXECUTE_8);
+      case OPCODE_IF: {
+        struct brw_instruction *if_inst = brw_IF(p, BRW_EXECUTE_8);
         /* Note that brw_IF smashes the predicate_control field. */
-        if_inst[if_depth]->header.predicate_control = get_predicate(inst);
+        if_inst->header.predicate_control = get_predicate(inst);
         if_depth_in_loop[loop_depth]++;
-        if_depth++;
         break;
+      }
       case OPCODE_ELSE:
         clear_current_const(c);
-        assert(if_depth > 0);
-        if_inst[if_depth-1] = brw_ELSE(p, if_inst[if_depth-1]);
+        brw_ELSE(p);
         break;
       case OPCODE_ENDIF:
         clear_current_const(c);
-         assert(if_depth > 0);
-        brw_ENDIF(p, if_inst[--if_depth]);
+        brw_ENDIF(p);
         if_depth_in_loop[loop_depth]--;
         break;                 
       case OPCODE_BGNLOOP:
@@ -1969,35 +2179,42 @@ void brw_vs_emit(struct brw_vs_compile *c )
          break;
       case OPCODE_CONT:
         brw_set_predicate_control(p, get_predicate(inst));
-        brw_CONT(p, if_depth_in_loop[loop_depth]);
+        if (intel->gen >= 6) {
+           gen6_CONT(p, loop_inst[loop_depth - 1]);
+        } else {
+           brw_CONT(p, if_depth_in_loop[loop_depth]);
+        }
          brw_set_predicate_control(p, BRW_PREDICATE_NONE);
          break;
-      case OPCODE_ENDLOOP: 
-         {
-           clear_current_const(c);
-            struct brw_instruction *inst0, *inst1;
-           GLuint br = 1;
-
-            loop_depth--;
-
-           if (intel->gen == 5)
-              br = 2;
-
-            inst0 = inst1 = brw_WHILE(p, loop_inst[loop_depth]);
-            /* patch all the BREAK/CONT instructions from last BEGINLOOP */
-            while (inst0 > loop_inst[loop_depth]) {
-               inst0--;
-               if (inst0->header.opcode == BRW_OPCODE_BREAK &&
+
+      case OPCODE_ENDLOOP: {
+        clear_current_const(c);
+        struct brw_instruction *inst0, *inst1;
+        GLuint br = 1;
+
+        loop_depth--;
+
+        if (intel->gen == 5)
+           br = 2;
+
+        inst0 = inst1 = brw_WHILE(p, loop_inst[loop_depth]);
+
+        if (intel->gen < 6) {
+           /* patch all the BREAK/CONT instructions from last BEGINLOOP */
+           while (inst0 > loop_inst[loop_depth]) {
+              inst0--;
+              if (inst0->header.opcode == BRW_OPCODE_BREAK &&
                   inst0->bits3.if_else.jump_count == 0) {
-                  inst0->bits3.if_else.jump_count = br * (inst1 - inst0 + 1);
-               }
-               else if (inst0->header.opcode == BRW_OPCODE_CONTINUE &&
-                       inst0->bits3.if_else.jump_count == 0) {
-                  inst0->bits3.if_else.jump_count = br * (inst1 - inst0);
-               }
-            }
-         }
+                 inst0->bits3.if_else.jump_count = br * (inst1 - inst0 + 1);
+              } else if (inst0->header.opcode == BRW_OPCODE_CONTINUE &&
+                         inst0->bits3.if_else.jump_count == 0) {
+                 inst0->bits3.if_else.jump_count = br * (inst1 - inst0);
+              }
+           }
+        }
+      }
          break;
+
       case OPCODE_BRA:
         brw_set_predicate_control(p, get_predicate(inst));
          brw_ADD(p, brw_ip_reg(), brw_ip_reg(), brw_imm_d(1*16));
@@ -2069,7 +2286,8 @@ void brw_vs_emit(struct brw_vs_compile *c )
        * instructions. Instead, we directly modify the header
        * of the last (already stored) instruction.
        */
-      if (inst->DstReg.File == PROGRAM_OUTPUT) {
+      if (inst->DstReg.File == PROGRAM_OUTPUT &&
+         c->key.clamp_vertex_color) {
          if ((inst->DstReg.Index == VERT_RESULT_COL0)
              || (inst->DstReg.Index == VERT_RESULT_COL1)
              || (inst->DstReg.Index == VERT_RESULT_BFC0)
@@ -2088,6 +2306,7 @@ void brw_vs_emit(struct brw_vs_compile *c )
    }
 
    brw_resolve_cals(p);
+   brw_set_uip_jip(p);
 
    brw_optimize(p);