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
:
114 case SVGA_QUERY_MEMORY_USED
:
117 assert(!"unexpected query type in svga_create_query()");
120 sq
->type
= query_type
;
125 sws
->buffer_destroy(sws
, sq
->hwbuf
);
132 static void svga_destroy_query(struct pipe_context
*pipe
,
133 struct pipe_query
*q
)
135 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
136 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
137 struct svga_query
*sq
= svga_query( q
);
139 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
142 case PIPE_QUERY_OCCLUSION_COUNTER
:
143 sws
->buffer_destroy(sws
, sq
->hwbuf
);
144 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
146 case SVGA_QUERY_DRAW_CALLS
:
147 case SVGA_QUERY_FALLBACKS
:
148 case SVGA_QUERY_MEMORY_USED
:
152 assert(!"svga: unexpected query type in svga_destroy_query()");
158 static void svga_begin_query(struct pipe_context
*pipe
,
159 struct pipe_query
*q
)
161 struct svga_screen
*svgascreen
= svga_screen(pipe
->screen
);
162 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
163 struct svga_context
*svga
= svga_context( pipe
);
164 struct svga_query
*sq
= svga_query( q
);
167 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
169 /* Need to flush out buffered drawing commands so that they don't
170 * get counted in the query results.
172 svga_hwtnl_flush_retry(svga
);
175 case PIPE_QUERY_OCCLUSION_COUNTER
:
177 if (sq
->queryResult
->state
== SVGA3D_QUERYSTATE_PENDING
) {
178 /* The application doesn't care for the pending query result. We cannot
179 * let go the existing buffer and just get a new one because its storage
180 * may be reused for other purposes and clobbered by the host when it
181 * determines the query result. So the only option here is to wait for
182 * the existing query's result -- not a big deal, given that no sane
183 * application would do this.
186 svga_get_query_result(pipe
, q
, TRUE
, (void*)&result
);
187 assert(sq
->queryResult
->state
!= SVGA3D_QUERYSTATE_PENDING
);
190 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_NEW
;
191 sws
->fence_reference(sws
, &sq
->fence
, NULL
);
193 ret
= SVGA3D_BeginQuery(svga
->swc
, sq
->svga_type
);
194 if (ret
!= PIPE_OK
) {
195 svga_context_flush(svga
, NULL
);
196 ret
= SVGA3D_BeginQuery(svga
->swc
, sq
->svga_type
);
197 assert(ret
== PIPE_OK
);
202 case SVGA_QUERY_DRAW_CALLS
:
203 sq
->begin_count
= svga
->num_draw_calls
;
205 case SVGA_QUERY_FALLBACKS
:
206 sq
->begin_count
= svga
->num_fallbacks
;
208 case SVGA_QUERY_MEMORY_USED
:
212 assert(!"unexpected query type in svga_begin_query()");
216 static void svga_end_query(struct pipe_context
*pipe
,
217 struct pipe_query
*q
)
219 struct svga_context
*svga
= svga_context( pipe
);
220 struct svga_query
*sq
= svga_query( q
);
223 SVGA_DBG(DEBUG_QUERY
, "%s\n", __FUNCTION__
);
225 svga_hwtnl_flush_retry(svga
);
228 case PIPE_QUERY_OCCLUSION_COUNTER
:
229 assert(svga
->sq
== sq
);
231 /* Set to PENDING before sending EndQuery. */
232 sq
->queryResult
->state
= SVGA3D_QUERYSTATE_PENDING
;
234 ret
= SVGA3D_EndQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
235 if (ret
!= PIPE_OK
) {
236 svga_context_flush(svga
, NULL
);
237 ret
= SVGA3D_EndQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
238 assert(ret
== PIPE_OK
);
241 /* TODO: Delay flushing. We don't really need to flush here, just ensure
242 * that there is one flush before svga_get_query_result attempts to get the
244 svga_context_flush(svga
, NULL
);
248 case SVGA_QUERY_DRAW_CALLS
:
249 sq
->end_count
= svga
->num_draw_calls
;
251 case SVGA_QUERY_FALLBACKS
:
252 sq
->end_count
= svga
->num_fallbacks
;
254 case SVGA_QUERY_MEMORY_USED
:
258 assert(!"unexpected query type in svga_end_query()");
262 static boolean
svga_get_query_result(struct pipe_context
*pipe
,
263 struct pipe_query
*q
,
265 union pipe_query_result
*vresult
)
267 struct svga_context
*svga
= svga_context( pipe
);
268 struct svga_screen
*svgascreen
= svga_screen( pipe
->screen
);
269 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
270 struct svga_query
*sq
= svga_query( q
);
271 SVGA3dQueryState state
;
272 uint64_t *result
= (uint64_t*)vresult
;
274 SVGA_DBG(DEBUG_QUERY
, "%s wait: %d\n", __FUNCTION__
);
277 case PIPE_QUERY_OCCLUSION_COUNTER
:
278 /* The query status won't be updated by the host unless
279 * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause a
280 * synchronous wait on the host.
285 ret
= SVGA3D_WaitForQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
286 if (ret
!= PIPE_OK
) {
287 svga_context_flush(svga
, NULL
);
288 ret
= SVGA3D_WaitForQuery( svga
->swc
, sq
->svga_type
, sq
->hwbuf
);
289 assert(ret
== PIPE_OK
);
292 svga_context_flush(svga
, &sq
->fence
);
297 state
= sq
->queryResult
->state
;
298 if (state
== SVGA3D_QUERYSTATE_PENDING
) {
301 sws
->fence_finish(sws
, sq
->fence
, SVGA_FENCE_FLAG_QUERY
);
302 state
= sq
->queryResult
->state
;
305 assert(state
== SVGA3D_QUERYSTATE_SUCCEEDED
||
306 state
== SVGA3D_QUERYSTATE_FAILED
);
308 *result
= (uint64_t)sq
->queryResult
->result32
;
310 case SVGA_QUERY_DRAW_CALLS
:
312 case SVGA_QUERY_FALLBACKS
:
313 vresult
->u64
= sq
->end_count
- sq
->begin_count
;
315 case SVGA_QUERY_MEMORY_USED
:
316 vresult
->u64
= svgascreen
->total_resource_bytes
;
319 assert(!"unexpected query type in svga_get_query_result");
322 SVGA_DBG(DEBUG_QUERY
, "%s result %d\n", __FUNCTION__
, (unsigned)*result
);
329 void svga_init_query_functions( struct svga_context
*svga
)
331 svga
->pipe
.create_query
= svga_create_query
;
332 svga
->pipe
.destroy_query
= svga_destroy_query
;
333 svga
->pipe
.begin_query
= svga_begin_query
;
334 svga
->pipe
.end_query
= svga_end_query
;
335 svga
->pipe
.get_query_result
= svga_get_query_result
;