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