1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "util/u_memory.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"
38 /* Fixme: want a public base class for all pipe structs, even if there
47 struct pipe_query base
;
48 unsigned type
; /**< PIPE_QUERY_x or SVGA_QUERY_x */
49 SVGA3dQueryType svga_type
; /**< SVGA3D_QUERYTYPE_x or unused */
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
;
56 /** For non-GPU SVGA_QUERY_x queries */
57 uint64_t begin_count
, end_count
;
62 static inline struct svga_query
*
63 svga_query( struct pipe_query
*q
)
65 return (struct svga_query
*)q
;
70 svga_get_query_result(struct pipe_context
*pipe
,
73 union pipe_query_result
*result
);
76 static struct pipe_query
*
77 svga_create_query(struct pipe_context
*pipe
,
81 struct svga_context
*svga
= svga_context( pipe
);
82 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
83 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
84 struct svga_query
*sq
;
86 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
88 sq
= CALLOC_STRUCT(svga_query
);
93 case PIPE_QUERY_OCCLUSION_COUNTER
:
94 sq
->svga_type
= SVGA3D_QUERYTYPE_OCCLUSION
;
96 sq
->hwbuf
= svga_winsys_buffer_create(svga
, 1,
97 SVGA_BUFFER_USAGE_PINNED
,
98 sizeof *sq
->queryResult
);
100 debug_printf("svga: failed to alloc query object!\n");
104 sq
->queryResult
= (SVGA3dQueryResult
*)
105 sws
->buffer_map(sws
, sq
->hwbuf
, PIPE_TRANSFER_WRITE
);
106 if (!sq
->queryResult
) {
107 debug_printf("svga: failed to map query object!\n");
108 goto no_query_result
;
111 sq
->queryResult
->totalSize
= sizeof *sq
->queryResult
;
112 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
114 /* We request the buffer to be pinned and assume it is always mapped.
115 * The reason is that we don't want to wait for fences when checking the
118 sws
->buffer_unmap(sws
, sq
->hwbuf
);
120 case SVGA_QUERY_DRAW_CALLS
:
121 case SVGA_QUERY_FALLBACKS
:
122 case SVGA_QUERY_MEMORY_USED
:
125 assert(!"unexpected query type in svga_create_query()");
128 sq
->type
= query_type
;
133 sws
->buffer_destroy(sws
, sq
->hwbuf
);
142 svga_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
144 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
145 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
146 struct svga_query
*sq
= svga_query( q
);
148 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
151 case PIPE_QUERY_OCCLUSION_COUNTER
:
152 sws
->buffer_destroy(sws
, sq
->hwbuf
);
154 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
156 case SVGA_QUERY_DRAW_CALLS
:
157 case SVGA_QUERY_FALLBACKS
:
158 case SVGA_QUERY_MEMORY_USED
:
162 assert(!"svga: unexpected query type in svga_destroy_query()");
170 svga_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
172 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
173 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
174 struct svga_context
*svga
= svga_context( pipe
);
175 struct svga_query
*sq
= svga_query( q
);
178 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
180 /* Need to flush out buffered drawing commands so that they don't
181 * get counted in the query results.
183 svga_hwtnl_flush_retry(svga
);
186 case PIPE_QUERY_OCCLUSION_COUNTER
:
188 if (sq
->queryResult
->state
== SVGA3D_QUERYSTATE_PENDING
) {
189 /* The application doesn't care for the pending query result.
190 * We cannot let go of the existing buffer and just get a new one
191 * because its storage may be reused for other purposes and clobbered
192 * by the host when it determines the query result. So the only
193 * option here is to wait for the existing query's result -- not a
194 * big deal, given that no sane application would do this.
197 svga_get_query_result(pipe
, q
, TRUE
, (void*)&result
);
198 assert(sq
->queryResult
->state
!= SVGA3D_QUERYSTATE_PENDING
);
201 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
202 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
204 ret
= SVGA3D_BeginQuery(svga
->swc
, sq
->svga_type
);
205 if (ret
!= PIPE_OK
) {
206 svga_context_flush(svga
, NULL
);
207 ret
= SVGA3D_BeginQuery(svga
->swc
, sq
->svga_type
);
208 assert(ret
== PIPE_OK
);
213 case SVGA_QUERY_DRAW_CALLS
:
214 sq
->begin_count
= svga
->num_draw_calls
;
216 case SVGA_QUERY_FALLBACKS
:
217 sq
->begin_count
= svga
->num_fallbacks
;
219 case SVGA_QUERY_MEMORY_USED
:
223 assert(!"unexpected query type in svga_begin_query()");
230 svga_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
232 struct svga_context
*svga
= svga_context( pipe
);
233 struct svga_query
*sq
= svga_query( q
);
236 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
238 svga_hwtnl_flush_retry(svga
);
241 case PIPE_QUERY_OCCLUSION_COUNTER
:
242 assert(svga
->sq
== sq
);
244 /* Set to PENDING before sending EndQuery. */
245 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_PENDING
;
247 ret
= SVGA3D_EndQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
248 if (ret
!= PIPE_OK
) {
249 svga_context_flush(svga
, NULL
);
250 ret
= SVGA3D_EndQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
251 assert(ret
== PIPE_OK
);
254 /* TODO: Delay flushing. We don't really need to flush here, just ensure
255 * that there is one flush before svga_get_query_result attempts to get
258 svga_context_flush(svga
, NULL
);
262 case SVGA_QUERY_DRAW_CALLS
:
263 sq
->end_count
= svga
->num_draw_calls
;
265 case SVGA_QUERY_FALLBACKS
:
266 sq
->end_count
= svga
->num_fallbacks
;
268 case SVGA_QUERY_MEMORY_USED
:
272 assert(!"unexpected query type in svga_end_query()");
278 svga_get_query_result(struct pipe_context
*pipe
,
279 struct pipe_query
*q
,
281 union pipe_query_result
*vresult
)
283 struct svga_context
*svga
= svga_context( pipe
);
284 struct svga_screen
*svgascreen
= svga_screen( pipe
->screen
);
285 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
286 struct svga_query
*sq
= svga_query( q
);
287 SVGA3dQueryState state
;
288 uint64_t *result
= (uint64_t *) vresult
;
290 SVGA_DBG(DEBUG_QUERY
, "%s wait: %d\n", __FUNCTION__
);
293 case PIPE_QUERY_OCCLUSION_COUNTER
:
294 /* The query status won't be updated by the host unless
295 * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause
296 * a synchronous wait on the host.
301 ret
= SVGA3D_WaitForQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
302 if (ret
!= PIPE_OK
) {
303 svga_context_flush(svga
, NULL
);
304 ret
= SVGA3D_WaitForQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
305 assert(ret
== PIPE_OK
);
308 svga_context_flush(svga
, &sq
->fence
);
313 state
= sq
->queryResult
->state
;
314 if (state
== SVGA3D_QUERYSTATE_PENDING
) {
317 sws
->fence_finish(sws
, sq
->fence
, SVGA_FENCE_FLAG_QUERY
);
318 state
= sq
->queryResult
->state
;
321 assert(state
== SVGA3D_QUERYSTATE_SUCCEEDED
||
322 state
== SVGA3D_QUERYSTATE_FAILED
);
324 *result
= (uint64_t) sq
->queryResult
->result32
;
326 case SVGA_QUERY_DRAW_CALLS
:
328 case SVGA_QUERY_FALLBACKS
:
329 vresult
->u64
= sq
->end_count
- sq
->begin_count
;
331 case SVGA_QUERY_MEMORY_USED
:
332 vresult
->u64
= svgascreen
->total_resource_bytes
;
335 assert(!"unexpected query type in svga_get_query_result");
338 SVGA_DBG(DEBUG_QUERY
, "%s result %d\n", __FUNCTION__
, (unsigned)*result
);
345 svga_init_query_functions(struct svga_context
*svga
)
347 svga
->pipe
.create_query
= svga_create_query
;
348 svga
->pipe
.destroy_query
= svga_destroy_query
;
349 svga
->pipe
.begin_query
= svga_begin_query
;
350 svga
->pipe
.end_query
= svga_end_query
;
351 svga
->pipe
.get_query_result
= svga_get_query_result
;