Updates for GL_EXT_timer_query:
[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 static 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 invalid or 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 /* if result is too large for returned type, clamp to max value */
348 if (q->Result > 0x7fffffff) {
349 *params = 0x7fffffff;
350 }
351 else {
352 *params = q->Result;
353 }
354 break;
355 case GL_QUERY_RESULT_AVAILABLE_ARB:
356 /* XXX revisit when we have a hardware implementation! */
357 *params = q->Ready;
358 break;
359 default:
360 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
361 return;
362 }
363 }
364
365
366 void GLAPIENTRY
367 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
368 {
369 struct gl_query_object *q = NULL;
370 GET_CURRENT_CONTEXT(ctx);
371 ASSERT_OUTSIDE_BEGIN_END(ctx);
372
373 if (id)
374 q = lookup_query_object(ctx, id);
375
376 if (!q || q->Active) {
377 _mesa_error(ctx, GL_INVALID_OPERATION,
378 "glGetQueryObjectuivARB(id=%d is invalid or active)", id);
379 return;
380 }
381
382 switch (pname) {
383 case GL_QUERY_RESULT_ARB:
384 while (!q->Ready) {
385 /* Wait for the query to finish! */
386 /* If using software rendering, the result will always be ready
387 * by time we get here. Otherwise, we must be using hardware!
388 */
389 ASSERT(ctx->Driver.EndQuery);
390 }
391 /* if result is too large for returned type, clamp to max value */
392 if (q->Result > 0xffffffff) {
393 *params = 0xffffffff;
394 }
395 else {
396 *params = q->Result;
397 }
398 break;
399 case GL_QUERY_RESULT_AVAILABLE_ARB:
400 /* XXX revisit when we have a hardware implementation! */
401 *params = q->Ready;
402 break;
403 default:
404 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
405 return;
406 }
407 }
408
409
410 /**
411 * New with GL_EXT_timer_query
412 */
413 void GLAPIENTRY
414 _mesa_GetQueryObjecti64vEXT(GLuint id, GLenum pname, GLint64EXT *params)
415 {
416 struct gl_query_object *q = NULL;
417 GET_CURRENT_CONTEXT(ctx);
418 ASSERT_OUTSIDE_BEGIN_END(ctx);
419
420 if (id)
421 q = lookup_query_object(ctx, id);
422
423 if (!q || q->Active) {
424 _mesa_error(ctx, GL_INVALID_OPERATION,
425 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id);
426 return;
427 }
428
429 switch (pname) {
430 case GL_QUERY_RESULT_ARB:
431 while (!q->Ready) {
432 /* Wait for the query to finish! */
433 /* If using software rendering, the result will always be ready
434 * by time we get here. Otherwise, we must be using hardware!
435 */
436 ASSERT(ctx->Driver.EndQuery);
437 }
438 *params = q->Result;
439 break;
440 case GL_QUERY_RESULT_AVAILABLE_ARB:
441 /* XXX revisit when we have a hardware implementation! */
442 *params = q->Ready;
443 break;
444 default:
445 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjecti64vARB(pname)");
446 return;
447 }
448 }
449
450
451 /**
452 * New with GL_EXT_timer_query
453 */
454 void GLAPIENTRY
455 _mesa_GetQueryObjectui64vEXT(GLuint id, GLenum pname, GLuint64EXT *params)
456 {
457 struct gl_query_object *q = NULL;
458 GET_CURRENT_CONTEXT(ctx);
459 ASSERT_OUTSIDE_BEGIN_END(ctx);
460
461 if (id)
462 q = lookup_query_object(ctx, id);
463
464 if (!q || q->Active) {
465 _mesa_error(ctx, GL_INVALID_OPERATION,
466 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id);
467 return;
468 }
469
470 switch (pname) {
471 case GL_QUERY_RESULT_ARB:
472 while (!q->Ready) {
473 /* Wait for the query to finish! */
474 /* If using software rendering, the result will always be ready
475 * by time we get here. Otherwise, we must be using hardware!
476 */
477 ASSERT(ctx->Driver.EndQuery);
478 }
479 *params = q->Result;
480 break;
481 case GL_QUERY_RESULT_AVAILABLE_ARB:
482 /* XXX revisit when we have a hardware implementation! */
483 *params = q->Ready;
484 break;
485 default:
486 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectui64vARB(pname)");
487 return;
488 }
489 }
490
491
492 /**
493 * Allocate/init the context state related to query objects.
494 */
495 void
496 _mesa_init_query(GLcontext *ctx)
497 {
498 #if FEATURE_ARB_occlusion_query
499 ctx->Query.QueryObjects = _mesa_NewHashTable();
500 ctx->Query.CurrentOcclusionObject = NULL;
501 #endif
502 }
503
504
505 /**
506 * Free the context state related to query objects.
507 */
508 void
509 _mesa_free_query_data(GLcontext *ctx)
510 {
511 while (1) {
512 GLuint id = _mesa_HashFirstEntry(ctx->Query.QueryObjects);
513 if (id) {
514 struct gl_query_object *q = lookup_query_object(ctx, id);
515 ASSERT(q);
516 delete_query_object(q);
517 _mesa_HashRemove(ctx->Query.QueryObjects, id);
518 }
519 else {
520 break;
521 }
522 }
523 _mesa_DeleteHashTable(ctx->Query.QueryObjects);
524 }