b282331bddec4fccf08831f70839f558242ffe53
[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 if (panfrost_afbc_can_ytr(pres->base.format))
485 afbc |= AFBC_FORMAT_MOD_YTR;
486
487 return DRM_FORMAT_MOD_ARM_AFBC(afbc);
488 } else if (panfrost_should_tile(dev, pres))
489 return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
490 else
491 return DRM_FORMAT_MOD_LINEAR;
492 }
493
494 static void
495 panfrost_resource_create_bo(struct panfrost_device *dev, struct panfrost_resource *pres,
496 uint64_t modifier)
497 {
498 struct pipe_resource *res = &pres->base;
499
500 pres->modifier = (modifier != DRM_FORMAT_MOD_INVALID) ? modifier :
501 panfrost_best_modifier(dev, pres);
502 pres->checksummed = (res->bind & PIPE_BIND_RENDER_TARGET);
503
504 /* We can only switch tiled->linear if the resource isn't already
505 * linear, and if we control the modifier, and if the resource can be
506 * linear. */
507 pres->modifier_constant = !((pres->modifier != DRM_FORMAT_MOD_LINEAR)
508 && (modifier == DRM_FORMAT_INVALID)
509 && panfrost_can_linear(dev, pres));
510
511 size_t bo_size;
512
513 panfrost_setup_slices(pres, &bo_size);
514
515 /* We create a BO immediately but don't bother mapping, since we don't
516 * care to map e.g. FBOs which the CPU probably won't touch */
517 pres->bo = panfrost_bo_create(dev, bo_size, PAN_BO_DELAY_MMAP);
518 }
519
520 void
521 panfrost_resource_set_damage_region(struct pipe_screen *screen,
522 struct pipe_resource *res,
523 unsigned int nrects,
524 const struct pipe_box *rects)
525 {
526 struct panfrost_resource *pres = pan_resource(res);
527 struct pipe_scissor_state *damage_extent = &pres->damage.extent;
528 unsigned int i;
529
530 if (pres->damage.inverted_rects)
531 ralloc_free(pres->damage.inverted_rects);
532
533 memset(&pres->damage, 0, sizeof(pres->damage));
534
535 pres->damage.inverted_rects =
536 pan_subtract_damage(pres,
537 res->width0, res->height0,
538 nrects, rects, &pres->damage.inverted_len);
539
540 /* Track the damage extent: the quad including all damage regions. Will
541 * be used restrict the rendering area */
542
543 damage_extent->minx = 0xffff;
544 damage_extent->miny = 0xffff;
545
546 for (i = 0; i < nrects; i++) {
547 int x = rects[i].x, w = rects[i].width, h = rects[i].height;
548 int y = res->height0 - (rects[i].y + h);
549
550 damage_extent->minx = MIN2(damage_extent->minx, x);
551 damage_extent->miny = MIN2(damage_extent->miny, y);
552 damage_extent->maxx = MAX2(damage_extent->maxx,
553 MIN2(x + w, res->width0));
554 damage_extent->maxy = MAX2(damage_extent->maxy,
555 MIN2(y + h, res->height0));
556 }
557
558 if (nrects == 0) {
559 damage_extent->minx = 0;
560 damage_extent->miny = 0;
561 damage_extent->maxx = res->width0;
562 damage_extent->maxy = res->height0;
563 }
564
565 }
566
567 static struct pipe_resource *
568 panfrost_resource_create_with_modifier(struct pipe_screen *screen,
569 const struct pipe_resource *template,
570 uint64_t modifier)
571 {
572 struct panfrost_device *dev = pan_device(screen);
573
574 /* Make sure we're familiar */
575 switch (template->target) {
576 case PIPE_BUFFER:
577 case PIPE_TEXTURE_1D:
578 case PIPE_TEXTURE_2D:
579 case PIPE_TEXTURE_3D:
580 case PIPE_TEXTURE_CUBE:
581 case PIPE_TEXTURE_RECT:
582 case PIPE_TEXTURE_1D_ARRAY:
583 case PIPE_TEXTURE_2D_ARRAY:
584 break;
585 default:
586 unreachable("Unknown texture target\n");
587 }
588
589 if (dev->ro && (template->bind &
590 (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | PIPE_BIND_SHARED)))
591 return panfrost_create_scanout_res(screen, template, modifier);
592
593 struct panfrost_resource *so = rzalloc(screen, struct panfrost_resource);
594 so->base = *template;
595 so->base.screen = screen;
596 so->internal_format = template->format;
597
598 pipe_reference_init(&so->base.reference, 1);
599
600 util_range_init(&so->valid_buffer_range);
601
602 panfrost_resource_create_bo(dev, so, modifier);
603 panfrost_resource_set_damage_region(NULL, &so->base, 0, NULL);
604
605 if (template->bind & PIPE_BIND_INDEX_BUFFER)
606 so->index_cache = rzalloc(so, struct panfrost_minmax_cache);
607
608 return (struct pipe_resource *)so;
609 }
610
611 /* Default is to create a resource as don't care */
612
613 static struct pipe_resource *
614 panfrost_resource_create(struct pipe_screen *screen,
615 const struct pipe_resource *template)
616 {
617 return panfrost_resource_create_with_modifier(screen, template,
618 DRM_FORMAT_MOD_INVALID);
619 }
620
621 /* If no modifier is specified, we'll choose. Otherwise, the order of
622 * preference is compressed, tiled, linear. */
623
624 static struct pipe_resource *
625 panfrost_resource_create_with_modifiers(struct pipe_screen *screen,
626 const struct pipe_resource *template,
627 const uint64_t *modifiers, int count)
628 {
629 for (unsigned i = 0; i < PAN_MODIFIER_COUNT; ++i) {
630 if (drm_find_modifier(pan_best_modifiers[i], modifiers, count)) {
631 return panfrost_resource_create_with_modifier(screen, template,
632 pan_best_modifiers[i]);
633 }
634 }
635
636 /* If we didn't find one, app specified invalid */
637 assert(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID);
638 return panfrost_resource_create(screen, template);
639 }
640
641 static void
642 panfrost_resource_destroy(struct pipe_screen *screen,
643 struct pipe_resource *pt)
644 {
645 struct panfrost_device *dev = pan_device(screen);
646 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
647
648 if (rsrc->scanout)
649 renderonly_scanout_destroy(rsrc->scanout, dev->ro);
650
651 if (rsrc->bo)
652 panfrost_bo_unreference(rsrc->bo);
653
654 if (rsrc->slices[0].checksum_bo)
655 panfrost_bo_unreference(rsrc->slices[0].checksum_bo);
656
657 util_range_destroy(&rsrc->valid_buffer_range);
658 ralloc_free(rsrc);
659 }
660
661 /* Most of the time we can do CPU-side transfers, but sometimes we need to use
662 * the 3D pipe for this. Let's wrap u_blitter to blit to/from staging textures.
663 * Code adapted from freedreno */
664
665 static struct panfrost_resource *
666 pan_alloc_staging(struct panfrost_context *ctx, struct panfrost_resource *rsc,
667 unsigned level, const struct pipe_box *box)
668 {
669 struct pipe_context *pctx = &ctx->base;
670 struct pipe_resource tmpl = rsc->base;
671
672 tmpl.width0 = box->width;
673 tmpl.height0 = box->height;
674 /* for array textures, box->depth is the array_size, otherwise
675 * for 3d textures, it is the depth:
676 */
677 if (tmpl.array_size > 1) {
678 if (tmpl.target == PIPE_TEXTURE_CUBE)
679 tmpl.target = PIPE_TEXTURE_2D_ARRAY;
680 tmpl.array_size = box->depth;
681 tmpl.depth0 = 1;
682 } else {
683 tmpl.array_size = 1;
684 tmpl.depth0 = box->depth;
685 }
686 tmpl.last_level = 0;
687 tmpl.bind |= PIPE_BIND_LINEAR;
688
689 struct pipe_resource *pstaging =
690 pctx->screen->resource_create(pctx->screen, &tmpl);
691 if (!pstaging)
692 return NULL;
693
694 return pan_resource(pstaging);
695 }
696
697 static void
698 pan_blit_from_staging(struct pipe_context *pctx, struct panfrost_gtransfer *trans)
699 {
700 struct pipe_resource *dst = trans->base.resource;
701 struct pipe_blit_info blit = {};
702
703 blit.dst.resource = dst;
704 blit.dst.format = dst->format;
705 blit.dst.level = trans->base.level;
706 blit.dst.box = trans->base.box;
707 blit.src.resource = trans->staging.rsrc;
708 blit.src.format = trans->staging.rsrc->format;
709 blit.src.level = 0;
710 blit.src.box = trans->staging.box;
711 blit.mask = util_format_get_mask(trans->staging.rsrc->format);
712 blit.filter = PIPE_TEX_FILTER_NEAREST;
713
714 panfrost_blit(pctx, &blit);
715 }
716
717 static void
718 pan_blit_to_staging(struct pipe_context *pctx, struct panfrost_gtransfer *trans)
719 {
720 struct pipe_resource *src = trans->base.resource;
721 struct pipe_blit_info blit = {};
722
723 blit.src.resource = src;
724 blit.src.format = src->format;
725 blit.src.level = trans->base.level;
726 blit.src.box = trans->base.box;
727 blit.dst.resource = trans->staging.rsrc;
728 blit.dst.format = trans->staging.rsrc->format;
729 blit.dst.level = 0;
730 blit.dst.box = trans->staging.box;
731 blit.mask = util_format_get_mask(trans->staging.rsrc->format);
732 blit.filter = PIPE_TEX_FILTER_NEAREST;
733
734 panfrost_blit(pctx, &blit);
735 }
736
737 static void *
738 panfrost_transfer_map(struct pipe_context *pctx,
739 struct pipe_resource *resource,
740 unsigned level,
741 unsigned usage, /* a combination of PIPE_TRANSFER_x */
742 const struct pipe_box *box,
743 struct pipe_transfer **out_transfer)
744 {
745 struct panfrost_context *ctx = pan_context(pctx);
746 struct panfrost_device *dev = pan_device(pctx->screen);
747 struct panfrost_resource *rsrc = pan_resource(resource);
748 int bytes_per_pixel = util_format_get_blocksize(rsrc->internal_format);
749 struct panfrost_bo *bo = rsrc->bo;
750
751 /* Can't map tiled/compressed directly */
752 if ((usage & PIPE_TRANSFER_MAP_DIRECTLY) && rsrc->modifier != DRM_FORMAT_MOD_LINEAR)
753 return NULL;
754
755 struct panfrost_gtransfer *transfer = rzalloc(pctx, struct panfrost_gtransfer);
756 transfer->base.level = level;
757 transfer->base.usage = usage;
758 transfer->base.box = *box;
759
760 pipe_resource_reference(&transfer->base.resource, resource);
761 *out_transfer = &transfer->base;
762
763 /* We don't have s/w routines for AFBC, so use a staging texture */
764 if (drm_is_afbc(rsrc->modifier)) {
765 struct panfrost_resource *staging = pan_alloc_staging(ctx, rsrc, level, box);
766 transfer->base.stride = staging->slices[0].stride;
767 transfer->base.layer_stride = transfer->base.stride * box->height;
768
769 transfer->staging.rsrc = &staging->base;
770
771 transfer->staging.box = *box;
772 transfer->staging.box.x = 0;
773 transfer->staging.box.y = 0;
774 transfer->staging.box.z = 0;
775
776 assert(transfer->staging.rsrc != NULL);
777
778 /* TODO: Eliminate this flush. It's only there to determine if
779 * we're initialized or not, when the initialization could come
780 * from a pending batch XXX */
781 panfrost_flush_batches_accessing_bo(ctx, rsrc->bo, true);
782
783 if ((usage & PIPE_TRANSFER_READ) && rsrc->slices[level].initialized) {
784 pan_blit_to_staging(pctx, transfer);
785 panfrost_flush_batches_accessing_bo(ctx, staging->bo, true);
786 panfrost_bo_wait(staging->bo, INT64_MAX, false);
787 }
788
789 panfrost_bo_mmap(staging->bo);
790 return staging->bo->cpu;
791 }
792
793 /* If we haven't already mmaped, now's the time */
794 panfrost_bo_mmap(bo);
795
796 if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
797 pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
798
799 bool create_new_bo = usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
800 bool copy_resource = false;
801
802 if (!create_new_bo &&
803 !(usage & PIPE_TRANSFER_UNSYNCHRONIZED) &&
804 (usage & PIPE_TRANSFER_WRITE) &&
805 !(resource->target == PIPE_BUFFER
806 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) &&
807 panfrost_pending_batches_access_bo(ctx, bo)) {
808
809 /* When a resource to be modified is already being used by a
810 * pending batch, it is often faster to copy the whole BO than
811 * to flush and split the frame in two. This also mostly
812 * mitigates broken depth reload.
813 */
814
815 panfrost_flush_batches_accessing_bo(ctx, bo, false);
816 panfrost_bo_wait(bo, INT64_MAX, false);
817
818 create_new_bo = true;
819 copy_resource = true;
820 }
821
822 if (create_new_bo) {
823 /* If the BO is used by one of the pending batches or if it's
824 * not ready yet (still accessed by one of the already flushed
825 * batches), we try to allocate a new one to avoid waiting.
826 */
827 if (panfrost_pending_batches_access_bo(ctx, bo) ||
828 !panfrost_bo_wait(bo, 0, true)) {
829 /* We want the BO to be MMAPed. */
830 uint32_t flags = bo->flags & ~PAN_BO_DELAY_MMAP;
831 struct panfrost_bo *newbo = NULL;
832
833 /* When the BO has been imported/exported, we can't
834 * replace it by another one, otherwise the
835 * importer/exporter wouldn't see the change we're
836 * doing to it.
837 */
838 if (!(bo->flags & PAN_BO_SHARED))
839 newbo = panfrost_bo_create(dev, bo->size,
840 flags);
841
842 if (newbo) {
843 if (copy_resource)
844 memcpy(newbo->cpu, rsrc->bo->cpu, bo->size);
845
846 panfrost_bo_unreference(bo);
847 rsrc->bo = newbo;
848 bo = newbo;
849 } else {
850 /* Allocation failed or was impossible, let's
851 * fall back on a flush+wait.
852 */
853 panfrost_flush_batches_accessing_bo(ctx, bo, true);
854 panfrost_bo_wait(bo, INT64_MAX, true);
855 }
856 }
857 } else if ((usage & PIPE_TRANSFER_WRITE)
858 && resource->target == PIPE_BUFFER
859 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) {
860 /* No flush for writes to uninitialized */
861 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
862 if (usage & PIPE_TRANSFER_WRITE) {
863 panfrost_flush_batches_accessing_bo(ctx, bo, true);
864 panfrost_bo_wait(bo, INT64_MAX, true);
865 } else if (usage & PIPE_TRANSFER_READ) {
866 panfrost_flush_batches_accessing_bo(ctx, bo, false);
867 panfrost_bo_wait(bo, INT64_MAX, false);
868 }
869 }
870
871 if (rsrc->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED) {
872 transfer->base.stride = box->width * bytes_per_pixel;
873 transfer->base.layer_stride = transfer->base.stride * box->height;
874 transfer->map = ralloc_size(transfer, transfer->base.layer_stride * box->depth);
875 assert(box->depth == 1);
876
877 if ((usage & PIPE_TRANSFER_READ) && rsrc->slices[level].initialized) {
878 panfrost_load_tiled_image(
879 transfer->map,
880 bo->cpu + rsrc->slices[level].offset,
881 box->x, box->y, box->width, box->height,
882 transfer->base.stride,
883 rsrc->slices[level].stride,
884 rsrc->internal_format);
885 }
886
887 return transfer->map;
888 } else {
889 assert (rsrc->modifier == DRM_FORMAT_MOD_LINEAR);
890
891 /* Direct, persistent writes create holes in time for
892 * caching... I don't know if this is actually possible but we
893 * should still get it right */
894
895 unsigned dpw = PIPE_TRANSFER_MAP_DIRECTLY | PIPE_TRANSFER_WRITE | PIPE_TRANSFER_PERSISTENT;
896
897 if ((usage & dpw) == dpw && rsrc->index_cache)
898 return NULL;
899
900 transfer->base.stride = rsrc->slices[level].stride;
901 transfer->base.layer_stride = panfrost_get_layer_stride(
902 rsrc->slices, rsrc->base.target == PIPE_TEXTURE_3D,
903 rsrc->cubemap_stride, level);
904
905 /* By mapping direct-write, we're implicitly already
906 * initialized (maybe), so be conservative */
907
908 if (usage & PIPE_TRANSFER_WRITE) {
909 rsrc->slices[level].initialized = true;
910 panfrost_minmax_cache_invalidate(rsrc->index_cache, &transfer->base);
911 }
912
913 return bo->cpu
914 + rsrc->slices[level].offset
915 + transfer->base.box.z * transfer->base.layer_stride
916 + transfer->base.box.y * rsrc->slices[level].stride
917 + transfer->base.box.x * bytes_per_pixel;
918 }
919 }
920
921 static void
922 panfrost_transfer_unmap(struct pipe_context *pctx,
923 struct pipe_transfer *transfer)
924 {
925 /* Gallium expects writeback here, so we tile */
926
927 struct panfrost_gtransfer *trans = pan_transfer(transfer);
928 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
929
930 /* AFBC will use a staging resource. `initialized` will be set when the
931 * fragment job is created; this is deferred to prevent useless surface
932 * reloads that can cascade into DATA_INVALID_FAULTs due to reading
933 * malformed AFBC data if uninitialized */
934
935 if (trans->staging.rsrc) {
936 if (transfer->usage & PIPE_TRANSFER_WRITE) {
937 pan_blit_from_staging(pctx, trans);
938 panfrost_flush_batches_accessing_bo(pan_context(pctx), pan_resource(trans->staging.rsrc)->bo, true);
939 }
940
941 pipe_resource_reference(&trans->staging.rsrc, NULL);
942 }
943
944 /* Tiling will occur in software from a staging cpu buffer */
945 if (trans->map) {
946 struct panfrost_bo *bo = prsrc->bo;
947
948 if (transfer->usage & PIPE_TRANSFER_WRITE) {
949 prsrc->slices[transfer->level].initialized = true;
950
951 if (prsrc->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED) {
952 assert(transfer->box.depth == 1);
953
954 /* Do we overwrite the entire resource? If so,
955 * we don't need an intermediate blit so it's a
956 * good time to switch the modifier. */
957
958 bool discards_content = prsrc->base.last_level == 0
959 && transfer->box.width == prsrc->base.width0
960 && transfer->box.height == prsrc->base.height0
961 && transfer->box.x == 0
962 && transfer->box.y == 0
963 && !prsrc->modifier_constant;
964
965 /* It also serves as a good heuristic for
966 * streaming textures (e.g. in video players),
967 * but we could do better */
968
969 if (discards_content)
970 ++prsrc->modifier_updates;
971
972 if (prsrc->modifier_updates >= LAYOUT_CONVERT_THRESHOLD)
973 {
974 prsrc->modifier = DRM_FORMAT_MOD_LINEAR;
975
976 util_copy_rect(
977 bo->cpu + prsrc->slices[0].offset,
978 prsrc->base.format,
979 prsrc->slices[0].stride,
980 0, 0,
981 transfer->box.width,
982 transfer->box.height,
983 trans->map,
984 transfer->stride,
985 0, 0);
986 } else {
987 panfrost_store_tiled_image(
988 bo->cpu + prsrc->slices[transfer->level].offset,
989 trans->map,
990 transfer->box.x, transfer->box.y,
991 transfer->box.width, transfer->box.height,
992 prsrc->slices[transfer->level].stride,
993 transfer->stride,
994 prsrc->internal_format);
995 }
996 }
997 }
998 }
999
1000
1001 util_range_add(&prsrc->base, &prsrc->valid_buffer_range,
1002 transfer->box.x,
1003 transfer->box.x + transfer->box.width);
1004
1005 panfrost_minmax_cache_invalidate(prsrc->index_cache, transfer);
1006
1007 /* Derefence the resource */
1008 pipe_resource_reference(&transfer->resource, NULL);
1009
1010 /* Transfer itself is RALLOCed at the moment */
1011 ralloc_free(transfer);
1012 }
1013
1014 static void
1015 panfrost_transfer_flush_region(struct pipe_context *pctx,
1016 struct pipe_transfer *transfer,
1017 const struct pipe_box *box)
1018 {
1019 struct panfrost_resource *rsc = pan_resource(transfer->resource);
1020
1021 if (transfer->resource->target == PIPE_BUFFER) {
1022 util_range_add(&rsc->base, &rsc->valid_buffer_range,
1023 transfer->box.x + box->x,
1024 transfer->box.x + box->x + box->width);
1025 } else {
1026 unsigned level = transfer->level;
1027 rsc->slices[level].initialized = true;
1028 }
1029 }
1030
1031 static void
1032 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
1033 {
1034 /* TODO */
1035 }
1036
1037 static enum pipe_format
1038 panfrost_resource_get_internal_format(struct pipe_resource *rsrc)
1039 {
1040 struct panfrost_resource *prsrc = (struct panfrost_resource *) rsrc;
1041 return prsrc->internal_format;
1042 }
1043
1044 static bool
1045 panfrost_generate_mipmap(
1046 struct pipe_context *pctx,
1047 struct pipe_resource *prsrc,
1048 enum pipe_format format,
1049 unsigned base_level,
1050 unsigned last_level,
1051 unsigned first_layer,
1052 unsigned last_layer)
1053 {
1054 struct panfrost_resource *rsrc = pan_resource(prsrc);
1055
1056 /* Generating a mipmap invalidates the written levels, so make that
1057 * explicit so we don't try to wallpaper them back and end up with
1058 * u_blitter recursion */
1059
1060 assert(rsrc->bo);
1061 for (unsigned l = base_level + 1; l <= last_level; ++l)
1062 rsrc->slices[l].initialized = false;
1063
1064 /* Beyond that, we just delegate the hard stuff. */
1065
1066 bool blit_res = util_gen_mipmap(
1067 pctx, prsrc, format,
1068 base_level, last_level,
1069 first_layer, last_layer,
1070 PIPE_TEX_FILTER_LINEAR);
1071
1072 return blit_res;
1073 }
1074
1075 /* Computes the address to a texture at a particular slice */
1076
1077 mali_ptr
1078 panfrost_get_texture_address(
1079 struct panfrost_resource *rsrc,
1080 unsigned level, unsigned face, unsigned sample)
1081 {
1082 bool is_3d = rsrc->base.target == PIPE_TEXTURE_3D;
1083 return rsrc->bo->gpu + panfrost_texture_offset(rsrc->slices, is_3d, rsrc->cubemap_stride, level, face, sample);
1084 }
1085
1086 static void
1087 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
1088 struct pipe_resource *stencil)
1089 {
1090 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
1091 }
1092
1093 static struct pipe_resource *
1094 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
1095 {
1096 return &pan_resource(prsrc)->separate_stencil->base;
1097 }
1098
1099 static const struct u_transfer_vtbl transfer_vtbl = {
1100 .resource_create = panfrost_resource_create,
1101 .resource_destroy = panfrost_resource_destroy,
1102 .transfer_map = panfrost_transfer_map,
1103 .transfer_unmap = panfrost_transfer_unmap,
1104 .transfer_flush_region = panfrost_transfer_flush_region,
1105 .get_internal_format = panfrost_resource_get_internal_format,
1106 .set_stencil = panfrost_resource_set_stencil,
1107 .get_stencil = panfrost_resource_get_stencil,
1108 };
1109
1110 void
1111 panfrost_resource_screen_init(struct pipe_screen *pscreen)
1112 {
1113 struct panfrost_device *dev = pan_device(pscreen);
1114
1115 bool fake_rgtc = !panfrost_supports_compressed_format(dev, MALI_BC4_UNORM);
1116
1117 pscreen->resource_create_with_modifiers =
1118 panfrost_resource_create_with_modifiers;
1119 pscreen->resource_create = u_transfer_helper_resource_create;
1120 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1121 pscreen->resource_from_handle = panfrost_resource_from_handle;
1122 pscreen->resource_get_handle = panfrost_resource_get_handle;
1123 pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1124 true, false,
1125 fake_rgtc, true);
1126 }
1127
1128 void
1129 panfrost_resource_context_init(struct pipe_context *pctx)
1130 {
1131 pctx->transfer_map = u_transfer_helper_transfer_map;
1132 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
1133 pctx->create_surface = panfrost_create_surface;
1134 pctx->surface_destroy = panfrost_surface_destroy;
1135 pctx->resource_copy_region = util_resource_copy_region;
1136 pctx->blit = panfrost_blit;
1137 pctx->generate_mipmap = panfrost_generate_mipmap;
1138 pctx->flush_resource = panfrost_flush_resource;
1139 pctx->invalidate_resource = panfrost_invalidate_resource;
1140 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1141 pctx->buffer_subdata = u_default_buffer_subdata;
1142 pctx->texture_subdata = u_default_texture_subdata;
1143 }