mesa/es2: Allow depth component cube maps in ES2 if the extension is enabled
[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
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 /* no-op */
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 q->Ready = GL_TRUE;
91 }
92
93
94 /**
95 * Wait for query to complete. Software driver fallback.
96 * Called via ctx->Driver.WaitQuery().
97 */
98 static void
99 _mesa_wait_query(struct gl_context *ctx, struct gl_query_object *q)
100 {
101 /* For software drivers, _mesa_end_query() should have completed the query.
102 * For real hardware, implement a proper WaitQuery() driver function,
103 * which may require issuing a flush.
104 */
105 assert(q->Ready);
106 }
107
108
109 /**
110 * Check if a query results are ready. Software driver fallback.
111 * Called via ctx->Driver.CheckQuery().
112 */
113 static void
114 _mesa_check_query(struct gl_context *ctx, struct gl_query_object *q)
115 {
116 /* No-op for sw rendering.
117 * HW drivers may need to flush at this time.
118 */
119 }
120
121
122 /**
123 * Delete a query object. Called via ctx->Driver.DeleteQuery().
124 * Not removed from hash table here.
125 */
126 static void
127 _mesa_delete_query(struct gl_context *ctx, struct gl_query_object *q)
128 {
129 free(q);
130 }
131
132
133 void
134 _mesa_init_query_object_functions(struct dd_function_table *driver)
135 {
136 driver->NewQueryObject = _mesa_new_query_object;
137 driver->DeleteQuery = _mesa_delete_query;
138 driver->BeginQuery = _mesa_begin_query;
139 driver->EndQuery = _mesa_end_query;
140 driver->WaitQuery = _mesa_wait_query;
141 driver->CheckQuery = _mesa_check_query;
142 }
143
144
145 /**
146 * Return pointer to the query object binding point for the given target.
147 * \return NULL if invalid target, else the address of binding point
148 */
149 static struct gl_query_object **
150 get_query_binding_point(struct gl_context *ctx, GLenum target)
151 {
152 switch (target) {
153 case GL_SAMPLES_PASSED_ARB:
154 if (ctx->Extensions.ARB_occlusion_query)
155 return &ctx->Query.CurrentOcclusionObject;
156 else
157 return NULL;
158 case GL_ANY_SAMPLES_PASSED:
159 if (ctx->Extensions.ARB_occlusion_query2)
160 return &ctx->Query.CurrentOcclusionObject;
161 else
162 return NULL;
163 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
164 if (ctx->Extensions.ARB_ES3_compatibility
165 || (ctx->API == API_OPENGLES2 && ctx->Version >= 30))
166 return &ctx->Query.CurrentOcclusionObject;
167 else
168 return NULL;
169 case GL_TIME_ELAPSED_EXT:
170 if (ctx->Extensions.EXT_timer_query)
171 return &ctx->Query.CurrentTimerObject;
172 else
173 return NULL;
174 case GL_PRIMITIVES_GENERATED:
175 if (ctx->Extensions.EXT_transform_feedback)
176 return &ctx->Query.PrimitivesGenerated;
177 else
178 return NULL;
179 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
180 if (ctx->Extensions.EXT_transform_feedback)
181 return &ctx->Query.PrimitivesWritten;
182 else
183 return NULL;
184 default:
185 return NULL;
186 }
187 }
188
189
190 void GLAPIENTRY
191 _mesa_GenQueries(GLsizei n, GLuint *ids)
192 {
193 GLuint first;
194 GET_CURRENT_CONTEXT(ctx);
195 ASSERT_OUTSIDE_BEGIN_END(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 ASSERT_OUTSIDE_BEGIN_END(ctx);
235 FLUSH_VERTICES(ctx, 0);
236
237 if (MESA_VERBOSE & VERBOSE_API)
238 _mesa_debug(ctx, "glDeleteQueries(%d)\n", n);
239
240 if (n < 0) {
241 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
242 return;
243 }
244
245 /* No query objects can be active at this time! */
246 if (ctx->Query.CurrentOcclusionObject ||
247 ctx->Query.CurrentTimerObject) {
248 _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteQueriesARB");
249 return;
250 }
251
252 for (i = 0; i < n; i++) {
253 if (ids[i] > 0) {
254 struct gl_query_object *q = _mesa_lookup_query_object(ctx, ids[i]);
255 if (q) {
256 ASSERT(!q->Active); /* should be caught earlier */
257 _mesa_HashRemove(ctx->Query.QueryObjects, ids[i]);
258 ctx->Driver.DeleteQuery(ctx, q);
259 }
260 }
261 }
262 }
263
264
265 GLboolean GLAPIENTRY
266 _mesa_IsQuery(GLuint id)
267 {
268 struct gl_query_object *q;
269
270 GET_CURRENT_CONTEXT(ctx);
271 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
272
273 if (MESA_VERBOSE & VERBOSE_API)
274 _mesa_debug(ctx, "glIsQuery(%u)\n", id);
275
276 if (id == 0)
277 return GL_FALSE;
278
279 q = _mesa_lookup_query_object(ctx, id);
280 if (q == NULL)
281 return GL_FALSE;
282
283 return q->EverBound;
284 }
285
286 static GLboolean
287 query_error_check_index(struct gl_context *ctx, GLenum target, GLuint index)
288 {
289 switch (target) {
290 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
291 case GL_PRIMITIVES_GENERATED:
292 if (index >= ctx->Const.MaxVertexStreams) {
293 _mesa_error(ctx, GL_INVALID_VALUE,
294 "glBeginQueryIndexed(index>=MaxVertexStreams)");
295 return GL_FALSE;
296 }
297 break;
298 default:
299 if (index > 0) {
300 _mesa_error(ctx, GL_INVALID_VALUE, "glBeginQueryIndexed(index>0)");
301 return GL_FALSE;
302 }
303 }
304 return GL_TRUE;
305 }
306
307 void GLAPIENTRY
308 _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
309 {
310 struct gl_query_object *q, **bindpt;
311 GET_CURRENT_CONTEXT(ctx);
312 ASSERT_OUTSIDE_BEGIN_END(ctx);
313
314 if (MESA_VERBOSE & VERBOSE_API)
315 _mesa_debug(ctx, "glBeginQueryIndexed(%s, %u, %u)\n",
316 _mesa_lookup_enum_by_nr(target), index, id);
317
318 if (!query_error_check_index(ctx, target, index))
319 return;
320
321 FLUSH_VERTICES(ctx, _NEW_DEPTH);
322
323 bindpt = get_query_binding_point(ctx, target);
324 if (!bindpt) {
325 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQuery{Indexed}(target)");
326 return;
327 }
328
329 /* From the GL_ARB_occlusion_query spec:
330 *
331 * "If BeginQueryARB is called while another query is already in
332 * progress with the same target, an INVALID_OPERATION error is
333 * generated."
334 */
335 if (*bindpt) {
336 _mesa_error(ctx, GL_INVALID_OPERATION,
337 "glBeginQuery{Indexed}(target=%s is active)",
338 _mesa_lookup_enum_by_nr(target));
339 return;
340 }
341
342 if (id == 0) {
343 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQuery{Indexed}(id==0)");
344 return;
345 }
346
347 q = _mesa_lookup_query_object(ctx, id);
348 if (!q) {
349 if (ctx->API != API_OPENGL_COMPAT) {
350 _mesa_error(ctx, GL_INVALID_OPERATION,
351 "glBeginQuery{Indexed}(non-gen name)");
352 return;
353 } else {
354 /* create new object */
355 q = ctx->Driver.NewQueryObject(ctx, id);
356 if (!q) {
357 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQuery{Indexed}");
358 return;
359 }
360 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
361 }
362 }
363 else {
364 /* pre-existing object */
365 if (q->Active) {
366 _mesa_error(ctx, GL_INVALID_OPERATION,
367 "glBeginQuery{Indexed}(query already active)");
368 return;
369 }
370 }
371
372 q->Target = target;
373 q->Active = GL_TRUE;
374 q->Result = 0;
375 q->Ready = GL_FALSE;
376 q->EverBound = GL_TRUE;
377
378 /* XXX should probably refcount query objects */
379 *bindpt = q;
380
381 ctx->Driver.BeginQuery(ctx, q);
382 }
383
384
385 void GLAPIENTRY
386 _mesa_EndQueryIndexed(GLenum target, GLuint index)
387 {
388 struct gl_query_object *q, **bindpt;
389 GET_CURRENT_CONTEXT(ctx);
390 ASSERT_OUTSIDE_BEGIN_END(ctx);
391
392 if (MESA_VERBOSE & VERBOSE_API)
393 _mesa_debug(ctx, "glEndQueryIndexed(%s, %u)\n",
394 _mesa_lookup_enum_by_nr(target), index);
395
396 if (!query_error_check_index(ctx, target, index))
397 return;
398
399 FLUSH_VERTICES(ctx, _NEW_DEPTH);
400
401 bindpt = get_query_binding_point(ctx, target);
402 if (!bindpt) {
403 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQuery{Indexed}(target)");
404 return;
405 }
406
407 /* XXX should probably refcount query objects */
408 q = *bindpt;
409
410 /* Check for GL_ANY_SAMPLES_PASSED vs GL_SAMPLES_PASSED. */
411 if (q && q->Target != target) {
412 _mesa_error(ctx, GL_INVALID_OPERATION,
413 "glEndQuery(target=%s with active query of target %s)",
414 _mesa_lookup_enum_by_nr(target),
415 _mesa_lookup_enum_by_nr(q->Target));
416 return;
417 }
418
419 *bindpt = NULL;
420
421 if (!q || !q->Active) {
422 _mesa_error(ctx, GL_INVALID_OPERATION,
423 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
424 return;
425 }
426
427 q->Active = GL_FALSE;
428 ctx->Driver.EndQuery(ctx, q);
429 }
430
431 void GLAPIENTRY
432 _mesa_BeginQuery(GLenum target, GLuint id)
433 {
434 _mesa_BeginQueryIndexed(target, 0, id);
435 }
436
437 void GLAPIENTRY
438 _mesa_EndQuery(GLenum target)
439 {
440 _mesa_EndQueryIndexed(target, 0);
441 }
442
443 void GLAPIENTRY
444 _mesa_QueryCounter(GLuint id, GLenum target)
445 {
446 struct gl_query_object *q;
447 GET_CURRENT_CONTEXT(ctx);
448 ASSERT_OUTSIDE_BEGIN_END(ctx);
449
450 if (MESA_VERBOSE & VERBOSE_API)
451 _mesa_debug(ctx, "glQueryCounter(%u, %s)\n", id,
452 _mesa_lookup_enum_by_nr(target));
453
454 /* error checking */
455 if (target != GL_TIMESTAMP) {
456 _mesa_error(ctx, GL_INVALID_ENUM, "glQueryCounter(target)");
457 return;
458 }
459
460 if (id == 0) {
461 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id==0)");
462 return;
463 }
464
465 q = _mesa_lookup_query_object(ctx, id);
466 if (!q) {
467 /* XXX the Core profile should throw INVALID_OPERATION here */
468
469 /* create new object */
470 q = ctx->Driver.NewQueryObject(ctx, id);
471 if (!q) {
472 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glQueryCounter");
473 return;
474 }
475 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
476 }
477 else {
478 if (q->Target && q->Target != GL_TIMESTAMP) {
479 _mesa_error(ctx, GL_INVALID_OPERATION,
480 "glQueryCounter(id has an invalid target)");
481 return;
482 }
483 }
484
485 if (q->Active) {
486 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id is active)");
487 return;
488 }
489
490 q->Target = target;
491 q->Result = 0;
492 q->Ready = GL_FALSE;
493
494 /* QueryCounter is implemented using EndQuery without BeginQuery
495 * in drivers. This is actually Direct3D and Gallium convention. */
496 ctx->Driver.EndQuery(ctx, q);
497 }
498
499
500 void GLAPIENTRY
501 _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
502 GLint *params)
503 {
504 struct gl_query_object *q = NULL, **bindpt = NULL;
505 GET_CURRENT_CONTEXT(ctx);
506 ASSERT_OUTSIDE_BEGIN_END(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 ASSERT_OUTSIDE_BEGIN_END(ctx);
587
588 if (MESA_VERBOSE & VERBOSE_API)
589 _mesa_debug(ctx, "glGetQueryObjectiv(%u, %s)\n", id,
590 _mesa_lookup_enum_by_nr(pname));
591
592 if (id)
593 q = _mesa_lookup_query_object(ctx, id);
594
595 if (!q || q->Active) {
596 _mesa_error(ctx, GL_INVALID_OPERATION,
597 "glGetQueryObjectivARB(id=%d is invalid or active)", id);
598 return;
599 }
600
601 switch (pname) {
602 case GL_QUERY_RESULT_ARB:
603 if (!q->Ready)
604 ctx->Driver.WaitQuery(ctx, q);
605 /* if result is too large for returned type, clamp to max value */
606 if (q->Target == GL_ANY_SAMPLES_PASSED
607 || q->Target == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) {
608 if (q->Result)
609 *params = GL_TRUE;
610 else
611 *params = GL_FALSE;
612 } else {
613 if (q->Result > 0x7fffffff) {
614 *params = 0x7fffffff;
615 }
616 else {
617 *params = (GLint)q->Result;
618 }
619 }
620 break;
621 case GL_QUERY_RESULT_AVAILABLE_ARB:
622 if (!q->Ready)
623 ctx->Driver.CheckQuery( ctx, q );
624 *params = q->Ready;
625 break;
626 default:
627 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
628 return;
629 }
630 }
631
632
633 void GLAPIENTRY
634 _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
635 {
636 struct gl_query_object *q = NULL;
637 GET_CURRENT_CONTEXT(ctx);
638 ASSERT_OUTSIDE_BEGIN_END(ctx);
639
640 if (MESA_VERBOSE & VERBOSE_API)
641 _mesa_debug(ctx, "glGetQueryObjectuiv(%u, %s)\n", id,
642 _mesa_lookup_enum_by_nr(pname));
643
644 if (id)
645 q = _mesa_lookup_query_object(ctx, id);
646
647 if (!q || q->Active) {
648 _mesa_error(ctx, GL_INVALID_OPERATION,
649 "glGetQueryObjectuivARB(id=%d is invalid or active)", id);
650 return;
651 }
652
653 switch (pname) {
654 case GL_QUERY_RESULT_ARB:
655 if (!q->Ready)
656 ctx->Driver.WaitQuery(ctx, q);
657 /* if result is too large for returned type, clamp to max value */
658 if (q->Target == GL_ANY_SAMPLES_PASSED
659 || q->Target == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) {
660 if (q->Result)
661 *params = GL_TRUE;
662 else
663 *params = GL_FALSE;
664 } else {
665 if (q->Result > 0xffffffff) {
666 *params = 0xffffffff;
667 }
668 else {
669 *params = (GLuint)q->Result;
670 }
671 }
672 break;
673 case GL_QUERY_RESULT_AVAILABLE_ARB:
674 if (!q->Ready)
675 ctx->Driver.CheckQuery( ctx, q );
676 *params = q->Ready;
677 break;
678 default:
679 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
680 return;
681 }
682 }
683
684
685 /**
686 * New with GL_EXT_timer_query
687 */
688 void GLAPIENTRY
689 _mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
690 {
691 struct gl_query_object *q = NULL;
692 GET_CURRENT_CONTEXT(ctx);
693 ASSERT_OUTSIDE_BEGIN_END(ctx);
694
695 if (MESA_VERBOSE & VERBOSE_API)
696 _mesa_debug(ctx, "glGetQueryObjecti64v(%u, %s)\n", id,
697 _mesa_lookup_enum_by_nr(pname));
698
699 if (id)
700 q = _mesa_lookup_query_object(ctx, id);
701
702 if (!q || q->Active) {
703 _mesa_error(ctx, GL_INVALID_OPERATION,
704 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id);
705 return;
706 }
707
708 switch (pname) {
709 case GL_QUERY_RESULT_ARB:
710 if (!q->Ready)
711 ctx->Driver.WaitQuery(ctx, q);
712 *params = q->Result;
713 break;
714 case GL_QUERY_RESULT_AVAILABLE_ARB:
715 if (!q->Ready)
716 ctx->Driver.CheckQuery( ctx, q );
717 *params = q->Ready;
718 break;
719 default:
720 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjecti64vARB(pname)");
721 return;
722 }
723 }
724
725
726 /**
727 * New with GL_EXT_timer_query
728 */
729 void GLAPIENTRY
730 _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
731 {
732 struct gl_query_object *q = NULL;
733 GET_CURRENT_CONTEXT(ctx);
734 ASSERT_OUTSIDE_BEGIN_END(ctx);
735
736 if (MESA_VERBOSE & VERBOSE_API)
737 _mesa_debug(ctx, "glGetQueryObjectui64v(%u, %s)\n", id,
738 _mesa_lookup_enum_by_nr(pname));
739
740 if (id)
741 q = _mesa_lookup_query_object(ctx, id);
742
743 if (!q || q->Active) {
744 _mesa_error(ctx, GL_INVALID_OPERATION,
745 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id);
746 return;
747 }
748
749 switch (pname) {
750 case GL_QUERY_RESULT_ARB:
751 if (!q->Ready)
752 ctx->Driver.WaitQuery(ctx, q);
753 *params = q->Result;
754 break;
755 case GL_QUERY_RESULT_AVAILABLE_ARB:
756 if (!q->Ready)
757 ctx->Driver.CheckQuery( ctx, q );
758 *params = q->Ready;
759 break;
760 default:
761 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectui64vARB(pname)");
762 return;
763 }
764 }
765
766
767 void
768 _mesa_init_queryobj_dispatch(const struct gl_context *ctx,
769 struct _glapi_table *disp)
770 {
771 SET_GenQueries(disp, _mesa_GenQueries);
772 SET_DeleteQueries(disp, _mesa_DeleteQueries);
773 SET_IsQuery(disp, _mesa_IsQuery);
774 SET_BeginQuery(disp, _mesa_BeginQuery);
775 SET_EndQuery(disp, _mesa_EndQuery);
776 SET_GetQueryiv(disp, _mesa_GetQueryiv);
777 SET_GetQueryObjectuiv(disp, _mesa_GetQueryObjectuiv);
778
779 if (_mesa_is_desktop_gl(ctx)) {
780 SET_GetQueryObjectiv(disp, _mesa_GetQueryObjectiv);
781 SET_QueryCounter(disp, _mesa_QueryCounter);
782
783 SET_GetQueryObjecti64v(disp, _mesa_GetQueryObjecti64v);
784 SET_GetQueryObjectui64v(disp, _mesa_GetQueryObjectui64v);
785
786 SET_BeginQueryIndexed(disp, _mesa_BeginQueryIndexed);
787 SET_EndQueryIndexed(disp, _mesa_EndQueryIndexed);
788 SET_GetQueryIndexediv(disp, _mesa_GetQueryIndexediv);
789 }
790 }
791
792
793 /**
794 * Allocate/init the context state related to query objects.
795 */
796 void
797 _mesa_init_queryobj(struct gl_context *ctx)
798 {
799 ctx->Query.QueryObjects = _mesa_NewHashTable();
800 ctx->Query.CurrentOcclusionObject = NULL;
801
802 ctx->Const.QueryCounterBits.SamplesPassed = 64;
803 ctx->Const.QueryCounterBits.TimeElapsed = 64;
804 ctx->Const.QueryCounterBits.Timestamp = 64;
805 ctx->Const.QueryCounterBits.PrimitivesGenerated = 64;
806 ctx->Const.QueryCounterBits.PrimitivesWritten = 64;
807 }
808
809
810 /**
811 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
812 */
813 static void
814 delete_queryobj_cb(GLuint id, void *data, void *userData)
815 {
816 struct gl_query_object *q= (struct gl_query_object *) data;
817 struct gl_context *ctx = (struct gl_context *)userData;
818 ctx->Driver.DeleteQuery(ctx, q);
819 }
820
821
822 /**
823 * Free the context state related to query objects.
824 */
825 void
826 _mesa_free_queryobj_data(struct gl_context *ctx)
827 {
828 _mesa_HashDeleteAll(ctx->Query.QueryObjects, delete_queryobj_cb, ctx);
829 _mesa_DeleteHashTable(ctx->Query.QueryObjects);
830 }