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