gallium: add an index argument to create_query
[mesa.git] / src / gallium / drivers / svga / svga_pipe_query.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "util/u_memory.h"
29
30 #include "svga_cmd.h"
31 #include "svga_context.h"
32 #include "svga_screen.h"
33 #include "svga_resource_buffer.h"
34 #include "svga_winsys.h"
35 #include "svga_debug.h"
36
37
38 /* Fixme: want a public base class for all pipe structs, even if there
39 * isn't much in them.
40 */
41 struct pipe_query {
42 int dummy;
43 };
44
45
46 struct svga_query {
47 struct pipe_query base;
48 unsigned type; /**< PIPE_QUERY_x or SVGA_QUERY_x */
49 SVGA3dQueryType svga_type; /**< SVGA3D_QUERYTYPE_x or unused */
50
51 /** For PIPE_QUERY_OCCLUSION_COUNTER / SVGA3D_QUERYTYPE_OCCLUSION */
52 struct svga_winsys_buffer *hwbuf;
53 volatile SVGA3dQueryResult *queryResult;
54 struct pipe_fence_handle *fence;
55
56 /** For non-GPU SVGA_QUERY_x queries */
57 uint64_t begin_count, end_count;
58 };
59
60
61 /** cast wrapper */
62 static INLINE struct svga_query *
63 svga_query( struct pipe_query *q )
64 {
65 return (struct svga_query *)q;
66 }
67
68
69 static boolean
70 svga_get_query_result(struct pipe_context *pipe,
71 struct pipe_query *q,
72 boolean wait,
73 union pipe_query_result *result);
74
75
76 static struct pipe_query *
77 svga_create_query(struct pipe_context *pipe,
78 unsigned query_type,
79 unsigned index)
80 {
81 struct svga_context *svga = svga_context( pipe );
82 struct svga_screen *svgascreen = svga_screen(pipe->screen);
83 struct svga_winsys_screen *sws = svgascreen->sws;
84 struct svga_query *sq;
85
86 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
87
88 sq = CALLOC_STRUCT(svga_query);
89 if (!sq)
90 goto no_sq;
91
92 switch (query_type) {
93 case PIPE_QUERY_OCCLUSION_COUNTER:
94 sq->svga_type = SVGA3D_QUERYTYPE_OCCLUSION;
95
96 sq->hwbuf = svga_winsys_buffer_create(svga, 1,
97 SVGA_BUFFER_USAGE_PINNED,
98 sizeof *sq->queryResult);
99 if (!sq->hwbuf) {
100 debug_printf("svga: failed to alloc query object!\n");
101 goto no_hwbuf;
102 }
103
104 sq->queryResult = (SVGA3dQueryResult *)
105 sws->buffer_map(sws, sq->hwbuf, PIPE_TRANSFER_WRITE);
106 if (!sq->queryResult) {
107 debug_printf("svga: failed to map query object!\n");
108 goto no_query_result;
109 }
110
111 sq->queryResult->totalSize = sizeof *sq->queryResult;
112 sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
113
114 /* We request the buffer to be pinned and assume it is always mapped.
115 * The reason is that we don't want to wait for fences when checking the
116 * query status.
117 */
118 sws->buffer_unmap(sws, sq->hwbuf);
119 break;
120 case SVGA_QUERY_DRAW_CALLS:
121 case SVGA_QUERY_FALLBACKS:
122 case SVGA_QUERY_MEMORY_USED:
123 break;
124 default:
125 assert(!"unexpected query type in svga_create_query()");
126 }
127
128 sq->type = query_type;
129
130 return &sq->base;
131
132 no_query_result:
133 sws->buffer_destroy(sws, sq->hwbuf);
134 no_hwbuf:
135 FREE(sq);
136 no_sq:
137 return NULL;
138 }
139
140
141 static void
142 svga_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
143 {
144 struct svga_screen *svgascreen = svga_screen(pipe->screen);
145 struct svga_winsys_screen *sws = svgascreen->sws;
146 struct svga_query *sq = svga_query( q );
147
148 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
149
150 switch (sq->type) {
151 case PIPE_QUERY_OCCLUSION_COUNTER:
152 sws->buffer_destroy(sws, sq->hwbuf);
153 sq->hwbuf = NULL;
154 sws->fence_reference(sws, &sq->fence, NULL);
155 break;
156 case SVGA_QUERY_DRAW_CALLS:
157 case SVGA_QUERY_FALLBACKS:
158 case SVGA_QUERY_MEMORY_USED:
159 /* nothing */
160 break;
161 default:
162 assert(!"svga: unexpected query type in svga_destroy_query()");
163 }
164
165 FREE(sq);
166 }
167
168
169 static void
170 svga_begin_query(struct pipe_context *pipe, struct pipe_query *q)
171 {
172 struct svga_screen *svgascreen = svga_screen(pipe->screen);
173 struct svga_winsys_screen *sws = svgascreen->sws;
174 struct svga_context *svga = svga_context( pipe );
175 struct svga_query *sq = svga_query( q );
176 enum pipe_error ret;
177
178 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
179
180 /* Need to flush out buffered drawing commands so that they don't
181 * get counted in the query results.
182 */
183 svga_hwtnl_flush_retry(svga);
184
185 switch (sq->type) {
186 case PIPE_QUERY_OCCLUSION_COUNTER:
187 assert(!svga->sq);
188 if (sq->queryResult->state == SVGA3D_QUERYSTATE_PENDING) {
189 /* The application doesn't care for the pending query result.
190 * We cannot let go of the existing buffer and just get a new one
191 * because its storage may be reused for other purposes and clobbered
192 * by the host when it determines the query result. So the only
193 * option here is to wait for the existing query's result -- not a
194 * big deal, given that no sane application would do this.
195 */
196 uint64_t result;
197 svga_get_query_result(pipe, q, TRUE, (void*)&result);
198 assert(sq->queryResult->state != SVGA3D_QUERYSTATE_PENDING);
199 }
200
201 sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
202 sws->fence_reference(sws, &sq->fence, NULL);
203
204 ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
205 if (ret != PIPE_OK) {
206 svga_context_flush(svga, NULL);
207 ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
208 assert(ret == PIPE_OK);
209 }
210
211 svga->sq = sq;
212 break;
213 case SVGA_QUERY_DRAW_CALLS:
214 sq->begin_count = svga->num_draw_calls;
215 break;
216 case SVGA_QUERY_FALLBACKS:
217 sq->begin_count = svga->num_fallbacks;
218 break;
219 case SVGA_QUERY_MEMORY_USED:
220 /* nothing */
221 break;
222 default:
223 assert(!"unexpected query type in svga_begin_query()");
224 }
225 }
226
227
228 static void
229 svga_end_query(struct pipe_context *pipe, struct pipe_query *q)
230 {
231 struct svga_context *svga = svga_context( pipe );
232 struct svga_query *sq = svga_query( q );
233 enum pipe_error ret;
234
235 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
236
237 svga_hwtnl_flush_retry(svga);
238
239 switch (sq->type) {
240 case PIPE_QUERY_OCCLUSION_COUNTER:
241 assert(svga->sq == sq);
242
243 /* Set to PENDING before sending EndQuery. */
244 sq->queryResult->state = SVGA3D_QUERYSTATE_PENDING;
245
246 ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
247 if (ret != PIPE_OK) {
248 svga_context_flush(svga, NULL);
249 ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
250 assert(ret == PIPE_OK);
251 }
252
253 /* TODO: Delay flushing. We don't really need to flush here, just ensure
254 * that there is one flush before svga_get_query_result attempts to get
255 * the result.
256 */
257 svga_context_flush(svga, NULL);
258
259 svga->sq = NULL;
260 break;
261 case SVGA_QUERY_DRAW_CALLS:
262 sq->end_count = svga->num_draw_calls;
263 break;
264 case SVGA_QUERY_FALLBACKS:
265 sq->end_count = svga->num_fallbacks;
266 break;
267 case SVGA_QUERY_MEMORY_USED:
268 /* nothing */
269 break;
270 default:
271 assert(!"unexpected query type in svga_end_query()");
272 }
273 }
274
275
276 static boolean
277 svga_get_query_result(struct pipe_context *pipe,
278 struct pipe_query *q,
279 boolean wait,
280 union pipe_query_result *vresult)
281 {
282 struct svga_context *svga = svga_context( pipe );
283 struct svga_screen *svgascreen = svga_screen( pipe->screen );
284 struct svga_winsys_screen *sws = svgascreen->sws;
285 struct svga_query *sq = svga_query( q );
286 SVGA3dQueryState state;
287 uint64_t *result = (uint64_t *) vresult;
288
289 SVGA_DBG(DEBUG_QUERY, "%s wait: %d\n", __FUNCTION__);
290
291 switch (sq->type) {
292 case PIPE_QUERY_OCCLUSION_COUNTER:
293 /* The query status won't be updated by the host unless
294 * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause
295 * a synchronous wait on the host.
296 */
297 if (!sq->fence) {
298 enum pipe_error ret;
299
300 ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
301 if (ret != PIPE_OK) {
302 svga_context_flush(svga, NULL);
303 ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
304 assert(ret == PIPE_OK);
305 }
306
307 svga_context_flush(svga, &sq->fence);
308
309 assert(sq->fence);
310 }
311
312 state = sq->queryResult->state;
313 if (state == SVGA3D_QUERYSTATE_PENDING) {
314 if (!wait)
315 return FALSE;
316 sws->fence_finish(sws, sq->fence, SVGA_FENCE_FLAG_QUERY);
317 state = sq->queryResult->state;
318 }
319
320 assert(state == SVGA3D_QUERYSTATE_SUCCEEDED ||
321 state == SVGA3D_QUERYSTATE_FAILED);
322
323 *result = (uint64_t) sq->queryResult->result32;
324 break;
325 case SVGA_QUERY_DRAW_CALLS:
326 /* fall-through */
327 case SVGA_QUERY_FALLBACKS:
328 vresult->u64 = sq->end_count - sq->begin_count;
329 break;
330 case SVGA_QUERY_MEMORY_USED:
331 vresult->u64 = svgascreen->total_resource_bytes;
332 break;
333 default:
334 assert(!"unexpected query type in svga_get_query_result");
335 }
336
337 SVGA_DBG(DEBUG_QUERY, "%s result %d\n", __FUNCTION__, (unsigned)*result);
338
339 return TRUE;
340 }
341
342
343 void
344 svga_init_query_functions(struct svga_context *svga)
345 {
346 svga->pipe.create_query = svga_create_query;
347 svga->pipe.destroy_query = svga_destroy_query;
348 svga->pipe.begin_query = svga_begin_query;
349 svga->pipe.end_query = svga_end_query;
350 svga->pipe.get_query_result = svga_get_query_result;
351 }