Merge branch 'gallium-drm-driver-drescriptor'
[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 };
46
47
48 static struct softpipe_query *softpipe_query( struct pipe_query *p )
49 {
50 return (struct softpipe_query *)p;
51 }
52
53 static struct pipe_query *
54 softpipe_create_query(struct pipe_context *pipe,
55 unsigned type)
56 {
57 struct softpipe_query* sq;
58
59 assert(type == PIPE_QUERY_OCCLUSION_COUNTER ||
60 type == PIPE_QUERY_TIME_ELAPSED ||
61 type == PIPE_QUERY_SO_STATISTICS ||
62 type == PIPE_QUERY_GPU_FINISHED ||
63 type == PIPE_QUERY_TIMESTAMP_DISJOINT);
64 sq = CALLOC_STRUCT( softpipe_query );
65 sq->type = type;
66
67 return (struct pipe_query *)sq;
68 }
69
70
71 static void
72 softpipe_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
73 {
74 FREE(q);
75 }
76
77
78 static void
79 softpipe_begin_query(struct pipe_context *pipe, struct pipe_query *q)
80 {
81 struct softpipe_context *softpipe = softpipe_context( pipe );
82 struct softpipe_query *sq = softpipe_query(q);
83
84 switch (sq->type) {
85 case PIPE_QUERY_OCCLUSION_COUNTER:
86 sq->start = softpipe->occlusion_count;
87 break;
88 case PIPE_QUERY_TIME_ELAPSED:
89 sq->start = 1000*os_time_get();
90 break;
91 case PIPE_QUERY_SO_STATISTICS:
92 sq->so.num_primitives_written = 0;
93 sq->so.primitives_storage_needed = 0;
94 break;
95 case PIPE_QUERY_GPU_FINISHED:
96 break;
97 case PIPE_QUERY_TIMESTAMP_DISJOINT:
98 default:
99 assert(0);
100 break;
101 }
102 softpipe->active_query_count++;
103 softpipe->dirty |= SP_NEW_QUERY;
104 }
105
106
107 static void
108 softpipe_end_query(struct pipe_context *pipe, struct pipe_query *q)
109 {
110 struct softpipe_context *softpipe = softpipe_context( pipe );
111 struct softpipe_query *sq = softpipe_query(q);
112
113 softpipe->active_query_count--;
114 switch (sq->type) {
115 case PIPE_QUERY_OCCLUSION_COUNTER:
116 sq->end = softpipe->occlusion_count;
117 break;
118 case PIPE_QUERY_TIME_ELAPSED:
119 sq->end = 1000*os_time_get();
120 break;
121 case PIPE_QUERY_SO_STATISTICS:
122 sq->so.num_primitives_written =
123 softpipe->so_stats.num_primitives_written;
124 sq->so.primitives_storage_needed =
125 softpipe->so_stats.primitives_storage_needed;
126 break;
127 case PIPE_QUERY_GPU_FINISHED:
128 case PIPE_QUERY_TIMESTAMP_DISJOINT:
129 break;
130 default:
131 assert(0);
132 break;
133 }
134 softpipe->dirty |= SP_NEW_QUERY;
135 }
136
137
138 static boolean
139 softpipe_get_query_result(struct pipe_context *pipe,
140 struct pipe_query *q,
141 boolean wait,
142 void *vresult)
143 {
144 struct softpipe_query *sq = softpipe_query(q);
145 uint64_t *result = (uint64_t*)vresult;
146
147 switch (sq->type) {
148 case PIPE_QUERY_SO_STATISTICS:
149 memcpy(vresult, &sq->so,
150 sizeof(struct pipe_query_data_so_statistics));
151 break;
152 case PIPE_QUERY_GPU_FINISHED:
153 *result = TRUE;
154 break;
155 case PIPE_QUERY_TIMESTAMP_DISJOINT: {
156 struct pipe_query_data_timestamp_disjoint td;
157 /*os_get_time is in microseconds*/
158 td.frequency = 1000000;
159 td.disjoint = FALSE;
160 memcpy(vresult, &sq->so,
161 sizeof(struct pipe_query_data_timestamp_disjoint));
162 }
163 break;
164 default:
165 *result = sq->end - sq->start;
166 break;
167 }
168 return TRUE;
169 }
170
171
172 /**
173 * Called by rendering function to check rendering is conditional.
174 * \return TRUE if we should render, FALSE if we should skip rendering
175 */
176 boolean
177 softpipe_check_render_cond(struct softpipe_context *sp)
178 {
179 struct pipe_context *pipe = &sp->pipe;
180 boolean b, wait;
181 uint64_t result;
182
183 if (!sp->render_cond_query) {
184 return TRUE; /* no query predicate, draw normally */
185 }
186
187 wait = (sp->render_cond_mode == PIPE_RENDER_COND_WAIT ||
188 sp->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT);
189
190 b = pipe->get_query_result(pipe, sp->render_cond_query, wait, &result);
191 if (b)
192 return result > 0;
193 else
194 return TRUE;
195 }
196
197
198 void softpipe_init_query_funcs(struct softpipe_context *softpipe )
199 {
200 softpipe->pipe.create_query = softpipe_create_query;
201 softpipe->pipe.destroy_query = softpipe_destroy_query;
202 softpipe->pipe.begin_query = softpipe_begin_query;
203 softpipe->pipe.end_query = softpipe_end_query;
204 softpipe->pipe.get_query_result = softpipe_get_query_result;
205 }
206
207