draw/translate: fix instancing
[mesa.git] / src / gallium / auxiliary / draw / draw_pt.c
index 5b6eaa739d04ac5ad04ab7bbb1d28e581ad414de..e89ccd254011f24c0d508edd2f7b87da678a83d6 100644 (file)
@@ -34,6 +34,7 @@
 #include "draw/draw_gs.h"
 #include "draw/draw_private.h"
 #include "draw/draw_pt.h"
+#include "draw/draw_vbuf.h"
 #include "draw/draw_vs.h"
 #include "tgsi/tgsi_dump.h"
 #include "util/u_math.h"
@@ -121,7 +122,7 @@ draw_pt_arrays(struct draw_context *draw,
          /* Flush draw state if eltSize changed.
           * This could be improved so only the frontend is flushed since it
           * converts all indices to ushorts and the fetch part of the middle
-          * always perpares both linear and indexed.
+          * always prepares both linear and indexed.
           */
          frontend->flush( frontend, DRAW_FLUSH_STATE_CHANGE );
          frontend = NULL;
@@ -139,6 +140,12 @@ draw_pt_arrays(struct draw_context *draw,
       draw->pt.opt = opt;
    }
 
+   if (draw->pt.rebind_parameters) {
+      /* update constants, viewport dims, clip planes, etc */
+      middle->bind_parameters(middle);
+      draw->pt.rebind_parameters = FALSE;
+   }
+
    frontend->run( frontend, start, count );
 
    return TRUE;
@@ -146,13 +153,19 @@ draw_pt_arrays(struct draw_context *draw,
 
 void draw_pt_flush( struct draw_context *draw, unsigned flags )
 {
+   assert(flags);
+
    if (draw->pt.frontend) {
       draw->pt.frontend->flush( draw->pt.frontend, flags );
 
       /* don't prepare if we only are flushing the backend */
-      if (!(flags & DRAW_FLUSH_BACKEND))
+      if (flags & DRAW_FLUSH_STATE_CHANGE)
          draw->pt.frontend = NULL;
    }
+
+   if (flags & DRAW_FLUSH_PARAMETER_CHANGE) {
+      draw->pt.rebind_parameters = TRUE;
+   }
 }
 
 
@@ -269,7 +282,7 @@ draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count)
 
       for (j = 0; j < draw->pt.nr_vertex_elements; j++) {
          uint buf = draw->pt.vertex_element[j].vertex_buffer_index;
-         ubyte *ptr = (ubyte *) draw->pt.user.vbuffer[buf];
+         ubyte *ptr = (ubyte *) draw->pt.user.vbuffer[buf].map;
 
          if (draw->pt.vertex_element[j].instance_divisor) {
             ii = draw->instance_id / draw->pt.vertex_element[j].instance_divisor;
@@ -313,6 +326,13 @@ draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count)
                             (void *) u);
             }
             break;
+         case PIPE_FORMAT_A8R8G8B8_UNORM:
+            {
+               ubyte *u = (ubyte *) ptr;
+               debug_printf("ARGB %d %d %d %d  @ %p\n", u[0], u[1], u[2], u[3],
+                            (void *) u);
+            }
+            break;
          default:
             debug_printf("other format %s (fix me)\n",
                      util_format_name(draw->pt.vertex_element[j].src_format));
@@ -326,7 +346,7 @@ draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count)
 #define PRIM_RESTART_LOOP(elements) \
    do { \
       for (i = start; i < end; i++) { \
-         if (elements[i] == info->restart_index) { \
+         if (i < elt_max && elements[i] == info->restart_index) { \
             if (cur_count > 0) { \
                /* draw elts up to prev pos */ \
                draw_pt_arrays(draw, prim, cur_start, cur_count); \
@@ -358,11 +378,12 @@ draw_pt_arrays_restart(struct draw_context *draw,
    const unsigned start = info->start;
    const unsigned count = info->count;
    const unsigned end = start + count;
+   const unsigned elt_max = draw->pt.user.eltMax;
    unsigned i, cur_start, cur_count;
 
    assert(info->primitive_restart);
 
-   if (draw->pt.user.elts) {
+   if (draw->pt.user.eltSize) {
       /* indexed prims (draw_elements) */
       cur_start = start;
       cur_count = 0;
@@ -399,57 +420,34 @@ draw_pt_arrays_restart(struct draw_context *draw,
 }
 
 
-
 /**
- * Non-instanced drawing.
- * \sa draw_arrays_instanced
+ * Resolve true values within pipe_draw_info.
+ * If we're rendering from transform feedback/stream output
+ * buffers both the count and max_index need to be computed
+ * from the attached stream output target. 
  */
-void
-draw_arrays(struct draw_context *draw, unsigned prim,
-            unsigned start, unsigned count)
-{
-   draw_arrays_instanced(draw, prim, start, count, 0, 1);
-}
-
-
-/**
- * Instanced drawing.
- * \sa draw_vbo
- */
-void
-draw_arrays_instanced(struct draw_context *draw,
-                      unsigned mode,
-                      unsigned start,
-                      unsigned count,
-                      unsigned startInstance,
-                      unsigned instanceCount)
+static void
+resolve_draw_info(const struct pipe_draw_info *raw_info,
+                  struct pipe_draw_info *info)
 {
-   struct pipe_draw_info info;
-
-   util_draw_init_info(&info);
+   memcpy(info, raw_info, sizeof(struct pipe_draw_info));
 
-   info.mode = mode;
-   info.start = start;
-   info.count = count;
-   info.start_instance = startInstance;
-   info.instance_count = instanceCount;
+   if (raw_info->count_from_stream_output) {
+      struct draw_so_target *target =
+         (struct draw_so_target *)info->count_from_stream_output;
+      info->count = target->emitted_vertices;
 
-   info.indexed = (draw->pt.user.elts != NULL);
-   if (!info.indexed) {
-      info.min_index = start;
-      info.max_index = start + count - 1;
+      /* Stream output draw can not be indexed */
+      debug_assert(!info->indexed);
+      info->max_index = info->count - 1;
    }
-
-   draw_vbo(draw, &info);
 }
 
-
 /**
  * Draw vertex arrays.
  * This is the main entrypoint into the drawing module.  If drawing an indexed
- * primitive, the draw_set_index_buffer() and draw_set_mapped_index_buffer()
- * functions should have already been called to specify the element/index
- * buffer information.
+ * primitive, the draw_set_indexes() function should have already been called
+ * to specify the element/index buffer information.
  */
 void
 draw_vbo(struct draw_context *draw,
@@ -457,18 +455,26 @@ draw_vbo(struct draw_context *draw,
 {
    unsigned instance;
    unsigned index_limit;
+   unsigned count;
+   struct pipe_draw_info resolved_info;
+
+   resolve_draw_info(info, &resolved_info);
+   info = &resolved_info;
 
    assert(info->instance_count > 0);
    if (info->indexed)
       assert(draw->pt.user.elts);
 
+   count = info->count;
+
    draw->pt.user.eltBias = info->index_bias;
    draw->pt.user.min_index = info->min_index;
    draw->pt.user.max_index = info->max_index;
+   draw->pt.user.eltSize = info->indexed ? draw->pt.user.eltSizeIB : 0;
 
    if (0)
       debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
-                   info->mode, info->start, info->count);
+                   info->mode, info->start, count);
 
    if (0)
       tgsi_dump(draw->vs.vertex_shader->state.tokens, 0);
@@ -486,30 +492,39 @@ draw_vbo(struct draw_context *draw,
       }
       debug_printf("Buffers:\n");
       for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
-         debug_printf("  %u: stride=%u offset=%u ptr=%p\n",
+         debug_printf("  %u: stride=%u offset=%u size=%d ptr=%p\n",
                       i,
                       draw->pt.vertex_buffer[i].stride,
                       draw->pt.vertex_buffer[i].buffer_offset,
-                      draw->pt.user.vbuffer[i]);
+                      (int) draw->pt.user.vbuffer[i].size,
+                      draw->pt.user.vbuffer[i].map);
       }
    }
 
    if (0)
-      draw_print_arrays(draw, info->mode, info->start, MIN2(info->count, 20));
+      draw_print_arrays(draw, info->mode, info->start, MIN2(count, 20));
 
    index_limit = util_draw_max_index(draw->pt.vertex_buffer,
                                      draw->pt.vertex_element,
                                      draw->pt.nr_vertex_elements,
                                      info);
-
-   if (index_limit == 0) {
+#if HAVE_LLVM
+   if (!draw->llvm)
+#endif
+   {
+      if (index_limit == 0) {
       /* one of the buffers is too small to do any valid drawing */
-      debug_warning("draw: VBO too small to draw anything\n");
-      return;
+         debug_warning("draw: VBO too small to draw anything\n");
+         return;
+      }
    }
 
-   draw->pt.max_index = index_limit - 1;
+   /* If we're collecting stats then make sure we start from scratch */
+   if (draw->collect_statistics) {
+      memset(&draw->statistics, 0, sizeof(draw->statistics));
+   }
 
+   draw->pt.max_index = index_limit - 1;
 
    /*
     * TODO: We could use draw->pt.max_index to further narrow
@@ -518,12 +533,26 @@ draw_vbo(struct draw_context *draw,
 
    for (instance = 0; instance < info->instance_count; instance++) {
       draw->instance_id = instance + info->start_instance;
+      draw->start_instance = info->start_instance;
+      /* check for overflow */
+      if (draw->instance_id < instance ||
+          draw->instance_id < info->start_instance) {
+         /* if we overflown just set the instance id to the max */
+         draw->instance_id = 0xffffffff;
+      }
+
+      draw_new_instance(draw);
 
       if (info->primitive_restart) {
          draw_pt_arrays_restart(draw, info);
       }
       else {
-         draw_pt_arrays(draw, info->mode, info->start, info->count);
+         draw_pt_arrays(draw, info->mode, info->start, count);
       }
    }
+
+   /* If requested emit the pipeline statistics for this run */
+   if (draw->collect_statistics) {
+      draw->render->pipeline_statistics(draw->render, &draw->statistics);
+   }
 }