Merge branch 'gallium-polygon-stipple'
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_state_validate.c
index 7406f6c7917da4a973ed08da0016292793006e2a..968d7a7bd4974b4f0b90296d5a1f85239eee2d55 100644 (file)
@@ -1,14 +1,15 @@
 
+#include "util/u_math.h"
+
 #include "nvc0_context.h"
-#include "os/os_time.h"
 
 static void
 nvc0_validate_zcull(struct nvc0_context *nvc0)
 {
     struct nouveau_channel *chan = nvc0->screen->base.channel;
     struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
-    struct nvc0_surface *sf = nvc0_surface(fb->zsbuf);
-    struct nvc0_miptree *mt = nvc0_miptree(sf->base.texture);
+    struct nv50_surface *sf = nv50_surface(fb->zsbuf);
+    struct nv50_miptree *mt = nv50_miptree(sf->base.texture);
     struct nouveau_bo *bo = mt->base.bo;
     uint32_t size;
     uint32_t offset = align(mt->total_size, 1 << 17);
@@ -58,6 +59,8 @@ nvc0_validate_fb(struct nvc0_context *nvc0)
     struct nouveau_channel *chan = nvc0->screen->base.channel;
     struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
     unsigned i;
+    unsigned ms_mode = NVC0_3D_MULTISAMPLE_MODE_MS1;
+    boolean serialize = FALSE;
 
     nvc0_bufctx_reset(nvc0, NVC0_BUFCTX_FRAME);
 
@@ -70,12 +73,12 @@ nvc0_validate_fb(struct nvc0_context *nvc0)
     MARK_RING(chan, 9 * fb->nr_cbufs, 2 * fb->nr_cbufs);
 
     for (i = 0; i < fb->nr_cbufs; ++i) {
-        struct nvc0_miptree *mt = nvc0_miptree(fb->cbufs[i]->texture);
-        struct nvc0_surface *sf = nvc0_surface(fb->cbufs[i]);
+        struct nv50_miptree *mt = nv50_miptree(fb->cbufs[i]->texture);
+        struct nv50_surface *sf = nv50_surface(fb->cbufs[i]);
         struct nouveau_bo *bo = mt->base.bo;
         uint32_t offset = sf->offset;
 
-        BEGIN_RING(chan, RING_3D(RT_ADDRESS_HIGH(i)), 8);
+        BEGIN_RING(chan, RING_3D(RT_ADDRESS_HIGH(i)), 9);
         OUT_RELOCh(chan, bo, offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
         OUT_RELOCl(chan, bo, offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
         OUT_RING  (chan, sf->width);
@@ -83,16 +86,25 @@ nvc0_validate_fb(struct nvc0_context *nvc0)
         OUT_RING  (chan, nvc0_format_table[sf->base.format].rt);
         OUT_RING  (chan, (mt->layout_3d << 16) |
                    mt->level[sf->base.u.tex.level].tile_mode);
-        OUT_RING  (chan, sf->depth);
+        OUT_RING  (chan, sf->base.u.tex.first_layer + sf->depth);
         OUT_RING  (chan, mt->layer_stride >> 2);
+        OUT_RING  (chan, sf->base.u.tex.first_layer);
+
+        ms_mode = mt->ms_mode;
+
+        if (mt->base.status & NOUVEAU_BUFFER_STATUS_GPU_READING)
+           serialize = TRUE;
+        mt->base.status |=  NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+        mt->base.status &= ~NOUVEAU_BUFFER_STATUS_GPU_READING;
 
+        /* only register for writing, otherwise we'd always serialize here */
         nvc0_bufctx_add_resident(nvc0, NVC0_BUFCTX_FRAME, &mt->base,
-                                 NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
+                                 NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
     }
 
     if (fb->zsbuf) {
-        struct nvc0_miptree *mt = nvc0_miptree(fb->zsbuf->texture);
-        struct nvc0_surface *sf = nvc0_surface(fb->zsbuf);
+        struct nv50_miptree *mt = nv50_miptree(fb->zsbuf->texture);
+        struct nv50_surface *sf = nv50_surface(fb->zsbuf);
         struct nouveau_bo *bo = mt->base.bo;
         int unk = mt->base.base.target == PIPE_TEXTURE_2D;
         uint32_t offset = sf->offset;
@@ -109,20 +121,31 @@ nvc0_validate_fb(struct nvc0_context *nvc0)
         BEGIN_RING(chan, RING_3D(ZETA_HORIZ), 3);
         OUT_RING  (chan, sf->width);
         OUT_RING  (chan, sf->height);
-        OUT_RING  (chan, (unk << 16) | sf->depth);
+        OUT_RING  (chan, (unk << 16) |
+                   (sf->base.u.tex.first_layer + sf->depth));
+        BEGIN_RING(chan, RING_3D(ZETA_BASE_LAYER), 1);
+        OUT_RING  (chan, sf->base.u.tex.first_layer);
+
+        ms_mode = mt->ms_mode;
+
+        if (mt->base.status & NOUVEAU_BUFFER_STATUS_GPU_READING)
+           serialize = TRUE;
+        mt->base.status |=  NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+        mt->base.status &= ~NOUVEAU_BUFFER_STATUS_GPU_READING;
 
         nvc0_bufctx_add_resident(nvc0, NVC0_BUFCTX_FRAME, &mt->base,
-                                 NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
+                                 NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
     } else {
         BEGIN_RING(chan, RING_3D(ZETA_ENABLE), 1);
         OUT_RING  (chan, 0);
     }
 
-#ifndef NVC0_SCISSORS_CLIPPING
-    BEGIN_RING(chan, RING_3D(VIEWPORT_HORIZ(0)), 2);
-    OUT_RING  (chan, fb->width << 16);
-    OUT_RING  (chan, fb->height << 16);
-#endif
+    IMMED_RING(chan, RING_3D(MULTISAMPLE_MODE), ms_mode);
+
+    if (serialize) {
+       BEGIN_RING(chan, RING_3D(SERIALIZE), 1);
+       OUT_RING  (chan, 0);
+    }
 }
 
 static void
@@ -141,11 +164,10 @@ static void
 nvc0_validate_stencil_ref(struct nvc0_context *nvc0)
 {
     struct nouveau_channel *chan = nvc0->screen->base.channel;
+    const ubyte *ref = &nvc0->stencil_ref.ref_value[0];
 
-    BEGIN_RING(chan, RING_3D(STENCIL_FRONT_FUNC_REF), 1);
-    OUT_RING  (chan, nvc0->stencil_ref.ref_value[0]);
-    BEGIN_RING(chan, RING_3D(STENCIL_BACK_FUNC_REF), 1);
-    OUT_RING  (chan, nvc0->stencil_ref.ref_value[1]);
+    IMMED_RING(chan, RING_3D(STENCIL_FRONT_FUNC_REF), ref[0]);
+    IMMED_RING(chan, RING_3D(STENCIL_BACK_FUNC_REF), ref[1]);
 }
 
 static void
@@ -164,65 +186,55 @@ nvc0_validate_scissor(struct nvc0_context *nvc0)
 {
     struct nouveau_channel *chan = nvc0->screen->base.channel;
     struct pipe_scissor_state *s = &nvc0->scissor;
-#ifdef NVC0_SCISSORS_CLIPPING
-    struct pipe_viewport_state *vp = &nvc0->viewport;
-    int minx, maxx, miny, maxy;
 
-    if (!(nvc0->dirty &
-          (NVC0_NEW_SCISSOR | NVC0_NEW_VIEWPORT | NVC0_NEW_FRAMEBUFFER)) &&
-        nvc0->state.scissor == nvc0->rast->pipe.scissor)
+    if (!(nvc0->dirty & NVC0_NEW_SCISSOR) &&
+        nvc0->rast->pipe.scissor == nvc0->state.scissor)
        return;
     nvc0->state.scissor = nvc0->rast->pipe.scissor;
 
-    if (nvc0->state.scissor) {
-       minx = s->minx;
-       maxx = s->maxx;
-       miny = s->miny;
-       maxy = s->maxy;
+    BEGIN_RING(chan, RING_3D(SCISSOR_HORIZ(0)), 2);
+    if (nvc0->rast->pipe.scissor) {
+       OUT_RING(chan, (s->maxx << 16) | s->minx);
+       OUT_RING(chan, (s->maxy << 16) | s->miny);
     } else {
-       minx = 0;
-       maxx = nvc0->framebuffer.width;
-       miny = 0;
-       maxy = nvc0->framebuffer.height;
+       OUT_RING(chan, (0xffff << 16) | 0);
+       OUT_RING(chan, (0xffff << 16) | 0);
     }
-
-    minx = MAX2(minx, (int)(vp->translate[0] - fabsf(vp->scale[0])));
-    maxx = MIN2(maxx, (int)(vp->translate[0] + fabsf(vp->scale[0])));
-    miny = MAX2(miny, (int)(vp->translate[1] - fabsf(vp->scale[1])));
-    maxy = MIN2(maxy, (int)(vp->translate[1] + fabsf(vp->scale[1])));
-
-    BEGIN_RING(chan, RING_3D(SCISSOR_HORIZ(0)), 2);
-    OUT_RING  (chan, (maxx << 16) | minx);
-    OUT_RING  (chan, (maxy << 16) | miny);
-    BEGIN_RING(chan, RING_3D(VIEWPORT_HORIZ(0)), 2);
-    OUT_RING  (chan, ((maxx - minx) << 16) | minx);
-    OUT_RING  (chan, ((maxy - miny) << 16) | miny);
-#else
-    BEGIN_RING(chan, RING_3D(SCISSOR_HORIZ(0)), 2);
-    OUT_RING  (chan, (s->maxx << 16) | s->minx);
-    OUT_RING  (chan, (s->maxy << 16) | s->miny);
-#endif
 }
 
 static void
 nvc0_validate_viewport(struct nvc0_context *nvc0)
 {
     struct nouveau_channel *chan = nvc0->screen->base.channel;
+    struct pipe_viewport_state *vp = &nvc0->viewport;
+    int x, y, w, h;
+    float zmin, zmax;
 
     BEGIN_RING(chan, RING_3D(VIEWPORT_TRANSLATE_X(0)), 3);
-    OUT_RINGf (chan, nvc0->viewport.translate[0]);
-    OUT_RINGf (chan, nvc0->viewport.translate[1]);
-    OUT_RINGf (chan, nvc0->viewport.translate[2]);
+    OUT_RINGf (chan, vp->translate[0]);
+    OUT_RINGf (chan, vp->translate[1]);
+    OUT_RINGf (chan, vp->translate[2]);
     BEGIN_RING(chan, RING_3D(VIEWPORT_SCALE_X(0)), 3);
-    OUT_RINGf (chan, nvc0->viewport.scale[0]);
-    OUT_RINGf (chan, nvc0->viewport.scale[1]);
-    OUT_RINGf (chan, nvc0->viewport.scale[2]);
+    OUT_RINGf (chan, vp->scale[0]);
+    OUT_RINGf (chan, vp->scale[1]);
+    OUT_RINGf (chan, vp->scale[2]);
+
+    /* now set the viewport rectangle to viewport dimensions for clipping */
+
+    x = util_iround(MAX2(0.0f, vp->translate[0] - fabsf(vp->scale[0])));
+    y = util_iround(MAX2(0.0f, vp->translate[1] - fabsf(vp->scale[1])));
+    w = util_iround(vp->translate[0] + fabsf(vp->scale[0])) - x;
+    h = util_iround(vp->translate[1] + fabsf(vp->scale[1])) - y;
 
-#ifdef NVC0_SCISSORS_CLIPPING
+    zmin = vp->translate[2] - fabsf(vp->scale[2]);
+    zmax = vp->translate[2] + fabsf(vp->scale[2]);
+
+    BEGIN_RING(chan, RING_3D(VIEWPORT_HORIZ(0)), 2);
+    OUT_RING  (chan, (w << 16) | x);
+    OUT_RING  (chan, (h << 16) | y);
     BEGIN_RING(chan, RING_3D(DEPTH_RANGE_NEAR(0)), 2);
-    OUT_RINGf (chan, nvc0->viewport.translate[2] - nvc0->viewport.scale[2]);
-    OUT_RINGf (chan, nvc0->viewport.translate[2] + nvc0->viewport.scale[2]);
-#endif
+    OUT_RINGf (chan, zmin);
+    OUT_RINGf (chan, zmax);
 }
 
 static void
@@ -231,10 +243,15 @@ nvc0_validate_clip(struct nvc0_context *nvc0)
    struct nouveau_channel *chan = nvc0->screen->base.channel;
    uint32_t clip;
 
-   clip = nvc0->clip.depth_clamp ? 0x201a : 0x0002;
-#ifndef NVC0_SCISSORS_CLIPPING
-   clip |= 0x1080;
-#endif
+   if (nvc0->clip.depth_clamp) {
+      clip =
+         NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK1_UNK1 |
+         NVC0_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_NEAR |
+         NVC0_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_FAR |
+         NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK12_UNK2;
+   } else {
+      clip = NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK1_UNK1;
+   }
 
    BEGIN_RING(chan, RING_3D(VIEW_VOLUME_CLIP_CTRL), 1);
    OUT_RING  (chan, clip);
@@ -285,34 +302,6 @@ nvc0_validate_rasterizer(struct nvc0_context *nvc0)
    OUT_RINGp(chan, nvc0->rast->state, nvc0->rast->size);
 }
 
-static void
-nvc0_validate_sprite_coords(struct nvc0_context *nvc0)
-{
-   struct nouveau_channel *chan = nvc0->screen->base.channel;
-   uint32_t reg;
-
-   if (nvc0->rast->pipe.sprite_coord_mode == PIPE_SPRITE_COORD_UPPER_LEFT)
-      reg = NVC0_3D_POINT_COORD_REPLACE_COORD_ORIGIN_UPPER_LEFT;
-   else
-      reg = NVC0_3D_POINT_COORD_REPLACE_COORD_ORIGIN_LOWER_LEFT;
-
-   if (nvc0->rast->pipe.point_quad_rasterization) {
-      uint32_t en = nvc0->rast->pipe.sprite_coord_enable;
-      int i;
-      struct nvc0_program *prog = nvc0->fragprog;
-
-      while (en) {
-         i = ffs(en) - 1;
-         en &= ~(1 << i);
-         if (prog->fp.in_pos[i] >= 0xc0 && prog->fp.in_pos[i] < 0xe0)
-            reg |= 8 << ((prog->fp.in_pos[i] - 0xc0) / 4);
-      }
-   }
-
-   BEGIN_RING(chan, RING_3D(POINT_COORD_REPLACE), 1);
-   OUT_RING  (chan, reg);
-}
-
 static void
 nvc0_constbufs_validate(struct nvc0_context *nvc0)
 {
@@ -321,7 +310,7 @@ nvc0_constbufs_validate(struct nvc0_context *nvc0)
    unsigned s;
 
    for (s = 0; s < 5; ++s) {
-      struct nvc0_resource *res;
+      struct nv04_resource *res;
       int i;
 
       while (nvc0->constbuf_dirty[s]) {
@@ -332,7 +321,7 @@ nvc0_constbufs_validate(struct nvc0_context *nvc0)
          i = ffs(nvc0->constbuf_dirty[s]) - 1;
          nvc0->constbuf_dirty[s] &= ~(1 << i);
 
-         res = nvc0_resource(nvc0->constbuf[s][i]);
+         res = nv04_resource(nvc0->constbuf[s][i]);
          if (!res) {
             BEGIN_RING(chan, RING_3D(CB_BIND(s)), 1);
             OUT_RING  (chan, (i << 4) | 0);
@@ -341,7 +330,7 @@ nvc0_constbufs_validate(struct nvc0_context *nvc0)
             continue;
          }
 
-         if (!nvc0_resource_mapped_by_gpu(&res->base)) {
+         if (!nouveau_resource_mapped_by_gpu(&res->base)) {
             if (i == 0) {
                base = s << 16;
                bo = nvc0->screen->uniforms;
@@ -407,6 +396,70 @@ nvc0_constbufs_validate(struct nvc0_context *nvc0)
    }
 }
 
+static void
+nvc0_validate_sample_mask(struct nvc0_context *nvc0)
+{
+   struct nouveau_channel *chan = nvc0->screen->base.channel;
+
+   unsigned mask[4] =
+   {
+      nvc0->sample_mask & 0xffff,
+      nvc0->sample_mask & 0xffff,
+      nvc0->sample_mask & 0xffff,
+      nvc0->sample_mask & 0xffff
+   };
+
+   BEGIN_RING(chan, RING_3D(MSAA_MASK(0)), 4);
+   OUT_RING  (chan, mask[0]);
+   OUT_RING  (chan, mask[1]);
+   OUT_RING  (chan, mask[2]);
+   OUT_RING  (chan, mask[3]);
+   BEGIN_RING(chan, RING_3D(SAMPLE_SHADING), 1);
+   OUT_RING  (chan, 0x01);
+}
+
+static void
+nvc0_validate_derived_1(struct nvc0_context *nvc0)
+{
+   struct nouveau_channel *chan = nvc0->screen->base.channel;
+   boolean early_z;
+
+   early_z = nvc0->fragprog->fp.early_z && !nvc0->zsa->pipe.alpha.enabled;
+
+   if (early_z != nvc0->state.early_z) {
+      nvc0->state.early_z = early_z;
+      IMMED_RING(chan, RING_3D(EARLY_FRAGMENT_TESTS), early_z);
+   }
+}
+
+static void
+nvc0_switch_pipe_context(struct nvc0_context *ctx_to)
+{
+   struct nvc0_context *ctx_from = ctx_to->screen->cur_ctx;
+
+   if (ctx_from)
+      ctx_to->state = ctx_from->state;
+
+   ctx_to->dirty = ~0;
+
+   if (!ctx_to->vertex)
+      ctx_to->dirty &= ~(NVC0_NEW_VERTEX | NVC0_NEW_ARRAYS);
+
+   if (!ctx_to->vertprog)
+      ctx_to->dirty &= ~NVC0_NEW_VERTPROG;
+   if (!ctx_to->fragprog)
+      ctx_to->dirty &= ~NVC0_NEW_FRAGPROG;
+
+   if (!ctx_to->blend)
+      ctx_to->dirty &= ~NVC0_NEW_BLEND;
+   if (!ctx_to->rast)
+      ctx_to->dirty &= ~NVC0_NEW_RASTERIZER;
+   if (!ctx_to->zsa)
+      ctx_to->dirty &= ~NVC0_NEW_ZSA;
+
+   ctx_to->screen->cur_ctx = ctx_to;
+}
+
 static struct state_validate {
     void (*func)(struct nvc0_context *);
     uint32_t states;
@@ -414,17 +467,12 @@ static struct state_validate {
     { nvc0_validate_fb,            NVC0_NEW_FRAMEBUFFER },
     { nvc0_validate_blend,         NVC0_NEW_BLEND },
     { nvc0_validate_zsa,           NVC0_NEW_ZSA },
+    { nvc0_validate_sample_mask,   NVC0_NEW_SAMPLE_MASK },
     { nvc0_validate_rasterizer,    NVC0_NEW_RASTERIZER },
     { nvc0_validate_blend_colour,  NVC0_NEW_BLEND_COLOUR },
     { nvc0_validate_stencil_ref,   NVC0_NEW_STENCIL_REF },
     { nvc0_validate_stipple,       NVC0_NEW_STIPPLE },
-#ifdef NVC0_SCISSORS_CLIPPING
-    { nvc0_validate_scissor,       NVC0_NEW_SCISSOR | NVC0_NEW_VIEWPORT |
-                                   NVC0_NEW_RASTERIZER |
-                                   NVC0_NEW_FRAMEBUFFER },
-#else
-    { nvc0_validate_scissor,       NVC0_NEW_SCISSOR },
-#endif
+    { nvc0_validate_scissor,       NVC0_NEW_SCISSOR | NVC0_NEW_RASTERIZER },
     { nvc0_validate_viewport,      NVC0_NEW_VIEWPORT },
     { nvc0_validate_clip,          NVC0_NEW_CLIP },
     { nvc0_vertprog_validate,      NVC0_NEW_VERTPROG },
@@ -432,7 +480,7 @@ static struct state_validate {
     { nvc0_tevlprog_validate,      NVC0_NEW_TEVLPROG },
     { nvc0_gmtyprog_validate,      NVC0_NEW_GMTYPROG },
     { nvc0_fragprog_validate,      NVC0_NEW_FRAGPROG },
-    { nvc0_validate_sprite_coords, NVC0_NEW_RASTERIZER | NVC0_NEW_FRAGPROG },
+    { nvc0_validate_derived_1,     NVC0_NEW_FRAGPROG | NVC0_NEW_ZSA },
     { nvc0_constbufs_validate,     NVC0_NEW_CONSTBUF },
     { nvc0_validate_textures,      NVC0_NEW_TEXTURES },
     { nvc0_validate_samplers,      NVC0_NEW_SAMPLERS },
@@ -445,11 +493,9 @@ boolean
 nvc0_state_validate(struct nvc0_context *nvc0)
 {
    unsigned i;
-#if 0
-   if (nvc0->screen->cur_ctx != nvc0) /* FIXME: not everything is valid */
-      nvc0->dirty = 0xffffffff;
-#endif
-   nvc0->screen->cur_ctx = nvc0;
+
+   if (nvc0->screen->cur_ctx != nvc0)
+      nvc0_switch_pipe_context(nvc0);
 
    if (nvc0->dirty) {
       for (i = 0; i < validate_list_len; ++i) {