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