mesa: Remove unnecessary header.
[mesa.git] / src / mesa / main / api_validate.c
index 4a1448deee6519f5112f84973bef230337ad4a5d..4fb7b5ad61f92fd5d38a812e252edfabfa4b2522 100644 (file)
@@ -28,7 +28,6 @@
 #include "context.h"
 #include "imports.h"
 #include "mtypes.h"
-#include "state.h"
 #include "vbo/vbo.h"
 
 
@@ -51,6 +50,51 @@ index_bytes(GLenum type, GLsizei count)
 }
 
 
+/**
+ * Find the max index in the given element/index buffer
+ */
+GLuint
+_mesa_max_buffer_index(GLcontext *ctx, GLuint count, GLenum type,
+                       const void *indices,
+                       struct gl_buffer_object *elementBuf)
+{
+   const GLubyte *map = NULL;
+   GLuint max = 0;
+   GLuint i;
+
+   if (_mesa_is_bufferobj(elementBuf)) {
+      /* elements are in a user-defined buffer object.  need to map it */
+      map = ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER,
+                                  GL_READ_ONLY, elementBuf);
+      /* Actual address is the sum of pointers */
+      indices = (const GLvoid *) ADD_POINTERS(map, (const GLubyte *) indices);
+   }
+
+   if (type == GL_UNSIGNED_INT) {
+      for (i = 0; i < count; i++)
+         if (((GLuint *) indices)[i] > max)
+            max = ((GLuint *) indices)[i];
+   }
+   else if (type == GL_UNSIGNED_SHORT) {
+      for (i = 0; i < count; i++)
+         if (((GLushort *) indices)[i] > max)
+            max = ((GLushort *) indices)[i];
+   }
+   else {
+      ASSERT(type == GL_UNSIGNED_BYTE);
+      for (i = 0; i < count; i++)
+         if (((GLubyte *) indices)[i] > max)
+            max = ((GLubyte *) indices)[i];
+   }
+
+   if (map) {
+      ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, elementBuf);
+   }
+
+   return max;
+}
+
+
 /**
  * Check if OK to draw arrays/elements.
  */
@@ -79,6 +123,12 @@ check_valid_to_render(GLcontext *ctx, const char *function)
    return GL_TRUE;
 }
 
+
+/**
+ * Do bounds checking on array element indexes.  Check that the vertices
+ * pointed to by the indices don't lie outside buffer object bounds.
+ * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
+ */
 static GLboolean
 check_index_bounds(GLcontext *ctx, GLsizei count, GLenum type,
                   const GLvoid *indices, GLint basevertex)
@@ -103,17 +153,18 @@ check_index_bounds(GLcontext *ctx, GLsizei count, GLenum type,
 
    vbo_get_minmax_index(ctx, &prim, &ib, &min, &max);
 
-   if (min + basevertex < 0 ||
+   if ((int)(min + basevertex) < 0 ||
        max + basevertex > ctx->Array.ArrayObj->_MaxElement) {
       /* the max element is out of bounds of one or more enabled arrays */
-      _mesa_warning(ctx, "glDrawElements() index=%u is "
-                   "out of bounds (max=%u)", max, ctx->Array.ArrayObj->_MaxElement);
+      _mesa_warning(ctx, "glDrawElements() index=%u is out of bounds (max=%u)",
+                    max, ctx->Array.ArrayObj->_MaxElement);
       return GL_FALSE;
    }
 
    return GL_TRUE;
 }
 
+
 /**
  * Error checking for glDrawElements().  Includes parameter checking
  * and VBO bounds checking.
@@ -145,9 +196,6 @@ _mesa_validate_DrawElements(GLcontext *ctx,
       return GL_FALSE;
    }
 
-   if (ctx->NewState)
-      _mesa_update_state(ctx);
-
    if (!check_valid_to_render(ctx, "glDrawElements"))
       return GL_FALSE;
 
@@ -209,9 +257,6 @@ _mesa_validate_DrawRangeElements(GLcontext *ctx, GLenum mode,
       return GL_FALSE;
    }
 
-   if (ctx->NewState)
-      _mesa_update_state(ctx);
-
    if (!check_valid_to_render(ctx, "glDrawRangeElements"))
       return GL_FALSE;
 
@@ -259,9 +304,6 @@ _mesa_validate_DrawArrays(GLcontext *ctx,
       return GL_FALSE;
    }
 
-   if (ctx->NewState)
-      _mesa_update_state(ctx);
-
    if (!check_valid_to_render(ctx, "glDrawArrays"))
       return GL_FALSE;