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.
27 * Functions to implement the GL_ARB_occlusion_query extension.
40 * Allocate a new occlusion query object.
41 * \param target - must be GL_SAMPLES_PASSED_ARB at this time
42 * \param id - the object's ID
43 * \return pointer to new query_object object or NULL if out of memory.
45 static struct gl_query_object
*
46 new_query_object(GLenum target
, GLuint id
)
48 struct gl_query_object
*q
= MALLOC_STRUCT(gl_query_object
);
60 * Delete an occlusion query object.
61 * Not removed from hash table here.
64 delete_query_object(struct gl_query_object
*q
)
70 struct gl_query_object
*
71 lookup_query_object(GLcontext
*ctx
, GLuint id
)
73 return (struct gl_query_object
*)
74 _mesa_HashLookup(ctx
->Query
.QueryObjects
, id
);
80 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
82 GET_CURRENT_CONTEXT(ctx
);
84 ASSERT_OUTSIDE_BEGIN_END(ctx
);
87 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
91 /* No query objects can be active at this time! */
92 if (ctx
->Query
.CurrentOcclusionObject
) {
93 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
97 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
100 for (i
= 0; i
< n
; i
++) {
101 struct gl_query_object
*q
= new_query_object(GL_SAMPLES_PASSED_ARB
,
104 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
108 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
115 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
117 GET_CURRENT_CONTEXT(ctx
);
119 ASSERT_OUTSIDE_BEGIN_END(ctx
);
122 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
126 /* No query objects can be active at this time! */
127 if (ctx
->Query
.CurrentOcclusionObject
) {
128 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
132 for (i
= 0; i
< n
; i
++) {
134 struct gl_query_object
*q
= lookup_query_object(ctx
, ids
[i
]);
136 ASSERT(!q
->Active
); /* should be caught earlier */
137 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
138 delete_query_object(q
);
146 _mesa_IsQueryARB(GLuint id
)
148 GET_CURRENT_CONTEXT(ctx
);
149 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
151 if (id
&& lookup_query_object(ctx
, id
))
159 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
161 GET_CURRENT_CONTEXT(ctx
);
162 struct gl_query_object
*q
;
163 ASSERT_OUTSIDE_BEGIN_END(ctx
);
165 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
167 if (target
!= GL_SAMPLES_PASSED_ARB
) {
168 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
173 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
177 if (ctx
->Query
.CurrentOcclusionObject
) {
178 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(target)");
182 q
= lookup_query_object(ctx
, id
);
184 /* create new object */
185 q
= new_query_object(target
, id
);
187 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
190 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
193 /* pre-existing object */
194 if (q
->Target
!= target
) {
195 _mesa_error(ctx
, GL_INVALID_OPERATION
,
196 "glBeginQueryARB(target mismatch)");
200 _mesa_error(ctx
, GL_INVALID_OPERATION
,
201 "glBeginQueryARB(query already active)");
209 ctx
->Query
.CurrentOcclusionObject
= q
;
211 if (ctx
->Driver
.BeginQuery
) {
212 ctx
->Driver
.BeginQuery(ctx
, q
);
218 _mesa_EndQueryARB(GLenum target
)
220 GET_CURRENT_CONTEXT(ctx
);
221 struct gl_query_object
*q
;
222 ASSERT_OUTSIDE_BEGIN_END(ctx
);
224 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
227 case GL_SAMPLES_PASSED_ARB
:
228 q
= ctx
->Query
.CurrentOcclusionObject
;
229 ctx
->Query
.CurrentOcclusionObject
= NULL
;
232 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
236 if (!q
|| !q
->Active
) {
237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
238 "glEndQueryARB(no matching glBeginQueryARB)");
242 q
->Active
= GL_FALSE
;
243 if (ctx
->Driver
.EndQuery
) {
244 ctx
->Driver
.EndQuery(ctx
, q
);
253 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
255 GET_CURRENT_CONTEXT(ctx
);
256 struct gl_query_object
*q
;
257 ASSERT_OUTSIDE_BEGIN_END(ctx
);
260 case GL_SAMPLES_PASSED_ARB
:
261 q
= ctx
->Query
.CurrentOcclusionObject
;
264 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(target)");
269 case GL_QUERY_COUNTER_BITS_ARB
:
270 *params
= 8 * sizeof(q
->Result
);
272 case GL_CURRENT_QUERY_ARB
:
273 *params
= q
? q
->Id
: 0;
276 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
283 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
285 GET_CURRENT_CONTEXT(ctx
);
286 struct gl_query_object
*q
= NULL
;
287 ASSERT_OUTSIDE_BEGIN_END(ctx
);
290 q
= lookup_query_object(ctx
, id
);
292 if (!q
|| q
->Active
) {
293 _mesa_error(ctx
, GL_INVALID_OPERATION
,
294 "glGetQueryObjectivARB(id=%d is active)", id
);
299 case GL_QUERY_RESULT_ARB
:
302 case GL_QUERY_RESULT_AVAILABLE_ARB
:
303 /* XXX revisit when we have a hardware implementation! */
307 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
314 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
316 GET_CURRENT_CONTEXT(ctx
);
317 struct gl_query_object
*q
= NULL
;
318 ASSERT_OUTSIDE_BEGIN_END(ctx
);
321 q
= lookup_query_object(ctx
, id
);
323 if (!q
|| q
->Active
) {
324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
325 "glGetQueryObjectuivARB(id=%d is active)", id
);
330 case GL_QUERY_RESULT_ARB
:
333 case GL_QUERY_RESULT_AVAILABLE_ARB
:
334 /* XXX revisit when we have a hardware implementation! */
338 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
346 * Allocate/init the context state related to occlusion query objects.
349 _mesa_init_occlude(GLcontext
*ctx
)
351 #if FEATURE_ARB_occlusion_query
352 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
353 ctx
->Query
.CurrentOcclusionObject
= NULL
;
359 * Free the context state related to occlusion query objects.
362 _mesa_free_occlude_data(GLcontext
*ctx
)
365 GLuint id
= _mesa_HashFirstEntry(ctx
->Query
.QueryObjects
);
367 struct gl_query_object
*q
= lookup_query_object(ctx
, id
);
369 delete_query_object(q
);
370 _mesa_HashRemove(ctx
->Query
.QueryObjects
, id
);
376 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);