+ bool ret = false;
+
+ st_flush_bitmap_cache(st_context(ctx));
+
+ if ((q->Target == GL_TIMESTAMP ||
+ q->Target == GL_TIME_ELAPSED) &&
+ !stq->pq) {
+ stq->pq = pipe->create_query(pipe, PIPE_QUERY_TIMESTAMP, 0);
+ stq->type = PIPE_QUERY_TIMESTAMP;
+ }
+
+ if (stq->pq)
+ ret = pipe->end_query(pipe, stq->pq);
+
+ if (!ret) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEndQuery");
+ return;
+ }
+
+ if (stq->type != PIPE_QUERY_TIMESTAMP)
+ st->active_queries--;
+}
+
+
+static boolean
+get_query_result(struct pipe_context *pipe,
+ struct st_query_object *stq,
+ boolean wait)
+{
+ union pipe_query_result data;
+
+ if (!stq->pq) {
+ /* Only needed in case we failed to allocate the gallium query earlier.
+ * Return TRUE so we don't spin on this forever.
+ */
+ return TRUE;
+ }
+
+ if (!pipe->get_query_result(pipe, stq->pq, wait, &data))
+ return FALSE;
+
+ switch (stq->type) {
+ case PIPE_QUERY_PIPELINE_STATISTICS:
+ switch (stq->base.Target) {
+ case GL_VERTICES_SUBMITTED_ARB:
+ stq->base.Result = data.pipeline_statistics.ia_vertices;
+ break;
+ case GL_PRIMITIVES_SUBMITTED_ARB:
+ stq->base.Result = data.pipeline_statistics.ia_primitives;
+ break;
+ case GL_VERTEX_SHADER_INVOCATIONS_ARB:
+ stq->base.Result = data.pipeline_statistics.vs_invocations;
+ break;
+ case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
+ stq->base.Result = data.pipeline_statistics.hs_invocations;
+ break;
+ case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
+ stq->base.Result = data.pipeline_statistics.ds_invocations;
+ break;
+ case GL_GEOMETRY_SHADER_INVOCATIONS:
+ stq->base.Result = data.pipeline_statistics.gs_invocations;
+ break;
+ case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
+ stq->base.Result = data.pipeline_statistics.gs_primitives;
+ break;
+ case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
+ stq->base.Result = data.pipeline_statistics.ps_invocations;
+ break;
+ case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
+ stq->base.Result = data.pipeline_statistics.cs_invocations;
+ break;
+ case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
+ stq->base.Result = data.pipeline_statistics.c_invocations;
+ break;
+ case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
+ stq->base.Result = data.pipeline_statistics.c_primitives;
+ break;
+ default:
+ unreachable("invalid pipeline statistics counter");
+ }
+ break;
+ case PIPE_QUERY_OCCLUSION_PREDICATE:
+ case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
+ case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
+ case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
+ stq->base.Result = !!data.b;
+ break;
+ default:
+ stq->base.Result = data.u64;
+ break;
+ }
+
+ if (stq->base.Target == GL_TIME_ELAPSED &&
+ stq->type == PIPE_QUERY_TIMESTAMP) {
+ /* Calculate the elapsed time from the two timestamp queries */
+ GLuint64EXT Result0 = 0;
+ assert(stq->pq_begin);
+ pipe->get_query_result(pipe, stq->pq_begin, TRUE, (void *)&Result0);
+ stq->base.Result -= Result0;
+ } else {
+ assert(!stq->pq_begin);
+ }