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