panfrost: Add support for B5G5R5X1
[mesa.git] / src / panfrost / encoder / pan_format.c
1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27 #include <stdio.h>
28 #include "panfrost-job.h"
29 #include "pan_texture.h"
30
31 static unsigned
32 panfrost_translate_channel_width(unsigned size)
33 {
34 switch (size) {
35 case 4:
36 return MALI_CHANNEL_4;
37 case 8:
38 return MALI_CHANNEL_8;
39 case 16:
40 return MALI_CHANNEL_16;
41 case 32:
42 return MALI_CHANNEL_32;
43 default:
44 unreachable("Invalid format width\n");
45 }
46 }
47
48 static unsigned
49 panfrost_translate_channel_type(unsigned type, unsigned size, bool norm)
50 {
51 switch (type) {
52 case UTIL_FORMAT_TYPE_UNSIGNED:
53 return norm ? MALI_FORMAT_UNORM : MALI_FORMAT_UINT;
54
55 case UTIL_FORMAT_TYPE_SIGNED:
56 return norm ? MALI_FORMAT_SNORM : MALI_FORMAT_SINT;
57
58 case UTIL_FORMAT_TYPE_FLOAT:
59 /* fp16 -- SINT, fp32 -- UNORM ... gotta use those bits */
60
61 if (size == 16)
62 return MALI_FORMAT_SINT;
63 else if (size == 32)
64 return MALI_FORMAT_UNORM;
65 else
66 unreachable("Invalid float size");
67
68 default:
69 unreachable("Invalid type");
70 }
71 }
72
73 /* Constructs a mali_format satisfying the specified Gallium format
74 * description */
75
76 enum mali_format
77 panfrost_find_format(const struct util_format_description *desc)
78 {
79 /* Find first non-VOID channel */
80 struct util_format_channel_description chan = desc->channel[0];
81
82 for (unsigned c = 0; c < 4; ++c)
83 {
84 if (desc->channel[c].type == UTIL_FORMAT_TYPE_VOID)
85 continue;
86
87 chan = desc->channel[c];
88 break;
89 }
90
91 /* Check for special formats */
92 switch (desc->format)
93 {
94 case PIPE_FORMAT_NV12:
95 return MALI_NV12;
96
97 case PIPE_FORMAT_R10G10B10X2_UNORM:
98 case PIPE_FORMAT_B10G10R10X2_UNORM:
99 case PIPE_FORMAT_R10G10B10A2_UNORM:
100 case PIPE_FORMAT_B10G10R10A2_UNORM:
101 return MALI_RGB10_A2_UNORM;
102
103 case PIPE_FORMAT_R10G10B10X2_SNORM:
104 case PIPE_FORMAT_R10G10B10A2_SNORM:
105 case PIPE_FORMAT_B10G10R10A2_SNORM:
106 return MALI_RGB10_A2_SNORM;
107
108 case PIPE_FORMAT_R10G10B10A2_UINT:
109 case PIPE_FORMAT_B10G10R10A2_UINT:
110 case PIPE_FORMAT_R10G10B10A2_USCALED:
111 case PIPE_FORMAT_B10G10R10A2_USCALED:
112 return MALI_RGB10_A2UI;
113
114 case PIPE_FORMAT_R10G10B10A2_SSCALED:
115 case PIPE_FORMAT_B10G10R10A2_SSCALED:
116 return MALI_RGB10_A2I;
117
118 case PIPE_FORMAT_Z32_UNORM:
119 case PIPE_FORMAT_Z24X8_UNORM:
120 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
121 return MALI_Z32_UNORM;
122
123 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
124 /* Z32F = R32F to the hardware */
125 return MALI_R32F;
126
127 case PIPE_FORMAT_B5G6R5_UNORM:
128 return MALI_RGB565;
129
130 case PIPE_FORMAT_B5G5R5X1_UNORM:
131 return MALI_RGB5_X1_UNORM;
132
133 case PIPE_FORMAT_B5G5R5A1_UNORM:
134 return MALI_RGB5_A1_UNORM;
135
136 case PIPE_FORMAT_A1B5G5R5_UNORM:
137 case PIPE_FORMAT_X1B5G5R5_UNORM:
138 /* Not supported - this is backwards from OpenGL! */
139 assert(0);
140 break;
141
142 case PIPE_FORMAT_R32_FIXED:
143 return MALI_R32_FIXED;
144 case PIPE_FORMAT_R32G32_FIXED:
145 return MALI_RG32_FIXED;
146 case PIPE_FORMAT_R32G32B32_FIXED:
147 return MALI_RGB32_FIXED;
148 case PIPE_FORMAT_R32G32B32A32_FIXED:
149 return MALI_RGBA32_FIXED;
150
151 case PIPE_FORMAT_R11G11B10_FLOAT:
152 return MALI_R11F_G11F_B10F;
153 case PIPE_FORMAT_R9G9B9E5_FLOAT:
154 return MALI_R9F_G9F_B9F_E5F;
155
156 case PIPE_FORMAT_ETC1_RGB8:
157 case PIPE_FORMAT_ETC2_RGB8:
158 case PIPE_FORMAT_ETC2_SRGB8:
159 return MALI_ETC2_RGB8;
160
161 case PIPE_FORMAT_ETC2_RGB8A1:
162 case PIPE_FORMAT_ETC2_SRGB8A1:
163 return MALI_ETC2_RGB8A1;
164
165 case PIPE_FORMAT_ETC2_RGBA8:
166 case PIPE_FORMAT_ETC2_SRGBA8:
167 return MALI_ETC2_RGBA8;
168
169 case PIPE_FORMAT_ETC2_R11_UNORM:
170 return MALI_ETC2_R11_UNORM;
171 case PIPE_FORMAT_ETC2_R11_SNORM:
172 return MALI_ETC2_R11_SNORM;
173
174 case PIPE_FORMAT_ETC2_RG11_UNORM:
175 return MALI_ETC2_RG11_UNORM;
176 case PIPE_FORMAT_ETC2_RG11_SNORM:
177 return MALI_ETC2_RG11_SNORM;
178
179 default:
180 /* Fallthrough to default */
181 break;
182 }
183
184 if (desc->layout == UTIL_FORMAT_LAYOUT_ASTC) {
185 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
186 return MALI_ASTC_SRGB_SUPP;
187 else
188 return MALI_ASTC_HDR_SUPP;
189 }
190
191 /* Formats must match in channel count */
192 assert(desc->nr_channels >= 1 && desc->nr_channels <= 4);
193 unsigned format = MALI_NR_CHANNELS(desc->nr_channels);
194
195 switch (chan.type)
196 {
197 case UTIL_FORMAT_TYPE_UNSIGNED:
198 case UTIL_FORMAT_TYPE_SIGNED:
199 case UTIL_FORMAT_TYPE_FIXED:
200 /* Channel width */
201 format |= panfrost_translate_channel_width(chan.size);
202
203 /* Channel type */
204 format |= panfrost_translate_channel_type(chan.type, chan.size, chan.normalized);
205 break;
206
207 case UTIL_FORMAT_TYPE_FLOAT:
208 /* Float formats use a special width and encode width
209 * with type mixed */
210
211 format |= MALI_CHANNEL_FLOAT;
212 format |= panfrost_translate_channel_type(chan.type, chan.size, chan.normalized);
213 break;
214
215 default:
216 fprintf(stderr, "%s\n", util_format_name(desc->format));
217 unreachable("Invalid format type");
218 }
219
220 return (enum mali_format) format;
221 }
222
223 /* Is a format encoded like Z24S8 and therefore compatible for render? */
224
225 bool
226 panfrost_is_z24s8_variant(enum pipe_format fmt)
227 {
228 switch (fmt) {
229 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
230 case PIPE_FORMAT_Z24X8_UNORM:
231 return true;
232 default:
233 return false;
234 }
235 }
236
237 /* Translate a PIPE swizzle quad to a 12-bit Mali swizzle code. PIPE
238 * swizzles line up with Mali swizzles for the XYZW01, but PIPE swizzles have
239 * an additional "NONE" field that we have to mask out to zero. Additionally,
240 * PIPE swizzles are sparse but Mali swizzles are packed */
241
242 unsigned
243 panfrost_translate_swizzle_4(const unsigned char swizzle[4])
244 {
245 unsigned out = 0;
246
247 for (unsigned i = 0; i < 4; ++i) {
248 unsigned translated = (swizzle[i] > PIPE_SWIZZLE_1) ? PIPE_SWIZZLE_0 : swizzle[i];
249 out |= (translated << (3*i));
250 }
251
252 return out;
253 }
254
255 void
256 panfrost_invert_swizzle(const unsigned char *in, unsigned char *out)
257 {
258 /* First, default to all zeroes to prevent uninitialized junk */
259
260 for (unsigned c = 0; c < 4; ++c)
261 out[c] = PIPE_SWIZZLE_0;
262
263 /* Now "do" what the swizzle says */
264
265 for (unsigned c = 0; c < 4; ++c) {
266 unsigned char i = in[c];
267
268 /* Who cares? */
269 assert(PIPE_SWIZZLE_X == 0);
270 if (i > PIPE_SWIZZLE_W)
271 continue;
272
273 /* Invert */
274 unsigned idx = i - PIPE_SWIZZLE_X;
275 out[idx] = PIPE_SWIZZLE_X + c;
276 }
277 }