freedreno/a6xx: Fix VFD_CONTROL emit
[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 if (rsrc->separate_stencil)
602 panfrost_batch_add_bo(batch, rsrc->separate_stencil->bo, flags);
603 }
604
605 void panfrost_batch_add_fbo_bos(struct panfrost_batch *batch)
606 {
607 uint32_t flags = PAN_BO_ACCESS_SHARED | PAN_BO_ACCESS_WRITE |
608 PAN_BO_ACCESS_VERTEX_TILER |
609 PAN_BO_ACCESS_FRAGMENT;
610
611 for (unsigned i = 0; i < batch->key.nr_cbufs; ++i) {
612 struct panfrost_resource *rsrc = pan_resource(batch->key.cbufs[i]->texture);
613 panfrost_batch_add_resource_bos(batch, rsrc, flags);
614 }
615
616 if (batch->key.zsbuf) {
617 struct panfrost_resource *rsrc = pan_resource(batch->key.zsbuf->texture);
618 panfrost_batch_add_resource_bos(batch, rsrc, flags);
619 }
620 }
621
622 struct panfrost_bo *
623 panfrost_batch_create_bo(struct panfrost_batch *batch, size_t size,
624 uint32_t create_flags, uint32_t access_flags)
625 {
626 struct panfrost_bo *bo;
627
628 bo = pan_bo_create(pan_device(batch->ctx->base.screen), size,
629 create_flags);
630 panfrost_batch_add_bo(batch, bo, access_flags);
631
632 /* panfrost_batch_add_bo() has retained a reference and
633 * pan_bo_create() initialize the refcnt to 1, so let's
634 * unreference the BO here so it gets released when the batch is
635 * destroyed (unless it's retained by someone else in the meantime).
636 */
637 panfrost_bo_unreference(bo);
638 return bo;
639 }
640
641 /* Returns the polygon list's GPU address if available, or otherwise allocates
642 * the polygon list. It's perfectly fast to use allocate/free BO directly,
643 * since we'll hit the BO cache and this is one-per-batch anyway. */
644
645 mali_ptr
646 panfrost_batch_get_polygon_list(struct panfrost_batch *batch, unsigned size)
647 {
648 if (batch->polygon_list) {
649 assert(batch->polygon_list->size >= size);
650 } else {
651 /* Create the BO as invisible, as there's no reason to map */
652 size = util_next_power_of_two(size);
653
654 batch->polygon_list = panfrost_batch_create_bo(batch, size,
655 PAN_BO_INVISIBLE,
656 PAN_BO_ACCESS_PRIVATE |
657 PAN_BO_ACCESS_RW |
658 PAN_BO_ACCESS_VERTEX_TILER |
659 PAN_BO_ACCESS_FRAGMENT);
660 }
661
662 return batch->polygon_list->gpu;
663 }
664
665 struct panfrost_bo *
666 panfrost_batch_get_scratchpad(struct panfrost_batch *batch,
667 unsigned shift,
668 unsigned thread_tls_alloc,
669 unsigned core_count)
670 {
671 unsigned size = panfrost_get_total_stack_size(shift,
672 thread_tls_alloc,
673 core_count);
674
675 if (batch->scratchpad) {
676 assert(batch->scratchpad->size >= size);
677 } else {
678 batch->scratchpad = panfrost_batch_create_bo(batch, size,
679 PAN_BO_INVISIBLE,
680 PAN_BO_ACCESS_PRIVATE |
681 PAN_BO_ACCESS_RW |
682 PAN_BO_ACCESS_VERTEX_TILER |
683 PAN_BO_ACCESS_FRAGMENT);
684 }
685
686 return batch->scratchpad;
687 }
688
689 struct panfrost_bo *
690 panfrost_batch_get_shared_memory(struct panfrost_batch *batch,
691 unsigned size,
692 unsigned workgroup_count)
693 {
694 if (batch->shared_memory) {
695 assert(batch->shared_memory->size >= size);
696 } else {
697 batch->shared_memory = panfrost_batch_create_bo(batch, size,
698 PAN_BO_INVISIBLE,
699 PAN_BO_ACCESS_PRIVATE |
700 PAN_BO_ACCESS_RW |
701 PAN_BO_ACCESS_VERTEX_TILER);
702 }
703
704 return batch->shared_memory;
705 }
706
707 struct panfrost_bo *
708 panfrost_batch_get_tiler_heap(struct panfrost_batch *batch)
709 {
710 if (batch->tiler_heap)
711 return batch->tiler_heap;
712
713 batch->tiler_heap = panfrost_batch_create_bo(batch, 4096 * 4096,
714 PAN_BO_INVISIBLE |
715 PAN_BO_GROWABLE,
716 PAN_BO_ACCESS_PRIVATE |
717 PAN_BO_ACCESS_RW |
718 PAN_BO_ACCESS_VERTEX_TILER |
719 PAN_BO_ACCESS_FRAGMENT);
720 assert(batch->tiler_heap);
721 return batch->tiler_heap;
722 }
723
724 mali_ptr
725 panfrost_batch_get_tiler_meta(struct panfrost_batch *batch, unsigned vertex_count)
726 {
727 if (!vertex_count)
728 return 0;
729
730 if (batch->tiler_meta)
731 return batch->tiler_meta;
732
733 struct panfrost_bo *tiler_heap;
734 tiler_heap = panfrost_batch_get_tiler_heap(batch);
735
736 struct bifrost_tiler_heap_meta tiler_heap_meta = {
737 .heap_size = tiler_heap->size,
738 .tiler_heap_start = tiler_heap->gpu,
739 .tiler_heap_free = tiler_heap->gpu,
740 .tiler_heap_end = tiler_heap->gpu + tiler_heap->size,
741 .unk1 = 0x1,
742 .unk7e007e = 0x7e007e,
743 };
744
745 struct bifrost_tiler_meta tiler_meta = {
746 .hierarchy_mask = 0x28,
747 .flags = 0x0,
748 .width = MALI_POSITIVE(batch->key.width),
749 .height = MALI_POSITIVE(batch->key.height),
750 .tiler_heap_meta = panfrost_upload_transient(batch, &tiler_heap_meta, sizeof(tiler_heap_meta)),
751 };
752
753 batch->tiler_meta = panfrost_upload_transient(batch, &tiler_meta, sizeof(tiler_meta));
754 return batch->tiler_meta;
755 }
756
757 struct panfrost_bo *
758 panfrost_batch_get_tiler_dummy(struct panfrost_batch *batch)
759 {
760 struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
761
762 uint32_t create_flags = 0;
763
764 if (batch->tiler_dummy)
765 return batch->tiler_dummy;
766
767 if (!(dev->quirks & MIDGARD_NO_HIER_TILING))
768 create_flags = PAN_BO_INVISIBLE;
769
770 batch->tiler_dummy = panfrost_batch_create_bo(batch, 4096,
771 create_flags,
772 PAN_BO_ACCESS_PRIVATE |
773 PAN_BO_ACCESS_RW |
774 PAN_BO_ACCESS_VERTEX_TILER |
775 PAN_BO_ACCESS_FRAGMENT);
776 assert(batch->tiler_dummy);
777 return batch->tiler_dummy;
778 }
779
780 static void
781 panfrost_batch_draw_wallpaper(struct panfrost_batch *batch)
782 {
783 /* Color 0 is cleared, no need to draw the wallpaper.
784 * TODO: MRT wallpapers.
785 */
786 if (batch->clear & PIPE_CLEAR_COLOR0)
787 return;
788
789 /* Nothing to reload? TODO: MRT wallpapers */
790 if (batch->key.cbufs[0] == NULL)
791 return;
792
793 /* No draw calls, and no clear on the depth/stencil bufs.
794 * Drawing the wallpaper would be useless.
795 */
796 if (!batch->tiler_dep &&
797 !(batch->clear & PIPE_CLEAR_DEPTHSTENCIL))
798 return;
799
800 /* Check if the buffer has any content on it worth preserving */
801
802 struct pipe_surface *surf = batch->key.cbufs[0];
803 struct panfrost_resource *rsrc = pan_resource(surf->texture);
804 unsigned level = surf->u.tex.level;
805
806 if (!rsrc->slices[level].initialized)
807 return;
808
809 batch->ctx->wallpaper_batch = batch;
810
811 /* Clamp the rendering area to the damage extent. The
812 * KHR_partial_update() spec states that trying to render outside of
813 * the damage region is "undefined behavior", so we should be safe.
814 */
815 unsigned damage_width = (rsrc->damage.extent.maxx - rsrc->damage.extent.minx);
816 unsigned damage_height = (rsrc->damage.extent.maxy - rsrc->damage.extent.miny);
817
818 if (damage_width && damage_height) {
819 panfrost_batch_intersection_scissor(batch,
820 rsrc->damage.extent.minx,
821 rsrc->damage.extent.miny,
822 rsrc->damage.extent.maxx,
823 rsrc->damage.extent.maxy);
824 }
825
826 /* FIXME: Looks like aligning on a tile is not enough, but
827 * aligning on twice the tile size seems to works. We don't
828 * know exactly what happens here but this deserves extra
829 * investigation to figure it out.
830 */
831 batch->minx = batch->minx & ~((MALI_TILE_LENGTH * 2) - 1);
832 batch->miny = batch->miny & ~((MALI_TILE_LENGTH * 2) - 1);
833 batch->maxx = MIN2(ALIGN_POT(batch->maxx, MALI_TILE_LENGTH * 2),
834 rsrc->base.width0);
835 batch->maxy = MIN2(ALIGN_POT(batch->maxy, MALI_TILE_LENGTH * 2),
836 rsrc->base.height0);
837
838 struct pipe_scissor_state damage;
839 struct pipe_box rects[4];
840
841 /* Clamp the damage box to the rendering area. */
842 damage.minx = MAX2(batch->minx, rsrc->damage.biggest_rect.x);
843 damage.miny = MAX2(batch->miny, rsrc->damage.biggest_rect.y);
844 damage.maxx = MIN2(batch->maxx,
845 rsrc->damage.biggest_rect.x +
846 rsrc->damage.biggest_rect.width);
847 damage.maxx = MAX2(damage.maxx, damage.minx);
848 damage.maxy = MIN2(batch->maxy,
849 rsrc->damage.biggest_rect.y +
850 rsrc->damage.biggest_rect.height);
851 damage.maxy = MAX2(damage.maxy, damage.miny);
852
853 /* One damage rectangle means we can end up with at most 4 reload
854 * regions:
855 * 1: left region, only exists if damage.x > 0
856 * 2: right region, only exists if damage.x + damage.width < fb->width
857 * 3: top region, only exists if damage.y > 0. The intersection with
858 * the left and right regions are dropped
859 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
860 * The intersection with the left and right regions are dropped
861 *
862 * ____________________________
863 * | | 3 | |
864 * | |___________| |
865 * | | damage | |
866 * | 1 | rect | 2 |
867 * | |___________| |
868 * | | 4 | |
869 * |_______|___________|______|
870 */
871 u_box_2d(batch->minx, batch->miny, damage.minx - batch->minx,
872 batch->maxy - batch->miny, &rects[0]);
873 u_box_2d(damage.maxx, batch->miny, batch->maxx - damage.maxx,
874 batch->maxy - batch->miny, &rects[1]);
875 u_box_2d(damage.minx, batch->miny, damage.maxx - damage.minx,
876 damage.miny - batch->miny, &rects[2]);
877 u_box_2d(damage.minx, damage.maxy, damage.maxx - damage.minx,
878 batch->maxy - damage.maxy, &rects[3]);
879
880 for (unsigned i = 0; i < 4; i++) {
881 /* Width and height are always >= 0 even if width is declared as a
882 * signed integer: u_box_2d() helper takes unsigned args and
883 * panfrost_set_damage_region() is taking care of clamping
884 * negative values.
885 */
886 if (!rects[i].width || !rects[i].height)
887 continue;
888
889 /* Blit the wallpaper in */
890 panfrost_blit_wallpaper(batch->ctx, &rects[i]);
891 }
892 batch->ctx->wallpaper_batch = NULL;
893 }
894
895 static int
896 panfrost_batch_submit_ioctl(struct panfrost_batch *batch,
897 mali_ptr first_job_desc,
898 uint32_t reqs)
899 {
900 struct panfrost_context *ctx = batch->ctx;
901 struct pipe_context *gallium = (struct pipe_context *) ctx;
902 struct panfrost_device *dev = pan_device(gallium->screen);
903 struct drm_panfrost_submit submit = {0,};
904 uint32_t *bo_handles, *in_syncs = NULL;
905 bool is_fragment_shader;
906 int ret;
907
908 is_fragment_shader = (reqs & PANFROST_JD_REQ_FS) && batch->first_job;
909 if (is_fragment_shader)
910 submit.in_sync_count = 1;
911 else
912 submit.in_sync_count = util_dynarray_num_elements(&batch->dependencies,
913 struct panfrost_batch_fence *);
914
915 if (submit.in_sync_count) {
916 in_syncs = calloc(submit.in_sync_count, sizeof(*in_syncs));
917 assert(in_syncs);
918 }
919
920 /* The fragment job always depends on the vertex/tiler job if there's
921 * one
922 */
923 if (is_fragment_shader) {
924 in_syncs[0] = batch->out_sync->syncobj;
925 } else {
926 unsigned int i = 0;
927
928 util_dynarray_foreach(&batch->dependencies,
929 struct panfrost_batch_fence *, dep)
930 in_syncs[i++] = (*dep)->syncobj;
931 }
932
933 submit.in_syncs = (uintptr_t)in_syncs;
934 submit.out_sync = batch->out_sync->syncobj;
935 submit.jc = first_job_desc;
936 submit.requirements = reqs;
937
938 bo_handles = calloc(batch->bos->entries, sizeof(*bo_handles));
939 assert(bo_handles);
940
941 hash_table_foreach(batch->bos, entry) {
942 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
943 uint32_t flags = (uintptr_t)entry->data;
944
945 assert(bo->gem_handle > 0);
946 bo_handles[submit.bo_handle_count++] = bo->gem_handle;
947
948 /* Update the BO access flags so that panfrost_bo_wait() knows
949 * about all pending accesses.
950 * We only keep the READ/WRITE info since this is all the BO
951 * wait logic cares about.
952 * We also preserve existing flags as this batch might not
953 * be the first one to access the BO.
954 */
955 bo->gpu_access |= flags & (PAN_BO_ACCESS_RW);
956 }
957
958 submit.bo_handles = (u64) (uintptr_t) bo_handles;
959 ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
960 free(bo_handles);
961 free(in_syncs);
962
963 if (ret) {
964 DBG("Error submitting: %m\n");
965 return errno;
966 }
967
968 /* Trace the job if we're doing that */
969 if (pan_debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
970 /* Wait so we can get errors reported back */
971 drmSyncobjWait(dev->fd, &batch->out_sync->syncobj, 1,
972 INT64_MAX, 0, NULL);
973
974 /* Trace gets priority over sync */
975 bool minimal = !(pan_debug & PAN_DBG_TRACE);
976 pandecode_jc(submit.jc, dev->quirks & IS_BIFROST, dev->gpu_id, minimal);
977 }
978
979 return 0;
980 }
981
982 static int
983 panfrost_batch_submit_jobs(struct panfrost_batch *batch)
984 {
985 bool has_draws = batch->first_job;
986 int ret = 0;
987
988 if (has_draws) {
989 ret = panfrost_batch_submit_ioctl(batch, batch->first_job, 0);
990 assert(!ret);
991 }
992
993 if (batch->tiler_dep || batch->clear) {
994 mali_ptr fragjob = panfrost_fragment_job(batch, has_draws);
995 ret = panfrost_batch_submit_ioctl(batch, fragjob, PANFROST_JD_REQ_FS);
996 assert(!ret);
997 }
998
999 return ret;
1000 }
1001
1002 static void
1003 panfrost_batch_submit(struct panfrost_batch *batch)
1004 {
1005 assert(batch);
1006
1007 /* Submit the dependencies first. */
1008 util_dynarray_foreach(&batch->dependencies,
1009 struct panfrost_batch_fence *, dep) {
1010 if ((*dep)->batch)
1011 panfrost_batch_submit((*dep)->batch);
1012 }
1013
1014 int ret;
1015
1016 /* Nothing to do! */
1017 if (!batch->first_job && !batch->clear) {
1018 /* Mark the fence as signaled so the fence logic does not try
1019 * to wait on it.
1020 */
1021 batch->out_sync->signaled = true;
1022 goto out;
1023 }
1024
1025 panfrost_batch_draw_wallpaper(batch);
1026
1027 /* Now that all draws are in, we can finally prepare the
1028 * FBD for the batch */
1029
1030 if (batch->framebuffer.gpu && batch->first_job) {
1031 struct panfrost_context *ctx = batch->ctx;
1032 struct pipe_context *gallium = (struct pipe_context *) ctx;
1033 struct panfrost_device *dev = pan_device(gallium->screen);
1034
1035 if (dev->quirks & MIDGARD_SFBD)
1036 panfrost_attach_sfbd(batch, ~0);
1037 else
1038 panfrost_attach_mfbd(batch, ~0);
1039 }
1040
1041 panfrost_scoreboard_initialize_tiler(batch);
1042
1043 ret = panfrost_batch_submit_jobs(batch);
1044
1045 if (ret)
1046 DBG("panfrost_batch_submit failed: %d\n", ret);
1047
1048 /* We must reset the damage info of our render targets here even
1049 * though a damage reset normally happens when the DRI layer swaps
1050 * buffers. That's because there can be implicit flushes the GL
1051 * app is not aware of, and those might impact the damage region: if
1052 * part of the damaged portion is drawn during those implicit flushes,
1053 * you have to reload those areas before next draws are pushed, and
1054 * since the driver can't easily know what's been modified by the draws
1055 * it flushed, the easiest solution is to reload everything.
1056 */
1057 for (unsigned i = 0; i < batch->key.nr_cbufs; i++) {
1058 struct panfrost_resource *res;
1059
1060 if (!batch->key.cbufs[i])
1061 continue;
1062
1063 res = pan_resource(batch->key.cbufs[i]->texture);
1064 panfrost_resource_reset_damage(res);
1065 }
1066
1067 out:
1068 panfrost_freeze_batch(batch);
1069 panfrost_free_batch(batch);
1070 }
1071
1072 void
1073 panfrost_flush_all_batches(struct panfrost_context *ctx, bool wait)
1074 {
1075 struct util_dynarray fences, syncobjs;
1076
1077 if (wait) {
1078 util_dynarray_init(&fences, NULL);
1079 util_dynarray_init(&syncobjs, NULL);
1080 }
1081
1082 hash_table_foreach(ctx->batches, hentry) {
1083 struct panfrost_batch *batch = hentry->data;
1084
1085 assert(batch);
1086
1087 if (wait) {
1088 panfrost_batch_fence_reference(batch->out_sync);
1089 util_dynarray_append(&fences, struct panfrost_batch_fence *,
1090 batch->out_sync);
1091 util_dynarray_append(&syncobjs, uint32_t,
1092 batch->out_sync->syncobj);
1093 }
1094
1095 panfrost_batch_submit(batch);
1096 }
1097
1098 assert(!ctx->batches->entries);
1099
1100 /* Collect batch fences before returning */
1101 panfrost_gc_fences(ctx);
1102
1103 if (!wait)
1104 return;
1105
1106 drmSyncobjWait(pan_device(ctx->base.screen)->fd,
1107 util_dynarray_begin(&syncobjs),
1108 util_dynarray_num_elements(&syncobjs, uint32_t),
1109 INT64_MAX, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL, NULL);
1110
1111 util_dynarray_foreach(&fences, struct panfrost_batch_fence *, fence)
1112 panfrost_batch_fence_unreference(*fence);
1113
1114 util_dynarray_fini(&fences);
1115 util_dynarray_fini(&syncobjs);
1116 }
1117
1118 bool
1119 panfrost_pending_batches_access_bo(struct panfrost_context *ctx,
1120 const struct panfrost_bo *bo)
1121 {
1122 struct panfrost_bo_access *access;
1123 struct hash_entry *hentry;
1124
1125 hentry = _mesa_hash_table_search(ctx->accessed_bos, bo);
1126 access = hentry ? hentry->data : NULL;
1127 if (!access)
1128 return false;
1129
1130 if (access->writer && access->writer->batch)
1131 return true;
1132
1133 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
1134 reader) {
1135 if (*reader && (*reader)->batch)
1136 return true;
1137 }
1138
1139 return false;
1140 }
1141
1142 void
1143 panfrost_flush_batches_accessing_bo(struct panfrost_context *ctx,
1144 struct panfrost_bo *bo,
1145 uint32_t access_type)
1146 {
1147 struct panfrost_bo_access *access;
1148 struct hash_entry *hentry;
1149
1150 /* It doesn't make any to flush only the readers. */
1151 assert(access_type == PAN_BO_ACCESS_WRITE ||
1152 access_type == PAN_BO_ACCESS_RW);
1153
1154 hentry = _mesa_hash_table_search(ctx->accessed_bos, bo);
1155 access = hentry ? hentry->data : NULL;
1156 if (!access)
1157 return;
1158
1159 if (access_type & PAN_BO_ACCESS_WRITE && access->writer &&
1160 access->writer->batch)
1161 panfrost_batch_submit(access->writer->batch);
1162
1163 if (!(access_type & PAN_BO_ACCESS_READ))
1164 return;
1165
1166 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
1167 reader) {
1168 if (*reader && (*reader)->batch)
1169 panfrost_batch_submit((*reader)->batch);
1170 }
1171 }
1172
1173 void
1174 panfrost_batch_set_requirements(struct panfrost_batch *batch)
1175 {
1176 struct panfrost_context *ctx = batch->ctx;
1177
1178 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
1179 batch->requirements |= PAN_REQ_MSAA;
1180
1181 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask)
1182 batch->requirements |= PAN_REQ_DEPTH_WRITE;
1183 }
1184
1185 void
1186 panfrost_batch_adjust_stack_size(struct panfrost_batch *batch)
1187 {
1188 struct panfrost_context *ctx = batch->ctx;
1189
1190 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
1191 struct panfrost_shader_state *ss;
1192
1193 ss = panfrost_get_shader_state(ctx, i);
1194 if (!ss)
1195 continue;
1196
1197 batch->stack_size = MAX2(batch->stack_size, ss->stack_size);
1198 }
1199 }
1200
1201 /* Helper to smear a 32-bit color across 128-bit components */
1202
1203 static void
1204 pan_pack_color_32(uint32_t *packed, uint32_t v)
1205 {
1206 for (unsigned i = 0; i < 4; ++i)
1207 packed[i] = v;
1208 }
1209
1210 static void
1211 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
1212 {
1213 for (unsigned i = 0; i < 4; i += 2) {
1214 packed[i + 0] = lo;
1215 packed[i + 1] = hi;
1216 }
1217 }
1218
1219 static void
1220 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
1221 {
1222 /* Alpha magicked to 1.0 if there is no alpha */
1223
1224 bool has_alpha = util_format_has_alpha(format);
1225 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
1226
1227 /* Packed color depends on the framebuffer format */
1228
1229 const struct util_format_description *desc =
1230 util_format_description(format);
1231
1232 if (util_format_is_rgba8_variant(desc)) {
1233 pan_pack_color_32(packed,
1234 ((uint32_t) float_to_ubyte(clear_alpha) << 24) |
1235 ((uint32_t) float_to_ubyte(color->f[2]) << 16) |
1236 ((uint32_t) float_to_ubyte(color->f[1]) << 8) |
1237 ((uint32_t) float_to_ubyte(color->f[0]) << 0));
1238 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
1239 /* First, we convert the components to R5, G6, B5 separately */
1240 unsigned r5 = _mesa_roundevenf(SATURATE(color->f[0]) * 31.0);
1241 unsigned g6 = _mesa_roundevenf(SATURATE(color->f[1]) * 63.0);
1242 unsigned b5 = _mesa_roundevenf(SATURATE(color->f[2]) * 31.0);
1243
1244 /* Then we pack into a sparse u32. TODO: Why these shifts? */
1245 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
1246 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
1247 /* Convert to 4-bits */
1248 unsigned r4 = _mesa_roundevenf(SATURATE(color->f[0]) * 15.0);
1249 unsigned g4 = _mesa_roundevenf(SATURATE(color->f[1]) * 15.0);
1250 unsigned b4 = _mesa_roundevenf(SATURATE(color->f[2]) * 15.0);
1251 unsigned a4 = _mesa_roundevenf(SATURATE(clear_alpha) * 15.0);
1252
1253 /* Pack on *byte* intervals */
1254 pan_pack_color_32(packed, (a4 << 28) | (b4 << 20) | (g4 << 12) | (r4 << 4));
1255 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
1256 /* Scale as expected but shift oddly */
1257 unsigned r5 = _mesa_roundevenf(SATURATE(color->f[0]) * 31.0);
1258 unsigned g5 = _mesa_roundevenf(SATURATE(color->f[1]) * 31.0);
1259 unsigned b5 = _mesa_roundevenf(SATURATE(color->f[2]) * 31.0);
1260 unsigned a1 = _mesa_roundevenf(SATURATE(clear_alpha) * 1.0);
1261
1262 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
1263 } else {
1264 /* Otherwise, it's generic subject to replication */
1265
1266 union util_color out = { 0 };
1267 unsigned size = util_format_get_blocksize(format);
1268
1269 util_pack_color(color->f, format, &out);
1270
1271 if (size == 1) {
1272 unsigned b = out.ui[0];
1273 unsigned s = b | (b << 8);
1274 pan_pack_color_32(packed, s | (s << 16));
1275 } else if (size == 2)
1276 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
1277 else if (size == 3 || size == 4)
1278 pan_pack_color_32(packed, out.ui[0]);
1279 else if (size == 6)
1280 pan_pack_color_64(packed, out.ui[0], out.ui[1] | (out.ui[1] << 16)); /* RGB16F -- RGBB */
1281 else if (size == 8)
1282 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
1283 else if (size == 16)
1284 memcpy(packed, out.ui, 16);
1285 else
1286 unreachable("Unknown generic format size packing clear colour");
1287 }
1288 }
1289
1290 void
1291 panfrost_batch_clear(struct panfrost_batch *batch,
1292 unsigned buffers,
1293 const union pipe_color_union *color,
1294 double depth, unsigned stencil)
1295 {
1296 struct panfrost_context *ctx = batch->ctx;
1297
1298 if (buffers & PIPE_CLEAR_COLOR) {
1299 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
1300 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
1301 continue;
1302
1303 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
1304 pan_pack_color(batch->clear_color[i], color, format);
1305 }
1306 }
1307
1308 if (buffers & PIPE_CLEAR_DEPTH) {
1309 batch->clear_depth = depth;
1310 }
1311
1312 if (buffers & PIPE_CLEAR_STENCIL) {
1313 batch->clear_stencil = stencil;
1314 }
1315
1316 batch->clear |= buffers;
1317
1318 /* Clearing affects the entire framebuffer (by definition -- this is
1319 * the Gallium clear callback, which clears the whole framebuffer. If
1320 * the scissor test were enabled from the GL side, the gallium frontend
1321 * would emit a quad instead and we wouldn't go down this code path) */
1322
1323 panfrost_batch_union_scissor(batch, 0, 0,
1324 ctx->pipe_framebuffer.width,
1325 ctx->pipe_framebuffer.height);
1326 }
1327
1328 static bool
1329 panfrost_batch_compare(const void *a, const void *b)
1330 {
1331 return util_framebuffer_state_equal(a, b);
1332 }
1333
1334 static uint32_t
1335 panfrost_batch_hash(const void *key)
1336 {
1337 return _mesa_hash_data(key, sizeof(struct pipe_framebuffer_state));
1338 }
1339
1340 /* Given a new bounding rectangle (scissor), let the job cover the union of the
1341 * new and old bounding rectangles */
1342
1343 void
1344 panfrost_batch_union_scissor(struct panfrost_batch *batch,
1345 unsigned minx, unsigned miny,
1346 unsigned maxx, unsigned maxy)
1347 {
1348 batch->minx = MIN2(batch->minx, minx);
1349 batch->miny = MIN2(batch->miny, miny);
1350 batch->maxx = MAX2(batch->maxx, maxx);
1351 batch->maxy = MAX2(batch->maxy, maxy);
1352 }
1353
1354 void
1355 panfrost_batch_intersection_scissor(struct panfrost_batch *batch,
1356 unsigned minx, unsigned miny,
1357 unsigned maxx, unsigned maxy)
1358 {
1359 batch->minx = MAX2(batch->minx, minx);
1360 batch->miny = MAX2(batch->miny, miny);
1361 batch->maxx = MIN2(batch->maxx, maxx);
1362 batch->maxy = MIN2(batch->maxy, maxy);
1363 }
1364
1365 /* Are we currently rendering to the dev (rather than an FBO)? */
1366
1367 bool
1368 panfrost_batch_is_scanout(struct panfrost_batch *batch)
1369 {
1370 /* If there is no color buffer, it's an FBO */
1371 if (batch->key.nr_cbufs != 1)
1372 return false;
1373
1374 /* If we're too early that no framebuffer was sent, it's scanout */
1375 if (!batch->key.cbufs[0])
1376 return true;
1377
1378 return batch->key.cbufs[0]->texture->bind & PIPE_BIND_DISPLAY_TARGET ||
1379 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SCANOUT ||
1380 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SHARED;
1381 }
1382
1383 void
1384 panfrost_batch_init(struct panfrost_context *ctx)
1385 {
1386 ctx->batches = _mesa_hash_table_create(ctx,
1387 panfrost_batch_hash,
1388 panfrost_batch_compare);
1389 ctx->accessed_bos = _mesa_hash_table_create(ctx, _mesa_hash_pointer,
1390 _mesa_key_pointer_equal);
1391 }