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_resource.h"
35 #include "freedreno_query_hw.h"
38 batch_init(struct fd_batch
*batch
)
40 struct fd_context
*ctx
= batch
->ctx
;
43 if (ctx
->screen
->reorder
)
44 util_queue_fence_init(&batch
->flush_fence
);
46 /* if kernel is too old to support unlimited # of cmd buffers, we
47 * have no option but to allocate large worst-case sizes so that
48 * we don't need to grow the ringbuffer. Performance is likely to
49 * suffer, but there is no good alternative.
51 if ((fd_device_version(ctx
->screen
->dev
) < FD_VERSION_UNLIMITED_CMDS
) ||
52 (fd_mesa_debug
& FD_DBG_NOGROW
)){
56 batch
->draw
= fd_ringbuffer_new(ctx
->screen
->pipe
, size
);
57 batch
->binning
= fd_ringbuffer_new(ctx
->screen
->pipe
, size
);
58 batch
->gmem
= fd_ringbuffer_new(ctx
->screen
->pipe
, size
);
60 fd_ringbuffer_set_parent(batch
->gmem
, NULL
);
61 fd_ringbuffer_set_parent(batch
->draw
, batch
->gmem
);
62 fd_ringbuffer_set_parent(batch
->binning
, batch
->gmem
);
64 batch
->in_fence_fd
= -1;
66 batch
->cleared
= batch
->partial_cleared
= 0;
67 batch
->restore
= batch
->resolve
= 0;
68 batch
->needs_flush
= false;
69 batch
->gmem_reason
= 0;
71 batch
->stage
= FD_STAGE_NULL
;
75 /* reset maximal bounds: */
76 batch
->max_scissor
.minx
= batch
->max_scissor
.miny
= ~0;
77 batch
->max_scissor
.maxx
= batch
->max_scissor
.maxy
= 0;
79 util_dynarray_init(&batch
->draw_patches
, NULL
);
81 if (is_a3xx(ctx
->screen
))
82 util_dynarray_init(&batch
->rbrc_patches
, NULL
);
84 assert(batch
->resources
->entries
== 0);
86 util_dynarray_init(&batch
->samples
, NULL
);
90 fd_batch_create(struct fd_context
*ctx
)
92 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
99 pipe_reference_init(&batch
->reference
, 1);
102 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
103 _mesa_key_pointer_equal
);
111 batch_fini(struct fd_batch
*batch
)
113 pipe_resource_reference(&batch
->query_buf
, NULL
);
115 if (batch
->in_fence_fd
!= -1)
116 close(batch
->in_fence_fd
);
118 fd_ringbuffer_del(batch
->draw
);
119 fd_ringbuffer_del(batch
->binning
);
120 fd_ringbuffer_del(batch
->gmem
);
121 if (batch
->lrz_clear
) {
122 fd_ringbuffer_del(batch
->lrz_clear
);
123 batch
->lrz_clear
= NULL
;
126 util_dynarray_fini(&batch
->draw_patches
);
128 if (is_a3xx(batch
->ctx
->screen
))
129 util_dynarray_fini(&batch
->rbrc_patches
);
131 while (batch
->samples
.size
> 0) {
132 struct fd_hw_sample
*samp
=
133 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
134 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
136 util_dynarray_fini(&batch
->samples
);
138 if (batch
->ctx
->screen
->reorder
)
139 util_queue_fence_destroy(&batch
->flush_fence
);
143 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
145 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
146 struct fd_batch
*dep
;
148 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
150 fd_batch_flush(dep
, false);
151 fd_batch_reference(&dep
, NULL
);
154 batch
->dependents_mask
= 0;
158 batch_reset_resources_locked(struct fd_batch
*batch
)
160 struct set_entry
*entry
;
162 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
164 set_foreach(batch
->resources
, entry
) {
165 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
166 _mesa_set_remove(batch
->resources
, entry
);
167 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
168 rsc
->batch_mask
&= ~(1 << batch
->idx
);
169 if (rsc
->write_batch
== batch
)
170 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
175 batch_reset_resources(struct fd_batch
*batch
)
177 mtx_lock(&batch
->ctx
->screen
->lock
);
178 batch_reset_resources_locked(batch
);
179 mtx_unlock(&batch
->ctx
->screen
->lock
);
183 batch_reset(struct fd_batch
*batch
)
187 fd_batch_sync(batch
);
189 batch_flush_reset_dependencies(batch
, false);
190 batch_reset_resources(batch
);
197 fd_batch_reset(struct fd_batch
*batch
)
199 if (batch
->needs_flush
)
204 __fd_batch_destroy(struct fd_batch
*batch
)
208 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
210 mtx_lock(&batch
->ctx
->screen
->lock
);
211 fd_bc_invalidate_batch(batch
, true);
212 mtx_unlock(&batch
->ctx
->screen
->lock
);
216 batch_reset_resources(batch
);
217 debug_assert(batch
->resources
->entries
== 0);
218 _mesa_set_destroy(batch
->resources
, NULL
);
220 batch_flush_reset_dependencies(batch
, false);
221 debug_assert(batch
->dependents_mask
== 0);
227 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
229 util_sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
233 fd_batch_sync(struct fd_batch
*batch
)
235 if (!batch
->ctx
->screen
->reorder
)
237 util_queue_fence_wait(&batch
->flush_fence
);
241 batch_flush_func(void *job
, int id
)
243 struct fd_batch
*batch
= job
;
245 fd_gmem_render_tiles(batch
);
246 batch_reset_resources(batch
);
250 batch_cleanup_func(void *job
, int id
)
252 struct fd_batch
*batch
= job
;
253 fd_batch_reference(&batch
, NULL
);
257 batch_flush(struct fd_batch
*batch
)
259 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
261 if (!batch
->needs_flush
)
264 batch
->needs_flush
= false;
266 /* close out the draw cmds by making sure any active queries are
269 fd_batch_set_stage(batch
, FD_STAGE_NULL
);
271 fd_context_all_dirty(batch
->ctx
);
272 batch_flush_reset_dependencies(batch
, true);
274 if (batch
->ctx
->screen
->reorder
) {
275 struct fd_batch
*tmp
= NULL
;
276 fd_batch_reference(&tmp
, batch
);
278 if (!util_queue_is_initialized(&batch
->ctx
->flush_queue
))
279 util_queue_init(&batch
->ctx
->flush_queue
, "flush_queue", 16, 1, 0);
281 util_queue_add_job(&batch
->ctx
->flush_queue
,
282 batch
, &batch
->flush_fence
,
283 batch_flush_func
, batch_cleanup_func
);
285 fd_gmem_render_tiles(batch
);
286 batch_reset_resources(batch
);
289 debug_assert(batch
->reference
.count
> 0);
291 if (batch
== batch
->ctx
->batch
) {
294 mtx_lock(&batch
->ctx
->screen
->lock
);
295 fd_bc_invalidate_batch(batch
, false);
296 mtx_unlock(&batch
->ctx
->screen
->lock
);
300 /* NOTE: could drop the last ref to batch */
302 fd_batch_flush(struct fd_batch
*batch
, bool sync
)
304 /* NOTE: we need to hold an extra ref across the body of flush,
305 * since the last ref to this batch could be dropped when cleaning
308 struct fd_batch
*tmp
= NULL
;
309 fd_batch_reference(&tmp
, batch
);
313 fd_batch_reference(&tmp
, NULL
);
316 /* does 'batch' depend directly or indirectly on 'other' ? */
318 batch_depends_on(struct fd_batch
*batch
, struct fd_batch
*other
)
320 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
321 struct fd_batch
*dep
;
323 if (batch
->dependents_mask
& (1 << other
->idx
))
326 foreach_batch(dep
, cache
, batch
->dependents_mask
)
327 if (batch_depends_on(batch
, dep
))
334 batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
336 if (batch
->dependents_mask
& (1 << dep
->idx
))
339 /* if the new depedency already depends on us, we need to flush
340 * to avoid a loop in the dependency graph.
342 if (batch_depends_on(dep
, batch
)) {
343 DBG("%p: flush forced on %p!", batch
, dep
);
344 mtx_unlock(&batch
->ctx
->screen
->lock
);
345 fd_batch_flush(dep
, false);
346 mtx_lock(&batch
->ctx
->screen
->lock
);
348 struct fd_batch
*other
= NULL
;
349 fd_batch_reference_locked(&other
, dep
);
350 batch
->dependents_mask
|= (1 << dep
->idx
);
351 DBG("%p: added dependency on %p", batch
, dep
);
356 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
358 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
361 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
363 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
365 /* note, invalidate write batch, to avoid further writes to rsc
366 * resulting in a write-after-read hazard.
370 /* if we are pending read or write by any other batch: */
371 if (rsc
->batch_mask
!= (1 << batch
->idx
)) {
372 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
373 struct fd_batch
*dep
;
374 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
375 struct fd_batch
*b
= NULL
;
376 /* note that batch_add_dep could flush and unref dep, so
377 * we need to hold a reference to keep it live for the
378 * fd_bc_invalidate_batch()
380 fd_batch_reference(&b
, dep
);
381 batch_add_dep(batch
, b
);
382 fd_bc_invalidate_batch(b
, false);
383 fd_batch_reference_locked(&b
, NULL
);
386 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
388 if (rsc
->write_batch
) {
389 batch_add_dep(batch
, rsc
->write_batch
);
390 fd_bc_invalidate_batch(rsc
->write_batch
, false);
394 if (rsc
->batch_mask
& (1 << batch
->idx
))
397 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
399 _mesa_set_add(batch
->resources
, rsc
);
400 rsc
->batch_mask
|= (1 << batch
->idx
);
404 fd_batch_check_size(struct fd_batch
*batch
)
406 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
409 struct fd_ringbuffer
*ring
= batch
->draw
;
410 if (((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000)) ||
411 (fd_mesa_debug
& FD_DBG_FLUSH
))
412 fd_batch_flush(batch
, true);
415 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
416 * been one since last draw:
419 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
421 if (batch
->needs_wfi
) {
422 if (batch
->ctx
->screen
->gpu_id
>= 500)
426 batch
->needs_wfi
= false;