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