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"
41 * Allocate a new query object. This is a fallback routine called via
42 * ctx->Driver.NewQueryObject().
43 * \param ctx - rendering context
44 * \param id - the new object's ID
45 * \return pointer to new query_object object or NULL if out of memory.
47 static struct gl_query_object
*
48 _mesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
50 struct gl_query_object
*q
= MALLOC_STRUCT(gl_query_object
);
56 q
->Ready
= GL_TRUE
; /* correct, see spec */
63 * Begin a query. Software driver fallback.
64 * Called via ctx->Driver.BeginQuery().
67 _mesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
74 * End a query. Software driver fallback.
75 * Called via ctx->Driver.EndQuery().
78 _mesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
85 * Wait for query to complete. Software driver fallback.
86 * Called via ctx->Driver.WaitQuery().
89 _mesa_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
91 /* For software drivers, _mesa_end_query() should have completed the query.
92 * For real hardware, implement a proper WaitQuery() driver function,
93 * which may require issuing a flush.
100 * Check if a query results are ready. Software driver fallback.
101 * Called via ctx->Driver.CheckQuery().
104 _mesa_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
106 /* No-op for sw rendering.
107 * HW drivers may need to flush at this time.
113 * Delete a query object. Called via ctx->Driver.DeleteQuery().
114 * Not removed from hash table here.
117 _mesa_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
124 _mesa_init_query_object_functions(struct dd_function_table
*driver
)
126 driver
->NewQueryObject
= _mesa_new_query_object
;
127 driver
->DeleteQuery
= _mesa_delete_query
;
128 driver
->BeginQuery
= _mesa_begin_query
;
129 driver
->EndQuery
= _mesa_end_query
;
130 driver
->WaitQuery
= _mesa_wait_query
;
131 driver
->CheckQuery
= _mesa_check_query
;
136 * Return pointer to the query object binding point for the given target.
137 * \return NULL if invalid target, else the address of binding point
139 static struct gl_query_object
**
140 get_query_binding_point(struct gl_context
*ctx
, GLenum target
)
143 case GL_SAMPLES_PASSED_ARB
:
144 if (ctx
->Extensions
.ARB_occlusion_query
)
145 return &ctx
->Query
.CurrentOcclusionObject
;
148 case GL_ANY_SAMPLES_PASSED
:
149 if (ctx
->Extensions
.ARB_occlusion_query2
)
150 return &ctx
->Query
.CurrentOcclusionObject
;
153 case GL_TIME_ELAPSED_EXT
:
154 if (ctx
->Extensions
.EXT_timer_query
)
155 return &ctx
->Query
.CurrentTimerObject
;
158 #if FEATURE_EXT_transform_feedback
159 case GL_PRIMITIVES_GENERATED
:
160 if (ctx
->Extensions
.EXT_transform_feedback
)
161 return &ctx
->Query
.PrimitivesGenerated
;
164 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
165 if (ctx
->Extensions
.EXT_transform_feedback
)
166 return &ctx
->Query
.PrimitivesWritten
;
176 static void GLAPIENTRY
177 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
180 GET_CURRENT_CONTEXT(ctx
);
181 ASSERT_OUTSIDE_BEGIN_END(ctx
);
183 if (MESA_VERBOSE
& VERBOSE_API
)
184 _mesa_debug(ctx
, "glGenQueries(%d)\n", n
);
187 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
191 /* No query objects can be active at this time! */
192 if (ctx
->Query
.CurrentOcclusionObject
||
193 ctx
->Query
.CurrentTimerObject
) {
194 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
198 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
201 for (i
= 0; i
< n
; i
++) {
202 struct gl_query_object
*q
203 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
205 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
209 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
215 static void GLAPIENTRY
216 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
219 GET_CURRENT_CONTEXT(ctx
);
220 ASSERT_OUTSIDE_BEGIN_END(ctx
);
221 FLUSH_VERTICES(ctx
, 0);
223 if (MESA_VERBOSE
& VERBOSE_API
)
224 _mesa_debug(ctx
, "glDeleeteQueries(%d)\n", n
);
227 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
231 /* No query objects can be active at this time! */
232 if (ctx
->Query
.CurrentOcclusionObject
||
233 ctx
->Query
.CurrentTimerObject
) {
234 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
238 for (i
= 0; i
< n
; i
++) {
240 struct gl_query_object
*q
= _mesa_lookup_query_object(ctx
, ids
[i
]);
242 ASSERT(!q
->Active
); /* should be caught earlier */
243 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
244 ctx
->Driver
.DeleteQuery(ctx
, q
);
251 static GLboolean GLAPIENTRY
252 _mesa_IsQueryARB(GLuint id
)
254 GET_CURRENT_CONTEXT(ctx
);
255 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
257 if (MESA_VERBOSE
& VERBOSE_API
)
258 _mesa_debug(ctx
, "glIsQuery(%u)\n", id
);
260 if (id
&& _mesa_lookup_query_object(ctx
, id
))
267 static void GLAPIENTRY
268 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
270 struct gl_query_object
*q
, **bindpt
;
271 GET_CURRENT_CONTEXT(ctx
);
272 ASSERT_OUTSIDE_BEGIN_END(ctx
);
274 if (MESA_VERBOSE
& VERBOSE_API
)
275 _mesa_debug(ctx
, "glBeginQuery(%s, %u)\n",
276 _mesa_lookup_enum_by_nr(target
), id
);
278 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
280 bindpt
= get_query_binding_point(ctx
, target
);
282 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
287 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
291 q
= _mesa_lookup_query_object(ctx
, id
);
293 /* create new object */
294 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
296 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
299 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
302 /* pre-existing object */
304 _mesa_error(ctx
, GL_INVALID_OPERATION
,
305 "glBeginQueryARB(query already active)");
315 /* XXX should probably refcount query objects */
318 ctx
->Driver
.BeginQuery(ctx
, q
);
322 static void GLAPIENTRY
323 _mesa_EndQueryARB(GLenum target
)
325 struct gl_query_object
*q
, **bindpt
;
326 GET_CURRENT_CONTEXT(ctx
);
327 ASSERT_OUTSIDE_BEGIN_END(ctx
);
329 if (MESA_VERBOSE
& VERBOSE_API
)
330 _mesa_debug(ctx
, "glEndQuery(%s)\n", _mesa_lookup_enum_by_nr(target
));
332 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
334 bindpt
= get_query_binding_point(ctx
, target
);
336 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
340 /* XXX should probably refcount query objects */
344 if (!q
|| !q
->Active
) {
345 _mesa_error(ctx
, GL_INVALID_OPERATION
,
346 "glEndQueryARB(no matching glBeginQueryARB)");
350 q
->Active
= GL_FALSE
;
351 ctx
->Driver
.EndQuery(ctx
, q
);
355 static void GLAPIENTRY
356 _mesa_QueryCounter(GLuint id
, GLenum target
)
358 struct gl_query_object
*q
;
359 GET_CURRENT_CONTEXT(ctx
);
360 ASSERT_OUTSIDE_BEGIN_END(ctx
);
362 if (MESA_VERBOSE
& VERBOSE_API
)
363 _mesa_debug(ctx
, "glQueryCounter(%u, %s)\n", id
,
364 _mesa_lookup_enum_by_nr(target
));
367 if (target
!= GL_TIMESTAMP
) {
368 _mesa_error(ctx
, GL_INVALID_ENUM
, "glQueryCounter(target)");
373 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id==0)");
377 q
= _mesa_lookup_query_object(ctx
, id
);
379 /* XXX the Core profile should throw INVALID_OPERATION here */
381 /* create new object */
382 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
384 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glQueryCounter");
387 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
390 if (q
->Target
&& q
->Target
!= GL_TIMESTAMP
) {
391 _mesa_error(ctx
, GL_INVALID_OPERATION
,
392 "glQueryCounter(id has an invalid target)");
398 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glQueryCounter(id is active)");
406 /* QueryCounter is implemented using EndQuery without BeginQuery
407 * in drivers. This is actually Direct3D and Gallium convention. */
408 ctx
->Driver
.EndQuery(ctx
, q
);
412 static void GLAPIENTRY
413 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
415 struct gl_query_object
*q
= NULL
, **bindpt
= NULL
;
416 GET_CURRENT_CONTEXT(ctx
);
417 ASSERT_OUTSIDE_BEGIN_END(ctx
);
419 if (MESA_VERBOSE
& VERBOSE_API
)
420 _mesa_debug(ctx
, "glGetQueryiv(%s, %s)\n",
421 _mesa_lookup_enum_by_nr(target
),
422 _mesa_lookup_enum_by_nr(pname
));
424 if (target
== GL_TIMESTAMP
) {
425 if (!ctx
->Extensions
.ARB_timer_query
) {
426 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
431 bindpt
= get_query_binding_point(ctx
, target
);
433 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
441 case GL_QUERY_COUNTER_BITS_ARB
:
442 *params
= 8 * sizeof(q
->Result
);
444 case GL_CURRENT_QUERY_ARB
:
445 *params
= q
? q
->Id
: 0;
448 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
454 static void GLAPIENTRY
455 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
457 struct gl_query_object
*q
= NULL
;
458 GET_CURRENT_CONTEXT(ctx
);
459 ASSERT_OUTSIDE_BEGIN_END(ctx
);
461 if (MESA_VERBOSE
& VERBOSE_API
)
462 _mesa_debug(ctx
, "glGetQueryObjectiv(%u, %s)\n", id
,
463 _mesa_lookup_enum_by_nr(pname
));
466 q
= _mesa_lookup_query_object(ctx
, id
);
468 if (!q
|| q
->Active
) {
469 _mesa_error(ctx
, GL_INVALID_OPERATION
,
470 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
475 case GL_QUERY_RESULT_ARB
:
477 ctx
->Driver
.WaitQuery(ctx
, q
);
478 /* if result is too large for returned type, clamp to max value */
479 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
485 if (q
->Result
> 0x7fffffff) {
486 *params
= 0x7fffffff;
489 *params
= (GLint
)q
->Result
;
493 case GL_QUERY_RESULT_AVAILABLE_ARB
:
495 ctx
->Driver
.CheckQuery( ctx
, q
);
499 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
505 static void GLAPIENTRY
506 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
508 struct gl_query_object
*q
= NULL
;
509 GET_CURRENT_CONTEXT(ctx
);
510 ASSERT_OUTSIDE_BEGIN_END(ctx
);
512 if (MESA_VERBOSE
& VERBOSE_API
)
513 _mesa_debug(ctx
, "glGetQueryObjectuiv(%u, %s)\n", id
,
514 _mesa_lookup_enum_by_nr(pname
));
517 q
= _mesa_lookup_query_object(ctx
, id
);
519 if (!q
|| q
->Active
) {
520 _mesa_error(ctx
, GL_INVALID_OPERATION
,
521 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
526 case GL_QUERY_RESULT_ARB
:
528 ctx
->Driver
.WaitQuery(ctx
, q
);
529 /* if result is too large for returned type, clamp to max value */
530 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
536 if (q
->Result
> 0xffffffff) {
537 *params
= 0xffffffff;
540 *params
= (GLuint
)q
->Result
;
544 case GL_QUERY_RESULT_AVAILABLE_ARB
:
546 ctx
->Driver
.CheckQuery( ctx
, q
);
550 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
557 * New with GL_EXT_timer_query
559 static void GLAPIENTRY
560 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
562 struct gl_query_object
*q
= NULL
;
563 GET_CURRENT_CONTEXT(ctx
);
564 ASSERT_OUTSIDE_BEGIN_END(ctx
);
566 if (MESA_VERBOSE
& VERBOSE_API
)
567 _mesa_debug(ctx
, "glGetQueryObjecti64v(%u, %s)\n", id
,
568 _mesa_lookup_enum_by_nr(pname
));
571 q
= _mesa_lookup_query_object(ctx
, id
);
573 if (!q
|| q
->Active
) {
574 _mesa_error(ctx
, GL_INVALID_OPERATION
,
575 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
580 case GL_QUERY_RESULT_ARB
:
582 ctx
->Driver
.WaitQuery(ctx
, q
);
585 case GL_QUERY_RESULT_AVAILABLE_ARB
:
587 ctx
->Driver
.CheckQuery( ctx
, q
);
591 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
598 * New with GL_EXT_timer_query
600 static void GLAPIENTRY
601 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*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
, "glGetQueryObjectui64v(%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 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
621 case GL_QUERY_RESULT_ARB
:
623 ctx
->Driver
.WaitQuery(ctx
, q
);
626 case GL_QUERY_RESULT_AVAILABLE_ARB
:
628 ctx
->Driver
.CheckQuery( ctx
, q
);
632 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
639 _mesa_init_queryobj_dispatch(struct _glapi_table
*disp
)
641 SET_GenQueriesARB(disp
, _mesa_GenQueriesARB
);
642 SET_DeleteQueriesARB(disp
, _mesa_DeleteQueriesARB
);
643 SET_IsQueryARB(disp
, _mesa_IsQueryARB
);
644 SET_BeginQueryARB(disp
, _mesa_BeginQueryARB
);
645 SET_EndQueryARB(disp
, _mesa_EndQueryARB
);
646 SET_GetQueryivARB(disp
, _mesa_GetQueryivARB
);
647 SET_GetQueryObjectivARB(disp
, _mesa_GetQueryObjectivARB
);
648 SET_GetQueryObjectuivARB(disp
, _mesa_GetQueryObjectuivARB
);
649 SET_QueryCounter(disp
, _mesa_QueryCounter
);
651 SET_GetQueryObjecti64vEXT(disp
, _mesa_GetQueryObjecti64vEXT
);
652 SET_GetQueryObjectui64vEXT(disp
, _mesa_GetQueryObjectui64vEXT
);
656 #endif /* FEATURE_queryobj */
660 * Allocate/init the context state related to query objects.
663 _mesa_init_queryobj(struct gl_context
*ctx
)
665 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
666 ctx
->Query
.CurrentOcclusionObject
= NULL
;
671 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
674 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
676 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
677 struct gl_context
*ctx
= (struct gl_context
*)userData
;
678 ctx
->Driver
.DeleteQuery(ctx
, q
);
683 * Free the context state related to query objects.
686 _mesa_free_queryobj_data(struct gl_context
*ctx
)
688 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
689 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);