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