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 "main/dispatch.h"
39 * Allocate a new query object. This is a fallback routine called via
40 * ctx->Driver.NewQueryObject().
41 * \param ctx - rendering context
42 * \param id - the new object's ID
43 * \return pointer to new query_object object or NULL if out of memory.
45 static struct gl_query_object
*
46 _mesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
48 struct gl_query_object
*q
= MALLOC_STRUCT(gl_query_object
);
54 q
->Ready
= GL_TRUE
; /* correct, see spec */
61 * Begin a query. Software driver fallback.
62 * Called via ctx->Driver.BeginQuery().
65 _mesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
72 * End a query. Software driver fallback.
73 * Called via ctx->Driver.EndQuery().
76 _mesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
83 * Wait for query to complete. Software driver fallback.
84 * Called via ctx->Driver.WaitQuery().
87 _mesa_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
89 /* For software drivers, _mesa_end_query() should have completed the query.
90 * For real hardware, implement a proper WaitQuery() driver function,
91 * which may require issuing a flush.
98 * Check if a query results are ready. Software driver fallback.
99 * Called via ctx->Driver.CheckQuery().
102 _mesa_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
104 /* No-op for sw rendering.
105 * HW drivers may need to flush at this time.
111 * Delete a query object. Called via ctx->Driver.DeleteQuery().
112 * Not removed from hash table here.
115 _mesa_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
122 _mesa_init_query_object_functions(struct dd_function_table
*driver
)
124 driver
->NewQueryObject
= _mesa_new_query_object
;
125 driver
->DeleteQuery
= _mesa_delete_query
;
126 driver
->BeginQuery
= _mesa_begin_query
;
127 driver
->EndQuery
= _mesa_end_query
;
128 driver
->WaitQuery
= _mesa_wait_query
;
129 driver
->CheckQuery
= _mesa_check_query
;
134 * Return pointer to the query object binding point for the given target.
135 * \return NULL if invalid target, else the address of binding point
137 static struct gl_query_object
**
138 get_query_binding_point(struct gl_context
*ctx
, GLenum target
)
141 case GL_SAMPLES_PASSED_ARB
:
142 if (ctx
->Extensions
.ARB_occlusion_query
)
143 return &ctx
->Query
.CurrentOcclusionObject
;
146 case GL_ANY_SAMPLES_PASSED
:
147 if (ctx
->Extensions
.ARB_occlusion_query2
)
148 return &ctx
->Query
.CurrentOcclusionObject
;
151 case GL_TIME_ELAPSED_EXT
:
152 if (ctx
->Extensions
.EXT_timer_query
)
153 return &ctx
->Query
.CurrentTimerObject
;
156 #if FEATURE_EXT_transform_feedback
157 case GL_PRIMITIVES_GENERATED
:
158 if (ctx
->Extensions
.EXT_transform_feedback
)
159 return &ctx
->Query
.PrimitivesGenerated
;
162 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
163 if (ctx
->Extensions
.EXT_transform_feedback
)
164 return &ctx
->Query
.PrimitivesWritten
;
175 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
178 GET_CURRENT_CONTEXT(ctx
);
179 ASSERT_OUTSIDE_BEGIN_END(ctx
);
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
);
218 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
222 /* No query objects can be active at this time! */
223 if (ctx
->Query
.CurrentOcclusionObject
||
224 ctx
->Query
.CurrentTimerObject
) {
225 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
229 for (i
= 0; i
< n
; i
++) {
231 struct gl_query_object
*q
= _mesa_lookup_query_object(ctx
, ids
[i
]);
233 ASSERT(!q
->Active
); /* should be caught earlier */
234 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
235 ctx
->Driver
.DeleteQuery(ctx
, q
);
243 _mesa_IsQueryARB(GLuint id
)
245 GET_CURRENT_CONTEXT(ctx
);
246 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
248 if (id
&& _mesa_lookup_query_object(ctx
, id
))
255 static void GLAPIENTRY
256 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
258 struct gl_query_object
*q
, **bindpt
;
259 GET_CURRENT_CONTEXT(ctx
);
260 ASSERT_OUTSIDE_BEGIN_END(ctx
);
262 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
264 bindpt
= get_query_binding_point(ctx
, target
);
266 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
271 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
275 q
= _mesa_lookup_query_object(ctx
, id
);
277 /* create new object */
278 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
280 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
283 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
286 /* pre-existing object */
288 _mesa_error(ctx
, GL_INVALID_OPERATION
,
289 "glBeginQueryARB(query already active)");
299 /* XXX should probably refcount query objects */
302 ctx
->Driver
.BeginQuery(ctx
, q
);
306 static void GLAPIENTRY
307 _mesa_EndQueryARB(GLenum target
)
309 struct gl_query_object
*q
, **bindpt
;
310 GET_CURRENT_CONTEXT(ctx
);
311 ASSERT_OUTSIDE_BEGIN_END(ctx
);
313 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
315 bindpt
= get_query_binding_point(ctx
, target
);
317 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
321 /* XXX should probably refcount query objects */
325 if (!q
|| !q
->Active
) {
326 _mesa_error(ctx
, GL_INVALID_OPERATION
,
327 "glEndQueryARB(no matching glBeginQueryARB)");
331 q
->Active
= GL_FALSE
;
332 ctx
->Driver
.EndQuery(ctx
, q
);
337 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
339 struct gl_query_object
*q
, **bindpt
;
340 GET_CURRENT_CONTEXT(ctx
);
341 ASSERT_OUTSIDE_BEGIN_END(ctx
);
343 bindpt
= get_query_binding_point(ctx
, target
);
345 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
352 case GL_QUERY_COUNTER_BITS_ARB
:
353 *params
= 8 * sizeof(q
->Result
);
355 case GL_CURRENT_QUERY_ARB
:
356 *params
= q
? q
->Id
: 0;
359 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
366 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
368 struct gl_query_object
*q
= NULL
;
369 GET_CURRENT_CONTEXT(ctx
);
370 ASSERT_OUTSIDE_BEGIN_END(ctx
);
373 q
= _mesa_lookup_query_object(ctx
, id
);
375 if (!q
|| q
->Active
) {
376 _mesa_error(ctx
, GL_INVALID_OPERATION
,
377 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
382 case GL_QUERY_RESULT_ARB
:
384 ctx
->Driver
.WaitQuery(ctx
, q
);
385 /* if result is too large for returned type, clamp to max value */
386 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
392 if (q
->Result
> 0x7fffffff) {
393 *params
= 0x7fffffff;
396 *params
= (GLint
)q
->Result
;
400 case GL_QUERY_RESULT_AVAILABLE_ARB
:
402 ctx
->Driver
.CheckQuery( ctx
, q
);
406 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
413 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
415 struct gl_query_object
*q
= NULL
;
416 GET_CURRENT_CONTEXT(ctx
);
417 ASSERT_OUTSIDE_BEGIN_END(ctx
);
420 q
= _mesa_lookup_query_object(ctx
, id
);
422 if (!q
|| q
->Active
) {
423 _mesa_error(ctx
, GL_INVALID_OPERATION
,
424 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
429 case GL_QUERY_RESULT_ARB
:
431 ctx
->Driver
.WaitQuery(ctx
, q
);
432 /* if result is too large for returned type, clamp to max value */
433 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
439 if (q
->Result
> 0xffffffff) {
440 *params
= 0xffffffff;
443 *params
= (GLuint
)q
->Result
;
447 case GL_QUERY_RESULT_AVAILABLE_ARB
:
449 ctx
->Driver
.CheckQuery( ctx
, q
);
453 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
460 * New with GL_EXT_timer_query
462 static void GLAPIENTRY
463 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
465 struct gl_query_object
*q
= NULL
;
466 GET_CURRENT_CONTEXT(ctx
);
467 ASSERT_OUTSIDE_BEGIN_END(ctx
);
470 q
= _mesa_lookup_query_object(ctx
, id
);
472 if (!q
|| q
->Active
) {
473 _mesa_error(ctx
, GL_INVALID_OPERATION
,
474 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
479 case GL_QUERY_RESULT_ARB
:
481 ctx
->Driver
.WaitQuery(ctx
, q
);
484 case GL_QUERY_RESULT_AVAILABLE_ARB
:
486 ctx
->Driver
.CheckQuery( ctx
, q
);
490 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
497 * New with GL_EXT_timer_query
499 static void GLAPIENTRY
500 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
502 struct gl_query_object
*q
= NULL
;
503 GET_CURRENT_CONTEXT(ctx
);
504 ASSERT_OUTSIDE_BEGIN_END(ctx
);
507 q
= _mesa_lookup_query_object(ctx
, id
);
509 if (!q
|| q
->Active
) {
510 _mesa_error(ctx
, GL_INVALID_OPERATION
,
511 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
516 case GL_QUERY_RESULT_ARB
:
518 ctx
->Driver
.WaitQuery(ctx
, q
);
521 case GL_QUERY_RESULT_AVAILABLE_ARB
:
523 ctx
->Driver
.CheckQuery( ctx
, q
);
527 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
534 _mesa_init_queryobj_dispatch(struct _glapi_table
*disp
)
536 SET_GenQueriesARB(disp
, _mesa_GenQueriesARB
);
537 SET_DeleteQueriesARB(disp
, _mesa_DeleteQueriesARB
);
538 SET_IsQueryARB(disp
, _mesa_IsQueryARB
);
539 SET_BeginQueryARB(disp
, _mesa_BeginQueryARB
);
540 SET_EndQueryARB(disp
, _mesa_EndQueryARB
);
541 SET_GetQueryivARB(disp
, _mesa_GetQueryivARB
);
542 SET_GetQueryObjectivARB(disp
, _mesa_GetQueryObjectivARB
);
543 SET_GetQueryObjectuivARB(disp
, _mesa_GetQueryObjectuivARB
);
545 SET_GetQueryObjecti64vEXT(disp
, _mesa_GetQueryObjecti64vEXT
);
546 SET_GetQueryObjectui64vEXT(disp
, _mesa_GetQueryObjectui64vEXT
);
550 #endif /* FEATURE_queryobj */
554 * Allocate/init the context state related to query objects.
557 _mesa_init_queryobj(struct gl_context
*ctx
)
559 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
560 ctx
->Query
.CurrentOcclusionObject
= NULL
;
565 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
568 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
570 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
571 struct gl_context
*ctx
= (struct gl_context
*)userData
;
572 ctx
->Driver
.DeleteQuery(ctx
, q
);
577 * Free the context state related to query objects.
580 _mesa_free_queryobj_data(struct gl_context
*ctx
)
582 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
583 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);