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 mali_pixel_format hw;
47 unsigned bind;
48 };
49
50 struct pan_blendable_format {
51 /* enum mali_color_buffer_internal_format */ uint16_t internal;
52 /* enum mali_mfbd_color_format */ uint16_t writeback;
53
54 /* Indexed by the dithered? flag. So _PU first, then _AU */
55 mali_pixel_format bifrost[2];
56 };
57
58 #define panfrost_blendable_formats_v4 panfrost_blendable_formats_v5
59 extern const struct pan_blendable_format
60 panfrost_blendable_formats_v5[PIPE_FORMAT_COUNT];
61 extern const struct pan_blendable_format
62 panfrost_blendable_formats_v6[PIPE_FORMAT_COUNT];
63 extern const struct pan_blendable_format
64 panfrost_blendable_formats_v7[PIPE_FORMAT_COUNT];
65 extern const struct pan_blendable_format
66 panfrost_blendable_formats_v9[PIPE_FORMAT_COUNT];
67 extern const struct pan_blendable_format
68 panfrost_blendable_formats_v10[PIPE_FORMAT_COUNT];
69
70 static inline const struct pan_blendable_format *
panfrost_blendable_format_table(unsigned arch)71 panfrost_blendable_format_table(unsigned arch)
72 {
73 switch (arch) {
74 #define FMT_TABLE(x) case x: return panfrost_blendable_formats_v ## x
75 FMT_TABLE(4);
76 FMT_TABLE(5);
77 FMT_TABLE(6);
78 FMT_TABLE(7);
79 FMT_TABLE(9);
80 FMT_TABLE(10);
81 #undef FMT_TABLE
82 default:
83 assert(!"Unsupported architecture");
84 return NULL;
85 }
86 }
87
88 #define panfrost_pipe_format_v4 panfrost_pipe_format_v5
89 extern const struct panfrost_format panfrost_pipe_format_v5[PIPE_FORMAT_COUNT];
90 extern const struct panfrost_format panfrost_pipe_format_v6[PIPE_FORMAT_COUNT];
91 extern const struct panfrost_format panfrost_pipe_format_v7[PIPE_FORMAT_COUNT];
92 extern const struct panfrost_format panfrost_pipe_format_v9[PIPE_FORMAT_COUNT];
93 extern const struct panfrost_format panfrost_pipe_format_v10[PIPE_FORMAT_COUNT];
94
95 static inline const struct panfrost_format *
panfrost_format_table(unsigned arch)96 panfrost_format_table(unsigned arch)
97 {
98 switch (arch) {
99 #define FMT_TABLE(x) case x: return panfrost_pipe_format_v ## x
100 FMT_TABLE(4);
101 FMT_TABLE(5);
102 FMT_TABLE(6);
103 FMT_TABLE(7);
104 FMT_TABLE(9);
105 FMT_TABLE(10);
106 #undef FMT_TABLE
107 default:
108 assert(!"Unsupported architecture");
109 return NULL;
110 }
111 }
112
113 /* Helpers to construct swizzles */
114
115 #define PAN_V6_SWIZZLE(R, G, B, A) \
116 (((MALI_CHANNEL_##R) << 0) | ((MALI_CHANNEL_##G) << 3) | \
117 ((MALI_CHANNEL_##B) << 6) | ((MALI_CHANNEL_##A) << 9))
118
119 static inline unsigned
panfrost_get_default_swizzle(unsigned components)120 panfrost_get_default_swizzle(unsigned components)
121 {
122 switch (components) {
123 case 1:
124 return PAN_V6_SWIZZLE(R, 0, 0, 1);
125 case 2:
126 return PAN_V6_SWIZZLE(R, G, 0, 1);
127 case 3:
128 return PAN_V6_SWIZZLE(R, G, B, 1);
129 case 4:
130 return PAN_V6_SWIZZLE(R, G, B, A);
131 default:
132 unreachable("Invalid number of components");
133 }
134 }
135
136 #if PAN_ARCH == 7 || PAN_ARCH >= 10
137 struct pan_decomposed_swizzle {
138 /* Component ordering to apply first */
139 enum mali_rgb_component_order pre;
140
141 /* Bijective swizzle applied after */
142 unsigned char post[4];
143 };
144
145 struct pan_decomposed_swizzle
146 GENX(pan_decompose_swizzle)(enum mali_rgb_component_order order);
147 #endif
148
149 #define MALI_SRGB_L (0)
150 #define MALI_SRGB_S (1)
151
152 #if PAN_ARCH <= 6
153
154 #define MALI_V6_0000 PAN_V6_SWIZZLE(0, 0, 0, 0)
155 #define MALI_V6_000R PAN_V6_SWIZZLE(0, 0, 0, R)
156 #define MALI_V6_0R00 PAN_V6_SWIZZLE(0, R, 0, 0)
157 #define MALI_V6_0A00 PAN_V6_SWIZZLE(0, A, 0, 0)
158 #define MALI_V6_AAAA PAN_V6_SWIZZLE(A, A, A, A)
159 #define MALI_V6_A001 PAN_V6_SWIZZLE(A, 0, 0, 1)
160 #define MALI_V6_ABG1 PAN_V6_SWIZZLE(A, B, G, 1)
161 #define MALI_V6_ABGR PAN_V6_SWIZZLE(A, B, G, R)
162 #define MALI_V6_BGR1 PAN_V6_SWIZZLE(B, G, R, 1)
163 #define MALI_V6_BGRA PAN_V6_SWIZZLE(B, G, R, A)
164 #define MALI_V6_GBA1 PAN_V6_SWIZZLE(G, B, A, 1)
165 #define MALI_V6_GBAR PAN_V6_SWIZZLE(G, B, A, R)
166 #define MALI_V6_R000 PAN_V6_SWIZZLE(R, 0, 0, 0)
167 #define MALI_V6_R001 PAN_V6_SWIZZLE(R, 0, 0, 1)
168 #define MALI_V6_RG01 PAN_V6_SWIZZLE(R, G, 0, 1)
169 #define MALI_V6_RGB1 PAN_V6_SWIZZLE(R, G, B, 1)
170 #define MALI_V6_RGBA PAN_V6_SWIZZLE(R, G, B, A)
171 #define MALI_V6_RRR1 PAN_V6_SWIZZLE(R, R, R, 1)
172 #define MALI_V6_RRRG PAN_V6_SWIZZLE(R, R, R, G)
173 #define MALI_V6_RRRR PAN_V6_SWIZZLE(R, R, R, R)
174 #define MALI_V6_GGGG PAN_V6_SWIZZLE(G, G, G, G)
175
176 #define MALI_PACK_FMT(mali, swizzle, srgb) \
177 (MALI_V6_##swizzle) | ((MALI_##mali) << 12) | (((MALI_SRGB_##srgb)) << 20)
178
179 #else
180
181 #define MALI_RGB_COMPONENT_ORDER_R001 MALI_RGB_COMPONENT_ORDER_RGB1
182 #define MALI_RGB_COMPONENT_ORDER_RG01 MALI_RGB_COMPONENT_ORDER_RGB1
183 #define MALI_RGB_COMPONENT_ORDER_GBAR MALI_RGB_COMPONENT_ORDER_ARGB
184 #define MALI_RGB_COMPONENT_ORDER_GBA1 MALI_RGB_COMPONENT_ORDER_1RGB
185 #define MALI_RGB_COMPONENT_ORDER_ABG1 MALI_RGB_COMPONENT_ORDER_1BGR
186
187 #define MALI_PACK_FMT(mali, swizzle, srgb) \
188 (MALI_RGB_COMPONENT_ORDER_##swizzle) | ((MALI_##mali) << 12) | \
189 (((MALI_SRGB_##srgb)) << 20)
190
191 #endif
192
panfrost_format_is_yuv(enum pipe_format f)193 static inline bool panfrost_format_is_yuv(enum pipe_format f)
194 {
195 enum util_format_layout layout = util_format_description(f)->layout;
196
197 /* Mesa's subsampled RGB formats are considered YUV formats on Mali */
198 return layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED ||
199 layout == UTIL_FORMAT_LAYOUT_PLANAR2 ||
200 layout == UTIL_FORMAT_LAYOUT_PLANAR3;
201 }
202
203 #ifdef PAN_ARCH
204 static inline const struct panfrost_format *
GENX(panfrost_format_from_pipe_format)205 GENX(panfrost_format_from_pipe_format)(enum pipe_format f)
206 {
207 return &GENX(panfrost_pipe_format)[f];
208 }
209
210 static inline const struct pan_blendable_format *
GENX(panfrost_blendable_format_from_pipe_format)211 GENX(panfrost_blendable_format_from_pipe_format)(enum pipe_format f)
212 {
213 return &GENX(panfrost_blendable_formats)[f];
214 }
215
216 #if PAN_ARCH >= 6
217 static inline unsigned
GENX(panfrost_dithered_format_from_pipe_format)218 GENX(panfrost_dithered_format_from_pipe_format)(enum pipe_format f, bool dithered)
219 {
220 mali_pixel_format pixfmt =
221 GENX(panfrost_blendable_formats)[f].bifrost[dithered];
222
223 /* Formats requiring blend shaders are stored raw in the tilebuffer and will
224 * have 0 as their pixel format. Assumes dithering is set, I don't know of a
225 * case when it makes sense to turn off dithering. */
226 return pixfmt ?: GENX(panfrost_format_from_pipe_format)(f)->hw;
227 }
228 #endif
229 #endif
230
231 #endif
232