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