Path: blob/21.2-virgl/src/panfrost/lib/pan_afbc.c
4560 views
/*1* Copyright (C) 2019 Collabora, Ltd.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Alyssa Rosenzweig <[email protected]>24*/2526#include "pan_texture.h"2728/* Arm FrameBuffer Compression (AFBC) is a lossless compression scheme natively29* implemented in Mali GPUs (as well as many display controllers paired with30* Mali GPUs, etc). Where possible, Panfrost prefers to use AFBC for both31* rendering and texturing. In most cases, this is a performance-win due to a32* dramatic reduction in memory bandwidth and cache locality compared to a33* linear resources.34*35* AFBC divides the framebuffer into 16x16 tiles (other sizes possible, TODO:36* do we need to support this?). So, the width and height each must be aligned37* up to 16 pixels. This is inherently good for performance; note that for a 438* byte-per-pixel format like RGBA8888, that means that rows are 16*4=64 byte39* aligned, which is the cache-line size.40*41* For each AFBC-compressed resource, there is a single contiguous42* (CPU/GPU-shared) buffer. This buffer itself is divided into two parts:43* header and body, placed immediately after each other.44*45* The AFBC header contains 16 bytes of metadata per tile.46*47* The AFBC body is the same size as the original linear resource (padded to48* the nearest tile). Although the body comes immediately after the header, it49* must also be cache-line aligned, so there can sometimes be a bit of padding50* between the header and body.51*52* As an example, a 64x64 RGBA framebuffer contains 64/16 = 4 tiles horizontally and53* 4 tiles vertically. There are 4*4=16 tiles in total, each containing 1654* bytes of metadata, so there is a 16*16=256 byte header. 64x64 is already55* tile aligned, so the body is 64*64 * 4 bytes per pixel = 16384 bytes of56* body.57*58* From userspace, Panfrost needs to be able to calculate these sizes. It59* explicitly does not and can not know the format of the data contained within60* this header and body. The GPU has native support for AFBC encode/decode. For61* an internal FBO or a framebuffer used for scanout with an AFBC-compatible62* winsys/display-controller, the buffer is maintained AFBC throughout flight,63* and the driver never needs to know the internal data. For edge cases where64* the driver really does need to read/write from the AFBC resource, we65* generate a linear staging buffer and use the GPU to blit AFBC<--->linear.66* TODO: Implement me. */6768#define AFBC_TILE_WIDTH 1669#define AFBC_TILE_HEIGHT 1670#define AFBC_CACHE_ALIGN 647172/* Is it possible to AFBC compress a particular format? Common formats (and73* YUV) are compressible. Some obscure formats are not and fallback on linear,74* at a performance hit. Also, if you need to disable AFBC entirely in the75* driver for debug/profiling, just always return false here. */7677bool78panfrost_format_supports_afbc(const struct panfrost_device *dev, enum pipe_format format)79{80switch (format) {81case PIPE_FORMAT_R8G8B8A8_UNORM:82case PIPE_FORMAT_R8G8B8X8_UNORM:83case PIPE_FORMAT_R8G8B8_UNORM:84case PIPE_FORMAT_R5G6B5_UNORM:85case PIPE_FORMAT_Z24_UNORM_S8_UINT:86case PIPE_FORMAT_Z24X8_UNORM:87case PIPE_FORMAT_Z16_UNORM:88return true;89case PIPE_FORMAT_B8G8R8A8_UNORM:90case PIPE_FORMAT_B8G8R8X8_UNORM:91case PIPE_FORMAT_A8R8G8B8_UNORM:92case PIPE_FORMAT_X8R8G8B8_UNORM:93case PIPE_FORMAT_X8B8G8R8_UNORM:94case PIPE_FORMAT_A8B8G8R8_UNORM:95case PIPE_FORMAT_B8G8R8_UNORM:96case PIPE_FORMAT_B5G6R5_UNORM:97return (dev->arch < 7);98default:99return false;100}101}102103unsigned104panfrost_afbc_header_size(unsigned width, unsigned height)105{106/* Align to tile */107unsigned aligned_width = ALIGN_POT(width, AFBC_TILE_WIDTH);108unsigned aligned_height = ALIGN_POT(height, AFBC_TILE_HEIGHT);109110/* Compute size in tiles, rather than pixels */111unsigned tile_count_x = aligned_width / AFBC_TILE_WIDTH;112unsigned tile_count_y = aligned_height / AFBC_TILE_HEIGHT;113unsigned tile_count = tile_count_x * tile_count_y;114115/* Multiply to find the header size */116unsigned header_bytes = tile_count * AFBC_HEADER_BYTES_PER_TILE;117118/* Align and go */119return ALIGN_POT(header_bytes, AFBC_CACHE_ALIGN);120121}122123/* The lossless colour transform (AFBC_FORMAT_MOD_YTR) requires RGB. */124125bool126panfrost_afbc_can_ytr(enum pipe_format format)127{128const struct util_format_description *desc =129util_format_description(format);130131/* YTR is only defined for RGB(A) */132if (desc->nr_channels != 3 && desc->nr_channels != 4)133return false;134135/* The fourth channel if it exists doesn't matter */136return desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB;137}138139140