mesa: remove unneeded #include of colormac.h
[mesa.git] / src / mesa / main / queryobj.c
index 60356b85d356c2fe04d11c479254753bf11c7b39..fbccf3fe6f415ee65d2509af67e0999ad71a2647 100644 (file)
@@ -43,7 +43,7 @@
 static struct gl_query_object *
 _mesa_new_query_object(struct gl_context *ctx, GLuint id)
 {
-   struct gl_query_object *q = MALLOC_STRUCT(gl_query_object);
+   struct gl_query_object *q = CALLOC_STRUCT(gl_query_object);
    (void) ctx;
    if (q) {
       q->Id = id;
@@ -126,6 +126,7 @@ _mesa_check_query(struct gl_context *ctx, struct gl_query_object *q)
 static void
 _mesa_delete_query(struct gl_context *ctx, struct gl_query_object *q)
 {
+   free(q->Label);
    free(q);
 }
 
@@ -141,13 +142,27 @@ _mesa_init_query_object_functions(struct dd_function_table *driver)
    driver->CheckQuery = _mesa_check_query;
 }
 
+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;
+
+   return &ctx->Query.pipeline_stats[which];
+}
 
 /**
- * Return pointer to the query object binding point for the given target.
+ * Return pointer to the query object binding point for the given target and
+ * index.
  * \return NULL if invalid target, else the address of binding point
  */
 static struct gl_query_object **
-get_query_binding_point(struct gl_context *ctx, GLenum target)
+get_query_binding_point(struct gl_context *ctx, GLenum target, GLuint index)
 {
    switch (target) {
    case GL_SAMPLES_PASSED_ARB:
@@ -173,38 +188,67 @@ get_query_binding_point(struct gl_context *ctx, GLenum target)
          return NULL;
    case GL_PRIMITIVES_GENERATED:
       if (ctx->Extensions.EXT_transform_feedback)
-         return &ctx->Query.PrimitivesGenerated;
+         return &ctx->Query.PrimitivesGenerated[index];
       else
          return NULL;
    case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
       if (ctx->Extensions.EXT_transform_feedback)
-         return &ctx->Query.PrimitivesWritten;
+         return &ctx->Query.PrimitivesWritten[index];
+      else
+         return NULL;
+
+   case GL_VERTICES_SUBMITTED_ARB:
+   case GL_PRIMITIVES_SUBMITTED_ARB:
+   case GL_VERTEX_SHADER_INVOCATIONS_ARB:
+   case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
+   case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
+   case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
+         return get_pipe_stats_binding_point(ctx, target);
+
+   case GL_GEOMETRY_SHADER_INVOCATIONS:
+      /* GL_GEOMETRY_SHADER_INVOCATIONS is defined in a non-sequential order */
+      target = GL_VERTICES_SUBMITTED_ARB + MAX_PIPELINE_STATISTICS - 1;
+      /* fallthrough */
+   case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
+      if (_mesa_has_geometry_shaders(ctx))
+         return get_pipe_stats_binding_point(ctx, target);
       else
          return NULL;
+
+   case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
+   case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
+      if (ctx->Extensions.ARB_tessellation_shader)
+         return get_pipe_stats_binding_point(ctx, target);
+      else
+         return NULL;
+
+   case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
+      if (_mesa_has_compute_shaders(ctx))
+         return get_pipe_stats_binding_point(ctx, target);
+      else
+         return NULL;
+
    default:
       return NULL;
    }
 }
 
-
-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)");
-      return;
-   }
-
-   /* No query objects can be active at this time! */
-   if (ctx->Query.CurrentOcclusionObject ||
-       ctx->Query.CurrentTimerObject) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glGenQueriesARB");
+      _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
       return;
    }
 
@@ -215,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);
@@ -224,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_lookup_enum_by_nr(target));
+      return;
+   }
+
+   create_queries(ctx, target, n, ids, true);
+}
+
 
 void GLAPIENTRY
 _mesa_DeleteQueries(GLsizei n, const GLuint *ids)
@@ -240,18 +318,20 @@ _mesa_DeleteQueries(GLsizei n, const GLuint *ids)
       return;
    }
 
-   /* No query objects can be active at this time! */
-   if (ctx->Query.CurrentOcclusionObject ||
-       ctx->Query.CurrentTimerObject) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
-      return;
-   }
-
    for (i = 0; i < n; i++) {
       if (ids[i] > 0) {
          struct gl_query_object *q = _mesa_lookup_query_object(ctx, ids[i]);
          if (q) {
-            ASSERT(!q->Active); /* should be caught earlier */
+            if (q->Active) {
+               struct gl_query_object **bindpt;
+               bindpt = get_query_binding_point(ctx, q->Target, q->Stream);
+               assert(bindpt); /* Should be non-null for active q. */
+               if (bindpt) {
+                  *bindpt = NULL;
+               }
+               q->Active = GL_FALSE;
+               ctx->Driver.EndQuery(ctx, q);
+            }
             _mesa_HashRemove(ctx->Query.QueryObjects, ids[i]);
             ctx->Driver.DeleteQuery(ctx, q);
          }
@@ -317,7 +397,7 @@ _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
 
    FLUSH_VERTICES(ctx, 0);
 
-   bindpt = get_query_binding_point(ctx, target);
+   bindpt = get_query_binding_point(ctx, target, index);
    if (!bindpt) {
       _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQuery{Indexed}(target)");
       return;
@@ -364,13 +444,42 @@ _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;
    q->Ready = GL_FALSE;
    q->EverBound = GL_TRUE;
+   q->Stream = index;
 
    /* XXX should probably refcount query objects */
    *bindpt = q;
@@ -394,7 +503,7 @@ _mesa_EndQueryIndexed(GLenum target, GLuint index)
 
    FLUSH_VERTICES(ctx, 0);
 
-   bindpt = get_query_binding_point(ctx, target);
+   bindpt = get_query_binding_point(ctx, target, index);
    if (!bindpt) {
       _mesa_error(ctx, GL_INVALID_ENUM, "glEndQuery{Indexed}(target)");
       return;
@@ -482,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;
@@ -521,7 +642,7 @@ _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
       }
    }
    else {
-      bindpt = get_query_binding_point(ctx, target);
+      bindpt = get_query_binding_point(ctx, target, index);
       if (!bindpt) {
          _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(target)");
          return;
@@ -555,6 +676,39 @@ _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
          case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
             *params = ctx->Const.QueryCounterBits.PrimitivesWritten;
             break;
+         case GL_VERTICES_SUBMITTED_ARB:
+            *params = ctx->Const.QueryCounterBits.VerticesSubmitted;
+            break;
+         case GL_PRIMITIVES_SUBMITTED_ARB:
+            *params = ctx->Const.QueryCounterBits.PrimitivesSubmitted;
+            break;
+         case GL_VERTEX_SHADER_INVOCATIONS_ARB:
+            *params = ctx->Const.QueryCounterBits.VsInvocations;
+            break;
+         case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
+            *params = ctx->Const.QueryCounterBits.TessPatches;
+            break;
+         case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
+            *params = ctx->Const.QueryCounterBits.TessInvocations;
+            break;
+         case GL_GEOMETRY_SHADER_INVOCATIONS:
+            *params = ctx->Const.QueryCounterBits.GsInvocations;
+            break;
+         case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
+            *params = ctx->Const.QueryCounterBits.GsPrimitives;
+            break;
+         case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
+            *params = ctx->Const.QueryCounterBits.FsInvocations;
+            break;
+         case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
+            *params = ctx->Const.QueryCounterBits.ComputeInvocations;
+            break;
+         case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
+            *params = ctx->Const.QueryCounterBits.ClInPrimitives;
+            break;
+         case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
+            *params = ctx->Const.QueryCounterBits.ClOutPrimitives;
+            break;
          default:
             _mesa_problem(ctx,
                           "Unknown target in glGetQueryIndexediv(target = %s)",
@@ -591,7 +745,7 @@ _mesa_GetQueryObjectiv(GLuint id, GLenum pname, GLint *params)
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
 
-   if (!q || q->Active) {
+   if (!q || q->Active || !q->EverBound) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetQueryObjectivARB(id=%d is invalid or active)", id);
       return;
@@ -618,8 +772,8 @@ _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;
       default:
@@ -642,7 +796,7 @@ _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
 
-   if (!q || q->Active) {
+   if (!q || q->Active || !q->EverBound) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetQueryObjectuivARB(id=%d is invalid or active)", id);
       return;
@@ -669,8 +823,8 @@ _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;
       default:
@@ -696,7 +850,7 @@ _mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
 
-   if (!q || q->Active) {
+   if (!q || q->Active || !q->EverBound) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetQueryObjectui64vARB(id=%d is invalid or active)", id);
       return;
@@ -709,8 +863,8 @@ _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;
       default:
@@ -736,7 +890,7 @@ _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
    if (id)
       q = _mesa_lookup_query_object(ctx, id);
 
-   if (!q || q->Active) {
+   if (!q || q->Active || !q->EverBound) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id);
       return;
@@ -749,8 +903,8 @@ _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;
       default:
@@ -759,6 +913,45 @@ _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
    }
 }
 
+/**
+ * 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.
  */
@@ -773,6 +966,18 @@ _mesa_init_queryobj(struct gl_context *ctx)
    ctx->Const.QueryCounterBits.Timestamp = 64;
    ctx->Const.QueryCounterBits.PrimitivesGenerated = 64;
    ctx->Const.QueryCounterBits.PrimitivesWritten = 64;
+
+   ctx->Const.QueryCounterBits.VerticesSubmitted = 64;
+   ctx->Const.QueryCounterBits.PrimitivesSubmitted = 64;
+   ctx->Const.QueryCounterBits.VsInvocations = 64;
+   ctx->Const.QueryCounterBits.TessPatches = 64;
+   ctx->Const.QueryCounterBits.TessInvocations = 64;
+   ctx->Const.QueryCounterBits.GsInvocations = 64;
+   ctx->Const.QueryCounterBits.GsPrimitives = 64;
+   ctx->Const.QueryCounterBits.FsInvocations = 64;
+   ctx->Const.QueryCounterBits.ComputeInvocations = 64;
+   ctx->Const.QueryCounterBits.ClInPrimitives = 64;
+   ctx->Const.QueryCounterBits.ClOutPrimitives = 64;
 }