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