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