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
);
72 batch
->cleared
= batch
->partial_cleared
= 0;
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 /* reset maximal bounds: */
83 batch
->max_scissor
.minx
= batch
->max_scissor
.miny
= ~0;
84 batch
->max_scissor
.maxx
= batch
->max_scissor
.maxy
= 0;
86 util_dynarray_init(&batch
->draw_patches
, NULL
);
88 if (is_a3xx(ctx
->screen
))
89 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
91 assert(batch
->resources
->entries
== 0);
93 util_dynarray_init(&batch
->samples
, NULL
);
97 fd_batch_create(struct fd_context
*ctx
, bool nondraw
)
99 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
106 pipe_reference_init(&batch
->reference
, 1);
108 batch
->nondraw
= nondraw
;
110 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
111 _mesa_key_pointer_equal
);
119 batch_fini(struct fd_batch
*batch
)
123 pipe_resource_reference(&batch
->query_buf
, NULL
);
125 if (batch
->in_fence_fd
!= -1)
126 close(batch
->in_fence_fd
);
128 /* in case batch wasn't flushed but fence was created: */
129 fd_fence_populate(batch
->fence
, 0, -1);
131 fd_fence_ref(NULL
, &batch
->fence
, NULL
);
133 fd_ringbuffer_del(batch
->draw
);
134 if (!batch
->nondraw
) {
135 fd_ringbuffer_del(batch
->binning
);
136 fd_ringbuffer_del(batch
->gmem
);
138 debug_assert(!batch
->binning
);
139 debug_assert(!batch
->gmem
);
141 if (batch
->lrz_clear
) {
142 fd_ringbuffer_del(batch
->lrz_clear
);
143 batch
->lrz_clear
= NULL
;
146 util_dynarray_fini(&batch
->draw_patches
);
148 if (is_a3xx(batch
->ctx
->screen
))
149 util_dynarray_fini(&batch
->rbrc_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
)
228 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
230 mtx_lock(&batch
->ctx
->screen
->lock
);
231 fd_bc_invalidate_batch(batch
, true);
232 mtx_unlock(&batch
->ctx
->screen
->lock
);
236 batch_reset_resources(batch
);
237 debug_assert(batch
->resources
->entries
== 0);
238 _mesa_set_destroy(batch
->resources
, NULL
);
240 batch_flush_reset_dependencies(batch
, false);
241 debug_assert(batch
->dependents_mask
== 0);
247 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
249 util_sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
253 fd_batch_sync(struct fd_batch
*batch
)
255 if (!batch
->ctx
->screen
->reorder
)
257 util_queue_fence_wait(&batch
->flush_fence
);
261 batch_flush_func(void *job
, int id
)
263 struct fd_batch
*batch
= job
;
267 fd_gmem_render_tiles(batch
);
268 batch_reset_resources(batch
);
272 batch_cleanup_func(void *job
, int id
)
274 struct fd_batch
*batch
= job
;
275 fd_batch_reference(&batch
, NULL
);
279 batch_flush(struct fd_batch
*batch
, bool force
)
281 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
286 batch
->needs_flush
= false;
288 /* close out the draw cmds by making sure any active queries are
291 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
293 fd_context_all_dirty(batch
->ctx
);
294 batch_flush_reset_dependencies(batch
, true);
296 batch
->flushed
= true;
298 if (batch
->ctx
->screen
->reorder
) {
299 struct fd_batch
*tmp
= NULL
;
300 fd_batch_reference(&tmp
, batch
);
302 if (!util_queue_is_initialized(&batch
->ctx
->flush_queue
))
303 util_queue_init(&batch
->ctx
->flush_queue
, "flush_queue", 16, 1, 0);
305 util_queue_add_job(&batch
->ctx
->flush_queue
,
306 batch
, &batch
->flush_fence
,
307 batch_flush_func
, batch_cleanup_func
);
309 fd_gmem_render_tiles(batch
);
310 batch_reset_resources(batch
);
313 debug_assert(batch
->reference
.count
> 0);
315 mtx_lock(&batch
->ctx
->screen
->lock
);
316 fd_bc_invalidate_batch(batch
, false);
317 mtx_unlock(&batch
->ctx
->screen
->lock
);
320 /* NOTE: could drop the last ref to batch
322 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
323 * to kernel before this returns, as opposed to just being queued to be
325 * @force: force a flush even if no rendering, mostly useful if you need
329 fd_batch_flush(struct fd_batch
*batch
, bool sync
, bool force
)
331 struct fd_batch
*tmp
= NULL
;
332 bool newbatch
= false;
334 /* NOTE: we need to hold an extra ref across the body of flush,
335 * since the last ref to this batch could be dropped when cleaning
338 fd_batch_reference(&tmp
, batch
);
340 if (batch
== batch
->ctx
->batch
) {
341 batch
->ctx
->batch
= NULL
;
345 batch_flush(tmp
, force
);
348 struct fd_context
*ctx
= batch
->ctx
;
349 struct fd_batch
*new_batch
=
350 fd_batch_from_fb(&ctx
->screen
->batch_cache
, ctx
, &batch
->framebuffer
);
352 util_copy_framebuffer_state(&new_batch
->framebuffer
, &batch
->framebuffer
);
354 fd_batch_reference(&batch
, NULL
);
355 ctx
->batch
= new_batch
;
361 fd_batch_reference(&tmp
, NULL
);
364 /* does 'batch' depend directly or indirectly on 'other' ? */
366 batch_depends_on(struct fd_batch
*batch
, struct fd_batch
*other
)
368 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
369 struct fd_batch
*dep
;
371 if (batch
->dependents_mask
& (1 << other
->idx
))
374 foreach_batch(dep
, cache
, batch
->dependents_mask
)
375 if (batch_depends_on(batch
, dep
))
382 fd_batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
384 if (batch
->dependents_mask
& (1 << dep
->idx
))
387 /* a loop should not be possible */
388 debug_assert(!batch_depends_on(dep
, batch
));
390 struct fd_batch
*other
= NULL
;
391 fd_batch_reference_locked(&other
, dep
);
392 batch
->dependents_mask
|= (1 << dep
->idx
);
393 DBG("%p: added dependency on %p", batch
, dep
);
397 flush_write_batch(struct fd_resource
*rsc
)
399 struct fd_batch
*b
= NULL
;
400 fd_batch_reference(&b
, rsc
->write_batch
);
402 mtx_unlock(&b
->ctx
->screen
->lock
);
403 fd_batch_flush(b
, true, false);
404 mtx_lock(&b
->ctx
->screen
->lock
);
406 fd_bc_invalidate_batch(b
, false);
407 fd_batch_reference_locked(&b
, NULL
);
411 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
413 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
416 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
418 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
423 /* note, invalidate write batch, to avoid further writes to rsc
424 * resulting in a write-after-read hazard.
428 /* if we are pending read or write by any other batch: */
429 if (rsc
->batch_mask
& ~(1 << batch
->idx
)) {
430 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
431 struct fd_batch
*dep
;
433 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
434 flush_write_batch(rsc
);
436 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
437 struct fd_batch
*b
= NULL
;
440 /* note that batch_add_dep could flush and unref dep, so
441 * we need to hold a reference to keep it live for the
442 * fd_bc_invalidate_batch()
444 fd_batch_reference(&b
, dep
);
445 fd_batch_add_dep(batch
, b
);
446 fd_bc_invalidate_batch(b
, false);
447 fd_batch_reference_locked(&b
, NULL
);
450 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
452 /* If reading a resource pending a write, go ahead and flush the
453 * writer. This avoids situations where we end up having to
454 * flush the current batch in _resource_used()
456 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
457 flush_write_batch(rsc
);
460 if (rsc
->batch_mask
& (1 << batch
->idx
))
463 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
465 _mesa_set_add(batch
->resources
, rsc
);
466 rsc
->batch_mask
|= (1 << batch
->idx
);
470 fd_batch_check_size(struct fd_batch
*batch
)
472 debug_assert(!batch
->flushed
);
474 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
477 struct fd_ringbuffer
*ring
= batch
->draw
;
478 if (((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000)) ||
479 (fd_mesa_debug
& FD_DBG_FLUSH
))
480 fd_batch_flush(batch
, true, false);
483 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
484 * been one since last draw:
487 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
489 if (batch
->needs_wfi
) {
490 if (batch
->ctx
->screen
->gpu_id
>= 500)
494 batch
->needs_wfi
= false;