panfrost: Use Midgard-specific reloads
[mesa.git] / src / gallium / drivers / panfrost / pan_job.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
4 *
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:
11 *
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
14 * Software.
15 *
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
22 * SOFTWARE.
23 *
24 */
25
26 #include <assert.h>
27
28 #include "drm-uapi/panfrost_drm.h"
29
30 #include "pan_bo.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"
36 #include "util/rounding.h"
37 #include "pan_util.h"
38 #include "pan_blending.h"
39 #include "pandecode/decode.h"
40 #include "panfrost-quirks.h"
41
42 /* panfrost_bo_access is here to help us keep track of batch accesses to BOs
43 * and build a proper dependency graph such that batches can be pipelined for
44 * better GPU utilization.
45 *
46 * Each accessed BO has a corresponding entry in the ->accessed_bos hash table.
47 * A BO is either being written or read at any time (see if writer != NULL).
48 * When the last access is a write, the batch writing the BO might have read
49 * dependencies (readers that have not been executed yet and want to read the
50 * previous BO content), and when the last access is a read, all readers might
51 * depend on another batch to push its results to memory. That's what the
52 * readers/writers keep track off.
53 * There can only be one writer at any given time, if a new batch wants to
54 * write to the same BO, a dependency will be added between the new writer and
55 * the old writer (at the batch level), and panfrost_bo_access->writer will be
56 * updated to point to the new writer.
57 */
58 struct panfrost_bo_access {
59 struct util_dynarray readers;
60 struct panfrost_batch_fence *writer;
61 };
62
63 static struct panfrost_batch_fence *
64 panfrost_create_batch_fence(struct panfrost_batch *batch)
65 {
66 struct panfrost_batch_fence *fence;
67 ASSERTED int ret;
68
69 fence = rzalloc(NULL, struct panfrost_batch_fence);
70 assert(fence);
71 pipe_reference_init(&fence->reference, 1);
72 fence->ctx = batch->ctx;
73 fence->batch = batch;
74 ret = drmSyncobjCreate(pan_device(batch->ctx->base.screen)->fd, 0,
75 &fence->syncobj);
76 assert(!ret);
77
78 return fence;
79 }
80
81 static void
82 panfrost_free_batch_fence(struct panfrost_batch_fence *fence)
83 {
84 drmSyncobjDestroy(pan_device(fence->ctx->base.screen)->fd,
85 fence->syncobj);
86 ralloc_free(fence);
87 }
88
89 void
90 panfrost_batch_fence_unreference(struct panfrost_batch_fence *fence)
91 {
92 if (pipe_reference(&fence->reference, NULL))
93 panfrost_free_batch_fence(fence);
94 }
95
96 void
97 panfrost_batch_fence_reference(struct panfrost_batch_fence *fence)
98 {
99 pipe_reference(NULL, &fence->reference);
100 }
101
102 static struct panfrost_batch *
103 panfrost_create_batch(struct panfrost_context *ctx,
104 const struct pipe_framebuffer_state *key)
105 {
106 struct panfrost_batch *batch = rzalloc(ctx, struct panfrost_batch);
107
108 batch->ctx = ctx;
109
110 batch->bos = _mesa_hash_table_create(batch, _mesa_hash_pointer,
111 _mesa_key_pointer_equal);
112
113 batch->minx = batch->miny = ~0;
114 batch->maxx = batch->maxy = 0;
115
116 batch->out_sync = panfrost_create_batch_fence(batch);
117 util_copy_framebuffer_state(&batch->key, key);
118
119 batch->pool = panfrost_create_pool(batch, pan_device(ctx->base.screen));
120
121 return batch;
122 }
123
124 static void
125 panfrost_freeze_batch(struct panfrost_batch *batch)
126 {
127 struct panfrost_context *ctx = batch->ctx;
128 struct hash_entry *entry;
129
130 /* Remove the entry in the FBO -> batch hash table if the batch
131 * matches. This way, next draws/clears targeting this FBO will trigger
132 * the creation of a new batch.
133 */
134 entry = _mesa_hash_table_search(ctx->batches, &batch->key);
135 if (entry && entry->data == batch)
136 _mesa_hash_table_remove(ctx->batches, entry);
137
138 /* If this is the bound batch, the panfrost_context parameters are
139 * relevant so submitting it invalidates those parameters, but if it's
140 * not bound, the context parameters are for some other batch so we
141 * can't invalidate them.
142 */
143 if (ctx->batch == batch) {
144 panfrost_invalidate_frame(ctx);
145 ctx->batch = NULL;
146 }
147 }
148
149 #ifdef PAN_BATCH_DEBUG
150 static bool panfrost_batch_is_frozen(struct panfrost_batch *batch)
151 {
152 struct panfrost_context *ctx = batch->ctx;
153 struct hash_entry *entry;
154
155 entry = _mesa_hash_table_search(ctx->batches, &batch->key);
156 if (entry && entry->data == batch)
157 return false;
158
159 if (ctx->batch == batch)
160 return false;
161
162 return true;
163 }
164 #endif
165
166 static void
167 panfrost_free_batch(struct panfrost_batch *batch)
168 {
169 if (!batch)
170 return;
171
172 #ifdef PAN_BATCH_DEBUG
173 assert(panfrost_batch_is_frozen(batch));
174 #endif
175
176 hash_table_foreach(batch->bos, entry)
177 panfrost_bo_unreference((struct panfrost_bo *)entry->key);
178
179 hash_table_foreach(batch->pool.bos, entry)
180 panfrost_bo_unreference((struct panfrost_bo *)entry->key);
181
182 util_dynarray_foreach(&batch->dependencies,
183 struct panfrost_batch_fence *, dep) {
184 panfrost_batch_fence_unreference(*dep);
185 }
186
187 /* The out_sync fence lifetime is different from the the batch one
188 * since other batches might want to wait on a fence of already
189 * submitted/signaled batch. All we need to do here is make sure the
190 * fence does not point to an invalid batch, which the core will
191 * interpret as 'batch is already submitted'.
192 */
193 batch->out_sync->batch = NULL;
194 panfrost_batch_fence_unreference(batch->out_sync);
195
196 util_unreference_framebuffer_state(&batch->key);
197 ralloc_free(batch);
198 }
199
200 #ifdef PAN_BATCH_DEBUG
201 static bool
202 panfrost_dep_graph_contains_batch(struct panfrost_batch *root,
203 struct panfrost_batch *batch)
204 {
205 if (!root)
206 return false;
207
208 util_dynarray_foreach(&root->dependencies,
209 struct panfrost_batch_fence *, dep) {
210 if ((*dep)->batch == batch ||
211 panfrost_dep_graph_contains_batch((*dep)->batch, batch))
212 return true;
213 }
214
215 return false;
216 }
217 #endif
218
219 static void
220 panfrost_batch_add_dep(struct panfrost_batch *batch,
221 struct panfrost_batch_fence *newdep)
222 {
223 if (batch == newdep->batch)
224 return;
225
226 /* We might want to turn ->dependencies into a set if the number of
227 * deps turns out to be big enough to make this 'is dep already there'
228 * search inefficient.
229 */
230 util_dynarray_foreach(&batch->dependencies,
231 struct panfrost_batch_fence *, dep) {
232 if (*dep == newdep)
233 return;
234 }
235
236 #ifdef PAN_BATCH_DEBUG
237 /* Make sure the dependency graph is acyclic. */
238 assert(!panfrost_dep_graph_contains_batch(newdep->batch, batch));
239 #endif
240
241 panfrost_batch_fence_reference(newdep);
242 util_dynarray_append(&batch->dependencies,
243 struct panfrost_batch_fence *, newdep);
244
245 /* We now have a batch depending on us, let's make sure new draw/clear
246 * calls targeting the same FBO use a new batch object.
247 */
248 if (newdep->batch)
249 panfrost_freeze_batch(newdep->batch);
250 }
251
252 static struct panfrost_batch *
253 panfrost_get_batch(struct panfrost_context *ctx,
254 const struct pipe_framebuffer_state *key)
255 {
256 /* Lookup the job first */
257 struct hash_entry *entry = _mesa_hash_table_search(ctx->batches, key);
258
259 if (entry)
260 return entry->data;
261
262 /* Otherwise, let's create a job */
263
264 struct panfrost_batch *batch = panfrost_create_batch(ctx, key);
265
266 /* Save the created job */
267 _mesa_hash_table_insert(ctx->batches, &batch->key, batch);
268
269 return batch;
270 }
271
272 /* Get the job corresponding to the FBO we're currently rendering into */
273
274 struct panfrost_batch *
275 panfrost_get_batch_for_fbo(struct panfrost_context *ctx)
276 {
277 /* If we're wallpapering, we special case to workaround
278 * u_blitter abuse */
279
280 if (ctx->wallpaper_batch)
281 return ctx->wallpaper_batch;
282
283 /* If we already began rendering, use that */
284
285 if (ctx->batch) {
286 assert(util_framebuffer_state_equal(&ctx->batch->key,
287 &ctx->pipe_framebuffer));
288 return ctx->batch;
289 }
290
291 /* If not, look up the job */
292 struct panfrost_batch *batch = panfrost_get_batch(ctx,
293 &ctx->pipe_framebuffer);
294
295 /* Set this job as the current FBO job. Will be reset when updating the
296 * FB state and when submitting or releasing a job.
297 */
298 ctx->batch = batch;
299 return batch;
300 }
301
302 struct panfrost_batch *
303 panfrost_get_fresh_batch_for_fbo(struct panfrost_context *ctx)
304 {
305 struct panfrost_batch *batch;
306
307 batch = panfrost_get_batch(ctx, &ctx->pipe_framebuffer);
308
309 /* The batch has no draw/clear queued, let's return it directly.
310 * Note that it's perfectly fine to re-use a batch with an
311 * existing clear, we'll just update it with the new clear request.
312 */
313 if (!batch->scoreboard.first_job)
314 return batch;
315
316 /* Otherwise, we need to freeze the existing one and instantiate a new
317 * one.
318 */
319 panfrost_freeze_batch(batch);
320 return panfrost_get_batch(ctx, &ctx->pipe_framebuffer);
321 }
322
323 static bool
324 panfrost_batch_fence_is_signaled(struct panfrost_batch_fence *fence)
325 {
326 if (fence->signaled)
327 return true;
328
329 /* Batch has not been submitted yet. */
330 if (fence->batch)
331 return false;
332
333 int ret = drmSyncobjWait(pan_device(fence->ctx->base.screen)->fd,
334 &fence->syncobj, 1, 0, 0, NULL);
335
336 /* Cache whether the fence was signaled */
337 fence->signaled = ret >= 0;
338 return fence->signaled;
339 }
340
341 static void
342 panfrost_bo_access_gc_fences(struct panfrost_context *ctx,
343 struct panfrost_bo_access *access,
344 const struct panfrost_bo *bo)
345 {
346 if (access->writer && panfrost_batch_fence_is_signaled(access->writer)) {
347 panfrost_batch_fence_unreference(access->writer);
348 access->writer = NULL;
349 }
350
351 struct panfrost_batch_fence **readers_array = util_dynarray_begin(&access->readers);
352 struct panfrost_batch_fence **new_readers = readers_array;
353
354 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
355 reader) {
356 if (!(*reader))
357 continue;
358
359 if (panfrost_batch_fence_is_signaled(*reader)) {
360 panfrost_batch_fence_unreference(*reader);
361 *reader = NULL;
362 } else {
363 /* Build a new array of only unsignaled fences in-place */
364 *(new_readers++) = *reader;
365 }
366 }
367
368 if (!util_dynarray_resize(&access->readers, struct panfrost_batch_fence *,
369 new_readers - readers_array) &&
370 new_readers != readers_array)
371 unreachable("Invalid dynarray access->readers");
372 }
373
374 /* Collect signaled fences to keep the kernel-side syncobj-map small. The
375 * idea is to collect those signaled fences at the end of each flush_all
376 * call. This function is likely to collect only fences from previous
377 * batch flushes not the one that have just have just been submitted and
378 * are probably still in flight when we trigger the garbage collection.
379 * Anyway, we need to do this garbage collection at some point if we don't
380 * want the BO access map to keep invalid entries around and retain
381 * syncobjs forever.
382 */
383 static void
384 panfrost_gc_fences(struct panfrost_context *ctx)
385 {
386 hash_table_foreach(ctx->accessed_bos, entry) {
387 struct panfrost_bo_access *access = entry->data;
388
389 assert(access);
390 panfrost_bo_access_gc_fences(ctx, access, entry->key);
391 if (!util_dynarray_num_elements(&access->readers,
392 struct panfrost_batch_fence *) &&
393 !access->writer) {
394 ralloc_free(access);
395 _mesa_hash_table_remove(ctx->accessed_bos, entry);
396 }
397 }
398 }
399
400 #ifdef PAN_BATCH_DEBUG
401 static bool
402 panfrost_batch_in_readers(struct panfrost_batch *batch,
403 struct panfrost_bo_access *access)
404 {
405 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
406 reader) {
407 if (*reader && (*reader)->batch == batch)
408 return true;
409 }
410
411 return false;
412 }
413 #endif
414
415 static void
416 panfrost_batch_update_bo_access(struct panfrost_batch *batch,
417 struct panfrost_bo *bo, bool writes,
418 bool already_accessed)
419 {
420 struct panfrost_context *ctx = batch->ctx;
421 struct panfrost_bo_access *access;
422 bool old_writes = false;
423 struct hash_entry *entry;
424
425 entry = _mesa_hash_table_search(ctx->accessed_bos, bo);
426 access = entry ? entry->data : NULL;
427 if (access) {
428 old_writes = access->writer != NULL;
429 } else {
430 access = rzalloc(ctx, struct panfrost_bo_access);
431 util_dynarray_init(&access->readers, access);
432 _mesa_hash_table_insert(ctx->accessed_bos, bo, access);
433 /* We are the first to access this BO, let's initialize
434 * old_writes to our own access type in that case.
435 */
436 old_writes = writes;
437 }
438
439 assert(access);
440
441 if (writes && !old_writes) {
442 /* Previous access was a read and we want to write this BO.
443 * We first need to add explicit deps between our batch and
444 * the previous readers.
445 */
446 util_dynarray_foreach(&access->readers,
447 struct panfrost_batch_fence *, reader) {
448 /* We were already reading the BO, no need to add a dep
449 * on ourself (the acyclic check would complain about
450 * that).
451 */
452 if (!(*reader) || (*reader)->batch == batch)
453 continue;
454
455 panfrost_batch_add_dep(batch, *reader);
456 }
457 panfrost_batch_fence_reference(batch->out_sync);
458
459 if (access->writer)
460 panfrost_batch_fence_unreference(access->writer);
461
462 /* We now are the new writer. */
463 access->writer = batch->out_sync;
464
465 /* Release the previous readers and reset the readers array. */
466 util_dynarray_foreach(&access->readers,
467 struct panfrost_batch_fence *,
468 reader) {
469 if (!*reader)
470 continue;
471 panfrost_batch_fence_unreference(*reader);
472 }
473
474 util_dynarray_clear(&access->readers);
475 } else if (writes && old_writes) {
476 /* First check if we were the previous writer, in that case
477 * there's nothing to do. Otherwise we need to add a
478 * dependency between the new writer and the old one.
479 */
480 if (access->writer != batch->out_sync) {
481 if (access->writer) {
482 panfrost_batch_add_dep(batch, access->writer);
483 panfrost_batch_fence_unreference(access->writer);
484 }
485 panfrost_batch_fence_reference(batch->out_sync);
486 access->writer = batch->out_sync;
487 }
488 } else if (!writes && old_writes) {
489 /* First check if we were the previous writer, in that case
490 * we want to keep the access type unchanged, as a write is
491 * more constraining than a read.
492 */
493 if (access->writer != batch->out_sync) {
494 /* Add a dependency on the previous writer. */
495 panfrost_batch_add_dep(batch, access->writer);
496
497 /* The previous access was a write, there's no reason
498 * to have entries in the readers array.
499 */
500 assert(!util_dynarray_num_elements(&access->readers,
501 struct panfrost_batch_fence *));
502
503 /* Add ourselves to the readers array. */
504 panfrost_batch_fence_reference(batch->out_sync);
505 util_dynarray_append(&access->readers,
506 struct panfrost_batch_fence *,
507 batch->out_sync);
508 access->writer = NULL;
509 }
510 } else {
511 /* We already accessed this BO before, so we should already be
512 * in the reader array.
513 */
514 #ifdef PAN_BATCH_DEBUG
515 if (already_accessed) {
516 assert(panfrost_batch_in_readers(batch, access));
517 return;
518 }
519 #endif
520
521 /* Previous access was a read and we want to read this BO.
522 * Add ourselves to the readers array and add a dependency on
523 * the previous writer if any.
524 */
525 panfrost_batch_fence_reference(batch->out_sync);
526 util_dynarray_append(&access->readers,
527 struct panfrost_batch_fence *,
528 batch->out_sync);
529
530 if (access->writer)
531 panfrost_batch_add_dep(batch, access->writer);
532 }
533 }
534
535 void
536 panfrost_batch_add_bo(struct panfrost_batch *batch, struct panfrost_bo *bo,
537 uint32_t flags)
538 {
539 if (!bo)
540 return;
541
542 struct hash_entry *entry;
543 uint32_t old_flags = 0;
544
545 entry = _mesa_hash_table_search(batch->bos, bo);
546 if (!entry) {
547 entry = _mesa_hash_table_insert(batch->bos, bo,
548 (void *)(uintptr_t)flags);
549 panfrost_bo_reference(bo);
550 } else {
551 old_flags = (uintptr_t)entry->data;
552
553 /* All batches have to agree on the shared flag. */
554 assert((old_flags & PAN_BO_ACCESS_SHARED) ==
555 (flags & PAN_BO_ACCESS_SHARED));
556 }
557
558 assert(entry);
559
560 if (old_flags == flags)
561 return;
562
563 flags |= old_flags;
564 entry->data = (void *)(uintptr_t)flags;
565
566 /* If this is not a shared BO, we don't really care about dependency
567 * tracking.
568 */
569 if (!(flags & PAN_BO_ACCESS_SHARED))
570 return;
571
572 /* All dependencies should have been flushed before we execute the
573 * wallpaper draw, so it should be harmless to skip the
574 * update_bo_access() call.
575 */
576 if (batch == batch->ctx->wallpaper_batch)
577 return;
578
579 assert(flags & PAN_BO_ACCESS_RW);
580 panfrost_batch_update_bo_access(batch, bo, flags & PAN_BO_ACCESS_WRITE,
581 old_flags != 0);
582 }
583
584 static void
585 panfrost_batch_add_resource_bos(struct panfrost_batch *batch,
586 struct panfrost_resource *rsrc,
587 uint32_t flags)
588 {
589 panfrost_batch_add_bo(batch, rsrc->bo, flags);
590
591 for (unsigned i = 0; i < MAX_MIP_LEVELS; i++)
592 if (rsrc->slices[i].checksum_bo)
593 panfrost_batch_add_bo(batch, rsrc->slices[i].checksum_bo, flags);
594
595 if (rsrc->separate_stencil)
596 panfrost_batch_add_bo(batch, rsrc->separate_stencil->bo, flags);
597 }
598
599 void panfrost_batch_add_fbo_bos(struct panfrost_batch *batch)
600 {
601 uint32_t flags = PAN_BO_ACCESS_SHARED | PAN_BO_ACCESS_WRITE |
602 PAN_BO_ACCESS_VERTEX_TILER |
603 PAN_BO_ACCESS_FRAGMENT;
604
605 for (unsigned i = 0; i < batch->key.nr_cbufs; ++i) {
606 struct panfrost_resource *rsrc = pan_resource(batch->key.cbufs[i]->texture);
607 panfrost_batch_add_resource_bos(batch, rsrc, flags);
608 }
609
610 if (batch->key.zsbuf) {
611 struct panfrost_resource *rsrc = pan_resource(batch->key.zsbuf->texture);
612 panfrost_batch_add_resource_bos(batch, rsrc, flags);
613 }
614 }
615
616 struct panfrost_bo *
617 panfrost_batch_create_bo(struct panfrost_batch *batch, size_t size,
618 uint32_t create_flags, uint32_t access_flags)
619 {
620 struct panfrost_bo *bo;
621
622 bo = panfrost_bo_create(pan_device(batch->ctx->base.screen), size,
623 create_flags);
624 panfrost_batch_add_bo(batch, bo, access_flags);
625
626 /* panfrost_batch_add_bo() has retained a reference and
627 * panfrost_bo_create() initialize the refcnt to 1, so let's
628 * unreference the BO here so it gets released when the batch is
629 * destroyed (unless it's retained by someone else in the meantime).
630 */
631 panfrost_bo_unreference(bo);
632 return bo;
633 }
634
635 /* Returns the polygon list's GPU address if available, or otherwise allocates
636 * the polygon list. It's perfectly fast to use allocate/free BO directly,
637 * since we'll hit the BO cache and this is one-per-batch anyway. */
638
639 mali_ptr
640 panfrost_batch_get_polygon_list(struct panfrost_batch *batch, unsigned size)
641 {
642 if (batch->polygon_list) {
643 assert(batch->polygon_list->size >= size);
644 } else {
645 /* Create the BO as invisible, as there's no reason to map */
646 size = util_next_power_of_two(size);
647
648 batch->polygon_list = panfrost_batch_create_bo(batch, size,
649 PAN_BO_INVISIBLE,
650 PAN_BO_ACCESS_PRIVATE |
651 PAN_BO_ACCESS_RW |
652 PAN_BO_ACCESS_VERTEX_TILER |
653 PAN_BO_ACCESS_FRAGMENT);
654 }
655
656 return batch->polygon_list->gpu;
657 }
658
659 struct panfrost_bo *
660 panfrost_batch_get_scratchpad(struct panfrost_batch *batch,
661 unsigned shift,
662 unsigned thread_tls_alloc,
663 unsigned core_count)
664 {
665 unsigned size = panfrost_get_total_stack_size(shift,
666 thread_tls_alloc,
667 core_count);
668
669 if (batch->scratchpad) {
670 assert(batch->scratchpad->size >= size);
671 } else {
672 batch->scratchpad = panfrost_batch_create_bo(batch, size,
673 PAN_BO_INVISIBLE,
674 PAN_BO_ACCESS_PRIVATE |
675 PAN_BO_ACCESS_RW |
676 PAN_BO_ACCESS_VERTEX_TILER |
677 PAN_BO_ACCESS_FRAGMENT);
678 }
679
680 return batch->scratchpad;
681 }
682
683 struct panfrost_bo *
684 panfrost_batch_get_shared_memory(struct panfrost_batch *batch,
685 unsigned size,
686 unsigned workgroup_count)
687 {
688 if (batch->shared_memory) {
689 assert(batch->shared_memory->size >= size);
690 } else {
691 batch->shared_memory = panfrost_batch_create_bo(batch, size,
692 PAN_BO_INVISIBLE,
693 PAN_BO_ACCESS_PRIVATE |
694 PAN_BO_ACCESS_RW |
695 PAN_BO_ACCESS_VERTEX_TILER);
696 }
697
698 return batch->shared_memory;
699 }
700
701 struct panfrost_bo *
702 panfrost_batch_get_tiler_heap(struct panfrost_batch *batch)
703 {
704 if (batch->tiler_heap)
705 return batch->tiler_heap;
706
707 batch->tiler_heap = panfrost_batch_create_bo(batch, 4096 * 4096,
708 PAN_BO_INVISIBLE |
709 PAN_BO_GROWABLE,
710 PAN_BO_ACCESS_PRIVATE |
711 PAN_BO_ACCESS_RW |
712 PAN_BO_ACCESS_VERTEX_TILER |
713 PAN_BO_ACCESS_FRAGMENT);
714 assert(batch->tiler_heap);
715 return batch->tiler_heap;
716 }
717
718 mali_ptr
719 panfrost_batch_get_tiler_meta(struct panfrost_batch *batch, unsigned vertex_count)
720 {
721 if (!vertex_count)
722 return 0;
723
724 if (batch->tiler_meta)
725 return batch->tiler_meta;
726
727 struct panfrost_bo *tiler_heap;
728 tiler_heap = panfrost_batch_get_tiler_heap(batch);
729
730 struct bifrost_tiler_heap_meta tiler_heap_meta = {
731 .heap_size = tiler_heap->size,
732 .tiler_heap_start = tiler_heap->gpu,
733 .tiler_heap_free = tiler_heap->gpu,
734 .tiler_heap_end = tiler_heap->gpu + tiler_heap->size,
735 .unk1 = 0x1,
736 .unk7e007e = 0x7e007e,
737 };
738
739 struct bifrost_tiler_meta tiler_meta = {
740 .hierarchy_mask = 0x28,
741 .flags = 0x0,
742 .width = MALI_POSITIVE(batch->key.width),
743 .height = MALI_POSITIVE(batch->key.height),
744 .tiler_heap_meta = panfrost_pool_upload(&batch->pool, &tiler_heap_meta, sizeof(tiler_heap_meta)),
745 };
746
747 batch->tiler_meta = panfrost_pool_upload(&batch->pool, &tiler_meta, sizeof(tiler_meta));
748 return batch->tiler_meta;
749 }
750
751 struct panfrost_bo *
752 panfrost_batch_get_tiler_dummy(struct panfrost_batch *batch)
753 {
754 struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
755
756 uint32_t create_flags = 0;
757
758 if (batch->tiler_dummy)
759 return batch->tiler_dummy;
760
761 if (!(dev->quirks & MIDGARD_NO_HIER_TILING))
762 create_flags = PAN_BO_INVISIBLE;
763
764 batch->tiler_dummy = panfrost_batch_create_bo(batch, 4096,
765 create_flags,
766 PAN_BO_ACCESS_PRIVATE |
767 PAN_BO_ACCESS_RW |
768 PAN_BO_ACCESS_VERTEX_TILER |
769 PAN_BO_ACCESS_FRAGMENT);
770 assert(batch->tiler_dummy);
771 return batch->tiler_dummy;
772 }
773
774 mali_ptr
775 panfrost_batch_reserve_framebuffer(struct panfrost_batch *batch)
776 {
777 struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
778
779 /* If we haven't, reserve space for the framebuffer */
780
781 if (!batch->framebuffer.gpu) {
782 unsigned size = (dev->quirks & MIDGARD_SFBD) ?
783 sizeof(struct mali_single_framebuffer) :
784 sizeof(struct mali_framebuffer);
785
786 batch->framebuffer = panfrost_pool_alloc(&batch->pool, size);
787
788 /* Tag the pointer */
789 if (!(dev->quirks & MIDGARD_SFBD))
790 batch->framebuffer.gpu |= MALI_MFBD;
791 }
792
793 return batch->framebuffer.gpu;
794 }
795
796
797
798 static void
799 panfrost_load_surface(struct panfrost_batch *batch, struct pipe_surface *surf, unsigned loc)
800 {
801 if (!surf)
802 return;
803
804 struct panfrost_resource *rsrc = pan_resource(surf->texture);
805 unsigned level = surf->u.tex.level;
806
807 if (!rsrc->slices[level].initialized)
808 return;
809
810 if (!rsrc->damage.inverted_len)
811 return;
812
813 /* Clamp the rendering area to the damage extent. The
814 * KHR_partial_update() spec states that trying to render outside of
815 * the damage region is "undefined behavior", so we should be safe.
816 */
817 unsigned damage_width = (rsrc->damage.extent.maxx - rsrc->damage.extent.minx);
818 unsigned damage_height = (rsrc->damage.extent.maxy - rsrc->damage.extent.miny);
819
820 if (damage_width && damage_height) {
821 panfrost_batch_intersection_scissor(batch,
822 rsrc->damage.extent.minx,
823 rsrc->damage.extent.miny,
824 rsrc->damage.extent.maxx,
825 rsrc->damage.extent.maxy);
826 }
827
828 /* XXX: Native blits on Bifrost */
829 if (batch->pool.dev->quirks & IS_BIFROST) {
830 if (loc != FRAG_RESULT_DATA0)
831 return;
832
833 /* XXX: why align on *twice* the tile length? */
834 batch->minx = batch->minx & ~((MALI_TILE_LENGTH * 2) - 1);
835 batch->miny = batch->miny & ~((MALI_TILE_LENGTH * 2) - 1);
836 batch->maxx = MIN2(ALIGN_POT(batch->maxx, MALI_TILE_LENGTH * 2),
837 rsrc->base.width0);
838 batch->maxy = MIN2(ALIGN_POT(batch->maxy, MALI_TILE_LENGTH * 2),
839 rsrc->base.height0);
840
841 struct pipe_box rect;
842 batch->ctx->wallpaper_batch = batch;
843 u_box_2d(batch->minx, batch->miny, batch->maxx - batch->minx,
844 batch->maxy - batch->miny, &rect);
845 panfrost_blit_wallpaper(batch->ctx, &rect);
846 batch->ctx->wallpaper_batch = NULL;
847 return;
848 }
849
850 enum pipe_format format = rsrc->base.format;
851
852 if (loc == FRAG_RESULT_DEPTH) {
853 if (!util_format_has_depth(util_format_description(format)))
854 return;
855
856 format = util_format_get_depth_only(format);
857 } else if (loc == FRAG_RESULT_STENCIL) {
858 if (!util_format_has_stencil(util_format_description(format)))
859 return;
860
861 if (rsrc->separate_stencil) {
862 rsrc = rsrc->separate_stencil;
863 format = rsrc->base.format;
864 }
865
866 format = util_format_stencil_only(format);
867 }
868
869 enum mali_texture_type type =
870 panfrost_translate_texture_type(rsrc->base.target);
871
872 unsigned nr_samples = surf->nr_samples;
873
874 if (!nr_samples)
875 nr_samples = surf->texture->nr_samples;
876
877 struct pan_image img = {
878 .width0 = rsrc->base.width0,
879 .height0 = rsrc->base.height0,
880 .depth0 = rsrc->base.depth0,
881 .format = format,
882 .type = type,
883 .layout = rsrc->layout,
884 .array_size = rsrc->base.array_size,
885 .first_level = level,
886 .last_level = level,
887 .first_layer = surf->u.tex.first_layer,
888 .last_layer = surf->u.tex.last_layer,
889 .nr_samples = nr_samples,
890 .cubemap_stride = rsrc->cubemap_stride,
891 .bo = rsrc->bo,
892 .slices = rsrc->slices
893 };
894
895 mali_ptr blend_shader = 0;
896
897 if (loc >= FRAG_RESULT_DATA0 && !panfrost_can_fixed_blend(rsrc->base.format)) {
898 struct panfrost_blend_shader *b =
899 panfrost_get_blend_shader(batch->ctx, &batch->ctx->blit_blend, rsrc->base.format, loc - FRAG_RESULT_DATA0);
900
901 struct panfrost_bo *bo = panfrost_batch_create_bo(batch, b->size,
902 PAN_BO_EXECUTE,
903 PAN_BO_ACCESS_PRIVATE |
904 PAN_BO_ACCESS_READ |
905 PAN_BO_ACCESS_FRAGMENT);
906
907 memcpy(bo->cpu, b->buffer, b->size);
908 assert(b->work_count <= 4);
909
910 blend_shader = bo->gpu | b->first_tag;
911 }
912
913 struct panfrost_transfer transfer = panfrost_pool_alloc(&batch->pool,
914 4 * 4 * 6 * rsrc->damage.inverted_len);
915
916 for (unsigned i = 0; i < rsrc->damage.inverted_len; ++i) {
917 float *o = (float *) (transfer.cpu + (4 * 4 * 6 * i));
918 struct pan_rect r = rsrc->damage.inverted_rects[i];
919
920 float rect[] = {
921 r.minx, rsrc->base.height0 - r.miny, 0.0, 1.0,
922 r.maxx, rsrc->base.height0 - r.miny, 0.0, 1.0,
923 r.minx, rsrc->base.height0 - r.maxy, 0.0, 1.0,
924
925 r.maxx, rsrc->base.height0 - r.miny, 0.0, 1.0,
926 r.minx, rsrc->base.height0 - r.maxy, 0.0, 1.0,
927 r.maxx, rsrc->base.height0 - r.maxy, 0.0, 1.0,
928 };
929
930 assert(sizeof(rect) == 4 * 4 * 6);
931 memcpy(o, rect, sizeof(rect));
932 }
933
934 panfrost_load_midg(&batch->pool, &batch->scoreboard,
935 blend_shader,
936 batch->framebuffer.gpu, transfer.gpu,
937 rsrc->damage.inverted_len * 6,
938 &img, loc);
939
940 panfrost_batch_add_bo(batch, batch->pool.dev->blit_shaders.bo,
941 PAN_BO_ACCESS_SHARED | PAN_BO_ACCESS_READ | PAN_BO_ACCESS_FRAGMENT);
942 }
943
944 static void
945 panfrost_batch_draw_wallpaper(struct panfrost_batch *batch)
946 {
947 panfrost_batch_reserve_framebuffer(batch);
948
949 /* Assume combined. If either depth or stencil is written, they will
950 * both be written so we need to be careful for reloading */
951
952 unsigned draws = batch->draws;
953
954 if (draws & PIPE_CLEAR_DEPTHSTENCIL)
955 draws |= PIPE_CLEAR_DEPTHSTENCIL;
956
957 /* Mask of buffers which need reload since they are not cleared and
958 * they are drawn. (If they are cleared, reload is useless; if they are
959 * not drawn and also not cleared, we can generally omit the attachment
960 * at the framebuffer descriptor level */
961
962 unsigned reload = ~batch->clear & draws;
963
964 for (unsigned i = 0; i < batch->key.nr_cbufs; ++i) {
965 if (reload & (PIPE_CLEAR_COLOR0 << i))
966 panfrost_load_surface(batch, batch->key.cbufs[i], FRAG_RESULT_DATA0 + i);
967 }
968
969 if (reload & PIPE_CLEAR_DEPTH)
970 panfrost_load_surface(batch, batch->key.zsbuf, FRAG_RESULT_DEPTH);
971
972 if (reload & PIPE_CLEAR_STENCIL)
973 panfrost_load_surface(batch, batch->key.zsbuf, FRAG_RESULT_STENCIL);
974 }
975
976 static void
977 panfrost_batch_record_bo(struct hash_entry *entry, unsigned *bo_handles, unsigned idx)
978 {
979 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
980 uint32_t flags = (uintptr_t)entry->data;
981
982 assert(bo->gem_handle > 0);
983 bo_handles[idx] = bo->gem_handle;
984
985 /* Update the BO access flags so that panfrost_bo_wait() knows
986 * about all pending accesses.
987 * We only keep the READ/WRITE info since this is all the BO
988 * wait logic cares about.
989 * We also preserve existing flags as this batch might not
990 * be the first one to access the BO.
991 */
992 bo->gpu_access |= flags & (PAN_BO_ACCESS_RW);
993 }
994
995 static int
996 panfrost_batch_submit_ioctl(struct panfrost_batch *batch,
997 mali_ptr first_job_desc,
998 uint32_t reqs)
999 {
1000 struct panfrost_context *ctx = batch->ctx;
1001 struct pipe_context *gallium = (struct pipe_context *) ctx;
1002 struct panfrost_device *dev = pan_device(gallium->screen);
1003 struct drm_panfrost_submit submit = {0,};
1004 uint32_t *bo_handles, *in_syncs = NULL;
1005 bool is_fragment_shader;
1006 int ret;
1007
1008 is_fragment_shader = (reqs & PANFROST_JD_REQ_FS) && batch->scoreboard.first_job;
1009 if (is_fragment_shader)
1010 submit.in_sync_count = 1;
1011 else
1012 submit.in_sync_count = util_dynarray_num_elements(&batch->dependencies,
1013 struct panfrost_batch_fence *);
1014
1015 if (submit.in_sync_count) {
1016 in_syncs = calloc(submit.in_sync_count, sizeof(*in_syncs));
1017 assert(in_syncs);
1018 }
1019
1020 /* The fragment job always depends on the vertex/tiler job if there's
1021 * one
1022 */
1023 if (is_fragment_shader) {
1024 in_syncs[0] = batch->out_sync->syncobj;
1025 } else {
1026 unsigned int i = 0;
1027
1028 util_dynarray_foreach(&batch->dependencies,
1029 struct panfrost_batch_fence *, dep)
1030 in_syncs[i++] = (*dep)->syncobj;
1031 }
1032
1033 submit.in_syncs = (uintptr_t)in_syncs;
1034 submit.out_sync = batch->out_sync->syncobj;
1035 submit.jc = first_job_desc;
1036 submit.requirements = reqs;
1037
1038 bo_handles = calloc(batch->pool.bos->entries + batch->bos->entries, sizeof(*bo_handles));
1039 assert(bo_handles);
1040
1041 hash_table_foreach(batch->bos, entry)
1042 panfrost_batch_record_bo(entry, bo_handles, submit.bo_handle_count++);
1043
1044 hash_table_foreach(batch->pool.bos, entry)
1045 panfrost_batch_record_bo(entry, bo_handles, submit.bo_handle_count++);
1046
1047 submit.bo_handles = (u64) (uintptr_t) bo_handles;
1048 ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
1049 free(bo_handles);
1050 free(in_syncs);
1051
1052 if (ret) {
1053 if (dev->debug & PAN_DBG_MSGS)
1054 fprintf(stderr, "Error submitting: %m\n");
1055
1056 return errno;
1057 }
1058
1059 /* Trace the job if we're doing that */
1060 if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
1061 /* Wait so we can get errors reported back */
1062 drmSyncobjWait(dev->fd, &batch->out_sync->syncobj, 1,
1063 INT64_MAX, 0, NULL);
1064
1065 /* Trace gets priority over sync */
1066 bool minimal = !(dev->debug & PAN_DBG_TRACE);
1067 pandecode_jc(submit.jc, dev->quirks & IS_BIFROST, dev->gpu_id, minimal);
1068 }
1069
1070 return 0;
1071 }
1072
1073 static int
1074 panfrost_batch_submit_jobs(struct panfrost_batch *batch)
1075 {
1076 bool has_draws = batch->scoreboard.first_job;
1077 int ret = 0;
1078
1079 if (has_draws) {
1080 ret = panfrost_batch_submit_ioctl(batch, batch->scoreboard.first_job, 0);
1081 assert(!ret);
1082 }
1083
1084 if (batch->scoreboard.tiler_dep || batch->clear) {
1085 mali_ptr fragjob = panfrost_fragment_job(batch, has_draws);
1086 ret = panfrost_batch_submit_ioctl(batch, fragjob, PANFROST_JD_REQ_FS);
1087 assert(!ret);
1088 }
1089
1090 return ret;
1091 }
1092
1093 static void
1094 panfrost_batch_submit(struct panfrost_batch *batch)
1095 {
1096 assert(batch);
1097 struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
1098
1099 /* Submit the dependencies first. */
1100 util_dynarray_foreach(&batch->dependencies,
1101 struct panfrost_batch_fence *, dep) {
1102 if ((*dep)->batch)
1103 panfrost_batch_submit((*dep)->batch);
1104 }
1105
1106 int ret;
1107
1108 /* Nothing to do! */
1109 if (!batch->scoreboard.first_job && !batch->clear) {
1110 /* Mark the fence as signaled so the fence logic does not try
1111 * to wait on it.
1112 */
1113 batch->out_sync->signaled = true;
1114 goto out;
1115 }
1116
1117 panfrost_batch_draw_wallpaper(batch);
1118
1119 /* Now that all draws are in, we can finally prepare the
1120 * FBD for the batch */
1121
1122 if (batch->framebuffer.gpu && batch->scoreboard.first_job) {
1123 struct panfrost_context *ctx = batch->ctx;
1124 struct pipe_context *gallium = (struct pipe_context *) ctx;
1125 struct panfrost_device *dev = pan_device(gallium->screen);
1126
1127 if (dev->quirks & MIDGARD_SFBD)
1128 panfrost_attach_sfbd(batch, ~0);
1129 else
1130 panfrost_attach_mfbd(batch, ~0);
1131 }
1132
1133 mali_ptr polygon_list = panfrost_batch_get_polygon_list(batch,
1134 MALI_TILER_MINIMUM_HEADER_SIZE);
1135
1136 panfrost_scoreboard_initialize_tiler(&batch->pool, &batch->scoreboard, polygon_list);
1137
1138 ret = panfrost_batch_submit_jobs(batch);
1139
1140 if (ret && dev->debug & PAN_DBG_MSGS)
1141 fprintf(stderr, "panfrost_batch_submit failed: %d\n", ret);
1142
1143 /* We must reset the damage info of our render targets here even
1144 * though a damage reset normally happens when the DRI layer swaps
1145 * buffers. That's because there can be implicit flushes the GL
1146 * app is not aware of, and those might impact the damage region: if
1147 * part of the damaged portion is drawn during those implicit flushes,
1148 * you have to reload those areas before next draws are pushed, and
1149 * since the driver can't easily know what's been modified by the draws
1150 * it flushed, the easiest solution is to reload everything.
1151 */
1152 for (unsigned i = 0; i < batch->key.nr_cbufs; i++) {
1153 if (!batch->key.cbufs[i])
1154 continue;
1155
1156 panfrost_resource_set_damage_region(NULL,
1157 batch->key.cbufs[i]->texture, 0, NULL);
1158 }
1159
1160 out:
1161 panfrost_freeze_batch(batch);
1162 panfrost_free_batch(batch);
1163 }
1164
1165 void
1166 panfrost_flush_all_batches(struct panfrost_context *ctx, bool wait)
1167 {
1168 struct util_dynarray fences, syncobjs;
1169
1170 if (wait) {
1171 util_dynarray_init(&fences, NULL);
1172 util_dynarray_init(&syncobjs, NULL);
1173 }
1174
1175 hash_table_foreach(ctx->batches, hentry) {
1176 struct panfrost_batch *batch = hentry->data;
1177
1178 assert(batch);
1179
1180 if (wait) {
1181 panfrost_batch_fence_reference(batch->out_sync);
1182 util_dynarray_append(&fences, struct panfrost_batch_fence *,
1183 batch->out_sync);
1184 util_dynarray_append(&syncobjs, uint32_t,
1185 batch->out_sync->syncobj);
1186 }
1187
1188 panfrost_batch_submit(batch);
1189 }
1190
1191 assert(!ctx->batches->entries);
1192
1193 /* Collect batch fences before returning */
1194 panfrost_gc_fences(ctx);
1195
1196 if (!wait)
1197 return;
1198
1199 drmSyncobjWait(pan_device(ctx->base.screen)->fd,
1200 util_dynarray_begin(&syncobjs),
1201 util_dynarray_num_elements(&syncobjs, uint32_t),
1202 INT64_MAX, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL, NULL);
1203
1204 util_dynarray_foreach(&fences, struct panfrost_batch_fence *, fence)
1205 panfrost_batch_fence_unreference(*fence);
1206
1207 util_dynarray_fini(&fences);
1208 util_dynarray_fini(&syncobjs);
1209 }
1210
1211 bool
1212 panfrost_pending_batches_access_bo(struct panfrost_context *ctx,
1213 const struct panfrost_bo *bo)
1214 {
1215 struct panfrost_bo_access *access;
1216 struct hash_entry *hentry;
1217
1218 hentry = _mesa_hash_table_search(ctx->accessed_bos, bo);
1219 access = hentry ? hentry->data : NULL;
1220 if (!access)
1221 return false;
1222
1223 if (access->writer && access->writer->batch)
1224 return true;
1225
1226 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
1227 reader) {
1228 if (*reader && (*reader)->batch)
1229 return true;
1230 }
1231
1232 return false;
1233 }
1234
1235 /* We always flush writers. We might also need to flush readers */
1236
1237 void
1238 panfrost_flush_batches_accessing_bo(struct panfrost_context *ctx,
1239 struct panfrost_bo *bo,
1240 bool flush_readers)
1241 {
1242 struct panfrost_bo_access *access;
1243 struct hash_entry *hentry;
1244
1245 hentry = _mesa_hash_table_search(ctx->accessed_bos, bo);
1246 access = hentry ? hentry->data : NULL;
1247 if (!access)
1248 return;
1249
1250 if (access->writer && access->writer->batch)
1251 panfrost_batch_submit(access->writer->batch);
1252
1253 if (!flush_readers)
1254 return;
1255
1256 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
1257 reader) {
1258 if (*reader && (*reader)->batch)
1259 panfrost_batch_submit((*reader)->batch);
1260 }
1261 }
1262
1263 void
1264 panfrost_batch_set_requirements(struct panfrost_batch *batch)
1265 {
1266 struct panfrost_context *ctx = batch->ctx;
1267
1268 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
1269 batch->requirements |= PAN_REQ_MSAA;
1270
1271 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask) {
1272 batch->requirements |= PAN_REQ_DEPTH_WRITE;
1273 batch->draws |= PIPE_CLEAR_DEPTH;
1274 }
1275
1276 if (ctx->depth_stencil && ctx->depth_stencil->stencil[0].enabled)
1277 batch->draws |= PIPE_CLEAR_STENCIL;
1278 }
1279
1280 void
1281 panfrost_batch_adjust_stack_size(struct panfrost_batch *batch)
1282 {
1283 struct panfrost_context *ctx = batch->ctx;
1284
1285 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
1286 struct panfrost_shader_state *ss;
1287
1288 ss = panfrost_get_shader_state(ctx, i);
1289 if (!ss)
1290 continue;
1291
1292 batch->stack_size = MAX2(batch->stack_size, ss->stack_size);
1293 }
1294 }
1295
1296 /* Helper to smear a 32-bit color across 128-bit components */
1297
1298 static void
1299 pan_pack_color_32(uint32_t *packed, uint32_t v)
1300 {
1301 for (unsigned i = 0; i < 4; ++i)
1302 packed[i] = v;
1303 }
1304
1305 static void
1306 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
1307 {
1308 for (unsigned i = 0; i < 4; i += 2) {
1309 packed[i + 0] = lo;
1310 packed[i + 1] = hi;
1311 }
1312 }
1313
1314 static void
1315 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
1316 {
1317 /* Alpha magicked to 1.0 if there is no alpha */
1318
1319 bool has_alpha = util_format_has_alpha(format);
1320 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
1321
1322 /* Packed color depends on the framebuffer format */
1323
1324 const struct util_format_description *desc =
1325 util_format_description(format);
1326
1327 if (util_format_is_rgba8_variant(desc)) {
1328 pan_pack_color_32(packed,
1329 ((uint32_t) float_to_ubyte(clear_alpha) << 24) |
1330 ((uint32_t) float_to_ubyte(color->f[2]) << 16) |
1331 ((uint32_t) float_to_ubyte(color->f[1]) << 8) |
1332 ((uint32_t) float_to_ubyte(color->f[0]) << 0));
1333 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
1334 /* First, we convert the components to R5, G6, B5 separately */
1335 unsigned r5 = _mesa_roundevenf(SATURATE(color->f[0]) * 31.0);
1336 unsigned g6 = _mesa_roundevenf(SATURATE(color->f[1]) * 63.0);
1337 unsigned b5 = _mesa_roundevenf(SATURATE(color->f[2]) * 31.0);
1338
1339 /* Then we pack into a sparse u32. TODO: Why these shifts? */
1340 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
1341 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
1342 /* Convert to 4-bits */
1343 unsigned r4 = _mesa_roundevenf(SATURATE(color->f[0]) * 15.0);
1344 unsigned g4 = _mesa_roundevenf(SATURATE(color->f[1]) * 15.0);
1345 unsigned b4 = _mesa_roundevenf(SATURATE(color->f[2]) * 15.0);
1346 unsigned a4 = _mesa_roundevenf(SATURATE(clear_alpha) * 15.0);
1347
1348 /* Pack on *byte* intervals */
1349 pan_pack_color_32(packed, (a4 << 28) | (b4 << 20) | (g4 << 12) | (r4 << 4));
1350 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
1351 /* Scale as expected but shift oddly */
1352 unsigned r5 = _mesa_roundevenf(SATURATE(color->f[0]) * 31.0);
1353 unsigned g5 = _mesa_roundevenf(SATURATE(color->f[1]) * 31.0);
1354 unsigned b5 = _mesa_roundevenf(SATURATE(color->f[2]) * 31.0);
1355 unsigned a1 = _mesa_roundevenf(SATURATE(clear_alpha) * 1.0);
1356
1357 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
1358 } else {
1359 /* Otherwise, it's generic subject to replication */
1360
1361 union util_color out = { 0 };
1362 unsigned size = util_format_get_blocksize(format);
1363
1364 util_pack_color(color->f, format, &out);
1365
1366 if (size == 1) {
1367 unsigned b = out.ui[0];
1368 unsigned s = b | (b << 8);
1369 pan_pack_color_32(packed, s | (s << 16));
1370 } else if (size == 2)
1371 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
1372 else if (size == 3 || size == 4)
1373 pan_pack_color_32(packed, out.ui[0]);
1374 else if (size == 6)
1375 pan_pack_color_64(packed, out.ui[0], out.ui[1] | (out.ui[1] << 16)); /* RGB16F -- RGBB */
1376 else if (size == 8)
1377 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
1378 else if (size == 16)
1379 memcpy(packed, out.ui, 16);
1380 else
1381 unreachable("Unknown generic format size packing clear colour");
1382 }
1383 }
1384
1385 void
1386 panfrost_batch_clear(struct panfrost_batch *batch,
1387 unsigned buffers,
1388 const union pipe_color_union *color,
1389 double depth, unsigned stencil)
1390 {
1391 struct panfrost_context *ctx = batch->ctx;
1392
1393 if (buffers & PIPE_CLEAR_COLOR) {
1394 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
1395 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
1396 continue;
1397
1398 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
1399 pan_pack_color(batch->clear_color[i], color, format);
1400 }
1401 }
1402
1403 if (buffers & PIPE_CLEAR_DEPTH) {
1404 batch->clear_depth = depth;
1405 }
1406
1407 if (buffers & PIPE_CLEAR_STENCIL) {
1408 batch->clear_stencil = stencil;
1409 }
1410
1411 batch->clear |= buffers;
1412
1413 /* Clearing affects the entire framebuffer (by definition -- this is
1414 * the Gallium clear callback, which clears the whole framebuffer. If
1415 * the scissor test were enabled from the GL side, the gallium frontend
1416 * would emit a quad instead and we wouldn't go down this code path) */
1417
1418 panfrost_batch_union_scissor(batch, 0, 0,
1419 ctx->pipe_framebuffer.width,
1420 ctx->pipe_framebuffer.height);
1421 }
1422
1423 static bool
1424 panfrost_batch_compare(const void *a, const void *b)
1425 {
1426 return util_framebuffer_state_equal(a, b);
1427 }
1428
1429 static uint32_t
1430 panfrost_batch_hash(const void *key)
1431 {
1432 return _mesa_hash_data(key, sizeof(struct pipe_framebuffer_state));
1433 }
1434
1435 /* Given a new bounding rectangle (scissor), let the job cover the union of the
1436 * new and old bounding rectangles */
1437
1438 void
1439 panfrost_batch_union_scissor(struct panfrost_batch *batch,
1440 unsigned minx, unsigned miny,
1441 unsigned maxx, unsigned maxy)
1442 {
1443 batch->minx = MIN2(batch->minx, minx);
1444 batch->miny = MIN2(batch->miny, miny);
1445 batch->maxx = MAX2(batch->maxx, maxx);
1446 batch->maxy = MAX2(batch->maxy, maxy);
1447 }
1448
1449 void
1450 panfrost_batch_intersection_scissor(struct panfrost_batch *batch,
1451 unsigned minx, unsigned miny,
1452 unsigned maxx, unsigned maxy)
1453 {
1454 batch->minx = MAX2(batch->minx, minx);
1455 batch->miny = MAX2(batch->miny, miny);
1456 batch->maxx = MIN2(batch->maxx, maxx);
1457 batch->maxy = MIN2(batch->maxy, maxy);
1458 }
1459
1460 /* Are we currently rendering to the dev (rather than an FBO)? */
1461
1462 bool
1463 panfrost_batch_is_scanout(struct panfrost_batch *batch)
1464 {
1465 /* If there is no color buffer, it's an FBO */
1466 if (batch->key.nr_cbufs != 1)
1467 return false;
1468
1469 /* If we're too early that no framebuffer was sent, it's scanout */
1470 if (!batch->key.cbufs[0])
1471 return true;
1472
1473 return batch->key.cbufs[0]->texture->bind & PIPE_BIND_DISPLAY_TARGET ||
1474 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SCANOUT ||
1475 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SHARED;
1476 }
1477
1478 void
1479 panfrost_batch_init(struct panfrost_context *ctx)
1480 {
1481 ctx->batches = _mesa_hash_table_create(ctx,
1482 panfrost_batch_hash,
1483 panfrost_batch_compare);
1484 ctx->accessed_bos = _mesa_hash_table_create(ctx, _mesa_hash_pointer,
1485 _mesa_key_pointer_equal);
1486 }