softpipe/so: use the correct variable for reporting stream out
[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_TIME_ELAPSED ||
64 type == PIPE_QUERY_SO_STATISTICS ||
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 sq->start = softpipe->occlusion_count;
94 break;
95 case PIPE_QUERY_TIMESTAMP_DISJOINT:
96 case PIPE_QUERY_TIME_ELAPSED:
97 sq->start = os_time_get_nano();
98 break;
99 case PIPE_QUERY_SO_STATISTICS:
100 sq->so.primitives_storage_needed = 0;
101 sq->num_primitives_generated = 0;
102 softpipe->num_primitives_generated = 0;
103 sq->so.num_primitives_written = 0;
104 softpipe->so_stats.num_primitives_written = 0;
105 case PIPE_QUERY_PRIMITIVES_EMITTED:
106 sq->so.num_primitives_written = 0;
107 softpipe->so_stats.num_primitives_written = 0;
108 break;
109 case PIPE_QUERY_PRIMITIVES_GENERATED:
110 sq->num_primitives_generated = 0;
111 softpipe->num_primitives_generated = 0;
112 break;
113 case PIPE_QUERY_TIMESTAMP:
114 case PIPE_QUERY_GPU_FINISHED:
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 sq->end = softpipe->occlusion_count;
145 break;
146 case PIPE_QUERY_TIMESTAMP:
147 sq->start = 0;
148 /* fall through */
149 case PIPE_QUERY_TIMESTAMP_DISJOINT:
150 case PIPE_QUERY_TIME_ELAPSED:
151 sq->end = os_time_get_nano();
152 break;
153 case PIPE_QUERY_SO_STATISTICS:
154 sq->num_primitives_generated =
155 softpipe->num_primitives_generated;
156 sq->so.num_primitives_written =
157 softpipe->so_stats.num_primitives_written;
158 break;
159 case PIPE_QUERY_PRIMITIVES_EMITTED:
160 sq->so.num_primitives_written =
161 softpipe->so_stats.num_primitives_written;
162 break;
163 case PIPE_QUERY_PRIMITIVES_GENERATED:
164 sq->num_primitives_generated = softpipe->num_primitives_generated;
165 break;
166 case PIPE_QUERY_GPU_FINISHED:
167 break;
168 case PIPE_QUERY_PIPELINE_STATISTICS:
169 sq->stats.ia_vertices =
170 softpipe->pipeline_statistics.ia_vertices - sq->stats.ia_vertices;
171 sq->stats.ia_primitives =
172 softpipe->pipeline_statistics.ia_primitives - sq->stats.ia_primitives;
173 sq->stats.vs_invocations =
174 softpipe->pipeline_statistics.vs_invocations - sq->stats.vs_invocations;
175 sq->stats.gs_invocations =
176 softpipe->pipeline_statistics.gs_invocations - sq->stats.gs_invocations;
177 sq->stats.gs_primitives =
178 softpipe->pipeline_statistics.gs_primitives - sq->stats.gs_primitives;
179 sq->stats.c_invocations =
180 softpipe->pipeline_statistics.c_invocations - sq->stats.c_invocations;
181 sq->stats.c_primitives =
182 softpipe->pipeline_statistics.c_primitives - sq->stats.c_primitives;
183 sq->stats.ps_invocations =
184 softpipe->pipeline_statistics.ps_invocations - sq->stats.ps_invocations;
185
186 softpipe->active_statistics_queries--;
187 break;
188 default:
189 assert(0);
190 break;
191 }
192 softpipe->dirty |= SP_NEW_QUERY;
193 }
194
195
196 static boolean
197 softpipe_get_query_result(struct pipe_context *pipe,
198 struct pipe_query *q,
199 boolean wait,
200 union pipe_query_result *vresult)
201 {
202 struct softpipe_query *sq = softpipe_query(q);
203 uint64_t *result = (uint64_t*)vresult;
204
205 switch (sq->type) {
206 case PIPE_QUERY_SO_STATISTICS: {
207 struct pipe_query_data_so_statistics *stats =
208 (struct pipe_query_data_so_statistics *)vresult;
209 stats->num_primitives_written = sq->so.num_primitives_written;
210 stats->primitives_storage_needed = sq->num_primitives_generated;
211 }
212 break;
213 case PIPE_QUERY_PIPELINE_STATISTICS:
214 memcpy(vresult, &sq->stats,
215 sizeof(struct pipe_query_data_pipeline_statistics));;
216 break;
217 case PIPE_QUERY_GPU_FINISHED:
218 *result = TRUE;
219 break;
220 case PIPE_QUERY_TIMESTAMP_DISJOINT: {
221 struct pipe_query_data_timestamp_disjoint td;
222 /* os_get_time_nano return nanoseconds */
223 td.frequency = UINT64_C(1000000000);
224 td.disjoint = sq->end != sq->start;
225 memcpy(vresult, &td,
226 sizeof(struct pipe_query_data_timestamp_disjoint));
227 }
228 break;
229 case PIPE_QUERY_PRIMITIVES_EMITTED:
230 *result = sq->so.num_primitives_written;
231 break;
232 case PIPE_QUERY_PRIMITIVES_GENERATED:
233 *result = sq->num_primitives_generated;
234 break;
235 default:
236 *result = sq->end - sq->start;
237 break;
238 }
239 return TRUE;
240 }
241
242
243 /**
244 * Called by rendering function to check rendering is conditional.
245 * \return TRUE if we should render, FALSE if we should skip rendering
246 */
247 boolean
248 softpipe_check_render_cond(struct softpipe_context *sp)
249 {
250 struct pipe_context *pipe = &sp->pipe;
251 boolean b, wait;
252 uint64_t result;
253
254 if (!sp->render_cond_query) {
255 return TRUE; /* no query predicate, draw normally */
256 }
257
258 wait = (sp->render_cond_mode == PIPE_RENDER_COND_WAIT ||
259 sp->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT);
260
261 b = pipe->get_query_result(pipe, sp->render_cond_query, wait,
262 (void*)&result);
263 if (b)
264 return result > 0;
265 else
266 return TRUE;
267 }
268
269
270 void softpipe_init_query_funcs(struct softpipe_context *softpipe )
271 {
272 softpipe->pipe.create_query = softpipe_create_query;
273 softpipe->pipe.destroy_query = softpipe_destroy_query;
274 softpipe->pipe.begin_query = softpipe_begin_query;
275 softpipe->pipe.end_query = softpipe_end_query;
276 softpipe->pipe.get_query_result = softpipe_get_query_result;
277 }
278
279