mesa: Return 0 for GL_CURRENT_QUERY with a mismatched 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
378 /* Check for GL_ANY_SAMPLES_PASSED vs GL_SAMPLES_PASSED. */
379 if (q && q->Target != target) {
380 _mesa_error(ctx, GL_INVALID_OPERATION,
381 "glEndQuery(target=%s with active query of target %s)",
382 _mesa_lookup_enum_by_nr(target),
383 _mesa_lookup_enum_by_nr(q->Target));
384 return;
385 }
386
387 *bindpt = NULL;
388
389 if (!q || !q->Active) {
390 _mesa_error(ctx, GL_INVALID_OPERATION,
391 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
392 return;
393 }
394
395 q->Active = GL_FALSE;
396 ctx->Driver.EndQuery(ctx, q);
397 }
398
399 static void GLAPIENTRY
400 _mesa_BeginQueryARB(GLenum target, GLuint id)
401 {
402 _mesa_BeginQueryIndexed(target, 0, id);
403 }
404
405 static void GLAPIENTRY
406 _mesa_EndQueryARB(GLenum target)
407 {
408 _mesa_EndQueryIndexed(target, 0);
409 }
410
411 static void GLAPIENTRY
412 _mesa_QueryCounter(GLuint id, GLenum target)
413 {
414 struct gl_query_object *q;
415 GET_CURRENT_CONTEXT(ctx);
416 ASSERT_OUTSIDE_BEGIN_END(ctx);
417
418 if (MESA_VERBOSE & VERBOSE_API)
419 _mesa_debug(ctx, "glQueryCounter(%u, %s)\n", id,
420 _mesa_lookup_enum_by_nr(target));
421
422 /* error checking */
423 if (target != GL_TIMESTAMP) {
424 _mesa_error(ctx, GL_INVALID_ENUM, "glQueryCounter(target)");
425 return;
426 }
427
428 if (id == 0) {
429 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id==0)");
430 return;
431 }
432
433 q = _mesa_lookup_query_object(ctx, id);
434 if (!q) {
435 /* XXX the Core profile should throw INVALID_OPERATION here */
436
437 /* create new object */
438 q = ctx->Driver.NewQueryObject(ctx, id);
439 if (!q) {
440 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glQueryCounter");
441 return;
442 }
443 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
444 }
445 else {
446 if (q->Target && q->Target != GL_TIMESTAMP) {
447 _mesa_error(ctx, GL_INVALID_OPERATION,
448 "glQueryCounter(id has an invalid target)");
449 return;
450 }
451 }
452
453 if (q->Active) {
454 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id is active)");
455 return;
456 }
457
458 q->Target = target;
459 q->Result = 0;
460 q->Ready = GL_FALSE;
461
462 /* QueryCounter is implemented using EndQuery without BeginQuery
463 * in drivers. This is actually Direct3D and Gallium convention. */
464 ctx->Driver.EndQuery(ctx, q);
465 }
466
467
468 static void GLAPIENTRY
469 _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
470 GLint *params)
471 {
472 struct gl_query_object *q = NULL, **bindpt = NULL;
473 GET_CURRENT_CONTEXT(ctx);
474 ASSERT_OUTSIDE_BEGIN_END(ctx);
475
476 if (MESA_VERBOSE & VERBOSE_API)
477 _mesa_debug(ctx, "glGetQueryIndexediv(%s, %u, %s)\n",
478 _mesa_lookup_enum_by_nr(target),
479 index,
480 _mesa_lookup_enum_by_nr(pname));
481
482 if (!query_error_check_index(ctx, target, index))
483 return;
484
485 if (target == GL_TIMESTAMP) {
486 if (!ctx->Extensions.ARB_timer_query) {
487 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryARB(target)");
488 return;
489 }
490 }
491 else {
492 bindpt = get_query_binding_point(ctx, target);
493 if (!bindpt) {
494 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(target)");
495 return;
496 }
497
498 q = *bindpt;
499 }
500
501 switch (pname) {
502 case GL_QUERY_COUNTER_BITS_ARB:
503 switch (target) {
504 case GL_SAMPLES_PASSED:
505 *params = ctx->Const.QueryCounterBits.SamplesPassed;
506 break;
507 case GL_ANY_SAMPLES_PASSED:
508 /* The minimum value of this is 1 if it's nonzero, and the value
509 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
510 * bits.
511 */
512 *params = 1;
513 break;
514 case GL_TIME_ELAPSED:
515 *params = ctx->Const.QueryCounterBits.TimeElapsed;
516 break;
517 case GL_TIMESTAMP:
518 *params = ctx->Const.QueryCounterBits.Timestamp;
519 break;
520 case GL_PRIMITIVES_GENERATED:
521 *params = ctx->Const.QueryCounterBits.PrimitivesGenerated;
522 break;
523 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
524 *params = ctx->Const.QueryCounterBits.PrimitivesWritten;
525 break;
526 default:
527 _mesa_problem(ctx,
528 "Unknown target in glGetQueryIndexediv(target = %s)",
529 _mesa_lookup_enum_by_nr(target));
530 *params = 0;
531 break;
532 }
533 break;
534 case GL_CURRENT_QUERY_ARB:
535 *params = (q && q->Target == target) ? q->Id : 0;
536 break;
537 default:
538 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(pname)");
539 return;
540 }
541 }
542
543 static void GLAPIENTRY
544 _mesa_GetQueryivARB(GLenum target, GLenum pname, GLint *params)
545 {
546 _mesa_GetQueryIndexediv(target, 0, pname, params);
547 }
548
549 static void GLAPIENTRY
550 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
551 {
552 struct gl_query_object *q = NULL;
553 GET_CURRENT_CONTEXT(ctx);
554 ASSERT_OUTSIDE_BEGIN_END(ctx);
555
556 if (MESA_VERBOSE & VERBOSE_API)
557 _mesa_debug(ctx, "glGetQueryObjectiv(%u, %s)\n", id,
558 _mesa_lookup_enum_by_nr(pname));
559
560 if (id)
561 q = _mesa_lookup_query_object(ctx, id);
562
563 if (!q || q->Active) {
564 _mesa_error(ctx, GL_INVALID_OPERATION,
565 "glGetQueryObjectivARB(id=%d is invalid or active)", id);
566 return;
567 }
568
569 switch (pname) {
570 case GL_QUERY_RESULT_ARB:
571 if (!q->Ready)
572 ctx->Driver.WaitQuery(ctx, q);
573 /* if result is too large for returned type, clamp to max value */
574 if (q->Target == GL_ANY_SAMPLES_PASSED) {
575 if (q->Result)
576 *params = GL_TRUE;
577 else
578 *params = GL_FALSE;
579 } else {
580 if (q->Result > 0x7fffffff) {
581 *params = 0x7fffffff;
582 }
583 else {
584 *params = (GLint)q->Result;
585 }
586 }
587 break;
588 case GL_QUERY_RESULT_AVAILABLE_ARB:
589 if (!q->Ready)
590 ctx->Driver.CheckQuery( ctx, q );
591 *params = q->Ready;
592 break;
593 default:
594 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
595 return;
596 }
597 }
598
599
600 static void GLAPIENTRY
601 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
602 {
603 struct gl_query_object *q = NULL;
604 GET_CURRENT_CONTEXT(ctx);
605 ASSERT_OUTSIDE_BEGIN_END(ctx);
606
607 if (MESA_VERBOSE & VERBOSE_API)
608 _mesa_debug(ctx, "glGetQueryObjectuiv(%u, %s)\n", id,
609 _mesa_lookup_enum_by_nr(pname));
610
611 if (id)
612 q = _mesa_lookup_query_object(ctx, id);
613
614 if (!q || q->Active) {
615 _mesa_error(ctx, GL_INVALID_OPERATION,
616 "glGetQueryObjectuivARB(id=%d is invalid or active)", id);
617 return;
618 }
619
620 switch (pname) {
621 case GL_QUERY_RESULT_ARB:
622 if (!q->Ready)
623 ctx->Driver.WaitQuery(ctx, q);
624 /* if result is too large for returned type, clamp to max value */
625 if (q->Target == GL_ANY_SAMPLES_PASSED) {
626 if (q->Result)
627 *params = GL_TRUE;
628 else
629 *params = GL_FALSE;
630 } else {
631 if (q->Result > 0xffffffff) {
632 *params = 0xffffffff;
633 }
634 else {
635 *params = (GLuint)q->Result;
636 }
637 }
638 break;
639 case GL_QUERY_RESULT_AVAILABLE_ARB:
640 if (!q->Ready)
641 ctx->Driver.CheckQuery( ctx, q );
642 *params = q->Ready;
643 break;
644 default:
645 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
646 return;
647 }
648 }
649
650
651 /**
652 * New with GL_EXT_timer_query
653 */
654 static void GLAPIENTRY
655 _mesa_GetQueryObjecti64vEXT(GLuint id, GLenum pname, GLint64EXT *params)
656 {
657 struct gl_query_object *q = NULL;
658 GET_CURRENT_CONTEXT(ctx);
659 ASSERT_OUTSIDE_BEGIN_END(ctx);
660
661 if (MESA_VERBOSE & VERBOSE_API)
662 _mesa_debug(ctx, "glGetQueryObjecti64v(%u, %s)\n", id,
663 _mesa_lookup_enum_by_nr(pname));
664
665 if (id)
666 q = _mesa_lookup_query_object(ctx, id);
667
668 if (!q || q->Active) {
669 _mesa_error(ctx, GL_INVALID_OPERATION,
670 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id);
671 return;
672 }
673
674 switch (pname) {
675 case GL_QUERY_RESULT_ARB:
676 if (!q->Ready)
677 ctx->Driver.WaitQuery(ctx, q);
678 *params = q->Result;
679 break;
680 case GL_QUERY_RESULT_AVAILABLE_ARB:
681 if (!q->Ready)
682 ctx->Driver.CheckQuery( ctx, q );
683 *params = q->Ready;
684 break;
685 default:
686 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjecti64vARB(pname)");
687 return;
688 }
689 }
690
691
692 /**
693 * New with GL_EXT_timer_query
694 */
695 static void GLAPIENTRY
696 _mesa_GetQueryObjectui64vEXT(GLuint id, GLenum pname, GLuint64EXT *params)
697 {
698 struct gl_query_object *q = NULL;
699 GET_CURRENT_CONTEXT(ctx);
700 ASSERT_OUTSIDE_BEGIN_END(ctx);
701
702 if (MESA_VERBOSE & VERBOSE_API)
703 _mesa_debug(ctx, "glGetQueryObjectui64v(%u, %s)\n", id,
704 _mesa_lookup_enum_by_nr(pname));
705
706 if (id)
707 q = _mesa_lookup_query_object(ctx, id);
708
709 if (!q || q->Active) {
710 _mesa_error(ctx, GL_INVALID_OPERATION,
711 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id);
712 return;
713 }
714
715 switch (pname) {
716 case GL_QUERY_RESULT_ARB:
717 if (!q->Ready)
718 ctx->Driver.WaitQuery(ctx, q);
719 *params = q->Result;
720 break;
721 case GL_QUERY_RESULT_AVAILABLE_ARB:
722 if (!q->Ready)
723 ctx->Driver.CheckQuery( ctx, q );
724 *params = q->Ready;
725 break;
726 default:
727 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectui64vARB(pname)");
728 return;
729 }
730 }
731
732
733 void
734 _mesa_init_queryobj_dispatch(struct _glapi_table *disp)
735 {
736 SET_GenQueriesARB(disp, _mesa_GenQueriesARB);
737 SET_DeleteQueriesARB(disp, _mesa_DeleteQueriesARB);
738 SET_IsQueryARB(disp, _mesa_IsQueryARB);
739 SET_BeginQueryARB(disp, _mesa_BeginQueryARB);
740 SET_EndQueryARB(disp, _mesa_EndQueryARB);
741 SET_GetQueryivARB(disp, _mesa_GetQueryivARB);
742 SET_GetQueryObjectivARB(disp, _mesa_GetQueryObjectivARB);
743 SET_GetQueryObjectuivARB(disp, _mesa_GetQueryObjectuivARB);
744 SET_QueryCounter(disp, _mesa_QueryCounter);
745
746 SET_GetQueryObjecti64vEXT(disp, _mesa_GetQueryObjecti64vEXT);
747 SET_GetQueryObjectui64vEXT(disp, _mesa_GetQueryObjectui64vEXT);
748
749 SET_BeginQueryIndexed(disp, _mesa_BeginQueryIndexed);
750 SET_EndQueryIndexed(disp, _mesa_EndQueryIndexed);
751 SET_GetQueryIndexediv(disp, _mesa_GetQueryIndexediv);
752 }
753
754
755 /**
756 * Allocate/init the context state related to query objects.
757 */
758 void
759 _mesa_init_queryobj(struct gl_context *ctx)
760 {
761 ctx->Query.QueryObjects = _mesa_NewHashTable();
762 ctx->Query.CurrentOcclusionObject = NULL;
763
764 ctx->Const.QueryCounterBits.SamplesPassed = 64;
765 ctx->Const.QueryCounterBits.TimeElapsed = 64;
766 ctx->Const.QueryCounterBits.Timestamp = 64;
767 ctx->Const.QueryCounterBits.PrimitivesGenerated = 64;
768 ctx->Const.QueryCounterBits.PrimitivesWritten = 64;
769 }
770
771
772 /**
773 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
774 */
775 static void
776 delete_queryobj_cb(GLuint id, void *data, void *userData)
777 {
778 struct gl_query_object *q= (struct gl_query_object *) data;
779 struct gl_context *ctx = (struct gl_context *)userData;
780 ctx->Driver.DeleteQuery(ctx, q);
781 }
782
783
784 /**
785 * Free the context state related to query objects.
786 */
787 void
788 _mesa_free_queryobj_data(struct gl_context *ctx)
789 {
790 _mesa_HashDeleteAll(ctx->Query.QueryObjects, delete_queryobj_cb, ctx);
791 _mesa_DeleteHashTable(ctx->Query.QueryObjects);
792 }