1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * glBegin/EndQuery interface to pipe
36 #include "main/imports.h"
37 #include "main/compiler.h"
38 #include "main/context.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "pipe/p_screen.h"
43 #include "util/u_inlines.h"
44 #include "st_context.h"
45 #include "st_cb_queryobj.h"
46 #include "st_cb_bitmap.h"
47 #include "st_cb_bufferobjects.h"
51 static struct gl_query_object
*
52 st_NewQueryObject(struct gl_context
*ctx
, GLuint id
)
54 struct st_query_object
*stq
= ST_CALLOC_STRUCT(st_query_object
);
57 stq
->base
.Ready
= GL_TRUE
;
59 stq
->type
= PIPE_QUERY_TYPES
; /* an invalid value */
67 free_queries(struct pipe_context
*pipe
, struct st_query_object
*stq
)
70 pipe
->destroy_query(pipe
, stq
->pq
);
75 pipe
->destroy_query(pipe
, stq
->pq_begin
);
82 st_DeleteQuery(struct gl_context
*ctx
, struct gl_query_object
*q
)
84 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
85 struct st_query_object
*stq
= st_query_object(q
);
87 free_queries(pipe
, stq
);
94 target_to_index(const struct st_context
*st
, const struct gl_query_object
*q
)
96 if (q
->Target
== GL_PRIMITIVES_GENERATED
||
97 q
->Target
== GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
||
98 q
->Target
== GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB
)
101 if (st
->has_single_pipe_stat
) {
103 case GL_VERTICES_SUBMITTED_ARB
:
104 return PIPE_STAT_QUERY_IA_VERTICES
;
105 case GL_PRIMITIVES_SUBMITTED_ARB
:
106 return PIPE_STAT_QUERY_IA_PRIMITIVES
;
107 case GL_VERTEX_SHADER_INVOCATIONS_ARB
:
108 return PIPE_STAT_QUERY_VS_INVOCATIONS
;
109 case GL_GEOMETRY_SHADER_INVOCATIONS
:
110 return PIPE_STAT_QUERY_GS_INVOCATIONS
;
111 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB
:
112 return PIPE_STAT_QUERY_GS_PRIMITIVES
;
113 case GL_CLIPPING_INPUT_PRIMITIVES_ARB
:
114 return PIPE_STAT_QUERY_C_INVOCATIONS
;
115 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB
:
116 return PIPE_STAT_QUERY_C_PRIMITIVES
;
117 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB
:
118 return PIPE_STAT_QUERY_PS_INVOCATIONS
;
119 case GL_TESS_CONTROL_SHADER_PATCHES_ARB
:
120 return PIPE_STAT_QUERY_HS_INVOCATIONS
;
121 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB
:
122 return PIPE_STAT_QUERY_DS_INVOCATIONS
;
123 case GL_COMPUTE_SHADER_INVOCATIONS_ARB
:
124 return PIPE_STAT_QUERY_CS_INVOCATIONS
;
134 st_BeginQuery(struct gl_context
*ctx
, struct gl_query_object
*q
)
136 struct st_context
*st
= st_context(ctx
);
137 struct pipe_context
*pipe
= st
->pipe
;
138 struct st_query_object
*stq
= st_query_object(q
);
142 st_flush_bitmap_cache(st_context(ctx
));
144 /* convert GL query type to Gallium query type */
146 case GL_ANY_SAMPLES_PASSED
:
147 type
= PIPE_QUERY_OCCLUSION_PREDICATE
;
149 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
150 type
= PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
;
152 case GL_SAMPLES_PASSED_ARB
:
153 type
= PIPE_QUERY_OCCLUSION_COUNTER
;
155 case GL_PRIMITIVES_GENERATED
:
156 type
= PIPE_QUERY_PRIMITIVES_GENERATED
;
158 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
:
159 type
= PIPE_QUERY_PRIMITIVES_EMITTED
;
161 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB
:
162 type
= PIPE_QUERY_SO_OVERFLOW_PREDICATE
;
164 case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB
:
165 type
= PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
;
167 case GL_TIME_ELAPSED
:
168 if (st
->has_time_elapsed
)
169 type
= PIPE_QUERY_TIME_ELAPSED
;
171 type
= PIPE_QUERY_TIMESTAMP
;
173 case GL_VERTICES_SUBMITTED_ARB
:
174 case GL_PRIMITIVES_SUBMITTED_ARB
:
175 case GL_VERTEX_SHADER_INVOCATIONS_ARB
:
176 case GL_TESS_CONTROL_SHADER_PATCHES_ARB
:
177 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB
:
178 case GL_GEOMETRY_SHADER_INVOCATIONS
:
179 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB
:
180 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB
:
181 case GL_COMPUTE_SHADER_INVOCATIONS_ARB
:
182 case GL_CLIPPING_INPUT_PRIMITIVES_ARB
:
183 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB
:
184 type
= st
->has_single_pipe_stat
? PIPE_QUERY_PIPELINE_STATISTICS_SINGLE
185 : PIPE_QUERY_PIPELINE_STATISTICS
;
188 assert(0 && "unexpected query target in st_BeginQuery()");
192 if (stq
->type
!= type
) {
193 /* free old query of different type */
194 free_queries(pipe
, stq
);
195 stq
->type
= PIPE_QUERY_TYPES
; /* an invalid value */
198 if (q
->Target
== GL_TIME_ELAPSED
&&
199 type
== PIPE_QUERY_TIMESTAMP
) {
200 /* Determine time elapsed by emitting two timestamp queries. */
201 if (!stq
->pq_begin
) {
202 stq
->pq_begin
= pipe
->create_query(pipe
, type
, 0);
206 ret
= pipe
->end_query(pipe
, stq
->pq_begin
);
209 stq
->pq
= pipe
->create_query(pipe
, type
, target_to_index(st
, q
));
213 ret
= pipe
->begin_query(pipe
, stq
->pq
);
217 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBeginQuery");
219 free_queries(pipe
, stq
);
220 q
->Active
= GL_FALSE
;
224 assert(stq
->type
== type
);
229 st_EndQuery(struct gl_context
*ctx
, struct gl_query_object
*q
)
231 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
232 struct st_query_object
*stq
= st_query_object(q
);
235 st_flush_bitmap_cache(st_context(ctx
));
237 if ((q
->Target
== GL_TIMESTAMP
||
238 q
->Target
== GL_TIME_ELAPSED
) &&
240 stq
->pq
= pipe
->create_query(pipe
, PIPE_QUERY_TIMESTAMP
, 0);
241 stq
->type
= PIPE_QUERY_TIMESTAMP
;
245 ret
= pipe
->end_query(pipe
, stq
->pq
);
248 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEndQuery");
255 get_query_result(struct pipe_context
*pipe
,
256 struct st_query_object
*stq
,
259 union pipe_query_result data
;
262 /* Only needed in case we failed to allocate the gallium query earlier.
263 * Return TRUE so we don't spin on this forever.
268 if (!pipe
->get_query_result(pipe
, stq
->pq
, wait
, &data
))
272 case PIPE_QUERY_PIPELINE_STATISTICS
:
273 switch (stq
->base
.Target
) {
274 case GL_VERTICES_SUBMITTED_ARB
:
275 stq
->base
.Result
= data
.pipeline_statistics
.ia_vertices
;
277 case GL_PRIMITIVES_SUBMITTED_ARB
:
278 stq
->base
.Result
= data
.pipeline_statistics
.ia_primitives
;
280 case GL_VERTEX_SHADER_INVOCATIONS_ARB
:
281 stq
->base
.Result
= data
.pipeline_statistics
.vs_invocations
;
283 case GL_TESS_CONTROL_SHADER_PATCHES_ARB
:
284 stq
->base
.Result
= data
.pipeline_statistics
.hs_invocations
;
286 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB
:
287 stq
->base
.Result
= data
.pipeline_statistics
.ds_invocations
;
289 case GL_GEOMETRY_SHADER_INVOCATIONS
:
290 stq
->base
.Result
= data
.pipeline_statistics
.gs_invocations
;
292 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB
:
293 stq
->base
.Result
= data
.pipeline_statistics
.gs_primitives
;
295 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB
:
296 stq
->base
.Result
= data
.pipeline_statistics
.ps_invocations
;
298 case GL_COMPUTE_SHADER_INVOCATIONS_ARB
:
299 stq
->base
.Result
= data
.pipeline_statistics
.cs_invocations
;
301 case GL_CLIPPING_INPUT_PRIMITIVES_ARB
:
302 stq
->base
.Result
= data
.pipeline_statistics
.c_invocations
;
304 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB
:
305 stq
->base
.Result
= data
.pipeline_statistics
.c_primitives
;
308 unreachable("invalid pipeline statistics counter");
311 case PIPE_QUERY_OCCLUSION_PREDICATE
:
312 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
313 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
314 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
:
315 stq
->base
.Result
= !!data
.b
;
318 stq
->base
.Result
= data
.u64
;
322 if (stq
->base
.Target
== GL_TIME_ELAPSED
&&
323 stq
->type
== PIPE_QUERY_TIMESTAMP
) {
324 /* Calculate the elapsed time from the two timestamp queries */
325 GLuint64EXT Result0
= 0;
326 assert(stq
->pq_begin
);
327 pipe
->get_query_result(pipe
, stq
->pq_begin
, TRUE
, (void *)&Result0
);
328 stq
->base
.Result
-= Result0
;
330 assert(!stq
->pq_begin
);
338 st_WaitQuery(struct gl_context
*ctx
, struct gl_query_object
*q
)
340 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
341 struct st_query_object
*stq
= st_query_object(q
);
343 /* this function should only be called if we don't have a ready result */
344 assert(!stq
->base
.Ready
);
346 while (!stq
->base
.Ready
&&
347 !get_query_result(pipe
, stq
, TRUE
))
357 st_CheckQuery(struct gl_context
*ctx
, struct gl_query_object
*q
)
359 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
360 struct st_query_object
*stq
= st_query_object(q
);
361 assert(!q
->Ready
); /* we should not get called if Ready is TRUE */
362 q
->Ready
= get_query_result(pipe
, stq
, FALSE
);
367 st_GetTimestamp(struct gl_context
*ctx
)
369 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
370 struct pipe_screen
*screen
= pipe
->screen
;
372 /* Prefer the per-screen function */
373 if (screen
->get_timestamp
) {
374 return screen
->get_timestamp(screen
);
377 /* Fall back to the per-context function */
378 assert(pipe
->get_timestamp
);
379 return pipe
->get_timestamp(pipe
);
384 st_StoreQueryResult(struct gl_context
*ctx
, struct gl_query_object
*q
,
385 struct gl_buffer_object
*buf
, intptr_t offset
,
386 GLenum pname
, GLenum ptype
)
388 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
389 struct st_query_object
*stq
= st_query_object(q
);
390 struct st_buffer_object
*stObj
= st_buffer_object(buf
);
391 boolean wait
= pname
== GL_QUERY_RESULT
;
392 enum pipe_query_value_type result_type
;
395 /* GL_QUERY_TARGET is a bit of an extension since it has nothing to
396 * do with the GPU end of the query. Write it in "by hand".
398 if (pname
== GL_QUERY_TARGET
) {
399 /* Assume that the data must be LE. The endianness situation wrt CPU and
400 * GPU is incredibly confusing, but the vast majority of GPUs are
401 * LE. When a BE one comes along, this needs some form of resolution.
403 unsigned data
[2] = { CPU_TO_LE32(q
->Target
), 0 };
404 pipe_buffer_write(pipe
, stObj
->buffer
, offset
,
405 (ptype
== GL_INT64_ARB
||
406 ptype
== GL_UNSIGNED_INT64_ARB
) ? 8 : 4,
413 result_type
= PIPE_QUERY_TYPE_I32
;
415 case GL_UNSIGNED_INT
:
416 result_type
= PIPE_QUERY_TYPE_U32
;
419 result_type
= PIPE_QUERY_TYPE_I64
;
421 case GL_UNSIGNED_INT64_ARB
:
422 result_type
= PIPE_QUERY_TYPE_U64
;
425 unreachable("Unexpected result type");
428 if (pname
== GL_QUERY_RESULT_AVAILABLE
) {
430 } else if (stq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
) {
432 case GL_VERTICES_SUBMITTED_ARB
:
433 index
= PIPE_STAT_QUERY_IA_VERTICES
;
435 case GL_PRIMITIVES_SUBMITTED_ARB
:
436 index
= PIPE_STAT_QUERY_IA_PRIMITIVES
;
438 case GL_VERTEX_SHADER_INVOCATIONS_ARB
:
439 index
= PIPE_STAT_QUERY_VS_INVOCATIONS
;
441 case GL_GEOMETRY_SHADER_INVOCATIONS
:
442 index
= PIPE_STAT_QUERY_GS_INVOCATIONS
;
444 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB
:
445 index
= PIPE_STAT_QUERY_GS_PRIMITIVES
;
447 case GL_CLIPPING_INPUT_PRIMITIVES_ARB
:
448 index
= PIPE_STAT_QUERY_C_INVOCATIONS
;
450 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB
:
451 index
= PIPE_STAT_QUERY_C_PRIMITIVES
;
453 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB
:
454 index
= PIPE_STAT_QUERY_PS_INVOCATIONS
;
456 case GL_TESS_CONTROL_SHADER_PATCHES_ARB
:
457 index
= PIPE_STAT_QUERY_HS_INVOCATIONS
;
459 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB
:
460 index
= PIPE_STAT_QUERY_DS_INVOCATIONS
;
462 case GL_COMPUTE_SHADER_INVOCATIONS_ARB
:
463 index
= PIPE_STAT_QUERY_CS_INVOCATIONS
;
466 unreachable("Unexpected target");
472 pipe
->get_query_result_resource(pipe
, stq
->pq
, wait
, result_type
, index
,
473 stObj
->buffer
, offset
);
476 void st_init_query_functions(struct dd_function_table
*functions
)
478 functions
->NewQueryObject
= st_NewQueryObject
;
479 functions
->DeleteQuery
= st_DeleteQuery
;
480 functions
->BeginQuery
= st_BeginQuery
;
481 functions
->EndQuery
= st_EndQuery
;
482 functions
->WaitQuery
= st_WaitQuery
;
483 functions
->CheckQuery
= st_CheckQuery
;
484 functions
->GetTimestamp
= st_GetTimestamp
;
485 functions
->StoreQueryResult
= st_StoreQueryResult
;