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"
38 static struct fd_ringbuffer
*
39 alloc_ring(struct fd_batch
*batch
, unsigned sz
, enum fd_ringbuffer_flags flags
)
41 struct fd_context
*ctx
= batch
->ctx
;
43 /* if kernel is too old to support unlimited # of cmd buffers, we
44 * have no option but to allocate large worst-case sizes so that
45 * we don't need to grow the ringbuffer. Performance is likely to
46 * suffer, but there is no good alternative.
48 * Otherwise if supported, allocate a growable ring with initial
51 if ((fd_device_version(ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
) &&
52 !(fd_mesa_debug
& FD_DBG_NOGROW
)){
53 flags
|= FD_RINGBUFFER_GROWABLE
;
57 return fd_submit_new_ringbuffer(batch
->submit
, sz
, flags
);
61 batch_init(struct fd_batch
*batch
)
63 struct fd_context
*ctx
= batch
->ctx
;
65 batch
->submit
= fd_submit_new(ctx
->pipe
);
67 batch
->draw
= alloc_ring(batch
, 0x100000, FD_RINGBUFFER_PRIMARY
);
69 batch
->gmem
= alloc_ring(batch
, 0x100000, FD_RINGBUFFER_PRIMARY
);
70 batch
->draw
= alloc_ring(batch
, 0x100000, 0);
72 /* a6xx+ re-uses draw rb for both draw and binning pass: */
73 if (ctx
->screen
->gpu_id
< 600) {
74 batch
->binning
= alloc_ring(batch
, 0x100000, 0);
78 batch
->in_fence_fd
= -1;
79 batch
->fence
= fd_fence_create(batch
);
82 batch
->fast_cleared
= 0;
83 batch
->invalidated
= 0;
84 batch
->restore
= batch
->resolve
= 0;
85 batch
->needs_flush
= false;
86 batch
->flushed
= false;
87 batch
->gmem_reason
= 0;
89 batch
->num_vertices
= 0;
90 batch
->num_bins_per_pipe
= 0;
91 batch
->prim_strm_bits
= 0;
92 batch
->draw_strm_bits
= 0;
93 batch
->stage
= FD_STAGE_NULL
;
97 util_dynarray_init(&batch
->draw_patches
, NULL
);
98 util_dynarray_init(&batch
->fb_read_patches
, NULL
);
100 if (is_a2xx(ctx
->screen
)) {
101 util_dynarray_init(&batch
->shader_patches
, NULL
);
102 util_dynarray_init(&batch
->gmem_patches
, NULL
);
105 if (is_a3xx(ctx
->screen
))
106 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
108 assert(batch
->resources
->entries
== 0);
110 util_dynarray_init(&batch
->samples
, NULL
);
112 list_inithead(&batch
->log_chunks
);
116 fd_batch_create(struct fd_context
*ctx
, bool nondraw
)
118 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
125 pipe_reference_init(&batch
->reference
, 1);
127 batch
->nondraw
= nondraw
;
129 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
130 _mesa_key_pointer_equal
);
134 fd_screen_assert_locked(ctx
->screen
);
136 _mesa_set_add(ctx
->screen
->live_batches
, batch
);
143 batch_fini(struct fd_batch
*batch
)
147 pipe_resource_reference(&batch
->query_buf
, NULL
);
149 if (batch
->in_fence_fd
!= -1)
150 close(batch
->in_fence_fd
);
152 /* in case batch wasn't flushed but fence was created: */
153 fd_fence_populate(batch
->fence
, 0, -1);
155 fd_fence_ref(&batch
->fence
, NULL
);
157 fd_ringbuffer_del(batch
->draw
);
158 if (!batch
->nondraw
) {
160 fd_ringbuffer_del(batch
->binning
);
161 fd_ringbuffer_del(batch
->gmem
);
163 debug_assert(!batch
->binning
);
164 debug_assert(!batch
->gmem
);
167 if (batch
->prologue
) {
168 fd_ringbuffer_del(batch
->prologue
);
169 batch
->prologue
= NULL
;
172 if (batch
->epilogue
) {
173 fd_ringbuffer_del(batch
->epilogue
);
174 batch
->epilogue
= NULL
;
177 if (batch
->tile_setup
) {
178 fd_ringbuffer_del(batch
->tile_setup
);
179 batch
->tile_setup
= NULL
;
182 if (batch
->tile_fini
) {
183 fd_ringbuffer_del(batch
->tile_fini
);
184 batch
->tile_fini
= NULL
;
187 if (batch
->tessellation
) {
188 fd_bo_del(batch
->tessfactor_bo
);
189 fd_bo_del(batch
->tessparam_bo
);
190 fd_ringbuffer_del(batch
->tess_addrs_constobj
);
193 fd_submit_del(batch
->submit
);
195 util_dynarray_fini(&batch
->draw_patches
);
196 util_dynarray_fini(&batch
->fb_read_patches
);
198 if (is_a2xx(batch
->ctx
->screen
)) {
199 util_dynarray_fini(&batch
->shader_patches
);
200 util_dynarray_fini(&batch
->gmem_patches
);
203 if (is_a3xx(batch
->ctx
->screen
))
204 util_dynarray_fini(&batch
->rbrc_patches
);
206 while (batch
->samples
.size
> 0) {
207 struct fd_hw_sample
*samp
=
208 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
209 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
211 util_dynarray_fini(&batch
->samples
);
213 assert(list_is_empty(&batch
->log_chunks
));
217 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
219 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
220 struct fd_batch
*dep
;
222 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
225 fd_batch_reference(&dep
, NULL
);
228 batch
->dependents_mask
= 0;
232 batch_reset_resources_locked(struct fd_batch
*batch
)
234 fd_screen_assert_locked(batch
->ctx
->screen
);
236 set_foreach(batch
->resources
, entry
) {
237 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
238 _mesa_set_remove(batch
->resources
, entry
);
239 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
240 rsc
->batch_mask
&= ~(1 << batch
->idx
);
241 if (rsc
->write_batch
== batch
)
242 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
247 batch_reset_resources(struct fd_batch
*batch
)
249 fd_screen_lock(batch
->ctx
->screen
);
250 batch_reset_resources_locked(batch
);
251 fd_screen_unlock(batch
->ctx
->screen
);
255 batch_reset(struct fd_batch
*batch
)
259 batch_flush_reset_dependencies(batch
, false);
260 batch_reset_resources(batch
);
267 fd_batch_reset(struct fd_batch
*batch
)
269 if (batch
->needs_flush
)
274 __fd_batch_destroy(struct fd_batch
*batch
)
276 struct fd_context
*ctx
= batch
->ctx
;
280 fd_context_assert_locked(batch
->ctx
);
283 _mesa_set_remove_key(ctx
->screen
->live_batches
, batch
);
286 fd_bc_invalidate_batch(batch
, true);
288 batch_reset_resources_locked(batch
);
289 debug_assert(batch
->resources
->entries
== 0);
290 _mesa_set_destroy(batch
->resources
, NULL
);
292 fd_context_unlock(ctx
);
293 batch_flush_reset_dependencies(batch
, false);
294 debug_assert(batch
->dependents_mask
== 0);
296 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
299 fd_context_lock(ctx
);
303 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
305 sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
309 batch_flush(struct fd_batch
*batch
)
311 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
316 batch
->needs_flush
= false;
318 /* close out the draw cmds by making sure any active queries are
321 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
323 batch_flush_reset_dependencies(batch
, true);
325 batch
->flushed
= true;
327 fd_fence_ref(&batch
->ctx
->last_fence
, batch
->fence
);
329 fd_gmem_render_tiles(batch
);
330 batch_reset_resources(batch
);
332 debug_assert(batch
->reference
.count
> 0);
334 fd_screen_lock(batch
->ctx
->screen
);
335 fd_bc_invalidate_batch(batch
, false);
336 fd_screen_unlock(batch
->ctx
->screen
);
339 /* Get per-batch prologue */
340 struct fd_ringbuffer
*
341 fd_batch_get_prologue(struct fd_batch
*batch
)
343 if (!batch
->prologue
)
344 batch
->prologue
= alloc_ring(batch
, 0x1000, 0);
345 return batch
->prologue
;
348 /* NOTE: could drop the last ref to batch
350 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
351 * to kernel before this returns, as opposed to just being queued to be
353 * @force: force a flush even if no rendering, mostly useful if you need
357 fd_batch_flush(struct fd_batch
*batch
)
359 struct fd_batch
*tmp
= NULL
;
361 /* NOTE: we need to hold an extra ref across the body of flush,
362 * since the last ref to this batch could be dropped when cleaning
365 fd_batch_reference(&tmp
, batch
);
369 if (batch
== batch
->ctx
->batch
) {
370 fd_batch_reference(&batch
->ctx
->batch
, NULL
);
373 fd_batch_reference(&tmp
, NULL
);
376 /* find a batches dependents mask, including recursive dependencies: */
378 recursive_dependents_mask(struct fd_batch
*batch
)
380 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
381 struct fd_batch
*dep
;
382 uint32_t dependents_mask
= batch
->dependents_mask
;
384 foreach_batch(dep
, cache
, batch
->dependents_mask
)
385 dependents_mask
|= recursive_dependents_mask(dep
);
387 return dependents_mask
;
391 fd_batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
393 fd_screen_assert_locked(batch
->ctx
->screen
);
395 if (batch
->dependents_mask
& (1 << dep
->idx
))
398 /* a loop should not be possible */
399 debug_assert(!((1 << batch
->idx
) & recursive_dependents_mask(dep
)));
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_locked(&b
, rsc
->write_batch
);
413 fd_screen_unlock(b
->ctx
->screen
);
415 fd_screen_lock(b
->ctx
->screen
);
417 fd_bc_invalidate_batch(b
, false);
418 fd_batch_reference_locked(&b
, NULL
);
422 fd_batch_add_resource(struct fd_batch
*batch
, struct fd_resource
*rsc
)
425 if (likely(fd_batch_references_resource(batch
, rsc
))) {
426 debug_assert(_mesa_set_search(batch
->resources
, rsc
));
430 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
432 _mesa_set_add(batch
->resources
, rsc
);
433 rsc
->batch_mask
|= (1 << batch
->idx
);
437 fd_batch_resource_write(struct fd_batch
*batch
, struct fd_resource
*rsc
)
439 fd_screen_assert_locked(batch
->ctx
->screen
);
442 fd_batch_resource_write(batch
, rsc
->stencil
);
444 DBG("%p: write %p", batch
, rsc
);
448 /* note, invalidate write batch, to avoid further writes to rsc
449 * resulting in a write-after-read hazard.
451 /* if we are pending read or write by any other batch: */
452 if (unlikely(rsc
->batch_mask
& ~(1 << batch
->idx
))) {
453 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
454 struct fd_batch
*dep
;
456 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
457 flush_write_batch(rsc
);
459 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
460 struct fd_batch
*b
= NULL
;
463 /* note that batch_add_dep could flush and unref dep, so
464 * we need to hold a reference to keep it live for the
465 * fd_bc_invalidate_batch()
467 fd_batch_reference(&b
, dep
);
468 fd_batch_add_dep(batch
, b
);
469 fd_bc_invalidate_batch(b
, false);
470 fd_batch_reference_locked(&b
, NULL
);
473 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
475 fd_batch_add_resource(batch
, rsc
);
479 fd_batch_resource_read_slowpath(struct fd_batch
*batch
, struct fd_resource
*rsc
)
481 fd_screen_assert_locked(batch
->ctx
->screen
);
484 fd_batch_resource_read(batch
, rsc
->stencil
);
486 DBG("%p: read %p", batch
, rsc
);
488 /* If reading a resource pending a write, go ahead and flush the
489 * writer. This avoids situations where we end up having to
490 * flush the current batch in _resource_used()
492 if (unlikely(rsc
->write_batch
&& rsc
->write_batch
!= batch
))
493 flush_write_batch(rsc
);
495 fd_batch_add_resource(batch
, rsc
);
499 fd_batch_check_size(struct fd_batch
*batch
)
501 debug_assert(!batch
->flushed
);
503 if (unlikely(fd_mesa_debug
& FD_DBG_FLUSH
)) {
504 fd_batch_flush(batch
);
508 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
511 struct fd_ringbuffer
*ring
= batch
->draw
;
512 if ((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000))
513 fd_batch_flush(batch
);
516 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
517 * been one since last draw:
520 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
522 if (batch
->needs_wfi
) {
523 if (batch
->ctx
->screen
->gpu_id
>= 500)
527 batch
->needs_wfi
= false;