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