2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #include "mfeatures.h"
34 #include "main/dispatch.h"
38 * Allocate a new query object. This is a fallback routine called via
39 * ctx->Driver.NewQueryObject().
40 * \param ctx - rendering context
41 * \param id - the new object's ID
42 * \return pointer to new query_object object or NULL if out of memory.
44 static struct gl_query_object
*
45 _mesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
47 struct gl_query_object
*q
= MALLOC_STRUCT(gl_query_object
);
53 q
->Ready
= GL_TRUE
; /* correct, see spec */
60 * Begin a query. Software driver fallback.
61 * Called via ctx->Driver.BeginQuery().
64 _mesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
71 * End a query. Software driver fallback.
72 * Called via ctx->Driver.EndQuery().
75 _mesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
82 * Wait for query to complete. Software driver fallback.
83 * Called via ctx->Driver.WaitQuery().
86 _mesa_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
88 /* For software drivers, _mesa_end_query() should have completed the query.
89 * For real hardware, implement a proper WaitQuery() driver function,
90 * which may require issuing a flush.
97 * Check if a query results are ready. Software driver fallback.
98 * Called via ctx->Driver.CheckQuery().
101 _mesa_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
103 /* No-op for sw rendering.
104 * HW drivers may need to flush at this time.
110 * Delete a query object. Called via ctx->Driver.DeleteQuery().
111 * Not removed from hash table here.
114 _mesa_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
121 _mesa_init_query_object_functions(struct dd_function_table
*driver
)
123 driver
->NewQueryObject
= _mesa_new_query_object
;
124 driver
->DeleteQuery
= _mesa_delete_query
;
125 driver
->BeginQuery
= _mesa_begin_query
;
126 driver
->EndQuery
= _mesa_end_query
;
127 driver
->WaitQuery
= _mesa_wait_query
;
128 driver
->CheckQuery
= _mesa_check_query
;
133 * Return pointer to the query object binding point for the given target.
134 * \return NULL if invalid target, else the address of binding point
136 static struct gl_query_object
**
137 get_query_binding_point(struct gl_context
*ctx
, GLenum target
)
140 case GL_SAMPLES_PASSED_ARB
:
141 if (ctx
->Extensions
.ARB_occlusion_query
)
142 return &ctx
->Query
.CurrentOcclusionObject
;
145 case GL_ANY_SAMPLES_PASSED
:
146 if (ctx
->Extensions
.ARB_occlusion_query2
)
147 return &ctx
->Query
.CurrentOcclusionObject
;
150 case GL_TIME_ELAPSED_EXT
:
151 if (ctx
->Extensions
.EXT_timer_query
)
152 return &ctx
->Query
.CurrentTimerObject
;
155 case GL_PRIMITIVES_GENERATED
:
156 if (ctx
->Extensions
.EXT_transform_feedback
)
157 return &ctx
->Query
.PrimitivesGenerated
;
160 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
161 if (ctx
->Extensions
.EXT_transform_feedback
)
162 return &ctx
->Query
.PrimitivesWritten
;
171 static void GLAPIENTRY
172 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
175 GET_CURRENT_CONTEXT(ctx
);
176 ASSERT_OUTSIDE_BEGIN_END(ctx
);
178 if (MESA_VERBOSE
& VERBOSE_API
)
179 _mesa_debug(ctx
, "glGenQueries(%d)\n", n
);
182 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
186 /* No query objects can be active at this time! */
187 if (ctx
->Query
.CurrentOcclusionObject
||
188 ctx
->Query
.CurrentTimerObject
) {
189 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
193 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
196 for (i
= 0; i
< n
; i
++) {
197 struct gl_query_object
*q
198 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
200 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
204 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
210 static void GLAPIENTRY
211 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
214 GET_CURRENT_CONTEXT(ctx
);
215 ASSERT_OUTSIDE_BEGIN_END(ctx
);
216 FLUSH_VERTICES(ctx
, 0);
218 if (MESA_VERBOSE
& VERBOSE_API
)
219 _mesa_debug(ctx
, "glDeleeteQueries(%d)\n", n
);
222 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
226 /* No query objects can be active at this time! */
227 if (ctx
->Query
.CurrentOcclusionObject
||
228 ctx
->Query
.CurrentTimerObject
) {
229 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
233 for (i
= 0; i
< n
; i
++) {
235 struct gl_query_object
*q
= _mesa_lookup_query_object(ctx
, ids
[i
]);
237 ASSERT(!q
->Active
); /* should be caught earlier */
238 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
239 ctx
->Driver
.DeleteQuery(ctx
, q
);
246 static GLboolean GLAPIENTRY
247 _mesa_IsQueryARB(GLuint id
)
249 GET_CURRENT_CONTEXT(ctx
);
250 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
252 if (MESA_VERBOSE
& VERBOSE_API
)
253 _mesa_debug(ctx
, "glIsQuery(%u)\n", id
);
255 if (id
&& _mesa_lookup_query_object(ctx
, id
))
262 query_error_check_index(struct gl_context
*ctx
, GLenum target
, GLuint index
)
265 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
266 case GL_PRIMITIVES_GENERATED
:
267 if (index
>= ctx
->Const
.MaxVertexStreams
) {
268 _mesa_error(ctx
, GL_INVALID_VALUE
,
269 "glBeginQueryIndexed(index>=MaxVertexStreams)");
275 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBeginQueryIndexed(index>0)");
282 static void GLAPIENTRY
283 _mesa_BeginQueryIndexed(GLenum target
, GLuint index
, GLuint id
)
285 struct gl_query_object
*q
, **bindpt
;
286 GET_CURRENT_CONTEXT(ctx
);
287 ASSERT_OUTSIDE_BEGIN_END(ctx
);
289 if (MESA_VERBOSE
& VERBOSE_API
)
290 _mesa_debug(ctx
, "glBeginQueryIndexed(%s, %u, %u)\n",
291 _mesa_lookup_enum_by_nr(target
), index
, id
);
293 if (!query_error_check_index(ctx
, target
, index
))
296 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
298 bindpt
= get_query_binding_point(ctx
, target
);
300 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQuery{Indexed}(target)");
304 /* From the GL_ARB_occlusion_query spec:
306 * "If BeginQueryARB is called while another query is already in
307 * progress with the same target, an INVALID_OPERATION error is
311 _mesa_error(ctx
, GL_INVALID_OPERATION
,
312 "glBeginQuery{Indexed}(target=%s is active)",
313 _mesa_lookup_enum_by_nr(target
));
318 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQuery{Indexed}(id==0)");
322 q
= _mesa_lookup_query_object(ctx
, id
);
324 /* create new object */
325 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
327 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQuery{Indexed}");
330 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
333 /* pre-existing object */
335 _mesa_error(ctx
, GL_INVALID_OPERATION
,
336 "glBeginQuery{Indexed}(query already active)");
346 /* XXX should probably refcount query objects */
349 ctx
->Driver
.BeginQuery(ctx
, q
);
353 static void GLAPIENTRY
354 _mesa_EndQueryIndexed(GLenum target
, GLuint index
)
356 struct gl_query_object
*q
, **bindpt
;
357 GET_CURRENT_CONTEXT(ctx
);
358 ASSERT_OUTSIDE_BEGIN_END(ctx
);
360 if (MESA_VERBOSE
& VERBOSE_API
)
361 _mesa_debug(ctx
, "glEndQueryIndexed(%s, %u)\n",
362 _mesa_lookup_enum_by_nr(target
), index
);
364 if (!query_error_check_index(ctx
, target
, index
))
367 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
369 bindpt
= get_query_binding_point(ctx
, target
);
371 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQuery{Indexed}(target)");
375 /* XXX should probably refcount query objects */
378 /* Check for GL_ANY_SAMPLES_PASSED vs GL_SAMPLES_PASSED. */
379 if (q
&& q
->Target
!= target
) {
380 _mesa_error(ctx
, GL_INVALID_OPERATION
,
381 "glEndQuery(target=%s with active query of target %s)",
382 _mesa_lookup_enum_by_nr(target
),
383 _mesa_lookup_enum_by_nr(q
->Target
));
389 if (!q
|| !q
->Active
) {
390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
391 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
395 q
->Active
= GL_FALSE
;
396 ctx
->Driver
.EndQuery(ctx
, q
);
399 static void GLAPIENTRY
400 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
402 _mesa_BeginQueryIndexed(target
, 0, id
);
405 static void GLAPIENTRY
406 _mesa_EndQueryARB(GLenum target
)
408 _mesa_EndQueryIndexed(target
, 0);
411 static void GLAPIENTRY
412 _mesa_QueryCounter(GLuint id
, GLenum target
)
414 struct gl_query_object
*q
;
415 GET_CURRENT_CONTEXT(ctx
);
416 ASSERT_OUTSIDE_BEGIN_END(ctx
);
418 if (MESA_VERBOSE
& VERBOSE_API
)
419 _mesa_debug(ctx
, "glQueryCounter(%u, %s)\n", id
,
420 _mesa_lookup_enum_by_nr(target
));
423 if (target
!= GL_TIMESTAMP
) {
424 _mesa_error(ctx
, GL_INVALID_ENUM
, "glQueryCounter(target)");
429 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id==0)");
433 q
= _mesa_lookup_query_object(ctx
, id
);
435 /* XXX the Core profile should throw INVALID_OPERATION here */
437 /* create new object */
438 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
440 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glQueryCounter");
443 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
446 if (q
->Target
&& q
->Target
!= GL_TIMESTAMP
) {
447 _mesa_error(ctx
, GL_INVALID_OPERATION
,
448 "glQueryCounter(id has an invalid target)");
454 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id is active)");
462 /* QueryCounter is implemented using EndQuery without BeginQuery
463 * in drivers. This is actually Direct3D and Gallium convention. */
464 ctx
->Driver
.EndQuery(ctx
, q
);
468 static void GLAPIENTRY
469 _mesa_GetQueryIndexediv(GLenum target
, GLuint index
, GLenum pname
,
472 struct gl_query_object
*q
= NULL
, **bindpt
= NULL
;
473 GET_CURRENT_CONTEXT(ctx
);
474 ASSERT_OUTSIDE_BEGIN_END(ctx
);
476 if (MESA_VERBOSE
& VERBOSE_API
)
477 _mesa_debug(ctx
, "glGetQueryIndexediv(%s, %u, %s)\n",
478 _mesa_lookup_enum_by_nr(target
),
480 _mesa_lookup_enum_by_nr(pname
));
482 if (!query_error_check_index(ctx
, target
, index
))
485 if (target
== GL_TIMESTAMP
) {
486 if (!ctx
->Extensions
.ARB_timer_query
) {
487 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
492 bindpt
= get_query_binding_point(ctx
, target
);
494 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(target)");
502 case GL_QUERY_COUNTER_BITS_ARB
:
504 case GL_SAMPLES_PASSED
:
505 *params
= ctx
->Const
.QueryCounterBits
.SamplesPassed
;
507 case GL_ANY_SAMPLES_PASSED
:
508 /* The minimum value of this is 1 if it's nonzero, and the value
509 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
514 case GL_TIME_ELAPSED
:
515 *params
= ctx
->Const
.QueryCounterBits
.TimeElapsed
;
518 *params
= ctx
->Const
.QueryCounterBits
.Timestamp
;
520 case GL_PRIMITIVES_GENERATED
:
521 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
;
523 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
524 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesWritten
;
528 "Unknown target in glGetQueryIndexediv(target = %s)",
529 _mesa_lookup_enum_by_nr(target
));
534 case GL_CURRENT_QUERY_ARB
:
535 *params
= (q
&& q
->Target
== target
) ? q
->Id
: 0;
538 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(pname)");
543 static void GLAPIENTRY
544 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
546 _mesa_GetQueryIndexediv(target
, 0, pname
, params
);
549 static void GLAPIENTRY
550 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
552 struct gl_query_object
*q
= NULL
;
553 GET_CURRENT_CONTEXT(ctx
);
554 ASSERT_OUTSIDE_BEGIN_END(ctx
);
556 if (MESA_VERBOSE
& VERBOSE_API
)
557 _mesa_debug(ctx
, "glGetQueryObjectiv(%u, %s)\n", id
,
558 _mesa_lookup_enum_by_nr(pname
));
561 q
= _mesa_lookup_query_object(ctx
, id
);
563 if (!q
|| q
->Active
) {
564 _mesa_error(ctx
, GL_INVALID_OPERATION
,
565 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
570 case GL_QUERY_RESULT_ARB
:
572 ctx
->Driver
.WaitQuery(ctx
, q
);
573 /* if result is too large for returned type, clamp to max value */
574 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
580 if (q
->Result
> 0x7fffffff) {
581 *params
= 0x7fffffff;
584 *params
= (GLint
)q
->Result
;
588 case GL_QUERY_RESULT_AVAILABLE_ARB
:
590 ctx
->Driver
.CheckQuery( ctx
, q
);
594 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
600 static void GLAPIENTRY
601 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
603 struct gl_query_object
*q
= NULL
;
604 GET_CURRENT_CONTEXT(ctx
);
605 ASSERT_OUTSIDE_BEGIN_END(ctx
);
607 if (MESA_VERBOSE
& VERBOSE_API
)
608 _mesa_debug(ctx
, "glGetQueryObjectuiv(%u, %s)\n", id
,
609 _mesa_lookup_enum_by_nr(pname
));
612 q
= _mesa_lookup_query_object(ctx
, id
);
614 if (!q
|| q
->Active
) {
615 _mesa_error(ctx
, GL_INVALID_OPERATION
,
616 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
621 case GL_QUERY_RESULT_ARB
:
623 ctx
->Driver
.WaitQuery(ctx
, q
);
624 /* if result is too large for returned type, clamp to max value */
625 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
631 if (q
->Result
> 0xffffffff) {
632 *params
= 0xffffffff;
635 *params
= (GLuint
)q
->Result
;
639 case GL_QUERY_RESULT_AVAILABLE_ARB
:
641 ctx
->Driver
.CheckQuery( ctx
, q
);
645 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
652 * New with GL_EXT_timer_query
654 static void GLAPIENTRY
655 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
657 struct gl_query_object
*q
= NULL
;
658 GET_CURRENT_CONTEXT(ctx
);
659 ASSERT_OUTSIDE_BEGIN_END(ctx
);
661 if (MESA_VERBOSE
& VERBOSE_API
)
662 _mesa_debug(ctx
, "glGetQueryObjecti64v(%u, %s)\n", id
,
663 _mesa_lookup_enum_by_nr(pname
));
666 q
= _mesa_lookup_query_object(ctx
, id
);
668 if (!q
|| q
->Active
) {
669 _mesa_error(ctx
, GL_INVALID_OPERATION
,
670 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
675 case GL_QUERY_RESULT_ARB
:
677 ctx
->Driver
.WaitQuery(ctx
, q
);
680 case GL_QUERY_RESULT_AVAILABLE_ARB
:
682 ctx
->Driver
.CheckQuery( ctx
, q
);
686 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
693 * New with GL_EXT_timer_query
695 static void GLAPIENTRY
696 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
698 struct gl_query_object
*q
= NULL
;
699 GET_CURRENT_CONTEXT(ctx
);
700 ASSERT_OUTSIDE_BEGIN_END(ctx
);
702 if (MESA_VERBOSE
& VERBOSE_API
)
703 _mesa_debug(ctx
, "glGetQueryObjectui64v(%u, %s)\n", id
,
704 _mesa_lookup_enum_by_nr(pname
));
707 q
= _mesa_lookup_query_object(ctx
, id
);
709 if (!q
|| q
->Active
) {
710 _mesa_error(ctx
, GL_INVALID_OPERATION
,
711 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
716 case GL_QUERY_RESULT_ARB
:
718 ctx
->Driver
.WaitQuery(ctx
, q
);
721 case GL_QUERY_RESULT_AVAILABLE_ARB
:
723 ctx
->Driver
.CheckQuery( ctx
, q
);
727 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
734 _mesa_init_queryobj_dispatch(struct _glapi_table
*disp
)
736 SET_GenQueriesARB(disp
, _mesa_GenQueriesARB
);
737 SET_DeleteQueriesARB(disp
, _mesa_DeleteQueriesARB
);
738 SET_IsQueryARB(disp
, _mesa_IsQueryARB
);
739 SET_BeginQueryARB(disp
, _mesa_BeginQueryARB
);
740 SET_EndQueryARB(disp
, _mesa_EndQueryARB
);
741 SET_GetQueryivARB(disp
, _mesa_GetQueryivARB
);
742 SET_GetQueryObjectivARB(disp
, _mesa_GetQueryObjectivARB
);
743 SET_GetQueryObjectuivARB(disp
, _mesa_GetQueryObjectuivARB
);
744 SET_QueryCounter(disp
, _mesa_QueryCounter
);
746 SET_GetQueryObjecti64vEXT(disp
, _mesa_GetQueryObjecti64vEXT
);
747 SET_GetQueryObjectui64vEXT(disp
, _mesa_GetQueryObjectui64vEXT
);
749 SET_BeginQueryIndexed(disp
, _mesa_BeginQueryIndexed
);
750 SET_EndQueryIndexed(disp
, _mesa_EndQueryIndexed
);
751 SET_GetQueryIndexediv(disp
, _mesa_GetQueryIndexediv
);
756 * Allocate/init the context state related to query objects.
759 _mesa_init_queryobj(struct gl_context
*ctx
)
761 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
762 ctx
->Query
.CurrentOcclusionObject
= NULL
;
764 ctx
->Const
.QueryCounterBits
.SamplesPassed
= 64;
765 ctx
->Const
.QueryCounterBits
.TimeElapsed
= 64;
766 ctx
->Const
.QueryCounterBits
.Timestamp
= 64;
767 ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
= 64;
768 ctx
->Const
.QueryCounterBits
.PrimitivesWritten
= 64;
773 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
776 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
778 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
779 struct gl_context
*ctx
= (struct gl_context
*)userData
;
780 ctx
->Driver
.DeleteQuery(ctx
, q
);
785 * Free the context state related to query objects.
788 _mesa_free_queryobj_data(struct gl_context
*ctx
)
790 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
791 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);