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