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
->gmem_reason
= 0;
77 batch
->stage
= FD_STAGE_NULL
;
81 /* reset maximal bounds: */
82 batch
->max_scissor
.minx
= batch
->max_scissor
.miny
= ~0;
83 batch
->max_scissor
.maxx
= batch
->max_scissor
.maxy
= 0;
85 util_dynarray_init(&batch
->draw_patches
, NULL
);
87 if (is_a3xx(ctx
->screen
))
88 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
90 assert(batch
->resources
->entries
== 0);
92 util_dynarray_init(&batch
->samples
, NULL
);
96 fd_batch_create(struct fd_context
*ctx
, bool nondraw
)
98 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
105 pipe_reference_init(&batch
->reference
, 1);
107 batch
->nondraw
= nondraw
;
109 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
110 _mesa_key_pointer_equal
);
118 batch_fini(struct fd_batch
*batch
)
120 pipe_resource_reference(&batch
->query_buf
, NULL
);
122 if (batch
->in_fence_fd
!= -1)
123 close(batch
->in_fence_fd
);
125 /* in case batch wasn't flushed but fence was created: */
126 fd_fence_populate(batch
->fence
, 0, -1);
128 fd_fence_ref(NULL
, &batch
->fence
, NULL
);
130 fd_ringbuffer_del(batch
->draw
);
131 if (!batch
->nondraw
) {
132 fd_ringbuffer_del(batch
->binning
);
133 fd_ringbuffer_del(batch
->gmem
);
135 debug_assert(!batch
->binning
);
136 debug_assert(!batch
->gmem
);
138 if (batch
->lrz_clear
) {
139 fd_ringbuffer_del(batch
->lrz_clear
);
140 batch
->lrz_clear
= NULL
;
143 util_dynarray_fini(&batch
->draw_patches
);
145 if (is_a3xx(batch
->ctx
->screen
))
146 util_dynarray_fini(&batch
->rbrc_patches
);
148 while (batch
->samples
.size
> 0) {
149 struct fd_hw_sample
*samp
=
150 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
151 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
153 util_dynarray_fini(&batch
->samples
);
155 if (batch
->ctx
->screen
->reorder
)
156 util_queue_fence_destroy(&batch
->flush_fence
);
160 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
162 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
163 struct fd_batch
*dep
;
165 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
167 fd_batch_flush(dep
, false, false);
168 fd_batch_reference(&dep
, NULL
);
171 batch
->dependents_mask
= 0;
175 batch_reset_resources_locked(struct fd_batch
*batch
)
177 struct set_entry
*entry
;
179 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
181 set_foreach(batch
->resources
, entry
) {
182 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
183 _mesa_set_remove(batch
->resources
, entry
);
184 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
185 rsc
->batch_mask
&= ~(1 << batch
->idx
);
186 if (rsc
->write_batch
== batch
)
187 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
192 batch_reset_resources(struct fd_batch
*batch
)
194 mtx_lock(&batch
->ctx
->screen
->lock
);
195 batch_reset_resources_locked(batch
);
196 mtx_unlock(&batch
->ctx
->screen
->lock
);
200 batch_reset(struct fd_batch
*batch
)
204 fd_batch_sync(batch
);
206 batch_flush_reset_dependencies(batch
, false);
207 batch_reset_resources(batch
);
214 fd_batch_reset(struct fd_batch
*batch
)
216 if (batch
->needs_flush
)
221 __fd_batch_destroy(struct fd_batch
*batch
)
225 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
227 mtx_lock(&batch
->ctx
->screen
->lock
);
228 fd_bc_invalidate_batch(batch
, true);
229 mtx_unlock(&batch
->ctx
->screen
->lock
);
233 batch_reset_resources(batch
);
234 debug_assert(batch
->resources
->entries
== 0);
235 _mesa_set_destroy(batch
->resources
, NULL
);
237 batch_flush_reset_dependencies(batch
, false);
238 debug_assert(batch
->dependents_mask
== 0);
244 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
246 util_sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
250 fd_batch_sync(struct fd_batch
*batch
)
252 if (!batch
->ctx
->screen
->reorder
)
254 util_queue_fence_wait(&batch
->flush_fence
);
258 batch_flush_func(void *job
, int id
)
260 struct fd_batch
*batch
= job
;
262 fd_gmem_render_tiles(batch
);
263 batch_reset_resources(batch
);
267 batch_cleanup_func(void *job
, int id
)
269 struct fd_batch
*batch
= job
;
270 fd_batch_reference(&batch
, NULL
);
274 batch_flush(struct fd_batch
*batch
, bool force
)
276 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
278 if (!batch
->needs_flush
) {
280 fd_gmem_render_noop(batch
);
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 if (batch
->ctx
->screen
->reorder
) {
297 struct fd_batch
*tmp
= NULL
;
298 fd_batch_reference(&tmp
, batch
);
300 if (!util_queue_is_initialized(&batch
->ctx
->flush_queue
))
301 util_queue_init(&batch
->ctx
->flush_queue
, "flush_queue", 16, 1, 0);
303 util_queue_add_job(&batch
->ctx
->flush_queue
,
304 batch
, &batch
->flush_fence
,
305 batch_flush_func
, batch_cleanup_func
);
307 fd_gmem_render_tiles(batch
);
308 batch_reset_resources(batch
);
311 debug_assert(batch
->reference
.count
> 0);
314 if (batch
== batch
->ctx
->batch
) {
317 mtx_lock(&batch
->ctx
->screen
->lock
);
318 fd_bc_invalidate_batch(batch
, false);
319 mtx_unlock(&batch
->ctx
->screen
->lock
);
323 /* NOTE: could drop the last ref to batch
325 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
326 * to kernel before this returns, as opposed to just being queued to be
328 * @force: force a flush even if no rendering, mostly useful if you need
332 fd_batch_flush(struct fd_batch
*batch
, bool sync
, bool force
)
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 struct fd_batch
*tmp
= NULL
;
340 fd_batch_reference(&tmp
, batch
);
341 batch_flush(tmp
, force
);
344 fd_batch_reference(&tmp
, NULL
);
347 /* does 'batch' depend directly or indirectly on 'other' ? */
349 batch_depends_on(struct fd_batch
*batch
, struct fd_batch
*other
)
351 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
352 struct fd_batch
*dep
;
354 if (batch
->dependents_mask
& (1 << other
->idx
))
357 foreach_batch(dep
, cache
, batch
->dependents_mask
)
358 if (batch_depends_on(batch
, dep
))
365 fd_batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
367 if (batch
->dependents_mask
& (1 << dep
->idx
))
370 /* if the new depedency already depends on us, we need to flush
371 * to avoid a loop in the dependency graph.
373 if (batch_depends_on(dep
, batch
)) {
374 DBG("%p: flush forced on %p!", batch
, dep
);
375 mtx_unlock(&batch
->ctx
->screen
->lock
);
376 fd_batch_flush(dep
, false, false);
377 mtx_lock(&batch
->ctx
->screen
->lock
);
379 struct fd_batch
*other
= NULL
;
380 fd_batch_reference_locked(&other
, dep
);
381 batch
->dependents_mask
|= (1 << dep
->idx
);
382 DBG("%p: added dependency on %p", batch
, dep
);
387 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
389 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
392 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
394 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
399 /* note, invalidate write batch, to avoid further writes to rsc
400 * resulting in a write-after-read hazard.
404 /* if we are pending read or write by any other batch: */
405 if (rsc
->batch_mask
!= (1 << batch
->idx
)) {
406 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
407 struct fd_batch
*dep
;
408 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
409 struct fd_batch
*b
= NULL
;
412 /* note that batch_add_dep could flush and unref dep, so
413 * we need to hold a reference to keep it live for the
414 * fd_bc_invalidate_batch()
416 fd_batch_reference(&b
, dep
);
417 fd_batch_add_dep(batch
, b
);
418 fd_bc_invalidate_batch(b
, false);
419 fd_batch_reference_locked(&b
, NULL
);
422 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
424 if (rsc
->write_batch
) {
425 fd_batch_add_dep(batch
, rsc
->write_batch
);
426 fd_bc_invalidate_batch(rsc
->write_batch
, false);
430 if (rsc
->batch_mask
& (1 << batch
->idx
))
433 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
435 _mesa_set_add(batch
->resources
, rsc
);
436 rsc
->batch_mask
|= (1 << batch
->idx
);
440 fd_batch_check_size(struct fd_batch
*batch
)
442 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
445 struct fd_ringbuffer
*ring
= batch
->draw
;
446 if (((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000)) ||
447 (fd_mesa_debug
& FD_DBG_FLUSH
))
448 fd_batch_flush(batch
, true, false);
451 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
452 * been one since last draw:
455 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
457 if (batch
->needs_wfi
) {
458 if (batch
->ctx
->screen
->gpu_id
>= 500)
462 batch
->needs_wfi
= false;