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
, unsigned query_type
)
79 struct svga_context
*svga
= svga_context( pipe
);
80 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
81 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
82 struct svga_query
*sq
;
84 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
86 sq
= CALLOC_STRUCT(svga_query
);
91 case PIPE_QUERY_OCCLUSION_COUNTER
:
92 sq
->svga_type
= SVGA3D_QUERYTYPE_OCCLUSION
;
94 sq
->hwbuf
= svga_winsys_buffer_create(svga
, 1,
95 SVGA_BUFFER_USAGE_PINNED
,
96 sizeof *sq
->queryResult
);
98 debug_printf("svga: failed to alloc query object!\n");
102 sq
->queryResult
= (SVGA3dQueryResult
*)
103 sws
->buffer_map(sws
, sq
->hwbuf
, PIPE_TRANSFER_WRITE
);
104 if (!sq
->queryResult
) {
105 debug_printf("svga: failed to map query object!\n");
106 goto no_query_result
;
109 sq
->queryResult
->totalSize
= sizeof *sq
->queryResult
;
110 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
112 /* We request the buffer to be pinned and assume it is always mapped.
113 * The reason is that we don't want to wait for fences when checking the
116 sws
->buffer_unmap(sws
, sq
->hwbuf
);
118 case SVGA_QUERY_DRAW_CALLS
:
119 case SVGA_QUERY_FALLBACKS
:
120 case SVGA_QUERY_MEMORY_USED
:
123 assert(!"unexpected query type in svga_create_query()");
126 sq
->type
= query_type
;
131 sws
->buffer_destroy(sws
, sq
->hwbuf
);
140 svga_destroy_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
142 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
143 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
144 struct svga_query
*sq
= svga_query( q
);
146 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
149 case PIPE_QUERY_OCCLUSION_COUNTER
:
150 sws
->buffer_destroy(sws
, sq
->hwbuf
);
152 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
154 case SVGA_QUERY_DRAW_CALLS
:
155 case SVGA_QUERY_FALLBACKS
:
156 case SVGA_QUERY_MEMORY_USED
:
160 assert(!"svga: unexpected query type in svga_destroy_query()");
168 svga_begin_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
170 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
171 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
172 struct svga_context
*svga
= svga_context( pipe
);
173 struct svga_query
*sq
= svga_query( q
);
176 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
178 /* Need to flush out buffered drawing commands so that they don't
179 * get counted in the query results.
181 svga_hwtnl_flush_retry(svga
);
184 case PIPE_QUERY_OCCLUSION_COUNTER
:
186 if (sq
->queryResult
->state
== SVGA3D_QUERYSTATE_PENDING
) {
187 /* The application doesn't care for the pending query result.
188 * We cannot let go of the existing buffer and just get a new one
189 * because its storage may be reused for other purposes and clobbered
190 * by the host when it determines the query result. So the only
191 * option here is to wait for the existing query's result -- not a
192 * big deal, given that no sane application would do this.
195 svga_get_query_result(pipe
, q
, TRUE
, (void*)&result
);
196 assert(sq
->queryResult
->state
!= SVGA3D_QUERYSTATE_PENDING
);
199 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
200 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
202 ret
= SVGA3D_BeginQuery(svga
->swc
, sq
->svga_type
);
203 if (ret
!= PIPE_OK
) {
204 svga_context_flush(svga
, NULL
);
205 ret
= SVGA3D_BeginQuery(svga
->swc
, sq
->svga_type
);
206 assert(ret
== PIPE_OK
);
211 case SVGA_QUERY_DRAW_CALLS
:
212 sq
->begin_count
= svga
->num_draw_calls
;
214 case SVGA_QUERY_FALLBACKS
:
215 sq
->begin_count
= svga
->num_fallbacks
;
217 case SVGA_QUERY_MEMORY_USED
:
221 assert(!"unexpected query type in svga_begin_query()");
227 svga_end_query(struct pipe_context
*pipe
, struct pipe_query
*q
)
229 struct svga_context
*svga
= svga_context( pipe
);
230 struct svga_query
*sq
= svga_query( q
);
233 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
235 svga_hwtnl_flush_retry(svga
);
238 case PIPE_QUERY_OCCLUSION_COUNTER
:
239 assert(svga
->sq
== sq
);
241 /* Set to PENDING before sending EndQuery. */
242 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_PENDING
;
244 ret
= SVGA3D_EndQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
245 if (ret
!= PIPE_OK
) {
246 svga_context_flush(svga
, NULL
);
247 ret
= SVGA3D_EndQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
248 assert(ret
== PIPE_OK
);
251 /* TODO: Delay flushing. We don't really need to flush here, just ensure
252 * that there is one flush before svga_get_query_result attempts to get
255 svga_context_flush(svga
, NULL
);
259 case SVGA_QUERY_DRAW_CALLS
:
260 sq
->end_count
= svga
->num_draw_calls
;
262 case SVGA_QUERY_FALLBACKS
:
263 sq
->end_count
= svga
->num_fallbacks
;
265 case SVGA_QUERY_MEMORY_USED
:
269 assert(!"unexpected query type in svga_end_query()");
275 svga_get_query_result(struct pipe_context
*pipe
,
276 struct pipe_query
*q
,
278 union pipe_query_result
*vresult
)
280 struct svga_context
*svga
= svga_context( pipe
);
281 struct svga_screen
*svgascreen
= svga_screen( pipe
->screen
);
282 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
283 struct svga_query
*sq
= svga_query( q
);
284 SVGA3dQueryState state
;
285 uint64_t *result
= (uint64_t *) vresult
;
287 SVGA_DBG(DEBUG_QUERY
, "%s wait: %d\n", __FUNCTION__
);
290 case PIPE_QUERY_OCCLUSION_COUNTER
:
291 /* The query status won't be updated by the host unless
292 * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause
293 * a synchronous wait on the host.
298 ret
= SVGA3D_WaitForQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
299 if (ret
!= PIPE_OK
) {
300 svga_context_flush(svga
, NULL
);
301 ret
= SVGA3D_WaitForQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
302 assert(ret
== PIPE_OK
);
305 svga_context_flush(svga
, &sq
->fence
);
310 state
= sq
->queryResult
->state
;
311 if (state
== SVGA3D_QUERYSTATE_PENDING
) {
314 sws
->fence_finish(sws
, sq
->fence
, SVGA_FENCE_FLAG_QUERY
);
315 state
= sq
->queryResult
->state
;
318 assert(state
== SVGA3D_QUERYSTATE_SUCCEEDED
||
319 state
== SVGA3D_QUERYSTATE_FAILED
);
321 *result
= (uint64_t) sq
->queryResult
->result32
;
323 case SVGA_QUERY_DRAW_CALLS
:
325 case SVGA_QUERY_FALLBACKS
:
326 vresult
->u64
= sq
->end_count
- sq
->begin_count
;
328 case SVGA_QUERY_MEMORY_USED
:
329 vresult
->u64
= svgascreen
->total_resource_bytes
;
332 assert(!"unexpected query type in svga_get_query_result");
335 SVGA_DBG(DEBUG_QUERY
, "%s result %d\n", __FUNCTION__
, (unsigned)*result
);
342 svga_init_query_functions(struct svga_context
*svga
)
344 svga
->pipe
.create_query
= svga_create_query
;
345 svga
->pipe
.destroy_query
= svga_destroy_query
;
346 svga
->pipe
.begin_query
= svga_begin_query
;
347 svga
->pipe
.end_query
= svga_end_query
;
348 svga
->pipe
.get_query_result
= svga_get_query_result
;