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
);
135 batch_fini(struct fd_batch
*batch
)
139 pipe_resource_reference(&batch
->query_buf
, NULL
);
141 if (batch
->in_fence_fd
!= -1)
142 close(batch
->in_fence_fd
);
144 /* in case batch wasn't flushed but fence was created: */
145 fd_fence_populate(batch
->fence
, 0, -1);
147 fd_fence_ref(&batch
->fence
, NULL
);
149 fd_ringbuffer_del(batch
->draw
);
150 if (!batch
->nondraw
) {
152 fd_ringbuffer_del(batch
->binning
);
153 fd_ringbuffer_del(batch
->gmem
);
155 debug_assert(!batch
->binning
);
156 debug_assert(!batch
->gmem
);
159 if (batch
->lrz_clear
) {
160 fd_ringbuffer_del(batch
->lrz_clear
);
161 batch
->lrz_clear
= NULL
;
164 if (batch
->tile_setup
) {
165 fd_ringbuffer_del(batch
->tile_setup
);
166 batch
->tile_setup
= NULL
;
169 if (batch
->tile_fini
) {
170 fd_ringbuffer_del(batch
->tile_fini
);
171 batch
->tile_fini
= NULL
;
174 if (batch
->tessellation
) {
175 fd_bo_del(batch
->tessfactor_bo
);
176 fd_bo_del(batch
->tessparam_bo
);
177 fd_ringbuffer_del(batch
->tess_addrs_constobj
);
180 fd_submit_del(batch
->submit
);
182 util_dynarray_fini(&batch
->draw_patches
);
183 util_dynarray_fini(&batch
->fb_read_patches
);
185 if (is_a2xx(batch
->ctx
->screen
)) {
186 util_dynarray_fini(&batch
->shader_patches
);
187 util_dynarray_fini(&batch
->gmem_patches
);
190 if (is_a3xx(batch
->ctx
->screen
))
191 util_dynarray_fini(&batch
->rbrc_patches
);
193 while (batch
->samples
.size
> 0) {
194 struct fd_hw_sample
*samp
=
195 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
196 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
198 util_dynarray_fini(&batch
->samples
);
200 assert(list_is_empty(&batch
->log_chunks
));
204 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
206 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
207 struct fd_batch
*dep
;
209 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
212 fd_batch_reference(&dep
, NULL
);
215 batch
->dependents_mask
= 0;
219 batch_reset_resources_locked(struct fd_batch
*batch
)
221 fd_screen_assert_locked(batch
->ctx
->screen
);
223 set_foreach(batch
->resources
, entry
) {
224 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
225 _mesa_set_remove(batch
->resources
, entry
);
226 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
227 rsc
->batch_mask
&= ~(1 << batch
->idx
);
228 if (rsc
->write_batch
== batch
)
229 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
234 batch_reset_resources(struct fd_batch
*batch
)
236 fd_screen_lock(batch
->ctx
->screen
);
237 batch_reset_resources_locked(batch
);
238 fd_screen_unlock(batch
->ctx
->screen
);
242 batch_reset(struct fd_batch
*batch
)
246 batch_flush_reset_dependencies(batch
, false);
247 batch_reset_resources(batch
);
254 fd_batch_reset(struct fd_batch
*batch
)
256 if (batch
->needs_flush
)
261 __fd_batch_destroy(struct fd_batch
*batch
)
263 struct fd_context
*ctx
= batch
->ctx
;
267 fd_context_assert_locked(batch
->ctx
);
269 fd_bc_invalidate_batch(batch
, true);
271 batch_reset_resources_locked(batch
);
272 debug_assert(batch
->resources
->entries
== 0);
273 _mesa_set_destroy(batch
->resources
, NULL
);
275 fd_context_unlock(ctx
);
276 batch_flush_reset_dependencies(batch
, false);
277 debug_assert(batch
->dependents_mask
== 0);
279 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
282 fd_context_lock(ctx
);
286 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
288 sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
292 batch_flush(struct fd_batch
*batch
)
294 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
299 batch
->needs_flush
= false;
301 /* close out the draw cmds by making sure any active queries are
304 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
306 batch_flush_reset_dependencies(batch
, true);
308 batch
->flushed
= true;
310 fd_fence_ref(&batch
->ctx
->last_fence
, batch
->fence
);
312 fd_gmem_render_tiles(batch
);
313 batch_reset_resources(batch
);
315 debug_assert(batch
->reference
.count
> 0);
317 fd_screen_lock(batch
->ctx
->screen
);
318 fd_bc_invalidate_batch(batch
, false);
319 fd_screen_unlock(batch
->ctx
->screen
);
322 /* NOTE: could drop the last ref to batch
324 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
325 * to kernel before this returns, as opposed to just being queued to be
327 * @force: force a flush even if no rendering, mostly useful if you need
331 fd_batch_flush(struct fd_batch
*batch
)
333 struct fd_batch
*tmp
= NULL
;
335 /* NOTE: we need to hold an extra ref across the body of flush,
336 * since the last ref to this batch could be dropped when cleaning
339 fd_batch_reference(&tmp
, batch
);
343 if (batch
== batch
->ctx
->batch
) {
344 fd_batch_reference(&batch
->ctx
->batch
, NULL
);
347 fd_batch_reference(&tmp
, NULL
);
350 /* find a batches dependents mask, including recursive dependencies: */
352 recursive_dependents_mask(struct fd_batch
*batch
)
354 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
355 struct fd_batch
*dep
;
356 uint32_t dependents_mask
= batch
->dependents_mask
;
358 foreach_batch(dep
, cache
, batch
->dependents_mask
)
359 dependents_mask
|= recursive_dependents_mask(dep
);
361 return dependents_mask
;
365 fd_batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
367 fd_screen_assert_locked(batch
->ctx
->screen
);
369 if (batch
->dependents_mask
& (1 << dep
->idx
))
372 /* a loop should not be possible */
373 debug_assert(!((1 << batch
->idx
) & recursive_dependents_mask(dep
)));
375 struct fd_batch
*other
= NULL
;
376 fd_batch_reference_locked(&other
, dep
);
377 batch
->dependents_mask
|= (1 << dep
->idx
);
378 DBG("%p: added dependency on %p", batch
, dep
);
382 flush_write_batch(struct fd_resource
*rsc
)
384 struct fd_batch
*b
= NULL
;
385 fd_batch_reference_locked(&b
, rsc
->write_batch
);
387 fd_screen_unlock(b
->ctx
->screen
);
389 fd_screen_lock(b
->ctx
->screen
);
391 fd_bc_invalidate_batch(b
, false);
392 fd_batch_reference_locked(&b
, NULL
);
396 fd_batch_references_resource(struct fd_batch
*batch
, struct fd_resource
*rsc
)
398 return rsc
->batch_mask
& (1 << batch
->idx
);
402 fd_batch_add_resource(struct fd_batch
*batch
, struct fd_resource
*rsc
)
405 if (likely(fd_batch_references_resource(batch
, rsc
))) {
406 debug_assert(_mesa_set_search(batch
->resources
, rsc
));
410 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
412 _mesa_set_add(batch
->resources
, rsc
);
413 rsc
->batch_mask
|= (1 << batch
->idx
);
417 fd_batch_resource_write(struct fd_batch
*batch
, struct fd_resource
*rsc
)
419 fd_screen_assert_locked(batch
->ctx
->screen
);
422 fd_batch_resource_write(batch
, rsc
->stencil
);
424 DBG("%p: write %p", batch
, rsc
);
428 /* note, invalidate write batch, to avoid further writes to rsc
429 * resulting in a write-after-read hazard.
431 /* if we are pending read or write by any other batch: */
432 if (unlikely(rsc
->batch_mask
& ~(1 << batch
->idx
))) {
433 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
434 struct fd_batch
*dep
;
436 if (rsc
->write_batch
&& rsc
->write_batch
!= batch
)
437 flush_write_batch(rsc
);
439 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
440 struct fd_batch
*b
= NULL
;
443 /* note that batch_add_dep could flush and unref dep, so
444 * we need to hold a reference to keep it live for the
445 * fd_bc_invalidate_batch()
447 fd_batch_reference(&b
, dep
);
448 fd_batch_add_dep(batch
, b
);
449 fd_bc_invalidate_batch(b
, false);
450 fd_batch_reference_locked(&b
, NULL
);
453 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
455 fd_batch_add_resource(batch
, rsc
);
459 fd_batch_resource_read(struct fd_batch
*batch
, struct fd_resource
*rsc
)
461 fd_screen_assert_locked(batch
->ctx
->screen
);
463 /* Early out, if we hit this then we know we don't have anyone else
464 * writing to it (since both _write and _read flush other writers), and
465 * that we've already recursed for stencil.
467 if (likely(fd_batch_references_resource(batch
, rsc
)))
471 fd_batch_resource_read(batch
, rsc
->stencil
);
473 DBG("%p: read %p", batch
, rsc
);
475 /* If reading a resource pending a write, go ahead and flush the
476 * writer. This avoids situations where we end up having to
477 * flush the current batch in _resource_used()
479 if (unlikely(rsc
->write_batch
&& rsc
->write_batch
!= batch
))
480 flush_write_batch(rsc
);
482 fd_batch_add_resource(batch
, rsc
);
486 fd_batch_check_size(struct fd_batch
*batch
)
488 debug_assert(!batch
->flushed
);
490 if (unlikely(fd_mesa_debug
& FD_DBG_FLUSH
)) {
491 fd_batch_flush(batch
);
495 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
498 struct fd_ringbuffer
*ring
= batch
->draw
;
499 if ((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000))
500 fd_batch_flush(batch
);
503 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
504 * been one since last draw:
507 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
509 if (batch
->needs_wfi
) {
510 if (batch
->ctx
->screen
->gpu_id
>= 500)
514 batch
->needs_wfi
= false;