panfrost: Fix linear depth textures
[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 "panfrost-quirks.h"
51
52 void
53 panfrost_resource_reset_damage(struct panfrost_resource *pres)
54 {
55 /* We set the damage extent to the full resource size but keep the
56 * damage box empty so that the FB content is reloaded by default.
57 */
58 memset(&pres->damage, 0, sizeof(pres->damage));
59 pres->damage.extent.maxx = pres->base.width0;
60 pres->damage.extent.maxy = pres->base.height0;
61 }
62
63 static struct pipe_resource *
64 panfrost_resource_from_handle(struct pipe_screen *pscreen,
65 const struct pipe_resource *templat,
66 struct winsys_handle *whandle,
67 unsigned usage)
68 {
69 struct panfrost_screen *screen = pan_screen(pscreen);
70 struct panfrost_resource *rsc;
71 struct pipe_resource *prsc;
72
73 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
74
75 rsc = rzalloc(pscreen, struct panfrost_resource);
76 if (!rsc)
77 return NULL;
78
79 prsc = &rsc->base;
80
81 *prsc = *templat;
82
83 pipe_reference_init(&prsc->reference, 1);
84 prsc->screen = pscreen;
85
86 rsc->bo = panfrost_bo_import(screen, whandle->handle);
87 rsc->internal_format = templat->format;
88 rsc->slices[0].stride = whandle->stride;
89 rsc->slices[0].offset = whandle->offset;
90 rsc->slices[0].initialized = true;
91 panfrost_resource_reset_damage(rsc);
92
93 if (screen->ro) {
94 rsc->scanout =
95 renderonly_create_gpu_import_for_resource(prsc, screen->ro, NULL);
96 /* failure is expected in some cases.. */
97 }
98
99 return prsc;
100 }
101
102 static bool
103 panfrost_resource_get_handle(struct pipe_screen *pscreen,
104 struct pipe_context *ctx,
105 struct pipe_resource *pt,
106 struct winsys_handle *handle,
107 unsigned usage)
108 {
109 struct panfrost_screen *screen = pan_screen(pscreen);
110 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
111 struct renderonly_scanout *scanout = rsrc->scanout;
112
113 handle->modifier = DRM_FORMAT_MOD_INVALID;
114
115 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
116 return false;
117 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
118 if (renderonly_get_handle(scanout, handle))
119 return true;
120
121 handle->handle = rsrc->bo->gem_handle;
122 handle->stride = rsrc->slices[0].stride;
123 handle->offset = rsrc->slices[0].offset;
124 return TRUE;
125 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
126 if (scanout) {
127 struct drm_prime_handle args = {
128 .handle = scanout->handle,
129 .flags = DRM_CLOEXEC,
130 };
131
132 int ret = drmIoctl(screen->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
133 if (ret == -1)
134 return false;
135
136 handle->stride = scanout->stride;
137 handle->handle = args.fd;
138
139 return true;
140 } else {
141 int fd = panfrost_bo_export(rsrc->bo);
142
143 if (fd < 0)
144 return false;
145
146 handle->handle = fd;
147 handle->stride = rsrc->slices[0].stride;
148 handle->offset = rsrc->slices[0].offset;
149 return true;
150 }
151 }
152
153 return false;
154 }
155
156 static void
157 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
158 {
159 //DBG("TODO %s\n", __func__);
160 }
161
162 static struct pipe_surface *
163 panfrost_create_surface(struct pipe_context *pipe,
164 struct pipe_resource *pt,
165 const struct pipe_surface *surf_tmpl)
166 {
167 struct pipe_surface *ps = NULL;
168
169 ps = rzalloc(pipe, struct pipe_surface);
170
171 if (ps) {
172 pipe_reference_init(&ps->reference, 1);
173 pipe_resource_reference(&ps->texture, pt);
174 ps->context = pipe;
175 ps->format = surf_tmpl->format;
176
177 if (pt->target != PIPE_BUFFER) {
178 assert(surf_tmpl->u.tex.level <= pt->last_level);
179 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
180 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
181 ps->u.tex.level = surf_tmpl->u.tex.level;
182 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
183 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
184 } else {
185 /* setting width as number of elements should get us correct renderbuffer width */
186 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
187 ps->height = pt->height0;
188 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
189 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
190 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
191 assert(ps->u.buf.last_element < ps->width);
192 }
193 }
194
195 return ps;
196 }
197
198 static void
199 panfrost_surface_destroy(struct pipe_context *pipe,
200 struct pipe_surface *surf)
201 {
202 assert(surf->texture);
203 pipe_resource_reference(&surf->texture, NULL);
204 ralloc_free(surf);
205 }
206
207 static struct pipe_resource *
208 panfrost_create_scanout_res(struct pipe_screen *screen,
209 const struct pipe_resource *template)
210 {
211 struct panfrost_screen *pscreen = pan_screen(screen);
212 struct pipe_resource scanout_templat = *template;
213 struct renderonly_scanout *scanout;
214 struct winsys_handle handle;
215 struct pipe_resource *res;
216
217 scanout = renderonly_scanout_for_resource(&scanout_templat,
218 pscreen->ro, &handle);
219 if (!scanout)
220 return NULL;
221
222 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
223 /* TODO: handle modifiers? */
224 res = screen->resource_from_handle(screen, template, &handle,
225 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
226 close(handle.handle);
227 if (!res)
228 return NULL;
229
230 struct panfrost_resource *pres = pan_resource(res);
231
232 pres->scanout = scanout;
233
234 return res;
235 }
236
237 /* Computes sizes for checksumming, which is 8 bytes per 16x16 tile */
238
239 #define CHECKSUM_TILE_WIDTH 16
240 #define CHECKSUM_TILE_HEIGHT 16
241 #define CHECKSUM_BYTES_PER_TILE 8
242
243 static unsigned
244 panfrost_compute_checksum_sizes(
245 struct panfrost_slice *slice,
246 unsigned width,
247 unsigned height)
248 {
249 unsigned aligned_width = ALIGN_POT(width, CHECKSUM_TILE_WIDTH);
250 unsigned aligned_height = ALIGN_POT(height, CHECKSUM_TILE_HEIGHT);
251
252 unsigned tile_count_x = aligned_width / CHECKSUM_TILE_WIDTH;
253 unsigned tile_count_y = aligned_height / CHECKSUM_TILE_HEIGHT;
254
255 slice->checksum_stride = tile_count_x * CHECKSUM_BYTES_PER_TILE;
256
257 return slice->checksum_stride * tile_count_y;
258 }
259
260 /* Setup the mip tree given a particular layout, possibly with checksumming */
261
262 static void
263 panfrost_setup_slices(struct panfrost_resource *pres, size_t *bo_size)
264 {
265 struct pipe_resource *res = &pres->base;
266 unsigned width = res->width0;
267 unsigned height = res->height0;
268 unsigned depth = res->depth0;
269 unsigned bytes_per_pixel = util_format_get_blocksize(res->format);
270
271 assert(depth > 0);
272
273 /* Tiled operates blockwise; linear is packed. Also, anything
274 * we render to has to be tile-aligned. Maybe not strictly
275 * necessary, but we're not *that* pressed for memory and it
276 * makes code a lot simpler */
277
278 bool renderable = res->bind &
279 (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL);
280 bool afbc = pres->layout == PAN_AFBC;
281 bool tiled = pres->layout == PAN_TILED;
282 bool should_align = renderable || tiled;
283
284 /* We don't know how to specify a 2D stride for 3D textures */
285
286 bool can_align_stride =
287 res->target != PIPE_TEXTURE_3D;
288
289 should_align &= can_align_stride;
290
291 unsigned offset = 0;
292 unsigned size_2d = 0;
293
294 for (unsigned l = 0; l <= res->last_level; ++l) {
295 struct panfrost_slice *slice = &pres->slices[l];
296
297 unsigned effective_width = width;
298 unsigned effective_height = height;
299 unsigned effective_depth = depth;
300
301 if (should_align) {
302 effective_width = ALIGN_POT(effective_width, 16);
303 effective_height = ALIGN_POT(effective_height, 16);
304
305 /* We don't need to align depth */
306 }
307
308 /* Align levels to cache-line as a performance improvement for
309 * linear/tiled and as a requirement for AFBC */
310
311 offset = ALIGN_POT(offset, 64);
312
313 slice->offset = offset;
314
315 /* Compute the would-be stride */
316 unsigned stride = bytes_per_pixel * effective_width;
317
318 /* ..but cache-line align it for performance */
319 if (can_align_stride && pres->layout == PAN_LINEAR)
320 stride = ALIGN_POT(stride, 64);
321
322 slice->stride = stride;
323
324 unsigned slice_one_size = slice->stride * effective_height;
325 unsigned slice_full_size = slice_one_size * effective_depth;
326
327 slice->size0 = slice_one_size;
328
329 /* Report 2D size for 3D texturing */
330
331 if (l == 0)
332 size_2d = slice_one_size;
333
334 /* Compute AFBC sizes if necessary */
335 if (afbc) {
336 slice->header_size =
337 panfrost_afbc_header_size(width, height);
338
339 offset += slice->header_size;
340 }
341
342 offset += slice_full_size;
343
344 /* Add a checksum region if necessary */
345 if (pres->checksummed) {
346 slice->checksum_offset = offset;
347
348 unsigned size = panfrost_compute_checksum_sizes(
349 slice, width, height);
350
351 offset += size;
352 }
353
354 width = u_minify(width, 1);
355 height = u_minify(height, 1);
356 depth = u_minify(depth, 1);
357 }
358
359 assert(res->array_size);
360
361 if (res->target != PIPE_TEXTURE_3D) {
362 /* Arrays and cubemaps have the entire miptree duplicated */
363
364 pres->cubemap_stride = ALIGN_POT(offset, 64);
365 *bo_size = ALIGN_POT(pres->cubemap_stride * res->array_size, 4096);
366 } else {
367 /* 3D strides across the 2D layers */
368 assert(res->array_size == 1);
369
370 pres->cubemap_stride = size_2d;
371 *bo_size = ALIGN_POT(offset, 4096);
372 }
373 }
374
375 static void
376 panfrost_resource_create_bo(struct panfrost_screen *screen, struct panfrost_resource *pres)
377 {
378 struct pipe_resource *res = &pres->base;
379
380 /* Based on the usage, figure out what storing will be used. There are
381 * various tradeoffs:
382 *
383 * Linear: the basic format, bad for memory bandwidth, bad for cache
384 * use. Zero-copy, though. Renderable.
385 *
386 * Tiled: Not compressed, but cache-optimized. Expensive to write into
387 * (due to software tiling), but cheap to sample from. Ideal for most
388 * textures.
389 *
390 * AFBC: Compressed and renderable (so always desirable for non-scanout
391 * rendertargets). Cheap to sample from. The format is black box, so we
392 * can't read/write from software.
393 *
394 * Tiling textures is almost always faster, unless we only use it once.
395 * Only a few types of resources can be tiled, ensure the bind is only
396 * (a combination of) one of the following */
397
398 const unsigned valid_binding =
399 PIPE_BIND_DEPTH_STENCIL |
400 PIPE_BIND_RENDER_TARGET |
401 PIPE_BIND_BLENDABLE |
402 PIPE_BIND_SAMPLER_VIEW |
403 PIPE_BIND_DISPLAY_TARGET;
404
405 bool is_2d = (res->target == PIPE_TEXTURE_2D);
406 bool should_tile = (res->usage != PIPE_USAGE_STREAM);
407 bool must_tile = (res->bind & PIPE_BIND_DEPTH_STENCIL) && (screen->quirks & MIDGARD_SFBD);
408 bool can_tile = is_2d && ((res->bind & ~valid_binding) == 0);
409
410 /* FBOs we would like to checksum, if at all possible */
411 bool can_checksum = !(res->bind & ~valid_binding);
412 bool should_checksum = res->bind & PIPE_BIND_RENDER_TARGET;
413
414 pres->checksummed = can_checksum && should_checksum;
415
416 /* Set the layout appropriately */
417 assert(!(must_tile && !can_tile)); /* must_tile => can_tile */
418 pres->layout = ((can_tile && should_tile) || must_tile) ? PAN_TILED : PAN_LINEAR;
419
420 size_t bo_size;
421
422 panfrost_setup_slices(pres, &bo_size);
423
424 /* We create a BO immediately but don't bother mapping, since we don't
425 * care to map e.g. FBOs which the CPU probably won't touch */
426 pres->bo = panfrost_bo_create(screen, bo_size, PAN_BO_DELAY_MMAP);
427 }
428
429 void
430 panfrost_resource_set_damage_region(struct pipe_screen *screen,
431 struct pipe_resource *res,
432 unsigned int nrects,
433 const struct pipe_box *rects)
434 {
435 struct panfrost_resource *pres = pan_resource(res);
436 struct pipe_box *damage_rect = &pres->damage.biggest_rect;
437 struct pipe_scissor_state *damage_extent = &pres->damage.extent;
438 unsigned int i;
439
440 if (!nrects) {
441 panfrost_resource_reset_damage(pres);
442 return;
443 }
444
445 /* We keep track of 2 different things here:
446 * 1 the damage extent: the quad including all damage regions. Will be
447 * used restrict the rendering area
448 * 2 the biggest damage rectangle: when there are more than one damage
449 * rect we keep the biggest one and will generate 4 wallpaper quads
450 * out of it (see panfrost_draw_wallpaper() for more details). We
451 * might want to do something smarter at some point.
452 *
453 * _________________________________
454 * | |
455 * | _________________________ |
456 * | | rect1| _________| |
457 * | |______|_____ | rect 3: | |
458 * | | | rect2 | | biggest | |
459 * | | |_______| | rect | |
460 * | |_______________|_________| |
461 * | damage extent |
462 * |_______________________________|
463 * resource
464 */
465 memset(&pres->damage, 0, sizeof(pres->damage));
466 damage_extent->minx = 0xffff;
467 damage_extent->miny = 0xffff;
468 for (i = 0; i < nrects; i++) {
469 int x = rects[i].x, w = rects[i].width, h = rects[i].height;
470 int y = res->height0 - (rects[i].y + h);
471
472 /* Clamp x,y,w,h to prevent negative values. */
473 if (x < 0) {
474 h += x;
475 x = 0;
476 }
477 if (y < 0) {
478 w += y;
479 y = 0;
480 }
481 w = MAX2(w, 0);
482 h = MAX2(h, 0);
483
484 if (damage_rect->width * damage_rect->height < w * h)
485 u_box_2d(x, y, w, h, damage_rect);
486
487 damage_extent->minx = MIN2(damage_extent->minx, x);
488 damage_extent->miny = MIN2(damage_extent->miny, y);
489 damage_extent->maxx = MAX2(damage_extent->maxx,
490 MIN2(x + w, res->width0));
491 damage_extent->maxy = MAX2(damage_extent->maxy,
492 MIN2(y + h, res->height0));
493 }
494 }
495
496 static struct pipe_resource *
497 panfrost_resource_create(struct pipe_screen *screen,
498 const struct pipe_resource *template)
499 {
500 /* Make sure we're familiar */
501 switch (template->target) {
502 case PIPE_BUFFER:
503 case PIPE_TEXTURE_1D:
504 case PIPE_TEXTURE_2D:
505 case PIPE_TEXTURE_3D:
506 case PIPE_TEXTURE_CUBE:
507 case PIPE_TEXTURE_RECT:
508 case PIPE_TEXTURE_2D_ARRAY:
509 break;
510 default:
511 DBG("Unknown texture target %d\n", template->target);
512 assert(0);
513 }
514
515 if (template->bind &
516 (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | PIPE_BIND_SHARED))
517 return panfrost_create_scanout_res(screen, template);
518
519 struct panfrost_resource *so = rzalloc(screen, struct panfrost_resource);
520 struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
521
522 so->base = *template;
523 so->base.screen = screen;
524 so->internal_format = template->format;
525
526 pipe_reference_init(&so->base.reference, 1);
527
528 util_range_init(&so->valid_buffer_range);
529
530 panfrost_resource_create_bo(pscreen, so);
531 panfrost_resource_reset_damage(so);
532
533 return (struct pipe_resource *)so;
534 }
535
536 static void
537 panfrost_resource_destroy(struct pipe_screen *screen,
538 struct pipe_resource *pt)
539 {
540 struct panfrost_screen *pscreen = pan_screen(screen);
541 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
542
543 if (rsrc->scanout)
544 renderonly_scanout_destroy(rsrc->scanout, pscreen->ro);
545
546 if (rsrc->bo)
547 panfrost_bo_unreference(rsrc->bo);
548
549 util_range_destroy(&rsrc->valid_buffer_range);
550 ralloc_free(rsrc);
551 }
552
553 static void *
554 panfrost_transfer_map(struct pipe_context *pctx,
555 struct pipe_resource *resource,
556 unsigned level,
557 unsigned usage, /* a combination of PIPE_TRANSFER_x */
558 const struct pipe_box *box,
559 struct pipe_transfer **out_transfer)
560 {
561 int bytes_per_pixel = util_format_get_blocksize(resource->format);
562 struct panfrost_resource *rsrc = pan_resource(resource);
563 struct panfrost_bo *bo = rsrc->bo;
564
565 struct panfrost_gtransfer *transfer = rzalloc(pctx, struct panfrost_gtransfer);
566 transfer->base.level = level;
567 transfer->base.usage = usage;
568 transfer->base.box = *box;
569
570 pipe_resource_reference(&transfer->base.resource, resource);
571
572 *out_transfer = &transfer->base;
573
574 /* If we haven't already mmaped, now's the time */
575 panfrost_bo_mmap(bo);
576
577 /* Check if we're bound for rendering and this is a read pixels. If so,
578 * we need to flush */
579
580 struct panfrost_context *ctx = pan_context(pctx);
581 struct pipe_framebuffer_state *fb = &ctx->pipe_framebuffer;
582
583 bool is_bound = false;
584
585 for (unsigned c = 0; c < fb->nr_cbufs; ++c) {
586 /* If cbufs is NULL, we're definitely not bound here */
587
588 if (fb->cbufs[c])
589 is_bound |= fb->cbufs[c]->texture == resource;
590 }
591
592 if (is_bound && (usage & PIPE_TRANSFER_READ))
593 assert(level == 0);
594
595 /* TODO: Respect usage flags */
596
597 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
598 /* If the BO is used by one of the pending batches or if it's
599 * not ready yet (still accessed by one of the already flushed
600 * batches), we try to allocate a new one to avoid waiting.
601 */
602 if (panfrost_pending_batches_access_bo(ctx, bo) ||
603 !panfrost_bo_wait(bo, 0, PAN_BO_ACCESS_RW)) {
604 struct panfrost_screen *screen = pan_screen(pctx->screen);
605 /* We want the BO to be MMAPed. */
606 uint32_t flags = bo->flags & ~PAN_BO_DELAY_MMAP;
607 struct panfrost_bo *newbo = NULL;
608
609 /* When the BO has been imported/exported, we can't
610 * replace it by another one, otherwise the
611 * importer/exporter wouldn't see the change we're
612 * doing to it.
613 */
614 if (!(bo->flags & (PAN_BO_IMPORTED | PAN_BO_EXPORTED)))
615 newbo = panfrost_bo_create(screen, bo->size,
616 flags);
617
618 if (newbo) {
619 panfrost_bo_unreference(bo);
620 rsrc->bo = newbo;
621 bo = newbo;
622 } else {
623 uint32_t access = PAN_BO_ACCESS_RW;
624
625 /* Allocation failed or was impossible, let's
626 * fall back on a flush+wait.
627 */
628 panfrost_flush_batches_accessing_bo(ctx, bo,
629 access);
630 panfrost_bo_wait(bo, INT64_MAX, access);
631 }
632 }
633 } else if ((usage & PIPE_TRANSFER_WRITE)
634 && resource->target == PIPE_BUFFER
635 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) {
636 /* No flush for writes to uninitialized */
637 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
638 if (usage & PIPE_TRANSFER_WRITE) {
639 panfrost_flush_batches_accessing_bo(ctx, bo, PAN_BO_ACCESS_RW);
640 panfrost_bo_wait(bo, INT64_MAX, PAN_BO_ACCESS_RW);
641 } else if (usage & PIPE_TRANSFER_READ) {
642 panfrost_flush_batches_accessing_bo(ctx, bo, PAN_BO_ACCESS_WRITE);
643 panfrost_bo_wait(bo, INT64_MAX, PAN_BO_ACCESS_WRITE);
644 } else {
645 /* Why are you even mapping?! */
646 }
647 }
648
649 if (rsrc->layout != PAN_LINEAR) {
650 /* Non-linear resources need to be indirectly mapped */
651
652 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
653 return NULL;
654
655 transfer->base.stride = box->width * bytes_per_pixel;
656 transfer->base.layer_stride = transfer->base.stride * box->height;
657 transfer->map = rzalloc_size(transfer, transfer->base.layer_stride * box->depth);
658 assert(box->depth == 1);
659
660 if ((usage & PIPE_TRANSFER_READ) && rsrc->slices[level].initialized) {
661 if (rsrc->layout == PAN_AFBC) {
662 DBG("Unimplemented: reads from AFBC");
663 } else if (rsrc->layout == PAN_TILED) {
664 panfrost_load_tiled_image(
665 transfer->map,
666 bo->cpu + rsrc->slices[level].offset,
667 box,
668 transfer->base.stride,
669 rsrc->slices[level].stride,
670 util_format_get_blocksize(resource->format));
671 }
672 }
673
674 return transfer->map;
675 } else {
676 transfer->base.stride = rsrc->slices[level].stride;
677 if (resource->target == PIPE_TEXTURE_3D)
678 transfer->base.layer_stride = rsrc->slices[level].size0;
679 else
680 transfer->base.layer_stride = rsrc->cubemap_stride;
681
682 /* By mapping direct-write, we're implicitly already
683 * initialized (maybe), so be conservative */
684
685 if ((usage & PIPE_TRANSFER_WRITE) && (usage & PIPE_TRANSFER_MAP_DIRECTLY))
686 rsrc->slices[level].initialized = true;
687
688 return bo->cpu
689 + rsrc->slices[level].offset
690 + transfer->base.box.z * transfer->base.layer_stride
691 + transfer->base.box.y * rsrc->slices[level].stride
692 + transfer->base.box.x * bytes_per_pixel;
693 }
694 }
695
696 static void
697 panfrost_transfer_unmap(struct pipe_context *pctx,
698 struct pipe_transfer *transfer)
699 {
700 /* Gallium expects writeback here, so we tile */
701
702 struct panfrost_gtransfer *trans = pan_transfer(transfer);
703 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
704
705 /* Mark whatever we wrote as written */
706 if (transfer->usage & PIPE_TRANSFER_WRITE)
707 prsrc->slices[transfer->level].initialized = true;
708
709 if (trans->map) {
710 struct panfrost_bo *bo = prsrc->bo;
711
712 if (transfer->usage & PIPE_TRANSFER_WRITE) {
713 if (prsrc->layout == PAN_AFBC) {
714 DBG("Unimplemented: writes to AFBC\n");
715 } else if (prsrc->layout == PAN_TILED) {
716 assert(transfer->box.depth == 1);
717
718 panfrost_store_tiled_image(
719 bo->cpu + prsrc->slices[transfer->level].offset,
720 trans->map,
721 &transfer->box,
722 prsrc->slices[transfer->level].stride,
723 transfer->stride,
724 util_format_get_blocksize(prsrc->base.format));
725 }
726 }
727 }
728
729
730 util_range_add(&prsrc->base, &prsrc->valid_buffer_range,
731 transfer->box.x,
732 transfer->box.x + transfer->box.width);
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 unsigned level_offset = rsrc->slices[level].offset;
827 unsigned face_offset = face * rsrc->cubemap_stride;
828
829 return rsrc->bo->gpu + level_offset + face_offset;
830 }
831
832 /* Given a resource that has already been allocated, hint that it should use a
833 * given layout. These are suggestions, not commands; it is perfectly legal to
834 * stub out this function, but there will be performance implications. */
835
836 void
837 panfrost_resource_hint_layout(
838 struct panfrost_screen *screen,
839 struct panfrost_resource *rsrc,
840 enum panfrost_memory_layout layout,
841 signed weight)
842 {
843 /* Nothing to do, although a sophisticated implementation might store
844 * the hint */
845
846 if (rsrc->layout == layout)
847 return;
848
849 /* We don't use the weight yet, but we should check that it's positive
850 * (semantically meaning that we should choose the given `layout`) */
851
852 if (weight <= 0)
853 return;
854
855 /* Check if the preferred layout is legal for this buffer */
856
857 if (layout == PAN_AFBC) {
858 bool can_afbc = panfrost_format_supports_afbc(rsrc->base.format);
859 bool is_scanout = rsrc->base.bind &
860 (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | PIPE_BIND_SHARED);
861
862 if (!can_afbc || is_scanout)
863 return;
864 }
865
866 /* Simple heuristic so far: if the resource is uninitialized, switch to
867 * the hinted layout. If it is initialized, keep the original layout.
868 * This misses some cases where it would be beneficial to switch and
869 * blit. */
870
871 bool is_initialized = false;
872
873 for (unsigned i = 0; i < MAX_MIP_LEVELS; ++i)
874 is_initialized |= rsrc->slices[i].initialized;
875
876 if (is_initialized)
877 return;
878
879 /* We're uninitialized, so do a layout switch. Reinitialize slices. */
880
881 size_t new_size;
882 rsrc->layout = layout;
883 panfrost_setup_slices(rsrc, &new_size);
884
885 /* If we grew in size, reallocate the BO */
886 if (new_size > rsrc->bo->size) {
887 panfrost_bo_unreference(rsrc->bo);
888 rsrc->bo = panfrost_bo_create(screen, new_size, PAN_BO_DELAY_MMAP);
889 }
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 panfrost_screen *pscreen)
918 {
919 //pscreen->base.resource_create_with_modifiers =
920 // panfrost_resource_create_with_modifiers;
921 pscreen->base.resource_create = u_transfer_helper_resource_create;
922 pscreen->base.resource_destroy = u_transfer_helper_resource_destroy;
923 pscreen->base.resource_from_handle = panfrost_resource_from_handle;
924 pscreen->base.resource_get_handle = panfrost_resource_get_handle;
925 pscreen->base.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 }