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