2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 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 * \brief Functions to implement the GL_ARB_occlusion_query extension.
38 #ifndef GL_SAMPLES_PASSED_ARB
39 #define GL_SAMPLES_PASSED_ARB 0x8914
40 #define GL_QUERY_COUNTER_BITS_ARB 0x8864
41 #define GL_CURRENT_QUERY_ARB 0x8865
42 #define GL_QUERY_RESULT_ARB 0x8866
43 #define GL_QUERY_RESULT_AVAILABLE_ARB 0x8867
47 struct occlusion_query
57 * Allocate a new occlusion query object.
58 * \param target - must be GL_SAMPLES_PASSED_ARB at this time
59 * \param id - the object's ID
60 * \return pointer to new occlusion_query object or NULL if out of memory.
62 static struct occlusion_query
*
63 new_query_object(GLenum target
, GLuint id
)
65 struct occlusion_query
*q
= MALLOC_STRUCT(occlusion_query
);
77 * Delete an occlusion query object.
80 delete_query_object(struct occlusion_query
*q
)
87 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
89 GET_CURRENT_CONTEXT(ctx
);
91 ASSERT_OUTSIDE_BEGIN_END(ctx
);
94 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
98 if (ctx
->Occlusion
.Active
) {
99 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
103 first
= _mesa_HashFindFreeKeyBlock(ctx
->Occlusion
.QueryObjects
, n
);
106 for (i
= 0; i
< n
; i
++) {
107 struct occlusion_query
*q
= new_query_object(GL_SAMPLES_PASSED_ARB
,
110 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
114 _mesa_HashInsert(ctx
->Occlusion
.QueryObjects
, first
+ i
, q
);
121 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
123 GET_CURRENT_CONTEXT(ctx
);
125 ASSERT_OUTSIDE_BEGIN_END(ctx
);
128 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
132 if (ctx
->Occlusion
.Active
) {
133 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
137 for (i
= 0; i
< n
; i
++) {
139 struct occlusion_query
*q
= (struct occlusion_query
*)
140 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, ids
[i
]);
142 _mesa_HashRemove(ctx
->Occlusion
.QueryObjects
, ids
[i
]);
143 delete_query_object(q
);
151 _mesa_IsQueryARB(GLuint id
)
153 GET_CURRENT_CONTEXT(ctx
);
154 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
156 if (id
&& _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
))
164 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
166 GET_CURRENT_CONTEXT(ctx
);
167 struct occlusion_query
*q
;
168 ASSERT_OUTSIDE_BEGIN_END(ctx
);
170 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
172 if (target
!= GL_SAMPLES_PASSED_ARB
) {
173 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
177 if (ctx
->Occlusion
.CurrentQueryObject
) {
178 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(target)");
182 q
= (struct occlusion_query
*)
183 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
184 if (q
&& q
->Active
) {
185 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
189 q
= new_query_object(target
, id
);
191 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
194 _mesa_HashInsert(ctx
->Occlusion
.QueryObjects
, id
, q
);
198 q
->PassedCounter
= 0;
199 ctx
->Occlusion
.Active
= GL_TRUE
;
200 ctx
->Occlusion
.CurrentQueryObject
= id
;
201 ctx
->Occlusion
.PassedCounter
= 0;
206 _mesa_EndQueryARB(GLenum target
)
208 GET_CURRENT_CONTEXT(ctx
);
209 struct occlusion_query
*q
;
210 ASSERT_OUTSIDE_BEGIN_END(ctx
);
212 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
214 if (target
!= GL_SAMPLES_PASSED_ARB
) {
215 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
219 q
= (struct occlusion_query
*)
220 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
,
221 ctx
->Occlusion
.CurrentQueryObject
);
222 if (!q
|| !q
->Active
) {
223 _mesa_problem(ctx
, "bad query object in glEndQueryARB");
227 q
->PassedCounter
= ctx
->Occlusion
.PassedCounter
;
228 q
->Active
= GL_FALSE
;
229 ctx
->Occlusion
.Active
= GL_FALSE
;
230 ctx
->Occlusion
.CurrentQueryObject
= 0;
235 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
237 GET_CURRENT_CONTEXT(ctx
);
238 ASSERT_OUTSIDE_BEGIN_END(ctx
);
240 if (target
!= GL_SAMPLES_PASSED_ARB
) {
241 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(target)");
246 case GL_QUERY_COUNTER_BITS_ARB
:
247 *params
= 8 * sizeof(GLuint
);
249 case GL_CURRENT_QUERY_ARB
:
250 *params
= ctx
->Occlusion
.CurrentQueryObject
;
253 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
260 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
262 GET_CURRENT_CONTEXT(ctx
);
263 struct occlusion_query
*q
;
264 ASSERT_OUTSIDE_BEGIN_END(ctx
);
266 q
= (struct occlusion_query
*)
267 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
268 if (!q
|| q
->Active
) {
269 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetQueryObjectivARB");
274 case GL_QUERY_RESULT_ARB
:
275 *params
= q
->PassedCounter
;
277 case GL_QUERY_RESULT_AVAILABLE_ARB
:
278 /* XXX revisit when we have a hardware implementation! */
282 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
289 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
291 GET_CURRENT_CONTEXT(ctx
);
292 struct occlusion_query
*q
;
293 ASSERT_OUTSIDE_BEGIN_END(ctx
);
295 q
= (struct occlusion_query
*)
296 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
297 if (!q
|| q
->Active
) {
298 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetQueryObjectuivARB");
303 case GL_QUERY_RESULT_ARB
:
304 *params
= q
->PassedCounter
;
306 case GL_QUERY_RESULT_AVAILABLE_ARB
:
307 /* XXX revisit when we have a hardware implementation! */
311 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");