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