2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include "drm-uapi/panfrost_drm.h"
31 #include "pan_context.h"
32 #include "util/hash_table.h"
33 #include "util/ralloc.h"
34 #include "util/format/u_format.h"
35 #include "util/u_pack_color.h"
37 #include "pandecode/decode.h"
38 #include "panfrost-quirks.h"
40 /* panfrost_bo_access is here to help us keep track of batch accesses to BOs
41 * and build a proper dependency graph such that batches can be pipelined for
42 * better GPU utilization.
44 * Each accessed BO has a corresponding entry in the ->accessed_bos hash table.
45 * A BO is either being written or read at any time, that's what the type field
47 * When the last access is a write, the batch writing the BO might have read
48 * dependencies (readers that have not been executed yet and want to read the
49 * previous BO content), and when the last access is a read, all readers might
50 * depend on another batch to push its results to memory. That's what the
51 * readers/writers keep track off.
52 * There can only be one writer at any given time, if a new batch wants to
53 * write to the same BO, a dependency will be added between the new writer and
54 * the old writer (at the batch level), and panfrost_bo_access->writer will be
55 * updated to point to the new writer.
57 struct panfrost_bo_access
{
59 struct util_dynarray readers
;
60 struct panfrost_batch_fence
*writer
;
63 static struct panfrost_batch_fence
*
64 panfrost_create_batch_fence(struct panfrost_batch
*batch
)
66 struct panfrost_batch_fence
*fence
;
69 fence
= rzalloc(NULL
, struct panfrost_batch_fence
);
71 pipe_reference_init(&fence
->reference
, 1);
72 fence
->ctx
= batch
->ctx
;
74 ret
= drmSyncobjCreate(pan_screen(batch
->ctx
->base
.screen
)->fd
, 0,
82 panfrost_free_batch_fence(struct panfrost_batch_fence
*fence
)
84 drmSyncobjDestroy(pan_screen(fence
->ctx
->base
.screen
)->fd
,
90 panfrost_batch_fence_unreference(struct panfrost_batch_fence
*fence
)
92 if (pipe_reference(&fence
->reference
, NULL
))
93 panfrost_free_batch_fence(fence
);
97 panfrost_batch_fence_reference(struct panfrost_batch_fence
*fence
)
99 pipe_reference(NULL
, &fence
->reference
);
102 static struct panfrost_batch
*
103 panfrost_create_batch(struct panfrost_context
*ctx
,
104 const struct pipe_framebuffer_state
*key
)
106 struct panfrost_batch
*batch
= rzalloc(ctx
, struct panfrost_batch
);
110 batch
->bos
= _mesa_hash_table_create(batch
, _mesa_hash_pointer
,
111 _mesa_key_pointer_equal
);
113 batch
->minx
= batch
->miny
= ~0;
114 batch
->maxx
= batch
->maxy
= 0;
115 batch
->transient_offset
= 0;
117 util_dynarray_init(&batch
->headers
, batch
);
118 util_dynarray_init(&batch
->gpu_headers
, batch
);
119 util_dynarray_init(&batch
->dependencies
, batch
);
120 batch
->out_sync
= panfrost_create_batch_fence(batch
);
121 util_copy_framebuffer_state(&batch
->key
, key
);
127 panfrost_freeze_batch(struct panfrost_batch
*batch
)
129 struct panfrost_context
*ctx
= batch
->ctx
;
130 struct hash_entry
*entry
;
132 /* Remove the entry in the FBO -> batch hash table if the batch
133 * matches. This way, next draws/clears targeting this FBO will trigger
134 * the creation of a new batch.
136 entry
= _mesa_hash_table_search(ctx
->batches
, &batch
->key
);
137 if (entry
&& entry
->data
== batch
)
138 _mesa_hash_table_remove(ctx
->batches
, entry
);
140 /* If this is the bound batch, the panfrost_context parameters are
141 * relevant so submitting it invalidates those parameters, but if it's
142 * not bound, the context parameters are for some other batch so we
143 * can't invalidate them.
145 if (ctx
->batch
== batch
) {
146 panfrost_invalidate_frame(ctx
);
152 static bool panfrost_batch_is_frozen(struct panfrost_batch
*batch
)
154 struct panfrost_context
*ctx
= batch
->ctx
;
155 struct hash_entry
*entry
;
157 entry
= _mesa_hash_table_search(ctx
->batches
, &batch
->key
);
158 if (entry
&& entry
->data
== batch
)
161 if (ctx
->batch
== batch
)
169 panfrost_free_batch(struct panfrost_batch
*batch
)
174 assert(panfrost_batch_is_frozen(batch
));
176 hash_table_foreach(batch
->bos
, entry
)
177 panfrost_bo_unreference((struct panfrost_bo
*)entry
->key
);
179 util_dynarray_foreach(&batch
->dependencies
,
180 struct panfrost_batch_fence
*, dep
) {
181 panfrost_batch_fence_unreference(*dep
);
184 /* The out_sync fence lifetime is different from the the batch one
185 * since other batches might want to wait on a fence of already
186 * submitted/signaled batch. All we need to do here is make sure the
187 * fence does not point to an invalid batch, which the core will
188 * interpret as 'batch is already submitted'.
190 batch
->out_sync
->batch
= NULL
;
191 panfrost_batch_fence_unreference(batch
->out_sync
);
193 util_unreference_framebuffer_state(&batch
->key
);
199 panfrost_dep_graph_contains_batch(struct panfrost_batch
*root
,
200 struct panfrost_batch
*batch
)
205 util_dynarray_foreach(&root
->dependencies
,
206 struct panfrost_batch_fence
*, dep
) {
207 if ((*dep
)->batch
== batch
||
208 panfrost_dep_graph_contains_batch((*dep
)->batch
, batch
))
217 panfrost_batch_add_dep(struct panfrost_batch
*batch
,
218 struct panfrost_batch_fence
*newdep
)
220 if (batch
== newdep
->batch
)
223 /* We might want to turn ->dependencies into a set if the number of
224 * deps turns out to be big enough to make this 'is dep already there'
225 * search inefficient.
227 util_dynarray_foreach(&batch
->dependencies
,
228 struct panfrost_batch_fence
*, dep
) {
233 /* Make sure the dependency graph is acyclic. */
234 assert(!panfrost_dep_graph_contains_batch(newdep
->batch
, batch
));
236 panfrost_batch_fence_reference(newdep
);
237 util_dynarray_append(&batch
->dependencies
,
238 struct panfrost_batch_fence
*, newdep
);
240 /* We now have a batch depending on us, let's make sure new draw/clear
241 * calls targeting the same FBO use a new batch object.
244 panfrost_freeze_batch(newdep
->batch
);
247 static struct panfrost_batch
*
248 panfrost_get_batch(struct panfrost_context
*ctx
,
249 const struct pipe_framebuffer_state
*key
)
251 /* Lookup the job first */
252 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->batches
, key
);
257 /* Otherwise, let's create a job */
259 struct panfrost_batch
*batch
= panfrost_create_batch(ctx
, key
);
261 /* Save the created job */
262 _mesa_hash_table_insert(ctx
->batches
, &batch
->key
, batch
);
267 /* Get the job corresponding to the FBO we're currently rendering into */
269 struct panfrost_batch
*
270 panfrost_get_batch_for_fbo(struct panfrost_context
*ctx
)
272 /* If we're wallpapering, we special case to workaround
275 if (ctx
->wallpaper_batch
)
276 return ctx
->wallpaper_batch
;
278 /* If we already began rendering, use that */
281 assert(util_framebuffer_state_equal(&ctx
->batch
->key
,
282 &ctx
->pipe_framebuffer
));
286 /* If not, look up the job */
287 struct panfrost_batch
*batch
= panfrost_get_batch(ctx
,
288 &ctx
->pipe_framebuffer
);
290 /* Set this job as the current FBO job. Will be reset when updating the
291 * FB state and when submitting or releasing a job.
297 struct panfrost_batch
*
298 panfrost_get_fresh_batch_for_fbo(struct panfrost_context
*ctx
)
300 struct panfrost_batch
*batch
;
302 batch
= panfrost_get_batch(ctx
, &ctx
->pipe_framebuffer
);
304 /* The batch has no draw/clear queued, let's return it directly.
305 * Note that it's perfectly fine to re-use a batch with an
306 * existing clear, we'll just update it with the new clear request.
308 if (!batch
->last_job
.gpu
)
311 /* Otherwise, we need to freeze the existing one and instantiate a new
314 panfrost_freeze_batch(batch
);
315 return panfrost_get_batch(ctx
, &ctx
->pipe_framebuffer
);
319 panfrost_batch_fence_is_signaled(struct panfrost_batch_fence
*fence
)
324 /* Batch has not been submitted yet. */
328 int ret
= drmSyncobjWait(pan_screen(fence
->ctx
->base
.screen
)->fd
,
329 &fence
->syncobj
, 1, 0, 0, NULL
);
331 /* Cache whether the fence was signaled */
332 fence
->signaled
= ret
>= 0;
333 return fence
->signaled
;
337 panfrost_bo_access_gc_fences(struct panfrost_context
*ctx
,
338 struct panfrost_bo_access
*access
,
339 const struct panfrost_bo
*bo
)
341 if (access
->writer
&& panfrost_batch_fence_is_signaled(access
->writer
)) {
342 panfrost_batch_fence_unreference(access
->writer
);
343 access
->writer
= NULL
;
346 unsigned nreaders
= 0;
347 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
352 if (panfrost_batch_fence_is_signaled(*reader
)) {
353 panfrost_batch_fence_unreference(*reader
);
361 util_dynarray_clear(&access
->readers
);
364 /* Collect signaled fences to keep the kernel-side syncobj-map small. The
365 * idea is to collect those signaled fences at the end of each flush_all
366 * call. This function is likely to collect only fences from previous
367 * batch flushes not the one that have just have just been submitted and
368 * are probably still in flight when we trigger the garbage collection.
369 * Anyway, we need to do this garbage collection at some point if we don't
370 * want the BO access map to keep invalid entries around and retain
374 panfrost_gc_fences(struct panfrost_context
*ctx
)
376 hash_table_foreach(ctx
->accessed_bos
, entry
) {
377 struct panfrost_bo_access
*access
= entry
->data
;
380 panfrost_bo_access_gc_fences(ctx
, access
, entry
->key
);
381 if (!util_dynarray_num_elements(&access
->readers
,
382 struct panfrost_batch_fence
*) &&
385 _mesa_hash_table_remove(ctx
->accessed_bos
, entry
);
392 panfrost_batch_in_readers(struct panfrost_batch
*batch
,
393 struct panfrost_bo_access
*access
)
395 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
397 if (*reader
&& (*reader
)->batch
== batch
)
406 panfrost_batch_update_bo_access(struct panfrost_batch
*batch
,
407 struct panfrost_bo
*bo
, uint32_t access_type
,
408 bool already_accessed
)
410 struct panfrost_context
*ctx
= batch
->ctx
;
411 struct panfrost_bo_access
*access
;
412 uint32_t old_access_type
;
413 struct hash_entry
*entry
;
415 assert(access_type
== PAN_BO_ACCESS_WRITE
||
416 access_type
== PAN_BO_ACCESS_READ
);
418 entry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
419 access
= entry
? entry
->data
: NULL
;
421 old_access_type
= access
->type
;
423 access
= rzalloc(ctx
, struct panfrost_bo_access
);
424 util_dynarray_init(&access
->readers
, access
);
425 _mesa_hash_table_insert(ctx
->accessed_bos
, bo
, access
);
426 /* We are the first to access this BO, let's initialize
427 * old_access_type to our own access type in that case.
429 old_access_type
= access_type
;
430 access
->type
= access_type
;
435 if (access_type
== PAN_BO_ACCESS_WRITE
&&
436 old_access_type
== PAN_BO_ACCESS_READ
) {
437 /* Previous access was a read and we want to write this BO.
438 * We first need to add explicit deps between our batch and
439 * the previous readers.
441 util_dynarray_foreach(&access
->readers
,
442 struct panfrost_batch_fence
*, reader
) {
443 /* We were already reading the BO, no need to add a dep
444 * on ourself (the acyclic check would complain about
447 if (!(*reader
) || (*reader
)->batch
== batch
)
450 panfrost_batch_add_dep(batch
, *reader
);
452 panfrost_batch_fence_reference(batch
->out_sync
);
454 /* We now are the new writer. */
455 access
->writer
= batch
->out_sync
;
456 access
->type
= access_type
;
458 /* Release the previous readers and reset the readers array. */
459 util_dynarray_foreach(&access
->readers
,
460 struct panfrost_batch_fence
*,
464 panfrost_batch_fence_unreference(*reader
);
467 util_dynarray_clear(&access
->readers
);
468 } else if (access_type
== PAN_BO_ACCESS_WRITE
&&
469 old_access_type
== PAN_BO_ACCESS_WRITE
) {
470 /* Previous access was a write and we want to write this BO.
471 * First check if we were the previous writer, in that case
472 * there's nothing to do. Otherwise we need to add a
473 * dependency between the new writer and the old one.
475 if (access
->writer
!= batch
->out_sync
) {
476 if (access
->writer
) {
477 panfrost_batch_add_dep(batch
, access
->writer
);
478 panfrost_batch_fence_unreference(access
->writer
);
480 panfrost_batch_fence_reference(batch
->out_sync
);
481 access
->writer
= batch
->out_sync
;
483 } else if (access_type
== PAN_BO_ACCESS_READ
&&
484 old_access_type
== PAN_BO_ACCESS_WRITE
) {
485 /* Previous access was a write and we want to read this BO.
486 * First check if we were the previous writer, in that case
487 * we want to keep the access type unchanged, as a write is
488 * more constraining than a read.
490 if (access
->writer
!= batch
->out_sync
) {
491 /* Add a dependency on the previous writer. */
492 panfrost_batch_add_dep(batch
, access
->writer
);
494 /* The previous access was a write, there's no reason
495 * to have entries in the readers array.
497 assert(!util_dynarray_num_elements(&access
->readers
,
498 struct panfrost_batch_fence
*));
500 /* Add ourselves to the readers array. */
501 panfrost_batch_fence_reference(batch
->out_sync
);
502 util_dynarray_append(&access
->readers
,
503 struct panfrost_batch_fence
*,
505 access
->type
= PAN_BO_ACCESS_READ
;
508 /* We already accessed this BO before, so we should already be
509 * in the reader array.
511 if (already_accessed
) {
512 assert(panfrost_batch_in_readers(batch
, access
));
516 /* Previous access was a read and we want to read this BO.
517 * Add ourselves to the readers array and add a dependency on
518 * the previous writer if any.
520 panfrost_batch_fence_reference(batch
->out_sync
);
521 util_dynarray_append(&access
->readers
,
522 struct panfrost_batch_fence
*,
526 panfrost_batch_add_dep(batch
, access
->writer
);
531 panfrost_batch_add_bo(struct panfrost_batch
*batch
, struct panfrost_bo
*bo
,
537 struct hash_entry
*entry
;
538 uint32_t old_flags
= 0;
540 entry
= _mesa_hash_table_search(batch
->bos
, bo
);
542 entry
= _mesa_hash_table_insert(batch
->bos
, bo
,
543 (void *)(uintptr_t)flags
);
544 panfrost_bo_reference(bo
);
546 old_flags
= (uintptr_t)entry
->data
;
548 /* All batches have to agree on the shared flag. */
549 assert((old_flags
& PAN_BO_ACCESS_SHARED
) ==
550 (flags
& PAN_BO_ACCESS_SHARED
));
555 if (old_flags
== flags
)
559 entry
->data
= (void *)(uintptr_t)flags
;
561 /* If this is not a shared BO, we don't really care about dependency
564 if (!(flags
& PAN_BO_ACCESS_SHARED
))
567 /* All dependencies should have been flushed before we execute the
568 * wallpaper draw, so it should be harmless to skip the
569 * update_bo_access() call.
571 if (batch
== batch
->ctx
->wallpaper_batch
)
574 /* Only pass R/W flags to the dep tracking logic. */
575 assert(flags
& PAN_BO_ACCESS_RW
);
576 flags
= (flags
& PAN_BO_ACCESS_WRITE
) ?
577 PAN_BO_ACCESS_WRITE
: PAN_BO_ACCESS_READ
;
578 panfrost_batch_update_bo_access(batch
, bo
, flags
, old_flags
!= 0);
581 void panfrost_batch_add_fbo_bos(struct panfrost_batch
*batch
)
583 uint32_t flags
= PAN_BO_ACCESS_SHARED
| PAN_BO_ACCESS_WRITE
|
584 PAN_BO_ACCESS_VERTEX_TILER
|
585 PAN_BO_ACCESS_FRAGMENT
;
587 for (unsigned i
= 0; i
< batch
->key
.nr_cbufs
; ++i
) {
588 struct panfrost_resource
*rsrc
= pan_resource(batch
->key
.cbufs
[i
]->texture
);
589 panfrost_batch_add_bo(batch
, rsrc
->bo
, flags
);
592 if (batch
->key
.zsbuf
) {
593 struct panfrost_resource
*rsrc
= pan_resource(batch
->key
.zsbuf
->texture
);
594 panfrost_batch_add_bo(batch
, rsrc
->bo
, flags
);
599 panfrost_batch_create_bo(struct panfrost_batch
*batch
, size_t size
,
600 uint32_t create_flags
, uint32_t access_flags
)
602 struct panfrost_bo
*bo
;
604 bo
= panfrost_bo_create(pan_screen(batch
->ctx
->base
.screen
), size
,
606 panfrost_batch_add_bo(batch
, bo
, access_flags
);
608 /* panfrost_batch_add_bo() has retained a reference and
609 * panfrost_bo_create() initialize the refcnt to 1, so let's
610 * unreference the BO here so it gets released when the batch is
611 * destroyed (unless it's retained by someone else in the meantime).
613 panfrost_bo_unreference(bo
);
617 /* Returns the polygon list's GPU address if available, or otherwise allocates
618 * the polygon list. It's perfectly fast to use allocate/free BO directly,
619 * since we'll hit the BO cache and this is one-per-batch anyway. */
622 panfrost_batch_get_polygon_list(struct panfrost_batch
*batch
, unsigned size
)
624 if (batch
->polygon_list
) {
625 assert(batch
->polygon_list
->size
>= size
);
627 /* Create the BO as invisible, as there's no reason to map */
628 size
= util_next_power_of_two(size
);
630 batch
->polygon_list
= panfrost_batch_create_bo(batch
, size
,
632 PAN_BO_ACCESS_PRIVATE
|
634 PAN_BO_ACCESS_VERTEX_TILER
|
635 PAN_BO_ACCESS_FRAGMENT
);
638 return batch
->polygon_list
->gpu
;
642 panfrost_batch_get_scratchpad(struct panfrost_batch
*batch
,
644 unsigned thread_tls_alloc
,
647 unsigned size
= panfrost_get_total_stack_size(shift
,
651 if (batch
->scratchpad
) {
652 assert(batch
->scratchpad
->size
>= size
);
654 batch
->scratchpad
= panfrost_batch_create_bo(batch
, size
,
656 PAN_BO_ACCESS_PRIVATE
|
658 PAN_BO_ACCESS_VERTEX_TILER
|
659 PAN_BO_ACCESS_FRAGMENT
);
662 return batch
->scratchpad
;
666 panfrost_batch_get_tiler_heap(struct panfrost_batch
*batch
)
668 if (batch
->tiler_heap
)
669 return batch
->tiler_heap
;
671 batch
->tiler_heap
= panfrost_batch_create_bo(batch
, 4096 * 4096,
674 PAN_BO_ACCESS_PRIVATE
|
676 PAN_BO_ACCESS_VERTEX_TILER
|
677 PAN_BO_ACCESS_FRAGMENT
);
678 assert(batch
->tiler_heap
);
679 return batch
->tiler_heap
;
683 panfrost_batch_get_tiler_dummy(struct panfrost_batch
*batch
)
685 struct panfrost_screen
*screen
= pan_screen(batch
->ctx
->base
.screen
);
687 uint32_t create_flags
= 0;
689 if (batch
->tiler_dummy
)
690 return batch
->tiler_dummy
;
692 if (!(screen
->quirks
& MIDGARD_NO_HIER_TILING
))
693 create_flags
= PAN_BO_INVISIBLE
;
695 batch
->tiler_dummy
= panfrost_batch_create_bo(batch
, 4096,
697 PAN_BO_ACCESS_PRIVATE
|
699 PAN_BO_ACCESS_VERTEX_TILER
|
700 PAN_BO_ACCESS_FRAGMENT
);
701 assert(batch
->tiler_dummy
);
702 return batch
->tiler_dummy
;
706 panfrost_batch_draw_wallpaper(struct panfrost_batch
*batch
)
708 /* Color 0 is cleared, no need to draw the wallpaper.
709 * TODO: MRT wallpapers.
711 if (batch
->clear
& PIPE_CLEAR_COLOR0
)
714 /* Nothing to reload? TODO: MRT wallpapers */
715 if (batch
->key
.cbufs
[0] == NULL
)
718 /* No draw calls, and no clear on the depth/stencil bufs.
719 * Drawing the wallpaper would be useless.
721 if (!batch
->last_tiler
.gpu
&&
722 !(batch
->clear
& PIPE_CLEAR_DEPTHSTENCIL
))
725 /* Check if the buffer has any content on it worth preserving */
727 struct pipe_surface
*surf
= batch
->key
.cbufs
[0];
728 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
729 unsigned level
= surf
->u
.tex
.level
;
731 if (!rsrc
->slices
[level
].initialized
)
734 batch
->ctx
->wallpaper_batch
= batch
;
736 /* Clamp the rendering area to the damage extent. The
737 * KHR_partial_update() spec states that trying to render outside of
738 * the damage region is "undefined behavior", so we should be safe.
740 unsigned damage_width
= (rsrc
->damage
.extent
.maxx
- rsrc
->damage
.extent
.minx
);
741 unsigned damage_height
= (rsrc
->damage
.extent
.maxy
- rsrc
->damage
.extent
.miny
);
743 if (damage_width
&& damage_height
) {
744 panfrost_batch_intersection_scissor(batch
,
745 rsrc
->damage
.extent
.minx
,
746 rsrc
->damage
.extent
.miny
,
747 rsrc
->damage
.extent
.maxx
,
748 rsrc
->damage
.extent
.maxy
);
751 /* FIXME: Looks like aligning on a tile is not enough, but
752 * aligning on twice the tile size seems to works. We don't
753 * know exactly what happens here but this deserves extra
754 * investigation to figure it out.
756 batch
->minx
= batch
->minx
& ~((MALI_TILE_LENGTH
* 2) - 1);
757 batch
->miny
= batch
->miny
& ~((MALI_TILE_LENGTH
* 2) - 1);
758 batch
->maxx
= MIN2(ALIGN_POT(batch
->maxx
, MALI_TILE_LENGTH
* 2),
760 batch
->maxy
= MIN2(ALIGN_POT(batch
->maxy
, MALI_TILE_LENGTH
* 2),
763 struct pipe_scissor_state damage
;
764 struct pipe_box rects
[4];
766 /* Clamp the damage box to the rendering area. */
767 damage
.minx
= MAX2(batch
->minx
, rsrc
->damage
.biggest_rect
.x
);
768 damage
.miny
= MAX2(batch
->miny
, rsrc
->damage
.biggest_rect
.y
);
769 damage
.maxx
= MIN2(batch
->maxx
,
770 rsrc
->damage
.biggest_rect
.x
+
771 rsrc
->damage
.biggest_rect
.width
);
772 damage
.maxy
= MIN2(batch
->maxy
,
773 rsrc
->damage
.biggest_rect
.y
+
774 rsrc
->damage
.biggest_rect
.height
);
776 /* One damage rectangle means we can end up with at most 4 reload
778 * 1: left region, only exists if damage.x > 0
779 * 2: right region, only exists if damage.x + damage.width < fb->width
780 * 3: top region, only exists if damage.y > 0. The intersection with
781 * the left and right regions are dropped
782 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
783 * The intersection with the left and right regions are dropped
785 * ____________________________
792 * |_______|___________|______|
794 u_box_2d(batch
->minx
, batch
->miny
, damage
.minx
- batch
->minx
,
795 batch
->maxy
- batch
->miny
, &rects
[0]);
796 u_box_2d(damage
.maxx
, batch
->miny
, batch
->maxx
- damage
.maxx
,
797 batch
->maxy
- batch
->miny
, &rects
[1]);
798 u_box_2d(damage
.minx
, batch
->miny
, damage
.maxx
- damage
.minx
,
799 damage
.miny
- batch
->miny
, &rects
[2]);
800 u_box_2d(damage
.minx
, damage
.maxy
, damage
.maxx
- damage
.minx
,
801 batch
->maxy
- damage
.maxy
, &rects
[3]);
803 for (unsigned i
= 0; i
< 4; i
++) {
804 /* Width and height are always >= 0 even if width is declared as a
805 * signed integer: u_box_2d() helper takes unsigned args and
806 * panfrost_set_damage_region() is taking care of clamping
809 if (!rects
[i
].width
|| !rects
[i
].height
)
812 /* Blit the wallpaper in */
813 panfrost_blit_wallpaper(batch
->ctx
, &rects
[i
]);
815 batch
->ctx
->wallpaper_batch
= NULL
;
819 panfrost_batch_submit_ioctl(struct panfrost_batch
*batch
,
820 mali_ptr first_job_desc
,
822 struct mali_job_descriptor_header
*header
)
824 struct panfrost_context
*ctx
= batch
->ctx
;
825 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
826 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
827 struct drm_panfrost_submit submit
= {0,};
828 uint32_t *bo_handles
, *in_syncs
= NULL
;
829 bool is_fragment_shader
;
832 is_fragment_shader
= (reqs
& PANFROST_JD_REQ_FS
) && batch
->first_job
.gpu
;
833 if (is_fragment_shader
)
834 submit
.in_sync_count
= 1;
836 submit
.in_sync_count
= util_dynarray_num_elements(&batch
->dependencies
,
837 struct panfrost_batch_fence
*);
839 if (submit
.in_sync_count
) {
840 in_syncs
= calloc(submit
.in_sync_count
, sizeof(*in_syncs
));
844 /* The fragment job always depends on the vertex/tiler job if there's
847 if (is_fragment_shader
) {
848 in_syncs
[0] = batch
->out_sync
->syncobj
;
852 util_dynarray_foreach(&batch
->dependencies
,
853 struct panfrost_batch_fence
*, dep
)
854 in_syncs
[i
++] = (*dep
)->syncobj
;
857 submit
.in_syncs
= (uintptr_t)in_syncs
;
858 submit
.out_sync
= batch
->out_sync
->syncobj
;
859 submit
.jc
= first_job_desc
;
860 submit
.requirements
= reqs
;
862 bo_handles
= calloc(batch
->bos
->entries
, sizeof(*bo_handles
));
865 hash_table_foreach(batch
->bos
, entry
) {
866 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
867 uint32_t flags
= (uintptr_t)entry
->data
;
869 assert(bo
->gem_handle
> 0);
870 bo_handles
[submit
.bo_handle_count
++] = bo
->gem_handle
;
872 /* Update the BO access flags so that panfrost_bo_wait() knows
873 * about all pending accesses.
874 * We only keep the READ/WRITE info since this is all the BO
875 * wait logic cares about.
876 * We also preserve existing flags as this batch might not
877 * be the first one to access the BO.
879 bo
->gpu_access
|= flags
& (PAN_BO_ACCESS_RW
);
882 submit
.bo_handles
= (u64
) (uintptr_t) bo_handles
;
883 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_SUBMIT
, &submit
);
888 fprintf(stderr
, "Error submitting: %m\n");
892 if (pan_debug
& PAN_DBG_SYNC
) {
895 /* Wait so we can get errors reported back */
896 drmSyncobjWait(screen
->fd
, &batch
->out_sync
->syncobj
, 1,
899 status
= header
->exception_status
;
901 if (status
&& status
!= 0x1) {
902 fprintf(stderr
, "Job %" PRIx64
" failed: source ID: 0x%x access: %s exception: 0x%x (exception_status 0x%x) fault_pointer 0x%" PRIx64
" \n",
904 (status
>> 16) & 0xFFFF,
905 pandecode_exception_access((status
>> 8) & 0x3),
908 header
->fault_pointer
);
912 /* Trace the job if we're doing that */
913 if (pan_debug
& PAN_DBG_TRACE
) {
914 /* Wait so we can get errors reported back */
915 drmSyncobjWait(screen
->fd
, &batch
->out_sync
->syncobj
, 1,
917 pandecode_jc(submit
.jc
, FALSE
, screen
->gpu_id
);
924 panfrost_batch_submit_jobs(struct panfrost_batch
*batch
)
926 bool has_draws
= batch
->first_job
.gpu
;
927 struct mali_job_descriptor_header
*header
;
931 header
= (struct mali_job_descriptor_header
*)batch
->first_job
.cpu
;
932 ret
= panfrost_batch_submit_ioctl(batch
, batch
->first_job
.gpu
, 0, header
);
936 if (batch
->first_tiler
.gpu
|| batch
->clear
) {
937 mali_ptr fragjob
= panfrost_fragment_job(batch
, has_draws
, &header
);
939 ret
= panfrost_batch_submit_ioctl(batch
, fragjob
, PANFROST_JD_REQ_FS
, header
);
947 panfrost_batch_submit(struct panfrost_batch
*batch
)
951 /* Submit the dependencies first. */
952 util_dynarray_foreach(&batch
->dependencies
,
953 struct panfrost_batch_fence
*, dep
) {
955 panfrost_batch_submit((*dep
)->batch
);
961 if (!batch
->last_job
.gpu
&& !batch
->clear
) {
962 /* Mark the fence as signaled so the fence logic does not try
965 batch
->out_sync
->signaled
= true;
969 panfrost_batch_draw_wallpaper(batch
);
971 /* Now that all draws are in, we can finally prepare the
972 * FBD for the batch */
974 if (batch
->framebuffer
.gpu
&& batch
->first_job
.gpu
) {
975 struct panfrost_context
*ctx
= batch
->ctx
;
976 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
977 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
979 if (screen
->quirks
& MIDGARD_SFBD
)
980 panfrost_attach_sfbd(batch
, ~0);
982 panfrost_attach_mfbd(batch
, ~0);
985 panfrost_scoreboard_link_batch(batch
);
987 ret
= panfrost_batch_submit_jobs(batch
);
990 fprintf(stderr
, "panfrost_batch_submit failed: %d\n", ret
);
992 /* We must reset the damage info of our render targets here even
993 * though a damage reset normally happens when the DRI layer swaps
994 * buffers. That's because there can be implicit flushes the GL
995 * app is not aware of, and those might impact the damage region: if
996 * part of the damaged portion is drawn during those implicit flushes,
997 * you have to reload those areas before next draws are pushed, and
998 * since the driver can't easily know what's been modified by the draws
999 * it flushed, the easiest solution is to reload everything.
1001 for (unsigned i
= 0; i
< batch
->key
.nr_cbufs
; i
++) {
1002 struct panfrost_resource
*res
;
1004 if (!batch
->key
.cbufs
[i
])
1007 res
= pan_resource(batch
->key
.cbufs
[i
]->texture
);
1008 panfrost_resource_reset_damage(res
);
1012 panfrost_freeze_batch(batch
);
1013 panfrost_free_batch(batch
);
1017 panfrost_flush_all_batches(struct panfrost_context
*ctx
, bool wait
)
1019 struct util_dynarray fences
, syncobjs
;
1022 util_dynarray_init(&fences
, NULL
);
1023 util_dynarray_init(&syncobjs
, NULL
);
1026 hash_table_foreach(ctx
->batches
, hentry
) {
1027 struct panfrost_batch
*batch
= hentry
->data
;
1032 panfrost_batch_fence_reference(batch
->out_sync
);
1033 util_dynarray_append(&fences
, struct panfrost_batch_fence
*,
1035 util_dynarray_append(&syncobjs
, uint32_t,
1036 batch
->out_sync
->syncobj
);
1039 panfrost_batch_submit(batch
);
1042 assert(!ctx
->batches
->entries
);
1044 /* Collect batch fences before returning */
1045 panfrost_gc_fences(ctx
);
1050 drmSyncobjWait(pan_screen(ctx
->base
.screen
)->fd
,
1051 util_dynarray_begin(&syncobjs
),
1052 util_dynarray_num_elements(&syncobjs
, uint32_t),
1053 INT64_MAX
, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
, NULL
);
1055 util_dynarray_foreach(&fences
, struct panfrost_batch_fence
*, fence
)
1056 panfrost_batch_fence_unreference(*fence
);
1058 util_dynarray_fini(&fences
);
1059 util_dynarray_fini(&syncobjs
);
1063 panfrost_pending_batches_access_bo(struct panfrost_context
*ctx
,
1064 const struct panfrost_bo
*bo
)
1066 struct panfrost_bo_access
*access
;
1067 struct hash_entry
*hentry
;
1069 hentry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
1070 access
= hentry
? hentry
->data
: NULL
;
1074 if (access
->writer
&& access
->writer
->batch
)
1077 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
1079 if (*reader
&& (*reader
)->batch
)
1087 panfrost_flush_batches_accessing_bo(struct panfrost_context
*ctx
,
1088 struct panfrost_bo
*bo
,
1089 uint32_t access_type
)
1091 struct panfrost_bo_access
*access
;
1092 struct hash_entry
*hentry
;
1094 /* It doesn't make any to flush only the readers. */
1095 assert(access_type
== PAN_BO_ACCESS_WRITE
||
1096 access_type
== PAN_BO_ACCESS_RW
);
1098 hentry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
1099 access
= hentry
? hentry
->data
: NULL
;
1103 if (access_type
& PAN_BO_ACCESS_WRITE
&& access
->writer
&&
1104 access
->writer
->batch
)
1105 panfrost_batch_submit(access
->writer
->batch
);
1107 if (!(access_type
& PAN_BO_ACCESS_READ
))
1110 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
1112 if (*reader
&& (*reader
)->batch
)
1113 panfrost_batch_submit((*reader
)->batch
);
1118 panfrost_batch_set_requirements(struct panfrost_batch
*batch
)
1120 struct panfrost_context
*ctx
= batch
->ctx
;
1122 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.multisample
)
1123 batch
->requirements
|= PAN_REQ_MSAA
;
1125 if (ctx
->depth_stencil
&& ctx
->depth_stencil
->depth
.writemask
)
1126 batch
->requirements
|= PAN_REQ_DEPTH_WRITE
;
1129 /* Helper to smear a 32-bit color across 128-bit components */
1132 pan_pack_color_32(uint32_t *packed
, uint32_t v
)
1134 for (unsigned i
= 0; i
< 4; ++i
)
1139 pan_pack_color_64(uint32_t *packed
, uint32_t lo
, uint32_t hi
)
1141 for (unsigned i
= 0; i
< 4; i
+= 2) {
1148 pan_pack_color(uint32_t *packed
, const union pipe_color_union
*color
, enum pipe_format format
)
1150 /* Alpha magicked to 1.0 if there is no alpha */
1152 bool has_alpha
= util_format_has_alpha(format
);
1153 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
1155 /* Packed color depends on the framebuffer format */
1157 const struct util_format_description
*desc
=
1158 util_format_description(format
);
1160 if (util_format_is_rgba8_variant(desc
)) {
1161 pan_pack_color_32(packed
,
1162 ((uint32_t) float_to_ubyte(clear_alpha
) << 24) |
1163 ((uint32_t) float_to_ubyte(color
->f
[2]) << 16) |
1164 ((uint32_t) float_to_ubyte(color
->f
[1]) << 8) |
1165 ((uint32_t) float_to_ubyte(color
->f
[0]) << 0));
1166 } else if (format
== PIPE_FORMAT_B5G6R5_UNORM
) {
1167 /* First, we convert the components to R5, G6, B5 separately */
1168 unsigned r5
= CLAMP(color
->f
[0], 0.0, 1.0) * 31.0;
1169 unsigned g6
= CLAMP(color
->f
[1], 0.0, 1.0) * 63.0;
1170 unsigned b5
= CLAMP(color
->f
[2], 0.0, 1.0) * 31.0;
1172 /* Then we pack into a sparse u32. TODO: Why these shifts? */
1173 pan_pack_color_32(packed
, (b5
<< 25) | (g6
<< 14) | (r5
<< 5));
1174 } else if (format
== PIPE_FORMAT_B4G4R4A4_UNORM
) {
1175 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
1176 unsigned r4
= CLAMP(color
->f
[0], 0.0, 1.0) * 240.0;
1177 unsigned g4
= CLAMP(color
->f
[1], 0.0, 1.0) * 240.0;
1178 unsigned b4
= CLAMP(color
->f
[2], 0.0, 1.0) * 240.0;
1179 unsigned a4
= CLAMP(clear_alpha
, 0.0, 1.0) * 240.0;
1181 /* Pack on *byte* intervals */
1182 pan_pack_color_32(packed
, (a4
<< 24) | (b4
<< 16) | (g4
<< 8) | r4
);
1183 } else if (format
== PIPE_FORMAT_B5G5R5A1_UNORM
) {
1184 /* Scale as expected but shift oddly */
1185 unsigned r5
= round(CLAMP(color
->f
[0], 0.0, 1.0)) * 31.0;
1186 unsigned g5
= round(CLAMP(color
->f
[1], 0.0, 1.0)) * 31.0;
1187 unsigned b5
= round(CLAMP(color
->f
[2], 0.0, 1.0)) * 31.0;
1188 unsigned a1
= round(CLAMP(clear_alpha
, 0.0, 1.0)) * 1.0;
1190 pan_pack_color_32(packed
, (a1
<< 31) | (b5
<< 25) | (g5
<< 15) | (r5
<< 5));
1192 /* Try Gallium's generic default path. Doesn't work for all
1193 * formats but it's a good guess. */
1195 union util_color out
;
1197 if (util_format_is_pure_integer(format
)) {
1198 memcpy(out
.ui
, color
->ui
, 16);
1200 util_pack_color(color
->f
, format
, &out
);
1203 unsigned size
= util_format_get_blocksize(format
);
1206 unsigned b
= out
.ui
[0];
1207 unsigned s
= b
| (b
<< 8);
1208 pan_pack_color_32(packed
, s
| (s
<< 16));
1209 } else if (size
== 2)
1210 pan_pack_color_32(packed
, out
.ui
[0] | (out
.ui
[0] << 16));
1211 else if (size
== 3 || size
== 4)
1212 pan_pack_color_32(packed
, out
.ui
[0]);
1214 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1] | (out
.ui
[1] << 16)); /* RGB16F -- RGBB */
1216 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1]);
1217 else if (size
== 16)
1218 memcpy(packed
, out
.ui
, 16);
1220 unreachable("Unknown generic format size packing clear colour");
1225 panfrost_batch_clear(struct panfrost_batch
*batch
,
1227 const union pipe_color_union
*color
,
1228 double depth
, unsigned stencil
)
1230 struct panfrost_context
*ctx
= batch
->ctx
;
1232 if (buffers
& PIPE_CLEAR_COLOR
) {
1233 for (unsigned i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
1234 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
1237 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[i
]->format
;
1238 pan_pack_color(batch
->clear_color
[i
], color
, format
);
1242 if (buffers
& PIPE_CLEAR_DEPTH
) {
1243 batch
->clear_depth
= depth
;
1246 if (buffers
& PIPE_CLEAR_STENCIL
) {
1247 batch
->clear_stencil
= stencil
;
1250 batch
->clear
|= buffers
;
1252 /* Clearing affects the entire framebuffer (by definition -- this is
1253 * the Gallium clear callback, which clears the whole framebuffer. If
1254 * the scissor test were enabled from the GL side, the state tracker
1255 * would emit a quad instead and we wouldn't go down this code path) */
1257 panfrost_batch_union_scissor(batch
, 0, 0,
1258 ctx
->pipe_framebuffer
.width
,
1259 ctx
->pipe_framebuffer
.height
);
1263 panfrost_batch_compare(const void *a
, const void *b
)
1265 return util_framebuffer_state_equal(a
, b
);
1269 panfrost_batch_hash(const void *key
)
1271 return _mesa_hash_data(key
, sizeof(struct pipe_framebuffer_state
));
1274 /* Given a new bounding rectangle (scissor), let the job cover the union of the
1275 * new and old bounding rectangles */
1278 panfrost_batch_union_scissor(struct panfrost_batch
*batch
,
1279 unsigned minx
, unsigned miny
,
1280 unsigned maxx
, unsigned maxy
)
1282 batch
->minx
= MIN2(batch
->minx
, minx
);
1283 batch
->miny
= MIN2(batch
->miny
, miny
);
1284 batch
->maxx
= MAX2(batch
->maxx
, maxx
);
1285 batch
->maxy
= MAX2(batch
->maxy
, maxy
);
1289 panfrost_batch_intersection_scissor(struct panfrost_batch
*batch
,
1290 unsigned minx
, unsigned miny
,
1291 unsigned maxx
, unsigned maxy
)
1293 batch
->minx
= MAX2(batch
->minx
, minx
);
1294 batch
->miny
= MAX2(batch
->miny
, miny
);
1295 batch
->maxx
= MIN2(batch
->maxx
, maxx
);
1296 batch
->maxy
= MIN2(batch
->maxy
, maxy
);
1299 /* Are we currently rendering to the screen (rather than an FBO)? */
1302 panfrost_batch_is_scanout(struct panfrost_batch
*batch
)
1304 /* If there is no color buffer, it's an FBO */
1305 if (batch
->key
.nr_cbufs
!= 1)
1308 /* If we're too early that no framebuffer was sent, it's scanout */
1309 if (!batch
->key
.cbufs
[0])
1312 return batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
1313 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
1314 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
1318 panfrost_batch_init(struct panfrost_context
*ctx
)
1320 ctx
->batches
= _mesa_hash_table_create(ctx
,
1321 panfrost_batch_hash
,
1322 panfrost_batch_compare
);
1323 ctx
->accessed_bos
= _mesa_hash_table_create(ctx
, _mesa_hash_pointer
,
1324 _mesa_key_pointer_equal
);