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