check the Ready flag when getting GL_QUERY_RESULT_ARB
[mesa.git] / src / mesa / main / occlude.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 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 /**
40 * Allocate a new occlusion query object.
41 * \param target - must be GL_SAMPLES_PASSED_ARB at this time
42 * \param id - the object's ID
43 * \return pointer to new query_object object or NULL if out of memory.
44 */
45 static struct gl_query_object *
46 new_query_object(GLenum target, GLuint id)
47 {
48 struct gl_query_object *q = MALLOC_STRUCT(gl_query_object);
49 if (q) {
50 q->Target = target;
51 q->Id = id;
52 q->Result = 0;
53 q->Active = GL_FALSE;
54 q->Ready = GL_TRUE; /* correct, see spec */
55 }
56 return q;
57 }
58
59
60 /**
61 * Delete an occlusion query object.
62 * Not removed from hash table here.
63 */
64 static void
65 delete_query_object(struct gl_query_object *q)
66 {
67 FREE(q);
68 }
69
70
71 struct gl_query_object *
72 lookup_query_object(GLcontext *ctx, GLuint id)
73 {
74 return (struct gl_query_object *)
75 _mesa_HashLookup(ctx->Query.QueryObjects, id);
76 }
77
78
79
80 void GLAPIENTRY
81 _mesa_GenQueriesARB(GLsizei n, GLuint *ids)
82 {
83 GET_CURRENT_CONTEXT(ctx);
84 GLuint first;
85 ASSERT_OUTSIDE_BEGIN_END(ctx);
86
87 if (n < 0) {
88 _mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
89 return;
90 }
91
92 /* No query objects can be active at this time! */
93 if (ctx->Query.CurrentOcclusionObject) {
94 _mesa_error(ctx, GL_INVALID_OPERATION, "glGenQueriesARB");
95 return;
96 }
97
98 first = _mesa_HashFindFreeKeyBlock(ctx->Query.QueryObjects, n);
99 if (first) {
100 GLsizei i;
101 for (i = 0; i < n; i++) {
102 struct gl_query_object *q = new_query_object(GL_SAMPLES_PASSED_ARB,
103 first + i);
104 if (!q) {
105 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
106 return;
107 }
108 ids[i] = first + i;
109 _mesa_HashInsert(ctx->Query.QueryObjects, first + i, q);
110 }
111 }
112 }
113
114
115 void GLAPIENTRY
116 _mesa_DeleteQueriesARB(GLsizei n, const GLuint *ids)
117 {
118 GET_CURRENT_CONTEXT(ctx);
119 GLint i;
120 ASSERT_OUTSIDE_BEGIN_END(ctx);
121
122 if (n < 0) {
123 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
124 return;
125 }
126
127 /* No query objects can be active at this time! */
128 if (ctx->Query.CurrentOcclusionObject) {
129 _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
130 return;
131 }
132
133 for (i = 0; i < n; i++) {
134 if (ids[i] > 0) {
135 struct gl_query_object *q = lookup_query_object(ctx, ids[i]);
136 if (q) {
137 ASSERT(!q->Active); /* should be caught earlier */
138 _mesa_HashRemove(ctx->Query.QueryObjects, ids[i]);
139 delete_query_object(q);
140 }
141 }
142 }
143 }
144
145
146 GLboolean GLAPIENTRY
147 _mesa_IsQueryARB(GLuint id)
148 {
149 GET_CURRENT_CONTEXT(ctx);
150 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
151
152 if (id && lookup_query_object(ctx, id))
153 return GL_TRUE;
154 else
155 return GL_FALSE;
156 }
157
158
159 void GLAPIENTRY
160 _mesa_BeginQueryARB(GLenum target, GLuint id)
161 {
162 GET_CURRENT_CONTEXT(ctx);
163 struct gl_query_object *q;
164 ASSERT_OUTSIDE_BEGIN_END(ctx);
165
166 FLUSH_VERTICES(ctx, _NEW_DEPTH);
167
168 if (target != GL_SAMPLES_PASSED_ARB) {
169 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
170 return;
171 }
172
173 if (id == 0) {
174 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(id==0)");
175 return;
176 }
177
178 if (ctx->Query.CurrentOcclusionObject) {
179 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(target)");
180 return;
181 }
182
183 q = lookup_query_object(ctx, id);
184 if (!q) {
185 /* create new object */
186 q = new_query_object(target, id);
187 if (!q) {
188 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQueryARB");
189 return;
190 }
191 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
192 }
193 else {
194 /* pre-existing object */
195 if (q->Target != target) {
196 _mesa_error(ctx, GL_INVALID_OPERATION,
197 "glBeginQueryARB(target mismatch)");
198 return;
199 }
200 if (q->Active) {
201 _mesa_error(ctx, GL_INVALID_OPERATION,
202 "glBeginQueryARB(query already active)");
203 return;
204 }
205 }
206
207 q->Active = GL_TRUE;
208 q->Result = 0;
209 q->Ready = GL_FALSE;
210 ctx->Query.CurrentOcclusionObject = q;
211
212 if (ctx->Driver.BeginQuery) {
213 ctx->Driver.BeginQuery(ctx, q);
214 }
215 }
216
217
218 void GLAPIENTRY
219 _mesa_EndQueryARB(GLenum target)
220 {
221 GET_CURRENT_CONTEXT(ctx);
222 struct gl_query_object *q;
223 ASSERT_OUTSIDE_BEGIN_END(ctx);
224
225 FLUSH_VERTICES(ctx, _NEW_DEPTH);
226
227 switch (target) {
228 case GL_SAMPLES_PASSED_ARB:
229 q = ctx->Query.CurrentOcclusionObject;
230 ctx->Query.CurrentOcclusionObject = NULL;
231 break;
232 default:
233 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
234 return;
235 }
236
237 if (!q || !q->Active) {
238 _mesa_error(ctx, GL_INVALID_OPERATION,
239 "glEndQueryARB(no matching glBeginQueryARB)");
240 return;
241 }
242
243 q->Active = GL_FALSE;
244 if (ctx->Driver.EndQuery) {
245 ctx->Driver.EndQuery(ctx, q);
246 }
247 else {
248 q->Ready = GL_TRUE;
249 }
250 }
251
252
253 void GLAPIENTRY
254 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
255 {
256 GET_CURRENT_CONTEXT(ctx);
257 struct gl_query_object *q;
258 ASSERT_OUTSIDE_BEGIN_END(ctx);
259
260 switch (target) {
261 case GL_SAMPLES_PASSED_ARB:
262 q = ctx->Query.CurrentOcclusionObject;
263 break;
264 default:
265 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(target)");
266 return;
267 }
268
269 switch (pname) {
270 case GL_QUERY_COUNTER_BITS_ARB:
271 *params = 8 * sizeof(q->Result);
272 break;
273 case GL_CURRENT_QUERY_ARB:
274 *params = q ? q->Id : 0;
275 break;
276 default:
277 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(pname)");
278 return;
279 }
280 }
281
282
283 void GLAPIENTRY
284 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
285 {
286 GET_CURRENT_CONTEXT(ctx);
287 struct gl_query_object *q = NULL;
288 ASSERT_OUTSIDE_BEGIN_END(ctx);
289
290 if (id)
291 q = lookup_query_object(ctx, id);
292
293 if (!q || q->Active) {
294 _mesa_error(ctx, GL_INVALID_OPERATION,
295 "glGetQueryObjectivARB(id=%d is active)", id);
296 return;
297 }
298
299 switch (pname) {
300 case GL_QUERY_RESULT_ARB:
301 while (!q->Ready) {
302 /* Wait for the query to finish! */
303 /* If using software rendering, the result will always be ready
304 * by time we get here. Otherwise, we must be using hardware!
305 */
306 ASSERT(ctx->Driver.EndQuery);
307 }
308 *params = q->Result;
309 break;
310 case GL_QUERY_RESULT_AVAILABLE_ARB:
311 /* XXX revisit when we have a hardware implementation! */
312 *params = q->Ready;
313 break;
314 default:
315 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
316 return;
317 }
318 }
319
320
321 void GLAPIENTRY
322 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
323 {
324 GET_CURRENT_CONTEXT(ctx);
325 struct gl_query_object *q = NULL;
326 ASSERT_OUTSIDE_BEGIN_END(ctx);
327
328 if (id)
329 q = lookup_query_object(ctx, id);
330
331 if (!q || q->Active) {
332 _mesa_error(ctx, GL_INVALID_OPERATION,
333 "glGetQueryObjectuivARB(id=%d is active)", id);
334 return;
335 }
336
337 switch (pname) {
338 case GL_QUERY_RESULT_ARB:
339 while (!q->Ready) {
340 /* Wait for the query to finish! */
341 /* If using software rendering, the result will always be ready
342 * by time we get here. Otherwise, we must be using hardware!
343 */
344 ASSERT(ctx->Driver.EndQuery);
345 }
346 *params = q->Result;
347 break;
348 case GL_QUERY_RESULT_AVAILABLE_ARB:
349 /* XXX revisit when we have a hardware implementation! */
350 *params = q->Ready;
351 break;
352 default:
353 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
354 return;
355 }
356 }
357
358
359
360 /**
361 * Allocate/init the context state related to occlusion query objects.
362 */
363 void
364 _mesa_init_occlude(GLcontext *ctx)
365 {
366 #if FEATURE_ARB_occlusion_query
367 ctx->Query.QueryObjects = _mesa_NewHashTable();
368 ctx->Query.CurrentOcclusionObject = NULL;
369 #endif
370 }
371
372
373 /**
374 * Free the context state related to occlusion query objects.
375 */
376 void
377 _mesa_free_occlude_data(GLcontext *ctx)
378 {
379 while (1) {
380 GLuint id = _mesa_HashFirstEntry(ctx->Query.QueryObjects);
381 if (id) {
382 struct gl_query_object *q = lookup_query_object(ctx, id);
383 ASSERT(q);
384 delete_query_object(q);
385 _mesa_HashRemove(ctx->Query.QueryObjects, id);
386 }
387 else {
388 break;
389 }
390 }
391 _mesa_DeleteHashTable(ctx->Query.QueryObjects);
392 }