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
->num_bins_per_pipe
= 0;
88 batch
->prim_strm_bits
= 0;
89 batch
->draw_strm_bits
= 0;
90 batch
->stage
= FD_STAGE_NULL
;
94 util_dynarray_init(&batch
->draw_patches
, NULL
);
95 util_dynarray_init(&batch
->fb_read_patches
, NULL
);
97 if (is_a2xx(ctx
->screen
)) {
98 util_dynarray_init(&batch
->shader_patches
, NULL
);
99 util_dynarray_init(&batch
->gmem_patches
, NULL
);
102 if (is_a3xx(ctx
->screen
))
103 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
105 assert(batch
->resources
->entries
== 0);
107 util_dynarray_init(&batch
->samples
, NULL
);
109 list_inithead(&batch
->log_chunks
);
113 fd_batch_create(struct fd_context
*ctx
, bool nondraw
)
115 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
122 pipe_reference_init(&batch
->reference
, 1);
124 batch
->nondraw
= nondraw
;
126 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
127 _mesa_key_pointer_equal
);
131 fd_screen_assert_locked(ctx
->screen
);
133 _mesa_set_add(ctx
->screen
->live_batches
, batch
);
140 batch_fini(struct fd_batch
*batch
)
144 pipe_resource_reference(&batch
->query_buf
, NULL
);
146 if (batch
->in_fence_fd
!= -1)
147 close(batch
->in_fence_fd
);
149 /* in case batch wasn't flushed but fence was created: */
150 fd_fence_populate(batch
->fence
, 0, -1);
152 fd_fence_ref(&batch
->fence
, NULL
);
154 fd_ringbuffer_del(batch
->draw
);
155 if (!batch
->nondraw
) {
157 fd_ringbuffer_del(batch
->binning
);
158 fd_ringbuffer_del(batch
->gmem
);
160 debug_assert(!batch
->binning
);
161 debug_assert(!batch
->gmem
);
164 if (batch
->lrz_clear
) {
165 fd_ringbuffer_del(batch
->lrz_clear
);
166 batch
->lrz_clear
= NULL
;
169 if (batch
->epilogue
) {
170 fd_ringbuffer_del(batch
->epilogue
);
171 batch
->epilogue
= NULL
;
174 if (batch
->tile_setup
) {
175 fd_ringbuffer_del(batch
->tile_setup
);
176 batch
->tile_setup
= NULL
;
179 if (batch
->tile_fini
) {
180 fd_ringbuffer_del(batch
->tile_fini
);
181 batch
->tile_fini
= NULL
;
184 if (batch
->tessellation
) {
185 fd_bo_del(batch
->tessfactor_bo
);
186 fd_bo_del(batch
->tessparam_bo
);
187 fd_ringbuffer_del(batch
->tess_addrs_constobj
);
190 fd_submit_del(batch
->submit
);
192 util_dynarray_fini(&batch
->draw_patches
);
193 util_dynarray_fini(&batch
->fb_read_patches
);
195 if (is_a2xx(batch
->ctx
->screen
)) {
196 util_dynarray_fini(&batch
->shader_patches
);
197 util_dynarray_fini(&batch
->gmem_patches
);
200 if (is_a3xx(batch
->ctx
->screen
))
201 util_dynarray_fini(&batch
->rbrc_patches
);
203 while (batch
->samples
.size
> 0) {
204 struct fd_hw_sample
*samp
=
205 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
206 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
208 util_dynarray_fini(&batch
->samples
);
210 assert(list_is_empty(&batch
->log_chunks
));
214 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
216 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
217 struct fd_batch
*dep
;
219 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
222 fd_batch_reference(&dep
, NULL
);
225 batch
->dependents_mask
= 0;
229 batch_reset_resources_locked(struct fd_batch
*batch
)
231 fd_screen_assert_locked(batch
->ctx
->screen
);
233 set_foreach(batch
->resources
, entry
) {
234 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
235 _mesa_set_remove(batch
->resources
, entry
);
236 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
237 rsc
->batch_mask
&= ~(1 << batch
->idx
);
238 if (rsc
->write_batch
== batch
)
239 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
244 batch_reset_resources(struct fd_batch
*batch
)
246 fd_screen_lock(batch
->ctx
->screen
);
247 batch_reset_resources_locked(batch
);
248 fd_screen_unlock(batch
->ctx
->screen
);
252 batch_reset(struct fd_batch
*batch
)
256 batch_flush_reset_dependencies(batch
, false);
257 batch_reset_resources(batch
);
264 fd_batch_reset(struct fd_batch
*batch
)
266 if (batch
->needs_flush
)
271 __fd_batch_destroy(struct fd_batch
*batch
)
273 struct fd_context
*ctx
= batch
->ctx
;
277 fd_context_assert_locked(batch
->ctx
);
280 _mesa_set_remove_key(ctx
->screen
->live_batches
, batch
);
283 fd_bc_invalidate_batch(batch
, true);
285 batch_reset_resources_locked(batch
);
286 debug_assert(batch
->resources
->entries
== 0);
287 _mesa_set_destroy(batch
->resources
, NULL
);
289 fd_context_unlock(ctx
);
290 batch_flush_reset_dependencies(batch
, false);
291 debug_assert(batch
->dependents_mask
== 0);
293 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
296 fd_context_lock(ctx
);
300 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
302 sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
306 batch_flush(struct fd_batch
*batch
)
308 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
313 batch
->needs_flush
= false;
315 /* close out the draw cmds by making sure any active queries are
318 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
320 batch_flush_reset_dependencies(batch
, true);
322 batch
->flushed
= true;
324 fd_fence_ref(&batch
->ctx
->last_fence
, batch
->fence
);
326 fd_gmem_render_tiles(batch
);
327 batch_reset_resources(batch
);
329 debug_assert(batch
->reference
.count
> 0);
331 fd_screen_lock(batch
->ctx
->screen
);
332 fd_bc_invalidate_batch(batch
, false);
333 fd_screen_unlock(batch
->ctx
->screen
);
336 /* NOTE: could drop the last ref to batch
338 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
339 * to kernel before this returns, as opposed to just being queued to be
341 * @force: force a flush even if no rendering, mostly useful if you need
345 fd_batch_flush(struct fd_batch
*batch
)
347 struct fd_batch
*tmp
= NULL
;
349 /* NOTE: we need to hold an extra ref across the body of flush,
350 * since the last ref to this batch could be dropped when cleaning
353 fd_batch_reference(&tmp
, batch
);
357 if (batch
== batch
->ctx
->batch
) {
358 fd_batch_reference(&batch
->ctx
->batch
, NULL
);
361 fd_batch_reference(&tmp
, NULL
);
364 /* find a batches dependents mask, including recursive dependencies: */
366 recursive_dependents_mask(struct fd_batch
*batch
)
368 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
369 struct fd_batch
*dep
;
370 uint32_t dependents_mask
= batch
->dependents_mask
;
372 foreach_batch(dep
, cache
, batch
->dependents_mask
)
373 dependents_mask
|= recursive_dependents_mask(dep
);
375 return dependents_mask
;
379 fd_batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
381 fd_screen_assert_locked(batch
->ctx
->screen
);
383 if (batch
->dependents_mask
& (1 << dep
->idx
))
386 /* a loop should not be possible */
387 debug_assert(!((1 << batch
->idx
) & recursive_dependents_mask(dep
)));
389 struct fd_batch
*other
= NULL
;
390 fd_batch_reference_locked(&other
, dep
);
391 batch
->dependents_mask
|= (1 << dep
->idx
);
392 DBG("%p: added dependency on %p", batch
, dep
);
396 flush_write_batch(struct fd_resource
*rsc
)
398 struct fd_batch
*b
= NULL
;
399 fd_batch_reference_locked(&b
, rsc
->write_batch
);
401 fd_screen_unlock(b
->ctx
->screen
);
403 fd_screen_lock(b
->ctx
->screen
);
405 fd_bc_invalidate_batch(b
, false);
406 fd_batch_reference_locked(&b
, NULL
);
410 fd_batch_add_resource(struct fd_batch
*batch
, struct fd_resource
*rsc
)
413 if (likely(fd_batch_references_resource(batch
, rsc
))) {
414 debug_assert(_mesa_set_search(batch
->resources
, rsc
));
418 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
420 _mesa_set_add(batch
->resources
, rsc
);
421 rsc
->batch_mask
|= (1 << batch
->idx
);
425 fd_batch_resource_write(struct fd_batch
*batch
, struct fd_resource
*rsc
)
427 fd_screen_assert_locked(batch
->ctx
->screen
);
430 fd_batch_resource_write(batch
, rsc
->stencil
);
432 DBG("%p: write %p", batch
, rsc
);
436 /* note, invalidate write batch, to avoid further writes to rsc
437 * resulting in a write-after-read hazard.
439 /* if we are pending read or write by any other batch: */
440 if (unlikely(rsc
->batch_mask
& ~(1 << batch
->idx
))) {
441 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
442 struct fd_batch
*dep
;
444 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
445 flush_write_batch(rsc
);
447 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
448 struct fd_batch
*b
= NULL
;
451 /* note that batch_add_dep could flush and unref dep, so
452 * we need to hold a reference to keep it live for the
453 * fd_bc_invalidate_batch()
455 fd_batch_reference(&b
, dep
);
456 fd_batch_add_dep(batch
, b
);
457 fd_bc_invalidate_batch(b
, false);
458 fd_batch_reference_locked(&b
, NULL
);
461 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
463 fd_batch_add_resource(batch
, rsc
);
467 fd_batch_resource_read_slowpath(struct fd_batch
*batch
, struct fd_resource
*rsc
)
469 fd_screen_assert_locked(batch
->ctx
->screen
);
472 fd_batch_resource_read(batch
, rsc
->stencil
);
474 DBG("%p: read %p", batch
, rsc
);
476 /* If reading a resource pending a write, go ahead and flush the
477 * writer. This avoids situations where we end up having to
478 * flush the current batch in _resource_used()
480 if (unlikely(rsc
->write_batch
&& rsc
->write_batch
!= batch
))
481 flush_write_batch(rsc
);
483 fd_batch_add_resource(batch
, rsc
);
487 fd_batch_check_size(struct fd_batch
*batch
)
489 debug_assert(!batch
->flushed
);
491 if (unlikely(fd_mesa_debug
& FD_DBG_FLUSH
)) {
492 fd_batch_flush(batch
);
496 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
499 struct fd_ringbuffer
*ring
= batch
->draw
;
500 if ((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000))
501 fd_batch_flush(batch
);
504 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
505 * been one since last draw:
508 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
510 if (batch
->needs_wfi
) {
511 if (batch
->ctx
->screen
->gpu_id
>= 500)
515 batch
->needs_wfi
= false;