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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
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
);
54 /* This is to satisfy the language of the specification: "In the initial
55 * state of a query object, the result is available" (OpenGL 3.1 §
60 /* OpenGL 3.1 § 2.13 says about GenQueries, "These names are marked as
61 * used, but no object is associated with them until the first time they
62 * are used by BeginQuery." Since our implementation actually does
63 * allocate an object at this point, use a flag to indicate that this
64 * object has not yet been bound so should not be considered a query.
66 q
->EverBound
= GL_FALSE
;
73 * Begin a query. Software driver fallback.
74 * Called via ctx->Driver.BeginQuery().
77 _mesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
79 ctx
->NewState
|= _NEW_DEPTH
; /* for swrast */
84 * End a query. Software driver fallback.
85 * Called via ctx->Driver.EndQuery().
88 _mesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
90 ctx
->NewState
|= _NEW_DEPTH
; /* for swrast */
96 * Wait for query to complete. Software driver fallback.
97 * Called via ctx->Driver.WaitQuery().
100 _mesa_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
102 /* For software drivers, _mesa_end_query() should have completed the query.
103 * For real hardware, implement a proper WaitQuery() driver function,
104 * which may require issuing a flush.
111 * Check if a query results are ready. Software driver fallback.
112 * Called via ctx->Driver.CheckQuery().
115 _mesa_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
117 /* No-op for sw rendering.
118 * HW drivers may need to flush at this time.
124 * Delete a query object. Called via ctx->Driver.DeleteQuery().
125 * Not removed from hash table here.
128 _mesa_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
135 _mesa_init_query_object_functions(struct dd_function_table
*driver
)
137 driver
->NewQueryObject
= _mesa_new_query_object
;
138 driver
->DeleteQuery
= _mesa_delete_query
;
139 driver
->BeginQuery
= _mesa_begin_query
;
140 driver
->EndQuery
= _mesa_end_query
;
141 driver
->WaitQuery
= _mesa_wait_query
;
142 driver
->CheckQuery
= _mesa_check_query
;
147 * Return pointer to the query object binding point for the given target.
148 * \return NULL if invalid target, else the address of binding point
150 static struct gl_query_object
**
151 get_query_binding_point(struct gl_context
*ctx
, GLenum target
)
154 case GL_SAMPLES_PASSED_ARB
:
155 if (ctx
->Extensions
.ARB_occlusion_query
)
156 return &ctx
->Query
.CurrentOcclusionObject
;
159 case GL_ANY_SAMPLES_PASSED
:
160 if (ctx
->Extensions
.ARB_occlusion_query2
)
161 return &ctx
->Query
.CurrentOcclusionObject
;
164 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
165 if (ctx
->Extensions
.ARB_ES3_compatibility
166 || (ctx
->API
== API_OPENGLES2
&& ctx
->Version
>= 30))
167 return &ctx
->Query
.CurrentOcclusionObject
;
170 case GL_TIME_ELAPSED_EXT
:
171 if (ctx
->Extensions
.EXT_timer_query
)
172 return &ctx
->Query
.CurrentTimerObject
;
175 case GL_PRIMITIVES_GENERATED
:
176 if (ctx
->Extensions
.EXT_transform_feedback
)
177 return &ctx
->Query
.PrimitivesGenerated
;
180 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
181 if (ctx
->Extensions
.EXT_transform_feedback
)
182 return &ctx
->Query
.PrimitivesWritten
;
192 _mesa_GenQueries(GLsizei n
, GLuint
*ids
)
195 GET_CURRENT_CONTEXT(ctx
);
197 if (MESA_VERBOSE
& VERBOSE_API
)
198 _mesa_debug(ctx
, "glGenQueries(%d)\n", n
);
201 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
205 /* No query objects can be active at this time! */
206 if (ctx
->Query
.CurrentOcclusionObject
||
207 ctx
->Query
.CurrentTimerObject
) {
208 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
212 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
215 for (i
= 0; i
< n
; i
++) {
216 struct gl_query_object
*q
217 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
219 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
223 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
230 _mesa_DeleteQueries(GLsizei n
, const GLuint
*ids
)
233 GET_CURRENT_CONTEXT(ctx
);
234 FLUSH_VERTICES(ctx
, 0);
236 if (MESA_VERBOSE
& VERBOSE_API
)
237 _mesa_debug(ctx
, "glDeleteQueries(%d)\n", n
);
240 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
244 /* No query objects can be active at this time! */
245 if (ctx
->Query
.CurrentOcclusionObject
||
246 ctx
->Query
.CurrentTimerObject
) {
247 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
251 for (i
= 0; i
< n
; i
++) {
253 struct gl_query_object
*q
= _mesa_lookup_query_object(ctx
, ids
[i
]);
255 ASSERT(!q
->Active
); /* should be caught earlier */
256 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
257 ctx
->Driver
.DeleteQuery(ctx
, q
);
265 _mesa_IsQuery(GLuint id
)
267 struct gl_query_object
*q
;
269 GET_CURRENT_CONTEXT(ctx
);
270 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
272 if (MESA_VERBOSE
& VERBOSE_API
)
273 _mesa_debug(ctx
, "glIsQuery(%u)\n", id
);
278 q
= _mesa_lookup_query_object(ctx
, id
);
286 query_error_check_index(struct gl_context
*ctx
, GLenum target
, GLuint index
)
289 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
290 case GL_PRIMITIVES_GENERATED
:
291 if (index
>= ctx
->Const
.MaxVertexStreams
) {
292 _mesa_error(ctx
, GL_INVALID_VALUE
,
293 "glBeginQueryIndexed(index>=MaxVertexStreams)");
299 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBeginQueryIndexed(index>0)");
307 _mesa_BeginQueryIndexed(GLenum target
, GLuint index
, GLuint id
)
309 struct gl_query_object
*q
, **bindpt
;
310 GET_CURRENT_CONTEXT(ctx
);
312 if (MESA_VERBOSE
& VERBOSE_API
)
313 _mesa_debug(ctx
, "glBeginQueryIndexed(%s, %u, %u)\n",
314 _mesa_lookup_enum_by_nr(target
), index
, id
);
316 if (!query_error_check_index(ctx
, target
, index
))
319 FLUSH_VERTICES(ctx
, 0);
321 bindpt
= get_query_binding_point(ctx
, target
);
323 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQuery{Indexed}(target)");
327 /* From the GL_ARB_occlusion_query spec:
329 * "If BeginQueryARB is called while another query is already in
330 * progress with the same target, an INVALID_OPERATION error is
334 _mesa_error(ctx
, GL_INVALID_OPERATION
,
335 "glBeginQuery{Indexed}(target=%s is active)",
336 _mesa_lookup_enum_by_nr(target
));
341 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQuery{Indexed}(id==0)");
345 q
= _mesa_lookup_query_object(ctx
, id
);
347 if (ctx
->API
!= API_OPENGL_COMPAT
) {
348 _mesa_error(ctx
, GL_INVALID_OPERATION
,
349 "glBeginQuery{Indexed}(non-gen name)");
352 /* create new object */
353 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
355 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQuery{Indexed}");
358 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
362 /* pre-existing object */
364 _mesa_error(ctx
, GL_INVALID_OPERATION
,
365 "glBeginQuery{Indexed}(query already active)");
374 q
->EverBound
= GL_TRUE
;
376 /* XXX should probably refcount query objects */
379 ctx
->Driver
.BeginQuery(ctx
, q
);
384 _mesa_EndQueryIndexed(GLenum target
, GLuint index
)
386 struct gl_query_object
*q
, **bindpt
;
387 GET_CURRENT_CONTEXT(ctx
);
389 if (MESA_VERBOSE
& VERBOSE_API
)
390 _mesa_debug(ctx
, "glEndQueryIndexed(%s, %u)\n",
391 _mesa_lookup_enum_by_nr(target
), index
);
393 if (!query_error_check_index(ctx
, target
, index
))
396 FLUSH_VERTICES(ctx
, 0);
398 bindpt
= get_query_binding_point(ctx
, target
);
400 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQuery{Indexed}(target)");
404 /* XXX should probably refcount query objects */
407 /* Check for GL_ANY_SAMPLES_PASSED vs GL_SAMPLES_PASSED. */
408 if (q
&& q
->Target
!= target
) {
409 _mesa_error(ctx
, GL_INVALID_OPERATION
,
410 "glEndQuery(target=%s with active query of target %s)",
411 _mesa_lookup_enum_by_nr(target
),
412 _mesa_lookup_enum_by_nr(q
->Target
));
418 if (!q
|| !q
->Active
) {
419 _mesa_error(ctx
, GL_INVALID_OPERATION
,
420 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
424 q
->Active
= GL_FALSE
;
425 ctx
->Driver
.EndQuery(ctx
, q
);
429 _mesa_BeginQuery(GLenum target
, GLuint id
)
431 _mesa_BeginQueryIndexed(target
, 0, id
);
435 _mesa_EndQuery(GLenum target
)
437 _mesa_EndQueryIndexed(target
, 0);
441 _mesa_QueryCounter(GLuint id
, GLenum target
)
443 struct gl_query_object
*q
;
444 GET_CURRENT_CONTEXT(ctx
);
446 if (MESA_VERBOSE
& VERBOSE_API
)
447 _mesa_debug(ctx
, "glQueryCounter(%u, %s)\n", id
,
448 _mesa_lookup_enum_by_nr(target
));
451 if (target
!= GL_TIMESTAMP
) {
452 _mesa_error(ctx
, GL_INVALID_ENUM
, "glQueryCounter(target)");
457 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id==0)");
461 q
= _mesa_lookup_query_object(ctx
, id
);
463 /* XXX the Core profile should throw INVALID_OPERATION here */
465 /* create new object */
466 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
468 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glQueryCounter");
471 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
474 if (q
->Target
&& q
->Target
!= GL_TIMESTAMP
) {
475 _mesa_error(ctx
, GL_INVALID_OPERATION
,
476 "glQueryCounter(id has an invalid target)");
482 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id is active)");
490 if (ctx
->Driver
.QueryCounter
) {
491 ctx
->Driver
.QueryCounter(ctx
, q
);
493 /* QueryCounter is implemented using EndQuery without BeginQuery
494 * in drivers. This is actually Direct3D and Gallium convention.
496 ctx
->Driver
.EndQuery(ctx
, q
);
502 _mesa_GetQueryIndexediv(GLenum target
, GLuint index
, GLenum pname
,
505 struct gl_query_object
*q
= NULL
, **bindpt
= NULL
;
506 GET_CURRENT_CONTEXT(ctx
);
508 if (MESA_VERBOSE
& VERBOSE_API
)
509 _mesa_debug(ctx
, "glGetQueryIndexediv(%s, %u, %s)\n",
510 _mesa_lookup_enum_by_nr(target
),
512 _mesa_lookup_enum_by_nr(pname
));
514 if (!query_error_check_index(ctx
, target
, index
))
517 if (target
== GL_TIMESTAMP
) {
518 if (!ctx
->Extensions
.ARB_timer_query
) {
519 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
524 bindpt
= get_query_binding_point(ctx
, target
);
526 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(target)");
534 case GL_QUERY_COUNTER_BITS_ARB
:
536 case GL_SAMPLES_PASSED
:
537 *params
= ctx
->Const
.QueryCounterBits
.SamplesPassed
;
539 case GL_ANY_SAMPLES_PASSED
:
540 /* The minimum value of this is 1 if it's nonzero, and the value
541 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
546 case GL_TIME_ELAPSED
:
547 *params
= ctx
->Const
.QueryCounterBits
.TimeElapsed
;
550 *params
= ctx
->Const
.QueryCounterBits
.Timestamp
;
552 case GL_PRIMITIVES_GENERATED
:
553 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
;
555 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
556 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesWritten
;
560 "Unknown target in glGetQueryIndexediv(target = %s)",
561 _mesa_lookup_enum_by_nr(target
));
566 case GL_CURRENT_QUERY_ARB
:
567 *params
= (q
&& q
->Target
== target
) ? q
->Id
: 0;
570 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(pname)");
576 _mesa_GetQueryiv(GLenum target
, GLenum pname
, GLint
*params
)
578 _mesa_GetQueryIndexediv(target
, 0, pname
, params
);
582 _mesa_GetQueryObjectiv(GLuint id
, GLenum pname
, GLint
*params
)
584 struct gl_query_object
*q
= NULL
;
585 GET_CURRENT_CONTEXT(ctx
);
587 if (MESA_VERBOSE
& VERBOSE_API
)
588 _mesa_debug(ctx
, "glGetQueryObjectiv(%u, %s)\n", id
,
589 _mesa_lookup_enum_by_nr(pname
));
592 q
= _mesa_lookup_query_object(ctx
, id
);
594 if (!q
|| q
->Active
) {
595 _mesa_error(ctx
, GL_INVALID_OPERATION
,
596 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
601 case GL_QUERY_RESULT_ARB
:
603 ctx
->Driver
.WaitQuery(ctx
, q
);
604 /* if result is too large for returned type, clamp to max value */
605 if (q
->Target
== GL_ANY_SAMPLES_PASSED
606 || q
->Target
== GL_ANY_SAMPLES_PASSED_CONSERVATIVE
) {
612 if (q
->Result
> 0x7fffffff) {
613 *params
= 0x7fffffff;
616 *params
= (GLint
)q
->Result
;
620 case GL_QUERY_RESULT_AVAILABLE_ARB
:
622 ctx
->Driver
.CheckQuery( ctx
, q
);
626 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
633 _mesa_GetQueryObjectuiv(GLuint id
, GLenum pname
, GLuint
*params
)
635 struct gl_query_object
*q
= NULL
;
636 GET_CURRENT_CONTEXT(ctx
);
638 if (MESA_VERBOSE
& VERBOSE_API
)
639 _mesa_debug(ctx
, "glGetQueryObjectuiv(%u, %s)\n", id
,
640 _mesa_lookup_enum_by_nr(pname
));
643 q
= _mesa_lookup_query_object(ctx
, id
);
645 if (!q
|| q
->Active
) {
646 _mesa_error(ctx
, GL_INVALID_OPERATION
,
647 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
652 case GL_QUERY_RESULT_ARB
:
654 ctx
->Driver
.WaitQuery(ctx
, q
);
655 /* if result is too large for returned type, clamp to max value */
656 if (q
->Target
== GL_ANY_SAMPLES_PASSED
657 || q
->Target
== GL_ANY_SAMPLES_PASSED_CONSERVATIVE
) {
663 if (q
->Result
> 0xffffffff) {
664 *params
= 0xffffffff;
667 *params
= (GLuint
)q
->Result
;
671 case GL_QUERY_RESULT_AVAILABLE_ARB
:
673 ctx
->Driver
.CheckQuery( ctx
, q
);
677 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
684 * New with GL_EXT_timer_query
687 _mesa_GetQueryObjecti64v(GLuint id
, GLenum pname
, GLint64EXT
*params
)
689 struct gl_query_object
*q
= NULL
;
690 GET_CURRENT_CONTEXT(ctx
);
692 if (MESA_VERBOSE
& VERBOSE_API
)
693 _mesa_debug(ctx
, "glGetQueryObjecti64v(%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 "glGetQueryObjectui64vARB(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
, "glGetQueryObjecti64vARB(pname)");
724 * New with GL_EXT_timer_query
727 _mesa_GetQueryObjectui64v(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
729 struct gl_query_object
*q
= NULL
;
730 GET_CURRENT_CONTEXT(ctx
);
732 if (MESA_VERBOSE
& VERBOSE_API
)
733 _mesa_debug(ctx
, "glGetQueryObjectui64v(%u, %s)\n", id
,
734 _mesa_lookup_enum_by_nr(pname
));
737 q
= _mesa_lookup_query_object(ctx
, id
);
739 if (!q
|| q
->Active
) {
740 _mesa_error(ctx
, GL_INVALID_OPERATION
,
741 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
746 case GL_QUERY_RESULT_ARB
:
748 ctx
->Driver
.WaitQuery(ctx
, q
);
751 case GL_QUERY_RESULT_AVAILABLE_ARB
:
753 ctx
->Driver
.CheckQuery( ctx
, q
);
757 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
763 * Allocate/init the context state related to query objects.
766 _mesa_init_queryobj(struct gl_context
*ctx
)
768 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
769 ctx
->Query
.CurrentOcclusionObject
= NULL
;
771 ctx
->Const
.QueryCounterBits
.SamplesPassed
= 64;
772 ctx
->Const
.QueryCounterBits
.TimeElapsed
= 64;
773 ctx
->Const
.QueryCounterBits
.Timestamp
= 64;
774 ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
= 64;
775 ctx
->Const
.QueryCounterBits
.PrimitivesWritten
= 64;
780 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
783 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
785 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
786 struct gl_context
*ctx
= (struct gl_context
*)userData
;
787 ctx
->Driver
.DeleteQuery(ctx
, q
);
792 * Free the context state related to query objects.
795 _mesa_free_queryobj_data(struct gl_context
*ctx
)
797 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
798 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);