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(GLcontext
*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(GLcontext
*ctx
, struct gl_query_object
*q
)
72 * End a query. Software driver fallback.
73 * Called via ctx->Driver.EndQuery().
76 _mesa_end_query(GLcontext
*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(GLcontext
*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(GLcontext
*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(GLcontext
*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(GLcontext
*ctx
, GLenum target
)
141 case GL_SAMPLES_PASSED_ARB
:
142 if (ctx
->Extensions
.ARB_occlusion_query
)
143 return &ctx
->Query
.CurrentOcclusionObject
;
146 case GL_TIME_ELAPSED_EXT
:
147 if (ctx
->Extensions
.EXT_timer_query
)
148 return &ctx
->Query
.CurrentTimerObject
;
151 #if FEATURE_EXT_transform_feedback
152 case GL_PRIMITIVES_GENERATED
:
153 if (ctx
->Extensions
.EXT_transform_feedback
)
154 return &ctx
->Query
.PrimitivesGenerated
;
157 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
158 if (ctx
->Extensions
.EXT_transform_feedback
)
159 return &ctx
->Query
.PrimitivesWritten
;
170 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
173 GET_CURRENT_CONTEXT(ctx
);
174 ASSERT_OUTSIDE_BEGIN_END(ctx
);
177 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
181 /* No query objects can be active at this time! */
182 if (ctx
->Query
.CurrentOcclusionObject
||
183 ctx
->Query
.CurrentTimerObject
) {
184 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
188 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
191 for (i
= 0; i
< n
; i
++) {
192 struct gl_query_object
*q
193 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
195 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
199 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
206 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
209 GET_CURRENT_CONTEXT(ctx
);
210 ASSERT_OUTSIDE_BEGIN_END(ctx
);
213 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
217 /* No query objects can be active at this time! */
218 if (ctx
->Query
.CurrentOcclusionObject
||
219 ctx
->Query
.CurrentTimerObject
) {
220 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
224 for (i
= 0; i
< n
; i
++) {
226 struct gl_query_object
*q
= _mesa_lookup_query_object(ctx
, ids
[i
]);
228 ASSERT(!q
->Active
); /* should be caught earlier */
229 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
230 ctx
->Driver
.DeleteQuery(ctx
, q
);
238 _mesa_IsQueryARB(GLuint id
)
240 GET_CURRENT_CONTEXT(ctx
);
241 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
243 if (id
&& _mesa_lookup_query_object(ctx
, id
))
250 static void GLAPIENTRY
251 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
253 struct gl_query_object
*q
, **bindpt
;
254 GET_CURRENT_CONTEXT(ctx
);
255 ASSERT_OUTSIDE_BEGIN_END(ctx
);
257 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
259 bindpt
= get_query_binding_point(ctx
, target
);
261 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
266 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
270 q
= _mesa_lookup_query_object(ctx
, id
);
272 /* create new object */
273 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
275 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
278 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
281 /* pre-existing object */
283 _mesa_error(ctx
, GL_INVALID_OPERATION
,
284 "glBeginQueryARB(query already active)");
294 /* XXX should probably refcount query objects */
297 ctx
->Driver
.BeginQuery(ctx
, q
);
301 static void GLAPIENTRY
302 _mesa_EndQueryARB(GLenum target
)
304 struct gl_query_object
*q
, **bindpt
;
305 GET_CURRENT_CONTEXT(ctx
);
306 ASSERT_OUTSIDE_BEGIN_END(ctx
);
308 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
310 bindpt
= get_query_binding_point(ctx
, target
);
312 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
316 /* XXX should probably refcount query objects */
320 if (!q
|| !q
->Active
) {
321 _mesa_error(ctx
, GL_INVALID_OPERATION
,
322 "glEndQueryARB(no matching glBeginQueryARB)");
326 q
->Active
= GL_FALSE
;
327 ctx
->Driver
.EndQuery(ctx
, q
);
332 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
334 struct gl_query_object
*q
, **bindpt
;
335 GET_CURRENT_CONTEXT(ctx
);
336 ASSERT_OUTSIDE_BEGIN_END(ctx
);
338 bindpt
= get_query_binding_point(ctx
, target
);
340 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryARB(target)");
347 case GL_QUERY_COUNTER_BITS_ARB
:
348 *params
= 8 * sizeof(q
->Result
);
350 case GL_CURRENT_QUERY_ARB
:
351 *params
= q
? q
->Id
: 0;
354 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
361 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
363 struct gl_query_object
*q
= NULL
;
364 GET_CURRENT_CONTEXT(ctx
);
365 ASSERT_OUTSIDE_BEGIN_END(ctx
);
368 q
= _mesa_lookup_query_object(ctx
, id
);
370 if (!q
|| q
->Active
) {
371 _mesa_error(ctx
, GL_INVALID_OPERATION
,
372 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
377 case GL_QUERY_RESULT_ARB
:
379 ctx
->Driver
.WaitQuery(ctx
, q
);
380 /* if result is too large for returned type, clamp to max value */
381 if (q
->Result
> 0x7fffffff) {
382 *params
= 0x7fffffff;
385 *params
= (GLint
)q
->Result
;
388 case GL_QUERY_RESULT_AVAILABLE_ARB
:
390 ctx
->Driver
.CheckQuery( ctx
, q
);
394 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
401 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
403 struct gl_query_object
*q
= NULL
;
404 GET_CURRENT_CONTEXT(ctx
);
405 ASSERT_OUTSIDE_BEGIN_END(ctx
);
408 q
= _mesa_lookup_query_object(ctx
, id
);
410 if (!q
|| q
->Active
) {
411 _mesa_error(ctx
, GL_INVALID_OPERATION
,
412 "glGetQueryObjectuivARB(id=%d is invalid or active)", id
);
417 case GL_QUERY_RESULT_ARB
:
419 ctx
->Driver
.WaitQuery(ctx
, q
);
420 /* if result is too large for returned type, clamp to max value */
421 if (q
->Result
> 0xffffffff) {
422 *params
= 0xffffffff;
425 *params
= (GLuint
)q
->Result
;
428 case GL_QUERY_RESULT_AVAILABLE_ARB
:
430 ctx
->Driver
.CheckQuery( ctx
, q
);
434 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
441 * New with GL_EXT_timer_query
443 static void GLAPIENTRY
444 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
446 struct gl_query_object
*q
= NULL
;
447 GET_CURRENT_CONTEXT(ctx
);
448 ASSERT_OUTSIDE_BEGIN_END(ctx
);
451 q
= _mesa_lookup_query_object(ctx
, id
);
453 if (!q
|| q
->Active
) {
454 _mesa_error(ctx
, GL_INVALID_OPERATION
,
455 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
460 case GL_QUERY_RESULT_ARB
:
462 ctx
->Driver
.WaitQuery(ctx
, q
);
465 case GL_QUERY_RESULT_AVAILABLE_ARB
:
467 ctx
->Driver
.CheckQuery( ctx
, q
);
471 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
478 * New with GL_EXT_timer_query
480 static void GLAPIENTRY
481 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
483 struct gl_query_object
*q
= NULL
;
484 GET_CURRENT_CONTEXT(ctx
);
485 ASSERT_OUTSIDE_BEGIN_END(ctx
);
488 q
= _mesa_lookup_query_object(ctx
, id
);
490 if (!q
|| q
->Active
) {
491 _mesa_error(ctx
, GL_INVALID_OPERATION
,
492 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
497 case GL_QUERY_RESULT_ARB
:
499 ctx
->Driver
.WaitQuery(ctx
, q
);
502 case GL_QUERY_RESULT_AVAILABLE_ARB
:
504 ctx
->Driver
.CheckQuery( ctx
, q
);
508 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
515 _mesa_init_queryobj_dispatch(struct _glapi_table
*disp
)
517 SET_GenQueriesARB(disp
, _mesa_GenQueriesARB
);
518 SET_DeleteQueriesARB(disp
, _mesa_DeleteQueriesARB
);
519 SET_IsQueryARB(disp
, _mesa_IsQueryARB
);
520 SET_BeginQueryARB(disp
, _mesa_BeginQueryARB
);
521 SET_EndQueryARB(disp
, _mesa_EndQueryARB
);
522 SET_GetQueryivARB(disp
, _mesa_GetQueryivARB
);
523 SET_GetQueryObjectivARB(disp
, _mesa_GetQueryObjectivARB
);
524 SET_GetQueryObjectuivARB(disp
, _mesa_GetQueryObjectuivARB
);
526 SET_GetQueryObjecti64vEXT(disp
, _mesa_GetQueryObjecti64vEXT
);
527 SET_GetQueryObjectui64vEXT(disp
, _mesa_GetQueryObjectui64vEXT
);
531 #endif /* FEATURE_queryobj */
535 * Allocate/init the context state related to query objects.
538 _mesa_init_queryobj(GLcontext
*ctx
)
540 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
541 ctx
->Query
.CurrentOcclusionObject
= NULL
;
546 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
549 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
551 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
552 GLcontext
*ctx
= (GLcontext
*)userData
;
553 ctx
->Driver
.DeleteQuery(ctx
, q
);
558 * Free the context state related to query objects.
561 _mesa_free_queryobj_data(GLcontext
*ctx
)
563 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
564 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);