svga: null out query's hwbuf pointer after destroying
[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 sq->hwbuf = NULL;
152 sws->fence_reference(sws, &sq->fence, NULL);
153 break;
154 case SVGA_QUERY_DRAW_CALLS:
155 case SVGA_QUERY_FALLBACKS:
156 case SVGA_QUERY_MEMORY_USED:
157 /* nothing */
158 break;
159 default:
160 assert(!"svga: unexpected query type in svga_destroy_query()");
161 }
162
163 FREE(sq);
164 }
165
166
167 static void
168 svga_begin_query(struct pipe_context *pipe, struct pipe_query *q)
169 {
170 struct svga_screen *svgascreen = svga_screen(pipe->screen);
171 struct svga_winsys_screen *sws = svgascreen->sws;
172 struct svga_context *svga = svga_context( pipe );
173 struct svga_query *sq = svga_query( q );
174 enum pipe_error ret;
175
176 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
177
178 /* Need to flush out buffered drawing commands so that they don't
179 * get counted in the query results.
180 */
181 svga_hwtnl_flush_retry(svga);
182
183 switch (sq->type) {
184 case PIPE_QUERY_OCCLUSION_COUNTER:
185 assert(!svga->sq);
186 if (sq->queryResult->state == SVGA3D_QUERYSTATE_PENDING) {
187 /* The application doesn't care for the pending query result.
188 * We cannot let go of the existing buffer and just get a new one
189 * because its storage may be reused for other purposes and clobbered
190 * by the host when it determines the query result. So the only
191 * option here is to wait for the existing query's result -- not a
192 * big deal, given that no sane application would do this.
193 */
194 uint64_t result;
195 svga_get_query_result(pipe, q, TRUE, (void*)&result);
196 assert(sq->queryResult->state != SVGA3D_QUERYSTATE_PENDING);
197 }
198
199 sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
200 sws->fence_reference(sws, &sq->fence, NULL);
201
202 ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
203 if (ret != PIPE_OK) {
204 svga_context_flush(svga, NULL);
205 ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
206 assert(ret == PIPE_OK);
207 }
208
209 svga->sq = sq;
210 break;
211 case SVGA_QUERY_DRAW_CALLS:
212 sq->begin_count = svga->num_draw_calls;
213 break;
214 case SVGA_QUERY_FALLBACKS:
215 sq->begin_count = svga->num_fallbacks;
216 break;
217 case SVGA_QUERY_MEMORY_USED:
218 /* nothing */
219 break;
220 default:
221 assert(!"unexpected query type in svga_begin_query()");
222 }
223 }
224
225
226 static void
227 svga_end_query(struct pipe_context *pipe, struct pipe_query *q)
228 {
229 struct svga_context *svga = svga_context( pipe );
230 struct svga_query *sq = svga_query( q );
231 enum pipe_error ret;
232
233 SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
234
235 svga_hwtnl_flush_retry(svga);
236
237 switch (sq->type) {
238 case PIPE_QUERY_OCCLUSION_COUNTER:
239 assert(svga->sq == sq);
240
241 /* Set to PENDING before sending EndQuery. */
242 sq->queryResult->state = SVGA3D_QUERYSTATE_PENDING;
243
244 ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
245 if (ret != PIPE_OK) {
246 svga_context_flush(svga, NULL);
247 ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
248 assert(ret == PIPE_OK);
249 }
250
251 /* TODO: Delay flushing. We don't really need to flush here, just ensure
252 * that there is one flush before svga_get_query_result attempts to get
253 * the result.
254 */
255 svga_context_flush(svga, NULL);
256
257 svga->sq = NULL;
258 break;
259 case SVGA_QUERY_DRAW_CALLS:
260 sq->end_count = svga->num_draw_calls;
261 break;
262 case SVGA_QUERY_FALLBACKS:
263 sq->end_count = svga->num_fallbacks;
264 break;
265 case SVGA_QUERY_MEMORY_USED:
266 /* nothing */
267 break;
268 default:
269 assert(!"unexpected query type in svga_end_query()");
270 }
271 }
272
273
274 static boolean
275 svga_get_query_result(struct pipe_context *pipe,
276 struct pipe_query *q,
277 boolean wait,
278 union pipe_query_result *vresult)
279 {
280 struct svga_context *svga = svga_context( pipe );
281 struct svga_screen *svgascreen = svga_screen( pipe->screen );
282 struct svga_winsys_screen *sws = svgascreen->sws;
283 struct svga_query *sq = svga_query( q );
284 SVGA3dQueryState state;
285 uint64_t *result = (uint64_t *) vresult;
286
287 SVGA_DBG(DEBUG_QUERY, "%s wait: %d\n", __FUNCTION__);
288
289 switch (sq->type) {
290 case PIPE_QUERY_OCCLUSION_COUNTER:
291 /* The query status won't be updated by the host unless
292 * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause
293 * a synchronous wait on the host.
294 */
295 if (!sq->fence) {
296 enum pipe_error ret;
297
298 ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
299 if (ret != PIPE_OK) {
300 svga_context_flush(svga, NULL);
301 ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
302 assert(ret == PIPE_OK);
303 }
304
305 svga_context_flush(svga, &sq->fence);
306
307 assert(sq->fence);
308 }
309
310 state = sq->queryResult->state;
311 if (state == SVGA3D_QUERYSTATE_PENDING) {
312 if (!wait)
313 return FALSE;
314 sws->fence_finish(sws, sq->fence, SVGA_FENCE_FLAG_QUERY);
315 state = sq->queryResult->state;
316 }
317
318 assert(state == SVGA3D_QUERYSTATE_SUCCEEDED ||
319 state == SVGA3D_QUERYSTATE_FAILED);
320
321 *result = (uint64_t) sq->queryResult->result32;
322 break;
323 case SVGA_QUERY_DRAW_CALLS:
324 /* fall-through */
325 case SVGA_QUERY_FALLBACKS:
326 vresult->u64 = sq->end_count - sq->begin_count;
327 break;
328 case SVGA_QUERY_MEMORY_USED:
329 vresult->u64 = svgascreen->total_resource_bytes;
330 break;
331 default:
332 assert(!"unexpected query type in svga_get_query_result");
333 }
334
335 SVGA_DBG(DEBUG_QUERY, "%s result %d\n", __FUNCTION__, (unsigned)*result);
336
337 return TRUE;
338 }
339
340
341 void
342 svga_init_query_functions(struct svga_context *svga)
343 {
344 svga->pipe.create_query = svga_create_query;
345 svga->pipe.destroy_query = svga_destroy_query;
346 svga->pipe.begin_query = svga_begin_query;
347 svga->pipe.end_query = svga_end_query;
348 svga->pipe.get_query_result = svga_get_query_result;
349 }