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