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
->gmem
= fd_ringbuffer_new(ctx
->pipe
, size
);
61 fd_ringbuffer_set_parent(batch
->gmem
, NULL
);
62 fd_ringbuffer_set_parent(batch
->draw
, batch
->gmem
);
64 if (ctx
->screen
->gpu_id
< 600) {
65 batch
->binning
= fd_ringbuffer_new(ctx
->pipe
, size
);
66 fd_ringbuffer_set_parent(batch
->binning
, batch
->gmem
);
69 fd_ringbuffer_set_parent(batch
->draw
, NULL
);
72 batch
->in_fence_fd
= -1;
73 batch
->fence
= fd_fence_create(batch
);
76 batch
->invalidated
= 0;
77 batch
->restore
= batch
->resolve
= 0;
78 batch
->needs_flush
= false;
79 batch
->flushed
= false;
80 batch
->gmem_reason
= 0;
82 batch
->stage
= FD_STAGE_NULL
;
86 util_dynarray_init(&batch
->draw_patches
, NULL
);
88 if (is_a3xx(ctx
->screen
))
89 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
91 util_dynarray_init(&batch
->gmem_patches
, NULL
);
93 assert(batch
->resources
->entries
== 0);
95 util_dynarray_init(&batch
->samples
, NULL
);
99 fd_batch_create(struct fd_context
*ctx
, bool nondraw
)
101 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
108 pipe_reference_init(&batch
->reference
, 1);
110 batch
->nondraw
= nondraw
;
112 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
113 _mesa_key_pointer_equal
);
121 batch_fini(struct fd_batch
*batch
)
125 pipe_resource_reference(&batch
->query_buf
, NULL
);
127 if (batch
->in_fence_fd
!= -1)
128 close(batch
->in_fence_fd
);
130 /* in case batch wasn't flushed but fence was created: */
131 fd_fence_populate(batch
->fence
, 0, -1);
133 fd_fence_ref(NULL
, &batch
->fence
, NULL
);
135 fd_ringbuffer_del(batch
->draw
);
136 if (!batch
->nondraw
) {
138 fd_ringbuffer_del(batch
->binning
);
139 fd_ringbuffer_del(batch
->gmem
);
141 debug_assert(!batch
->binning
);
142 debug_assert(!batch
->gmem
);
144 if (batch
->lrz_clear
) {
145 fd_ringbuffer_del(batch
->lrz_clear
);
146 batch
->lrz_clear
= NULL
;
149 util_dynarray_fini(&batch
->draw_patches
);
151 if (is_a3xx(batch
->ctx
->screen
))
152 util_dynarray_fini(&batch
->rbrc_patches
);
154 util_dynarray_fini(&batch
->gmem_patches
);
156 while (batch
->samples
.size
> 0) {
157 struct fd_hw_sample
*samp
=
158 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
159 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
161 util_dynarray_fini(&batch
->samples
);
163 if (batch
->ctx
->screen
->reorder
)
164 util_queue_fence_destroy(&batch
->flush_fence
);
168 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
170 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
171 struct fd_batch
*dep
;
173 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
175 fd_batch_flush(dep
, false, false);
176 fd_batch_reference(&dep
, NULL
);
179 batch
->dependents_mask
= 0;
183 batch_reset_resources_locked(struct fd_batch
*batch
)
185 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
187 set_foreach(batch
->resources
, entry
) {
188 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
189 _mesa_set_remove(batch
->resources
, entry
);
190 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
191 rsc
->batch_mask
&= ~(1 << batch
->idx
);
192 if (rsc
->write_batch
== batch
)
193 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
198 batch_reset_resources(struct fd_batch
*batch
)
200 mtx_lock(&batch
->ctx
->screen
->lock
);
201 batch_reset_resources_locked(batch
);
202 mtx_unlock(&batch
->ctx
->screen
->lock
);
206 batch_reset(struct fd_batch
*batch
)
210 fd_batch_sync(batch
);
212 batch_flush_reset_dependencies(batch
, false);
213 batch_reset_resources(batch
);
220 fd_batch_reset(struct fd_batch
*batch
)
222 if (batch
->needs_flush
)
227 __fd_batch_destroy(struct fd_batch
*batch
)
229 struct fd_context
*ctx
= batch
->ctx
;
233 fd_context_assert_locked(batch
->ctx
);
235 fd_bc_invalidate_batch(batch
, true);
237 batch_reset_resources_locked(batch
);
238 debug_assert(batch
->resources
->entries
== 0);
239 _mesa_set_destroy(batch
->resources
, NULL
);
241 fd_context_unlock(ctx
);
242 batch_flush_reset_dependencies(batch
, false);
243 debug_assert(batch
->dependents_mask
== 0);
245 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
248 fd_context_lock(ctx
);
252 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
254 util_sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
258 fd_batch_sync(struct fd_batch
*batch
)
260 if (!batch
->ctx
->screen
->reorder
)
262 util_queue_fence_wait(&batch
->flush_fence
);
266 batch_flush_func(void *job
, int id
)
268 struct fd_batch
*batch
= job
;
272 fd_gmem_render_tiles(batch
);
273 batch_reset_resources(batch
);
277 batch_cleanup_func(void *job
, int id
)
279 struct fd_batch
*batch
= job
;
280 fd_batch_reference(&batch
, NULL
);
284 batch_flush(struct fd_batch
*batch
, bool force
)
286 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
291 batch
->needs_flush
= false;
293 /* close out the draw cmds by making sure any active queries are
296 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
298 batch_flush_reset_dependencies(batch
, true);
300 batch
->flushed
= true;
302 if (batch
->ctx
->screen
->reorder
) {
303 struct fd_batch
*tmp
= NULL
;
304 fd_batch_reference(&tmp
, batch
);
306 if (!util_queue_is_initialized(&batch
->ctx
->flush_queue
))
307 util_queue_init(&batch
->ctx
->flush_queue
, "flush_queue", 16, 1, 0);
309 util_queue_add_job(&batch
->ctx
->flush_queue
,
310 batch
, &batch
->flush_fence
,
311 batch_flush_func
, batch_cleanup_func
);
313 fd_gmem_render_tiles(batch
);
314 batch_reset_resources(batch
);
317 debug_assert(batch
->reference
.count
> 0);
319 mtx_lock(&batch
->ctx
->screen
->lock
);
320 fd_bc_invalidate_batch(batch
, false);
321 mtx_unlock(&batch
->ctx
->screen
->lock
);
324 /* NOTE: could drop the last ref to batch
326 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
327 * to kernel before this returns, as opposed to just being queued to be
329 * @force: force a flush even if no rendering, mostly useful if you need
333 fd_batch_flush(struct fd_batch
*batch
, bool sync
, bool force
)
335 struct fd_batch
*tmp
= NULL
;
336 bool newbatch
= false;
338 /* NOTE: we need to hold an extra ref across the body of flush,
339 * since the last ref to this batch could be dropped when cleaning
342 fd_batch_reference(&tmp
, batch
);
344 if (batch
== batch
->ctx
->batch
) {
345 batch
->ctx
->batch
= NULL
;
349 batch_flush(tmp
, force
);
352 struct fd_context
*ctx
= batch
->ctx
;
353 struct fd_batch
*new_batch
;
355 if (ctx
->screen
->reorder
) {
356 /* defer allocating new batch until one is needed for rendering
357 * to avoid unused batches for apps that create many contexts
361 new_batch
= fd_batch_create(ctx
, false);
362 util_copy_framebuffer_state(&new_batch
->framebuffer
, &batch
->framebuffer
);
365 fd_batch_reference(&batch
, NULL
);
366 ctx
->batch
= new_batch
;
372 fd_batch_reference(&tmp
, NULL
);
375 /* does 'batch' depend directly or indirectly on 'other' ? */
377 batch_depends_on(struct fd_batch
*batch
, struct fd_batch
*other
)
379 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
380 struct fd_batch
*dep
;
382 if (batch
->dependents_mask
& (1 << other
->idx
))
385 foreach_batch(dep
, cache
, batch
->dependents_mask
)
386 if (batch_depends_on(batch
, dep
))
393 fd_batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
395 if (batch
->dependents_mask
& (1 << dep
->idx
))
398 /* a loop should not be possible */
399 debug_assert(!batch_depends_on(dep
, batch
));
401 struct fd_batch
*other
= NULL
;
402 fd_batch_reference_locked(&other
, dep
);
403 batch
->dependents_mask
|= (1 << dep
->idx
);
404 DBG("%p: added dependency on %p", batch
, dep
);
408 flush_write_batch(struct fd_resource
*rsc
)
410 struct fd_batch
*b
= NULL
;
411 fd_batch_reference(&b
, rsc
->write_batch
);
413 mtx_unlock(&b
->ctx
->screen
->lock
);
414 fd_batch_flush(b
, true, false);
415 mtx_lock(&b
->ctx
->screen
->lock
);
417 fd_bc_invalidate_batch(b
, false);
418 fd_batch_reference_locked(&b
, NULL
);
422 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
424 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
427 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
429 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
434 /* note, invalidate write batch, to avoid further writes to rsc
435 * resulting in a write-after-read hazard.
439 /* if we are pending read or write by any other batch: */
440 if (rsc
->batch_mask
& ~(1 << batch
->idx
)) {
441 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
442 struct fd_batch
*dep
;
444 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
445 flush_write_batch(rsc
);
447 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
448 struct fd_batch
*b
= NULL
;
451 /* note that batch_add_dep could flush and unref dep, so
452 * we need to hold a reference to keep it live for the
453 * fd_bc_invalidate_batch()
455 fd_batch_reference(&b
, dep
);
456 fd_batch_add_dep(batch
, b
);
457 fd_bc_invalidate_batch(b
, false);
458 fd_batch_reference_locked(&b
, NULL
);
461 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
463 /* If reading a resource pending a write, go ahead and flush the
464 * writer. This avoids situations where we end up having to
465 * flush the current batch in _resource_used()
467 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
468 flush_write_batch(rsc
);
471 if (rsc
->batch_mask
& (1 << batch
->idx
))
474 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
476 _mesa_set_add(batch
->resources
, rsc
);
477 rsc
->batch_mask
|= (1 << batch
->idx
);
481 fd_batch_check_size(struct fd_batch
*batch
)
483 debug_assert(!batch
->flushed
);
485 if (unlikely(fd_mesa_debug
& FD_DBG_FLUSH
)) {
486 fd_batch_flush(batch
, true, false);
490 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
493 struct fd_ringbuffer
*ring
= batch
->draw
;
494 if ((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000))
495 fd_batch_flush(batch
, true, false);
498 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
499 * been one since last draw:
502 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
504 if (batch
->needs_wfi
) {
505 if (batch
->ctx
->screen
->gpu_id
>= 500)
509 batch
->needs_wfi
= false;