nv50: update to handle new sampler view state
[mesa.git] / src / gallium / drivers / nv50 / nv50_state_validate.c
index 799d2758fee17ee6468136f1c4e35acb816c66eb..741f8e8f59d719578b5ba72e60817265a1a5d018 100644 (file)
@@ -20,6 +20,8 @@
  * SOFTWARE.
  */
 
+#include "util/u_format.h"
+
 #include "nv50_context.h"
 #include "nouveau/nouveau_stateobj.h"
 
@@ -33,7 +35,7 @@ static void
 nv50_state_validate_fb(struct nv50_context *nv50)
 {
        struct nouveau_grobj *tesla = nv50->screen->tesla;
-       struct nouveau_stateobj *so = so_new(128, 18);
+       struct nouveau_stateobj *so = so_new(32, 79, 18);
        struct pipe_framebuffer_state *fb = &nv50->framebuffer;
        unsigned i, w, h, gw = 0;
 
@@ -41,7 +43,7 @@ nv50_state_validate_fb(struct nv50_context *nv50)
         * FP result 0 always goes to RT[0], bits 4 - 6 are ignored.
         * Ambiguous assignment results in no rendering (no DATA_ERROR).
         */
-       so_method(so, tesla, 0x121c, 1);
+       so_method(so, tesla, NV50TCL_RT_CONTROL, 1);
        so_data  (so, fb->nr_cbufs |
                  (0 <<  4) | (1 <<  7) | (2 << 10) | (3 << 13) |
                  (4 << 16) | (5 << 19) | (6 << 22) | (7 << 25));
@@ -79,7 +81,7 @@ nv50_state_validate_fb(struct nv50_context *nv50)
                NV50_CBUF_FORMAT_CASE(R16G16_UNORM);
                default:
                        NOUVEAU_ERR("AIIII unknown format %s\n",
-                                   pf_name(fb->cbufs[i]->format));
+                                   util_format_name(fb->cbufs[i]->format));
                        so_data(so, NV50TCL_RT_FORMAT_X8R8G8B8_UNORM);
                        break;
                }
@@ -87,7 +89,7 @@ nv50_state_validate_fb(struct nv50_context *nv50)
                                level[fb->cbufs[i]->level].tile_mode << 4);
                so_data(so, 0x00000000);
 
-               so_method(so, tesla, 0x1224, 1);
+               so_method(so, tesla, NV50TCL_RT_ARRAY_MODE, 1);
                so_data  (so, 1);
        }
 
@@ -116,7 +118,7 @@ nv50_state_validate_fb(struct nv50_context *nv50)
                NV50_ZETA_FORMAT_CASE(Z32_FLOAT);
                default:
                        NOUVEAU_ERR("AIIII unknown format %s\n",
-                                   pf_name(fb->zsbuf->format));
+                                   util_format_name(fb->zsbuf->format));
                        so_data(so, NV50TCL_ZETA_FORMAT_S8Z24_UNORM);
                        break;
                }
@@ -124,22 +126,22 @@ nv50_state_validate_fb(struct nv50_context *nv50)
                                level[fb->zsbuf->level].tile_mode << 4);
                so_data(so, 0x00000000);
 
-               so_method(so, tesla, 0x1538, 1);
+               so_method(so, tesla, NV50TCL_ZETA_ENABLE, 1);
                so_data  (so, 1);
                so_method(so, tesla, NV50TCL_ZETA_HORIZ, 3);
                so_data  (so, fb->zsbuf->width);
                so_data  (so, fb->zsbuf->height);
                so_data  (so, 0x00010001);
        } else {
-               so_method(so, tesla, 0x1538, 1);
+               so_method(so, tesla, NV50TCL_ZETA_ENABLE, 1);
                so_data  (so, 0);
        }
 
-       so_method(so, tesla, NV50TCL_VIEWPORT_HORIZ, 2);
+       so_method(so, tesla, NV50TCL_VIEWPORT_HORIZ(0), 2);
        so_data  (so, w << 16);
        so_data  (so, h << 16);
        /* set window lower left corner */
-       so_method(so, tesla, NV50TCL_WINDOW_LEFT, 2);
+       so_method(so, tesla, NV50TCL_WINDOW_OFFSET_X, 2);
        so_data  (so, 0);
        so_data  (so, 0);
        /* set screen scissor rectangle */
@@ -155,13 +157,40 @@ nv50_state_validate_fb(struct nv50_context *nv50)
        so_ref(NULL, &so);
 }
 
+static void
+nv50_validate_samplers(struct nv50_context *nv50, struct nouveau_stateobj *so,
+                      unsigned p)
+{
+       struct nouveau_grobj *eng2d = nv50->screen->eng2d;
+       unsigned i, j, dw = nv50->sampler_nr[p] * 8;
+
+       if (!dw)
+               return;
+       nv50_so_init_sifc(nv50, so, nv50->screen->tsc, NOUVEAU_BO_VRAM,
+                         p * (32 * 8 * 4), dw * 4);
+
+       so_method(so, eng2d, NV50_2D_SIFC_DATA | (2 << 29), dw);
+
+       for (i = 0; i < nv50->sampler_nr[p]; ++i) {
+               if (nv50->sampler[p][i])
+                       so_datap(so, nv50->sampler[p][i]->tsc, 8);
+               else {
+                       for (j = 0; j < 8; ++j) /* you get punished */
+                               so_data(so, 0); /* ... for leaving holes */
+               }
+       }
+}
+
 static void
 nv50_state_emit(struct nv50_context *nv50)
 {
        struct nv50_screen *screen = nv50->screen;
        struct nouveau_channel *chan = screen->base.channel;
 
-       if (nv50->pctx_id != screen->cur_pctx) {
+       /* XXX: this is racy for multiple contexts active on separate
+        * threads.
+        */
+       if (screen->cur_ctx != nv50) {
                if (nv50->state.fb)
                        nv50->state.dirty |= NV50_NEW_FRAMEBUFFER;
                if (nv50->state.blend)
@@ -172,10 +201,14 @@ nv50_state_emit(struct nv50_context *nv50)
                        nv50->state.dirty |= NV50_NEW_VERTPROG;
                if (nv50->state.fragprog)
                        nv50->state.dirty |= NV50_NEW_FRAGPROG;
+               if (nv50->state.geomprog)
+                       nv50->state.dirty |= NV50_NEW_GEOMPROG;
                if (nv50->state.rast)
                        nv50->state.dirty |= NV50_NEW_RASTERIZER;
                if (nv50->state.blend_colour)
                        nv50->state.dirty |= NV50_NEW_BLEND_COLOUR;
+               if (nv50->state.stencil_ref)
+                       nv50->state.dirty |= NV50_NEW_STENCIL_REF;
                if (nv50->state.stipple)
                        nv50->state.dirty |= NV50_NEW_STIPPLE;
                if (nv50->state.scissor)
@@ -188,7 +221,7 @@ nv50_state_emit(struct nv50_context *nv50)
                        nv50->state.dirty |= NV50_NEW_TEXTURE;
                if (nv50->state.vtxfmt && nv50->state.vtxbuf)
                        nv50->state.dirty |= NV50_NEW_ARRAYS;
-               screen->cur_pctx = nv50->pctx_id;
+               screen->cur_ctx = nv50;
        }
 
        if (nv50->state.dirty & NV50_NEW_FRAMEBUFFER)
@@ -201,12 +234,20 @@ nv50_state_emit(struct nv50_context *nv50)
                so_emit(chan, nv50->state.vertprog);
        if (nv50->state.dirty & NV50_NEW_FRAGPROG)
                so_emit(chan, nv50->state.fragprog);
-       if (nv50->state.dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG))
-               so_emit(chan, nv50->state.programs);
+       if (nv50->state.dirty & NV50_NEW_GEOMPROG && nv50->state.geomprog)
+               so_emit(chan, nv50->state.geomprog);
+       if (nv50->state.dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG |
+                                NV50_NEW_GEOMPROG | NV50_NEW_RASTERIZER))
+               so_emit(chan, nv50->state.fp_linkage);
+       if ((nv50->state.dirty & (NV50_NEW_VERTPROG | NV50_NEW_GEOMPROG))
+           && nv50->state.gp_linkage)
+               so_emit(chan, nv50->state.gp_linkage);
        if (nv50->state.dirty & NV50_NEW_RASTERIZER)
                so_emit(chan, nv50->state.rast);
        if (nv50->state.dirty & NV50_NEW_BLEND_COLOUR)
                so_emit(chan, nv50->state.blend_colour);
+       if (nv50->state.dirty & NV50_NEW_STENCIL_REF)
+               so_emit(chan, nv50->state.stencil_ref);
        if (nv50->state.dirty & NV50_NEW_STIPPLE)
                so_emit(chan, nv50->state.stipple);
        if (nv50->state.dirty & NV50_NEW_SCISSOR)
@@ -239,13 +280,15 @@ nv50_state_flush_notify(struct nouveau_channel *chan)
        so_emit_reloc_markers(chan, nv50->state.fragprog);
        so_emit_reloc_markers(chan, nv50->state.vtxbuf);
        so_emit_reloc_markers(chan, nv50->screen->static_init);
+
+       if (nv50->state.instbuf)
+               so_emit_reloc_markers(chan, nv50->state.instbuf);
 }
 
 boolean
 nv50_state_validate(struct nv50_context *nv50)
 {
        struct nouveau_grobj *tesla = nv50->screen->tesla;
-       struct nouveau_grobj *eng2d = nv50->screen->eng2d;
        struct nouveau_stateobj *so;
        unsigned i;
 
@@ -264,14 +307,21 @@ nv50_state_validate(struct nv50_context *nv50)
        if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_FRAGPROG_CB))
                nv50_fragprog_validate(nv50);
 
-       if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG))
-               nv50_linkage_validate(nv50);
+       if (nv50->dirty & (NV50_NEW_GEOMPROG | NV50_NEW_GEOMPROG_CB))
+               nv50_geomprog_validate(nv50);
+
+       if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG |
+                          NV50_NEW_GEOMPROG | NV50_NEW_RASTERIZER))
+               nv50_fp_linkage_validate(nv50);
+
+       if (nv50->dirty & (NV50_NEW_GEOMPROG | NV50_NEW_VERTPROG))
+               nv50_gp_linkage_validate(nv50);
 
        if (nv50->dirty & NV50_NEW_RASTERIZER)
                so_ref(nv50->rasterizer->so, &nv50->state.rast);
 
        if (nv50->dirty & NV50_NEW_BLEND_COLOUR) {
-               so = so_new(5, 0);
+               so = so_new(1, 4, 0);
                so_method(so, tesla, NV50TCL_BLEND_COLOR(0), 4);
                so_data  (so, fui(nv50->blend_colour.color[0]));
                so_data  (so, fui(nv50->blend_colour.color[1]));
@@ -281,11 +331,21 @@ nv50_state_validate(struct nv50_context *nv50)
                so_ref(NULL, &so);
        }
 
+       if (nv50->dirty & NV50_NEW_STENCIL_REF) {
+               so = so_new(2, 2, 0);
+               so_method(so, tesla, NV50TCL_STENCIL_FRONT_FUNC_REF, 1);
+               so_data  (so, nv50->stencil_ref.ref_value[0]);
+               so_method(so, tesla, NV50TCL_STENCIL_BACK_FUNC_REF, 1);
+               so_data  (so, nv50->stencil_ref.ref_value[1]);
+               so_ref(so, &nv50->state.stencil_ref);
+               so_ref(NULL, &so);
+       }
+
        if (nv50->dirty & NV50_NEW_STIPPLE) {
-               so = so_new(33, 0);
+               so = so_new(1, 32, 0);
                so_method(so, tesla, NV50TCL_POLYGON_STIPPLE_PATTERN(0), 32);
                for (i = 0; i < 32; i++)
-                       so_data(so, nv50->stipple.stipple[i]);
+                       so_data(so, util_bswap32(nv50->stipple.stipple[i]));
                so_ref(so, &nv50->state.stipple);
                so_ref(NULL, &so);
        }
@@ -299,8 +359,8 @@ nv50_state_validate(struct nv50_context *nv50)
                        goto scissor_uptodate;
                nv50->state.scissor_enabled = rast->scissor;
 
-               so = so_new(3, 0);
-               so_method(so, tesla, NV50TCL_SCISSOR_HORIZ, 2);
+               so = so_new(1, 2, 0);
+               so_method(so, tesla, NV50TCL_SCISSOR_HORIZ(0), 2);
                if (nv50->state.scissor_enabled) {
                        so_data(so, (s->maxx << 16) | s->minx);
                        so_data(so, (s->maxy << 16) | s->miny);
@@ -328,13 +388,13 @@ scissor_uptodate:
                        goto viewport_uptodate;
                nv50->state.viewport_bypass = bypass;
 
-               so = so_new(14, 0);
+               so = so_new(5, 9, 0);
                if (!bypass) {
-                       so_method(so, tesla, NV50TCL_VIEWPORT_TRANSLATE(0), 3);
+                       so_method(so, tesla, NV50TCL_VIEWPORT_TRANSLATE_X(0), 3);
                        so_data  (so, fui(nv50->viewport.translate[0]));
                        so_data  (so, fui(nv50->viewport.translate[1]));
                        so_data  (so, fui(nv50->viewport.translate[2]));
-                       so_method(so, tesla, NV50TCL_VIEWPORT_SCALE(0), 3);
+                       so_method(so, tesla, NV50TCL_VIEWPORT_SCALE_X(0), 3);
                        so_data  (so, fui(nv50->viewport.scale[0]));
                        so_data  (so, fui(nv50->viewport.scale[1]));
                        so_data  (so, fui(nv50->viewport.scale[2]));
@@ -367,22 +427,18 @@ scissor_uptodate:
 viewport_uptodate:
 
        if (nv50->dirty & NV50_NEW_SAMPLER) {
-               unsigned i;
+               unsigned nr = 0;
 
-               so = so_new(nv50->sampler_nr * 9 + 23 + 4, 2);
+               for (i = 0; i < PIPE_SHADER_TYPES; ++i)
+                       nr += nv50->sampler_nr[i];
 
-               nv50_so_init_sifc(nv50, so, nv50->screen->tsc, NOUVEAU_BO_VRAM,
-                                 nv50->sampler_nr * 8 * 4);
+               so = so_new(1 + 5 * PIPE_SHADER_TYPES,
+                           1 + 19 * PIPE_SHADER_TYPES + nr * 8,
+                           PIPE_SHADER_TYPES * 2);
 
-               for (i = 0; i < nv50->sampler_nr; i++) {
-                       if (!nv50->sampler[i])
-                               continue;
-                       so_method(so, eng2d, NV50_2D_SIFC_DATA | (2 << 29), 8);
-                       so_datap (so, nv50->sampler[i]->tsc, 8);
-               }
+               nv50_validate_samplers(nv50, so, 0); /* VP samplers */
+               nv50_validate_samplers(nv50, so, 2); /* FP samplers */
 
-               so_method(so, tesla, 0x1440, 1); /* sync SIFC */
-               so_data  (so, 0);
                so_method(so, tesla, 0x1334, 1); /* flush TSC */
                so_data  (so, 0);
 
@@ -405,10 +461,13 @@ viewport_uptodate:
 
 void nv50_so_init_sifc(struct nv50_context *nv50,
                       struct nouveau_stateobj *so,
-                      struct nouveau_bo *bo, unsigned reloc, unsigned size)
+                      struct nouveau_bo *bo, unsigned reloc,
+                      unsigned offset, unsigned size)
 {
        struct nouveau_grobj *eng2d = nv50->screen->eng2d;
 
+       reloc |= NOUVEAU_BO_WR;
+
        so_method(so, eng2d, NV50_2D_DST_FORMAT, 2);
        so_data  (so, NV50_2D_DST_FORMAT_R8_UNORM);
        so_data  (so, 1);
@@ -416,9 +475,9 @@ void nv50_so_init_sifc(struct nv50_context *nv50,
        so_data  (so, 262144);
        so_data  (so, 65536);
        so_data  (so, 1);
-       so_reloc (so, bo, 0, reloc | NOUVEAU_BO_WR | NOUVEAU_BO_HIGH, 0, 0);
-       so_reloc (so, bo, 0, reloc | NOUVEAU_BO_WR | NOUVEAU_BO_LOW, 0, 0);
-       so_method(so, eng2d, NV50_2D_SIFC_UNK0800, 2);
+       so_reloc (so, bo, offset, reloc | NOUVEAU_BO_HIGH, 0, 0);
+       so_reloc (so, bo, offset, reloc | NOUVEAU_BO_LOW, 0, 0);
+       so_method(so, eng2d, NV50_2D_SIFC_BITMAP_ENABLE, 2);
        so_data  (so, 0);
        so_data  (so, NV50_2D_SIFC_FORMAT_R8_UNORM);
        so_method(so, eng2d, NV50_2D_SIFC_WIDTH, 10);