OSDN Git Service

gallium: make pipe_context::begin_query return a boolean
[android-x86/external-mesa.git] / src / gallium / drivers / nouveau / 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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Christoph Bumiller
23  */
24
25 #define NV50_PUSH_EXPLICIT_SPACE_CHECKING
26
27 #include "nv50/nv50_context.h"
28 #include "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    uint16_t type;
40    uint16_t index;
41    uint32_t sequence;
42    struct nouveau_bo *bo;
43    uint32_t base;
44    uint32_t offset; /* base + i * 32 */
45    boolean ready;
46    boolean flushed;
47    boolean is64bit;
48    struct nouveau_mm_allocation *mm;
49 };
50
51 #define NV50_QUERY_ALLOC_SPACE 256
52
53 static INLINE struct nv50_query *
54 nv50_query(struct pipe_query *pipe)
55 {
56    return (struct nv50_query *)pipe;
57 }
58
59 static boolean
60 nv50_query_allocate(struct nv50_context *nv50, struct nv50_query *q, int size)
61 {
62    struct nv50_screen *screen = nv50->screen;
63    int ret;
64
65    if (q->bo) {
66       nouveau_bo_ref(NULL, &q->bo);
67       if (q->mm) {
68          if (q->ready)
69             nouveau_mm_free(q->mm);
70          else
71             nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work,
72                                q->mm);
73       }
74    }
75    if (size) {
76       q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
77       if (!q->bo)
78          return FALSE;
79       q->offset = q->base;
80
81       ret = nouveau_bo_map(q->bo, 0, screen->base.client);
82       if (ret) {
83          nv50_query_allocate(nv50, q, 0);
84          return FALSE;
85       }
86       q->data = (uint32_t *)((uint8_t *)q->bo->map + q->base);
87    }
88    return TRUE;
89 }
90
91 static void
92 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
93 {
94    nv50_query_allocate(nv50_context(pipe), nv50_query(pq), 0);
95    FREE(nv50_query(pq));
96 }
97
98 static struct pipe_query *
99 nv50_query_create(struct pipe_context *pipe, unsigned type, unsigned index)
100 {
101    struct nv50_context *nv50 = nv50_context(pipe);
102    struct nv50_query *q;
103
104    q = CALLOC_STRUCT(nv50_query);
105    if (!q)
106       return NULL;
107
108    if (!nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE)) {
109       FREE(q);
110       return NULL;
111    }
112
113    q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
114                  type == PIPE_QUERY_PRIMITIVES_EMITTED ||
115                  type == PIPE_QUERY_SO_STATISTICS);
116    q->type = type;
117
118    if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
119       q->offset -= 32;
120       q->data -= 32 / sizeof(*q->data); /* we advance before query_begin ! */
121    }
122
123    return (struct pipe_query *)q;
124 }
125
126 static void
127 nv50_query_get(struct nouveau_pushbuf *push, struct nv50_query *q,
128                unsigned offset, uint32_t get)
129 {
130    offset += q->offset;
131
132    PUSH_SPACE(push, 5);
133    PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
134    BEGIN_NV04(push, NV50_3D(QUERY_ADDRESS_HIGH), 4);
135    PUSH_DATAh(push, q->bo->offset + offset);
136    PUSH_DATA (push, q->bo->offset + offset);
137    PUSH_DATA (push, q->sequence);
138    PUSH_DATA (push, get);
139 }
140
141 static boolean
142 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
143 {
144    struct nv50_context *nv50 = nv50_context(pipe);
145    struct nouveau_pushbuf *push = nv50->base.pushbuf;
146    struct nv50_query *q = nv50_query(pq);
147
148    /* For occlusion queries we have to change the storage, because a previous
149     * query might set the initial render conition to FALSE even *after* we re-
150     * initialized it to TRUE.
151     */
152    if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
153       q->offset += 32;
154       q->data += 32 / sizeof(*q->data);
155       if (q->offset - q->base == NV50_QUERY_ALLOC_SPACE)
156          nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE);
157
158       /* XXX: can we do this with the GPU, and sync with respect to a previous
159        *  query ?
160        */
161       q->data[0] = q->sequence; /* initialize sequence */
162       q->data[1] = 1; /* initial render condition = TRUE */
163       q->data[4] = q->sequence + 1; /* for comparison COND_MODE */
164       q->data[5] = 0;
165    }
166    if (!q->is64bit)
167       q->data[0] = q->sequence++; /* the previously used one */
168
169    switch (q->type) {
170    case PIPE_QUERY_OCCLUSION_COUNTER:
171       PUSH_SPACE(push, 4);
172       BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
173       PUSH_DATA (push, NV50_3D_COUNTER_RESET_SAMPLECNT);
174       BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
175       PUSH_DATA (push, 1);
176       break;
177    case PIPE_QUERY_PRIMITIVES_GENERATED:
178       nv50_query_get(push, q, 0x10, 0x06805002);
179       break;
180    case PIPE_QUERY_PRIMITIVES_EMITTED:
181       nv50_query_get(push, q, 0x10, 0x05805002);
182       break;
183    case PIPE_QUERY_SO_STATISTICS:
184       nv50_query_get(push, q, 0x20, 0x05805002);
185       nv50_query_get(push, q, 0x30, 0x06805002);
186       break;
187    case PIPE_QUERY_PIPELINE_STATISTICS:
188       nv50_query_get(push, q, 0x80, 0x00801002); /* VFETCH, VERTICES */
189       nv50_query_get(push, q, 0x90, 0x01801002); /* VFETCH, PRIMS */
190       nv50_query_get(push, q, 0xa0, 0x02802002); /* VP, LAUNCHES */
191       nv50_query_get(push, q, 0xb0, 0x03806002); /* GP, LAUNCHES */
192       nv50_query_get(push, q, 0xc0, 0x04806002); /* GP, PRIMS_OUT */
193       nv50_query_get(push, q, 0xd0, 0x07804002); /* RAST, PRIMS_IN */
194       nv50_query_get(push, q, 0xe0, 0x08804002); /* RAST, PRIMS_OUT */
195       nv50_query_get(push, q, 0xf0, 0x0980a002); /* ROP, PIXELS */
196       break;
197    case PIPE_QUERY_TIME_ELAPSED:
198       nv50_query_get(push, q, 0x10, 0x00005002);
199       break;
200    default:
201       break;
202    }
203    q->ready = FALSE;
204    return true;
205 }
206
207 static void
208 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
209 {
210    struct nv50_context *nv50 = nv50_context(pipe);
211    struct nouveau_pushbuf *push = nv50->base.pushbuf;
212    struct nv50_query *q = nv50_query(pq);
213
214    switch (q->type) {
215    case PIPE_QUERY_OCCLUSION_COUNTER:
216       nv50_query_get(push, q, 0, 0x0100f002);
217       PUSH_SPACE(push, 2);
218       BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
219       PUSH_DATA (push, 0);
220       break;
221    case PIPE_QUERY_PRIMITIVES_GENERATED:
222       nv50_query_get(push, q, 0, 0x06805002);
223       break;
224    case PIPE_QUERY_PRIMITIVES_EMITTED:
225       nv50_query_get(push, q, 0, 0x05805002);
226       break;
227    case PIPE_QUERY_SO_STATISTICS:
228       nv50_query_get(push, q, 0x00, 0x05805002);
229       nv50_query_get(push, q, 0x10, 0x06805002);
230       break;
231    case PIPE_QUERY_PIPELINE_STATISTICS:
232       nv50_query_get(push, q, 0x00, 0x00801002); /* VFETCH, VERTICES */
233       nv50_query_get(push, q, 0x10, 0x01801002); /* VFETCH, PRIMS */
234       nv50_query_get(push, q, 0x20, 0x02802002); /* VP, LAUNCHES */
235       nv50_query_get(push, q, 0x30, 0x03806002); /* GP, LAUNCHES */
236       nv50_query_get(push, q, 0x40, 0x04806002); /* GP, PRIMS_OUT */
237       nv50_query_get(push, q, 0x50, 0x07804002); /* RAST, PRIMS_IN */
238       nv50_query_get(push, q, 0x60, 0x08804002); /* RAST, PRIMS_OUT */
239       nv50_query_get(push, q, 0x70, 0x0980a002); /* ROP, PIXELS */
240       break;
241    case PIPE_QUERY_TIMESTAMP:
242       q->sequence++;
243       /* fall through */
244    case PIPE_QUERY_TIME_ELAPSED:
245       nv50_query_get(push, q, 0, 0x00005002);
246       break;
247    case PIPE_QUERY_GPU_FINISHED:
248       q->sequence++;
249       nv50_query_get(push, q, 0, 0x1000f010);
250       break;
251    case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
252       nv50_query_get(push, q, 0, 0x0d005002 | (q->index << 5));
253       break;
254    case PIPE_QUERY_TIMESTAMP_DISJOINT:
255       /* This query is not issued on GPU because disjoint is forced to FALSE */
256       q->ready = TRUE;
257       break;
258    default:
259       assert(0);
260       break;
261    }
262    q->ready = q->flushed = FALSE;
263 }
264
265 static INLINE boolean
266 nv50_query_ready(struct nv50_query *q)
267 {
268    return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
269 }
270
271 static boolean
272 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
273                   boolean wait, union pipe_query_result *result)
274 {
275    struct nv50_context *nv50 = nv50_context(pipe);
276    struct nv50_query *q = nv50_query(pq);
277    uint64_t *res64 = (uint64_t *)result;
278    uint32_t *res32 = (uint32_t *)result;
279    boolean *res8 = (boolean *)result;
280    uint64_t *data64 = (uint64_t *)q->data;
281    int i;
282
283    if (!q->ready) /* update ? */
284       q->ready = nv50_query_ready(q);
285    if (!q->ready) {
286       if (!wait) {
287          /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
288          if (!q->flushed) {
289             q->flushed = TRUE;
290             PUSH_KICK(nv50->base.pushbuf);
291          }
292          return FALSE;
293       }
294       if (nouveau_bo_wait(q->bo, NOUVEAU_BO_RD, nv50->screen->base.client))
295          return FALSE;
296    }
297    q->ready = TRUE;
298
299    switch (q->type) {
300    case PIPE_QUERY_GPU_FINISHED:
301       res8[0] = TRUE;
302       break;
303    case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
304       res64[0] = q->data[1];
305       break;
306    case PIPE_QUERY_PRIMITIVES_GENERATED: /* u64 count, u64 time */
307    case PIPE_QUERY_PRIMITIVES_EMITTED: /* u64 count, u64 time */
308       res64[0] = data64[0] - data64[2];
309       break;
310    case PIPE_QUERY_SO_STATISTICS:
311       res64[0] = data64[0] - data64[4];
312       res64[1] = data64[2] - data64[6];
313       break;
314    case PIPE_QUERY_PIPELINE_STATISTICS:
315       for (i = 0; i < 8; ++i)
316          res64[i] = data64[i * 2] - data64[16 + i * 2];
317       break;
318    case PIPE_QUERY_TIMESTAMP:
319       res64[0] = data64[1];
320       break;
321    case PIPE_QUERY_TIMESTAMP_DISJOINT:
322       res64[0] = 1000000000;
323       res8[8] = FALSE;
324       break;
325    case PIPE_QUERY_TIME_ELAPSED:
326       res64[0] = data64[1] - data64[3];
327       break;
328    case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
329       res32[0] = q->data[1];
330       break;
331    default:
332       return FALSE;
333    }
334
335    return TRUE;
336 }
337
338 void
339 nv84_query_fifo_wait(struct nouveau_pushbuf *push, struct pipe_query *pq)
340 {
341    struct nv50_query *q = nv50_query(pq);
342    unsigned offset = q->offset;
343
344    PUSH_SPACE(push, 5);
345    PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
346    BEGIN_NV04(push, SUBC_3D(NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH), 4);
347    PUSH_DATAh(push, q->bo->offset + offset);
348    PUSH_DATA (push, q->bo->offset + offset);
349    PUSH_DATA (push, q->sequence);
350    PUSH_DATA (push, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
351 }
352
353 static void
354 nv50_render_condition(struct pipe_context *pipe,
355                       struct pipe_query *pq,
356                       boolean condition, uint mode)
357 {
358    struct nv50_context *nv50 = nv50_context(pipe);
359    struct nouveau_pushbuf *push = nv50->base.pushbuf;
360    struct nv50_query *q;
361    uint32_t cond;
362    boolean wait =
363       mode != PIPE_RENDER_COND_NO_WAIT &&
364       mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
365
366    if (!pq) {
367       cond = NV50_3D_COND_MODE_ALWAYS;
368    }
369    else {
370       q = nv50_query(pq);
371       /* NOTE: comparison of 2 queries only works if both have completed */
372       switch (q->type) {
373       case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
374          cond = condition ? NV50_3D_COND_MODE_EQUAL :
375                             NV50_3D_COND_MODE_NOT_EQUAL;
376          wait = TRUE;
377          break;
378       case PIPE_QUERY_OCCLUSION_COUNTER:
379       case PIPE_QUERY_OCCLUSION_PREDICATE:
380          if (likely(!condition)) {
381             /* XXX: Placeholder, handle nesting here if available */
382             if (unlikely(false))
383                cond = wait ? NV50_3D_COND_MODE_NOT_EQUAL :
384                              NV50_3D_COND_MODE_ALWAYS;
385             else
386                cond = NV50_3D_COND_MODE_RES_NON_ZERO;
387          } else {
388             cond = wait ? NV50_3D_COND_MODE_EQUAL : NV50_3D_COND_MODE_ALWAYS;
389          }
390          break;
391       default:
392          assert(!"render condition query not a predicate");
393          cond = NV50_3D_COND_MODE_ALWAYS;
394          break;
395       }
396    }
397
398    nv50->cond_query = pq;
399    nv50->cond_cond = condition;
400    nv50->cond_condmode = cond;
401    nv50->cond_mode = mode;
402
403    if (!pq) {
404       PUSH_SPACE(push, 2);
405       BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
406       PUSH_DATA (push, cond);
407       return;
408    }
409
410    PUSH_SPACE(push, 9);
411
412    if (wait) {
413       BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
414       PUSH_DATA (push, 0);
415    }
416
417    PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
418    BEGIN_NV04(push, NV50_3D(COND_ADDRESS_HIGH), 3);
419    PUSH_DATAh(push, q->bo->offset + q->offset);
420    PUSH_DATA (push, q->bo->offset + q->offset);
421    PUSH_DATA (push, cond);
422
423    BEGIN_NV04(push, NV50_2D(COND_ADDRESS_HIGH), 2);
424    PUSH_DATAh(push, q->bo->offset + q->offset);
425    PUSH_DATA (push, q->bo->offset + q->offset);
426 }
427
428 void
429 nv50_query_pushbuf_submit(struct nouveau_pushbuf *push,
430                           struct pipe_query *pq, unsigned result_offset)
431 {
432    struct nv50_query *q = nv50_query(pq);
433
434    /* XXX: does this exist ? */
435 #define NV50_IB_ENTRY_1_NO_PREFETCH (0 << (31 - 8))
436
437    nouveau_pushbuf_space(push, 0, 0, 1);
438    nouveau_pushbuf_data(push, q->bo, q->offset + result_offset, 4 |
439                         NV50_IB_ENTRY_1_NO_PREFETCH);
440 }
441
442 void
443 nva0_so_target_save_offset(struct pipe_context *pipe,
444                            struct pipe_stream_output_target *ptarg,
445                            unsigned index, boolean serialize)
446 {
447    struct nv50_so_target *targ = nv50_so_target(ptarg);
448
449    if (serialize) {
450       struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
451       PUSH_SPACE(push, 2);
452       BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
453       PUSH_DATA (push, 0);
454    }
455
456    nv50_query(targ->pq)->index = index;
457    nv50_query_end(pipe, targ->pq);
458 }
459
460 void
461 nv50_init_query_functions(struct nv50_context *nv50)
462 {
463    struct pipe_context *pipe = &nv50->base.pipe;
464
465    pipe->create_query = nv50_query_create;
466    pipe->destroy_query = nv50_query_destroy;
467    pipe->begin_query = nv50_query_begin;
468    pipe->end_query = nv50_query_end;
469    pipe->get_query_result = nv50_query_result;
470    pipe->render_condition = nv50_render_condition;
471 }