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 * 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
58 _mesa_init_occlude(GLcontext
*ctx
)
60 #if FEATURE_ARB_occlusion_query
61 ctx
->Occlusion
.QueryObjects
= _mesa_NewHashTable();
64 ctx
->OcclusionResult
= GL_FALSE
;
65 ctx
->OcclusionResultSaved
= GL_FALSE
;
71 * Allocate a new occlusion query object.
72 * \param target - must be GL_SAMPLES_PASSED_ARB at this time
73 * \param id - the object's ID
74 * \return pointer to new occlusion_query object or NULL if out of memory.
76 static struct occlusion_query
*
77 new_query_object(GLenum target
, GLuint id
)
79 struct occlusion_query
*q
= MALLOC_STRUCT(occlusion_query
);
91 * Delete an occlusion query object.
94 delete_query_object(struct occlusion_query
*q
)
101 _mesa_GenQueriesARB(GLsizei n
, GLuint
*ids
)
103 GET_CURRENT_CONTEXT(ctx
);
105 ASSERT_OUTSIDE_BEGIN_END(ctx
);
108 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenQueriesARB(n < 0)");
112 if (ctx
->Occlusion
.Active
) {
113 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGenQueriesARB");
117 first
= _mesa_HashFindFreeKeyBlock(ctx
->Occlusion
.QueryObjects
, n
);
120 for (i
= 0; i
< n
; i
++) {
121 struct occlusion_query
*q
= new_query_object(GL_SAMPLES_PASSED_ARB
,
124 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenQueriesARB");
128 _mesa_HashInsert(ctx
->Occlusion
.QueryObjects
, first
+ i
, q
);
135 _mesa_DeleteQueriesARB(GLsizei n
, const GLuint
*ids
)
137 GET_CURRENT_CONTEXT(ctx
);
139 ASSERT_OUTSIDE_BEGIN_END(ctx
);
142 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteQueriesARB(n < 0)");
146 if (ctx
->Occlusion
.Active
) {
147 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDeleteQueriesARB");
151 for (i
= 0; i
< n
; i
++) {
153 struct occlusion_query
*q
= (struct occlusion_query
*)
154 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, ids
[i
]);
156 _mesa_HashRemove(ctx
->Occlusion
.QueryObjects
, ids
[i
]);
157 delete_query_object(q
);
165 _mesa_IsQueryARB(GLuint id
)
167 GET_CURRENT_CONTEXT(ctx
);
168 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
170 if (id
&& _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
))
178 _mesa_BeginQueryARB(GLenum target
, GLuint id
)
180 GET_CURRENT_CONTEXT(ctx
);
181 struct occlusion_query
*q
;
182 ASSERT_OUTSIDE_BEGIN_END(ctx
);
184 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
186 if (target
!= GL_SAMPLES_PASSED_ARB
) {
187 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginQueryARB(target)");
191 if (ctx
->Occlusion
.CurrentQueryObject
) {
192 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB(target)");
196 q
= (struct occlusion_query
*)
197 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
198 if (q
&& q
->Active
) {
199 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginQueryARB");
203 q
= new_query_object(target
, id
);
205 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQueryARB");
208 _mesa_HashInsert(ctx
->Occlusion
.QueryObjects
, id
, q
);
212 q
->PassedCounter
= 0;
213 ctx
->Occlusion
.Active
= GL_TRUE
;
214 ctx
->Occlusion
.CurrentQueryObject
= id
;
215 ctx
->Occlusion
.PassedCounter
= 0;
220 _mesa_EndQueryARB(GLenum target
)
222 GET_CURRENT_CONTEXT(ctx
);
223 struct occlusion_query
*q
= NULL
;
224 ASSERT_OUTSIDE_BEGIN_END(ctx
);
226 FLUSH_VERTICES(ctx
, _NEW_DEPTH
);
228 if (target
!= GL_SAMPLES_PASSED_ARB
) {
229 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEndQueryARB(target)");
233 if (ctx
->Occlusion
.CurrentQueryObject
)
234 q
= (struct occlusion_query
*)
235 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
,
236 ctx
->Occlusion
.CurrentQueryObject
);
237 if (!q
|| !q
->Active
) {
238 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glEndQuery with no glBeginQuery");
242 q
->PassedCounter
= ctx
->Occlusion
.PassedCounter
;
243 q
->Active
= GL_FALSE
;
244 ctx
->Occlusion
.Active
= GL_FALSE
;
245 ctx
->Occlusion
.CurrentQueryObject
= 0;
250 _mesa_GetQueryivARB(GLenum target
, GLenum pname
, GLint
*params
)
252 GET_CURRENT_CONTEXT(ctx
);
253 ASSERT_OUTSIDE_BEGIN_END(ctx
);
255 if (target
!= GL_SAMPLES_PASSED_ARB
) {
256 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(target)");
261 case GL_QUERY_COUNTER_BITS_ARB
:
262 *params
= 8 * sizeof(GLuint
);
264 case GL_CURRENT_QUERY_ARB
:
265 *params
= ctx
->Occlusion
.CurrentQueryObject
;
268 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryivARB(pname)");
275 _mesa_GetQueryObjectivARB(GLuint id
, GLenum pname
, GLint
*params
)
277 GET_CURRENT_CONTEXT(ctx
);
278 struct occlusion_query
*q
= NULL
;
279 ASSERT_OUTSIDE_BEGIN_END(ctx
);
282 q
= (struct occlusion_query
*)
283 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
285 if (!q
|| q
->Active
) {
286 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetQueryObjectivARB(id=%d)", id
);
291 case GL_QUERY_RESULT_ARB
:
292 *params
= q
->PassedCounter
;
294 case GL_QUERY_RESULT_AVAILABLE_ARB
:
295 /* XXX revisit when we have a hardware implementation! */
299 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectivARB(pname)");
306 _mesa_GetQueryObjectuivARB(GLuint id
, GLenum pname
, GLuint
*params
)
308 GET_CURRENT_CONTEXT(ctx
);
309 struct occlusion_query
*q
= NULL
;
310 ASSERT_OUTSIDE_BEGIN_END(ctx
);
313 q
= (struct occlusion_query
*)
314 _mesa_HashLookup(ctx
->Occlusion
.QueryObjects
, id
);
315 if (!q
|| q
->Active
) {
316 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetQueryObjectuivARB(id=%d", id
);
321 case GL_QUERY_RESULT_ARB
:
322 *params
= q
->PassedCounter
;
324 case GL_QUERY_RESULT_AVAILABLE_ARB
:
325 /* XXX revisit when we have a hardware implementation! */
329 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetQueryObjectuivARB(pname)");