llvmpipe: Reset the bin when shading a whole tile with an opaque shader.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
index 6d20975cb81507257e1013ce4aa827006c80417f..61b968c49f5e88c750741bf7fc0129f13aacc38a 100644 (file)
 #include "pipe/p_inlines.h"
 #include "util/u_memory.h"
 #include "util/u_pack_color.h"
-#include "lp_bin.h"
-#include "lp_bin_queue.h"
-#include "lp_debug.h"
-#include "lp_fence.h"
-#include "lp_state.h"
+#include "util/u_surface.h"
+#include "lp_scene.h"
+#include "lp_scene_queue.h"
 #include "lp_buffer.h"
 #include "lp_texture.h"
+#include "lp_debug.h"
+#include "lp_fence.h"
+#include "lp_rast.h"
 #include "lp_setup_context.h"
 
+#include "draw/draw_context.h"
+#include "draw/draw_vbuf.h"
+
 
 /** XXX temporary value, temporary here */
-#define MAX_BINS 2
+#define MAX_SCENES 2
 
 
-static void set_state( struct setup_context *, unsigned );
+static void set_scene_state( struct setup_context *, unsigned );
 
 
-struct lp_bins *
-lp_setup_get_current_bins(struct setup_context *setup)
+struct lp_scene *
+lp_setup_get_current_scene(struct setup_context *setup)
 {
-   if (!setup->bins) {
+   if (!setup->scene) {
       /* wait for a free/empty bin */
-      setup->bins = lp_bins_dequeue(setup->empty_bins);
-      if(0)lp_reset_bins( setup->bins ); /* XXX temporary? */
+      setup->scene = lp_scene_dequeue(setup->empty_scenes);
+      if(0)lp_scene_reset( setup->scene ); /* XXX temporary? */
 
-      if (setup->fb) {
-         lp_bin_set_framebuffer_size(setup->bins,
-                                     setup->fb->width, setup->fb->height);
-      }
+      lp_scene_set_framebuffer_size(setup->scene,
+                                    setup->fb.width, 
+                                    setup->fb.height);
    }
-   return setup->bins;
+   return setup->scene;
 }
 
 
@@ -76,7 +79,7 @@ first_triangle( struct setup_context *setup,
                 const float (*v1)[4],
                 const float (*v2)[4])
 {
-   set_state( setup, SETUP_ACTIVE );
+   set_scene_state( setup, SETUP_ACTIVE );
    lp_setup_choose_triangle( setup );
    setup->triangle( setup, v0, v1, v2 );
 }
@@ -86,7 +89,7 @@ first_line( struct setup_context *setup,
            const float (*v0)[4],
            const float (*v1)[4])
 {
-   set_state( setup, SETUP_ACTIVE );
+   set_scene_state( setup, SETUP_ACTIVE );
    lp_setup_choose_line( setup );
    setup->line( setup, v0, v1 );
 }
@@ -95,7 +98,7 @@ static void
 first_point( struct setup_context *setup,
             const float (*v0)[4])
 {
-   set_state( setup, SETUP_ACTIVE );
+   set_scene_state( setup, SETUP_ACTIVE );
    lp_setup_choose_point( setup );
    setup->point( setup, v0 );
 }
@@ -111,7 +114,7 @@ static void reset_context( struct setup_context *setup )
    setup->dirty = ~0;
 
    /* no current bin */
-   setup->bins = NULL;
+   setup->scene = NULL;
 
    /* Reset some state:
     */
@@ -126,17 +129,17 @@ static void reset_context( struct setup_context *setup )
 }
 
 
-/** Rasterize all tile's bins */
+/** Rasterize all scene's bins */
 static void
-lp_setup_rasterize_bins( struct setup_context *setup,
+lp_setup_rasterize_scene( struct setup_context *setup,
                         boolean write_depth )
 {
-   struct lp_bins *bins = lp_setup_get_current_bins(setup);
+   struct lp_scene *scene = lp_setup_get_current_scene(setup);
 
-   lp_rasterize_bins(setup->rast,
-                     bins,
-                     setup->fb,
-                     write_depth);
+   lp_rasterize_scene(setup->rast,
+                      scene,
+                      &setup->fb,
+                      write_depth);
 
    reset_context( setup );
 
@@ -148,30 +151,32 @@ lp_setup_rasterize_bins( struct setup_context *setup,
 static void
 begin_binning( struct setup_context *setup )
 {
-   struct lp_bins *bins = lp_setup_get_current_bins(setup);
+   struct lp_scene *scene = lp_setup_get_current_scene(setup);
 
-   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
+   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");
 
-   if (setup->fb->cbufs[0]) {
+   if (setup->fb.nr_cbufs) {
       if (setup->clear.flags & PIPE_CLEAR_COLOR)
-         lp_bin_everywhere( bins
-                            lp_rast_clear_color, 
-                            setup->clear.color );
+         lp_scene_bin_everywhere( scene
+                                 lp_rast_clear_color, 
+                                 setup->clear.color );
       else
-         lp_bin_everywhere( bins,
-                            lp_rast_load_color,
-                            lp_rast_arg_null() );
+         lp_scene_bin_everywhere( scene,
+                                 lp_rast_load_color,
+                                 lp_rast_arg_null() );
    }
 
-   if (setup->fb->zsbuf) {
+   if (setup->fb.zsbuf) {
       if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
-         lp_bin_everywhere( bins
-                            lp_rast_clear_zstencil, 
-                            setup->clear.zstencil );
+         lp_scene_bin_everywhere( scene
+                                 lp_rast_clear_zstencil, 
+                                 setup->clear.zstencil );
       else
-         lp_bin_everywhere( bins,
-                            lp_rast_load_zstencil,
-                            lp_rast_arg_null() );
+         lp_scene_bin_everywhere( scene,
+                                 lp_rast_load_zstencil,
+                                 lp_rast_arg_null() );
    }
 
    LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
@@ -189,12 +194,12 @@ execute_clears( struct setup_context *setup )
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
    begin_binning( setup );
-   lp_setup_rasterize_bins( setup, TRUE );
+   lp_setup_rasterize_scene( setup, TRUE );
 }
 
 
 static void
-set_state( struct setup_context *setup,
+set_scene_state( struct setup_context *setup,
            unsigned new_state )
 {
    unsigned old_state = setup->state;
@@ -220,7 +225,7 @@ set_state( struct setup_context *setup,
       if (old_state == SETUP_CLEARED)
          execute_clears( setup );
       else
-         lp_setup_rasterize_bins( setup, TRUE );
+         lp_setup_rasterize_scene( setup, TRUE );
       break;
    }
 
@@ -234,7 +239,7 @@ lp_setup_flush( struct setup_context *setup,
 {
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
-   set_state( setup, SETUP_FLUSHED );
+   set_scene_state( setup, SETUP_FLUSHED );
 }
 
 
@@ -242,15 +247,15 @@ void
 lp_setup_bind_framebuffer( struct setup_context *setup,
                            const struct pipe_framebuffer_state *fb )
 {
-   struct lp_bins *bins = lp_setup_get_current_bins(setup);
+   struct lp_scene *scene = lp_setup_get_current_scene(setup);
 
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
-   set_state( setup, SETUP_FLUSHED );
+   set_scene_state( setup, SETUP_FLUSHED );
 
-   setup->fb = fb;
+   util_copy_framebuffer_state(&setup->fb, fb);
 
-   lp_bin_set_framebuffer_size(bins, setup->fb->width, setup->fb->height);
+   lp_scene_set_framebuffer_size(scene, setup->fb.width, setup->fb.height);
 }
 
 
@@ -261,7 +266,7 @@ lp_setup_clear( struct setup_context *setup,
                 unsigned stencil,
                 unsigned flags )
 {
-   struct lp_bins *bins = lp_setup_get_current_bins(setup);
+   struct lp_scene *scene = lp_setup_get_current_scene(setup);
    unsigned i;
 
    LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
@@ -274,25 +279,25 @@ lp_setup_clear( struct setup_context *setup,
 
    if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
       setup->clear.zstencil.clear_zstencil = 
-         util_pack_z_stencil(setup->fb->zsbuf->format, 
+         util_pack_z_stencil(setup->fb.zsbuf->format, 
                              depth,
                              stencil);
    }
 
    if (setup->state == SETUP_ACTIVE) {
-      /* Add the clear to existing bins.  In the unusual case where
+      /* 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
        * binned scene and start again, but I don't see that as being
        * a common usage.
        */
       if (flags & PIPE_CLEAR_COLOR)
-         lp_bin_everywhere( bins
+         lp_scene_bin_everywhere( scene
                             lp_rast_clear_color, 
                             setup->clear.color );
 
       if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
-         lp_bin_everywhere( bins
+         lp_scene_bin_everywhere( scene
                             lp_rast_clear_zstencil, 
                             setup->clear.zstencil );
    }
@@ -302,7 +307,7 @@ lp_setup_clear( struct setup_context *setup,
        * buffers which the app or state-tracker might issue
        * separately.
        */
-      set_state( setup, SETUP_CLEARED );
+      set_scene_state( setup, SETUP_CLEARED );
 
       setup->clear.flags |= flags;
    }
@@ -315,18 +320,18 @@ lp_setup_clear( struct setup_context *setup,
 struct pipe_fence_handle *
 lp_setup_fence( struct setup_context *setup )
 {
-   struct lp_bins *bins = lp_setup_get_current_bins(setup);
-   const unsigned rank = lp_bin_get_num_bins( bins );
+   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);
 
    LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
 
-   set_state( setup, SETUP_ACTIVE );
+   set_scene_state( setup, SETUP_ACTIVE );
 
    /* insert the fence into all command bins */
-   lp_bin_everywhere( bins,
-                      lp_rast_fence,
-                      lp_rast_arg_fence(fence) );
+   lp_scene_bin_everywhere( scene,
+                           lp_rast_fence,
+                           lp_rast_arg_fence(fence) );
 
    return (struct pipe_fence_handle *) fence;
 }
@@ -358,13 +363,15 @@ lp_setup_set_fs_inputs( struct setup_context *setup,
 }
 
 void
-lp_setup_set_fs( struct setup_context *setup,
-                 struct lp_fragment_shader *fs )
+lp_setup_set_fs_function( struct setup_context *setup,
+                          lp_jit_frag_func jit_function,
+                          boolean opaque )
 {
-   LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) fs);
+   LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function);
    /* FIXME: reference count */
 
-   setup->fs.current.jit_function = fs ? fs->current->jit_function : NULL;
+   setup->fs.current.jit_function = jit_function;
+   setup->fs.current.opaque = opaque;
    setup->dirty |= LP_SETUP_NEW_FS;
 }
 
@@ -406,6 +413,25 @@ lp_setup_set_blend_color( struct setup_context *setup,
    }
 }
 
+
+void 
+lp_setup_set_flatshade_first( struct setup_context *setup,
+                              boolean flatshade_first )
+{
+   setup->flatshade_first = flatshade_first;
+}
+
+
+void 
+lp_setup_set_vertex_info( struct setup_context *setup,
+                          struct vertex_info *vertex_info )
+{
+   /* XXX: just silently holding onto the pointer:
+    */
+   setup->vertex_info = vertex_info;
+}
+
+
 void
 lp_setup_set_sampler_textures( struct setup_context *setup,
                                unsigned num, struct pipe_texture **texture)
@@ -429,8 +455,8 @@ lp_setup_set_sampler_textures( struct setup_context *setup,
          struct llvmpipe_texture *lp_tex = llvmpipe_texture(tex);
          struct lp_jit_texture *jit_tex;
          jit_tex = &setup->fs.current.jit_context.textures[i];
-         jit_tex->width = tex->width[0];
-         jit_tex->height = tex->height[0];
+         jit_tex->width = tex->width0;
+         jit_tex->height = tex->height0;
          jit_tex->stride = lp_tex->stride[0];
          if(!lp_tex->dt)
             jit_tex->data = lp_tex->data;
@@ -452,10 +478,10 @@ lp_setup_is_texture_referenced( struct setup_context *setup,
 }
 
 
-static INLINE void
-lp_setup_update_shader_state( struct setup_context *setup )
+void
+lp_setup_update_state( struct setup_context *setup )
 {
-   struct lp_bins *bins = lp_setup_get_current_bins(setup);
+   struct lp_scene *scene = lp_setup_get_current_scene(setup);
 
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
@@ -465,7 +491,7 @@ lp_setup_update_shader_state( struct setup_context *setup )
       uint8_t *stored;
       unsigned i, j;
 
-      stored = lp_bin_alloc_aligned(bins, 4 * 16, 16);
+      stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
 
       /* smear each blend color component across 16 ubyte elements */
       for (i = 0; i < 4; ++i) {
@@ -497,7 +523,7 @@ lp_setup_update_shader_state( struct setup_context *setup )
                    current_size) != 0) {
             void *stored;
 
-            stored = lp_bin_alloc(bins, current_size);
+            stored = lp_scene_alloc(scene, current_size);
             if(stored) {
                memcpy(stored,
                       current_data,
@@ -522,12 +548,12 @@ lp_setup_update_shader_state( struct setup_context *setup )
          memcmp(setup->fs.stored,
                 &setup->fs.current,
                 sizeof setup->fs.current) != 0) {
-         /* The fs state that's been stored in the bins is different from
+         /* 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.
           */
          struct lp_rast_state *stored =
-            (struct lp_rast_state *) lp_bin_alloc(bins, sizeof *stored);
+            (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
          if(stored) {
             memcpy(stored,
                    &setup->fs.current,
@@ -535,9 +561,9 @@ lp_setup_update_shader_state( struct setup_context *setup )
             setup->fs.stored = stored;
 
             /* put the state-set command into all bins */
-            lp_bin_state_command( bins,
-                                  lp_rast_set_state, 
-                                  lp_rast_arg_state(setup->fs.stored) );
+            lp_scene_bin_state_command( scene,
+                                       lp_rast_set_state, 
+                                       lp_rast_arg_state(setup->fs.stored) );
          }
       }
    }
@@ -548,38 +574,9 @@ lp_setup_update_shader_state( struct setup_context *setup )
 }
 
 
-/* Stubs for lines & points for now:
- */
-void
-lp_setup_point(struct setup_context *setup,
-                    const float (*v0)[4])
-{
-   lp_setup_update_shader_state(setup);
-   setup->point( setup, v0 );
-}
-
-void
-lp_setup_line(struct setup_context *setup,
-                   const float (*v0)[4],
-                   const float (*v1)[4])
-{
-   lp_setup_update_shader_state(setup);
-   setup->line( setup, v0, v1 );
-}
-
-void
-lp_setup_tri(struct setup_context *setup,
-             const float (*v0)[4],
-             const float (*v1)[4],
-             const float (*v2)[4])
-{
-   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
-
-   lp_setup_update_shader_state(setup);
-   setup->triangle( setup, v0, v1, v2 );
-}
-
 
+/* Only caller is lp_setup_vbuf_destroy()
+ */
 void 
 lp_setup_destroy( struct setup_context *setup )
 {
@@ -587,12 +584,12 @@ lp_setup_destroy( struct setup_context *setup )
 
    pipe_buffer_reference(&setup->constants.current, NULL);
 
-   /* free the bins in the 'empty' queue */
-   while (lp_bins_queue_size(setup->empty_bins) > 0) {
-      struct lp_bins *bins = lp_bins_dequeue(setup->empty_bins);
-      if (!bins)
+   /* free the scenes in the 'empty' queue */
+   while (lp_scene_queue_count(setup->empty_scenes) > 0) {
+      struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes);
+      if (!scene)
          break;
-      lp_bins_destroy(bins);
+      lp_scene_destroy(scene);
    }
 
    lp_rast_destroy( setup->rast );
@@ -602,11 +599,13 @@ lp_setup_destroy( struct setup_context *setup )
 
 
 /**
- * Create a new primitive tiling engine.  Currently also creates a
- * rasterizer to use with it.
+ * Create a new primitive tiling engine.  Plug it into the backend of
+ * the draw module.  Currently also creates a rasterizer to use with
+ * it.
  */
 struct setup_context *
-lp_setup_create( struct pipe_screen *screen )
+lp_setup_create( struct pipe_screen *screen,
+                 struct draw_context *draw )
 {
    unsigned i;
    struct setup_context *setup = CALLOC_STRUCT(setup_context);
@@ -614,18 +613,27 @@ lp_setup_create( struct pipe_screen *screen )
    if (!setup)
       return NULL;
 
-   setup->empty_bins = lp_bins_queue_create();
-   if (!setup->empty_bins)
+   lp_setup_init_vbuf(setup);
+
+   setup->empty_scenes = lp_scene_queue_create();
+   if (!setup->empty_scenes)
       goto fail;
 
-   setup->rast = lp_rast_create( screen, setup->empty_bins );
+   setup->rast = lp_rast_create( screen, setup->empty_scenes );
    if (!setup->rast) 
       goto fail;
 
-   /* create some empty bins */
-   for (i = 0; i < MAX_BINS; i++) {
-      struct lp_bins *bins = lp_bins_create();
-      lp_bins_enqueue(setup->empty_bins, bins);
+   setup->vbuf = draw_vbuf_stage(draw, &setup->base);
+   if (!setup->vbuf)
+      goto fail;
+
+   draw_set_rasterize_stage(draw, setup->vbuf);
+   draw_set_render(draw, &setup->base);
+
+   /* create some empty scenes */
+   for (i = 0; i < MAX_SCENES; i++) {
+      struct lp_scene *scene = lp_scene_create();
+      lp_scene_enqueue(setup->empty_scenes, scene);
    }
 
    setup->triangle = first_triangle;
@@ -637,8 +645,14 @@ lp_setup_create( struct pipe_screen *screen )
    return setup;
 
 fail:
-   if (setup->empty_bins)
-      lp_bins_queue_destroy(setup->empty_bins);
+   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;