mesa: move declaration before code
[mesa.git] / src / mesa / main / queryobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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 "mtypes.h"
33 #include "main/dispatch.h"
34
35
36 /**
37 * Allocate a new query object. This is a fallback routine called via
38 * ctx->Driver.NewQueryObject().
39 * \param ctx - rendering context
40 * \param id - the new object's ID
41 * \return pointer to new query_object object or NULL if out of memory.
42 */
43 static struct gl_query_object *
44 _mesa_new_query_object(struct gl_context *ctx, GLuint id)
45 {
46 struct gl_query_object *q = CALLOC_STRUCT(gl_query_object);
47 (void) ctx;
48 if (q) {
49 q->Id = id;
50 q->Result = 0;
51 q->Active = GL_FALSE;
52
53 /* This is to satisfy the language of the specification: "In the initial
54 * state of a query object, the result is available" (OpenGL 3.1 §
55 * 2.13).
56 */
57 q->Ready = GL_TRUE;
58
59 /* OpenGL 3.1 § 2.13 says about GenQueries, "These names are marked as
60 * used, but no object is associated with them until the first time they
61 * are used by BeginQuery." Since our implementation actually does
62 * allocate an object at this point, use a flag to indicate that this
63 * object has not yet been bound so should not be considered a query.
64 */
65 q->EverBound = GL_FALSE;
66 }
67 return q;
68 }
69
70
71 /**
72 * Begin a query. Software driver fallback.
73 * Called via ctx->Driver.BeginQuery().
74 */
75 static void
76 _mesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
77 {
78 ctx->NewState |= _NEW_DEPTH; /* for swrast */
79 }
80
81
82 /**
83 * End a query. Software driver fallback.
84 * Called via ctx->Driver.EndQuery().
85 */
86 static void
87 _mesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
88 {
89 ctx->NewState |= _NEW_DEPTH; /* for swrast */
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->Label);
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 static struct gl_query_object **
146 get_pipe_stats_binding_point(struct gl_context *ctx,
147 GLenum target)
148 {
149 const int which = target - GL_VERTICES_SUBMITTED_ARB;
150 assert(which < MAX_PIPELINE_STATISTICS);
151
152 if (!_mesa_is_desktop_gl(ctx) ||
153 !ctx->Extensions.ARB_pipeline_statistics_query)
154 return NULL;
155
156 return &ctx->Query.pipeline_stats[which];
157 }
158
159 /**
160 * Return pointer to the query object binding point for the given target and
161 * index.
162 * \return NULL if invalid target, else the address of binding point
163 */
164 static struct gl_query_object **
165 get_query_binding_point(struct gl_context *ctx, GLenum target, GLuint index)
166 {
167 switch (target) {
168 case GL_SAMPLES_PASSED_ARB:
169 if (ctx->Extensions.ARB_occlusion_query)
170 return &ctx->Query.CurrentOcclusionObject;
171 else
172 return NULL;
173 case GL_ANY_SAMPLES_PASSED:
174 if (ctx->Extensions.ARB_occlusion_query2)
175 return &ctx->Query.CurrentOcclusionObject;
176 else
177 return NULL;
178 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
179 if (ctx->Extensions.ARB_ES3_compatibility
180 || (ctx->API == API_OPENGLES2 && ctx->Version >= 30))
181 return &ctx->Query.CurrentOcclusionObject;
182 else
183 return NULL;
184 case GL_TIME_ELAPSED_EXT:
185 if (ctx->Extensions.EXT_timer_query)
186 return &ctx->Query.CurrentTimerObject;
187 else
188 return NULL;
189 case GL_PRIMITIVES_GENERATED:
190 if (ctx->Extensions.EXT_transform_feedback)
191 return &ctx->Query.PrimitivesGenerated[index];
192 else
193 return NULL;
194 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
195 if (ctx->Extensions.EXT_transform_feedback)
196 return &ctx->Query.PrimitivesWritten[index];
197 else
198 return NULL;
199
200 case GL_VERTICES_SUBMITTED_ARB:
201 case GL_PRIMITIVES_SUBMITTED_ARB:
202 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
203 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
204 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
205 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
206 return get_pipe_stats_binding_point(ctx, target);
207
208 case GL_GEOMETRY_SHADER_INVOCATIONS:
209 /* GL_GEOMETRY_SHADER_INVOCATIONS is defined in a non-sequential order */
210 target = GL_VERTICES_SUBMITTED_ARB + MAX_PIPELINE_STATISTICS - 1;
211 /* fallthrough */
212 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
213 if (_mesa_has_geometry_shaders(ctx))
214 return get_pipe_stats_binding_point(ctx, target);
215 else
216 return NULL;
217
218 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
219 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
220 if (ctx->Extensions.ARB_tessellation_shader)
221 return get_pipe_stats_binding_point(ctx, target);
222 else
223 return NULL;
224
225 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
226 if (_mesa_has_compute_shaders(ctx))
227 return get_pipe_stats_binding_point(ctx, target);
228 else
229 return NULL;
230
231 default:
232 return NULL;
233 }
234 }
235
236
237 void GLAPIENTRY
238 _mesa_GenQueries(GLsizei n, GLuint *ids)
239 {
240 GLuint first;
241 GET_CURRENT_CONTEXT(ctx);
242
243 if (MESA_VERBOSE & VERBOSE_API)
244 _mesa_debug(ctx, "glGenQueries(%d)\n", n);
245
246 if (n < 0) {
247 _mesa_error(ctx, GL_INVALID_VALUE, "glGenQueriesARB(n < 0)");
248 return;
249 }
250
251 first = _mesa_HashFindFreeKeyBlock(ctx->Query.QueryObjects, n);
252 if (first) {
253 GLsizei i;
254 for (i = 0; i < n; i++) {
255 struct gl_query_object *q
256 = ctx->Driver.NewQueryObject(ctx, first + i);
257 if (!q) {
258 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenQueriesARB");
259 return;
260 }
261 ids[i] = first + i;
262 _mesa_HashInsert(ctx->Query.QueryObjects, first + i, q);
263 }
264 }
265 }
266
267
268 void GLAPIENTRY
269 _mesa_DeleteQueries(GLsizei n, const GLuint *ids)
270 {
271 GLint i;
272 GET_CURRENT_CONTEXT(ctx);
273 FLUSH_VERTICES(ctx, 0);
274
275 if (MESA_VERBOSE & VERBOSE_API)
276 _mesa_debug(ctx, "glDeleteQueries(%d)\n", n);
277
278 if (n < 0) {
279 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
280 return;
281 }
282
283 for (i = 0; i < n; i++) {
284 if (ids[i] > 0) {
285 struct gl_query_object *q = _mesa_lookup_query_object(ctx, ids[i]);
286 if (q) {
287 if (q->Active) {
288 struct gl_query_object **bindpt;
289 bindpt = get_query_binding_point(ctx, q->Target, q->Stream);
290 assert(bindpt); /* Should be non-null for active q. */
291 if (bindpt) {
292 *bindpt = NULL;
293 }
294 q->Active = GL_FALSE;
295 ctx->Driver.EndQuery(ctx, q);
296 }
297 _mesa_HashRemove(ctx->Query.QueryObjects, ids[i]);
298 ctx->Driver.DeleteQuery(ctx, q);
299 }
300 }
301 }
302 }
303
304
305 GLboolean GLAPIENTRY
306 _mesa_IsQuery(GLuint id)
307 {
308 struct gl_query_object *q;
309
310 GET_CURRENT_CONTEXT(ctx);
311 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
312
313 if (MESA_VERBOSE & VERBOSE_API)
314 _mesa_debug(ctx, "glIsQuery(%u)\n", id);
315
316 if (id == 0)
317 return GL_FALSE;
318
319 q = _mesa_lookup_query_object(ctx, id);
320 if (q == NULL)
321 return GL_FALSE;
322
323 return q->EverBound;
324 }
325
326 static GLboolean
327 query_error_check_index(struct gl_context *ctx, GLenum target, GLuint index)
328 {
329 switch (target) {
330 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
331 case GL_PRIMITIVES_GENERATED:
332 if (index >= ctx->Const.MaxVertexStreams) {
333 _mesa_error(ctx, GL_INVALID_VALUE,
334 "glBeginQueryIndexed(index>=MaxVertexStreams)");
335 return GL_FALSE;
336 }
337 break;
338 default:
339 if (index > 0) {
340 _mesa_error(ctx, GL_INVALID_VALUE, "glBeginQueryIndexed(index>0)");
341 return GL_FALSE;
342 }
343 }
344 return GL_TRUE;
345 }
346
347 void GLAPIENTRY
348 _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
349 {
350 struct gl_query_object *q, **bindpt;
351 GET_CURRENT_CONTEXT(ctx);
352
353 if (MESA_VERBOSE & VERBOSE_API)
354 _mesa_debug(ctx, "glBeginQueryIndexed(%s, %u, %u)\n",
355 _mesa_lookup_enum_by_nr(target), index, id);
356
357 if (!query_error_check_index(ctx, target, index))
358 return;
359
360 FLUSH_VERTICES(ctx, 0);
361
362 bindpt = get_query_binding_point(ctx, target, index);
363 if (!bindpt) {
364 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQuery{Indexed}(target)");
365 return;
366 }
367
368 /* From the GL_ARB_occlusion_query spec:
369 *
370 * "If BeginQueryARB is called while another query is already in
371 * progress with the same target, an INVALID_OPERATION error is
372 * generated."
373 */
374 if (*bindpt) {
375 _mesa_error(ctx, GL_INVALID_OPERATION,
376 "glBeginQuery{Indexed}(target=%s is active)",
377 _mesa_lookup_enum_by_nr(target));
378 return;
379 }
380
381 if (id == 0) {
382 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQuery{Indexed}(id==0)");
383 return;
384 }
385
386 q = _mesa_lookup_query_object(ctx, id);
387 if (!q) {
388 if (ctx->API != API_OPENGL_COMPAT) {
389 _mesa_error(ctx, GL_INVALID_OPERATION,
390 "glBeginQuery{Indexed}(non-gen name)");
391 return;
392 } else {
393 /* create new object */
394 q = ctx->Driver.NewQueryObject(ctx, id);
395 if (!q) {
396 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQuery{Indexed}");
397 return;
398 }
399 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
400 }
401 }
402 else {
403 /* pre-existing object */
404 if (q->Active) {
405 _mesa_error(ctx, GL_INVALID_OPERATION,
406 "glBeginQuery{Indexed}(query already active)");
407 return;
408 }
409
410 /* Section 2.14 Asynchronous Queries, page 84 of the OpenGL ES 3.0.4
411 * spec states:
412 *
413 * "BeginQuery generates an INVALID_OPERATION error if any of the
414 * following conditions hold: [...] id is the name of an
415 * existing query object whose type does not match target; [...]
416 *
417 * Similar wording exists in the OpenGL 4.5 spec, section 4.2. QUERY
418 * OBJECTS AND ASYNCHRONOUS QUERIES, page 43.
419 */
420 if (q->EverBound && q->Target != target) {
421 _mesa_error(ctx, GL_INVALID_OPERATION,
422 "glBeginQuery{Indexed}(target mismatch)");
423 return;
424 }
425 }
426
427 q->Target = target;
428 q->Active = GL_TRUE;
429 q->Result = 0;
430 q->Ready = GL_FALSE;
431 q->EverBound = GL_TRUE;
432 q->Stream = index;
433
434 /* XXX should probably refcount query objects */
435 *bindpt = q;
436
437 ctx->Driver.BeginQuery(ctx, q);
438 }
439
440
441 void GLAPIENTRY
442 _mesa_EndQueryIndexed(GLenum target, GLuint index)
443 {
444 struct gl_query_object *q, **bindpt;
445 GET_CURRENT_CONTEXT(ctx);
446
447 if (MESA_VERBOSE & VERBOSE_API)
448 _mesa_debug(ctx, "glEndQueryIndexed(%s, %u)\n",
449 _mesa_lookup_enum_by_nr(target), index);
450
451 if (!query_error_check_index(ctx, target, index))
452 return;
453
454 FLUSH_VERTICES(ctx, 0);
455
456 bindpt = get_query_binding_point(ctx, target, index);
457 if (!bindpt) {
458 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQuery{Indexed}(target)");
459 return;
460 }
461
462 /* XXX should probably refcount query objects */
463 q = *bindpt;
464
465 /* Check for GL_ANY_SAMPLES_PASSED vs GL_SAMPLES_PASSED. */
466 if (q && q->Target != target) {
467 _mesa_error(ctx, GL_INVALID_OPERATION,
468 "glEndQuery(target=%s with active query of target %s)",
469 _mesa_lookup_enum_by_nr(target),
470 _mesa_lookup_enum_by_nr(q->Target));
471 return;
472 }
473
474 *bindpt = NULL;
475
476 if (!q || !q->Active) {
477 _mesa_error(ctx, GL_INVALID_OPERATION,
478 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
479 return;
480 }
481
482 q->Active = GL_FALSE;
483 ctx->Driver.EndQuery(ctx, q);
484 }
485
486 void GLAPIENTRY
487 _mesa_BeginQuery(GLenum target, GLuint id)
488 {
489 _mesa_BeginQueryIndexed(target, 0, id);
490 }
491
492 void GLAPIENTRY
493 _mesa_EndQuery(GLenum target)
494 {
495 _mesa_EndQueryIndexed(target, 0);
496 }
497
498 void GLAPIENTRY
499 _mesa_QueryCounter(GLuint id, GLenum target)
500 {
501 struct gl_query_object *q;
502 GET_CURRENT_CONTEXT(ctx);
503
504 if (MESA_VERBOSE & VERBOSE_API)
505 _mesa_debug(ctx, "glQueryCounter(%u, %s)\n", id,
506 _mesa_lookup_enum_by_nr(target));
507
508 /* error checking */
509 if (target != GL_TIMESTAMP) {
510 _mesa_error(ctx, GL_INVALID_ENUM, "glQueryCounter(target)");
511 return;
512 }
513
514 if (id == 0) {
515 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id==0)");
516 return;
517 }
518
519 q = _mesa_lookup_query_object(ctx, id);
520 if (!q) {
521 /* XXX the Core profile should throw INVALID_OPERATION here */
522
523 /* create new object */
524 q = ctx->Driver.NewQueryObject(ctx, id);
525 if (!q) {
526 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glQueryCounter");
527 return;
528 }
529 _mesa_HashInsert(ctx->Query.QueryObjects, id, q);
530 }
531 else {
532 if (q->Target && q->Target != GL_TIMESTAMP) {
533 _mesa_error(ctx, GL_INVALID_OPERATION,
534 "glQueryCounter(id has an invalid target)");
535 return;
536 }
537 }
538
539 if (q->Active) {
540 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id is active)");
541 return;
542 }
543
544 q->Target = target;
545 q->Result = 0;
546 q->Ready = GL_FALSE;
547 q->EverBound = GL_TRUE;
548
549 if (ctx->Driver.QueryCounter) {
550 ctx->Driver.QueryCounter(ctx, q);
551 } else {
552 /* QueryCounter is implemented using EndQuery without BeginQuery
553 * in drivers. This is actually Direct3D and Gallium convention.
554 */
555 ctx->Driver.EndQuery(ctx, q);
556 }
557 }
558
559
560 void GLAPIENTRY
561 _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
562 GLint *params)
563 {
564 struct gl_query_object *q = NULL, **bindpt = NULL;
565 GET_CURRENT_CONTEXT(ctx);
566
567 if (MESA_VERBOSE & VERBOSE_API)
568 _mesa_debug(ctx, "glGetQueryIndexediv(%s, %u, %s)\n",
569 _mesa_lookup_enum_by_nr(target),
570 index,
571 _mesa_lookup_enum_by_nr(pname));
572
573 if (!query_error_check_index(ctx, target, index))
574 return;
575
576 if (target == GL_TIMESTAMP) {
577 if (!ctx->Extensions.ARB_timer_query) {
578 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryARB(target)");
579 return;
580 }
581 }
582 else {
583 bindpt = get_query_binding_point(ctx, target, index);
584 if (!bindpt) {
585 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(target)");
586 return;
587 }
588
589 q = *bindpt;
590 }
591
592 switch (pname) {
593 case GL_QUERY_COUNTER_BITS_ARB:
594 switch (target) {
595 case GL_SAMPLES_PASSED:
596 *params = ctx->Const.QueryCounterBits.SamplesPassed;
597 break;
598 case GL_ANY_SAMPLES_PASSED:
599 /* The minimum value of this is 1 if it's nonzero, and the value
600 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
601 * bits.
602 */
603 *params = 1;
604 break;
605 case GL_TIME_ELAPSED:
606 *params = ctx->Const.QueryCounterBits.TimeElapsed;
607 break;
608 case GL_TIMESTAMP:
609 *params = ctx->Const.QueryCounterBits.Timestamp;
610 break;
611 case GL_PRIMITIVES_GENERATED:
612 *params = ctx->Const.QueryCounterBits.PrimitivesGenerated;
613 break;
614 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
615 *params = ctx->Const.QueryCounterBits.PrimitivesWritten;
616 break;
617 case GL_VERTICES_SUBMITTED_ARB:
618 *params = ctx->Const.QueryCounterBits.VerticesSubmitted;
619 break;
620 case GL_PRIMITIVES_SUBMITTED_ARB:
621 *params = ctx->Const.QueryCounterBits.PrimitivesSubmitted;
622 break;
623 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
624 *params = ctx->Const.QueryCounterBits.VsInvocations;
625 break;
626 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
627 *params = ctx->Const.QueryCounterBits.TessPatches;
628 break;
629 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
630 *params = ctx->Const.QueryCounterBits.TessInvocations;
631 break;
632 case GL_GEOMETRY_SHADER_INVOCATIONS:
633 *params = ctx->Const.QueryCounterBits.GsInvocations;
634 break;
635 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
636 *params = ctx->Const.QueryCounterBits.GsPrimitives;
637 break;
638 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
639 *params = ctx->Const.QueryCounterBits.FsInvocations;
640 break;
641 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
642 *params = ctx->Const.QueryCounterBits.ComputeInvocations;
643 break;
644 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
645 *params = ctx->Const.QueryCounterBits.ClInPrimitives;
646 break;
647 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
648 *params = ctx->Const.QueryCounterBits.ClOutPrimitives;
649 break;
650 default:
651 _mesa_problem(ctx,
652 "Unknown target in glGetQueryIndexediv(target = %s)",
653 _mesa_lookup_enum_by_nr(target));
654 *params = 0;
655 break;
656 }
657 break;
658 case GL_CURRENT_QUERY_ARB:
659 *params = (q && q->Target == target) ? q->Id : 0;
660 break;
661 default:
662 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(pname)");
663 return;
664 }
665 }
666
667 void GLAPIENTRY
668 _mesa_GetQueryiv(GLenum target, GLenum pname, GLint *params)
669 {
670 _mesa_GetQueryIndexediv(target, 0, pname, params);
671 }
672
673 void GLAPIENTRY
674 _mesa_GetQueryObjectiv(GLuint id, GLenum pname, GLint *params)
675 {
676 struct gl_query_object *q = NULL;
677 GET_CURRENT_CONTEXT(ctx);
678
679 if (MESA_VERBOSE & VERBOSE_API)
680 _mesa_debug(ctx, "glGetQueryObjectiv(%u, %s)\n", id,
681 _mesa_lookup_enum_by_nr(pname));
682
683 if (id)
684 q = _mesa_lookup_query_object(ctx, id);
685
686 if (!q || q->Active || !q->EverBound) {
687 _mesa_error(ctx, GL_INVALID_OPERATION,
688 "glGetQueryObjectivARB(id=%d is invalid or active)", id);
689 return;
690 }
691
692 switch (pname) {
693 case GL_QUERY_RESULT_ARB:
694 if (!q->Ready)
695 ctx->Driver.WaitQuery(ctx, q);
696 /* if result is too large for returned type, clamp to max value */
697 if (q->Target == GL_ANY_SAMPLES_PASSED
698 || q->Target == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) {
699 if (q->Result)
700 *params = GL_TRUE;
701 else
702 *params = GL_FALSE;
703 } else {
704 if (q->Result > 0x7fffffff) {
705 *params = 0x7fffffff;
706 }
707 else {
708 *params = (GLint)q->Result;
709 }
710 }
711 break;
712 case GL_QUERY_RESULT_AVAILABLE_ARB:
713 if (!q->Ready)
714 ctx->Driver.CheckQuery( ctx, q );
715 *params = q->Ready;
716 break;
717 default:
718 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectivARB(pname)");
719 return;
720 }
721 }
722
723
724 void GLAPIENTRY
725 _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
726 {
727 struct gl_query_object *q = NULL;
728 GET_CURRENT_CONTEXT(ctx);
729
730 if (MESA_VERBOSE & VERBOSE_API)
731 _mesa_debug(ctx, "glGetQueryObjectuiv(%u, %s)\n", id,
732 _mesa_lookup_enum_by_nr(pname));
733
734 if (id)
735 q = _mesa_lookup_query_object(ctx, id);
736
737 if (!q || q->Active || !q->EverBound) {
738 _mesa_error(ctx, GL_INVALID_OPERATION,
739 "glGetQueryObjectuivARB(id=%d is invalid or active)", id);
740 return;
741 }
742
743 switch (pname) {
744 case GL_QUERY_RESULT_ARB:
745 if (!q->Ready)
746 ctx->Driver.WaitQuery(ctx, q);
747 /* if result is too large for returned type, clamp to max value */
748 if (q->Target == GL_ANY_SAMPLES_PASSED
749 || q->Target == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) {
750 if (q->Result)
751 *params = GL_TRUE;
752 else
753 *params = GL_FALSE;
754 } else {
755 if (q->Result > 0xffffffff) {
756 *params = 0xffffffff;
757 }
758 else {
759 *params = (GLuint)q->Result;
760 }
761 }
762 break;
763 case GL_QUERY_RESULT_AVAILABLE_ARB:
764 if (!q->Ready)
765 ctx->Driver.CheckQuery( ctx, q );
766 *params = q->Ready;
767 break;
768 default:
769 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectuivARB(pname)");
770 return;
771 }
772 }
773
774
775 /**
776 * New with GL_EXT_timer_query
777 */
778 void GLAPIENTRY
779 _mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
780 {
781 struct gl_query_object *q = NULL;
782 GET_CURRENT_CONTEXT(ctx);
783
784 if (MESA_VERBOSE & VERBOSE_API)
785 _mesa_debug(ctx, "glGetQueryObjecti64v(%u, %s)\n", id,
786 _mesa_lookup_enum_by_nr(pname));
787
788 if (id)
789 q = _mesa_lookup_query_object(ctx, id);
790
791 if (!q || q->Active || !q->EverBound) {
792 _mesa_error(ctx, GL_INVALID_OPERATION,
793 "glGetQueryObjectui64vARB(id=%d is invalid or active)", id);
794 return;
795 }
796
797 switch (pname) {
798 case GL_QUERY_RESULT_ARB:
799 if (!q->Ready)
800 ctx->Driver.WaitQuery(ctx, q);
801 *params = q->Result;
802 break;
803 case GL_QUERY_RESULT_AVAILABLE_ARB:
804 if (!q->Ready)
805 ctx->Driver.CheckQuery( ctx, q );
806 *params = q->Ready;
807 break;
808 default:
809 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjecti64vARB(pname)");
810 return;
811 }
812 }
813
814
815 /**
816 * New with GL_EXT_timer_query
817 */
818 void GLAPIENTRY
819 _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
820 {
821 struct gl_query_object *q = NULL;
822 GET_CURRENT_CONTEXT(ctx);
823
824 if (MESA_VERBOSE & VERBOSE_API)
825 _mesa_debug(ctx, "glGetQueryObjectui64v(%u, %s)\n", id,
826 _mesa_lookup_enum_by_nr(pname));
827
828 if (id)
829 q = _mesa_lookup_query_object(ctx, id);
830
831 if (!q || q->Active || !q->EverBound) {
832 _mesa_error(ctx, GL_INVALID_OPERATION,
833 "glGetQueryObjectuui64vARB(id=%d is invalid or active)", id);
834 return;
835 }
836
837 switch (pname) {
838 case GL_QUERY_RESULT_ARB:
839 if (!q->Ready)
840 ctx->Driver.WaitQuery(ctx, q);
841 *params = q->Result;
842 break;
843 case GL_QUERY_RESULT_AVAILABLE_ARB:
844 if (!q->Ready)
845 ctx->Driver.CheckQuery( ctx, q );
846 *params = q->Ready;
847 break;
848 default:
849 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryObjectui64vARB(pname)");
850 return;
851 }
852 }
853
854 /**
855 * Allocate/init the context state related to query objects.
856 */
857 void
858 _mesa_init_queryobj(struct gl_context *ctx)
859 {
860 ctx->Query.QueryObjects = _mesa_NewHashTable();
861 ctx->Query.CurrentOcclusionObject = NULL;
862
863 ctx->Const.QueryCounterBits.SamplesPassed = 64;
864 ctx->Const.QueryCounterBits.TimeElapsed = 64;
865 ctx->Const.QueryCounterBits.Timestamp = 64;
866 ctx->Const.QueryCounterBits.PrimitivesGenerated = 64;
867 ctx->Const.QueryCounterBits.PrimitivesWritten = 64;
868
869 ctx->Const.QueryCounterBits.VerticesSubmitted = 64;
870 ctx->Const.QueryCounterBits.PrimitivesSubmitted = 64;
871 ctx->Const.QueryCounterBits.VsInvocations = 64;
872 ctx->Const.QueryCounterBits.TessPatches = 64;
873 ctx->Const.QueryCounterBits.TessInvocations = 64;
874 ctx->Const.QueryCounterBits.GsInvocations = 64;
875 ctx->Const.QueryCounterBits.GsPrimitives = 64;
876 ctx->Const.QueryCounterBits.FsInvocations = 64;
877 ctx->Const.QueryCounterBits.ComputeInvocations = 64;
878 ctx->Const.QueryCounterBits.ClInPrimitives = 64;
879 ctx->Const.QueryCounterBits.ClOutPrimitives = 64;
880 }
881
882
883 /**
884 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
885 */
886 static void
887 delete_queryobj_cb(GLuint id, void *data, void *userData)
888 {
889 struct gl_query_object *q= (struct gl_query_object *) data;
890 struct gl_context *ctx = (struct gl_context *)userData;
891 ctx->Driver.DeleteQuery(ctx, q);
892 }
893
894
895 /**
896 * Free the context state related to query objects.
897 */
898 void
899 _mesa_free_queryobj_data(struct gl_context *ctx)
900 {
901 _mesa_HashDeleteAll(ctx->Query.QueryObjects, delete_queryobj_cb, ctx);
902 _mesa_DeleteHashTable(ctx->Query.QueryObjects);
903 }