revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / mesa / src / gallium / drivers / nvc0 / nvc0_query.c
blobead015b6b84e7f4aa03a236c6c600c88f68b15b7
1 /*
2 * Copyright 2011 Nouveau Project
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Christoph Bumiller
25 #include "nvc0_context.h"
26 #include "nouveau/nv_object.xml.h"
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.
31 * The first is not that big of an issue because OpenGL does not allow nested
32 * queries anyway.
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 nouveau_mm_allocation *mm;
47 #define NVC0_QUERY_ALLOC_SPACE 128
49 static INLINE struct nvc0_query *
50 nvc0_query(struct pipe_query *pipe)
52 return (struct nvc0_query *)pipe;
55 static boolean
56 nvc0_query_allocate(struct nvc0_context *nvc0, struct nvc0_query *q, int size)
58 struct nvc0_screen *screen = nvc0->screen;
59 int ret;
61 if (q->bo) {
62 nouveau_bo_ref(NULL, &q->bo);
63 if (q->mm) {
64 if (q->ready)
65 nouveau_mm_free(q->mm);
66 else
67 nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work, q->mm);
70 if (size) {
71 q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
72 if (!q->bo)
73 return FALSE;
74 q->offset = q->base;
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;
82 q->data = q->bo->map;
83 nouveau_bo_unmap(q->bo);
85 return TRUE;
88 static void
89 nvc0_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
91 nvc0_query_allocate(nvc0_context(pipe), nvc0_query(pq), 0);
92 FREE(nvc0_query(pq));
95 static struct pipe_query *
96 nvc0_query_create(struct pipe_context *pipe, unsigned type)
98 struct nvc0_context *nvc0 = nvc0_context(pipe);
99 struct nvc0_query *q;
101 q = CALLOC_STRUCT(nvc0_query);
102 if (!q)
103 return NULL;
105 if (!nvc0_query_allocate(nvc0, q, NVC0_QUERY_ALLOC_SPACE)) {
106 FREE(q);
107 return NULL;
110 q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
111 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
112 type == PIPE_QUERY_SO_STATISTICS);
113 q->type = type;
115 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
116 q->offset -= 16;
117 q->data -= 16 / sizeof(*q->data); /* we advance before query_begin ! */
120 return (struct pipe_query *)q;
123 static void
124 nvc0_query_get(struct nouveau_channel *chan, struct nvc0_query *q,
125 unsigned offset, uint32_t get)
127 offset += q->offset;
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);
137 static void
138 nvc0_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
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);
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.
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);
154 /* XXX: can we do this with the GPU, and sync with respect to a previous
155 * query ?
157 q->data[1] = 1; /* initial render condition = TRUE */
159 if (!q->is64bit)
160 q->data[0] = q->sequence++; /* the previously used one */
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;
187 q->ready = FALSE;
190 static void
191 nvc0_query_end(struct pipe_context *pipe, struct pipe_query *pq)
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);
197 const int index = 0; /* for multiple vertex streams */
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;
228 static INLINE boolean
229 nvc0_query_ready(struct nvc0_query *q)
231 return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
234 static INLINE boolean
235 nvc0_query_wait(struct nvc0_query *q)
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;
244 static boolean
245 nvc0_query_result(struct pipe_context *pipe, struct pipe_query *pq,
246 boolean wait, void *result)
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;
254 if (q->type == PIPE_QUERY_GPU_FINISHED) {
255 res8[0] = nvc0_query_ready(q);
256 return TRUE;
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;
268 if (!nvc0_query_wait(q))
269 return FALSE;
271 q->ready = TRUE;
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;
296 return TRUE;
299 static void
300 nvc0_render_condition(struct pipe_context *pipe,
301 struct pipe_query *pq, uint mode)
303 struct nvc0_context *nvc0 = nvc0_context(pipe);
304 struct nouveau_channel *chan = nvc0->screen->base.channel;
305 struct nvc0_query *q;
307 if (!pq) {
308 IMMED_RING(chan, RING_3D(COND_MODE), NVC0_3D_COND_MODE_ALWAYS);
309 return;
311 q = nvc0_query(pq);
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);
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);
330 void
331 nvc0_init_query_functions(struct nvc0_context *nvc0)
333 struct pipe_context *pipe = &nvc0->base.pipe;
335 pipe->create_query = nvc0_query_create;
336 pipe->destroy_query = nvc0_query_destroy;
337 pipe->begin_query = nvc0_query_begin;
338 pipe->end_query = nvc0_query_end;
339 pipe->get_query_result = nvc0_query_result;
340 pipe->render_condition = nvc0_render_condition;