[Mesa-dev] [PATCH 2/7] mesa: Refactor common validation code to validate_DrawElements_common

Ian Romanick idr at freedesktop.org
Thu Nov 20 11:14:50 PST 2014


From: Ian Romanick <ian.d.romanick at intel.com>

Most of the code in _mesa_validate_DrawElements,
_mesa_validate_DrawRangeElements, and
_mesa_validate_DrawElementsInstanced was the same.  Refactor this out to
common code.

As a side-effect, a bug in _mesa_validate_DrawElementsInstanced was
fixed.  Previously this function would not generate an error when
check_valid_to_render failed if numInstances was 0.

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/mesa/main/api_validate.c | 168 +++++++++++--------------------------------
 1 file changed, 43 insertions(+), 125 deletions(-)

diff --git a/src/mesa/main/api_validate.c b/src/mesa/main/api_validate.c
index 006fca4..276aab7 100644
--- a/src/mesa/main/api_validate.c
+++ b/src/mesa/main/api_validate.c
@@ -318,18 +318,12 @@ valid_elements_type(struct gl_context *ctx, GLenum type, const char *name)
    }
 }
 
-/**
- * Error checking for glDrawElements().  Includes parameter checking
- * and VBO bounds checking.
- * \return GL_TRUE if OK to render, GL_FALSE if error found
- */
-GLboolean
-_mesa_validate_DrawElements(struct gl_context *ctx,
-			    GLenum mode, GLsizei count, GLenum type,
-			    const GLvoid *indices, GLint basevertex)
+static bool
+validate_DrawElements_common(struct gl_context *ctx,
+                             GLenum mode, GLsizei count, GLenum type,
+                             const GLvoid *indices,
+                             const char *caller)
 {
-   FLUSH_CURRENT(ctx, 0);
-
    /* From the GLES3 specification, section 2.14.2 (Transform Feedback
     * Primitive Capture):
     *
@@ -339,44 +333,60 @@ _mesa_validate_DrawElements(struct gl_context *ctx,
     */
    if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glDrawElements(transform feedback active)");
-      return GL_FALSE;
+                  "%s(transform feedback active)", caller);
+      return false;
    }
 
    if (count < 0) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
-      return GL_FALSE;
+      _mesa_error(ctx, GL_INVALID_VALUE, "%s(count)", caller);
+      return false;
    }
 
-   if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElements")) {
-      return GL_FALSE;
+   if (!_mesa_valid_prim_mode(ctx, mode, caller)) {
+      return false;
    }
 
-   if (!valid_elements_type(ctx, type, "glDrawElements"))
-      return GL_FALSE;
+   if (!valid_elements_type(ctx, type, caller))
+      return false;
 
-   if (!check_valid_to_render(ctx, "glDrawElements"))
-      return GL_FALSE;
+   if (!check_valid_to_render(ctx, caller))
+      return false;
 
    /* Vertex buffer object tests */
    if (_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
       /* use indices in the buffer object */
       /* make sure count doesn't go outside buffer bounds */
       if (index_bytes(type, count) > ctx->Array.VAO->IndexBufferObj->Size) {
-         _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
-         return GL_FALSE;
+         _mesa_warning(ctx, "%s index out of buffer bounds", caller);
+         return false;
       }
    }
    else {
       /* not using a VBO */
       if (!indices)
-         return GL_FALSE;
+         return false;
    }
 
    if (count == 0)
-      return GL_FALSE;
+      return false;
 
-   return GL_TRUE;
+   return true;
+}
+
+/**
+ * Error checking for glDrawElements().  Includes parameter checking
+ * and VBO bounds checking.
+ * \return GL_TRUE if OK to render, GL_FALSE if error found
+ */
+GLboolean
+_mesa_validate_DrawElements(struct gl_context *ctx,
+			    GLenum mode, GLsizei count, GLenum type,
+			    const GLvoid *indices, GLint basevertex)
+{
+   FLUSH_CURRENT(ctx, 0);
+
+   return validate_DrawElements_common(ctx, mode, count, type, indices,
+                                       "glDrawElements");
 }
 
 
@@ -451,58 +461,13 @@ _mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
 {
    FLUSH_CURRENT(ctx, 0);
 
-   /* From the GLES3 specification, section 2.14.2 (Transform Feedback
-    * Primitive Capture):
-    *
-    *   The error INVALID_OPERATION is also generated by DrawElements,
-    *   DrawElementsInstanced, and DrawRangeElements while transform feedback
-    *   is active and not paused, regardless of mode.
-    */
-   if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glDrawElements(transform feedback active)");
-      return GL_FALSE;
-   }
-
-   if (count < 0) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
-      return GL_FALSE;
-   }
-
-   if (!_mesa_valid_prim_mode(ctx, mode, "glDrawRangeElements")) {
-      return GL_FALSE;
-   }
-
    if (end < start) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
       return GL_FALSE;
    }
 
-   if (!valid_elements_type(ctx, type, "glDrawRangeElements"))
-      return GL_FALSE;
-
-   if (!check_valid_to_render(ctx, "glDrawRangeElements"))
-      return GL_FALSE;
-
-   /* Vertex buffer object tests */
-   if (_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
-      /* use indices in the buffer object */
-      /* make sure count doesn't go outside buffer bounds */
-      if (index_bytes(type, count) > ctx->Array.VAO->IndexBufferObj->Size) {
-         _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
-         return GL_FALSE;
-      }
-   }
-   else {
-      /* not using a VBO */
-      if (!indices)
-         return GL_FALSE;
-   }
-
-   if (count == 0)
-      return GL_FALSE;
-
-   return GL_TRUE;
+   return validate_DrawElements_common(ctx, mode, count, type, indices,
+                                       "glDrawRangeElements");
 }
 
 
@@ -634,62 +599,15 @@ _mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
 {
    FLUSH_CURRENT(ctx, 0);
 
-   /* From the GLES3 specification, section 2.14.2 (Transform Feedback
-    * Primitive Capture):
-    *
-    *   The error INVALID_OPERATION is also generated by DrawElements,
-    *   DrawElementsInstanced, and DrawRangeElements while transform feedback
-    *   is active and not paused, regardless of mode.
-    */
-   if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glDrawElements(transform feedback active)");
-      return GL_FALSE;
-   }
-
-   if (count < 0) {
+   if (numInstances < 0) {
       _mesa_error(ctx, GL_INVALID_VALUE,
-                  "glDrawElementsInstanced(count=%d)", count);
+                  "glDrawElementsInstanced(numInstances=%d)", numInstances);
       return GL_FALSE;
    }
 
-   if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElementsInstanced")) {
-      return GL_FALSE;
-   }
-
-   if (!valid_elements_type(ctx, type, "glDrawElementsInstanced"))
-      return GL_FALSE;
-
-   if (numInstances <= 0) {
-      if (numInstances < 0)
-         _mesa_error(ctx, GL_INVALID_VALUE,
-                     "glDrawElementsInstanced(numInstances=%d)", numInstances);
-      return GL_FALSE;
-   }
-
-   if (!check_valid_to_render(ctx, "glDrawElementsInstanced"))
-      return GL_FALSE;
-
-   /* Vertex buffer object tests */
-   if (_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
-      /* use indices in the buffer object */
-      /* make sure count doesn't go outside buffer bounds */
-      if (index_bytes(type, count) > ctx->Array.VAO->IndexBufferObj->Size) {
-         _mesa_warning(ctx,
-                       "glDrawElementsInstanced index out of buffer bounds");
-         return GL_FALSE;
-      }
-   }
-   else {
-      /* not using a VBO */
-      if (!indices)
-         return GL_FALSE;
-   }
-
-   if (count == 0)
-      return GL_FALSE;
-
-   return GL_TRUE;
+   return validate_DrawElements_common(ctx, mode, count, type, indices,
+                                       "glDrawElementsInstanced")
+      && (numInstances > 0);
 }
 
 
-- 
1.8.1.4



More information about the mesa-dev mailing list