summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/v3d/v3d_query.c
blob: d3447793b0a4c30306b962b18d81a647c6189ce5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright © 2014 Broadcom
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * Gallium query object support.
 *
 * The HW has native support for occlusion queries, with the query result
 * being loaded and stored by the TLB unit. From a SW perspective, we have to
 * be careful to make sure that the jobs that need to be tracking queries are
 * bracketed by the start and end of counting, even across FBO transitions.
 *
 * For the transform feedback PRIMITIVES_GENERATED/WRITTEN queries, we have to
 * do the calculations in software at draw time.
 */

#include "v3d_context.h"
#include "broadcom/cle/v3d_packet_v33_pack.h"

struct v3d_query
{
        enum pipe_query_type type;
        struct v3d_bo *bo;

        uint32_t start, end;
};

static struct pipe_query *
v3d_create_query(struct pipe_context *pctx, unsigned query_type, unsigned index)
{
        struct v3d_query *q = calloc(1, sizeof(*q));

        q->type = query_type;

        /* Note that struct pipe_query isn't actually defined anywhere. */
        return (struct pipe_query *)q;
}

static void
v3d_destroy_query(struct pipe_context *pctx, struct pipe_query *query)
{
        struct v3d_query *q = (struct v3d_query *)query;

        v3d_bo_unreference(&q->bo);
        free(q);
}

static boolean
v3d_begin_query(struct pipe_context *pctx, struct pipe_query *query)
{
        struct v3d_context *v3d = v3d_context(pctx);
        struct v3d_query *q = (struct v3d_query *)query;

        switch (q->type) {
        case PIPE_QUERY_PRIMITIVES_GENERATED:
                q->start = v3d->prims_generated;
                break;
        case PIPE_QUERY_PRIMITIVES_EMITTED:
                q->start = v3d->tf_prims_generated;
                break;
        default:
                q->bo = v3d_bo_alloc(v3d->screen, 4096, "query");

                uint32_t *map = v3d_bo_map(q->bo);
                *map = 0;
                v3d->current_oq = q->bo;
                v3d->dirty |= VC5_DIRTY_OQ;
                break;
        }

        return true;
}

static bool
v3d_end_query(struct pipe_context *pctx, struct pipe_query *query)
{
        struct v3d_context *v3d = v3d_context(pctx);
        struct v3d_query *q = (struct v3d_query *)query;

        switch (q->type) {
        case PIPE_QUERY_PRIMITIVES_GENERATED:
                q->end = v3d->prims_generated;
                break;
        case PIPE_QUERY_PRIMITIVES_EMITTED:
                q->end = v3d->tf_prims_generated;
                break;
        default:
                v3d->current_oq = NULL;
                v3d->dirty |= VC5_DIRTY_OQ;
                break;
        }

        return true;
}

static boolean
v3d_get_query_result(struct pipe_context *pctx, struct pipe_query *query,
                     boolean wait, union pipe_query_result *vresult)
{
        struct v3d_query *q = (struct v3d_query *)query;
        uint32_t result = 0;

        if (q->bo) {
                /* XXX: Only flush the jobs using this BO. */
                v3d_flush(pctx);

                if (wait) {
                        if (!v3d_bo_wait(q->bo, 0, "query"))
                                return false;
                } else {
                        if (!v3d_bo_wait(q->bo, ~0ull, "query"))
                                return false;
                }

                /* XXX: Sum up per-core values. */
                uint32_t *map = v3d_bo_map(q->bo);
                result = *map;

                v3d_bo_unreference(&q->bo);
        }

        switch (q->type) {
        case PIPE_QUERY_OCCLUSION_COUNTER:
                vresult->u64 = result;
                break;
        case PIPE_QUERY_OCCLUSION_PREDICATE:
        case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
                vresult->b = result != 0;
                break;
        case PIPE_QUERY_PRIMITIVES_GENERATED:
        case PIPE_QUERY_PRIMITIVES_EMITTED:
                vresult->u64 = q->end - q->start;
                break;
        default:
                unreachable("unsupported query type");
        }

        return true;
}

static void
v3d_set_active_query_state(struct pipe_context *pctx, boolean enable)
{
        struct v3d_context *v3d = v3d_context(pctx);

        v3d->active_queries = enable;
        v3d->dirty |= VC5_DIRTY_OQ;
        v3d->dirty |= VC5_DIRTY_STREAMOUT;
}

void
v3d_query_init(struct pipe_context *pctx)
{
        pctx->create_query = v3d_create_query;
        pctx->destroy_query = v3d_destroy_query;
        pctx->begin_query = v3d_begin_query;
        pctx->end_query = v3d_end_query;
        pctx->get_query_result = v3d_get_query_result;
        pctx->set_active_query_state = v3d_set_active_query_state;
}