nv50,nvc0: prevent multiple flushes when user spins on get_query_result
[mesa.git] / src / gallium / drivers / nv50 / nv50_query.c
1 /*
2 * Copyright 2011 Nouveau Project
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 * Authors: Christoph Bumiller
23 */
24
25 #define NV50_PUSH_EXPLICIT_SPACE_CHECKING
26
27 #include "nv50_context.h"
28 #include "nouveau/nv_object.xml.h"
29
30 /* XXX: Nested queries, and simultaneous queries on multiple gallium contexts
31 * (since we use only a single GPU channel per screen) will not work properly.
32 *
33 * The first is not that big of an issue because OpenGL does not allow nested
34 * queries anyway.
35 */
36
37 struct nv50_query {
38 uint32_t *data;
39 uint32_t type;
40 uint32_t sequence;
41 struct nouveau_bo *bo;
42 uint32_t base;
43 uint32_t offset; /* base + i * 16 */
44 boolean ready;
45 boolean flushed;
46 boolean is64bit;
47 struct nouveau_mm_allocation *mm;
48 };
49
50 #define NV50_QUERY_ALLOC_SPACE 128
51
52 static INLINE struct nv50_query *
53 nv50_query(struct pipe_query *pipe)
54 {
55 return (struct nv50_query *)pipe;
56 }
57
58 static boolean
59 nv50_query_allocate(struct nv50_context *nv50, struct nv50_query *q, int size)
60 {
61 struct nv50_screen *screen = nv50->screen;
62 int ret;
63
64 if (q->bo) {
65 nouveau_bo_ref(NULL, &q->bo);
66 if (q->mm) {
67 if (q->ready)
68 nouveau_mm_free(q->mm);
69 else
70 nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work,
71 q->mm);
72 }
73 }
74 if (size) {
75 q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
76 if (!q->bo)
77 return FALSE;
78 q->offset = q->base;
79
80 ret = nouveau_bo_map(q->bo, 0, screen->base.client);
81 if (ret) {
82 nv50_query_allocate(nv50, q, 0);
83 return FALSE;
84 }
85 q->data = (uint32_t *)((uint8_t *)q->bo->map + q->base);
86 }
87 return TRUE;
88 }
89
90 static void
91 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
92 {
93 nv50_query_allocate(nv50_context(pipe), nv50_query(pq), 0);
94 FREE(nv50_query(pq));
95 }
96
97 static struct pipe_query *
98 nv50_query_create(struct pipe_context *pipe, unsigned type)
99 {
100 struct nv50_context *nv50 = nv50_context(pipe);
101 struct nv50_query *q;
102
103 q = CALLOC_STRUCT(nv50_query);
104 if (!q)
105 return NULL;
106
107 if (!nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE)) {
108 FREE(q);
109 return NULL;
110 }
111
112 q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
113 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
114 type == PIPE_QUERY_SO_STATISTICS);
115 q->type = type;
116
117 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
118 q->offset -= 16;
119 q->data -= 16 / sizeof(*q->data); /* we advance before query_begin ! */
120 }
121
122 return (struct pipe_query *)q;
123 }
124
125 static void
126 nv50_query_get(struct nouveau_pushbuf *push, struct nv50_query *q,
127 unsigned offset, uint32_t get)
128 {
129 offset += q->offset;
130
131 PUSH_SPACE(push, 5);
132 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
133 BEGIN_NV04(push, NV50_3D(QUERY_ADDRESS_HIGH), 4);
134 PUSH_DATAh(push, q->bo->offset + offset);
135 PUSH_DATA (push, q->bo->offset + offset);
136 PUSH_DATA (push, q->sequence);
137 PUSH_DATA (push, get);
138 }
139
140 static void
141 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
142 {
143 struct nv50_context *nv50 = nv50_context(pipe);
144 struct nouveau_pushbuf *push = nv50->base.pushbuf;
145 struct nv50_query *q = nv50_query(pq);
146
147 /* For occlusion queries we have to change the storage, because a previous
148 * query might set the initial render conition to FALSE even *after* we re-
149 * initialized it to TRUE.
150 */
151 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
152 q->offset += 16;
153 q->data += 16 / sizeof(*q->data);
154 if (q->offset - q->base == NV50_QUERY_ALLOC_SPACE)
155 nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE);
156
157 /* XXX: can we do this with the GPU, and sync with respect to a previous
158 * query ?
159 */
160 q->data[1] = 1; /* initial render condition = TRUE */
161 }
162 if (!q->is64bit)
163 q->data[0] = q->sequence++; /* the previously used one */
164
165 switch (q->type) {
166 case PIPE_QUERY_OCCLUSION_COUNTER:
167 PUSH_SPACE(push, 4);
168 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
169 PUSH_DATA (push, NV50_3D_COUNTER_RESET_SAMPLECNT);
170 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
171 PUSH_DATA (push, 1);
172 break;
173 case PIPE_QUERY_PRIMITIVES_GENERATED: /* store before & after instead ? */
174 PUSH_SPACE(push, 2);
175 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
176 PUSH_DATA (push, NV50_3D_COUNTER_RESET_GENERATED_PRIMITIVES);
177 break;
178 case PIPE_QUERY_PRIMITIVES_EMITTED:
179 PUSH_SPACE(push, 2);
180 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
181 PUSH_DATA (push, NV50_3D_COUNTER_RESET_TRANSFORM_FEEDBACK);
182 break;
183 case PIPE_QUERY_SO_STATISTICS:
184 PUSH_SPACE(push, 3);
185 BEGIN_NI04(push, NV50_3D(COUNTER_RESET), 2);
186 PUSH_DATA (push, NV50_3D_COUNTER_RESET_TRANSFORM_FEEDBACK);
187 PUSH_DATA (push, NV50_3D_COUNTER_RESET_GENERATED_PRIMITIVES);
188 break;
189 case PIPE_QUERY_TIMESTAMP_DISJOINT:
190 case PIPE_QUERY_TIME_ELAPSED:
191 nv50_query_get(push, q, 0x10, 0x00005002);
192 break;
193 default:
194 break;
195 }
196 q->ready = FALSE;
197 }
198
199 static void
200 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
201 {
202 struct nv50_context *nv50 = nv50_context(pipe);
203 struct nouveau_pushbuf *push = nv50->base.pushbuf;
204 struct nv50_query *q = nv50_query(pq);
205
206 switch (q->type) {
207 case PIPE_QUERY_OCCLUSION_COUNTER:
208 nv50_query_get(push, q, 0, 0x0100f002);
209 PUSH_SPACE(push, 2);
210 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
211 PUSH_DATA (push, 0);
212 break;
213 case PIPE_QUERY_PRIMITIVES_GENERATED:
214 nv50_query_get(push, q, 0, 0x06805002);
215 break;
216 case PIPE_QUERY_PRIMITIVES_EMITTED:
217 nv50_query_get(push, q, 0, 0x05805002);
218 break;
219 case PIPE_QUERY_SO_STATISTICS:
220 nv50_query_get(push, q, 0x00, 0x05805002);
221 nv50_query_get(push, q, 0x10, 0x06805002);
222 break;
223 case PIPE_QUERY_TIMESTAMP_DISJOINT:
224 case PIPE_QUERY_TIME_ELAPSED:
225 nv50_query_get(push, q, 0, 0x00005002);
226 break;
227 case PIPE_QUERY_GPU_FINISHED:
228 nv50_query_get(push, q, 0, 0x1000f010);
229 break;
230 default:
231 assert(0);
232 break;
233 }
234 q->flushed = FALSE;
235 }
236
237 static INLINE boolean
238 nv50_query_ready(struct nv50_query *q)
239 {
240 return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
241 }
242
243 static boolean
244 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
245 boolean wait, union pipe_query_result *result)
246 {
247 struct nv50_context *nv50 = nv50_context(pipe);
248 struct nv50_query *q = nv50_query(pq);
249 uint64_t *res64 = (uint64_t *)result;
250 boolean *res8 = (boolean *)result;
251 uint64_t *data64 = (uint64_t *)q->data;
252
253 if (!q->ready) /* update ? */
254 q->ready = nv50_query_ready(q);
255 if (!q->ready) {
256 if (!wait) {
257 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
258 if (!q->flushed) {
259 q->flushed = TRUE;
260 PUSH_KICK(nv50->base.pushbuf);
261 }
262 return FALSE;
263 }
264 if (nouveau_bo_wait(q->bo, NOUVEAU_BO_RD, nv50->screen->base.client))
265 return FALSE;
266 }
267 q->ready = TRUE;
268
269 switch (q->type) {
270 case PIPE_QUERY_GPU_FINISHED:
271 res8[0] = TRUE;
272 break;
273 case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
274 res64[0] = q->data[1];
275 break;
276 case PIPE_QUERY_PRIMITIVES_GENERATED: /* u64 count, u64 time */
277 case PIPE_QUERY_PRIMITIVES_EMITTED: /* u64 count, u64 time */
278 res64[0] = data64[0];
279 break;
280 case PIPE_QUERY_SO_STATISTICS:
281 res64[0] = data64[0];
282 res64[1] = data64[1];
283 break;
284 case PIPE_QUERY_TIMESTAMP_DISJOINT: /* u32 sequence, u32 0, u64 time */
285 res64[0] = 1000000000;
286 res8[8] = (data64[0] == data64[2]) ? FALSE : TRUE;
287 break;
288 case PIPE_QUERY_TIME_ELAPSED:
289 res64[0] = data64[1] - data64[3];
290 break;
291 default:
292 return FALSE;
293 }
294
295 return TRUE;
296 }
297
298 static void
299 nv50_render_condition(struct pipe_context *pipe,
300 struct pipe_query *pq, uint mode)
301 {
302 struct nv50_context *nv50 = nv50_context(pipe);
303 struct nouveau_pushbuf *push = nv50->base.pushbuf;
304 struct nv50_query *q;
305
306 PUSH_SPACE(push, 6);
307
308 if (!pq) {
309 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
310 PUSH_DATA (push, NV50_3D_COND_MODE_ALWAYS);
311 return;
312 }
313 q = nv50_query(pq);
314
315 if (mode == PIPE_RENDER_COND_WAIT ||
316 mode == PIPE_RENDER_COND_BY_REGION_WAIT) {
317 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
318 PUSH_DATA (push, 0);
319 }
320
321 BEGIN_NV04(push, NV50_3D(COND_ADDRESS_HIGH), 3);
322 PUSH_DATAh(push, q->bo->offset + q->offset);
323 PUSH_DATA (push, q->bo->offset + q->offset);
324 PUSH_DATA (push, NV50_3D_COND_MODE_RES_NON_ZERO);
325 }
326
327 void
328 nv50_init_query_functions(struct nv50_context *nv50)
329 {
330 struct pipe_context *pipe = &nv50->base.pipe;
331
332 pipe->create_query = nv50_query_create;
333 pipe->destroy_query = nv50_query_destroy;
334 pipe->begin_query = nv50_query_begin;
335 pipe->end_query = nv50_query_end;
336 pipe->get_query_result = nv50_query_result;
337 pipe->render_condition = nv50_render_condition;
338 }