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