Make flushing more lazy in the draw module.
[mesa.git] / src / mesa / pipe / draw / draw_context.c
index a97f488387361f1126a1fc48174e06626242e57d..66c66ff698e86ba9bc405e4226ed5b8231191390 100644 (file)
   *   Keith Whitwell <keith@tungstengraphics.com>
   */
 
-#include "imports.h"
-#include "macros.h"
 
+#include "pipe/p_util.h"
 #include "draw_context.h"
 #include "draw_private.h"
 
 
+
 struct draw_context *draw_create( void )
 {
    struct draw_context *draw = CALLOC_STRUCT( draw_context );
@@ -48,6 +48,9 @@ struct draw_context *draw_create( void )
    draw->pipeline.clip      = draw_clip_stage( draw );
    draw->pipeline.flatshade = draw_flatshade_stage( draw );
    draw->pipeline.cull      = draw_cull_stage( draw );
+   draw->pipeline.feedback  = draw_feedback_stage( draw );
+   draw->pipeline.validate  = draw_validate_stage( draw );
+   draw->pipeline.first     = draw->pipeline.validate;
 
    ASSIGN_4V( draw->plane[0], -1,  0,  0, 1 );
    ASSIGN_4V( draw->plane[1],  1,  0,  0, 1 );
@@ -57,91 +60,59 @@ struct draw_context *draw_create( void )
    ASSIGN_4V( draw->plane[5],  0,  0, -1, 1 ); /* mesa's a bit wonky */
    draw->nr_planes = 6;
 
-   draw->vf = vf_create( GL_TRUE );
-
-   return draw;
-}
+   /* Statically allocate maximum sized vertices for the cache - could be cleverer...
+    */
+   {
+      int i;
+      char *tmp = malloc(Elements(draw->vcache.vertex) * MAX_VERTEX_SIZE);
 
+      for (i = 0; i < Elements(draw->vcache.vertex); i++)
+        draw->vcache.vertex[i] = (struct vertex_header *)(tmp + i * MAX_VERTEX_SIZE);
+   }
 
-void draw_destroy( struct draw_context *draw )
-{
-   if (draw->header.storage)
-      ALIGN_FREE( draw->header.storage );
+   draw->attrib_front0 = -1;
+   draw->attrib_back0 = -1;
+   draw->attrib_front1 = -1;
+   draw->attrib_back1 = -1;
 
-   vf_destroy( draw->vf );
+   draw_vertex_cache_invalidate( draw );
 
-   FREE( draw );
+   return draw;
 }
 
 
-/**
- * Rebuild the rendering pipeline.
- */
-static void validate_pipeline( struct draw_context *draw )
+void draw_destroy( struct draw_context *draw )
 {
-   struct draw_stage *next = draw->pipeline.setup;
-
-   /*
-    * NOTE: we build up the pipeline in end-to-start order.
-    *
-    * TODO: make the current primitive part of the state and build
-    * shorter pipelines for lines & points.
-    */
+   free( draw->vcache.vertex[0] ); /* Frees all the vertices. */
+   free( draw );
+}
 
-   if (draw->setup.fill_cw != PIPE_POLYGON_MODE_FILL ||
-       draw->setup.fill_ccw != PIPE_POLYGON_MODE_FILL) {
-      draw->pipeline.unfilled->next = next;
-      next = draw->pipeline.unfilled;
-   }
-        
-   if (draw->setup.offset_cw ||
-       draw->setup.offset_ccw) {
-      draw->pipeline.offset->next = next;
-      next = draw->pipeline.offset;
-   }
 
-   if (draw->setup.light_twoside) {
-      draw->pipeline.twoside->next = next;
-      next = draw->pipeline.twoside;
-   }
 
-   /* Always run the cull stage as we calculate determinant there
-    * also.  Fix this..
-    */
-   {
-      draw->pipeline.cull->next = next;
-      next = draw->pipeline.cull;
-   }
+void draw_flush( struct draw_context *draw )
+{
+   if (draw->drawing)
+      draw_do_flush( draw, DRAW_FLUSH_DRAW );
+}
 
-   /* Clip stage
-    */
-   {
-      draw->pipeline.clip->next = next;
-      next = draw->pipeline.clip;
-   }
 
-   /* Do software flatshading prior to clipping.  XXX: should only do
-    * this for clipped primitives, ie it is a part of the clip
-    * routine.
-    */
-   if (draw->setup.flatshade) {
-      draw->pipeline.flatshade->next = next;
-      next = draw->pipeline.flatshade;
-   }
-   
-   draw->pipeline.first = next;
+void draw_set_feedback_state( struct draw_context *draw,
+                              const struct pipe_feedback_state *feedback )
+{
+   draw_flush( draw );
+   draw->feedback = *feedback;
 }
 
 
 /**
- * Register new primitive setup/rendering state.
+ * Register new primitive rasterization/rendering state.
  * This causes the drawing pipeline to be rebuilt.
  */
-void draw_set_setup_state( struct draw_context *draw,
-                           const struct pipe_setup_state *setup )
+void draw_set_rasterizer_state( struct draw_context *draw,
+                                const struct pipe_rasterizer_state *raster )
 {
-   memcpy( &draw->setup, setup, sizeof(*setup) );
-   validate_pipeline( draw );
+   draw_flush( draw );
+   draw->rasterizer = raster;
 }
 
 
@@ -149,10 +120,11 @@ void draw_set_setup_state( struct draw_context *draw,
  * Plug in the primitive rendering/rasterization stage.
  * This is provided by the device driver.
  */
-void draw_set_setup_stage( struct draw_context *draw,
-                           struct draw_stage *stage )
+void draw_set_rasterize_stage( struct draw_context *draw,
+                               struct draw_stage *stage )
 {
-   draw->pipeline.setup = stage;
+   draw_flush( draw );
+   draw->pipeline.rasterize = stage;
 }
 
 
@@ -162,8 +134,13 @@ void draw_set_setup_stage( struct draw_context *draw,
 void draw_set_clip_state( struct draw_context *draw,
                           const struct pipe_clip_state *clip )
 {
+   draw_flush( draw );
+
+   assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
    memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
    draw->nr_planes = 6 + clip->nr;
+   /* bitmask of the enabled user-defined clip planes */
+   draw->user_clipmask = ((1 << clip->nr) - 1) << 6;
 }
 
 
@@ -173,11 +150,106 @@ void draw_set_clip_state( struct draw_context *draw,
 void draw_set_viewport_state( struct draw_context *draw,
                               const struct pipe_viewport_state *viewport )
 {
+   draw_flush( draw );
    draw->viewport = *viewport; /* struct copy */
+}
 
-   vf_set_vp_scale_translate( draw->vf, viewport->scale, viewport->translate );
 
-   /* Using tnl/ and vf/ modules is temporary while getting started.
-    * Full pipe will have vertex shader, vertex fetch of its own.
-    */
+void
+draw_set_vertex_shader(struct draw_context *draw,
+                       const struct pipe_shader_state *shader)
+{
+   draw_flush( draw );
+   draw->vertex_shader = *shader;
+}
+
+
+void
+draw_set_vertex_buffer(struct draw_context *draw,
+                       unsigned attr,
+                       const struct pipe_vertex_buffer *buffer)
+{
+   draw_flush( draw );
+
+   assert(attr < PIPE_ATTRIB_MAX);
+   draw->vertex_buffer[attr] = *buffer;
 }
+
+
+void
+draw_set_vertex_element(struct draw_context *draw,
+                        unsigned attr,
+                        const struct pipe_vertex_element *element)
+{
+   draw_flush( draw );
+
+   assert(attr < PIPE_ATTRIB_MAX);
+   draw->vertex_element[attr] = *element;
+}
+
+
+/**
+ * Tell drawing context where to find mapped vertex buffers.
+ */
+void
+draw_set_mapped_vertex_buffer(struct draw_context *draw,
+                              unsigned attr, const void *buffer)
+{
+   draw_flush( draw );
+
+   draw->mapped_vbuffer[attr] = buffer;
+}
+
+
+void
+draw_set_mapped_constant_buffer(struct draw_context *draw,
+                                const void *buffer)
+{
+   draw_flush( draw );
+
+   draw->mapped_constants = buffer;
+}
+
+
+void
+draw_set_mapped_feedback_buffer(struct draw_context *draw, uint index,
+                                void *buffer, uint size)
+{
+   draw_flush( draw );
+
+   assert(index < PIPE_MAX_FEEDBACK_ATTRIBS);
+   draw->mapped_feedback_buffer[index] = buffer;
+   draw->mapped_feedback_buffer_size[index] = size; /* in bytes */
+}
+
+
+
+
+
+/**
+ * Allocate space for temporary post-transform vertices, such as for clipping.
+ */
+void draw_alloc_tmps( struct draw_stage *stage, unsigned nr )
+{
+   stage->nr_tmps = nr;
+
+   if (nr) {
+      ubyte *store = (ubyte *) malloc(MAX_VERTEX_SIZE * nr);
+      unsigned i;
+
+      stage->tmp = (struct vertex_header **) malloc(sizeof(struct vertex_header *) * nr);
+      
+      for (i = 0; i < nr; i++)
+        stage->tmp[i] = (struct vertex_header *)(store + i * MAX_VERTEX_SIZE);
+   }
+}
+
+void draw_free_tmps( struct draw_stage *stage )
+{
+   if (stage->tmp) {
+      free(stage->tmp[0]);
+      free(stage->tmp);
+   }
+}
+
+