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 "util/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 unsigned index;
43 uint64_t start;
44 uint64_t end;
45 struct pipe_query_data_so_statistics so;
46 struct pipe_query_data_pipeline_statistics stats;
47 };
48
49
softpipe_query(struct pipe_query * p)50 static struct softpipe_query *softpipe_query( struct pipe_query *p )
51 {
52 return (struct softpipe_query *)p;
53 }
54
55 static struct pipe_query *
softpipe_create_query(struct pipe_context * pipe,unsigned type,unsigned index)56 softpipe_create_query(struct pipe_context *pipe,
57 unsigned type,
58 unsigned index)
59 {
60 struct softpipe_query* sq;
61
62 assert(type == PIPE_QUERY_OCCLUSION_COUNTER ||
63 type == PIPE_QUERY_OCCLUSION_PREDICATE ||
64 type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE ||
65 type == PIPE_QUERY_TIME_ELAPSED ||
66 type == PIPE_QUERY_SO_STATISTICS ||
67 type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ||
68 type == PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE ||
69 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
70 type == PIPE_QUERY_PRIMITIVES_GENERATED ||
71 type == PIPE_QUERY_PIPELINE_STATISTICS ||
72 type == PIPE_QUERY_GPU_FINISHED ||
73 type == PIPE_QUERY_TIMESTAMP ||
74 type == PIPE_QUERY_TIMESTAMP_DISJOINT);
75 sq = CALLOC_STRUCT( softpipe_query );
76 sq->type = type;
77 sq->index = index;
78 return (struct pipe_query *)sq;
79 }
80
81
82 static void
softpipe_destroy_query(struct pipe_context * pipe,struct pipe_query * q)83 softpipe_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
84 {
85 FREE(q);
86 }
87
88
89 static bool
softpipe_begin_query(struct pipe_context * pipe,struct pipe_query * q)90 softpipe_begin_query(struct pipe_context *pipe, struct pipe_query *q)
91 {
92 struct softpipe_context *softpipe = softpipe_context( pipe );
93 struct softpipe_query *sq = softpipe_query(q);
94
95 switch (sq->type) {
96 case PIPE_QUERY_OCCLUSION_COUNTER:
97 case PIPE_QUERY_OCCLUSION_PREDICATE:
98 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
99 sq->start = softpipe->occlusion_count;
100 break;
101 case PIPE_QUERY_TIME_ELAPSED:
102 sq->start = os_time_get_nano();
103 break;
104 case PIPE_QUERY_SO_STATISTICS:
105 sq->so.num_primitives_written = softpipe->so_stats[0].num_primitives_written;
106 sq->so.primitives_storage_needed = softpipe->so_stats[0].primitives_storage_needed;
107 break;
108 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
109 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
110 sq->so.num_primitives_written = softpipe->so_stats[sq->index].num_primitives_written;
111 sq->so.primitives_storage_needed = softpipe->so_stats[sq->index].primitives_storage_needed;
112 break;
113 case PIPE_QUERY_PRIMITIVES_EMITTED:
114 sq->so.num_primitives_written = softpipe->so_stats[sq->index].num_primitives_written;
115 break;
116 case PIPE_QUERY_PRIMITIVES_GENERATED:
117 sq->so.primitives_storage_needed = softpipe->so_stats[sq->index].primitives_storage_needed;
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 return true;
140 }
141
142
143 static bool
softpipe_end_query(struct pipe_context * pipe,struct pipe_query * q)144 softpipe_end_query(struct pipe_context *pipe, struct pipe_query *q)
145 {
146 struct softpipe_context *softpipe = softpipe_context( pipe );
147 struct softpipe_query *sq = softpipe_query(q);
148
149 softpipe->active_query_count--;
150 switch (sq->type) {
151 case PIPE_QUERY_OCCLUSION_COUNTER:
152 case PIPE_QUERY_OCCLUSION_PREDICATE:
153 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
154 sq->end = softpipe->occlusion_count;
155 break;
156 case PIPE_QUERY_TIMESTAMP:
157 sq->start = 0;
158 /* fall through */
159 case PIPE_QUERY_TIME_ELAPSED:
160 sq->end = os_time_get_nano();
161 break;
162 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
163 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
164 sq->so.num_primitives_written =
165 softpipe->so_stats[0].num_primitives_written - sq->so.num_primitives_written;
166 sq->so.primitives_storage_needed =
167 softpipe->so_stats[0].primitives_storage_needed - sq->so.primitives_storage_needed;
168 sq->end = sq->so.primitives_storage_needed > sq->so.num_primitives_written;
169 break;
170 case PIPE_QUERY_SO_STATISTICS:
171 sq->so.num_primitives_written =
172 softpipe->so_stats[sq->index].num_primitives_written - sq->so.num_primitives_written;
173 sq->so.primitives_storage_needed =
174 softpipe->so_stats[sq->index].primitives_storage_needed - sq->so.primitives_storage_needed;
175 break;
176 case PIPE_QUERY_PRIMITIVES_EMITTED:
177 sq->so.num_primitives_written =
178 softpipe->so_stats[sq->index].num_primitives_written - sq->so.num_primitives_written;
179 break;
180 case PIPE_QUERY_PRIMITIVES_GENERATED:
181 sq->so.primitives_storage_needed =
182 softpipe->so_stats[sq->index].primitives_storage_needed - sq->so.primitives_storage_needed;
183 break;
184 case PIPE_QUERY_GPU_FINISHED:
185 case PIPE_QUERY_TIMESTAMP_DISJOINT:
186 break;
187 case PIPE_QUERY_PIPELINE_STATISTICS:
188 sq->stats.ia_vertices =
189 softpipe->pipeline_statistics.ia_vertices - sq->stats.ia_vertices;
190 sq->stats.ia_primitives =
191 softpipe->pipeline_statistics.ia_primitives - sq->stats.ia_primitives;
192 sq->stats.vs_invocations =
193 softpipe->pipeline_statistics.vs_invocations - sq->stats.vs_invocations;
194 sq->stats.gs_invocations =
195 softpipe->pipeline_statistics.gs_invocations - sq->stats.gs_invocations;
196 sq->stats.gs_primitives =
197 softpipe->pipeline_statistics.gs_primitives - sq->stats.gs_primitives;
198 sq->stats.c_invocations =
199 softpipe->pipeline_statistics.c_invocations - sq->stats.c_invocations;
200 sq->stats.c_primitives =
201 softpipe->pipeline_statistics.c_primitives - sq->stats.c_primitives;
202 sq->stats.ps_invocations =
203 softpipe->pipeline_statistics.ps_invocations - sq->stats.ps_invocations;
204 sq->stats.cs_invocations =
205 softpipe->pipeline_statistics.cs_invocations - sq->stats.cs_invocations;
206
207 softpipe->active_statistics_queries--;
208 break;
209 default:
210 assert(0);
211 break;
212 }
213 softpipe->dirty |= SP_NEW_QUERY;
214 return true;
215 }
216
217
218 static bool
softpipe_get_query_result(struct pipe_context * pipe,struct pipe_query * q,bool wait,union pipe_query_result * vresult)219 softpipe_get_query_result(struct pipe_context *pipe,
220 struct pipe_query *q,
221 bool wait,
222 union pipe_query_result *vresult)
223 {
224 struct softpipe_query *sq = softpipe_query(q);
225 uint64_t *result = (uint64_t*)vresult;
226
227 switch (sq->type) {
228 case PIPE_QUERY_SO_STATISTICS: {
229 struct pipe_query_data_so_statistics *stats =
230 (struct pipe_query_data_so_statistics *)vresult;
231 stats->num_primitives_written = sq->so.num_primitives_written;
232 stats->primitives_storage_needed = sq->so.primitives_storage_needed;
233 }
234 break;
235 case PIPE_QUERY_PIPELINE_STATISTICS:
236 memcpy(vresult, &sq->stats,
237 sizeof(struct pipe_query_data_pipeline_statistics));
238 break;
239 case PIPE_QUERY_GPU_FINISHED:
240 vresult->b = true;
241 break;
242 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
243 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
244 vresult->b = sq->end != 0;
245 break;
246 case PIPE_QUERY_TIMESTAMP_DISJOINT: {
247 struct pipe_query_data_timestamp_disjoint *td =
248 (struct pipe_query_data_timestamp_disjoint *)vresult;
249 /* os_get_time_nano return nanoseconds */
250 td->frequency = UINT64_C(1000000000);
251 td->disjoint = false;
252 }
253 break;
254 case PIPE_QUERY_PRIMITIVES_EMITTED:
255 *result = sq->so.num_primitives_written;
256 break;
257 case PIPE_QUERY_PRIMITIVES_GENERATED:
258 *result = sq->so.primitives_storage_needed;
259 break;
260 case PIPE_QUERY_OCCLUSION_PREDICATE:
261 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
262 vresult->b = sq->end - sq->start != 0;
263 break;
264 default:
265 *result = sq->end - sq->start;
266 break;
267 }
268 return true;
269 }
270
271
272 /**
273 * Called by rendering function to check rendering is conditional.
274 * \return TRUE if we should render, FALSE if we should skip rendering
275 */
276 boolean
softpipe_check_render_cond(struct softpipe_context * sp)277 softpipe_check_render_cond(struct softpipe_context *sp)
278 {
279 struct pipe_context *pipe = &sp->pipe;
280 boolean b, wait;
281 uint64_t result;
282
283 if (!sp->render_cond_query) {
284 return TRUE; /* no query predicate, draw normally */
285 }
286
287 wait = (sp->render_cond_mode == PIPE_RENDER_COND_WAIT ||
288 sp->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT);
289
290 b = pipe->get_query_result(pipe, sp->render_cond_query, wait,
291 (void*)&result);
292 if (b)
293 return (!result) == sp->render_cond_cond;
294 else
295 return TRUE;
296 }
297
298
299 static void
softpipe_set_active_query_state(struct pipe_context * pipe,bool enable)300 softpipe_set_active_query_state(struct pipe_context *pipe, bool enable)
301 {
302 }
303
304
softpipe_init_query_funcs(struct softpipe_context * softpipe)305 void softpipe_init_query_funcs(struct softpipe_context *softpipe )
306 {
307 softpipe->pipe.create_query = softpipe_create_query;
308 softpipe->pipe.destroy_query = softpipe_destroy_query;
309 softpipe->pipe.begin_query = softpipe_begin_query;
310 softpipe->pipe.end_query = softpipe_end_query;
311 softpipe->pipe.get_query_result = softpipe_get_query_result;
312 softpipe->pipe.set_active_query_state = softpipe_set_active_query_state;
313 }
314
315
316