nvfx: new 2D: new render temporaries with resources
[mesa.git] / src / gallium / drivers / nvfx / nvfx_state_emit.c
index cf73f81fb2c35a70f12f91390f8134e7d91571fe..dc70f3de87020acf01b9a7ee6451a228ecf0f5fa 100644 (file)
-#include "nv30/nv30_context.h"
-#include "nv40/nv40_context.h"
+#include "nvfx_context.h"
 #include "nvfx_state.h"
+#include "nvfx_resource.h"
 #include "draw/draw_context.h"
 
-#define RENDER_STATES(name, nvxx, vbo) \
-static struct nvfx_state_entry *name##_render_states[] = { \
-       &nvxx##_state_framebuffer, \
-       &nvfx_state_rasterizer, \
-       &nvfx_state_scissor, \
-       &nvxx##_state_stipple, \
-       &nvxx##_state_fragprog, \
-       &nvxx##_state_fragtex, \
-       &nvxx##_state_vertprog, \
-       &nvfx_state_blend, \
-       &nvfx_state_blend_colour, \
-       &nvxx##_state_zsa, \
-       &nvxx##_state_sr, \
-       &nvxx##_state_viewport, \
-       &nvxx##_state_##vbo, \
-       NULL \
-}
-
-RENDER_STATES(nv30, nv30, vbo);
-RENDER_STATES(nv30_swtnl, nv30, vbo); /* TODO: replace with vtxfmt once draw is unified */
-RENDER_STATES(nv40, nv40, vbo);
-RENDER_STATES(nv40_swtnl, nv40, vtxfmt);
-
-static void
-nvfx_state_do_validate(struct nvfx_context *nvfx,
-                      struct nvfx_state_entry **states)
+static boolean
+nvfx_state_validate_common(struct nvfx_context *nvfx)
 {
-       while (*states) {
-               struct nvfx_state_entry *e = *states;
+       struct nouveau_channel* chan = nvfx->screen->base.channel;
+       unsigned dirty;
+       int all_swizzled = -1;
+       boolean flush_tex_cache = FALSE;
+
+       if(nvfx != nvfx->screen->cur_ctx)
+       {
+               nvfx->dirty = ~0;
+               nvfx->screen->cur_ctx = nvfx;
+       }
 
-               if (nvfx->dirty & e->dirty.pipe) {
-                       if (e->validate(nvfx))
-                               nvfx->state.dirty |= (1ULL << e->dirty.hw);
+       /* These can trigger use the of 3D engine to copy temporaries.
+        * That will recurse here and thus dirty all 3D state, so we need to this before anything else, and in a loop..
+        * This converges to having clean temps, then binding both fragtexes and framebuffers.
+        */
+       while(nvfx->dirty & (NVFX_NEW_FB | NVFX_NEW_SAMPLER))
+       {
+               if(nvfx->dirty & NVFX_NEW_SAMPLER)
+               {
+                       nvfx->dirty &=~ NVFX_NEW_SAMPLER;
+                       nvfx_fragtex_validate(nvfx);
+
+                       // TODO: only set this if really necessary
+                       flush_tex_cache = TRUE;
                }
 
-               states++;
+               if(nvfx->dirty & NVFX_NEW_FB)
+               {
+                       nvfx->dirty &=~ NVFX_NEW_FB;
+                       all_swizzled = nvfx_framebuffer_prepare(nvfx);
+
+                       // TODO: make sure this doesn't happen, i.e. fbs have matching formats
+                       assert(all_swizzled >= 0);
+               }
        }
-       nvfx->dirty = 0;
-}
 
-void
-nvfx_state_emit(struct nvfx_context *nvfx)
-{
-       struct nvfx_state *state = &nvfx->state;
-       struct nvfx_screen *screen = nvfx->screen;
-       struct nouveau_channel *chan = screen->base.channel;
-       struct nouveau_grobj *eng3d = screen->eng3d;
-       unsigned i;
-       uint64_t states;
-
-       /* XXX: race conditions
-        */
-       if (nvfx != screen->cur_ctx) {
-               for (i = 0; i < NVFX_STATE_MAX; i++) {
-                       if (state->hw[i] && screen->state[i] != state->hw[i])
-                               state->dirty |= (1ULL << i);
+       dirty = nvfx->dirty;
+
+       if(nvfx->render_mode == HW)
+       {
+               if(dirty & (NVFX_NEW_VERTPROG | NVFX_NEW_VERTCONST | NVFX_NEW_UCP))
+               {
+                       if(!nvfx_vertprog_validate(nvfx))
+                               return FALSE;
                }
 
-               screen->cur_ctx = nvfx;
+               if(dirty & (NVFX_NEW_ARRAYS))
+               {
+                       if(!nvfx_vbo_validate(nvfx))
+                               return FALSE;
+               }
        }
+       else
+       {
+               /* TODO: this looks a bit misdesigned */
+               if(dirty & (NVFX_NEW_VERTPROG | NVFX_NEW_UCP))
+                       nvfx_vertprog_validate(nvfx);
 
-       for (i = 0, states = state->dirty; states; i++) {
-               if (!(states & (1ULL << i)))
-                       continue;
-               so_ref (state->hw[i], &nvfx->screen->state[i]);
-               if (state->hw[i])
-                       so_emit(chan, nvfx->screen->state[i]);
-               states &= ~(1ULL << i);
+               if(dirty & (NVFX_NEW_ARRAYS | NVFX_NEW_FRAGPROG))
+                       nvfx_vtxfmt_validate(nvfx);
        }
 
-       /* TODO: could nv30 need this or something similar too? */
-       if(nvfx->is_nv4x) {
-               if (state->dirty & ((1ULL << NVFX_STATE_FRAGPROG) |
-                                   (1ULL << NVFX_STATE_FRAGTEX0))) {
-                       BEGIN_RING(chan, eng3d, NV40TCL_TEX_CACHE_CTL, 1);
-                       OUT_RING  (chan, 2);
-                       BEGIN_RING(chan, eng3d, NV40TCL_TEX_CACHE_CTL, 1);
-                       OUT_RING  (chan, 1);
+       if(dirty & NVFX_NEW_RAST)
+               sb_emit(chan, nvfx->rasterizer->sb, nvfx->rasterizer->sb_len);
+
+       if(dirty & NVFX_NEW_SCISSOR)
+               nvfx_state_scissor_validate(nvfx);
+
+       if(dirty & NVFX_NEW_STIPPLE)
+               nvfx_state_stipple_validate(nvfx);
+
+       if(dirty & (NVFX_NEW_FRAGPROG | NVFX_NEW_FRAGCONST))
+       {
+               nvfx_fragprog_validate(nvfx);
+               if(dirty & NVFX_NEW_FRAGPROG)
+                       flush_tex_cache = TRUE; // TODO: do we need this?
+       }
+
+       if(all_swizzled >= 0)
+               nvfx_framebuffer_validate(nvfx, all_swizzled);
+
+       if(dirty & NVFX_NEW_BLEND)
+               sb_emit(chan, nvfx->blend->sb, nvfx->blend->sb_len);
+
+       if(dirty & NVFX_NEW_BCOL)
+               nvfx_state_blend_colour_validate(nvfx);
+
+       if(dirty & NVFX_NEW_ZSA)
+               sb_emit(chan, nvfx->zsa->sb, nvfx->zsa->sb_len);
+
+       if(dirty & NVFX_NEW_SR)
+               nvfx_state_sr_validate(nvfx);
+
+/* Having this depend on FB looks wrong, but it seems
+   necessary to make this work on nv3x
+   TODO: find the right fix
+*/
+       if(dirty & (NVFX_NEW_VIEWPORT | NVFX_NEW_FB))
+               nvfx_state_viewport_validate(nvfx);
+
+       if(flush_tex_cache)
+       {
+               // TODO: what about nv30?
+               if(nvfx->is_nv4x)
+               {
+                       WAIT_RING(chan, 4);
+                       OUT_RING(chan, RING_3D(NV40TCL_TEX_CACHE_CTL, 1));
+                       OUT_RING(chan, 2);
+                       OUT_RING(chan, RING_3D(NV40TCL_TEX_CACHE_CTL, 1));
+                       OUT_RING(chan, 1);
                }
        }
-       state->dirty = 0;
+       nvfx->dirty = 0;
+       return TRUE;
 }
 
 void
-nvfx_state_flush_notify(struct nouveau_channel *chan)
+nvfx_state_emit(struct nvfx_context *nvfx)
 {
-       struct nvfx_context *nvfx = chan->user_private;
-       struct nvfx_state *state = &nvfx->state;
-       unsigned i, samplers;
-
-       so_emit_reloc_markers(chan, state->hw[NVFX_STATE_FB]);
-       for (i = 0, samplers = state->fp_samplers; i < 16 && samplers; i++) {
-               if (!(samplers & (1 << i)))
-                       continue;
-               so_emit_reloc_markers(chan,
-                                     state->hw[NVFX_STATE_FRAGTEX0+i]);
-               samplers &= ~(1ULL << i);
+       struct nouveau_channel* chan = nvfx->screen->base.channel;
+       /* we need to ensure there is enough space to output relocations in one go */
+       unsigned max_relocs = 0
+             + 16 /* vertex buffers, incl. dma flag */
+             + 2 /* index buffer plus format+dma flag */
+             + 2 * 5 /* 4 cbufs + zsbuf, plus dma objects */
+             + 2 * 16 /* fragment textures plus format+dma flag */
+             + 2 * 4 /* vertex textures plus format+dma flag */
+             + 1 /* fragprog incl dma flag */
+             ;
+       MARK_RING(chan, max_relocs * 2, max_relocs * 2);
+       nvfx_state_relocate(nvfx);
+
+       unsigned render_temps = nvfx->state.render_temps;
+       if(render_temps)
+       {
+               for(int i = 0; i < nvfx->framebuffer.nr_cbufs; ++i)
+               {
+                       if(render_temps & (1 << i))
+                               util_dirty_surface_set_dirty(nvfx_surface_get_dirty_surfaces(nvfx->framebuffer.cbufs[i]),
+                                               (struct util_dirty_surface*)nvfx->framebuffer.cbufs[i]);
+               }
+
+               if(render_temps & 0x80)
+                       util_dirty_surface_set_dirty(nvfx_surface_get_dirty_surfaces(nvfx->framebuffer.zsbuf),
+                                       (struct util_dirty_surface*)nvfx->framebuffer.zsbuf);
        }
-       so_emit_reloc_markers(chan, state->hw[NVFX_STATE_FRAGPROG]);
-       if (state->hw[NVFX_STATE_VTXBUF] && nvfx->render_mode == HW)
-               so_emit_reloc_markers(chan, state->hw[NVFX_STATE_VTXBUF]);
+}
+
+void
+nvfx_state_relocate(struct nvfx_context *nvfx)
+{
+       nvfx_framebuffer_relocate(nvfx);
+       nvfx_fragtex_relocate(nvfx);
+       nvfx_fragprog_relocate(nvfx);
+       if (nvfx->render_mode == HW)
+               nvfx_vbo_relocate(nvfx);
 }
 
 boolean
@@ -120,19 +178,13 @@ nvfx_state_validate(struct nvfx_context *nvfx)
                        return FALSE;
 
                /* Attempt to go to hwtnl again */
-               nvfx->pipe.flush(&nvfx->pipe, 0, NULL);
                nvfx->dirty |= (NVFX_NEW_VIEWPORT |
                                NVFX_NEW_VERTPROG |
                                NVFX_NEW_ARRAYS);
                nvfx->render_mode = HW;
        }
 
-       if(!nvfx->is_nv4x)
-               nvfx_state_do_validate(nvfx, nv30_render_states);
-       else
-               nvfx_state_do_validate(nvfx, nv40_render_states);
-
-       if (nvfx->fallback_swtnl || nvfx->fallback_swrast)
+       if(!nvfx_state_validate_common(nvfx))
                return FALSE;
 
        if (was_sw)
@@ -160,7 +212,8 @@ nvfx_state_validate_swtnl(struct nvfx_context *nvfx)
                draw_bind_vertex_shader(draw, nvfx->vertprog->draw);
 
        if (nvfx->draw_dirty & NVFX_NEW_RAST)
-               draw_set_rasterizer_state(draw, &nvfx->rasterizer->pipe);
+           draw_set_rasterizer_state(draw, &nvfx->rasterizer->pipe,
+                                     nvfx->rasterizer);
 
        if (nvfx->draw_dirty & NVFX_NEW_UCP)
                draw_set_clip_state(draw, &nvfx->clip);
@@ -173,15 +226,7 @@ nvfx_state_validate_swtnl(struct nvfx_context *nvfx)
                draw_set_vertex_elements(draw, nvfx->vtxelt->num_elements, nvfx->vtxelt->pipe);
        }
 
-       if(!nvfx->is_nv4x)
-               nvfx_state_do_validate(nvfx, nv30_swtnl_render_states);
-       else
-               nvfx_state_do_validate(nvfx, nv40_swtnl_render_states);
-
-       if (nvfx->fallback_swrast) {
-               NOUVEAU_ERR("swtnl->swrast 0x%08x\n", nvfx->fallback_swrast);
-               return FALSE;
-       }
+       nvfx_state_validate_common(nvfx);
 
        nvfx->draw_dirty = 0;
        return TRUE;