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