2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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.
39 struct occlusion_query
50 _mesa_init_occlude(GLcontext
*ctx
)
52 #if FEATURE_ARB_occlusion_query
53 ctx
->Occlusion
.QueryObjects
= _mesa_NewHashTable();
56 ctx
->OcclusionResult
= GL_FALSE
;
57 ctx
->OcclusionResultSaved
= GL_FALSE
;
63 * Allocate a new occlusion query object.
64 * \param target - must be GL_SAMPLES_PASSED_ARB at this time
65 * \param id - the object's ID
66 * \return pointer to new occlusion_query object or NULL if out of memory.
68 static struct occlusion_query
*
69 new_query_object(GLenum target
, GLuint id
)
71 struct occlusion_query
*q
= MALLOC_STRUCT(occlusion_query
);
83 * Delete an occlusion query object.
86 delete_query_object(struct occlusion_query
*q
)
93 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
95 GET_CURRENT_CONTEXT(ctx
);
97 ASSERT_OUTSIDE_BEGIN_END(ctx
);
100 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
104 if (ctx
->Occlusion
.Active
) {
105 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
109 first
= _mesa_HashFindFreeKeyBlock(ctx
->Occlusion
.QueryObjects
, n
);
112 for (i
= 0; i
< n
; i
++) {
113 struct occlusion_query
*q
= new_query_object(GL_SAMPLES_PASSED_ARB
,
116 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
120 _mesa_HashInsert(ctx
->Occlusion
.QueryObjects
, first
+ i
, q
);
127 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
129 GET_CURRENT_CONTEXT(ctx
);
131 ASSERT_OUTSIDE_BEGIN_END(ctx
);
134 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
138 if (ctx
->Occlusion
.Active
) {
139 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
143 for (i
= 0; i
< n
; i
++) {
145 struct occlusion_query
*q
= (struct occlusion_query
*)
146 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, ids
[i
]);
148 _mesa_HashRemove(ctx
->Occlusion
.QueryObjects
, ids
[i
]);
149 delete_query_object(q
);
157 _mesa_IsQueryARB(GLuint id
)
159 GET_CURRENT_CONTEXT(ctx
);
160 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
162 if (id
&& _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
))
170 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
172 GET_CURRENT_CONTEXT(ctx
);
173 struct occlusion_query
*q
;
174 ASSERT_OUTSIDE_BEGIN_END(ctx
);
176 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
178 if (target
!= GL_SAMPLES_PASSED_ARB
) {
179 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
184 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(id==0)");
188 if (ctx
->Occlusion
.CurrentQueryObject
) {
189 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(target)");
193 q
= (struct occlusion_query
*)
194 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
195 if (q
&& q
->Active
) {
196 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
200 q
= new_query_object(target
, id
);
202 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
205 _mesa_HashInsert(ctx
->Occlusion
.QueryObjects
, id
, q
);
209 q
->PassedCounter
= 0;
210 ctx
->Occlusion
.Active
= GL_TRUE
;
211 ctx
->Occlusion
.CurrentQueryObject
= id
;
212 ctx
->Occlusion
.PassedCounter
= 0;
217 _mesa_EndQueryARB(GLenum target
)
219 GET_CURRENT_CONTEXT(ctx
);
220 struct occlusion_query
*q
= NULL
;
221 ASSERT_OUTSIDE_BEGIN_END(ctx
);
223 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
225 if (target
!= GL_SAMPLES_PASSED_ARB
) {
226 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
230 if (ctx
->Occlusion
.CurrentQueryObject
)
231 q
= (struct occlusion_query
*)
232 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
,
233 ctx
->Occlusion
.CurrentQueryObject
);
234 if (!q
|| !q
->Active
) {
235 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glEndQuery with no glBeginQuery");
239 q
->PassedCounter
= ctx
->Occlusion
.PassedCounter
;
240 q
->Active
= GL_FALSE
;
241 ctx
->Occlusion
.Active
= GL_FALSE
;
242 ctx
->Occlusion
.CurrentQueryObject
= 0;
247 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
249 GET_CURRENT_CONTEXT(ctx
);
250 ASSERT_OUTSIDE_BEGIN_END(ctx
);
252 if (target
!= GL_SAMPLES_PASSED_ARB
) {
253 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(target)");
258 case GL_QUERY_COUNTER_BITS_ARB
:
259 *params
= 8 * sizeof(GLuint
);
261 case GL_CURRENT_QUERY_ARB
:
262 *params
= ctx
->Occlusion
.CurrentQueryObject
;
265 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
272 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
274 GET_CURRENT_CONTEXT(ctx
);
275 struct occlusion_query
*q
= NULL
;
276 ASSERT_OUTSIDE_BEGIN_END(ctx
);
279 q
= (struct occlusion_query
*)
280 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
282 if (!q
|| q
->Active
) {
283 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetQueryObjectivARB(id=%d)", id
);
288 case GL_QUERY_RESULT_ARB
:
289 *params
= q
->PassedCounter
;
291 case GL_QUERY_RESULT_AVAILABLE_ARB
:
292 /* XXX revisit when we have a hardware implementation! */
296 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
303 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
305 GET_CURRENT_CONTEXT(ctx
);
306 struct occlusion_query
*q
= NULL
;
307 ASSERT_OUTSIDE_BEGIN_END(ctx
);
310 q
= (struct occlusion_query
*)
311 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
312 if (!q
|| q
->Active
) {
313 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetQueryObjectuivARB(id=%d", id
);
318 case GL_QUERY_RESULT_ARB
:
319 *params
= q
->PassedCounter
;
321 case GL_QUERY_RESULT_AVAILABLE_ARB
:
322 /* XXX revisit when we have a hardware implementation! */
326 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");