v3d: do not automatically flush current job for SSBOs and shader images
[mesa.git] / src / gallium / drivers / v3d / v3d_query.c
1 /*
2 * Copyright © 2014 Broadcom
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * Gallium query object support.
26 *
27 * The HW has native support for occlusion queries, with the query result
28 * being loaded and stored by the TLB unit. From a SW perspective, we have to
29 * be careful to make sure that the jobs that need to be tracking queries are
30 * bracketed by the start and end of counting, even across FBO transitions.
31 *
32 * For the transform feedback PRIMITIVES_GENERATED/WRITTEN queries, we have to
33 * do the calculations in software at draw time.
34 */
35
36 #include "v3d_context.h"
37 #include "broadcom/cle/v3d_packet_v33_pack.h"
38
39 struct v3d_query
40 {
41 enum pipe_query_type type;
42 struct v3d_bo *bo;
43
44 uint32_t start, end;
45 };
46
47 static struct pipe_query *
48 v3d_create_query(struct pipe_context *pctx, unsigned query_type, unsigned index)
49 {
50 struct v3d_query *q = calloc(1, sizeof(*q));
51
52 q->type = query_type;
53
54 /* Note that struct pipe_query isn't actually defined anywhere. */
55 return (struct pipe_query *)q;
56 }
57
58 static void
59 v3d_destroy_query(struct pipe_context *pctx, struct pipe_query *query)
60 {
61 struct v3d_query *q = (struct v3d_query *)query;
62
63 v3d_bo_unreference(&q->bo);
64 free(q);
65 }
66
67 static bool
68 v3d_begin_query(struct pipe_context *pctx, struct pipe_query *query)
69 {
70 struct v3d_context *v3d = v3d_context(pctx);
71 struct v3d_query *q = (struct v3d_query *)query;
72
73 switch (q->type) {
74 case PIPE_QUERY_PRIMITIVES_GENERATED:
75 q->start = v3d->prims_generated;
76 break;
77 case PIPE_QUERY_PRIMITIVES_EMITTED:
78 /* If we are inside transform feedback we need to update the
79 * primitive counts to skip primtives recorded before this.
80 */
81 if (v3d->streamout.num_targets > 0)
82 v3d_tf_update_counters(v3d);
83 q->start = v3d->tf_prims_generated;
84 break;
85 case PIPE_QUERY_OCCLUSION_COUNTER:
86 case PIPE_QUERY_OCCLUSION_PREDICATE:
87 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
88 q->bo = v3d_bo_alloc(v3d->screen, 4096, "query");
89 uint32_t *map = v3d_bo_map(q->bo);
90 *map = 0;
91
92 v3d->current_oq = q->bo;
93 v3d->dirty |= VC5_DIRTY_OQ;
94 break;
95 default:
96 unreachable("unsupported query type");
97 }
98
99 return true;
100 }
101
102 static bool
103 v3d_end_query(struct pipe_context *pctx, struct pipe_query *query)
104 {
105 struct v3d_context *v3d = v3d_context(pctx);
106 struct v3d_query *q = (struct v3d_query *)query;
107
108 switch (q->type) {
109 case PIPE_QUERY_PRIMITIVES_GENERATED:
110 q->end = v3d->prims_generated;
111 break;
112 case PIPE_QUERY_PRIMITIVES_EMITTED:
113 /* If transform feedback has ended, then we have already
114 * updated the primitive counts at glEndTransformFeedback()
115 * time. Otherwise, we have to do it now.
116 */
117 if (v3d->streamout.num_targets > 0)
118 v3d_tf_update_counters(v3d);
119 q->end = v3d->tf_prims_generated;
120 break;
121 case PIPE_QUERY_OCCLUSION_COUNTER:
122 case PIPE_QUERY_OCCLUSION_PREDICATE:
123 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
124 v3d->current_oq = NULL;
125 v3d->dirty |= VC5_DIRTY_OQ;
126 break;
127 default:
128 unreachable("unsupported query type");
129 }
130
131 return true;
132 }
133
134 static bool
135 v3d_get_query_result(struct pipe_context *pctx, struct pipe_query *query,
136 bool wait, union pipe_query_result *vresult)
137 {
138 struct v3d_context *v3d = v3d_context(pctx);
139 struct v3d_query *q = (struct v3d_query *)query;
140 uint32_t result = 0;
141
142 if (q->bo) {
143 v3d_flush_jobs_using_bo(v3d, q->bo);
144
145 if (wait) {
146 if (!v3d_bo_wait(q->bo, ~0ull, "query"))
147 return false;
148 } else {
149 if (!v3d_bo_wait(q->bo, 0, "query"))
150 return false;
151 }
152
153 /* XXX: Sum up per-core values. */
154 uint32_t *map = v3d_bo_map(q->bo);
155 result = *map;
156
157 v3d_bo_unreference(&q->bo);
158 }
159
160 switch (q->type) {
161 case PIPE_QUERY_OCCLUSION_COUNTER:
162 vresult->u64 = result;
163 break;
164 case PIPE_QUERY_OCCLUSION_PREDICATE:
165 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
166 vresult->b = result != 0;
167 break;
168 case PIPE_QUERY_PRIMITIVES_GENERATED:
169 case PIPE_QUERY_PRIMITIVES_EMITTED:
170 vresult->u64 = q->end - q->start;
171 break;
172 default:
173 unreachable("unsupported query type");
174 }
175
176 return true;
177 }
178
179 static void
180 v3d_set_active_query_state(struct pipe_context *pctx, bool enable)
181 {
182 struct v3d_context *v3d = v3d_context(pctx);
183
184 v3d->active_queries = enable;
185 v3d->dirty |= VC5_DIRTY_OQ;
186 v3d->dirty |= VC5_DIRTY_STREAMOUT;
187 }
188
189 void
190 v3d_query_init(struct pipe_context *pctx)
191 {
192 pctx->create_query = v3d_create_query;
193 pctx->destroy_query = v3d_destroy_query;
194 pctx->begin_query = v3d_begin_query;
195 pctx->end_query = v3d_end_query;
196 pctx->get_query_result = v3d_get_query_result;
197 pctx->set_active_query_state = v3d_set_active_query_state;
198 }
199