panfrost: Choose AFBC when available
[mesa.git] / src / gallium / drivers / panfrost / pan_resource.c
1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4 * Copyright (C) 2014-2017 Broadcom
5 * Copyright (C) 2018-2019 Alyssa Rosenzweig
6 * Copyright (C) 2019 Collabora, Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * Authors (Collabora):
28 * Tomeu Vizoso <tomeu.vizoso@collabora.com>
29 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
30 *
31 */
32
33 #include <xf86drm.h>
34 #include <fcntl.h>
35 #include "drm-uapi/drm_fourcc.h"
36
37 #include "frontend/winsys_handle.h"
38 #include "util/format/u_format.h"
39 #include "util/u_memory.h"
40 #include "util/u_surface.h"
41 #include "util/u_transfer.h"
42 #include "util/u_transfer_helper.h"
43 #include "util/u_gen_mipmap.h"
44 #include "util/u_drm.h"
45
46 #include "pan_bo.h"
47 #include "pan_context.h"
48 #include "pan_screen.h"
49 #include "pan_resource.h"
50 #include "pan_util.h"
51 #include "pan_tiling.h"
52 #include "decode.h"
53 #include "panfrost-quirks.h"
54
55 static struct pipe_resource *
56 panfrost_resource_from_handle(struct pipe_screen *pscreen,
57 const struct pipe_resource *templat,
58 struct winsys_handle *whandle,
59 unsigned usage)
60 {
61 struct panfrost_device *dev = pan_device(pscreen);
62 struct panfrost_resource *rsc;
63 struct pipe_resource *prsc;
64
65 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
66
67 rsc = rzalloc(pscreen, struct panfrost_resource);
68 if (!rsc)
69 return NULL;
70
71 prsc = &rsc->base;
72
73 *prsc = *templat;
74
75 pipe_reference_init(&prsc->reference, 1);
76 prsc->screen = pscreen;
77
78 rsc->bo = panfrost_bo_import(dev, whandle->handle);
79 rsc->internal_format = templat->format;
80 rsc->modifier = (whandle->modifier == DRM_FORMAT_MOD_INVALID) ?
81 DRM_FORMAT_MOD_LINEAR : whandle->modifier;
82 rsc->slices[0].stride = whandle->stride;
83 rsc->slices[0].offset = whandle->offset;
84 rsc->slices[0].initialized = true;
85 panfrost_resource_set_damage_region(NULL, &rsc->base, 0, NULL);
86
87 if (dev->quirks & IS_BIFROST &&
88 templat->bind & PIPE_BIND_RENDER_TARGET) {
89 unsigned size = panfrost_compute_checksum_size(
90 &rsc->slices[0], templat->width0, templat->height0);
91 rsc->slices[0].checksum_bo = panfrost_bo_create(dev, size, 0);
92 rsc->checksummed = true;
93 }
94
95 if (drm_is_afbc(whandle->modifier)) {
96 rsc->slices[0].header_size =
97 panfrost_afbc_header_size(templat->width0, templat->height0);
98 }
99
100 if (dev->ro) {
101 rsc->scanout =
102 renderonly_create_gpu_import_for_resource(prsc, dev->ro, NULL);
103 /* failure is expected in some cases.. */
104 }
105
106 return prsc;
107 }
108
109 static bool
110 panfrost_resource_get_handle(struct pipe_screen *pscreen,
111 struct pipe_context *ctx,
112 struct pipe_resource *pt,
113 struct winsys_handle *handle,
114 unsigned usage)
115 {
116 struct panfrost_device *dev = pan_device(pscreen);
117 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
118 struct renderonly_scanout *scanout = rsrc->scanout;
119
120 handle->modifier = rsrc->modifier;
121
122 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
123 return false;
124 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
125 if (renderonly_get_handle(scanout, handle))
126 return true;
127
128 handle->handle = rsrc->bo->gem_handle;
129 handle->stride = rsrc->slices[0].stride;
130 handle->offset = rsrc->slices[0].offset;
131 return TRUE;
132 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
133 if (scanout) {
134 struct drm_prime_handle args = {
135 .handle = scanout->handle,
136 .flags = DRM_CLOEXEC,
137 };
138
139 int ret = drmIoctl(dev->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
140 if (ret == -1)
141 return false;
142
143 handle->stride = scanout->stride;
144 handle->handle = args.fd;
145
146 return true;
147 } else {
148 int fd = panfrost_bo_export(rsrc->bo);
149
150 if (fd < 0)
151 return false;
152
153 handle->handle = fd;
154 handle->stride = rsrc->slices[0].stride;
155 handle->offset = rsrc->slices[0].offset;
156 return true;
157 }
158 }
159
160 return false;
161 }
162
163 static void
164 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
165 {
166 /* TODO */
167 }
168
169 static struct pipe_surface *
170 panfrost_create_surface(struct pipe_context *pipe,
171 struct pipe_resource *pt,
172 const struct pipe_surface *surf_tmpl)
173 {
174 struct pipe_surface *ps = NULL;
175
176 ps = rzalloc(pipe, struct pipe_surface);
177
178 if (ps) {
179 pipe_reference_init(&ps->reference, 1);
180 pipe_resource_reference(&ps->texture, pt);
181 ps->context = pipe;
182 ps->format = surf_tmpl->format;
183
184 if (pt->target != PIPE_BUFFER) {
185 assert(surf_tmpl->u.tex.level <= pt->last_level);
186 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
187 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
188 ps->nr_samples = surf_tmpl->nr_samples;
189 ps->u.tex.level = surf_tmpl->u.tex.level;
190 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
191 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
192 } else {
193 /* setting width as number of elements should get us correct renderbuffer width */
194 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
195 ps->height = pt->height0;
196 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
197 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
198 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
199 assert(ps->u.buf.last_element < ps->width);
200 }
201 }
202
203 return ps;
204 }
205
206 static void
207 panfrost_surface_destroy(struct pipe_context *pipe,
208 struct pipe_surface *surf)
209 {
210 assert(surf->texture);
211 pipe_resource_reference(&surf->texture, NULL);
212 ralloc_free(surf);
213 }
214
215 static struct pipe_resource *
216 panfrost_create_scanout_res(struct pipe_screen *screen,
217 const struct pipe_resource *template,
218 uint64_t modifier)
219 {
220 struct panfrost_device *dev = pan_device(screen);
221 struct pipe_resource scanout_templat = *template;
222 struct renderonly_scanout *scanout;
223 struct winsys_handle handle;
224 struct pipe_resource *res;
225
226 scanout = renderonly_scanout_for_resource(&scanout_templat,
227 dev->ro, &handle);
228 if (!scanout)
229 return NULL;
230
231 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
232 handle.modifier = modifier;
233 res = screen->resource_from_handle(screen, template, &handle,
234 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
235 close(handle.handle);
236 if (!res)
237 return NULL;
238
239 struct panfrost_resource *pres = pan_resource(res);
240
241 pres->scanout = scanout;
242
243 return res;
244 }
245
246 /* Setup the mip tree given a particular modifier, possibly with checksumming */
247
248 static void
249 panfrost_setup_slices(struct panfrost_resource *pres, size_t *bo_size)
250 {
251 struct pipe_resource *res = &pres->base;
252 unsigned width = res->width0;
253 unsigned height = res->height0;
254 unsigned depth = res->depth0;
255 unsigned bytes_per_pixel = util_format_get_blocksize(pres->internal_format);
256
257 /* MSAA is implemented as a 3D texture with z corresponding to the
258 * sample #, horrifyingly enough */
259
260 bool msaa = res->nr_samples > 1;
261
262 if (msaa) {
263 assert(depth == 1);
264 depth = res->nr_samples;
265 }
266
267 assert(depth > 0);
268
269 /* Tiled operates blockwise; linear is packed. Also, anything
270 * we render to has to be tile-aligned. Maybe not strictly
271 * necessary, but we're not *that* pressed for memory and it
272 * makes code a lot simpler */
273
274 bool renderable = res->bind &
275 (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL) &&
276 res->target != PIPE_BUFFER;
277 bool afbc = drm_is_afbc(pres->modifier);
278 bool tiled = pres->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
279 bool linear = pres->modifier == DRM_FORMAT_MOD_LINEAR;
280 bool should_align = renderable || tiled;
281
282 /* We don't know how to specify a 2D stride for 3D textures */
283
284 bool can_align_stride =
285 res->target != PIPE_TEXTURE_3D;
286
287 should_align &= can_align_stride;
288
289 unsigned offset = 0;
290 unsigned size_2d = 0;
291
292 for (unsigned l = 0; l <= res->last_level; ++l) {
293 struct panfrost_slice *slice = &pres->slices[l];
294
295 unsigned effective_width = width;
296 unsigned effective_height = height;
297 unsigned effective_depth = depth;
298
299 if (should_align) {
300 effective_width = ALIGN_POT(effective_width, 16);
301 effective_height = ALIGN_POT(effective_height, 16);
302
303 /* We don't need to align depth */
304 }
305
306 /* Align levels to cache-line as a performance improvement for
307 * linear/tiled and as a requirement for AFBC */
308
309 offset = ALIGN_POT(offset, 64);
310
311 slice->offset = offset;
312
313 /* Compute the would-be stride */
314 unsigned stride = bytes_per_pixel * effective_width;
315
316 if (util_format_is_compressed(pres->internal_format))
317 stride /= 4;
318
319 /* ..but cache-line align it for performance */
320 if (can_align_stride && linear)
321 stride = ALIGN_POT(stride, 64);
322
323 slice->stride = stride;
324
325 unsigned slice_one_size = slice->stride * effective_height;
326 unsigned slice_full_size = slice_one_size * effective_depth;
327
328 slice->size0 = slice_one_size;
329
330 /* Report 2D size for 3D texturing */
331
332 if (l == 0)
333 size_2d = slice_one_size;
334
335 /* Compute AFBC sizes if necessary */
336 if (afbc) {
337 slice->header_size =
338 panfrost_afbc_header_size(width, height);
339
340 offset += slice->header_size;
341 }
342
343 offset += slice_full_size;
344
345 /* Add a checksum region if necessary */
346 if (pres->checksummed) {
347 slice->checksum_offset = offset;
348
349 unsigned size = panfrost_compute_checksum_size(
350 slice, width, height);
351
352 offset += size;
353 }
354
355 width = u_minify(width, 1);
356 height = u_minify(height, 1);
357
358 /* Don't mipmap the sample count */
359 if (!msaa)
360 depth = u_minify(depth, 1);
361 }
362
363 assert(res->array_size);
364
365 if (res->target != PIPE_TEXTURE_3D) {
366 /* Arrays and cubemaps have the entire miptree duplicated */
367
368 pres->cubemap_stride = ALIGN_POT(offset, 64);
369 *bo_size = ALIGN_POT(pres->cubemap_stride * res->array_size, 4096);
370 } else {
371 /* 3D strides across the 2D layers */
372 assert(res->array_size == 1);
373
374 pres->cubemap_stride = size_2d;
375 *bo_size = ALIGN_POT(offset, 4096);
376 }
377 }
378
379 /* Based on the usage, determine if it makes sense to use u-inteleaved tiling.
380 * We only have routines to tile 2D textures of sane bpps. On the hardware
381 * level, not all usages are valid for tiling. Finally, if the app is hinting
382 * that the contents frequently change, tiling will be a loss.
383 *
384 * Due to incomplete information on some platforms, we may need to force tiling
385 * in some cases.
386 *
387 * On platforms where it is supported, AFBC is even better. */
388
389 static bool
390 panfrost_can_linear(struct panfrost_device *dev, const struct panfrost_resource *pres)
391 {
392 /* XXX: We should be able to do linear Z/S with the right bits.. */
393 return !((pres->base.bind & PIPE_BIND_DEPTH_STENCIL) &&
394 (dev->quirks & (MIDGARD_SFBD | IS_BIFROST)));
395 }
396
397 static bool
398 panfrost_should_afbc(struct panfrost_device *dev, const struct panfrost_resource *pres)
399 {
400 /* AFBC resources may be rendered to, textured from, or shared across
401 * processes, but may not be used as e.g buffers */
402 const unsigned valid_binding =
403 PIPE_BIND_DEPTH_STENCIL |
404 PIPE_BIND_RENDER_TARGET |
405 PIPE_BIND_BLENDABLE |
406 PIPE_BIND_SAMPLER_VIEW |
407 PIPE_BIND_DISPLAY_TARGET |
408 PIPE_BIND_SCANOUT |
409 PIPE_BIND_SHARED;
410
411 if (pres->base.bind & ~valid_binding)
412 return false;
413
414 /* AFBC introduced with Mali T760 */
415 if (dev->quirks & MIDGARD_NO_AFBC)
416 return false;
417
418 /* AFBC<-->staging is expensive */
419 if (pres->base.usage == PIPE_USAGE_STREAM)
420 return false;
421
422 /* Only a small selection of formats are AFBC'able */
423 if (!panfrost_format_supports_afbc(pres->internal_format))
424 return false;
425
426 /* AFBC does not support layered (GLES3 style) multisampling. Use
427 * EXT_multisampled_render_to_texture instead */
428 if (pres->base.nr_samples > 1)
429 return false;
430
431 /* TODO: Is AFBC of 3D textures possible? */
432 if ((pres->base.target != PIPE_TEXTURE_2D) && (pres->base.target != PIPE_TEXTURE_RECT))
433 return false;
434
435 /* For one tile, AFBC is a loss compared to u-interleaved */
436 if (pres->base.width0 <= 16 && pres->base.height0 <= 16)
437 return false;
438
439 /* Otherwise, we'd prefer AFBC as it is dramatically more efficient
440 * than linear or usually even u-interleaved */
441 return true;
442 }
443
444 static bool
445 panfrost_should_tile(struct panfrost_device *dev, const struct panfrost_resource *pres)
446 {
447 const unsigned valid_binding =
448 PIPE_BIND_DEPTH_STENCIL |
449 PIPE_BIND_RENDER_TARGET |
450 PIPE_BIND_BLENDABLE |
451 PIPE_BIND_SAMPLER_VIEW |
452 PIPE_BIND_DISPLAY_TARGET |
453 PIPE_BIND_SCANOUT |
454 PIPE_BIND_SHARED;
455
456 unsigned bpp = util_format_get_blocksizebits(pres->internal_format);
457
458 bool is_sane_bpp =
459 bpp == 8 || bpp == 16 || bpp == 24 || bpp == 32 ||
460 bpp == 64 || bpp == 128;
461
462 bool is_2d = (pres->base.target == PIPE_TEXTURE_2D)
463 || (pres->base.target == PIPE_TEXTURE_RECT);
464
465 bool can_tile = is_2d && is_sane_bpp && ((pres->base.bind & ~valid_binding) == 0);
466
467 if (!panfrost_can_linear(dev, pres)) {
468 assert(can_tile);
469 return true;
470 }
471
472 return can_tile && (pres->base.usage != PIPE_USAGE_STREAM);
473 }
474
475 static uint64_t
476 panfrost_best_modifier(struct panfrost_device *dev,
477 const struct panfrost_resource *pres)
478 {
479 if (panfrost_should_afbc(dev, pres)) {
480 uint64_t afbc =
481 AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 |
482 AFBC_FORMAT_MOD_SPARSE;
483
484 return DRM_FORMAT_MOD_ARM_AFBC(afbc);
485 } else if (panfrost_should_tile(dev, pres))
486 return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
487 else
488 return DRM_FORMAT_MOD_LINEAR;
489 }
490
491 static void
492 panfrost_resource_create_bo(struct panfrost_device *dev, struct panfrost_resource *pres,
493 uint64_t modifier)
494 {
495 struct pipe_resource *res = &pres->base;
496
497 pres->modifier = (modifier != DRM_FORMAT_MOD_INVALID) ? modifier :
498 panfrost_best_modifier(dev, pres);
499 pres->checksummed = (res->bind & PIPE_BIND_RENDER_TARGET);
500
501 /* We can only switch tiled->linear if the resource isn't already
502 * linear, and if we control the modifier, and if the resource can be
503 * linear. */
504 pres->modifier_constant = !((pres->modifier != DRM_FORMAT_MOD_LINEAR)
505 && (modifier == DRM_FORMAT_INVALID)
506 && panfrost_can_linear(dev, pres));
507
508 size_t bo_size;
509
510 panfrost_setup_slices(pres, &bo_size);
511
512 /* We create a BO immediately but don't bother mapping, since we don't
513 * care to map e.g. FBOs which the CPU probably won't touch */
514 pres->bo = panfrost_bo_create(dev, bo_size, PAN_BO_DELAY_MMAP);
515 }
516
517 void
518 panfrost_resource_set_damage_region(struct pipe_screen *screen,
519 struct pipe_resource *res,
520 unsigned int nrects,
521 const struct pipe_box *rects)
522 {
523 struct panfrost_resource *pres = pan_resource(res);
524 struct pipe_scissor_state *damage_extent = &pres->damage.extent;
525 unsigned int i;
526
527 if (pres->damage.inverted_rects)
528 ralloc_free(pres->damage.inverted_rects);
529
530 memset(&pres->damage, 0, sizeof(pres->damage));
531
532 pres->damage.inverted_rects =
533 pan_subtract_damage(pres,
534 res->width0, res->height0,
535 nrects, rects, &pres->damage.inverted_len);
536
537 /* Track the damage extent: the quad including all damage regions. Will
538 * be used restrict the rendering area */
539
540 damage_extent->minx = 0xffff;
541 damage_extent->miny = 0xffff;
542
543 for (i = 0; i < nrects; i++) {
544 int x = rects[i].x, w = rects[i].width, h = rects[i].height;
545 int y = res->height0 - (rects[i].y + h);
546
547 damage_extent->minx = MIN2(damage_extent->minx, x);
548 damage_extent->miny = MIN2(damage_extent->miny, y);
549 damage_extent->maxx = MAX2(damage_extent->maxx,
550 MIN2(x + w, res->width0));
551 damage_extent->maxy = MAX2(damage_extent->maxy,
552 MIN2(y + h, res->height0));
553 }
554
555 if (nrects == 0) {
556 damage_extent->minx = 0;
557 damage_extent->miny = 0;
558 damage_extent->maxx = res->width0;
559 damage_extent->maxy = res->height0;
560 }
561
562 }
563
564 static struct pipe_resource *
565 panfrost_resource_create_with_modifier(struct pipe_screen *screen,
566 const struct pipe_resource *template,
567 uint64_t modifier)
568 {
569 struct panfrost_device *dev = pan_device(screen);
570
571 /* Make sure we're familiar */
572 switch (template->target) {
573 case PIPE_BUFFER:
574 case PIPE_TEXTURE_1D:
575 case PIPE_TEXTURE_2D:
576 case PIPE_TEXTURE_3D:
577 case PIPE_TEXTURE_CUBE:
578 case PIPE_TEXTURE_RECT:
579 case PIPE_TEXTURE_1D_ARRAY:
580 case PIPE_TEXTURE_2D_ARRAY:
581 break;
582 default:
583 unreachable("Unknown texture target\n");
584 }
585
586 if (dev->ro && (template->bind &
587 (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | PIPE_BIND_SHARED)))
588 return panfrost_create_scanout_res(screen, template, modifier);
589
590 struct panfrost_resource *so = rzalloc(screen, struct panfrost_resource);
591 so->base = *template;
592 so->base.screen = screen;
593 so->internal_format = template->format;
594
595 pipe_reference_init(&so->base.reference, 1);
596
597 util_range_init(&so->valid_buffer_range);
598
599 panfrost_resource_create_bo(dev, so, modifier);
600 panfrost_resource_set_damage_region(NULL, &so->base, 0, NULL);
601
602 if (template->bind & PIPE_BIND_INDEX_BUFFER)
603 so->index_cache = rzalloc(so, struct panfrost_minmax_cache);
604
605 return (struct pipe_resource *)so;
606 }
607
608 /* Default is to create a resource as don't care */
609
610 static struct pipe_resource *
611 panfrost_resource_create(struct pipe_screen *screen,
612 const struct pipe_resource *template)
613 {
614 return panfrost_resource_create_with_modifier(screen, template,
615 DRM_FORMAT_MOD_INVALID);
616 }
617
618 /* If no modifier is specified, we'll choose. Otherwise, the order of
619 * preference is compressed, tiled, linear. */
620
621 static struct pipe_resource *
622 panfrost_resource_create_with_modifiers(struct pipe_screen *screen,
623 const struct pipe_resource *template,
624 const uint64_t *modifiers, int count)
625 {
626 for (unsigned i = 0; i < PAN_MODIFIER_COUNT; ++i) {
627 if (drm_find_modifier(pan_best_modifiers[i], modifiers, count)) {
628 return panfrost_resource_create_with_modifier(screen, template,
629 pan_best_modifiers[i]);
630 }
631 }
632
633 /* If we didn't find one, app specified invalid */
634 assert(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID);
635 return panfrost_resource_create(screen, template);
636 }
637
638 static void
639 panfrost_resource_destroy(struct pipe_screen *screen,
640 struct pipe_resource *pt)
641 {
642 struct panfrost_device *dev = pan_device(screen);
643 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
644
645 if (rsrc->scanout)
646 renderonly_scanout_destroy(rsrc->scanout, dev->ro);
647
648 if (rsrc->bo)
649 panfrost_bo_unreference(rsrc->bo);
650
651 if (rsrc->slices[0].checksum_bo)
652 panfrost_bo_unreference(rsrc->slices[0].checksum_bo);
653
654 util_range_destroy(&rsrc->valid_buffer_range);
655 ralloc_free(rsrc);
656 }
657
658 /* Most of the time we can do CPU-side transfers, but sometimes we need to use
659 * the 3D pipe for this. Let's wrap u_blitter to blit to/from staging textures.
660 * Code adapted from freedreno */
661
662 static struct panfrost_resource *
663 pan_alloc_staging(struct panfrost_context *ctx, struct panfrost_resource *rsc,
664 unsigned level, const struct pipe_box *box)
665 {
666 struct pipe_context *pctx = &ctx->base;
667 struct pipe_resource tmpl = rsc->base;
668
669 tmpl.width0 = box->width;
670 tmpl.height0 = box->height;
671 /* for array textures, box->depth is the array_size, otherwise
672 * for 3d textures, it is the depth:
673 */
674 if (tmpl.array_size > 1) {
675 if (tmpl.target == PIPE_TEXTURE_CUBE)
676 tmpl.target = PIPE_TEXTURE_2D_ARRAY;
677 tmpl.array_size = box->depth;
678 tmpl.depth0 = 1;
679 } else {
680 tmpl.array_size = 1;
681 tmpl.depth0 = box->depth;
682 }
683 tmpl.last_level = 0;
684 tmpl.bind |= PIPE_BIND_LINEAR;
685
686 struct pipe_resource *pstaging =
687 pctx->screen->resource_create(pctx->screen, &tmpl);
688 if (!pstaging)
689 return NULL;
690
691 return pan_resource(pstaging);
692 }
693
694 static void
695 pan_blit_from_staging(struct pipe_context *pctx, struct panfrost_gtransfer *trans)
696 {
697 struct pipe_resource *dst = trans->base.resource;
698 struct pipe_blit_info blit = {};
699
700 blit.dst.resource = dst;
701 blit.dst.format = dst->format;
702 blit.dst.level = trans->base.level;
703 blit.dst.box = trans->base.box;
704 blit.src.resource = trans->staging.rsrc;
705 blit.src.format = trans->staging.rsrc->format;
706 blit.src.level = 0;
707 blit.src.box = trans->staging.box;
708 blit.mask = util_format_get_mask(trans->staging.rsrc->format);
709 blit.filter = PIPE_TEX_FILTER_NEAREST;
710
711 panfrost_blit(pctx, &blit);
712 }
713
714 static void
715 pan_blit_to_staging(struct pipe_context *pctx, struct panfrost_gtransfer *trans)
716 {
717 struct pipe_resource *src = trans->base.resource;
718 struct pipe_blit_info blit = {};
719
720 blit.src.resource = src;
721 blit.src.format = src->format;
722 blit.src.level = trans->base.level;
723 blit.src.box = trans->base.box;
724 blit.dst.resource = trans->staging.rsrc;
725 blit.dst.format = trans->staging.rsrc->format;
726 blit.dst.level = 0;
727 blit.dst.box = trans->staging.box;
728 blit.mask = util_format_get_mask(trans->staging.rsrc->format);
729 blit.filter = PIPE_TEX_FILTER_NEAREST;
730
731 panfrost_blit(pctx, &blit);
732 }
733
734 static void *
735 panfrost_transfer_map(struct pipe_context *pctx,
736 struct pipe_resource *resource,
737 unsigned level,
738 unsigned usage, /* a combination of PIPE_TRANSFER_x */
739 const struct pipe_box *box,
740 struct pipe_transfer **out_transfer)
741 {
742 struct panfrost_context *ctx = pan_context(pctx);
743 struct panfrost_device *dev = pan_device(pctx->screen);
744 struct panfrost_resource *rsrc = pan_resource(resource);
745 int bytes_per_pixel = util_format_get_blocksize(rsrc->internal_format);
746 struct panfrost_bo *bo = rsrc->bo;
747
748 /* Can't map tiled/compressed directly */
749 if ((usage & PIPE_TRANSFER_MAP_DIRECTLY) && rsrc->modifier != DRM_FORMAT_MOD_LINEAR)
750 return NULL;
751
752 struct panfrost_gtransfer *transfer = rzalloc(pctx, struct panfrost_gtransfer);
753 transfer->base.level = level;
754 transfer->base.usage = usage;
755 transfer->base.box = *box;
756
757 pipe_resource_reference(&transfer->base.resource, resource);
758 *out_transfer = &transfer->base;
759
760 /* We don't have s/w routines for AFBC, so use a staging texture */
761 if (drm_is_afbc(rsrc->modifier)) {
762 struct panfrost_resource *staging = pan_alloc_staging(ctx, rsrc, level, box);
763 transfer->base.stride = staging->slices[0].stride;
764 transfer->base.layer_stride = transfer->base.stride * box->height;
765
766 transfer->staging.rsrc = &staging->base;
767
768 transfer->staging.box = *box;
769 transfer->staging.box.x = 0;
770 transfer->staging.box.y = 0;
771 transfer->staging.box.z = 0;
772
773 assert(transfer->staging.rsrc != NULL);
774
775 /* TODO: Eliminate this flush. It's only there to determine if
776 * we're initialized or not, when the initialization could come
777 * from a pending batch XXX */
778 panfrost_flush_batches_accessing_bo(ctx, rsrc->bo, true);
779
780 if ((usage & PIPE_TRANSFER_READ) && rsrc->slices[level].initialized) {
781 pan_blit_to_staging(pctx, transfer);
782 panfrost_flush_batches_accessing_bo(ctx, staging->bo, true);
783 panfrost_bo_wait(staging->bo, INT64_MAX, false);
784 }
785
786 panfrost_bo_mmap(staging->bo);
787 return staging->bo->cpu;
788 }
789
790 /* If we haven't already mmaped, now's the time */
791 panfrost_bo_mmap(bo);
792
793 if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
794 pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
795
796 bool create_new_bo = usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
797 bool copy_resource = false;
798
799 if (!create_new_bo &&
800 !(usage & PIPE_TRANSFER_UNSYNCHRONIZED) &&
801 (usage & PIPE_TRANSFER_WRITE) &&
802 !(resource->target == PIPE_BUFFER
803 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) &&
804 panfrost_pending_batches_access_bo(ctx, bo)) {
805
806 /* When a resource to be modified is already being used by a
807 * pending batch, it is often faster to copy the whole BO than
808 * to flush and split the frame in two. This also mostly
809 * mitigates broken depth reload.
810 */
811
812 panfrost_flush_batches_accessing_bo(ctx, bo, false);
813 panfrost_bo_wait(bo, INT64_MAX, false);
814
815 create_new_bo = true;
816 copy_resource = true;
817 }
818
819 if (create_new_bo) {
820 /* If the BO is used by one of the pending batches or if it's
821 * not ready yet (still accessed by one of the already flushed
822 * batches), we try to allocate a new one to avoid waiting.
823 */
824 if (panfrost_pending_batches_access_bo(ctx, bo) ||
825 !panfrost_bo_wait(bo, 0, true)) {
826 /* We want the BO to be MMAPed. */
827 uint32_t flags = bo->flags & ~PAN_BO_DELAY_MMAP;
828 struct panfrost_bo *newbo = NULL;
829
830 /* When the BO has been imported/exported, we can't
831 * replace it by another one, otherwise the
832 * importer/exporter wouldn't see the change we're
833 * doing to it.
834 */
835 if (!(bo->flags & PAN_BO_SHARED))
836 newbo = panfrost_bo_create(dev, bo->size,
837 flags);
838
839 if (newbo) {
840 if (copy_resource)
841 memcpy(newbo->cpu, rsrc->bo->cpu, bo->size);
842
843 panfrost_bo_unreference(bo);
844 rsrc->bo = newbo;
845 bo = newbo;
846 } else {
847 /* Allocation failed or was impossible, let's
848 * fall back on a flush+wait.
849 */
850 panfrost_flush_batches_accessing_bo(ctx, bo, true);
851 panfrost_bo_wait(bo, INT64_MAX, true);
852 }
853 }
854 } else if ((usage & PIPE_TRANSFER_WRITE)
855 && resource->target == PIPE_BUFFER
856 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) {
857 /* No flush for writes to uninitialized */
858 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
859 if (usage & PIPE_TRANSFER_WRITE) {
860 panfrost_flush_batches_accessing_bo(ctx, bo, true);
861 panfrost_bo_wait(bo, INT64_MAX, true);
862 } else if (usage & PIPE_TRANSFER_READ) {
863 panfrost_flush_batches_accessing_bo(ctx, bo, false);
864 panfrost_bo_wait(bo, INT64_MAX, false);
865 }
866 }
867
868 if (rsrc->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED) {
869 transfer->base.stride = box->width * bytes_per_pixel;
870 transfer->base.layer_stride = transfer->base.stride * box->height;
871 transfer->map = ralloc_size(transfer, transfer->base.layer_stride * box->depth);
872 assert(box->depth == 1);
873
874 if ((usage & PIPE_TRANSFER_READ) && rsrc->slices[level].initialized) {
875 panfrost_load_tiled_image(
876 transfer->map,
877 bo->cpu + rsrc->slices[level].offset,
878 box->x, box->y, box->width, box->height,
879 transfer->base.stride,
880 rsrc->slices[level].stride,
881 rsrc->internal_format);
882 }
883
884 return transfer->map;
885 } else {
886 assert (rsrc->modifier == DRM_FORMAT_MOD_LINEAR);
887
888 /* Direct, persistent writes create holes in time for
889 * caching... I don't know if this is actually possible but we
890 * should still get it right */
891
892 unsigned dpw = PIPE_TRANSFER_MAP_DIRECTLY | PIPE_TRANSFER_WRITE | PIPE_TRANSFER_PERSISTENT;
893
894 if ((usage & dpw) == dpw && rsrc->index_cache)
895 return NULL;
896
897 transfer->base.stride = rsrc->slices[level].stride;
898 transfer->base.layer_stride = panfrost_get_layer_stride(
899 rsrc->slices, rsrc->base.target == PIPE_TEXTURE_3D,
900 rsrc->cubemap_stride, level);
901
902 /* By mapping direct-write, we're implicitly already
903 * initialized (maybe), so be conservative */
904
905 if (usage & PIPE_TRANSFER_WRITE) {
906 rsrc->slices[level].initialized = true;
907 panfrost_minmax_cache_invalidate(rsrc->index_cache, &transfer->base);
908 }
909
910 return bo->cpu
911 + rsrc->slices[level].offset
912 + transfer->base.box.z * transfer->base.layer_stride
913 + transfer->base.box.y * rsrc->slices[level].stride
914 + transfer->base.box.x * bytes_per_pixel;
915 }
916 }
917
918 static void
919 panfrost_transfer_unmap(struct pipe_context *pctx,
920 struct pipe_transfer *transfer)
921 {
922 /* Gallium expects writeback here, so we tile */
923
924 struct panfrost_gtransfer *trans = pan_transfer(transfer);
925 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
926
927 /* AFBC will use a staging resource. `initialized` will be set when the
928 * fragment job is created; this is deferred to prevent useless surface
929 * reloads that can cascade into DATA_INVALID_FAULTs due to reading
930 * malformed AFBC data if uninitialized */
931
932 if (trans->staging.rsrc) {
933 if (transfer->usage & PIPE_TRANSFER_WRITE) {
934 pan_blit_from_staging(pctx, trans);
935 panfrost_flush_batches_accessing_bo(pan_context(pctx), pan_resource(trans->staging.rsrc)->bo, true);
936 }
937
938 pipe_resource_reference(&trans->staging.rsrc, NULL);
939 }
940
941 /* Tiling will occur in software from a staging cpu buffer */
942 if (trans->map) {
943 struct panfrost_bo *bo = prsrc->bo;
944
945 if (transfer->usage & PIPE_TRANSFER_WRITE) {
946 prsrc->slices[transfer->level].initialized = true;
947
948 if (prsrc->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED) {
949 assert(transfer->box.depth == 1);
950
951 /* Do we overwrite the entire resource? If so,
952 * we don't need an intermediate blit so it's a
953 * good time to switch the modifier. */
954
955 bool discards_content = prsrc->base.last_level == 0
956 && transfer->box.width == prsrc->base.width0
957 && transfer->box.height == prsrc->base.height0
958 && transfer->box.x == 0
959 && transfer->box.y == 0
960 && !prsrc->modifier_constant;
961
962 /* It also serves as a good heuristic for
963 * streaming textures (e.g. in video players),
964 * but we could do better */
965
966 if (discards_content)
967 ++prsrc->modifier_updates;
968
969 if (prsrc->modifier_updates >= LAYOUT_CONVERT_THRESHOLD)
970 {
971 prsrc->modifier = DRM_FORMAT_MOD_LINEAR;
972
973 util_copy_rect(
974 bo->cpu + prsrc->slices[0].offset,
975 prsrc->base.format,
976 prsrc->slices[0].stride,
977 0, 0,
978 transfer->box.width,
979 transfer->box.height,
980 trans->map,
981 transfer->stride,
982 0, 0);
983 } else {
984 panfrost_store_tiled_image(
985 bo->cpu + prsrc->slices[transfer->level].offset,
986 trans->map,
987 transfer->box.x, transfer->box.y,
988 transfer->box.width, transfer->box.height,
989 prsrc->slices[transfer->level].stride,
990 transfer->stride,
991 prsrc->internal_format);
992 }
993 }
994 }
995 }
996
997
998 util_range_add(&prsrc->base, &prsrc->valid_buffer_range,
999 transfer->box.x,
1000 transfer->box.x + transfer->box.width);
1001
1002 panfrost_minmax_cache_invalidate(prsrc->index_cache, transfer);
1003
1004 /* Derefence the resource */
1005 pipe_resource_reference(&transfer->resource, NULL);
1006
1007 /* Transfer itself is RALLOCed at the moment */
1008 ralloc_free(transfer);
1009 }
1010
1011 static void
1012 panfrost_transfer_flush_region(struct pipe_context *pctx,
1013 struct pipe_transfer *transfer,
1014 const struct pipe_box *box)
1015 {
1016 struct panfrost_resource *rsc = pan_resource(transfer->resource);
1017
1018 if (transfer->resource->target == PIPE_BUFFER) {
1019 util_range_add(&rsc->base, &rsc->valid_buffer_range,
1020 transfer->box.x + box->x,
1021 transfer->box.x + box->x + box->width);
1022 } else {
1023 unsigned level = transfer->level;
1024 rsc->slices[level].initialized = true;
1025 }
1026 }
1027
1028 static void
1029 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
1030 {
1031 /* TODO */
1032 }
1033
1034 static enum pipe_format
1035 panfrost_resource_get_internal_format(struct pipe_resource *rsrc)
1036 {
1037 struct panfrost_resource *prsrc = (struct panfrost_resource *) rsrc;
1038 return prsrc->internal_format;
1039 }
1040
1041 static bool
1042 panfrost_generate_mipmap(
1043 struct pipe_context *pctx,
1044 struct pipe_resource *prsrc,
1045 enum pipe_format format,
1046 unsigned base_level,
1047 unsigned last_level,
1048 unsigned first_layer,
1049 unsigned last_layer)
1050 {
1051 struct panfrost_resource *rsrc = pan_resource(prsrc);
1052
1053 /* Generating a mipmap invalidates the written levels, so make that
1054 * explicit so we don't try to wallpaper them back and end up with
1055 * u_blitter recursion */
1056
1057 assert(rsrc->bo);
1058 for (unsigned l = base_level + 1; l <= last_level; ++l)
1059 rsrc->slices[l].initialized = false;
1060
1061 /* Beyond that, we just delegate the hard stuff. */
1062
1063 bool blit_res = util_gen_mipmap(
1064 pctx, prsrc, format,
1065 base_level, last_level,
1066 first_layer, last_layer,
1067 PIPE_TEX_FILTER_LINEAR);
1068
1069 return blit_res;
1070 }
1071
1072 /* Computes the address to a texture at a particular slice */
1073
1074 mali_ptr
1075 panfrost_get_texture_address(
1076 struct panfrost_resource *rsrc,
1077 unsigned level, unsigned face, unsigned sample)
1078 {
1079 bool is_3d = rsrc->base.target == PIPE_TEXTURE_3D;
1080 return rsrc->bo->gpu + panfrost_texture_offset(rsrc->slices, is_3d, rsrc->cubemap_stride, level, face, sample);
1081 }
1082
1083 static void
1084 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
1085 struct pipe_resource *stencil)
1086 {
1087 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
1088 }
1089
1090 static struct pipe_resource *
1091 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
1092 {
1093 return &pan_resource(prsrc)->separate_stencil->base;
1094 }
1095
1096 static const struct u_transfer_vtbl transfer_vtbl = {
1097 .resource_create = panfrost_resource_create,
1098 .resource_destroy = panfrost_resource_destroy,
1099 .transfer_map = panfrost_transfer_map,
1100 .transfer_unmap = panfrost_transfer_unmap,
1101 .transfer_flush_region = panfrost_transfer_flush_region,
1102 .get_internal_format = panfrost_resource_get_internal_format,
1103 .set_stencil = panfrost_resource_set_stencil,
1104 .get_stencil = panfrost_resource_get_stencil,
1105 };
1106
1107 void
1108 panfrost_resource_screen_init(struct pipe_screen *pscreen)
1109 {
1110 struct panfrost_device *dev = pan_device(pscreen);
1111
1112 bool fake_rgtc = !panfrost_supports_compressed_format(dev, MALI_BC4_UNORM);
1113
1114 pscreen->resource_create_with_modifiers =
1115 panfrost_resource_create_with_modifiers;
1116 pscreen->resource_create = u_transfer_helper_resource_create;
1117 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1118 pscreen->resource_from_handle = panfrost_resource_from_handle;
1119 pscreen->resource_get_handle = panfrost_resource_get_handle;
1120 pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1121 true, false,
1122 fake_rgtc, true);
1123 }
1124
1125 void
1126 panfrost_resource_context_init(struct pipe_context *pctx)
1127 {
1128 pctx->transfer_map = u_transfer_helper_transfer_map;
1129 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
1130 pctx->create_surface = panfrost_create_surface;
1131 pctx->surface_destroy = panfrost_surface_destroy;
1132 pctx->resource_copy_region = util_resource_copy_region;
1133 pctx->blit = panfrost_blit;
1134 pctx->generate_mipmap = panfrost_generate_mipmap;
1135 pctx->flush_resource = panfrost_flush_resource;
1136 pctx->invalidate_resource = panfrost_invalidate_resource;
1137 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1138 pctx->buffer_subdata = u_default_buffer_subdata;
1139 pctx->texture_subdata = u_default_texture_subdata;
1140 }