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