nir: Take a shader and variable mode in nir_assign_io_var_locations
[mesa.git] / src / mesa / vbo / vbo_exec.c
index f9cf8355ed453ee83237a1f8987bfbe126d69371..f54eb81fe1779074a234875de85d8874ef0135ee 100644 (file)
@@ -28,7 +28,6 @@
 
 #include "main/glheader.h"
 #include "main/arrayobj.h"
-#include "main/mtypes.h"
 #include "main/api_arrayelt.h"
 #include "main/vtxfmt.h"
 #include "vbo_private.h"
@@ -110,24 +109,17 @@ _vbo_attribute_alias_map[VP_MODE_MAX][VERT_ATTRIB_MAX] = {
 
 
 void
-vbo_exec_init(struct gl_context *ctx)
+vbo_exec_init(struct gl_context *ctx, bool use_buffer_objects)
 {
    struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
 
    exec->ctx = ctx;
 
-   /* aelt_context should have been created by the caller */
-   assert(ctx->aelt_context);
-
-   vbo_exec_vtx_init(exec);
+   vbo_exec_vtx_init(exec, use_buffer_objects);
 
    ctx->Driver.NeedFlush = 0;
    ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
 
-   /* The aelt_context state should still be dirty from its creation */
-   assert(_ae_is_state_dirty(ctx));
-
-   exec->array.recalculate_inputs = GL_TRUE;
    exec->eval.recalculate_maps = GL_TRUE;
 }
 
@@ -136,11 +128,6 @@ void vbo_exec_destroy( struct gl_context *ctx )
 {
    struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
 
-   if (ctx->aelt_context) {
-      _ae_destroy_context( ctx );
-      ctx->aelt_context = NULL;
-   }
-
    vbo_exec_vtx_destroy( exec );
 }
 
@@ -181,21 +168,13 @@ vbo_try_prim_conversion(struct _mesa_prim *p)
 
 
 /**
- * Helper function for determining if two subsequent glBegin/glEnd
- * primitives can be combined.  This is only possible for GL_POINTS,
- * GL_LINES, GL_TRIANGLES and GL_QUADS.
- * If we return true, it means that we can concatenate p1 onto p0 (and
- * discard p1).
+ * Function for merging two subsequent glBegin/glEnd draws.
+ * Return true if p1 was concatenated onto p0 (to discard p1 in the caller).
  */
 bool
-vbo_can_merge_prims(const struct _mesa_prim *p0, const struct _mesa_prim *p1)
+vbo_merge_draws(struct gl_context *ctx, bool in_dlist,
+                struct _mesa_prim *p0, const struct _mesa_prim *p1)
 {
-   if (!p0->begin ||
-       !p1->begin ||
-       !p0->end ||
-       !p1->end)
-      return false;
-
    /* The prim mode must match (ex: both GL_TRIANGLES) */
    if (p0->mode != p1->mode)
       return false;
@@ -204,133 +183,177 @@ vbo_can_merge_prims(const struct _mesa_prim *p0, const struct _mesa_prim *p1)
    if (p0->start + p0->count != p1->start)
       return false;
 
-   if (p0->basevertex != p1->basevertex ||
-       p0->num_instances != p1->num_instances ||
-       p0->base_instance != p1->base_instance)
-      return false;
-
-   /* can always merge subsequent GL_POINTS primitives */
-   if (p0->mode == GL_POINTS)
-      return true;
-
-   /* independent lines with no extra vertices */
-   if (p0->mode == GL_LINES && p0->count % 2 == 0 && p1->count % 2 == 0)
-      return true;
-
-   /* independent tris */
-   if (p0->mode == GL_TRIANGLES && p0->count % 3 == 0 && p1->count % 3 == 0)
-      return true;
-
-   /* independent quads */
-   if (p0->mode == GL_QUADS && p0->count % 4 == 0 && p1->count % 4 == 0)
-      return true;
-
-   return false;
-}
-
+   /* This checks whether mode is equal to any line primitive type, taking
+    * advantage of the fact that primitives types go from 0 to 14.
+    */
+   if ((1 << p0->mode) &
+       ((1 << GL_LINES) |
+        (1 << GL_LINE_LOOP) |
+        (1 << GL_LINE_STRIP) |
+        (1 << GL_LINES_ADJACENCY) |
+        (1 << GL_LINE_STRIP_ADJACENCY))) {
+      /* "begin" resets the line stipple pattern during line stipple emulation
+       * in tnl.
+       *
+       * StippleFlag can be unknown when compiling a display list.
+       *
+       * Other uses of "begin" are internal to the vbo module, and in those
+       * cases, "begin" is not used after merging draws.
+       */
+      if (p1->begin == 1 && (in_dlist || ctx->Line.StippleFlag))
+         return false;
+
+      /* _mesa_prim::end is irrelevant at this point and is only used
+       * before this function is called.
+       */
+   }
 
-/**
- * If we've determined that p0 and p1 can be merged, this function
- * concatenates p1 onto p0.
- */
-void
-vbo_merge_prims(struct _mesa_prim *p0, const struct _mesa_prim *p1)
-{
-   assert(vbo_can_merge_prims(p0, p1));
+   assert(p0->basevertex == p1->basevertex);
+
+   switch (p0->mode) {
+   case GL_POINTS:
+      /* can always merge subsequent GL_POINTS primitives */
+      break;
+   /* check independent primitives with no extra vertices */
+   case GL_LINES:
+      if (p0->count % 2)
+         return false;
+      break;
+   case GL_TRIANGLES:
+      if (p0->count % 3)
+         return false;
+      break;
+   case GL_QUADS:
+   case GL_LINES_ADJACENCY:
+      if (p0->count % 4)
+         return false;
+      break;
+   case GL_TRIANGLES_ADJACENCY:
+      if (p0->count % 6)
+         return false;
+      break;
+   case GL_PATCHES:
+      /* "patch_vertices" can be unknown when compiling a display list. */
+      if (in_dlist ||
+          p0->count % ctx->TessCtrlProgram.patch_vertices)
+         return false;
+      break;
+   default:
+      return false;
+   }
 
+   /* Merge draws. */
    p0->count += p1->count;
    p0->end = p1->end;
+   return true;
 }
 
-
-void
-_vbo_set_recalculate_inputs(struct gl_context *ctx)
-{
-   vbo_context(ctx)->exec.array.recalculate_inputs = GL_TRUE;
-}
-
-
-void
-_vbo_init_inputs(struct vbo_inputs *inputs)
-{
-   inputs->current = 0;
-   inputs->vertex_processing_mode = VP_MODE_FF;
-}
-
-
-/**
- * Update the vbo_inputs's arrays to point to the vao->_VertexArray arrays
- * according to the 'enable' bitmask.
- * \param enable  bitfield of VERT_BIT_x flags.
- */
-static inline void
-update_vao_inputs(struct gl_context *ctx,
-                  struct vbo_inputs *inputs, GLbitfield enable)
-{
-   const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO;
-
-   /* Make sure we process only arrays enabled in the VAO */
-   assert((enable & ~_mesa_get_vao_vp_inputs(vao)) == 0);
-
-   /* Fill in the client arrays from the VAO */
-   const GLubyte *const map = _mesa_vao_attribute_map[vao->_AttributeMapMode];
-   const struct gl_array_attributes *attribs = &vao->VertexAttrib[0];
-   const struct gl_vertex_buffer_binding *bindings = &vao->BufferBinding[0];
-   while (enable) {
-      const int attr = u_bit_scan(&enable);
-      struct gl_vertex_array *input = &inputs->inputs[attr];
-      const struct gl_array_attributes *attrib = &attribs[map[attr]];
-      input->VertexAttrib = attrib;
-      input->BufferBinding = &bindings[attrib->BufferBindingIndex];
-   }
-}
-
-
 /**
- * Update the vbo_inputs's arrays to point to the vbo->currval arrays
- * according to the 'current' bitmask.
- * \param current  bitfield of VERT_BIT_x flags.
+ * Copy zero, one or two vertices from the current vertex buffer into
+ * the temporary "copy" buffer.
+ * This is used when a single primitive overflows a vertex buffer and
+ * we need to continue the primitive in a new vertex buffer.
+ * The temporary "copy" buffer holds the vertices which need to get
+ * copied from the old buffer to the new one.
  */
-static inline void
-update_current_inputs(struct gl_context *ctx,
-                      struct vbo_inputs *inputs, GLbitfield current)
+unsigned
+vbo_copy_vertices(struct gl_context *ctx,
+                  GLenum mode,
+                  struct _mesa_prim *last_prim,
+                  unsigned vertex_size,
+                  bool in_dlist,
+                  fi_type *dst,
+                  const fi_type *src)
 {
-   gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode;
-
-   /* All previously non current array pointers need update. */
-   GLbitfield mask = current & ~inputs->current;
-   /* On mode change, the slots aliasing with materials need update too */
-   if (mode != inputs->vertex_processing_mode)
-      mask |= current & VERT_BIT_MAT_ALL;
-
-   struct vbo_context *vbo = vbo_context(ctx);
-   const struct gl_array_attributes *const currval = &vbo->current[0];
-   const GLubyte *const map = _vbo_attribute_alias_map[mode];
-   while (mask) {
-      const int attr = u_bit_scan(&mask);
-      struct gl_vertex_array *input = &inputs->inputs[attr];
-      input->VertexAttrib = &currval[map[attr]];
-      input->BufferBinding = &vbo->binding;
+   const unsigned count = last_prim->count;
+   unsigned copy = 0;
+
+   switch (mode) {
+   case GL_POINTS:
+      return 0;
+   case GL_LINES:
+      copy = count % 2;
+      break;
+   case GL_TRIANGLES:
+      copy = count % 3;
+      break;
+   case GL_QUADS:
+   case GL_LINES_ADJACENCY:
+      copy = count % 4;
+      break;
+   case GL_TRIANGLES_ADJACENCY:
+      copy = count % 6;
+      break;
+   case GL_LINE_STRIP:
+      copy = MIN2(1, count);
+      break;
+   case GL_LINE_STRIP_ADJACENCY:
+      /* We need to copy 3 vertices, because:
+       *    Last strip:  ---o---o---x     (last line)
+       *    Next strip:     x---o---o---  (next line)
+       */
+      copy = MIN2(3, count);
+      break;
+   case GL_PATCHES:
+      if (in_dlist) {
+         /* We don't know the value of GL_PATCH_VERTICES when compiling
+          * a display list.
+          *
+          * Fail an assertion in debug builds and use the value of 3
+          * in release builds, which is more likely than any other value.
+          */
+         assert(!"patch_vertices is unknown");
+         copy = count % 3;
+      } else {
+         copy = count % ctx->TessCtrlProgram.patch_vertices;
+      }
+      break;
+   case GL_LINE_LOOP:
+      if (!in_dlist && last_prim->begin == 0) {
+         /* We're dealing with the second or later section of a split/wrapped
+          * GL_LINE_LOOP.  Since we're converting line loops to line strips,
+          * we've already incremented the last_prim->start counter by one to
+          * skip the 0th vertex in the loop.  We need to undo that (effectively
+          * subtract one from last_prim->start) so that we copy the 0th vertex
+          * to the next vertex buffer.
+          */
+         assert(last_prim->start > 0);
+         src -= vertex_size;
+      }
+      /* fall-through */
+   case GL_TRIANGLE_FAN:
+   case GL_POLYGON:
+      if (count == 0) {
+         return 0;
+      } else if (count == 1) {
+         memcpy(dst, src + 0, vertex_size * sizeof(GLfloat));
+         return 1;
+      } else {
+         memcpy(dst, src + 0, vertex_size * sizeof(GLfloat));
+         memcpy(dst + vertex_size, src + (count - 1) * vertex_size,
+                vertex_size * sizeof(GLfloat));
+         return 2;
+      }
+   case GL_TRIANGLE_STRIP:
+      /* Draw an even number of triangles to keep front/back facing the same. */
+      last_prim->count -= count % 2;
+      /* fallthrough */
+   case GL_QUAD_STRIP:
+      if (count <= 1)
+         copy = count;
+      else
+         copy = 2 + (count % 2);
+      break;
+   case PRIM_OUTSIDE_BEGIN_END:
+      return 0;
+   case GL_TRIANGLE_STRIP_ADJACENCY:
+      /* TODO: Splitting tri strips with adjacency is too complicated. */
+   default:
+      unreachable("Unexpected primitive type");
+      return 0;
    }
 
-   inputs->current = current;
-   inputs->vertex_processing_mode = mode;
-}
-
-
-/**
- * Update the vbo_inputs's arrays to point to the vao->_VertexArray and
- * vbo->currval arrays according to Array._DrawVAO and
- * Array._DrawVAOEnableAttribs.
- */
-void
-_vbo_update_inputs(struct gl_context *ctx, struct vbo_inputs *inputs)
-{
-   const GLbitfield enable = ctx->Array._DrawVAOEnabledAttribs;
-
-   /* Update array input pointers */
-   update_vao_inputs(ctx, inputs, enable);
-
-   /* The rest must be current inputs. */
-   update_current_inputs(ctx, inputs, ~enable & VERT_BIT_ALL);
+   memcpy(dst, src + (count - copy) * vertex_size,
+          copy * vertex_size * sizeof(GLfloat));
+   return copy;
 }