3505f373788eb438ab8d6f734748186874d4da84
[mesa.git] / src / gallium / drivers / panfrost / pan_resource.c
1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019 Collabora, Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 * Authors (Collabora):
27 * Tomeu Vizoso <tomeu.vizoso@collabora.com>
28 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
29 *
30 */
31
32 #include <xf86drm.h>
33 #include <fcntl.h>
34 #include "drm-uapi/drm_fourcc.h"
35
36 #include "state_tracker/winsys_handle.h"
37 #include "util/format/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_surface.h"
40 #include "util/u_transfer.h"
41 #include "util/u_transfer_helper.h"
42 #include "util/u_gen_mipmap.h"
43
44 #include "pan_bo.h"
45 #include "pan_context.h"
46 #include "pan_screen.h"
47 #include "pan_resource.h"
48 #include "pan_util.h"
49 #include "pan_tiling.h"
50 #include "pandecode/decode.h"
51 #include "panfrost-quirks.h"
52
53 /* Wrapper around panfrost_bo_create that handles pandecode */
54
55 struct panfrost_bo *
56 pan_bo_create(struct panfrost_device *dev, size_t size, uint32_t flags)
57 {
58 struct panfrost_bo *bo = panfrost_bo_create(dev, size, flags);
59
60 if (pan_debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
61 if (flags & PAN_BO_INVISIBLE)
62 pandecode_inject_mmap(bo->gpu, NULL, bo->size, NULL);
63 else if (!(flags & PAN_BO_DELAY_MMAP))
64 pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
65 }
66
67 return bo;
68 }
69
70 void
71 panfrost_resource_reset_damage(struct panfrost_resource *pres)
72 {
73 /* We set the damage extent to the full resource size but keep the
74 * damage box empty so that the FB content is reloaded by default.
75 */
76 memset(&pres->damage, 0, sizeof(pres->damage));
77 pres->damage.extent.maxx = pres->base.width0;
78 pres->damage.extent.maxy = pres->base.height0;
79 }
80
81 static struct pipe_resource *
82 panfrost_resource_from_handle(struct pipe_screen *pscreen,
83 const struct pipe_resource *templat,
84 struct winsys_handle *whandle,
85 unsigned usage)
86 {
87 struct panfrost_device *dev = pan_device(pscreen);
88 struct panfrost_resource *rsc;
89 struct pipe_resource *prsc;
90
91 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
92
93 rsc = rzalloc(pscreen, struct panfrost_resource);
94 if (!rsc)
95 return NULL;
96
97 prsc = &rsc->base;
98
99 *prsc = *templat;
100
101 pipe_reference_init(&prsc->reference, 1);
102 prsc->screen = pscreen;
103
104 rsc->bo = panfrost_bo_import(dev, whandle->handle);
105 rsc->internal_format = templat->format;
106 rsc->layout = MALI_TEXTURE_LINEAR;
107 rsc->slices[0].stride = whandle->stride;
108 rsc->slices[0].offset = whandle->offset;
109 rsc->slices[0].initialized = true;
110 panfrost_resource_reset_damage(rsc);
111
112 if (dev->ro) {
113 rsc->scanout =
114 renderonly_create_gpu_import_for_resource(prsc, dev->ro, NULL);
115 /* failure is expected in some cases.. */
116 }
117
118 return prsc;
119 }
120
121 static bool
122 panfrost_resource_get_handle(struct pipe_screen *pscreen,
123 struct pipe_context *ctx,
124 struct pipe_resource *pt,
125 struct winsys_handle *handle,
126 unsigned usage)
127 {
128 struct panfrost_device *dev = pan_device(pscreen);
129 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
130 struct renderonly_scanout *scanout = rsrc->scanout;
131
132 handle->modifier = DRM_FORMAT_MOD_INVALID;
133
134 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
135 return false;
136 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
137 if (renderonly_get_handle(scanout, handle))
138 return true;
139
140 handle->handle = rsrc->bo->gem_handle;
141 handle->stride = rsrc->slices[0].stride;
142 handle->offset = rsrc->slices[0].offset;
143 return TRUE;
144 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
145 if (scanout) {
146 struct drm_prime_handle args = {
147 .handle = scanout->handle,
148 .flags = DRM_CLOEXEC,
149 };
150
151 int ret = drmIoctl(dev->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
152 if (ret == -1)
153 return false;
154
155 handle->stride = scanout->stride;
156 handle->handle = args.fd;
157
158 return true;
159 } else {
160 int fd = panfrost_bo_export(rsrc->bo);
161
162 if (fd < 0)
163 return false;
164
165 handle->handle = fd;
166 handle->stride = rsrc->slices[0].stride;
167 handle->offset = rsrc->slices[0].offset;
168 return true;
169 }
170 }
171
172 return false;
173 }
174
175 static void
176 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
177 {
178 //DBG("TODO %s\n", __func__);
179 }
180
181 static struct pipe_surface *
182 panfrost_create_surface(struct pipe_context *pipe,
183 struct pipe_resource *pt,
184 const struct pipe_surface *surf_tmpl)
185 {
186 struct pipe_surface *ps = NULL;
187
188 ps = rzalloc(pipe, struct pipe_surface);
189
190 if (ps) {
191 pipe_reference_init(&ps->reference, 1);
192 pipe_resource_reference(&ps->texture, pt);
193 ps->context = pipe;
194 ps->format = surf_tmpl->format;
195
196 if (pt->target != PIPE_BUFFER) {
197 assert(surf_tmpl->u.tex.level <= pt->last_level);
198 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
199 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
200 ps->u.tex.level = surf_tmpl->u.tex.level;
201 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
202 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
203 } else {
204 /* setting width as number of elements should get us correct renderbuffer width */
205 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
206 ps->height = pt->height0;
207 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
208 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
209 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
210 assert(ps->u.buf.last_element < ps->width);
211 }
212 }
213
214 return ps;
215 }
216
217 static void
218 panfrost_surface_destroy(struct pipe_context *pipe,
219 struct pipe_surface *surf)
220 {
221 assert(surf->texture);
222 pipe_resource_reference(&surf->texture, NULL);
223 ralloc_free(surf);
224 }
225
226 static struct pipe_resource *
227 panfrost_create_scanout_res(struct pipe_screen *screen,
228 const struct pipe_resource *template)
229 {
230 struct panfrost_device *dev = pan_device(screen);
231 struct pipe_resource scanout_templat = *template;
232 struct renderonly_scanout *scanout;
233 struct winsys_handle handle;
234 struct pipe_resource *res;
235
236 scanout = renderonly_scanout_for_resource(&scanout_templat,
237 dev->ro, &handle);
238 if (!scanout)
239 return NULL;
240
241 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
242 /* TODO: handle modifiers? */
243 res = screen->resource_from_handle(screen, template, &handle,
244 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
245 close(handle.handle);
246 if (!res)
247 return NULL;
248
249 struct panfrost_resource *pres = pan_resource(res);
250
251 pres->scanout = scanout;
252
253 return res;
254 }
255
256 /* Setup the mip tree given a particular layout, possibly with checksumming */
257
258 static void
259 panfrost_setup_slices(struct panfrost_resource *pres, size_t *bo_size)
260 {
261 struct pipe_resource *res = &pres->base;
262 unsigned width = res->width0;
263 unsigned height = res->height0;
264 unsigned depth = res->depth0;
265 unsigned bytes_per_pixel = util_format_get_blocksize(res->format);
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 bool afbc = pres->layout == MALI_TEXTURE_AFBC;
277 bool tiled = pres->layout == MALI_TEXTURE_TILED;
278 bool should_align = renderable || tiled;
279
280 /* We don't know how to specify a 2D stride for 3D textures */
281
282 bool can_align_stride =
283 res->target != PIPE_TEXTURE_3D;
284
285 should_align &= can_align_stride;
286
287 unsigned offset = 0;
288 unsigned size_2d = 0;
289
290 for (unsigned l = 0; l <= res->last_level; ++l) {
291 struct panfrost_slice *slice = &pres->slices[l];
292
293 unsigned effective_width = width;
294 unsigned effective_height = height;
295 unsigned effective_depth = depth;
296
297 if (should_align) {
298 effective_width = ALIGN_POT(effective_width, 16);
299 effective_height = ALIGN_POT(effective_height, 16);
300
301 /* We don't need to align depth */
302 }
303
304 /* Align levels to cache-line as a performance improvement for
305 * linear/tiled and as a requirement for AFBC */
306
307 offset = ALIGN_POT(offset, 64);
308
309 slice->offset = offset;
310
311 /* Compute the would-be stride */
312 unsigned stride = bytes_per_pixel * effective_width;
313
314 if (util_format_is_compressed(res->format))
315 stride /= 4;
316
317 /* ..but cache-line align it for performance */
318 if (can_align_stride && pres->layout == MALI_TEXTURE_LINEAR)
319 stride = ALIGN_POT(stride, 64);
320
321 slice->stride = stride;
322
323 unsigned slice_one_size = slice->stride * effective_height;
324 unsigned slice_full_size = slice_one_size * effective_depth;
325
326 slice->size0 = slice_one_size;
327
328 /* Report 2D size for 3D texturing */
329
330 if (l == 0)
331 size_2d = slice_one_size;
332
333 /* Compute AFBC sizes if necessary */
334 if (afbc) {
335 slice->header_size =
336 panfrost_afbc_header_size(width, height);
337
338 offset += slice->header_size;
339 }
340
341 offset += slice_full_size;
342
343 /* Add a checksum region if necessary */
344 if (pres->checksummed) {
345 slice->checksum_offset = offset;
346
347 unsigned size = panfrost_compute_checksum_size(
348 slice, width, height);
349
350 offset += size;
351 }
352
353 width = u_minify(width, 1);
354 height = u_minify(height, 1);
355 depth = u_minify(depth, 1);
356 }
357
358 assert(res->array_size);
359
360 if (res->target != PIPE_TEXTURE_3D) {
361 /* Arrays and cubemaps have the entire miptree duplicated */
362
363 pres->cubemap_stride = ALIGN_POT(offset, 64);
364 *bo_size = ALIGN_POT(pres->cubemap_stride * res->array_size, 4096);
365 } else {
366 /* 3D strides across the 2D layers */
367 assert(res->array_size == 1);
368
369 pres->cubemap_stride = size_2d;
370 *bo_size = ALIGN_POT(offset, 4096);
371 }
372 }
373
374 static void
375 panfrost_resource_create_bo(struct panfrost_device *dev, struct panfrost_resource *pres)
376 {
377 struct pipe_resource *res = &pres->base;
378
379 /* Based on the usage, figure out what storing will be used. There are
380 * various tradeoffs:
381 *
382 * Linear: the basic format, bad for memory bandwidth, bad for cache
383 * use. Zero-copy, though. Renderable.
384 *
385 * Tiled: Not compressed, but cache-optimized. Expensive to write into
386 * (due to software tiling), but cheap to sample from. Ideal for most
387 * textures.
388 *
389 * AFBC: Compressed and renderable (so always desirable for non-scanout
390 * rendertargets). Cheap to sample from. The format is black box, so we
391 * can't read/write from software.
392 *
393 * Tiling textures is almost always faster, unless we only use it once.
394 * Only a few types of resources can be tiled, ensure the bind is only
395 * (a combination of) one of the following */
396
397 const unsigned valid_binding =
398 PIPE_BIND_DEPTH_STENCIL |
399 PIPE_BIND_RENDER_TARGET |
400 PIPE_BIND_BLENDABLE |
401 PIPE_BIND_SAMPLER_VIEW |
402 PIPE_BIND_DISPLAY_TARGET;
403
404 unsigned bpp = util_format_get_blocksizebits(res->format);
405 bool is_2d = (res->target == PIPE_TEXTURE_2D);
406 bool is_sane_bpp = bpp == 8 || bpp == 16 || bpp == 32 || bpp == 64 || bpp == 128;
407 bool should_tile = (res->usage != PIPE_USAGE_STREAM);
408 bool must_tile = (res->bind & PIPE_BIND_DEPTH_STENCIL) && (dev->quirks & MIDGARD_SFBD);
409 bool can_tile = is_2d && is_sane_bpp && ((res->bind & ~valid_binding) == 0);
410
411 /* FBOs we would like to checksum, if at all possible */
412 bool can_checksum = !(res->bind & ~valid_binding);
413 bool should_checksum = res->bind & PIPE_BIND_RENDER_TARGET;
414
415 pres->checksummed = can_checksum && should_checksum;
416
417 /* Set the layout appropriately */
418 assert(!(must_tile && !can_tile)); /* must_tile => can_tile */
419 pres->layout = ((can_tile && should_tile) || must_tile) ? MALI_TEXTURE_TILED : MALI_TEXTURE_LINEAR;
420
421 size_t bo_size;
422
423 panfrost_setup_slices(pres, &bo_size);
424
425 /* We create a BO immediately but don't bother mapping, since we don't
426 * care to map e.g. FBOs which the CPU probably won't touch */
427 pres->bo = pan_bo_create(dev, bo_size, PAN_BO_DELAY_MMAP);
428 }
429
430 void
431 panfrost_resource_set_damage_region(struct pipe_screen *screen,
432 struct pipe_resource *res,
433 unsigned int nrects,
434 const struct pipe_box *rects)
435 {
436 struct panfrost_resource *pres = pan_resource(res);
437 struct pipe_box *damage_rect = &pres->damage.biggest_rect;
438 struct pipe_scissor_state *damage_extent = &pres->damage.extent;
439 unsigned int i;
440
441 if (!nrects) {
442 panfrost_resource_reset_damage(pres);
443 return;
444 }
445
446 /* We keep track of 2 different things here:
447 * 1 the damage extent: the quad including all damage regions. Will be
448 * used restrict the rendering area
449 * 2 the biggest damage rectangle: when there are more than one damage
450 * rect we keep the biggest one and will generate 4 wallpaper quads
451 * out of it (see panfrost_draw_wallpaper() for more details). We
452 * might want to do something smarter at some point.
453 *
454 * _________________________________
455 * | |
456 * | _________________________ |
457 * | | rect1| _________| |
458 * | |______|_____ | rect 3: | |
459 * | | | rect2 | | biggest | |
460 * | | |_______| | rect | |
461 * | |_______________|_________| |
462 * | damage extent |
463 * |_______________________________|
464 * resource
465 */
466 memset(&pres->damage, 0, sizeof(pres->damage));
467 damage_extent->minx = 0xffff;
468 damage_extent->miny = 0xffff;
469 for (i = 0; i < nrects; i++) {
470 int x = rects[i].x, w = rects[i].width, h = rects[i].height;
471 int y = res->height0 - (rects[i].y + h);
472
473 /* Clamp x,y,w,h to prevent negative values. */
474 if (x < 0) {
475 h += x;
476 x = 0;
477 }
478 if (y < 0) {
479 w += y;
480 y = 0;
481 }
482 w = MAX2(w, 0);
483 h = MAX2(h, 0);
484
485 if (damage_rect->width * damage_rect->height < w * h)
486 u_box_2d(x, y, w, h, damage_rect);
487
488 damage_extent->minx = MIN2(damage_extent->minx, x);
489 damage_extent->miny = MIN2(damage_extent->miny, y);
490 damage_extent->maxx = MAX2(damage_extent->maxx,
491 MIN2(x + w, res->width0));
492 damage_extent->maxy = MAX2(damage_extent->maxy,
493 MIN2(y + h, res->height0));
494 }
495 }
496
497 static struct pipe_resource *
498 panfrost_resource_create(struct pipe_screen *screen,
499 const struct pipe_resource *template)
500 {
501 /* Make sure we're familiar */
502 switch (template->target) {
503 case PIPE_BUFFER:
504 case PIPE_TEXTURE_1D:
505 case PIPE_TEXTURE_2D:
506 case PIPE_TEXTURE_3D:
507 case PIPE_TEXTURE_CUBE:
508 case PIPE_TEXTURE_RECT:
509 case PIPE_TEXTURE_2D_ARRAY:
510 break;
511 default:
512 DBG("Unknown texture target %d\n", template->target);
513 assert(0);
514 }
515
516 if (template->bind &
517 (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | PIPE_BIND_SHARED))
518 return panfrost_create_scanout_res(screen, template);
519
520 struct panfrost_resource *so = rzalloc(screen, struct panfrost_resource);
521 struct panfrost_device *dev = pan_device(screen);
522
523 so->base = *template;
524 so->base.screen = screen;
525 so->internal_format = template->format;
526
527 pipe_reference_init(&so->base.reference, 1);
528
529 util_range_init(&so->valid_buffer_range);
530
531 panfrost_resource_create_bo(dev, so);
532 panfrost_resource_reset_damage(so);
533
534 if (template->bind & PIPE_BIND_INDEX_BUFFER)
535 so->index_cache = rzalloc(so, struct panfrost_minmax_cache);
536
537 return (struct pipe_resource *)so;
538 }
539
540 static void
541 panfrost_resource_destroy(struct pipe_screen *screen,
542 struct pipe_resource *pt)
543 {
544 struct panfrost_device *dev = pan_device(screen);
545 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
546
547 if (rsrc->scanout)
548 renderonly_scanout_destroy(rsrc->scanout, dev->ro);
549
550 if (rsrc->bo)
551 panfrost_bo_unreference(rsrc->bo);
552
553 util_range_destroy(&rsrc->valid_buffer_range);
554 ralloc_free(rsrc);
555 }
556
557
558 static void *
559 panfrost_transfer_map(struct pipe_context *pctx,
560 struct pipe_resource *resource,
561 unsigned level,
562 unsigned usage, /* a combination of PIPE_TRANSFER_x */
563 const struct pipe_box *box,
564 struct pipe_transfer **out_transfer)
565 {
566 struct panfrost_context *ctx = pan_context(pctx);
567 int bytes_per_pixel = util_format_get_blocksize(resource->format);
568 struct panfrost_resource *rsrc = pan_resource(resource);
569 struct panfrost_bo *bo = rsrc->bo;
570
571 struct panfrost_gtransfer *transfer = rzalloc(pctx, struct panfrost_gtransfer);
572 transfer->base.level = level;
573 transfer->base.usage = usage;
574 transfer->base.box = *box;
575
576 pipe_resource_reference(&transfer->base.resource, resource);
577
578 *out_transfer = &transfer->base;
579
580 /* If we haven't already mmaped, now's the time */
581 panfrost_bo_mmap(bo);
582
583 if (pan_debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
584 pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
585
586 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
587 /* If the BO is used by one of the pending batches or if it's
588 * not ready yet (still accessed by one of the already flushed
589 * batches), we try to allocate a new one to avoid waiting.
590 */
591 if (panfrost_pending_batches_access_bo(ctx, bo) ||
592 !panfrost_bo_wait(bo, 0, PAN_BO_ACCESS_RW)) {
593 struct panfrost_device *dev = pan_device(pctx->screen);
594 /* We want the BO to be MMAPed. */
595 uint32_t flags = bo->flags & ~PAN_BO_DELAY_MMAP;
596 struct panfrost_bo *newbo = NULL;
597
598 /* When the BO has been imported/exported, we can't
599 * replace it by another one, otherwise the
600 * importer/exporter wouldn't see the change we're
601 * doing to it.
602 */
603 if (!(bo->flags & (PAN_BO_IMPORTED | PAN_BO_EXPORTED)))
604 newbo = pan_bo_create(dev, bo->size,
605 flags);
606
607 if (newbo) {
608 panfrost_bo_unreference(bo);
609 rsrc->bo = newbo;
610 bo = newbo;
611 } else {
612 uint32_t access = PAN_BO_ACCESS_RW;
613
614 /* Allocation failed or was impossible, let's
615 * fall back on a flush+wait.
616 */
617 panfrost_flush_batches_accessing_bo(ctx, bo,
618 access);
619 panfrost_bo_wait(bo, INT64_MAX, access);
620 }
621 }
622 } else if ((usage & PIPE_TRANSFER_WRITE)
623 && resource->target == PIPE_BUFFER
624 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) {
625 /* No flush for writes to uninitialized */
626 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
627 if (usage & PIPE_TRANSFER_WRITE) {
628 panfrost_flush_batches_accessing_bo(ctx, bo, PAN_BO_ACCESS_RW);
629 panfrost_bo_wait(bo, INT64_MAX, PAN_BO_ACCESS_RW);
630 } else if (usage & PIPE_TRANSFER_READ) {
631 panfrost_flush_batches_accessing_bo(ctx, bo, PAN_BO_ACCESS_WRITE);
632 panfrost_bo_wait(bo, INT64_MAX, PAN_BO_ACCESS_WRITE);
633 }
634 }
635
636 if (rsrc->layout != MALI_TEXTURE_LINEAR) {
637 /* Non-linear resources need to be indirectly mapped */
638
639 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
640 return NULL;
641
642 transfer->base.stride = box->width * bytes_per_pixel;
643 transfer->base.layer_stride = transfer->base.stride * box->height;
644 transfer->map = rzalloc_size(transfer, transfer->base.layer_stride * box->depth);
645 assert(box->depth == 1);
646
647 if ((usage & PIPE_TRANSFER_READ) && rsrc->slices[level].initialized) {
648 if (rsrc->layout == MALI_TEXTURE_AFBC) {
649 DBG("Unimplemented: reads from AFBC");
650 } else if (rsrc->layout == MALI_TEXTURE_TILED) {
651 panfrost_load_tiled_image(
652 transfer->map,
653 bo->cpu + rsrc->slices[level].offset,
654 box->x, box->y, box->width, box->height,
655 transfer->base.stride,
656 rsrc->slices[level].stride,
657 resource->format);
658 }
659 }
660
661 return transfer->map;
662 } else {
663 /* Direct, persistent writes create holes in time for
664 * caching... I don't know if this is actually possible but we
665 * should still get it right */
666
667 unsigned dpw = PIPE_TRANSFER_MAP_DIRECTLY | PIPE_TRANSFER_WRITE | PIPE_TRANSFER_PERSISTENT;
668
669 if ((usage & dpw) == dpw && rsrc->index_cache)
670 return NULL;
671
672 transfer->base.stride = rsrc->slices[level].stride;
673 transfer->base.layer_stride = panfrost_get_layer_stride(
674 rsrc->slices, rsrc->base.target == PIPE_TEXTURE_3D,
675 rsrc->cubemap_stride, level);
676
677 /* By mapping direct-write, we're implicitly already
678 * initialized (maybe), so be conservative */
679
680 if ((usage & PIPE_TRANSFER_WRITE) && (usage & PIPE_TRANSFER_MAP_DIRECTLY)) {
681 rsrc->slices[level].initialized = true;
682 panfrost_minmax_cache_invalidate(rsrc->index_cache, &transfer->base);
683 }
684
685 return bo->cpu
686 + rsrc->slices[level].offset
687 + transfer->base.box.z * transfer->base.layer_stride
688 + transfer->base.box.y * rsrc->slices[level].stride
689 + transfer->base.box.x * bytes_per_pixel;
690 }
691 }
692
693 static void
694 panfrost_transfer_unmap(struct pipe_context *pctx,
695 struct pipe_transfer *transfer)
696 {
697 /* Gallium expects writeback here, so we tile */
698
699 struct panfrost_gtransfer *trans = pan_transfer(transfer);
700 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
701
702 /* Mark whatever we wrote as written */
703 if (transfer->usage & PIPE_TRANSFER_WRITE)
704 prsrc->slices[transfer->level].initialized = true;
705
706 if (trans->map) {
707 struct panfrost_bo *bo = prsrc->bo;
708
709 if (transfer->usage & PIPE_TRANSFER_WRITE) {
710 if (prsrc->layout == MALI_TEXTURE_AFBC) {
711 DBG("Unimplemented: writes to AFBC\n");
712 } else if (prsrc->layout == MALI_TEXTURE_TILED) {
713 assert(transfer->box.depth == 1);
714
715 panfrost_store_tiled_image(
716 bo->cpu + prsrc->slices[transfer->level].offset,
717 trans->map,
718 transfer->box.x, transfer->box.y,
719 transfer->box.width, transfer->box.height,
720 prsrc->slices[transfer->level].stride,
721 transfer->stride,
722 prsrc->base.format);
723 }
724 }
725 }
726
727
728 util_range_add(&prsrc->base, &prsrc->valid_buffer_range,
729 transfer->box.x,
730 transfer->box.x + transfer->box.width);
731
732 panfrost_minmax_cache_invalidate(prsrc->index_cache, transfer);
733
734 /* Derefence the resource */
735 pipe_resource_reference(&transfer->resource, NULL);
736
737 /* Transfer itself is RALLOCed at the moment */
738 ralloc_free(transfer);
739 }
740
741 static void
742 panfrost_transfer_flush_region(struct pipe_context *pctx,
743 struct pipe_transfer *transfer,
744 const struct pipe_box *box)
745 {
746 struct panfrost_resource *rsc = pan_resource(transfer->resource);
747
748 if (transfer->resource->target == PIPE_BUFFER) {
749 util_range_add(&rsc->base, &rsc->valid_buffer_range,
750 transfer->box.x + box->x,
751 transfer->box.x + box->x + box->width);
752 } else {
753 unsigned level = transfer->level;
754 rsc->slices[level].initialized = true;
755 }
756 }
757
758 static void
759 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
760 {
761 //DBG("TODO %s\n", __func__);
762 }
763
764 static enum pipe_format
765 panfrost_resource_get_internal_format(struct pipe_resource *rsrc) {
766 struct panfrost_resource *prsrc = (struct panfrost_resource *) rsrc;
767 return prsrc->internal_format;
768 }
769
770 static bool
771 panfrost_generate_mipmap(
772 struct pipe_context *pctx,
773 struct pipe_resource *prsrc,
774 enum pipe_format format,
775 unsigned base_level,
776 unsigned last_level,
777 unsigned first_layer,
778 unsigned last_layer)
779 {
780 struct panfrost_context *ctx = pan_context(pctx);
781 struct panfrost_resource *rsrc = pan_resource(prsrc);
782
783 /* Generating a mipmap invalidates the written levels, so make that
784 * explicit so we don't try to wallpaper them back and end up with
785 * u_blitter recursion */
786
787 assert(rsrc->bo);
788 for (unsigned l = base_level + 1; l <= last_level; ++l)
789 rsrc->slices[l].initialized = false;
790
791 /* Beyond that, we just delegate the hard stuff. We're careful to
792 * include flushes on both ends to make sure the data is really valid.
793 * We could be doing a lot better perf-wise, especially once we have
794 * reorder-type optimizations in place. But for now prioritize
795 * correctness. */
796
797 panfrost_flush_batches_accessing_bo(ctx, rsrc->bo, PAN_BO_ACCESS_RW);
798 panfrost_bo_wait(rsrc->bo, INT64_MAX, PAN_BO_ACCESS_RW);
799
800 /* We've flushed the original buffer if needed, now trigger a blit */
801
802 bool blit_res = util_gen_mipmap(
803 pctx, prsrc, format,
804 base_level, last_level,
805 first_layer, last_layer,
806 PIPE_TEX_FILTER_LINEAR);
807
808 /* If the blit was successful, flush once more. If it wasn't, well, let
809 * the state tracker deal with it. */
810
811 if (blit_res) {
812 panfrost_flush_batches_accessing_bo(ctx, rsrc->bo, PAN_BO_ACCESS_WRITE);
813 panfrost_bo_wait(rsrc->bo, INT64_MAX, PAN_BO_ACCESS_WRITE);
814 }
815
816 return blit_res;
817 }
818
819 /* Computes the address to a texture at a particular slice */
820
821 mali_ptr
822 panfrost_get_texture_address(
823 struct panfrost_resource *rsrc,
824 unsigned level, unsigned face)
825 {
826 bool is_3d = rsrc->base.target == PIPE_TEXTURE_3D;
827 return rsrc->bo->gpu + panfrost_texture_offset(rsrc->slices, is_3d, rsrc->cubemap_stride, level, face);
828 }
829
830 /* Given a resource that has already been allocated, hint that it should use a
831 * given layout. These are suggestions, not commands; it is perfectly legal to
832 * stub out this function, but there will be performance implications. */
833
834 void
835 panfrost_resource_hint_layout(
836 struct panfrost_device *dev,
837 struct panfrost_resource *rsrc,
838 enum mali_texture_layout layout,
839 signed weight)
840 {
841 /* Nothing to do, although a sophisticated implementation might store
842 * the hint */
843
844 if (rsrc->layout == layout)
845 return;
846
847 /* We don't use the weight yet, but we should check that it's positive
848 * (semantically meaning that we should choose the given `layout`) */
849
850 if (weight <= 0)
851 return;
852
853 /* Check if the preferred layout is legal for this buffer */
854
855 if (layout == MALI_TEXTURE_AFBC) {
856 bool can_afbc = panfrost_format_supports_afbc(rsrc->base.format);
857 bool is_scanout = rsrc->base.bind &
858 (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | PIPE_BIND_SHARED);
859
860 if (!can_afbc || is_scanout)
861 return;
862 }
863
864 /* Simple heuristic so far: if the resource is uninitialized, switch to
865 * the hinted layout. If it is initialized, keep the original layout.
866 * This misses some cases where it would be beneficial to switch and
867 * blit. */
868
869 bool is_initialized = false;
870
871 for (unsigned i = 0; i < MAX_MIP_LEVELS; ++i)
872 is_initialized |= rsrc->slices[i].initialized;
873
874 if (is_initialized)
875 return;
876
877 /* We're uninitialized, so do a layout switch. Reinitialize slices. */
878
879 size_t new_size;
880 rsrc->layout = layout;
881 panfrost_setup_slices(rsrc, &new_size);
882
883 /* If we grew in size, reallocate the BO */
884 if (new_size > rsrc->bo->size) {
885 panfrost_bo_unreference(rsrc->bo);
886 rsrc->bo = pan_bo_create(dev, new_size, PAN_BO_DELAY_MMAP);
887 }
888
889 /* TODO: If there are textures bound, regenerate their descriptors */
890 }
891
892 static void
893 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
894 struct pipe_resource *stencil)
895 {
896 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
897 }
898
899 static struct pipe_resource *
900 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
901 {
902 return &pan_resource(prsrc)->separate_stencil->base;
903 }
904
905 static const struct u_transfer_vtbl transfer_vtbl = {
906 .resource_create = panfrost_resource_create,
907 .resource_destroy = panfrost_resource_destroy,
908 .transfer_map = panfrost_transfer_map,
909 .transfer_unmap = panfrost_transfer_unmap,
910 .transfer_flush_region = panfrost_transfer_flush_region,
911 .get_internal_format = panfrost_resource_get_internal_format,
912 .set_stencil = panfrost_resource_set_stencil,
913 .get_stencil = panfrost_resource_get_stencil,
914 };
915
916 void
917 panfrost_resource_screen_init(struct pipe_screen *pscreen)
918 {
919 //pscreen->base.resource_create_with_modifiers =
920 // panfrost_resource_create_with_modifiers;
921 pscreen->resource_create = u_transfer_helper_resource_create;
922 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
923 pscreen->resource_from_handle = panfrost_resource_from_handle;
924 pscreen->resource_get_handle = panfrost_resource_get_handle;
925 pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
926 true, false,
927 true, true);
928 }
929
930 void
931 panfrost_resource_context_init(struct pipe_context *pctx)
932 {
933 pctx->transfer_map = u_transfer_helper_transfer_map;
934 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
935 pctx->create_surface = panfrost_create_surface;
936 pctx->surface_destroy = panfrost_surface_destroy;
937 pctx->resource_copy_region = util_resource_copy_region;
938 pctx->blit = panfrost_blit;
939 pctx->generate_mipmap = panfrost_generate_mipmap;
940 pctx->flush_resource = panfrost_flush_resource;
941 pctx->invalidate_resource = panfrost_invalidate_resource;
942 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
943 pctx->buffer_subdata = u_default_buffer_subdata;
944 pctx->texture_subdata = u_default_texture_subdata;
945 }