added missing error check in _mesa_BeginQueryARB (Cedric Gautier). minor clean-ups.
[mesa.git] / src / mesa / main / occlude.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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
39 struct occlusion_query
40 {
41 GLenum Target;
42 GLuint Id;
43 GLuint PassedCounter;
44 GLboolean Active;
45 };
46
47
48
49 void
50 _mesa_init_occlude(GLcontext *ctx)
51 {
52 #if FEATURE_ARB_occlusion_query
53 ctx->Occlusion.QueryObjects = _mesa_NewHashTable();
54 #endif
55
56 ctx->OcclusionResult = GL_FALSE;
57 ctx->OcclusionResultSaved = GL_FALSE;
58 }
59
60
61
62 /**
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.
67 */
68 static struct occlusion_query *
69 new_query_object(GLenum target, GLuint id)
70 {
71 struct occlusion_query *q = MALLOC_STRUCT(occlusion_query);
72 if (q) {
73 q->Target = target;
74 q->Id = id;
75 q->PassedCounter = 0;
76 q->Active = GL_FALSE;
77 }
78 return q;
79 }
80
81
82 /**
83 * Delete an occlusion query object.
84 */
85 static void
86 delete_query_object(struct occlusion_query *q)
87 {
88 FREE(q);
89 }
90
91
92 void GLAPIENTRY
93 _mesa_GenQueriesARB(GLsizei n, GLuint *ids)
94 {
95 GET_CURRENT_CONTEXT(ctx);
96 GLuint first;
97 ASSERT_OUTSIDE_BEGIN_END(ctx);
98
99 if (n < 0) {
100 _mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
101 return;
102 }
103
104 if (ctx->Occlusion.Active) {
105 _mesa_error(ctx, GL_INVALID_OPERATION, "glGenQueriesARB");
106 return;
107 }
108
109 first = _mesa_HashFindFreeKeyBlock(ctx->Occlusion.QueryObjects, n);
110 if (first) {
111 GLsizei i;
112 for (i = 0; i < n; i++) {
113 struct occlusion_query *q = new_query_object(GL_SAMPLES_PASSED_ARB,
114 first + i);
115 if (!q) {
116 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
117 return;
118 }
119 ids[i] = first + i;
120 _mesa_HashInsert(ctx->Occlusion.QueryObjects, first + i, q);
121 }
122 }
123 }
124
125
126 void GLAPIENTRY
127 _mesa_DeleteQueriesARB(GLsizei n, const GLuint *ids)
128 {
129 GET_CURRENT_CONTEXT(ctx);
130 GLint i;
131 ASSERT_OUTSIDE_BEGIN_END(ctx);
132
133 if (n < 0) {
134 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
135 return;
136 }
137
138 if (ctx->Occlusion.Active) {
139 _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
140 return;
141 }
142
143 for (i = 0; i < n; i++) {
144 if (ids[i] > 0) {
145 struct occlusion_query *q = (struct occlusion_query *)
146 _mesa_HashLookup(ctx->Occlusion.QueryObjects, ids[i]);
147 if (q) {
148 _mesa_HashRemove(ctx->Occlusion.QueryObjects, ids[i]);
149 delete_query_object(q);
150 }
151 }
152 }
153 }
154
155
156 GLboolean GLAPIENTRY
157 _mesa_IsQueryARB(GLuint id)
158 {
159 GET_CURRENT_CONTEXT(ctx);
160 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
161
162 if (id && _mesa_HashLookup(ctx->Occlusion.QueryObjects, id))
163 return GL_TRUE;
164 else
165 return GL_FALSE;
166 }
167
168
169 void GLAPIENTRY
170 _mesa_BeginQueryARB(GLenum target, GLuint id)
171 {
172 GET_CURRENT_CONTEXT(ctx);
173 struct occlusion_query *q;
174 ASSERT_OUTSIDE_BEGIN_END(ctx);
175
176 FLUSH_VERTICES(ctx, _NEW_DEPTH);
177
178 if (target != GL_SAMPLES_PASSED_ARB) {
179 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
180 return;
181 }
182
183 if (id == 0) {
184 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(id==0)");
185 return;
186 }
187
188 if (ctx->Occlusion.CurrentQueryObject) {
189 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(target)");
190 return;
191 }
192
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");
197 return;
198 }
199 else if (!q) {
200 q = new_query_object(target, id);
201 if (!q) {
202 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQueryARB");
203 return;
204 }
205 _mesa_HashInsert(ctx->Occlusion.QueryObjects, id, q);
206 }
207
208 q->Active = GL_TRUE;
209 q->PassedCounter = 0;
210 ctx->Occlusion.Active = GL_TRUE;
211 ctx->Occlusion.CurrentQueryObject = id;
212 ctx->Occlusion.PassedCounter = 0;
213 }
214
215
216 void GLAPIENTRY
217 _mesa_EndQueryARB(GLenum target)
218 {
219 GET_CURRENT_CONTEXT(ctx);
220 struct occlusion_query *q = NULL;
221 ASSERT_OUTSIDE_BEGIN_END(ctx);
222
223 FLUSH_VERTICES(ctx, _NEW_DEPTH);
224
225 if (target != GL_SAMPLES_PASSED_ARB) {
226 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
227 return;
228 }
229
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");
236 return;
237 }
238
239 q->PassedCounter = ctx->Occlusion.PassedCounter;
240 q->Active = GL_FALSE;
241 ctx->Occlusion.Active = GL_FALSE;
242 ctx->Occlusion.CurrentQueryObject = 0;
243 }
244
245
246 void GLAPIENTRY
247 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
248 {
249 GET_CURRENT_CONTEXT(ctx);
250 ASSERT_OUTSIDE_BEGIN_END(ctx);
251
252 if (target != GL_SAMPLES_PASSED_ARB) {
253 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(target)");
254 return;
255 }
256
257 switch (pname) {
258 case GL_QUERY_COUNTER_BITS_ARB:
259 *params = 8 * sizeof(GLuint);
260 break;
261 case GL_CURRENT_QUERY_ARB:
262 *params = ctx->Occlusion.CurrentQueryObject;
263 break;
264 default:
265 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(pname)");
266 return;
267 }
268 }
269
270
271 void GLAPIENTRY
272 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
273 {
274 GET_CURRENT_CONTEXT(ctx);
275 struct occlusion_query *q = NULL;
276 ASSERT_OUTSIDE_BEGIN_END(ctx);
277
278 if (id)
279 q = (struct occlusion_query *)
280 _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
281
282 if (!q || q->Active) {
283 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectivARB(id=%d)", id);
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 = NULL;
307 ASSERT_OUTSIDE_BEGIN_END(ctx);
308
309 if (id)
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);
314 return;
315 }
316
317 switch (pname) {
318 case GL_QUERY_RESULT_ARB:
319 *params = q->PassedCounter;
320 break;
321 case GL_QUERY_RESULT_AVAILABLE_ARB:
322 /* XXX revisit when we have a hardware implementation! */
323 *params = GL_TRUE;
324 break;
325 default:
326 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
327 return;
328 }
329 }