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