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