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
46 struct pipe_query base
;
47 unsigned type
; /**< PIPE_QUERY_x or SVGA_QUERY_x */
48 SVGA3dQueryType svga_type
; /**< SVGA3D_QUERYTYPE_x or unused */
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
;
55 /** For non-GPU SVGA_QUERY_x queries */
56 uint64_t begin_count
, end_count
;
59 /***********************************************************************
60 * Inline conversion functions. These are better-typed than the
61 * macros used previously:
63 static INLINE
struct svga_query
*
64 svga_query( struct pipe_query
*q
)
66 return (struct svga_query
*)q
;
69 static boolean
svga_get_query_result(struct pipe_context
*pipe
,
72 union pipe_query_result
*result
);
74 static struct pipe_query
*svga_create_query( struct pipe_context
*pipe
,
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
;
82 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
84 sq
= CALLOC_STRUCT(svga_query
);
89 case PIPE_QUERY_OCCLUSION_COUNTER
:
90 sq
->svga_type
= SVGA3D_QUERYTYPE_OCCLUSION
;
92 sq
->hwbuf
= svga_winsys_buffer_create(svga
, 1,
93 SVGA_BUFFER_USAGE_PINNED
,
94 sizeof *sq
->queryResult
);
98 sq
->queryResult
= (SVGA3dQueryResult
*)
99 sws
->buffer_map(sws
, sq
->hwbuf
, PIPE_TRANSFER_WRITE
);
100 if (!sq
->queryResult
)
101 goto no_query_result
;
103 sq
->queryResult
->totalSize
= sizeof *sq
->queryResult
;
104 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
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
110 sws
->buffer_unmap(sws
, sq
->hwbuf
);
112 case SVGA_QUERY_DRAW_CALLS
:
113 case SVGA_QUERY_FALLBACKS
:
116 assert(!"unexpected query type in svga_create_query()");
119 sq
->type
= query_type
;
124 sws
->buffer_destroy(sws
, sq
->hwbuf
);
131 static void svga_destroy_query(struct pipe_context
*pipe
,
132 struct pipe_query
*q
)
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
);
138 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
141 case PIPE_QUERY_OCCLUSION_COUNTER
:
142 sws
->buffer_destroy(sws
, sq
->hwbuf
);
143 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
145 case SVGA_QUERY_DRAW_CALLS
:
146 case SVGA_QUERY_FALLBACKS
:
150 assert(!"svga: unexpected query type in svga_destroy_query()");
156 static void svga_begin_query(struct pipe_context
*pipe
,
157 struct pipe_query
*q
)
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
);
165 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
167 /* Need to flush out buffered drawing commands so that they don't
168 * get counted in the query results.
170 svga_hwtnl_flush_retry(svga
);
173 case PIPE_QUERY_OCCLUSION_COUNTER
:
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.
184 svga_get_query_result(pipe
, q
, TRUE
, (void*)&result
);
185 assert(sq
->queryResult
->state
!= SVGA3D_QUERYSTATE_PENDING
);
188 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
189 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
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
);
200 case SVGA_QUERY_DRAW_CALLS
:
201 sq
->begin_count
= svga
->num_draw_calls
;
203 case SVGA_QUERY_FALLBACKS
:
204 sq
->begin_count
= svga
->num_fallbacks
;
207 assert(!"unexpected query type in svga_begin_query()");
211 static void svga_end_query(struct pipe_context
*pipe
,
212 struct pipe_query
*q
)
214 struct svga_context
*svga
= svga_context( pipe
);
215 struct svga_query
*sq
= svga_query( q
);
218 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
220 svga_hwtnl_flush_retry(svga
);
223 case PIPE_QUERY_OCCLUSION_COUNTER
:
224 assert(svga
->sq
== sq
);
226 /* Set to PENDING before sending EndQuery. */
227 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_PENDING
;
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
);
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
239 svga_context_flush(svga
, NULL
);
243 case SVGA_QUERY_DRAW_CALLS
:
244 sq
->end_count
= svga
->num_draw_calls
;
246 case SVGA_QUERY_FALLBACKS
:
247 sq
->end_count
= svga
->num_fallbacks
;
250 assert(!"unexpected query type in svga_end_query()");
254 static boolean
svga_get_query_result(struct pipe_context
*pipe
,
255 struct pipe_query
*q
,
257 union pipe_query_result
*vresult
)
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
;
266 SVGA_DBG(DEBUG_QUERY
, "%s wait: %d\n", __FUNCTION__
);
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.
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
);
284 svga_context_flush(svga
, &sq
->fence
);
289 state
= sq
->queryResult
->state
;
290 if (state
== SVGA3D_QUERYSTATE_PENDING
) {
293 sws
->fence_finish(sws
, sq
->fence
, SVGA_FENCE_FLAG_QUERY
);
294 state
= sq
->queryResult
->state
;
297 assert(state
== SVGA3D_QUERYSTATE_SUCCEEDED
||
298 state
== SVGA3D_QUERYSTATE_FAILED
);
300 *result
= (uint64_t)sq
->queryResult
->result32
;
302 case SVGA_QUERY_DRAW_CALLS
:
304 case SVGA_QUERY_FALLBACKS
:
305 vresult
->u64
= sq
->end_count
- sq
->begin_count
;
308 assert(!"unexpected query type in svga_get_query_result");
311 SVGA_DBG(DEBUG_QUERY
, "%s result %d\n", __FUNCTION__
, (unsigned)*result
);
318 void svga_init_query_functions( struct svga_context
*svga
)
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
;