1 /*
2 * Copyright © 2014 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * Gallium query object support.
26 *
27 * The HW has native support for occlusion queries, with the query result
28 * being loaded and stored by the TLB unit. From a SW perspective, we have to
29 * be careful to make sure that the jobs that need to be tracking queries are
30 * bracketed by the start and end of counting, even across FBO transitions.
31 *
32 * For the transform feedback PRIMITIVES_GENERATED/WRITTEN queries, we have to
33 * do the calculations in software at draw time.
34 */
35
36 #include "v3d_query.h"
37
38 struct v3d_query_pipe
39 {
40 struct v3d_query base;
41
42 enum pipe_query_type type;
43 struct v3d_bo *bo;
44
45 uint32_t start, end;
46 uint32_t result;
47
48 /* these fields are used for timestamp queries */
49 uint64_t time_result;
50 uint32_t sync[2];
51 };
52
53 static void
v3d_destroy_query_pipe(struct v3d_context * v3d,struct v3d_query * query)54 v3d_destroy_query_pipe(struct v3d_context *v3d, struct v3d_query *query)
55 {
56 struct v3d_query_pipe *pquery = (struct v3d_query_pipe *)query;
57
58 if (pquery->sync[0])
59 drmSyncobjDestroy(v3d->fd, pquery->sync[0]);
60 if (pquery->sync[1])
61 drmSyncobjDestroy(v3d->fd, pquery->sync[1]);
62 v3d_bo_unreference(&pquery->bo);
63 free(pquery);
64 }
65
66 static bool
v3d_begin_query_pipe(struct v3d_context * v3d,struct v3d_query * query)67 v3d_begin_query_pipe(struct v3d_context *v3d, struct v3d_query *query)
68 {
69 struct v3d_query_pipe *pquery = (struct v3d_query_pipe *)query;
70
71 switch (pquery->type) {
72 case PIPE_QUERY_PRIMITIVES_GENERATED:
73 /* If we are using PRIMITIVE_COUNTS_FEEDBACK to retrieve
74 * primitive counts from the GPU (which we need when a GS
75 * is present), then we need to update our counters now
76 * to discard any primitives generated before this.
77 */
78 if (v3d->prog.gs)
79 v3d_update_primitive_counters(v3d);
80 pquery->start = v3d->prims_generated;
81 v3d->n_primitives_generated_queries_in_flight++;
82 break;
83 case PIPE_QUERY_PRIMITIVES_EMITTED:
84 /* If we are inside transform feedback we need to update the
85 * primitive counts to skip primitives recorded before this.
86 */
87 if (v3d->streamout.num_targets > 0)
88 v3d_update_primitive_counters(v3d);
89 pquery->start = v3d->tf_prims_generated;
90 break;
91 case PIPE_QUERY_OCCLUSION_COUNTER:
92 case PIPE_QUERY_OCCLUSION_PREDICATE:
93 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
94 v3d_bo_unreference(&pquery->bo);
95 pquery->bo = v3d_bo_alloc(v3d->screen, 4096, "query");
96 uint32_t *map = v3d_bo_map(pquery->bo);
97 *map = 0;
98
99 v3d->current_oq = pquery->bo;
100 v3d->dirty |= V3D_DIRTY_OQ;
101 break;
102 case PIPE_QUERY_TIME_ELAPSED:
103 /* GL_TIME_ELAPSED: Records the time that it takes for the GPU
104 * to execute all of the scoped commands.
105 *
106 * The timer starts when all commands before the scope have
107 * completed, and the timer ends when the last scoped command
108 * has completed.
109 */
110 assert(pquery->bo);
111
112 /* flush any pending jobs */
113 v3d_flush(&v3d->base);
114
115 /* submit time elapsed query to cpu queue */
116 v3d_submit_timestamp_query(&v3d->base, pquery->bo,
117 pquery->sync[0], 0);
118 break;
119 case PIPE_QUERY_TIMESTAMP_DISJOINT:
120 break;
121 default:
122 unreachable("unsupported query type");
123 }
124
125 return true;
126 }
127
128 static bool
v3d_end_query_pipe(struct v3d_context * v3d,struct v3d_query * query)129 v3d_end_query_pipe(struct v3d_context *v3d, struct v3d_query *query)
130 {
131 struct v3d_query_pipe *pquery = (struct v3d_query_pipe *)query;
132
133 switch (pquery->type) {
134 case PIPE_QUERY_PRIMITIVES_GENERATED:
135 /* If we are using PRIMITIVE_COUNTS_FEEDBACK to retrieve
136 * primitive counts from the GPU (which we need when a GS
137 * is present), then we need to update our counters now.
138 */
139 if (v3d->prog.gs)
140 v3d_update_primitive_counters(v3d);
141 pquery->end = v3d->prims_generated;
142 v3d->n_primitives_generated_queries_in_flight--;
143 break;
144 case PIPE_QUERY_PRIMITIVES_EMITTED:
145 /* If transform feedback has ended, then we have already
146 * updated the primitive counts at glEndTransformFeedback()
147 * time. Otherwise, we have to do it now.
148 */
149 if (v3d->streamout.num_targets > 0)
150 v3d_update_primitive_counters(v3d);
151 pquery->end = v3d->tf_prims_generated;
152 break;
153 case PIPE_QUERY_OCCLUSION_COUNTER:
154 case PIPE_QUERY_OCCLUSION_PREDICATE:
155 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
156 v3d->current_oq = NULL;
157 v3d->dirty |= V3D_DIRTY_OQ;
158 break;
159 case PIPE_QUERY_TIMESTAMP:
160 case PIPE_QUERY_TIME_ELAPSED:
161 /* Mesa only calls EndQuery and not BeginQuery for regular
162 * timestamp queries
163 *
164 * This will store into the query object the time when the GPU
165 * will have completed all previously issued commands.
166 */
167 assert(pquery->bo);
168
169 /* flush any pending jobs */
170 v3d_flush(&v3d->base);
171
172 /* submit time elapsed query to cpu queue */
173 uint32_t offset = pquery->type == PIPE_QUERY_TIME_ELAPSED ?
174 sizeof(uint64_t) : 0;
175 uint32_t sync = pquery->type == PIPE_QUERY_TIMESTAMP ? 0 : 1;
176 v3d_submit_timestamp_query(&v3d->base, pquery->bo,
177 pquery->sync[sync], offset);
178 break;
179 case PIPE_QUERY_TIMESTAMP_DISJOINT:
180 break;
181 default:
182 unreachable("unsupported query type");
183 }
184
185 return true;
186 }
187
188 static bool
v3d_get_query_result_pipe(struct v3d_context * v3d,struct v3d_query * query,bool wait,union pipe_query_result * vresult)189 v3d_get_query_result_pipe(struct v3d_context *v3d, struct v3d_query *query,
190 bool wait, union pipe_query_result *vresult)
191 {
192 struct v3d_query_pipe *pquery = (struct v3d_query_pipe *)query;
193
194 if (pquery->bo) {
195 /* For timestamp & time elapsed queries we already flush
196 * relevant jobs before submitting the query */
197 if (pquery->type != PIPE_QUERY_TIMESTAMP &&
198 pquery->type != PIPE_QUERY_TIME_ELAPSED) {
199 v3d_flush_jobs_using_bo(v3d, pquery->bo);
200 }
201
202 if (wait) {
203 if (!v3d_bo_wait(pquery->bo, ~0ull, "query"))
204 return false;
205
206 assert((pquery->type != PIPE_QUERY_TIMESTAMP &&
207 pquery->type != PIPE_QUERY_TIME_ELAPSED) ||
208 drmSyncobjWait(v3d->fd, &pquery->sync[0], 1, 0,
209 0, NULL) != -ETIME);
210
211 assert(pquery->type != PIPE_QUERY_TIME_ELAPSED ||
212 drmSyncobjWait(v3d->fd, &pquery->sync[1], 1,
213 0, 0, NULL) != -ETIME);
214 } else {
215 if (!v3d_bo_wait(pquery->bo, 0, "query"))
216 return false;
217 }
218
219 if (pquery->type == PIPE_QUERY_TIMESTAMP) {
220 uint64_t *map = v3d_bo_map(pquery->bo);
221 pquery->time_result = *map;
222 } else if (pquery->type == PIPE_QUERY_TIME_ELAPSED) {
223 uint64_t *map = v3d_bo_map(pquery->bo);
224 pquery->time_result = map[1] - map[0];
225 } else {
226 /* XXX: Sum up per-core values. */
227 uint32_t *map = v3d_bo_map(pquery->bo);
228 pquery->result = *map;
229
230 /* FIXME: we should move creation and destruction of
231 * the BO for all queries to query create/destruction,
232 * like we do with timestamps */
233 v3d_bo_unreference(&pquery->bo);
234 }
235 }
236
237 switch (pquery->type) {
238 case PIPE_QUERY_OCCLUSION_COUNTER:
239 vresult->u64 = pquery->result;
240 break;
241 case PIPE_QUERY_OCCLUSION_PREDICATE:
242 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
243 vresult->b = pquery->result != 0;
244 break;
245 case PIPE_QUERY_PRIMITIVES_GENERATED:
246 case PIPE_QUERY_PRIMITIVES_EMITTED:
247 vresult->u64 = pquery->end - pquery->start;
248 break;
249 case PIPE_QUERY_TIMESTAMP:
250 case PIPE_QUERY_TIME_ELAPSED:
251 vresult->u64 = pquery->time_result;
252 break;
253 case PIPE_QUERY_TIMESTAMP_DISJOINT:
254 /* os_time_get_nano returns time in nanoseconds */
255 vresult->timestamp_disjoint.frequency = UINT64_C(1000000000);
256 vresult->timestamp_disjoint.disjoint = false;
257 break;
258 default:
259 unreachable("unsupported query type");
260 }
261
262 return true;
263 }
264
265 static const struct v3d_query_funcs pipe_query_funcs = {
266 .destroy_query = v3d_destroy_query_pipe,
267 .begin_query = v3d_begin_query_pipe,
268 .end_query = v3d_end_query_pipe,
269 .get_query_result = v3d_get_query_result_pipe,
270 };
271
272 struct pipe_query *
v3d_create_query_pipe(struct v3d_context * v3d,unsigned query_type,unsigned index)273 v3d_create_query_pipe(struct v3d_context *v3d, unsigned query_type, unsigned index)
274 {
275 if (query_type >= PIPE_QUERY_DRIVER_SPECIFIC)
276 return NULL;
277
278 struct v3d_query_pipe *pquery = calloc(1, sizeof(*pquery));
279 struct v3d_query *query = &pquery->base;
280
281 pquery->type = query_type;
282 query->funcs = &pipe_query_funcs;
283
284 /* FIXME: we should probably allocate BOs for occlusion queries here
285 * as well
286 */
287 switch (pquery->type) {
288 case PIPE_QUERY_TIMESTAMP:
289 case PIPE_QUERY_TIME_ELAPSED:
290 pquery->bo = v3d_bo_alloc(v3d->screen, 4096, "query");
291 uint32_t *map = v3d_bo_map(pquery->bo);
292 *map = 0;
293
294 drmSyncobjCreate(v3d->fd, 0, &pquery->sync[0]);
295 if (pquery->type == PIPE_QUERY_TIME_ELAPSED)
296 drmSyncobjCreate(v3d->fd, 0, &pquery->sync[1]);
297 break;
298 default:
299 break;
300 }
301
302 /* Note that struct pipe_query isn't actually defined anywhere. */
303 return (struct pipe_query *)query;
304 }
305