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.
31 #include "mfeatures.h"
33 #include "main/dispatch.h"
40 * Allocate a new query object. This is a fallback routine called via
41 * ctx->Driver.NewQueryObject().
42 * \param ctx - rendering context
43 * \param id - the new object's ID
44 * \return pointer to new query_object object or NULL if out of memory.
46 static struct gl_query_object
*
47 _mesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
49 struct gl_query_object
*q
= MALLOC_STRUCT(gl_query_object
);
55 q
->Ready
= GL_TRUE
; /* correct, see spec */
62 * Begin a query. Software driver fallback.
63 * Called via ctx->Driver.BeginQuery().
66 _mesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
73 * End a query. Software driver fallback.
74 * Called via ctx->Driver.EndQuery().
77 _mesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
84 * Wait for query to complete. Software driver fallback.
85 * Called via ctx->Driver.WaitQuery().
88 _mesa_wait_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
90 /* For software drivers, _mesa_end_query() should have completed the query.
91 * For real hardware, implement a proper WaitQuery() driver function,
92 * which may require issuing a flush.
99 * Check if a query results are ready. Software driver fallback.
100 * Called via ctx->Driver.CheckQuery().
103 _mesa_check_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
105 /* No-op for sw rendering.
106 * HW drivers may need to flush at this time.
112 * Delete a query object. Called via ctx->Driver.DeleteQuery().
113 * Not removed from hash table here.
116 _mesa_delete_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
123 _mesa_init_query_object_functions(struct dd_function_table
*driver
)
125 driver
->NewQueryObject
= _mesa_new_query_object
;
126 driver
->DeleteQuery
= _mesa_delete_query
;
127 driver
->BeginQuery
= _mesa_begin_query
;
128 driver
->EndQuery
= _mesa_end_query
;
129 driver
->WaitQuery
= _mesa_wait_query
;
130 driver
->CheckQuery
= _mesa_check_query
;
135 * Return pointer to the query object binding point for the given target.
136 * \return NULL if invalid target, else the address of binding point
138 static struct gl_query_object
**
139 get_query_binding_point(struct gl_context
*ctx
, GLenum target
)
142 case GL_SAMPLES_PASSED_ARB
:
143 if (ctx
->Extensions
.ARB_occlusion_query
)
144 return &ctx
->Query
.CurrentOcclusionObject
;
147 case GL_ANY_SAMPLES_PASSED
:
148 if (ctx
->Extensions
.ARB_occlusion_query2
)
149 return &ctx
->Query
.CurrentOcclusionObject
;
152 case GL_TIME_ELAPSED_EXT
:
153 if (ctx
->Extensions
.EXT_timer_query
)
154 return &ctx
->Query
.CurrentTimerObject
;
157 #if FEATURE_EXT_transform_feedback
158 case GL_PRIMITIVES_GENERATED
:
159 if (ctx
->Extensions
.EXT_transform_feedback
)
160 return &ctx
->Query
.PrimitivesGenerated
;
163 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
164 if (ctx
->Extensions
.EXT_transform_feedback
)
165 return &ctx
->Query
.PrimitivesWritten
;
176 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
179 GET_CURRENT_CONTEXT(ctx
);
180 ASSERT_OUTSIDE_BEGIN_END(ctx
);
183 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
187 /* No query objects can be active at this time! */
188 if (ctx
->Query
.CurrentOcclusionObject
||
189 ctx
->Query
.CurrentTimerObject
) {
190 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
194 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
197 for (i
= 0; i
< n
; i
++) {
198 struct gl_query_object
*q
199 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
201 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
205 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
212 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
215 GET_CURRENT_CONTEXT(ctx
);
216 ASSERT_OUTSIDE_BEGIN_END(ctx
);
219 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
223 /* No query objects can be active at this time! */
224 if (ctx
->Query
.CurrentOcclusionObject
||
225 ctx
->Query
.CurrentTimerObject
) {
226 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
230 for (i
= 0; i
< n
; i
++) {
232 struct gl_query_object
*q
= _mesa_lookup_query_object(ctx
, ids
[i
]);
234 ASSERT(!q
->Active
); /* should be caught earlier */
235 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
236 ctx
->Driver
.DeleteQuery(ctx
, q
);
244 _mesa_IsQueryARB(GLuint id
)
246 GET_CURRENT_CONTEXT(ctx
);
247 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
249 if (id
&& _mesa_lookup_query_object(ctx
, id
))
256 static void GLAPIENTRY
257 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
259 struct gl_query_object
*q
, **bindpt
;
260 GET_CURRENT_CONTEXT(ctx
);
261 ASSERT_OUTSIDE_BEGIN_END(ctx
);
263 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
265 bindpt
= get_query_binding_point(ctx
, target
);
267 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
272 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
276 q
= _mesa_lookup_query_object(ctx
, id
);
278 /* create new object */
279 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
281 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
284 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
287 /* pre-existing object */
289 _mesa_error(ctx
, GL_INVALID_OPERATION
,
290 "glBeginQueryARB(query already active)");
300 /* XXX should probably refcount query objects */
303 ctx
->Driver
.BeginQuery(ctx
, q
);
307 static void GLAPIENTRY
308 _mesa_EndQueryARB(GLenum target
)
310 struct gl_query_object
*q
, **bindpt
;
311 GET_CURRENT_CONTEXT(ctx
);
312 ASSERT_OUTSIDE_BEGIN_END(ctx
);
314 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
316 bindpt
= get_query_binding_point(ctx
, target
);
318 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
322 /* XXX should probably refcount query objects */
326 if (!q
|| !q
->Active
) {
327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
328 "glEndQueryARB(no matching glBeginQueryARB)");
332 q
->Active
= GL_FALSE
;
333 ctx
->Driver
.EndQuery(ctx
, q
);
338 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
340 struct gl_query_object
*q
, **bindpt
;
341 GET_CURRENT_CONTEXT(ctx
);
342 ASSERT_OUTSIDE_BEGIN_END(ctx
);
344 bindpt
= get_query_binding_point(ctx
, target
);
346 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
353 case GL_QUERY_COUNTER_BITS_ARB
:
354 *params
= 8 * sizeof(q
->Result
);
356 case GL_CURRENT_QUERY_ARB
:
357 *params
= q
? q
->Id
: 0;
360 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
367 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
369 struct gl_query_object
*q
= NULL
;
370 GET_CURRENT_CONTEXT(ctx
);
371 ASSERT_OUTSIDE_BEGIN_END(ctx
);
374 q
= _mesa_lookup_query_object(ctx
, id
);
376 if (!q
|| q
->Active
) {
377 _mesa_error(ctx
, GL_INVALID_OPERATION
,
378 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
383 case GL_QUERY_RESULT_ARB
:
385 ctx
->Driver
.WaitQuery(ctx
, q
);
386 /* if result is too large for returned type, clamp to max value */
387 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
393 if (q
->Result
> 0x7fffffff) {
394 *params
= 0x7fffffff;
397 *params
= (GLint
)q
->Result
;
401 case GL_QUERY_RESULT_AVAILABLE_ARB
:
403 ctx
->Driver
.CheckQuery( ctx
, q
);
407 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
414 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
416 struct gl_query_object
*q
= NULL
;
417 GET_CURRENT_CONTEXT(ctx
);
418 ASSERT_OUTSIDE_BEGIN_END(ctx
);
421 q
= _mesa_lookup_query_object(ctx
, id
);
423 if (!q
|| q
->Active
) {
424 _mesa_error(ctx
, GL_INVALID_OPERATION
,
425 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
430 case GL_QUERY_RESULT_ARB
:
432 ctx
->Driver
.WaitQuery(ctx
, q
);
433 /* if result is too large for returned type, clamp to max value */
434 if (q
->Target
== GL_ANY_SAMPLES_PASSED
) {
440 if (q
->Result
> 0xffffffff) {
441 *params
= 0xffffffff;
444 *params
= (GLuint
)q
->Result
;
448 case GL_QUERY_RESULT_AVAILABLE_ARB
:
450 ctx
->Driver
.CheckQuery( ctx
, q
);
454 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
461 * New with GL_EXT_timer_query
463 static void GLAPIENTRY
464 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
466 struct gl_query_object
*q
= NULL
;
467 GET_CURRENT_CONTEXT(ctx
);
468 ASSERT_OUTSIDE_BEGIN_END(ctx
);
471 q
= _mesa_lookup_query_object(ctx
, id
);
473 if (!q
|| q
->Active
) {
474 _mesa_error(ctx
, GL_INVALID_OPERATION
,
475 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
480 case GL_QUERY_RESULT_ARB
:
482 ctx
->Driver
.WaitQuery(ctx
, q
);
485 case GL_QUERY_RESULT_AVAILABLE_ARB
:
487 ctx
->Driver
.CheckQuery( ctx
, q
);
491 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
498 * New with GL_EXT_timer_query
500 static void GLAPIENTRY
501 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
503 struct gl_query_object
*q
= NULL
;
504 GET_CURRENT_CONTEXT(ctx
);
505 ASSERT_OUTSIDE_BEGIN_END(ctx
);
508 q
= _mesa_lookup_query_object(ctx
, id
);
510 if (!q
|| q
->Active
) {
511 _mesa_error(ctx
, GL_INVALID_OPERATION
,
512 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
517 case GL_QUERY_RESULT_ARB
:
519 ctx
->Driver
.WaitQuery(ctx
, q
);
522 case GL_QUERY_RESULT_AVAILABLE_ARB
:
524 ctx
->Driver
.CheckQuery( ctx
, q
);
528 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
535 _mesa_init_queryobj_dispatch(struct _glapi_table
*disp
)
537 SET_GenQueriesARB(disp
, _mesa_GenQueriesARB
);
538 SET_DeleteQueriesARB(disp
, _mesa_DeleteQueriesARB
);
539 SET_IsQueryARB(disp
, _mesa_IsQueryARB
);
540 SET_BeginQueryARB(disp
, _mesa_BeginQueryARB
);
541 SET_EndQueryARB(disp
, _mesa_EndQueryARB
);
542 SET_GetQueryivARB(disp
, _mesa_GetQueryivARB
);
543 SET_GetQueryObjectivARB(disp
, _mesa_GetQueryObjectivARB
);
544 SET_GetQueryObjectuivARB(disp
, _mesa_GetQueryObjectuivARB
);
546 SET_GetQueryObjecti64vEXT(disp
, _mesa_GetQueryObjecti64vEXT
);
547 SET_GetQueryObjectui64vEXT(disp
, _mesa_GetQueryObjectui64vEXT
);
551 #endif /* FEATURE_queryobj */
555 * Allocate/init the context state related to query objects.
558 _mesa_init_queryobj(struct gl_context
*ctx
)
560 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
561 ctx
->Query
.CurrentOcclusionObject
= NULL
;
566 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
569 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
571 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
572 struct gl_context
*ctx
= (struct gl_context
*)userData
;
573 ctx
->Driver
.DeleteQuery(ctx
, q
);
578 * Free the context state related to query objects.
581 _mesa_free_queryobj_data(struct gl_context
*ctx
)
583 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
584 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);