1 /*
2 * Copyright (C) 2023 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 * Louis-Francis Ratté-Boulianne <[email protected]>
25 */
26
27 #include "pan_texture.h"
28
29 /* Arm Fixed-Rate Compression (AFRC) is a lossy compression scheme natively
30 * implemented in Mali GPUs. AFRC images can only be rendered or textured
31 * from. It is currently not possible to do image reads or writes to such
32 * resources.
33 *
34 * AFRC divides the image into an array of fixed-size coding units which are
35 * grouped into paging tiles. The size of the coding units (clump size)
36 * depends on the image format and the pixel layout (whether it is optimized
37 * for 2D locality and rotation, or for scan line order access). The last
38 * parameter is the size of the compressed block that can be either 16, 24,
39 * or 32 bytes.
40 *
41 * The compression rate can be calculated by dividing the compressed block
42 * size by the uncompressed block size (clump size multiplied by the component
43 * size and the number of components).
44 */
45
46 struct pan_afrc_format_info
panfrost_afrc_get_format_info(enum pipe_format format)47 panfrost_afrc_get_format_info(enum pipe_format format)
48 {
49 const struct util_format_description *desc = util_format_description(format);
50 struct pan_afrc_format_info info = {0};
51
52 /* No AFRC(ZS). */
53 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
54 return info;
55
56 unsigned bpc = 0;
57 for (unsigned c = 0; c < desc->nr_channels; c++) {
58 if (bpc && bpc != desc->channel[c].size)
59 return info;
60
61 bpc = desc->channel[0].size;
62 }
63
64 info.bpc = bpc;
65
66 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_YUV) {
67 if (desc->layout != UTIL_FORMAT_LAYOUT_SUBSAMPLED)
68 info.ichange_fmt = PAN_AFRC_ICHANGE_FORMAT_YUV444;
69 else if (util_format_is_subsampled_422(format))
70 info.ichange_fmt = PAN_AFRC_ICHANGE_FORMAT_YUV422;
71 else
72 info.ichange_fmt = PAN_AFRC_ICHANGE_FORMAT_YUV420;
73 } else {
74 assert(desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
75 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB);
76 info.ichange_fmt = PAN_AFRC_ICHANGE_FORMAT_RAW;
77 }
78
79 info.num_planes = util_format_get_num_planes(format);
80 info.num_comps = util_format_get_nr_components(format);
81 return info;
82 }
83
84 bool
panfrost_format_supports_afrc(enum pipe_format format)85 panfrost_format_supports_afrc(enum pipe_format format)
86 {
87 const struct util_format_description *desc = util_format_description(format);
88 int c = util_format_get_first_non_void_channel(desc->format);
89
90 if (c == -1)
91 return false;
92
93 return desc->is_array && desc->channel[c].size == 8;
94 }
95
96 struct panfrost_afrc_block_size {
97 unsigned size; /* Block size in bytes */
98 unsigned alignment; /* Buffer alignment */
99 uint64_t modifier_flag; /* Part of the modifier for CU size */
100 };
101
102 #define BLOCK_SIZE(block_size, buffer_alignment) \
103 { \
104 .size = block_size, .alignment = buffer_alignment, \
105 .modifier_flag = AFRC_FORMAT_MOD_CU_SIZE_##block_size, \
106 }
107
108 /* clang-format off */
109 const struct panfrost_afrc_block_size panfrost_afrc_block_sizes[] = {
110 BLOCK_SIZE(16, 1024),
111 BLOCK_SIZE(24, 512),
112 BLOCK_SIZE(32, 2048),
113 };
114 /* clang-format on */
115
116 /* Total number of components in a AFRC coding unit */
117 static unsigned
panfrost_afrc_clump_get_nr_components(enum pipe_format format,bool scan)118 panfrost_afrc_clump_get_nr_components(enum pipe_format format, bool scan)
119 {
120 const struct util_format_description *desc = util_format_description(format);
121 struct pan_block_size clump_sz = panfrost_afrc_clump_size(format, scan);
122 return clump_sz.width * clump_sz.height * desc->nr_channels;
123 }
124
125 unsigned
panfrost_afrc_query_rates(enum pipe_format format,unsigned max,uint32_t * rates)126 panfrost_afrc_query_rates(enum pipe_format format, unsigned max,
127 uint32_t *rates)
128 {
129 if (!panfrost_format_supports_afrc(format))
130 return 0;
131
132 unsigned clump_comps = panfrost_afrc_clump_get_nr_components(format, false);
133 unsigned nr_rates = 0;
134
135 /**
136 * From EGL_EXT_surface_compression:
137 *
138 * "For pixel formats with different number of bits per component, the
139 * specified fixed-rate compression rate applies to the component with
140 * the highest number of bits."
141 *
142 * We only support formats where all components have the same size for now.
143 * Let's just use the first component size for calculation.
144 */
145 unsigned uncompressed_rate =
146 util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0);
147
148 for (unsigned i = 0; i < ARRAY_SIZE(panfrost_afrc_block_sizes); ++i) {
149 unsigned clump_sz = panfrost_afrc_block_sizes[i].size * 8;
150 unsigned rate = clump_sz / clump_comps;
151
152 if (rate >= uncompressed_rate)
153 continue;
154
155 if (nr_rates < max)
156 rates[nr_rates] = rate;
157 nr_rates++;
158
159 if (max > 0 && nr_rates == max)
160 break;
161 }
162
163 return nr_rates;
164 }
165
166 unsigned
panfrost_afrc_get_modifiers(enum pipe_format format,uint32_t rate,unsigned max,uint64_t * modifiers)167 panfrost_afrc_get_modifiers(enum pipe_format format, uint32_t rate,
168 unsigned max, uint64_t *modifiers)
169 {
170 if (!panfrost_format_supports_afrc(format))
171 return 0;
172
173 /* For now, the number of components in a clump is always the same no
174 * matter the layout for all supported formats */
175 unsigned clump_comps = panfrost_afrc_clump_get_nr_components(format, false);
176 unsigned count = 0;
177
178 /* FIXME Choose a more sensitive default compression rate? */
179 if (rate == PAN_AFRC_RATE_DEFAULT) {
180 if (max > 0)
181 modifiers[0] = DRM_FORMAT_MOD_ARM_AFRC(AFRC_FORMAT_MOD_CU_SIZE_24);
182
183 if (max > 1)
184 modifiers[1] = DRM_FORMAT_MOD_ARM_AFRC(AFRC_FORMAT_MOD_CU_SIZE_24 |
185 AFRC_FORMAT_MOD_LAYOUT_SCAN);
186
187 return 2;
188 }
189
190 for (unsigned i = 0; i < ARRAY_SIZE(panfrost_afrc_block_sizes); ++i) {
191 unsigned clump_sz = panfrost_afrc_block_sizes[i].size * 8;
192 if (rate == clump_sz / clump_comps) {
193 for (unsigned scan = 0; scan < 2; ++scan) {
194 if (count < max) {
195 modifiers[count] = DRM_FORMAT_MOD_ARM_AFRC(
196 panfrost_afrc_block_sizes[i].modifier_flag |
197 (scan ? AFRC_FORMAT_MOD_LAYOUT_SCAN : 0));
198 }
199 count++;
200 }
201 }
202 }
203
204 return count;
205 }
206
207 uint32_t
panfrost_afrc_get_rate(enum pipe_format format,uint64_t modifier)208 panfrost_afrc_get_rate(enum pipe_format format, uint64_t modifier)
209 {
210 if (!drm_is_afrc(modifier) || !panfrost_format_supports_afrc(format))
211 return PAN_AFRC_RATE_NONE;
212
213 bool scan = panfrost_afrc_is_scan(modifier);
214 unsigned block_comps = panfrost_afrc_clump_get_nr_components(format, scan);
215 uint32_t block_sz = panfrost_afrc_block_size_from_modifier(modifier) * 8;
216
217 return block_sz / block_comps;
218 }
219