Debian Codegraph @

src/panfrost/lib/pan_format.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_FORMAT_H
10 #define __PAN_FORMAT_H
11
12 #include "genxml/gen_macros.h"
13
14 #include "util/format/u_format.h"
15
16 #include "drm-uapi/drm_fourcc.h"
17
18 /*
19 * List of supported modifiers, in descending order of preference. AFBC is
20 * faster than u-interleaved tiling which is faster than linear. Within AFBC,
21 * enabling the YUV-like transform is typically a win where possible.
22 * AFRC is only used if explicitly asked for (only for RGB formats).
23 * Similarly MTK 16L32 is only used if explicitly asked for.
24 */
25 #define PAN_SUPPORTED_MODIFIERS(__name) \
26 static const uint64_t __name[] = { \
27 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_32x8 | \
28 AFBC_FORMAT_MOD_SPARSE | AFBC_FORMAT_MOD_SPLIT), \
29 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_32x8 | \
30 AFBC_FORMAT_MOD_SPARSE | AFBC_FORMAT_MOD_SPLIT | \
31 AFBC_FORMAT_MOD_YTR), \
32 \
33 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 | \
34 AFBC_FORMAT_MOD_TILED | AFBC_FORMAT_MOD_SC | \
35 AFBC_FORMAT_MOD_SPARSE | AFBC_FORMAT_MOD_YTR), \
36 \
37 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 | \
38 AFBC_FORMAT_MOD_TILED | AFBC_FORMAT_MOD_SC | \
39 AFBC_FORMAT_MOD_SPARSE), \
40 \
41 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 | \
42 AFBC_FORMAT_MOD_SPARSE | AFBC_FORMAT_MOD_YTR), \
43 \
44 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 | \
45 AFBC_FORMAT_MOD_SPARSE), \
46 \
47 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 | \
48 AFBC_FORMAT_MOD_SPARSE | AFBC_FORMAT_MOD_SPLIT), \
49 \
50 DRM_FORMAT_MOD_ARM_INTERLEAVED_64K, \
51 DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED, \
52 DRM_FORMAT_MOD_LINEAR, \
53 \
54 DRM_FORMAT_MOD_ARM_AFRC( \
55 AFRC_FORMAT_MOD_CU_SIZE_P0(AFRC_FORMAT_MOD_CU_SIZE_16)), \
56 DRM_FORMAT_MOD_ARM_AFRC( \
57 AFRC_FORMAT_MOD_CU_SIZE_P0(AFRC_FORMAT_MOD_CU_SIZE_24)), \
58 DRM_FORMAT_MOD_ARM_AFRC( \
59 AFRC_FORMAT_MOD_CU_SIZE_P0(AFRC_FORMAT_MOD_CU_SIZE_32)), \
60 DRM_FORMAT_MOD_ARM_AFRC( \
61 AFRC_FORMAT_MOD_CU_SIZE_P0(AFRC_FORMAT_MOD_CU_SIZE_16) | \
62 AFRC_FORMAT_MOD_LAYOUT_SCAN), \
63 DRM_FORMAT_MOD_ARM_AFRC( \
64 AFRC_FORMAT_MOD_CU_SIZE_P0(AFRC_FORMAT_MOD_CU_SIZE_24) | \
65 AFRC_FORMAT_MOD_LAYOUT_SCAN), \
66 DRM_FORMAT_MOD_ARM_AFRC( \
67 AFRC_FORMAT_MOD_CU_SIZE_P0(AFRC_FORMAT_MOD_CU_SIZE_32) | \
68 AFRC_FORMAT_MOD_LAYOUT_SCAN), \
69 }
70
71 /* DRM modifier helper */
72
73 #define drm_is_afbc(mod) \
74 ((mod >> 52) == \
75 (DRM_FORMAT_MOD_ARM_TYPE_AFBC | (DRM_FORMAT_MOD_VENDOR_ARM << 4)))
76
77 #define drm_is_afrc(mod) \
78 ((mod >> 52) == \
79 (DRM_FORMAT_MOD_ARM_TYPE_AFRC | (DRM_FORMAT_MOD_VENDOR_ARM << 4)))
80
81 static inline bool
82 pan_u_tiled_or_linear_supports_format(enum pipe_format format)
83 {
84 switch (format) {
85 case PIPE_FORMAT_R8G8B8_420_UNORM_PACKED:
86 case PIPE_FORMAT_R10G10B10_420_UNORM_PACKED:
87 return false;
88
89 default:
90 return true;
91 }
92 }
93
94 /* Formats */
95
96 static inline unsigned
97 pan_format_get_plane_blocksize(enum pipe_format format, unsigned plane_idx)
98 {
99 switch (format) {
100 case PIPE_FORMAT_R8_G8B8_420_UNORM:
101 case PIPE_FORMAT_R8_B8G8_420_UNORM:
102 case PIPE_FORMAT_R8_G8B8_422_UNORM:
103 case PIPE_FORMAT_R8_B8G8_422_UNORM:
104 return plane_idx ? 2 : 1;
105 case PIPE_FORMAT_R10_G10B10_420_UNORM:
106 case PIPE_FORMAT_R10_G10B10_422_UNORM:
107 return plane_idx ? 10 : 5;
108 case PIPE_FORMAT_R8_G8_B8_420_UNORM:
109 case PIPE_FORMAT_R8_B8_G8_420_UNORM:
110 return 1;
111 default:
112 assert(util_format_get_num_planes(format) == 1);
113 return util_format_get_blocksize(format);
114 }
115 }
116
117 static inline unsigned
118 pan_format_tib_size(enum pipe_format format, bool internal)
119 {
120 if (internal && !util_format_is_float(format)) {
121 /* Blendable UNORM and sRGB formats are always 32-bits in the tile
122 * buffer, extra bits are used as padding or to dither */
123 return 4;
124 } else {
125 /* Non-blendable and float formats are raw, rounded up to the nearest
126 * power-of-two size */
127 unsigned bytes = util_format_get_blocksize(format);
128 return util_next_power_of_two(bytes);
129 }
130 }
131
132 typedef uint32_t mali_pixel_format;
133
134 /* pan bind flags */
135 #define PAN_BIND_DEPTH_STENCIL BITFIELD_BIT(0)
136 #define PAN_BIND_RENDER_TARGET BITFIELD_BIT(1)
137 #define PAN_BIND_SAMPLER_VIEW BITFIELD_BIT(2)
138 #define PAN_BIND_VERTEX_BUFFER BITFIELD_BIT(3)
139 #define PAN_BIND_STORAGE_IMAGE BITFIELD_BIT(4)
140 #define PAN_BIND_TEXEL_BUFFER BITFIELD_BIT(5)
141
142 struct pan_format {
143 uint32_t hw : 21;
144 uint32_t texfeat_bit : 5;
145 uint32_t bind : 6;
146 };
147
148 struct pan_blendable_format {
149 /* enum mali_color_buffer_internal_format */ uint16_t internal;
150 /* enum mali_mfbd_color_format */ uint16_t writeback;
151
152 /* Indexed by the dithered? flag. So _PU first, then _AU */
153 mali_pixel_format bifrost[2];
154 };
155
156 #define pan_blendable_formats_v4 pan_blendable_formats_v5
157 extern const struct pan_blendable_format
158 pan_blendable_formats_v5[PIPE_FORMAT_COUNT];
159 extern const struct pan_blendable_format
160 pan_blendable_formats_v6[PIPE_FORMAT_COUNT];
161 extern const struct pan_blendable_format
162 pan_blendable_formats_v7[PIPE_FORMAT_COUNT];
163 extern const struct pan_blendable_format
164 pan_blendable_formats_v9[PIPE_FORMAT_COUNT];
165 extern const struct pan_blendable_format
166 pan_blendable_formats_v10[PIPE_FORMAT_COUNT];
167 extern const struct pan_blendable_format
168 pan_blendable_formats_v12[PIPE_FORMAT_COUNT];
169 extern const struct pan_blendable_format
170 pan_blendable_formats_v13[PIPE_FORMAT_COUNT];
171
172 uint8_t pan_raw_format_mask_midgard(enum pipe_format *formats);
173
174 static inline const struct pan_blendable_format *
175 pan_blendable_format_table(unsigned arch)
176 {
177 switch (arch) {
178 #define FMT_TABLE(x) case x: return pan_blendable_formats_v ## x
179 FMT_TABLE(4);
180 FMT_TABLE(5);
181 FMT_TABLE(6);
182 FMT_TABLE(7);
183 FMT_TABLE(9);
184 FMT_TABLE(10);
185 FMT_TABLE(12);
186 FMT_TABLE(13);
187 #undef FMT_TABLE
188 default:
189 assert(!"Unsupported architecture");
190 return NULL;
191 }
192 }
193
194 #define pan_pipe_format_v4 pan_pipe_format_v5
195 extern const struct pan_format pan_pipe_format_v5[PIPE_FORMAT_COUNT];
196 extern const struct pan_format pan_pipe_format_v6[PIPE_FORMAT_COUNT];
197 extern const struct pan_format pan_pipe_format_v7[PIPE_FORMAT_COUNT];
198 extern const struct pan_format pan_pipe_format_v9[PIPE_FORMAT_COUNT];
199 extern const struct pan_format pan_pipe_format_v10[PIPE_FORMAT_COUNT];
200 extern const struct pan_format pan_pipe_format_v12[PIPE_FORMAT_COUNT];
201 extern const struct pan_format pan_pipe_format_v13[PIPE_FORMAT_COUNT];
202
203 static inline const struct pan_format *
204 pan_format_table(unsigned arch)
205 {
206 switch (arch) {
207 #define FMT_TABLE(x) case x: return pan_pipe_format_v ## x
208 FMT_TABLE(4);
209 FMT_TABLE(5);
210 FMT_TABLE(6);
211 FMT_TABLE(7);
212 FMT_TABLE(9);
213 FMT_TABLE(10);
214 FMT_TABLE(12);
215 FMT_TABLE(13);
216 #undef FMT_TABLE
217 default:
218 assert(!"Unsupported architecture");
219 return NULL;
220 }
221 }
222
223 /* Helpers to construct swizzles */
224
225 #define PAN_V6_SWIZZLE(R, G, B, A) \
226 (((MALI_CHANNEL_##R) << 0) | ((MALI_CHANNEL_##G) << 3) | \
227 ((MALI_CHANNEL_##B) << 6) | ((MALI_CHANNEL_##A) << 9))
228
229 static inline unsigned
230 pan_get_default_swizzle(unsigned components)
231 {
232 switch (components) {
233 case 1:
234 return PAN_V6_SWIZZLE(R, 0, 0, 1);
235 case 2:
236 return PAN_V6_SWIZZLE(R, G, 0, 1);
237 case 3:
238 return PAN_V6_SWIZZLE(R, G, B, 1);
239 case 4:
240 return PAN_V6_SWIZZLE(R, G, B, A);
241 default:
242 UNREACHABLE("Invalid number of components");
243 }
244 }
245
246 #if PAN_ARCH == 7 || PAN_ARCH >= 10
247 struct pan_decomposed_swizzle {
248 /* Component ordering to apply first */
249 enum mali_rgb_component_order pre;
250
251 /* Bijective swizzle applied after */
252 unsigned char post[4];
253 };
254
255 struct pan_decomposed_swizzle
256 GENX(pan_decompose_swizzle)(enum mali_rgb_component_order order);
257 #endif
258
259 #define MALI_SRGB_L (0)
260 #define MALI_SRGB_S (1)
261
262 #if PAN_ARCH <= 6
263
264 #define MALI_V6_0000 PAN_V6_SWIZZLE(0, 0, 0, 0)
265 #define MALI_V6_000R PAN_V6_SWIZZLE(0, 0, 0, R)
266 #define MALI_V6_0R00 PAN_V6_SWIZZLE(0, R, 0, 0)
267 #define MALI_V6_0A00 PAN_V6_SWIZZLE(0, A, 0, 0)
268 #define MALI_V6_AAAA PAN_V6_SWIZZLE(A, A, A, A)
269 #define MALI_V6_A001 PAN_V6_SWIZZLE(A, 0, 0, 1)
270 #define MALI_V6_ABG1 PAN_V6_SWIZZLE(A, B, G, 1)
271 #define MALI_V6_ABGR PAN_V6_SWIZZLE(A, B, G, R)
272 #define MALI_V6_ARGB PAN_V6_SWIZZLE(A, R, G, B)
273 #define MALI_V6_BGR1 PAN_V6_SWIZZLE(B, G, R, 1)
274 #define MALI_V6_BGRA PAN_V6_SWIZZLE(B, G, R, A)
275 #define MALI_V6_GBA1 PAN_V6_SWIZZLE(G, B, A, 1)
276 #define MALI_V6_GBAR PAN_V6_SWIZZLE(G, B, A, R)
277 #define MALI_V6_R000 PAN_V6_SWIZZLE(R, 0, 0, 0)
278 #define MALI_V6_R001 PAN_V6_SWIZZLE(R, 0, 0, 1)
279 #define MALI_V6_RG01 PAN_V6_SWIZZLE(R, G, 0, 1)
280 #define MALI_V6_RGB1 PAN_V6_SWIZZLE(R, G, B, 1)
281 #define MALI_V6_RGBA PAN_V6_SWIZZLE(R, G, B, A)
282 #define MALI_V6_RRR1 PAN_V6_SWIZZLE(R, R, R, 1)
283 #define MALI_V6_RRRG PAN_V6_SWIZZLE(R, R, R, G)
284 #define MALI_V6_RRRR PAN_V6_SWIZZLE(R, R, R, R)
285 #define MALI_V6_GGGG PAN_V6_SWIZZLE(G, G, G, G)
286
287 #define MALI_PACK_FMT(mali, swizzle, srgb) \
288 (MALI_V6_##swizzle) | ((MALI_##mali) << 12) | (((MALI_SRGB_##srgb)) << 20)
289
290 #else
291
292 #define MALI_RGB_COMPONENT_ORDER_R001 MALI_RGB_COMPONENT_ORDER_RGB1
293 #define MALI_RGB_COMPONENT_ORDER_RG01 MALI_RGB_COMPONENT_ORDER_RGB1
294 #define MALI_RGB_COMPONENT_ORDER_GBAR MALI_RGB_COMPONENT_ORDER_ARGB
295 #define MALI_RGB_COMPONENT_ORDER_GBA1 MALI_RGB_COMPONENT_ORDER_1RGB
296 #define MALI_RGB_COMPONENT_ORDER_ABG1 MALI_RGB_COMPONENT_ORDER_1BGR
297
298 #define MALI_PACK_FMT(mali, swizzle, srgb) \
299 (MALI_RGB_COMPONENT_ORDER_##swizzle) | ((MALI_##mali) << 12) | \
300 (((MALI_SRGB_##srgb)) << 20)
301
302 #endif
303
304 #define MALI_EXTRACT_INDEX(pixfmt) (((pixfmt) >> 12) & 0xFF)
305
306 static inline bool
307 pan_format_is_yuv(enum pipe_format f)
308 {
309 enum util_format_layout layout = util_format_description(f)->layout;
310
311 /* Mesa's subsampled RGB formats are considered YUV formats on Mali */
312 return layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED ||
313 layout == UTIL_FORMAT_LAYOUT_PLANAR2 ||
314 layout == UTIL_FORMAT_LAYOUT_PLANAR3;
315 }
316
317 #ifdef PAN_ARCH
318 static inline const struct pan_format *
319 GENX(pan_format_from_pipe_format)(enum pipe_format f)
320 {
321 return &GENX(pan_pipe_format)[f];
322 }
323
324 static inline const struct pan_blendable_format *
325 GENX(pan_blendable_format_from_pipe_format)(enum pipe_format f)
326 {
327 return &GENX(pan_blendable_formats)[f];
328 }
329
330 #if PAN_ARCH >= 6
331 static inline unsigned
332 GENX(pan_dithered_format_from_pipe_format)(enum pipe_format f, bool dithered)
333 {
334 mali_pixel_format pixfmt = GENX(pan_blendable_formats)[f].bifrost[dithered];
335
336 /* Formats requiring blend shaders are stored raw in the tilebuffer and will
337 * have 0 as their pixel format. Assumes dithering is set, I don't know of a
338 * case when it makes sense to turn off dithering. */
339 return pixfmt ?: GENX(pan_format_from_pipe_format)(f)->hw;
340 }
341 #endif
342
343 static inline bool
344 GENX(pan_format_supports_hw_blend)(enum pipe_format format)
345 {
346 return GENX(pan_blendable_format_from_pipe_format)(format)->internal;
347 }
348
349 static inline bool
350 GENX(pan_format_supports_msaa_average)(enum pipe_format format)
351 {
352 if (!GENX(pan_format_supports_hw_blend)(format))
353 return false;
354
355 /* F16 formats are blendable on v10 but don't support averaging until v11 */
356 if (util_format_is_float16(format) && PAN_ARCH < 11)
357 return false;
358
359 return true;
360 }
361 #endif
362
363 #endif