2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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.
35 * Allocate a new query object. This is a fallback routine called via
36 * ctx->Driver.NewQueryObject().
37 * \param ctx - rendering context
38 * \param id - the new object's ID
39 * \return pointer to new query_object object or NULL if out of memory.
41 struct gl_query_object
*
42 _mesa_new_query_object(GLcontext
*ctx
, GLuint id
)
44 struct gl_query_object
*q
= MALLOC_STRUCT(gl_query_object
);
50 q
->Ready
= GL_TRUE
; /* correct, see spec */
57 * Delete an occlusion query object.
58 * Not removed from hash table here.
59 * XXX maybe add Delete() method to gl_query_object class and call that instead
62 delete_query_object(struct gl_query_object
*q
)
68 static struct gl_query_object
*
69 lookup_query_object(GLcontext
*ctx
, GLuint id
)
71 return (struct gl_query_object
*)
72 _mesa_HashLookup(ctx
->Query
.QueryObjects
, id
);
78 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
81 GET_CURRENT_CONTEXT(ctx
);
82 ASSERT_OUTSIDE_BEGIN_END(ctx
);
85 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
89 /* No query objects can be active at this time! */
90 if (ctx
->Query
.CurrentOcclusionObject
||
91 ctx
->Query
.CurrentTimerObject
) {
92 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
96 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
99 for (i
= 0; i
< n
; i
++) {
100 struct gl_query_object
*q
101 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
103 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
107 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
114 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
117 GET_CURRENT_CONTEXT(ctx
);
118 ASSERT_OUTSIDE_BEGIN_END(ctx
);
121 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
125 /* No query objects can be active at this time! */
126 if (ctx
->Query
.CurrentOcclusionObject
||
127 ctx
->Query
.CurrentTimerObject
) {
128 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
132 for (i
= 0; i
< n
; i
++) {
134 struct gl_query_object
*q
= lookup_query_object(ctx
, ids
[i
]);
136 ASSERT(!q
->Active
); /* should be caught earlier */
137 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
138 delete_query_object(q
);
146 _mesa_IsQueryARB(GLuint id
)
148 GET_CURRENT_CONTEXT(ctx
);
149 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
151 if (id
&& lookup_query_object(ctx
, id
))
159 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
161 struct gl_query_object
*q
;
162 GET_CURRENT_CONTEXT(ctx
);
163 ASSERT_OUTSIDE_BEGIN_END(ctx
);
165 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
168 case GL_SAMPLES_PASSED_ARB
:
169 if (!ctx
->Extensions
.ARB_occlusion_query
) {
170 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
173 if (ctx
->Query
.CurrentOcclusionObject
) {
174 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
178 #if FEATURE_EXT_timer_query
179 case GL_TIME_ELAPSED_EXT
:
180 if (!ctx
->Extensions
.EXT_timer_query
) {
181 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
184 if (ctx
->Query
.CurrentTimerObject
) {
185 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
191 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
196 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
200 q
= lookup_query_object(ctx
, id
);
202 /* create new object */
203 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
205 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
208 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
211 /* pre-existing object */
213 _mesa_error(ctx
, GL_INVALID_OPERATION
,
214 "glBeginQueryARB(query already active)");
223 if (target
== GL_SAMPLES_PASSED_ARB
) {
224 ctx
->Query
.CurrentOcclusionObject
= q
;
226 #if FEATURE_EXT_timer_query
227 else if (target
== GL_TIME_ELAPSED_EXT
) {
228 ctx
->Query
.CurrentTimerObject
= q
;
232 if (ctx
->Driver
.BeginQuery
) {
233 ctx
->Driver
.BeginQuery(ctx
, target
, q
);
239 _mesa_EndQueryARB(GLenum target
)
241 struct gl_query_object
*q
;
242 GET_CURRENT_CONTEXT(ctx
);
243 ASSERT_OUTSIDE_BEGIN_END(ctx
);
245 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
248 case GL_SAMPLES_PASSED_ARB
:
249 if (!ctx
->Extensions
.ARB_occlusion_query
) {
250 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
253 q
= ctx
->Query
.CurrentOcclusionObject
;
254 ctx
->Query
.CurrentOcclusionObject
= NULL
;
256 #if FEATURE_EXT_timer_query
257 case GL_TIME_ELAPSED_EXT
:
258 if (!ctx
->Extensions
.EXT_timer_query
) {
259 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
262 q
= ctx
->Query
.CurrentTimerObject
;
263 ctx
->Query
.CurrentTimerObject
= NULL
;
267 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
271 if (!q
|| !q
->Active
) {
272 _mesa_error(ctx
, GL_INVALID_OPERATION
,
273 "glEndQueryARB(no matching glBeginQueryARB)");
277 q
->Active
= GL_FALSE
;
278 if (ctx
->Driver
.EndQuery
) {
279 ctx
->Driver
.EndQuery(ctx
, target
, q
);
282 /* if we're using software rendering/querying */
289 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
291 struct gl_query_object
*q
;
292 GET_CURRENT_CONTEXT(ctx
);
293 ASSERT_OUTSIDE_BEGIN_END(ctx
);
296 case GL_SAMPLES_PASSED_ARB
:
297 if (!ctx
->Extensions
.ARB_occlusion_query
) {
298 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
301 q
= ctx
->Query
.CurrentOcclusionObject
;
303 #if FEATURE_EXT_timer_query
304 case GL_TIME_ELAPSED_EXT
:
305 if (!ctx
->Extensions
.EXT_timer_query
) {
306 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
309 q
= ctx
->Query
.CurrentTimerObject
;
313 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(target)");
318 case GL_QUERY_COUNTER_BITS_ARB
:
319 *params
= 8 * sizeof(q
->Result
);
321 case GL_CURRENT_QUERY_ARB
:
322 *params
= q
? q
->Id
: 0;
325 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
332 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
334 struct gl_query_object
*q
= NULL
;
335 GET_CURRENT_CONTEXT(ctx
);
336 ASSERT_OUTSIDE_BEGIN_END(ctx
);
339 q
= lookup_query_object(ctx
, id
);
341 if (!q
|| q
->Active
) {
342 _mesa_error(ctx
, GL_INVALID_OPERATION
,
343 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
348 case GL_QUERY_RESULT_ARB
:
350 /* Wait for the query to finish! */
351 /* If using software rendering, the result will always be ready
352 * by time we get here. Otherwise, we must be using hardware!
354 ASSERT(ctx
->Driver
.EndQuery
);
356 /* if result is too large for returned type, clamp to max value */
357 if (q
->Result
> 0x7fffffff) {
358 *params
= 0x7fffffff;
364 case GL_QUERY_RESULT_AVAILABLE_ARB
:
365 /* XXX revisit when we have a hardware implementation! */
369 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
376 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
378 struct gl_query_object
*q
= NULL
;
379 GET_CURRENT_CONTEXT(ctx
);
380 ASSERT_OUTSIDE_BEGIN_END(ctx
);
383 q
= lookup_query_object(ctx
, id
);
385 if (!q
|| q
->Active
) {
386 _mesa_error(ctx
, GL_INVALID_OPERATION
,
387 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
392 case GL_QUERY_RESULT_ARB
:
394 /* Wait for the query to finish! */
395 /* If using software rendering, the result will always be ready
396 * by time we get here. Otherwise, we must be using hardware!
398 ASSERT(ctx
->Driver
.EndQuery
);
400 /* if result is too large for returned type, clamp to max value */
401 if (q
->Result
> 0xffffffff) {
402 *params
= 0xffffffff;
408 case GL_QUERY_RESULT_AVAILABLE_ARB
:
409 /* XXX revisit when we have a hardware implementation! */
413 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
419 #if FEATURE_EXT_timer_query
422 * New with GL_EXT_timer_query
425 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
427 struct gl_query_object
*q
= NULL
;
428 GET_CURRENT_CONTEXT(ctx
);
429 ASSERT_OUTSIDE_BEGIN_END(ctx
);
432 q
= lookup_query_object(ctx
, id
);
434 if (!q
|| q
->Active
) {
435 _mesa_error(ctx
, GL_INVALID_OPERATION
,
436 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
441 case GL_QUERY_RESULT_ARB
:
443 /* Wait for the query to finish! */
444 /* If using software rendering, the result will always be ready
445 * by time we get here. Otherwise, we must be using hardware!
447 ASSERT(ctx
->Driver
.EndQuery
);
451 case GL_QUERY_RESULT_AVAILABLE_ARB
:
452 /* XXX revisit when we have a hardware implementation! */
456 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
463 * New with GL_EXT_timer_query
466 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
468 struct gl_query_object
*q
= NULL
;
469 GET_CURRENT_CONTEXT(ctx
);
470 ASSERT_OUTSIDE_BEGIN_END(ctx
);
473 q
= lookup_query_object(ctx
, id
);
475 if (!q
|| q
->Active
) {
476 _mesa_error(ctx
, GL_INVALID_OPERATION
,
477 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
482 case GL_QUERY_RESULT_ARB
:
484 /* Wait for the query to finish! */
485 /* If using software rendering, the result will always be ready
486 * by time we get here. Otherwise, we must be using hardware!
488 ASSERT(ctx
->Driver
.EndQuery
);
492 case GL_QUERY_RESULT_AVAILABLE_ARB
:
493 /* XXX revisit when we have a hardware implementation! */
497 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
502 #endif /* FEATURE_EXT_timer_query */
506 * Allocate/init the context state related to query objects.
509 _mesa_init_query(GLcontext
*ctx
)
511 #if FEATURE_ARB_occlusion_query
512 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
513 ctx
->Query
.CurrentOcclusionObject
= NULL
;
519 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
522 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
524 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
526 delete_query_object(q
);
531 * Free the context state related to query objects.
534 _mesa_free_query_data(GLcontext
*ctx
)
536 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, NULL
);
537 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);