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