mesa: Throw an error for a new query on an already-active query target.
[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 "enums.h"
29 #include "hash.h"
30 #include "imports.h"
31 #include "queryobj.h"
32 #include "mfeatures.h"
33 #include "mtypes.h"
34 #include "main/dispatch.h"
35
36
37 /**
38 * Allocate a new query object. This is a fallback routine called via
39 * ctx->Driver.NewQueryObject().
40 * \param ctx - rendering context
41 * \param id - the new object's ID
42 * \return pointer to new query_object object or NULL if out of memory.
43 */
44 static struct gl_query_object *
45 _mesa_new_query_object(struct gl_context *ctx, GLuint id)
46 {
47 struct gl_query_object *q = MALLOC_STRUCT(gl_query_object);
48 (void) ctx;
49 if (q) {
50 q->Id = id;
51 q->Result = 0;
52 q->Active = GL_FALSE;
53 q->Ready = GL_TRUE; /* correct, see spec */
54 }
55 return q;
56 }
57
58
59 /**
60 * Begin a query. Software driver fallback.
61 * Called via ctx->Driver.BeginQuery().
62 */
63 static void
64 _mesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
65 {
66 /* no-op */
67 }
68
69
70 /**
71 * End a query. Software driver fallback.
72 * Called via ctx->Driver.EndQuery().
73 */
74 static void
75 _mesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
76 {
77 q->Ready = GL_TRUE;
78 }
79
80
81 /**
82 * Wait for query to complete. Software driver fallback.
83 * Called via ctx->Driver.WaitQuery().
84 */
85 static void
86 _mesa_wait_query(struct gl_context *ctx, struct gl_query_object *q)
87 {
88 /* For software drivers, _mesa_end_query() should have completed the query.
89 * For real hardware, implement a proper WaitQuery() driver function,
90 * which may require issuing a flush.
91 */
92 assert(q->Ready);
93 }
94
95
96 /**
97 * Check if a query results are ready. Software driver fallback.
98 * Called via ctx->Driver.CheckQuery().
99 */
100 static void
101 _mesa_check_query(struct gl_context *ctx, struct gl_query_object *q)
102 {
103 /* No-op for sw rendering.
104 * HW drivers may need to flush at this time.
105 */
106 }
107
108
109 /**
110 * Delete a query object. Called via ctx->Driver.DeleteQuery().
111 * Not removed from hash table here.
112 */
113 static void
114 _mesa_delete_query(struct gl_context *ctx, struct gl_query_object *q)
115 {
116 free(q);
117 }
118
119
120 void
121 _mesa_init_query_object_functions(struct dd_function_table *driver)
122 {
123 driver->NewQueryObject = _mesa_new_query_object;
124 driver->DeleteQuery = _mesa_delete_query;
125 driver->BeginQuery = _mesa_begin_query;
126 driver->EndQuery = _mesa_end_query;
127 driver->WaitQuery = _mesa_wait_query;
128 driver->CheckQuery = _mesa_check_query;
129 }
130
131
132 /**
133 * Return pointer to the query object binding point for the given target.
134 * \return NULL if invalid target, else the address of binding point
135 */
136 static struct gl_query_object **
137 get_query_binding_point(struct gl_context *ctx, GLenum target)
138 {
139 switch (target) {
140 case GL_SAMPLES_PASSED_ARB:
141 if (ctx->Extensions.ARB_occlusion_query)
142 return &ctx->Query.CurrentOcclusionObject;
143 else
144 return NULL;
145 case GL_ANY_SAMPLES_PASSED:
146 if (ctx->Extensions.ARB_occlusion_query2)
147 return &ctx->Query.CurrentOcclusionObject;
148 else
149 return NULL;
150 case GL_TIME_ELAPSED_EXT:
151 if (ctx->Extensions.EXT_timer_query)
152 return &ctx->Query.CurrentTimerObject;
153 else
154 return NULL;
155 case GL_PRIMITIVES_GENERATED:
156 if (ctx->Extensions.EXT_transform_feedback)
157 return &ctx->Query.PrimitivesGenerated;
158 else
159 return NULL;
160 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
161 if (ctx->Extensions.EXT_transform_feedback)
162 return &ctx->Query.PrimitivesWritten;
163 else
164 return NULL;
165 default:
166 return NULL;
167 }
168 }
169
170
171 static void GLAPIENTRY
172 _mesa_GenQueriesARB(GLsizei n, GLuint *ids)
173 {
174 GLuint first;
175 GET_CURRENT_CONTEXT(ctx);
176 ASSERT_OUTSIDE_BEGIN_END(ctx);
177
178 if (MESA_VERBOSE & VERBOSE_API)
179 _mesa_debug(ctx, "glGenQueries(%d)\n", n);
180
181 if (n < 0) {
182 _mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
183 return;
184 }
185
186 /* No query objects can be active at this time! */
187 if (ctx->Query.CurrentOcclusionObject ||
188 ctx->Query.CurrentTimerObject) {
189 _mesa_error(ctx, GL_INVALID_OPERATION, "glGenQueriesARB");
190 return;
191 }
192
193 first = _mesa_HashFindFreeKeyBlock(ctx->Query.QueryObjects, n);
194 if (first) {
195 GLsizei i;
196 for (i = 0; i < n; i++) {
197 struct gl_query_object *q
198 = ctx->Driver.NewQueryObject(ctx, first + i);
199 if (!q) {
200 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
201 return;
202 }
203 ids[i] = first + i;
204 _mesa_HashInsert(ctx->Query.QueryObjects, first + i, q);
205 }
206 }
207 }
208
209
210 static void GLAPIENTRY
211 _mesa_DeleteQueriesARB(GLsizei n, const GLuint *ids)
212 {
213 GLint i;
214 GET_CURRENT_CONTEXT(ctx);
215 ASSERT_OUTSIDE_BEGIN_END(ctx);
216 FLUSH_VERTICES(ctx, 0);
217
218 if (MESA_VERBOSE & VERBOSE_API)
219 _mesa_debug(ctx, "glDeleeteQueries(%d)\n", n);
220
221 if (n < 0) {
222 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
223 return;
224 }
225
226 /* No query objects can be active at this time! */
227 if (ctx->Query.CurrentOcclusionObject ||
228 ctx->Query.CurrentTimerObject) {
229 _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
230 return;
231 }
232
233 for (i = 0; i < n; i++) {
234 if (ids[i] > 0) {
235 struct gl_query_object *q = _mesa_lookup_query_object(ctx, ids[i]);
236 if (q) {
237 ASSERT(!q->Active); /* should be caught earlier */
238 _mesa_HashRemove(ctx->Query.QueryObjects, ids[i]);
239 ctx->Driver.DeleteQuery(ctx, q);
240 }
241 }
242 }
243 }
244
245
246 static GLboolean GLAPIENTRY
247 _mesa_IsQueryARB(GLuint id)
248 {
249 GET_CURRENT_CONTEXT(ctx);
250 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
251
252 if (MESA_VERBOSE & VERBOSE_API)
253 _mesa_debug(ctx, "glIsQuery(%u)\n", id);
254
255 if (id && _mesa_lookup_query_object(ctx, id))
256 return GL_TRUE;
257 else
258 return GL_FALSE;
259 }
260
261 static GLboolean
262 query_error_check_index(struct gl_context *ctx, GLenum target, GLuint index)
263 {
264 switch (target) {
265 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
266 case GL_PRIMITIVES_GENERATED:
267 if (index >= ctx->Const.MaxVertexStreams) {
268 _mesa_error(ctx, GL_INVALID_VALUE,
269 "glBeginQueryIndexed(index>=MaxVertexStreams)");
270 return GL_FALSE;
271 }
272 break;
273 default:
274 if (index > 0) {
275 _mesa_error(ctx, GL_INVALID_VALUE, "glBeginQueryIndexed(index>0)");
276 return GL_FALSE;
277 }
278 }
279 return GL_TRUE;
280 }
281
282 static void GLAPIENTRY
283 _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
284 {
285 struct gl_query_object *q, **bindpt;
286 GET_CURRENT_CONTEXT(ctx);
287 ASSERT_OUTSIDE_BEGIN_END(ctx);
288
289 if (MESA_VERBOSE & VERBOSE_API)
290 _mesa_debug(ctx, "glBeginQueryIndexed(%s, %u, %u)\n",
291 _mesa_lookup_enum_by_nr(target), index, id);
292
293 if (!query_error_check_index(ctx, target, index))
294 return;
295
296 FLUSH_VERTICES(ctx, _NEW_DEPTH);
297
298 bindpt = get_query_binding_point(ctx, target);
299 if (!bindpt) {
300 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQuery{Indexed}(target)");
301 return;
302 }
303
304 /* From the GL_ARB_occlusion_query spec:
305 *
306 * "If BeginQueryARB is called while another query is already in
307 * progress with the same target, an INVALID_OPERATION error is
308 * generated."
309 */
310 if (*bindpt) {
311 _mesa_error(ctx, GL_INVALID_OPERATION,
312 "glBeginQuery{Indexed}(target=%s is active)",
313 _mesa_lookup_enum_by_nr(target));
314 return;
315 }
316
317 if (id == 0) {
318 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQuery{Indexed}(id==0)");
319 return;
320 }
321
322 q = _mesa_lookup_query_object(ctx, id);
323 if (!q) {
324 /* create new object */
325 q = ctx->Driver.NewQueryObject(ctx, id);
326 if (!q) {
327 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQuery{Indexed}");
328 return;
329 }
330 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
331 }
332 else {
333 /* pre-existing object */
334 if (q->Active) {
335 _mesa_error(ctx, GL_INVALID_OPERATION,
336 "glBeginQuery{Indexed}(query already active)");
337 return;
338 }
339 }
340
341 q->Target = target;
342 q->Active = GL_TRUE;
343 q->Result = 0;
344 q->Ready = GL_FALSE;
345
346 /* XXX should probably refcount query objects */
347 *bindpt = q;
348
349 ctx->Driver.BeginQuery(ctx, q);
350 }
351
352
353 static void GLAPIENTRY
354 _mesa_EndQueryIndexed(GLenum target, GLuint index)
355 {
356 struct gl_query_object *q, **bindpt;
357 GET_CURRENT_CONTEXT(ctx);
358 ASSERT_OUTSIDE_BEGIN_END(ctx);
359
360 if (MESA_VERBOSE & VERBOSE_API)
361 _mesa_debug(ctx, "glEndQueryIndexed(%s, %u)\n",
362 _mesa_lookup_enum_by_nr(target), index);
363
364 if (!query_error_check_index(ctx, target, index))
365 return;
366
367 FLUSH_VERTICES(ctx, _NEW_DEPTH);
368
369 bindpt = get_query_binding_point(ctx, target);
370 if (!bindpt) {
371 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQuery{Indexed}(target)");
372 return;
373 }
374
375 /* XXX should probably refcount query objects */
376 q = *bindpt;
377 *bindpt = NULL;
378
379 if (!q || !q->Active) {
380 _mesa_error(ctx, GL_INVALID_OPERATION,
381 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
382 return;
383 }
384
385 q->Active = GL_FALSE;
386 ctx->Driver.EndQuery(ctx, q);
387 }
388
389 static void GLAPIENTRY
390 _mesa_BeginQueryARB(GLenum target, GLuint id)
391 {
392 _mesa_BeginQueryIndexed(target, 0, id);
393 }
394
395 static void GLAPIENTRY
396 _mesa_EndQueryARB(GLenum target)
397 {
398 _mesa_EndQueryIndexed(target, 0);
399 }
400
401 static void GLAPIENTRY
402 _mesa_QueryCounter(GLuint id, GLenum target)
403 {
404 struct gl_query_object *q;
405 GET_CURRENT_CONTEXT(ctx);
406 ASSERT_OUTSIDE_BEGIN_END(ctx);
407
408 if (MESA_VERBOSE & VERBOSE_API)
409 _mesa_debug(ctx, "glQueryCounter(%u, %s)\n", id,
410 _mesa_lookup_enum_by_nr(target));
411
412 /* error checking */
413 if (target != GL_TIMESTAMP) {
414 _mesa_error(ctx, GL_INVALID_ENUM, "glQueryCounter(target)");
415 return;
416 }
417
418 if (id == 0) {
419 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id==0)");
420 return;
421 }
422
423 q = _mesa_lookup_query_object(ctx, id);
424 if (!q) {
425 /* XXX the Core profile should throw INVALID_OPERATION here */
426
427 /* create new object */
428 q = ctx->Driver.NewQueryObject(ctx, id);
429 if (!q) {
430 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glQueryCounter");
431 return;
432 }
433 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
434 }
435 else {
436 if (q->Target && q->Target != GL_TIMESTAMP) {
437 _mesa_error(ctx, GL_INVALID_OPERATION,
438 "glQueryCounter(id has an invalid target)");
439 return;
440 }
441 }
442
443 if (q->Active) {
444 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id is active)");
445 return;
446 }
447
448 q->Target = target;
449 q->Result = 0;
450 q->Ready = GL_FALSE;
451
452 /* QueryCounter is implemented using EndQuery without BeginQuery
453 * in drivers. This is actually Direct3D and Gallium convention. */
454 ctx->Driver.EndQuery(ctx, q);
455 }
456
457
458 static void GLAPIENTRY
459 _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
460 GLint *params)
461 {
462 struct gl_query_object *q = NULL, **bindpt = NULL;
463 GET_CURRENT_CONTEXT(ctx);
464 ASSERT_OUTSIDE_BEGIN_END(ctx);
465
466 if (MESA_VERBOSE & VERBOSE_API)
467 _mesa_debug(ctx, "glGetQueryIndexediv(%s, %u, %s)\n",
468 _mesa_lookup_enum_by_nr(target),
469 index,
470 _mesa_lookup_enum_by_nr(pname));
471
472 if (!query_error_check_index(ctx, target, index))
473 return;
474
475 if (target == GL_TIMESTAMP) {
476 if (!ctx->Extensions.ARB_timer_query) {
477 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryARB(target)");
478 return;
479 }
480 }
481 else {
482 bindpt = get_query_binding_point(ctx, target);
483 if (!bindpt) {
484 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(target)");
485 return;
486 }
487
488 q = *bindpt;
489 }
490
491 switch (pname) {
492 case GL_QUERY_COUNTER_BITS_ARB:
493 switch (target) {
494 case GL_SAMPLES_PASSED:
495 *params = ctx->Const.QueryCounterBits.SamplesPassed;
496 break;
497 case GL_ANY_SAMPLES_PASSED:
498 /* The minimum value of this is 1 if it's nonzero, and the value
499 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
500 * bits.
501 */
502 *params = 1;
503 break;
504 case GL_TIME_ELAPSED:
505 *params = ctx->Const.QueryCounterBits.TimeElapsed;
506 break;
507 case GL_TIMESTAMP:
508 *params = ctx->Const.QueryCounterBits.Timestamp;
509 break;
510 case GL_PRIMITIVES_GENERATED:
511 *params = ctx->Const.QueryCounterBits.PrimitivesGenerated;
512 break;
513 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
514 *params = ctx->Const.QueryCounterBits.PrimitivesWritten;
515 break;
516 default:
517 _mesa_problem(ctx,
518 "Unknown target in glGetQueryIndexediv(target = %s)",
519 _mesa_lookup_enum_by_nr(target));
520 *params = 0;
521 break;
522 }
523 break;
524 case GL_CURRENT_QUERY_ARB:
525 *params = q ? q->Id : 0;
526 break;
527 default:
528 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(pname)");
529 return;
530 }
531 }
532
533 static void GLAPIENTRY
534 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
535 {
536 _mesa_GetQueryIndexediv(target, 0, pname, params);
537 }
538
539 static void GLAPIENTRY
540 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
541 {
542 struct gl_query_object *q = NULL;
543 GET_CURRENT_CONTEXT(ctx);
544 ASSERT_OUTSIDE_BEGIN_END(ctx);
545
546 if (MESA_VERBOSE & VERBOSE_API)
547 _mesa_debug(ctx, "glGetQueryObjectiv(%u, %s)\n", id,
548 _mesa_lookup_enum_by_nr(pname));
549
550 if (id)
551 q = _mesa_lookup_query_object(ctx, id);
552
553 if (!q || q->Active) {
554 _mesa_error(ctx, GL_INVALID_OPERATION,
555 "glGetQueryObjectivARB(id=%d is invalid or active)", id);
556 return;
557 }
558
559 switch (pname) {
560 case GL_QUERY_RESULT_ARB:
561 if (!q->Ready)
562 ctx->Driver.WaitQuery(ctx, q);
563 /* if result is too large for returned type, clamp to max value */
564 if (q->Target == GL_ANY_SAMPLES_PASSED) {
565 if (q->Result)
566 *params = GL_TRUE;
567 else
568 *params = GL_FALSE;
569 } else {
570 if (q->Result > 0x7fffffff) {
571 *params = 0x7fffffff;
572 }
573 else {
574 *params = (GLint)q->Result;
575 }
576 }
577 break;
578 case GL_QUERY_RESULT_AVAILABLE_ARB:
579 if (!q->Ready)
580 ctx->Driver.CheckQuery( ctx, q );
581 *params = q->Ready;
582 break;
583 default:
584 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
585 return;
586 }
587 }
588
589
590 static void GLAPIENTRY
591 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
592 {
593 struct gl_query_object *q = NULL;
594 GET_CURRENT_CONTEXT(ctx);
595 ASSERT_OUTSIDE_BEGIN_END(ctx);
596
597 if (MESA_VERBOSE & VERBOSE_API)
598 _mesa_debug(ctx, "glGetQueryObjectuiv(%u, %s)\n", id,
599 _mesa_lookup_enum_by_nr(pname));
600
601 if (id)
602 q = _mesa_lookup_query_object(ctx, id);
603
604 if (!q || q->Active) {
605 _mesa_error(ctx, GL_INVALID_OPERATION,
606 "glGetQueryObjectuivARB(id=%d is invalid or active)", id);
607 return;
608 }
609
610 switch (pname) {
611 case GL_QUERY_RESULT_ARB:
612 if (!q->Ready)
613 ctx->Driver.WaitQuery(ctx, q);
614 /* if result is too large for returned type, clamp to max value */
615 if (q->Target == GL_ANY_SAMPLES_PASSED) {
616 if (q->Result)
617 *params = GL_TRUE;
618 else
619 *params = GL_FALSE;
620 } else {
621 if (q->Result > 0xffffffff) {
622 *params = 0xffffffff;
623 }
624 else {
625 *params = (GLuint)q->Result;
626 }
627 }
628 break;
629 case GL_QUERY_RESULT_AVAILABLE_ARB:
630 if (!q->Ready)
631 ctx->Driver.CheckQuery( ctx, q );
632 *params = q->Ready;
633 break;
634 default:
635 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
636 return;
637 }
638 }
639
640
641 /**
642 * New with GL_EXT_timer_query
643 */
644 static void GLAPIENTRY
645 _mesa_GetQueryObjecti64vEXT(GLuint id, GLenum pname, GLint64EXT *params)
646 {
647 struct gl_query_object *q = NULL;
648 GET_CURRENT_CONTEXT(ctx);
649 ASSERT_OUTSIDE_BEGIN_END(ctx);
650
651 if (MESA_VERBOSE & VERBOSE_API)
652 _mesa_debug(ctx, "glGetQueryObjecti64v(%u, %s)\n", id,
653 _mesa_lookup_enum_by_nr(pname));
654
655 if (id)
656 q = _mesa_lookup_query_object(ctx, id);
657
658 if (!q || q->Active) {
659 _mesa_error(ctx, GL_INVALID_OPERATION,
660 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id);
661 return;
662 }
663
664 switch (pname) {
665 case GL_QUERY_RESULT_ARB:
666 if (!q->Ready)
667 ctx->Driver.WaitQuery(ctx, q);
668 *params = q->Result;
669 break;
670 case GL_QUERY_RESULT_AVAILABLE_ARB:
671 if (!q->Ready)
672 ctx->Driver.CheckQuery( ctx, q );
673 *params = q->Ready;
674 break;
675 default:
676 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjecti64vARB(pname)");
677 return;
678 }
679 }
680
681
682 /**
683 * New with GL_EXT_timer_query
684 */
685 static void GLAPIENTRY
686 _mesa_GetQueryObjectui64vEXT(GLuint id, GLenum pname, GLuint64EXT *params)
687 {
688 struct gl_query_object *q = NULL;
689 GET_CURRENT_CONTEXT(ctx);
690 ASSERT_OUTSIDE_BEGIN_END(ctx);
691
692 if (MESA_VERBOSE & VERBOSE_API)
693 _mesa_debug(ctx, "glGetQueryObjectui64v(%u, %s)\n", id,
694 _mesa_lookup_enum_by_nr(pname));
695
696 if (id)
697 q = _mesa_lookup_query_object(ctx, id);
698
699 if (!q || q->Active) {
700 _mesa_error(ctx, GL_INVALID_OPERATION,
701 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id);
702 return;
703 }
704
705 switch (pname) {
706 case GL_QUERY_RESULT_ARB:
707 if (!q->Ready)
708 ctx->Driver.WaitQuery(ctx, q);
709 *params = q->Result;
710 break;
711 case GL_QUERY_RESULT_AVAILABLE_ARB:
712 if (!q->Ready)
713 ctx->Driver.CheckQuery( ctx, q );
714 *params = q->Ready;
715 break;
716 default:
717 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectui64vARB(pname)");
718 return;
719 }
720 }
721
722
723 void
724 _mesa_init_queryobj_dispatch(struct _glapi_table *disp)
725 {
726 SET_GenQueriesARB(disp, _mesa_GenQueriesARB);
727 SET_DeleteQueriesARB(disp, _mesa_DeleteQueriesARB);
728 SET_IsQueryARB(disp, _mesa_IsQueryARB);
729 SET_BeginQueryARB(disp, _mesa_BeginQueryARB);
730 SET_EndQueryARB(disp, _mesa_EndQueryARB);
731 SET_GetQueryivARB(disp, _mesa_GetQueryivARB);
732 SET_GetQueryObjectivARB(disp, _mesa_GetQueryObjectivARB);
733 SET_GetQueryObjectuivARB(disp, _mesa_GetQueryObjectuivARB);
734 SET_QueryCounter(disp, _mesa_QueryCounter);
735
736 SET_GetQueryObjecti64vEXT(disp, _mesa_GetQueryObjecti64vEXT);
737 SET_GetQueryObjectui64vEXT(disp, _mesa_GetQueryObjectui64vEXT);
738
739 SET_BeginQueryIndexed(disp, _mesa_BeginQueryIndexed);
740 SET_EndQueryIndexed(disp, _mesa_EndQueryIndexed);
741 SET_GetQueryIndexediv(disp, _mesa_GetQueryIndexediv);
742 }
743
744
745 /**
746 * Allocate/init the context state related to query objects.
747 */
748 void
749 _mesa_init_queryobj(struct gl_context *ctx)
750 {
751 ctx->Query.QueryObjects = _mesa_NewHashTable();
752 ctx->Query.CurrentOcclusionObject = NULL;
753
754 ctx->Const.QueryCounterBits.SamplesPassed = 64;
755 ctx->Const.QueryCounterBits.TimeElapsed = 64;
756 ctx->Const.QueryCounterBits.Timestamp = 64;
757 ctx->Const.QueryCounterBits.PrimitivesGenerated = 64;
758 ctx->Const.QueryCounterBits.PrimitivesWritten = 64;
759 }
760
761
762 /**
763 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
764 */
765 static void
766 delete_queryobj_cb(GLuint id, void *data, void *userData)
767 {
768 struct gl_query_object *q= (struct gl_query_object *) data;
769 struct gl_context *ctx = (struct gl_context *)userData;
770 ctx->Driver.DeleteQuery(ctx, q);
771 }
772
773
774 /**
775 * Free the context state related to query objects.
776 */
777 void
778 _mesa_free_queryobj_data(struct gl_context *ctx)
779 {
780 _mesa_HashDeleteAll(ctx->Query.QueryObjects, delete_queryobj_cb, ctx);
781 _mesa_DeleteHashTable(ctx->Query.QueryObjects);
782 }