llvmpipe: get 3D texture image stride from an array rather than computing it
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
index 76a8b87a3095168e6d8ada7274e8408bd14b09b3..1bd5f57412e13f756a9145aa2b2686548dcaebfd 100644 (file)
 #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_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 *
@@ -157,21 +158,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__);
@@ -195,7 +196,7 @@ execute_clears( struct lp_setup_context *setup )
 
 static void
 set_scene_state( struct lp_setup_context *setup,
-           unsigned new_state )
+                 enum setup_state new_state )
 {
    unsigned old_state = setup->state;
 
@@ -222,18 +223,39 @@ set_scene_state( struct lp_setup_context *setup,
       else
          lp_setup_rasterize_scene( setup, TRUE );
       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;
+
+      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 +309,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
@@ -379,11 +406,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 +508,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 +520,22 @@ 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++) {
+#if 0
                jit_tex->data[j] =
                   (ubyte *) lp_tex->data + lp_tex->level_offset[j];
-               jit_tex->row_stride[j] = lp_tex->stride[j];
+#else
+               jit_tex->data[j] =
+                  llvmpipe_get_texture_image_all(lp_tex, j, LP_TEX_USAGE_READ,
+                                                 LP_TEX_LAYOUT_LINEAR);
+#endif
+               jit_tex->row_stride[j] = lp_tex->row_stride[j];
+               jit_tex->img_stride[j] = lp_tex->img_stride[j];
             }
          }
          else {
@@ -513,8 +547,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 +565,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 +581,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;
       }
    }
@@ -567,6 +602,20 @@ lp_setup_update_state( struct lp_setup_context *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;
@@ -607,11 +656,11 @@ lp_setup_update_state( struct lp_setup_context *setup )
    }
 
    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? */
 
@@ -693,10 +742,10 @@ lp_setup_destroy( struct lp_setup_context *setup )
    reset_context( setup );
 
    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) {