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 batch
->binning
= fd_ringbuffer_new(ctx
->pipe
, size
);
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
);
63 fd_ringbuffer_set_parent(batch
->binning
, batch
->gmem
);
65 batch
->in_fence_fd
= -1;
66 batch
->fence
= fd_fence_create(batch
);
68 batch
->cleared
= batch
->partial_cleared
= 0;
69 batch
->restore
= batch
->resolve
= 0;
70 batch
->needs_flush
= false;
71 batch
->gmem_reason
= 0;
73 batch
->stage
= FD_STAGE_NULL
;
77 /* reset maximal bounds: */
78 batch
->max_scissor
.minx
= batch
->max_scissor
.miny
= ~0;
79 batch
->max_scissor
.maxx
= batch
->max_scissor
.maxy
= 0;
81 util_dynarray_init(&batch
->draw_patches
, NULL
);
83 if (is_a3xx(ctx
->screen
))
84 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
86 assert(batch
->resources
->entries
== 0);
88 util_dynarray_init(&batch
->samples
, NULL
);
92 fd_batch_create(struct fd_context
*ctx
)
94 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
101 pipe_reference_init(&batch
->reference
, 1);
104 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
105 _mesa_key_pointer_equal
);
113 batch_fini(struct fd_batch
*batch
)
115 pipe_resource_reference(&batch
->query_buf
, NULL
);
117 if (batch
->in_fence_fd
!= -1)
118 close(batch
->in_fence_fd
);
120 /* in case batch wasn't flushed but fence was created: */
121 fd_fence_populate(batch
->fence
, 0, -1);
123 fd_fence_ref(NULL
, &batch
->fence
, NULL
);
125 fd_ringbuffer_del(batch
->draw
);
126 fd_ringbuffer_del(batch
->binning
);
127 fd_ringbuffer_del(batch
->gmem
);
128 if (batch
->lrz_clear
) {
129 fd_ringbuffer_del(batch
->lrz_clear
);
130 batch
->lrz_clear
= NULL
;
133 util_dynarray_fini(&batch
->draw_patches
);
135 if (is_a3xx(batch
->ctx
->screen
))
136 util_dynarray_fini(&batch
->rbrc_patches
);
138 while (batch
->samples
.size
> 0) {
139 struct fd_hw_sample
*samp
=
140 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
141 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
143 util_dynarray_fini(&batch
->samples
);
145 if (batch
->ctx
->screen
->reorder
)
146 util_queue_fence_destroy(&batch
->flush_fence
);
150 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
152 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
153 struct fd_batch
*dep
;
155 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
157 fd_batch_flush(dep
, false, false);
158 fd_batch_reference(&dep
, NULL
);
161 batch
->dependents_mask
= 0;
165 batch_reset_resources_locked(struct fd_batch
*batch
)
167 struct set_entry
*entry
;
169 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
171 set_foreach(batch
->resources
, entry
) {
172 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
173 _mesa_set_remove(batch
->resources
, entry
);
174 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
175 rsc
->batch_mask
&= ~(1 << batch
->idx
);
176 if (rsc
->write_batch
== batch
)
177 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
182 batch_reset_resources(struct fd_batch
*batch
)
184 mtx_lock(&batch
->ctx
->screen
->lock
);
185 batch_reset_resources_locked(batch
);
186 mtx_unlock(&batch
->ctx
->screen
->lock
);
190 batch_reset(struct fd_batch
*batch
)
194 fd_batch_sync(batch
);
196 batch_flush_reset_dependencies(batch
, false);
197 batch_reset_resources(batch
);
204 fd_batch_reset(struct fd_batch
*batch
)
206 if (batch
->needs_flush
)
211 __fd_batch_destroy(struct fd_batch
*batch
)
215 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
217 mtx_lock(&batch
->ctx
->screen
->lock
);
218 fd_bc_invalidate_batch(batch
, true);
219 mtx_unlock(&batch
->ctx
->screen
->lock
);
223 batch_reset_resources(batch
);
224 debug_assert(batch
->resources
->entries
== 0);
225 _mesa_set_destroy(batch
->resources
, NULL
);
227 batch_flush_reset_dependencies(batch
, false);
228 debug_assert(batch
->dependents_mask
== 0);
234 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
236 util_sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
240 fd_batch_sync(struct fd_batch
*batch
)
242 if (!batch
->ctx
->screen
->reorder
)
244 util_queue_fence_wait(&batch
->flush_fence
);
248 batch_flush_func(void *job
, int id
)
250 struct fd_batch
*batch
= job
;
252 fd_gmem_render_tiles(batch
);
253 batch_reset_resources(batch
);
257 batch_cleanup_func(void *job
, int id
)
259 struct fd_batch
*batch
= job
;
260 fd_batch_reference(&batch
, NULL
);
264 batch_flush(struct fd_batch
*batch
, bool force
)
266 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
268 if (!batch
->needs_flush
) {
270 fd_gmem_render_noop(batch
);
276 batch
->needs_flush
= false;
278 /* close out the draw cmds by making sure any active queries are
281 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
283 fd_context_all_dirty(batch
->ctx
);
284 batch_flush_reset_dependencies(batch
, true);
286 if (batch
->ctx
->screen
->reorder
) {
287 struct fd_batch
*tmp
= NULL
;
288 fd_batch_reference(&tmp
, batch
);
290 if (!util_queue_is_initialized(&batch
->ctx
->flush_queue
))
291 util_queue_init(&batch
->ctx
->flush_queue
, "flush_queue", 16, 1, 0);
293 util_queue_add_job(&batch
->ctx
->flush_queue
,
294 batch
, &batch
->flush_fence
,
295 batch_flush_func
, batch_cleanup_func
);
297 fd_gmem_render_tiles(batch
);
298 batch_reset_resources(batch
);
301 debug_assert(batch
->reference
.count
> 0);
304 if (batch
== batch
->ctx
->batch
) {
307 mtx_lock(&batch
->ctx
->screen
->lock
);
308 fd_bc_invalidate_batch(batch
, false);
309 mtx_unlock(&batch
->ctx
->screen
->lock
);
313 /* NOTE: could drop the last ref to batch
315 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
316 * to kernel before this returns, as opposed to just being queued to be
318 * @force: force a flush even if no rendering, mostly useful if you need
322 fd_batch_flush(struct fd_batch
*batch
, bool sync
, bool force
)
324 /* NOTE: we need to hold an extra ref across the body of flush,
325 * since the last ref to this batch could be dropped when cleaning
328 struct fd_batch
*tmp
= NULL
;
329 fd_batch_reference(&tmp
, batch
);
330 batch_flush(tmp
, force
);
333 fd_batch_reference(&tmp
, NULL
);
336 /* does 'batch' depend directly or indirectly on 'other' ? */
338 batch_depends_on(struct fd_batch
*batch
, struct fd_batch
*other
)
340 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
341 struct fd_batch
*dep
;
343 if (batch
->dependents_mask
& (1 << other
->idx
))
346 foreach_batch(dep
, cache
, batch
->dependents_mask
)
347 if (batch_depends_on(batch
, dep
))
354 batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
356 if (batch
->dependents_mask
& (1 << dep
->idx
))
359 /* if the new depedency already depends on us, we need to flush
360 * to avoid a loop in the dependency graph.
362 if (batch_depends_on(dep
, batch
)) {
363 DBG("%p: flush forced on %p!", batch
, dep
);
364 mtx_unlock(&batch
->ctx
->screen
->lock
);
365 fd_batch_flush(dep
, false, false);
366 mtx_lock(&batch
->ctx
->screen
->lock
);
368 struct fd_batch
*other
= NULL
;
369 fd_batch_reference_locked(&other
, dep
);
370 batch
->dependents_mask
|= (1 << dep
->idx
);
371 DBG("%p: added dependency on %p", batch
, dep
);
376 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
378 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
381 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
383 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
385 /* note, invalidate write batch, to avoid further writes to rsc
386 * resulting in a write-after-read hazard.
390 /* if we are pending read or write by any other batch: */
391 if (rsc
->batch_mask
!= (1 << batch
->idx
)) {
392 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
393 struct fd_batch
*dep
;
394 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
395 struct fd_batch
*b
= NULL
;
396 /* note that batch_add_dep could flush and unref dep, so
397 * we need to hold a reference to keep it live for the
398 * fd_bc_invalidate_batch()
400 fd_batch_reference(&b
, dep
);
401 batch_add_dep(batch
, b
);
402 fd_bc_invalidate_batch(b
, false);
403 fd_batch_reference_locked(&b
, NULL
);
406 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
408 if (rsc
->write_batch
) {
409 batch_add_dep(batch
, rsc
->write_batch
);
410 fd_bc_invalidate_batch(rsc
->write_batch
, false);
414 if (rsc
->batch_mask
& (1 << batch
->idx
))
417 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
419 _mesa_set_add(batch
->resources
, rsc
);
420 rsc
->batch_mask
|= (1 << batch
->idx
);
424 fd_batch_check_size(struct fd_batch
*batch
)
426 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
429 struct fd_ringbuffer
*ring
= batch
->draw
;
430 if (((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000)) ||
431 (fd_mesa_debug
& FD_DBG_FLUSH
))
432 fd_batch_flush(batch
, true, false);
435 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
436 * been one since last draw:
439 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
441 if (batch
->needs_wfi
) {
442 if (batch
->ctx
->screen
->gpu_id
>= 500)
446 batch
->needs_wfi
= false;