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