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