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 "glapi/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 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
137 GET_CURRENT_CONTEXT(ctx
);
138 ASSERT_OUTSIDE_BEGIN_END(ctx
);
141 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
145 /* No query objects can be active at this time! */
146 if (ctx
->Query
.CurrentOcclusionObject
||
147 ctx
->Query
.CurrentTimerObject
) {
148 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
152 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
155 for (i
= 0; i
< n
; i
++) {
156 struct gl_query_object
*q
157 = ctx
->Driver
.NewQueryObject(ctx
, first
+ i
);
159 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
163 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
170 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
173 GET_CURRENT_CONTEXT(ctx
);
174 ASSERT_OUTSIDE_BEGIN_END(ctx
);
177 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(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
, "glDeleteQueriesARB");
188 for (i
= 0; i
< n
; i
++) {
190 struct gl_query_object
*q
= _mesa_lookup_query_object(ctx
, ids
[i
]);
192 ASSERT(!q
->Active
); /* should be caught earlier */
193 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
194 ctx
->Driver
.DeleteQuery(ctx
, q
);
202 _mesa_IsQueryARB(GLuint id
)
204 GET_CURRENT_CONTEXT(ctx
);
205 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
207 if (id
&& _mesa_lookup_query_object(ctx
, id
))
214 static void GLAPIENTRY
215 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
217 struct gl_query_object
*q
;
218 GET_CURRENT_CONTEXT(ctx
);
219 ASSERT_OUTSIDE_BEGIN_END(ctx
);
221 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
224 case GL_SAMPLES_PASSED_ARB
:
225 if (!ctx
->Extensions
.ARB_occlusion_query
) {
226 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
229 if (ctx
->Query
.CurrentOcclusionObject
) {
230 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
234 case GL_TIME_ELAPSED_EXT
:
235 if (!ctx
->Extensions
.EXT_timer_query
) {
236 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
239 if (ctx
->Query
.CurrentTimerObject
) {
240 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
245 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
250 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
254 q
= _mesa_lookup_query_object(ctx
, id
);
256 /* create new object */
257 q
= ctx
->Driver
.NewQueryObject(ctx
, id
);
259 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
262 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
265 /* pre-existing object */
267 _mesa_error(ctx
, GL_INVALID_OPERATION
,
268 "glBeginQueryARB(query already active)");
278 if (target
== GL_SAMPLES_PASSED_ARB
) {
279 ctx
->Query
.CurrentOcclusionObject
= q
;
281 else if (target
== GL_TIME_ELAPSED_EXT
) {
282 ctx
->Query
.CurrentTimerObject
= q
;
285 ctx
->Driver
.BeginQuery(ctx
, q
);
289 static void GLAPIENTRY
290 _mesa_EndQueryARB(GLenum target
)
292 struct gl_query_object
*q
;
293 GET_CURRENT_CONTEXT(ctx
);
294 ASSERT_OUTSIDE_BEGIN_END(ctx
);
296 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
299 case GL_SAMPLES_PASSED_ARB
:
300 if (!ctx
->Extensions
.ARB_occlusion_query
) {
301 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
304 q
= ctx
->Query
.CurrentOcclusionObject
;
305 ctx
->Query
.CurrentOcclusionObject
= NULL
;
307 case GL_TIME_ELAPSED_EXT
:
308 if (!ctx
->Extensions
.EXT_timer_query
) {
309 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
312 q
= ctx
->Query
.CurrentTimerObject
;
313 ctx
->Query
.CurrentTimerObject
= NULL
;
316 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
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
;
335 GET_CURRENT_CONTEXT(ctx
);
336 ASSERT_OUTSIDE_BEGIN_END(ctx
);
339 case GL_SAMPLES_PASSED_ARB
:
340 if (!ctx
->Extensions
.ARB_occlusion_query
) {
341 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
344 q
= ctx
->Query
.CurrentOcclusionObject
;
346 case GL_TIME_ELAPSED_EXT
:
347 if (!ctx
->Extensions
.EXT_timer_query
) {
348 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
351 q
= ctx
->Query
.CurrentTimerObject
;
354 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(target)");
359 case GL_QUERY_COUNTER_BITS_ARB
:
360 *params
= 8 * sizeof(q
->Result
);
362 case GL_CURRENT_QUERY_ARB
:
363 *params
= q
? q
->Id
: 0;
366 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
373 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
375 struct gl_query_object
*q
= NULL
;
376 GET_CURRENT_CONTEXT(ctx
);
377 ASSERT_OUTSIDE_BEGIN_END(ctx
);
380 q
= _mesa_lookup_query_object(ctx
, id
);
382 if (!q
|| q
->Active
) {
383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
384 "glGetQueryObjectivARB(id=%d is invalid or active)", id
);
389 case GL_QUERY_RESULT_ARB
:
391 ctx
->Driver
.WaitQuery(ctx
, q
);
392 /* if result is too large for returned type, clamp to max value */
393 if (q
->Result
> 0x7fffffff) {
394 *params
= 0x7fffffff;
397 *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
->Result
> 0xffffffff) {
434 *params
= 0xffffffff;
437 *params
= (GLuint
)q
->Result
;
440 case GL_QUERY_RESULT_AVAILABLE_ARB
:
442 ctx
->Driver
.CheckQuery( ctx
, q
);
446 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
453 * New with GL_EXT_timer_query
455 static void GLAPIENTRY
456 _mesa_GetQueryObjecti64vEXT(GLuint id
, GLenum pname
, GLint64EXT
*params
)
458 struct gl_query_object
*q
= NULL
;
459 GET_CURRENT_CONTEXT(ctx
);
460 ASSERT_OUTSIDE_BEGIN_END(ctx
);
463 q
= _mesa_lookup_query_object(ctx
, id
);
465 if (!q
|| q
->Active
) {
466 _mesa_error(ctx
, GL_INVALID_OPERATION
,
467 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id
);
472 case GL_QUERY_RESULT_ARB
:
474 ctx
->Driver
.WaitQuery(ctx
, q
);
477 case GL_QUERY_RESULT_AVAILABLE_ARB
:
479 ctx
->Driver
.CheckQuery( ctx
, q
);
483 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjecti64vARB(pname)");
490 * New with GL_EXT_timer_query
492 static void GLAPIENTRY
493 _mesa_GetQueryObjectui64vEXT(GLuint id
, GLenum pname
, GLuint64EXT
*params
)
495 struct gl_query_object
*q
= NULL
;
496 GET_CURRENT_CONTEXT(ctx
);
497 ASSERT_OUTSIDE_BEGIN_END(ctx
);
500 q
= _mesa_lookup_query_object(ctx
, id
);
502 if (!q
|| q
->Active
) {
503 _mesa_error(ctx
, GL_INVALID_OPERATION
,
504 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id
);
509 case GL_QUERY_RESULT_ARB
:
511 ctx
->Driver
.WaitQuery(ctx
, q
);
514 case GL_QUERY_RESULT_AVAILABLE_ARB
:
516 ctx
->Driver
.CheckQuery( ctx
, q
);
520 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectui64vARB(pname)");
527 _mesa_init_queryobj_dispatch(struct _glapi_table
*disp
)
529 SET_GenQueriesARB(disp
, _mesa_GenQueriesARB
);
530 SET_DeleteQueriesARB(disp
, _mesa_DeleteQueriesARB
);
531 SET_IsQueryARB(disp
, _mesa_IsQueryARB
);
532 SET_BeginQueryARB(disp
, _mesa_BeginQueryARB
);
533 SET_EndQueryARB(disp
, _mesa_EndQueryARB
);
534 SET_GetQueryivARB(disp
, _mesa_GetQueryivARB
);
535 SET_GetQueryObjectivARB(disp
, _mesa_GetQueryObjectivARB
);
536 SET_GetQueryObjectuivARB(disp
, _mesa_GetQueryObjectuivARB
);
538 SET_GetQueryObjecti64vEXT(disp
, _mesa_GetQueryObjecti64vEXT
);
539 SET_GetQueryObjectui64vEXT(disp
, _mesa_GetQueryObjectui64vEXT
);
543 #endif /* FEATURE_queryobj */
547 * Allocate/init the context state related to query objects.
550 _mesa_init_queryobj(GLcontext
*ctx
)
552 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
553 ctx
->Query
.CurrentOcclusionObject
= NULL
;
558 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
561 delete_queryobj_cb(GLuint id
, void *data
, void *userData
)
563 struct gl_query_object
*q
= (struct gl_query_object
*) data
;
564 GLcontext
*ctx
= (GLcontext
*)userData
;
565 ctx
->Driver
.DeleteQuery(ctx
, q
);
570 * Free the context state related to query objects.
573 _mesa_free_queryobj_data(GLcontext
*ctx
)
575 _mesa_HashDeleteAll(ctx
->Query
.QueryObjects
, delete_queryobj_cb
, ctx
);
576 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);