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
40struct softpipe_query {
41   unsigned type;
42   unsigned index;
43   uint64_t start;
44   uint64_t end;
45   struct pipe_query_data_so_statistics so[PIPE_MAX_VERTEX_STREAMS];
46   struct pipe_query_data_pipeline_statistics stats;
47};
48
49
50static struct softpipe_query *softpipe_query( struct pipe_query *p )
51{
52   return (struct softpipe_query *)p;
53}
54
55static struct pipe_query *
56softpipe_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
82static void
83softpipe_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
84{
85   FREE(q);
86}
87
88
89static bool
90softpipe_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[sq->index].num_primitives_written = softpipe->so_stats[sq->index].num_primitives_written;
106      sq->so[sq->index].primitives_storage_needed = softpipe->so_stats[sq->index].primitives_storage_needed;
107      break;
108   case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
109      sq->so[sq->index].num_primitives_written = softpipe->so_stats[sq->index].num_primitives_written;
110      sq->so[sq->index].primitives_storage_needed = softpipe->so_stats[sq->index].primitives_storage_needed;
111      break;
112   case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
113      for (unsigned i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++) {
114         sq->so[i].num_primitives_written = softpipe->so_stats[i].num_primitives_written;
115         sq->so[i].primitives_storage_needed = softpipe->so_stats[i].primitives_storage_needed;
116      }
117      break;
118   case PIPE_QUERY_PRIMITIVES_EMITTED:
119      sq->so[sq->index].num_primitives_written = softpipe->so_stats[sq->index].num_primitives_written;
120      break;
121   case PIPE_QUERY_PRIMITIVES_GENERATED:
122      sq->so[sq->index].primitives_storage_needed = softpipe->so_stats[sq->index].primitives_storage_needed;
123      break;
124   case PIPE_QUERY_TIMESTAMP:
125   case PIPE_QUERY_GPU_FINISHED:
126   case PIPE_QUERY_TIMESTAMP_DISJOINT:
127      break;
128   case PIPE_QUERY_PIPELINE_STATISTICS:
129      /* reset our cache */
130      if (softpipe->active_statistics_queries == 0) {
131         memset(&softpipe->pipeline_statistics, 0,
132                sizeof(softpipe->pipeline_statistics));
133      }
134      memcpy(&sq->stats, &softpipe->pipeline_statistics,
135             sizeof(sq->stats));
136      softpipe->active_statistics_queries++;
137      break;
138   default:
139      assert(0);
140      break;
141   }
142   softpipe->active_query_count++;
143   softpipe->dirty |= SP_NEW_QUERY;
144   return true;
145}
146
147
148static bool
149softpipe_end_query(struct pipe_context *pipe, struct pipe_query *q)
150{
151   struct softpipe_context *softpipe = softpipe_context( pipe );
152   struct softpipe_query *sq = softpipe_query(q);
153
154   softpipe->active_query_count--;
155   switch (sq->type) {
156   case PIPE_QUERY_OCCLUSION_COUNTER:
157   case PIPE_QUERY_OCCLUSION_PREDICATE:
158   case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
159      sq->end = softpipe->occlusion_count;
160      break;
161   case PIPE_QUERY_TIMESTAMP:
162      sq->start = 0;
163      FALLTHROUGH;
164   case PIPE_QUERY_TIME_ELAPSED:
165      sq->end = os_time_get_nano();
166      break;
167   case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
168      sq->so[sq->index].num_primitives_written =
169         softpipe->so_stats[sq->index].num_primitives_written - sq->so[sq->index].num_primitives_written;
170      sq->so[sq->index].primitives_storage_needed =
171         softpipe->so_stats[sq->index].primitives_storage_needed - sq->so[sq->index].primitives_storage_needed;
172      sq->end = sq->so[sq->index].primitives_storage_needed > sq->so[sq->index].num_primitives_written;
173      break;
174   case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
175      sq->end = 0;
176      for (unsigned i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++) {
177         sq->so[i].num_primitives_written =
178            softpipe->so_stats[i].num_primitives_written - sq->so[i].num_primitives_written;
179         sq->so[i].primitives_storage_needed =
180            softpipe->so_stats[i].primitives_storage_needed - sq->so[i].primitives_storage_needed;
181         sq->end |= sq->so[i].primitives_storage_needed > sq->so[i].num_primitives_written;
182      }
183      break;
184   case PIPE_QUERY_SO_STATISTICS:
185      sq->so[sq->index].num_primitives_written =
186         softpipe->so_stats[sq->index].num_primitives_written - sq->so[sq->index].num_primitives_written;
187      sq->so[sq->index].primitives_storage_needed =
188         softpipe->so_stats[sq->index].primitives_storage_needed - sq->so[sq->index].primitives_storage_needed;
189      break;
190   case PIPE_QUERY_PRIMITIVES_EMITTED:
191      sq->so[sq->index].num_primitives_written =
192         softpipe->so_stats[sq->index].num_primitives_written - sq->so[sq->index].num_primitives_written;
193      break;
194   case PIPE_QUERY_PRIMITIVES_GENERATED:
195      sq->so[sq->index].primitives_storage_needed =
196         softpipe->so_stats[sq->index].primitives_storage_needed - sq->so[sq->index].primitives_storage_needed;
197      break;
198   case PIPE_QUERY_GPU_FINISHED:
199   case PIPE_QUERY_TIMESTAMP_DISJOINT:
200      break;
201   case PIPE_QUERY_PIPELINE_STATISTICS:
202      sq->stats.ia_vertices =
203         softpipe->pipeline_statistics.ia_vertices - sq->stats.ia_vertices;
204      sq->stats.ia_primitives =
205         softpipe->pipeline_statistics.ia_primitives - sq->stats.ia_primitives;
206      sq->stats.vs_invocations =
207         softpipe->pipeline_statistics.vs_invocations - sq->stats.vs_invocations;
208      sq->stats.gs_invocations =
209         softpipe->pipeline_statistics.gs_invocations - sq->stats.gs_invocations;
210      sq->stats.gs_primitives =
211         softpipe->pipeline_statistics.gs_primitives - sq->stats.gs_primitives;
212      sq->stats.c_invocations =
213         softpipe->pipeline_statistics.c_invocations - sq->stats.c_invocations;
214      sq->stats.c_primitives =
215         softpipe->pipeline_statistics.c_primitives - sq->stats.c_primitives;
216      sq->stats.ps_invocations =
217         softpipe->pipeline_statistics.ps_invocations - sq->stats.ps_invocations;
218      sq->stats.cs_invocations =
219         softpipe->pipeline_statistics.cs_invocations - sq->stats.cs_invocations;
220
221      softpipe->active_statistics_queries--;
222      break;
223   default:
224      assert(0);
225      break;
226   }
227   softpipe->dirty |= SP_NEW_QUERY;
228   return true;
229}
230
231
232static bool
233softpipe_get_query_result(struct pipe_context *pipe,
234                          struct pipe_query *q,
235                          bool wait,
236                          union pipe_query_result *vresult)
237{
238   struct softpipe_query *sq = softpipe_query(q);
239   uint64_t *result = (uint64_t*)vresult;
240
241   switch (sq->type) {
242   case PIPE_QUERY_SO_STATISTICS: {
243      struct pipe_query_data_so_statistics *stats =
244         (struct pipe_query_data_so_statistics *)vresult;
245      stats->num_primitives_written = sq->so[sq->index].num_primitives_written;
246      stats->primitives_storage_needed = sq->so[sq->index].primitives_storage_needed;
247   }
248      break;
249   case PIPE_QUERY_PIPELINE_STATISTICS:
250      memcpy(vresult, &sq->stats,
251             sizeof(struct pipe_query_data_pipeline_statistics));
252      break;
253   case PIPE_QUERY_GPU_FINISHED:
254      vresult->b = true;
255      break;
256   case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
257   case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
258      vresult->b = sq->end != 0;
259      break;
260   case PIPE_QUERY_TIMESTAMP_DISJOINT: {
261      struct pipe_query_data_timestamp_disjoint *td =
262          (struct pipe_query_data_timestamp_disjoint *)vresult;
263      /* os_get_time_nano return nanoseconds */
264      td->frequency = UINT64_C(1000000000);
265      td->disjoint = false;
266   }
267      break;
268   case PIPE_QUERY_PRIMITIVES_EMITTED:
269      *result = sq->so[sq->index].num_primitives_written;
270      break;
271   case PIPE_QUERY_PRIMITIVES_GENERATED:
272      *result = sq->so[sq->index].primitives_storage_needed;
273      break;
274   case PIPE_QUERY_OCCLUSION_PREDICATE:
275   case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
276      vresult->b = sq->end - sq->start != 0;
277      break;
278   default:
279      *result = sq->end - sq->start;
280      break;
281   }
282   return true;
283}
284
285static bool
286is_result_nonzero(struct pipe_query *q,
287                  union pipe_query_result *vresult)
288{
289   struct softpipe_query *sq = softpipe_query(q);
290
291   switch (sq->type) {
292   case PIPE_QUERY_TIMESTAMP_DISJOINT:
293   case PIPE_QUERY_SO_STATISTICS:
294   case PIPE_QUERY_PIPELINE_STATISTICS:
295      unreachable("unpossible");
296      break;
297   case PIPE_QUERY_GPU_FINISHED:
298   case PIPE_QUERY_OCCLUSION_PREDICATE:
299   case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
300   case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
301   case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
302      return vresult->b;
303   default:
304      return !!vresult->u64;
305   }
306   return false;
307}
308
309/**
310 * Called by rendering function to check rendering is conditional.
311 * \return TRUE if we should render, FALSE if we should skip rendering
312 */
313boolean
314softpipe_check_render_cond(struct softpipe_context *sp)
315{
316   struct pipe_context *pipe = &sp->pipe;
317   boolean b, wait;
318   union pipe_query_result result;
319   memset(&result, 0, sizeof(union pipe_query_result));
320
321   if (!sp->render_cond_query) {
322      return TRUE;  /* no query predicate, draw normally */
323   }
324
325   wait = (sp->render_cond_mode == PIPE_RENDER_COND_WAIT ||
326           sp->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT);
327
328   b = pipe->get_query_result(pipe, sp->render_cond_query, wait,
329                              &result);
330   if (b)
331      return !is_result_nonzero(sp->render_cond_query, &result) == sp->render_cond_cond;
332   else
333      return TRUE;
334}
335
336
337static void
338softpipe_set_active_query_state(struct pipe_context *pipe, bool enable)
339{
340}
341
342
343void softpipe_init_query_funcs(struct softpipe_context *softpipe )
344{
345   softpipe->pipe.create_query = softpipe_create_query;
346   softpipe->pipe.destroy_query = softpipe_destroy_query;
347   softpipe->pipe.begin_query = softpipe_begin_query;
348   softpipe->pipe.end_query = softpipe_end_query;
349   softpipe->pipe.get_query_result = softpipe_get_query_result;
350   softpipe->pipe.set_active_query_state = softpipe_set_active_query_state;
351}
352
353
354