NV30/NV40 CMP and SCS src == dst handling
[mesa.git] / src / gallium / drivers / nv50 / nv50_program.c
index 576d075318f9b187bc8ba7a3cb5ebaafa7dcddf9..b9910b430a16aaeb6d72673d875e0cd3b0942354 100644 (file)
 
 #include "nv50_context.h"
 
-#define NV50_SU_MAX_TEMP 64
+#define NV50_SU_MAX_TEMP 127
+#define NV50_SU_MAX_ADDR 4
 //#define NV50_PROGRAM_DUMP
 
+/* $a5 and $a6 always seem to be 0, and using $a7 gives you noise */
+
 /* ARL - gallium craps itself on progs/vp/arl.txt
  *
  * MSB - Like MAD, but MUL+SUB
@@ -79,26 +82,40 @@ struct nv50_reg {
                P_ATTR,
                P_RESULT,
                P_CONST,
-               P_IMMD
+               P_IMMD,
+               P_ADDR
        } type;
        int index;
 
        int hw;
-       int neg;
+       int mod;
 
        int rhw; /* result hw for FP outputs, or interpolant index */
        int acc; /* instruction where this reg is last read (first insn == 1) */
 };
 
-/* arbitrary limits */
-#define MAX_IF_DEPTH 4
-#define MAX_LOOP_DEPTH 4
+#define NV50_MOD_NEG 1
+#define NV50_MOD_ABS 2
+#define NV50_MOD_SAT 4
+
+/* STACK: Conditionals and loops have to use the (per warp) stack.
+ * Stack entries consist of an entry type (divergent path, join at),
+ * a mask indicating the active threads of the warp, and an address.
+ * MPs can store 12 stack entries internally, if we need more (and
+ * we probably do), we have to create a stack buffer in VRAM.
+ */
+/* impose low limits for now */
+#define NV50_MAX_COND_NESTING 4
+#define NV50_MAX_LOOP_NESTING 3
+
+#define JOIN_ON(e) e; pc->p->exec_tail->inst[1] |= 2
 
 struct nv50_pc {
        struct nv50_program *p;
 
        /* hw resources */
        struct nv50_reg *r_temp[NV50_SU_MAX_TEMP];
+       struct nv50_reg r_addr[NV50_SU_MAX_ADDR];
 
        /* tgsi resources */
        struct nv50_reg *temp;
@@ -110,8 +127,11 @@ struct nv50_pc {
        struct nv50_reg *param;
        int param_nr;
        struct nv50_reg *immd;
-       float *immd_buf;
+       uint32_t *immd_buf;
        int immd_nr;
+       struct nv50_reg **addr;
+       int addr_nr;
+       uint8_t addr_alloc; /* set bit indicates used for TGSI_FILE_ADDRESS */
 
        struct nv50_reg *temp_temp[16];
        unsigned temp_temp_nr;
@@ -120,32 +140,50 @@ struct nv50_pc {
        struct nv50_reg *r_brdc;
        struct nv50_reg *r_dst[4];
 
+       struct nv50_reg reg_instances[16];
+       unsigned reg_instance_nr;
+
        unsigned interp_mode[32];
        /* perspective interpolation registers */
        struct nv50_reg *iv_p;
        struct nv50_reg *iv_c;
 
-       struct nv50_program_exec *if_cond;
-       struct nv50_program_exec *if_insn[MAX_IF_DEPTH];
-       struct nv50_program_exec *br_join[MAX_IF_DEPTH];
-       struct nv50_program_exec *br_loop[MAX_LOOP_DEPTH]; /* for BRK branch */
+       struct nv50_program_exec *if_insn[NV50_MAX_COND_NESTING];
+       struct nv50_program_exec *if_join[NV50_MAX_COND_NESTING];
+       struct nv50_program_exec *loop_brka[NV50_MAX_LOOP_NESTING];
        int if_lvl, loop_lvl;
-       unsigned loop_pos[MAX_LOOP_DEPTH];
+       unsigned loop_pos[NV50_MAX_LOOP_NESTING];
 
        /* current instruction and total number of insns */
        unsigned insn_cur;
        unsigned insn_nr;
 
        boolean allow32;
+
+       uint8_t edgeflag_out;
 };
 
+static INLINE struct nv50_reg *
+reg_instance(struct nv50_pc *pc, struct nv50_reg *reg)
+{
+       struct nv50_reg *ri;
+
+       assert(pc->reg_instance_nr < 16);
+       ri = &pc->reg_instances[pc->reg_instance_nr++];
+       if (reg) {
+               *ri = *reg;
+               reg->mod = 0;
+       }
+       return ri;
+}
+
 static INLINE void
 ctor_reg(struct nv50_reg *reg, unsigned type, int index, int hw)
 {
        reg->type = type;
        reg->index = index;
        reg->hw = hw;
-       reg->neg = 0;
+       reg->mod = 0;
        reg->rhw = -1;
        reg->acc = 0;
 }
@@ -158,6 +196,16 @@ popcnt4(uint32_t val)
        return cnt[val & 0xf];
 }
 
+static void
+terminate_mbb(struct nv50_pc *pc)
+{
+       int i;
+
+       /* remove records of temporary address register values */
+       for (i = 0; i < NV50_SU_MAX_ADDR; ++i)
+               pc->r_addr[i].rhw = -1;
+}
+
 static void
 alloc_reg(struct nv50_pc *pc, struct nv50_reg *reg)
 {
@@ -320,25 +368,34 @@ static void
 kill_temp_temp(struct nv50_pc *pc)
 {
        int i;
-       
+
        for (i = 0; i < pc->temp_temp_nr; i++)
                free_temp(pc, pc->temp_temp[i]);
        pc->temp_temp_nr = 0;
 }
 
 static int
-ctor_immd(struct nv50_pc *pc, float x, float y, float z, float w)
+ctor_immd_4u32(struct nv50_pc *pc,
+              uint32_t x, uint32_t y, uint32_t z, uint32_t w)
 {
-       pc->immd_buf = REALLOC(pc->immd_buf, (pc->immd_nr * 4 * sizeof(float)),
-                              (pc->immd_nr + 1) * 4 * sizeof(float));
+       unsigned size = pc->immd_nr * 4 * sizeof(uint32_t);
+
+       pc->immd_buf = REALLOC(pc->immd_buf, size, size + 4 * sizeof(uint32_t));
+
        pc->immd_buf[(pc->immd_nr * 4) + 0] = x;
        pc->immd_buf[(pc->immd_nr * 4) + 1] = y;
        pc->immd_buf[(pc->immd_nr * 4) + 2] = z;
        pc->immd_buf[(pc->immd_nr * 4) + 3] = w;
-       
+
        return pc->immd_nr++;
 }
 
+static INLINE int
+ctor_immd_4f32(struct nv50_pc *pc, float x, float y, float z, float w)
+{
+       return ctor_immd_4u32(pc, fui(x), fui(y), fui(z), fui(w));
+}
+
 static struct nv50_reg *
 alloc_immd(struct nv50_pc *pc, float f)
 {
@@ -346,11 +403,11 @@ alloc_immd(struct nv50_pc *pc, float f)
        unsigned hw;
 
        for (hw = 0; hw < pc->immd_nr * 4; hw++)
-               if (pc->immd_buf[hw] == f)
+               if (pc->immd_buf[hw] == fui(f))
                        break;
 
        if (hw == pc->immd_nr * 4)
-               hw = ctor_immd(pc, f, -f, 0.5 * f, 0) * 4;
+               hw = ctor_immd_4f32(pc, f, -f, 0.5 * f, 0) * 4;
 
        ctor_reg(r, P_IMMD, -1, hw);
        return r;
@@ -434,29 +491,128 @@ set_dst(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_program_exec *e)
        }
 
        alloc_reg(pc, dst);
+       if (dst->hw > 63)
+               set_long(pc, e);
        e->inst[0] |= (dst->hw << 2);
 }
 
 static INLINE void
 set_immd(struct nv50_pc *pc, struct nv50_reg *imm, struct nv50_program_exec *e)
 {
-       float f = pc->immd_buf[imm->hw];
-       unsigned val = fui(imm->neg ? -f : f);
+       union {
+               float f;
+               uint32_t ui;
+       } u;
+       u.ui = pc->immd_buf[imm->hw];
+
+       u.f = (imm->mod & NV50_MOD_ABS) ? fabsf(u.f) : u.f;
+       u.f = (imm->mod & NV50_MOD_NEG) ? -u.f : u.f;
 
        set_long(pc, e);
-       /*XXX: can't be predicated - bits overlap.. catch cases where both
-        *     are required and avoid them. */
+       /* XXX: can't be predicated - bits overlap; cases where both
+        * are required should be avoided by using pc->allow32 */
        set_pred(pc, 0, 0, e);
        set_pred_wr(pc, 0, 0, e);
 
        e->inst[1] |= 0x00000002 | 0x00000001;
-       e->inst[0] |= (val & 0x3f) << 16;
-       e->inst[1] |= (val >> 6) << 2;
+       e->inst[0] |= (u.ui & 0x3f) << 16;
+       e->inst[1] |= (u.ui >> 6) << 2;
+}
+
+static INLINE void
+set_addr(struct nv50_program_exec *e, struct nv50_reg *a)
+{
+       assert(!(e->inst[0] & 0x0c000000));
+       assert(!(e->inst[1] & 0x00000004));
+
+       e->inst[0] |= (a->hw & 3) << 26;
+       e->inst[1] |= (a->hw >> 2) << 2;
 }
 
+static void
+emit_add_addr_imm(struct nv50_pc *pc, struct nv50_reg *dst,
+                 struct nv50_reg *src0, uint16_t src1_val)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0xd0000000 | (src1_val << 9);
+       e->inst[1] = 0x20000000;
+       set_long(pc, e);
+       e->inst[0] |= dst->hw << 2;
+       if (src0) /* otherwise will add to $a0, which is always 0 */
+               set_addr(e, src0);
+
+       emit(pc, e);
+}
+
+static struct nv50_reg *
+alloc_addr(struct nv50_pc *pc, struct nv50_reg *ref)
+{
+       struct nv50_reg *a_tgsi = NULL, *a = NULL;
+       int i;
+       uint8_t avail = ~pc->addr_alloc;
+
+       if (!ref) {
+               /* allocate for TGSI_FILE_ADDRESS */
+               while (avail) {
+                       i = ffs(avail) - 1;
+
+                       if (pc->r_addr[i].rhw < 0 ||
+                           pc->r_addr[i].acc != pc->insn_cur) {
+                               pc->addr_alloc |= (1 << i);
+
+                               pc->r_addr[i].rhw = -1;
+                               pc->r_addr[i].index = i;
+                               return &pc->r_addr[i];
+                       }
+                       avail &= ~(1 << i);
+               }
+               assert(0);
+               return NULL;
+       }
+
+       /* Allocate and set an address reg so we can access 'ref'.
+        *
+        * If and r_addr->index will be -1 or the hw index the value
+        * value in rhw is relative to. If rhw < 0, the reg has not
+        * been initialized or is in use for TGSI_FILE_ADDRESS.
+        */
+       while (avail) { /* only consider regs that are not TGSI */
+               i = ffs(avail) - 1;
+               avail &= ~(1 << i);
+
+               if ((!a || a->rhw >= 0) && pc->r_addr[i].rhw < 0) {
+                       /* prefer an usused reg with low hw index */
+                       a = &pc->r_addr[i];
+                       continue;
+               }
+               if (!a && pc->r_addr[i].acc != pc->insn_cur)
+                       a = &pc->r_addr[i];
+
+               if (ref->hw - pc->r_addr[i].rhw >= 128)
+                       continue;
+
+               if ((ref->acc >= 0 && pc->r_addr[i].index < 0) ||
+                   (ref->acc < 0 && pc->r_addr[i].index == ref->index)) {
+                       pc->r_addr[i].acc = pc->insn_cur;
+                       return &pc->r_addr[i];
+               }
+       }
+       assert(a);
+
+       if (ref->acc < 0)
+               a_tgsi = pc->addr[ref->index];
+
+       emit_add_addr_imm(pc, a, a_tgsi, (ref->hw & ~0x7f) * 4);
+
+       a->rhw = ref->hw & ~0x7f;
+       a->acc = pc->insn_cur;
+       a->index = a_tgsi ? ref->index : -1;
+       return a;
+}
 
 #define INTERP_LINEAR          0
-#define INTERP_FLAT                    1
+#define INTERP_FLAT            1
 #define INTERP_PERSPECTIVE     2
 #define INTERP_CENTROID                4
 
@@ -494,10 +650,18 @@ set_data(struct nv50_pc *pc, struct nv50_reg *src, unsigned m, unsigned s,
 {
        set_long(pc, e);
 
-       e->param.index = src->hw;
+       e->param.index = src->hw & 127;
        e->param.shift = s;
        e->param.mask = m << (s % 32);
 
+       if (src->hw > 127)
+               set_addr(e, alloc_addr(pc, src));
+       else
+       if (src->acc < 0) {
+               assert(src->type == P_CONST);
+               set_addr(e, pc->addr[src->index]);
+       }
+
        e->inst[1] |= (((src->type == P_IMMD) ? 0 : 1) << 22);
 }
 
@@ -506,11 +670,13 @@ emit_mov(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
 {
        struct nv50_program_exec *e = exec(pc);
 
-       e->inst[0] |= 0x10000000;
+       e->inst[0] = 0x10000000;
+       if (!pc->allow32)
+               set_long(pc, e);
 
        set_dst(pc, dst, e);
 
-       if (pc->allow32 && dst->type != P_RESULT && src->type == P_IMMD) {
+       if (!is_long(e) && src->type == P_IMMD) {
                set_immd(pc, src, e);
                /*XXX: 32-bit, but steals part of "half" reg space - need to
                 *     catch and handle this case if/when we do half-regs
@@ -519,7 +685,7 @@ emit_mov(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
        if (src->type == P_IMMD || src->type == P_CONST) {
                set_long(pc, e);
                set_data(pc, src, 0x7f, 9, e);
-               e->inst[1] |= 0x20000000; /* src0 const? */
+               e->inst[1] |= 0x20000000; /* mov from c[] */
        } else {
                if (src->type == P_ATTR) {
                        set_long(pc, e);
@@ -527,14 +693,16 @@ emit_mov(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
                }
 
                alloc_reg(pc, src);
+               if (src->hw > 63)
+                       set_long(pc, e);
                e->inst[0] |= (src->hw << 9);
        }
 
        if (is_long(e) && !is_immd(e)) {
                e->inst[1] |= 0x04000000; /* 32-bit */
-               e->inst[1] |= 0x0000c000; /* "subsubop" 0x3 */
+               e->inst[1] |= 0x0000c000; /* 32-bit c[] load / lane mask 0:1 */
                if (!(e->inst[1] & 0x20000000))
-                       e->inst[1] |= 0x00030000; /* "subsubop" 0xf */
+                       e->inst[1] |= 0x00030000; /* lane mask 2:3 */
        } else
                e->inst[0] |= 0x00008000;
 
@@ -549,6 +717,17 @@ emit_mov_immdval(struct nv50_pc *pc, struct nv50_reg *dst, float f)
        FREE(imm);
 }
 
+static void
+emit_nop(struct nv50_pc *pc)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0xf0000000;
+       set_long(pc, e);
+       e->inst[1] = 0xe0000000;
+       emit(pc, e);
+}
+
 static boolean
 check_swap_src_0_1(struct nv50_pc *pc,
                   struct nv50_reg **s0, struct nv50_reg **s1)
@@ -586,6 +765,8 @@ set_src_0_restricted(struct nv50_pc *pc, struct nv50_reg *src,
        }
 
        alloc_reg(pc, src);
+       if (src->hw > 63)
+               set_long(pc, e);
        e->inst[0] |= (src->hw << 9);
 }
 
@@ -604,6 +785,8 @@ set_src_0(struct nv50_pc *pc, struct nv50_reg *src, struct nv50_program_exec *e)
        }
 
        alloc_reg(pc, src);
+       if (src->hw > 63)
+               set_long(pc, e);
        e->inst[0] |= (src->hw << 9);
 }
 
@@ -630,7 +813,9 @@ set_src_1(struct nv50_pc *pc, struct nv50_reg *src, struct nv50_program_exec *e)
        }
 
        alloc_reg(pc, src);
-       e->inst[0] |= (src->hw << 16);
+       if (src->hw > 63)
+               set_long(pc, e);
+       e->inst[0] |= ((src->hw & 127) << 16);
 }
 
 static void
@@ -658,7 +843,34 @@ set_src_2(struct nv50_pc *pc, struct nv50_reg *src, struct nv50_program_exec *e)
        }
 
        alloc_reg(pc, src);
-       e->inst[1] |= (src->hw << 14);
+       e->inst[1] |= ((src->hw & 127) << 14);
+}
+
+static void
+emit_mov_from_pred(struct nv50_pc *pc, struct nv50_reg *dst, int pred)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       assert(dst->type == P_TEMP);
+       e->inst[1] = 0x20000000 | (pred << 12);
+       set_long(pc, e);
+       set_dst(pc, dst, e);
+
+       emit(pc, e);
+}
+
+static void
+emit_mov_to_pred(struct nv50_pc *pc, int pred, struct nv50_reg *src)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0x000001fc;
+       e->inst[1] = 0xa0000008;
+       set_long(pc, e);
+       set_pred_wr(pc, 1, pred, e);
+       set_src_0_restricted(pc, src, e);
+
+       emit(pc, e);
 }
 
 static void
@@ -676,12 +888,12 @@ emit_mul(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
        set_dst(pc, dst, e);
        set_src_0(pc, src0, e);
        if (src1->type == P_IMMD && !is_long(e)) {
-               if (src0->neg)
+               if (src0->mod & NV50_MOD_NEG)
                        e->inst[0] |= 0x00008000;
                set_immd(pc, src1, e);
        } else {
                set_src_1(pc, src1, e);
-               if (src0->neg ^ src1->neg) {
+               if ((src0->mod ^ src1->mod) & NV50_MOD_NEG) {
                        if (is_long(e))
                                e->inst[1] |= 0x08000000;
                        else
@@ -698,13 +910,15 @@ emit_add(struct nv50_pc *pc, struct nv50_reg *dst,
 {
        struct nv50_program_exec *e = exec(pc);
 
-       e->inst[0] |= 0xb0000000;
+       e->inst[0] = 0xb0000000;
 
+       alloc_reg(pc, src1);
        check_swap_src_0_1(pc, &src0, &src1);
 
-       if (!pc->allow32 || src0->neg || src1->neg) {
+       if (!pc->allow32 || (src0->mod | src1->mod) || src1->hw > 63) {
                set_long(pc, e);
-               e->inst[1] |= (src0->neg << 26) | (src1->neg << 27);
+               e->inst[1] |= ((src0->mod & NV50_MOD_NEG) << 26) |
+                             ((src1->mod & NV50_MOD_NEG) << 27);
        }
 
        set_dst(pc, dst, e);
@@ -720,6 +934,22 @@ emit_add(struct nv50_pc *pc, struct nv50_reg *dst,
        emit(pc, e);
 }
 
+static void
+emit_arl(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src,
+        uint8_t s)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       set_long(pc, e);
+       e->inst[1] |= 0xc0000000;
+
+       e->inst[0] |= dst->hw << 2;
+       e->inst[0] |= s << 16; /* shift left */
+       set_src_0_restricted(pc, src, e);
+
+       emit(pc, e);
+}
+
 static void
 emit_minmax(struct nv50_pc *pc, unsigned sub, struct nv50_reg *dst,
            struct nv50_reg *src0, struct nv50_reg *src1)
@@ -735,6 +965,11 @@ emit_minmax(struct nv50_pc *pc, unsigned sub, struct nv50_reg *dst,
        set_src_0(pc, src0, e);
        set_src_1(pc, src1, e);
 
+       if (src0->mod & NV50_MOD_ABS)
+               e->inst[1] |= 0x00100000;
+       if (src1->mod & NV50_MOD_ABS)
+               e->inst[1] |= 0x00080000;
+
        emit(pc, e);
 }
 
@@ -742,9 +977,46 @@ static INLINE void
 emit_sub(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
         struct nv50_reg *src1)
 {
-       src1->neg ^= 1;
+       src1->mod ^= NV50_MOD_NEG;
        emit_add(pc, dst, src0, src1);
-       src1->neg ^= 1;
+       src1->mod ^= NV50_MOD_NEG;
+}
+
+static void
+emit_bitop2(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
+           struct nv50_reg *src1, unsigned op)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0xd0000000;
+       set_long(pc, e);
+
+       check_swap_src_0_1(pc, &src0, &src1);
+       set_dst(pc, dst, e);
+       set_src_0(pc, src0, e);
+
+       if (op != TGSI_OPCODE_AND && op != TGSI_OPCODE_OR &&
+           op != TGSI_OPCODE_XOR)
+               assert(!"invalid bit op");
+
+       if (src1->type == P_IMMD && src0->type == P_TEMP && pc->allow32) {
+               set_immd(pc, src1, e);
+               if (op == TGSI_OPCODE_OR)
+                       e->inst[0] |= 0x0100;
+               else
+               if (op == TGSI_OPCODE_XOR)
+                       e->inst[0] |= 0x8000;
+       } else {
+               set_src_1(pc, src1, e);
+               e->inst[1] |= 0x04000000; /* 32 bit */
+               if (op == TGSI_OPCODE_OR)
+                       e->inst[1] |= 0x4000;
+               else
+               if (op == TGSI_OPCODE_XOR)
+                       e->inst[1] |= 0x8000;
+       }
+
+       emit(pc, e);
 }
 
 static void
@@ -761,9 +1033,9 @@ emit_mad(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
        set_src_1(pc, src1, e);
        set_src_2(pc, src2, e);
 
-       if (src0->neg ^ src1->neg)
+       if ((src0->mod ^ src1->mod) & NV50_MOD_NEG)
                e->inst[1] |= 0x04000000;
-       if (src2->neg)
+       if (src2->mod & NV50_MOD_NEG)
                e->inst[1] |= 0x08000000;
 
        emit(pc, e);
@@ -773,9 +1045,9 @@ static INLINE void
 emit_msb(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
         struct nv50_reg *src1, struct nv50_reg *src2)
 {
-       src2->neg ^= 1;
+       src2->mod ^= NV50_MOD_NEG;
        emit_mad(pc, dst, src0, src1, src2);
-       src2->neg ^= 1;
+       src2->mod ^= NV50_MOD_NEG;
 }
 
 static void
@@ -837,12 +1109,11 @@ emit_precossin(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
 #define CVTOP_SAT      0x08
 #define CVTOP_ABS      0x10
 
-/* 0x04 == 32 bit */
+/* 0x04 == 32 bit dst */
 /* 0x40 == dst is float */
 /* 0x80 == src is float */
 #define CVT_F32_F32 0xc4
 #define CVT_F32_S32 0x44
-#define CVT_F32_U32 0x64
 #define CVT_S32_F32 0x8c
 #define CVT_S32_S32 0x0c
 #define CVT_NEG     0x20
@@ -858,7 +1129,7 @@ emit_cvt(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src,
        set_long(pc, e);
 
        e->inst[0] |= 0xa0000000;
-       e->inst[1] |= 0x00004000;
+       e->inst[1] |= 0x00004000; /* 32 bit src */
        e->inst[1] |= (cvn << 16);
        e->inst[1] |= (fmt << 24);
        set_src_0(pc, src, e);
@@ -926,7 +1197,6 @@ emit_set(struct nv50_pc *pc, unsigned ccode, struct nv50_reg *dst, int wp,
        set_src_1(pc, src1, e);
 
        emit(pc, e);
-       pc->if_cond = pc->p->exec_tail; /* record for OPCODE_IF */
 
        /* cvt.f32.u32/s32 (?) if we didn't only write the predicate */
        if (rdst)
@@ -1037,133 +1307,374 @@ emit_lit(struct nv50_pc *pc, struct nv50_reg **dst, unsigned mask,
        FREE(one);
 }
 
-static void
+static INLINE void
 emit_neg(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
+{
+       emit_cvt(pc, dst, src, -1, CVTOP_RN, CVT_F32_F32 | CVT_NEG);
+}
+
+static void
+emit_kil(struct nv50_pc *pc, struct nv50_reg *src)
+{
+       struct nv50_program_exec *e;
+       const int r_pred = 1;
+
+       e = exec(pc);
+       e->inst[0] = 0x00000002; /* discard */
+       set_long(pc, e); /* sets cond code to ALWAYS */
+
+       if (src) {
+               unsigned cvn = CVT_F32_F32;
+
+               set_pred(pc, 0x1 /* cc = LT */, r_pred, e);
+
+               if (src->mod & NV50_MOD_NEG)
+                       cvn |= CVT_NEG;
+               /* write predicate reg */
+               emit_cvt(pc, NULL, src, r_pred, CVTOP_RN, cvn);
+       }
+
+       emit(pc, e);
+}
+
+static struct nv50_program_exec *
+emit_breakaddr(struct nv50_pc *pc)
 {
        struct nv50_program_exec *e = exec(pc);
 
+       e->inst[0] = 0x40000002;
        set_long(pc, e);
-       e->inst[0] |= 0xa0000000; /* delta */
-       e->inst[1] |= (7 << 29); /* delta */
-       e->inst[1] |= 0x04000000; /* negate arg0? probably not */
-       e->inst[1] |= (1 << 14); /* src .f32 */
-       set_dst(pc, dst, e);
-       set_src_0(pc, src, e);
 
        emit(pc, e);
+       return e;
 }
 
 static void
-emit_kil(struct nv50_pc *pc, struct nv50_reg *src)
+emit_break(struct nv50_pc *pc, int pred, unsigned cc)
 {
-       struct nv50_program_exec *e;
-       const int r_pred = 1;
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0x50000002;
+       set_long(pc, e);
+       if (pred >= 0)
+               set_pred(pc, cc, pred, e);
 
-       /* Sets predicate reg ? */
-       e = exec(pc);
-       e->inst[0] = 0xa00001fd;
-       e->inst[1] = 0xc4014788;
-       set_src_0(pc, src, e);
-       set_pred_wr(pc, 1, r_pred, e);
-       if (src->neg)
-               e->inst[1] |= 0x20000000;
        emit(pc, e);
+}
 
-       /* This is probably KILP */
-       e = exec(pc);
-       e->inst[0] = 0x000001fe;
+static struct nv50_program_exec *
+emit_joinat(struct nv50_pc *pc)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0xa0000002;
        set_long(pc, e);
-       set_pred(pc, 1 /* LT? */, r_pred, e);
+
        emit(pc, e);
+       return e;
+}
+
+static struct nv50_program_exec *
+emit_branch(struct nv50_pc *pc, int pred, unsigned cc)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0x10000002;
+       set_long(pc, e);
+       if (pred >= 0)
+               set_pred(pc, cc, pred, e);
+       emit(pc, e);
+       return pc->p->exec_tail;
 }
 
 static void
-emit_tex(struct nv50_pc *pc, struct nv50_reg **dst, unsigned mask,
-        struct nv50_reg **src, unsigned unit, unsigned type, boolean proj)
+emit_ret(struct nv50_pc *pc, int pred, unsigned cc)
 {
-       struct nv50_reg *temp, *t[4];
-       struct nv50_program_exec *e;
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0x30000002;
+       set_long(pc, e);
+       if (pred >= 0)
+               set_pred(pc, cc, pred, e);
+
+       emit(pc, e);
+}
+
+#define QOP_ADD 0
+#define QOP_SUBR 1
+#define QOP_SUB 2
+#define QOP_MOV_SRC1 3
+
+/* For a quad of threads / top left, top right, bottom left, bottom right
+ * pixels, do a different operation, and take src0 from a specific thread.
+ */
+static void
+emit_quadop(struct nv50_pc *pc, struct nv50_reg *dst, int wp, int lane_src0,
+           struct nv50_reg *src0, struct nv50_reg *src1, ubyte qop)
+{
+       struct nv50_program_exec *e = exec(pc);
+
+       e->inst[0] = 0xc0000000;
+       e->inst[1] = 0x80000000;
+       set_long(pc, e);
+       e->inst[0] |= lane_src0 << 16;
+       set_src_0(pc, src0, e);
+       set_src_2(pc, src1, e);
+
+       if (wp >= 0)
+              set_pred_wr(pc, 1, wp, e);
+
+       if (dst)
+              set_dst(pc, dst, e);
+       else {
+              e->inst[0] |= 0x000001fc;
+              e->inst[1] |= 0x00000008;
+       }
 
-       unsigned c, mode, dim;
+       e->inst[0] |= (qop & 3) << 20;
+       e->inst[1] |= (qop >> 2) << 22;
 
+       emit(pc, e);
+}
+
+static void
+load_cube_tex_coords(struct nv50_pc *pc, struct nv50_reg *t[4],
+                    struct nv50_reg **src, unsigned arg, boolean proj)
+{
+       int mod[3] = { src[0]->mod, src[1]->mod, src[2]->mod };
+
+       src[0]->mod |= NV50_MOD_ABS;
+       src[1]->mod |= NV50_MOD_ABS;
+       src[2]->mod |= NV50_MOD_ABS;
+
+       emit_minmax(pc, 4, t[2], src[0], src[1]);
+       emit_minmax(pc, 4, t[2], src[2], t[2]);
+
+       src[0]->mod = mod[0];
+       src[1]->mod = mod[1];
+       src[2]->mod = mod[2];
+
+       if (proj && 0 /* looks more correct without this */)
+               emit_mul(pc, t[2], t[2], src[3]);
+       else
+       if (arg == 4) /* there is no textureProj(samplerCubeShadow) */
+               emit_mov(pc, t[3], src[3]);
+
+       emit_flop(pc, 0, t[2], t[2]);
+
+       emit_mul(pc, t[0], src[0], t[2]);
+       emit_mul(pc, t[1], src[1], t[2]);
+       emit_mul(pc, t[2], src[2], t[2]);
+}
+
+static void
+load_proj_tex_coords(struct nv50_pc *pc, struct nv50_reg *t[4],
+                    struct nv50_reg **src, unsigned dim, unsigned arg)
+{
+       unsigned c, mode;
+
+       if (src[0]->type == P_TEMP && src[0]->rhw != -1) {
+               mode = pc->interp_mode[src[0]->index] | INTERP_PERSPECTIVE;
+
+               t[3]->rhw = src[3]->rhw;
+               emit_interp(pc, t[3], NULL, (mode & INTERP_CENTROID));
+               emit_flop(pc, 0, t[3], t[3]);
+
+               for (c = 0; c < dim; ++c) {
+                       t[c]->rhw = src[c]->rhw;
+                       emit_interp(pc, t[c], t[3], mode);
+               }
+               if (arg != dim) { /* depth reference value */
+                       t[dim]->rhw = src[2]->rhw;
+                       emit_interp(pc, t[dim], t[3], mode);
+               }
+       } else {
+               /* XXX: for some reason the blob sometimes uses MAD
+                * (mad f32 $rX $rY $rZ neg $r63)
+                */
+               emit_flop(pc, 0, t[3], src[3]);
+               for (c = 0; c < dim; ++c)
+                       emit_mul(pc, t[c], src[c], t[3]);
+               if (arg != dim) /* depth reference value */
+                       emit_mul(pc, t[dim], src[2], t[3]);
+       }
+}
+
+static INLINE void
+get_tex_dim(unsigned type, unsigned *dim, unsigned *arg)
+{
        switch (type) {
        case TGSI_TEXTURE_1D:
-               dim = 1;
+               *arg = *dim = 1;
+               break;
+       case TGSI_TEXTURE_SHADOW1D:
+               *dim = 1;
+               *arg = 2;
                break;
        case TGSI_TEXTURE_UNKNOWN:
        case TGSI_TEXTURE_2D:
-       case TGSI_TEXTURE_SHADOW1D: /* XXX: x, z */
        case TGSI_TEXTURE_RECT:
-               dim = 2;
+               *arg = *dim = 2;
+               break;
+       case TGSI_TEXTURE_SHADOW2D:
+       case TGSI_TEXTURE_SHADOWRECT:
+               *dim = 2;
+               *arg = 3;
                break;
        case TGSI_TEXTURE_3D:
        case TGSI_TEXTURE_CUBE:
-       case TGSI_TEXTURE_SHADOW2D:
-       case TGSI_TEXTURE_SHADOWRECT: /* XXX */
-               dim = 3;
+               *dim = *arg = 3;
                break;
        default:
                assert(0);
                break;
        }
+}
 
-       /* some cards need t[0]'s hw index to be a multiple of 4 */
-       alloc_temp4(pc, t, 0);
+/* We shouldn't execute TEXLOD if any of the pixels in a quad have
+ * different LOD values, so branch off groups of equal LOD.
+ */
+static void
+emit_texlod_sequence(struct nv50_pc *pc, struct nv50_reg *tlod,
+                    struct nv50_reg *src, struct nv50_program_exec *tex)
+{
+       struct nv50_program_exec *join_at;
+       unsigned i, target = pc->p->exec_size + 7 * 2;
 
-       if (proj) {
-               if (src[0]->type == P_TEMP && src[0]->rhw != -1) {
-                       mode = pc->interp_mode[src[0]->index];
+       /* Subtract lod of each pixel from lod of top left pixel, jump
+        * texlod insn if result is 0, then repeat for 2 other pixels.
+        */
+       join_at = emit_joinat(pc);
+       emit_quadop(pc, NULL, 0, 0, tlod, tlod, 0x55);
+       emit_branch(pc, 0, 2)->param.index = target;
 
-                       t[3]->rhw = src[3]->rhw;
-                       emit_interp(pc, t[3], NULL, (mode & INTERP_CENTROID));
-                       emit_flop(pc, 0, t[3], t[3]);
+       for (i = 1; i < 4; ++i) {
+               emit_quadop(pc, NULL, 0, i, tlod, tlod, 0x55);
+               emit_branch(pc, 0, 2)->param.index = target;
+       }
 
-                       for (c = 0; c < dim; c++) {
-                               t[c]->rhw = src[c]->rhw;
-                               emit_interp(pc, t[c], t[3],
-                                           (mode | INTERP_PERSPECTIVE));
-                       }
-               } else {
-                       emit_flop(pc, 0, t[3], src[3]);
-                       for (c = 0; c < dim; c++)
-                               emit_mul(pc, t[c], src[c], t[3]);
+       emit_mov(pc, tlod, src); /* target */
+       emit(pc, tex); /* texlod */
 
-                       /* XXX: for some reason the blob sometimes uses MAD:
-                        * emit_mad(pc, t[c], src[0][c], t[3], t[3])
-                        * pc->p->exec_tail->inst[1] |= 0x080fc000;
-                        */
-               }
-       } else {
-               if (type == TGSI_TEXTURE_CUBE) {
-                       temp = temp_temp(pc);
-                       emit_minmax(pc, 4, temp, src[0], src[1]);
-                       emit_minmax(pc, 4, temp, temp, src[2]);
-                       emit_flop(pc, 0, temp, temp);
-                       for (c = 0; c < 3; c++)
-                               emit_mul(pc, t[c], src[c], temp);
-               } else {
-                       for (c = 0; c < dim; c++)
-                               emit_mov(pc, t[c], src[c]);
+       join_at->param.index = target + 2 * 2;
+       JOIN_ON(emit_nop(pc)); /* join _after_ tex */
+}
+
+static void
+emit_texbias_sequence(struct nv50_pc *pc, struct nv50_reg *t[4], unsigned arg,
+                     struct nv50_program_exec *tex)
+{
+       struct nv50_program_exec *e;
+       struct nv50_reg imm_1248, *t123[4][4], *r_bits = alloc_temp(pc, NULL);
+       int r_pred = 0;
+       unsigned n, c, i, cc[4] = { 0x0a, 0x13, 0x11, 0x10 };
+
+       pc->allow32 = FALSE;
+       ctor_reg(&imm_1248, P_IMMD, -1, ctor_immd_4u32(pc, 1, 2, 4, 8) * 4);
+
+       /* Subtract bias value of thread i from bias values of each thread,
+        * store result in r_pred, and set bit i in r_bits if result was 0.
+        */
+       assert(arg < 4);
+       for (i = 0; i < 4; ++i, ++imm_1248.hw) {
+               emit_quadop(pc, NULL, r_pred, i, t[arg], t[arg], 0x55);
+               emit_mov(pc, r_bits, &imm_1248);
+               set_pred(pc, 2, r_pred, pc->p->exec_tail);
+       }
+       emit_mov_to_pred(pc, r_pred, r_bits);
+
+       /* The lanes of a quad are now grouped by the bit in r_pred they have
+        * set. Put the input values for TEX into a new register set for each
+        * group and execute TEX only for a specific group.
+        * We cannot use the same register set for each group because we need
+        * the derivatives, which are implicitly calculated, to be correct.
+        */
+       for (i = 1; i < 4; ++i) {
+               alloc_temp4(pc, t123[i], 0);
+
+               for (c = 0; c <= arg; ++c)
+                       emit_mov(pc, t123[i][c], t[c]);
+
+               *(e = exec(pc)) = *(tex);
+               e->inst[0] &= ~0x01fc;
+               set_dst(pc, t123[i][0], e);
+               set_pred(pc, cc[i], r_pred, e);
+               emit(pc, e);
+       }
+       /* finally TEX on the original regs (where we kept the input) */
+       set_pred(pc, cc[0], r_pred, tex);
+       emit(pc, tex);
+
+       /* put the 3 * n other results into regs for lane 0 */
+       n = popcnt4(((e->inst[0] >> 25) & 0x3) | ((e->inst[1] >> 12) & 0xc));
+       for (i = 1; i < 4; ++i) {
+               for (c = 0; c < n; ++c) {
+                       emit_mov(pc, t[c], t123[i][c]);
+                       set_pred(pc, cc[i], r_pred, pc->p->exec_tail);
                }
+               free_temp4(pc, t123[i]);
        }
 
+       emit_nop(pc);
+       free_temp(pc, r_bits);
+}
+
+static void
+emit_tex(struct nv50_pc *pc, struct nv50_reg **dst, unsigned mask,
+        struct nv50_reg **src, unsigned unit, unsigned type,
+        boolean proj, int bias_lod)
+{
+       struct nv50_reg *t[4];
+       struct nv50_program_exec *e;
+       unsigned c, dim, arg;
+
+       /* t[i] must be within a single 128 bit super-reg */
+       alloc_temp4(pc, t, 0);
+
        e = exec(pc);
+       e->inst[0] = 0xf0000000;
        set_long(pc, e);
-       e->inst[0] |= 0xf0000000;
-       e->inst[1] |= 0x00000004;
        set_dst(pc, t[0], e);
-       e->inst[0] |= (unit << 9);
 
-       if (dim == 2)
-               e->inst[0] |= 0x00400000;
-       else
-       if (dim == 3)
-               e->inst[0] |= 0x00800000;
+       /* TIC and TSC binding indices (TSC is ignored as TSC_LINKED = TRUE): */
+       e->inst[0] |= (unit << 9) /* | (unit << 17) */;
+
+       /* live flag (don't set if TEX results affect input to another TEX): */
+       /* e->inst[0] |= 0x00000004; */
+
+       get_tex_dim(type, &dim, &arg);
+
+       if (type == TGSI_TEXTURE_CUBE) {
+               e->inst[0] |= 0x08000000;
+               load_cube_tex_coords(pc, t, src, arg, proj);
+       } else
+       if (proj)
+               load_proj_tex_coords(pc, t, src, dim, arg);
+       else {
+               for (c = 0; c < dim; c++)
+                       emit_mov(pc, t[c], src[c]);
+               if (arg != dim) /* depth reference value (always src.z here) */
+                       emit_mov(pc, t[dim], src[2]);
+       }
 
        e->inst[0] |= (mask & 0x3) << 25;
        e->inst[1] |= (mask & 0xc) << 12;
 
-       emit(pc, e);
+       if (!bias_lod) {
+               e->inst[0] |= (arg - 1) << 22;
+               emit(pc, e);
+       } else
+       if (bias_lod < 0) {
+               e->inst[0] |= arg << 22;
+               e->inst[1] |= 0x20000000; /* texbias */
+               emit_mov(pc, t[arg], src[3]);
+               emit_texbias_sequence(pc, t, arg, e);
+       } else {
+               e->inst[0] |= arg << 22;
+               e->inst[1] |= 0x40000000; /* texlod */
+               emit_mov(pc, t[arg], src[3]);
+               emit_texlod_sequence(pc, t[arg], src[3], e);
+       }
 
 #if 1
        c = 0;
@@ -1187,34 +1698,36 @@ emit_tex(struct nv50_pc *pc, struct nv50_reg **dst, unsigned mask,
 }
 
 static void
-emit_branch(struct nv50_pc *pc, int pred, unsigned cc,
-           struct nv50_program_exec **join)
+emit_ddx(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
 {
        struct nv50_program_exec *e = exec(pc);
 
-       if (join) {
-               set_long(pc, e);
-               e->inst[0] |= 0xa0000002;
-               emit(pc, e);
-               *join = e;
-               e = exec(pc);
-       }
+       assert(src->type == P_TEMP);
 
+       e->inst[0] = (src->mod & NV50_MOD_NEG) ? 0xc0240000 : 0xc0140000;
+       e->inst[1] = (src->mod & NV50_MOD_NEG) ? 0x86400000 : 0x89800000;
        set_long(pc, e);
-       e->inst[0] |= 0x10000002;
-       if (pred >= 0)
-               set_pred(pc, cc, pred, e);
+       set_dst(pc, dst, e);
+       set_src_0(pc, src, e);
+       set_src_2(pc, src, e);
+
        emit(pc, e);
 }
 
 static void
-emit_nop(struct nv50_pc *pc)
+emit_ddy(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
 {
        struct nv50_program_exec *e = exec(pc);
 
-       e->inst[0] = 0xf0000000;
+       assert(src->type == P_TEMP);
+
+       e->inst[0] = (src->mod & NV50_MOD_NEG) ? 0xc0250000 : 0xc0150000;
+       e->inst[1] = (src->mod & NV50_MOD_NEG) ? 0x85800000 : 0x8a400000;
        set_long(pc, e);
-       e->inst[1] = 0xe0000000;
+       set_dst(pc, dst, e);
+       set_src_0(pc, src, e);
+       set_src_2(pc, src, e);
+
        emit(pc, e);
 }
 
@@ -1266,10 +1779,13 @@ convert_to_long(struct nv50_pc *pc, struct nv50_program_exec *e)
        e->inst[1] |= q;
 }
 
+/* Some operations support an optional negation flag. */
 static boolean
 negate_supported(const struct tgsi_full_instruction *insn, int i)
 {
        switch (insn->Instruction.Opcode) {
+       case TGSI_OPCODE_DDX:
+       case TGSI_OPCODE_DDY:
        case TGSI_OPCODE_DP3:
        case TGSI_OPCODE_DP4:
        case TGSI_OPCODE_MUL:
@@ -1279,7 +1795,9 @@ negate_supported(const struct tgsi_full_instruction *insn, int i)
        case TGSI_OPCODE_MAD:
                return TRUE;
        case TGSI_OPCODE_POW:
-               return (i == 1) ? TRUE : FALSE;
+               if (i == 1)
+                       return TRUE;
+               return FALSE;
        default:
                return FALSE;
        }
@@ -1289,7 +1807,7 @@ negate_supported(const struct tgsi_full_instruction *insn, int i)
 static unsigned
 nv50_tgsi_src_mask(const struct tgsi_full_instruction *insn, int c)
 {
-       unsigned x, mask = insn->FullDstRegisters[0].DstRegister.WriteMask;
+       unsigned x, mask = insn->Dst[0].Register.WriteMask;
 
        switch (insn->Instruction.Opcode) {
        case TGSI_OPCODE_COS:
@@ -1310,24 +1828,32 @@ nv50_tgsi_src_mask(const struct tgsi_full_instruction *insn, int c)
        case TGSI_OPCODE_RSQ:
        case TGSI_OPCODE_SCS:
                return 0x1;
+       case TGSI_OPCODE_IF:
+               return 0x1;
        case TGSI_OPCODE_LIT:
                return 0xb;
        case TGSI_OPCODE_TEX:
+       case TGSI_OPCODE_TXB:
+       case TGSI_OPCODE_TXL:
        case TGSI_OPCODE_TXP:
        {
-               const struct tgsi_instruction_ext_texture *tex;
+               const struct tgsi_instruction_texture *tex;
 
-               assert(insn->Instruction.Extended);
-               tex = &insn->InstructionExtTexture;
+               assert(insn->Instruction.Texture);
+               tex = &insn->Texture;
 
                mask = 0x7;
-               if (insn->Instruction.Opcode == TGSI_OPCODE_TXP)
-                       mask |= 0x8;
+               if (insn->Instruction.Opcode != TGSI_OPCODE_TEX &&
+                   insn->Instruction.Opcode != TGSI_OPCODE_TXD)
+                       mask |= 0x8; /* bias, lod or proj */
 
                switch (tex->Texture) {
                case TGSI_TEXTURE_1D:
                        mask &= 0x9;
                        break;
+               case TGSI_TEXTURE_SHADOW1D:
+                       mask &= 0x5;
+                       break;
                case TGSI_TEXTURE_2D:
                        mask &= 0xb;
                        break;
@@ -1352,11 +1878,21 @@ nv50_tgsi_src_mask(const struct tgsi_full_instruction *insn, int c)
 static struct nv50_reg *
 tgsi_dst(struct nv50_pc *pc, int c, const struct tgsi_full_dst_register *dst)
 {
-       switch (dst->DstRegister.File) {
+       switch (dst->Register.File) {
        case TGSI_FILE_TEMPORARY:
-               return &pc->temp[dst->DstRegister.Index * 4 + c];
+               return &pc->temp[dst->Register.Index * 4 + c];
        case TGSI_FILE_OUTPUT:
-               return &pc->result[dst->DstRegister.Index * 4 + c];
+               return &pc->result[dst->Register.Index * 4 + c];
+       case TGSI_FILE_ADDRESS:
+       {
+               struct nv50_reg *r = pc->addr[dst->Register.Index * 4 + c];
+               if (!r) {
+                       r = alloc_addr(pc, NULL);
+                       pc->addr[dst->Register.Index * 4 + c] = r;
+               }
+               assert(r);
+               return r;
+       }
        case TGSI_FILE_NULL:
                return NULL;
        default:
@@ -1372,43 +1908,56 @@ tgsi_src(struct nv50_pc *pc, int chan, const struct tgsi_full_src_register *src,
 {
        struct nv50_reg *r = NULL;
        struct nv50_reg *temp;
-       unsigned sgn, c;
+       unsigned sgn, c, swz;
+
+       if (src->Register.File != TGSI_FILE_CONSTANT)
+               assert(!src->Register.Indirect);
 
        sgn = tgsi_util_get_full_src_register_sign_mode(src, chan);
 
-       c = tgsi_util_get_full_src_register_extswizzle(src, chan);
+       c = tgsi_util_get_full_src_register_swizzle(src, chan);
        switch (c) {
-       case TGSI_EXTSWIZZLE_X:
-       case TGSI_EXTSWIZZLE_Y:
-       case TGSI_EXTSWIZZLE_Z:
-       case TGSI_EXTSWIZZLE_W:
-               switch (src->SrcRegister.File) {
+       case TGSI_SWIZZLE_X:
+       case TGSI_SWIZZLE_Y:
+       case TGSI_SWIZZLE_Z:
+       case TGSI_SWIZZLE_W:
+               switch (src->Register.File) {
                case TGSI_FILE_INPUT:
-                       r = &pc->attr[src->SrcRegister.Index * 4 + c];
+                       r = &pc->attr[src->Register.Index * 4 + c];
                        break;
                case TGSI_FILE_TEMPORARY:
-                       r = &pc->temp[src->SrcRegister.Index * 4 + c];
+                       r = &pc->temp[src->Register.Index * 4 + c];
                        break;
                case TGSI_FILE_CONSTANT:
-                       r = &pc->param[src->SrcRegister.Index * 4 + c];
+                       if (!src->Register.Indirect) {
+                               r = &pc->param[src->Register.Index * 4 + c];
+                               break;
+                       }
+                       /* Indicate indirection by setting r->acc < 0 and
+                        * use the index field to select the address reg.
+                        */
+                       r = reg_instance(pc, NULL);
+                       swz = tgsi_util_get_src_register_swizzle(
+                                                &src->Indirect, 0);
+                       ctor_reg(r, P_CONST,
+                                src->Indirect.Index * 4 + swz,
+                                src->Register.Index * 4 + c);
+                       r->acc = -1;
                        break;
                case TGSI_FILE_IMMEDIATE:
-                       r = &pc->immd[src->SrcRegister.Index * 4 + c];
+                       r = &pc->immd[src->Register.Index * 4 + c];
                        break;
                case TGSI_FILE_SAMPLER:
                        break;
+               case TGSI_FILE_ADDRESS:
+                       r = pc->addr[src->Register.Index * 4 + c];
+                       assert(r);
+                       break;
                default:
                        assert(0);
                        break;
                }
                break;
-       case TGSI_EXTSWIZZLE_ZERO:
-               r = alloc_immd(pc, 0.0);
-               return r;
-       case TGSI_EXTSWIZZLE_ONE:
-               if (sgn == TGSI_UTIL_SIGN_TOGGLE || sgn == TGSI_UTIL_SIGN_SET)
-                       return alloc_immd(pc, -1.0);
-               return alloc_immd(pc, 1.0);
        default:
                assert(0);
                break;
@@ -1424,7 +1973,7 @@ tgsi_src(struct nv50_pc *pc, int chan, const struct tgsi_full_src_register *src,
                break;
        case TGSI_UTIL_SIGN_TOGGLE:
                if (neg)
-                       r->neg = 1;
+                       r->mod = NV50_MOD_NEG;
                else {
                        temp = temp_temp(pc);
                        emit_neg(pc, temp, r);
@@ -1433,11 +1982,7 @@ tgsi_src(struct nv50_pc *pc, int chan, const struct tgsi_full_src_register *src,
                break;
        case TGSI_UTIL_SIGN_SET:
                temp = temp_temp(pc);
-               emit_abs(pc, temp, r);
-               if (neg)
-                       temp->neg = 1;
-               else
-                       emit_neg(pc, temp, temp);
+               emit_cvt(pc, temp, r, -1, CVTOP_ABS, CVT_F32_F32 | CVT_NEG);
                r = temp;
                break;
        default:
@@ -1445,6 +1990,8 @@ tgsi_src(struct nv50_pc *pc, int chan, const struct tgsi_full_src_register *src,
                break;
        }
 
+       if (r && r->acc >= 0 && r != temp)
+               return reg_instance(pc, r);
        return r;
 }
 
@@ -1500,6 +2047,8 @@ nv50_tgsi_dst_revdep(unsigned op, int s, int c)
        case TGSI_OPCODE_LIT:
        case TGSI_OPCODE_SCS:
        case TGSI_OPCODE_TEX:
+       case TGSI_OPCODE_TXB:
+       case TGSI_OPCODE_TXL:
        case TGSI_OPCODE_TXP:
                /* these take care of dangerous swizzles themselves */
                return 0x0;
@@ -1541,28 +2090,43 @@ nv50_kill_branch(struct nv50_pc *pc)
         */
        if (has_pred(pc->if_insn[lvl], 0xf))
                return FALSE;
-       assert(pc->if_insn[lvl] && pc->br_join[lvl]);
+       assert(pc->if_insn[lvl] && pc->if_join[lvl]);
 
-       /* We'll use the exec allocated for JOIN_AT (as we can't easily
-        * update prev's next); if exec_tail is BRK, update the pointer.
+       /* We'll use the exec allocated for JOIN_AT (we can't easily
+        * access nv50_program_exec's prev).
         */
-       if (pc->loop_lvl && pc->br_loop[pc->loop_lvl - 1] == pc->p->exec_tail)
-               pc->br_loop[pc->loop_lvl - 1] = pc->br_join[lvl];
-
        pc->p->exec_size -= 4; /* remove JOIN_AT and BRA */
 
-       *pc->br_join[lvl] = *pc->p->exec_tail;
+       *pc->if_join[lvl] = *pc->p->exec_tail;
 
        FREE(pc->if_insn[lvl]);
        FREE(pc->p->exec_tail);
 
-       pc->p->exec_tail = pc->br_join[lvl];
+       pc->p->exec_tail = pc->if_join[lvl];
        pc->p->exec_tail->next = NULL;
        set_pred(pc, 0xd, 0, pc->p->exec_tail);
 
        return TRUE;
 }
 
+static void
+nv50_fp_move_results(struct nv50_pc *pc)
+{
+       struct nv50_reg reg;
+       unsigned i;
+
+       ctor_reg(&reg, P_TEMP, -1, -1);
+
+       for (i = 0; i < pc->result_nr * 4; ++i) {
+               if (pc->result[i].rhw < 0 || pc->result[i].hw < 0)
+                       continue;
+               if (pc->result[i].rhw != pc->result[i].hw) {
+                       reg.hw = pc->result[i].rhw;
+                       emit_mov(pc, &reg, &pc->result[i]);
+               }
+       }
+}
+
 static boolean
 nv50_program_tx_insn(struct nv50_pc *pc,
                     const struct tgsi_full_instruction *inst)
@@ -1571,29 +2135,29 @@ nv50_program_tx_insn(struct nv50_pc *pc,
        unsigned mask, sat, unit;
        int i, c;
 
-       mask = inst->FullDstRegisters[0].DstRegister.WriteMask;
+       mask = inst->Dst[0].Register.WriteMask;
        sat = inst->Instruction.Saturate == TGSI_SAT_ZERO_ONE;
 
        memset(src, 0, sizeof(src));
 
        for (c = 0; c < 4; c++) {
                if ((mask & (1 << c)) && !pc->r_dst[c])
-                       dst[c] = tgsi_dst(pc, c, &inst->FullDstRegisters[0]);
+                       dst[c] = tgsi_dst(pc, c, &inst->Dst[0]);
                else
                        dst[c] = pc->r_dst[c];
                rdst[c] = dst[c];
        }
 
        for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
-               const struct tgsi_full_src_register *fs = &inst->FullSrcRegisters[i];
+               const struct tgsi_full_src_register *fs = &inst->Src[i];
                unsigned src_mask;
                boolean neg_supp;
 
                src_mask = nv50_tgsi_src_mask(inst, i);
                neg_supp = negate_supported(inst, i);
 
-               if (fs->SrcRegister.File == TGSI_FILE_SAMPLER)
-                       unit = fs->SrcRegister.Index;
+               if (fs->Register.File == TGSI_FILE_SAMPLER)
+                       unit = fs->Register.Index;
 
                for (c = 0; c < 4; c++)
                        if (src_mask & (1 << c))
@@ -1610,7 +2174,7 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                for (c = 0; c < 4; c++) {
                        if (!(mask & (1 << c)) || dst[c]->type == P_TEMP)
                                continue;
-                       rdst[c] = dst[c];
+                       /* rdst[c] = dst[c]; */ /* done above */
                        dst[c] = temp_temp(pc);
                }
        }
@@ -1632,13 +2196,30 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                        emit_add(pc, dst[c], src[0][c], src[1][c]);
                }
                break;
+       case TGSI_OPCODE_AND:
+       case TGSI_OPCODE_XOR:
+       case TGSI_OPCODE_OR:
+               for (c = 0; c < 4; c++) {
+                       if (!(mask & (1 << c)))
+                               continue;
+                       emit_bitop2(pc, dst[c], src[0][c], src[1][c],
+                                   inst->Instruction.Opcode);
+               }
+               break;
+       case TGSI_OPCODE_ARL:
+               assert(src[0][0]);
+               temp = temp_temp(pc);
+               emit_cvt(pc, temp, src[0][0], -1, CVTOP_FLOOR, CVT_S32_F32);
+               emit_arl(pc, dst[0], temp, 4);
+               break;
        case TGSI_OPCODE_BGNLOOP:
+               pc->loop_brka[pc->loop_lvl] = emit_breakaddr(pc);
                pc->loop_pos[pc->loop_lvl++] = pc->p->exec_size;
+               terminate_mbb(pc);
                break;
        case TGSI_OPCODE_BRK:
-               emit_branch(pc, -1, 0, NULL);
                assert(pc->loop_lvl > 0);
-               pc->br_loop[pc->loop_lvl - 1] = pc->p->exec_tail;
+               emit_break(pc, -1, 0);
                break;
        case TGSI_OPCODE_CEIL:
                for (c = 0; c < 4; c++) {
@@ -1648,6 +2229,23 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                                 CVTOP_CEIL, CVT_F32_F32 | CVT_RI);
                }
                break;
+       case TGSI_OPCODE_CMP:
+               pc->allow32 = FALSE;
+               for (c = 0; c < 4; c++) {
+                       if (!(mask & (1 << c)))
+                               continue;
+                       emit_cvt(pc, NULL, src[0][c], 1, CVTOP_RN, CVT_F32_F32);
+                       emit_mov(pc, dst[c], src[1][c]);
+                       set_pred(pc, 0x1, 1, pc->p->exec_tail); /* @SF */
+                       emit_mov(pc, dst[c], src[2][c]);
+                       set_pred(pc, 0x6, 1, pc->p->exec_tail); /* @NSF */
+               }
+               break;
+       case TGSI_OPCODE_CONT:
+               assert(pc->loop_lvl > 0);
+               emit_branch(pc, -1, 0)->param.index =
+                       pc->loop_pos[pc->loop_lvl - 1];
+               break;
        case TGSI_OPCODE_COS:
                if (mask & 8) {
                        emit_precossin(pc, temp, src[0][3]);
@@ -1660,6 +2258,20 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                emit_precossin(pc, temp, src[0][0]);
                emit_flop(pc, 5, brdc, temp);
                break;
+       case TGSI_OPCODE_DDX:
+               for (c = 0; c < 4; c++) {
+                       if (!(mask & (1 << c)))
+                               continue;
+                       emit_ddx(pc, dst[c], src[0][c]);
+               }
+               break;
+       case TGSI_OPCODE_DDY:
+               for (c = 0; c < 4; c++) {
+                       if (!(mask & (1 << c)))
+                               continue;
+                       emit_ddy(pc, dst[c], src[0][c]);
+               }
+               break;
        case TGSI_OPCODE_DP3:
                emit_mul(pc, temp, src[0][0], src[1][0]);
                emit_mad(pc, temp, src[0][1], src[1][1], temp);
@@ -1688,9 +2300,10 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                        emit_mov_immdval(pc, dst[0], 1.0f);
                break;
        case TGSI_OPCODE_ELSE:
-               emit_branch(pc, -1, 0, NULL);
+               emit_branch(pc, -1, 0);
                pc->if_insn[--pc->if_lvl]->param.index = pc->p->exec_size;
                pc->if_insn[pc->if_lvl++] = pc->p->exec_tail;
+               terminate_mbb(pc);
                break;
        case TGSI_OPCODE_ENDIF:
                pc->if_insn[--pc->if_lvl]->param.index = pc->p->exec_size;
@@ -1699,20 +2312,21 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                if (nv50_kill_branch(pc) == TRUE)
                        break;
 
-               if (pc->br_join[pc->if_lvl]) {
-                       pc->br_join[pc->if_lvl]->param.index = pc->p->exec_size;
-                       pc->br_join[pc->if_lvl] = NULL;
+               if (pc->if_join[pc->if_lvl]) {
+                       pc->if_join[pc->if_lvl]->param.index = pc->p->exec_size;
+                       pc->if_join[pc->if_lvl] = NULL;
                }
+               terminate_mbb(pc);
                /* emit a NOP as join point, we could set it on the next
                 * one, but would have to make sure it is long and !immd
                 */
-               emit_nop(pc);
-               pc->p->exec_tail->inst[1] |= 2;
+               JOIN_ON(emit_nop(pc));
                break;
        case TGSI_OPCODE_ENDLOOP:
-               emit_branch(pc, -1, 0, NULL);
-               pc->p->exec_tail->param.index = pc->loop_pos[--pc->loop_lvl];
-               pc->br_loop[pc->loop_lvl]->param.index = pc->p->exec_size;
+               emit_branch(pc, -1, 0)->param.index =
+                       pc->loop_pos[--pc->loop_lvl];
+               pc->loop_brka[pc->loop_lvl]->param.index = pc->p->exec_size;
+               terminate_mbb(pc);
                break;
        case TGSI_OPCODE_EX2:
                emit_preex2(pc, temp, src[0][0]);
@@ -1735,18 +2349,23 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                }
                break;
        case TGSI_OPCODE_IF:
-               /* emitting a join_at may not be necessary */
-               assert(pc->if_lvl < MAX_IF_DEPTH);
-               set_pred_wr(pc, 1, 0, pc->if_cond);
-               emit_branch(pc, 0, 2, &pc->br_join[pc->if_lvl]);
-               pc->if_insn[pc->if_lvl++] = pc->p->exec_tail;
+               assert(pc->if_lvl < NV50_MAX_COND_NESTING);
+               emit_cvt(pc, NULL, src[0][0], 0, CVTOP_ABS | CVTOP_RN,
+                        CVT_F32_F32);
+               pc->if_join[pc->if_lvl] = emit_joinat(pc);
+               pc->if_insn[pc->if_lvl++] = emit_branch(pc, 0, 2);;
+               terminate_mbb(pc);
                break;
        case TGSI_OPCODE_KIL:
+               assert(src[0][0] && src[0][1] && src[0][2] && src[0][3]);
                emit_kil(pc, src[0][0]);
                emit_kil(pc, src[0][1]);
                emit_kil(pc, src[0][2]);
                emit_kil(pc, src[0][3]);
                break;
+       case TGSI_OPCODE_KILP:
+               emit_kil(pc, NULL);
+               break;
        case TGSI_OPCODE_LIT:
                emit_lit(pc, &dst[0], mask, &src[0][0]);
                break;
@@ -1784,7 +2403,6 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                }
                break;
        case TGSI_OPCODE_MOV:
-       case TGSI_OPCODE_SWZ:
                for (c = 0; c < 4; c++) {
                        if (!(mask & (1 << c)))
                                continue;
@@ -1804,6 +2422,11 @@ nv50_program_tx_insn(struct nv50_pc *pc,
        case TGSI_OPCODE_RCP:
                emit_flop(pc, 0, brdc, src[0][0]);
                break;
+       case TGSI_OPCODE_RET:
+               if (pc->p->type == PIPE_SHADER_FRAGMENT)
+                       nv50_fp_move_results(pc);
+               emit_ret(pc, -1, 0);
+               break;
        case TGSI_OPCODE_RSQ:
                emit_flop(pc, 2, brdc, src[0][0]);
                break;
@@ -1854,11 +2477,19 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                break;
        case TGSI_OPCODE_TEX:
                emit_tex(pc, dst, mask, src[0], unit,
-                        inst->InstructionExtTexture.Texture, FALSE);
+                        inst->Texture.Texture, FALSE, 0);
+               break;
+       case TGSI_OPCODE_TXB:
+               emit_tex(pc, dst, mask, src[0], unit,
+                        inst->Texture.Texture, FALSE, -1);
+               break;
+       case TGSI_OPCODE_TXL:
+               emit_tex(pc, dst, mask, src[0], unit,
+                        inst->Texture.Texture, FALSE, 1);
                break;
        case TGSI_OPCODE_TXP:
                emit_tex(pc, dst, mask, src[0], unit,
-                        inst->InstructionExtTexture.Texture, TRUE);
+                        inst->Texture.Texture, TRUE, 0);
                break;
        case TGSI_OPCODE_TRUNC:
                for (c = 0; c < 4; c++) {
@@ -1903,23 +2534,18 @@ nv50_program_tx_insn(struct nv50_pc *pc,
                for (c = 0; c < 4; c++) {
                        if (!(mask & (1 << c)))
                                continue;
-                       /* in this case we saturate later */
-                       if (dst[c]->type == P_TEMP && dst[c]->index < 0)
+                       /* In this case we saturate later, and dst[c] won't
+                        * be another temp_temp (and thus lost), since rdst
+                        * already is TEMP (see above). */
+                       if (rdst[c]->type == P_TEMP && rdst[c]->index < 0)
                                continue;
                        emit_sat(pc, rdst[c], dst[c]);
                }
        }
 
-       for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
-               for (c = 0; c < 4; c++) {
-                       if (!src[i][c])
-                               continue;
-                       if (src[i][c]->index == -1 && src[i][c]->type == P_IMMD)
-                               FREE(src[i][c]);
-               }
-       }
-
        kill_temp_temp(pc);
+       pc->reg_instance_nr = 0;
+
        return TRUE;
 }
 
@@ -1931,14 +2557,20 @@ prep_inspect_insn(struct nv50_pc *pc, const struct tgsi_full_instruction *insn)
        const struct tgsi_dst_register *dst;
        unsigned i, c, k, mask;
 
-       dst = &insn->FullDstRegisters[0].DstRegister;
+       dst = &insn->Dst[0].Register;
        mask = dst->WriteMask;
 
         if (dst->File == TGSI_FILE_TEMPORARY)
-                reg = pc->temp;
+               reg = pc->temp;
         else
-        if (dst->File == TGSI_FILE_OUTPUT)
-                reg = pc->result;
+       if (dst->File == TGSI_FILE_OUTPUT) {
+               reg = pc->result;
+
+               if (insn->Instruction.Opcode == TGSI_OPCODE_MOV &&
+                   dst->Index == pc->edgeflag_out &&
+                   insn->Src[0].Register.File == TGSI_FILE_INPUT)
+                       pc->p->cfg.edgeflag_in = insn->Src[0].Register.Index;
+       }
 
        if (reg) {
                for (c = 0; c < 4; c++) {
@@ -1949,12 +2581,12 @@ prep_inspect_insn(struct nv50_pc *pc, const struct tgsi_full_instruction *insn)
        }
 
        for (i = 0; i < insn->Instruction.NumSrcRegs; i++) {
-               src = &insn->FullSrcRegisters[i];
+               src = &insn->Src[i];
 
-               if (src->SrcRegister.File == TGSI_FILE_TEMPORARY)
+               if (src->Register.File == TGSI_FILE_TEMPORARY)
                        reg = pc->temp;
                else
-               if (src->SrcRegister.File == TGSI_FILE_INPUT)
+               if (src->Register.File == TGSI_FILE_INPUT)
                        reg = pc->attr;
                else
                        continue;
@@ -1964,12 +2596,9 @@ prep_inspect_insn(struct nv50_pc *pc, const struct tgsi_full_instruction *insn)
                for (c = 0; c < 4; c++) {
                        if (!(mask & (1 << c)))
                                continue;
-                       k = tgsi_util_get_full_src_register_extswizzle(src, c);
+                       k = tgsi_util_get_full_src_register_swizzle(src, c);
 
-                       if (k > TGSI_EXTSWIZZLE_W)
-                               continue;
-
-                       reg[src->SrcRegister.Index * 4 + k].acc = pc->insn_nr;
+                       reg[src->Register.Index * 4 + k].acc = pc->insn_nr;
                }
        }
 }
@@ -2029,13 +2658,13 @@ static struct nv50_reg *
 tgsi_broadcast_dst(struct nv50_pc *pc,
                   const struct tgsi_full_dst_register *fd, unsigned mask)
 {
-       if (fd->DstRegister.File == TGSI_FILE_TEMPORARY) {
-               int c = ffs(~mask & fd->DstRegister.WriteMask);
+       if (fd->Register.File == TGSI_FILE_TEMPORARY) {
+               int c = ffs(~mask & fd->Register.WriteMask);
                if (c)
                        return tgsi_dst(pc, c - 1, fd);
        } else {
-               int c = ffs(fd->DstRegister.WriteMask) - 1;
-               if ((1 << c) == fd->DstRegister.WriteMask)
+               int c = ffs(fd->Register.WriteMask) - 1;
+               if ((1 << c) == fd->Register.WriteMask)
                        return tgsi_dst(pc, c, fd);
        }
 
@@ -2049,7 +2678,7 @@ static unsigned
 nv50_tgsi_scan_swizzle(const struct tgsi_full_instruction *insn,
                       unsigned rdep[4])
 {
-       const struct tgsi_full_dst_register *fd = &insn->FullDstRegisters[0];
+       const struct tgsi_full_dst_register *fd = &insn->Dst[0];
        const struct tgsi_full_src_register *fs;
        unsigned i, deqs = 0;
 
@@ -2060,9 +2689,9 @@ nv50_tgsi_scan_swizzle(const struct tgsi_full_instruction *insn,
                unsigned chn, mask = nv50_tgsi_src_mask(insn, i);
                boolean neg_supp = negate_supported(insn, i);
 
-               fs = &insn->FullSrcRegisters[i];
-               if (fs->SrcRegister.File != fd->DstRegister.File ||
-                   fs->SrcRegister.Index != fd->DstRegister.Index)
+               fs = &insn->Src[i];
+               if (fs->Register.File != fd->Register.File ||
+                   fs->Register.Index != fd->Register.Index)
                        continue;
 
                for (chn = 0; chn < 4; ++chn) {
@@ -2070,11 +2699,10 @@ nv50_tgsi_scan_swizzle(const struct tgsi_full_instruction *insn,
 
                        if (!(mask & (1 << chn))) /* src is not read */
                                continue;
-                       c = tgsi_util_get_full_src_register_extswizzle(fs, chn);
+                       c = tgsi_util_get_full_src_register_swizzle(fs, chn);
                        s = tgsi_util_get_full_src_register_sign_mode(fs, chn);
 
-                       if (c > TGSI_EXTSWIZZLE_W ||
-                           !(fd->DstRegister.WriteMask & (1 << c)))
+                       if (!(fd->Register.WriteMask & (1 << c)))
                                continue;
 
                        /* no danger if src is copied to TEMP first */
@@ -2098,7 +2726,7 @@ nv50_tgsi_insn(struct nv50_pc *pc, const union tgsi_full_token *tok)
        const struct tgsi_full_dst_register *fd;
        unsigned i, deqs, rdep[4], m[4];
 
-       fd = &tok->FullInstruction.FullDstRegisters[0];
+       fd = &tok->FullInstruction.Dst[0];
        deqs = nv50_tgsi_scan_swizzle(&insn, rdep);
 
        if (is_scalar_op(insn.Instruction.Opcode)) {
@@ -2117,10 +2745,10 @@ nv50_tgsi_insn(struct nv50_pc *pc, const union tgsi_full_token *tok)
        for (i = 0; i < 4; ++i) {
                assert(pc->r_dst[m[i]] == NULL);
 
-               insn.FullDstRegisters[0].DstRegister.WriteMask =
-                       fd->DstRegister.WriteMask & (1 << m[i]);
+               insn.Dst[0].Register.WriteMask =
+                       fd->Register.WriteMask & (1 << m[i]);
 
-               if (!insn.FullDstRegisters[0].DstRegister.WriteMask)
+               if (!insn.Dst[0].Register.WriteMask)
                        continue;
 
                if (deqs & (1 << i))
@@ -2170,6 +2798,23 @@ load_interpolant(struct nv50_pc *pc, struct nv50_reg *reg)
        emit_interp(pc, reg, iv, mode);
 }
 
+/* The face input is always at v[255] (varying space), with a
+ * value of 0 for back-facing, and 0xffffffff for front-facing.
+ */
+static void
+load_frontfacing(struct nv50_pc *pc, struct nv50_reg *a)
+{
+       struct nv50_reg *one = alloc_immd(pc, 1.0f);
+
+       assert(a->rhw == -1);
+       alloc_reg(pc, a); /* do this before rhw is set */
+       a->rhw = 255;
+       load_interpolant(pc, a);
+       emit_bitop2(pc, a, a, one, TGSI_OPCODE_AND);
+
+       FREE(one);
+}
+
 static boolean
 nv50_program_tx_prep(struct nv50_pc *pc)
 {
@@ -2189,10 +2834,10 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                        const struct tgsi_full_immediate *imm =
                                &tp.FullToken.FullImmediate;
 
-                       ctor_immd(pc, imm->u[0].Float,
-                                     imm->u[1].Float,
-                                     imm->u[2].Float,
-                                     imm->u[3].Float);
+                       ctor_immd_4f32(pc, imm->u[0].Float,
+                                      imm->u[1].Float,
+                                      imm->u[2].Float,
+                                      imm->u[3].Float);
                }
                        break;
                case TGSI_TOKEN_TYPE_DECLARATION:
@@ -2201,8 +2846,8 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                        unsigned si, last, first, mode;
 
                        d = &tp.FullToken.FullDeclaration;
-                       first = d->DeclarationRange.First;
-                       last = d->DeclarationRange.Last;
+                       first = d->Range.First;
+                       last = d->Range.Last;
 
                        switch (d->Declaration.File) {
                        case TGSI_FILE_TEMPORARY:
@@ -2212,8 +2857,8 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                                    p->type == PIPE_SHADER_FRAGMENT)
                                        break;
 
-                               si = d->Semantic.SemanticIndex;
-                               switch (d->Semantic.SemanticName) {
+                               si = d->Semantic.Index;
+                               switch (d->Semantic.Name) {
                                case TGSI_SEMANTIC_BCOLOR:
                                        p->cfg.two_side[si].hw = first;
                                        if (p->cfg.io_nr > first)
@@ -2224,6 +2869,9 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                                        if (p->cfg.io_nr > first)
                                                p->cfg.io_nr = first;
                                        break;
+                               case TGSI_SEMANTIC_EDGEFLAG:
+                                       pc->edgeflag_out = first;
+                                       break;
                                        /*
                                case TGSI_SEMANTIC_CLIP_DISTANCE:
                                        p->cfg.clpd = MIN2(p->cfg.clpd, first);
@@ -2259,8 +2907,8 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                                        pc->interp_mode[i] = mode;
                        }
                                break;
+                       case TGSI_FILE_ADDRESS:
                        case TGSI_FILE_CONSTANT:
-                               break;
                        case TGSI_FILE_SAMPLER:
                                break;
                        default:
@@ -2291,7 +2939,7 @@ nv50_program_tx_prep(struct nv50_pc *pc)
 
                for (i = 0, rid = 0; i < pc->result_nr; ++i) {
                        p->cfg.io[i].hw = rid;
-                       p->cfg.io[i].id_vp = i;
+                       p->cfg.io[i].id = i;
 
                        for (c = 0; c < 4; ++c) {
                                int n = i * 4 + c;
@@ -2314,6 +2962,8 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                int rid, aid;
                unsigned n = 0, m = pc->attr_nr - flat_nr;
 
+               pc->allow32 = TRUE;
+
                int base = (TGSI_SEMANTIC_POSITION ==
                            p->info.input_semantic_name[0]) ? 0 : 1;
 
@@ -2321,14 +2971,12 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                 * the lower hardware IDs, so sort them:
                 */
                for (i = 0; i < pc->attr_nr; i++) {
-                       if (pc->interp_mode[i] == INTERP_FLAT) {
-                               p->cfg.io[m].id_vp = i + base;
-                               p->cfg.io[m++].id_fp = i;
-                       } else {
+                       if (pc->interp_mode[i] == INTERP_FLAT)
+                               p->cfg.io[m++].id = i;
+                       else {
                                if (!(pc->interp_mode[i] & INTERP_PERSPECTIVE))
                                        p->cfg.io[n].linear = TRUE;
-                               p->cfg.io[n].id_vp = i + base;
-                               p->cfg.io[n++].id_fp = i;
+                               p->cfg.io[n++].id = i;
                        }
                }
 
@@ -2340,7 +2988,13 @@ nv50_program_tx_prep(struct nv50_pc *pc)
 
                for (n = 0; n < pc->attr_nr; ++n) {
                        p->cfg.io[n].hw = rid = aid;
-                       i = p->cfg.io[n].id_fp;
+                       i = p->cfg.io[n].id;
+
+                       if (p->info.input_semantic_name[n] ==
+                           TGSI_SEMANTIC_FACE) {
+                               load_frontfacing(pc, &pc->attr[i * 4]);
+                               continue;
+                       }
 
                        for (c = 0; c < 4; ++c) {
                                if (!pc->attr[i * 4 + c].acc)
@@ -2374,8 +3028,8 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                for (i = 0; i < pc->attr_nr; i++) {
                        ubyte si, sn;
 
-                       sn = p->info.input_semantic_name[p->cfg.io[i].id_fp];
-                       si = p->info.input_semantic_index[p->cfg.io[i].id_fp];
+                       sn = p->info.input_semantic_name[p->cfg.io[i].id];
+                       si = p->info.input_semantic_index[p->cfg.io[i].id];
 
                        if (sn == TGSI_SEMANTIC_COLOR) {
                                p->cfg.two_side[si] = p->cfg.io[i];
@@ -2400,6 +3054,10 @@ nv50_program_tx_prep(struct nv50_pc *pc)
                        pc->result[2].rhw = rid;
 
                p->cfg.high_result = rid;
+
+               /* separate/different colour results for MRTs ? */
+               if (pc->result_nr - (p->info.writes_z ? 1 : 0) > 1)
+                       p->cfg.regs[2] |= 1;
        }
 
        if (pc->immd_nr) {
@@ -2454,12 +3112,16 @@ ctor_nv50_pc(struct nv50_pc *pc, struct nv50_program *p)
        pc->attr_nr = p->info.file_max[TGSI_FILE_INPUT] + 1;
        pc->result_nr = p->info.file_max[TGSI_FILE_OUTPUT] + 1;
        pc->param_nr = p->info.file_max[TGSI_FILE_CONSTANT] + 1;
+       pc->addr_nr = p->info.file_max[TGSI_FILE_ADDRESS] + 1;
+       assert(pc->addr_nr <= 2);
 
        p->cfg.high_temp = 4;
 
        p->cfg.two_side[0].hw = 0x40;
        p->cfg.two_side[1].hw = 0x40;
 
+       p->cfg.edgeflag_in = pc->edgeflag_out = 0xff;
+
        switch (p->type) {
        case PIPE_SHADER_VERTEX:
                p->cfg.psiz = 0x40;
@@ -2522,31 +3184,21 @@ ctor_nv50_pc(struct nv50_pc *pc, struct nv50_program *p)
                                ctor_reg(&pc->param[rid], P_CONST, i, rid);
        }
 
-       return TRUE;
-}
-
-static void
-nv50_fp_move_results(struct nv50_pc *pc)
-{
-       struct nv50_reg reg;
-       unsigned i;
-
-       ctor_reg(&reg, P_TEMP, -1, -1);
-
-       for (i = 0; i < pc->result_nr * 4; ++i) {
-               if (pc->result[i].rhw < 0 || pc->result[i].hw < 0)
-                       continue;
-               if (pc->result[i].rhw != pc->result[i].hw) {
-                       reg.hw = pc->result[i].rhw;
-                       emit_mov(pc, &reg, &pc->result[i]);
-               }
+       if (pc->addr_nr) {
+               pc->addr = CALLOC(pc->addr_nr * 4, sizeof(struct nv50_reg *));
+               if (!pc->addr)
+                       return FALSE;
        }
+       for (i = 0; i < NV50_SU_MAX_ADDR; ++i)
+               ctor_reg(&pc->r_addr[i], P_ADDR, -256, i + 1);
+
+       return TRUE;
 }
 
 static void
 nv50_program_fixup_insns(struct nv50_pc *pc)
 {
-       struct nv50_program_exec *e, *prev = NULL, **bra_list;
+       struct nv50_program_exec *e, **bra_list;
        unsigned i, n, pos;
 
        bra_list = CALLOC(pc->p->exec_size, sizeof(struct nv50_program_exec *));
@@ -2558,6 +3210,16 @@ nv50_program_fixup_insns(struct nv50_pc *pc)
                if (e->param.index >= 0 && !e->param.mask)
                        bra_list[n++] = e;
 
+       /* last instruction must be long so it can have the exit bit set */
+       if (!is_long(pc->p->exec_tail))
+               convert_to_long(pc, pc->p->exec_tail);
+       /* set exit bit */
+       pc->p->exec_tail->inst[1] |= 1;
+
+       /* !immd on exit insn simultaneously means !join */
+       assert(!is_immd(pc->p->exec_head));
+       assert(!is_immd(pc->p->exec_tail));
+
        /* Make sure we don't have any single 32 bit instructions. */
        for (e = pc->p->exec_head, pos = 0; e; e = e->next) {
                pos += is_long(e) ? 2 : 1;
@@ -2569,22 +3231,7 @@ nv50_program_fixup_insns(struct nv50_pc *pc)
                        convert_to_long(pc, e);
                        ++pos;
                }
-               if (e->next)
-                       prev = e;
-       }
-
-       assert(!is_immd(pc->p->exec_head));
-       assert(!is_immd(pc->p->exec_tail));
-
-       /* last instruction must be long so it can have the end bit set */
-       if (!is_long(pc->p->exec_tail)) {
-               convert_to_long(pc, pc->p->exec_tail);
-               if (prev)
-                       convert_to_long(pc, prev);
        }
-       assert(!(pc->p->exec_tail->inst[1] & 2));
-       /* set the end-bit */
-       pc->p->exec_tail->inst[1] |= 1;
 
        FREE(bra_list);
 }
@@ -2657,7 +3304,7 @@ nv50_program_validate(struct nv50_context *nv50, struct nv50_program *p)
 }
 
 static void
-nv50_program_upload_data(struct nv50_context *nv50, float *map,
+nv50_program_upload_data(struct nv50_context *nv50, uint32_t *map,
                        unsigned start, unsigned count, unsigned cbuf)
 {
        struct nouveau_channel *chan = nv50->screen->base.channel;
@@ -2701,12 +3348,12 @@ nv50_program_validate_data(struct nv50_context *nv50, struct nv50_program *p)
                                         p->immd_nr, NV50_CB_PMISC);
        }
 
-       assert(p->param_nr <= 128);
+       assert(p->param_nr <= 512);
 
        if (p->param_nr) {
                unsigned cb;
-               float *map = pipe_buffer_map(pscreen, nv50->constbuf[p->type],
-                                            PIPE_BUFFER_USAGE_CPU_READ);
+               uint32_t *map = pipe_buffer_map(pscreen, nv50->constbuf[p->type],
+                                               PIPE_BUFFER_USAGE_CPU_READ);
 
                if (p->type == PIPE_SHADER_VERTEX)
                        cb = NV50_CB_PVP;
@@ -2722,11 +3369,8 @@ static void
 nv50_program_validate_code(struct nv50_context *nv50, struct nv50_program *p)
 {
        struct nouveau_channel *chan = nv50->screen->base.channel;
-       struct nouveau_grobj *tesla = nv50->screen->tesla;
        struct nv50_program_exec *e;
-       struct nouveau_stateobj *so;
-       const unsigned flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_WR;
-       unsigned start, count, *up, *ptr;
+       uint32_t *up, i;
        boolean upload = FALSE;
 
        if (!p->bo) {
@@ -2741,32 +3385,37 @@ nv50_program_validate_code(struct nv50_context *nv50, struct nv50_program *p)
        if (!upload)
                return;
 
-       for (e = p->exec_head; e; e = e->next) {
+       up = MALLOC(p->exec_size * 4);
+
+       for (i = 0, e = p->exec_head; e; e = e->next) {
                unsigned ei, ci, bs;
 
-               if (e->param.index < 0)
-                       continue;
+               if (e->param.index >= 0 && e->param.mask) {
+                       bs = (e->inst[1] >> 22) & 0x07;
+                       assert(bs < 2);
+                       ei = e->param.shift >> 5;
+                       ci = e->param.index;
+                       if (bs == 0)
+                               ci += p->data[bs]->start;
 
-               if (e->param.mask == 0) {
+                       e->inst[ei] &= ~e->param.mask;
+                       e->inst[ei] |= (ci << e->param.shift);
+               } else
+               if (e->param.index >= 0) {
+                       /* zero mask means param is a jump/branch offset */
                        assert(!(e->param.index & 1));
                        /* seem to be 8 byte steps */
                        ei = (e->param.index >> 1) + 0 /* START_ID */;
 
                        e->inst[0] &= 0xf0000fff;
                        e->inst[0] |= ei << 12;
-                       continue;
                }
 
-               bs = (e->inst[1] >> 22) & 0x07;
-               assert(bs < 2);
-               ei = e->param.shift >> 5;
-               ci = e->param.index;
-               if (bs == 0)
-                       ci += p->data[bs]->start;
-
-               e->inst[ei] &= ~e->param.mask;
-               e->inst[ei] |= (ci << e->param.shift);
+               up[i++] = e->inst[0];
+               if (is_long(e))
+                       up[i++] = e->inst[1];
        }
+       assert(i == p->exec_size);
 
        if (p->data[0])
                p->data_start[0] = p->data[0]->start;
@@ -2779,45 +3428,12 @@ nv50_program_validate_code(struct nv50_context *nv50, struct nv50_program *p)
                        NOUVEAU_ERR("0x%08x\n", e->inst[1]);
        }
 #endif
-
-       up = ptr = MALLOC(p->exec_size * 4);
-       for (e = p->exec_head; e; e = e->next) {
-               *(ptr++) = e->inst[0];
-               if (is_long(e))
-                       *(ptr++) = e->inst[1];
-       }
-
-       so = so_new(4,2);
-       so_method(so, nv50->screen->tesla, NV50TCL_CB_DEF_ADDRESS_HIGH, 3);
-       so_reloc (so, p->bo, 0, flags | NOUVEAU_BO_HIGH, 0, 0);
-       so_reloc (so, p->bo, 0, flags | NOUVEAU_BO_LOW, 0, 0);
-       so_data  (so, (NV50_CB_PUPLOAD << 16) | 0x0800); //(p->exec_size * 4));
-
-       start = 0; count = p->exec_size;
-       while (count) {
-               struct nouveau_channel *chan = nv50->screen->base.channel;
-               unsigned nr;
-
-               so_emit(chan, so);
-
-               nr = MIN2(count, 2047);
-               nr = MIN2(chan->pushbuf->remaining, nr);
-               if (chan->pushbuf->remaining < (nr + 3)) {
-                       FIRE_RING(chan);
-                       continue;
-               }
-
-               BEGIN_RING(chan, tesla, NV50TCL_CB_ADDR, 1);
-               OUT_RING  (chan, (start << 8) | NV50_CB_PUPLOAD);
-               BEGIN_RING(chan, tesla, NV50TCL_CB_DATA(0) | 0x40000000, nr);
-               OUT_RINGp (chan, up + start, nr);
-
-               start += nr;
-               count -= nr;
-       }
+       nv50_upload_sifc(nv50, p->bo, 0, NOUVEAU_BO_VRAM,
+                        NV50_2D_DST_FORMAT_R8_UNORM, 65536, 1, 262144,
+                        up, NV50_2D_SIFC_FORMAT_R8_UNORM, 0,
+                        0, 0, p->exec_size * 4, 1, 1);
 
        FREE(up);
-       so_ref(NULL, &so);
 }
 
 void
@@ -2882,7 +3498,7 @@ nv50_fragprog_validate(struct nv50_context *nv50)
        so_data  (so, p->cfg.high_temp);
        so_method(so, tesla, NV50TCL_FP_RESULT_COUNT, 1);
        so_data  (so, p->cfg.high_result);
-       so_method(so, tesla, NV50TCL_FP_CTRL_UNK19A8, 1);
+       so_method(so, tesla, NV50TCL_FP_CONTROL, 1);
        so_data  (so, p->cfg.regs[2]);
        so_method(so, tesla, NV50TCL_FP_CTRL_UNK196C, 1);
        so_data  (so, p->cfg.regs[3]);
@@ -2899,15 +3515,15 @@ nv50_pntc_replace(struct nv50_context *nv50, uint32_t pntc[8], unsigned base)
        struct nv50_program *vp = nv50->vertprog;
        unsigned i, c, m = base;
 
-       /* XXX: This can't work correctly in all cases yet, we either
-        * have to create TGSI_SEMANTIC_PNTC or sprite_coord_mode has
-        * to be per FP input instead of per VP output
+       /* XXX: this might not work correctly in all cases yet - we'll
+        * just assume that an FP generic input that is not written in
+        * the VP is PointCoord.
         */
        memset(pntc, 0, 8 * sizeof(uint32_t));
 
        for (i = 0; i < fp->cfg.io_nr; i++) {
                uint8_t sn, si;
-               uint8_t j = fp->cfg.io[i].id_vp, k = fp->cfg.io[i].id_fp;
+               uint8_t j, k = fp->cfg.io[i].id;
                unsigned n = popcnt4(fp->cfg.io[i].mask);
 
                if (fp->info.input_semantic_name[k] != TGSI_SEMANTIC_GENERIC) {
@@ -2915,10 +3531,16 @@ nv50_pntc_replace(struct nv50_context *nv50, uint32_t pntc[8], unsigned base)
                        continue;
                }
 
-               sn = vp->info.input_semantic_name[j];
-               si = vp->info.input_semantic_index[j];
+               for (j = 0; j < vp->info.num_outputs; ++j) {
+                       sn = vp->info.output_semantic_name[j];
+                       si = vp->info.output_semantic_index[j];
 
-               if (j < fp->cfg.io_nr && sn == TGSI_SEMANTIC_GENERIC) {
+                       if (sn == fp->info.input_semantic_name[k] &&
+                           si == fp->info.input_semantic_index[k])
+                               break;
+               }
+
+               if (j < vp->info.num_outputs) {
                        ubyte mode =
                                nv50->rasterizer->pipe.sprite_coord_mode[si];
 
@@ -3006,20 +3628,24 @@ nv50_linkage_validate(struct nv50_context *nv50)
        reg[0] += m - 4; /* adjust FFC0 id */
        reg[4] |= m << 8; /* set mid where 'normal' FP inputs start */
 
-       i = 0;
-       if (fp->info.input_semantic_name[0] == TGSI_SEMANTIC_POSITION)
-               i = 1;
-       for (; i < fp->cfg.io_nr; i++) {
-               ubyte sn = fp->info.input_semantic_name[fp->cfg.io[i].id_fp];
-               ubyte si = fp->info.input_semantic_index[fp->cfg.io[i].id_fp];
-
-               n = fp->cfg.io[i].id_vp;
-               if (n >= vp->cfg.io_nr ||
-                   vp->info.output_semantic_name[n] != sn ||
-                   vp->info.output_semantic_index[n] != si)
-                       vpo = &dummy;
-               else
-                       vpo = &vp->cfg.io[n];
+       for (i = 0; i < fp->cfg.io_nr; i++) {
+               ubyte sn = fp->info.input_semantic_name[fp->cfg.io[i].id];
+               ubyte si = fp->info.input_semantic_index[fp->cfg.io[i].id];
+
+               /* position must be mapped first */
+               assert(i == 0 || sn != TGSI_SEMANTIC_POSITION);
+
+               /* maybe even remove these from cfg.io */
+               if (sn == TGSI_SEMANTIC_POSITION || sn == TGSI_SEMANTIC_FACE)
+                       continue;
+
+               /* VP outputs and vp->cfg.io are in the same order */
+               for (n = 0; n < vp->info.num_outputs; ++n) {
+                       if (vp->info.output_semantic_name[n] == sn &&
+                           vp->info.output_semantic_index[n] == si)
+                               break;
+               }
+               vpo = (n < vp->info.num_outputs) ? &vp->cfg.io[n] : &dummy;
 
                m = nv50_sreg4_map(map, m, lin, &fp->cfg.io[i], vpo);
        }
@@ -3044,7 +3670,7 @@ nv50_linkage_validate(struct nv50_context *nv50)
        so_method(so, tesla, NV50TCL_FP_INTERPOLANT_CTRL, 1);
        so_data  (so, reg[4]);
 
-       so_method(so, tesla, 0x1540, 4);
+       so_method(so, tesla, NV50TCL_NOPERSPECTIVE_BITMAP(0), 4);
        so_datap (so, lin, 4);
 
        if (nv50->rasterizer->pipe.point_sprite) {