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
);
54 q
->Ready
= GL_TRUE
; /* correct, see spec */
61 * Delete an occlusion query object.
62 * Not removed from hash table here.
65 delete_query_object(struct gl_query_object
*q
)
71 struct gl_query_object
*
72 lookup_query_object(GLcontext
*ctx
, GLuint id
)
74 return (struct gl_query_object
*)
75 _mesa_HashLookup(ctx
->Query
.QueryObjects
, id
);
81 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
83 GET_CURRENT_CONTEXT(ctx
);
85 ASSERT_OUTSIDE_BEGIN_END(ctx
);
88 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
92 /* No query objects can be active at this time! */
93 if (ctx
->Query
.CurrentOcclusionObject
) {
94 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
98 first
= _mesa_HashFindFreeKeyBlock(ctx
->Query
.QueryObjects
, n
);
101 for (i
= 0; i
< n
; i
++) {
102 struct gl_query_object
*q
= new_query_object(GL_SAMPLES_PASSED_ARB
,
105 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
109 _mesa_HashInsert(ctx
->Query
.QueryObjects
, first
+ i
, q
);
116 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
118 GET_CURRENT_CONTEXT(ctx
);
120 ASSERT_OUTSIDE_BEGIN_END(ctx
);
123 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
127 /* No query objects can be active at this time! */
128 if (ctx
->Query
.CurrentOcclusionObject
) {
129 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
133 for (i
= 0; i
< n
; i
++) {
135 struct gl_query_object
*q
= lookup_query_object(ctx
, ids
[i
]);
137 ASSERT(!q
->Active
); /* should be caught earlier */
138 _mesa_HashRemove(ctx
->Query
.QueryObjects
, ids
[i
]);
139 delete_query_object(q
);
147 _mesa_IsQueryARB(GLuint id
)
149 GET_CURRENT_CONTEXT(ctx
);
150 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
152 if (id
&& lookup_query_object(ctx
, id
))
160 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
162 GET_CURRENT_CONTEXT(ctx
);
163 struct gl_query_object
*q
;
164 ASSERT_OUTSIDE_BEGIN_END(ctx
);
166 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
168 if (target
!= GL_SAMPLES_PASSED_ARB
) {
169 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
174 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
178 if (ctx
->Query
.CurrentOcclusionObject
) {
179 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(target)");
183 q
= lookup_query_object(ctx
, id
);
185 /* create new object */
186 q
= new_query_object(target
, id
);
188 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
191 _mesa_HashInsert(ctx
->Query
.QueryObjects
, id
, q
);
194 /* pre-existing object */
195 if (q
->Target
!= target
) {
196 _mesa_error(ctx
, GL_INVALID_OPERATION
,
197 "glBeginQueryARB(target mismatch)");
201 _mesa_error(ctx
, GL_INVALID_OPERATION
,
202 "glBeginQueryARB(query already active)");
210 ctx
->Query
.CurrentOcclusionObject
= q
;
212 if (ctx
->Driver
.BeginQuery
) {
213 ctx
->Driver
.BeginQuery(ctx
, q
);
219 _mesa_EndQueryARB(GLenum target
)
221 GET_CURRENT_CONTEXT(ctx
);
222 struct gl_query_object
*q
;
223 ASSERT_OUTSIDE_BEGIN_END(ctx
);
225 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
228 case GL_SAMPLES_PASSED_ARB
:
229 q
= ctx
->Query
.CurrentOcclusionObject
;
230 ctx
->Query
.CurrentOcclusionObject
= NULL
;
233 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
237 if (!q
|| !q
->Active
) {
238 _mesa_error(ctx
, GL_INVALID_OPERATION
,
239 "glEndQueryARB(no matching glBeginQueryARB)");
243 q
->Active
= GL_FALSE
;
244 if (ctx
->Driver
.EndQuery
) {
245 ctx
->Driver
.EndQuery(ctx
, q
);
254 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
256 GET_CURRENT_CONTEXT(ctx
);
257 struct gl_query_object
*q
;
258 ASSERT_OUTSIDE_BEGIN_END(ctx
);
261 case GL_SAMPLES_PASSED_ARB
:
262 q
= ctx
->Query
.CurrentOcclusionObject
;
265 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(target)");
270 case GL_QUERY_COUNTER_BITS_ARB
:
271 *params
= 8 * sizeof(q
->Result
);
273 case GL_CURRENT_QUERY_ARB
:
274 *params
= q
? q
->Id
: 0;
277 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
284 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
286 GET_CURRENT_CONTEXT(ctx
);
287 struct gl_query_object
*q
= NULL
;
288 ASSERT_OUTSIDE_BEGIN_END(ctx
);
291 q
= lookup_query_object(ctx
, id
);
293 if (!q
|| q
->Active
) {
294 _mesa_error(ctx
, GL_INVALID_OPERATION
,
295 "glGetQueryObjectivARB(id=%d is active)", id
);
300 case GL_QUERY_RESULT_ARB
:
302 /* Wait for the query to finish! */
303 /* If using software rendering, the result will always be ready
304 * by time we get here. Otherwise, we must be using hardware!
306 ASSERT(ctx
->Driver
.EndQuery
);
310 case GL_QUERY_RESULT_AVAILABLE_ARB
:
311 /* XXX revisit when we have a hardware implementation! */
315 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
322 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
324 GET_CURRENT_CONTEXT(ctx
);
325 struct gl_query_object
*q
= NULL
;
326 ASSERT_OUTSIDE_BEGIN_END(ctx
);
329 q
= lookup_query_object(ctx
, id
);
331 if (!q
|| q
->Active
) {
332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
333 "glGetQueryObjectuivARB(id=%d is active)", id
);
338 case GL_QUERY_RESULT_ARB
:
340 /* Wait for the query to finish! */
341 /* If using software rendering, the result will always be ready
342 * by time we get here. Otherwise, we must be using hardware!
344 ASSERT(ctx
->Driver
.EndQuery
);
348 case GL_QUERY_RESULT_AVAILABLE_ARB
:
349 /* XXX revisit when we have a hardware implementation! */
353 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");
361 * Allocate/init the context state related to occlusion query objects.
364 _mesa_init_occlude(GLcontext
*ctx
)
366 #if FEATURE_ARB_occlusion_query
367 ctx
->Query
.QueryObjects
= _mesa_NewHashTable();
368 ctx
->Query
.CurrentOcclusionObject
= NULL
;
374 * Free the context state related to occlusion query objects.
377 _mesa_free_occlude_data(GLcontext
*ctx
)
380 GLuint id
= _mesa_HashFirstEntry(ctx
->Query
.QueryObjects
);
382 struct gl_query_object
*q
= lookup_query_object(ctx
, id
);
384 delete_query_object(q
);
385 _mesa_HashRemove(ctx
->Query
.QueryObjects
, id
);
391 _mesa_DeleteHashTable(ctx
->Query
.QueryObjects
);