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