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