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