2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019 Collabora, Ltd.
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:
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
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
26 * Authors (Collabora):
27 * Tomeu Vizoso <tomeu.vizoso@collabora.com>
28 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
34 #include "drm-uapi/drm_fourcc.h"
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"
45 #include "pan_context.h"
46 #include "pan_screen.h"
47 #include "pan_resource.h"
49 #include "pan_tiling.h"
50 #include "pandecode/decode.h"
51 #include "panfrost-quirks.h"
53 /* Wrapper around panfrost_bo_create that handles pandecode */
56 pan_bo_create(struct panfrost_device
*dev
, size_t size
, uint32_t flags
)
58 struct panfrost_bo
*bo
= panfrost_bo_create(dev
, size
, flags
);
60 if (pan_debug
& (PAN_DBG_TRACE
| PAN_DBG_SYNC
)) {
61 if (flags
& PAN_BO_INVISIBLE
)
62 pandecode_inject_mmap(bo
->gpu
, NULL
, bo
->size
, NULL
);
63 else if (!(flags
& PAN_BO_DELAY_MMAP
))
64 pandecode_inject_mmap(bo
->gpu
, bo
->cpu
, bo
->size
, NULL
);
71 panfrost_resource_reset_damage(struct panfrost_resource
*pres
)
73 /* We set the damage extent to the full resource size but keep the
74 * damage box empty so that the FB content is reloaded by default.
76 memset(&pres
->damage
, 0, sizeof(pres
->damage
));
77 pres
->damage
.extent
.maxx
= pres
->base
.width0
;
78 pres
->damage
.extent
.maxy
= pres
->base
.height0
;
81 static struct pipe_resource
*
82 panfrost_resource_from_handle(struct pipe_screen
*pscreen
,
83 const struct pipe_resource
*templat
,
84 struct winsys_handle
*whandle
,
87 struct panfrost_device
*dev
= pan_device(pscreen
);
88 struct panfrost_resource
*rsc
;
89 struct pipe_resource
*prsc
;
91 assert(whandle
->type
== WINSYS_HANDLE_TYPE_FD
);
93 rsc
= rzalloc(pscreen
, struct panfrost_resource
);
101 pipe_reference_init(&prsc
->reference
, 1);
102 prsc
->screen
= pscreen
;
104 rsc
->bo
= panfrost_bo_import(dev
, whandle
->handle
);
105 rsc
->internal_format
= templat
->format
;
106 rsc
->layout
= MALI_TEXTURE_LINEAR
;
107 rsc
->slices
[0].stride
= whandle
->stride
;
108 rsc
->slices
[0].offset
= whandle
->offset
;
109 rsc
->slices
[0].initialized
= true;
110 panfrost_resource_reset_damage(rsc
);
112 if (dev
->quirks
& IS_BIFROST
&&
113 templat
->bind
& PIPE_BIND_RENDER_TARGET
) {
114 unsigned size
= panfrost_compute_checksum_size(
115 &rsc
->slices
[0], templat
->width0
, templat
->height0
);
116 rsc
->slices
[0].checksum_bo
= pan_bo_create(dev
, size
, 0);
117 rsc
->checksummed
= true;
122 renderonly_create_gpu_import_for_resource(prsc
, dev
->ro
, NULL
);
123 /* failure is expected in some cases.. */
130 panfrost_resource_get_handle(struct pipe_screen
*pscreen
,
131 struct pipe_context
*ctx
,
132 struct pipe_resource
*pt
,
133 struct winsys_handle
*handle
,
136 struct panfrost_device
*dev
= pan_device(pscreen
);
137 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) pt
;
138 struct renderonly_scanout
*scanout
= rsrc
->scanout
;
140 handle
->modifier
= DRM_FORMAT_MOD_INVALID
;
142 if (handle
->type
== WINSYS_HANDLE_TYPE_SHARED
) {
144 } else if (handle
->type
== WINSYS_HANDLE_TYPE_KMS
) {
145 if (renderonly_get_handle(scanout
, handle
))
148 handle
->handle
= rsrc
->bo
->gem_handle
;
149 handle
->stride
= rsrc
->slices
[0].stride
;
150 handle
->offset
= rsrc
->slices
[0].offset
;
152 } else if (handle
->type
== WINSYS_HANDLE_TYPE_FD
) {
154 struct drm_prime_handle args
= {
155 .handle
= scanout
->handle
,
156 .flags
= DRM_CLOEXEC
,
159 int ret
= drmIoctl(dev
->ro
->kms_fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
163 handle
->stride
= scanout
->stride
;
164 handle
->handle
= args
.fd
;
168 int fd
= panfrost_bo_export(rsrc
->bo
);
174 handle
->stride
= rsrc
->slices
[0].stride
;
175 handle
->offset
= rsrc
->slices
[0].offset
;
184 panfrost_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
186 //DBG("TODO %s\n", __func__);
189 static struct pipe_surface
*
190 panfrost_create_surface(struct pipe_context
*pipe
,
191 struct pipe_resource
*pt
,
192 const struct pipe_surface
*surf_tmpl
)
194 struct pipe_surface
*ps
= NULL
;
196 ps
= rzalloc(pipe
, struct pipe_surface
);
199 pipe_reference_init(&ps
->reference
, 1);
200 pipe_resource_reference(&ps
->texture
, pt
);
202 ps
->format
= surf_tmpl
->format
;
204 if (pt
->target
!= PIPE_BUFFER
) {
205 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
206 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
207 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
208 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
209 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
210 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
212 /* setting width as number of elements should get us correct renderbuffer width */
213 ps
->width
= surf_tmpl
->u
.buf
.last_element
- surf_tmpl
->u
.buf
.first_element
+ 1;
214 ps
->height
= pt
->height0
;
215 ps
->u
.buf
.first_element
= surf_tmpl
->u
.buf
.first_element
;
216 ps
->u
.buf
.last_element
= surf_tmpl
->u
.buf
.last_element
;
217 assert(ps
->u
.buf
.first_element
<= ps
->u
.buf
.last_element
);
218 assert(ps
->u
.buf
.last_element
< ps
->width
);
226 panfrost_surface_destroy(struct pipe_context
*pipe
,
227 struct pipe_surface
*surf
)
229 assert(surf
->texture
);
230 pipe_resource_reference(&surf
->texture
, NULL
);
234 static struct pipe_resource
*
235 panfrost_create_scanout_res(struct pipe_screen
*screen
,
236 const struct pipe_resource
*template)
238 struct panfrost_device
*dev
= pan_device(screen
);
239 struct pipe_resource scanout_templat
= *template;
240 struct renderonly_scanout
*scanout
;
241 struct winsys_handle handle
;
242 struct pipe_resource
*res
;
244 scanout
= renderonly_scanout_for_resource(&scanout_templat
,
249 assert(handle
.type
== WINSYS_HANDLE_TYPE_FD
);
250 /* TODO: handle modifiers? */
251 res
= screen
->resource_from_handle(screen
, template, &handle
,
252 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
);
253 close(handle
.handle
);
257 struct panfrost_resource
*pres
= pan_resource(res
);
259 pres
->scanout
= scanout
;
264 /* Setup the mip tree given a particular layout, possibly with checksumming */
267 panfrost_setup_slices(struct panfrost_resource
*pres
, size_t *bo_size
)
269 struct pipe_resource
*res
= &pres
->base
;
270 unsigned width
= res
->width0
;
271 unsigned height
= res
->height0
;
272 unsigned depth
= res
->depth0
;
273 unsigned bytes_per_pixel
= util_format_get_blocksize(res
->format
);
277 /* Tiled operates blockwise; linear is packed. Also, anything
278 * we render to has to be tile-aligned. Maybe not strictly
279 * necessary, but we're not *that* pressed for memory and it
280 * makes code a lot simpler */
282 bool renderable
= res
->bind
&
283 (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DEPTH_STENCIL
);
284 bool afbc
= pres
->layout
== MALI_TEXTURE_AFBC
;
285 bool tiled
= pres
->layout
== MALI_TEXTURE_TILED
;
286 bool should_align
= renderable
|| tiled
;
288 /* We don't know how to specify a 2D stride for 3D textures */
290 bool can_align_stride
=
291 res
->target
!= PIPE_TEXTURE_3D
;
293 should_align
&= can_align_stride
;
296 unsigned size_2d
= 0;
298 for (unsigned l
= 0; l
<= res
->last_level
; ++l
) {
299 struct panfrost_slice
*slice
= &pres
->slices
[l
];
301 unsigned effective_width
= width
;
302 unsigned effective_height
= height
;
303 unsigned effective_depth
= depth
;
306 effective_width
= ALIGN_POT(effective_width
, 16);
307 effective_height
= ALIGN_POT(effective_height
, 16);
309 /* We don't need to align depth */
312 /* Align levels to cache-line as a performance improvement for
313 * linear/tiled and as a requirement for AFBC */
315 offset
= ALIGN_POT(offset
, 64);
317 slice
->offset
= offset
;
319 /* Compute the would-be stride */
320 unsigned stride
= bytes_per_pixel
* effective_width
;
322 if (util_format_is_compressed(res
->format
))
325 /* ..but cache-line align it for performance */
326 if (can_align_stride
&& pres
->layout
== MALI_TEXTURE_LINEAR
)
327 stride
= ALIGN_POT(stride
, 64);
329 slice
->stride
= stride
;
331 unsigned slice_one_size
= slice
->stride
* effective_height
;
332 unsigned slice_full_size
= slice_one_size
* effective_depth
;
334 slice
->size0
= slice_one_size
;
336 /* Report 2D size for 3D texturing */
339 size_2d
= slice_one_size
;
341 /* Compute AFBC sizes if necessary */
344 panfrost_afbc_header_size(width
, height
);
346 offset
+= slice
->header_size
;
349 offset
+= slice_full_size
;
351 /* Add a checksum region if necessary */
352 if (pres
->checksummed
) {
353 slice
->checksum_offset
= offset
;
355 unsigned size
= panfrost_compute_checksum_size(
356 slice
, width
, height
);
361 width
= u_minify(width
, 1);
362 height
= u_minify(height
, 1);
363 depth
= u_minify(depth
, 1);
366 assert(res
->array_size
);
368 if (res
->target
!= PIPE_TEXTURE_3D
) {
369 /* Arrays and cubemaps have the entire miptree duplicated */
371 pres
->cubemap_stride
= ALIGN_POT(offset
, 64);
372 *bo_size
= ALIGN_POT(pres
->cubemap_stride
* res
->array_size
, 4096);
374 /* 3D strides across the 2D layers */
375 assert(res
->array_size
== 1);
377 pres
->cubemap_stride
= size_2d
;
378 *bo_size
= ALIGN_POT(offset
, 4096);
383 panfrost_resource_create_bo(struct panfrost_device
*dev
, struct panfrost_resource
*pres
)
385 struct pipe_resource
*res
= &pres
->base
;
387 /* Based on the usage, figure out what storing will be used. There are
390 * Linear: the basic format, bad for memory bandwidth, bad for cache
391 * use. Zero-copy, though. Renderable.
393 * Tiled: Not compressed, but cache-optimized. Expensive to write into
394 * (due to software tiling), but cheap to sample from. Ideal for most
397 * AFBC: Compressed and renderable (so always desirable for non-scanout
398 * rendertargets). Cheap to sample from. The format is black box, so we
399 * can't read/write from software.
401 * Tiling textures is almost always faster, unless we only use it once.
402 * Only a few types of resources can be tiled, ensure the bind is only
403 * (a combination of) one of the following */
405 const unsigned valid_binding
=
406 PIPE_BIND_DEPTH_STENCIL
|
407 PIPE_BIND_RENDER_TARGET
|
408 PIPE_BIND_BLENDABLE
|
409 PIPE_BIND_SAMPLER_VIEW
|
410 PIPE_BIND_DISPLAY_TARGET
;
412 unsigned bpp
= util_format_get_blocksizebits(res
->format
);
413 bool is_2d
= (res
->target
== PIPE_TEXTURE_2D
);
414 bool is_sane_bpp
= bpp
== 8 || bpp
== 16 || bpp
== 32 || bpp
== 64 || bpp
== 128;
415 bool should_tile
= (res
->usage
!= PIPE_USAGE_STREAM
);
416 bool must_tile
= (res
->bind
& PIPE_BIND_DEPTH_STENCIL
) && (dev
->quirks
& MIDGARD_SFBD
);
417 bool can_tile
= is_2d
&& is_sane_bpp
&& ((res
->bind
& ~valid_binding
) == 0);
419 /* FBOs we would like to checksum, if at all possible */
420 bool can_checksum
= !(res
->bind
& ~valid_binding
);
421 bool should_checksum
= res
->bind
& PIPE_BIND_RENDER_TARGET
;
423 pres
->checksummed
= can_checksum
&& should_checksum
;
425 /* Set the layout appropriately */
426 assert(!(must_tile
&& !can_tile
)); /* must_tile => can_tile */
427 pres
->layout
= ((can_tile
&& should_tile
) || must_tile
) ? MALI_TEXTURE_TILED
: MALI_TEXTURE_LINEAR
;
431 panfrost_setup_slices(pres
, &bo_size
);
433 /* We create a BO immediately but don't bother mapping, since we don't
434 * care to map e.g. FBOs which the CPU probably won't touch */
435 pres
->bo
= pan_bo_create(dev
, bo_size
, PAN_BO_DELAY_MMAP
);
439 panfrost_resource_set_damage_region(struct pipe_screen
*screen
,
440 struct pipe_resource
*res
,
442 const struct pipe_box
*rects
)
444 struct panfrost_resource
*pres
= pan_resource(res
);
445 struct pipe_box
*damage_rect
= &pres
->damage
.biggest_rect
;
446 struct pipe_scissor_state
*damage_extent
= &pres
->damage
.extent
;
450 panfrost_resource_reset_damage(pres
);
454 /* We keep track of 2 different things here:
455 * 1 the damage extent: the quad including all damage regions. Will be
456 * used restrict the rendering area
457 * 2 the biggest damage rectangle: when there are more than one damage
458 * rect we keep the biggest one and will generate 4 wallpaper quads
459 * out of it (see panfrost_draw_wallpaper() for more details). We
460 * might want to do something smarter at some point.
462 * _________________________________
464 * | _________________________ |
465 * | | rect1| _________| |
466 * | |______|_____ | rect 3: | |
467 * | | | rect2 | | biggest | |
468 * | | |_______| | rect | |
469 * | |_______________|_________| |
471 * |_______________________________|
474 memset(&pres
->damage
, 0, sizeof(pres
->damage
));
475 damage_extent
->minx
= 0xffff;
476 damage_extent
->miny
= 0xffff;
477 for (i
= 0; i
< nrects
; i
++) {
478 int x
= rects
[i
].x
, w
= rects
[i
].width
, h
= rects
[i
].height
;
479 int y
= res
->height0
- (rects
[i
].y
+ h
);
481 /* Clamp x,y,w,h to prevent negative values. */
493 if (damage_rect
->width
* damage_rect
->height
< w
* h
)
494 u_box_2d(x
, y
, w
, h
, damage_rect
);
496 damage_extent
->minx
= MIN2(damage_extent
->minx
, x
);
497 damage_extent
->miny
= MIN2(damage_extent
->miny
, y
);
498 damage_extent
->maxx
= MAX2(damage_extent
->maxx
,
499 MIN2(x
+ w
, res
->width0
));
500 damage_extent
->maxy
= MAX2(damage_extent
->maxy
,
501 MIN2(y
+ h
, res
->height0
));
505 static struct pipe_resource
*
506 panfrost_resource_create(struct pipe_screen
*screen
,
507 const struct pipe_resource
*template)
509 /* Make sure we're familiar */
510 switch (template->target
) {
512 case PIPE_TEXTURE_1D
:
513 case PIPE_TEXTURE_2D
:
514 case PIPE_TEXTURE_3D
:
515 case PIPE_TEXTURE_CUBE
:
516 case PIPE_TEXTURE_RECT
:
517 case PIPE_TEXTURE_2D_ARRAY
:
520 DBG("Unknown texture target %d\n", template->target
);
525 (PIPE_BIND_DISPLAY_TARGET
| PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
))
526 return panfrost_create_scanout_res(screen
, template);
528 struct panfrost_resource
*so
= rzalloc(screen
, struct panfrost_resource
);
529 struct panfrost_device
*dev
= pan_device(screen
);
531 so
->base
= *template;
532 so
->base
.screen
= screen
;
533 so
->internal_format
= template->format
;
535 pipe_reference_init(&so
->base
.reference
, 1);
537 util_range_init(&so
->valid_buffer_range
);
539 panfrost_resource_create_bo(dev
, so
);
540 panfrost_resource_reset_damage(so
);
542 if (template->bind
& PIPE_BIND_INDEX_BUFFER
)
543 so
->index_cache
= rzalloc(so
, struct panfrost_minmax_cache
);
545 return (struct pipe_resource
*)so
;
549 panfrost_resource_destroy(struct pipe_screen
*screen
,
550 struct pipe_resource
*pt
)
552 struct panfrost_device
*dev
= pan_device(screen
);
553 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) pt
;
556 renderonly_scanout_destroy(rsrc
->scanout
, dev
->ro
);
559 panfrost_bo_unreference(rsrc
->bo
);
561 if (rsrc
->slices
[0].checksum_bo
)
562 panfrost_bo_unreference(rsrc
->slices
[0].checksum_bo
);
564 util_range_destroy(&rsrc
->valid_buffer_range
);
570 panfrost_transfer_map(struct pipe_context
*pctx
,
571 struct pipe_resource
*resource
,
573 unsigned usage
, /* a combination of PIPE_TRANSFER_x */
574 const struct pipe_box
*box
,
575 struct pipe_transfer
**out_transfer
)
577 struct panfrost_context
*ctx
= pan_context(pctx
);
578 int bytes_per_pixel
= util_format_get_blocksize(resource
->format
);
579 struct panfrost_resource
*rsrc
= pan_resource(resource
);
580 struct panfrost_bo
*bo
= rsrc
->bo
;
582 struct panfrost_gtransfer
*transfer
= rzalloc(pctx
, struct panfrost_gtransfer
);
583 transfer
->base
.level
= level
;
584 transfer
->base
.usage
= usage
;
585 transfer
->base
.box
= *box
;
587 pipe_resource_reference(&transfer
->base
.resource
, resource
);
589 *out_transfer
= &transfer
->base
;
591 /* If we haven't already mmaped, now's the time */
592 panfrost_bo_mmap(bo
);
594 if (pan_debug
& (PAN_DBG_TRACE
| PAN_DBG_SYNC
))
595 pandecode_inject_mmap(bo
->gpu
, bo
->cpu
, bo
->size
, NULL
);
597 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
598 /* If the BO is used by one of the pending batches or if it's
599 * not ready yet (still accessed by one of the already flushed
600 * batches), we try to allocate a new one to avoid waiting.
602 if (panfrost_pending_batches_access_bo(ctx
, bo
) ||
603 !panfrost_bo_wait(bo
, 0, PAN_BO_ACCESS_RW
)) {
604 struct panfrost_device
*dev
= pan_device(pctx
->screen
);
605 /* We want the BO to be MMAPed. */
606 uint32_t flags
= bo
->flags
& ~PAN_BO_DELAY_MMAP
;
607 struct panfrost_bo
*newbo
= NULL
;
609 /* When the BO has been imported/exported, we can't
610 * replace it by another one, otherwise the
611 * importer/exporter wouldn't see the change we're
614 if (!(bo
->flags
& (PAN_BO_IMPORTED
| PAN_BO_EXPORTED
)))
615 newbo
= pan_bo_create(dev
, bo
->size
,
619 panfrost_bo_unreference(bo
);
623 uint32_t access
= PAN_BO_ACCESS_RW
;
625 /* Allocation failed or was impossible, let's
626 * fall back on a flush+wait.
628 panfrost_flush_batches_accessing_bo(ctx
, bo
,
630 panfrost_bo_wait(bo
, INT64_MAX
, access
);
633 } else if ((usage
& PIPE_TRANSFER_WRITE
)
634 && resource
->target
== PIPE_BUFFER
635 && !util_ranges_intersect(&rsrc
->valid_buffer_range
, box
->x
, box
->x
+ box
->width
)) {
636 /* No flush for writes to uninitialized */
637 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
638 if (usage
& PIPE_TRANSFER_WRITE
) {
639 panfrost_flush_batches_accessing_bo(ctx
, bo
, PAN_BO_ACCESS_RW
);
640 panfrost_bo_wait(bo
, INT64_MAX
, PAN_BO_ACCESS_RW
);
641 } else if (usage
& PIPE_TRANSFER_READ
) {
642 panfrost_flush_batches_accessing_bo(ctx
, bo
, PAN_BO_ACCESS_WRITE
);
643 panfrost_bo_wait(bo
, INT64_MAX
, PAN_BO_ACCESS_WRITE
);
647 if (rsrc
->layout
!= MALI_TEXTURE_LINEAR
) {
648 /* Non-linear resources need to be indirectly mapped */
650 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
653 transfer
->base
.stride
= box
->width
* bytes_per_pixel
;
654 transfer
->base
.layer_stride
= transfer
->base
.stride
* box
->height
;
655 transfer
->map
= rzalloc_size(transfer
, transfer
->base
.layer_stride
* box
->depth
);
656 assert(box
->depth
== 1);
658 if ((usage
& PIPE_TRANSFER_READ
) && rsrc
->slices
[level
].initialized
) {
659 if (rsrc
->layout
== MALI_TEXTURE_AFBC
) {
660 DBG("Unimplemented: reads from AFBC");
661 } else if (rsrc
->layout
== MALI_TEXTURE_TILED
) {
662 panfrost_load_tiled_image(
664 bo
->cpu
+ rsrc
->slices
[level
].offset
,
665 box
->x
, box
->y
, box
->width
, box
->height
,
666 transfer
->base
.stride
,
667 rsrc
->slices
[level
].stride
,
672 return transfer
->map
;
674 /* Direct, persistent writes create holes in time for
675 * caching... I don't know if this is actually possible but we
676 * should still get it right */
678 unsigned dpw
= PIPE_TRANSFER_MAP_DIRECTLY
| PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_PERSISTENT
;
680 if ((usage
& dpw
) == dpw
&& rsrc
->index_cache
)
683 transfer
->base
.stride
= rsrc
->slices
[level
].stride
;
684 transfer
->base
.layer_stride
= panfrost_get_layer_stride(
685 rsrc
->slices
, rsrc
->base
.target
== PIPE_TEXTURE_3D
,
686 rsrc
->cubemap_stride
, level
);
688 /* By mapping direct-write, we're implicitly already
689 * initialized (maybe), so be conservative */
691 if ((usage
& PIPE_TRANSFER_WRITE
) && (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)) {
692 rsrc
->slices
[level
].initialized
= true;
693 panfrost_minmax_cache_invalidate(rsrc
->index_cache
, &transfer
->base
);
697 + rsrc
->slices
[level
].offset
698 + transfer
->base
.box
.z
* transfer
->base
.layer_stride
699 + transfer
->base
.box
.y
* rsrc
->slices
[level
].stride
700 + transfer
->base
.box
.x
* bytes_per_pixel
;
705 panfrost_transfer_unmap(struct pipe_context
*pctx
,
706 struct pipe_transfer
*transfer
)
708 /* Gallium expects writeback here, so we tile */
710 struct panfrost_gtransfer
*trans
= pan_transfer(transfer
);
711 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) transfer
->resource
;
713 /* Mark whatever we wrote as written */
714 if (transfer
->usage
& PIPE_TRANSFER_WRITE
)
715 prsrc
->slices
[transfer
->level
].initialized
= true;
718 struct panfrost_bo
*bo
= prsrc
->bo
;
720 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
721 if (prsrc
->layout
== MALI_TEXTURE_AFBC
) {
722 DBG("Unimplemented: writes to AFBC\n");
723 } else if (prsrc
->layout
== MALI_TEXTURE_TILED
) {
724 assert(transfer
->box
.depth
== 1);
726 panfrost_store_tiled_image(
727 bo
->cpu
+ prsrc
->slices
[transfer
->level
].offset
,
729 transfer
->box
.x
, transfer
->box
.y
,
730 transfer
->box
.width
, transfer
->box
.height
,
731 prsrc
->slices
[transfer
->level
].stride
,
739 util_range_add(&prsrc
->base
, &prsrc
->valid_buffer_range
,
741 transfer
->box
.x
+ transfer
->box
.width
);
743 panfrost_minmax_cache_invalidate(prsrc
->index_cache
, transfer
);
745 /* Derefence the resource */
746 pipe_resource_reference(&transfer
->resource
, NULL
);
748 /* Transfer itself is RALLOCed at the moment */
749 ralloc_free(transfer
);
753 panfrost_transfer_flush_region(struct pipe_context
*pctx
,
754 struct pipe_transfer
*transfer
,
755 const struct pipe_box
*box
)
757 struct panfrost_resource
*rsc
= pan_resource(transfer
->resource
);
759 if (transfer
->resource
->target
== PIPE_BUFFER
) {
760 util_range_add(&rsc
->base
, &rsc
->valid_buffer_range
,
761 transfer
->box
.x
+ box
->x
,
762 transfer
->box
.x
+ box
->x
+ box
->width
);
764 unsigned level
= transfer
->level
;
765 rsc
->slices
[level
].initialized
= true;
770 panfrost_invalidate_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
772 //DBG("TODO %s\n", __func__);
775 static enum pipe_format
776 panfrost_resource_get_internal_format(struct pipe_resource
*rsrc
) {
777 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) rsrc
;
778 return prsrc
->internal_format
;
782 panfrost_generate_mipmap(
783 struct pipe_context
*pctx
,
784 struct pipe_resource
*prsrc
,
785 enum pipe_format format
,
788 unsigned first_layer
,
791 struct panfrost_context
*ctx
= pan_context(pctx
);
792 struct panfrost_resource
*rsrc
= pan_resource(prsrc
);
794 /* Generating a mipmap invalidates the written levels, so make that
795 * explicit so we don't try to wallpaper them back and end up with
796 * u_blitter recursion */
799 for (unsigned l
= base_level
+ 1; l
<= last_level
; ++l
)
800 rsrc
->slices
[l
].initialized
= false;
802 /* Beyond that, we just delegate the hard stuff. We're careful to
803 * include flushes on both ends to make sure the data is really valid.
804 * We could be doing a lot better perf-wise, especially once we have
805 * reorder-type optimizations in place. But for now prioritize
808 panfrost_flush_batches_accessing_bo(ctx
, rsrc
->bo
, PAN_BO_ACCESS_RW
);
809 panfrost_bo_wait(rsrc
->bo
, INT64_MAX
, PAN_BO_ACCESS_RW
);
811 /* We've flushed the original buffer if needed, now trigger a blit */
813 bool blit_res
= util_gen_mipmap(
815 base_level
, last_level
,
816 first_layer
, last_layer
,
817 PIPE_TEX_FILTER_LINEAR
);
819 /* If the blit was successful, flush once more. If it wasn't, well, let
820 * the state tracker deal with it. */
823 panfrost_flush_batches_accessing_bo(ctx
, rsrc
->bo
, PAN_BO_ACCESS_WRITE
);
824 panfrost_bo_wait(rsrc
->bo
, INT64_MAX
, PAN_BO_ACCESS_WRITE
);
830 /* Computes the address to a texture at a particular slice */
833 panfrost_get_texture_address(
834 struct panfrost_resource
*rsrc
,
835 unsigned level
, unsigned face
)
837 bool is_3d
= rsrc
->base
.target
== PIPE_TEXTURE_3D
;
838 return rsrc
->bo
->gpu
+ panfrost_texture_offset(rsrc
->slices
, is_3d
, rsrc
->cubemap_stride
, level
, face
);
841 /* Given a resource that has already been allocated, hint that it should use a
842 * given layout. These are suggestions, not commands; it is perfectly legal to
843 * stub out this function, but there will be performance implications. */
846 panfrost_resource_hint_layout(
847 struct panfrost_device
*dev
,
848 struct panfrost_resource
*rsrc
,
849 enum mali_texture_layout layout
,
852 /* Nothing to do, although a sophisticated implementation might store
855 if (rsrc
->layout
== layout
)
858 /* We don't use the weight yet, but we should check that it's positive
859 * (semantically meaning that we should choose the given `layout`) */
864 /* Check if the preferred layout is legal for this buffer */
866 if (layout
== MALI_TEXTURE_AFBC
) {
867 bool can_afbc
= panfrost_format_supports_afbc(rsrc
->base
.format
);
868 bool is_scanout
= rsrc
->base
.bind
&
869 (PIPE_BIND_DISPLAY_TARGET
| PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
);
871 if (!can_afbc
|| is_scanout
)
875 /* Simple heuristic so far: if the resource is uninitialized, switch to
876 * the hinted layout. If it is initialized, keep the original layout.
877 * This misses some cases where it would be beneficial to switch and
880 bool is_initialized
= false;
882 for (unsigned i
= 0; i
< MAX_MIP_LEVELS
; ++i
)
883 is_initialized
|= rsrc
->slices
[i
].initialized
;
888 /* We're uninitialized, so do a layout switch. Reinitialize slices. */
891 rsrc
->layout
= layout
;
892 panfrost_setup_slices(rsrc
, &new_size
);
894 /* If we grew in size, reallocate the BO */
895 if (new_size
> rsrc
->bo
->size
) {
896 panfrost_bo_unreference(rsrc
->bo
);
897 rsrc
->bo
= pan_bo_create(dev
, new_size
, PAN_BO_DELAY_MMAP
);
900 /* TODO: If there are textures bound, regenerate their descriptors */
904 panfrost_resource_set_stencil(struct pipe_resource
*prsrc
,
905 struct pipe_resource
*stencil
)
907 pan_resource(prsrc
)->separate_stencil
= pan_resource(stencil
);
910 static struct pipe_resource
*
911 panfrost_resource_get_stencil(struct pipe_resource
*prsrc
)
913 return &pan_resource(prsrc
)->separate_stencil
->base
;
916 static const struct u_transfer_vtbl transfer_vtbl
= {
917 .resource_create
= panfrost_resource_create
,
918 .resource_destroy
= panfrost_resource_destroy
,
919 .transfer_map
= panfrost_transfer_map
,
920 .transfer_unmap
= panfrost_transfer_unmap
,
921 .transfer_flush_region
= panfrost_transfer_flush_region
,
922 .get_internal_format
= panfrost_resource_get_internal_format
,
923 .set_stencil
= panfrost_resource_set_stencil
,
924 .get_stencil
= panfrost_resource_get_stencil
,
928 panfrost_resource_screen_init(struct pipe_screen
*pscreen
)
930 //pscreen->base.resource_create_with_modifiers =
931 // panfrost_resource_create_with_modifiers;
932 pscreen
->resource_create
= u_transfer_helper_resource_create
;
933 pscreen
->resource_destroy
= u_transfer_helper_resource_destroy
;
934 pscreen
->resource_from_handle
= panfrost_resource_from_handle
;
935 pscreen
->resource_get_handle
= panfrost_resource_get_handle
;
936 pscreen
->transfer_helper
= u_transfer_helper_create(&transfer_vtbl
,
942 panfrost_resource_context_init(struct pipe_context
*pctx
)
944 pctx
->transfer_map
= u_transfer_helper_transfer_map
;
945 pctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
946 pctx
->create_surface
= panfrost_create_surface
;
947 pctx
->surface_destroy
= panfrost_surface_destroy
;
948 pctx
->resource_copy_region
= util_resource_copy_region
;
949 pctx
->blit
= panfrost_blit
;
950 pctx
->generate_mipmap
= panfrost_generate_mipmap
;
951 pctx
->flush_resource
= panfrost_flush_resource
;
952 pctx
->invalidate_resource
= panfrost_invalidate_resource
;
953 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
954 pctx
->buffer_subdata
= u_default_buffer_subdata
;
955 pctx
->texture_subdata
= u_default_texture_subdata
;