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