freedreno: threaded batch flush
[mesa.git] / src / gallium / drivers / freedreno / freedreno_batch.c
1 /*
2 * Copyright (C) 2016 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/list.h"
28 #include "util/set.h"
29 #include "util/hash_table.h"
30 #include "util/u_string.h"
31
32 #include "freedreno_batch.h"
33 #include "freedreno_context.h"
34 #include "freedreno_resource.h"
35 #include "freedreno_query_hw.h"
36
37 static void
38 batch_init(struct fd_batch *batch)
39 {
40 struct fd_context *ctx = batch->ctx;
41 unsigned size = 0;
42
43 if (ctx->screen->reorder)
44 util_queue_fence_init(&batch->flush_fence);
45
46 /* if kernel is too old to support unlimited # of cmd buffers, we
47 * have no option but to allocate large worst-case sizes so that
48 * we don't need to grow the ringbuffer. Performance is likely to
49 * suffer, but there is no good alternative.
50 */
51 if (fd_device_version(ctx->screen->dev) < FD_VERSION_UNLIMITED_CMDS) {
52 size = 0x100000;
53 }
54
55 batch->draw = fd_ringbuffer_new(ctx->screen->pipe, size);
56 batch->binning = fd_ringbuffer_new(ctx->screen->pipe, size);
57 batch->gmem = fd_ringbuffer_new(ctx->screen->pipe, size);
58
59 fd_ringbuffer_set_parent(batch->gmem, NULL);
60 fd_ringbuffer_set_parent(batch->draw, batch->gmem);
61 fd_ringbuffer_set_parent(batch->binning, batch->gmem);
62
63 batch->cleared = batch->partial_cleared = 0;
64 batch->restore = batch->resolve = 0;
65 batch->needs_flush = false;
66 batch->gmem_reason = 0;
67 batch->num_draws = 0;
68 batch->stage = FD_STAGE_NULL;
69
70 /* reset maximal bounds: */
71 batch->max_scissor.minx = batch->max_scissor.miny = ~0;
72 batch->max_scissor.maxx = batch->max_scissor.maxy = 0;
73
74 util_dynarray_init(&batch->draw_patches);
75
76 if (is_a3xx(ctx->screen))
77 util_dynarray_init(&batch->rbrc_patches);
78
79 assert(batch->resources->entries == 0);
80
81 util_dynarray_init(&batch->samples);
82 }
83
84 struct fd_batch *
85 fd_batch_create(struct fd_context *ctx)
86 {
87 struct fd_batch *batch = CALLOC_STRUCT(fd_batch);
88
89 if (!batch)
90 return NULL;
91
92 DBG("%p", batch);
93
94 pipe_reference_init(&batch->reference, 1);
95 batch->ctx = ctx;
96
97 batch->resources = _mesa_set_create(NULL, _mesa_hash_pointer,
98 _mesa_key_pointer_equal);
99
100 batch_init(batch);
101
102 return batch;
103 }
104
105 static void
106 batch_fini(struct fd_batch *batch)
107 {
108 pipe_resource_reference(&batch->query_buf, NULL);
109
110 fd_ringbuffer_del(batch->draw);
111 fd_ringbuffer_del(batch->binning);
112 fd_ringbuffer_del(batch->gmem);
113
114 util_dynarray_fini(&batch->draw_patches);
115
116 if (is_a3xx(batch->ctx->screen))
117 util_dynarray_fini(&batch->rbrc_patches);
118
119 while (batch->samples.size > 0) {
120 struct fd_hw_sample *samp =
121 util_dynarray_pop(&batch->samples, struct fd_hw_sample *);
122 fd_hw_sample_reference(batch->ctx, &samp, NULL);
123 }
124 util_dynarray_fini(&batch->samples);
125
126 if (batch->ctx->screen->reorder)
127 util_queue_fence_destroy(&batch->flush_fence);
128 }
129
130 static void
131 batch_flush_reset_dependencies(struct fd_batch *batch, bool flush)
132 {
133 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
134 struct fd_batch *dep;
135
136 foreach_batch(dep, cache, batch->dependents_mask) {
137 if (flush)
138 fd_batch_flush(dep, false);
139 fd_batch_reference(&dep, NULL);
140 }
141
142 batch->dependents_mask = 0;
143 }
144
145 static void
146 batch_reset_resources(struct fd_batch *batch)
147 {
148 struct set_entry *entry;
149
150 set_foreach(batch->resources, entry) {
151 struct fd_resource *rsc = (struct fd_resource *)entry->key;
152 _mesa_set_remove(batch->resources, entry);
153 debug_assert(rsc->batch_mask & (1 << batch->idx));
154 rsc->batch_mask &= ~(1 << batch->idx);
155 if (rsc->write_batch == batch)
156 fd_batch_reference(&rsc->write_batch, NULL);
157 }
158 }
159
160 static void
161 batch_reset(struct fd_batch *batch)
162 {
163 DBG("%p", batch);
164
165 fd_batch_sync(batch);
166
167 batch_flush_reset_dependencies(batch, false);
168 batch_reset_resources(batch);
169
170 batch_fini(batch);
171 batch_init(batch);
172 }
173
174 void
175 fd_batch_reset(struct fd_batch *batch)
176 {
177 if (batch->needs_flush)
178 batch_reset(batch);
179 }
180
181 void
182 __fd_batch_destroy(struct fd_batch *batch)
183 {
184 fd_bc_invalidate_batch(batch, true);
185
186 DBG("%p", batch);
187
188 util_copy_framebuffer_state(&batch->framebuffer, NULL);
189
190 batch_fini(batch);
191
192 batch_reset_resources(batch);
193 debug_assert(batch->resources->entries == 0);
194 _mesa_set_destroy(batch->resources, NULL);
195
196 batch_flush_reset_dependencies(batch, false);
197 debug_assert(batch->dependents_mask == 0);
198
199 free(batch);
200 }
201
202 void
203 __fd_batch_describe(char* buf, const struct fd_batch *batch)
204 {
205 util_sprintf(buf, "fd_batch<%u>", batch->seqno);
206 }
207
208 void
209 fd_batch_sync(struct fd_batch *batch)
210 {
211 if (!batch->ctx->screen->reorder)
212 return;
213 util_queue_job_wait(&batch->flush_fence);
214 }
215
216 static void
217 batch_flush_func(void *job, int id)
218 {
219 struct fd_batch *batch = job;
220
221 fd_gmem_render_tiles(batch);
222 batch_reset_resources(batch);
223 batch->ctx->last_fence = fd_ringbuffer_timestamp(batch->gmem);
224 }
225
226 static void
227 batch_cleanup_func(void *job, int id)
228 {
229 struct fd_batch *batch = job;
230 fd_batch_reference(&batch, NULL);
231 }
232
233 static void
234 batch_flush(struct fd_batch *batch)
235 {
236 DBG("%p: needs_flush=%d", batch, batch->needs_flush);
237
238 if (!batch->needs_flush)
239 return;
240
241 batch->needs_flush = false;
242
243 /* close out the draw cmds by making sure any active queries are
244 * paused:
245 */
246 fd_hw_query_set_stage(batch, batch->draw, FD_STAGE_NULL);
247
248 batch->ctx->dirty = ~0;
249 batch_flush_reset_dependencies(batch, true);
250
251 if (batch->ctx->screen->reorder) {
252 struct fd_batch *tmp = NULL;
253 fd_batch_reference(&tmp, batch);
254 util_queue_add_job(&batch->ctx->flush_queue,
255 batch, &batch->flush_fence,
256 batch_flush_func, batch_cleanup_func);
257 } else {
258 fd_gmem_render_tiles(batch);
259 batch_reset_resources(batch);
260 batch->ctx->last_fence = fd_ringbuffer_timestamp(batch->gmem);
261 }
262
263 debug_assert(batch->reference.count > 0);
264
265 if (batch == batch->ctx->batch) {
266 batch_reset(batch);
267 } else {
268 fd_bc_invalidate_batch(batch, false);
269 }
270 }
271
272 /* NOTE: could drop the last ref to batch */
273 void
274 fd_batch_flush(struct fd_batch *batch, bool sync)
275 {
276 /* NOTE: we need to hold an extra ref across the body of flush,
277 * since the last ref to this batch could be dropped when cleaning
278 * up used_resources
279 */
280 struct fd_batch *tmp = NULL;
281 fd_batch_reference(&tmp, batch);
282 batch_flush(tmp);
283 if (sync)
284 fd_batch_sync(tmp);
285 fd_batch_reference(&tmp, NULL);
286 }
287
288 /* does 'batch' depend directly or indirectly on 'other' ? */
289 static bool
290 batch_depends_on(struct fd_batch *batch, struct fd_batch *other)
291 {
292 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
293 struct fd_batch *dep;
294
295 if (batch->dependents_mask & (1 << other->idx))
296 return true;
297
298 foreach_batch(dep, cache, batch->dependents_mask)
299 if (batch_depends_on(batch, dep))
300 return true;
301
302 return false;
303 }
304
305 static void
306 batch_add_dep(struct fd_batch *batch, struct fd_batch *dep)
307 {
308 if (batch->dependents_mask & (1 << dep->idx))
309 return;
310
311 /* if the new depedency already depends on us, we need to flush
312 * to avoid a loop in the dependency graph.
313 */
314 if (batch_depends_on(dep, batch)) {
315 DBG("%p: flush forced on %p!", batch, dep);
316 fd_batch_flush(dep, false);
317 } else {
318 struct fd_batch *other = NULL;
319 fd_batch_reference(&other, dep);
320 batch->dependents_mask |= (1 << dep->idx);
321 DBG("%p: added dependency on %p", batch, dep);
322 }
323 }
324
325 void
326 fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool write)
327 {
328 if (rsc->stencil)
329 fd_batch_resource_used(batch, rsc->stencil, write);
330
331 DBG("%p: %s %p", batch, write ? "write" : "read", rsc);
332
333 /* note, invalidate write batch, to avoid further writes to rsc
334 * resulting in a write-after-read hazard.
335 */
336
337 if (write) {
338 /* if we are pending read or write by any other batch: */
339 if (rsc->batch_mask != (1 << batch->idx)) {
340 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
341 struct fd_batch *dep;
342 foreach_batch(dep, cache, rsc->batch_mask) {
343 struct fd_batch *b = NULL;
344 /* note that batch_add_dep could flush and unref dep, so
345 * we need to hold a reference to keep it live for the
346 * fd_bc_invalidate_batch()
347 */
348 fd_batch_reference(&b, dep);
349 batch_add_dep(batch, b);
350 fd_bc_invalidate_batch(b, false);
351 fd_batch_reference_locked(&b, NULL);
352 }
353 }
354 fd_batch_reference(&rsc->write_batch, batch);
355 } else {
356 if (rsc->write_batch) {
357 batch_add_dep(batch, rsc->write_batch);
358 fd_bc_invalidate_batch(rsc->write_batch, false);
359 }
360 }
361
362 if (rsc->batch_mask & (1 << batch->idx))
363 return;
364
365 debug_assert(!_mesa_set_search(batch->resources, rsc));
366
367 _mesa_set_add(batch->resources, rsc);
368 rsc->batch_mask |= (1 << batch->idx);
369 }
370
371 void
372 fd_batch_check_size(struct fd_batch *batch)
373 {
374 if (fd_device_version(batch->ctx->screen->dev) >= FD_VERSION_UNLIMITED_CMDS)
375 return;
376
377 struct fd_ringbuffer *ring = batch->draw;
378 if (((ring->cur - ring->start) > (ring->size/4 - 0x1000)) ||
379 (fd_mesa_debug & FD_DBG_FLUSH))
380 fd_batch_flush(batch, true);
381 }