svga: add some debug_printf() calls in the query object code
[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, unsigned query_type )
78 {
79 struct svga_context *svga = svga_context( pipe );
80 struct svga_screen *svgascreen = svga_screen(pipe->screen);
81 struct svga_winsys_screen *sws = svgascreen->sws;
82 struct svga_query *sq;
83
84 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
85
86 sq = CALLOC_STRUCT(svga_query);
87 if (!sq)
88 goto no_sq;
89
90 switch (query_type) {
91 case PIPE_QUERY_OCCLUSION_COUNTER:
92 sq->svga_type = SVGA3D_QUERYTYPE_OCCLUSION;
93
94 sq->hwbuf = svga_winsys_buffer_create(svga, 1,
95 SVGA_BUFFER_USAGE_PINNED,
96 sizeof *sq->queryResult);
97 if (!sq->hwbuf) {
98 debug_printf("svga: failed to alloc query object!\n");
99 goto no_hwbuf;
100 }
101
102 sq->queryResult = (SVGA3dQueryResult *)
103 sws->buffer_map(sws, sq->hwbuf, PIPE_TRANSFER_WRITE);
104 if (!sq->queryResult) {
105 debug_printf("svga: failed to map query object!\n");
106 goto no_query_result;
107 }
108
109 sq->queryResult->totalSize = sizeof *sq->queryResult;
110 sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
111
112 /* We request the buffer to be pinned and assume it is always mapped.
113 * The reason is that we don't want to wait for fences when checking the
114 * query status.
115 */
116 sws->buffer_unmap(sws, sq->hwbuf);
117 break;
118 case SVGA_QUERY_DRAW_CALLS:
119 case SVGA_QUERY_FALLBACKS:
120 case SVGA_QUERY_MEMORY_USED:
121 break;
122 default:
123 assert(!"unexpected query type in svga_create_query()");
124 }
125
126 sq->type = query_type;
127
128 return &sq->base;
129
130 no_query_result:
131 sws->buffer_destroy(sws, sq->hwbuf);
132 no_hwbuf:
133 FREE(sq);
134 no_sq:
135 return NULL;
136 }
137
138
139 static void
140 svga_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
141 {
142 struct svga_screen *svgascreen = svga_screen(pipe->screen);
143 struct svga_winsys_screen *sws = svgascreen->sws;
144 struct svga_query *sq = svga_query( q );
145
146 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
147
148 switch (sq->type) {
149 case PIPE_QUERY_OCCLUSION_COUNTER:
150 sws->buffer_destroy(sws, sq->hwbuf);
151 sws->fence_reference(sws, &sq->fence, NULL);
152 break;
153 case SVGA_QUERY_DRAW_CALLS:
154 case SVGA_QUERY_FALLBACKS:
155 case SVGA_QUERY_MEMORY_USED:
156 /* nothing */
157 break;
158 default:
159 assert(!"svga: unexpected query type in svga_destroy_query()");
160 }
161
162 FREE(sq);
163 }
164
165
166 static void
167 svga_begin_query(struct pipe_context *pipe, struct pipe_query *q)
168 {
169 struct svga_screen *svgascreen = svga_screen(pipe->screen);
170 struct svga_winsys_screen *sws = svgascreen->sws;
171 struct svga_context *svga = svga_context( pipe );
172 struct svga_query *sq = svga_query( q );
173 enum pipe_error ret;
174
175 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
176
177 /* Need to flush out buffered drawing commands so that they don't
178 * get counted in the query results.
179 */
180 svga_hwtnl_flush_retry(svga);
181
182 switch (sq->type) {
183 case PIPE_QUERY_OCCLUSION_COUNTER:
184 assert(!svga->sq);
185 if (sq->queryResult->state == SVGA3D_QUERYSTATE_PENDING) {
186 /* The application doesn't care for the pending query result.
187 * We cannot let go of the existing buffer and just get a new one
188 * because its storage may be reused for other purposes and clobbered
189 * by the host when it determines the query result. So the only
190 * option here is to wait for the existing query's result -- not a
191 * big deal, given that no sane application would do this.
192 */
193 uint64_t result;
194 svga_get_query_result(pipe, q, TRUE, (void*)&result);
195 assert(sq->queryResult->state != SVGA3D_QUERYSTATE_PENDING);
196 }
197
198 sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
199 sws->fence_reference(sws, &sq->fence, NULL);
200
201 ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
202 if (ret != PIPE_OK) {
203 svga_context_flush(svga, NULL);
204 ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
205 assert(ret == PIPE_OK);
206 }
207
208 svga->sq = sq;
209 break;
210 case SVGA_QUERY_DRAW_CALLS:
211 sq->begin_count = svga->num_draw_calls;
212 break;
213 case SVGA_QUERY_FALLBACKS:
214 sq->begin_count = svga->num_fallbacks;
215 break;
216 case SVGA_QUERY_MEMORY_USED:
217 /* nothing */
218 break;
219 default:
220 assert(!"unexpected query type in svga_begin_query()");
221 }
222 }
223
224
225 static void
226 svga_end_query(struct pipe_context *pipe, struct pipe_query *q)
227 {
228 struct svga_context *svga = svga_context( pipe );
229 struct svga_query *sq = svga_query( q );
230 enum pipe_error ret;
231
232 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
233
234 svga_hwtnl_flush_retry(svga);
235
236 switch (sq->type) {
237 case PIPE_QUERY_OCCLUSION_COUNTER:
238 assert(svga->sq == sq);
239
240 /* Set to PENDING before sending EndQuery. */
241 sq->queryResult->state = SVGA3D_QUERYSTATE_PENDING;
242
243 ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
244 if (ret != PIPE_OK) {
245 svga_context_flush(svga, NULL);
246 ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
247 assert(ret == PIPE_OK);
248 }
249
250 /* TODO: Delay flushing. We don't really need to flush here, just ensure
251 * that there is one flush before svga_get_query_result attempts to get
252 * the result.
253 */
254 svga_context_flush(svga, NULL);
255
256 svga->sq = NULL;
257 break;
258 case SVGA_QUERY_DRAW_CALLS:
259 sq->end_count = svga->num_draw_calls;
260 break;
261 case SVGA_QUERY_FALLBACKS:
262 sq->end_count = svga->num_fallbacks;
263 break;
264 case SVGA_QUERY_MEMORY_USED:
265 /* nothing */
266 break;
267 default:
268 assert(!"unexpected query type in svga_end_query()");
269 }
270 }
271
272
273 static boolean
274 svga_get_query_result(struct pipe_context *pipe,
275 struct pipe_query *q,
276 boolean wait,
277 union pipe_query_result *vresult)
278 {
279 struct svga_context *svga = svga_context( pipe );
280 struct svga_screen *svgascreen = svga_screen( pipe->screen );
281 struct svga_winsys_screen *sws = svgascreen->sws;
282 struct svga_query *sq = svga_query( q );
283 SVGA3dQueryState state;
284 uint64_t *result = (uint64_t *) vresult;
285
286 SVGA_DBG(DEBUG_QUERY, "%s wait: %d\n", __FUNCTION__);
287
288 switch (sq->type) {
289 case PIPE_QUERY_OCCLUSION_COUNTER:
290 /* The query status won't be updated by the host unless
291 * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause
292 * a synchronous wait on the host.
293 */
294 if (!sq->fence) {
295 enum pipe_error ret;
296
297 ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
298 if (ret != PIPE_OK) {
299 svga_context_flush(svga, NULL);
300 ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
301 assert(ret == PIPE_OK);
302 }
303
304 svga_context_flush(svga, &sq->fence);
305
306 assert(sq->fence);
307 }
308
309 state = sq->queryResult->state;
310 if (state == SVGA3D_QUERYSTATE_PENDING) {
311 if (!wait)
312 return FALSE;
313 sws->fence_finish(sws, sq->fence, SVGA_FENCE_FLAG_QUERY);
314 state = sq->queryResult->state;
315 }
316
317 assert(state == SVGA3D_QUERYSTATE_SUCCEEDED ||
318 state == SVGA3D_QUERYSTATE_FAILED);
319
320 *result = (uint64_t) sq->queryResult->result32;
321 break;
322 case SVGA_QUERY_DRAW_CALLS:
323 /* fall-through */
324 case SVGA_QUERY_FALLBACKS:
325 vresult->u64 = sq->end_count - sq->begin_count;
326 break;
327 case SVGA_QUERY_MEMORY_USED:
328 vresult->u64 = svgascreen->total_resource_bytes;
329 break;
330 default:
331 assert(!"unexpected query type in svga_get_query_result");
332 }
333
334 SVGA_DBG(DEBUG_QUERY, "%s result %d\n", __FUNCTION__, (unsigned)*result);
335
336 return TRUE;
337 }
338
339
340 void
341 svga_init_query_functions(struct svga_context *svga)
342 {
343 svga->pipe.create_query = svga_create_query;
344 svga->pipe.destroy_query = svga_destroy_query;
345 svga->pipe.begin_query = svga_begin_query;
346 svga->pipe.end_query = svga_end_query;
347 svga->pipe.get_query_result = svga_get_query_result;
348 }