Debian Codegraph @

src/panfrost/lib/pan_afbc.h

raw
1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019-2020 Collabora, Ltd.
6 * SPDX-License-Identifier: MIT
7 */
8
9 #ifndef __PAN_AFBC_H
10 #define __PAN_AFBC_H
11
12 #include "pan_format.h"
13 #include "pan_layout.h"
14
15 #include "drm-uapi/drm_fourcc.h"
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 /* Arm FrameBuffer Compression (AFBC) is a lossless compression scheme natively
22 * implemented in Mali GPUs (as well as many display controllers paired with
23 * Mali GPUs, etc). Where possible, Panfrost prefers to use AFBC for both
24 * rendering and texturing. In most cases, this is a performance-win due to a
25 * dramatic reduction in memory bandwidth and cache locality compared to a
26 * linear resources.
27 *
28 * AFBC divides the framebuffer into 16x16 tiles (other sizes possible, TODO:
29 * do we need to support this?). So, the width and height each must be aligned
30 * up to 16 pixels. This is inherently good for performance; note that for a 4
31 * byte-per-pixel format like RGBA8888, that means that rows are 16*4=64 byte
32 * aligned, which is the cache-line size.
33 *
34 * For each AFBC-compressed resource, there is a single contiguous
35 * (CPU/GPU-shared) buffer. This buffer itself is divided into two parts:
36 * header and body, placed immediately after each other.
37 *
38 * The AFBC header contains 16 bytes of metadata per tile.
39 *
40 * The AFBC body is the same size as the original linear resource (padded to
41 * the nearest tile). Although the body comes immediately after the header, it
42 * must also be cache-line aligned, so there can sometimes be a bit of padding
43 * between the header and body.
44 *
45 * As an example, a 64x64 RGBA framebuffer contains 64/16 = 4 tiles horizontally
46 * and 4 tiles vertically. There are 4*4=16 tiles in total, each containing 16
47 * bytes of metadata, so there is a 16*16=256 byte header. 64x64 is already
48 * tile aligned, so the body is 64*64 * 4 bytes per pixel = 16384 bytes of
49 * body.
50 *
51 * From userspace, Panfrost needs to be able to calculate these sizes. It
52 * explicitly does not and can not know the format of the data contained within
53 * this header and body. The GPU has native support for AFBC encode/decode. For
54 * an internal FBO or a framebuffer used for scanout with an AFBC-compatible
55 * winsys/display-controller, the buffer is maintained AFBC throughout flight,
56 * and the driver never needs to know the internal data. For edge cases where
57 * the driver really does need to read/write from the AFBC resource, we
58 * generate a linear staging buffer and use the GPU to blit AFBC<--->linear.
59 */
60
61 #define AFBC_HEADER_BYTES_PER_TILE 16
62
63 /* AFBC format mode. The ordering is intended to match the Valhall hardware enum
64 * ("AFBC Compression Mode"), but this enum is required in software on older
65 * hardware for correct handling of texture views. Defining the enum lets us
66 * unify these code paths.
67 */
68 enum pan_afbc_mode {
69 PAN_AFBC_MODE_R8,
70 PAN_AFBC_MODE_R8G8,
71 PAN_AFBC_MODE_R5G6B5,
72 PAN_AFBC_MODE_R4G4B4A4,
73 PAN_AFBC_MODE_R5G5B5A1,
74 PAN_AFBC_MODE_R8G8B8,
75 PAN_AFBC_MODE_R8G8B8A8,
76 PAN_AFBC_MODE_R10G10B10A2,
77 PAN_AFBC_MODE_R11G11B10,
78
79 /* 16 bit modes, available on v10+ */
80 PAN_AFBC_MODE_R16,
81 PAN_AFBC_MODE_R16G16,
82 PAN_AFBC_MODE_R16G16B16,
83 PAN_AFBC_MODE_R16G16B16A16,
84
85 /* YUV special modes */
86 PAN_AFBC_MODE_YUV420_6C8,
87 PAN_AFBC_MODE_YUV420_2C8,
88 PAN_AFBC_MODE_YUV420_1C8,
89 PAN_AFBC_MODE_YUV420_6C10,
90 PAN_AFBC_MODE_YUV420_2C10,
91 PAN_AFBC_MODE_YUV420_1C10,
92
93 PAN_AFBC_MODE_YUV422_4C8,
94 PAN_AFBC_MODE_YUV422_2C8,
95 PAN_AFBC_MODE_YUV422_1C8,
96 PAN_AFBC_MODE_YUV422_4C10,
97 PAN_AFBC_MODE_YUV422_2C10,
98 PAN_AFBC_MODE_YUV422_1C10,
99
100 /* Sentintel signalling a format that cannot be compressed */
101 PAN_AFBC_MODE_INVALID
102 };
103
104 /*
105 * An AFBC header block provides access to an associated superblock payload of
106 * 4x4 subblocks or to an embedded solid color.
107 */
108 struct pan_afbc_headerblock {
109 union {
110 /* Superblock payload. */
111 struct {
112 /* Offset in bytes from the start of the AFBC buffer (1st header
113 * block) to the start of the superblock payload data. */
114 uint32_t offset;
115
116 /* Sizes in bytes of the 4x4 6-bit subblocks. */
117 uint8_t subblock_sizes[12];
118 } payload;
119
120 /* Solid color. */
121 struct {
122 uint64_t reserved;
123
124 /* RGBA 8-8-8-8 color format. */
125 /* XXX: Add other formats. */
126 struct {
127 uint8_t r, g, b, a;
128 uint32_t reserved;
129 } rgba8888;
130 } color;
131
132 /* Random access. */
133 uint8_t u8[16];
134 uint16_t u16[8];
135 uint32_t u32[4];
136 uint64_t u64[2];
137 };
138 };
139
140 /*
141 * An AFBC payload extent describes the extent of the payload data (compressed
142 * superblock data) associated to a pan_afbc_headerblock.
143 */
144 struct pan_afbc_payload_extent {
145 uint32_t size;
146 uint32_t offset;
147 };
148
149 /*
150 * Given an AFBC modifier, return the superblock size.
151 *
152 * We do not yet have any use cases for multiplanar YCBCr formats with different
153 * superblock sizes on the luma and chroma planes. These formats are unsupported
154 * for now.
155 */
156 static inline struct pan_image_block_size
157 pan_afbc_superblock_size(uint64_t modifier)
158 {
159 unsigned index = (modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
160
161 assert(drm_is_afbc(modifier));
162
163 switch (index) {
164 case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
165 return (struct pan_image_block_size){16, 16};
166 case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
167 return (struct pan_image_block_size){32, 8};
168 case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
169 return (struct pan_image_block_size){64, 4};
170 default:
171 assert(!"Unsupported AFBC block size");
172 return (struct pan_image_block_size){0, 0};
173 }
174 }
175
176 /* Same as pan_afbc_superblock_size_el() but counted in block elements
177 * instead of pixels. For anything non-YUV this is the same. */
178 static inline struct pan_image_block_size
179 pan_afbc_superblock_size_el(enum pipe_format format, uint64_t modifier)
180 {
181 struct pan_image_block_size sb_size_px = pan_afbc_superblock_size(modifier);
182
183 assert(sb_size_px.width % util_format_get_blockwidth(format) == 0);
184 assert(sb_size_px.height % util_format_get_blockheight(format) == 0);
185
186 return (struct pan_image_block_size){
187 .width = sb_size_px.width / util_format_get_blockwidth(format),
188 .height = sb_size_px.height / util_format_get_blockheight(format),
189 };
190 }
191
192 /*
193 * Given an AFBC modifier, return the render size.
194 */
195 static inline struct pan_image_block_size
196 pan_afbc_renderblock_size(uint64_t modifier)
197 {
198 struct pan_image_block_size blk_size = pan_afbc_superblock_size(modifier);
199
200 /* The GPU needs to render 16x16 tiles. For wide tiles, that means we
201 * have to extend the render region to have a height of 16 pixels.
202 */
203 blk_size.height = ALIGN_POT(blk_size.height, 16);
204 return blk_size;
205 }
206
207
208 /* Same as pan_afbc_renderblock_size() but counted in block elements
209 * instead of pixels. For anything non-YUV this is the same. */
210 static inline struct pan_image_block_size
211 pan_afbc_renderblock_size_el(enum pipe_format format, uint64_t modifier)
212 {
213 struct pan_image_block_size rb_size_px = pan_afbc_renderblock_size(modifier);
214
215 assert(rb_size_px.width % util_format_get_blockwidth(format) == 0);
216 assert(rb_size_px.height % util_format_get_blockheight(format) == 0);
217
218 return (struct pan_image_block_size){
219 .width = rb_size_px.width / util_format_get_blockwidth(format),
220 .height = rb_size_px.height / util_format_get_blockheight(format),
221 };
222 }
223
224 /*
225 * Given an AFBC modifier, return the width of the superblock.
226 */
227 static inline unsigned
228 pan_afbc_superblock_width(uint64_t modifier)
229 {
230 return pan_afbc_superblock_size(modifier).width;
231 }
232
233 /*
234 * Given an AFBC modifier, return the height of the superblock.
235 */
236 static inline unsigned
237 pan_afbc_superblock_height(uint64_t modifier)
238 {
239 return pan_afbc_superblock_size(modifier).height;
240 }
241
242 /*
243 * Given an AFBC modifier, return if "wide blocks" are used. Wide blocks are
244 * defined as superblocks wider than 16 pixels, the minimum (and default) super
245 * block width.
246 */
247 static inline bool
248 pan_afbc_is_wide(uint64_t modifier)
249 {
250 return pan_afbc_superblock_width(modifier) > 16;
251 }
252
253 /*
254 * Given an AFBC modifier, return the subblock size (subdivision of a
255 * superblock). This is always 4x4 for now as we only support one AFBC
256 * superblock layout.
257 */
258 static inline struct pan_image_block_size
259 pan_afbc_subblock_size(uint64_t modifier)
260 {
261 return (struct pan_image_block_size){4, 4};
262 }
263
264 /*
265 * Given an AFBC header block, return the size of the subblock at the given
266 * index in the range [0, 15].
267 */
268 static inline unsigned
269 pan_afbc_header_subblock_size(struct pan_afbc_headerblock header,
270 uint32_t index)
271 {
272 uint64_t mask = BITFIELD_MASK(6);
273
274 switch (index) {
275 case 0: return (header.u64[0] >> 32) & mask; break;
276 case 1: return (header.u64[0] >> 38) & mask; break;
277 case 2: return (header.u64[0] >> 44) & mask; break;
278 case 3: return (header.u64[0] >> 50) & mask; break;
279 case 4: return (header.u64[0] >> 56) & mask; break;
280 case 5: return ((header.u64[0] >> 62) |
281 (header.u64[1] << 2)) & mask; break;
282 case 6: return (header.u64[1] >> 4) & mask; break;
283 case 7: return (header.u64[1] >> 10) & mask; break;
284 case 8: return (header.u64[1] >> 16) & mask; break;
285 case 9: return (header.u64[1] >> 22) & mask; break;
286 case 10: return (header.u64[1] >> 28) & mask; break;
287 case 11: return (header.u64[1] >> 34) & mask; break;
288 case 12: return (header.u64[1] >> 40) & mask; break;
289 case 13: return (header.u64[1] >> 46) & mask; break;
290 case 14: return (header.u64[1] >> 52) & mask; break;
291 case 15: return (header.u64[1] >> 58) & mask; break;
292 default: UNREACHABLE("invalid index"); return 0;
293 }
294 }
295
296 /*
297 * Given an AFBC header block, return the size in bytes of the associated
298 * superblock payload data (for the superblock layouts 0, 3, 4 and 7).
299 */
300 static inline uint32_t
301 pan_afbc_payload_size(unsigned arch,
302 struct pan_afbc_headerblock header,
303 uint32_t uncompressed_size)
304 {
305 /* Skip sum if the 1st subblock is 0 (solid color encoding). */
306 if (arch >= 7 && pan_afbc_header_subblock_size(header, 0) == 0)
307 return 0;
308
309 uint64_t size = 0;
310
311 for (unsigned i = 0; i < 16; i++) {
312 unsigned sub_size = pan_afbc_header_subblock_size(header, i);
313 size += sub_size != 1 ? sub_size : uncompressed_size;
314 }
315
316 return ALIGN_POT(size, 16);
317 }
318
319 /*
320 * Given a format and a modifier, return the size in bytes of an uncompressed
321 * superblock payload.
322 */
323 static inline uint32_t
324 pan_afbc_payload_uncompressed_size(enum pipe_format format, uint64_t modifier)
325 {
326 struct pan_image_block_size size_px = pan_afbc_subblock_size(modifier);
327 uint32_t size_B = util_format_get_blocksizebits(format) / 8;
328 size_B *= size_px.width * size_px.height;
329
330 assert(size_B == ALIGN_POT(size_B, 16));
331
332 return size_B;
333 }
334
335 /*
336 * Calculate the size of each AFBC superblock payload data from the given
337 * header blocks, generate a packed AFBC payload layout and return the body
338 * size.
339 */
340 uint32_t
341 pan_afbc_payload_layout_packed(unsigned arch,
342 const struct pan_afbc_headerblock *headers,
343 struct pan_afbc_payload_extent *layout,
344 uint32_t nr_blocks, enum pipe_format format,
345 uint64_t modifier);
346
347 static inline uint32_t
348 pan_afbc_header_row_stride_align(unsigned arch, enum pipe_format format,
349 uint64_t modifier)
350 {
351 if (arch <= 7 || !(modifier & AFBC_FORMAT_MOD_TILED))
352 return 16;
353
354 if (util_format_get_blocksizebits(format) <= 32)
355 return 1024;
356 else
357 return 256;
358 }
359
360 static inline uint32_t
361 pan_afbc_header_align(unsigned arch, uint64_t modifier)
362 {
363 if (modifier & AFBC_FORMAT_MOD_TILED)
364 return 4096;
365 else if (arch >= 6)
366 return 128;
367 else
368 return 64;
369 }
370
371 /*
372 * Determine the required alignment for the body offset of an AFBC image. For
373 * now, this depends only on whether tiling is in use. These minimum alignments
374 * are required on all current GPUs.
375 */
376 static inline uint32_t
377 pan_afbc_body_align(unsigned arch, uint64_t modifier)
378 {
379 /* Body and header alignments are actually the same. */
380 return pan_afbc_header_align(arch, modifier);
381 }
382
383 /* Get the body offset for a given AFBC header size. */
384 static inline uint32_t
385 pan_afbc_body_offset(unsigned arch, uint64_t modifier, uint32_t header_size)
386 {
387 return ALIGN_POT(header_size, pan_afbc_body_align(arch, modifier));
388 }
389
390 /*
391 * Determine the tile size used by AFBC. This tiles superblocks themselves.
392 * Current GPUs support either 8x8 tiling or no tiling (1x1) for most
393 * formats; but note that formats with more than 32bpp use 4x4 tiling
394 */
395 static inline unsigned
396 pan_afbc_tile_size(enum pipe_format format, uint64_t modifier)
397 {
398 if ( !(modifier & AFBC_FORMAT_MOD_TILED) ) {
399 return 1;
400 }
401 return util_format_get_blocksizebits(format) <= 32 ? 8 : 4;
402 }
403
404 /*
405 * Determine the number of bytes between header rows for an AFBC image. For an
406 * image with linear headers, this is simply the number of header blocks
407 * (=superblocks) per row times the numbers of bytes per header block. For an
408 * image with tiled headers, this is multipled by the number of rows of
409 * header blocks are in a tile together.
410 */
411 static inline uint32_t
412 pan_afbc_row_stride(enum pipe_format format, uint64_t modifier, uint32_t width)
413 {
414 unsigned block_width = pan_afbc_superblock_width(modifier);
415
416 return (width / block_width) * pan_afbc_tile_size(format, modifier) *
417 AFBC_HEADER_BYTES_PER_TILE;
418 }
419
420 /*
421 * Determine the number of header blocks between header rows. This is equal to
422 * the number of bytes between header rows divided by the bytes per blocks of a
423 * header tile. This is also divided by the tile size to give a "line stride" in
424 * blocks, rather than a real row stride. This is required by Bifrost.
425 */
426 static inline uint32_t
427 pan_afbc_stride_blocks(enum pipe_format format, uint64_t modifier, uint32_t row_stride_bytes)
428 {
429 return row_stride_bytes /
430 (AFBC_HEADER_BYTES_PER_TILE * pan_afbc_tile_size(format, modifier));
431 }
432
433 /* Returns a height in superblocks taking into account the tile alignment
434 * requirement coming from the modifier.
435 */
436 static inline uint32_t
437 pan_afbc_height_blocks(enum pipe_format format, uint64_t modifier, uint32_t height_px)
438 {
439 return ALIGN_POT(
440 DIV_ROUND_UP(height_px, pan_afbc_superblock_height(modifier)),
441 pan_afbc_tile_size(format, modifier));
442 }
443
444 static inline enum pipe_format
445 pan_afbc_unswizzled_format(unsigned arch, enum pipe_format format)
446 {
447 switch (format) {
448 case PIPE_FORMAT_A8_UNORM:
449 case PIPE_FORMAT_L8_UNORM:
450 case PIPE_FORMAT_I8_UNORM:
451 return PIPE_FORMAT_R8_UNORM;
452
453 case PIPE_FORMAT_L8A8_UNORM:
454 return PIPE_FORMAT_R8G8_UNORM;
455
456 case PIPE_FORMAT_B8G8R8_UNORM:
457 return PIPE_FORMAT_R8G8B8_UNORM;
458
459 case PIPE_FORMAT_R8G8B8X8_UNORM:
460 case PIPE_FORMAT_B8G8R8A8_UNORM:
461 case PIPE_FORMAT_B8G8R8X8_UNORM:
462 return PIPE_FORMAT_R8G8B8A8_UNORM;
463 case PIPE_FORMAT_A8R8G8B8_UNORM:
464 case PIPE_FORMAT_X8R8G8B8_UNORM:
465 case PIPE_FORMAT_X8B8G8R8_UNORM:
466 case PIPE_FORMAT_A8B8G8R8_UNORM:
467 /* v7 can only support AFBC for RGB and BGR */
468 return arch == 7 ? format : PIPE_FORMAT_R8G8B8A8_UNORM;
469
470 case PIPE_FORMAT_B5G6R5_UNORM:
471 return PIPE_FORMAT_R5G6B5_UNORM;
472
473 case PIPE_FORMAT_B5G5R5A1_UNORM:
474 return PIPE_FORMAT_R5G5B5A1_UNORM;
475
476 case PIPE_FORMAT_R10G10B10X2_UNORM:
477 case PIPE_FORMAT_B10G10R10A2_UNORM:
478 case PIPE_FORMAT_B10G10R10X2_UNORM:
479 return PIPE_FORMAT_R10G10B10A2_UNORM;
480
481 case PIPE_FORMAT_B4G4R4A4_UNORM:
482 return PIPE_FORMAT_R4G4B4A4_UNORM;
483 case PIPE_FORMAT_A4B4G4R4_UNORM:
484 /* v7 can only support AFBC for RGB and BGR */
485 return arch == 7 ? format : PIPE_FORMAT_R4G4B4A4_UNORM;
486
487 default:
488 return format;
489 }
490 }
491
492 /* AFBC supports compressing a few canonical formats. Additional formats are
493 * available by using a canonical internal format. Given a PIPE format, find
494 * the canonical AFBC internal format if it exists, or NONE if the format
495 * cannot be compressed. */
496
497 static inline enum pan_afbc_mode
498 pan_afbc_format(unsigned arch, enum pipe_format format, unsigned plane_idx)
499 {
500 assert(plane_idx < util_format_get_num_planes(format));
501
502 switch (format) {
503 case PIPE_FORMAT_R8_G8B8_420_UNORM:
504 case PIPE_FORMAT_R8_B8G8_420_UNORM:
505 return plane_idx == 0 ? PAN_AFBC_MODE_YUV420_1C8
506 : PAN_AFBC_MODE_YUV420_2C8;
507 case PIPE_FORMAT_R8_G8_B8_420_UNORM:
508 case PIPE_FORMAT_R8_B8_G8_420_UNORM:
509 return PAN_AFBC_MODE_YUV420_1C8;
510 case PIPE_FORMAT_R8_G8B8_422_UNORM:
511 return plane_idx == 0 ? PAN_AFBC_MODE_YUV422_1C8
512 : PAN_AFBC_MODE_YUV422_2C8;
513 case PIPE_FORMAT_R10_G10B10_420_UNORM:
514 return plane_idx == 0 ? PAN_AFBC_MODE_YUV420_1C10
515 : PAN_AFBC_MODE_YUV420_2C10;
516 case PIPE_FORMAT_R10_G10B10_422_UNORM:
517 return plane_idx == 0 ? PAN_AFBC_MODE_YUV422_1C10
518 : PAN_AFBC_MODE_YUV422_2C10;
519 case PIPE_FORMAT_R8G8B8_420_UNORM_PACKED:
520 return PAN_AFBC_MODE_YUV420_6C8;
521 case PIPE_FORMAT_R10G10B10_420_UNORM_PACKED:
522 return PAN_AFBC_MODE_YUV420_6C10;
523 case PIPE_FORMAT_R8G8_R8B8_UNORM:
524 case PIPE_FORMAT_R8B8_R8G8_UNORM:
525 return PAN_AFBC_MODE_YUV422_4C8;
526 case PIPE_FORMAT_X6R10X6G10_X6R10X6B10_422_UNORM:
527 return PAN_AFBC_MODE_YUV422_4C10;
528 default:
529 break;
530 }
531
532 /* sRGB does not change the pixel format itself, only the
533 * interpretation. The interpretation is handled by conversion hardware
534 * independent to the compression hardware, so we can compress sRGB
535 * formats by using the corresponding linear format.
536 */
537 format = util_format_linear(format);
538
539 /* AFBC is type agnostic on v9+, so the following logic is done on UNORM
540 * for simplicity, so make sure we convert to _UNORM first.
541 */
542 if (arch >= 9)
543 format = util_format_any_to_unorm(format);
544
545 /* Luminance-alpha not supported for AFBC on v7+ */
546 /* 16 bit modes only available for AFBC on v10 and later */
547 switch (format) {
548 case PIPE_FORMAT_A8_UNORM:
549 case PIPE_FORMAT_L8_UNORM:
550 case PIPE_FORMAT_I8_UNORM:
551 case PIPE_FORMAT_L8A8_UNORM:
552 if (arch >= 7)
553 return PAN_AFBC_MODE_INVALID;
554 else
555 break;
556 case PIPE_FORMAT_R16_UNORM:
557 case PIPE_FORMAT_R16G16_UNORM:
558 case PIPE_FORMAT_R16G16B16_UNORM:
559 case PIPE_FORMAT_R16G16B16A16_UNORM:
560 if (arch < 10)
561 return PAN_AFBC_MODE_INVALID;
562 else
563 break;
564 case PIPE_FORMAT_S8_UINT:
565 case PIPE_FORMAT_R11G11B10_FLOAT:
566 if (arch < 9)
567 return PAN_AFBC_MODE_INVALID;
568 else
569 break;
570 default:
571 break;
572 }
573
574 /* We handle swizzling orthogonally to AFBC */
575 format = pan_afbc_unswizzled_format(arch, format);
576
577 /* clang-format off */
578 switch (format) {
579 case PIPE_FORMAT_R8_UNORM: return PAN_AFBC_MODE_R8;
580 case PIPE_FORMAT_R8G8_UNORM: return PAN_AFBC_MODE_R8G8;
581 case PIPE_FORMAT_R8G8B8_UNORM: return PAN_AFBC_MODE_R8G8B8;
582 case PIPE_FORMAT_R8G8B8A8_UNORM: return PAN_AFBC_MODE_R8G8B8A8;
583 case PIPE_FORMAT_R5G6B5_UNORM: return PAN_AFBC_MODE_R5G6B5;
584 case PIPE_FORMAT_R5G5B5A1_UNORM: return PAN_AFBC_MODE_R5G5B5A1;
585 case PIPE_FORMAT_R10G10B10A2_UNORM: return PAN_AFBC_MODE_R10G10B10A2;
586 case PIPE_FORMAT_R4G4B4A4_UNORM: return PAN_AFBC_MODE_R4G4B4A4;
587 case PIPE_FORMAT_Z16_UNORM: return PAN_AFBC_MODE_R8G8;
588
589 case PIPE_FORMAT_Z32_FLOAT: return PAN_AFBC_MODE_R8G8B8A8;
590 case PIPE_FORMAT_Z24_UNORM_S8_UINT: return PAN_AFBC_MODE_R8G8B8A8;
591 case PIPE_FORMAT_Z24X8_UNORM: return PAN_AFBC_MODE_R8G8B8A8;
592 case PIPE_FORMAT_X24S8_UINT: return PAN_AFBC_MODE_R8G8B8A8;
593 case PIPE_FORMAT_Z24_UNORM_PACKED: return PAN_AFBC_MODE_R8G8B8;
594
595 case PIPE_FORMAT_S8_UINT: return PAN_AFBC_MODE_R8;
596 case PIPE_FORMAT_R11G11B10_FLOAT: return PAN_AFBC_MODE_R11G11B10;
597
598 case PIPE_FORMAT_R16_UNORM: return PAN_AFBC_MODE_R16;
599 case PIPE_FORMAT_R16G16_UNORM: return PAN_AFBC_MODE_R16G16;
600 case PIPE_FORMAT_R16G16B16_UNORM: return PAN_AFBC_MODE_R16G16B16;
601 case PIPE_FORMAT_R16G16B16A16_UNORM:
602 return PAN_AFBC_MODE_R16G16B16A16;
603
604 default: return PAN_AFBC_MODE_INVALID;
605 }
606 /* clang-format on */
607 }
608
609 /* A format may be compressed as AFBC if it has an AFBC internal format */
610
611 static inline bool
612 pan_afbc_supports_format(unsigned arch, enum pipe_format format)
613 {
614 unsigned plane_count = util_format_get_num_planes(format);
615
616 for (unsigned i = 0; i < plane_count; i++) {
617 if (pan_afbc_format(arch, format, i) == PAN_AFBC_MODE_INVALID)
618 return false;
619 }
620
621 return true;
622 }
623
624 /* The lossless colour transform (AFBC_FORMAT_MOD_YTR) requires RGB. */
625
626 static inline bool
627 pan_afbc_can_ytr(enum pipe_format format)
628 {
629 const struct util_format_description *desc = util_format_description(format);
630
631 /* YTR is only defined for RGB(A) */
632 if (desc->nr_channels != 3 && desc->nr_channels != 4)
633 return false;
634
635 /* The fourth channel if it exists doesn't matter */
636 return desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB;
637 }
638
639 static inline bool
640 pan_afbc_can_split(unsigned arch, enum pan_afbc_mode mode, uint64_t modifier)
641 {
642 unsigned block_width = pan_afbc_superblock_width(modifier);
643
644 if (arch < 6)
645 return false;
646
647 if (block_width == 16) {
648 return true;
649 } else if (block_width == 32) {
650 return (mode == PAN_AFBC_MODE_R8G8B8A8 ||
651 mode == PAN_AFBC_MODE_R10G10B10A2);
652 }
653
654 return false;
655 }
656
657 /* Only support packing for RGB formats for now. */
658
659 static inline bool
660 pan_afbc_can_pack(enum pipe_format format)
661 {
662 const struct util_format_description *desc = util_format_description(format);
663
664 return desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB;
665 }
666
667 /*
668 * Check if a gen supports AFBC with tiled headers (and hence also solid
669 * colour blocks).
670 */
671 static inline bool
672 pan_afbc_can_tile(unsigned arch)
673 {
674 return arch >= 7;
675 }
676
677 #if PAN_ARCH >= 9
678 static inline enum mali_afbc_compression_mode
679 pan_afbc_compression_mode(enum pan_afbc_mode mode)
680 {
681 /* Map canonical formats to the hardware enum. This only
682 * needs to handle the subset of formats returned by
683 * pan_afbc_format.
684 */
685 switch (mode) {
686 case PAN_AFBC_MODE_R8:
687 return MALI_AFBC_COMPRESSION_MODE_R8;
688 case PAN_AFBC_MODE_R8G8:
689 return MALI_AFBC_COMPRESSION_MODE_R8G8;
690 case PAN_AFBC_MODE_R5G6B5:
691 return MALI_AFBC_COMPRESSION_MODE_R5G6B5;
692 case PAN_AFBC_MODE_R4G4B4A4:
693 return MALI_AFBC_COMPRESSION_MODE_R4G4B4A4;
694 case PAN_AFBC_MODE_R5G5B5A1:
695 return MALI_AFBC_COMPRESSION_MODE_R5G5B5A1;
696 case PAN_AFBC_MODE_R8G8B8:
697 return MALI_AFBC_COMPRESSION_MODE_R8G8B8;
698 case PAN_AFBC_MODE_R8G8B8A8:
699 return MALI_AFBC_COMPRESSION_MODE_R8G8B8A8;
700 case PAN_AFBC_MODE_R10G10B10A2:
701 return MALI_AFBC_COMPRESSION_MODE_R10G10B10A2;
702 case PAN_AFBC_MODE_R11G11B10:
703 return MALI_AFBC_COMPRESSION_MODE_R11G11B10;
704 #if PAN_ARCH >= 10
705 case PAN_AFBC_MODE_R16:
706 return MALI_AFBC_COMPRESSION_MODE_R16;
707 case PAN_AFBC_MODE_R16G16:
708 return MALI_AFBC_COMPRESSION_MODE_R16G16;
709 case PAN_AFBC_MODE_R16G16B16:
710 return MALI_AFBC_COMPRESSION_MODE_R16G16B16;
711 case PAN_AFBC_MODE_R16G16B16A16:
712 return MALI_AFBC_COMPRESSION_MODE_R16G16B16A16;
713 #endif
714 case PAN_AFBC_MODE_YUV420_6C8:
715 return MALI_AFBC_COMPRESSION_MODE_YUV420_6C8;
716 case PAN_AFBC_MODE_YUV420_2C8:
717 return MALI_AFBC_COMPRESSION_MODE_YUV420_2C8;
718 case PAN_AFBC_MODE_YUV420_1C8:
719 return MALI_AFBC_COMPRESSION_MODE_YUV420_1C8;
720 case PAN_AFBC_MODE_YUV420_6C10:
721 return MALI_AFBC_COMPRESSION_MODE_YUV420_6C10;
722 case PAN_AFBC_MODE_YUV420_2C10:
723 return MALI_AFBC_COMPRESSION_MODE_YUV420_2C10;
724 case PAN_AFBC_MODE_YUV420_1C10:
725 return MALI_AFBC_COMPRESSION_MODE_YUV420_1C10;
726 case PAN_AFBC_MODE_YUV422_4C8:
727 return MALI_AFBC_COMPRESSION_MODE_YUV422_4C8;
728 case PAN_AFBC_MODE_YUV422_2C8:
729 return MALI_AFBC_COMPRESSION_MODE_YUV422_2C8;
730 case PAN_AFBC_MODE_YUV422_1C8:
731 return MALI_AFBC_COMPRESSION_MODE_YUV422_1C8;
732 case PAN_AFBC_MODE_YUV422_4C10:
733 return MALI_AFBC_COMPRESSION_MODE_YUV422_4C10;
734 case PAN_AFBC_MODE_YUV422_2C10:
735 return MALI_AFBC_COMPRESSION_MODE_YUV422_2C10;
736 case PAN_AFBC_MODE_YUV422_1C10:
737 return MALI_AFBC_COMPRESSION_MODE_YUV422_1C10;
738 #if PAN_ARCH == 9
739 case PAN_AFBC_MODE_R16:
740 case PAN_AFBC_MODE_R16G16:
741 case PAN_AFBC_MODE_R16G16B16:
742 case PAN_AFBC_MODE_R16G16B16A16:
743 #endif
744 case PAN_AFBC_MODE_INVALID:
745 UNREACHABLE("Invalid AFBC format");
746 }
747
748 UNREACHABLE("all AFBC formats handled");
749 }
750
751 static inline enum mali_afbc_compression_mode
752 pan_afbc_decompression_mode(enum pipe_format view_format,
753 enum pan_afbc_mode img_mode)
754 {
755 /* There are special cases for texture views of stencil images. */
756 if (img_mode == PAN_AFBC_MODE_R8G8B8A8 &&
757 view_format == PIPE_FORMAT_X24S8_UINT)
758 return MALI_AFBC_COMPRESSION_MODE_X24S8;
759 else if (view_format == PIPE_FORMAT_S8_UINT)
760 return MALI_AFBC_COMPRESSION_MODE_S8;
761
762 return pan_afbc_compression_mode(img_mode);
763 }
764 #endif
765
766 static inline bool
767 format_requires_afbc(enum pipe_format format) {
768 switch (format) {
769 case PIPE_FORMAT_R8G8B8_420_UNORM_PACKED:
770 case PIPE_FORMAT_R10G10B10_420_UNORM_PACKED:
771 case PIPE_FORMAT_X6R10X6G10_X6R10X6B10_422_UNORM:
772 return true;
773 default:
774 return false;
775 }
776 }
777
778 #ifdef __cplusplus
779 } /* extern C */
780 #endif
781
782 #endif