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