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
;
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
);
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
, "glDeleteQueries(%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
);
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)");
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 if (ctx
->API
== API_OPENGL_CORE
) {
325 _mesa_error(ctx
, GL_INVALID_OPERATION
,
326 "glBeginQuery{Indexed}(non-gen name)");
329 /* create new object */
330 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
332 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQuery{Indexed}");
335 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
339 /* pre-existing object */
341 _mesa_error(ctx
, GL_INVALID_OPERATION
,
342 "glBeginQuery{Indexed}(query already active)");
352 /* XXX should probably refcount query objects */
355 ctx
->Driver
.BeginQuery(ctx
, q
);
360 _mesa_EndQueryIndexed(GLenum target
, GLuint index
)
362 struct gl_query_object
*q
, **bindpt
;
363 GET_CURRENT_CONTEXT(ctx
);
364 ASSERT_OUTSIDE_BEGIN_END(ctx
);
366 if (MESA_VERBOSE
& VERBOSE_API
)
367 _mesa_debug(ctx
, "glEndQueryIndexed(%s, %u)\n",
368 _mesa_lookup_enum_by_nr(target
), index
);
370 if (!query_error_check_index(ctx
, target
, index
))
373 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
375 bindpt
= get_query_binding_point(ctx
, target
);
377 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQuery{Indexed}(target)");
381 /* XXX should probably refcount query objects */
384 /* Check for GL_ANY_SAMPLES_PASSED vs GL_SAMPLES_PASSED. */
385 if (q
&& q
->Target
!= target
) {
386 _mesa_error(ctx
, GL_INVALID_OPERATION
,
387 "glEndQuery(target=%s with active query of target %s)",
388 _mesa_lookup_enum_by_nr(target
),
389 _mesa_lookup_enum_by_nr(q
->Target
));
395 if (!q
|| !q
->Active
) {
396 _mesa_error(ctx
, GL_INVALID_OPERATION
,
397 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
401 q
->Active
= GL_FALSE
;
402 ctx
->Driver
.EndQuery(ctx
, q
);
406 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
408 _mesa_BeginQueryIndexed(target
, 0, id
);
412 _mesa_EndQueryARB(GLenum target
)
414 _mesa_EndQueryIndexed(target
, 0);
418 _mesa_QueryCounter(GLuint id
, GLenum target
)
420 struct gl_query_object
*q
;
421 GET_CURRENT_CONTEXT(ctx
);
422 ASSERT_OUTSIDE_BEGIN_END(ctx
);
424 if (MESA_VERBOSE
& VERBOSE_API
)
425 _mesa_debug(ctx
, "glQueryCounter(%u, %s)\n", id
,
426 _mesa_lookup_enum_by_nr(target
));
429 if (target
!= GL_TIMESTAMP
) {
430 _mesa_error(ctx
, GL_INVALID_ENUM
, "glQueryCounter(target)");
435 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id==0)");
439 q
= _mesa_lookup_query_object(ctx
, id
);
441 /* XXX the Core profile should throw INVALID_OPERATION here */
443 /* create new object */
444 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
446 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glQueryCounter");
449 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
452 if (q
->Target
&& q
->Target
!= GL_TIMESTAMP
) {
453 _mesa_error(ctx
, GL_INVALID_OPERATION
,
454 "glQueryCounter(id has an invalid target)");
460 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id is active)");
468 /* QueryCounter is implemented using EndQuery without BeginQuery
469 * in drivers. This is actually Direct3D and Gallium convention. */
470 ctx
->Driver
.EndQuery(ctx
, q
);
475 _mesa_GetQueryIndexediv(GLenum target
, GLuint index
, GLenum pname
,
478 struct gl_query_object
*q
= NULL
, **bindpt
= NULL
;
479 GET_CURRENT_CONTEXT(ctx
);
480 ASSERT_OUTSIDE_BEGIN_END(ctx
);
482 if (MESA_VERBOSE
& VERBOSE_API
)
483 _mesa_debug(ctx
, "glGetQueryIndexediv(%s, %u, %s)\n",
484 _mesa_lookup_enum_by_nr(target
),
486 _mesa_lookup_enum_by_nr(pname
));
488 if (!query_error_check_index(ctx
, target
, index
))
491 if (target
== GL_TIMESTAMP
) {
492 if (!ctx
->Extensions
.ARB_timer_query
) {
493 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
498 bindpt
= get_query_binding_point(ctx
, target
);
500 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(target)");
508 case GL_QUERY_COUNTER_BITS_ARB
:
510 case GL_SAMPLES_PASSED
:
511 *params
= ctx
->Const
.QueryCounterBits
.SamplesPassed
;
513 case GL_ANY_SAMPLES_PASSED
:
514 /* The minimum value of this is 1 if it's nonzero, and the value
515 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
520 case GL_TIME_ELAPSED
:
521 *params
= ctx
->Const
.QueryCounterBits
.TimeElapsed
;
524 *params
= ctx
->Const
.QueryCounterBits
.Timestamp
;
526 case GL_PRIMITIVES_GENERATED
:
527 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
;
529 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
530 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesWritten
;
534 "Unknown target in glGetQueryIndexediv(target = %s)",
535 _mesa_lookup_enum_by_nr(target
));
540 case GL_CURRENT_QUERY_ARB
:
541 *params
= (q
&& q
->Target
== target
) ? q
->Id
: 0;
544 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(pname)");
550 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
552 _mesa_GetQueryIndexediv(target
, 0, pname
, params
);
556 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
558 struct gl_query_object
*q
= NULL
;
559 GET_CURRENT_CONTEXT(ctx
);
560 ASSERT_OUTSIDE_BEGIN_END(ctx
);
562 if (MESA_VERBOSE
& VERBOSE_API
)
563 _mesa_debug(ctx
, "glGetQueryObjectiv(%u, %s)\n", id
,
564 _mesa_lookup_enum_by_nr(pname
));
567 q
= _mesa_lookup_query_object(ctx
, id
);
569 if (!q
|| q
->Active
) {
570 _mesa_error(ctx
, GL_INVALID_OPERATION
,
571 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
576 case GL_QUERY_RESULT_ARB
:
578 ctx
->Driver
.WaitQuery(ctx
, q
);
579 /* if result is too large for returned type, clamp to max value */
580 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
586 if (q
->Result
> 0x7fffffff) {
587 *params
= 0x7fffffff;
590 *params
= (GLint
)q
->Result
;
594 case GL_QUERY_RESULT_AVAILABLE_ARB
:
596 ctx
->Driver
.CheckQuery( ctx
, q
);
600 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
607 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
609 struct gl_query_object
*q
= NULL
;
610 GET_CURRENT_CONTEXT(ctx
);
611 ASSERT_OUTSIDE_BEGIN_END(ctx
);
613 if (MESA_VERBOSE
& VERBOSE_API
)
614 _mesa_debug(ctx
, "glGetQueryObjectuiv(%u, %s)\n", id
,
615 _mesa_lookup_enum_by_nr(pname
));
618 q
= _mesa_lookup_query_object(ctx
, id
);
620 if (!q
|| q
->Active
) {
621 _mesa_error(ctx
, GL_INVALID_OPERATION
,
622 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
627 case GL_QUERY_RESULT_ARB
:
629 ctx
->Driver
.WaitQuery(ctx
, q
);
630 /* if result is too large for returned type, clamp to max value */
631 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
637 if (q
->Result
> 0xffffffff) {
638 *params
= 0xffffffff;
641 *params
= (GLuint
)q
->Result
;
645 case GL_QUERY_RESULT_AVAILABLE_ARB
:
647 ctx
->Driver
.CheckQuery( ctx
, q
);
651 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
658 * New with GL_EXT_timer_query
661 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
663 struct gl_query_object
*q
= NULL
;
664 GET_CURRENT_CONTEXT(ctx
);
665 ASSERT_OUTSIDE_BEGIN_END(ctx
);
667 if (MESA_VERBOSE
& VERBOSE_API
)
668 _mesa_debug(ctx
, "glGetQueryObjecti64v(%u, %s)\n", id
,
669 _mesa_lookup_enum_by_nr(pname
));
672 q
= _mesa_lookup_query_object(ctx
, id
);
674 if (!q
|| q
->Active
) {
675 _mesa_error(ctx
, GL_INVALID_OPERATION
,
676 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
681 case GL_QUERY_RESULT_ARB
:
683 ctx
->Driver
.WaitQuery(ctx
, q
);
686 case GL_QUERY_RESULT_AVAILABLE_ARB
:
688 ctx
->Driver
.CheckQuery( ctx
, q
);
692 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
699 * New with GL_EXT_timer_query
702 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
704 struct gl_query_object
*q
= NULL
;
705 GET_CURRENT_CONTEXT(ctx
);
706 ASSERT_OUTSIDE_BEGIN_END(ctx
);
708 if (MESA_VERBOSE
& VERBOSE_API
)
709 _mesa_debug(ctx
, "glGetQueryObjectui64v(%u, %s)\n", id
,
710 _mesa_lookup_enum_by_nr(pname
));
713 q
= _mesa_lookup_query_object(ctx
, id
);
715 if (!q
|| q
->Active
) {
716 _mesa_error(ctx
, GL_INVALID_OPERATION
,
717 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
722 case GL_QUERY_RESULT_ARB
:
724 ctx
->Driver
.WaitQuery(ctx
, q
);
727 case GL_QUERY_RESULT_AVAILABLE_ARB
:
729 ctx
->Driver
.CheckQuery( ctx
, q
);
733 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
740 _mesa_init_queryobj_dispatch(const struct gl_context
*ctx
,
741 struct _glapi_table
*disp
)
743 SET_GenQueriesARB(disp
, _mesa_GenQueriesARB
);
744 SET_DeleteQueriesARB(disp
, _mesa_DeleteQueriesARB
);
745 SET_IsQueryARB(disp
, _mesa_IsQueryARB
);
746 SET_BeginQueryARB(disp
, _mesa_BeginQueryARB
);
747 SET_EndQueryARB(disp
, _mesa_EndQueryARB
);
748 SET_GetQueryivARB(disp
, _mesa_GetQueryivARB
);
749 SET_GetQueryObjectuivARB(disp
, _mesa_GetQueryObjectuivARB
);
751 if (_mesa_is_desktop_gl(ctx
)) {
752 SET_GetQueryObjectivARB(disp
, _mesa_GetQueryObjectivARB
);
753 SET_QueryCounter(disp
, _mesa_QueryCounter
);
755 SET_GetQueryObjecti64vEXT(disp
, _mesa_GetQueryObjecti64vEXT
);
756 SET_GetQueryObjectui64vEXT(disp
, _mesa_GetQueryObjectui64vEXT
);
758 SET_BeginQueryIndexed(disp
, _mesa_BeginQueryIndexed
);
759 SET_EndQueryIndexed(disp
, _mesa_EndQueryIndexed
);
760 SET_GetQueryIndexediv(disp
, _mesa_GetQueryIndexediv
);
766 * Allocate/init the context state related to query objects.
769 _mesa_init_queryobj(struct gl_context
*ctx
)
771 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
772 ctx
->Query
.CurrentOcclusionObject
= NULL
;
774 ctx
->Const
.QueryCounterBits
.SamplesPassed
= 64;
775 ctx
->Const
.QueryCounterBits
.TimeElapsed
= 64;
776 ctx
->Const
.QueryCounterBits
.Timestamp
= 64;
777 ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
= 64;
778 ctx
->Const
.QueryCounterBits
.PrimitivesWritten
= 64;
783 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
786 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
788 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
789 struct gl_context
*ctx
= (struct gl_context
*)userData
;
790 ctx
->Driver
.DeleteQuery(ctx
, q
);
795 * Free the context state related to query objects.
798 _mesa_free_queryobj_data(struct gl_context
*ctx
)
800 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
801 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);