freedreno: Remove always-true return from per-gen begin_query.
[mesa.git] / src / gallium / drivers / freedreno / freedreno_query_acc.c
1 /*
2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/u_memory.h"
28 #include "util/u_inlines.h"
29
30 #include "freedreno_query_acc.h"
31 #include "freedreno_context.h"
32 #include "freedreno_resource.h"
33 #include "freedreno_util.h"
34
35
36 static bool
37 is_active(struct fd_acc_query *aq, enum fd_render_stage stage)
38 {
39 return !!(aq->provider->active & stage);
40 }
41
42 static void
43 fd_acc_destroy_query(struct fd_context *ctx, struct fd_query *q)
44 {
45 struct fd_acc_query *aq = fd_acc_query(q);
46
47 DBG("%p: active=%d", q, q->active);
48
49 pipe_resource_reference(&aq->prsc, NULL);
50 list_del(&aq->node);
51
52 free(aq->query_data);
53 free(aq);
54 }
55
56 static void
57 realloc_query_bo(struct fd_context *ctx, struct fd_acc_query *aq)
58 {
59 struct fd_resource *rsc;
60 void *map;
61
62 pipe_resource_reference(&aq->prsc, NULL);
63
64 aq->prsc = pipe_buffer_create(&ctx->screen->base,
65 PIPE_BIND_QUERY_BUFFER, 0, 0x1000);
66
67 /* don't assume the buffer is zero-initialized: */
68 rsc = fd_resource(aq->prsc);
69
70 fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_WRITE);
71
72 map = fd_bo_map(rsc->bo);
73 memset(map, 0, aq->size);
74 fd_bo_cpu_fini(rsc->bo);
75 }
76
77 static void
78 fd_acc_begin_query(struct fd_context *ctx, struct fd_query *q)
79 {
80 struct fd_batch *batch = fd_context_batch(ctx);
81 struct fd_acc_query *aq = fd_acc_query(q);
82 const struct fd_acc_sample_provider *p = aq->provider;
83
84 DBG("%p: active=%d", q, q->active);
85
86 /* ->begin_query() discards previous results, so realloc bo: */
87 realloc_query_bo(ctx, aq);
88
89 /* then resume query if needed to collect first sample: */
90 if (batch && is_active(aq, batch->stage))
91 p->resume(aq, batch);
92
93 /* add to active list: */
94 assert(list_is_empty(&aq->node));
95 list_addtail(&aq->node, &ctx->acc_active_queries);
96 }
97
98 static void
99 fd_acc_end_query(struct fd_context *ctx, struct fd_query *q)
100 {
101 struct fd_batch *batch = fd_context_batch(ctx);
102 struct fd_acc_query *aq = fd_acc_query(q);
103 const struct fd_acc_sample_provider *p = aq->provider;
104
105 DBG("%p: active=%d", q, q->active);
106
107 if (batch && is_active(aq, batch->stage))
108 p->pause(aq, batch);
109
110 /* remove from active list: */
111 list_delinit(&aq->node);
112 }
113
114 static bool
115 fd_acc_get_query_result(struct fd_context *ctx, struct fd_query *q,
116 bool wait, union pipe_query_result *result)
117 {
118 struct fd_acc_query *aq = fd_acc_query(q);
119 const struct fd_acc_sample_provider *p = aq->provider;
120 struct fd_resource *rsc = fd_resource(aq->prsc);
121
122 DBG("%p: wait=%d, active=%d", q, wait, q->active);
123
124 assert(list_is_empty(&aq->node));
125
126 /* if !wait, then check the last sample (the one most likely to
127 * not be ready yet) and bail if it is not ready:
128 */
129 if (!wait) {
130 int ret;
131
132 if (pending(rsc, false)) {
133 /* piglit spec@arb_occlusion_query@occlusion_query_conform
134 * test, and silly apps perhaps, get stuck in a loop trying
135 * to get query result forever with wait==false.. we don't
136 * wait to flush unnecessarily but we also don't want to
137 * spin forever:
138 */
139 if (aq->no_wait_cnt++ > 5)
140 fd_batch_flush(rsc->write_batch);
141 return false;
142 }
143
144 ret = fd_bo_cpu_prep(rsc->bo, ctx->pipe,
145 DRM_FREEDRENO_PREP_READ | DRM_FREEDRENO_PREP_NOSYNC);
146 if (ret)
147 return false;
148
149 fd_bo_cpu_fini(rsc->bo);
150 }
151
152 if (rsc->write_batch)
153 fd_batch_flush(rsc->write_batch);
154
155 /* get the result: */
156 fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_READ);
157
158 void *ptr = fd_bo_map(rsc->bo);
159 p->result(aq, ptr, result);
160 fd_bo_cpu_fini(rsc->bo);
161
162 return true;
163 }
164
165 static const struct fd_query_funcs acc_query_funcs = {
166 .destroy_query = fd_acc_destroy_query,
167 .begin_query = fd_acc_begin_query,
168 .end_query = fd_acc_end_query,
169 .get_query_result = fd_acc_get_query_result,
170 };
171
172 struct fd_query *
173 fd_acc_create_query2(struct fd_context *ctx, unsigned query_type,
174 unsigned index, const struct fd_acc_sample_provider *provider)
175 {
176 struct fd_acc_query *aq;
177 struct fd_query *q;
178
179 aq = CALLOC_STRUCT(fd_acc_query);
180 if (!aq)
181 return NULL;
182
183 DBG("%p: query_type=%u", aq, query_type);
184
185 aq->provider = provider;
186 aq->size = provider->size;
187
188 list_inithead(&aq->node);
189
190 q = &aq->base;
191 q->funcs = &acc_query_funcs;
192 q->type = query_type;
193 q->index = index;
194
195 return q;
196 }
197
198 struct fd_query *
199 fd_acc_create_query(struct fd_context *ctx, unsigned query_type,
200 unsigned index)
201 {
202 int idx = pidx(query_type);
203
204 if ((idx < 0) || !ctx->acc_sample_providers[idx])
205 return NULL;
206
207 return fd_acc_create_query2(ctx, query_type, index,
208 ctx->acc_sample_providers[idx]);
209 }
210
211 void
212 fd_acc_query_set_stage(struct fd_batch *batch, enum fd_render_stage stage)
213 {
214 if (stage != batch->stage) {
215 struct fd_acc_query *aq;
216 LIST_FOR_EACH_ENTRY(aq, &batch->ctx->acc_active_queries, node) {
217 const struct fd_acc_sample_provider *p = aq->provider;
218
219 bool was_active = is_active(aq, batch->stage);
220 bool now_active = is_active(aq, stage);
221
222 if (now_active && !was_active)
223 p->resume(aq, batch);
224 else if (was_active && !now_active)
225 p->pause(aq, batch);
226 }
227 }
228 }
229
230 void
231 fd_acc_query_register_provider(struct pipe_context *pctx,
232 const struct fd_acc_sample_provider *provider)
233 {
234 struct fd_context *ctx = fd_context(pctx);
235 int idx = pidx(provider->query_type);
236
237 assert((0 <= idx) && (idx < MAX_HW_SAMPLE_PROVIDERS));
238 assert(!ctx->acc_sample_providers[idx]);
239
240 ctx->acc_sample_providers[idx] = provider;
241 }