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