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
;
42 enum fd_ringbuffer_flags flags
= 0;
45 /* if kernel is too old to support unlimited # of cmd buffers, we
46 * have no option but to allocate large worst-case sizes so that
47 * we don't need to grow the ringbuffer. Performance is likely to
48 * suffer, but there is no good alternative.
50 * XXX I think we can just require new enough kernel for this?
52 if ((fd_device_version(ctx
->screen
->dev
) < FD_VERSION_UNLIMITED_CMDS
) ||
53 (fd_mesa_debug
& FD_DBG_NOGROW
)){
56 flags
= FD_RINGBUFFER_GROWABLE
;
59 batch
->submit
= fd_submit_new(ctx
->pipe
);
61 batch
->draw
= fd_submit_new_ringbuffer(batch
->submit
, size
,
62 FD_RINGBUFFER_PRIMARY
| flags
);
64 batch
->gmem
= fd_submit_new_ringbuffer(batch
->submit
, size
,
65 FD_RINGBUFFER_PRIMARY
| flags
);
66 batch
->draw
= fd_submit_new_ringbuffer(batch
->submit
, size
,
69 if (ctx
->screen
->gpu_id
< 600) {
70 batch
->binning
= fd_submit_new_ringbuffer(batch
->submit
,
75 batch
->in_fence_fd
= -1;
76 batch
->fence
= fd_fence_create(batch
);
79 batch
->fast_cleared
= 0;
80 batch
->invalidated
= 0;
81 batch
->restore
= batch
->resolve
= 0;
82 batch
->needs_flush
= false;
83 batch
->flushed
= false;
84 batch
->gmem_reason
= 0;
86 batch
->num_vertices
= 0;
87 batch
->stage
= FD_STAGE_NULL
;
91 util_dynarray_init(&batch
->draw_patches
, NULL
);
92 util_dynarray_init(&batch
->fb_read_patches
, NULL
);
94 if (is_a2xx(ctx
->screen
)) {
95 util_dynarray_init(&batch
->shader_patches
, NULL
);
96 util_dynarray_init(&batch
->gmem_patches
, NULL
);
99 if (is_a3xx(ctx
->screen
))
100 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
102 assert(batch
->resources
->entries
== 0);
104 util_dynarray_init(&batch
->samples
, NULL
);
106 list_inithead(&batch
->log_chunks
);
110 fd_batch_create(struct fd_context
*ctx
, bool nondraw
)
112 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
119 pipe_reference_init(&batch
->reference
, 1);
121 batch
->nondraw
= nondraw
;
123 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
124 _mesa_key_pointer_equal
);
132 batch_fini(struct fd_batch
*batch
)
136 pipe_resource_reference(&batch
->query_buf
, NULL
);
138 if (batch
->in_fence_fd
!= -1)
139 close(batch
->in_fence_fd
);
141 /* in case batch wasn't flushed but fence was created: */
142 fd_fence_populate(batch
->fence
, 0, -1);
144 fd_fence_ref(&batch
->fence
, NULL
);
146 fd_ringbuffer_del(batch
->draw
);
147 if (!batch
->nondraw
) {
149 fd_ringbuffer_del(batch
->binning
);
150 fd_ringbuffer_del(batch
->gmem
);
152 debug_assert(!batch
->binning
);
153 debug_assert(!batch
->gmem
);
156 if (batch
->lrz_clear
) {
157 fd_ringbuffer_del(batch
->lrz_clear
);
158 batch
->lrz_clear
= NULL
;
161 if (batch
->tile_setup
) {
162 fd_ringbuffer_del(batch
->tile_setup
);
163 batch
->tile_setup
= NULL
;
166 if (batch
->tile_fini
) {
167 fd_ringbuffer_del(batch
->tile_fini
);
168 batch
->tile_fini
= NULL
;
171 if (batch
->tessellation
) {
172 fd_bo_del(batch
->tessfactor_bo
);
173 fd_bo_del(batch
->tessparam_bo
);
174 fd_ringbuffer_del(batch
->tess_addrs_constobj
);
177 fd_submit_del(batch
->submit
);
179 util_dynarray_fini(&batch
->draw_patches
);
180 util_dynarray_fini(&batch
->fb_read_patches
);
182 if (is_a2xx(batch
->ctx
->screen
)) {
183 util_dynarray_fini(&batch
->shader_patches
);
184 util_dynarray_fini(&batch
->gmem_patches
);
187 if (is_a3xx(batch
->ctx
->screen
))
188 util_dynarray_fini(&batch
->rbrc_patches
);
190 while (batch
->samples
.size
> 0) {
191 struct fd_hw_sample
*samp
=
192 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
193 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
195 util_dynarray_fini(&batch
->samples
);
197 assert(list_is_empty(&batch
->log_chunks
));
201 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
203 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
204 struct fd_batch
*dep
;
206 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
209 fd_batch_reference(&dep
, NULL
);
212 batch
->dependents_mask
= 0;
216 batch_reset_resources_locked(struct fd_batch
*batch
)
218 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
220 set_foreach(batch
->resources
, entry
) {
221 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
222 _mesa_set_remove(batch
->resources
, entry
);
223 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
224 rsc
->batch_mask
&= ~(1 << batch
->idx
);
225 if (rsc
->write_batch
== batch
)
226 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
231 batch_reset_resources(struct fd_batch
*batch
)
233 mtx_lock(&batch
->ctx
->screen
->lock
);
234 batch_reset_resources_locked(batch
);
235 mtx_unlock(&batch
->ctx
->screen
->lock
);
239 batch_reset(struct fd_batch
*batch
)
243 batch_flush_reset_dependencies(batch
, false);
244 batch_reset_resources(batch
);
251 fd_batch_reset(struct fd_batch
*batch
)
253 if (batch
->needs_flush
)
258 __fd_batch_destroy(struct fd_batch
*batch
)
260 struct fd_context
*ctx
= batch
->ctx
;
264 fd_context_assert_locked(batch
->ctx
);
266 fd_bc_invalidate_batch(batch
, true);
268 batch_reset_resources_locked(batch
);
269 debug_assert(batch
->resources
->entries
== 0);
270 _mesa_set_destroy(batch
->resources
, NULL
);
272 fd_context_unlock(ctx
);
273 batch_flush_reset_dependencies(batch
, false);
274 debug_assert(batch
->dependents_mask
== 0);
276 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
279 fd_context_lock(ctx
);
283 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
285 sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
289 batch_flush(struct fd_batch
*batch
)
291 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
296 batch
->needs_flush
= false;
298 /* close out the draw cmds by making sure any active queries are
301 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
303 batch_flush_reset_dependencies(batch
, true);
305 batch
->flushed
= true;
307 fd_fence_ref(&batch
->ctx
->last_fence
, batch
->fence
);
309 fd_gmem_render_tiles(batch
);
310 batch_reset_resources(batch
);
312 debug_assert(batch
->reference
.count
> 0);
314 mtx_lock(&batch
->ctx
->screen
->lock
);
315 fd_bc_invalidate_batch(batch
, false);
316 mtx_unlock(&batch
->ctx
->screen
->lock
);
319 /* NOTE: could drop the last ref to batch
321 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
322 * to kernel before this returns, as opposed to just being queued to be
324 * @force: force a flush even if no rendering, mostly useful if you need
328 fd_batch_flush(struct fd_batch
*batch
)
330 struct fd_batch
*tmp
= NULL
;
332 /* NOTE: we need to hold an extra ref across the body of flush,
333 * since the last ref to this batch could be dropped when cleaning
336 fd_batch_reference(&tmp
, batch
);
340 if (batch
== batch
->ctx
->batch
) {
341 fd_batch_reference(&batch
->ctx
->batch
, NULL
);
344 fd_batch_reference(&tmp
, NULL
);
347 /* find a batches dependents mask, including recursive dependencies: */
349 recursive_dependents_mask(struct fd_batch
*batch
)
351 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
352 struct fd_batch
*dep
;
353 uint32_t dependents_mask
= batch
->dependents_mask
;
355 foreach_batch(dep
, cache
, batch
->dependents_mask
)
356 dependents_mask
|= recursive_dependents_mask(dep
);
358 return dependents_mask
;
362 fd_batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
364 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
366 if (batch
->dependents_mask
& (1 << dep
->idx
))
369 /* a loop should not be possible */
370 debug_assert(!((1 << batch
->idx
) & recursive_dependents_mask(dep
)));
372 struct fd_batch
*other
= NULL
;
373 fd_batch_reference_locked(&other
, dep
);
374 batch
->dependents_mask
|= (1 << dep
->idx
);
375 DBG("%p: added dependency on %p", batch
, dep
);
379 flush_write_batch(struct fd_resource
*rsc
)
381 struct fd_batch
*b
= NULL
;
382 fd_batch_reference_locked(&b
, rsc
->write_batch
);
384 mtx_unlock(&b
->ctx
->screen
->lock
);
386 mtx_lock(&b
->ctx
->screen
->lock
);
388 fd_bc_invalidate_batch(b
, false);
389 fd_batch_reference_locked(&b
, NULL
);
393 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
395 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
398 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
400 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
405 /* note, invalidate write batch, to avoid further writes to rsc
406 * resulting in a write-after-read hazard.
410 /* if we are pending read or write by any other batch: */
411 if (rsc
->batch_mask
& ~(1 << batch
->idx
)) {
412 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
413 struct fd_batch
*dep
;
415 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
416 flush_write_batch(rsc
);
418 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
419 struct fd_batch
*b
= NULL
;
422 /* note that batch_add_dep could flush and unref dep, so
423 * we need to hold a reference to keep it live for the
424 * fd_bc_invalidate_batch()
426 fd_batch_reference(&b
, dep
);
427 fd_batch_add_dep(batch
, b
);
428 fd_bc_invalidate_batch(b
, false);
429 fd_batch_reference_locked(&b
, NULL
);
432 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
434 /* If reading a resource pending a write, go ahead and flush the
435 * writer. This avoids situations where we end up having to
436 * flush the current batch in _resource_used()
438 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
439 flush_write_batch(rsc
);
442 if (rsc
->batch_mask
& (1 << batch
->idx
)) {
443 debug_assert(_mesa_set_search(batch
->resources
, rsc
));
447 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
449 _mesa_set_add(batch
->resources
, rsc
);
450 rsc
->batch_mask
|= (1 << batch
->idx
);
454 fd_batch_check_size(struct fd_batch
*batch
)
456 debug_assert(!batch
->flushed
);
458 if (unlikely(fd_mesa_debug
& FD_DBG_FLUSH
)) {
459 fd_batch_flush(batch
);
463 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
466 struct fd_ringbuffer
*ring
= batch
->draw
;
467 if ((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000))
468 fd_batch_flush(batch
);
471 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
472 * been one since last draw:
475 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
477 if (batch
->needs_wfi
) {
478 if (batch
->ctx
->screen
->gpu_id
>= 500)
482 batch
->needs_wfi
= false;