nvc0: fix user vertex buffer updates
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_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 #include "nvc0_context.h"
26 #include "nouveau/nv_object.xml.h"
27
28 /* XXX: Nested queries, and simultaneous queries on multiple gallium contexts
29 * (since we use only a single GPU channel per screen) will not work properly.
30 *
31 * The first is not that big of an issue because OpenGL does not allow nested
32 * queries anyway.
33 */
34
35 struct nvc0_query {
36 uint32_t *data;
37 uint32_t type;
38 uint32_t sequence;
39 struct nouveau_bo *bo;
40 uint32_t base;
41 uint32_t offset; /* base + i * 16 */
42 boolean ready;
43 boolean is64bit;
44 struct nvc0_mm_allocation *mm;
45 };
46
47 #define NVC0_QUERY_ALLOC_SPACE 128
48
49 static INLINE struct nvc0_query *
50 nvc0_query(struct pipe_query *pipe)
51 {
52 return (struct nvc0_query *)pipe;
53 }
54
55 static boolean
56 nvc0_query_allocate(struct nvc0_context *nvc0, struct nvc0_query *q, int size)
57 {
58 struct nvc0_screen *screen = nvc0->screen;
59 int ret;
60
61 if (q->bo) {
62 nouveau_bo_ref(NULL, &q->bo);
63 if (q->mm) {
64 if (q->ready)
65 nvc0_mm_free(q->mm);
66 else
67 nvc0_fence_sched_release(screen->fence.current, q->mm);
68 }
69 }
70 if (size) {
71 q->mm = nvc0_mm_allocate(screen->mm_GART, size, &q->bo, &q->base);
72 if (!q->bo)
73 return FALSE;
74 q->offset = q->base;
75
76 ret = nouveau_bo_map_range(q->bo, q->base, size, NOUVEAU_BO_RD |
77 NOUVEAU_BO_NOSYNC);
78 if (ret) {
79 nvc0_query_allocate(nvc0, q, 0);
80 return FALSE;
81 }
82 q->data = q->bo->map;
83 nouveau_bo_unmap(q->bo);
84 }
85 return TRUE;
86 }
87
88 static void
89 nvc0_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
90 {
91 nvc0_query_allocate(nvc0_context(pipe), nvc0_query(pq), 0);
92 FREE(nvc0_query(pq));
93 }
94
95 static struct pipe_query *
96 nvc0_query_create(struct pipe_context *pipe, unsigned type)
97 {
98 struct nvc0_context *nvc0 = nvc0_context(pipe);
99 struct nvc0_query *q;
100
101 q = CALLOC_STRUCT(nvc0_query);
102 if (!q)
103 return NULL;
104
105 if (!nvc0_query_allocate(nvc0, q, NVC0_QUERY_ALLOC_SPACE)) {
106 FREE(q);
107 return NULL;
108 }
109
110 q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
111 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
112 type == PIPE_QUERY_SO_STATISTICS);
113 q->type = type;
114
115 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
116 q->offset -= 16;
117 q->data -= 16 / sizeof(*q->data); /* we advance before query_begin ! */
118 }
119
120 return (struct pipe_query *)q;
121 }
122
123 static void
124 nvc0_query_get(struct nouveau_channel *chan, struct nvc0_query *q,
125 unsigned offset, uint32_t get)
126 {
127 offset += q->offset;
128
129 MARK_RING (chan, 5, 2);
130 BEGIN_RING(chan, RING_3D(QUERY_ADDRESS_HIGH), 4);
131 OUT_RELOCh(chan, q->bo, offset, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
132 OUT_RELOCl(chan, q->bo, offset, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
133 OUT_RING (chan, q->sequence);
134 OUT_RING (chan, get);
135 }
136
137 static void
138 nvc0_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
139 {
140 struct nvc0_context *nvc0 = nvc0_context(pipe);
141 struct nouveau_channel *chan = nvc0->screen->base.channel;
142 struct nvc0_query *q = nvc0_query(pq);
143
144 /* For occlusion queries we have to change the storage, because a previous
145 * query might set the initial render conition to FALSE even *after* we re-
146 * initialized it to TRUE.
147 */
148 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
149 q->offset += 16;
150 q->data += 16 / sizeof(*q->data);
151 if (q->offset - q->base == NVC0_QUERY_ALLOC_SPACE)
152 nvc0_query_allocate(nvc0, q, NVC0_QUERY_ALLOC_SPACE);
153
154 /* XXX: can we do this with the GPU, and sync with respect to a previous
155 * query ?
156 */
157 q->data[1] = 1; /* initial render condition = TRUE */
158 }
159 if (!q->is64bit)
160 q->data[0] = q->sequence++; /* the previously used one */
161
162 switch (q->type) {
163 case PIPE_QUERY_OCCLUSION_COUNTER:
164 IMMED_RING(chan, RING_3D(COUNTER_RESET), NVC0_3D_COUNTER_RESET_SAMPLECNT);
165 IMMED_RING(chan, RING_3D(SAMPLECNT_ENABLE), 1);
166 break;
167 case PIPE_QUERY_PRIMITIVES_GENERATED: /* store before & after instead ? */
168 IMMED_RING(chan, RING_3D(COUNTER_RESET),
169 NVC0_3D_COUNTER_RESET_GENERATED_PRIMITIVES);
170 break;
171 case PIPE_QUERY_PRIMITIVES_EMITTED:
172 IMMED_RING(chan, RING_3D(COUNTER_RESET),
173 NVC0_3D_COUNTER_RESET_EMITTED_PRIMITIVES);
174 break;
175 case PIPE_QUERY_SO_STATISTICS:
176 BEGIN_RING_NI(chan, RING_3D(COUNTER_RESET), 2);
177 OUT_RING (chan, NVC0_3D_COUNTER_RESET_EMITTED_PRIMITIVES);
178 OUT_RING (chan, NVC0_3D_COUNTER_RESET_GENERATED_PRIMITIVES);
179 break;
180 case PIPE_QUERY_TIMESTAMP_DISJOINT:
181 case PIPE_QUERY_TIME_ELAPSED:
182 nvc0_query_get(chan, q, 0x10, 0x00005002);
183 break;
184 default:
185 break;
186 }
187 q->ready = FALSE;
188 }
189
190 static void
191 nvc0_query_end(struct pipe_context *pipe, struct pipe_query *pq)
192 {
193 struct nvc0_context *nvc0 = nvc0_context(pipe);
194 struct nouveau_channel *chan = nvc0->screen->base.channel;
195 struct nvc0_query *q = nvc0_query(pq);
196
197 const int index = 0; /* for multiple vertex streams */
198
199 switch (q->type) {
200 case PIPE_QUERY_OCCLUSION_COUNTER:
201 nvc0_query_get(chan, q, 0, 0x0100f002);
202 BEGIN_RING(chan, RING_3D(SAMPLECNT_ENABLE), 1);
203 OUT_RING (chan, 0);
204 break;
205 case PIPE_QUERY_PRIMITIVES_GENERATED:
206 nvc0_query_get(chan, q, 0, 0x09005002 | (index << 5));
207 break;
208 case PIPE_QUERY_PRIMITIVES_EMITTED:
209 nvc0_query_get(chan, q, 0, 0x05805002 | (index << 5));
210 break;
211 case PIPE_QUERY_SO_STATISTICS:
212 nvc0_query_get(chan, q, 0x00, 0x05805002 | (index << 5));
213 nvc0_query_get(chan, q, 0x10, 0x09005002 | (index << 5));
214 break;
215 case PIPE_QUERY_TIMESTAMP_DISJOINT:
216 case PIPE_QUERY_TIME_ELAPSED:
217 nvc0_query_get(chan, q, 0, 0x00005002);
218 break;
219 case PIPE_QUERY_GPU_FINISHED:
220 nvc0_query_get(chan, q, 0, 0x1000f010);
221 break;
222 default:
223 assert(0);
224 break;
225 }
226 }
227
228 static INLINE boolean
229 nvc0_query_ready(struct nvc0_query *q)
230 {
231 return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
232 }
233
234 static INLINE boolean
235 nvc0_query_wait(struct nvc0_query *q)
236 {
237 int ret = nouveau_bo_map(q->bo, NOUVEAU_BO_RD);
238 if (ret)
239 return FALSE;
240 nouveau_bo_unmap(q->bo);
241 return TRUE;
242 }
243
244 static boolean
245 nvc0_query_result(struct pipe_context *pipe, struct pipe_query *pq,
246 boolean wait, void *result)
247 {
248 struct nvc0_query *q = nvc0_query(pq);
249 uint64_t *res64 = result;
250 uint32_t *res32 = result;
251 boolean *res8 = result;
252 uint64_t *data64 = (uint64_t *)q->data;
253
254 if (q->type == PIPE_QUERY_GPU_FINISHED) {
255 res8[0] = nvc0_query_ready(q);
256 return TRUE;
257 }
258
259 if (!q->ready) /* update ? */
260 q->ready = nvc0_query_ready(q);
261 if (!q->ready) {
262 struct nouveau_channel *chan = nvc0_context(pipe)->screen->base.channel;
263 if (!wait) {
264 if (nouveau_bo_pending(q->bo) & NOUVEAU_BO_WR) /* for daft apps */
265 FIRE_RING(chan);
266 return FALSE;
267 }
268 if (!nvc0_query_wait(q))
269 return FALSE;
270 }
271 q->ready = TRUE;
272
273 switch (q->type) {
274 case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
275 res32[0] = q->data[1];
276 break;
277 case PIPE_QUERY_PRIMITIVES_GENERATED: /* u64 count, u64 time */
278 case PIPE_QUERY_PRIMITIVES_EMITTED: /* u64 count, u64 time */
279 res64[0] = data64[0];
280 break;
281 case PIPE_QUERY_SO_STATISTICS:
282 res64[0] = data64[0];
283 res64[1] = data64[1];
284 break;
285 case PIPE_QUERY_TIMESTAMP_DISJOINT: /* u32 sequence, u32 0, u64 time */
286 res64[0] = 1000000000;
287 res8[8] = (data64[0] == data64[2]) ? FALSE : TRUE;
288 break;
289 case PIPE_QUERY_TIME_ELAPSED:
290 res64[0] = data64[1] - data64[3];
291 break;
292 default:
293 return FALSE;
294 }
295
296 return TRUE;
297 }
298
299 static void
300 nvc0_render_condition(struct pipe_context *pipe,
301 struct pipe_query *pq, uint mode)
302 {
303 struct nvc0_context *nvc0 = nvc0_context(pipe);
304 struct nouveau_channel *chan = nvc0->screen->base.channel;
305 struct nvc0_query *q;
306
307 if (!pq) {
308 IMMED_RING(chan, RING_3D(COND_MODE), NVC0_3D_COND_MODE_ALWAYS);
309 return;
310 }
311 q = nvc0_query(pq);
312
313 if (mode == PIPE_RENDER_COND_WAIT ||
314 mode == PIPE_RENDER_COND_BY_REGION_WAIT) {
315 MARK_RING (chan, 5, 2);
316 BEGIN_RING(chan, RING_3D_(NV84_SUBCHAN_QUERY_ADDRESS_HIGH), 4);
317 OUT_RELOCh(chan, q->bo, q->offset, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
318 OUT_RELOCl(chan, q->bo, q->offset, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
319 OUT_RING (chan, q->sequence);
320 OUT_RING (chan, 0x00001001);
321 }
322
323 MARK_RING (chan, 4, 2);
324 BEGIN_RING(chan, RING_3D(COND_ADDRESS_HIGH), 3);
325 OUT_RELOCh(chan, q->bo, q->offset, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
326 OUT_RELOCl(chan, q->bo, q->offset, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
327 OUT_RING (chan, NVC0_3D_COND_MODE_RES_NON_ZERO);
328 }
329
330 void
331 nvc0_init_query_functions(struct nvc0_context *nvc0)
332 {
333 nvc0->pipe.create_query = nvc0_query_create;
334 nvc0->pipe.destroy_query = nvc0_query_destroy;
335 nvc0->pipe.begin_query = nvc0_query_begin;
336 nvc0->pipe.end_query = nvc0_query_end;
337 nvc0->pipe.get_query_result = nvc0_query_result;
338 nvc0->pipe.render_condition = nvc0_render_condition;
339 }