Make flushing more lazy in the draw module.
[mesa.git] / src / mesa / pipe / draw / draw_context.c
index c15f7652a6ffe9ed75926b9e89fbcb598e9ed94b..66c66ff698e86ba9bc405e4226ed5b8231191390 100644 (file)
@@ -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 );
@@ -72,6 +75,8 @@ struct draw_context *draw_create( void )
    draw->attrib_front1 = -1;
    draw->attrib_back1 = -1;
 
+   draw_vertex_cache_invalidate( draw );
+
    return draw;
 }
 
@@ -83,74 +88,31 @@ void draw_destroy( struct draw_context *draw )
 }
 
 
-/**
- * Rebuild the rendering pipeline.
- */
-static void validate_pipeline( 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.
-    */
-
-   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;
-   }
+void draw_flush( struct draw_context *draw )
+{
+   if (draw->drawing)
+      draw_do_flush( draw, DRAW_FLUSH_DRAW );
+}
 
-   /* Always run the cull stage as we calculate determinant there
-    * also.  Fix this..
-    */
-   {
-      draw->pipeline.cull->next = next;
-      next = draw->pipeline.cull;
-   }
 
-   /* 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 )
 {
-   draw->setup = *setup;  /* struct copy */
-   validate_pipeline( draw );
+   draw_flush( draw );
+   draw->rasterizer = raster;
 }
 
 
@@ -158,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;
 }
 
 
@@ -171,6 +134,8 @@ 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;
@@ -185,15 +150,27 @@ 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 */
 }
 
 
+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;
 }
@@ -204,50 +181,75 @@ 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_vertex_shader(struct draw_context *draw,
-                       const struct pipe_shader_state *shader)
+draw_set_mapped_vertex_buffer(struct draw_context *draw,
+                              unsigned attr, const void *buffer)
 {
-   draw->vertex_shader = *shader;
+   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 */
 }
 
 
+
+
+
 /**
- * This function is used to tell the draw module about attributes
- * (like colors) that need to be selected based on front/back face
- * orientation.
- *
- * The logic is:
- *    if (polygon is back-facing) {
- *       vertex->attrib[front0] = vertex->attrib[back0];
- *       vertex->attrib[front1] = vertex->attrib[back1];
- *    }
- *
- * \param front0  first attrib to replace if the polygon is back-facing
- * \param back0  first attrib to copy if the polygon is back-facing
- * \param front1  second attrib to replace if the polygon is back-facing
- * \param back1  second attrib to copy if the polygon is back-facing
- *
- * Pass -1 to disable two-sided attributes.
+ * Allocate space for temporary post-transform vertices, such as for clipping.
  */
-void
-draw_set_twoside_attributes(struct draw_context *draw,
-                            uint front0, uint back0,
-                            uint front1, uint back1)
-{
-   /* XXX we could alternately pass an array of front/back attribs if there's
-    * ever need for more than two.  One could imagine a shader extension
-    * that allows arbitrary attributes to be selected based on polygon
-    * orientation...
-    */
-   draw->attrib_front0 = front0;
-   draw->attrib_back0 = back0;
-   draw->attrib_front1 = front1;
-   draw->attrib_back1 = back1;
+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);
+   }
 }
 
+