llvmpipe: Cleanup depth-stencil clears.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
index 21509560084cbef357801f240ad80e33440691bb..e72ead0def4041224c1bdcfefe10a789ffdf750d 100644 (file)
  * lp_setup_flush().
  */
 
+#include <limits.h>
+
 #include "pipe/p_defines.h"
+#include "util/u_framebuffer.h"
 #include "util/u_inlines.h"
 #include "util/u_memory.h"
 #include "util/u_pack_color.h"
-#include "util/u_surface.h"
 #include "lp_context.h"
+#include "lp_memory.h"
 #include "lp_scene.h"
-#include "lp_scene_queue.h"
 #include "lp_texture.h"
 #include "lp_debug.h"
 #include "lp_fence.h"
+#include "lp_query.h"
 #include "lp_rast.h"
 #include "lp_setup_context.h"
 #include "lp_screen.h"
 #include "draw/draw_vbuf.h"
 
 
-static void set_scene_state( struct lp_setup_context *, enum setup_state );
+static void set_scene_state( struct lp_setup_context *, enum setup_state,
+                             const char *reason);
+static boolean try_update_scene_state( struct lp_setup_context *setup );
 
 
-struct lp_scene *
-lp_setup_get_current_scene(struct lp_setup_context *setup)
+static void
+lp_setup_get_empty_scene(struct lp_setup_context *setup)
 {
-   if (!setup->scene) {
-
-      /* wait for a free/empty scene
-       */
-      setup->scene = lp_scene_dequeue(setup->empty_scenes, TRUE);
+   assert(setup->scene == NULL);
 
-      assert(lp_scene_is_empty(setup->scene));
+   setup->scene_idx++;
+   setup->scene_idx %= Elements(setup->scenes);
 
-      lp_scene_begin_binning(setup->scene,
-                             &setup->fb );
-   }
-   return setup->scene;
-}
+   setup->scene = setup->scenes[setup->scene_idx];
 
+   if (setup->scene->fence) {
+      if (LP_DEBUG & DEBUG_SETUP)
+         debug_printf("%s: wait for scene %d\n",
+                      __FUNCTION__, setup->scene->fence->id);
 
-/**
- * Check if the size of the current scene has exceeded the limit.
- * If so, flush/render it.
- */
-static void
-setup_check_scene_size_and_flush(struct lp_setup_context *setup)
-{
-   if (setup->scene) {
-      struct lp_scene *scene = lp_setup_get_current_scene(setup);
-      unsigned size = lp_scene_get_size(scene);
-
-      if (size > LP_MAX_SCENE_SIZE) {
-         /*printf("LLVMPIPE: scene size = %u, flushing.\n", size);*/
-         set_scene_state( setup, SETUP_FLUSHED );
-         /*assert(lp_scene_get_size(scene) == 0);*/
-      }
+      lp_fence_wait(setup->scene->fence);
    }
+
+   lp_scene_begin_binning(setup->scene, &setup->fb);
+   
 }
 
 
@@ -100,7 +90,7 @@ first_triangle( struct lp_setup_context *setup,
                 const float (*v1)[4],
                 const float (*v2)[4])
 {
-   set_scene_state( setup, SETUP_ACTIVE );
+   assert(setup->state == SETUP_ACTIVE);
    lp_setup_choose_triangle( setup );
    setup->triangle( setup, v0, v1, v2 );
 }
@@ -110,7 +100,7 @@ first_line( struct lp_setup_context *setup,
            const float (*v0)[4],
            const float (*v1)[4])
 {
-   set_scene_state( setup, SETUP_ACTIVE );
+   assert(setup->state == SETUP_ACTIVE);
    lp_setup_choose_line( setup );
    setup->line( setup, v0, v1 );
 }
@@ -119,12 +109,12 @@ static void
 first_point( struct lp_setup_context *setup,
             const float (*v0)[4])
 {
-   set_scene_state( setup, SETUP_ACTIVE );
+   assert(setup->state == SETUP_ACTIVE);
    lp_setup_choose_point( setup );
    setup->point( setup, v0 );
 }
 
-static void reset_context( struct lp_setup_context *setup )
+static void lp_setup_reset( struct lp_setup_context *setup )
 {
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
@@ -139,7 +129,7 @@ static void reset_context( struct lp_setup_context *setup )
 
    /* Reset some state:
     */
-   setup->clear.flags = 0;
+   memset(&setup->clear, 0, sizeof setup->clear);
 
    /* Have an explicit "start-binning" call and get rid of this
     * pointer twiddling?
@@ -154,11 +144,23 @@ static void reset_context( struct lp_setup_context *setup )
 static void
 lp_setup_rasterize_scene( struct lp_setup_context *setup )
 {
-   struct lp_scene *scene = lp_setup_get_current_scene(setup);
+   struct lp_scene *scene = setup->scene;
+   struct llvmpipe_screen *screen = llvmpipe_screen(scene->pipe->screen);
 
-   lp_scene_rasterize(scene, setup->rast);
+   lp_scene_end_binning(scene);
 
-   reset_context( setup );
+   lp_fence_reference(&setup->last_fence, scene->fence);
+
+   if (setup->last_fence)
+      setup->last_fence->issued = TRUE;
+
+   pipe_mutex_lock(screen->rast_mutex);
+   lp_rast_queue_scene(screen->rast, scene);
+   lp_rast_finish(screen->rast);
+   pipe_mutex_unlock(screen->rast_mutex);
+
+   lp_scene_end_rasterization(setup->scene);
+   lp_setup_reset( setup );
 
    LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
 }
@@ -168,30 +170,73 @@ lp_setup_rasterize_scene( struct lp_setup_context *setup )
 static void
 begin_binning( struct lp_setup_context *setup )
 {
-   struct lp_scene *scene = lp_setup_get_current_scene(setup);
+   struct lp_scene *scene = setup->scene;
+   boolean need_zsload = FALSE;
+   boolean ok;
+   unsigned i, j;
+
+   assert(scene);
+   assert(scene->fence == NULL);
+
+   /* Always create a fence:
+    */
+   scene->fence = lp_fence_create(MAX2(1, setup->num_threads));
+
+   /* Initialize the bin flags and x/y coords:
+    */
+   for (i = 0; i < scene->tiles_x; i++) {
+      for (j = 0; j < scene->tiles_y; j++) {
+         scene->tile[i][j].x = i;
+         scene->tile[i][j].y = j;
+      }
+   }
+
+   ok = try_update_scene_state(setup);
+   assert(ok);
+
+   if (setup->fb.zsbuf &&
+       ((setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
+        util_format_is_depth_and_stencil(setup->fb.zsbuf->format))
+      need_zsload = TRUE;
 
    LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
           (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
-          (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) ? "clear": "load");
+          need_zsload ? "clear": "load");
 
    if (setup->fb.nr_cbufs) {
       if (setup->clear.flags & PIPE_CLEAR_COLOR) {
-         lp_scene_bin_everywhere( scene, 
-                                 lp_rast_clear_color
-                                 setup->clear.color );
-         scene->has_color_clear = TRUE;
+         ok = lp_scene_bin_everywhere( scene, 
+                                       LP_RAST_OP_CLEAR_COLOR
+                                       setup->clear.color );
+         assert(ok);
       }
    }
 
    if (setup->fb.zsbuf) {
       if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
-         lp_scene_bin_everywhere( scene, 
-                                 lp_rast_clear_zstencil, 
-                                 setup->clear.zstencil );
-         scene->has_depth_clear = TRUE;
+         if (!need_zsload)
+            scene->has_depthstencil_clear = TRUE;
+         ok = lp_scene_bin_everywhere( scene,
+                                       LP_RAST_OP_CLEAR_ZSTENCIL,
+                                       lp_rast_arg_clearzs(
+                                          setup->clear.zsvalue,
+                                          setup->clear.zsmask));
+         assert(ok);
       }
    }
 
+   if (setup->active_query) {
+      ok = lp_scene_bin_everywhere( scene,
+                                    LP_RAST_OP_BEGIN_QUERY,
+                                    lp_rast_arg_query(setup->active_query) );
+      assert(ok);
+   }
+      
+
+   setup->clear.flags = 0;
+   setup->clear.zsmask = 0;
+   setup->clear.zsvalue = 0;
+
    LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
 }
 
@@ -207,38 +252,57 @@ execute_clears( struct lp_setup_context *setup )
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
    begin_binning( setup );
-   lp_setup_rasterize_scene( setup );
 }
 
+const char *states[] = {
+   "FLUSHED",
+   "EMPTY  ",
+   "CLEARED",
+   "ACTIVE "
+};
+
 
 static void
 set_scene_state( struct lp_setup_context *setup,
-                 enum setup_state new_state )
+                 enum setup_state new_state,
+                 const char *reason)
 {
    unsigned old_state = setup->state;
 
    if (old_state == new_state)
       return;
-       
-   LP_DBG(DEBUG_SETUP, "%s old %d new %d\n", __FUNCTION__, old_state, new_state);
+   
+   if (LP_DEBUG & DEBUG_SCENE) {
+      debug_printf("%s old %s new %s%s%s\n",
+                   __FUNCTION__,
+                   states[old_state],
+                   states[new_state],
+                   (new_state == SETUP_FLUSHED) ? ": " : "",
+                   (new_state == SETUP_FLUSHED) ? reason : "");
+
+      if (new_state == SETUP_FLUSHED && setup->scene)
+         lp_debug_draw_bins_by_cmd_length(setup->scene);
+   }
+
+   /* wait for a free/empty scene
+    */
+   if (old_state == SETUP_FLUSHED) 
+      lp_setup_get_empty_scene(setup);
 
    switch (new_state) {
+   case SETUP_CLEARED:
+      break;
+
    case SETUP_ACTIVE:
       begin_binning( setup );
       break;
 
-   case SETUP_CLEARED:
-      if (old_state == SETUP_ACTIVE) {
-         assert(0);
-         return;
-      }
-      break;
-      
    case SETUP_FLUSHED:
       if (old_state == SETUP_CLEARED)
          execute_clears( setup );
-      else
-         lp_setup_rasterize_scene( setup );
+
+      lp_setup_rasterize_scene( setup );
+      assert(setup->scene == NULL);
       break;
 
    default:
@@ -254,26 +318,15 @@ set_scene_state( struct lp_setup_context *setup,
  */
 void
 lp_setup_flush( struct lp_setup_context *setup,
-                unsigned flags )
+                unsigned flags,
+                struct pipe_fence_handle **fence,
+                const char *reason)
 {
-   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
+   set_scene_state( setup, SETUP_FLUSHED, reason );
 
-   if (setup->scene) {
-      struct lp_scene *scene = lp_setup_get_current_scene(setup);
-      union lp_rast_cmd_arg dummy = {0};
-
-      if (flags & (PIPE_FLUSH_SWAPBUFFERS |
-                   PIPE_FLUSH_FRAME)) {
-         /* Store colors in the linear color buffer(s).
-          * If we don't do this here, we'll end up converting the tiled
-          * data to linear in the texture_unmap() function, which will
-          * not be a parallel/threaded operation as here.
-          */
-         lp_scene_bin_everywhere(scene, lp_rast_store_color, dummy);
-      }
+   if (fence) {
+      lp_fence_reference((struct lp_fence **)fence, setup->last_fence);
    }
-
-   set_scene_state( setup, SETUP_FLUSHED );
 }
 
 
@@ -285,41 +338,63 @@ lp_setup_bind_framebuffer( struct lp_setup_context *setup,
 
    /* Flush any old scene.
     */
-   set_scene_state( setup, SETUP_FLUSHED );
+   set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
+
+   /*
+    * Ensure the old scene is not reused.
+    */
+   assert(!setup->scene);
 
    /* Set new state.  This will be picked up later when we next need a
     * scene.
     */
    util_copy_framebuffer_state(&setup->fb, fb);
+   setup->framebuffer.x0 = 0;
+   setup->framebuffer.y0 = 0;
+   setup->framebuffer.x1 = fb->width-1;
+   setup->framebuffer.y1 = fb->height-1;
+   setup->dirty |= LP_SETUP_NEW_SCISSOR;
 }
 
 
-void
-lp_setup_clear( struct lp_setup_context *setup,
-                const float *color,
-                double depth,
-                unsigned stencil,
-                unsigned flags )
+static boolean
+lp_setup_try_clear( struct lp_setup_context *setup,
+                    const float *color,
+                    double depth,
+                    unsigned stencil,
+                    unsigned flags )
 {
-   struct lp_scene *scene = lp_setup_get_current_scene(setup);
+   uint32_t zsmask = 0;
+   uint32_t zsvalue = 0;
+   union lp_rast_cmd_arg color_arg;
    unsigned i;
 
    LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
 
-
    if (flags & PIPE_CLEAR_COLOR) {
-      for (i = 0; i < 4; ++i)
-         setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
+      for (i = 0; i < 4; i++)
+         color_arg.clear_color[i] = float_to_ubyte(color[i]);
    }
 
    if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
-      setup->clear.zstencil.clear_zstencil = 
-         util_pack_z_stencil(setup->fb.zsbuf->format, 
-                             depth,
-                             stencil);
+      uint32_t zmask = (flags & PIPE_CLEAR_DEPTH) ? ~0 : 0;
+      uint32_t smask = (flags & PIPE_CLEAR_STENCIL) ? ~0 : 0;
+
+      zsvalue = util_pack_z_stencil(setup->fb.zsbuf->format,
+                                    depth,
+                                    stencil);
+
+
+      zsmask = util_pack_mask_z_stencil(setup->fb.zsbuf->format,
+                                        zmask,
+                                        smask);
+
+      zsvalue &= zsmask;
    }
 
    if (setup->state == SETUP_ACTIVE) {
+      struct lp_scene *scene = setup->scene;
+
       /* Add the clear to existing scene.  In the unusual case where
        * both color and depth-stencil are being cleared when there's
        * already been some rendering, we could discard the currently
@@ -327,19 +402,18 @@ lp_setup_clear( struct lp_setup_context *setup,
        * a common usage.
        */
       if (flags & PIPE_CLEAR_COLOR) {
-         lp_scene_bin_everywhere( scene, 
-                                  lp_rast_clear_color,
-                                  setup->clear.color );
-         scene->has_color_clear = TRUE;
+         if (!lp_scene_bin_everywhere( scene, 
+                                       LP_RAST_OP_CLEAR_COLOR,
+                                       color_arg ))
+            return FALSE;
       }
 
-      if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
-         lp_scene_bin_everywhere( scene, 
-                                  lp_rast_clear_zstencil,
-                                  setup->clear.zstencil );
-         scene->has_depth_clear = TRUE;
+      if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
+         if (!lp_scene_bin_everywhere( scene,
+                                       LP_RAST_OP_CLEAR_ZSTENCIL,
+                                       lp_rast_arg_clearzs(zsvalue, zsmask) ))
+            return FALSE;
       }
-
    }
    else {
       /* Put ourselves into the 'pre-clear' state, specifically to try
@@ -347,39 +421,43 @@ lp_setup_clear( struct lp_setup_context *setup,
        * buffers which the app or state-tracker might issue
        * separately.
        */
-      set_scene_state( setup, SETUP_CLEARED );
+      set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );
 
       setup->clear.flags |= flags;
+
+      if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
+         setup->clear.zsmask |= zsmask;
+         setup->clear.zsvalue =
+            (setup->clear.zsvalue & ~zsmask) | (zsvalue & zsmask);
+      }
+
+      if (flags & PIPE_CLEAR_COLOR) {
+         memcpy(setup->clear.color.clear_color,
+                &color_arg,
+                sizeof setup->clear.color.clear_color);
+      }
    }
+   
+   return TRUE;
 }
 
-
-/**
- * Emit a fence.
- */
-struct pipe_fence_handle *
-lp_setup_fence( struct lp_setup_context *setup )
+void
+lp_setup_clear( struct lp_setup_context *setup,
+                const float *color,
+                double depth,
+                unsigned stencil,
+                unsigned flags )
 {
-   if (setup->num_threads == 0) {
-      return NULL;
-   }
-   else {
-      struct lp_scene *scene = lp_setup_get_current_scene(setup);
-      const unsigned rank = lp_scene_get_num_bins( scene ); /* xxx */
-      struct lp_fence *fence = lp_fence_create(rank);
+   if (!lp_setup_try_clear( setup, color, depth, stencil, flags )) {
+      lp_setup_flush(setup, 0, NULL, __FUNCTION__);
 
-      LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
+      if (!lp_setup_try_clear( setup, color, depth, stencil, flags ))
+         assert(0);
+   }
+}
 
-      set_scene_state( setup, SETUP_ACTIVE );
 
-      /* insert the fence into all command bins */
-      lp_scene_bin_everywhere( scene,
-                               lp_rast_fence,
-                               lp_rast_arg_fence(fence) );
 
-      return (struct pipe_fence_handle *) fence;
-   }
-}
 
 
 void 
@@ -394,11 +472,37 @@ lp_setup_set_triangle_state( struct lp_setup_context *setup,
    setup->ccw_is_frontface = ccw_is_frontface;
    setup->cullmode = cull_mode;
    setup->triangle = first_triangle;
-   setup->scissor_test = scissor;
    setup->pixel_offset = gl_rasterization_rules ? 0.5f : 0.0f;
+
+   if (setup->scissor_test != scissor) {
+      setup->dirty |= LP_SETUP_NEW_SCISSOR;
+      setup->scissor_test = scissor;
+   }
+}
+
+void 
+lp_setup_set_line_state( struct lp_setup_context *setup,
+                        float line_width)
+{
+   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
+
+   setup->line_width = line_width;
 }
 
+void 
+lp_setup_set_point_state( struct lp_setup_context *setup,
+                          float point_size,                          
+                          boolean point_size_per_vertex,
+                          uint sprite_coord_enable,
+                          uint sprite_coord_origin)
+{
+   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
+   setup->point_size = point_size;
+   setup->sprite_coord_enable = sprite_coord_enable;
+   setup->sprite_coord_origin = sprite_coord_origin;
+   setup->point_size_per_vertex = point_size_per_vertex;
+}
 
 void
 lp_setup_set_fs_inputs( struct lp_setup_context *setup,
@@ -412,17 +516,14 @@ lp_setup_set_fs_inputs( struct lp_setup_context *setup,
 }
 
 void
-lp_setup_set_fs_functions( struct lp_setup_context *setup,
-                           lp_jit_frag_func jit_function0,
-                           lp_jit_frag_func jit_function1,
-                           boolean opaque )
+lp_setup_set_fs_variant( struct lp_setup_context *setup,
+                         struct lp_fragment_shader_variant *variant)
 {
-   LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function0);
+   LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
+          variant);
    /* FIXME: reference count */
 
-   setup->fs.current.jit_function[0] = jit_function0;
-   setup->fs.current.jit_function[1] = jit_function1;
-   setup->fs.current.opaque = opaque;
+   setup->fs.current.variant = variant;
    setup->dirty |= LP_SETUP_NEW_FS;
 }
 
@@ -487,10 +588,11 @@ lp_setup_set_scissor( struct lp_setup_context *setup,
 
    assert(scissor);
 
-   if (memcmp(&setup->scissor.current, scissor, sizeof(*scissor)) != 0) {
-      setup->scissor.current = *scissor; /* struct copy */
-      setup->dirty |= LP_SETUP_NEW_SCISSOR;
-   }
+   setup->scissor.x0 = scissor->minx;
+   setup->scissor.x1 = scissor->maxx-1;
+   setup->scissor.y0 = scissor->miny;
+   setup->scissor.y1 = scissor->maxy-1;
+   setup->dirty |= LP_SETUP_NEW_SCISSOR;
 }
 
 
@@ -529,7 +631,7 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
    for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
       struct pipe_sampler_view *view = i < num ? views[i] : NULL;
 
-      if(view) {
+      if (view) {
          struct pipe_resource *tex = view->texture;
          struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
          struct lp_jit_texture *jit_tex;
@@ -553,6 +655,18 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
                                                  LP_TEX_LAYOUT_LINEAR);
                jit_tex->row_stride[j] = lp_tex->row_stride[j];
                jit_tex->img_stride[j] = lp_tex->img_stride[j];
+
+               if ((LP_PERF & PERF_TEX_MEM) ||
+                  !jit_tex->data[j]) {
+                  /* out of memory - use dummy tile memory */
+                  jit_tex->data[j] = lp_dummy_tile;
+                  jit_tex->width = TILE_SIZE/8;
+                  jit_tex->height = TILE_SIZE/8;
+                  jit_tex->depth = 1;
+                  jit_tex->last_level = 0;
+                  jit_tex->row_stride[j] = 0;
+                  jit_tex->img_stride[j] = 0;
+               }
             }
          }
          else {
@@ -560,7 +674,6 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
             /*
              * XXX: Where should this be unmapped?
              */
-
             struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
             struct sw_winsys *winsys = screen->winsys;
             jit_tex->data[0] = winsys->displaytarget_map(winsys, lp_tex->dt,
@@ -576,6 +689,38 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
 }
 
 
+/**
+ * Called during state validation when LP_NEW_SAMPLER is set.
+ */
+void
+lp_setup_set_fragment_sampler_state(struct lp_setup_context *setup,
+                                    unsigned num,
+                                    const struct pipe_sampler_state **samplers)
+{
+   unsigned i;
+
+   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
+
+   assert(num <= PIPE_MAX_SAMPLERS);
+
+   for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
+      const struct pipe_sampler_state *sampler = i < num ? samplers[i] : NULL;
+
+      if (sampler) {
+         struct lp_jit_texture *jit_tex;
+         jit_tex = &setup->fs.current.jit_context.textures[i];
+
+         jit_tex->min_lod = sampler->min_lod;
+         jit_tex->max_lod = sampler->max_lod;
+         jit_tex->lod_bias = sampler->lod_bias;
+         COPY_4V(jit_tex->border_color, sampler->border_color);
+      }
+   }
+
+   setup->dirty |= LP_SETUP_NEW_FS;
+}
+
+
 /**
  * Is the given texture referenced by any scene?
  * Note: we have to check all scenes including any scenes currently
@@ -610,38 +755,23 @@ lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
 /**
  * Called by vbuf code when we're about to draw something.
  */
-void
-lp_setup_update_state( struct lp_setup_context *setup )
+static boolean
+try_update_scene_state( struct lp_setup_context *setup )
 {
-   struct lp_scene *scene;
+   boolean new_scene = (setup->fs.stored == NULL);
+   struct lp_scene *scene = setup->scene;
 
-   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
-
-   setup_check_scene_size_and_flush(setup);
-
-   scene = lp_setup_get_current_scene(setup);
-
-   assert(setup->fs.current.jit_function);
-
-   /* Some of the 'draw' pipeline stages may have changed some driver state.
-    * Make sure we've processed those state changes before anything else.
-    *
-    * XXX this is the only place where llvmpipe_context is used in the
-    * setup code.  This may get refactored/changed...
-    */
-   {
-      struct llvmpipe_context *lp = llvmpipe_context(scene->pipe);
-      if (lp->dirty) {
-         llvmpipe_update_derived(lp);
-      }
-      assert(lp->dirty == 0);
-   }
+   assert(scene);
 
    if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
       uint8_t *stored;
       unsigned i, j;
 
       stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
+      if (!stored) {
+         assert(!new_scene);
+         return FALSE;
+      }
 
       /* smear each blend color component across 16 ubyte elements */
       for (i = 0; i < 4; ++i) {
@@ -651,31 +781,10 @@ lp_setup_update_state( struct lp_setup_context *setup )
       }
 
       setup->blend_color.stored = stored;
-
       setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
       setup->dirty |= LP_SETUP_NEW_FS;
    }
 
-   if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
-      float *stored;
-
-      stored = lp_scene_alloc_aligned(scene, 4 * sizeof(int32_t), 16);
-
-      stored[0] = (float) setup->scissor.current.minx;
-      stored[1] = (float) setup->scissor.current.miny;
-      stored[2] = (float) setup->scissor.current.maxx;
-      stored[3] = (float) setup->scissor.current.maxy;
-
-      setup->scissor.stored = stored;
-
-      setup->fs.current.jit_context.scissor_xmin = stored[0];
-      setup->fs.current.jit_context.scissor_ymin = stored[1];
-      setup->fs.current.jit_context.scissor_xmax = stored[2];
-      setup->fs.current.jit_context.scissor_ymax = stored[3];
-
-      setup->dirty |= LP_SETUP_NEW_FS;
-   }
-
    if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
       struct pipe_resource *buffer = setup->constants.current;
 
@@ -693,13 +802,16 @@ lp_setup_update_state( struct lp_setup_context *setup )
             void *stored;
 
             stored = lp_scene_alloc(scene, current_size);
-            if(stored) {
-               memcpy(stored,
-                      current_data,
-                      current_size);
-               setup->constants.stored_size = current_size;
-               setup->constants.stored_data = stored;
+            if (!stored) {
+               assert(!new_scene);
+               return FALSE;
             }
+
+            memcpy(stored,
+                   current_data,
+                   current_size);
+            setup->constants.stored_size = current_size;
+            setup->constants.stored_data = stored;
          }
       }
       else {
@@ -712,43 +824,98 @@ lp_setup_update_state( struct lp_setup_context *setup )
    }
 
 
-   if(setup->dirty & LP_SETUP_NEW_FS) {
-      if(!setup->fs.stored ||
-         memcmp(setup->fs.stored,
-                &setup->fs.current,
-                sizeof setup->fs.current) != 0) {
+   if (setup->dirty & LP_SETUP_NEW_FS) {
+      if (!setup->fs.stored ||
+          memcmp(setup->fs.stored,
+                 &setup->fs.current,
+                 sizeof setup->fs.current) != 0)
+      {
+         struct lp_rast_state *stored;
+         uint i;
+         
          /* The fs state that's been stored in the scene is different from
           * the new, current state.  So allocate a new lp_rast_state object
           * and append it to the bin's setup data buffer.
           */
-         uint i;
-         struct lp_rast_state *stored =
-            (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
-         if(stored) {
-            memcpy(stored,
-                   &setup->fs.current,
-                   sizeof setup->fs.current);
-            setup->fs.stored = stored;
-
-            /* put the state-set command into all bins */
-            lp_scene_bin_state_command( scene,
-                                       lp_rast_set_state, 
-                                       lp_rast_arg_state(setup->fs.stored) );
+         stored = (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
+         if (!stored) {
+            assert(!new_scene);
+            return FALSE;
          }
 
+         memcpy(stored,
+                &setup->fs.current,
+                sizeof setup->fs.current);
+         setup->fs.stored = stored;
+         
          /* The scene now references the textures in the rasterization
           * state record.  Note that now.
           */
          for (i = 0; i < Elements(setup->fs.current_tex); i++) {
-            if (setup->fs.current_tex[i])
-               lp_scene_add_resource_reference(scene, setup->fs.current_tex[i]);
+            if (setup->fs.current_tex[i]) {
+               if (!lp_scene_add_resource_reference(scene,
+                                                    setup->fs.current_tex[i],
+                                                    new_scene)) {
+                  assert(!new_scene);
+                  return FALSE;
+               }
+            }
          }
       }
    }
 
+   if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
+      setup->draw_region = setup->framebuffer;
+      if (setup->scissor_test) {
+         u_rect_possible_intersection(&setup->scissor,
+                                      &setup->draw_region);
+      }
+   }
+                                      
    setup->dirty = 0;
 
    assert(setup->fs.stored);
+   return TRUE;
+}
+
+void
+lp_setup_update_state( struct lp_setup_context *setup,
+                       boolean update_scene )
+{
+   /* Some of the 'draw' pipeline stages may have changed some driver state.
+    * Make sure we've processed those state changes before anything else.
+    *
+    * XXX this is the only place where llvmpipe_context is used in the
+    * setup code.  This may get refactored/changed...
+    */
+   {
+      struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
+      if (lp->dirty) {
+         llvmpipe_update_derived(lp);
+      }
+
+      /* Will probably need to move this somewhere else, just need  
+       * to know about vertex shader point size attribute.
+       */
+      setup->psize = lp->psize_slot;
+
+      assert(lp->dirty == 0);
+   }
+
+   if (update_scene)
+      set_scene_state( setup, SETUP_ACTIVE, __FUNCTION__ );
+
+   /* Only call into update_scene_state() if we already have a
+    * scene:
+    */
+   if (update_scene && setup->scene) {
+      assert(setup->state == SETUP_ACTIVE);
+      if (!try_update_scene_state(setup)) {
+         lp_setup_flush_and_restart(setup);
+         if (!try_update_scene_state(setup))
+            assert(0);
+      }
+   }
 }
 
 
@@ -760,7 +927,7 @@ lp_setup_destroy( struct lp_setup_context *setup )
 {
    uint i;
 
-   reset_context( setup );
+   lp_setup_reset( setup );
 
    util_unreference_framebuffer_state(&setup->fb);
 
@@ -771,16 +938,14 @@ lp_setup_destroy( struct lp_setup_context *setup )
    pipe_resource_reference(&setup->constants.current, NULL);
 
    /* free the scenes in the 'empty' queue */
-   while (1) {
-      struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes, FALSE);
-      if (!scene)
-         break;
-      lp_scene_destroy(scene);
-   }
+   for (i = 0; i < Elements(setup->scenes); i++) {
+      struct lp_scene *scene = setup->scenes[i];
 
-   lp_scene_queue_destroy(setup->empty_scenes);
+      if (scene->fence)
+         lp_fence_wait(scene->fence);
 
-   lp_rast_destroy( setup->rast );
+      lp_scene_destroy(scene);
+   }
 
    FREE( setup );
 }
@@ -803,18 +968,13 @@ lp_setup_create( struct pipe_context *pipe,
       return NULL;
 
    lp_setup_init_vbuf(setup);
+   
+   /* Used only in update_state():
+    */
+   setup->pipe = pipe;
 
-   setup->empty_scenes = lp_scene_queue_create();
-   if (!setup->empty_scenes)
-      goto fail;
 
-   /* XXX: move this to the screen and share between contexts:
-    */
    setup->num_threads = screen->num_threads;
-   setup->rast = lp_rast_create(screen->num_threads);
-   if (!setup->rast) 
-      goto fail;
-
    setup->vbuf = draw_vbuf_stage(draw, &setup->base);
    if (!setup->vbuf)
       goto fail;
@@ -824,9 +984,7 @@ lp_setup_create( struct pipe_context *pipe,
 
    /* create some empty scenes */
    for (i = 0; i < MAX_SCENES; i++) {
-      setup->scenes[i] = lp_scene_create( pipe, setup->empty_scenes );
-
-      lp_scene_enqueue(setup->empty_scenes, setup->scenes[i]);
+      setup->scenes[i] = lp_scene_create( pipe );
    }
 
    setup->triangle = first_triangle;
@@ -838,16 +996,86 @@ lp_setup_create( struct pipe_context *pipe,
    return setup;
 
 fail:
-   if (setup->rast)
-      lp_rast_destroy( setup->rast );
-   
    if (setup->vbuf)
       ;
 
-   if (setup->empty_scenes)
-      lp_scene_queue_destroy(setup->empty_scenes);
-
    FREE(setup);
    return NULL;
 }
 
+
+/**
+ * Put a BeginQuery command into all bins.
+ */
+void
+lp_setup_begin_query(struct lp_setup_context *setup,
+                     struct llvmpipe_query *pq)
+{
+   /* init the query to its beginning state */
+   assert(setup->active_query == NULL);
+   
+   if (setup->scene) {
+      if (!lp_scene_bin_everywhere(setup->scene,
+                                   LP_RAST_OP_BEGIN_QUERY,
+                                   lp_rast_arg_query(pq))) {
+
+         lp_setup_flush_and_restart(setup);
+
+         if (!lp_scene_bin_everywhere(setup->scene,
+                                      LP_RAST_OP_BEGIN_QUERY,
+                                      lp_rast_arg_query(pq))) {
+            assert(0);
+            return;
+         }
+      }
+   }
+
+   setup->active_query = pq;
+}
+
+
+/**
+ * Put an EndQuery command into all bins.
+ */
+void
+lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
+{
+   union lp_rast_cmd_arg dummy = { 0 };
+
+   assert(setup->active_query == pq);
+   setup->active_query = NULL;
+
+   /* Setup will automatically re-issue any query which carried over a
+    * scene boundary, and the rasterizer automatically "ends" queries
+    * which are active at the end of a scene, so there is no need to
+    * retry this commands on failure.
+    */
+   if (setup->scene) {
+      /* pq->fence should be the fence of the *last* scene which
+       * contributed to the query result.
+       */
+      lp_fence_reference(&pq->fence, setup->scene->fence);
+
+      if (!lp_scene_bin_everywhere(setup->scene,
+                                   LP_RAST_OP_END_QUERY,
+                                   dummy)) {
+         lp_setup_flush(setup, 0, NULL, __FUNCTION__);
+      }
+   }
+   else {
+      lp_fence_reference(&pq->fence, setup->last_fence);
+   }
+}
+
+
+void
+lp_setup_flush_and_restart(struct lp_setup_context *setup)
+{
+   if (0) debug_printf("%s\n", __FUNCTION__);
+
+   assert(setup->state == SETUP_ACTIVE);
+   set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__);
+   lp_setup_update_state(setup, TRUE);
+}
+
+