freedreno: Fix detection of being in a blit for acc queries.
[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 static void
36 fd_acc_destroy_query(struct fd_context *ctx, struct fd_query *q)
37 {
38 struct fd_acc_query *aq = fd_acc_query(q);
39
40 DBG("%p", q);
41
42 pipe_resource_reference(&aq->prsc, NULL);
43 list_del(&aq->node);
44
45 free(aq->query_data);
46 free(aq);
47 }
48
49 static void
50 realloc_query_bo(struct fd_context *ctx, struct fd_acc_query *aq)
51 {
52 struct fd_resource *rsc;
53 void *map;
54
55 pipe_resource_reference(&aq->prsc, NULL);
56
57 aq->prsc = pipe_buffer_create(&ctx->screen->base,
58 PIPE_BIND_QUERY_BUFFER, 0, 0x1000);
59
60 /* don't assume the buffer is zero-initialized: */
61 rsc = fd_resource(aq->prsc);
62
63 fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_WRITE);
64
65 map = fd_bo_map(rsc->bo);
66 memset(map, 0, aq->size);
67 fd_bo_cpu_fini(rsc->bo);
68 }
69
70 static void
71 fd_acc_query_pause(struct fd_acc_query *aq)
72 {
73 const struct fd_acc_sample_provider *p = aq->provider;
74
75 if (!aq->batch)
76 return;
77
78 p->pause(aq, aq->batch);
79 aq->batch = NULL;
80 }
81
82 static void
83 fd_acc_query_resume(struct fd_acc_query *aq, struct fd_batch *batch)
84 {
85 const struct fd_acc_sample_provider *p = aq->provider;
86
87 aq->batch = batch;
88 p->resume(aq, aq->batch);
89
90 fd_batch_resource_used(batch, fd_resource(aq->prsc), true);
91 }
92
93 static void
94 fd_acc_begin_query(struct fd_context *ctx, struct fd_query *q)
95 {
96 struct fd_acc_query *aq = fd_acc_query(q);
97
98 DBG("%p", q);
99
100 /* ->begin_query() discards previous results, so realloc bo: */
101 realloc_query_bo(ctx, aq);
102
103 /* Signal that we need to update the active queries on the next draw */
104 ctx->update_active_queries = true;
105
106 /* add to active list: */
107 assert(list_is_empty(&aq->node));
108 list_addtail(&aq->node, &ctx->acc_active_queries);
109
110 /* TIMESTAMP/GPU_FINISHED and don't do normal bracketing at draw time, we
111 * need to just emit the capture at this moment.
112 */
113 if (skip_begin_query(q->type))
114 fd_acc_query_resume(aq, fd_context_batch(ctx));
115 }
116
117 static void
118 fd_acc_end_query(struct fd_context *ctx, struct fd_query *q)
119 {
120 struct fd_acc_query *aq = fd_acc_query(q);
121
122 DBG("%p", q);
123
124 fd_acc_query_pause(aq);
125
126 /* remove from active list: */
127 list_delinit(&aq->node);
128 }
129
130 static bool
131 fd_acc_get_query_result(struct fd_context *ctx, struct fd_query *q,
132 bool wait, union pipe_query_result *result)
133 {
134 struct fd_acc_query *aq = fd_acc_query(q);
135 const struct fd_acc_sample_provider *p = aq->provider;
136 struct fd_resource *rsc = fd_resource(aq->prsc);
137
138 DBG("%p: wait=%d", q, wait);
139
140 assert(list_is_empty(&aq->node));
141
142 /* if !wait, then check the last sample (the one most likely to
143 * not be ready yet) and bail if it is not ready:
144 */
145 if (!wait) {
146 int ret;
147
148 if (pending(rsc, false)) {
149 /* piglit spec@arb_occlusion_query@occlusion_query_conform
150 * test, and silly apps perhaps, get stuck in a loop trying
151 * to get query result forever with wait==false.. we don't
152 * wait to flush unnecessarily but we also don't want to
153 * spin forever:
154 */
155 if (aq->no_wait_cnt++ > 5)
156 fd_batch_flush(rsc->write_batch);
157 return false;
158 }
159
160 ret = fd_bo_cpu_prep(rsc->bo, ctx->pipe,
161 DRM_FREEDRENO_PREP_READ | DRM_FREEDRENO_PREP_NOSYNC);
162 if (ret)
163 return false;
164
165 fd_bo_cpu_fini(rsc->bo);
166 }
167
168 if (rsc->write_batch)
169 fd_batch_flush(rsc->write_batch);
170
171 /* get the result: */
172 fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_READ);
173
174 void *ptr = fd_bo_map(rsc->bo);
175 p->result(aq, ptr, result);
176 fd_bo_cpu_fini(rsc->bo);
177
178 return true;
179 }
180
181 static const struct fd_query_funcs acc_query_funcs = {
182 .destroy_query = fd_acc_destroy_query,
183 .begin_query = fd_acc_begin_query,
184 .end_query = fd_acc_end_query,
185 .get_query_result = fd_acc_get_query_result,
186 };
187
188 struct fd_query *
189 fd_acc_create_query2(struct fd_context *ctx, unsigned query_type,
190 unsigned index, const struct fd_acc_sample_provider *provider)
191 {
192 struct fd_acc_query *aq;
193 struct fd_query *q;
194
195 aq = CALLOC_STRUCT(fd_acc_query);
196 if (!aq)
197 return NULL;
198
199 DBG("%p: query_type=%u", aq, query_type);
200
201 aq->provider = provider;
202 aq->size = provider->size;
203
204 list_inithead(&aq->node);
205
206 q = &aq->base;
207 q->funcs = &acc_query_funcs;
208 q->type = query_type;
209 q->index = index;
210
211 return q;
212 }
213
214 struct fd_query *
215 fd_acc_create_query(struct fd_context *ctx, unsigned query_type,
216 unsigned index)
217 {
218 int idx = pidx(query_type);
219
220 if ((idx < 0) || !ctx->acc_sample_providers[idx])
221 return NULL;
222
223 return fd_acc_create_query2(ctx, query_type, index,
224 ctx->acc_sample_providers[idx]);
225 }
226
227 /* Called at clear/draw/blit time to enable/disable the appropriate queries in
228 * the batch (and transfer active querying between batches in the case of
229 * batch reordering).
230 */
231 void
232 fd_acc_query_set_stage(struct fd_batch *batch, enum fd_render_stage stage)
233 {
234 struct fd_context *ctx = batch->ctx;
235
236 if (stage != batch->stage || ctx->update_active_queries) {
237 struct fd_acc_query *aq;
238 LIST_FOR_EACH_ENTRY(aq, &ctx->acc_active_queries, node) {
239 bool batch_change = aq->batch != batch;
240 bool was_active = aq->batch != NULL;
241 bool now_active = stage != FD_STAGE_NULL &&
242 (ctx->active_queries || aq->provider->always);
243
244 if (was_active && (!now_active || batch_change))
245 fd_acc_query_pause(aq);
246 if (now_active && (!was_active || batch_change))
247 fd_acc_query_resume(aq, batch);
248 }
249 }
250
251 ctx->update_active_queries = false;
252 }
253
254 void
255 fd_acc_query_register_provider(struct pipe_context *pctx,
256 const struct fd_acc_sample_provider *provider)
257 {
258 struct fd_context *ctx = fd_context(pctx);
259 int idx = pidx(provider->query_type);
260
261 assert((0 <= idx) && (idx < MAX_HW_SAMPLE_PROVIDERS));
262 assert(!ctx->acc_sample_providers[idx]);
263
264 ctx->acc_sample_providers[idx] = provider;
265 }