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
) {
55 batch
->draw
= fd_ringbuffer_new(ctx
->screen
->pipe
, size
);
56 batch
->binning
= fd_ringbuffer_new(ctx
->screen
->pipe
, size
);
57 batch
->gmem
= fd_ringbuffer_new(ctx
->screen
->pipe
, size
);
59 fd_ringbuffer_set_parent(batch
->gmem
, NULL
);
60 fd_ringbuffer_set_parent(batch
->draw
, batch
->gmem
);
61 fd_ringbuffer_set_parent(batch
->binning
, batch
->gmem
);
63 batch
->in_fence_fd
= -1;
65 batch
->cleared
= batch
->partial_cleared
= 0;
66 batch
->restore
= batch
->resolve
= 0;
67 batch
->needs_flush
= false;
68 batch
->gmem_reason
= 0;
70 batch
->stage
= FD_STAGE_NULL
;
74 /* reset maximal bounds: */
75 batch
->max_scissor
.minx
= batch
->max_scissor
.miny
= ~0;
76 batch
->max_scissor
.maxx
= batch
->max_scissor
.maxy
= 0;
78 util_dynarray_init(&batch
->draw_patches
);
80 if (is_a3xx(ctx
->screen
))
81 util_dynarray_init(&batch
->rbrc_patches
);
83 assert(batch
->resources
->entries
== 0);
85 util_dynarray_init(&batch
->samples
);
89 fd_batch_create(struct fd_context
*ctx
)
91 struct fd_batch
*batch
= CALLOC_STRUCT(fd_batch
);
98 pipe_reference_init(&batch
->reference
, 1);
101 batch
->resources
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
102 _mesa_key_pointer_equal
);
110 batch_fini(struct fd_batch
*batch
)
112 pipe_resource_reference(&batch
->query_buf
, NULL
);
114 if (batch
->in_fence_fd
!= -1)
115 close(batch
->in_fence_fd
);
117 fd_ringbuffer_del(batch
->draw
);
118 fd_ringbuffer_del(batch
->binning
);
119 fd_ringbuffer_del(batch
->gmem
);
121 util_dynarray_fini(&batch
->draw_patches
);
123 if (is_a3xx(batch
->ctx
->screen
))
124 util_dynarray_fini(&batch
->rbrc_patches
);
126 while (batch
->samples
.size
> 0) {
127 struct fd_hw_sample
*samp
=
128 util_dynarray_pop(&batch
->samples
, struct fd_hw_sample
*);
129 fd_hw_sample_reference(batch
->ctx
, &samp
, NULL
);
131 util_dynarray_fini(&batch
->samples
);
133 if (batch
->ctx
->screen
->reorder
)
134 util_queue_fence_destroy(&batch
->flush_fence
);
138 batch_flush_reset_dependencies(struct fd_batch
*batch
, bool flush
)
140 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
141 struct fd_batch
*dep
;
143 foreach_batch(dep
, cache
, batch
->dependents_mask
) {
145 fd_batch_flush(dep
, false);
146 fd_batch_reference(&dep
, NULL
);
149 batch
->dependents_mask
= 0;
153 batch_reset_resources_locked(struct fd_batch
*batch
)
155 struct set_entry
*entry
;
157 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
159 set_foreach(batch
->resources
, entry
) {
160 struct fd_resource
*rsc
= (struct fd_resource
*)entry
->key
;
161 _mesa_set_remove(batch
->resources
, entry
);
162 debug_assert(rsc
->batch_mask
& (1 << batch
->idx
));
163 rsc
->batch_mask
&= ~(1 << batch
->idx
);
164 if (rsc
->write_batch
== batch
)
165 fd_batch_reference_locked(&rsc
->write_batch
, NULL
);
170 batch_reset_resources(struct fd_batch
*batch
)
172 pipe_mutex_lock(batch
->ctx
->screen
->lock
);
173 batch_reset_resources_locked(batch
);
174 pipe_mutex_unlock(batch
->ctx
->screen
->lock
);
178 batch_reset(struct fd_batch
*batch
)
182 fd_batch_sync(batch
);
184 batch_flush_reset_dependencies(batch
, false);
185 batch_reset_resources(batch
);
192 fd_batch_reset(struct fd_batch
*batch
)
194 if (batch
->needs_flush
)
199 __fd_batch_destroy(struct fd_batch
*batch
)
203 util_copy_framebuffer_state(&batch
->framebuffer
, NULL
);
205 pipe_mutex_lock(batch
->ctx
->screen
->lock
);
206 fd_bc_invalidate_batch(batch
, true);
207 pipe_mutex_unlock(batch
->ctx
->screen
->lock
);
211 batch_reset_resources(batch
);
212 debug_assert(batch
->resources
->entries
== 0);
213 _mesa_set_destroy(batch
->resources
, NULL
);
215 batch_flush_reset_dependencies(batch
, false);
216 debug_assert(batch
->dependents_mask
== 0);
222 __fd_batch_describe(char* buf
, const struct fd_batch
*batch
)
224 util_sprintf(buf
, "fd_batch<%u>", batch
->seqno
);
228 fd_batch_sync(struct fd_batch
*batch
)
230 if (!batch
->ctx
->screen
->reorder
)
232 util_queue_job_wait(&batch
->flush_fence
);
236 batch_flush_func(void *job
, int id
)
238 struct fd_batch
*batch
= job
;
240 fd_gmem_render_tiles(batch
);
241 batch_reset_resources(batch
);
245 batch_cleanup_func(void *job
, int id
)
247 struct fd_batch
*batch
= job
;
248 fd_batch_reference(&batch
, NULL
);
252 batch_flush(struct fd_batch
*batch
)
254 DBG("%p: needs_flush=%d", batch
, batch
->needs_flush
);
256 if (!batch
->needs_flush
)
259 batch
->needs_flush
= false;
261 /* close out the draw cmds by making sure any active queries are
264 fd_hw_query_set_stage(batch
, batch
->draw
, FD_STAGE_NULL
);
266 batch
->ctx
->dirty
= ~0;
267 batch_flush_reset_dependencies(batch
, true);
269 if (batch
->ctx
->screen
->reorder
) {
270 struct fd_batch
*tmp
= NULL
;
271 fd_batch_reference(&tmp
, batch
);
273 if (!util_queue_is_initialized(&batch
->ctx
->flush_queue
))
274 util_queue_init(&batch
->ctx
->flush_queue
, "flush_queue", 16, 1);
276 util_queue_add_job(&batch
->ctx
->flush_queue
,
277 batch
, &batch
->flush_fence
,
278 batch_flush_func
, batch_cleanup_func
);
280 fd_gmem_render_tiles(batch
);
281 batch_reset_resources(batch
);
284 debug_assert(batch
->reference
.count
> 0);
286 if (batch
== batch
->ctx
->batch
) {
289 pipe_mutex_lock(batch
->ctx
->screen
->lock
);
290 fd_bc_invalidate_batch(batch
, false);
291 pipe_mutex_unlock(batch
->ctx
->screen
->lock
);
295 /* NOTE: could drop the last ref to batch */
297 fd_batch_flush(struct fd_batch
*batch
, bool sync
)
299 /* NOTE: we need to hold an extra ref across the body of flush,
300 * since the last ref to this batch could be dropped when cleaning
303 struct fd_batch
*tmp
= NULL
;
304 fd_batch_reference(&tmp
, batch
);
308 fd_batch_reference(&tmp
, NULL
);
311 /* does 'batch' depend directly or indirectly on 'other' ? */
313 batch_depends_on(struct fd_batch
*batch
, struct fd_batch
*other
)
315 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
316 struct fd_batch
*dep
;
318 if (batch
->dependents_mask
& (1 << other
->idx
))
321 foreach_batch(dep
, cache
, batch
->dependents_mask
)
322 if (batch_depends_on(batch
, dep
))
329 batch_add_dep(struct fd_batch
*batch
, struct fd_batch
*dep
)
331 if (batch
->dependents_mask
& (1 << dep
->idx
))
334 /* if the new depedency already depends on us, we need to flush
335 * to avoid a loop in the dependency graph.
337 if (batch_depends_on(dep
, batch
)) {
338 DBG("%p: flush forced on %p!", batch
, dep
);
339 pipe_mutex_unlock(batch
->ctx
->screen
->lock
);
340 fd_batch_flush(dep
, false);
341 pipe_mutex_lock(batch
->ctx
->screen
->lock
);
343 struct fd_batch
*other
= NULL
;
344 fd_batch_reference_locked(&other
, dep
);
345 batch
->dependents_mask
|= (1 << dep
->idx
);
346 DBG("%p: added dependency on %p", batch
, dep
);
351 fd_batch_resource_used(struct fd_batch
*batch
, struct fd_resource
*rsc
, bool write
)
353 pipe_mutex_assert_locked(batch
->ctx
->screen
->lock
);
356 fd_batch_resource_used(batch
, rsc
->stencil
, write
);
358 DBG("%p: %s %p", batch
, write
? "write" : "read", rsc
);
360 /* note, invalidate write batch, to avoid further writes to rsc
361 * resulting in a write-after-read hazard.
365 /* if we are pending read or write by any other batch: */
366 if (rsc
->batch_mask
!= (1 << batch
->idx
)) {
367 struct fd_batch_cache
*cache
= &batch
->ctx
->screen
->batch_cache
;
368 struct fd_batch
*dep
;
369 foreach_batch(dep
, cache
, rsc
->batch_mask
) {
370 struct fd_batch
*b
= NULL
;
371 /* note that batch_add_dep could flush and unref dep, so
372 * we need to hold a reference to keep it live for the
373 * fd_bc_invalidate_batch()
375 fd_batch_reference(&b
, dep
);
376 batch_add_dep(batch
, b
);
377 fd_bc_invalidate_batch(b
, false);
378 fd_batch_reference_locked(&b
, NULL
);
381 fd_batch_reference_locked(&rsc
->write_batch
, batch
);
383 if (rsc
->write_batch
) {
384 batch_add_dep(batch
, rsc
->write_batch
);
385 fd_bc_invalidate_batch(rsc
->write_batch
, false);
389 if (rsc
->batch_mask
& (1 << batch
->idx
))
392 debug_assert(!_mesa_set_search(batch
->resources
, rsc
));
394 _mesa_set_add(batch
->resources
, rsc
);
395 rsc
->batch_mask
|= (1 << batch
->idx
);
399 fd_batch_check_size(struct fd_batch
*batch
)
401 if (fd_device_version(batch
->ctx
->screen
->dev
) >= FD_VERSION_UNLIMITED_CMDS
)
404 struct fd_ringbuffer
*ring
= batch
->draw
;
405 if (((ring
->cur
- ring
->start
) > (ring
->size
/4 - 0x1000)) ||
406 (fd_mesa_debug
& FD_DBG_FLUSH
))
407 fd_batch_flush(batch
, true);
410 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
411 * been one since last draw:
414 fd_wfi(struct fd_batch
*batch
, struct fd_ringbuffer
*ring
)
416 if (batch
->needs_wfi
) {
417 if (batch
->ctx
->screen
->gpu_id
>= 500)
421 batch
->needs_wfi
= false;