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 */
379 if (!q
|| !q
->Active
) {
380 _mesa_error(ctx
, GL_INVALID_OPERATION
,
381 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
385 q
->Active
= GL_FALSE
;
386 ctx
->Driver
.EndQuery(ctx
, q
);
389 static void GLAPIENTRY
390 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
392 _mesa_BeginQueryIndexed(target
, 0, id
);
395 static void GLAPIENTRY
396 _mesa_EndQueryARB(GLenum target
)
398 _mesa_EndQueryIndexed(target
, 0);
401 static void GLAPIENTRY
402 _mesa_QueryCounter(GLuint id
, GLenum target
)
404 struct gl_query_object
*q
;
405 GET_CURRENT_CONTEXT(ctx
);
406 ASSERT_OUTSIDE_BEGIN_END(ctx
);
408 if (MESA_VERBOSE
& VERBOSE_API
)
409 _mesa_debug(ctx
, "glQueryCounter(%u, %s)\n", id
,
410 _mesa_lookup_enum_by_nr(target
));
413 if (target
!= GL_TIMESTAMP
) {
414 _mesa_error(ctx
, GL_INVALID_ENUM
, "glQueryCounter(target)");
419 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id==0)");
423 q
= _mesa_lookup_query_object(ctx
, id
);
425 /* XXX the Core profile should throw INVALID_OPERATION here */
427 /* create new object */
428 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
430 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glQueryCounter");
433 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
436 if (q
->Target
&& q
->Target
!= GL_TIMESTAMP
) {
437 _mesa_error(ctx
, GL_INVALID_OPERATION
,
438 "glQueryCounter(id has an invalid target)");
444 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id is active)");
452 /* QueryCounter is implemented using EndQuery without BeginQuery
453 * in drivers. This is actually Direct3D and Gallium convention. */
454 ctx
->Driver
.EndQuery(ctx
, q
);
458 static void GLAPIENTRY
459 _mesa_GetQueryIndexediv(GLenum target
, GLuint index
, GLenum pname
,
462 struct gl_query_object
*q
= NULL
, **bindpt
= NULL
;
463 GET_CURRENT_CONTEXT(ctx
);
464 ASSERT_OUTSIDE_BEGIN_END(ctx
);
466 if (MESA_VERBOSE
& VERBOSE_API
)
467 _mesa_debug(ctx
, "glGetQueryIndexediv(%s, %u, %s)\n",
468 _mesa_lookup_enum_by_nr(target
),
470 _mesa_lookup_enum_by_nr(pname
));
472 if (!query_error_check_index(ctx
, target
, index
))
475 if (target
== GL_TIMESTAMP
) {
476 if (!ctx
->Extensions
.ARB_timer_query
) {
477 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
482 bindpt
= get_query_binding_point(ctx
, target
);
484 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(target)");
492 case GL_QUERY_COUNTER_BITS_ARB
:
494 case GL_SAMPLES_PASSED
:
495 *params
= ctx
->Const
.QueryCounterBits
.SamplesPassed
;
497 case GL_ANY_SAMPLES_PASSED
:
498 /* The minimum value of this is 1 if it's nonzero, and the value
499 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
504 case GL_TIME_ELAPSED
:
505 *params
= ctx
->Const
.QueryCounterBits
.TimeElapsed
;
508 *params
= ctx
->Const
.QueryCounterBits
.Timestamp
;
510 case GL_PRIMITIVES_GENERATED
:
511 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
;
513 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
514 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesWritten
;
518 "Unknown target in glGetQueryIndexediv(target = %s)",
519 _mesa_lookup_enum_by_nr(target
));
524 case GL_CURRENT_QUERY_ARB
:
525 *params
= q
? q
->Id
: 0;
528 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(pname)");
533 static void GLAPIENTRY
534 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
536 _mesa_GetQueryIndexediv(target
, 0, pname
, params
);
539 static void GLAPIENTRY
540 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
542 struct gl_query_object
*q
= NULL
;
543 GET_CURRENT_CONTEXT(ctx
);
544 ASSERT_OUTSIDE_BEGIN_END(ctx
);
546 if (MESA_VERBOSE
& VERBOSE_API
)
547 _mesa_debug(ctx
, "glGetQueryObjectiv(%u, %s)\n", id
,
548 _mesa_lookup_enum_by_nr(pname
));
551 q
= _mesa_lookup_query_object(ctx
, id
);
553 if (!q
|| q
->Active
) {
554 _mesa_error(ctx
, GL_INVALID_OPERATION
,
555 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
560 case GL_QUERY_RESULT_ARB
:
562 ctx
->Driver
.WaitQuery(ctx
, q
);
563 /* if result is too large for returned type, clamp to max value */
564 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
570 if (q
->Result
> 0x7fffffff) {
571 *params
= 0x7fffffff;
574 *params
= (GLint
)q
->Result
;
578 case GL_QUERY_RESULT_AVAILABLE_ARB
:
580 ctx
->Driver
.CheckQuery( ctx
, q
);
584 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
590 static void GLAPIENTRY
591 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
593 struct gl_query_object
*q
= NULL
;
594 GET_CURRENT_CONTEXT(ctx
);
595 ASSERT_OUTSIDE_BEGIN_END(ctx
);
597 if (MESA_VERBOSE
& VERBOSE_API
)
598 _mesa_debug(ctx
, "glGetQueryObjectuiv(%u, %s)\n", id
,
599 _mesa_lookup_enum_by_nr(pname
));
602 q
= _mesa_lookup_query_object(ctx
, id
);
604 if (!q
|| q
->Active
) {
605 _mesa_error(ctx
, GL_INVALID_OPERATION
,
606 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
611 case GL_QUERY_RESULT_ARB
:
613 ctx
->Driver
.WaitQuery(ctx
, q
);
614 /* if result is too large for returned type, clamp to max value */
615 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
621 if (q
->Result
> 0xffffffff) {
622 *params
= 0xffffffff;
625 *params
= (GLuint
)q
->Result
;
629 case GL_QUERY_RESULT_AVAILABLE_ARB
:
631 ctx
->Driver
.CheckQuery( ctx
, q
);
635 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
642 * New with GL_EXT_timer_query
644 static void GLAPIENTRY
645 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
647 struct gl_query_object
*q
= NULL
;
648 GET_CURRENT_CONTEXT(ctx
);
649 ASSERT_OUTSIDE_BEGIN_END(ctx
);
651 if (MESA_VERBOSE
& VERBOSE_API
)
652 _mesa_debug(ctx
, "glGetQueryObjecti64v(%u, %s)\n", id
,
653 _mesa_lookup_enum_by_nr(pname
));
656 q
= _mesa_lookup_query_object(ctx
, id
);
658 if (!q
|| q
->Active
) {
659 _mesa_error(ctx
, GL_INVALID_OPERATION
,
660 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
665 case GL_QUERY_RESULT_ARB
:
667 ctx
->Driver
.WaitQuery(ctx
, q
);
670 case GL_QUERY_RESULT_AVAILABLE_ARB
:
672 ctx
->Driver
.CheckQuery( ctx
, q
);
676 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
683 * New with GL_EXT_timer_query
685 static void GLAPIENTRY
686 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
688 struct gl_query_object
*q
= NULL
;
689 GET_CURRENT_CONTEXT(ctx
);
690 ASSERT_OUTSIDE_BEGIN_END(ctx
);
692 if (MESA_VERBOSE
& VERBOSE_API
)
693 _mesa_debug(ctx
, "glGetQueryObjectui64v(%u, %s)\n", id
,
694 _mesa_lookup_enum_by_nr(pname
));
697 q
= _mesa_lookup_query_object(ctx
, id
);
699 if (!q
|| q
->Active
) {
700 _mesa_error(ctx
, GL_INVALID_OPERATION
,
701 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
706 case GL_QUERY_RESULT_ARB
:
708 ctx
->Driver
.WaitQuery(ctx
, q
);
711 case GL_QUERY_RESULT_AVAILABLE_ARB
:
713 ctx
->Driver
.CheckQuery( ctx
, q
);
717 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
724 _mesa_init_queryobj_dispatch(struct _glapi_table
*disp
)
726 SET_GenQueriesARB(disp
, _mesa_GenQueriesARB
);
727 SET_DeleteQueriesARB(disp
, _mesa_DeleteQueriesARB
);
728 SET_IsQueryARB(disp
, _mesa_IsQueryARB
);
729 SET_BeginQueryARB(disp
, _mesa_BeginQueryARB
);
730 SET_EndQueryARB(disp
, _mesa_EndQueryARB
);
731 SET_GetQueryivARB(disp
, _mesa_GetQueryivARB
);
732 SET_GetQueryObjectivARB(disp
, _mesa_GetQueryObjectivARB
);
733 SET_GetQueryObjectuivARB(disp
, _mesa_GetQueryObjectuivARB
);
734 SET_QueryCounter(disp
, _mesa_QueryCounter
);
736 SET_GetQueryObjecti64vEXT(disp
, _mesa_GetQueryObjecti64vEXT
);
737 SET_GetQueryObjectui64vEXT(disp
, _mesa_GetQueryObjectui64vEXT
);
739 SET_BeginQueryIndexed(disp
, _mesa_BeginQueryIndexed
);
740 SET_EndQueryIndexed(disp
, _mesa_EndQueryIndexed
);
741 SET_GetQueryIndexediv(disp
, _mesa_GetQueryIndexediv
);
746 * Allocate/init the context state related to query objects.
749 _mesa_init_queryobj(struct gl_context
*ctx
)
751 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
752 ctx
->Query
.CurrentOcclusionObject
= NULL
;
754 ctx
->Const
.QueryCounterBits
.SamplesPassed
= 64;
755 ctx
->Const
.QueryCounterBits
.TimeElapsed
= 64;
756 ctx
->Const
.QueryCounterBits
.Timestamp
= 64;
757 ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
= 64;
758 ctx
->Const
.QueryCounterBits
.PrimitivesWritten
= 64;
763 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
766 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
768 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
769 struct gl_context
*ctx
= (struct gl_context
*)userData
;
770 ctx
->Driver
.DeleteQuery(ctx
, q
);
775 * Free the context state related to query objects.
778 _mesa_free_queryobj_data(struct gl_context
*ctx
)
780 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
781 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);