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 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 */
27
28 #ifndef __PAN_FORMAT_H
29 #define __PAN_FORMAT_H
30
31 #include "genxml/gen_macros.h"
32
33 #include "util/format/u_format.h"
34
35 /* Formats */
36
37 typedef uint32_t mali_pixel_format;
38
39 /* pan bind flags */
40 #define PAN_BIND_DEPTH_STENCIL (1 << 0)
41 #define PAN_BIND_RENDER_TARGET (1 << 1)
42 #define PAN_BIND_SAMPLER_VIEW (1 << 3)
43 #define PAN_BIND_VERTEX_BUFFER (1 << 4)
44
45 struct panfrost_format {
46 uint32_t hw : 22;
47 uint32_t texfeat_bit : 5;
48 unsigned bind;
49 };
50
51 struct pan_blendable_format {
52 /* enum mali_color_buffer_internal_format */ uint16_t internal;
53 /* enum mali_mfbd_color_format */ uint16_t writeback;
54
55 /* Indexed by the dithered? flag. So _PU first, then _AU */
56 mali_pixel_format bifrost[2];
57 };
58
59 #define panfrost_blendable_formats_v4 panfrost_blendable_formats_v5
60 extern const struct pan_blendable_format
61 panfrost_blendable_formats_v5[PIPE_FORMAT_COUNT];
62 extern const struct pan_blendable_format
63 panfrost_blendable_formats_v6[PIPE_FORMAT_COUNT];
64 extern const struct pan_blendable_format
65 panfrost_blendable_formats_v7[PIPE_FORMAT_COUNT];
66 extern const struct pan_blendable_format
67 panfrost_blendable_formats_v9[PIPE_FORMAT_COUNT];
68 extern const struct pan_blendable_format
69 panfrost_blendable_formats_v10[PIPE_FORMAT_COUNT];
70
71 static inline const struct pan_blendable_format *
panfrost_blendable_format_table(unsigned arch)72 panfrost_blendable_format_table(unsigned arch)
73 {
74 switch (arch) {
75 #define FMT_TABLE(x) case x: return panfrost_blendable_formats_v ## x
76 FMT_TABLE(4);
77 FMT_TABLE(5);
78 FMT_TABLE(6);
79 FMT_TABLE(7);
80 FMT_TABLE(9);
81 FMT_TABLE(10);
82 #undef FMT_TABLE
83 default:
84 assert(!"Unsupported architecture");
85 return NULL;
86 }
87 }
88
89 #define panfrost_pipe_format_v4 panfrost_pipe_format_v5
90 extern const struct panfrost_format panfrost_pipe_format_v5[PIPE_FORMAT_COUNT];
91 extern const struct panfrost_format panfrost_pipe_format_v6[PIPE_FORMAT_COUNT];
92 extern const struct panfrost_format panfrost_pipe_format_v7[PIPE_FORMAT_COUNT];
93 extern const struct panfrost_format panfrost_pipe_format_v9[PIPE_FORMAT_COUNT];
94 extern const struct panfrost_format panfrost_pipe_format_v10[PIPE_FORMAT_COUNT];
95
96 static inline const struct panfrost_format *
panfrost_format_table(unsigned arch)97 panfrost_format_table(unsigned arch)
98 {
99 switch (arch) {
100 #define FMT_TABLE(x) case x: return panfrost_pipe_format_v ## x
101 FMT_TABLE(4);
102 FMT_TABLE(5);
103 FMT_TABLE(6);
104 FMT_TABLE(7);
105 FMT_TABLE(9);
106 FMT_TABLE(10);
107 #undef FMT_TABLE
108 default:
109 assert(!"Unsupported architecture");
110 return NULL;
111 }
112 }
113
114 /* Helpers to construct swizzles */
115
116 #define PAN_V6_SWIZZLE(R, G, B, A) \
117 (((MALI_CHANNEL_##R) << 0) | ((MALI_CHANNEL_##G) << 3) | \
118 ((MALI_CHANNEL_##B) << 6) | ((MALI_CHANNEL_##A) << 9))
119
120 static inline unsigned
panfrost_get_default_swizzle(unsigned components)121 panfrost_get_default_swizzle(unsigned components)
122 {
123 switch (components) {
124 case 1:
125 return PAN_V6_SWIZZLE(R, 0, 0, 1);
126 case 2:
127 return PAN_V6_SWIZZLE(R, G, 0, 1);
128 case 3:
129 return PAN_V6_SWIZZLE(R, G, B, 1);
130 case 4:
131 return PAN_V6_SWIZZLE(R, G, B, A);
132 default:
133 unreachable("Invalid number of components");
134 }
135 }
136
137 #if PAN_ARCH == 7 || PAN_ARCH >= 10
138 struct pan_decomposed_swizzle {
139 /* Component ordering to apply first */
140 enum mali_rgb_component_order pre;
141
142 /* Bijective swizzle applied after */
143 unsigned char post[4];
144 };
145
146 struct pan_decomposed_swizzle
147 GENX(pan_decompose_swizzle)(enum mali_rgb_component_order order);
148 #endif
149
150 #define MALI_SRGB_L (0)
151 #define MALI_SRGB_S (1)
152
153 #if PAN_ARCH <= 6
154
155 #define MALI_V6_0000 PAN_V6_SWIZZLE(0, 0, 0, 0)
156 #define MALI_V6_000R PAN_V6_SWIZZLE(0, 0, 0, R)
157 #define MALI_V6_0R00 PAN_V6_SWIZZLE(0, R, 0, 0)
158 #define MALI_V6_0A00 PAN_V6_SWIZZLE(0, A, 0, 0)
159 #define MALI_V6_AAAA PAN_V6_SWIZZLE(A, A, A, A)
160 #define MALI_V6_A001 PAN_V6_SWIZZLE(A, 0, 0, 1)
161 #define MALI_V6_ABG1 PAN_V6_SWIZZLE(A, B, G, 1)
162 #define MALI_V6_ABGR PAN_V6_SWIZZLE(A, B, G, R)
163 #define MALI_V6_ARGB PAN_V6_SWIZZLE(A, R, G, B)
164 #define MALI_V6_BGR1 PAN_V6_SWIZZLE(B, G, R, 1)
165 #define MALI_V6_BGRA PAN_V6_SWIZZLE(B, G, R, A)
166 #define MALI_V6_GBA1 PAN_V6_SWIZZLE(G, B, A, 1)
167 #define MALI_V6_GBAR PAN_V6_SWIZZLE(G, B, A, R)
168 #define MALI_V6_R000 PAN_V6_SWIZZLE(R, 0, 0, 0)
169 #define MALI_V6_R001 PAN_V6_SWIZZLE(R, 0, 0, 1)
170 #define MALI_V6_RG01 PAN_V6_SWIZZLE(R, G, 0, 1)
171 #define MALI_V6_RGB1 PAN_V6_SWIZZLE(R, G, B, 1)
172 #define MALI_V6_RGBA PAN_V6_SWIZZLE(R, G, B, A)
173 #define MALI_V6_RRR1 PAN_V6_SWIZZLE(R, R, R, 1)
174 #define MALI_V6_RRRG PAN_V6_SWIZZLE(R, R, R, G)
175 #define MALI_V6_RRRR PAN_V6_SWIZZLE(R, R, R, R)
176 #define MALI_V6_GGGG PAN_V6_SWIZZLE(G, G, G, G)
177
178 #define MALI_PACK_FMT(mali, swizzle, srgb) \
179 (MALI_V6_##swizzle) | ((MALI_##mali) << 12) | (((MALI_SRGB_##srgb)) << 20)
180
181 #else
182
183 #define MALI_RGB_COMPONENT_ORDER_R001 MALI_RGB_COMPONENT_ORDER_RGB1
184 #define MALI_RGB_COMPONENT_ORDER_RG01 MALI_RGB_COMPONENT_ORDER_RGB1
185 #define MALI_RGB_COMPONENT_ORDER_GBAR MALI_RGB_COMPONENT_ORDER_ARGB
186 #define MALI_RGB_COMPONENT_ORDER_GBA1 MALI_RGB_COMPONENT_ORDER_1RGB
187 #define MALI_RGB_COMPONENT_ORDER_ABG1 MALI_RGB_COMPONENT_ORDER_1BGR
188
189 #define MALI_PACK_FMT(mali, swizzle, srgb) \
190 (MALI_RGB_COMPONENT_ORDER_##swizzle) | ((MALI_##mali) << 12) | \
191 (((MALI_SRGB_##srgb)) << 20)
192
193 #endif
194
195 #define MALI_EXTRACT_INDEX(pixfmt) (((pixfmt) >> 12) & 0xFF)
196
panfrost_format_is_yuv(enum pipe_format f)197 static inline bool panfrost_format_is_yuv(enum pipe_format f)
198 {
199 enum util_format_layout layout = util_format_description(f)->layout;
200
201 /* Mesa's subsampled RGB formats are considered YUV formats on Mali */
202 return layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED ||
203 layout == UTIL_FORMAT_LAYOUT_PLANAR2 ||
204 layout == UTIL_FORMAT_LAYOUT_PLANAR3;
205 }
206
207 #ifdef PAN_ARCH
208 static inline const struct panfrost_format *
GENX(panfrost_format_from_pipe_format)209 GENX(panfrost_format_from_pipe_format)(enum pipe_format f)
210 {
211 return &GENX(panfrost_pipe_format)[f];
212 }
213
214 static inline const struct pan_blendable_format *
GENX(panfrost_blendable_format_from_pipe_format)215 GENX(panfrost_blendable_format_from_pipe_format)(enum pipe_format f)
216 {
217 return &GENX(panfrost_blendable_formats)[f];
218 }
219
220 #if PAN_ARCH >= 6
221 static inline unsigned
GENX(panfrost_dithered_format_from_pipe_format)222 GENX(panfrost_dithered_format_from_pipe_format)(enum pipe_format f, bool dithered)
223 {
224 mali_pixel_format pixfmt =
225 GENX(panfrost_blendable_formats)[f].bifrost[dithered];
226
227 /* Formats requiring blend shaders are stored raw in the tilebuffer and will
228 * have 0 as their pixel format. Assumes dithering is set, I don't know of a
229 * case when it makes sense to turn off dithering. */
230 return pixfmt ?: GENX(panfrost_format_from_pipe_format)(f)->hw;
231 }
232 #endif
233 #endif
234
235 #endif
236