added a cast
[mesa.git] / src / mesa / main / occlude.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /*
27 * Functions to implement the GL_ARB_occlusion_query extension.
28 */
29
30
31 #include "glheader.h"
32 #include "context.h"
33 #include "hash.h"
34 #include "imports.h"
35 #include "occlude.h"
36 #include "mtypes.h"
37
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
44 #endif
45
46
47 struct occlusion_query
48 {
49 GLenum Target;
50 GLuint Id;
51 GLuint PassedCounter;
52 GLboolean Active;
53 };
54
55
56
57 void
58 _mesa_init_occlude(GLcontext *ctx)
59 {
60 #if FEATURE_ARB_occlusion_query
61 ctx->Occlusion.QueryObjects = _mesa_NewHashTable();
62 #endif
63
64 ctx->OcclusionResult = GL_FALSE;
65 ctx->OcclusionResultSaved = GL_FALSE;
66 }
67
68
69
70 /**
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.
75 */
76 static struct occlusion_query *
77 new_query_object(GLenum target, GLuint id)
78 {
79 struct occlusion_query *q = MALLOC_STRUCT(occlusion_query);
80 if (q) {
81 q->Target = target;
82 q->Id = id;
83 q->PassedCounter = 0;
84 q->Active = GL_FALSE;
85 }
86 return q;
87 }
88
89
90 /**
91 * Delete an occlusion query object.
92 */
93 static void
94 delete_query_object(struct occlusion_query *q)
95 {
96 FREE(q);
97 }
98
99
100 void GLAPIENTRY
101 _mesa_GenQueriesARB(GLsizei n, GLuint *ids)
102 {
103 GET_CURRENT_CONTEXT(ctx);
104 GLuint first;
105 ASSERT_OUTSIDE_BEGIN_END(ctx);
106
107 if (n < 0) {
108 _mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
109 return;
110 }
111
112 if (ctx->Occlusion.Active) {
113 _mesa_error(ctx, GL_INVALID_OPERATION, "glGenQueriesARB");
114 return;
115 }
116
117 first = _mesa_HashFindFreeKeyBlock(ctx->Occlusion.QueryObjects, n);
118 if (first) {
119 GLsizei i;
120 for (i = 0; i < n; i++) {
121 struct occlusion_query *q = new_query_object(GL_SAMPLES_PASSED_ARB,
122 first + i);
123 if (!q) {
124 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
125 return;
126 }
127 ids[i] = first + i;
128 _mesa_HashInsert(ctx->Occlusion.QueryObjects, first + i, q);
129 }
130 }
131 }
132
133
134 void GLAPIENTRY
135 _mesa_DeleteQueriesARB(GLsizei n, const GLuint *ids)
136 {
137 GET_CURRENT_CONTEXT(ctx);
138 GLint i;
139 ASSERT_OUTSIDE_BEGIN_END(ctx);
140
141 if (n < 0) {
142 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
143 return;
144 }
145
146 if (ctx->Occlusion.Active) {
147 _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
148 return;
149 }
150
151 for (i = 0; i < n; i++) {
152 if (ids[i] > 0) {
153 struct occlusion_query *q = (struct occlusion_query *)
154 _mesa_HashLookup(ctx->Occlusion.QueryObjects, ids[i]);
155 if (q) {
156 _mesa_HashRemove(ctx->Occlusion.QueryObjects, ids[i]);
157 delete_query_object(q);
158 }
159 }
160 }
161 }
162
163
164 GLboolean GLAPIENTRY
165 _mesa_IsQueryARB(GLuint id)
166 {
167 GET_CURRENT_CONTEXT(ctx);
168 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
169
170 if (id && _mesa_HashLookup(ctx->Occlusion.QueryObjects, id))
171 return GL_TRUE;
172 else
173 return GL_FALSE;
174 }
175
176
177 void GLAPIENTRY
178 _mesa_BeginQueryARB(GLenum target, GLuint id)
179 {
180 GET_CURRENT_CONTEXT(ctx);
181 struct occlusion_query *q;
182 ASSERT_OUTSIDE_BEGIN_END(ctx);
183
184 FLUSH_VERTICES(ctx, _NEW_DEPTH);
185
186 if (target != GL_SAMPLES_PASSED_ARB) {
187 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
188 return;
189 }
190
191 if (ctx->Occlusion.CurrentQueryObject) {
192 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(target)");
193 return;
194 }
195
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");
200 return;
201 }
202 else if (!q) {
203 q = new_query_object(target, id);
204 if (!q) {
205 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQueryARB");
206 return;
207 }
208 _mesa_HashInsert(ctx->Occlusion.QueryObjects, id, q);
209 }
210
211 q->Active = GL_TRUE;
212 q->PassedCounter = 0;
213 ctx->Occlusion.Active = GL_TRUE;
214 ctx->Occlusion.CurrentQueryObject = id;
215 ctx->Occlusion.PassedCounter = 0;
216 }
217
218
219 void GLAPIENTRY
220 _mesa_EndQueryARB(GLenum target)
221 {
222 GET_CURRENT_CONTEXT(ctx);
223 struct occlusion_query *q;
224 ASSERT_OUTSIDE_BEGIN_END(ctx);
225
226 FLUSH_VERTICES(ctx, _NEW_DEPTH);
227
228 if (target != GL_SAMPLES_PASSED_ARB) {
229 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
230 return;
231 }
232
233 q = (struct occlusion_query *)
234 _mesa_HashLookup(ctx->Occlusion.QueryObjects,
235 ctx->Occlusion.CurrentQueryObject);
236 if (!q || !q->Active) {
237 _mesa_problem(ctx, "bad query object in glEndQueryARB");
238 return;
239 }
240
241 q->PassedCounter = ctx->Occlusion.PassedCounter;
242 q->Active = GL_FALSE;
243 ctx->Occlusion.Active = GL_FALSE;
244 ctx->Occlusion.CurrentQueryObject = 0;
245 }
246
247
248 void GLAPIENTRY
249 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
250 {
251 GET_CURRENT_CONTEXT(ctx);
252 ASSERT_OUTSIDE_BEGIN_END(ctx);
253
254 if (target != GL_SAMPLES_PASSED_ARB) {
255 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(target)");
256 return;
257 }
258
259 switch (pname) {
260 case GL_QUERY_COUNTER_BITS_ARB:
261 *params = 8 * sizeof(GLuint);
262 break;
263 case GL_CURRENT_QUERY_ARB:
264 *params = ctx->Occlusion.CurrentQueryObject;
265 break;
266 default:
267 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(pname)");
268 return;
269 }
270 }
271
272
273 void GLAPIENTRY
274 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
275 {
276 GET_CURRENT_CONTEXT(ctx);
277 struct occlusion_query *q;
278 ASSERT_OUTSIDE_BEGIN_END(ctx);
279
280 q = (struct occlusion_query *)
281 _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
282 if (!q || q->Active) {
283 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectivARB");
284 return;
285 }
286
287 switch (pname) {
288 case GL_QUERY_RESULT_ARB:
289 *params = q->PassedCounter;
290 break;
291 case GL_QUERY_RESULT_AVAILABLE_ARB:
292 /* XXX revisit when we have a hardware implementation! */
293 *params = GL_TRUE;
294 break;
295 default:
296 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
297 return;
298 }
299 }
300
301
302 void GLAPIENTRY
303 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
304 {
305 GET_CURRENT_CONTEXT(ctx);
306 struct occlusion_query *q;
307 ASSERT_OUTSIDE_BEGIN_END(ctx);
308
309 q = (struct occlusion_query *)
310 _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
311 if (!q || q->Active) {
312 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectuivARB");
313 return;
314 }
315
316 switch (pname) {
317 case GL_QUERY_RESULT_ARB:
318 *params = q->PassedCounter;
319 break;
320 case GL_QUERY_RESULT_AVAILABLE_ARB:
321 /* XXX revisit when we have a hardware implementation! */
322 *params = GL_TRUE;
323 break;
324 default:
325 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
326 return;
327 }
328 }