panfrost: Skip specifying in_syncs
[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;
1005 int ret;
1006
1007 submit.out_sync = batch->out_sync->syncobj;
1008 submit.jc = first_job_desc;
1009 submit.requirements = reqs;
1010
1011 bo_handles = calloc(batch->pool.bos->entries + batch->bos->entries, sizeof(*bo_handles));
1012 assert(bo_handles);
1013
1014 hash_table_foreach(batch->bos, entry)
1015 panfrost_batch_record_bo(entry, bo_handles, submit.bo_handle_count++);
1016
1017 hash_table_foreach(batch->pool.bos, entry)
1018 panfrost_batch_record_bo(entry, bo_handles, submit.bo_handle_count++);
1019
1020 submit.bo_handles = (u64) (uintptr_t) bo_handles;
1021 ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
1022 free(bo_handles);
1023
1024 if (ret) {
1025 if (dev->debug & PAN_DBG_MSGS)
1026 fprintf(stderr, "Error submitting: %m\n");
1027
1028 return errno;
1029 }
1030
1031 /* Trace the job if we're doing that */
1032 if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
1033 /* Wait so we can get errors reported back */
1034 drmSyncobjWait(dev->fd, &batch->out_sync->syncobj, 1,
1035 INT64_MAX, 0, NULL);
1036
1037 /* Trace gets priority over sync */
1038 bool minimal = !(dev->debug & PAN_DBG_TRACE);
1039 pandecode_jc(submit.jc, dev->quirks & IS_BIFROST, dev->gpu_id, minimal);
1040 }
1041
1042 return 0;
1043 }
1044
1045 static int
1046 panfrost_batch_submit_jobs(struct panfrost_batch *batch)
1047 {
1048 bool has_draws = batch->scoreboard.first_job;
1049 int ret = 0;
1050
1051 if (has_draws) {
1052 ret = panfrost_batch_submit_ioctl(batch, batch->scoreboard.first_job, 0);
1053 assert(!ret);
1054 }
1055
1056 if (batch->scoreboard.tiler_dep || batch->clear) {
1057 /* Whether we program the fragment job for draws or not depends
1058 * on whether there is any *tiler* activity (so fragment
1059 * shaders). If there are draws but entirely RASTERIZER_DISCARD
1060 * (say, for transform feedback), we want a fragment job that
1061 * *only* clears, since otherwise the tiler structures will be
1062 * uninitialized leading to faults (or state leaks) */
1063
1064 mali_ptr fragjob = panfrost_fragment_job(batch,
1065 batch->scoreboard.tiler_dep != 0);
1066 ret = panfrost_batch_submit_ioctl(batch, fragjob, PANFROST_JD_REQ_FS);
1067 assert(!ret);
1068 }
1069
1070 return ret;
1071 }
1072
1073 static void
1074 panfrost_batch_submit(struct panfrost_batch *batch)
1075 {
1076 assert(batch);
1077 struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
1078
1079 /* Submit the dependencies first. */
1080 util_dynarray_foreach(&batch->dependencies,
1081 struct panfrost_batch_fence *, dep) {
1082 if ((*dep)->batch)
1083 panfrost_batch_submit((*dep)->batch);
1084 }
1085
1086 int ret;
1087
1088 /* Nothing to do! */
1089 if (!batch->scoreboard.first_job && !batch->clear) {
1090 /* Mark the fence as signaled so the fence logic does not try
1091 * to wait on it.
1092 */
1093 batch->out_sync->signaled = true;
1094 goto out;
1095 }
1096
1097 panfrost_batch_draw_wallpaper(batch);
1098
1099 /* Now that all draws are in, we can finally prepare the
1100 * FBD for the batch */
1101
1102 if (batch->framebuffer.gpu && batch->scoreboard.first_job) {
1103 struct panfrost_context *ctx = batch->ctx;
1104 struct pipe_context *gallium = (struct pipe_context *) ctx;
1105 struct panfrost_device *dev = pan_device(gallium->screen);
1106
1107 if (dev->quirks & MIDGARD_SFBD)
1108 panfrost_attach_sfbd(batch, ~0);
1109 else
1110 panfrost_attach_mfbd(batch, ~0);
1111 }
1112
1113 mali_ptr polygon_list = panfrost_batch_get_polygon_list(batch,
1114 MALI_TILER_MINIMUM_HEADER_SIZE);
1115
1116 panfrost_scoreboard_initialize_tiler(&batch->pool, &batch->scoreboard, polygon_list);
1117
1118 ret = panfrost_batch_submit_jobs(batch);
1119
1120 if (ret && dev->debug & PAN_DBG_MSGS)
1121 fprintf(stderr, "panfrost_batch_submit failed: %d\n", ret);
1122
1123 /* We must reset the damage info of our render targets here even
1124 * though a damage reset normally happens when the DRI layer swaps
1125 * buffers. That's because there can be implicit flushes the GL
1126 * app is not aware of, and those might impact the damage region: if
1127 * part of the damaged portion is drawn during those implicit flushes,
1128 * you have to reload those areas before next draws are pushed, and
1129 * since the driver can't easily know what's been modified by the draws
1130 * it flushed, the easiest solution is to reload everything.
1131 */
1132 for (unsigned i = 0; i < batch->key.nr_cbufs; i++) {
1133 if (!batch->key.cbufs[i])
1134 continue;
1135
1136 panfrost_resource_set_damage_region(NULL,
1137 batch->key.cbufs[i]->texture, 0, NULL);
1138 }
1139
1140 out:
1141 panfrost_freeze_batch(batch);
1142 panfrost_free_batch(batch);
1143 }
1144
1145 void
1146 panfrost_flush_all_batches(struct panfrost_context *ctx)
1147 {
1148 hash_table_foreach(ctx->batches, hentry) {
1149 struct panfrost_batch *batch = hentry->data;
1150 assert(batch);
1151
1152 panfrost_batch_submit(batch);
1153 }
1154
1155 assert(!ctx->batches->entries);
1156
1157 /* Collect batch fences before returning */
1158 panfrost_gc_fences(ctx);
1159 }
1160
1161 bool
1162 panfrost_pending_batches_access_bo(struct panfrost_context *ctx,
1163 const struct panfrost_bo *bo)
1164 {
1165 struct panfrost_bo_access *access;
1166 struct hash_entry *hentry;
1167
1168 hentry = _mesa_hash_table_search(ctx->accessed_bos, bo);
1169 access = hentry ? hentry->data : NULL;
1170 if (!access)
1171 return false;
1172
1173 if (access->writer && access->writer->batch)
1174 return true;
1175
1176 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
1177 reader) {
1178 if (*reader && (*reader)->batch)
1179 return true;
1180 }
1181
1182 return false;
1183 }
1184
1185 /* We always flush writers. We might also need to flush readers */
1186
1187 void
1188 panfrost_flush_batches_accessing_bo(struct panfrost_context *ctx,
1189 struct panfrost_bo *bo,
1190 bool flush_readers)
1191 {
1192 struct panfrost_bo_access *access;
1193 struct hash_entry *hentry;
1194
1195 hentry = _mesa_hash_table_search(ctx->accessed_bos, bo);
1196 access = hentry ? hentry->data : NULL;
1197 if (!access)
1198 return;
1199
1200 if (access->writer && access->writer->batch)
1201 panfrost_batch_submit(access->writer->batch);
1202
1203 if (!flush_readers)
1204 return;
1205
1206 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
1207 reader) {
1208 if (*reader && (*reader)->batch)
1209 panfrost_batch_submit((*reader)->batch);
1210 }
1211 }
1212
1213 void
1214 panfrost_batch_set_requirements(struct panfrost_batch *batch)
1215 {
1216 struct panfrost_context *ctx = batch->ctx;
1217
1218 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
1219 batch->requirements |= PAN_REQ_MSAA;
1220
1221 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask) {
1222 batch->requirements |= PAN_REQ_DEPTH_WRITE;
1223 batch->draws |= PIPE_CLEAR_DEPTH;
1224 }
1225
1226 if (ctx->depth_stencil && ctx->depth_stencil->stencil[0].enabled)
1227 batch->draws |= PIPE_CLEAR_STENCIL;
1228 }
1229
1230 void
1231 panfrost_batch_adjust_stack_size(struct panfrost_batch *batch)
1232 {
1233 struct panfrost_context *ctx = batch->ctx;
1234
1235 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
1236 struct panfrost_shader_state *ss;
1237
1238 ss = panfrost_get_shader_state(ctx, i);
1239 if (!ss)
1240 continue;
1241
1242 batch->stack_size = MAX2(batch->stack_size, ss->stack_size);
1243 }
1244 }
1245
1246 /* Helper to smear a 32-bit color across 128-bit components */
1247
1248 static void
1249 pan_pack_color_32(uint32_t *packed, uint32_t v)
1250 {
1251 for (unsigned i = 0; i < 4; ++i)
1252 packed[i] = v;
1253 }
1254
1255 static void
1256 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
1257 {
1258 for (unsigned i = 0; i < 4; i += 2) {
1259 packed[i + 0] = lo;
1260 packed[i + 1] = hi;
1261 }
1262 }
1263
1264 static void
1265 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
1266 {
1267 /* Alpha magicked to 1.0 if there is no alpha */
1268
1269 bool has_alpha = util_format_has_alpha(format);
1270 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
1271
1272 /* Packed color depends on the framebuffer format */
1273
1274 const struct util_format_description *desc =
1275 util_format_description(format);
1276
1277 if (util_format_is_rgba8_variant(desc) && desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
1278 pan_pack_color_32(packed,
1279 ((uint32_t) float_to_ubyte(clear_alpha) << 24) |
1280 ((uint32_t) float_to_ubyte(color->f[2]) << 16) |
1281 ((uint32_t) float_to_ubyte(color->f[1]) << 8) |
1282 ((uint32_t) float_to_ubyte(color->f[0]) << 0));
1283 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
1284 /* First, we convert the components to R5, G6, B5 separately */
1285 unsigned r5 = _mesa_roundevenf(SATURATE(color->f[0]) * 31.0);
1286 unsigned g6 = _mesa_roundevenf(SATURATE(color->f[1]) * 63.0);
1287 unsigned b5 = _mesa_roundevenf(SATURATE(color->f[2]) * 31.0);
1288
1289 /* Then we pack into a sparse u32. TODO: Why these shifts? */
1290 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
1291 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
1292 /* Convert to 4-bits */
1293 unsigned r4 = _mesa_roundevenf(SATURATE(color->f[0]) * 15.0);
1294 unsigned g4 = _mesa_roundevenf(SATURATE(color->f[1]) * 15.0);
1295 unsigned b4 = _mesa_roundevenf(SATURATE(color->f[2]) * 15.0);
1296 unsigned a4 = _mesa_roundevenf(SATURATE(clear_alpha) * 15.0);
1297
1298 /* Pack on *byte* intervals */
1299 pan_pack_color_32(packed, (a4 << 28) | (b4 << 20) | (g4 << 12) | (r4 << 4));
1300 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
1301 /* Scale as expected but shift oddly */
1302 unsigned r5 = _mesa_roundevenf(SATURATE(color->f[0]) * 31.0);
1303 unsigned g5 = _mesa_roundevenf(SATURATE(color->f[1]) * 31.0);
1304 unsigned b5 = _mesa_roundevenf(SATURATE(color->f[2]) * 31.0);
1305 unsigned a1 = _mesa_roundevenf(SATURATE(clear_alpha) * 1.0);
1306
1307 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
1308 } else {
1309 /* Otherwise, it's generic subject to replication */
1310
1311 union util_color out = { 0 };
1312 unsigned size = util_format_get_blocksize(format);
1313
1314 util_pack_color(color->f, format, &out);
1315
1316 if (size == 1) {
1317 unsigned b = out.ui[0];
1318 unsigned s = b | (b << 8);
1319 pan_pack_color_32(packed, s | (s << 16));
1320 } else if (size == 2)
1321 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
1322 else if (size == 3 || size == 4)
1323 pan_pack_color_32(packed, out.ui[0]);
1324 else if (size == 6)
1325 pan_pack_color_64(packed, out.ui[0], out.ui[1] | (out.ui[1] << 16)); /* RGB16F -- RGBB */
1326 else if (size == 8)
1327 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
1328 else if (size == 16)
1329 memcpy(packed, out.ui, 16);
1330 else
1331 unreachable("Unknown generic format size packing clear colour");
1332 }
1333 }
1334
1335 void
1336 panfrost_batch_clear(struct panfrost_batch *batch,
1337 unsigned buffers,
1338 const union pipe_color_union *color,
1339 double depth, unsigned stencil)
1340 {
1341 struct panfrost_context *ctx = batch->ctx;
1342
1343 if (buffers & PIPE_CLEAR_COLOR) {
1344 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
1345 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
1346 continue;
1347
1348 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
1349 pan_pack_color(batch->clear_color[i], color, format);
1350 }
1351 }
1352
1353 if (buffers & PIPE_CLEAR_DEPTH) {
1354 batch->clear_depth = depth;
1355 }
1356
1357 if (buffers & PIPE_CLEAR_STENCIL) {
1358 batch->clear_stencil = stencil;
1359 }
1360
1361 batch->clear |= buffers;
1362
1363 /* Clearing affects the entire framebuffer (by definition -- this is
1364 * the Gallium clear callback, which clears the whole framebuffer. If
1365 * the scissor test were enabled from the GL side, the gallium frontend
1366 * would emit a quad instead and we wouldn't go down this code path) */
1367
1368 panfrost_batch_union_scissor(batch, 0, 0,
1369 ctx->pipe_framebuffer.width,
1370 ctx->pipe_framebuffer.height);
1371 }
1372
1373 static bool
1374 panfrost_batch_compare(const void *a, const void *b)
1375 {
1376 return util_framebuffer_state_equal(a, b);
1377 }
1378
1379 static uint32_t
1380 panfrost_batch_hash(const void *key)
1381 {
1382 return _mesa_hash_data(key, sizeof(struct pipe_framebuffer_state));
1383 }
1384
1385 /* Given a new bounding rectangle (scissor), let the job cover the union of the
1386 * new and old bounding rectangles */
1387
1388 void
1389 panfrost_batch_union_scissor(struct panfrost_batch *batch,
1390 unsigned minx, unsigned miny,
1391 unsigned maxx, unsigned maxy)
1392 {
1393 batch->minx = MIN2(batch->minx, minx);
1394 batch->miny = MIN2(batch->miny, miny);
1395 batch->maxx = MAX2(batch->maxx, maxx);
1396 batch->maxy = MAX2(batch->maxy, maxy);
1397 }
1398
1399 void
1400 panfrost_batch_intersection_scissor(struct panfrost_batch *batch,
1401 unsigned minx, unsigned miny,
1402 unsigned maxx, unsigned maxy)
1403 {
1404 batch->minx = MAX2(batch->minx, minx);
1405 batch->miny = MAX2(batch->miny, miny);
1406 batch->maxx = MIN2(batch->maxx, maxx);
1407 batch->maxy = MIN2(batch->maxy, maxy);
1408 }
1409
1410 /* Are we currently rendering to the dev (rather than an FBO)? */
1411
1412 bool
1413 panfrost_batch_is_scanout(struct panfrost_batch *batch)
1414 {
1415 /* If there is no color buffer, it's an FBO */
1416 if (batch->key.nr_cbufs != 1)
1417 return false;
1418
1419 /* If we're too early that no framebuffer was sent, it's scanout */
1420 if (!batch->key.cbufs[0])
1421 return true;
1422
1423 return batch->key.cbufs[0]->texture->bind & PIPE_BIND_DISPLAY_TARGET ||
1424 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SCANOUT ||
1425 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SHARED;
1426 }
1427
1428 void
1429 panfrost_batch_init(struct panfrost_context *ctx)
1430 {
1431 ctx->batches = _mesa_hash_table_create(ctx,
1432 panfrost_batch_hash,
1433 panfrost_batch_compare);
1434 ctx->accessed_bos = _mesa_hash_table_create(ctx, _mesa_hash_pointer,
1435 _mesa_key_pointer_equal);
1436 }