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