gallium: more work on ccw flag removal
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
index 76a8b87a3095168e6d8ada7274e8408bd14b09b3..656e6cc38a61dea2aae5ff73b9e8c317a22ef2ee 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_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_query.h"
 #include "lp_rast.h"
 #include "lp_setup_context.h"
 #include "lp_screen.h"
+#include "lp_state.h"
 #include "state_tracker/sw_winsys.h"
 
 #include "draw/draw_context.h"
 #include "draw/draw_vbuf.h"
 
 
-static void set_scene_state( struct lp_setup_context *, unsigned );
+static void set_scene_state( struct lp_setup_context *, enum setup_state );
 
 
 struct lp_scene *
@@ -73,6 +77,26 @@ lp_setup_get_current_scene(struct lp_setup_context *setup)
 }
 
 
+/**
+ * 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);*/
+      }
+   }
+}
+
+
 static void
 first_triangle( struct lp_setup_context *setup,
                 const float (*v0)[4],
@@ -131,14 +155,11 @@ static void reset_context( struct lp_setup_context *setup )
 
 /** Rasterize all scene's bins */
 static void
-lp_setup_rasterize_scene( struct lp_setup_context *setup,
-                          boolean write_depth )
+lp_setup_rasterize_scene( struct lp_setup_context *setup )
 {
    struct lp_scene *scene = lp_setup_get_current_scene(setup);
 
-   lp_scene_rasterize(scene,
-                      setup->rast,
-                      write_depth);
+   lp_scene_rasterize(scene, setup->rast);
 
    reset_context( setup );
 
@@ -157,21 +178,21 @@ begin_binning( struct lp_setup_context *setup )
           (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) ? "clear": "load");
 
    if (setup->fb.nr_cbufs) {
-      if (setup->clear.flags & PIPE_CLEAR_COLOR)
+      if (setup->clear.flags & PIPE_CLEAR_COLOR) {
          lp_scene_bin_everywhere( scene, 
                                  lp_rast_clear_color, 
                                  setup->clear.color );
-      else
-         lp_scene_bin_everywhere( scene,
-                                 lp_rast_load_color,
-                                 lp_rast_arg_null() );
+         scene->has_color_clear = TRUE;
+      }
    }
 
    if (setup->fb.zsbuf) {
-      if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
+      if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
          lp_scene_bin_everywhere( scene, 
                                  lp_rast_clear_zstencil, 
                                  setup->clear.zstencil );
+         scene->has_depth_clear = TRUE;
+      }
    }
 
    LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
@@ -189,13 +210,13 @@ execute_clears( struct lp_setup_context *setup )
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
    begin_binning( setup );
-   lp_setup_rasterize_scene( setup, TRUE );
+   lp_setup_rasterize_scene( setup );
 }
 
 
 static void
 set_scene_state( struct lp_setup_context *setup,
-           unsigned new_state )
+                 enum setup_state new_state )
 {
    unsigned old_state = setup->state;
 
@@ -220,20 +241,41 @@ set_scene_state( struct lp_setup_context *setup,
       if (old_state == SETUP_CLEARED)
          execute_clears( setup );
       else
-         lp_setup_rasterize_scene( setup, TRUE );
+         lp_setup_rasterize_scene( setup );
       break;
+
+   default:
+      assert(0 && "invalid setup state mode");
    }
 
    setup->state = new_state;
 }
 
 
+/**
+ * \param flags  bitmask of PIPE_FLUSH_x flags
+ */
 void
 lp_setup_flush( struct lp_setup_context *setup,
                 unsigned flags )
 {
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
+   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);
+      }
+   }
+
    set_scene_state( setup, SETUP_FLUSHED );
 }
 
@@ -287,15 +329,20 @@ lp_setup_clear( struct lp_setup_context *setup,
        * binned scene and start again, but I don't see that as being
        * a common usage.
        */
-      if (flags & PIPE_CLEAR_COLOR)
+      if (flags & PIPE_CLEAR_COLOR) {
          lp_scene_bin_everywhere( scene, 
                                   lp_rast_clear_color,
                                   setup->clear.color );
+         scene->has_color_clear = TRUE;
+      }
 
-      if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
+      if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
          lp_scene_bin_everywhere( scene, 
                                   lp_rast_clear_zstencil,
                                   setup->clear.zstencil );
+         scene->has_depth_clear = TRUE;
+      }
+
    }
    else {
       /* Put ourselves into the 'pre-clear' state, specifically to try
@@ -316,20 +363,25 @@ lp_setup_clear( struct lp_setup_context *setup,
 struct pipe_fence_handle *
 lp_setup_fence( struct lp_setup_context *setup )
 {
-   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 (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);
 
-   LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
+      LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
 
-   set_scene_state( setup, SETUP_ACTIVE );
+      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) );
+      /* 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;
+      return (struct pipe_fence_handle *) fence;
+   }
 }
 
 
@@ -368,7 +420,8 @@ lp_setup_set_fs_functions( struct lp_setup_context *setup,
                            lp_jit_frag_func jit_function1,
                            boolean opaque )
 {
-   LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function0);
+   LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
+          cast_lp_jit_frag_func_to_voidptr(jit_function0));
    /* FIXME: reference count */
 
    setup->fs.current.jit_function[0] = jit_function0;
@@ -379,11 +432,11 @@ lp_setup_set_fs_functions( struct lp_setup_context *setup,
 
 void
 lp_setup_set_fs_constants(struct lp_setup_context *setup,
-                          struct pipe_buffer *buffer)
+                          struct pipe_resource *buffer)
 {
    LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
 
-   pipe_buffer_reference(&setup->constants.current, buffer);
+   pipe_resource_reference(&setup->constants.current, buffer);
 
    setup->dirty |= LP_SETUP_NEW_CONSTANTS;
 }
@@ -481,8 +534,8 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
       struct pipe_sampler_view *view = i < num ? views[i] : NULL;
 
       if(view) {
-         struct pipe_texture *tex = view->texture;
-         struct llvmpipe_texture *lp_tex = llvmpipe_texture(tex);
+         struct pipe_resource *tex = view->texture;
+         struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
          struct lp_jit_texture *jit_tex;
          jit_tex = &setup->fs.current.jit_context.textures[i];
          jit_tex->width = tex->width0;
@@ -493,15 +546,17 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
          /* We're referencing the texture's internal data, so save a
           * reference to it.
           */
-         pipe_texture_reference(&setup->fs.current_tex[i], tex);
+         pipe_resource_reference(&setup->fs.current_tex[i], tex);
 
          if (!lp_tex->dt) {
             /* regular texture - setup array of mipmap level pointers */
             int j;
             for (j = 0; j <= tex->last_level; j++) {
                jit_tex->data[j] =
-                  (ubyte *) lp_tex->data + lp_tex->level_offset[j];
-               jit_tex->row_stride[j] = lp_tex->stride[j];
+                  llvmpipe_get_texture_image_all(lp_tex, j, LP_TEX_USAGE_READ,
+                                                 LP_TEX_LAYOUT_LINEAR);
+               jit_tex->row_stride[j] = lp_tex->row_stride[j];
+               jit_tex->img_stride[j] = lp_tex->img_stride[j];
             }
          }
          else {
@@ -513,8 +568,9 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
             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,
-                                                      PIPE_BUFFER_USAGE_CPU_READ);
-            jit_tex->row_stride[0] = lp_tex->stride[0];
+                                                        PIPE_TRANSFER_READ);
+            jit_tex->row_stride[0] = lp_tex->row_stride[0];
+            jit_tex->img_stride[0] = lp_tex->img_stride[0];
             assert(jit_tex->data[0]);
          }
       }
@@ -530,8 +586,8 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
  * being rendered and the current scene being built.
  */
 unsigned
-lp_setup_is_texture_referenced( const struct lp_setup_context *setup,
-                                const struct pipe_texture *texture )
+lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
+                                const struct pipe_resource *texture )
 {
    unsigned i;
 
@@ -546,7 +602,7 @@ lp_setup_is_texture_referenced( const struct lp_setup_context *setup,
 
    /* check textures referenced by the scene */
    for (i = 0; i < Elements(setup->scenes); i++) {
-      if (lp_scene_is_texture_referenced(setup->scenes[i], texture)) {
+      if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
          return PIPE_REFERENCED_FOR_READ;
       }
    }
@@ -561,28 +617,49 @@ lp_setup_is_texture_referenced( const struct lp_setup_context *setup,
 void
 lp_setup_update_state( struct lp_setup_context *setup )
 {
-   struct lp_scene *scene = lp_setup_get_current_scene(setup);
+   struct lp_scene *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);
+   }
+
    if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
       uint8_t *stored;
       unsigned i, j;
 
       stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
 
-      /* smear each blend color component across 16 ubyte elements */
-      for (i = 0; i < 4; ++i) {
-         uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
-         for (j = 0; j < 16; ++j)
-            stored[i*16 + j] = c;
-      }
+      if (stored) {
+         /* smear each blend color component across 16 ubyte elements */
+         for (i = 0; i < 4; ++i) {
+            uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
+            for (j = 0; j < 16; ++j)
+               stored[i*16 + j] = c;
+         }
+
+         setup->blend_color.stored = stored;
 
-      setup->blend_color.stored = stored;
+         setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
+      }
 
-      setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
       setup->dirty |= LP_SETUP_NEW_FS;
    }
 
@@ -591,27 +668,29 @@ lp_setup_update_state( struct lp_setup_context *setup )
 
       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;
+      if (stored) {
+         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->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->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_buffer *buffer = setup->constants.current;
+      struct pipe_resource *buffer = setup->constants.current;
 
       if(buffer) {
-         unsigned current_size = buffer->size;
-         const void *current_data = llvmpipe_buffer(buffer)->data;
+         unsigned current_size = buffer->width0;
+         const void *current_data = llvmpipe_resource_data(buffer);
 
          /* TODO: copy only the actually used constants? */
 
@@ -671,7 +750,7 @@ lp_setup_update_state( struct lp_setup_context *setup )
           */
          for (i = 0; i < Elements(setup->fs.current_tex); i++) {
             if (setup->fs.current_tex[i])
-               lp_scene_texture_reference(scene, setup->fs.current_tex[i]);
+               lp_scene_add_resource_reference(scene, setup->fs.current_tex[i]);
          }
       }
    }
@@ -692,11 +771,13 @@ lp_setup_destroy( struct lp_setup_context *setup )
 
    reset_context( setup );
 
+   util_unreference_framebuffer_state(&setup->fb);
+
    for (i = 0; i < Elements(setup->fs.current_tex); i++) {
-      pipe_texture_reference(&setup->fs.current_tex[i], NULL);
+      pipe_resource_reference(&setup->fs.current_tex[i], NULL);
    }
 
-   pipe_buffer_reference(&setup->constants.current, NULL);
+   pipe_resource_reference(&setup->constants.current, NULL);
 
    /* free the scenes in the 'empty' queue */
    while (1) {
@@ -706,6 +787,8 @@ lp_setup_destroy( struct lp_setup_context *setup )
       lp_scene_destroy(scene);
    }
 
+   lp_scene_queue_destroy(setup->empty_scenes);
+
    lp_rast_destroy( setup->rast );
 
    FREE( setup );
@@ -721,8 +804,9 @@ struct lp_setup_context *
 lp_setup_create( struct pipe_context *pipe,
                  struct draw_context *draw )
 {
-   unsigned i;
+   struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
    struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
+   unsigned i;
 
    if (!setup)
       return NULL;
@@ -735,7 +819,8 @@ lp_setup_create( struct pipe_context *pipe,
 
    /* XXX: move this to the screen and share between contexts:
     */
-   setup->rast = lp_rast_create();
+   setup->num_threads = screen->num_threads;
+   setup->rast = lp_rast_create(screen->num_threads);
    if (!setup->rast) 
       goto fail;
 
@@ -775,3 +860,40 @@ fail:
    return NULL;
 }
 
+
+/**
+ * Put a BeginQuery command into all bins.
+ */
+void
+lp_setup_begin_query(struct lp_setup_context *setup,
+                     struct llvmpipe_query *pq)
+{
+   struct lp_scene * scene = lp_setup_get_current_scene(setup);
+   union lp_rast_cmd_arg cmd_arg;
+
+   /* init the query to its beginning state */
+   pq->done = FALSE;
+   pq->tile_count = 0;
+   pq->num_tiles = scene->tiles_x * scene->tiles_y;
+   assert(pq->num_tiles > 0);
+
+   memset(pq->count, 0, sizeof(pq->count));  /* reset all counters */
+
+   cmd_arg.query_obj = pq;
+   lp_scene_bin_everywhere(scene, lp_rast_begin_query, cmd_arg);
+   pq->binned = TRUE;
+}
+
+
+/**
+ * Put an EndQuery command into all bins.
+ */
+void
+lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
+{
+   struct lp_scene * scene = lp_setup_get_current_scene(setup);
+   union lp_rast_cmd_arg cmd_arg;
+
+   cmd_arg.query_obj = pq;
+   lp_scene_bin_everywhere(scene, lp_rast_end_query, cmd_arg);
+}