2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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
);
49 q
->Ready
= GL_TRUE
; /* correct, see spec */
56 * Delete an occlusion query object.
57 * Not removed from hash table here.
58 * XXX maybe add Delete() method to gl_query_object class and call that instead
61 delete_query_object(struct gl_query_object
*q
)
67 static struct gl_query_object
*
68 lookup_query_object(GLcontext
*ctx
, GLuint id
)
70 return (struct gl_query_object
*)
71 _mesa_HashLookup(ctx
->Query
.QueryObjects
, id
);
77 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
80 GET_CURRENT_CONTEXT(ctx
);
81 ASSERT_OUTSIDE_BEGIN_END(ctx
);
84 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
88 /* No query objects can be active at this time! */
89 if (ctx
->Query
.CurrentOcclusionObject
||
90 ctx
->Query
.CurrentTimerObject
) {
91 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
95 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
98 for (i
= 0; i
< n
; i
++) {
99 struct gl_query_object
*q
100 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
102 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
106 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
113 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
116 GET_CURRENT_CONTEXT(ctx
);
117 ASSERT_OUTSIDE_BEGIN_END(ctx
);
120 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
124 /* No query objects can be active at this time! */
125 if (ctx
->Query
.CurrentOcclusionObject
||
126 ctx
->Query
.CurrentTimerObject
) {
127 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
131 for (i
= 0; i
< n
; i
++) {
133 struct gl_query_object
*q
= lookup_query_object(ctx
, ids
[i
]);
135 ASSERT(!q
->Active
); /* should be caught earlier */
136 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
137 delete_query_object(q
);
145 _mesa_IsQueryARB(GLuint id
)
147 GET_CURRENT_CONTEXT(ctx
);
148 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
150 if (id
&& lookup_query_object(ctx
, id
))
158 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
160 struct gl_query_object
*q
;
161 GET_CURRENT_CONTEXT(ctx
);
162 ASSERT_OUTSIDE_BEGIN_END(ctx
);
164 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
167 case GL_SAMPLES_PASSED_ARB
:
168 if (!ctx
->Extensions
.ARB_occlusion_query
) {
169 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
172 if (ctx
->Query
.CurrentOcclusionObject
) {
173 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
177 case GL_TIME_ELAPSED_EXT
:
178 if (!ctx
->Extensions
.EXT_timer_query
) {
179 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
182 if (ctx
->Query
.CurrentTimerObject
) {
183 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
188 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
193 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
197 q
= lookup_query_object(ctx
, id
);
199 /* create new object */
200 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
202 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
205 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
208 /* pre-existing object */
210 _mesa_error(ctx
, GL_INVALID_OPERATION
,
211 "glBeginQueryARB(query already active)");
220 if (target
== GL_SAMPLES_PASSED_ARB
) {
221 ctx
->Query
.CurrentOcclusionObject
= q
;
223 else if (target
== GL_TIME_ELAPSED_EXT
) {
224 ctx
->Query
.CurrentTimerObject
= q
;
227 if (ctx
->Driver
.BeginQuery
) {
228 ctx
->Driver
.BeginQuery(ctx
, target
, q
);
234 _mesa_EndQueryARB(GLenum target
)
236 struct gl_query_object
*q
;
237 GET_CURRENT_CONTEXT(ctx
);
238 ASSERT_OUTSIDE_BEGIN_END(ctx
);
240 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
243 case GL_SAMPLES_PASSED_ARB
:
244 if (!ctx
->Extensions
.ARB_occlusion_query
) {
245 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
248 q
= ctx
->Query
.CurrentOcclusionObject
;
249 ctx
->Query
.CurrentOcclusionObject
= NULL
;
251 case GL_TIME_ELAPSED_EXT
:
252 if (!ctx
->Extensions
.EXT_timer_query
) {
253 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
256 q
= ctx
->Query
.CurrentTimerObject
;
257 ctx
->Query
.CurrentTimerObject
= NULL
;
260 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
264 if (!q
|| !q
->Active
) {
265 _mesa_error(ctx
, GL_INVALID_OPERATION
,
266 "glEndQueryARB(no matching glBeginQueryARB)");
270 q
->Active
= GL_FALSE
;
271 if (ctx
->Driver
.EndQuery
) {
272 ctx
->Driver
.EndQuery(ctx
, target
, q
);
275 /* if we're using software rendering/querying */
282 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
284 struct gl_query_object
*q
;
285 GET_CURRENT_CONTEXT(ctx
);
286 ASSERT_OUTSIDE_BEGIN_END(ctx
);
289 case GL_SAMPLES_PASSED_ARB
:
290 if (!ctx
->Extensions
.ARB_occlusion_query
) {
291 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
294 q
= ctx
->Query
.CurrentOcclusionObject
;
296 case GL_TIME_ELAPSED_EXT
:
297 if (!ctx
->Extensions
.EXT_timer_query
) {
298 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
301 q
= ctx
->Query
.CurrentTimerObject
;
304 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(target)");
309 case GL_QUERY_COUNTER_BITS_ARB
:
310 *params
= 8 * sizeof(q
->Result
);
312 case GL_CURRENT_QUERY_ARB
:
313 *params
= q
? q
->Id
: 0;
316 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
323 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
325 struct gl_query_object
*q
= NULL
;
326 GET_CURRENT_CONTEXT(ctx
);
327 ASSERT_OUTSIDE_BEGIN_END(ctx
);
330 q
= lookup_query_object(ctx
, id
);
332 if (!q
|| q
->Active
) {
333 _mesa_error(ctx
, GL_INVALID_OPERATION
,
334 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
339 case GL_QUERY_RESULT_ARB
:
341 /* Wait for the query to finish! */
342 /* If using software rendering, the result will always be ready
343 * by time we get here. Otherwise, we must be using hardware!
345 ASSERT(ctx
->Driver
.EndQuery
);
347 /* if result is too large for returned type, clamp to max value */
348 if (q
->Result
> 0x7fffffff) {
349 *params
= 0x7fffffff;
355 case GL_QUERY_RESULT_AVAILABLE_ARB
:
356 /* XXX revisit when we have a hardware implementation! */
360 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
367 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
369 struct gl_query_object
*q
= NULL
;
370 GET_CURRENT_CONTEXT(ctx
);
371 ASSERT_OUTSIDE_BEGIN_END(ctx
);
374 q
= lookup_query_object(ctx
, id
);
376 if (!q
|| q
->Active
) {
377 _mesa_error(ctx
, GL_INVALID_OPERATION
,
378 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
383 case GL_QUERY_RESULT_ARB
:
385 /* Wait for the query to finish! */
386 /* If using software rendering, the result will always be ready
387 * by time we get here. Otherwise, we must be using hardware!
389 ASSERT(ctx
->Driver
.EndQuery
);
391 /* if result is too large for returned type, clamp to max value */
392 if (q
->Result
> 0xffffffff) {
393 *params
= 0xffffffff;
399 case GL_QUERY_RESULT_AVAILABLE_ARB
:
400 /* XXX revisit when we have a hardware implementation! */
404 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
411 * New with GL_EXT_timer_query
414 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
416 struct gl_query_object
*q
= NULL
;
417 GET_CURRENT_CONTEXT(ctx
);
418 ASSERT_OUTSIDE_BEGIN_END(ctx
);
421 q
= lookup_query_object(ctx
, id
);
423 if (!q
|| q
->Active
) {
424 _mesa_error(ctx
, GL_INVALID_OPERATION
,
425 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
430 case GL_QUERY_RESULT_ARB
:
432 /* Wait for the query to finish! */
433 /* If using software rendering, the result will always be ready
434 * by time we get here. Otherwise, we must be using hardware!
436 ASSERT(ctx
->Driver
.EndQuery
);
440 case GL_QUERY_RESULT_AVAILABLE_ARB
:
441 /* XXX revisit when we have a hardware implementation! */
445 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
452 * New with GL_EXT_timer_query
455 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
457 struct gl_query_object
*q
= NULL
;
458 GET_CURRENT_CONTEXT(ctx
);
459 ASSERT_OUTSIDE_BEGIN_END(ctx
);
462 q
= lookup_query_object(ctx
, id
);
464 if (!q
|| q
->Active
) {
465 _mesa_error(ctx
, GL_INVALID_OPERATION
,
466 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
471 case GL_QUERY_RESULT_ARB
:
473 /* Wait for the query to finish! */
474 /* If using software rendering, the result will always be ready
475 * by time we get here. Otherwise, we must be using hardware!
477 ASSERT(ctx
->Driver
.EndQuery
);
481 case GL_QUERY_RESULT_AVAILABLE_ARB
:
482 /* XXX revisit when we have a hardware implementation! */
486 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
493 * Allocate/init the context state related to query objects.
496 _mesa_init_query(GLcontext
*ctx
)
498 #if FEATURE_ARB_occlusion_query
499 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
500 ctx
->Query
.CurrentOcclusionObject
= NULL
;
506 * Free the context state related to query objects.
509 _mesa_free_query_data(GLcontext
*ctx
)
512 GLuint id
= _mesa_HashFirstEntry(ctx
->Query
.QueryObjects
);
514 struct gl_query_object
*q
= lookup_query_object(ctx
, id
);
516 delete_query_object(q
);
517 _mesa_HashRemove(ctx
->Query
.QueryObjects
, id
);
523 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);