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 target - GL_SAMPLES_PASSED_ARB or GL_TIME_ELAPSED_EXT or 0.
38 * \param ctx - rendering context
39 * \param id - the object's ID
40 * \return pointer to new query_object object or NULL if out of memory.
42 struct gl_query_object
*
43 _mesa_new_query_object(GLcontext
*ctx
, GLuint id
)
45 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.
61 delete_query_object(struct gl_query_object
*q
)
67 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 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
);
349 case GL_QUERY_RESULT_AVAILABLE_ARB
:
350 /* XXX revisit when we have a hardware implementation! */
354 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
361 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
363 struct gl_query_object
*q
= NULL
;
364 GET_CURRENT_CONTEXT(ctx
);
365 ASSERT_OUTSIDE_BEGIN_END(ctx
);
368 q
= lookup_query_object(ctx
, id
);
370 if (!q
|| q
->Active
) {
371 _mesa_error(ctx
, GL_INVALID_OPERATION
,
372 "glGetQueryObjectuivARB(id=%d is active)", id
);
377 case GL_QUERY_RESULT_ARB
:
379 /* Wait for the query to finish! */
380 /* If using software rendering, the result will always be ready
381 * by time we get here. Otherwise, we must be using hardware!
383 ASSERT(ctx
->Driver
.EndQuery
);
387 case GL_QUERY_RESULT_AVAILABLE_ARB
:
388 /* XXX revisit when we have a hardware implementation! */
392 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
400 * Allocate/init the context state related to query objects.
403 _mesa_init_query(GLcontext
*ctx
)
405 #if FEATURE_ARB_occlusion_query
406 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
407 ctx
->Query
.CurrentOcclusionObject
= NULL
;
413 * Free the context state related to query objects.
416 _mesa_free_query_data(GLcontext
*ctx
)
419 GLuint id
= _mesa_HashFirstEntry(ctx
->Query
.QueryObjects
);
421 struct gl_query_object
*q
= lookup_query_object(ctx
, id
);
423 delete_query_object(q
);
424 _mesa_HashRemove(ctx
->Query
.QueryObjects
, id
);
430 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);