Debian Codegraph @

src/panfrost/lib/pan_afrc.h

raw
1 /*
2 * Copyright (C) 2023 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef __PAN_AFRC_H
7 #define __PAN_AFRC_H
8
9 #include "pan_format.h"
10 #include "pan_layout.h"
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 /* Arm Fixed-Rate Compression (AFRC) is a lossy compression scheme natively
17 * implemented in Mali GPUs. AFRC images can only be rendered or textured
18 * from. It is currently not possible to do image reads or writes to such
19 * resources.
20 *
21 * AFRC divides the image into an array of fixed-size coding units which are
22 * grouped into paging tiles. The size of the coding units (clump size)
23 * depends on the image format and the pixel layout (whether it is optimized
24 * for 2D locality and rotation, or for scan line order access). The last
25 * parameter is the size of the compressed block that can be either 16, 24,
26 * or 32 bytes.
27 *
28 * The compression rate can be calculated by dividing the compressed block
29 * size by the uncompressed block size (clump size multiplied by the component
30 * size and the number of components).
31 */
32
33 #define AFRC_CLUMPS_PER_TILE 64
34
35 enum pan_afrc_rate {
36 PAN_AFRC_RATE_NONE,
37 PAN_AFRC_RATE_1BPC,
38 PAN_AFRC_RATE_2BPC,
39 PAN_AFRC_RATE_3BPC,
40 PAN_AFRC_RATE_4BPC,
41 PAN_AFRC_RATE_5BPC,
42 PAN_AFRC_RATE_6BPC,
43 PAN_AFRC_RATE_7BPC,
44 PAN_AFRC_RATE_8BPC,
45 PAN_AFRC_RATE_9BPC,
46 PAN_AFRC_RATE_10BPC,
47 PAN_AFRC_RATE_11BPC,
48 PAN_AFRC_RATE_12BPC,
49 PAN_AFRC_RATE_DEFAULT = 0xF
50 };
51
52 enum pan_afrc_interchange_format {
53 PAN_AFRC_ICHANGE_FORMAT_RAW,
54 PAN_AFRC_ICHANGE_FORMAT_YUV444,
55 PAN_AFRC_ICHANGE_FORMAT_YUV422,
56 PAN_AFRC_ICHANGE_FORMAT_YUV420,
57 };
58
59 struct pan_afrc_format_info {
60 unsigned bpc : 4;
61 unsigned num_comps : 3;
62 unsigned ichange_fmt : 2;
63 unsigned num_planes : 2;
64 };
65
66 /*
67 * Given an AFRC modifier, return whether the layout is optimized for scan
68 * order (vs rotation order).
69 */
70 static inline bool
71 pan_afrc_is_scan(uint64_t modifier)
72 {
73 return modifier & AFRC_FORMAT_MOD_LAYOUT_SCAN;
74 }
75
76 static inline struct pan_afrc_format_info
77 pan_afrc_get_format_info(enum pipe_format format)
78 {
79 const struct util_format_description *desc = util_format_description(format);
80 struct pan_afrc_format_info info = {0};
81
82 /* No AFRC(compressed) */
83 if (util_format_is_compressed(format))
84 return info;
85
86 /* No AFRC(ZS). */
87 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
88 return info;
89
90 /* No AFRC(YUV) yet. */
91 if (pan_format_is_yuv(format))
92 return info;
93
94 unsigned bpc = 0;
95 for (unsigned c = 0; c < desc->nr_channels; c++) {
96 if (bpc && bpc != desc->channel[c].size)
97 return info;
98
99 bpc = desc->channel[0].size;
100 }
101
102 info.bpc = bpc;
103
104 assert(desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
105 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB);
106 info.ichange_fmt = PAN_AFRC_ICHANGE_FORMAT_RAW;
107 info.num_planes = util_format_get_num_planes(format);
108 info.num_comps = util_format_get_nr_components(format);
109 return info;
110 }
111
112 struct pan_afrc_block_size {
113 unsigned size; /* Block size in bytes */
114 unsigned alignment; /* Buffer alignment */
115 uint64_t modifier_flag; /* Part of the modifier for CU size */
116 };
117
118 #define BLOCK_SIZE(block_size, buffer_alignment) \
119 { \
120 .size = block_size, \
121 .alignment = buffer_alignment, \
122 .modifier_flag = AFRC_FORMAT_MOD_CU_SIZE_##block_size, \
123 }
124
125 #define AFRC_BLOCK_SIZES(__name) \
126 static const struct pan_afrc_block_size __name[] = { \
127 BLOCK_SIZE(16, 1024), \
128 BLOCK_SIZE(24, 512), \
129 BLOCK_SIZE(32, 2048), \
130 }
131
132 static inline struct pan_image_block_size
133 pan_afrc_clump_size(enum pipe_format format, bool scan)
134 {
135 struct pan_afrc_format_info finfo = pan_afrc_get_format_info(format);
136
137 switch (finfo.num_comps) {
138 case 1:
139 return scan ? (struct pan_image_block_size){16, 4}
140 : (struct pan_image_block_size){8, 8};
141 case 2:
142 return (struct pan_image_block_size){8, 4};
143 case 3:
144 case 4:
145 return (struct pan_image_block_size){4, 4};
146 default:
147 assert(0);
148 return (struct pan_image_block_size){0, 0};
149 }
150 }
151
152 /* Total number of components in a AFRC coding unit */
153 static inline unsigned
154 pan_afrc_clump_get_nr_components(enum pipe_format format, bool scan)
155 {
156 const struct util_format_description *desc = util_format_description(format);
157 struct pan_image_block_size clump_sz = pan_afrc_clump_size(format, scan);
158 return clump_sz.width * clump_sz.height * desc->nr_channels;
159 }
160
161 static inline bool
162 pan_afrc_supports_format(enum pipe_format format)
163 {
164 struct pan_afrc_format_info finfo = pan_afrc_get_format_info(format);
165
166 return finfo.num_comps != 0;
167 }
168
169 static inline unsigned
170 pan_afrc_query_rates(enum pipe_format format, unsigned max, uint32_t *rates)
171 {
172 if (!pan_afrc_supports_format(format))
173 return 0;
174
175 unsigned clump_comps = pan_afrc_clump_get_nr_components(format, false);
176 unsigned nr_rates = 0;
177
178 /**
179 * From EGL_EXT_surface_compression:
180 *
181 * "For pixel formats with different number of bits per component, the
182 * specified fixed-rate compression rate applies to the component with
183 * the highest number of bits."
184 *
185 * We only support formats where all components have the same size for now.
186 * Let's just use the first component size for calculation.
187 */
188 unsigned uncompressed_rate =
189 util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0);
190
191 AFRC_BLOCK_SIZES(block_sizes);
192
193 for (unsigned i = 0; i < ARRAY_SIZE(block_sizes); ++i) {
194 unsigned clump_sz = block_sizes[i].size * 8;
195 unsigned rate = clump_sz / clump_comps;
196
197 if (rate >= uncompressed_rate)
198 continue;
199
200 if (nr_rates < max)
201 rates[nr_rates] = rate;
202 nr_rates++;
203
204 if (max > 0 && nr_rates == max)
205 break;
206 }
207
208 return nr_rates;
209 }
210
211 static inline unsigned
212 pan_afrc_get_modifiers(enum pipe_format format, uint32_t rate, unsigned max,
213 uint64_t *modifiers)
214 {
215 if (!pan_afrc_supports_format(format))
216 return 0;
217
218 /* For now, the number of components in a clump is always the same no
219 * matter the layout for all supported formats */
220 unsigned clump_comps = pan_afrc_clump_get_nr_components(format, false);
221 unsigned count = 0;
222
223 /* FIXME Choose a more sensitive default compression rate? */
224 if (rate == PAN_AFRC_RATE_DEFAULT) {
225 if (max > 0)
226 modifiers[0] = DRM_FORMAT_MOD_ARM_AFRC(AFRC_FORMAT_MOD_CU_SIZE_24);
227
228 if (max > 1)
229 modifiers[1] = DRM_FORMAT_MOD_ARM_AFRC(AFRC_FORMAT_MOD_CU_SIZE_24 |
230 AFRC_FORMAT_MOD_LAYOUT_SCAN);
231
232 return 2;
233 }
234
235 AFRC_BLOCK_SIZES(block_sizes);
236 for (unsigned i = 0; i < ARRAY_SIZE(block_sizes); ++i) {
237 unsigned clump_sz = block_sizes[i].size * 8;
238 if (rate == clump_sz / clump_comps) {
239 for (unsigned scan = 0; scan < 2; ++scan) {
240 if (count < max) {
241 modifiers[count] = DRM_FORMAT_MOD_ARM_AFRC(
242 block_sizes[i].modifier_flag |
243 (scan ? AFRC_FORMAT_MOD_LAYOUT_SCAN : 0));
244 }
245 count++;
246 }
247 }
248 }
249
250 return count;
251 }
252
253 static inline unsigned
254 pan_afrc_block_size_from_modifier(uint64_t modifier)
255 {
256 switch (modifier & AFRC_FORMAT_MOD_CU_SIZE_MASK) {
257 case AFRC_FORMAT_MOD_CU_SIZE_16:
258 return 16;
259 case AFRC_FORMAT_MOD_CU_SIZE_24:
260 return 24;
261 case AFRC_FORMAT_MOD_CU_SIZE_32:
262 return 32;
263 default:
264 UNREACHABLE("invalid coding unit size flag in modifier");
265 };
266 }
267
268 static inline uint32_t
269 pan_afrc_get_rate(enum pipe_format format, uint64_t modifier)
270 {
271 if (!drm_is_afrc(modifier) || !pan_afrc_supports_format(format))
272 return PAN_AFRC_RATE_NONE;
273
274 bool scan = pan_afrc_is_scan(modifier);
275 unsigned block_comps = pan_afrc_clump_get_nr_components(format, scan);
276 uint32_t block_sz = pan_afrc_block_size_from_modifier(modifier) * 8;
277
278 return block_sz / block_comps;
279 }
280
281 static inline struct pan_image_block_size
282 pan_afrc_layout_size(uint64_t modifier)
283 {
284 if (pan_afrc_is_scan(modifier))
285 return (struct pan_image_block_size){16, 4};
286 else
287 return (struct pan_image_block_size){8, 8};
288 }
289
290 static inline struct pan_image_block_size
291 pan_afrc_tile_size(enum pipe_format format, uint64_t modifier)
292 {
293 bool scan = pan_afrc_is_scan(modifier);
294 struct pan_image_block_size clump_sz = pan_afrc_clump_size(format, scan);
295 struct pan_image_block_size layout_sz = pan_afrc_layout_size(modifier);
296
297 return (struct pan_image_block_size){clump_sz.width * layout_sz.width,
298 clump_sz.height * layout_sz.height};
299 }
300
301 static inline unsigned
302 pan_afrc_buffer_alignment_from_modifier(uint64_t modifier)
303 {
304 switch (modifier & AFRC_FORMAT_MOD_CU_SIZE_MASK) {
305 case AFRC_FORMAT_MOD_CU_SIZE_16:
306 return 1024;
307 case AFRC_FORMAT_MOD_CU_SIZE_24:
308 return 512;
309 case AFRC_FORMAT_MOD_CU_SIZE_32:
310 return 2048;
311 default:
312 UNREACHABLE("invalid coding unit size flag in modifier");
313 };
314 }
315
316 /*
317 * Determine the number of bytes between rows of paging tiles in an AFRC image
318 */
319 static inline uint32_t
320 pan_afrc_row_stride(enum pipe_format format, uint64_t modifier, uint32_t width)
321 {
322 struct pan_image_block_size tile_size = pan_afrc_tile_size(format, modifier);
323 unsigned block_size = pan_afrc_block_size_from_modifier(modifier);
324
325 return (width / tile_size.width) * block_size * AFRC_CLUMPS_PER_TILE;
326 }
327
328 #if PAN_ARCH >= 10
329 static inline enum mali_afrc_format
330 pan_afrc_format(struct pan_afrc_format_info info, uint64_t modifier,
331 unsigned plane)
332 {
333 bool scan = pan_afrc_is_scan(modifier);
334
335 assert(info.bpc == 8 || info.bpc == 10);
336 assert(info.num_comps > 0 && info.num_comps <= 4);
337
338 switch (info.ichange_fmt) {
339 case PAN_AFRC_ICHANGE_FORMAT_RAW:
340 assert(plane == 0);
341
342 if (info.bpc == 8)
343 return (scan ? MALI_AFRC_FORMAT_R8_SCAN : MALI_AFRC_FORMAT_R8_ROT) +
344 (info.num_comps - 1);
345
346 assert(info.num_comps == 4);
347 return (scan ? MALI_AFRC_FORMAT_R10G10B10A10_SCAN
348 : MALI_AFRC_FORMAT_R10G10B10A10_ROT);
349
350 case PAN_AFRC_ICHANGE_FORMAT_YUV444:
351 if (info.bpc == 8) {
352 if (plane == 0 || info.num_planes == 3)
353 return (scan ? MALI_AFRC_FORMAT_R8_444_SCAN
354 : MALI_AFRC_FORMAT_R8_444_ROT);
355
356 return (scan ? MALI_AFRC_FORMAT_R8G8_444_SCAN
357 : MALI_AFRC_FORMAT_R8G8_444_ROT);
358 }
359
360 assert(info.num_planes == 3);
361 return (scan ? MALI_AFRC_FORMAT_R10_444_SCAN
362 : MALI_AFRC_FORMAT_R10_444_ROT);
363
364 case PAN_AFRC_ICHANGE_FORMAT_YUV422:
365 if (info.bpc == 8) {
366 if (plane == 0 || info.num_planes == 3)
367 return (scan ? MALI_AFRC_FORMAT_R8_422_SCAN
368 : MALI_AFRC_FORMAT_R8_422_ROT);
369
370 return (scan ? MALI_AFRC_FORMAT_R8G8_422_SCAN
371 : MALI_AFRC_FORMAT_R8G8_422_ROT);
372 }
373
374 if (plane == 0 || info.num_planes == 3)
375 return (scan ? MALI_AFRC_FORMAT_R10_422_SCAN
376 : MALI_AFRC_FORMAT_R10_422_ROT);
377
378 return (scan ? MALI_AFRC_FORMAT_R10G10_422_SCAN
379 : MALI_AFRC_FORMAT_R10G10_422_ROT);
380
381 case PAN_AFRC_ICHANGE_FORMAT_YUV420:
382 if (info.bpc == 8) {
383 if (plane == 0 || info.num_planes == 3)
384 return (scan ? MALI_AFRC_FORMAT_R8_420_SCAN
385 : MALI_AFRC_FORMAT_R8_420_ROT);
386
387 return (scan ? MALI_AFRC_FORMAT_R8G8_420_SCAN
388 : MALI_AFRC_FORMAT_R8G8_420_ROT);
389 }
390
391 if (plane == 0 || info.num_planes == 3)
392 return (scan ? MALI_AFRC_FORMAT_R10_420_SCAN
393 : MALI_AFRC_FORMAT_R10_420_ROT);
394
395 return (scan ? MALI_AFRC_FORMAT_R10G10_420_SCAN
396 : MALI_AFRC_FORMAT_R10G10_420_ROT);
397
398 default:
399 return MALI_AFRC_FORMAT_INVALID;
400 }
401 }
402
403 static inline enum mali_afrc_block_size
404 pan_afrc_block_size(uint64_t modifier, unsigned index)
405 {
406 /* Clump size flag for planes 1 and 2 is shifted by 4 bits */
407 unsigned shift = index == 0 ? 0 : 4;
408 uint64_t flag = (modifier >> shift) & AFRC_FORMAT_MOD_CU_SIZE_MASK;
409
410 switch (flag) {
411 case AFRC_FORMAT_MOD_CU_SIZE_16:
412 return MALI_AFRC_BLOCK_SIZE_16;
413 case AFRC_FORMAT_MOD_CU_SIZE_24:
414 return MALI_AFRC_BLOCK_SIZE_24;
415 case AFRC_FORMAT_MOD_CU_SIZE_32:
416 return MALI_AFRC_BLOCK_SIZE_32;
417 default:
418 UNREACHABLE("invalid code unit size");
419 }
420 }
421 #endif
422
423 #ifdef __cplusplus
424 } /* extern C */
425 #endif
426
427 #endif