Merge branch 'draw-instanced'
[mesa.git] / src / mesa / main / queryobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 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 "queryobj.h"
31 #include "mfeatures.h"
32 #include "mtypes.h"
33 #include "main/dispatch.h"
34
35
36 #if FEATURE_queryobj
37
38
39 /**
40 * Allocate a new query object. This is a fallback routine called via
41 * ctx->Driver.NewQueryObject().
42 * \param ctx - rendering context
43 * \param id - the new object's ID
44 * \return pointer to new query_object object or NULL if out of memory.
45 */
46 static struct gl_query_object *
47 _mesa_new_query_object(struct gl_context *ctx, GLuint id)
48 {
49 struct gl_query_object *q = MALLOC_STRUCT(gl_query_object);
50 (void) ctx;
51 if (q) {
52 q->Id = id;
53 q->Result = 0;
54 q->Active = GL_FALSE;
55 q->Ready = GL_TRUE; /* correct, see spec */
56 }
57 return q;
58 }
59
60
61 /**
62 * Begin a query. Software driver fallback.
63 * Called via ctx->Driver.BeginQuery().
64 */
65 static void
66 _mesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
67 {
68 /* no-op */
69 }
70
71
72 /**
73 * End a query. Software driver fallback.
74 * Called via ctx->Driver.EndQuery().
75 */
76 static void
77 _mesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
78 {
79 q->Ready = GL_TRUE;
80 }
81
82
83 /**
84 * Wait for query to complete. Software driver fallback.
85 * Called via ctx->Driver.WaitQuery().
86 */
87 static void
88 _mesa_wait_query(struct gl_context *ctx, struct gl_query_object *q)
89 {
90 /* For software drivers, _mesa_end_query() should have completed the query.
91 * For real hardware, implement a proper WaitQuery() driver function,
92 * which may require issuing a flush.
93 */
94 assert(q->Ready);
95 }
96
97
98 /**
99 * Check if a query results are ready. Software driver fallback.
100 * Called via ctx->Driver.CheckQuery().
101 */
102 static void
103 _mesa_check_query(struct gl_context *ctx, struct gl_query_object *q)
104 {
105 /* No-op for sw rendering.
106 * HW drivers may need to flush at this time.
107 */
108 }
109
110
111 /**
112 * Delete a query object. Called via ctx->Driver.DeleteQuery().
113 * Not removed from hash table here.
114 */
115 static void
116 _mesa_delete_query(struct gl_context *ctx, struct gl_query_object *q)
117 {
118 free(q);
119 }
120
121
122 void
123 _mesa_init_query_object_functions(struct dd_function_table *driver)
124 {
125 driver->NewQueryObject = _mesa_new_query_object;
126 driver->DeleteQuery = _mesa_delete_query;
127 driver->BeginQuery = _mesa_begin_query;
128 driver->EndQuery = _mesa_end_query;
129 driver->WaitQuery = _mesa_wait_query;
130 driver->CheckQuery = _mesa_check_query;
131 }
132
133
134 /**
135 * Return pointer to the query object binding point for the given target.
136 * \return NULL if invalid target, else the address of binding point
137 */
138 static struct gl_query_object **
139 get_query_binding_point(struct gl_context *ctx, GLenum target)
140 {
141 switch (target) {
142 case GL_SAMPLES_PASSED_ARB:
143 if (ctx->Extensions.ARB_occlusion_query)
144 return &ctx->Query.CurrentOcclusionObject;
145 else
146 return NULL;
147 case GL_ANY_SAMPLES_PASSED:
148 if (ctx->Extensions.ARB_occlusion_query2)
149 return &ctx->Query.CurrentOcclusionObject;
150 else
151 return NULL;
152 case GL_TIME_ELAPSED_EXT:
153 if (ctx->Extensions.EXT_timer_query)
154 return &ctx->Query.CurrentTimerObject;
155 else
156 return NULL;
157 #if FEATURE_EXT_transform_feedback
158 case GL_PRIMITIVES_GENERATED:
159 if (ctx->Extensions.EXT_transform_feedback)
160 return &ctx->Query.PrimitivesGenerated;
161 else
162 return NULL;
163 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
164 if (ctx->Extensions.EXT_transform_feedback)
165 return &ctx->Query.PrimitivesWritten;
166 else
167 return NULL;
168 #endif
169 default:
170 return NULL;
171 }
172 }
173
174
175 void GLAPIENTRY
176 _mesa_GenQueriesARB(GLsizei n, GLuint *ids)
177 {
178 GLuint first;
179 GET_CURRENT_CONTEXT(ctx);
180 ASSERT_OUTSIDE_BEGIN_END(ctx);
181
182 if (n < 0) {
183 _mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
184 return;
185 }
186
187 /* No query objects can be active at this time! */
188 if (ctx->Query.CurrentOcclusionObject ||
189 ctx->Query.CurrentTimerObject) {
190 _mesa_error(ctx, GL_INVALID_OPERATION, "glGenQueriesARB");
191 return;
192 }
193
194 first = _mesa_HashFindFreeKeyBlock(ctx->Query.QueryObjects, n);
195 if (first) {
196 GLsizei i;
197 for (i = 0; i < n; i++) {
198 struct gl_query_object *q
199 = ctx->Driver.NewQueryObject(ctx, first + i);
200 if (!q) {
201 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
202 return;
203 }
204 ids[i] = first + i;
205 _mesa_HashInsert(ctx->Query.QueryObjects, first + i, q);
206 }
207 }
208 }
209
210
211 void GLAPIENTRY
212 _mesa_DeleteQueriesARB(GLsizei n, const GLuint *ids)
213 {
214 GLint i;
215 GET_CURRENT_CONTEXT(ctx);
216 ASSERT_OUTSIDE_BEGIN_END(ctx);
217
218 if (n < 0) {
219 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
220 return;
221 }
222
223 /* No query objects can be active at this time! */
224 if (ctx->Query.CurrentOcclusionObject ||
225 ctx->Query.CurrentTimerObject) {
226 _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
227 return;
228 }
229
230 for (i = 0; i < n; i++) {
231 if (ids[i] > 0) {
232 struct gl_query_object *q = _mesa_lookup_query_object(ctx, ids[i]);
233 if (q) {
234 ASSERT(!q->Active); /* should be caught earlier */
235 _mesa_HashRemove(ctx->Query.QueryObjects, ids[i]);
236 ctx->Driver.DeleteQuery(ctx, q);
237 }
238 }
239 }
240 }
241
242
243 GLboolean GLAPIENTRY
244 _mesa_IsQueryARB(GLuint id)
245 {
246 GET_CURRENT_CONTEXT(ctx);
247 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
248
249 if (id && _mesa_lookup_query_object(ctx, id))
250 return GL_TRUE;
251 else
252 return GL_FALSE;
253 }
254
255
256 static void GLAPIENTRY
257 _mesa_BeginQueryARB(GLenum target, GLuint id)
258 {
259 struct gl_query_object *q, **bindpt;
260 GET_CURRENT_CONTEXT(ctx);
261 ASSERT_OUTSIDE_BEGIN_END(ctx);
262
263 FLUSH_VERTICES(ctx, _NEW_DEPTH);
264
265 bindpt = get_query_binding_point(ctx, target);
266 if (!bindpt) {
267 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQueryARB(target)");
268 return;
269 }
270
271 if (id == 0) {
272 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(id==0)");
273 return;
274 }
275
276 q = _mesa_lookup_query_object(ctx, id);
277 if (!q) {
278 /* create new object */
279 q = ctx->Driver.NewQueryObject(ctx, id);
280 if (!q) {
281 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQueryARB");
282 return;
283 }
284 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
285 }
286 else {
287 /* pre-existing object */
288 if (q->Active) {
289 _mesa_error(ctx, GL_INVALID_OPERATION,
290 "glBeginQueryARB(query already active)");
291 return;
292 }
293 }
294
295 q->Target = target;
296 q->Active = GL_TRUE;
297 q->Result = 0;
298 q->Ready = GL_FALSE;
299
300 /* XXX should probably refcount query objects */
301 *bindpt = q;
302
303 ctx->Driver.BeginQuery(ctx, q);
304 }
305
306
307 static void GLAPIENTRY
308 _mesa_EndQueryARB(GLenum target)
309 {
310 struct gl_query_object *q, **bindpt;
311 GET_CURRENT_CONTEXT(ctx);
312 ASSERT_OUTSIDE_BEGIN_END(ctx);
313
314 FLUSH_VERTICES(ctx, _NEW_DEPTH);
315
316 bindpt = get_query_binding_point(ctx, target);
317 if (!bindpt) {
318 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQueryARB(target)");
319 return;
320 }
321
322 /* XXX should probably refcount query objects */
323 q = *bindpt;
324 *bindpt = NULL;
325
326 if (!q || !q->Active) {
327 _mesa_error(ctx, GL_INVALID_OPERATION,
328 "glEndQueryARB(no matching glBeginQueryARB)");
329 return;
330 }
331
332 q->Active = GL_FALSE;
333 ctx->Driver.EndQuery(ctx, q);
334 }
335
336
337 void GLAPIENTRY
338 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
339 {
340 struct gl_query_object *q, **bindpt;
341 GET_CURRENT_CONTEXT(ctx);
342 ASSERT_OUTSIDE_BEGIN_END(ctx);
343
344 bindpt = get_query_binding_point(ctx, target);
345 if (!bindpt) {
346 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryARB(target)");
347 return;
348 }
349
350 q = *bindpt;
351
352 switch (pname) {
353 case GL_QUERY_COUNTER_BITS_ARB:
354 *params = 8 * sizeof(q->Result);
355 break;
356 case GL_CURRENT_QUERY_ARB:
357 *params = q ? q->Id : 0;
358 break;
359 default:
360 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivARB(pname)");
361 return;
362 }
363 }
364
365
366 void GLAPIENTRY
367 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *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 = _mesa_lookup_query_object(ctx, id);
375
376 if (!q || q->Active) {
377 _mesa_error(ctx, GL_INVALID_OPERATION,
378 "glGetQueryObjectivARB(id=%d is invalid or active)", id);
379 return;
380 }
381
382 switch (pname) {
383 case GL_QUERY_RESULT_ARB:
384 if (!q->Ready)
385 ctx->Driver.WaitQuery(ctx, q);
386 /* if result is too large for returned type, clamp to max value */
387 if (q->Target == GL_ANY_SAMPLES_PASSED) {
388 if (q->Result)
389 *params = GL_TRUE;
390 else
391 *params = GL_FALSE;
392 } else {
393 if (q->Result > 0x7fffffff) {
394 *params = 0x7fffffff;
395 }
396 else {
397 *params = (GLint)q->Result;
398 }
399 }
400 break;
401 case GL_QUERY_RESULT_AVAILABLE_ARB:
402 if (!q->Ready)
403 ctx->Driver.CheckQuery( ctx, q );
404 *params = q->Ready;
405 break;
406 default:
407 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
408 return;
409 }
410 }
411
412
413 void GLAPIENTRY
414 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *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 = _mesa_lookup_query_object(ctx, id);
422
423 if (!q || q->Active) {
424 _mesa_error(ctx, GL_INVALID_OPERATION,
425 "glGetQueryObjectuivARB(id=%d is invalid or active)", id);
426 return;
427 }
428
429 switch (pname) {
430 case GL_QUERY_RESULT_ARB:
431 if (!q->Ready)
432 ctx->Driver.WaitQuery(ctx, q);
433 /* if result is too large for returned type, clamp to max value */
434 if (q->Target == GL_ANY_SAMPLES_PASSED) {
435 if (q->Result)
436 *params = GL_TRUE;
437 else
438 *params = GL_FALSE;
439 } else {
440 if (q->Result > 0xffffffff) {
441 *params = 0xffffffff;
442 }
443 else {
444 *params = (GLuint)q->Result;
445 }
446 }
447 break;
448 case GL_QUERY_RESULT_AVAILABLE_ARB:
449 if (!q->Ready)
450 ctx->Driver.CheckQuery( ctx, q );
451 *params = q->Ready;
452 break;
453 default:
454 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
455 return;
456 }
457 }
458
459
460 /**
461 * New with GL_EXT_timer_query
462 */
463 static void GLAPIENTRY
464 _mesa_GetQueryObjecti64vEXT(GLuint id, GLenum pname, GLint64EXT *params)
465 {
466 struct gl_query_object *q = NULL;
467 GET_CURRENT_CONTEXT(ctx);
468 ASSERT_OUTSIDE_BEGIN_END(ctx);
469
470 if (id)
471 q = _mesa_lookup_query_object(ctx, id);
472
473 if (!q || q->Active) {
474 _mesa_error(ctx, GL_INVALID_OPERATION,
475 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id);
476 return;
477 }
478
479 switch (pname) {
480 case GL_QUERY_RESULT_ARB:
481 if (!q->Ready)
482 ctx->Driver.WaitQuery(ctx, q);
483 *params = q->Result;
484 break;
485 case GL_QUERY_RESULT_AVAILABLE_ARB:
486 if (!q->Ready)
487 ctx->Driver.CheckQuery( ctx, q );
488 *params = q->Ready;
489 break;
490 default:
491 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjecti64vARB(pname)");
492 return;
493 }
494 }
495
496
497 /**
498 * New with GL_EXT_timer_query
499 */
500 static void GLAPIENTRY
501 _mesa_GetQueryObjectui64vEXT(GLuint id, GLenum pname, GLuint64EXT *params)
502 {
503 struct gl_query_object *q = NULL;
504 GET_CURRENT_CONTEXT(ctx);
505 ASSERT_OUTSIDE_BEGIN_END(ctx);
506
507 if (id)
508 q = _mesa_lookup_query_object(ctx, id);
509
510 if (!q || q->Active) {
511 _mesa_error(ctx, GL_INVALID_OPERATION,
512 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id);
513 return;
514 }
515
516 switch (pname) {
517 case GL_QUERY_RESULT_ARB:
518 if (!q->Ready)
519 ctx->Driver.WaitQuery(ctx, q);
520 *params = q->Result;
521 break;
522 case GL_QUERY_RESULT_AVAILABLE_ARB:
523 if (!q->Ready)
524 ctx->Driver.CheckQuery( ctx, q );
525 *params = q->Ready;
526 break;
527 default:
528 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectui64vARB(pname)");
529 return;
530 }
531 }
532
533
534 void
535 _mesa_init_queryobj_dispatch(struct _glapi_table *disp)
536 {
537 SET_GenQueriesARB(disp, _mesa_GenQueriesARB);
538 SET_DeleteQueriesARB(disp, _mesa_DeleteQueriesARB);
539 SET_IsQueryARB(disp, _mesa_IsQueryARB);
540 SET_BeginQueryARB(disp, _mesa_BeginQueryARB);
541 SET_EndQueryARB(disp, _mesa_EndQueryARB);
542 SET_GetQueryivARB(disp, _mesa_GetQueryivARB);
543 SET_GetQueryObjectivARB(disp, _mesa_GetQueryObjectivARB);
544 SET_GetQueryObjectuivARB(disp, _mesa_GetQueryObjectuivARB);
545
546 SET_GetQueryObjecti64vEXT(disp, _mesa_GetQueryObjecti64vEXT);
547 SET_GetQueryObjectui64vEXT(disp, _mesa_GetQueryObjectui64vEXT);
548 }
549
550
551 #endif /* FEATURE_queryobj */
552
553
554 /**
555 * Allocate/init the context state related to query objects.
556 */
557 void
558 _mesa_init_queryobj(struct gl_context *ctx)
559 {
560 ctx->Query.QueryObjects = _mesa_NewHashTable();
561 ctx->Query.CurrentOcclusionObject = NULL;
562 }
563
564
565 /**
566 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
567 */
568 static void
569 delete_queryobj_cb(GLuint id, void *data, void *userData)
570 {
571 struct gl_query_object *q= (struct gl_query_object *) data;
572 struct gl_context *ctx = (struct gl_context *)userData;
573 ctx->Driver.DeleteQuery(ctx, q);
574 }
575
576
577 /**
578 * Free the context state related to query objects.
579 */
580 void
581 _mesa_free_queryobj_data(struct gl_context *ctx)
582 {
583 _mesa_HashDeleteAll(ctx->Query.QueryObjects, delete_queryobj_cb, ctx);
584 _mesa_DeleteHashTable(ctx->Query.QueryObjects);
585 }