softpipe: handle all queries, and change for the new disjoint semantics
[mesa.git] / src / gallium / drivers / softpipe / sp_query.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <keith@tungstengraphics.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 unsigned num_primitives_generated;
46
47 struct pipe_query_data_pipeline_statistics stats;
48 };
49
50
51 static struct softpipe_query *softpipe_query( struct pipe_query *p )
52 {
53 return (struct softpipe_query *)p;
54 }
55
56 static struct pipe_query *
57 softpipe_create_query(struct pipe_context *pipe,
58 unsigned type)
59 {
60 struct softpipe_query* sq;
61
62 assert(type == PIPE_QUERY_OCCLUSION_COUNTER ||
63 type == PIPE_QUERY_OCCLUSION_PREDICATE ||
64 type == PIPE_QUERY_TIME_ELAPSED ||
65 type == PIPE_QUERY_SO_STATISTICS ||
66 type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ||
67 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
68 type == PIPE_QUERY_PRIMITIVES_GENERATED ||
69 type == PIPE_QUERY_PIPELINE_STATISTICS ||
70 type == PIPE_QUERY_GPU_FINISHED ||
71 type == PIPE_QUERY_TIMESTAMP ||
72 type == PIPE_QUERY_TIMESTAMP_DISJOINT);
73 sq = CALLOC_STRUCT( softpipe_query );
74 sq->type = type;
75
76 return (struct pipe_query *)sq;
77 }
78
79
80 static void
81 softpipe_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
82 {
83 FREE(q);
84 }
85
86
87 static void
88 softpipe_begin_query(struct pipe_context *pipe, struct pipe_query *q)
89 {
90 struct softpipe_context *softpipe = softpipe_context( pipe );
91 struct softpipe_query *sq = softpipe_query(q);
92
93 switch (sq->type) {
94 case PIPE_QUERY_OCCLUSION_COUNTER:
95 case PIPE_QUERY_OCCLUSION_PREDICATE:
96 sq->start = softpipe->occlusion_count;
97 break;
98 case PIPE_QUERY_TIME_ELAPSED:
99 sq->start = os_time_get_nano();
100 break;
101 case PIPE_QUERY_SO_STATISTICS:
102 sq->so.primitives_storage_needed = 0;
103 sq->num_primitives_generated = 0;
104 softpipe->num_primitives_generated = 0;
105 sq->so.num_primitives_written = 0;
106 softpipe->so_stats.num_primitives_written = 0;
107 break;
108 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
109 sq->end = FALSE;
110 break;
111 case PIPE_QUERY_PRIMITIVES_EMITTED:
112 sq->so.num_primitives_written = 0;
113 softpipe->so_stats.num_primitives_written = 0;
114 break;
115 case PIPE_QUERY_PRIMITIVES_GENERATED:
116 sq->num_primitives_generated = 0;
117 softpipe->num_primitives_generated = 0;
118 break;
119 case PIPE_QUERY_TIMESTAMP:
120 case PIPE_QUERY_GPU_FINISHED:
121 case PIPE_QUERY_TIMESTAMP_DISJOINT:
122 break;
123 case PIPE_QUERY_PIPELINE_STATISTICS:
124 /* reset our cache */
125 if (softpipe->active_statistics_queries == 0) {
126 memset(&softpipe->pipeline_statistics, 0,
127 sizeof(softpipe->pipeline_statistics));
128 }
129 memcpy(&sq->stats, &softpipe->pipeline_statistics,
130 sizeof(sq->stats));
131 softpipe->active_statistics_queries++;
132 break;
133 default:
134 assert(0);
135 break;
136 }
137 softpipe->active_query_count++;
138 softpipe->dirty |= SP_NEW_QUERY;
139 }
140
141
142 static void
143 softpipe_end_query(struct pipe_context *pipe, struct pipe_query *q)
144 {
145 struct softpipe_context *softpipe = softpipe_context( pipe );
146 struct softpipe_query *sq = softpipe_query(q);
147
148 softpipe->active_query_count--;
149 switch (sq->type) {
150 case PIPE_QUERY_OCCLUSION_COUNTER:
151 case PIPE_QUERY_OCCLUSION_PREDICATE:
152 sq->end = softpipe->occlusion_count;
153 break;
154 case PIPE_QUERY_TIMESTAMP:
155 sq->start = 0;
156 /* fall through */
157 case PIPE_QUERY_TIME_ELAPSED:
158 sq->end = os_time_get_nano();
159 break;
160 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
161 sq->end = (softpipe->num_primitives_generated >
162 softpipe->so_stats.num_primitives_written);
163 break;
164 case PIPE_QUERY_SO_STATISTICS:
165 sq->num_primitives_generated =
166 softpipe->num_primitives_generated;
167 sq->so.num_primitives_written =
168 softpipe->so_stats.num_primitives_written;
169 break;
170 case PIPE_QUERY_PRIMITIVES_EMITTED:
171 sq->so.num_primitives_written =
172 softpipe->so_stats.num_primitives_written;
173 break;
174 case PIPE_QUERY_PRIMITIVES_GENERATED:
175 sq->num_primitives_generated = softpipe->num_primitives_generated;
176 break;
177 case PIPE_QUERY_GPU_FINISHED:
178 case PIPE_QUERY_TIMESTAMP_DISJOINT:
179 break;
180 case PIPE_QUERY_PIPELINE_STATISTICS:
181 sq->stats.ia_vertices =
182 softpipe->pipeline_statistics.ia_vertices - sq->stats.ia_vertices;
183 sq->stats.ia_primitives =
184 softpipe->pipeline_statistics.ia_primitives - sq->stats.ia_primitives;
185 sq->stats.vs_invocations =
186 softpipe->pipeline_statistics.vs_invocations - sq->stats.vs_invocations;
187 sq->stats.gs_invocations =
188 softpipe->pipeline_statistics.gs_invocations - sq->stats.gs_invocations;
189 sq->stats.gs_primitives =
190 softpipe->pipeline_statistics.gs_primitives - sq->stats.gs_primitives;
191 sq->stats.c_invocations =
192 softpipe->pipeline_statistics.c_invocations - sq->stats.c_invocations;
193 sq->stats.c_primitives =
194 softpipe->pipeline_statistics.c_primitives - sq->stats.c_primitives;
195 sq->stats.ps_invocations =
196 softpipe->pipeline_statistics.ps_invocations - sq->stats.ps_invocations;
197
198 softpipe->active_statistics_queries--;
199 break;
200 default:
201 assert(0);
202 break;
203 }
204 softpipe->dirty |= SP_NEW_QUERY;
205 }
206
207
208 static boolean
209 softpipe_get_query_result(struct pipe_context *pipe,
210 struct pipe_query *q,
211 boolean wait,
212 union pipe_query_result *vresult)
213 {
214 struct softpipe_query *sq = softpipe_query(q);
215 uint64_t *result = (uint64_t*)vresult;
216
217 switch (sq->type) {
218 case PIPE_QUERY_SO_STATISTICS: {
219 struct pipe_query_data_so_statistics *stats =
220 (struct pipe_query_data_so_statistics *)vresult;
221 stats->num_primitives_written = sq->so.num_primitives_written;
222 stats->primitives_storage_needed = sq->num_primitives_generated;
223 }
224 break;
225 case PIPE_QUERY_PIPELINE_STATISTICS:
226 memcpy(vresult, &sq->stats,
227 sizeof(struct pipe_query_data_pipeline_statistics));;
228 break;
229 case PIPE_QUERY_GPU_FINISHED:
230 vresult->b = TRUE;
231 break;
232 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
233 vresult->b = sq->end != 0;
234 break;
235 case PIPE_QUERY_TIMESTAMP_DISJOINT: {
236 struct pipe_query_data_timestamp_disjoint *td =
237 (struct pipe_query_data_timestamp_disjoint *)vresult;
238 /* os_get_time_nano return nanoseconds */
239 td->frequency = UINT64_C(1000000000);
240 td->disjoint = FALSE;
241 }
242 break;
243 case PIPE_QUERY_PRIMITIVES_EMITTED:
244 *result = sq->so.num_primitives_written;
245 break;
246 case PIPE_QUERY_PRIMITIVES_GENERATED:
247 *result = sq->num_primitives_generated;
248 break;
249 case PIPE_QUERY_OCCLUSION_PREDICATE:
250 vresult->b = sq->end - sq->start != 0;
251 break;
252 default:
253 *result = sq->end - sq->start;
254 break;
255 }
256 return TRUE;
257 }
258
259
260 /**
261 * Called by rendering function to check rendering is conditional.
262 * \return TRUE if we should render, FALSE if we should skip rendering
263 */
264 boolean
265 softpipe_check_render_cond(struct softpipe_context *sp)
266 {
267 struct pipe_context *pipe = &sp->pipe;
268 boolean b, wait;
269 uint64_t result;
270
271 if (!sp->render_cond_query) {
272 return TRUE; /* no query predicate, draw normally */
273 }
274
275 wait = (sp->render_cond_mode == PIPE_RENDER_COND_WAIT ||
276 sp->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT);
277
278 b = pipe->get_query_result(pipe, sp->render_cond_query, wait,
279 (void*)&result);
280 if (b)
281 return (!result == sp->render_cond_cond);
282 else
283 return TRUE;
284 }
285
286
287 void softpipe_init_query_funcs(struct softpipe_context *softpipe )
288 {
289 softpipe->pipe.create_query = softpipe_create_query;
290 softpipe->pipe.destroy_query = softpipe_destroy_query;
291 softpipe->pipe.begin_query = softpipe_begin_query;
292 softpipe->pipe.end_query = softpipe_end_query;
293 softpipe->pipe.get_query_result = softpipe_get_query_result;
294 }
295
296