mesa: Rename _mesa_lookup_enum_by_nr() to _mesa_enum_to_string().
[mesa.git] / src / mesa / main / queryobj.c
index e00ab9476fc58ec49a49ec3049d77dcf9b426ce0..e5c6f9ae31efe2f900119510fb00f606aebaaebc 100644 (file)
@@ -146,12 +146,13 @@ static struct gl_query_object **
 get_pipe_stats_binding_point(struct gl_context *ctx,
                              GLenum target)
 {
+   const int which = target - GL_VERTICES_SUBMITTED_ARB;
+   assert(which < MAX_PIPELINE_STATISTICS);
+
    if (!_mesa_is_desktop_gl(ctx) ||
        !ctx->Extensions.ARB_pipeline_statistics_query)
       return NULL;
 
-   const int which = target - GL_VERTICES_SUBMITTED_ARB;
-   assert(which < MAX_PIPELINE_STATISTICS);
    return &ctx->Query.pipeline_stats[which];
 }
 
@@ -232,18 +233,22 @@ get_query_binding_point(struct gl_context *ctx, GLenum target, GLuint index)
    }
 }
 
-
-void GLAPIENTRY
-_mesa_GenQueries(GLsizei n, GLuint *ids)
+/**
+ * Create $n query objects and store them in *ids. Make them of type $target
+ * if dsa is set. Called from _mesa_GenQueries() and _mesa_CreateQueries().
+ */
+static void
+create_queries(struct gl_context *ctx, GLenum target, GLsizei n, GLuint *ids,
+               bool dsa)
 {
+   const char *func = dsa ? "glGenQueries" : "glCreateQueries";
    GLuint first;
-   GET_CURRENT_CONTEXT(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
-      _mesa_debug(ctx, "glGenQueries(%d)\n", n);
+      _mesa_debug(ctx, "%s(%d)\n", func, n);
 
    if (n < 0) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
       return;
    }
 
@@ -254,8 +259,12 @@ _mesa_GenQueries(GLsizei n, GLuint *ids)
          struct gl_query_object *q
             = ctx->Driver.NewQueryObject(ctx, first + i);
          if (!q) {
-            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
+            _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
             return;
+         } else if (dsa) {
+            /* Do the equivalent of binding the buffer with a target */
+            q->Target = target;
+            q->EverBound = GL_TRUE;
          }
          ids[i] = first + i;
          _mesa_HashInsert(ctx->Query.QueryObjects, first + i, q);
@@ -263,6 +272,36 @@ _mesa_GenQueries(GLsizei n, GLuint *ids)
    }
 }
 
+void GLAPIENTRY
+_mesa_GenQueries(GLsizei n, GLuint *ids)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   create_queries(ctx, 0, n, ids, false);
+}
+
+void GLAPIENTRY
+_mesa_CreateQueries(GLenum target, GLsizei n, GLuint *ids)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   switch (target) {
+   case GL_SAMPLES_PASSED:
+   case GL_ANY_SAMPLES_PASSED:
+   case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
+   case GL_TIME_ELAPSED:
+   case GL_TIMESTAMP:
+   case GL_PRIMITIVES_GENERATED:
+   case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
+      break;
+   default:
+      _mesa_error(ctx, GL_INVALID_ENUM, "glCreateQueries(invalid target = %s)",
+                  _mesa_enum_to_string(target));
+      return;
+   }
+
+   create_queries(ctx, target, n, ids, true);
+}
+
 
 void GLAPIENTRY
 _mesa_DeleteQueries(GLsizei n, const GLuint *ids)
@@ -351,7 +390,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;
@@ -373,7 +412,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;
    }
 
@@ -405,8 +444,36 @@ _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
                      "glBeginQuery{Indexed}(query already active)");
          return;
       }
+
+      /* Section 2.14 Asynchronous Queries, page 84 of the OpenGL ES 3.0.4
+       * spec states:
+       *
+       *     "BeginQuery generates an INVALID_OPERATION error if any of the
+       *      following conditions hold: [...] id is the name of an
+       *      existing query object whose type does not match target; [...]
+       *
+       * Similar wording exists in the OpenGL 4.5 spec, section 4.2. QUERY
+       * OBJECTS AND ASYNCHRONOUS QUERIES, page 43.
+       */
+      if (q->EverBound && q->Target != target) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBeginQuery{Indexed}(target mismatch)");
+         return;
+      }
    }
 
+   /* This possibly changes the target of a buffer allocated by
+    * CreateQueries. Issue 39) in the ARB_direct_state_access extension states
+    * the following:
+    *
+    * "CreateQueries adds a <target>, so strictly speaking the <target>
+    * command isn't needed for BeginQuery/EndQuery, but in the end, this also
+    * isn't a selector, so we decided not to change it."
+    *
+    * Updating the target of the query object should be acceptable, so let's
+    * do that.
+    */
+
    q->Target = target;
    q->Active = GL_TRUE;
    q->Result = 0;
@@ -429,7 +496,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;
@@ -449,8 +516,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;
    }
 
@@ -486,7 +553,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) {
@@ -524,6 +591,18 @@ _mesa_QueryCounter(GLuint id, GLenum target)
       return;
    }
 
+   /* This possibly changes the target of a buffer allocated by
+    * CreateQueries. Issue 39) in the ARB_direct_state_access extension states
+    * the following:
+    *
+    * "CreateQueries adds a <target>, so strictly speaking the <target>
+    * command isn't needed for BeginQuery/EndQuery, but in the end, this also
+    * isn't a selector, so we decided not to change it."
+    *
+    * Updating the target of the query object should be acceptable, so let's
+    * do that.
+    */
+
    q->Target = target;
    q->Result = 0;
    q->Ready = GL_FALSE;
@@ -549,9 +628,9 @@ _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;
@@ -633,7 +712,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;
          }
@@ -661,7 +740,7 @@ _mesa_GetQueryObjectiv(GLuint id, GLenum pname, GLint *params)
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryObjectiv(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(pname));
+                  _mesa_enum_to_string(pname));
 
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
@@ -693,10 +772,13 @@ _mesa_GetQueryObjectiv(GLuint id, GLenum pname, GLint *params)
          }
          break;
       case GL_QUERY_RESULT_AVAILABLE_ARB:
-        if (!q->Ready)
-           ctx->Driver.CheckQuery( ctx, q );
+         if (!q->Ready)
+            ctx->Driver.CheckQuery( ctx, q );
          *params = q->Ready;
          break;
+      case GL_QUERY_TARGET:
+         *params = q->Target;
+         break;
       default:
          _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
          return;
@@ -712,7 +794,7 @@ _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryObjectuiv(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(pname));
+                  _mesa_enum_to_string(pname));
 
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
@@ -744,10 +826,13 @@ _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
          }
          break;
       case GL_QUERY_RESULT_AVAILABLE_ARB:
-        if (!q->Ready)
-           ctx->Driver.CheckQuery( ctx, q );
+         if (!q->Ready)
+            ctx->Driver.CheckQuery( ctx, q );
          *params = q->Ready;
          break;
+      case GL_QUERY_TARGET:
+         *params = q->Target;
+         break;
       default:
          _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
          return;
@@ -766,7 +851,7 @@ _mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryObjecti64v(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(pname));
+                  _mesa_enum_to_string(pname));
 
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
@@ -784,10 +869,13 @@ _mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
          *params = q->Result;
          break;
       case GL_QUERY_RESULT_AVAILABLE_ARB:
-        if (!q->Ready)
-           ctx->Driver.CheckQuery( ctx, q );
+         if (!q->Ready)
+            ctx->Driver.CheckQuery( ctx, q );
          *params = q->Ready;
          break;
+      case GL_QUERY_TARGET:
+         *params = q->Target;
+         break;
       default:
          _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjecti64vARB(pname)");
          return;
@@ -806,7 +894,7 @@ _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryObjectui64v(%u, %s)\n", id,
-                  _mesa_lookup_enum_by_nr(pname));
+                  _mesa_enum_to_string(pname));
 
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
@@ -824,16 +912,58 @@ _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
          *params = q->Result;
          break;
       case GL_QUERY_RESULT_AVAILABLE_ARB:
-        if (!q->Ready)
-           ctx->Driver.CheckQuery( ctx, q );
+         if (!q->Ready)
+            ctx->Driver.CheckQuery( ctx, q );
          *params = q->Ready;
          break;
+      case GL_QUERY_TARGET:
+         *params = q->Target;
+         break;
       default:
          _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectui64vARB(pname)");
          return;
    }
 }
 
+/**
+ * New with GL_ARB_query_buffer_object
+ */
+void GLAPIENTRY
+_mesa_GetQueryBufferObjectiv(GLuint id, GLuint buffer, GLenum pname,
+                             GLintptr offset)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryBufferObjectiv");
+}
+
+
+void GLAPIENTRY
+_mesa_GetQueryBufferObjectuiv(GLuint id, GLuint buffer, GLenum pname,
+                              GLintptr offset)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryBufferObjectuiv");
+}
+
+
+void GLAPIENTRY
+_mesa_GetQueryBufferObjecti64v(GLuint id, GLuint buffer, GLenum pname,
+                               GLintptr offset)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryBufferObjecti64v");
+}
+
+
+void GLAPIENTRY
+_mesa_GetQueryBufferObjectui64v(GLuint id, GLuint buffer, GLenum pname,
+                                GLintptr offset)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryBufferObjectui64v");
+}
+
+
 /**
  * Allocate/init the context state related to query objects.
  */