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