gallium: Add the ability to query a single pipeline statistics counter
[mesa.git] / src / mesa / state_tracker / st_cb_queryobj.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * 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
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * glBegin/EndQuery interface to pipe
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/compiler.h"
38 #include "main/context.h"
39
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"
48
49
50 static struct gl_query_object *
51 st_NewQueryObject(struct gl_context *ctx, GLuint id)
52 {
53 struct st_query_object *stq = ST_CALLOC_STRUCT(st_query_object);
54 if (stq) {
55 stq->base.Id = id;
56 stq->base.Ready = GL_TRUE;
57 stq->pq = NULL;
58 stq->type = PIPE_QUERY_TYPES; /* an invalid value */
59 return &stq->base;
60 }
61 return NULL;
62 }
63
64
65 static void
66 free_queries(struct pipe_context *pipe, struct st_query_object *stq)
67 {
68 if (stq->pq) {
69 pipe->destroy_query(pipe, stq->pq);
70 stq->pq = NULL;
71 }
72
73 if (stq->pq_begin) {
74 pipe->destroy_query(pipe, stq->pq_begin);
75 stq->pq_begin = NULL;
76 }
77 }
78
79
80 static void
81 st_DeleteQuery(struct gl_context *ctx, struct gl_query_object *q)
82 {
83 struct pipe_context *pipe = st_context(ctx)->pipe;
84 struct st_query_object *stq = st_query_object(q);
85
86 free_queries(pipe, stq);
87
88 free(stq);
89 }
90
91 static int
92 target_to_index(const struct st_context *st, const struct gl_query_object *q)
93 {
94 if (q->Target == GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN ||
95 q->Target == GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB)
96 return q->Stream;
97
98 if (st->has_single_pipe_stat) {
99 switch (q->Target) {
100 case GL_VERTICES_SUBMITTED_ARB:
101 return PIPE_STAT_QUERY_IA_VERTICES;
102 case GL_PRIMITIVES_SUBMITTED_ARB:
103 return PIPE_STAT_QUERY_IA_PRIMITIVES;
104 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
105 return PIPE_STAT_QUERY_VS_INVOCATIONS;
106 case GL_GEOMETRY_SHADER_INVOCATIONS:
107 return PIPE_STAT_QUERY_GS_INVOCATIONS;
108 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
109 return PIPE_STAT_QUERY_GS_PRIMITIVES;
110 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
111 return PIPE_STAT_QUERY_C_INVOCATIONS;
112 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
113 return PIPE_STAT_QUERY_C_PRIMITIVES;
114 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
115 return PIPE_STAT_QUERY_PS_INVOCATIONS;
116 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
117 return PIPE_STAT_QUERY_HS_INVOCATIONS;
118 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
119 return PIPE_STAT_QUERY_DS_INVOCATIONS;
120 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
121 return PIPE_STAT_QUERY_CS_INVOCATIONS;
122 default:
123 break;
124 }
125 }
126
127 return 0;
128 }
129
130 static void
131 st_BeginQuery(struct gl_context *ctx, struct gl_query_object *q)
132 {
133 struct st_context *st = st_context(ctx);
134 struct pipe_context *pipe = st->pipe;
135 struct st_query_object *stq = st_query_object(q);
136 unsigned type;
137 bool ret = false;
138
139 st_flush_bitmap_cache(st_context(ctx));
140
141 /* convert GL query type to Gallium query type */
142 switch (q->Target) {
143 case GL_ANY_SAMPLES_PASSED:
144 type = PIPE_QUERY_OCCLUSION_PREDICATE;
145 break;
146 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
147 type = PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE;
148 break;
149 case GL_SAMPLES_PASSED_ARB:
150 type = PIPE_QUERY_OCCLUSION_COUNTER;
151 break;
152 case GL_PRIMITIVES_GENERATED:
153 type = PIPE_QUERY_PRIMITIVES_GENERATED;
154 break;
155 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
156 type = PIPE_QUERY_PRIMITIVES_EMITTED;
157 break;
158 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
159 type = PIPE_QUERY_SO_OVERFLOW_PREDICATE;
160 break;
161 case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB:
162 type = PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE;
163 break;
164 case GL_TIME_ELAPSED:
165 if (st->has_time_elapsed)
166 type = PIPE_QUERY_TIME_ELAPSED;
167 else
168 type = PIPE_QUERY_TIMESTAMP;
169 break;
170 case GL_VERTICES_SUBMITTED_ARB:
171 case GL_PRIMITIVES_SUBMITTED_ARB:
172 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
173 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
174 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
175 case GL_GEOMETRY_SHADER_INVOCATIONS:
176 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
177 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
178 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
179 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
180 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
181 type = st->has_single_pipe_stat ? PIPE_QUERY_PIPELINE_STATISTICS_SINGLE
182 : PIPE_QUERY_PIPELINE_STATISTICS;
183 break;
184 default:
185 assert(0 && "unexpected query target in st_BeginQuery()");
186 return;
187 }
188
189 if (stq->type != type) {
190 /* free old query of different type */
191 free_queries(pipe, stq);
192 stq->type = PIPE_QUERY_TYPES; /* an invalid value */
193 }
194
195 if (q->Target == GL_TIME_ELAPSED &&
196 type == PIPE_QUERY_TIMESTAMP) {
197 /* Determine time elapsed by emitting two timestamp queries. */
198 if (!stq->pq_begin) {
199 stq->pq_begin = pipe->create_query(pipe, type, 0);
200 stq->type = type;
201 }
202 if (stq->pq_begin)
203 ret = pipe->end_query(pipe, stq->pq_begin);
204 } else {
205 if (!stq->pq) {
206 stq->pq = pipe->create_query(pipe, type, target_to_index(st, q));
207 stq->type = type;
208 }
209 if (stq->pq)
210 ret = pipe->begin_query(pipe, stq->pq);
211 }
212
213 if (!ret) {
214 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBeginQuery");
215
216 free_queries(pipe, stq);
217 q->Active = GL_FALSE;
218 return;
219 }
220
221 assert(stq->type == type);
222 }
223
224
225 static void
226 st_EndQuery(struct gl_context *ctx, struct gl_query_object *q)
227 {
228 struct pipe_context *pipe = st_context(ctx)->pipe;
229 struct st_query_object *stq = st_query_object(q);
230 bool ret = false;
231
232 st_flush_bitmap_cache(st_context(ctx));
233
234 if ((q->Target == GL_TIMESTAMP ||
235 q->Target == GL_TIME_ELAPSED) &&
236 !stq->pq) {
237 stq->pq = pipe->create_query(pipe, PIPE_QUERY_TIMESTAMP, 0);
238 stq->type = PIPE_QUERY_TIMESTAMP;
239 }
240
241 if (stq->pq)
242 ret = pipe->end_query(pipe, stq->pq);
243
244 if (!ret) {
245 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEndQuery");
246 return;
247 }
248 }
249
250
251 static boolean
252 get_query_result(struct pipe_context *pipe,
253 struct st_query_object *stq,
254 boolean wait)
255 {
256 union pipe_query_result data;
257
258 if (!stq->pq) {
259 /* Only needed in case we failed to allocate the gallium query earlier.
260 * Return TRUE so we don't spin on this forever.
261 */
262 return TRUE;
263 }
264
265 if (!pipe->get_query_result(pipe, stq->pq, wait, &data))
266 return FALSE;
267
268 switch (stq->type) {
269 case PIPE_QUERY_PIPELINE_STATISTICS:
270 switch (stq->base.Target) {
271 case GL_VERTICES_SUBMITTED_ARB:
272 stq->base.Result = data.pipeline_statistics.ia_vertices;
273 break;
274 case GL_PRIMITIVES_SUBMITTED_ARB:
275 stq->base.Result = data.pipeline_statistics.ia_primitives;
276 break;
277 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
278 stq->base.Result = data.pipeline_statistics.vs_invocations;
279 break;
280 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
281 stq->base.Result = data.pipeline_statistics.hs_invocations;
282 break;
283 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
284 stq->base.Result = data.pipeline_statistics.ds_invocations;
285 break;
286 case GL_GEOMETRY_SHADER_INVOCATIONS:
287 stq->base.Result = data.pipeline_statistics.gs_invocations;
288 break;
289 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
290 stq->base.Result = data.pipeline_statistics.gs_primitives;
291 break;
292 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
293 stq->base.Result = data.pipeline_statistics.ps_invocations;
294 break;
295 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
296 stq->base.Result = data.pipeline_statistics.cs_invocations;
297 break;
298 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
299 stq->base.Result = data.pipeline_statistics.c_invocations;
300 break;
301 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
302 stq->base.Result = data.pipeline_statistics.c_primitives;
303 break;
304 default:
305 unreachable("invalid pipeline statistics counter");
306 }
307 break;
308 case PIPE_QUERY_OCCLUSION_PREDICATE:
309 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
310 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
311 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
312 stq->base.Result = !!data.b;
313 break;
314 default:
315 stq->base.Result = data.u64;
316 break;
317 }
318
319 if (stq->base.Target == GL_TIME_ELAPSED &&
320 stq->type == PIPE_QUERY_TIMESTAMP) {
321 /* Calculate the elapsed time from the two timestamp queries */
322 GLuint64EXT Result0 = 0;
323 assert(stq->pq_begin);
324 pipe->get_query_result(pipe, stq->pq_begin, TRUE, (void *)&Result0);
325 stq->base.Result -= Result0;
326 } else {
327 assert(!stq->pq_begin);
328 }
329
330 return TRUE;
331 }
332
333
334 static void
335 st_WaitQuery(struct gl_context *ctx, struct gl_query_object *q)
336 {
337 struct pipe_context *pipe = st_context(ctx)->pipe;
338 struct st_query_object *stq = st_query_object(q);
339
340 /* this function should only be called if we don't have a ready result */
341 assert(!stq->base.Ready);
342
343 while (!stq->base.Ready &&
344 !get_query_result(pipe, stq, TRUE))
345 {
346 /* nothing */
347 }
348
349 q->Ready = GL_TRUE;
350 }
351
352
353 static void
354 st_CheckQuery(struct gl_context *ctx, struct gl_query_object *q)
355 {
356 struct pipe_context *pipe = st_context(ctx)->pipe;
357 struct st_query_object *stq = st_query_object(q);
358 assert(!q->Ready); /* we should not get called if Ready is TRUE */
359 q->Ready = get_query_result(pipe, stq, FALSE);
360 }
361
362
363 static uint64_t
364 st_GetTimestamp(struct gl_context *ctx)
365 {
366 struct pipe_context *pipe = st_context(ctx)->pipe;
367 struct pipe_screen *screen = pipe->screen;
368
369 /* Prefer the per-screen function */
370 if (screen->get_timestamp) {
371 return screen->get_timestamp(screen);
372 }
373 else {
374 /* Fall back to the per-context function */
375 assert(pipe->get_timestamp);
376 return pipe->get_timestamp(pipe);
377 }
378 }
379
380 static void
381 st_StoreQueryResult(struct gl_context *ctx, struct gl_query_object *q,
382 struct gl_buffer_object *buf, intptr_t offset,
383 GLenum pname, GLenum ptype)
384 {
385 struct pipe_context *pipe = st_context(ctx)->pipe;
386 struct st_query_object *stq = st_query_object(q);
387 struct st_buffer_object *stObj = st_buffer_object(buf);
388 boolean wait = pname == GL_QUERY_RESULT;
389 enum pipe_query_value_type result_type;
390 int index;
391
392 /* GL_QUERY_TARGET is a bit of an extension since it has nothing to
393 * do with the GPU end of the query. Write it in "by hand".
394 */
395 if (pname == GL_QUERY_TARGET) {
396 /* Assume that the data must be LE. The endianness situation wrt CPU and
397 * GPU is incredibly confusing, but the vast majority of GPUs are
398 * LE. When a BE one comes along, this needs some form of resolution.
399 */
400 unsigned data[2] = { CPU_TO_LE32(q->Target), 0 };
401 pipe_buffer_write(pipe, stObj->buffer, offset,
402 (ptype == GL_INT64_ARB ||
403 ptype == GL_UNSIGNED_INT64_ARB) ? 8 : 4,
404 data);
405 return;
406 }
407
408 switch (ptype) {
409 case GL_INT:
410 result_type = PIPE_QUERY_TYPE_I32;
411 break;
412 case GL_UNSIGNED_INT:
413 result_type = PIPE_QUERY_TYPE_U32;
414 break;
415 case GL_INT64_ARB:
416 result_type = PIPE_QUERY_TYPE_I64;
417 break;
418 case GL_UNSIGNED_INT64_ARB:
419 result_type = PIPE_QUERY_TYPE_U64;
420 break;
421 default:
422 unreachable("Unexpected result type");
423 }
424
425 if (pname == GL_QUERY_RESULT_AVAILABLE) {
426 index = -1;
427 } else if (stq->type == PIPE_QUERY_PIPELINE_STATISTICS) {
428 switch (q->Target) {
429 case GL_VERTICES_SUBMITTED_ARB:
430 index = PIPE_STAT_QUERY_IA_VERTICES;
431 break;
432 case GL_PRIMITIVES_SUBMITTED_ARB:
433 index = PIPE_STAT_QUERY_IA_PRIMITIVES;
434 break;
435 case GL_VERTEX_SHADER_INVOCATIONS_ARB:
436 index = PIPE_STAT_QUERY_VS_INVOCATIONS;
437 break;
438 case GL_GEOMETRY_SHADER_INVOCATIONS:
439 index = PIPE_STAT_QUERY_GS_INVOCATIONS;
440 break;
441 case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
442 index = PIPE_STAT_QUERY_GS_PRIMITIVES;
443 break;
444 case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
445 index = PIPE_STAT_QUERY_C_INVOCATIONS;
446 break;
447 case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
448 index = PIPE_STAT_QUERY_C_PRIMITIVES;
449 break;
450 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
451 index = PIPE_STAT_QUERY_PS_INVOCATIONS;
452 break;
453 case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
454 index = PIPE_STAT_QUERY_HS_INVOCATIONS;
455 break;
456 case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
457 index = PIPE_STAT_QUERY_DS_INVOCATIONS;
458 break;
459 case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
460 index = PIPE_STAT_QUERY_CS_INVOCATIONS;
461 break;
462 default:
463 unreachable("Unexpected target");
464 }
465 } else {
466 index = 0;
467 }
468
469 pipe->get_query_result_resource(pipe, stq->pq, wait, result_type, index,
470 stObj->buffer, offset);
471 }
472
473 void st_init_query_functions(struct dd_function_table *functions)
474 {
475 functions->NewQueryObject = st_NewQueryObject;
476 functions->DeleteQuery = st_DeleteQuery;
477 functions->BeginQuery = st_BeginQuery;
478 functions->EndQuery = st_EndQuery;
479 functions->WaitQuery = st_WaitQuery;
480 functions->CheckQuery = st_CheckQuery;
481 functions->GetTimestamp = st_GetTimestamp;
482 functions->StoreQueryResult = st_StoreQueryResult;
483 }