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
);
81 if (is_a3xx(ctx
->screen
))
82 util_dynarray_init(&batch
->rbrc_patches
);
84 assert(batch
->resources
->entries
== 0);
86 util_dynarray_init(&batch
->samples
);
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
);
122 util_dynarray_fini(&batch
->draw_patches
);
124 if (is_a3xx(batch
->ctx
->screen
))
125 util_dynarray_fini(&batch
->rbrc_patches
);
127 while (batch
->samples
.size
> 0) {
128 struct fd_hw_sample
*samp
=
129 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
130 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
132 util_dynarray_fini(&batch
->samples
);
134 if (batch
->ctx
->screen
->reorder
)
135 util_queue_fence_destroy(&batch
->flush_fence
);
139 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
141 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
142 struct fd_batch
*dep
;
144 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
146 fd_batch_flush(dep
, false);
147 fd_batch_reference(&dep
, NULL
);
150 batch
->dependents_mask
= 0;
154 batch_reset_resources_locked(struct fd_batch
*batch
)
156 struct set_entry
*entry
;
158 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
160 set_foreach(batch
->resources
, entry
) {
161 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
162 _mesa_set_remove(batch
->resources
, entry
);
163 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
164 rsc
->batch_mask
&= ~(1 << batch
->idx
);
165 if (rsc
->write_batch
== batch
)
166 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
171 batch_reset_resources(struct fd_batch
*batch
)
173 mtx_lock(&batch
->ctx
->screen
->lock
);
174 batch_reset_resources_locked(batch
);
175 mtx_unlock(&batch
->ctx
->screen
->lock
);
179 batch_reset(struct fd_batch
*batch
)
183 fd_batch_sync(batch
);
185 batch_flush_reset_dependencies(batch
, false);
186 batch_reset_resources(batch
);
193 fd_batch_reset(struct fd_batch
*batch
)
195 if (batch
->needs_flush
)
200 __fd_batch_destroy(struct fd_batch
*batch
)
204 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
206 mtx_lock(&batch
->ctx
->screen
->lock
);
207 fd_bc_invalidate_batch(batch
, true);
208 mtx_unlock(&batch
->ctx
->screen
->lock
);
212 batch_reset_resources(batch
);
213 debug_assert(batch
->resources
->entries
== 0);
214 _mesa_set_destroy(batch
->resources
, NULL
);
216 batch_flush_reset_dependencies(batch
, false);
217 debug_assert(batch
->dependents_mask
== 0);
223 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
225 util_sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
229 fd_batch_sync(struct fd_batch
*batch
)
231 if (!batch
->ctx
->screen
->reorder
)
233 util_queue_fence_wait(&batch
->flush_fence
);
237 batch_flush_func(void *job
, int id
)
239 struct fd_batch
*batch
= job
;
241 fd_gmem_render_tiles(batch
);
242 batch_reset_resources(batch
);
246 batch_cleanup_func(void *job
, int id
)
248 struct fd_batch
*batch
= job
;
249 fd_batch_reference(&batch
, NULL
);
253 batch_flush(struct fd_batch
*batch
)
255 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
257 if (!batch
->needs_flush
)
260 batch
->needs_flush
= false;
262 /* close out the draw cmds by making sure any active queries are
265 fd_batch_set_stage(batch
, batch
->draw
, FD_STAGE_NULL
);
267 fd_context_all_dirty(batch
->ctx
);
268 batch_flush_reset_dependencies(batch
, true);
270 if (batch
->ctx
->screen
->reorder
) {
271 struct fd_batch
*tmp
= NULL
;
272 fd_batch_reference(&tmp
, batch
);
274 if (!util_queue_is_initialized(&batch
->ctx
->flush_queue
))
275 util_queue_init(&batch
->ctx
->flush_queue
, "flush_queue", 16, 1);
277 util_queue_add_job(&batch
->ctx
->flush_queue
,
278 batch
, &batch
->flush_fence
,
279 batch_flush_func
, batch_cleanup_func
);
281 fd_gmem_render_tiles(batch
);
282 batch_reset_resources(batch
);
285 debug_assert(batch
->reference
.count
> 0);
287 if (batch
== batch
->ctx
->batch
) {
290 mtx_lock(&batch
->ctx
->screen
->lock
);
291 fd_bc_invalidate_batch(batch
, false);
292 mtx_unlock(&batch
->ctx
->screen
->lock
);
296 /* NOTE: could drop the last ref to batch */
298 fd_batch_flush(struct fd_batch
*batch
, bool sync
)
300 /* NOTE: we need to hold an extra ref across the body of flush,
301 * since the last ref to this batch could be dropped when cleaning
304 struct fd_batch
*tmp
= NULL
;
305 fd_batch_reference(&tmp
, batch
);
309 fd_batch_reference(&tmp
, NULL
);
312 /* does 'batch' depend directly or indirectly on 'other' ? */
314 batch_depends_on(struct fd_batch
*batch
, struct fd_batch
*other
)
316 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
317 struct fd_batch
*dep
;
319 if (batch
->dependents_mask
& (1 << other
->idx
))
322 foreach_batch(dep
, cache
, batch
->dependents_mask
)
323 if (batch_depends_on(batch
, dep
))
330 batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
332 if (batch
->dependents_mask
& (1 << dep
->idx
))
335 /* if the new depedency already depends on us, we need to flush
336 * to avoid a loop in the dependency graph.
338 if (batch_depends_on(dep
, batch
)) {
339 DBG("%p: flush forced on %p!", batch
, dep
);
340 mtx_unlock(&batch
->ctx
->screen
->lock
);
341 fd_batch_flush(dep
, false);
342 mtx_lock(&batch
->ctx
->screen
->lock
);
344 struct fd_batch
*other
= NULL
;
345 fd_batch_reference_locked(&other
, dep
);
346 batch
->dependents_mask
|= (1 << dep
->idx
);
347 DBG("%p: added dependency on %p", batch
, dep
);
352 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
354 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
357 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
359 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
361 /* note, invalidate write batch, to avoid further writes to rsc
362 * resulting in a write-after-read hazard.
366 /* if we are pending read or write by any other batch: */
367 if (rsc
->batch_mask
!= (1 << batch
->idx
)) {
368 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
369 struct fd_batch
*dep
;
370 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
371 struct fd_batch
*b
= NULL
;
372 /* note that batch_add_dep could flush and unref dep, so
373 * we need to hold a reference to keep it live for the
374 * fd_bc_invalidate_batch()
376 fd_batch_reference(&b
, dep
);
377 batch_add_dep(batch
, b
);
378 fd_bc_invalidate_batch(b
, false);
379 fd_batch_reference_locked(&b
, NULL
);
382 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
384 if (rsc
->write_batch
) {
385 batch_add_dep(batch
, rsc
->write_batch
);
386 fd_bc_invalidate_batch(rsc
->write_batch
, false);
390 if (rsc
->batch_mask
& (1 << batch
->idx
))
393 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
395 _mesa_set_add(batch
->resources
, rsc
);
396 rsc
->batch_mask
|= (1 << batch
->idx
);
400 fd_batch_check_size(struct fd_batch
*batch
)
402 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
405 struct fd_ringbuffer
*ring
= batch
->draw
;
406 if (((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000)) ||
407 (fd_mesa_debug
& FD_DBG_FLUSH
))
408 fd_batch_flush(batch
, true);
411 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
412 * been one since last draw:
415 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
417 if (batch
->needs_wfi
) {
418 if (batch
->ctx
->screen
->gpu_id
>= 500)
422 batch
->needs_wfi
= false;