mesa: enum checks for GL_EXT_occlusion_query_boolean
[mesa.git] / src / mesa / main / queryobj.c
index 19350584a8cda80d20509457d9ab88c69e1eaa52..d966814a768b6f73091ae55fca2eb028cc923753 100644 (file)
@@ -23,6 +23,7 @@
  */
 
 
+#include "bufferobj.h"
 #include "glheader.h"
 #include "context.h"
 #include "enums.h"
@@ -164,6 +165,20 @@ get_pipe_stats_binding_point(struct gl_context *ctx,
 static struct gl_query_object **
 get_query_binding_point(struct gl_context *ctx, GLenum target, GLuint index)
 {
+
+   /* From GL_EXT_occlusion_query_boolean spec:
+    *
+    *    "Accepted by the <target> parameter of BeginQueryEXT, EndQueryEXT,
+    *    and GetQueryivEXT:
+    *
+    *   ANY_SAMPLES_PASSED_EXT                         0x8C2F
+    *   ANY_SAMPLES_PASSED_CONSERVATIVE_EXT            0x8D6A"
+    */
+   if ((_mesa_is_gles(ctx) && ctx->Version == 20) &&
+       (target != GL_ANY_SAMPLES_PASSED &&
+        target != GL_ANY_SAMPLES_PASSED_CONSERVATIVE))
+      return NULL;
+
    switch (target) {
    case GL_SAMPLES_PASSED_ARB:
       if (ctx->Extensions.ARB_occlusion_query)
@@ -196,6 +211,16 @@ get_query_binding_point(struct gl_context *ctx, GLenum target, GLuint index)
          return &ctx->Query.PrimitivesWritten[index];
       else
          return NULL;
+   case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
+      if (ctx->Extensions.ARB_transform_feedback_overflow_query)
+         return &ctx->Query.TransformFeedbackOverflow[index];
+      else
+         return NULL;
+   case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB:
+      if (ctx->Extensions.ARB_transform_feedback_overflow_query)
+         return &ctx->Query.TransformFeedbackOverflowAny;
+      else
+         return NULL;
 
    case GL_VERTICES_SUBMITTED_ARB:
    case GL_PRIMITIVES_SUBMITTED_ARB:
@@ -217,7 +242,7 @@ get_query_binding_point(struct gl_context *ctx, GLenum target, GLuint index)
 
    case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
    case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
-      if (ctx->Extensions.ARB_tessellation_shader)
+      if (_mesa_has_tessellation(ctx))
          return get_pipe_stats_binding_point(ctx, target);
       else
          return NULL;
@@ -267,7 +292,7 @@ create_queries(struct gl_context *ctx, GLenum target, GLsizei n, GLuint *ids,
             q->EverBound = GL_TRUE;
          }
          ids[i] = first + i;
-         _mesa_HashInsert(ctx->Query.QueryObjects, first + i, q);
+         _mesa_HashInsertLocked(ctx->Query.QueryObjects, first + i, q);
       }
    }
 }
@@ -292,10 +317,12 @@ _mesa_CreateQueries(GLenum target, GLsizei n, GLuint *ids)
    case GL_TIMESTAMP:
    case GL_PRIMITIVES_GENERATED:
    case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
+   case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
+   case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB:
       break;
    default:
       _mesa_error(ctx, GL_INVALID_ENUM, "glCreateQueries(invalid target = %s)",
-                  _mesa_lookup_enum_by_nr(target));
+                  _mesa_enum_to_string(target));
       return;
    }
 
@@ -332,7 +359,7 @@ _mesa_DeleteQueries(GLsizei n, const GLuint *ids)
                q->Active = GL_FALSE;
                ctx->Driver.EndQuery(ctx, q);
             }
-            _mesa_HashRemove(ctx->Query.QueryObjects, ids[i]);
+            _mesa_HashRemoveLocked(ctx->Query.QueryObjects, ids[i]);
             ctx->Driver.DeleteQuery(ctx, q);
          }
       }
@@ -367,6 +394,7 @@ query_error_check_index(struct gl_context *ctx, GLenum target, GLuint index)
    switch (target) {
    case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
    case GL_PRIMITIVES_GENERATED:
+   case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
       if (index >= ctx->Const.MaxVertexStreams) {
          _mesa_error(ctx, GL_INVALID_VALUE,
                      "glBeginQueryIndexed(index>=MaxVertexStreams)");
@@ -390,7 +418,7 @@ _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glBeginQueryIndexed(%s, %u, %u)\n",
-                  _mesa_lookup_enum_by_nr(target), index, id);
+                  _mesa_enum_to_string(target), index, id);
 
    if (!query_error_check_index(ctx, target, index))
       return;
@@ -412,7 +440,7 @@ _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
    if (*bindpt) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glBeginQuery{Indexed}(target=%s is active)",
-                  _mesa_lookup_enum_by_nr(target));
+                  _mesa_enum_to_string(target));
       return;
    }
 
@@ -434,7 +462,7 @@ _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQuery{Indexed}");
             return;
          }
-         _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
+         _mesa_HashInsertLocked(ctx->Query.QueryObjects, id, q);
       }
    }
    else {
@@ -496,7 +524,7 @@ _mesa_EndQueryIndexed(GLenum target, GLuint index)
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glEndQueryIndexed(%s, %u)\n",
-                  _mesa_lookup_enum_by_nr(target), index);
+                  _mesa_enum_to_string(target), index);
 
    if (!query_error_check_index(ctx, target, index))
       return;
@@ -516,8 +544,8 @@ _mesa_EndQueryIndexed(GLenum target, GLuint index)
    if (q && q->Target != target) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glEndQuery(target=%s with active query of target %s)",
-                  _mesa_lookup_enum_by_nr(target),
-                  _mesa_lookup_enum_by_nr(q->Target));
+                  _mesa_enum_to_string(target),
+                  _mesa_enum_to_string(q->Target));
       return;
    }
 
@@ -553,7 +581,7 @@ _mesa_QueryCounter(GLuint id, GLenum target)
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glQueryCounter(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(target));
+                  _mesa_enum_to_string(target));
 
    /* error checking */
    if (target != GL_TIMESTAMP) {
@@ -576,7 +604,7 @@ _mesa_QueryCounter(GLuint id, GLenum target)
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glQueryCounter");
          return;
       }
-      _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
+      _mesa_HashInsertLocked(ctx->Query.QueryObjects, id, q);
    }
    else {
       if (q->Target && q->Target != GL_TIMESTAMP) {
@@ -628,13 +656,26 @@ _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryIndexediv(%s, %u, %s)\n",
-                  _mesa_lookup_enum_by_nr(target),
+                  _mesa_enum_to_string(target),
                   index,
-                  _mesa_lookup_enum_by_nr(pname));
+                  _mesa_enum_to_string(pname));
 
    if (!query_error_check_index(ctx, target, index))
       return;
 
+   /* From the GL_EXT_occlusion_query_boolean spec:
+    *
+    * "The error INVALID_ENUM is generated if GetQueryivEXT is called where
+    * <pname> is not CURRENT_QUERY_EXT."
+    *
+    * Same rule is present also in ES 3.2 spec.
+    */
+   if (_mesa_is_gles(ctx) && pname != GL_CURRENT_QUERY) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivEXT(%s)",
+                  _mesa_enum_to_string(pname));
+      return;
+   }
+
    if (target == GL_TIMESTAMP) {
       if (!ctx->Extensions.ARB_timer_query) {
          _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryARB(target)");
@@ -676,6 +717,14 @@ _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
          case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
             *params = ctx->Const.QueryCounterBits.PrimitivesWritten;
             break;
+         case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
+         case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB:
+            /* The minimum value of this is 1 if it's nonzero, and the value
+             * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
+             * bits.
+             */
+            *params = 1;
+            break;
          case GL_VERTICES_SUBMITTED_ARB:
             *params = ctx->Const.QueryCounterBits.VerticesSubmitted;
             break;
@@ -712,7 +761,7 @@ _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
          default:
             _mesa_problem(ctx,
                           "Unknown target in glGetQueryIndexediv(target = %s)",
-                          _mesa_lookup_enum_by_nr(target));
+                          _mesa_enum_to_string(target));
             *params = 0;
             break;
          }
@@ -732,105 +781,162 @@ _mesa_GetQueryiv(GLenum target, GLenum pname, GLint *params)
    _mesa_GetQueryIndexediv(target, 0, pname, params);
 }
 
-void GLAPIENTRY
-_mesa_GetQueryObjectiv(GLuint id, GLenum pname, GLint *params)
+static void
+get_query_object(struct gl_context *ctx, const char *func,
+                 GLuint id, GLenum pname, GLenum ptype,
+                 struct gl_buffer_object *buf, intptr_t offset)
 {
    struct gl_query_object *q = NULL;
-   GET_CURRENT_CONTEXT(ctx);
+   uint64_t value;
 
    if (MESA_VERBOSE & VERBOSE_API)
-      _mesa_debug(ctx, "glGetQueryObjectiv(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(pname));
+      _mesa_debug(ctx, "%s(%u, %s)\n", func, id,
+                  _mesa_enum_to_string(pname));
 
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
 
    if (!q || q->Active || !q->EverBound) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glGetQueryObjectivARB(id=%d is invalid or active)", id);
+                  "%s(id=%d is invalid or active)", func, id);
       return;
    }
 
+   /* From GL_EXT_occlusion_query_boolean spec:
+    *
+    *    "Accepted by the <pname> parameter of GetQueryObjectivEXT and
+    *    GetQueryObjectuivEXT:
+    *
+    *    QUERY_RESULT_EXT                               0x8866
+    *    QUERY_RESULT_AVAILABLE_EXT                     0x8867"
+    *
+    * Same rule is present also in ES 3.2 spec.
+    */
+   if (_mesa_is_gles(ctx) &&
+       (pname != GL_QUERY_RESULT && pname != GL_QUERY_RESULT_AVAILABLE)) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s)", func,
+                  _mesa_enum_to_string(pname));
+      return;
+   }
+
+   if (buf && buf != ctx->Shared->NullBufferObj) {
+      bool is_64bit = ptype == GL_INT64_ARB ||
+         ptype == GL_UNSIGNED_INT64_ARB;
+      if (!ctx->Extensions.ARB_query_buffer_object) {
+         _mesa_error(ctx, GL_INVALID_OPERATION, "%s(not supported)", func);
+         return;
+      }
+      if (buf->Size < offset + 4 * (is_64bit ? 2 : 1)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION, "%s(out of bounds)", func);
+         return;
+      }
+
+      if (offset < 0) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset is negative)", func);
+         return;
+      }
+
+      switch (pname) {
+      case GL_QUERY_RESULT:
+      case GL_QUERY_RESULT_NO_WAIT:
+      case GL_QUERY_RESULT_AVAILABLE:
+      case GL_QUERY_TARGET:
+         ctx->Driver.StoreQueryResult(ctx, q, buf, offset, pname, ptype);
+         return;
+      }
+
+      /* fall through to get error below */
+   }
+
    switch (pname) {
-      case GL_QUERY_RESULT_ARB:
-         if (!q->Ready)
-            ctx->Driver.WaitQuery(ctx, q);
-         /* if result is too large for returned type, clamp to max value */
-         if (q->Target == GL_ANY_SAMPLES_PASSED
-             || q->Target == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) {
-            if (q->Result)
-               *params = GL_TRUE;
-            else
-               *params = GL_FALSE;
-         } else {
-            if (q->Result > 0x7fffffff) {
-               *params = 0x7fffffff;
-            }
-            else {
-               *params = (GLint)q->Result;
-            }
-         }
-         break;
-      case GL_QUERY_RESULT_AVAILABLE_ARB:
-         if (!q->Ready)
-            ctx->Driver.CheckQuery( ctx, q );
-         *params = q->Ready;
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
+   case GL_QUERY_RESULT:
+      if (!q->Ready)
+         ctx->Driver.WaitQuery(ctx, q);
+      value = q->Result;
+      break;
+   case GL_QUERY_RESULT_NO_WAIT:
+      if (!ctx->Extensions.ARB_query_buffer_object)
+         goto invalid_enum;
+      ctx->Driver.CheckQuery(ctx, q);
+      if (!q->Ready)
          return;
+      value = q->Result;
+      break;
+   case GL_QUERY_RESULT_AVAILABLE:
+      if (!q->Ready)
+         ctx->Driver.CheckQuery(ctx, q);
+      value = q->Ready;
+      break;
+   case GL_QUERY_TARGET:
+      value = q->Target;
+      break;
+   default:
+invalid_enum:
+      _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=%s)",
+                  func, _mesa_enum_to_string(pname));
+      return;
+   }
+
+   switch (ptype) {
+   case GL_INT: {
+      GLint *param = (GLint *)offset;
+      if (value > 0x7fffffff)
+         *param = 0x7fffffff;
+      else
+         *param = value;
+      break;
+   }
+   case GL_UNSIGNED_INT: {
+      GLuint *param = (GLuint *)offset;
+      if (value > 0xffffffff)
+         *param = 0xffffffff;
+      else
+         *param = value;
+      break;
+   }
+   case GL_INT64_ARB:
+   case GL_UNSIGNED_INT64_ARB: {
+      GLuint64EXT *param = (GLuint64EXT *)offset;
+      *param = value;
+      break;
+   }
+   default:
+      unreachable("unexpected ptype");
    }
 }
 
+void GLAPIENTRY
+_mesa_GetQueryObjectiv(GLuint id, GLenum pname, GLint *params)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   get_query_object(ctx, "glGetQueryObjectiv",
+                    id, pname, GL_INT, ctx->QueryBuffer, (intptr_t)params);
+}
+
 
 void GLAPIENTRY
 _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
 {
-   struct gl_query_object *q = NULL;
    GET_CURRENT_CONTEXT(ctx);
 
-   if (MESA_VERBOSE & VERBOSE_API)
-      _mesa_debug(ctx, "glGetQueryObjectuiv(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(pname));
+   get_query_object(ctx, "glGetQueryObjectuiv",
+                    id, pname, GL_UNSIGNED_INT,
+                    ctx->QueryBuffer, (intptr_t)params);
+}
 
-   if (id)
-      q = _mesa_lookup_query_object(ctx, id);
 
-   if (!q || q->Active || !q->EverBound) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glGetQueryObjectuivARB(id=%d is invalid or active)", id);
-      return;
-   }
+/**
+ * New with GL_EXT_timer_query
+ */
+void GLAPIENTRY
+_mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
+{
+   GET_CURRENT_CONTEXT(ctx);
 
-   switch (pname) {
-      case GL_QUERY_RESULT_ARB:
-         if (!q->Ready)
-            ctx->Driver.WaitQuery(ctx, q);
-         /* if result is too large for returned type, clamp to max value */
-         if (q->Target == GL_ANY_SAMPLES_PASSED
-             || q->Target == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) {
-            if (q->Result)
-               *params = GL_TRUE;
-            else
-               *params = GL_FALSE;
-         } else {
-            if (q->Result > 0xffffffff) {
-               *params = 0xffffffff;
-            }
-            else {
-               *params = (GLuint)q->Result;
-            }
-         }
-         break;
-      case GL_QUERY_RESULT_AVAILABLE_ARB:
-         if (!q->Ready)
-            ctx->Driver.CheckQuery( ctx, q );
-         *params = q->Ready;
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
-         return;
-   }
+   get_query_object(ctx, "glGetQueryObjecti64v",
+                    id, pname, GL_INT64_ARB,
+                    ctx->QueryBuffer, (intptr_t)params);
 }
 
 
@@ -838,81 +944,82 @@ _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
  * New with GL_EXT_timer_query
  */
 void GLAPIENTRY
-_mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
+_mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
 {
-   struct gl_query_object *q = NULL;
    GET_CURRENT_CONTEXT(ctx);
 
-   if (MESA_VERBOSE & VERBOSE_API)
-      _mesa_debug(ctx, "glGetQueryObjecti64v(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(pname));
+   get_query_object(ctx, "glGetQueryObjectui64v",
+                    id, pname, GL_UNSIGNED_INT64_ARB,
+                    ctx->QueryBuffer, (intptr_t)params);
+}
 
-   if (id)
-      q = _mesa_lookup_query_object(ctx, id);
+/**
+ * New with GL_ARB_query_buffer_object
+ */
+void GLAPIENTRY
+_mesa_GetQueryBufferObjectiv(GLuint id, GLuint buffer, GLenum pname,
+                             GLintptr offset)
+{
+   struct gl_buffer_object *buf;
+   GET_CURRENT_CONTEXT(ctx);
 
-   if (!q || q->Active || !q->EverBound) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glGetQueryObjectui64vARB(id=%d is invalid or active)", id);
+   buf = _mesa_lookup_bufferobj_err(ctx, buffer, "glGetQueryBufferObjectiv");
+   if (!buf)
       return;
-   }
 
-   switch (pname) {
-      case GL_QUERY_RESULT_ARB:
-         if (!q->Ready)
-            ctx->Driver.WaitQuery(ctx, q);
-         *params = q->Result;
-         break;
-      case GL_QUERY_RESULT_AVAILABLE_ARB:
-         if (!q->Ready)
-            ctx->Driver.CheckQuery( ctx, q );
-         *params = q->Ready;
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjecti64vARB(pname)");
-         return;
-   }
+   get_query_object(ctx, "glGetQueryBufferObjectiv",
+                    id, pname, GL_INT, buf, offset);
 }
 
 
-/**
- * New with GL_EXT_timer_query
- */
 void GLAPIENTRY
-_mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
+_mesa_GetQueryBufferObjectuiv(GLuint id, GLuint buffer, GLenum pname,
+                              GLintptr offset)
 {
-   struct gl_query_object *q = NULL;
+   struct gl_buffer_object *buf;
    GET_CURRENT_CONTEXT(ctx);
 
-   if (MESA_VERBOSE & VERBOSE_API)
-      _mesa_debug(ctx, "glGetQueryObjectui64v(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(pname));
+   buf = _mesa_lookup_bufferobj_err(ctx, buffer, "glGetQueryBufferObjectuiv");
+   if (!buf)
+      return;
 
-   if (id)
-      q = _mesa_lookup_query_object(ctx, id);
+   get_query_object(ctx, "glGetQueryBufferObjectuiv",
+                    id, pname, GL_UNSIGNED_INT, buf, offset);
+}
 
-   if (!q || q->Active || !q->EverBound) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id);
+
+void GLAPIENTRY
+_mesa_GetQueryBufferObjecti64v(GLuint id, GLuint buffer, GLenum pname,
+                               GLintptr offset)
+{
+   struct gl_buffer_object *buf;
+   GET_CURRENT_CONTEXT(ctx);
+
+   buf = _mesa_lookup_bufferobj_err(ctx, buffer, "glGetQueryBufferObjecti64v");
+   if (!buf)
       return;
-   }
 
-   switch (pname) {
-      case GL_QUERY_RESULT_ARB:
-         if (!q->Ready)
-            ctx->Driver.WaitQuery(ctx, q);
-         *params = q->Result;
-         break;
-      case GL_QUERY_RESULT_AVAILABLE_ARB:
-         if (!q->Ready)
-            ctx->Driver.CheckQuery( ctx, q );
-         *params = q->Ready;
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectui64vARB(pname)");
-         return;
-   }
+   get_query_object(ctx, "glGetQueryBufferObjecti64v",
+                    id, pname, GL_INT64_ARB, buf, offset);
 }
 
+
+void GLAPIENTRY
+_mesa_GetQueryBufferObjectui64v(GLuint id, GLuint buffer, GLenum pname,
+                                GLintptr offset)
+{
+   struct gl_buffer_object *buf;
+   GET_CURRENT_CONTEXT(ctx);
+
+   buf = _mesa_lookup_bufferobj_err(ctx, buffer, "glGetQueryBufferObjectui64v");
+   if (!buf)
+      return;
+
+   get_query_object(ctx, "glGetQueryBufferObjectui64v",
+                    id, pname, GL_UNSIGNED_INT64_ARB, buf, offset);
+}
+
+
 /**
  * Allocate/init the context state related to query objects.
  */