1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
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
27 #include "pan_texture.h"
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
54 * and 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 */
68
69 static enum pipe_format
unswizzled_format(unsigned arch,enum pipe_format format)70 unswizzled_format(unsigned arch, enum pipe_format format)
71 {
72 switch (format) {
73 case PIPE_FORMAT_A8_UNORM:
74 case PIPE_FORMAT_L8_UNORM:
75 case PIPE_FORMAT_I8_UNORM:
76 return PIPE_FORMAT_R8_UNORM;
77
78 case PIPE_FORMAT_L8A8_UNORM:
79 return PIPE_FORMAT_R8G8_UNORM;
80
81 case PIPE_FORMAT_B8G8R8_UNORM:
82 return PIPE_FORMAT_R8G8B8_UNORM;
83
84 case PIPE_FORMAT_R8G8B8X8_UNORM:
85 case PIPE_FORMAT_B8G8R8A8_UNORM:
86 case PIPE_FORMAT_B8G8R8X8_UNORM:
87 return PIPE_FORMAT_R8G8B8A8_UNORM;
88 case PIPE_FORMAT_A8R8G8B8_UNORM:
89 case PIPE_FORMAT_X8R8G8B8_UNORM:
90 case PIPE_FORMAT_X8B8G8R8_UNORM:
91 case PIPE_FORMAT_A8B8G8R8_UNORM:
92 /* v7 can only support AFBC for RGB and BGR */
93 return arch == 7 ? format : PIPE_FORMAT_R8G8B8A8_UNORM;
94
95 case PIPE_FORMAT_B5G6R5_UNORM:
96 return PIPE_FORMAT_R5G6B5_UNORM;
97
98 case PIPE_FORMAT_B5G5R5A1_UNORM:
99 return PIPE_FORMAT_R5G5B5A1_UNORM;
100
101 case PIPE_FORMAT_R10G10B10X2_UNORM:
102 case PIPE_FORMAT_B10G10R10A2_UNORM:
103 case PIPE_FORMAT_B10G10R10X2_UNORM:
104 return PIPE_FORMAT_R10G10B10A2_UNORM;
105
106 case PIPE_FORMAT_B4G4R4A4_UNORM:
107 return PIPE_FORMAT_R4G4B4A4_UNORM;
108 case PIPE_FORMAT_A4B4G4R4_UNORM:
109 /* v7 can only support AFBC for RGB and BGR */
110 return arch == 7 ? format : PIPE_FORMAT_R4G4B4A4_UNORM;
111
112 default:
113 return format;
114 }
115 }
116
117 /* AFBC supports compressing a few canonical formats. Additional formats are
118 * available by using a canonical internal format. Given a PIPE format, find
119 * the canonical AFBC internal format if it exists, or NONE if the format
120 * cannot be compressed. */
121
122 enum pan_afbc_mode
panfrost_afbc_format(unsigned arch,enum pipe_format format)123 panfrost_afbc_format(unsigned arch, enum pipe_format format)
124 {
125 /* sRGB does not change the pixel format itself, only the
126 * interpretation. The interpretation is handled by conversion hardware
127 * independent to the compression hardware, so we can compress sRGB
128 * formats by using the corresponding linear format.
129 */
130 format = util_format_linear(format);
131
132 /* Luminance-alpha not supported for AFBC on v7+ */
133 switch (format) {
134 case PIPE_FORMAT_A8_UNORM:
135 case PIPE_FORMAT_L8_UNORM:
136 case PIPE_FORMAT_I8_UNORM:
137 case PIPE_FORMAT_L8A8_UNORM:
138 if (arch >= 7)
139 return PAN_AFBC_MODE_INVALID;
140 else
141 break;
142 default:
143 break;
144 }
145
146 /* We handle swizzling orthogonally to AFBC */
147 format = unswizzled_format(arch, format);
148
149 /* clang-format off */
150 switch (format) {
151 case PIPE_FORMAT_R8_UNORM: return PAN_AFBC_MODE_R8;
152 case PIPE_FORMAT_R8G8_UNORM: return PAN_AFBC_MODE_R8G8;
153 case PIPE_FORMAT_R8G8B8_UNORM: return PAN_AFBC_MODE_R8G8B8;
154 case PIPE_FORMAT_R8G8B8A8_UNORM: return PAN_AFBC_MODE_R8G8B8A8;
155 case PIPE_FORMAT_R5G6B5_UNORM: return PAN_AFBC_MODE_R5G6B5;
156 case PIPE_FORMAT_R5G5B5A1_UNORM: return PAN_AFBC_MODE_R5G5B5A1;
157 case PIPE_FORMAT_R10G10B10A2_UNORM: return PAN_AFBC_MODE_R10G10B10A2;
158 case PIPE_FORMAT_R4G4B4A4_UNORM: return PAN_AFBC_MODE_R4G4B4A4;
159 case PIPE_FORMAT_Z16_UNORM: return PAN_AFBC_MODE_R8G8;
160
161 case PIPE_FORMAT_Z24_UNORM_S8_UINT: return PAN_AFBC_MODE_R8G8B8A8;
162 case PIPE_FORMAT_Z24X8_UNORM: return PAN_AFBC_MODE_R8G8B8A8;
163 case PIPE_FORMAT_X24S8_UINT: return PAN_AFBC_MODE_R8G8B8A8;
164
165 default: return PAN_AFBC_MODE_INVALID;
166 }
167 /* clang-format on */
168 }
169
170 /* The lossless colour transform (AFBC_FORMAT_MOD_YTR) requires RGB. */
171
172 bool
panfrost_afbc_can_ytr(enum pipe_format format)173 panfrost_afbc_can_ytr(enum pipe_format format)
174 {
175 const struct util_format_description *desc = util_format_description(format);
176
177 /* YTR is only defined for RGB(A) */
178 if (desc->nr_channels != 3 && desc->nr_channels != 4)
179 return false;
180
181 /* The fourth channel if it exists doesn't matter */
182 return desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB;
183 }
184
185 bool
panfrost_afbc_can_split(unsigned arch,enum pipe_format format,uint64_t modifier)186 panfrost_afbc_can_split(unsigned arch, enum pipe_format format,
187 uint64_t modifier)
188 {
189 unsigned block_width = panfrost_afbc_superblock_width(modifier);
190
191 if (arch < 6)
192 return false;
193
194 if (block_width == 16) {
195 return true;
196 } else if (block_width == 32) {
197 enum pan_afbc_mode mode = panfrost_afbc_format(arch, format);
198 return (mode == PAN_AFBC_MODE_R8G8B8A8 ||
199 mode == PAN_AFBC_MODE_R10G10B10A2);
200 }
201
202 return false;
203 }
204
205 /* Only support packing for RGB formats for now. */
206
207 bool
panfrost_afbc_can_pack(enum pipe_format format)208 panfrost_afbc_can_pack(enum pipe_format format)
209 {
210 const struct util_format_description *desc = util_format_description(format);
211
212 return desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB;
213 }
214