s/Tungsten Graphics/VMware/
[mesa.git] / src / gallium / drivers / softpipe / sp_query.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Author:
29 * Keith Whitwell <keithw@vmware.com>
30 */
31
32 #include "draw/draw_context.h"
33 #include "os/os_time.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_memory.h"
36 #include "sp_context.h"
37 #include "sp_query.h"
38 #include "sp_state.h"
39
40 struct softpipe_query {
41 unsigned type;
42 uint64_t start;
43 uint64_t end;
44 struct pipe_query_data_so_statistics so;
45 struct pipe_query_data_pipeline_statistics stats;
46 };
47
48
49 static struct softpipe_query *softpipe_query( struct pipe_query *p )
50 {
51 return (struct softpipe_query *)p;
52 }
53
54 static struct pipe_query *
55 softpipe_create_query(struct pipe_context *pipe,
56 unsigned type)
57 {
58 struct softpipe_query* sq;
59
60 assert(type == PIPE_QUERY_OCCLUSION_COUNTER ||
61 type == PIPE_QUERY_OCCLUSION_PREDICATE ||
62 type == PIPE_QUERY_TIME_ELAPSED ||
63 type == PIPE_QUERY_SO_STATISTICS ||
64 type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ||
65 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
66 type == PIPE_QUERY_PRIMITIVES_GENERATED ||
67 type == PIPE_QUERY_PIPELINE_STATISTICS ||
68 type == PIPE_QUERY_GPU_FINISHED ||
69 type == PIPE_QUERY_TIMESTAMP ||
70 type == PIPE_QUERY_TIMESTAMP_DISJOINT);
71 sq = CALLOC_STRUCT( softpipe_query );
72 sq->type = type;
73
74 return (struct pipe_query *)sq;
75 }
76
77
78 static void
79 softpipe_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
80 {
81 FREE(q);
82 }
83
84
85 static void
86 softpipe_begin_query(struct pipe_context *pipe, struct pipe_query *q)
87 {
88 struct softpipe_context *softpipe = softpipe_context( pipe );
89 struct softpipe_query *sq = softpipe_query(q);
90
91 switch (sq->type) {
92 case PIPE_QUERY_OCCLUSION_COUNTER:
93 case PIPE_QUERY_OCCLUSION_PREDICATE:
94 sq->start = softpipe->occlusion_count;
95 break;
96 case PIPE_QUERY_TIME_ELAPSED:
97 sq->start = os_time_get_nano();
98 break;
99 case PIPE_QUERY_SO_STATISTICS:
100 sq->so.num_primitives_written = softpipe->so_stats.num_primitives_written;
101 sq->so.primitives_storage_needed = softpipe->so_stats.primitives_storage_needed;
102 break;
103 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
104 sq->end = FALSE;
105 break;
106 case PIPE_QUERY_PRIMITIVES_EMITTED:
107 sq->so.num_primitives_written = softpipe->so_stats.num_primitives_written;
108 break;
109 case PIPE_QUERY_PRIMITIVES_GENERATED:
110 sq->so.primitives_storage_needed = softpipe->so_stats.primitives_storage_needed;
111 break;
112 case PIPE_QUERY_TIMESTAMP:
113 case PIPE_QUERY_GPU_FINISHED:
114 case PIPE_QUERY_TIMESTAMP_DISJOINT:
115 break;
116 case PIPE_QUERY_PIPELINE_STATISTICS:
117 /* reset our cache */
118 if (softpipe->active_statistics_queries == 0) {
119 memset(&softpipe->pipeline_statistics, 0,
120 sizeof(softpipe->pipeline_statistics));
121 }
122 memcpy(&sq->stats, &softpipe->pipeline_statistics,
123 sizeof(sq->stats));
124 softpipe->active_statistics_queries++;
125 break;
126 default:
127 assert(0);
128 break;
129 }
130 softpipe->active_query_count++;
131 softpipe->dirty |= SP_NEW_QUERY;
132 }
133
134
135 static void
136 softpipe_end_query(struct pipe_context *pipe, struct pipe_query *q)
137 {
138 struct softpipe_context *softpipe = softpipe_context( pipe );
139 struct softpipe_query *sq = softpipe_query(q);
140
141 softpipe->active_query_count--;
142 switch (sq->type) {
143 case PIPE_QUERY_OCCLUSION_COUNTER:
144 case PIPE_QUERY_OCCLUSION_PREDICATE:
145 sq->end = softpipe->occlusion_count;
146 break;
147 case PIPE_QUERY_TIMESTAMP:
148 sq->start = 0;
149 /* fall through */
150 case PIPE_QUERY_TIME_ELAPSED:
151 sq->end = os_time_get_nano();
152 break;
153 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
154 sq->so.num_primitives_written =
155 softpipe->so_stats.num_primitives_written - sq->so.num_primitives_written;
156 sq->so.primitives_storage_needed =
157 softpipe->so_stats.primitives_storage_needed - sq->so.primitives_storage_needed;
158 sq->end = sq->so.primitives_storage_needed > sq->so.num_primitives_written;
159 break;
160 case PIPE_QUERY_SO_STATISTICS:
161 sq->so.num_primitives_written =
162 softpipe->so_stats.num_primitives_written - sq->so.num_primitives_written;
163 sq->so.primitives_storage_needed =
164 softpipe->so_stats.primitives_storage_needed - sq->so.primitives_storage_needed;
165 break;
166 case PIPE_QUERY_PRIMITIVES_EMITTED:
167 sq->so.num_primitives_written =
168 softpipe->so_stats.num_primitives_written - sq->so.num_primitives_written;
169 break;
170 case PIPE_QUERY_PRIMITIVES_GENERATED:
171 sq->so.primitives_storage_needed =
172 softpipe->so_stats.primitives_storage_needed - sq->so.primitives_storage_needed;
173 break;
174 case PIPE_QUERY_GPU_FINISHED:
175 case PIPE_QUERY_TIMESTAMP_DISJOINT:
176 break;
177 case PIPE_QUERY_PIPELINE_STATISTICS:
178 sq->stats.ia_vertices =
179 softpipe->pipeline_statistics.ia_vertices - sq->stats.ia_vertices;
180 sq->stats.ia_primitives =
181 softpipe->pipeline_statistics.ia_primitives - sq->stats.ia_primitives;
182 sq->stats.vs_invocations =
183 softpipe->pipeline_statistics.vs_invocations - sq->stats.vs_invocations;
184 sq->stats.gs_invocations =
185 softpipe->pipeline_statistics.gs_invocations - sq->stats.gs_invocations;
186 sq->stats.gs_primitives =
187 softpipe->pipeline_statistics.gs_primitives - sq->stats.gs_primitives;
188 sq->stats.c_invocations =
189 softpipe->pipeline_statistics.c_invocations - sq->stats.c_invocations;
190 sq->stats.c_primitives =
191 softpipe->pipeline_statistics.c_primitives - sq->stats.c_primitives;
192 sq->stats.ps_invocations =
193 softpipe->pipeline_statistics.ps_invocations - sq->stats.ps_invocations;
194
195 softpipe->active_statistics_queries--;
196 break;
197 default:
198 assert(0);
199 break;
200 }
201 softpipe->dirty |= SP_NEW_QUERY;
202 }
203
204
205 static boolean
206 softpipe_get_query_result(struct pipe_context *pipe,
207 struct pipe_query *q,
208 boolean wait,
209 union pipe_query_result *vresult)
210 {
211 struct softpipe_query *sq = softpipe_query(q);
212 uint64_t *result = (uint64_t*)vresult;
213
214 switch (sq->type) {
215 case PIPE_QUERY_SO_STATISTICS: {
216 struct pipe_query_data_so_statistics *stats =
217 (struct pipe_query_data_so_statistics *)vresult;
218 stats->num_primitives_written = sq->so.num_primitives_written;
219 stats->primitives_storage_needed = sq->so.primitives_storage_needed;
220 }
221 break;
222 case PIPE_QUERY_PIPELINE_STATISTICS:
223 memcpy(vresult, &sq->stats,
224 sizeof(struct pipe_query_data_pipeline_statistics));;
225 break;
226 case PIPE_QUERY_GPU_FINISHED:
227 vresult->b = TRUE;
228 break;
229 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
230 vresult->b = sq->end != 0;
231 break;
232 case PIPE_QUERY_TIMESTAMP_DISJOINT: {
233 struct pipe_query_data_timestamp_disjoint *td =
234 (struct pipe_query_data_timestamp_disjoint *)vresult;
235 /* os_get_time_nano return nanoseconds */
236 td->frequency = UINT64_C(1000000000);
237 td->disjoint = FALSE;
238 }
239 break;
240 case PIPE_QUERY_PRIMITIVES_EMITTED:
241 *result = sq->so.num_primitives_written;
242 break;
243 case PIPE_QUERY_PRIMITIVES_GENERATED:
244 *result = sq->so.primitives_storage_needed;
245 break;
246 case PIPE_QUERY_OCCLUSION_PREDICATE:
247 vresult->b = sq->end - sq->start != 0;
248 break;
249 default:
250 *result = sq->end - sq->start;
251 break;
252 }
253 return TRUE;
254 }
255
256
257 /**
258 * Called by rendering function to check rendering is conditional.
259 * \return TRUE if we should render, FALSE if we should skip rendering
260 */
261 boolean
262 softpipe_check_render_cond(struct softpipe_context *sp)
263 {
264 struct pipe_context *pipe = &sp->pipe;
265 boolean b, wait;
266 uint64_t result;
267
268 if (!sp->render_cond_query) {
269 return TRUE; /* no query predicate, draw normally */
270 }
271
272 wait = (sp->render_cond_mode == PIPE_RENDER_COND_WAIT ||
273 sp->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT);
274
275 b = pipe->get_query_result(pipe, sp->render_cond_query, wait,
276 (void*)&result);
277 if (b)
278 return (!result == sp->render_cond_cond);
279 else
280 return TRUE;
281 }
282
283
284 void softpipe_init_query_funcs(struct softpipe_context *softpipe )
285 {
286 softpipe->pipe.create_query = softpipe_create_query;
287 softpipe->pipe.destroy_query = softpipe_destroy_query;
288 softpipe->pipe.begin_query = softpipe_begin_query;
289 softpipe->pipe.end_query = softpipe_end_query;
290 softpipe->pipe.get_query_result = softpipe_get_query_result;
291 }
292
293