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