llvmpipe: improve empty-bin test further
[mesa.git] / src / gallium / drivers / llvmpipe / lp_rast.c
index d5fe6e936993b123cee6285161b2b6ec7284f8c1..0a8d730580afebac0d19518d2a48ca0ae8875ea2 100644 (file)
  *
  **************************************************************************/
 
+#include <limits.h>
 #include "util/u_memory.h"
+#include "util/u_math.h"
+#include "util/u_cpu_detect.h"
+#include "util/u_surface.h"
 
-#include "lp_state.h"
+#include "lp_scene_queue.h"
+#include "lp_debug.h"
+#include "lp_fence.h"
 #include "lp_rast.h"
 #include "lp_rast_priv.h"
 #include "lp_tile_soa.h"
 #include "lp_bld_debug.h"
-
-#define RAST_DEBUG debug_printf
-
-struct lp_rasterizer *lp_rast_create( struct pipe_screen *screen )
-{
-   struct lp_rasterizer *rast;
-
-   rast = CALLOC_STRUCT(lp_rasterizer);
-   if(!rast)
-      return NULL;
-
-   rast->screen = screen;
-   rast->tile.color = align_malloc( TILE_SIZE*TILE_SIZE*4, 16 );
-   rast->tile.depth = align_malloc( TILE_SIZE*TILE_SIZE*4, 16 );
-
-   return rast;
-}
+#include "lp_scene.h"
 
 
 /**
  * Begin the rasterization phase.
  * Map the framebuffer surfaces.  Initialize the 'rast' state.
  */
-boolean lp_rast_begin( struct lp_rasterizer *rast,
-                       struct pipe_surface *cbuf,
-                       struct pipe_surface *zsbuf,
-                       boolean write_color,
-                       boolean write_zstencil,
-                       unsigned width,
-                       unsigned height )
+static boolean
+lp_rast_begin( struct lp_rasterizer *rast,
+               const struct pipe_framebuffer_state *fb,
+               boolean write_color,
+               boolean write_zstencil )
 {
    struct pipe_screen *screen = rast->screen;
+   struct pipe_surface *cbuf, *zsbuf;
+   int i;
 
-   RAST_DEBUG("%s %dx%d\n", __FUNCTION__, width, height);
+   LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
 
-   pipe_surface_reference(&rast->state.cbuf, cbuf);
-   pipe_surface_reference(&rast->state.zsbuf, zsbuf);
+   util_copy_framebuffer_state(&rast->state.fb, fb);
 
-   rast->width = width;
-   rast->height = height;
    rast->state.write_zstencil = write_zstencil;
    rast->state.write_color = write_color;
 
-   rast->check_for_clipped_tiles = (width % TILE_SIZE != 0 ||
-                                    height % TILE_SIZE != 0);
-
-   if (cbuf) {
-      rast->cbuf_transfer = screen->get_tex_transfer(rast->screen,
-                                                     cbuf->texture,
-                                                     cbuf->face,
-                                                     cbuf->level,
-                                                     cbuf->zslice,
-                                                     PIPE_TRANSFER_READ_WRITE,
-                                                     0, 0, width, height);
-      if (!rast->cbuf_transfer)
-         return FALSE;
+   rast->check_for_clipped_tiles = (fb->width % TILE_SIZE != 0 ||
+                                    fb->height % TILE_SIZE != 0);
 
-      rast->cbuf_map = screen->transfer_map(rast->screen, 
-                                            rast->cbuf_transfer);
-      if (!rast->cbuf_map)
-         return FALSE;
+   
+   for (i = 0; i < rast->state.fb.nr_cbufs; i++) {
+      cbuf = rast->state.fb.cbufs[i];
+      if (cbuf) {
+        rast->cbuf_transfer[i] = screen->get_tex_transfer(rast->screen,
+                                                          cbuf->texture,
+                                                          cbuf->face,
+                                                          cbuf->level,
+                                                          cbuf->zslice,
+                                                          PIPE_TRANSFER_READ_WRITE,
+                                                          0, 0,
+                                                          cbuf->width, 
+                                                          cbuf->height);
+        if (!rast->cbuf_transfer[i])
+           goto fail;
+
+        rast->cbuf_map[i] = screen->transfer_map(rast->screen, 
+                                                 rast->cbuf_transfer[i]);
+        if (!rast->cbuf_map[i])
+           goto fail;
+      }
    }
 
+   zsbuf = rast->state.fb.zsbuf;
    if (zsbuf) {
       rast->zsbuf_transfer = screen->get_tex_transfer(rast->screen,
-                                                     zsbuf->texture,
-                                                     zsbuf->face,
-                                                     zsbuf->level,
-                                                     zsbuf->zslice,
-                                                     PIPE_TRANSFER_READ_WRITE,
-                                                     0, 0, width, height);
+                                                      zsbuf->texture,
+                                                      zsbuf->face,
+                                                      zsbuf->level,
+                                                      zsbuf->zslice,
+                                                      PIPE_TRANSFER_READ_WRITE,
+                                                      0, 0,
+                                                      zsbuf->width,
+                                                     zsbuf->height);
       if (!rast->zsbuf_transfer)
-         return FALSE;
+         goto fail;
 
       rast->zsbuf_map = screen->transfer_map(rast->screen, 
                                             rast->zsbuf_transfer);
       if (!rast->zsbuf_map)
-         return FALSE;
+        goto fail;
    }
 
    return TRUE;
+
+fail:
+   /* Unmap and release transfers?
+    */
+   return FALSE;
 }
 
 
@@ -120,25 +121,30 @@ boolean lp_rast_begin( struct lp_rasterizer *rast,
  * Finish the rasterization phase.
  * Unmap framebuffer surfaces.
  */
-void lp_rast_end( struct lp_rasterizer *rast )
+static void
+lp_rast_end( struct lp_rasterizer *rast )
 {
    struct pipe_screen *screen = rast->screen;
+   unsigned i;
+
+   for (i = 0; i < rast->state.fb.nr_cbufs; i++) {
+      if (rast->cbuf_map[i]) 
+        screen->transfer_unmap(screen, rast->cbuf_transfer[i]);
 
-   if (rast->cbuf_map) 
-      screen->transfer_unmap(screen, rast->cbuf_transfer);
+      if (rast->cbuf_transfer[i])
+        screen->tex_transfer_destroy(rast->cbuf_transfer[i]);
+
+      rast->cbuf_transfer[i] = NULL;
+      rast->cbuf_map[i] = NULL;
+   }
 
    if (rast->zsbuf_map) 
       screen->transfer_unmap(screen, rast->zsbuf_transfer);
 
-   if (rast->cbuf_transfer)
-      screen->tex_transfer_destroy(rast->cbuf_transfer);
-
    if (rast->zsbuf_transfer)
       screen->tex_transfer_destroy(rast->zsbuf_transfer);
 
-   rast->cbuf_transfer = NULL;
    rast->zsbuf_transfer = NULL;
-   rast->cbuf_map = NULL;
    rast->zsbuf_map = NULL;
 }
 
@@ -148,14 +154,15 @@ void lp_rast_end( struct lp_rasterizer *rast )
  * \param x  window X position of the tile, in pixels
  * \param y  window Y position of the tile, in pixels
  */
-void lp_rast_start_tile( struct lp_rasterizer *rast,
-                        unsigned x,
-                        unsigned y )
+static void
+lp_rast_start_tile( struct lp_rasterizer *rast,
+                    unsigned thread_index,
+                    unsigned x, unsigned y )
 {
-   RAST_DEBUG("%s %d,%d\n", __FUNCTION__, x, y);
+   LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, x, y);
 
-   rast->x = x;
-   rast->y = y;
+   rast->tasks[thread_index].x = x;
+   rast->tasks[thread_index].y = y;
 }
 
 
@@ -164,11 +171,14 @@ void lp_rast_start_tile( struct lp_rasterizer *rast,
  * This is a bin command called during bin processing.
  */
 void lp_rast_clear_color( struct lp_rasterizer *rast,
+                          unsigned thread_index,
                           const union lp_rast_cmd_arg arg )
 {
    const uint8_t *clear_color = arg.clear_color;
-   
-   RAST_DEBUG("%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__, 
+   uint8_t **color_tile = rast->tasks[thread_index].tile.color;
+   unsigned i;
+
+   LP_DBG(DEBUG_RAST, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__, 
               clear_color[0],
               clear_color[1],
               clear_color[2],
@@ -177,14 +187,17 @@ void lp_rast_clear_color( struct lp_rasterizer *rast,
    if (clear_color[0] == clear_color[1] &&
        clear_color[1] == clear_color[2] &&
        clear_color[2] == clear_color[3]) {
-      memset(rast->tile.color, clear_color[0], TILE_SIZE * TILE_SIZE * 4);
+      for (i = 0; i < rast->state.fb.nr_cbufs; i++) {
+        memset(color_tile[i], clear_color[0], TILE_SIZE * TILE_SIZE * 4);
+      }
    }
    else {
       unsigned x, y, chan;
-      for (y = 0; y < TILE_SIZE; y++)
-         for (x = 0; x < TILE_SIZE; x++)
-            for (chan = 0; chan < 4; ++chan)
-               TILE_PIXEL(rast->tile.color, x, y, chan) = clear_color[chan];
+      for (i = 0; i < rast->state.fb.nr_cbufs; i++)
+        for (y = 0; y < TILE_SIZE; y++)
+           for (x = 0; x < TILE_SIZE; x++)
+              for (chan = 0; chan < 4; ++chan)
+                 TILE_PIXEL(color_tile[i], x, y, chan) = clear_color[chan];
    }
 }
 
@@ -194,15 +207,17 @@ void lp_rast_clear_color( struct lp_rasterizer *rast,
  * This is a bin command called during bin processing.
  */
 void lp_rast_clear_zstencil( struct lp_rasterizer *rast,
+                             unsigned thread_index,
                              const union lp_rast_cmd_arg arg)
 {
    unsigned i, j;
+   uint32_t *depth_tile = rast->tasks[thread_index].tile.depth;
    
-   RAST_DEBUG("%s 0x%x\n", __FUNCTION__, arg.clear_zstencil);
+   LP_DBG(DEBUG_RAST, "%s 0x%x\n", __FUNCTION__, arg.clear_zstencil);
 
    for (i = 0; i < TILE_SIZE; i++)
       for (j = 0; j < TILE_SIZE; j++)
-        rast->tile.depth[i*TILE_SIZE + j] = arg.clear_zstencil;
+        depth_tile[i*TILE_SIZE + j] = arg.clear_zstencil;
 }
 
 
@@ -211,11 +226,46 @@ void lp_rast_clear_zstencil( struct lp_rasterizer *rast,
  * This is a bin command called during bin processing.
  */
 void lp_rast_load_color( struct lp_rasterizer *rast,
+                         unsigned thread_index,
                          const union lp_rast_cmd_arg arg)
 {
-   RAST_DEBUG("%s\n", __FUNCTION__);
-
-   /* call u_tile func to load colors from surface */
+   struct lp_rasterizer_task *task = &rast->tasks[thread_index];
+   const unsigned x = task->x;
+   const unsigned y = task->y;
+   unsigned i;
+
+   LP_DBG(DEBUG_RAST, "%s at %u, %u\n", __FUNCTION__, x, y);
+
+   for (i = 0; i < rast->state.fb.nr_cbufs; i++) {
+      struct pipe_transfer *transfer = rast->cbuf_transfer[i];
+      int w = TILE_SIZE;
+      int h = TILE_SIZE;
+
+      if (x >= transfer->width)
+        continue;
+
+      if (y >= transfer->height)
+        continue;
+      /* XXX: require tile-size aligned render target dimensions:
+       */
+      if (x + w > transfer->width)
+        w -= x + w - transfer->width;
+
+      if (y + h > transfer->height)
+        h -= y + h - transfer->height;
+
+      assert(w >= 0);
+      assert(h >= 0);
+      assert(w <= TILE_SIZE);
+      assert(h <= TILE_SIZE);
+
+      lp_tile_read_4ub(transfer->texture->format,
+                      rast->tasks[thread_index].tile.color[i],
+                      rast->cbuf_map[i], 
+                      transfer->stride,
+                      x, y,
+                      w, h);
+   }
 }
 
 
@@ -224,22 +274,25 @@ void lp_rast_load_color( struct lp_rasterizer *rast,
  * This is a bin command called during bin processing.
  */
 void lp_rast_load_zstencil( struct lp_rasterizer *rast,
+                            unsigned thread_index,
                             const union lp_rast_cmd_arg arg )
 {
-   RAST_DEBUG("%s\n", __FUNCTION__);
+   LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
 
    /* call u_tile func to load depth (and stencil?) from surface */
 }
 
 
 void lp_rast_set_state( struct lp_rasterizer *rast,
+                        unsigned thread_index,
                         const union lp_rast_cmd_arg arg )
 {
    const struct lp_rast_state *state = arg.set_state;
 
-   RAST_DEBUG("%s\n", __FUNCTION__);
+   LP_DBG(DEBUG_RAST, "%s %p\n", __FUNCTION__, (void *) state);
 
-   /* XXX to do */
+   /* just set the current state pointer for this rasterizer */
+   rast->tasks[thread_index].current_state = state;
 }
 
 
@@ -253,19 +306,28 @@ void lp_rast_set_state( struct lp_rasterizer *rast,
  * This is a bin command called during bin processing.
  */
 void lp_rast_shade_tile( struct lp_rasterizer *rast,
+                         unsigned thread_index,
                          const union lp_rast_cmd_arg arg )
 {
+   /* Set c1,c2,c3 to large values so the in/out test always passes */
+   const int32_t c1 = INT_MIN, c2 = INT_MIN, c3 = INT_MIN;
    const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
-   const unsigned mask = ~0;
+   const unsigned tile_x = rast->tasks[thread_index].x;
+   const unsigned tile_y = rast->tasks[thread_index].y;
    unsigned x, y;
 
-   RAST_DEBUG("%s\n", __FUNCTION__);
+   LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
 
    /* Use the existing preference for 4x4 (four quads) shading:
     */
    for (y = 0; y < TILE_SIZE; y += 4)
       for (x = 0; x < TILE_SIZE; x += 4)
-         lp_rast_shade_quads( rast, inputs, rast->x + x, rast->y + y, mask);
+         lp_rast_shade_quads( rast,
+                              thread_index,
+                              inputs,
+                              tile_x + x,
+                              tile_y + y,
+                              c1, c2, c3);
 }
 
 
@@ -274,59 +336,30 @@ void lp_rast_shade_tile( struct lp_rasterizer *rast,
  * This is a bin command called during bin processing.
  */
 void lp_rast_shade_quads( struct lp_rasterizer *rast,
+                          unsigned thread_index,
                           const struct lp_rast_shader_inputs *inputs,
                           unsigned x, unsigned y,
-                          unsigned mask)
+                          int32_t c1, int32_t c2, int32_t c3)
 {
-#if 1
-   const struct lp_rast_state *state = inputs->state;
-   struct lp_rast_tile *tile = &rast->tile;
-   void *color;
+   const struct lp_rast_state *state = rast->tasks[thread_index].current_state;
+   struct lp_rast_tile *tile = &rast->tasks[thread_index].tile;
+   uint8_t *color[PIPE_MAX_COLOR_BUFS];
    void *depth;
-   uint32_t ALIGN16_ATTRIB masks[2][2][2][2];
+   unsigned i;
    unsigned ix, iy;
    int block_offset;
 
+#ifdef DEBUG
+   assert(state);
+
    /* Sanity checks */
    assert(x % TILE_VECTOR_WIDTH == 0);
    assert(y % TILE_VECTOR_HEIGHT == 0);
 
-   /* mask: the rasterizer wants to treat pixels in 4x4 blocks, but
-    * the pixel shader wants to swizzle them into 4 2x2 quads.
-    * 
-    * Additionally, the pixel shader wants masks as full dword ~0,
-    * while the rasterizer wants to pack per-pixel bits tightly.
-    */
-#if 0
-   unsigned qx, qy;
-   for (qy = 0; qy < 2; ++qy)
-      for (qx = 0; qx < 2; ++qx)
-        for (iy = 0; iy < 2; ++iy)
-           for (ix = 0; ix < 2; ++ix)
-              masks[qy][qx][iy][ix] = mask & (1 << (qy*8+iy*4+qx*2+ix)) ? ~0 : 0;
-#else
-   masks[0][0][0][0] = mask & (1 << (0*8+0*4+0*2+0)) ? ~0 : 0;
-   masks[0][0][0][1] = mask & (1 << (0*8+0*4+0*2+1)) ? ~0 : 0;
-   masks[0][0][1][0] = mask & (1 << (0*8+1*4+0*2+0)) ? ~0 : 0;
-   masks[0][0][1][1] = mask & (1 << (0*8+1*4+0*2+1)) ? ~0 : 0;
-   masks[0][1][0][0] = mask & (1 << (0*8+0*4+1*2+0)) ? ~0 : 0;
-   masks[0][1][0][1] = mask & (1 << (0*8+0*4+1*2+1)) ? ~0 : 0;
-   masks[0][1][1][0] = mask & (1 << (0*8+1*4+1*2+0)) ? ~0 : 0;
-   masks[0][1][1][1] = mask & (1 << (0*8+1*4+1*2+1)) ? ~0 : 0;
-
-   masks[1][0][0][0] = mask & (1 << (1*8+0*4+0*2+0)) ? ~0 : 0;
-   masks[1][0][0][1] = mask & (1 << (1*8+0*4+0*2+1)) ? ~0 : 0;
-   masks[1][0][1][0] = mask & (1 << (1*8+1*4+0*2+0)) ? ~0 : 0;
-   masks[1][0][1][1] = mask & (1 << (1*8+1*4+0*2+1)) ? ~0 : 0;
-   masks[1][1][0][0] = mask & (1 << (1*8+0*4+1*2+0)) ? ~0 : 0;
-   masks[1][1][0][1] = mask & (1 << (1*8+0*4+1*2+1)) ? ~0 : 0;
-   masks[1][1][1][0] = mask & (1 << (1*8+1*4+1*2+0)) ? ~0 : 0;
-   masks[1][1][1][1] = mask & (1 << (1*8+1*4+1*2+1)) ? ~0 : 0;
+   assert((x % 4) == 0);
+   assert((y % 4) == 0);
 #endif
 
-   assert((x % 2) == 0);
-   assert((y % 2) == 0);
-
    ix = x % TILE_SIZE;
    iy = y % TILE_SIZE;
 
@@ -334,44 +367,34 @@ void lp_rast_shade_quads( struct lp_rasterizer *rast,
    block_offset = ((iy/4)*(16*16) + (ix/4)*16);
 
    /* color buffer */
-   color = tile->color + 4 * block_offset;
+   for (i = 0; i < rast->state.fb.nr_cbufs; i++)
+      color[i] = tile->color[i] + 4 * block_offset;
 
    /* depth buffer */
    depth = tile->depth + block_offset;
 
-   /* XXX: This will most likely fail on 32bit x86 without -mstackrealign */
-   assert(lp_check_alignment(masks, 16));
 
-   assert(lp_check_alignment(depth, 16));
-   assert(lp_check_alignment(color, 16));
+
+#ifdef DEBUG
+   assert(lp_check_alignment(tile->depth, 16));
+   assert(lp_check_alignment(tile->color[0], 16));
    assert(lp_check_alignment(state->jit_context.blend_color, 16));
 
+   assert(lp_check_alignment(inputs->step[0], 16));
+   assert(lp_check_alignment(inputs->step[1], 16));
+   assert(lp_check_alignment(inputs->step[2], 16));
+#endif
+
    /* run shader */
    state->jit_function( &state->jit_context,
                         x, y,
                         inputs->a0,
                         inputs->dadx,
                         inputs->dady,
-                        &masks[0][0][0][0],
                         color,
-                        depth);
-#else
-   struct lp_rast_tile *tile = &rast->tile;
-   unsigned chan_index;
-   unsigned q, ix, iy;
-
-   x %= TILE_SIZE;
-   y %= TILE_SIZE;
-
-   /* mask */
-   for (q = 0; q < 4; ++q)
-      for(iy = 0; iy < 2; ++iy)
-         for(ix = 0; ix < 2; ++ix)
-            if(masks[q] & (1 << (iy*2 + ix)))
-               for (chan_index = 0; chan_index < NUM_CHANNELS; ++chan_index)
-                  TILE_PIXEL(tile->color, x + q*2 + ix, y + iy, chan_index) = 0xff;
-
-#endif
+                        depth,
+                        c1, c2, c3,
+                        inputs->step[0], inputs->step[1], inputs->step[2]);
 }
 
 
@@ -382,27 +405,47 @@ void lp_rast_shade_quads( struct lp_rasterizer *rast,
 /**
  * Write the rasterizer's color tile to the framebuffer.
  */
-static void lp_rast_store_color( struct lp_rasterizer *rast )
+static void lp_rast_store_color( struct lp_rasterizer *rast,
+                                 unsigned thread_index)
 {
-   const unsigned x = rast->x;
-   const unsigned y = rast->y;
-   unsigned w = TILE_SIZE;
-   unsigned h = TILE_SIZE;
-
-   if (x + w > rast->width)
-      w -= x + w - rast->width;
-
-   if (y + h > rast->height)
-      h -= y + h - rast->height;
-
-   RAST_DEBUG("%s %d,%d %dx%d\n", __FUNCTION__, x, y, w, h);
-
-   lp_tile_write_4ub(rast->cbuf_transfer->format,
-                     rast->tile.color,
-                     rast->cbuf_map, 
-                     rast->cbuf_transfer->stride,
-                     x, y,
-                     w, h);
+   const unsigned x = rast->tasks[thread_index].x;
+   const unsigned y = rast->tasks[thread_index].y;
+   unsigned i;
+
+   for (i = 0; i < rast->state.fb.nr_cbufs; i++) {
+      struct pipe_transfer *transfer = rast->cbuf_transfer[i];
+      int w = TILE_SIZE;
+      int h = TILE_SIZE;
+
+      if (x >= transfer->width)
+        continue;
+
+      if (y >= transfer->height)
+        continue;
+
+      /* XXX: require tile-size aligned render target dimensions:
+       */
+      if (x + w > transfer->width)
+        w -= x + w - transfer->width;
+
+      if (y + h > transfer->height)
+        h -= y + h - transfer->height;
+
+      assert(w >= 0);
+      assert(h >= 0);
+      assert(w <= TILE_SIZE);
+      assert(h <= TILE_SIZE);
+
+      LP_DBG(DEBUG_RAST, "%s [%u] %d,%d %dx%d\n", __FUNCTION__,
+            thread_index, x, y, w, h);
+
+      lp_tile_write_4ub(transfer->texture->format,
+                       rast->tasks[thread_index].tile.color[i],
+                       rast->cbuf_map[i], 
+                       transfer->stride,
+                       x, y,
+                       w, h);
+   }
 }
 
 
@@ -424,23 +467,24 @@ lp_tile_write_z32(const uint32_t *src, uint8_t *dst, unsigned dst_stride,
 /**
  * Write the rasterizer's z/stencil tile to the framebuffer.
  */
-static void lp_rast_store_zstencil( struct lp_rasterizer *rast )
+static void lp_rast_store_zstencil( struct lp_rasterizer *rast,
+                                    unsigned thread_index )
 {
-   const unsigned x = rast->x;
-   const unsigned y = rast->y;
+   const unsigned x = rast->tasks[thread_index].x;
+   const unsigned y = rast->tasks[thread_index].y;
    unsigned w = TILE_SIZE;
    unsigned h = TILE_SIZE;
 
-   if (x + w > rast->width)
-      w -= x + w - rast->width;
+   if (x + w > rast->state.fb.width)
+      w -= x + w - rast->state.fb.width;
 
-   if (y + h > rast->height)
-      h -= y + h - rast->height;
+   if (y + h > rast->state.fb.height)
+      h -= y + h - rast->state.fb.height;
 
-   RAST_DEBUG("%s %d,%d %dx%d\n", __FUNCTION__, x, y, w, h);
+   LP_DBG(DEBUG_RAST, "%s %d,%d %dx%d\n", __FUNCTION__, x, y, w, h);
 
-   assert(rast->zsbuf_transfer->format == PIPE_FORMAT_Z32_UNORM);
-   lp_tile_write_z32(rast->tile.depth,
+   assert(rast->zsbuf_transfer->texture->format == PIPE_FORMAT_Z32_UNORM);
+   lp_tile_write_z32(rast->tasks[thread_index].tile.depth,
                      rast->zsbuf_map, 
                      rast->zsbuf_transfer->stride,
                      x, y, w, h);
@@ -450,15 +494,353 @@ static void lp_rast_store_zstencil( struct lp_rasterizer *rast )
 /**
  * Write the rasterizer's tiles to the framebuffer.
  */
-void lp_rast_end_tile( struct lp_rasterizer *rast )
+static void
+lp_rast_end_tile( struct lp_rasterizer *rast,
+                  unsigned thread_index )
 {
-   RAST_DEBUG("%s\n", __FUNCTION__);
+   LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
 
    if (rast->state.write_color)
-      lp_rast_store_color(rast);
+      lp_rast_store_color(rast, thread_index);
 
    if (rast->state.write_zstencil)
-      lp_rast_store_zstencil(rast);
+      lp_rast_store_zstencil(rast, thread_index);
+}
+
+
+/**
+ * Signal on a fence.  This is called during bin execution/rasterization.
+ * Called per thread.
+ */
+void lp_rast_fence( struct lp_rasterizer *rast,
+                    unsigned thread_index,
+                    const union lp_rast_cmd_arg arg )
+{
+   struct lp_fence *fence = arg.fence;
+
+   pipe_mutex_lock( fence->mutex );
+
+   fence->count++;
+   assert(fence->count <= fence->rank);
+
+   LP_DBG(DEBUG_RAST, "%s count=%u rank=%u\n", __FUNCTION__,
+          fence->count, fence->rank);
+
+   pipe_condvar_signal( fence->signalled );
+
+   pipe_mutex_unlock( fence->mutex );
+}
+
+
+/**
+ * When all the threads are done rasterizing a scene, one thread will
+ * call this function to reset the scene and put it onto the empty queue.
+ */
+static void
+release_scene( struct lp_rasterizer *rast,
+              struct lp_scene *scene )
+{
+   util_unreference_framebuffer_state( &scene->fb );
+
+   lp_scene_reset( scene );
+   lp_scene_enqueue( rast->empty_scenes, scene );
+   rast->curr_scene = NULL;
+}
+
+
+/**
+ * Rasterize commands for a single bin.
+ * \param x, y  position of the bin's tile in the framebuffer
+ * Must be called between lp_rast_begin() and lp_rast_end().
+ * Called per thread.
+ */
+static void
+rasterize_bin( struct lp_rasterizer *rast,
+               unsigned thread_index,
+               const struct cmd_bin *bin,
+               int x, int y)
+{
+   const struct cmd_block_list *commands = &bin->commands;
+   struct cmd_block *block;
+   unsigned k;
+
+   lp_rast_start_tile( rast, thread_index, x, y );
+
+   /* simply execute each of the commands in the block list */
+   for (block = commands->head; block; block = block->next) {
+      for (k = 0; k < block->count; k++) {
+         block->cmd[k]( rast, thread_index, block->arg[k] );
+      }
+   }
+
+   lp_rast_end_tile( rast, thread_index );
+}
+
+/* An empty bin is one that just loads the contents of the tile and
+ * stores them again unchanged.  This typically happens when bins have
+ * been flushed for some reason in the middle of a frame, or when
+ * incremental updates are being made to a render target.
+ * 
+ * Try to avoid doing pointless work in this case.
+ */
+static boolean
+is_empty_bin( const struct cmd_bin *bin )
+{
+   const struct cmd_block *head = bin->commands.head;
+   int i;
+
+   /* We emit at most two load-tile commands at the start of the first
+    * command block.  If there are more than two commands in the
+    * block, we know that the bin is non-empty.
+    */
+   if (head->next != NULL ||
+       head->count > 2)
+      return FALSE;
+
+   for (i = 0; i < head->count; i++)
+      if (head->cmd[i] != lp_rast_load_color &&
+          head->cmd[i] != lp_rast_load_zstencil)
+         return FALSE;
+
+   return TRUE;
+}
+
+
+
+/**
+ * Rasterize/execute all bins within a scene.
+ * Called per thread.
+ */
+static void
+rasterize_scene( struct lp_rasterizer *rast,
+                unsigned thread_index,
+                struct lp_scene *scene,
+                bool write_depth )
+{
+   /* loop over scene bins, rasterize each */
+#if 0
+   {
+      unsigned i, j;
+      for (i = 0; i < scene->tiles_x; i++) {
+         for (j = 0; j < scene->tiles_y; j++) {
+            struct cmd_bin *bin = lp_get_bin(scene, i, j);
+            rasterize_bin( rast, thread_index,
+                           bin, i * TILE_SIZE, j * TILE_SIZE );
+         }
+      }
+   }
+#else
+   {
+      struct cmd_bin *bin;
+      int x, y;
+
+      assert(scene);
+      while ((bin = lp_scene_bin_iter_next(scene, &x, &y))) {
+         if (!is_empty_bin( bin ))
+            rasterize_bin( rast, thread_index, bin, x * TILE_SIZE, y * TILE_SIZE);
+      }
+   }
+#endif
+}
+
+
+/**
+ * Called by setup module when it has something for us to render.
+ */
+void
+lp_rasterize_scene( struct lp_rasterizer *rast,
+                   struct lp_scene *scene,
+                   const struct pipe_framebuffer_state *fb,
+                   bool write_depth )
+{
+   boolean debug = false;
+
+   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
+
+   if (debug) {
+      unsigned x, y;
+      printf("rasterize scene:\n");
+      printf("  data size: %u\n", lp_scene_data_size(scene));
+      for (y = 0; y < scene->tiles_y; y++) {
+         for (x = 0; x < scene->tiles_x; x++) {
+            printf("  bin %u, %u size: %u\n", x, y,
+                   lp_scene_bin_size(scene, x, y));
+         }
+      }
+   }
+
+   /* save framebuffer state in the bin */
+   util_copy_framebuffer_state(&scene->fb, fb);
+   scene->write_depth = write_depth;
+
+   if (rast->num_threads == 0) {
+      /* no threading */
+
+      lp_rast_begin( rast, fb,
+                     fb->nr_cbufs != 0, /* always write color if cbufs present */
+                     fb->zsbuf != NULL && write_depth );
+
+      lp_scene_bin_iter_begin( scene );
+      rasterize_scene( rast, 0, scene, write_depth );
+
+      release_scene( rast, scene );
+
+      lp_rast_end( rast );
+   }
+   else {
+      /* threaded rendering! */
+      unsigned i;
+
+      lp_scene_enqueue( rast->full_scenes, scene );
+
+      /* signal the threads that there's work to do */
+      for (i = 0; i < rast->num_threads; i++) {
+         pipe_semaphore_signal(&rast->tasks[i].work_ready);
+      }
+
+      /* wait for work to complete */
+      for (i = 0; i < rast->num_threads; i++) {
+         pipe_semaphore_wait(&rast->tasks[i].work_done);
+      }
+   }
+
+   LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
+}
+
+
+/**
+ * This is the thread's main entrypoint.
+ * It's a simple loop:
+ *   1. wait for work
+ *   2. do work
+ *   3. signal that we're done
+ */
+static void *
+thread_func( void *init_data )
+{
+   struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
+   struct lp_rasterizer *rast = task->rast;
+   boolean debug = false;
+
+   while (1) {
+      /* wait for work */
+      if (debug)
+         debug_printf("thread %d waiting for work\n", task->thread_index);
+      pipe_semaphore_wait(&task->work_ready);
+
+      if (task->thread_index == 0) {
+         /* thread[0]:
+          *  - get next scene to rasterize
+          *  - map the framebuffer surfaces
+          */
+         const struct pipe_framebuffer_state *fb;
+         boolean write_depth;
+
+         rast->curr_scene = lp_scene_dequeue( rast->full_scenes );
+
+         lp_scene_bin_iter_begin( rast->curr_scene );
+
+         fb = &rast->curr_scene->fb;
+         write_depth = rast->curr_scene->write_depth;
+
+         lp_rast_begin( rast, fb,
+                        fb->nr_cbufs != 0,
+                        fb->zsbuf != NULL && write_depth );
+      }
+
+      /* Wait for all threads to get here so that threads[1+] don't
+       * get a null rast->curr_scene pointer.
+       */
+      pipe_barrier_wait( &rast->barrier );
+
+      /* do work */
+      if (debug)
+         debug_printf("thread %d doing work\n", task->thread_index);
+      rasterize_scene(rast, 
+                    task->thread_index,
+                     rast->curr_scene, 
+                    rast->curr_scene->write_depth);
+      
+      /* wait for all threads to finish with this scene */
+      pipe_barrier_wait( &rast->barrier );
+
+      if (task->thread_index == 0) {
+         /* thread[0]:
+          * - release the scene object
+          * - unmap the framebuffer surfaces
+          */
+         release_scene( rast, rast->curr_scene );
+         lp_rast_end( rast );
+      }
+
+      /* signal done with work */
+      if (debug)
+         debug_printf("thread %d done working\n", task->thread_index);
+      pipe_semaphore_signal(&task->work_done);
+   }
+
+   return NULL;
+}
+
+
+/**
+ * Initialize semaphores and spawn the threads.
+ */
+static void
+create_rast_threads(struct lp_rasterizer *rast)
+{
+   unsigned i;
+
+   rast->num_threads = util_cpu_caps.nr_cpus;
+   rast->num_threads = debug_get_num_option("LP_NUM_THREADS", rast->num_threads);
+   rast->num_threads = MIN2(rast->num_threads, MAX_THREADS);
+
+   /* NOTE: if num_threads is zero, we won't use any threads */
+   for (i = 0; i < rast->num_threads; i++) {
+      pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
+      pipe_semaphore_init(&rast->tasks[i].work_done, 0);
+      rast->threads[i] = pipe_thread_create(thread_func,
+                                            (void *) &rast->tasks[i]);
+   }
+}
+
+
+
+/**
+ * Create new lp_rasterizer.
+ * \param empty  the queue to put empty scenes on after we've finished
+ *               processing them.
+ */
+struct lp_rasterizer *
+lp_rast_create( struct pipe_screen *screen, struct lp_scene_queue *empty )
+{
+   struct lp_rasterizer *rast;
+   unsigned i, cbuf;
+
+   rast = CALLOC_STRUCT(lp_rasterizer);
+   if(!rast)
+      return NULL;
+
+   rast->screen = screen;
+
+   rast->empty_scenes = empty;
+   rast->full_scenes = lp_scene_queue_create();
+
+   for (i = 0; i < Elements(rast->tasks); i++) {
+      for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
+        rast->tasks[i].tile.color[cbuf] = align_malloc( TILE_SIZE*TILE_SIZE*4, 16 );
+
+      rast->tasks[i].tile.depth = align_malloc( TILE_SIZE*TILE_SIZE*4, 16 );
+      rast->tasks[i].rast = rast;
+      rast->tasks[i].thread_index = i;
+   }
+
+   create_rast_threads(rast);
+
+   /* for synchronizing rasterization threads */
+   pipe_barrier_init( &rast->barrier, rast->num_threads );
+
+   return rast;
 }
 
 
@@ -466,10 +848,26 @@ void lp_rast_end_tile( struct lp_rasterizer *rast )
  */
 void lp_rast_destroy( struct lp_rasterizer *rast )
 {
-   pipe_surface_reference(&rast->state.cbuf, NULL);
-   pipe_surface_reference(&rast->state.zsbuf, NULL);
-   align_free(rast->tile.depth);
-   align_free(rast->tile.color);
+   unsigned i, cbuf;
+
+   util_unreference_framebuffer_state(&rast->state.fb);
+
+   for (i = 0; i < Elements(rast->tasks); i++) {
+      align_free(rast->tasks[i].tile.depth);
+      for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
+        align_free(rast->tasks[i].tile.color[cbuf]);
+   }
+
+   /* for synchronizing rasterization threads */
+   pipe_barrier_destroy( &rast->barrier );
+
    FREE(rast);
 }
 
+
+/** Return number of rasterization threads */
+unsigned
+lp_rast_get_num_threads( struct lp_rasterizer *rast )
+{
+   return rast->num_threads;
+}