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