mesa: move finite macro to imports.h
[mesa.git] / src / mesa / main / queryobj.c
index c48ba5be6211e18ed029b0556a9feaf1e584357a..0842b540de4b38f3c78c55a1a4acd5b171a9abe0 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.1
  *
  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
@@ -29,7 +29,6 @@
 #include "hash.h"
 #include "imports.h"
 #include "queryobj.h"
-#include "mfeatures.h"
 #include "mtypes.h"
 #include "main/dispatch.h"
 
@@ -44,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;
@@ -76,7 +75,7 @@ _mesa_new_query_object(struct gl_context *ctx, GLuint id)
 static void
 _mesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
 {
-   /* no-op */
+   ctx->NewState |= _NEW_DEPTH; /* for swrast */
 }
 
 
@@ -87,6 +86,7 @@ _mesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
 static void
 _mesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
 {
+   ctx->NewState |= _NEW_DEPTH; /* for swrast */
    q->Ready = GL_TRUE;
 }
 
@@ -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,14 +188,46 @@ 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;
    }
@@ -192,7 +239,6 @@ _mesa_GenQueries(GLsizei n, GLuint *ids)
 {
    GLuint first;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGenQueries(%d)\n", n);
@@ -202,13 +248,6 @@ _mesa_GenQueries(GLsizei n, 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, "glGenQueriesARB");
-      return;
-   }
-
    first = _mesa_HashFindFreeKeyBlock(ctx->Query.QueryObjects, n);
    if (first) {
       GLsizei i;
@@ -231,7 +270,6 @@ _mesa_DeleteQueries(GLsizei n, const GLuint *ids)
 {
    GLint i;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
    FLUSH_VERTICES(ctx, 0);
 
    if (MESA_VERBOSE & VERBOSE_API)
@@ -242,18 +280,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);
          }
@@ -309,7 +349,6 @@ _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
 {
    struct gl_query_object *q, **bindpt;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glBeginQueryIndexed(%s, %u, %u)\n",
@@ -318,9 +357,9 @@ _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
    if (!query_error_check_index(ctx, target, index))
       return;
 
-   FLUSH_VERTICES(ctx, _NEW_DEPTH);
+   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;
@@ -367,6 +406,22 @@ _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;
+      }
    }
 
    q->Target = target;
@@ -374,6 +429,7 @@ _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
    q->Result = 0;
    q->Ready = GL_FALSE;
    q->EverBound = GL_TRUE;
+   q->Stream = index;
 
    /* XXX should probably refcount query objects */
    *bindpt = q;
@@ -387,7 +443,6 @@ _mesa_EndQueryIndexed(GLenum target, GLuint index)
 {
    struct gl_query_object *q, **bindpt;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glEndQueryIndexed(%s, %u)\n",
@@ -396,9 +451,9 @@ _mesa_EndQueryIndexed(GLenum target, GLuint index)
    if (!query_error_check_index(ctx, target, index))
       return;
 
-   FLUSH_VERTICES(ctx, _NEW_DEPTH);
+   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;
@@ -445,7 +500,6 @@ _mesa_QueryCounter(GLuint id, GLenum target)
 {
    struct gl_query_object *q;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glQueryCounter(%u, %s)\n", id,
@@ -490,10 +544,16 @@ _mesa_QueryCounter(GLuint id, GLenum target)
    q->Target = target;
    q->Result = 0;
    q->Ready = GL_FALSE;
+   q->EverBound = GL_TRUE;
 
-   /* QueryCounter is implemented using EndQuery without BeginQuery
-    * in drivers. This is actually Direct3D and Gallium convention. */
-   ctx->Driver.EndQuery(ctx, q);
+   if (ctx->Driver.QueryCounter) {
+      ctx->Driver.QueryCounter(ctx, q);
+   } else {
+      /* QueryCounter is implemented using EndQuery without BeginQuery
+       * in drivers. This is actually Direct3D and Gallium convention.
+       */
+      ctx->Driver.EndQuery(ctx, q);
+   }
 }
 
 
@@ -503,7 +563,6 @@ _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
 {
    struct gl_query_object *q = NULL, **bindpt = NULL;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryIndexediv(%s, %u, %s)\n",
@@ -521,7 +580,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 +614,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)",
@@ -583,7 +675,6 @@ _mesa_GetQueryObjectiv(GLuint id, GLenum pname, GLint *params)
 {
    struct gl_query_object *q = NULL;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryObjectiv(%u, %s)\n", id,
@@ -592,7 +683,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;
@@ -635,7 +726,6 @@ _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
 {
    struct gl_query_object *q = NULL;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryObjectuiv(%u, %s)\n", id,
@@ -644,7 +734,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;
@@ -690,7 +780,6 @@ _mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
 {
    struct gl_query_object *q = NULL;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryObjecti64v(%u, %s)\n", id,
@@ -699,7 +788,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;
@@ -731,7 +820,6 @@ _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
 {
    struct gl_query_object *q = NULL;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glGetQueryObjectui64v(%u, %s)\n", id,
@@ -740,7 +828,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;
@@ -763,33 +851,6 @@ _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
    }
 }
 
-
-void
-_mesa_init_queryobj_dispatch(const struct gl_context *ctx,
-                             struct _glapi_table *disp)
-{
-   SET_GenQueries(disp, _mesa_GenQueries);
-   SET_DeleteQueries(disp, _mesa_DeleteQueries);
-   SET_IsQuery(disp, _mesa_IsQuery);
-   SET_BeginQuery(disp, _mesa_BeginQuery);
-   SET_EndQuery(disp, _mesa_EndQuery);
-   SET_GetQueryiv(disp, _mesa_GetQueryiv);
-   SET_GetQueryObjectuiv(disp, _mesa_GetQueryObjectuiv);
-
-   if (_mesa_is_desktop_gl(ctx)) {
-      SET_GetQueryObjectiv(disp, _mesa_GetQueryObjectiv);
-      SET_QueryCounter(disp, _mesa_QueryCounter);
-
-      SET_GetQueryObjecti64v(disp, _mesa_GetQueryObjecti64v);
-      SET_GetQueryObjectui64v(disp, _mesa_GetQueryObjectui64v);
-
-      SET_BeginQueryIndexed(disp, _mesa_BeginQueryIndexed);
-      SET_EndQueryIndexed(disp, _mesa_EndQueryIndexed);
-      SET_GetQueryIndexediv(disp, _mesa_GetQueryIndexediv);
-   }
-}
-
-
 /**
  * Allocate/init the context state related to query objects.
  */
@@ -804,6 +865,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;
 }