Merge branch '7.8' into master
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
index 74f3054864c99359e57d3f1fff1b179d840115fe..cd16b6b2d38783010e3494964a87642cbb208486 100644 (file)
@@ -33,7 +33,7 @@
  */
 
 #include "pipe/p_defines.h"
-#include "pipe/p_inlines.h"
+#include "util/u_inlines.h"
 #include "util/u_memory.h"
 #include "util/u_pack_color.h"
 #include "util/u_surface.h"
 #include "lp_fence.h"
 #include "lp_rast.h"
 #include "lp_setup_context.h"
+#include "lp_screen.h"
+#include "state_tracker/sw_winsys.h"
 
 #include "draw/draw_context.h"
 #include "draw/draw_vbuf.h"
 
 
-/** XXX temporary value, temporary here */
-#define MAX_SCENES 2
-
-
-static void set_scene_state( struct setup_context *, unsigned );
+static void set_scene_state( struct lp_setup_context *, unsigned );
 
 
 struct lp_scene *
-lp_setup_get_current_scene(struct setup_context *setup)
+lp_setup_get_current_scene(struct lp_setup_context *setup)
 {
    if (!setup->scene) {
-      /* wait for a free/empty bin */
-      setup->scene = lp_scene_dequeue(setup->empty_scenes);
-      if(0)lp_scene_reset( setup->scene ); /* XXX temporary? */
 
-      lp_scene_set_framebuffer_size(setup->scene,
-                                    setup->fb.width, 
-                                    setup->fb.height);
+      /* wait for a free/empty scene
+       */
+      setup->scene = lp_scene_dequeue(setup->empty_scenes, TRUE);
+
+      assert(lp_scene_is_empty(setup->scene));
+
+      lp_scene_begin_binning(setup->scene,
+                             &setup->fb );
    }
    return setup->scene;
 }
 
 
 static void
-first_triangle( struct setup_context *setup,
+first_triangle( struct lp_setup_context *setup,
                 const float (*v0)[4],
                 const float (*v1)[4],
                 const float (*v2)[4])
@@ -85,7 +85,7 @@ first_triangle( struct setup_context *setup,
 }
 
 static void
-first_line( struct setup_context *setup,
+first_line( struct lp_setup_context *setup,
            const float (*v0)[4],
            const float (*v1)[4])
 {
@@ -95,7 +95,7 @@ first_line( struct setup_context *setup,
 }
 
 static void
-first_point( struct setup_context *setup,
+first_point( struct lp_setup_context *setup,
             const float (*v0)[4])
 {
    set_scene_state( setup, SETUP_ACTIVE );
@@ -103,7 +103,7 @@ first_point( struct setup_context *setup,
    setup->point( setup, v0 );
 }
 
-static void reset_context( struct setup_context *setup )
+static void reset_context( struct lp_setup_context *setup )
 {
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
@@ -131,14 +131,13 @@ static void reset_context( struct setup_context *setup )
 
 /** Rasterize all scene's bins */
 static void
-lp_setup_rasterize_scene( struct setup_context *setup,
-                        boolean write_depth )
+lp_setup_rasterize_scene( struct lp_setup_context *setup,
+                          boolean write_depth )
 {
    struct lp_scene *scene = lp_setup_get_current_scene(setup);
 
-   lp_rasterize_scene(setup->rast,
-                      scene,
-                      &setup->fb,
+   lp_scene_rasterize(scene,
+                      setup->rast,
                       write_depth);
 
    reset_context( setup );
@@ -149,11 +148,13 @@ lp_setup_rasterize_scene( struct setup_context *setup,
 
 
 static void
-begin_binning( struct setup_context *setup )
+begin_binning( struct lp_setup_context *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.nr_cbufs) {
       if (setup->clear.flags & PIPE_CLEAR_COLOR)
@@ -171,10 +172,6 @@ begin_binning( struct setup_context *setup )
          lp_scene_bin_everywhere( scene, 
                                  lp_rast_clear_zstencil, 
                                  setup->clear.zstencil );
-      else
-         lp_scene_bin_everywhere( scene,
-                                 lp_rast_load_zstencil,
-                                 lp_rast_arg_null() );
    }
 
    LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
@@ -187,7 +184,7 @@ begin_binning( struct setup_context *setup )
  * TODO: fast path for fullscreen clears and no triangles.
  */
 static void
-execute_clears( struct setup_context *setup )
+execute_clears( struct lp_setup_context *setup )
 {
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
@@ -197,7 +194,7 @@ execute_clears( struct setup_context *setup )
 
 
 static void
-set_scene_state( struct setup_context *setup,
+set_scene_state( struct lp_setup_context *setup,
            unsigned new_state )
 {
    unsigned old_state = setup->state;
@@ -232,7 +229,7 @@ set_scene_state( struct setup_context *setup,
 
 
 void
-lp_setup_flush( struct setup_context *setup,
+lp_setup_flush( struct lp_setup_context *setup,
                 unsigned flags )
 {
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
@@ -242,23 +239,24 @@ lp_setup_flush( struct setup_context *setup,
 
 
 void
-lp_setup_bind_framebuffer( struct setup_context *setup,
+lp_setup_bind_framebuffer( struct lp_setup_context *setup,
                            const struct pipe_framebuffer_state *fb )
 {
-   struct lp_scene *scene = lp_setup_get_current_scene(setup);
-
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
+   /* Flush any old scene.
+    */
    set_scene_state( setup, SETUP_FLUSHED );
 
+   /* Set new state.  This will be picked up later when we next need a
+    * scene.
+    */
    util_copy_framebuffer_state(&setup->fb, fb);
-
-   lp_scene_set_framebuffer_size(scene, setup->fb.width, setup->fb.height);
 }
 
 
 void
-lp_setup_clear( struct setup_context *setup,
+lp_setup_clear( struct lp_setup_context *setup,
                 const float *color,
                 double depth,
                 unsigned stencil,
@@ -291,13 +289,13 @@ lp_setup_clear( struct setup_context *setup,
        */
       if (flags & PIPE_CLEAR_COLOR)
          lp_scene_bin_everywhere( scene, 
-                            lp_rast_clear_color, 
-                            setup->clear.color );
+                                  lp_rast_clear_color,
+                                  setup->clear.color );
 
       if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
          lp_scene_bin_everywhere( scene, 
-                            lp_rast_clear_zstencil, 
-                            setup->clear.zstencil );
+                                  lp_rast_clear_zstencil,
+                                  setup->clear.zstencil );
    }
    else {
       /* Put ourselves into the 'pre-clear' state, specifically to try
@@ -316,7 +314,7 @@ lp_setup_clear( struct setup_context *setup,
  * Emit a fence.
  */
 struct pipe_fence_handle *
-lp_setup_fence( struct setup_context *setup )
+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 */
@@ -336,21 +334,25 @@ lp_setup_fence( struct setup_context *setup )
 
 
 void 
-lp_setup_set_triangle_state( struct setup_context *setup,
+lp_setup_set_triangle_state( struct lp_setup_context *setup,
                              unsigned cull_mode,
-                             boolean ccw_is_frontface)
+                             boolean ccw_is_frontface,
+                             boolean scissor,
+                             boolean gl_rasterization_rules)
 {
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
    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;
 }
 
 
 
 void
-lp_setup_set_fs_inputs( struct setup_context *setup,
+lp_setup_set_fs_inputs( struct lp_setup_context *setup,
                         const struct lp_shader_input *input,
                         unsigned nr )
 {
@@ -361,18 +363,22 @@ lp_setup_set_fs_inputs( struct setup_context *setup,
 }
 
 void
-lp_setup_set_fs_function( struct setup_context *setup,
-                          lp_jit_frag_func jit_function )
+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_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function);
+   LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function0);
    /* FIXME: reference count */
 
-   setup->fs.current.jit_function = jit_function;
+   setup->fs.current.jit_function[0] = jit_function0;
+   setup->fs.current.jit_function[1] = jit_function1;
+   setup->fs.current.opaque = opaque;
    setup->dirty |= LP_SETUP_NEW_FS;
 }
 
 void
-lp_setup_set_fs_constants(struct setup_context *setup,
+lp_setup_set_fs_constants(struct lp_setup_context *setup,
                           struct pipe_buffer *buffer)
 {
    LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
@@ -384,7 +390,7 @@ lp_setup_set_fs_constants(struct setup_context *setup,
 
 
 void
-lp_setup_set_alpha_ref_value( struct setup_context *setup,
+lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
                               float alpha_ref_value )
 {
    LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
@@ -396,7 +402,7 @@ lp_setup_set_alpha_ref_value( struct setup_context *setup,
 }
 
 void
-lp_setup_set_blend_color( struct setup_context *setup,
+lp_setup_set_blend_color( struct lp_setup_context *setup,
                           const struct pipe_blend_color *blend_color )
 {
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
@@ -410,8 +416,23 @@ lp_setup_set_blend_color( struct setup_context *setup,
 }
 
 
+void
+lp_setup_set_scissor( struct lp_setup_context *setup,
+                      const struct pipe_scissor_state *scissor )
+{
+   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
+
+   assert(scissor);
+
+   if (memcmp(&setup->scissor.current, scissor, sizeof(*scissor)) != 0) {
+      setup->scissor.current = *scissor; /* struct copy */
+      setup->dirty |= LP_SETUP_NEW_SCISSOR;
+   }
+}
+
+
 void 
-lp_setup_set_flatshade_first( struct setup_context *setup,
+lp_setup_set_flatshade_first( struct lp_setup_context *setup,
                               boolean flatshade_first )
 {
    setup->flatshade_first = flatshade_first;
@@ -419,7 +440,7 @@ lp_setup_set_flatshade_first( struct setup_context *setup,
 
 
 void 
-lp_setup_set_vertex_info( struct setup_context *setup,
+lp_setup_set_vertex_info( struct lp_setup_context *setup,
                           struct vertex_info *vertex_info )
 {
    /* XXX: just silently holding onto the pointer:
@@ -428,54 +449,103 @@ lp_setup_set_vertex_info( struct setup_context *setup,
 }
 
 
+/**
+ * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
+ */
 void
-lp_setup_set_sampler_textures( struct setup_context *setup,
-                               unsigned num, struct pipe_texture **texture)
+lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
+                                    unsigned num,
+                                    struct pipe_sampler_view **views)
 {
-   struct pipe_texture *dummy;
    unsigned i;
 
    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
 
-
    assert(num <= PIPE_MAX_SAMPLERS);
 
    for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
-      struct pipe_texture *tex = i < num ? texture[i] : NULL;
-
-      /* FIXME: hold on to the reference */
-      dummy = NULL;
-      pipe_texture_reference(&dummy, tex);
+      struct pipe_sampler_view *view = i < num ? views[i] : NULL;
 
-      if(tex) {
+      if(view) {
+         struct pipe_texture *tex = view->texture;
          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->width0;
          jit_tex->height = tex->height0;
-         jit_tex->stride = lp_tex->stride[0];
-         if(!lp_tex->dt)
-            jit_tex->data = lp_tex->data;
-         else
-            /* FIXME: map the rendertarget */
-            assert(0);
+         jit_tex->depth = tex->depth0;
+         jit_tex->last_level = tex->last_level;
+         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];
+            }
+         }
+         else {
+            /* display target texture/surface */
+            /*
+             * 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,
+                                                      PIPE_BUFFER_USAGE_CPU_READ);
+            jit_tex->row_stride[0] = lp_tex->stride[0];
+            assert(jit_tex->data[0]);
+         }
+
+         /* the scene references this texture */
+         {
+            struct lp_scene *scene = lp_setup_get_current_scene(setup);
+            lp_scene_texture_reference(scene, tex);
+         }
       }
    }
 
    setup->dirty |= LP_SETUP_NEW_FS;
 }
 
-boolean
-lp_setup_is_texture_referenced( struct setup_context *setup,
+
+/**
+ * Is the given texture referenced by any scene?
+ * Note: we have to check all scenes including any scenes currently
+ * 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 )
 {
-   /* FIXME */
+   unsigned i;
+
+   /* check the render targets */
+   for (i = 0; i < setup->fb.nr_cbufs; i++) {
+      if (setup->fb.cbufs[i]->texture == texture)
+         return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
+   }
+   if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
+      return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
+   }
+
+   /* check textures referenced by the scene */
+   for (i = 0; i < Elements(setup->scenes); i++) {
+      if (lp_scene_is_texture_referenced(setup->scenes[i], texture)) {
+         return PIPE_REFERENCED_FOR_READ;
+      }
+   }
+
    return PIPE_UNREFERENCED;
 }
 
 
+/**
+ * Called by vbuf code when we're about to draw something.
+ */
 void
-lp_setup_update_state( struct setup_context *setup )
+lp_setup_update_state( struct lp_setup_context *setup )
 {
    struct lp_scene *scene = lp_setup_get_current_scene(setup);
 
@@ -502,6 +572,25 @@ lp_setup_update_state( struct setup_context *setup )
       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_buffer *buffer = setup->constants.current;
@@ -574,15 +663,15 @@ lp_setup_update_state( struct setup_context *setup )
 /* Only caller is lp_setup_vbuf_destroy()
  */
 void 
-lp_setup_destroy( struct setup_context *setup )
+lp_setup_destroy( struct lp_setup_context *setup )
 {
    reset_context( setup );
 
    pipe_buffer_reference(&setup->constants.current, NULL);
 
    /* 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);
+   while (1) {
+      struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes, FALSE);
       if (!scene)
          break;
       lp_scene_destroy(scene);
@@ -599,12 +688,12 @@ lp_setup_destroy( struct setup_context *setup )
  * the draw module.  Currently also creates a rasterizer to use with
  * it.
  */
-struct setup_context *
-lp_setup_create( struct pipe_screen *screen,
+struct lp_setup_context *
+lp_setup_create( struct pipe_context *pipe,
                  struct draw_context *draw )
 {
    unsigned i;
-   struct setup_context *setup = CALLOC_STRUCT(setup_context);
+   struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
 
    if (!setup)
       return NULL;
@@ -615,7 +704,9 @@ lp_setup_create( struct pipe_screen *screen,
    if (!setup->empty_scenes)
       goto fail;
 
-   setup->rast = lp_rast_create( screen, setup->empty_scenes );
+   /* XXX: move this to the screen and share between contexts:
+    */
+   setup->rast = lp_rast_create();
    if (!setup->rast) 
       goto fail;
 
@@ -628,8 +719,9 @@ lp_setup_create( struct pipe_screen *screen,
 
    /* 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->scenes[i] = lp_scene_create( pipe, setup->empty_scenes );
+
+      lp_scene_enqueue(setup->empty_scenes, setup->scenes[i]);
    }
 
    setup->triangle = first_triangle;