2 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/list.h"
29 #include "util/hash_table.h"
30 #include "util/u_string.h"
32 #include "freedreno_batch.h"
33 #include "freedreno_context.h"
34 #include "freedreno_fence.h"
35 #include "freedreno_resource.h"
36 #include "freedreno_query_hw.h"
39 batch_init(struct fd_batch
*batch
)
41 struct fd_context
*ctx
= batch
->ctx
;
44 if (ctx
->screen
->reorder
)
45 util_queue_fence_init(&batch
->flush_fence
);
47 /* if kernel is too old to support unlimited # of cmd buffers, we
48 * have no option but to allocate large worst-case sizes so that
49 * we don't need to grow the ringbuffer. Performance is likely to
50 * suffer, but there is no good alternative.
52 if ((fd_device_version(ctx
->screen
->dev
) < FD_VERSION_UNLIMITED_CMDS
) ||
53 (fd_mesa_debug
& FD_DBG_NOGROW
)){
57 batch
->draw
= fd_ringbuffer_new(ctx
->pipe
, size
);
58 if (!batch
->nondraw
) {
59 batch
->binning
= fd_ringbuffer_new(ctx
->pipe
, size
);
60 batch
->gmem
= fd_ringbuffer_new(ctx
->pipe
, size
);
62 fd_ringbuffer_set_parent(batch
->gmem
, NULL
);
63 fd_ringbuffer_set_parent(batch
->draw
, batch
->gmem
);
64 fd_ringbuffer_set_parent(batch
->binning
, batch
->gmem
);
66 fd_ringbuffer_set_parent(batch
->draw
, NULL
);
69 batch
->in_fence_fd
= -1;
70 batch
->fence
= fd_fence_create(batch
);
73 batch
->restore
= batch
->resolve
= 0;
74 batch
->needs_flush
= false;
75 batch
->flushed
= false;
76 batch
->gmem_reason
= 0;
78 batch
->stage
= FD_STAGE_NULL
;
82 util_dynarray_init(&batch
->draw_patches
, NULL
);
84 if (is_a3xx(ctx
->screen
))
85 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
87 util_dynarray_init(&batch
->gmem_patches
, NULL
);
89 assert(batch
->resources
->entries
== 0);
91 util_dynarray_init(&batch
->samples
, NULL
);
95 fd_batch_create(struct fd_context
*ctx
, bool nondraw
)
97 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
104 pipe_reference_init(&batch
->reference
, 1);
106 batch
->nondraw
= nondraw
;
108 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
109 _mesa_key_pointer_equal
);
117 batch_fini(struct fd_batch
*batch
)
121 pipe_resource_reference(&batch
->query_buf
, NULL
);
123 if (batch
->in_fence_fd
!= -1)
124 close(batch
->in_fence_fd
);
126 /* in case batch wasn't flushed but fence was created: */
127 fd_fence_populate(batch
->fence
, 0, -1);
129 fd_fence_ref(NULL
, &batch
->fence
, NULL
);
131 fd_ringbuffer_del(batch
->draw
);
132 if (!batch
->nondraw
) {
133 fd_ringbuffer_del(batch
->binning
);
134 fd_ringbuffer_del(batch
->gmem
);
136 debug_assert(!batch
->binning
);
137 debug_assert(!batch
->gmem
);
139 if (batch
->lrz_clear
) {
140 fd_ringbuffer_del(batch
->lrz_clear
);
141 batch
->lrz_clear
= NULL
;
144 util_dynarray_fini(&batch
->draw_patches
);
146 if (is_a3xx(batch
->ctx
->screen
))
147 util_dynarray_fini(&batch
->rbrc_patches
);
149 util_dynarray_fini(&batch
->gmem_patches
);
151 while (batch
->samples
.size
> 0) {
152 struct fd_hw_sample
*samp
=
153 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
154 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
156 util_dynarray_fini(&batch
->samples
);
158 if (batch
->ctx
->screen
->reorder
)
159 util_queue_fence_destroy(&batch
->flush_fence
);
163 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
165 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
166 struct fd_batch
*dep
;
168 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
170 fd_batch_flush(dep
, false, false);
171 fd_batch_reference(&dep
, NULL
);
174 batch
->dependents_mask
= 0;
178 batch_reset_resources_locked(struct fd_batch
*batch
)
180 struct set_entry
*entry
;
182 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
184 set_foreach(batch
->resources
, entry
) {
185 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
186 _mesa_set_remove(batch
->resources
, entry
);
187 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
188 rsc
->batch_mask
&= ~(1 << batch
->idx
);
189 if (rsc
->write_batch
== batch
)
190 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
195 batch_reset_resources(struct fd_batch
*batch
)
197 mtx_lock(&batch
->ctx
->screen
->lock
);
198 batch_reset_resources_locked(batch
);
199 mtx_unlock(&batch
->ctx
->screen
->lock
);
203 batch_reset(struct fd_batch
*batch
)
207 fd_batch_sync(batch
);
209 batch_flush_reset_dependencies(batch
, false);
210 batch_reset_resources(batch
);
217 fd_batch_reset(struct fd_batch
*batch
)
219 if (batch
->needs_flush
)
224 __fd_batch_destroy(struct fd_batch
*batch
)
226 struct fd_context
*ctx
= batch
->ctx
;
230 fd_context_assert_locked(batch
->ctx
);
232 fd_bc_invalidate_batch(batch
, true);
234 batch_reset_resources_locked(batch
);
235 debug_assert(batch
->resources
->entries
== 0);
236 _mesa_set_destroy(batch
->resources
, NULL
);
238 fd_context_unlock(ctx
);
239 batch_flush_reset_dependencies(batch
, false);
240 debug_assert(batch
->dependents_mask
== 0);
242 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
245 fd_context_lock(ctx
);
249 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
251 util_sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
255 fd_batch_sync(struct fd_batch
*batch
)
257 if (!batch
->ctx
->screen
->reorder
)
259 util_queue_fence_wait(&batch
->flush_fence
);
263 batch_flush_func(void *job
, int id
)
265 struct fd_batch
*batch
= job
;
269 fd_gmem_render_tiles(batch
);
270 batch_reset_resources(batch
);
274 batch_cleanup_func(void *job
, int id
)
276 struct fd_batch
*batch
= job
;
277 fd_batch_reference(&batch
, NULL
);
281 batch_flush(struct fd_batch
*batch
, bool force
)
283 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
288 batch
->needs_flush
= false;
290 /* close out the draw cmds by making sure any active queries are
293 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
295 batch_flush_reset_dependencies(batch
, true);
297 batch
->flushed
= true;
299 if (batch
->ctx
->screen
->reorder
) {
300 struct fd_batch
*tmp
= NULL
;
301 fd_batch_reference(&tmp
, batch
);
303 if (!util_queue_is_initialized(&batch
->ctx
->flush_queue
))
304 util_queue_init(&batch
->ctx
->flush_queue
, "flush_queue", 16, 1, 0);
306 util_queue_add_job(&batch
->ctx
->flush_queue
,
307 batch
, &batch
->flush_fence
,
308 batch_flush_func
, batch_cleanup_func
);
310 fd_gmem_render_tiles(batch
);
311 batch_reset_resources(batch
);
314 debug_assert(batch
->reference
.count
> 0);
316 mtx_lock(&batch
->ctx
->screen
->lock
);
317 fd_bc_invalidate_batch(batch
, false);
318 mtx_unlock(&batch
->ctx
->screen
->lock
);
321 /* NOTE: could drop the last ref to batch
323 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
324 * to kernel before this returns, as opposed to just being queued to be
326 * @force: force a flush even if no rendering, mostly useful if you need
330 fd_batch_flush(struct fd_batch
*batch
, bool sync
, bool force
)
332 struct fd_batch
*tmp
= NULL
;
333 bool newbatch
= false;
335 /* NOTE: we need to hold an extra ref across the body of flush,
336 * since the last ref to this batch could be dropped when cleaning
339 fd_batch_reference(&tmp
, batch
);
341 if (batch
== batch
->ctx
->batch
) {
342 batch
->ctx
->batch
= NULL
;
346 batch_flush(tmp
, force
);
349 struct fd_context
*ctx
= batch
->ctx
;
350 struct fd_batch
*new_batch
;
352 if (ctx
->screen
->reorder
) {
353 /* defer allocating new batch until one is needed for rendering
354 * to avoid unused batches for apps that create many contexts
358 new_batch
= fd_batch_create(ctx
, false);
359 util_copy_framebuffer_state(&new_batch
->framebuffer
, &batch
->framebuffer
);
362 fd_batch_reference(&batch
, NULL
);
363 ctx
->batch
= new_batch
;
369 fd_batch_reference(&tmp
, NULL
);
372 /* does 'batch' depend directly or indirectly on 'other' ? */
374 batch_depends_on(struct fd_batch
*batch
, struct fd_batch
*other
)
376 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
377 struct fd_batch
*dep
;
379 if (batch
->dependents_mask
& (1 << other
->idx
))
382 foreach_batch(dep
, cache
, batch
->dependents_mask
)
383 if (batch_depends_on(batch
, dep
))
390 fd_batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
392 if (batch
->dependents_mask
& (1 << dep
->idx
))
395 /* a loop should not be possible */
396 debug_assert(!batch_depends_on(dep
, batch
));
398 struct fd_batch
*other
= NULL
;
399 fd_batch_reference_locked(&other
, dep
);
400 batch
->dependents_mask
|= (1 << dep
->idx
);
401 DBG("%p: added dependency on %p", batch
, dep
);
405 flush_write_batch(struct fd_resource
*rsc
)
407 struct fd_batch
*b
= NULL
;
408 fd_batch_reference(&b
, rsc
->write_batch
);
410 mtx_unlock(&b
->ctx
->screen
->lock
);
411 fd_batch_flush(b
, true, false);
412 mtx_lock(&b
->ctx
->screen
->lock
);
414 fd_bc_invalidate_batch(b
, false);
415 fd_batch_reference_locked(&b
, NULL
);
419 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
421 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
424 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
426 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
431 /* note, invalidate write batch, to avoid further writes to rsc
432 * resulting in a write-after-read hazard.
436 /* if we are pending read or write by any other batch: */
437 if (rsc
->batch_mask
& ~(1 << batch
->idx
)) {
438 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
439 struct fd_batch
*dep
;
441 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
442 flush_write_batch(rsc
);
444 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
445 struct fd_batch
*b
= NULL
;
448 /* note that batch_add_dep could flush and unref dep, so
449 * we need to hold a reference to keep it live for the
450 * fd_bc_invalidate_batch()
452 fd_batch_reference(&b
, dep
);
453 fd_batch_add_dep(batch
, b
);
454 fd_bc_invalidate_batch(b
, false);
455 fd_batch_reference_locked(&b
, NULL
);
458 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
460 /* If reading a resource pending a write, go ahead and flush the
461 * writer. This avoids situations where we end up having to
462 * flush the current batch in _resource_used()
464 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
465 flush_write_batch(rsc
);
468 if (rsc
->batch_mask
& (1 << batch
->idx
))
471 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
473 _mesa_set_add(batch
->resources
, rsc
);
474 rsc
->batch_mask
|= (1 << batch
->idx
);
478 fd_batch_check_size(struct fd_batch
*batch
)
480 debug_assert(!batch
->flushed
);
482 if (unlikely(fd_mesa_debug
& FD_DBG_FLUSH
)) {
483 fd_batch_flush(batch
, true, false);
487 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
490 struct fd_ringbuffer
*ring
= batch
->draw
;
491 if ((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000))
492 fd_batch_flush(batch
, true, false);
495 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
496 * been one since last draw:
499 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
501 if (batch
->needs_wfi
) {
502 if (batch
->ctx
->screen
->gpu_id
>= 500)
506 batch
->needs_wfi
= false;