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