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