2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
33 #include "main/dispatch.h"
37 * Allocate a new query object. This is a fallback routine called via
38 * ctx->Driver.NewQueryObject().
39 * \param ctx - rendering context
40 * \param id - the new object's ID
41 * \return pointer to new query_object object or NULL if out of memory.
43 static struct gl_query_object
*
44 _mesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
46 struct gl_query_object
*q
= CALLOC_STRUCT(gl_query_object
);
53 /* This is to satisfy the language of the specification: "In the initial
54 * state of a query object, the result is available" (OpenGL 3.1 §
59 /* OpenGL 3.1 § 2.13 says about GenQueries, "These names are marked as
60 * used, but no object is associated with them until the first time they
61 * are used by BeginQuery." Since our implementation actually does
62 * allocate an object at this point, use a flag to indicate that this
63 * object has not yet been bound so should not be considered a query.
65 q
->EverBound
= GL_FALSE
;
72 * Begin a query. Software driver fallback.
73 * Called via ctx->Driver.BeginQuery().
76 _mesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
78 ctx
->NewState
|= _NEW_DEPTH
; /* for swrast */
83 * End a query. Software driver fallback.
84 * Called via ctx->Driver.EndQuery().
87 _mesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
89 ctx
->NewState
|= _NEW_DEPTH
; /* for swrast */
95 * Wait for query to complete. Software driver fallback.
96 * Called via ctx->Driver.WaitQuery().
99 _mesa_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
101 /* For software drivers, _mesa_end_query() should have completed the query.
102 * For real hardware, implement a proper WaitQuery() driver function,
103 * which may require issuing a flush.
110 * Check if a query results are ready. Software driver fallback.
111 * Called via ctx->Driver.CheckQuery().
114 _mesa_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
116 /* No-op for sw rendering.
117 * HW drivers may need to flush at this time.
123 * Delete a query object. Called via ctx->Driver.DeleteQuery().
124 * Not removed from hash table here.
127 _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 and
149 * \return NULL if invalid target, else the address of binding point
151 static struct gl_query_object
**
152 get_query_binding_point(struct gl_context
*ctx
, GLenum target
, GLuint index
)
155 case GL_SAMPLES_PASSED_ARB
:
156 if (ctx
->Extensions
.ARB_occlusion_query
)
157 return &ctx
->Query
.CurrentOcclusionObject
;
160 case GL_ANY_SAMPLES_PASSED
:
161 if (ctx
->Extensions
.ARB_occlusion_query2
)
162 return &ctx
->Query
.CurrentOcclusionObject
;
165 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
166 if (ctx
->Extensions
.ARB_ES3_compatibility
167 || (ctx
->API
== API_OPENGLES2
&& ctx
->Version
>= 30))
168 return &ctx
->Query
.CurrentOcclusionObject
;
171 case GL_TIME_ELAPSED_EXT
:
172 if (ctx
->Extensions
.EXT_timer_query
)
173 return &ctx
->Query
.CurrentTimerObject
;
176 case GL_PRIMITIVES_GENERATED
:
177 if (ctx
->Extensions
.EXT_transform_feedback
)
178 return &ctx
->Query
.PrimitivesGenerated
[index
];
181 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
182 if (ctx
->Extensions
.EXT_transform_feedback
)
183 return &ctx
->Query
.PrimitivesWritten
[index
];
193 _mesa_GenQueries(GLsizei n
, GLuint
*ids
)
196 GET_CURRENT_CONTEXT(ctx
);
198 if (MESA_VERBOSE
& VERBOSE_API
)
199 _mesa_debug(ctx
, "glGenQueries(%d)\n", n
);
202 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
206 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
209 for (i
= 0; i
< n
; i
++) {
210 struct gl_query_object
*q
211 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
213 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
217 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
224 _mesa_DeleteQueries(GLsizei n
, const GLuint
*ids
)
227 GET_CURRENT_CONTEXT(ctx
);
228 FLUSH_VERTICES(ctx
, 0);
230 if (MESA_VERBOSE
& VERBOSE_API
)
231 _mesa_debug(ctx
, "glDeleteQueries(%d)\n", n
);
234 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
238 for (i
= 0; i
< n
; i
++) {
240 struct gl_query_object
*q
= _mesa_lookup_query_object(ctx
, ids
[i
]);
243 struct gl_query_object
**bindpt
;
244 bindpt
= get_query_binding_point(ctx
, q
->Target
, q
->Stream
);
245 assert(bindpt
); /* Should be non-null for active q. */
249 q
->Active
= GL_FALSE
;
250 ctx
->Driver
.EndQuery(ctx
, q
);
252 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
253 ctx
->Driver
.DeleteQuery(ctx
, q
);
261 _mesa_IsQuery(GLuint id
)
263 struct gl_query_object
*q
;
265 GET_CURRENT_CONTEXT(ctx
);
266 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
268 if (MESA_VERBOSE
& VERBOSE_API
)
269 _mesa_debug(ctx
, "glIsQuery(%u)\n", id
);
274 q
= _mesa_lookup_query_object(ctx
, id
);
282 query_error_check_index(struct gl_context
*ctx
, GLenum target
, GLuint index
)
285 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
286 case GL_PRIMITIVES_GENERATED
:
287 if (index
>= ctx
->Const
.MaxVertexStreams
) {
288 _mesa_error(ctx
, GL_INVALID_VALUE
,
289 "glBeginQueryIndexed(index>=MaxVertexStreams)");
295 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBeginQueryIndexed(index>0)");
303 _mesa_BeginQueryIndexed(GLenum target
, GLuint index
, GLuint id
)
305 struct gl_query_object
*q
, **bindpt
;
306 GET_CURRENT_CONTEXT(ctx
);
308 if (MESA_VERBOSE
& VERBOSE_API
)
309 _mesa_debug(ctx
, "glBeginQueryIndexed(%s, %u, %u)\n",
310 _mesa_lookup_enum_by_nr(target
), index
, id
);
312 if (!query_error_check_index(ctx
, target
, index
))
315 FLUSH_VERTICES(ctx
, 0);
317 bindpt
= get_query_binding_point(ctx
, target
, index
);
319 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQuery{Indexed}(target)");
323 /* From the GL_ARB_occlusion_query spec:
325 * "If BeginQueryARB is called while another query is already in
326 * progress with the same target, an INVALID_OPERATION error is
330 _mesa_error(ctx
, GL_INVALID_OPERATION
,
331 "glBeginQuery{Indexed}(target=%s is active)",
332 _mesa_lookup_enum_by_nr(target
));
337 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQuery{Indexed}(id==0)");
341 q
= _mesa_lookup_query_object(ctx
, id
);
343 if (ctx
->API
!= API_OPENGL_COMPAT
) {
344 _mesa_error(ctx
, GL_INVALID_OPERATION
,
345 "glBeginQuery{Indexed}(non-gen name)");
348 /* create new object */
349 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
351 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQuery{Indexed}");
354 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
358 /* pre-existing object */
360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
361 "glBeginQuery{Indexed}(query already active)");
370 q
->EverBound
= GL_TRUE
;
373 /* XXX should probably refcount query objects */
376 ctx
->Driver
.BeginQuery(ctx
, q
);
381 _mesa_EndQueryIndexed(GLenum target
, GLuint index
)
383 struct gl_query_object
*q
, **bindpt
;
384 GET_CURRENT_CONTEXT(ctx
);
386 if (MESA_VERBOSE
& VERBOSE_API
)
387 _mesa_debug(ctx
, "glEndQueryIndexed(%s, %u)\n",
388 _mesa_lookup_enum_by_nr(target
), index
);
390 if (!query_error_check_index(ctx
, target
, index
))
393 FLUSH_VERTICES(ctx
, 0);
395 bindpt
= get_query_binding_point(ctx
, target
, index
);
397 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQuery{Indexed}(target)");
401 /* XXX should probably refcount query objects */
404 /* Check for GL_ANY_SAMPLES_PASSED vs GL_SAMPLES_PASSED. */
405 if (q
&& q
->Target
!= target
) {
406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
407 "glEndQuery(target=%s with active query of target %s)",
408 _mesa_lookup_enum_by_nr(target
),
409 _mesa_lookup_enum_by_nr(q
->Target
));
415 if (!q
|| !q
->Active
) {
416 _mesa_error(ctx
, GL_INVALID_OPERATION
,
417 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
421 q
->Active
= GL_FALSE
;
422 ctx
->Driver
.EndQuery(ctx
, q
);
426 _mesa_BeginQuery(GLenum target
, GLuint id
)
428 _mesa_BeginQueryIndexed(target
, 0, id
);
432 _mesa_EndQuery(GLenum target
)
434 _mesa_EndQueryIndexed(target
, 0);
438 _mesa_QueryCounter(GLuint id
, GLenum target
)
440 struct gl_query_object
*q
;
441 GET_CURRENT_CONTEXT(ctx
);
443 if (MESA_VERBOSE
& VERBOSE_API
)
444 _mesa_debug(ctx
, "glQueryCounter(%u, %s)\n", id
,
445 _mesa_lookup_enum_by_nr(target
));
448 if (target
!= GL_TIMESTAMP
) {
449 _mesa_error(ctx
, GL_INVALID_ENUM
, "glQueryCounter(target)");
454 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id==0)");
458 q
= _mesa_lookup_query_object(ctx
, id
);
460 /* XXX the Core profile should throw INVALID_OPERATION here */
462 /* create new object */
463 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
465 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glQueryCounter");
468 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
471 if (q
->Target
&& q
->Target
!= GL_TIMESTAMP
) {
472 _mesa_error(ctx
, GL_INVALID_OPERATION
,
473 "glQueryCounter(id has an invalid target)");
479 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id is active)");
486 q
->EverBound
= GL_TRUE
;
488 if (ctx
->Driver
.QueryCounter
) {
489 ctx
->Driver
.QueryCounter(ctx
, q
);
491 /* QueryCounter is implemented using EndQuery without BeginQuery
492 * in drivers. This is actually Direct3D and Gallium convention.
494 ctx
->Driver
.EndQuery(ctx
, q
);
500 _mesa_GetQueryIndexediv(GLenum target
, GLuint index
, GLenum pname
,
503 struct gl_query_object
*q
= NULL
, **bindpt
= NULL
;
504 GET_CURRENT_CONTEXT(ctx
);
506 if (MESA_VERBOSE
& VERBOSE_API
)
507 _mesa_debug(ctx
, "glGetQueryIndexediv(%s, %u, %s)\n",
508 _mesa_lookup_enum_by_nr(target
),
510 _mesa_lookup_enum_by_nr(pname
));
512 if (!query_error_check_index(ctx
, target
, index
))
515 if (target
== GL_TIMESTAMP
) {
516 if (!ctx
->Extensions
.ARB_timer_query
) {
517 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
522 bindpt
= get_query_binding_point(ctx
, target
, index
);
524 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(target)");
532 case GL_QUERY_COUNTER_BITS_ARB
:
534 case GL_SAMPLES_PASSED
:
535 *params
= ctx
->Const
.QueryCounterBits
.SamplesPassed
;
537 case GL_ANY_SAMPLES_PASSED
:
538 /* The minimum value of this is 1 if it's nonzero, and the value
539 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
544 case GL_TIME_ELAPSED
:
545 *params
= ctx
->Const
.QueryCounterBits
.TimeElapsed
;
548 *params
= ctx
->Const
.QueryCounterBits
.Timestamp
;
550 case GL_PRIMITIVES_GENERATED
:
551 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
;
553 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
554 *params
= ctx
->Const
.QueryCounterBits
.PrimitivesWritten
;
558 "Unknown target in glGetQueryIndexediv(target = %s)",
559 _mesa_lookup_enum_by_nr(target
));
564 case GL_CURRENT_QUERY_ARB
:
565 *params
= (q
&& q
->Target
== target
) ? q
->Id
: 0;
568 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQuery{Indexed}iv(pname)");
574 _mesa_GetQueryiv(GLenum target
, GLenum pname
, GLint
*params
)
576 _mesa_GetQueryIndexediv(target
, 0, pname
, params
);
580 _mesa_GetQueryObjectiv(GLuint id
, GLenum pname
, GLint
*params
)
582 struct gl_query_object
*q
= NULL
;
583 GET_CURRENT_CONTEXT(ctx
);
585 if (MESA_VERBOSE
& VERBOSE_API
)
586 _mesa_debug(ctx
, "glGetQueryObjectiv(%u, %s)\n", id
,
587 _mesa_lookup_enum_by_nr(pname
));
590 q
= _mesa_lookup_query_object(ctx
, id
);
592 if (!q
|| q
->Active
) {
593 _mesa_error(ctx
, GL_INVALID_OPERATION
,
594 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
599 case GL_QUERY_RESULT_ARB
:
601 ctx
->Driver
.WaitQuery(ctx
, q
);
602 /* if result is too large for returned type, clamp to max value */
603 if (q
->Target
== GL_ANY_SAMPLES_PASSED
604 || q
->Target
== GL_ANY_SAMPLES_PASSED_CONSERVATIVE
) {
610 if (q
->Result
> 0x7fffffff) {
611 *params
= 0x7fffffff;
614 *params
= (GLint
)q
->Result
;
618 case GL_QUERY_RESULT_AVAILABLE_ARB
:
620 ctx
->Driver
.CheckQuery( ctx
, q
);
624 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
631 _mesa_GetQueryObjectuiv(GLuint id
, GLenum pname
, GLuint
*params
)
633 struct gl_query_object
*q
= NULL
;
634 GET_CURRENT_CONTEXT(ctx
);
636 if (MESA_VERBOSE
& VERBOSE_API
)
637 _mesa_debug(ctx
, "glGetQueryObjectuiv(%u, %s)\n", id
,
638 _mesa_lookup_enum_by_nr(pname
));
641 q
= _mesa_lookup_query_object(ctx
, id
);
643 if (!q
|| q
->Active
) {
644 _mesa_error(ctx
, GL_INVALID_OPERATION
,
645 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
650 case GL_QUERY_RESULT_ARB
:
652 ctx
->Driver
.WaitQuery(ctx
, q
);
653 /* if result is too large for returned type, clamp to max value */
654 if (q
->Target
== GL_ANY_SAMPLES_PASSED
655 || q
->Target
== GL_ANY_SAMPLES_PASSED_CONSERVATIVE
) {
661 if (q
->Result
> 0xffffffff) {
662 *params
= 0xffffffff;
665 *params
= (GLuint
)q
->Result
;
669 case GL_QUERY_RESULT_AVAILABLE_ARB
:
671 ctx
->Driver
.CheckQuery( ctx
, q
);
675 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
682 * New with GL_EXT_timer_query
685 _mesa_GetQueryObjecti64v(GLuint id
, GLenum pname
, GLint64EXT
*params
)
687 struct gl_query_object
*q
= NULL
;
688 GET_CURRENT_CONTEXT(ctx
);
690 if (MESA_VERBOSE
& VERBOSE_API
)
691 _mesa_debug(ctx
, "glGetQueryObjecti64v(%u, %s)\n", id
,
692 _mesa_lookup_enum_by_nr(pname
));
695 q
= _mesa_lookup_query_object(ctx
, id
);
697 if (!q
|| q
->Active
) {
698 _mesa_error(ctx
, GL_INVALID_OPERATION
,
699 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
704 case GL_QUERY_RESULT_ARB
:
706 ctx
->Driver
.WaitQuery(ctx
, q
);
709 case GL_QUERY_RESULT_AVAILABLE_ARB
:
711 ctx
->Driver
.CheckQuery( ctx
, q
);
715 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
722 * New with GL_EXT_timer_query
725 _mesa_GetQueryObjectui64v(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
727 struct gl_query_object
*q
= NULL
;
728 GET_CURRENT_CONTEXT(ctx
);
730 if (MESA_VERBOSE
& VERBOSE_API
)
731 _mesa_debug(ctx
, "glGetQueryObjectui64v(%u, %s)\n", id
,
732 _mesa_lookup_enum_by_nr(pname
));
735 q
= _mesa_lookup_query_object(ctx
, id
);
737 if (!q
|| q
->Active
) {
738 _mesa_error(ctx
, GL_INVALID_OPERATION
,
739 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
744 case GL_QUERY_RESULT_ARB
:
746 ctx
->Driver
.WaitQuery(ctx
, q
);
749 case GL_QUERY_RESULT_AVAILABLE_ARB
:
751 ctx
->Driver
.CheckQuery( ctx
, q
);
755 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
761 * Allocate/init the context state related to query objects.
764 _mesa_init_queryobj(struct gl_context
*ctx
)
766 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
767 ctx
->Query
.CurrentOcclusionObject
= NULL
;
769 ctx
->Const
.QueryCounterBits
.SamplesPassed
= 64;
770 ctx
->Const
.QueryCounterBits
.TimeElapsed
= 64;
771 ctx
->Const
.QueryCounterBits
.Timestamp
= 64;
772 ctx
->Const
.QueryCounterBits
.PrimitivesGenerated
= 64;
773 ctx
->Const
.QueryCounterBits
.PrimitivesWritten
= 64;
778 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
781 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
783 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
784 struct gl_context
*ctx
= (struct gl_context
*)userData
;
785 ctx
->Driver
.DeleteQuery(ctx
, q
);
790 * Free the context state related to query objects.
793 _mesa_free_queryobj_data(struct gl_context
*ctx
)
795 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
796 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);