Merge branch 'glapi-reorg'
[mesa.git] / src / mesa / main / queryobj.c
index 387a82fc9dff1f9a6d78bb24cf8ec90b08e3103f..8874397720633fe65e36118388fd84b878ff07f8 100644 (file)
@@ -29,7 +29,7 @@
 #include "imports.h"
 #include "queryobj.h"
 #include "mtypes.h"
-#include "glapi/dispatch.h"
+#include "main/dispatch.h"
 
 
 #if FEATURE_queryobj
@@ -43,7 +43,7 @@
  * \return pointer to new query_object object or NULL if out of memory.
  */
 static struct gl_query_object *
-_mesa_new_query_object(GLcontext *ctx, GLuint id)
+_mesa_new_query_object(struct gl_context *ctx, GLuint id)
 {
    struct gl_query_object *q = MALLOC_STRUCT(gl_query_object);
    (void) ctx;
@@ -62,7 +62,7 @@ _mesa_new_query_object(GLcontext *ctx, GLuint id)
  * Called via ctx->Driver.BeginQuery().
  */
 static void
-_mesa_begin_query(GLcontext *ctx, struct gl_query_object *q)
+_mesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
 {
    /* no-op */
 }
@@ -73,7 +73,7 @@ _mesa_begin_query(GLcontext *ctx, struct gl_query_object *q)
  * Called via ctx->Driver.EndQuery().
  */
 static void
-_mesa_end_query(GLcontext *ctx, struct gl_query_object *q)
+_mesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
 {
    q->Ready = GL_TRUE;
 }
@@ -84,7 +84,7 @@ _mesa_end_query(GLcontext *ctx, struct gl_query_object *q)
  * Called via ctx->Driver.WaitQuery().
  */
 static void
-_mesa_wait_query(GLcontext *ctx, struct gl_query_object *q)
+_mesa_wait_query(struct gl_context *ctx, struct gl_query_object *q)
 {
    /* For software drivers, _mesa_end_query() should have completed the query.
     * For real hardware, implement a proper WaitQuery() driver function,
@@ -99,7 +99,7 @@ _mesa_wait_query(GLcontext *ctx, struct gl_query_object *q)
  * Called via ctx->Driver.CheckQuery().
  */
 static void
-_mesa_check_query(GLcontext *ctx, struct gl_query_object *q)
+_mesa_check_query(struct gl_context *ctx, struct gl_query_object *q)
 {
    /* No-op for sw rendering.
     * HW drivers may need to flush at this time.
@@ -112,9 +112,9 @@ _mesa_check_query(GLcontext *ctx, struct gl_query_object *q)
  * Not removed from hash table here.
  */
 static void
-_mesa_delete_query(GLcontext *ctx, struct gl_query_object *q)
+_mesa_delete_query(struct gl_context *ctx, struct gl_query_object *q)
 {
-   _mesa_free(q);
+   free(q);
 }
 
 
@@ -130,6 +130,42 @@ _mesa_init_query_object_functions(struct dd_function_table *driver)
 }
 
 
+/**
+ * Return pointer to the query object binding point for the given target.
+ * \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)
+{
+   switch (target) {
+   case GL_SAMPLES_PASSED_ARB:
+      if (ctx->Extensions.ARB_occlusion_query)
+         return &ctx->Query.CurrentOcclusionObject;
+      else
+         return NULL;
+   case GL_TIME_ELAPSED_EXT:
+      if (ctx->Extensions.EXT_timer_query)
+         return &ctx->Query.CurrentTimerObject;
+      else
+         return NULL;
+#if FEATURE_EXT_transform_feedback
+   case GL_PRIMITIVES_GENERATED:
+      if (ctx->Extensions.EXT_transform_feedback)
+         return &ctx->Query.PrimitivesGenerated;
+      else
+         return NULL;
+   case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
+      if (ctx->Extensions.EXT_transform_feedback)
+         return &ctx->Query.PrimitivesWritten;
+      else
+         return NULL;
+#endif
+   default:
+      return NULL;
+   }
+}
+
+
 void GLAPIENTRY
 _mesa_GenQueriesARB(GLsizei n, GLuint *ids)
 {
@@ -214,36 +250,16 @@ _mesa_IsQueryARB(GLuint id)
 static void GLAPIENTRY
 _mesa_BeginQueryARB(GLenum target, GLuint id)
 {
-   struct gl_query_object *q;
+   struct gl_query_object *q, **bindpt;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    FLUSH_VERTICES(ctx, _NEW_DEPTH);
 
-   switch (target) {
-      case GL_SAMPLES_PASSED_ARB:
-         if (!ctx->Extensions.ARB_occlusion_query) {
-            _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
-            return;
-         }
-         if (ctx->Query.CurrentOcclusionObject) {
-            _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB");
-            return;
-         }
-         break;
-      case GL_TIME_ELAPSED_EXT:
-         if (!ctx->Extensions.EXT_timer_query) {
-            _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
-            return;
-         }
-         if (ctx->Query.CurrentTimerObject) {
-            _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB");
-            return;
-         }
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
-         return;
+   bindpt = get_query_binding_point(ctx, target);
+   if (!bindpt) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
+      return;
    }
 
    if (id == 0) {
@@ -275,12 +291,8 @@ _mesa_BeginQueryARB(GLenum target, GLuint id)
    q->Result = 0;
    q->Ready = GL_FALSE;
 
-   if (target == GL_SAMPLES_PASSED_ARB) {
-      ctx->Query.CurrentOcclusionObject = q;
-   }
-   else if (target == GL_TIME_ELAPSED_EXT) {
-      ctx->Query.CurrentTimerObject = q;
-   }
+   /* XXX should probably refcount query objects */
+   *bindpt = q;
 
    ctx->Driver.BeginQuery(ctx, q);
 }
@@ -289,34 +301,22 @@ _mesa_BeginQueryARB(GLenum target, GLuint id)
 static void GLAPIENTRY
 _mesa_EndQueryARB(GLenum target)
 {
-   struct gl_query_object *q;
+   struct gl_query_object *q, **bindpt;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    FLUSH_VERTICES(ctx, _NEW_DEPTH);
 
-   switch (target) {
-      case GL_SAMPLES_PASSED_ARB:
-         if (!ctx->Extensions.ARB_occlusion_query) {
-            _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
-            return;
-         }
-         q = ctx->Query.CurrentOcclusionObject;
-         ctx->Query.CurrentOcclusionObject = NULL;
-         break;
-      case GL_TIME_ELAPSED_EXT:
-         if (!ctx->Extensions.EXT_timer_query) {
-            _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
-            return;
-         }
-         q = ctx->Query.CurrentTimerObject;
-         ctx->Query.CurrentTimerObject = NULL;
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
-         return;
+   bindpt = get_query_binding_point(ctx, target);
+   if (!bindpt) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
+      return;
    }
 
+   /* XXX should probably refcount query objects */
+   q = *bindpt;
+   *bindpt = NULL;
+
    if (!q || !q->Active) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glEndQueryARB(no matching glBeginQueryARB)");
@@ -331,30 +331,18 @@ _mesa_EndQueryARB(GLenum target)
 void GLAPIENTRY
 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
 {
-   struct gl_query_object *q;
+   struct gl_query_object *q, **bindpt;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   switch (target) {
-      case GL_SAMPLES_PASSED_ARB:
-         if (!ctx->Extensions.ARB_occlusion_query) {
-            _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
-            return;
-         }
-         q = ctx->Query.CurrentOcclusionObject;
-         break;
-      case GL_TIME_ELAPSED_EXT:
-         if (!ctx->Extensions.EXT_timer_query) {
-            _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
-            return;
-         }
-         q = ctx->Query.CurrentTimerObject;
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(target)");
-         return;
+   bindpt = get_query_binding_point(ctx, target);
+   if (!bindpt) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryARB(target)");
+      return;
    }
 
+   q = *bindpt;
+
    switch (pname) {
       case GL_QUERY_COUNTER_BITS_ARB:
          *params = 8 * sizeof(q->Result);
@@ -547,7 +535,7 @@ _mesa_init_queryobj_dispatch(struct _glapi_table *disp)
  * Allocate/init the context state related to query objects.
  */
 void
-_mesa_init_queryobj(GLcontext *ctx)
+_mesa_init_queryobj(struct gl_context *ctx)
 {
    ctx->Query.QueryObjects = _mesa_NewHashTable();
    ctx->Query.CurrentOcclusionObject = NULL;
@@ -561,7 +549,7 @@ static void
 delete_queryobj_cb(GLuint id, void *data, void *userData)
 {
    struct gl_query_object *q= (struct gl_query_object *) data;
-   GLcontext *ctx = (GLcontext *)userData;
+   struct gl_context *ctx = (struct gl_context *)userData;
    ctx->Driver.DeleteQuery(ctx, q);
 }
 
@@ -570,7 +558,7 @@ delete_queryobj_cb(GLuint id, void *data, void *userData)
  * Free the context state related to query objects.
  */
 void
-_mesa_free_queryobj_data(GLcontext *ctx)
+_mesa_free_queryobj_data(struct gl_context *ctx)
 {
    _mesa_HashDeleteAll(ctx->Query.QueryObjects, delete_queryobj_cb, ctx);
    _mesa_DeleteHashTable(ctx->Query.QueryObjects);