radeonsi: Fix draws using user index buffer.
[mesa.git] / src / gallium / drivers / nv50 / nv50_shader_state.c
index 7d2989da05b83119cd02fc7eb46a5cda30a970e6..7f0524373579dd3bcb02067d384a06aa228d4585 100644 (file)
 
 #include "nv50_context.h"
 
-static void
-nv50_transfer_constbuf(struct nv50_context *nv50,
-                       struct pipe_resource *buf, unsigned size, unsigned cbi)
+void
+nv50_constbufs_validate(struct nv50_context *nv50)
 {
-   struct pipe_context *pipe = &nv50->pipe;
-   struct pipe_transfer *transfer;
-   struct nouveau_channel *chan = nv50->screen->base.channel;
-   struct nouveau_grobj *tesla = nv50->screen->tesla;
-   uint32_t *map;
-   unsigned count, start;
-
-   map = pipe_buffer_map(pipe, buf, PIPE_TRANSFER_READ, &transfer);
-   if (!map)
-      return;
-
-   count = (buf->width0 + 3) / 4;
-   start = 0;
-
-   while (count) {
-      unsigned nr = AVAIL_RING(chan);
-
-      if (nr < 8) {
-         FIRE_RING(chan);
-         continue;
-      }
-      nr = MIN2(count, nr - 7);
-      nr = MIN2(nr, 2074);
-
-      nv50_screen_reloc_constbuf(nv50->screen, cbi);
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   unsigned s;
 
-      BEGIN_RING(chan, tesla, NV50TCL_CB_ADDR, 1);
-      OUT_RING  (chan, (start << 8) | cbi);
-      BEGIN_RING_NI(chan, tesla, NV50TCL_CB_DATA(0), nr);
-      OUT_RINGp (chan, map, nr);
-
-      count -= nr;
-      start += nr;
-      map += nr;
-   }
+   for (s = 0; s < 3; ++s) {
+      unsigned p;
 
-   pipe_buffer_unmap(pipe, buf, transfer);
-}
-
-static void
-nv50_program_validate_data(struct nv50_context *nv50, struct nv50_program *p)
-{
-   struct nouveau_channel *chan = nv50->screen->base.channel;
-   struct nouveau_grobj *tesla = nv50->screen->tesla;
-   unsigned cbi;
-
-   if (p->immd_size) {
-      uint32_t *data = p->immd;
-      unsigned count = p->immd_size / 4;
-      unsigned start = 0;
-
-      while (count) {
-         unsigned nr = AVAIL_RING(chan);
-
-         if (nr < 8) {
-            FIRE_RING(chan);
-            continue;
+      if (s == PIPE_SHADER_FRAGMENT)
+         p = NV50_3D_SET_PROGRAM_CB_PROGRAM_FRAGMENT;
+      else
+      if (s == PIPE_SHADER_GEOMETRY)
+         p = NV50_3D_SET_PROGRAM_CB_PROGRAM_GEOMETRY;
+      else
+         p = NV50_3D_SET_PROGRAM_CB_PROGRAM_VERTEX;
+
+      while (nv50->constbuf_dirty[s]) {
+         const int i = ffs(nv50->constbuf_dirty[s]) - 1;
+         nv50->constbuf_dirty[s] &= ~(1 << i);
+
+         if (nv50->constbuf[s][i].user) {
+            const unsigned b = NV50_CB_PVP + s;
+            unsigned start = 0;
+            unsigned words = nv50->constbuf[s][0].size / 4;
+            if (i) {
+               NOUVEAU_ERR("user constbufs only supported in slot 0\n");
+               continue;
+            }
+            if (!nv50->state.uniform_buffer_bound[s]) {
+               nv50->state.uniform_buffer_bound[s] = TRUE;
+               BEGIN_NV04(push, NV50_3D(SET_PROGRAM_CB), 1);
+               PUSH_DATA (push, (b << 12) | (i << 8) | p | 1);
+            }
+            while (words) {
+               unsigned nr;
+
+               if (!PUSH_SPACE(push, 16))
+                  break;
+               nr = PUSH_AVAIL(push);
+               assert(nr >= 16);
+               nr = MIN2(MIN2(nr - 3, words), NV04_PFIFO_MAX_PACKET_LEN);
+
+               BEGIN_NV04(push, NV50_3D(CB_ADDR), 1);
+               PUSH_DATA (push, (start << 8) | b);
+               BEGIN_NI04(push, NV50_3D(CB_DATA(0)), nr);
+               PUSH_DATAp(push, &nv50->constbuf[s][0].u.data[start * 4], nr);
+
+               start += nr;
+               words -= nr;
+            }
+         } else {
+            struct nv04_resource *res =
+               nv04_resource(nv50->constbuf[s][i].u.buf);
+            if (res) {
+               /* TODO: allocate persistent bindings */
+               const unsigned b = s * 16 + i;
+
+               assert(nouveau_resource_mapped_by_gpu(&res->base));
+
+               BEGIN_NV04(push, NV50_3D(CB_DEF_ADDRESS_HIGH), 3);
+               PUSH_DATAh(push, res->address + nv50->constbuf[s][i].offset);
+               PUSH_DATA (push, res->address + nv50->constbuf[s][i].offset);
+               PUSH_DATA (push, (b << 16) |
+                          (nv50->constbuf[s][i].size & 0xffff));
+               BEGIN_NV04(push, NV50_3D(SET_PROGRAM_CB), 1);
+               PUSH_DATA (push, (b << 12) | (i << 8) | p | 1);
+
+               BCTX_REFN(nv50->bufctx_3d, CB(s, i), res, RD);
+            } else {
+               BEGIN_NV04(push, NV50_3D(SET_PROGRAM_CB), 1);
+               PUSH_DATA (push, (i << 8) | p | 0);
+            }
+            if (i == 0)
+               nv50->state.uniform_buffer_bound[s] = FALSE;
          }
-         nr = MIN2(count, nr - 7);
-         nr = MIN2(nr, 2074);
-
-         nv50_screen_reloc_constbuf(nv50->screen, NV50_CB_PMISC);
-
-         BEGIN_RING(chan, tesla, NV50TCL_CB_ADDR, 1);
-         OUT_RING  (chan, (start << 8) | NV50_CB_PMISC);
-         BEGIN_RING_NI(chan, tesla, NV50TCL_CB_DATA(0), nr);
-         OUT_RINGp (chan, data, nr);
-
-         count -= nr;
-         start += nr;
-         data += nr;
       }
    }
-
-   /* If the state tracker doesn't change the constbuf, and it is first
-    * validated with a program that doesn't use it, this check prevents
-    * it from even being uploaded. */
-   /*
-   if (p->parm_size == 0)
-      return;
-   */
-
-   switch (p->type) {
-   case PIPE_SHADER_VERTEX:
-      cbi = NV50_CB_PVP;
-      break;
-   case PIPE_SHADER_FRAGMENT:
-      cbi = NV50_CB_PFP;
-      break;
-   case PIPE_SHADER_GEOMETRY:
-      cbi = NV50_CB_PGP;
-      break;
-   default:
-      assert(0);
-      return;
-   }
-
-   nv50_transfer_constbuf(nv50, nv50->constbuf[p->type], p->parm_size, cbi);
-}
-
-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 nouveau_grobj *eng2d = nv50->screen->eng2d;
-   int ret;
-   unsigned offset;
-   unsigned size = p->code_size;
-   uint32_t *data = p->code;
-
-   assert(p->translated);
-
-   /* TODO: use a single bo (for each type) for shader code */
-   if (p->bo)
-      return;
-   ret = nouveau_bo_new(chan->device, NOUVEAU_BO_VRAM, 0x100, size, &p->bo);
-   assert(!ret);
-
-   offset = p->code_start = 0;
-
-   BEGIN_RING(chan, eng2d, NV50_2D_DST_FORMAT, 2);
-   OUT_RING  (chan, NV50_2D_DST_FORMAT_R8_UNORM);
-   OUT_RING  (chan, 1);
-   BEGIN_RING(chan, eng2d, NV50_2D_DST_PITCH, 1);
-   OUT_RING  (chan, 0x40000);
-   BEGIN_RING(chan, eng2d, NV50_2D_DST_WIDTH, 2);
-   OUT_RING  (chan, 0x10000);
-   OUT_RING  (chan, 1);
-
-   while (size) {
-      unsigned nr = size / 4;
-
-      if (AVAIL_RING(chan) < 32)
-         FIRE_RING(chan);
-
-      nr = MIN2(nr, AVAIL_RING(chan) - 18);
-      nr = MIN2(nr, 1792);
-      if (nr < (size / 4))
-         nr &= ~0x3f;
-      assert(!(size & 3));
-
-      BEGIN_RING(chan, eng2d, NV50_2D_DST_ADDRESS_HIGH, 2);
-      OUT_RELOCh(chan, p->bo, offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
-      OUT_RELOCl(chan, p->bo, offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
-      BEGIN_RING(chan, eng2d, NV50_2D_SIFC_BITMAP_ENABLE, 2);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, NV50_2D_SIFC_FORMAT_R8_UNORM);
-      BEGIN_RING(chan, eng2d, NV50_2D_SIFC_WIDTH, 10);
-      OUT_RING  (chan, nr * 4);
-      OUT_RING  (chan, 1);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, 1);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, 1);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, 0);
-
-      BEGIN_RING_NI(chan, eng2d, NV50_2D_SIFC_DATA, nr);
-      OUT_RINGp (chan, data, nr);
-
-      data += nr;
-      offset += nr * 4;
-      size -= nr * 4;
-   }
-
-   BEGIN_RING(chan, tesla, NV50TCL_CODE_CB_FLUSH, 1);
-   OUT_RING  (chan, 0);
 }
 
-static void
-nv50_vp_update_stateobj(struct nv50_context *nv50, struct nv50_program *p)
+static boolean
+nv50_program_validate(struct nv50_context *nv50, struct nv50_program *prog)
 {
-   struct nouveau_grobj *tesla = nv50->screen->tesla;
-   struct nouveau_stateobj *so = so_new(5, 7, 2);
-
-   nv50_program_validate_code(nv50, p);
-
-   so_method(so, tesla, NV50TCL_VP_ADDRESS_HIGH, 2);
-   so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
-             NOUVEAU_BO_HIGH, 0, 0);
-   so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
-             NOUVEAU_BO_LOW, 0, 0);
-   so_method(so, tesla, NV50TCL_VP_ATTR_EN_0, 2);
-   so_data  (so, p->vp.attrs[0]);
-   so_data  (so, p->vp.attrs[1]);
-   so_method(so, tesla, NV50TCL_VP_REG_ALLOC_RESULT, 1);
-   so_data  (so, p->max_out);
-   so_method(so, tesla, NV50TCL_VP_REG_ALLOC_TEMP, 1);
-   so_data  (so, p->max_gpr);
-   so_method(so, tesla, NV50TCL_VP_START_ID, 1);
-   so_data  (so, p->code_start);
-
-   so_ref(so, &p->so);
-   so_ref(NULL, &so);
+   if (!prog->translated) {
+      prog->translated = nv50_program_translate(
+         prog, nv50->screen->base.device->chipset);
+      if (!prog->translated)
+         return FALSE;
+   } else
+   if (prog->mem)
+      return TRUE;
+
+   return nv50_program_upload_code(nv50, prog);
 }
 
-static void
-nv50_fp_update_stateobj(struct nv50_context *nv50, struct nv50_program *p)
+static INLINE void
+nv50_program_update_context_state(struct nv50_context *nv50,
+                                  struct nv50_program *prog, int stage)
 {
-   struct nouveau_grobj *tesla = nv50->screen->tesla;
-       struct nouveau_stateobj *so = so_new(6, 7, 2);
-
-   nv50_program_validate_code(nv50, p);
-
-   so_method(so, tesla, NV50TCL_FP_ADDRESS_HIGH, 2);
-   so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
-             NOUVEAU_BO_HIGH, 0, 0);
-   so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
-             NOUVEAU_BO_LOW, 0, 0);
-   so_method(so, tesla, NV50TCL_FP_REG_ALLOC_TEMP, 1);
-   so_data  (so, p->max_gpr);
-   so_method(so, tesla, NV50TCL_FP_RESULT_COUNT, 1);
-   so_data  (so, p->max_out);
-   so_method(so, tesla, NV50TCL_FP_CONTROL, 1);
-   so_data  (so, p->fp.flags[0]);
-   so_method(so, tesla, NV50TCL_FP_CTRL_UNK196C, 1);
-   so_data  (so, p->fp.flags[1]);
-   so_method(so, tesla, NV50TCL_FP_START_ID, 1);
-   so_data  (so, p->code_start);
-
-   so_ref(so, &p->so);
-   so_ref(NULL, &so);
+   const unsigned flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
+
+   if (prog && prog->tls_space) {
+      if (nv50->state.new_tls_space)
+         nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_TLS);
+      if (!nv50->state.tls_required || nv50->state.new_tls_space)
+         BCTX_REFN_bo(nv50->bufctx_3d, TLS, flags, nv50->screen->tls_bo);
+      nv50->state.new_tls_space = FALSE;
+      nv50->state.tls_required |= 1 << stage;
+   } else {
+      if (nv50->state.tls_required == (1 << stage))
+         nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_TLS);
+      nv50->state.tls_required &= ~(1 << stage);
+   }
 }
 
-static void
-nv50_gp_update_stateobj(struct nv50_context *nv50, struct nv50_program *p)
+void
+nv50_vertprog_validate(struct nv50_context *nv50)
 {
-   struct nouveau_grobj *tesla = nv50->screen->tesla;
-       struct nouveau_stateobj *so = so_new(6, 7, 2);
-
-   nv50_program_validate_code(nv50, p);
-
-   so_method(so, tesla, NV50TCL_GP_ADDRESS_HIGH, 2);
-   so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
-             NOUVEAU_BO_HIGH, 0, 0);
-   so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
-             NOUVEAU_BO_LOW, 0, 0);
-   so_method(so, tesla, NV50TCL_GP_REG_ALLOC_TEMP, 1);
-   so_data  (so, p->max_gpr);
-   so_method(so, tesla, NV50TCL_GP_REG_ALLOC_RESULT, 1);
-   so_data  (so, p->max_out);
-   so_method(so, tesla, NV50TCL_GP_OUTPUT_PRIMITIVE_TYPE, 1);
-   so_data  (so, p->gp.prim_type);
-   so_method(so, tesla, NV50TCL_GP_VERTEX_OUTPUT_COUNT, 1);
-   so_data  (so, p->gp.vert_count);
-   so_method(so, tesla, NV50TCL_GP_START_ID, 1);
-   so_data  (so, p->code_start);
-
-   so_ref(so, &p->so);
-   so_ref(NULL, &so);
-}
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   struct nv50_program *vp = nv50->vertprog;
 
-static boolean
-nv50_program_validate(struct nv50_program *p)
-{
-   p->translated = nv50_program_tx(p);
-   assert(p->translated);
-   return p->translated;
+   if (!nv50_program_validate(nv50, vp))
+         return;
+   nv50_program_update_context_state(nv50, vp, 0);
+
+   BEGIN_NV04(push, NV50_3D(VP_ATTR_EN(0)), 2);
+   PUSH_DATA (push, vp->vp.attrs[0]);
+   PUSH_DATA (push, vp->vp.attrs[1]);
+   BEGIN_NV04(push, NV50_3D(VP_REG_ALLOC_RESULT), 1);
+   PUSH_DATA (push, vp->max_out);
+   BEGIN_NV04(push, NV50_3D(VP_REG_ALLOC_TEMP), 1);
+   PUSH_DATA (push, vp->max_gpr);
+   BEGIN_NV04(push, NV50_3D(VP_START_ID), 1);
+   PUSH_DATA (push, vp->code_base);
 }
 
-static INLINE void
-nv50_program_validate_common(struct nv50_context *nv50, struct nv50_program *p)
+void
+nv50_fragprog_validate(struct nv50_context *nv50)
 {
-   nv50_program_validate_code(nv50, p);
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   struct nv50_program *fp = nv50->fragprog;
 
-   if (p->uses_lmem)
-      nv50->req_lmem |= 1 << p->type;
-   else
-      nv50->req_lmem &= ~(1 << p->type);
+   if (!nv50_program_validate(nv50, fp))
+         return;
+   nv50_program_update_context_state(nv50, fp, 1);
+
+   BEGIN_NV04(push, NV50_3D(FP_REG_ALLOC_TEMP), 1);
+   PUSH_DATA (push, fp->max_gpr);
+   BEGIN_NV04(push, NV50_3D(FP_RESULT_COUNT), 1);
+   PUSH_DATA (push, fp->max_out);
+   BEGIN_NV04(push, NV50_3D(FP_CONTROL), 1);
+   PUSH_DATA (push, fp->fp.flags[0]);
+   BEGIN_NV04(push, NV50_3D(FP_CTRL_UNK196C), 1);
+   PUSH_DATA (push, fp->fp.flags[1]);
+   BEGIN_NV04(push, NV50_3D(FP_START_ID), 1);
+   PUSH_DATA (push, fp->code_base);
 }
 
-struct nouveau_stateobj *
-nv50_vertprog_validate(struct nv50_context *nv50)
+void
+nv50_gmtyprog_validate(struct nv50_context *nv50)
 {
-   struct nv50_program *p = nv50->vertprog;
-   struct nouveau_stateobj *so = NULL;
-
-   if (!p->translated) {
-      if (nv50_program_validate(p))
-         nv50_vp_update_stateobj(nv50, p);
-      else
-         return NULL;
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   struct nv50_program *gp = nv50->gmtyprog;
+
+   if (gp) {
+      BEGIN_NV04(push, NV50_3D(GP_REG_ALLOC_TEMP), 1);
+      PUSH_DATA (push, gp->max_gpr);
+      BEGIN_NV04(push, NV50_3D(GP_REG_ALLOC_RESULT), 1);
+      PUSH_DATA (push, gp->max_out);
+      BEGIN_NV04(push, NV50_3D(GP_OUTPUT_PRIMITIVE_TYPE), 1);
+      PUSH_DATA (push, gp->gp.prim_type);
+      BEGIN_NV04(push, NV50_3D(GP_VERTEX_OUTPUT_COUNT), 1);
+      PUSH_DATA (push, gp->gp.vert_count);
+      BEGIN_NV04(push, NV50_3D(GP_START_ID), 1);
+      PUSH_DATA (push, gp->code_base);
+
+      nv50->state.prim_size = gp->gp.prim_type; /* enum matches vertex count */
    }
+   nv50_program_update_context_state(nv50, gp, 2);
 
-   if (nv50->dirty & NV50_NEW_VERTPROG_CB)
-      nv50_program_validate_data(nv50, p);
-
-   if (!(nv50->dirty & NV50_NEW_VERTPROG))
-      return NULL;
-
-   nv50_program_validate_common(nv50, p);
-
-   so_ref(p->so, &so);
-   return so;
+   /* GP_ENABLE is updated in linkage validation */
 }
 
-struct nouveau_stateobj *
-nv50_fragprog_validate(struct nv50_context *nv50)
+static void
+nv50_sprite_coords_validate(struct nv50_context *nv50)
 {
-   struct nv50_program *p = nv50->fragprog;
-   struct nouveau_stateobj *so = NULL;
-
-   if (!p->translated) {
-      if (nv50_program_validate(p))
-         nv50_fp_update_stateobj(nv50, p);
-      else
-         return NULL;
-   }
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   uint32_t pntc[8], mode;
+   struct nv50_program *fp = nv50->fragprog;
+   unsigned i, c;
+   unsigned m = (nv50->state.interpolant_ctrl >> 8) & 0xff;
 
-   if (nv50->dirty & NV50_NEW_FRAGPROG_CB)
-      nv50_program_validate_data(nv50, p);
+   if (!nv50->rast->pipe.point_quad_rasterization) {
+      if (nv50->state.point_sprite) {
+         BEGIN_NV04(push, NV50_3D(POINT_COORD_REPLACE_MAP(0)), 8);
+         for (i = 0; i < 8; ++i)
+            PUSH_DATA(push, 0);
 
-   if (!(nv50->dirty & NV50_NEW_FRAGPROG))
-      return NULL;
+         nv50->state.point_sprite = FALSE;
+      }
+      return;
+   } else {
+      nv50->state.point_sprite = TRUE;
+   }
 
-   nv50_program_validate_common(nv50, p);
+   memset(pntc, 0, sizeof(pntc));
 
-   so_ref(p->so, &so);
-   return so;
-}
+   for (i = 0; i < fp->in_nr; i++) {
+      unsigned n = util_bitcount(fp->in[i].mask);
 
-struct nouveau_stateobj *
-nv50_geomprog_validate(struct nv50_context *nv50)
-{
-   struct nv50_program *p = nv50->geomprog;
-   struct nouveau_stateobj *so = NULL;
+      if (fp->in[i].sn != TGSI_SEMANTIC_GENERIC) {
+         m += n;
+         continue;
+      }
+      if (!(nv50->rast->pipe.sprite_coord_enable & (1 << fp->in[i].si))) {
+         m += n;
+         continue;
+      }
 
-   if (!p->translated) {
-      if (nv50_program_validate(p))
-         nv50_gp_update_stateobj(nv50, p);
-      else
-         return NULL;
+      for (c = 0; c < 4; ++c) {
+         if (fp->in[i].mask & (1 << c)) {
+            pntc[m / 8] |= (c + 1) << ((m % 8) * 4);
+            ++m;
+         }
+      }
    }
 
-   if (nv50->dirty & NV50_NEW_GEOMPROG_CB)
-      nv50_program_validate_data(nv50, p);
-
-   if (!(nv50->dirty & NV50_NEW_GEOMPROG))
-      return NULL;
+   if (nv50->rast->pipe.sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
+      mode = 0x00;
+   else
+      mode = 0x10;
 
-   nv50_program_validate_common(nv50, p);
+   BEGIN_NV04(push, NV50_3D(POINT_SPRITE_CTRL), 1);
+   PUSH_DATA (push, mode);
 
-   so_ref(p->so, &so);
-   return so;
+   BEGIN_NV04(push, NV50_3D(POINT_COORD_REPLACE_MAP(0)), 8);
+   PUSH_DATAp(push, pntc, 8);
 }
 
-/* XXX: this might not work correctly in all cases yet: we assume that
- * an FP generic input that is not written in the VP is gl_PointCoord.
- */
-static uint32_t
-nv50_pntc_replace(struct nv50_context *nv50, uint32_t pntc[8], unsigned m)
+/* Validate state derived from shaders and the rasterizer cso. */
+void
+nv50_validate_derived_rs(struct nv50_context *nv50)
 {
-   struct nv50_program *vp = nv50->vertprog;
-   struct nv50_program *fp = nv50->fragprog;
-   unsigned i, c;
-
-   memset(pntc, 0, 8 * sizeof(uint32_t));
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   uint32_t color, psize;
 
-   if (nv50->geomprog)
-      vp = nv50->geomprog;
+   nv50_sprite_coords_validate(nv50);
 
-   for (i = 0; i < fp->in_nr; i++) {
-      unsigned j, n = util_bitcount(fp->in[i].mask);
+   if (nv50->state.rasterizer_discard != nv50->rast->pipe.rasterizer_discard) {
+      nv50->state.rasterizer_discard = nv50->rast->pipe.rasterizer_discard;
+      BEGIN_NV04(push, NV50_3D(RASTERIZE_ENABLE), 1);
+      PUSH_DATA (push, !nv50->rast->pipe.rasterizer_discard);
+   }
 
-      if (fp->in[i].sn != TGSI_SEMANTIC_GENERIC) {
-         m += n;
-         continue;
-      }
+   if (nv50->dirty & NV50_NEW_FRAGPROG)
+      return;
+   psize = nv50->state.semantic_psize & ~NV50_3D_SEMANTIC_PTSZ_PTSZ_EN__MASK;
+   color = nv50->state.semantic_color & ~NV50_3D_SEMANTIC_COLOR_CLMP_EN;
 
-      for (j = 0; j < vp->out_nr; ++j)
-         if (vp->out[j].sn == fp->in[i].sn && vp->out[j].si == fp->in[i].si)
-            break;
+   if (nv50->rast->pipe.clamp_vertex_color)
+      color |= NV50_3D_SEMANTIC_COLOR_CLMP_EN;
 
-      if (j < vp->out_nr) {
-         uint32_t en = nv50->rasterizer->pipe.sprite_coord_enable;
+   if (color != nv50->state.semantic_color) {
+      nv50->state.semantic_color = color;
+      BEGIN_NV04(push, NV50_3D(SEMANTIC_COLOR), 1);
+      PUSH_DATA (push, color);
+   }
 
-         if (!(en & (1 << vp->out[j].si))) {
-            m += n;
-            continue;
-         }
-      }
+   if (nv50->rast->pipe.point_size_per_vertex)
+      psize |= NV50_3D_SEMANTIC_PTSZ_PTSZ_EN__MASK;
 
-      /* this is either PointCoord or replaced by sprite coords */
-      for (c = 0; c < 4; c++) {
-         if (!(fp->in[i].mask & (1 << c)))
-            continue;
-         pntc[m / 8] |= (c + 1) << ((m % 8) * 4);
-         ++m;
-      }
+   if (psize != nv50->state.semantic_psize) {
+      nv50->state.semantic_psize = psize;
+      BEGIN_NV04(push, NV50_3D(SEMANTIC_PTSZ), 1);
+      PUSH_DATA (push, psize);
    }
-   if (nv50->rasterizer->pipe.sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
-      return 0;
-   return (1 << 4);
 }
 
 static int
-nv50_vec4_map(uint32_t *map32, int mid, uint32_t lin[4],
+nv50_vec4_map(uint8_t *map, int mid, uint32_t lin[4],
               struct nv50_varying *in, struct nv50_varying *out)
 {
    int c;
    uint8_t mv = out->mask, mf = in->mask, oid = out->hw;
-   uint8_t *map = (uint8_t *)map32;
 
    for (c = 0; c < 4; ++c) {
       if (mf & 1) {
@@ -458,140 +333,164 @@ nv50_vec4_map(uint32_t *map32, int mid, uint32_t lin[4],
    return mid;
 }
 
-struct nouveau_stateobj *
+void
 nv50_fp_linkage_validate(struct nv50_context *nv50)
 {
-   struct nouveau_grobj *tesla = nv50->screen->tesla;
-   struct nv50_program *vp;
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   struct nv50_program *vp = nv50->gmtyprog ? nv50->gmtyprog : nv50->vertprog;
    struct nv50_program *fp = nv50->fragprog;
-   struct nouveau_stateobj *so;
    struct nv50_varying dummy;
    int i, n, c, m;
-
-   uint32_t map[16], lin[4], pntc[8];
-
+   uint32_t primid = 0;
+   uint32_t psiz = 0x000;
    uint32_t interp = fp->fp.interp;
    uint32_t colors = fp->fp.colors;
-   uint32_t clip = 0x04;
-   uint32_t psiz = 0x000;
-   uint32_t primid = 0;
-   uint32_t sysval = 0;
-
-   if (nv50->geomprog) {
-      vp = nv50->geomprog;
-      memset(map, 0x80, sizeof(map));
-   } else {
-      vp = nv50->vertprog;
-      memset(map, 0x40, sizeof(map));
+   uint32_t lin[4];
+   uint8_t map[64];
+   uint8_t so_map[64];
+
+   if (!(nv50->dirty & (NV50_NEW_VERTPROG |
+                        NV50_NEW_FRAGPROG |
+                        NV50_NEW_GMTYPROG))) {
+      uint8_t bfc, ffc;
+      ffc = (nv50->state.semantic_color & NV50_3D_SEMANTIC_COLOR_FFC0_ID__MASK);
+      bfc = (nv50->state.semantic_color & NV50_3D_SEMANTIC_COLOR_BFC0_ID__MASK)
+         >> 8;
+      if (nv50->rast->pipe.light_twoside == ((ffc == bfc) ? 0 : 1))
+         return;
    }
-   memset(lin, 0, sizeof(lin));
 
-   dummy.linear = 0;
+   memset(lin, 0x00, sizeof(lin));
+
+   /* XXX: in buggy-endian mode, is the first element of map (u32)0x000000xx
+    *  or is it the first byte ?
+    */
+   memset(map, nv50->gmtyprog ? 0x80 : 0x40, sizeof(map));
+
    dummy.mask = 0xf; /* map all components of HPOS */
+   dummy.linear = 0;
    m = nv50_vec4_map(map, 0, lin, &dummy, &vp->out[0]);
 
-   if (vp->vp.clpd < 0x40) {
-      for (c = 0; c < vp->vp.clpd_nr; ++c) {
-         map[m / 4] |= (vp->vp.clpd + c) << ((m % 4) * 8);
-         ++m;
-      }
-      clip |= vp->vp.clpd_nr << 8;
-   }
+   for (c = 0; c < vp->vp.clpd_nr; ++c)
+      map[m++] = vp->vp.clpd[c / 4] + (c % 4);
 
    colors |= m << 8; /* adjust BFC0 id */
 
-   /* if light_twoside is active, it seems FFC0_ID == BFC0_ID is bad */
-   if (nv50->rasterizer->pipe.light_twoside) {
-      for (i = 0; i < 2; ++i)
-         m = nv50_vec4_map(map, m, lin,
-                           &fp->in[fp->vp.bfc[i]],
-                           &vp->out[vp->vp.bfc[i]]);
-   }
+   dummy.mask = 0x0;
 
+   /* if light_twoside is active, FFC0_ID == BFC0_ID is invalid */
+   if (nv50->rast->pipe.light_twoside) {
+      for (i = 0; i < 2; ++i) {
+         n = vp->vp.bfc[i];
+         if (fp->vp.bfc[i] >= fp->in_nr)
+            continue;
+         m = nv50_vec4_map(map, m, lin, &fp->in[fp->vp.bfc[i]],
+                           (n < vp->out_nr) ? &vp->out[n] : &dummy);
+      }
+   }
    colors += m - 4; /* adjust FFC0 id */
-   interp |= m << 8; /* set mid where 'normal' FP inputs start */
+   interp |= m << 8; /* set map id where 'normal' FP inputs start */
 
-   dummy.mask = 0x0;
-   for (i = 0; i < fp->in_nr; i++) {
+   for (i = 0; i < fp->in_nr; ++i) {
       for (n = 0; n < vp->out_nr; ++n)
          if (vp->out[n].sn == fp->in[i].sn &&
              vp->out[n].si == fp->in[i].si)
             break;
-
       m = nv50_vec4_map(map, m, lin,
                         &fp->in[i], (n < vp->out_nr) ? &vp->out[n] : &dummy);
-       }
+   }
 
    /* PrimitiveID either is replaced by the system value, or
     * written by the geometry shader into an output register
     */
-   if (fp->gp.primid < 0x40) {
-      i = (m % 4) * 8;
-      map[m / 4] = (map[m / 4] & ~(0xff << i)) | (vp->gp.primid << i);
-      primid = m++;
+   if (fp->gp.primid < 0x80) {
+      primid = m;
+      map[m++] = vp->gp.primid;
    }
 
-   if (nv50->rasterizer->pipe.point_size_per_vertex) {
-      i = (m % 4) * 8;
-      map[m / 4] = (map[m / 4] & ~(0xff << i)) | (vp->vp.psiz << i);
-      psiz = (m++ << 4) | 1;
+   if (nv50->rast->pipe.point_size_per_vertex) {
+      psiz = (m << 4) | 1;
+      map[m++] = vp->vp.psiz;
    }
 
-   /* now fill the stateobj (at most 28 so_data)  */
-   so = so_new(10, 54, 0);
+   if (nv50->rast->pipe.clamp_vertex_color)
+      colors |= NV50_3D_SEMANTIC_COLOR_CLMP_EN;
+
+   if (unlikely(vp->so)) {
+      /* Slot i in STRMOUT_MAP specifies the offset where slot i in RESULT_MAP
+       * gets written.
+       *
+       * TODO:
+       * Inverting vp->so->map (output -> offset) would probably speed this up.
+       */
+      memset(so_map, 0, sizeof(so_map));
+      for (i = 0; i < vp->so->map_size; ++i) {
+         if (vp->so->map[i] == 0xff)
+            continue;
+         for (c = 0; c < m; ++c)
+            if (map[c] == vp->so->map[i] && !so_map[c])
+               break;
+         if (c == m) {
+            c = m;
+            map[m++] = vp->so->map[i];
+         }
+         so_map[c] = 0x80 | i;
+      }
+      for (c = m; c & 3; ++c)
+         so_map[c] = 0;
+   }
 
    n = (m + 3) / 4;
    assert(m <= 64);
-   if (vp->type == PIPE_SHADER_GEOMETRY) {
-      so_method(so, tesla, NV50TCL_GP_RESULT_MAP_SIZE, 1);
-      so_data  (so, m);
-      so_method(so, tesla, NV50TCL_GP_RESULT_MAP(0), n);
-      so_datap (so, map, n);
+
+   if (unlikely(nv50->gmtyprog)) {
+      BEGIN_NV04(push, NV50_3D(GP_RESULT_MAP_SIZE), 1);
+      PUSH_DATA (push, m);
+      BEGIN_NV04(push, NV50_3D(GP_RESULT_MAP(0)), n);
+      PUSH_DATAp(push, map, n);
    } else {
-      so_method(so, tesla, NV50TCL_VP_GP_BUILTIN_ATTR_EN, 1);
-      so_data  (so, vp->vp.attrs[2]);
+      BEGIN_NV04(push, NV50_3D(VP_GP_BUILTIN_ATTR_EN), 1);
+      PUSH_DATA (push, vp->vp.attrs[2]);
 
-      so_method(so, tesla, NV50TCL_MAP_SEMANTIC_4, 1);
-      so_data  (so, primid);
+      BEGIN_NV04(push, NV50_3D(SEMANTIC_PRIM_ID), 1);
+      PUSH_DATA (push, primid);
 
-      so_method(so, tesla, NV50TCL_VP_RESULT_MAP_SIZE, 1);
-      so_data  (so, m);
-      so_method(so, tesla, NV50TCL_VP_RESULT_MAP(0), n);
-      so_datap (so, map, n);
+      BEGIN_NV04(push, NV50_3D(VP_RESULT_MAP_SIZE), 1);
+      PUSH_DATA (push, m);
+      BEGIN_NV04(push, NV50_3D(VP_RESULT_MAP(0)), n);
+      PUSH_DATAp(push, map, n);
    }
 
-   so_method(so, tesla, NV50TCL_MAP_SEMANTIC_0, 4);
-   so_data  (so, colors);
-   so_data  (so, clip);
-   so_data  (so, sysval);
-   so_data  (so, psiz);
+   BEGIN_NV04(push, NV50_3D(SEMANTIC_COLOR), 4);
+   PUSH_DATA (push, colors);
+   PUSH_DATA (push, (vp->vp.clpd_nr << 8) | 4);
+   PUSH_DATA (push, 0);
+   PUSH_DATA (push, psiz);
 
-   so_method(so, tesla, NV50TCL_FP_INTERPOLANT_CTRL, 1);
-   so_data  (so, interp);
+   BEGIN_NV04(push, NV50_3D(FP_INTERPOLANT_CTRL), 1);
+   PUSH_DATA (push, interp);
 
-   so_method(so, tesla, NV50TCL_NOPERSPECTIVE_BITMAP(0), 4);
-   so_datap (so, lin, 4);
+   nv50->state.interpolant_ctrl = interp;
 
-   if (nv50->rasterizer->pipe.point_quad_rasterization) {
-      so_method(so, tesla, NV50TCL_POINT_SPRITE_CTRL, 1);
-      so_data  (so,
-                nv50_pntc_replace(nv50, pntc, (interp >> 8) & 0xff));
+   nv50->state.semantic_color = colors;
+   nv50->state.semantic_psize = psiz;
 
-      so_method(so, tesla, NV50TCL_POINT_COORD_REPLACE_MAP(0), 8);
-      so_datap (so, pntc, 8);
-   }
+   BEGIN_NV04(push, NV50_3D(NOPERSPECTIVE_BITMAP(0)), 4);
+   PUSH_DATAp(push, lin, 4);
 
-   so_method(so, tesla, NV50TCL_GP_ENABLE, 1);
-   so_data  (so, (vp->type == PIPE_SHADER_GEOMETRY) ? 1 : 0);
+   BEGIN_NV04(push, NV50_3D(GP_ENABLE), 1);
+   PUSH_DATA (push, nv50->gmtyprog ? 1 : 0);
 
-   return so;
+   if (vp->so) {
+      BEGIN_NV04(push, NV50_3D(STRMOUT_MAP(0)), n);
+      PUSH_DATAp(push, so_map, n);
+   }
 }
 
 static int
-nv50_vp_gp_mapping(uint32_t *map32, int m,
+nv50_vp_gp_mapping(uint8_t *map, int m,
                    struct nv50_program *vp, struct nv50_program *gp)
 {
-   uint8_t *map = (uint8_t *)map32;
    int i, j, c;
 
    for (i = 0; i < gp->in_nr; ++i) {
@@ -618,34 +517,107 @@ nv50_vp_gp_mapping(uint32_t *map32, int m,
    return m;
 }
 
-struct nouveau_stateobj *
+void
 nv50_gp_linkage_validate(struct nv50_context *nv50)
 {
-   struct nouveau_grobj *tesla = nv50->screen->tesla;
-   struct nouveau_stateobj *so;
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
    struct nv50_program *vp = nv50->vertprog;
-   struct nv50_program *gp = nv50->geomprog;
-   uint32_t map[16];
+   struct nv50_program *gp = nv50->gmtyprog;
    int m = 0;
+   int n;
+   uint8_t map[64];
 
    if (!gp)
-      return NULL;
+      return;
    memset(map, 0, sizeof(map));
 
    m = nv50_vp_gp_mapping(map, m, vp, gp);
 
-   so = so_new(3, 24 - 3, 0);
+   n = (m + 3) / 4;
+
+   BEGIN_NV04(push, NV50_3D(VP_GP_BUILTIN_ATTR_EN), 1);
+   PUSH_DATA (push, vp->vp.attrs[2] | gp->vp.attrs[2]);
 
-   so_method(so, tesla, NV50TCL_VP_GP_BUILTIN_ATTR_EN, 1);
-   so_data  (so, vp->vp.attrs[2] | gp->vp.attrs[2]);
+   BEGIN_NV04(push, NV50_3D(VP_RESULT_MAP_SIZE), 1);
+   PUSH_DATA (push, m);
+   BEGIN_NV04(push, NV50_3D(VP_RESULT_MAP(0)), n);
+   PUSH_DATAp(push, map, n);
+}
 
-   assert(m <= 32);
-   so_method(so, tesla, NV50TCL_VP_RESULT_MAP_SIZE, 1);
-   so_data  (so, m);
+void
+nv50_stream_output_validate(struct nv50_context *nv50)
+{
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   struct nv50_stream_output_state *so;
+   uint32_t ctrl;
+   unsigned i;
+   unsigned prims = ~0;
+
+   so = nv50->gmtyprog ? nv50->gmtyprog->so : nv50->vertprog->so;
+
+   BEGIN_NV04(push, NV50_3D(STRMOUT_ENABLE), 1);
+   PUSH_DATA (push, 0);
+   if (!so || !nv50->num_so_targets) {
+      if (nv50->screen->base.class_3d < NVA0_3D_CLASS) {
+         BEGIN_NV04(push, NV50_3D(STRMOUT_PRIMITIVE_LIMIT), 1);
+         PUSH_DATA (push, 0);
+      }
+      BEGIN_NV04(push, NV50_3D(STRMOUT_PARAMS_LATCH), 1);
+      PUSH_DATA (push, 1);
+      return;
+   }
 
-   m = (m + 3) / 4;
-   so_method(so, tesla, NV50TCL_VP_RESULT_MAP(0), m);
-   so_datap (so, map, m);
+   /* previous TFB needs to complete */
+   if (nv50->screen->base.class_3d < NVA0_3D_CLASS) {
+      BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
+      PUSH_DATA (push, 0);
+   }
 
-   return so;
+   ctrl = so->ctrl;
+   if (nv50->screen->base.class_3d >= NVA0_3D_CLASS)
+      ctrl |= NVA0_3D_STRMOUT_BUFFERS_CTRL_LIMIT_MODE_OFFSET;
+
+   BEGIN_NV04(push, NV50_3D(STRMOUT_BUFFERS_CTRL), 1);
+   PUSH_DATA (push, ctrl);
+
+   nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_SO);
+
+   for (i = 0; i < nv50->num_so_targets; ++i) {
+      struct nv50_so_target *targ = nv50_so_target(nv50->so_target[i]);
+      struct nv04_resource *buf = nv04_resource(targ->pipe.buffer);
+
+      const unsigned n = nv50->screen->base.class_3d >= NVA0_3D_CLASS ? 4 : 3;
+
+      if (n == 4 && !targ->clean)
+         nv84_query_fifo_wait(push, targ->pq);
+      BEGIN_NV04(push, NV50_3D(STRMOUT_ADDRESS_HIGH(i)), n);
+      PUSH_DATAh(push, buf->address + targ->pipe.buffer_offset);
+      PUSH_DATA (push, buf->address + targ->pipe.buffer_offset);
+      PUSH_DATA (push, so->num_attribs[i]);
+      if (n == 4) {
+         PUSH_DATA(push, targ->pipe.buffer_size);
+
+         BEGIN_NV04(push, NVA0_3D(STRMOUT_OFFSET(i)), 1);
+         if (!targ->clean) {
+            assert(targ->pq);
+            nv50_query_pushbuf_submit(push, targ->pq, 0x4);
+         } else {
+            PUSH_DATA(push, 0);
+            targ->clean = FALSE;
+         }
+      } else {
+         const unsigned limit = targ->pipe.buffer_size /
+            (so->stride[i] * nv50->state.prim_size);
+         prims = MIN2(prims, limit);
+      }
+      BCTX_REFN(nv50->bufctx_3d, SO, buf, WR);
+   }
+   if (prims != ~0) {
+      BEGIN_NV04(push, NV50_3D(STRMOUT_PRIMITIVE_LIMIT), 1);
+      PUSH_DATA (push, prims);
+   }
+   BEGIN_NV04(push, NV50_3D(STRMOUT_PARAMS_LATCH), 1);
+   PUSH_DATA (push, 1);
+   BEGIN_NV04(push, NV50_3D(STRMOUT_ENABLE), 1);
+   PUSH_DATA (push, 1);
 }