Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 1 | /* |
Alyssa Rosenzweig | d4575c3 | 2019-06-25 13:30:17 -0700 | [diff] [blame] | 2 | * Copyright (C) 2019 Collabora, Ltd. |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com> |
| 25 | */ |
| 26 | |
Alyssa Rosenzweig | 58f1401 | 2020-02-18 12:17:59 -0500 | [diff] [blame] | 27 | #include "pan_texture.h" |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 28 | |
| 29 | /* Arm FrameBuffer Compression (AFBC) is a lossless compression scheme natively |
| 30 | * implemented in Mali GPUs (as well as many display controllers paired with |
| 31 | * Mali GPUs, etc). Where possible, Panfrost prefers to use AFBC for both |
| 32 | * rendering and texturing. In most cases, this is a performance-win due to a |
| 33 | * dramatic reduction in memory bandwidth and cache locality compared to a |
| 34 | * linear resources. |
| 35 | * |
| 36 | * AFBC divides the framebuffer into 16x16 tiles (other sizes possible, TODO: |
| 37 | * do we need to support this?). So, the width and height each must be aligned |
| 38 | * up to 16 pixels. This is inherently good for performance; note that for a 4 |
| 39 | * byte-per-pixel format like RGBA8888, that means that rows are 16*4=64 byte |
| 40 | * aligned, which is the cache-line size. |
| 41 | * |
| 42 | * For each AFBC-compressed resource, there is a single contiguous |
| 43 | * (CPU/GPU-shared) buffer. This buffer itself is divided into two parts: |
| 44 | * header and body, placed immediately after each other. |
| 45 | * |
| 46 | * The AFBC header contains 16 bytes of metadata per tile. |
| 47 | * |
| 48 | * The AFBC body is the same size as the original linear resource (padded to |
| 49 | * the nearest tile). Although the body comes immediately after the header, it |
| 50 | * must also be cache-line aligned, so there can sometimes be a bit of padding |
| 51 | * between the header and body. |
| 52 | * |
| 53 | * As an example, a 64x64 RGBA framebuffer contains 64/16 = 4 tiles horizontally and |
| 54 | * 4 tiles vertically. There are 4*4=16 tiles in total, each containing 16 |
| 55 | * bytes of metadata, so there is a 16*16=256 byte header. 64x64 is already |
| 56 | * tile aligned, so the body is 64*64 * 4 bytes per pixel = 16384 bytes of |
| 57 | * body. |
| 58 | * |
| 59 | * From userspace, Panfrost needs to be able to calculate these sizes. It |
| 60 | * explicitly does not and can not know the format of the data contained within |
| 61 | * this header and body. The GPU has native support for AFBC encode/decode. For |
| 62 | * an internal FBO or a framebuffer used for scanout with an AFBC-compatible |
| 63 | * winsys/display-controller, the buffer is maintained AFBC throughout flight, |
| 64 | * and the driver never needs to know the internal data. For edge cases where |
| 65 | * the driver really does need to read/write from the AFBC resource, we |
| 66 | * generate a linear staging buffer and use the GPU to blit AFBC<--->linear. |
| 67 | * TODO: Implement me. */ |
| 68 | |
| 69 | #define AFBC_TILE_WIDTH 16 |
| 70 | #define AFBC_TILE_HEIGHT 16 |
| 71 | #define AFBC_HEADER_BYTES_PER_TILE 16 |
| 72 | #define AFBC_CACHE_ALIGN 64 |
| 73 | |
| 74 | /* Is it possible to AFBC compress a particular format? Common formats (and |
| 75 | * YUV) are compressible. Some obscure formats are not and fallback on linear, |
| 76 | * at a performance hit. Also, if you need to disable AFBC entirely in the |
| 77 | * driver for debug/profiling, just always return false here. */ |
| 78 | |
| 79 | bool |
| 80 | panfrost_format_supports_afbc(enum pipe_format format) |
| 81 | { |
| 82 | const struct util_format_description *desc = |
| 83 | util_format_description(format); |
| 84 | |
Alyssa Rosenzweig | 5aa51ba | 2019-06-18 07:41:26 -0700 | [diff] [blame] | 85 | /* sRGB cannot be AFBC, but it can be tiled. TODO: Verify. The blob |
| 86 | * does not do AFBC for SRGB8_ALPHA8, but it's not clear why it |
| 87 | * shouldn't be able to. */ |
| 88 | |
| 89 | if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) |
| 90 | return false; |
| 91 | |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 92 | if (util_format_is_rgba8_variant(desc)) |
| 93 | return true; |
| 94 | |
Alyssa Rosenzweig | 27d3685 | 2020-06-04 17:35:40 -0400 | [diff] [blame] | 95 | switch (format) { |
| 96 | case PIPE_FORMAT_R8G8B8_UNORM: |
| 97 | case PIPE_FORMAT_B8G8R8_UNORM: |
| 98 | case PIPE_FORMAT_R5G6B5_UNORM: |
| 99 | case PIPE_FORMAT_B5G6R5_UNORM: |
Alyssa Rosenzweig | cae1be9 | 2020-10-28 14:14:48 -0400 | [diff] [blame^] | 100 | case PIPE_FORMAT_Z24_UNORM_S8_UINT: |
| 101 | case PIPE_FORMAT_Z24X8_UNORM: |
Icecream95 | a188533 | 2020-10-17 01:49:58 +1300 | [diff] [blame] | 102 | case PIPE_FORMAT_Z16_UNORM: |
Alyssa Rosenzweig | 89a9cc7 | 2020-05-21 19:43:18 -0400 | [diff] [blame] | 103 | return true; |
Alyssa Rosenzweig | 27d3685 | 2020-06-04 17:35:40 -0400 | [diff] [blame] | 104 | default: |
Alyssa Rosenzweig | 8b6820c | 2020-06-04 17:36:16 -0400 | [diff] [blame] | 105 | return false; |
Alyssa Rosenzweig | 27d3685 | 2020-06-04 17:35:40 -0400 | [diff] [blame] | 106 | } |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Alyssa Rosenzweig | 3609b50 | 2019-06-21 14:54:44 -0700 | [diff] [blame] | 109 | unsigned |
| 110 | panfrost_afbc_header_size(unsigned width, unsigned height) |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 111 | { |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 112 | /* Align to tile */ |
Alyssa Rosenzweig | 7c82dfb | 2019-07-05 08:25:56 -0700 | [diff] [blame] | 113 | unsigned aligned_width = ALIGN_POT(width, AFBC_TILE_WIDTH); |
| 114 | unsigned aligned_height = ALIGN_POT(height, AFBC_TILE_HEIGHT); |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 115 | |
| 116 | /* Compute size in tiles, rather than pixels */ |
| 117 | unsigned tile_count_x = aligned_width / AFBC_TILE_WIDTH; |
| 118 | unsigned tile_count_y = aligned_height / AFBC_TILE_HEIGHT; |
| 119 | unsigned tile_count = tile_count_x * tile_count_y; |
| 120 | |
Alyssa Rosenzweig | 3609b50 | 2019-06-21 14:54:44 -0700 | [diff] [blame] | 121 | /* Multiply to find the header size */ |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 122 | unsigned header_bytes = tile_count * AFBC_HEADER_BYTES_PER_TILE; |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 123 | |
Alyssa Rosenzweig | 3609b50 | 2019-06-21 14:54:44 -0700 | [diff] [blame] | 124 | /* Align and go */ |
Alyssa Rosenzweig | 7c82dfb | 2019-07-05 08:25:56 -0700 | [diff] [blame] | 125 | return ALIGN_POT(header_bytes, AFBC_CACHE_ALIGN); |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 126 | |
Alyssa Rosenzweig | d878753 | 2019-06-07 09:39:31 -0700 | [diff] [blame] | 127 | } |
Alyssa Rosenzweig | d285ea6 | 2020-07-22 11:07:03 -0400 | [diff] [blame] | 128 | |
| 129 | /* The lossless colour transform (AFBC_FORMAT_MOD_YTR) requires RGB. */ |
| 130 | |
| 131 | bool |
| 132 | panfrost_afbc_can_ytr(enum pipe_format format) |
| 133 | { |
| 134 | const struct util_format_description *desc = |
| 135 | util_format_description(format); |
| 136 | |
| 137 | /* YTR is only defined for RGB(A) */ |
| 138 | if (desc->nr_channels != 3 && desc->nr_channels != 4) |
| 139 | return false; |
| 140 | |
| 141 | /* The fourth channel if it exists doesn't matter */ |
| 142 | return desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB; |
| 143 | } |