replace color table FloatTable boolean with Type enum
[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 = NULL;
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 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");
239 return;
240 }
241
242 q->PassedCounter = ctx->Occlusion.PassedCounter;
243 q->Active = GL_FALSE;
244 ctx->Occlusion.Active = GL_FALSE;
245 ctx->Occlusion.CurrentQueryObject = 0;
246 }
247
248
249 void GLAPIENTRY
250 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
251 {
252 GET_CURRENT_CONTEXT(ctx);
253 ASSERT_OUTSIDE_BEGIN_END(ctx);
254
255 if (target != GL_SAMPLES_PASSED_ARB) {
256 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(target)");
257 return;
258 }
259
260 switch (pname) {
261 case GL_QUERY_COUNTER_BITS_ARB:
262 *params = 8 * sizeof(GLuint);
263 break;
264 case GL_CURRENT_QUERY_ARB:
265 *params = ctx->Occlusion.CurrentQueryObject;
266 break;
267 default:
268 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(pname)");
269 return;
270 }
271 }
272
273
274 void GLAPIENTRY
275 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
276 {
277 GET_CURRENT_CONTEXT(ctx);
278 struct occlusion_query *q = NULL;
279 ASSERT_OUTSIDE_BEGIN_END(ctx);
280
281 if (id)
282 q = (struct occlusion_query *)
283 _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
284
285 if (!q || q->Active) {
286 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectivARB(id=%d)", id);
287 return;
288 }
289
290 switch (pname) {
291 case GL_QUERY_RESULT_ARB:
292 *params = q->PassedCounter;
293 break;
294 case GL_QUERY_RESULT_AVAILABLE_ARB:
295 /* XXX revisit when we have a hardware implementation! */
296 *params = GL_TRUE;
297 break;
298 default:
299 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
300 return;
301 }
302 }
303
304
305 void GLAPIENTRY
306 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
307 {
308 GET_CURRENT_CONTEXT(ctx);
309 struct occlusion_query *q = NULL;
310 ASSERT_OUTSIDE_BEGIN_END(ctx);
311
312 if (id)
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);
317 return;
318 }
319
320 switch (pname) {
321 case GL_QUERY_RESULT_ARB:
322 *params = q->PassedCounter;
323 break;
324 case GL_QUERY_RESULT_AVAILABLE_ARB:
325 /* XXX revisit when we have a hardware implementation! */
326 *params = GL_TRUE;
327 break;
328 default:
329 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
330 return;
331 }
332 }