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