af558f0e227d8228c11c7614f7f1f32f462b2928
[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 "bufferobj.h"
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
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;
150 assert(which < MAX_PIPELINE_STATISTICS);
151
152 if (!_mesa_has_ARB_pipeline_statistics_query(ctx))
153 return NULL;
154
155 return &ctx->Query.pipeline_stats[which];
156 }
157
158 /**
159 * Return pointer to the query object binding point for the given target and
160 * index.
161 * \return NULL if invalid target, else the address of binding point
162 */
163 static struct gl_query_object **
164 get_query_binding_point(struct gl_context *ctx, GLenum target, GLuint index)
165 {
166 switch (target) {
167 case GL_SAMPLES_PASSED:
168 if (_mesa_has_ARB_occlusion_query(ctx) ||
169 _mesa_has_ARB_occlusion_query2(ctx))
170 return &ctx->Query.CurrentOcclusionObject;
171 else
172 return NULL;
173 case GL_ANY_SAMPLES_PASSED:
174 if (_mesa_has_ARB_occlusion_query2(ctx) ||
175 _mesa_has_EXT_occlusion_query_boolean(ctx))
176 return &ctx->Query.CurrentOcclusionObject;
177 else
178 return NULL;
179 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
180 if (_mesa_has_ARB_ES3_compatibility(ctx) ||
181 _mesa_has_EXT_occlusion_query_boolean(ctx))
182 return &ctx->Query.CurrentOcclusionObject;
183 else
184 return NULL;
185 case GL_TIME_ELAPSED:
186 if (_mesa_has_EXT_timer_query(ctx) ||
187 _mesa_has_EXT_disjoint_timer_query(ctx))
188 return &ctx->Query.CurrentTimerObject;
189 else
190 return NULL;
191 case GL_PRIMITIVES_GENERATED:
192 if (_mesa_has_EXT_transform_feedback(ctx) ||
193 _mesa_has_OES_geometry_shader(ctx))
194 return &ctx->Query.PrimitivesGenerated[index];
195 else
196 return NULL;
197 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
198 if (_mesa_has_EXT_transform_feedback(ctx) || _mesa_is_gles3(ctx))
199 return &ctx->Query.PrimitivesWritten[index];
200 else
201 return NULL;
202 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW:
203 if (_mesa_has_ARB_transform_feedback_overflow_query(ctx))
204 return &ctx->Query.TransformFeedbackOverflow[index];
205 else
206 return NULL;
207 case GL_TRANSFORM_FEEDBACK_OVERFLOW:
208 if (_mesa_has_ARB_transform_feedback_overflow_query(ctx))
209 return &ctx->Query.TransformFeedbackOverflowAny;
210 else
211 return NULL;
212
213 case GL_VERTICES_SUBMITTED:
214 case GL_PRIMITIVES_SUBMITTED:
215 case GL_VERTEX_SHADER_INVOCATIONS:
216 case GL_FRAGMENT_SHADER_INVOCATIONS:
217 case GL_CLIPPING_INPUT_PRIMITIVES:
218 case GL_CLIPPING_OUTPUT_PRIMITIVES:
219 return get_pipe_stats_binding_point(ctx, target);
220
221 case GL_GEOMETRY_SHADER_INVOCATIONS:
222 /* GL_GEOMETRY_SHADER_INVOCATIONS is defined in a non-sequential order */
223 target = GL_VERTICES_SUBMITTED + MAX_PIPELINE_STATISTICS - 1;
224 /* fallthrough */
225 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED:
226 if (_mesa_has_geometry_shaders(ctx))
227 return get_pipe_stats_binding_point(ctx, target);
228 else
229 return NULL;
230
231 case GL_TESS_CONTROL_SHADER_PATCHES:
232 case GL_TESS_EVALUATION_SHADER_INVOCATIONS:
233 if (_mesa_has_tessellation(ctx))
234 return get_pipe_stats_binding_point(ctx, target);
235 else
236 return NULL;
237
238 case GL_COMPUTE_SHADER_INVOCATIONS:
239 if (_mesa_has_compute_shaders(ctx))
240 return get_pipe_stats_binding_point(ctx, target);
241 else
242 return NULL;
243
244 default:
245 return NULL;
246 }
247 }
248
249 /**
250 * Create $n query objects and store them in *ids. Make them of type $target
251 * if dsa is set. Called from _mesa_GenQueries() and _mesa_CreateQueries().
252 */
253 static void
254 create_queries(struct gl_context *ctx, GLenum target, GLsizei n, GLuint *ids,
255 bool dsa)
256 {
257 const char *func = dsa ? "glGenQueries" : "glCreateQueries";
258 GLuint first;
259
260 if (MESA_VERBOSE & VERBOSE_API)
261 _mesa_debug(ctx, "%s(%d)\n", func, n);
262
263 if (n < 0) {
264 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
265 return;
266 }
267
268 first = _mesa_HashFindFreeKeyBlock(ctx->Query.QueryObjects, n);
269 if (first) {
270 GLsizei i;
271 for (i = 0; i < n; i++) {
272 struct gl_query_object *q
273 = ctx->Driver.NewQueryObject(ctx, first + i);
274 if (!q) {
275 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
276 return;
277 } else if (dsa) {
278 /* Do the equivalent of binding the buffer with a target */
279 q->Target = target;
280 q->EverBound = GL_TRUE;
281 }
282 ids[i] = first + i;
283 _mesa_HashInsertLocked(ctx->Query.QueryObjects, first + i, q);
284 }
285 }
286 }
287
288 void GLAPIENTRY
289 _mesa_GenQueries(GLsizei n, GLuint *ids)
290 {
291 GET_CURRENT_CONTEXT(ctx);
292 create_queries(ctx, 0, n, ids, false);
293 }
294
295 void GLAPIENTRY
296 _mesa_CreateQueries(GLenum target, GLsizei n, GLuint *ids)
297 {
298 GET_CURRENT_CONTEXT(ctx);
299
300 switch (target) {
301 case GL_SAMPLES_PASSED:
302 case GL_ANY_SAMPLES_PASSED:
303 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
304 case GL_TIME_ELAPSED:
305 case GL_TIMESTAMP:
306 case GL_PRIMITIVES_GENERATED:
307 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
308 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW:
309 case GL_TRANSFORM_FEEDBACK_OVERFLOW:
310 break;
311 default:
312 _mesa_error(ctx, GL_INVALID_ENUM, "glCreateQueries(invalid target = %s)",
313 _mesa_enum_to_string(target));
314 return;
315 }
316
317 create_queries(ctx, target, n, ids, true);
318 }
319
320
321 void GLAPIENTRY
322 _mesa_DeleteQueries(GLsizei n, const GLuint *ids)
323 {
324 GLint i;
325 GET_CURRENT_CONTEXT(ctx);
326 FLUSH_VERTICES(ctx, 0);
327
328 if (MESA_VERBOSE & VERBOSE_API)
329 _mesa_debug(ctx, "glDeleteQueries(%d)\n", n);
330
331 if (n < 0) {
332 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteQueriesARB(n < 0)");
333 return;
334 }
335
336 for (i = 0; i < n; i++) {
337 if (ids[i] > 0) {
338 struct gl_query_object *q = _mesa_lookup_query_object(ctx, ids[i]);
339 if (q) {
340 if (q->Active) {
341 struct gl_query_object **bindpt;
342 bindpt = get_query_binding_point(ctx, q->Target, q->Stream);
343 assert(bindpt); /* Should be non-null for active q. */
344 if (bindpt) {
345 *bindpt = NULL;
346 }
347 q->Active = GL_FALSE;
348 ctx->Driver.EndQuery(ctx, q);
349 }
350 _mesa_HashRemoveLocked(ctx->Query.QueryObjects, ids[i]);
351 ctx->Driver.DeleteQuery(ctx, q);
352 }
353 }
354 }
355 }
356
357
358 GLboolean GLAPIENTRY
359 _mesa_IsQuery(GLuint id)
360 {
361 struct gl_query_object *q;
362
363 GET_CURRENT_CONTEXT(ctx);
364 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
365
366 if (MESA_VERBOSE & VERBOSE_API)
367 _mesa_debug(ctx, "glIsQuery(%u)\n", id);
368
369 if (id == 0)
370 return GL_FALSE;
371
372 q = _mesa_lookup_query_object(ctx, id);
373 if (q == NULL)
374 return GL_FALSE;
375
376 return q->EverBound;
377 }
378
379 static GLboolean
380 query_error_check_index(struct gl_context *ctx, GLenum target, GLuint index)
381 {
382 switch (target) {
383 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
384 case GL_PRIMITIVES_GENERATED:
385 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW:
386 if (index >= ctx->Const.MaxVertexStreams) {
387 _mesa_error(ctx, GL_INVALID_VALUE,
388 "glBeginQueryIndexed(index>=MaxVertexStreams)");
389 return GL_FALSE;
390 }
391 break;
392 default:
393 if (index > 0) {
394 _mesa_error(ctx, GL_INVALID_VALUE, "glBeginQueryIndexed(index>0)");
395 return GL_FALSE;
396 }
397 }
398 return GL_TRUE;
399 }
400
401 void GLAPIENTRY
402 _mesa_BeginQueryIndexed(GLenum target, GLuint index, GLuint id)
403 {
404 struct gl_query_object *q, **bindpt;
405 GET_CURRENT_CONTEXT(ctx);
406
407 if (MESA_VERBOSE & VERBOSE_API)
408 _mesa_debug(ctx, "glBeginQueryIndexed(%s, %u, %u)\n",
409 _mesa_enum_to_string(target), index, id);
410
411 if (!query_error_check_index(ctx, target, index))
412 return;
413
414 FLUSH_VERTICES(ctx, 0);
415
416 bindpt = get_query_binding_point(ctx, target, index);
417 if (!bindpt) {
418 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginQuery{Indexed}(target)");
419 return;
420 }
421
422 /* From the GL_ARB_occlusion_query spec:
423 *
424 * "If BeginQueryARB is called while another query is already in
425 * progress with the same target, an INVALID_OPERATION error is
426 * generated."
427 */
428 if (*bindpt) {
429 _mesa_error(ctx, GL_INVALID_OPERATION,
430 "glBeginQuery{Indexed}(target=%s is active)",
431 _mesa_enum_to_string(target));
432 return;
433 }
434
435 if (id == 0) {
436 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQuery{Indexed}(id==0)");
437 return;
438 }
439
440 q = _mesa_lookup_query_object(ctx, id);
441 if (!q) {
442 if (ctx->API != API_OPENGL_COMPAT) {
443 _mesa_error(ctx, GL_INVALID_OPERATION,
444 "glBeginQuery{Indexed}(non-gen name)");
445 return;
446 } else {
447 /* create new object */
448 q = ctx->Driver.NewQueryObject(ctx, id);
449 if (!q) {
450 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQuery{Indexed}");
451 return;
452 }
453 _mesa_HashInsertLocked(ctx->Query.QueryObjects, id, q);
454 }
455 }
456 else {
457 /* pre-existing object */
458 if (q->Active) {
459 _mesa_error(ctx, GL_INVALID_OPERATION,
460 "glBeginQuery{Indexed}(query already active)");
461 return;
462 }
463
464 /* Section 2.14 Asynchronous Queries, page 84 of the OpenGL ES 3.0.4
465 * spec states:
466 *
467 * "BeginQuery generates an INVALID_OPERATION error if any of the
468 * following conditions hold: [...] id is the name of an
469 * existing query object whose type does not match target; [...]
470 *
471 * Similar wording exists in the OpenGL 4.5 spec, section 4.2. QUERY
472 * OBJECTS AND ASYNCHRONOUS QUERIES, page 43.
473 */
474 if (q->EverBound && q->Target != target) {
475 _mesa_error(ctx, GL_INVALID_OPERATION,
476 "glBeginQuery{Indexed}(target mismatch)");
477 return;
478 }
479 }
480
481 /* This possibly changes the target of a buffer allocated by
482 * CreateQueries. Issue 39) in the ARB_direct_state_access extension states
483 * the following:
484 *
485 * "CreateQueries adds a <target>, so strictly speaking the <target>
486 * command isn't needed for BeginQuery/EndQuery, but in the end, this also
487 * isn't a selector, so we decided not to change it."
488 *
489 * Updating the target of the query object should be acceptable, so let's
490 * do that.
491 */
492
493 q->Target = target;
494 q->Active = GL_TRUE;
495 q->Result = 0;
496 q->Ready = GL_FALSE;
497 q->EverBound = GL_TRUE;
498 q->Stream = index;
499
500 /* XXX should probably refcount query objects */
501 *bindpt = q;
502
503 ctx->Driver.BeginQuery(ctx, q);
504 }
505
506
507 void GLAPIENTRY
508 _mesa_EndQueryIndexed(GLenum target, GLuint index)
509 {
510 struct gl_query_object *q, **bindpt;
511 GET_CURRENT_CONTEXT(ctx);
512
513 if (MESA_VERBOSE & VERBOSE_API)
514 _mesa_debug(ctx, "glEndQueryIndexed(%s, %u)\n",
515 _mesa_enum_to_string(target), index);
516
517 if (!query_error_check_index(ctx, target, index))
518 return;
519
520 FLUSH_VERTICES(ctx, 0);
521
522 bindpt = get_query_binding_point(ctx, target, index);
523 if (!bindpt) {
524 _mesa_error(ctx, GL_INVALID_ENUM, "glEndQuery{Indexed}(target)");
525 return;
526 }
527
528 /* XXX should probably refcount query objects */
529 q = *bindpt;
530
531 /* Check for GL_ANY_SAMPLES_PASSED vs GL_SAMPLES_PASSED. */
532 if (q && q->Target != target) {
533 _mesa_error(ctx, GL_INVALID_OPERATION,
534 "glEndQuery(target=%s with active query of target %s)",
535 _mesa_enum_to_string(target),
536 _mesa_enum_to_string(q->Target));
537 return;
538 }
539
540 *bindpt = NULL;
541
542 if (!q || !q->Active) {
543 _mesa_error(ctx, GL_INVALID_OPERATION,
544 "glEndQuery{Indexed}(no matching glBeginQuery{Indexed})");
545 return;
546 }
547
548 q->Active = GL_FALSE;
549 ctx->Driver.EndQuery(ctx, q);
550 }
551
552 void GLAPIENTRY
553 _mesa_BeginQuery(GLenum target, GLuint id)
554 {
555 _mesa_BeginQueryIndexed(target, 0, id);
556 }
557
558 void GLAPIENTRY
559 _mesa_EndQuery(GLenum target)
560 {
561 _mesa_EndQueryIndexed(target, 0);
562 }
563
564 void GLAPIENTRY
565 _mesa_QueryCounter(GLuint id, GLenum target)
566 {
567 struct gl_query_object *q;
568 GET_CURRENT_CONTEXT(ctx);
569
570 if (MESA_VERBOSE & VERBOSE_API)
571 _mesa_debug(ctx, "glQueryCounter(%u, %s)\n", id,
572 _mesa_enum_to_string(target));
573
574 /* error checking */
575 if (target != GL_TIMESTAMP) {
576 _mesa_error(ctx, GL_INVALID_ENUM, "glQueryCounter(target)");
577 return;
578 }
579
580 if (id == 0) {
581 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id==0)");
582 return;
583 }
584
585 q = _mesa_lookup_query_object(ctx, id);
586 if (!q) {
587 /* XXX the Core profile should throw INVALID_OPERATION here */
588
589 /* create new object */
590 q = ctx->Driver.NewQueryObject(ctx, id);
591 if (!q) {
592 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glQueryCounter");
593 return;
594 }
595 _mesa_HashInsertLocked(ctx->Query.QueryObjects, id, q);
596 }
597 else {
598 if (q->Target && q->Target != GL_TIMESTAMP) {
599 _mesa_error(ctx, GL_INVALID_OPERATION,
600 "glQueryCounter(id has an invalid target)");
601 return;
602 }
603 }
604
605 if (q->Active) {
606 _mesa_error(ctx, GL_INVALID_OPERATION, "glQueryCounter(id is active)");
607 return;
608 }
609
610 /* This possibly changes the target of a buffer allocated by
611 * CreateQueries. Issue 39) in the ARB_direct_state_access extension states
612 * the following:
613 *
614 * "CreateQueries adds a <target>, so strictly speaking the <target>
615 * command isn't needed for BeginQuery/EndQuery, but in the end, this also
616 * isn't a selector, so we decided not to change it."
617 *
618 * Updating the target of the query object should be acceptable, so let's
619 * do that.
620 */
621
622 q->Target = target;
623 q->Result = 0;
624 q->Ready = GL_FALSE;
625 q->EverBound = GL_TRUE;
626
627 if (ctx->Driver.QueryCounter) {
628 ctx->Driver.QueryCounter(ctx, q);
629 } else {
630 /* QueryCounter is implemented using EndQuery without BeginQuery
631 * in drivers. This is actually Direct3D and Gallium convention.
632 */
633 ctx->Driver.EndQuery(ctx, q);
634 }
635 }
636
637
638 void GLAPIENTRY
639 _mesa_GetQueryIndexediv(GLenum target, GLuint index, GLenum pname,
640 GLint *params)
641 {
642 struct gl_query_object *q = NULL, **bindpt = NULL;
643 GET_CURRENT_CONTEXT(ctx);
644
645 if (MESA_VERBOSE & VERBOSE_API)
646 _mesa_debug(ctx, "glGetQueryIndexediv(%s, %u, %s)\n",
647 _mesa_enum_to_string(target),
648 index,
649 _mesa_enum_to_string(pname));
650
651 if (!query_error_check_index(ctx, target, index))
652 return;
653
654 /* From the GL_EXT_occlusion_query_boolean spec:
655 *
656 * "The error INVALID_ENUM is generated if GetQueryivEXT is called where
657 * <pname> is not CURRENT_QUERY_EXT."
658 *
659 * Same rule is present also in ES 3.2 spec.
660 */
661 if (_mesa_is_gles(ctx) && pname != GL_CURRENT_QUERY) {
662 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryivEXT(%s)",
663 _mesa_enum_to_string(pname));
664 return;
665 }
666
667 if (target == GL_TIMESTAMP) {
668 if (!_mesa_has_ARB_timer_query(ctx) &&
669 !_mesa_has_EXT_disjoint_timer_query(ctx)) {
670 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQueryARB(target)");
671 return;
672 }
673 }
674 else {
675 bindpt = get_query_binding_point(ctx, target, index);
676 if (!bindpt) {
677 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(target)");
678 return;
679 }
680
681 q = *bindpt;
682 }
683
684 switch (pname) {
685 case GL_QUERY_COUNTER_BITS:
686 switch (target) {
687 case GL_SAMPLES_PASSED:
688 *params = ctx->Const.QueryCounterBits.SamplesPassed;
689 break;
690 case GL_ANY_SAMPLES_PASSED:
691 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
692 /* The minimum value of this is 1 if it's nonzero, and the value
693 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
694 * bits.
695 */
696 *params = 1;
697 break;
698 case GL_TIME_ELAPSED:
699 *params = ctx->Const.QueryCounterBits.TimeElapsed;
700 break;
701 case GL_TIMESTAMP:
702 *params = ctx->Const.QueryCounterBits.Timestamp;
703 break;
704 case GL_PRIMITIVES_GENERATED:
705 *params = ctx->Const.QueryCounterBits.PrimitivesGenerated;
706 break;
707 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
708 *params = ctx->Const.QueryCounterBits.PrimitivesWritten;
709 break;
710 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW:
711 case GL_TRANSFORM_FEEDBACK_OVERFLOW:
712 /* The minimum value of this is 1 if it's nonzero, and the value
713 * is only ever GL_TRUE or GL_FALSE, so no sense in reporting more
714 * bits.
715 */
716 *params = 1;
717 break;
718 case GL_VERTICES_SUBMITTED:
719 *params = ctx->Const.QueryCounterBits.VerticesSubmitted;
720 break;
721 case GL_PRIMITIVES_SUBMITTED:
722 *params = ctx->Const.QueryCounterBits.PrimitivesSubmitted;
723 break;
724 case GL_VERTEX_SHADER_INVOCATIONS:
725 *params = ctx->Const.QueryCounterBits.VsInvocations;
726 break;
727 case GL_TESS_CONTROL_SHADER_PATCHES:
728 *params = ctx->Const.QueryCounterBits.TessPatches;
729 break;
730 case GL_TESS_EVALUATION_SHADER_INVOCATIONS:
731 *params = ctx->Const.QueryCounterBits.TessInvocations;
732 break;
733 case GL_GEOMETRY_SHADER_INVOCATIONS:
734 *params = ctx->Const.QueryCounterBits.GsInvocations;
735 break;
736 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED:
737 *params = ctx->Const.QueryCounterBits.GsPrimitives;
738 break;
739 case GL_FRAGMENT_SHADER_INVOCATIONS:
740 *params = ctx->Const.QueryCounterBits.FsInvocations;
741 break;
742 case GL_COMPUTE_SHADER_INVOCATIONS:
743 *params = ctx->Const.QueryCounterBits.ComputeInvocations;
744 break;
745 case GL_CLIPPING_INPUT_PRIMITIVES:
746 *params = ctx->Const.QueryCounterBits.ClInPrimitives;
747 break;
748 case GL_CLIPPING_OUTPUT_PRIMITIVES:
749 *params = ctx->Const.QueryCounterBits.ClOutPrimitives;
750 break;
751 default:
752 _mesa_problem(ctx,
753 "Unknown target in glGetQueryIndexediv(target = %s)",
754 _mesa_enum_to_string(target));
755 *params = 0;
756 break;
757 }
758 break;
759 case GL_CURRENT_QUERY:
760 *params = (q && q->Target == target) ? q->Id : 0;
761 break;
762 default:
763 _mesa_error(ctx, GL_INVALID_ENUM, "glGetQuery{Indexed}iv(pname)");
764 return;
765 }
766 }
767
768 void GLAPIENTRY
769 _mesa_GetQueryiv(GLenum target, GLenum pname, GLint *params)
770 {
771 _mesa_GetQueryIndexediv(target, 0, pname, params);
772 }
773
774 static void
775 get_query_object(struct gl_context *ctx, const char *func,
776 GLuint id, GLenum pname, GLenum ptype,
777 struct gl_buffer_object *buf, intptr_t offset)
778 {
779 struct gl_query_object *q = NULL;
780 uint64_t value;
781
782 if (MESA_VERBOSE & VERBOSE_API)
783 _mesa_debug(ctx, "%s(%u, %s)\n", func, id,
784 _mesa_enum_to_string(pname));
785
786 if (id)
787 q = _mesa_lookup_query_object(ctx, id);
788
789 if (!q || q->Active || !q->EverBound) {
790 _mesa_error(ctx, GL_INVALID_OPERATION,
791 "%s(id=%d is invalid or active)", func, id);
792 return;
793 }
794
795 /* From GL_EXT_occlusion_query_boolean spec:
796 *
797 * "Accepted by the <pname> parameter of GetQueryObjectivEXT and
798 * GetQueryObjectuivEXT:
799 *
800 * QUERY_RESULT_EXT 0x8866
801 * QUERY_RESULT_AVAILABLE_EXT 0x8867"
802 *
803 * Same rule is present also in ES 3.2 spec.
804 */
805 if (_mesa_is_gles(ctx) &&
806 (pname != GL_QUERY_RESULT && pname != GL_QUERY_RESULT_AVAILABLE)) {
807 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s)", func,
808 _mesa_enum_to_string(pname));
809 return;
810 }
811
812 if (buf && buf != ctx->Shared->NullBufferObj) {
813 bool is_64bit = ptype == GL_INT64_ARB ||
814 ptype == GL_UNSIGNED_INT64_ARB;
815 if (!_mesa_has_ARB_query_buffer_object(ctx)) {
816 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(not supported)", func);
817 return;
818 }
819 if (buf->Size < offset + 4 * (is_64bit ? 2 : 1)) {
820 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(out of bounds)", func);
821 return;
822 }
823
824 if (offset < 0) {
825 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset is negative)", func);
826 return;
827 }
828
829 switch (pname) {
830 case GL_QUERY_RESULT:
831 case GL_QUERY_RESULT_NO_WAIT:
832 case GL_QUERY_RESULT_AVAILABLE:
833 case GL_QUERY_TARGET:
834 ctx->Driver.StoreQueryResult(ctx, q, buf, offset, pname, ptype);
835 return;
836 }
837
838 /* fall through to get error below */
839 }
840
841 switch (pname) {
842 case GL_QUERY_RESULT:
843 if (!q->Ready)
844 ctx->Driver.WaitQuery(ctx, q);
845 value = q->Result;
846 break;
847 case GL_QUERY_RESULT_NO_WAIT:
848 if (!_mesa_has_ARB_query_buffer_object(ctx))
849 goto invalid_enum;
850 ctx->Driver.CheckQuery(ctx, q);
851 if (!q->Ready)
852 return;
853 value = q->Result;
854 break;
855 case GL_QUERY_RESULT_AVAILABLE:
856 if (!q->Ready)
857 ctx->Driver.CheckQuery(ctx, q);
858 value = q->Ready;
859 break;
860 case GL_QUERY_TARGET:
861 value = q->Target;
862 break;
863 default:
864 invalid_enum:
865 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=%s)",
866 func, _mesa_enum_to_string(pname));
867 return;
868 }
869
870 switch (ptype) {
871 case GL_INT: {
872 GLint *param = (GLint *)offset;
873 if (value > 0x7fffffff)
874 *param = 0x7fffffff;
875 else
876 *param = value;
877 break;
878 }
879 case GL_UNSIGNED_INT: {
880 GLuint *param = (GLuint *)offset;
881 if (value > 0xffffffff)
882 *param = 0xffffffff;
883 else
884 *param = value;
885 break;
886 }
887 case GL_INT64_ARB:
888 case GL_UNSIGNED_INT64_ARB: {
889 GLuint64EXT *param = (GLuint64EXT *)offset;
890 *param = value;
891 break;
892 }
893 default:
894 unreachable("unexpected ptype");
895 }
896 }
897
898 void GLAPIENTRY
899 _mesa_GetQueryObjectiv(GLuint id, GLenum pname, GLint *params)
900 {
901 GET_CURRENT_CONTEXT(ctx);
902
903 get_query_object(ctx, "glGetQueryObjectiv",
904 id, pname, GL_INT, ctx->QueryBuffer, (intptr_t)params);
905 }
906
907
908 void GLAPIENTRY
909 _mesa_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
910 {
911 GET_CURRENT_CONTEXT(ctx);
912
913 get_query_object(ctx, "glGetQueryObjectuiv",
914 id, pname, GL_UNSIGNED_INT,
915 ctx->QueryBuffer, (intptr_t)params);
916 }
917
918
919 /**
920 * New with GL_EXT_timer_query
921 */
922 void GLAPIENTRY
923 _mesa_GetQueryObjecti64v(GLuint id, GLenum pname, GLint64EXT *params)
924 {
925 GET_CURRENT_CONTEXT(ctx);
926
927 get_query_object(ctx, "glGetQueryObjecti64v",
928 id, pname, GL_INT64_ARB,
929 ctx->QueryBuffer, (intptr_t)params);
930 }
931
932
933 /**
934 * New with GL_EXT_timer_query
935 */
936 void GLAPIENTRY
937 _mesa_GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64EXT *params)
938 {
939 GET_CURRENT_CONTEXT(ctx);
940
941 get_query_object(ctx, "glGetQueryObjectui64v",
942 id, pname, GL_UNSIGNED_INT64_ARB,
943 ctx->QueryBuffer, (intptr_t)params);
944 }
945
946 /**
947 * New with GL_ARB_query_buffer_object
948 */
949 void GLAPIENTRY
950 _mesa_GetQueryBufferObjectiv(GLuint id, GLuint buffer, GLenum pname,
951 GLintptr offset)
952 {
953 struct gl_buffer_object *buf;
954 GET_CURRENT_CONTEXT(ctx);
955
956 buf = _mesa_lookup_bufferobj_err(ctx, buffer, "glGetQueryBufferObjectiv");
957 if (!buf)
958 return;
959
960 get_query_object(ctx, "glGetQueryBufferObjectiv",
961 id, pname, GL_INT, buf, offset);
962 }
963
964
965 void GLAPIENTRY
966 _mesa_GetQueryBufferObjectuiv(GLuint id, GLuint buffer, GLenum pname,
967 GLintptr offset)
968 {
969 struct gl_buffer_object *buf;
970 GET_CURRENT_CONTEXT(ctx);
971
972 buf = _mesa_lookup_bufferobj_err(ctx, buffer, "glGetQueryBufferObjectuiv");
973 if (!buf)
974 return;
975
976 get_query_object(ctx, "glGetQueryBufferObjectuiv",
977 id, pname, GL_UNSIGNED_INT, buf, offset);
978 }
979
980
981 void GLAPIENTRY
982 _mesa_GetQueryBufferObjecti64v(GLuint id, GLuint buffer, GLenum pname,
983 GLintptr offset)
984 {
985 struct gl_buffer_object *buf;
986 GET_CURRENT_CONTEXT(ctx);
987
988 buf = _mesa_lookup_bufferobj_err(ctx, buffer, "glGetQueryBufferObjecti64v");
989 if (!buf)
990 return;
991
992 get_query_object(ctx, "glGetQueryBufferObjecti64v",
993 id, pname, GL_INT64_ARB, buf, offset);
994 }
995
996
997 void GLAPIENTRY
998 _mesa_GetQueryBufferObjectui64v(GLuint id, GLuint buffer, GLenum pname,
999 GLintptr offset)
1000 {
1001 struct gl_buffer_object *buf;
1002 GET_CURRENT_CONTEXT(ctx);
1003
1004 buf = _mesa_lookup_bufferobj_err(ctx, buffer, "glGetQueryBufferObjectui64v");
1005 if (!buf)
1006 return;
1007
1008 get_query_object(ctx, "glGetQueryBufferObjectui64v",
1009 id, pname, GL_UNSIGNED_INT64_ARB, buf, offset);
1010 }
1011
1012
1013 /**
1014 * Allocate/init the context state related to query objects.
1015 */
1016 void
1017 _mesa_init_queryobj(struct gl_context *ctx)
1018 {
1019 ctx->Query.QueryObjects = _mesa_NewHashTable();
1020 ctx->Query.CurrentOcclusionObject = NULL;
1021
1022 ctx->Const.QueryCounterBits.SamplesPassed = 64;
1023 ctx->Const.QueryCounterBits.TimeElapsed = 64;
1024 ctx->Const.QueryCounterBits.Timestamp = 64;
1025 ctx->Const.QueryCounterBits.PrimitivesGenerated = 64;
1026 ctx->Const.QueryCounterBits.PrimitivesWritten = 64;
1027
1028 ctx->Const.QueryCounterBits.VerticesSubmitted = 64;
1029 ctx->Const.QueryCounterBits.PrimitivesSubmitted = 64;
1030 ctx->Const.QueryCounterBits.VsInvocations = 64;
1031 ctx->Const.QueryCounterBits.TessPatches = 64;
1032 ctx->Const.QueryCounterBits.TessInvocations = 64;
1033 ctx->Const.QueryCounterBits.GsInvocations = 64;
1034 ctx->Const.QueryCounterBits.GsPrimitives = 64;
1035 ctx->Const.QueryCounterBits.FsInvocations = 64;
1036 ctx->Const.QueryCounterBits.ComputeInvocations = 64;
1037 ctx->Const.QueryCounterBits.ClInPrimitives = 64;
1038 ctx->Const.QueryCounterBits.ClOutPrimitives = 64;
1039 }
1040
1041
1042 /**
1043 * Callback for deleting a query object. Called by _mesa_HashDeleteAll().
1044 */
1045 static void
1046 delete_queryobj_cb(GLuint id, void *data, void *userData)
1047 {
1048 struct gl_query_object *q= (struct gl_query_object *) data;
1049 struct gl_context *ctx = (struct gl_context *)userData;
1050 ctx->Driver.DeleteQuery(ctx, q);
1051 }
1052
1053
1054 /**
1055 * Free the context state related to query objects.
1056 */
1057 void
1058 _mesa_free_queryobj_data(struct gl_context *ctx)
1059 {
1060 _mesa_HashDeleteAll(ctx->Query.QueryObjects, delete_queryobj_cb, ctx);
1061 _mesa_DeleteHashTable(ctx->Query.QueryObjects);
1062 }