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