1 /*
2 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
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 */
24
25 #include "pipe/p_defines.h"
26 #include "util/format/u_format.h"
27
28 #include "fd3_format.h"
29
30 /* Specifies the table of all the formats and their features. Also supplies
31 * the helpers that look up various data in those tables.
32 */
33
34 struct fd3_format {
35 enum a3xx_vtx_fmt vtx;
36 enum a3xx_tex_fmt tex;
37 enum a3xx_color_fmt rb;
38 enum a3xx_color_swap swap;
39 boolean present;
40 };
41
42 /* vertex + texture */
43 #define VT(pipe, fmt, rbfmt, swapfmt) \
44 [PIPE_FORMAT_ ## pipe] = { \
45 .present = 1, \
46 .vtx = VFMT_ ## fmt, \
47 .tex = TFMT_ ## fmt, \
48 .rb = RB_ ## rbfmt, \
49 .swap = swapfmt \
50 }
51
52 /* texture-only */
53 #define _T(pipe, fmt, rbfmt, swapfmt) \
54 [PIPE_FORMAT_ ## pipe] = { \
55 .present = 1, \
56 .vtx = VFMT_NONE, \
57 .tex = TFMT_ ## fmt, \
58 .rb = RB_ ## rbfmt, \
59 .swap = swapfmt \
60 }
61
62 /* vertex-only */
63 #define V_(pipe, fmt, rbfmt, swapfmt) \
64 [PIPE_FORMAT_ ## pipe] = { \
65 .present = 1, \
66 .vtx = VFMT_ ## fmt, \
67 .tex = TFMT_NONE, \
68 .rb = RB_ ## rbfmt, \
69 .swap = swapfmt \
70 }
71
72 static struct fd3_format formats[PIPE_FORMAT_COUNT] = {
73 /* 8-bit */
74 VT(R8_UNORM, 8_UNORM, R8_UNORM, WZYX),
75 VT(R8_SNORM, 8_SNORM, NONE, WZYX),
76 VT(R8_UINT, 8_UINT, NONE, WZYX),
77 VT(R8_SINT, 8_SINT, NONE, WZYX),
78 V_(R8_USCALED, 8_UINT, NONE, WZYX),
79 V_(R8_SSCALED, 8_SINT, NONE, WZYX),
80
81 _T(A8_UNORM, 8_UNORM, A8_UNORM, WZYX),
82 _T(L8_UNORM, 8_UNORM, R8_UNORM, WZYX),
83 _T(I8_UNORM, 8_UNORM, NONE, WZYX),
84
85 _T(A8_UINT, 8_UINT, NONE, WZYX),
86 _T(A8_SINT, 8_SINT, NONE, WZYX),
87 _T(L8_UINT, 8_UINT, NONE, WZYX),
88 _T(L8_SINT, 8_SINT, NONE, WZYX),
89 _T(I8_UINT, 8_UINT, NONE, WZYX),
90 _T(I8_SINT, 8_SINT, NONE, WZYX),
91
92 _T(S8_UINT, 8_UINT, R8_UNORM, WZYX),
93
94 /* 16-bit */
95 VT(R16_UNORM, 16_UNORM, NONE, WZYX),
96 VT(R16_SNORM, 16_SNORM, NONE, WZYX),
97 VT(R16_UINT, 16_UINT, R16_UINT, WZYX),
98 VT(R16_SINT, 16_SINT, R16_SINT, WZYX),
99 V_(R16_USCALED, 16_UINT, NONE, WZYX),
100 V_(R16_SSCALED, 16_SINT, NONE, WZYX),
101 VT(R16_FLOAT, 16_FLOAT, R16_FLOAT,WZYX),
102
103 _T(A16_UINT, 16_UINT, NONE, WZYX),
104 _T(A16_SINT, 16_SINT, NONE, WZYX),
105 _T(L16_UINT, 16_UINT, NONE, WZYX),
106 _T(L16_SINT, 16_SINT, NONE, WZYX),
107 _T(I16_UINT, 16_UINT, NONE, WZYX),
108 _T(I16_SINT, 16_SINT, NONE, WZYX),
109
110 VT(R8G8_UNORM, 8_8_UNORM, R8G8_UNORM, WZYX),
111 VT(R8G8_SNORM, 8_8_SNORM, R8G8_SNORM, WZYX),
112 VT(R8G8_UINT, 8_8_UINT, R8G8_UINT, WZYX),
113 VT(R8G8_SINT, 8_8_SINT, R8G8_SINT, WZYX),
114 V_(R8G8_USCALED, 8_8_UINT, NONE, WZYX),
115 V_(R8G8_SSCALED, 8_8_SINT, NONE, WZYX),
116
117 _T(L8A8_UINT, 8_8_UINT, NONE, WZYX),
118 _T(L8A8_SINT, 8_8_SINT, NONE, WZYX),
119
120 _T(Z16_UNORM, Z16_UNORM, R8G8_UNORM, WZYX),
121 _T(B5G6R5_UNORM, 5_6_5_UNORM, R5G6B5_UNORM, WXYZ),
122 _T(B5G5R5A1_UNORM, 5_5_5_1_UNORM, R5G5B5A1_UNORM, WXYZ),
123 _T(B5G5R5X1_UNORM, 5_5_5_1_UNORM, R5G5B5A1_UNORM, WXYZ),
124 _T(B4G4R4A4_UNORM, 4_4_4_4_UNORM, R4G4B4A4_UNORM, WXYZ),
125
126 /* 24-bit */
127 V_(R8G8B8_UNORM, 8_8_8_UNORM, NONE, WZYX),
128 V_(R8G8B8_SNORM, 8_8_8_SNORM, NONE, WZYX),
129 V_(R8G8B8_UINT, 8_8_8_UINT, NONE, WZYX),
130 V_(R8G8B8_SINT, 8_8_8_SINT, NONE, WZYX),
131 V_(R8G8B8_USCALED, 8_8_8_UINT, NONE, WZYX),
132 V_(R8G8B8_SSCALED, 8_8_8_SINT, NONE, WZYX),
133
134 /* 32-bit */
135 VT(R32_UINT, 32_UINT, R32_UINT, WZYX),
136 VT(R32_SINT, 32_SINT, R32_SINT, WZYX),
137 V_(R32_USCALED, 32_UINT, NONE, WZYX),
138 V_(R32_SSCALED, 32_SINT, NONE, WZYX),
139 VT(R32_FLOAT, 32_FLOAT, R32_FLOAT,WZYX),
140 V_(R32_FIXED, 32_FIXED, NONE, WZYX),
141
142 _T(A32_UINT, 32_UINT, NONE, WZYX),
143 _T(A32_SINT, 32_SINT, NONE, WZYX),
144 _T(L32_UINT, 32_UINT, NONE, WZYX),
145 _T(L32_SINT, 32_SINT, NONE, WZYX),
146 _T(I32_UINT, 32_UINT, NONE, WZYX),
147 _T(I32_SINT, 32_SINT, NONE, WZYX),
148
149 VT(R16G16_UNORM, 16_16_UNORM, NONE, WZYX),
150 VT(R16G16_SNORM, 16_16_SNORM, NONE, WZYX),
151 VT(R16G16_UINT, 16_16_UINT, R16G16_UINT, WZYX),
152 VT(R16G16_SINT, 16_16_SINT, R16G16_SINT, WZYX),
153 V_(R16G16_USCALED, 16_16_UINT, NONE, WZYX),
154 V_(R16G16_SSCALED, 16_16_SINT, NONE, WZYX),
155 VT(R16G16_FLOAT, 16_16_FLOAT, R16G16_FLOAT,WZYX),
156
157 _T(L16A16_UINT, 16_16_UINT, NONE, WZYX),
158 _T(L16A16_SINT, 16_16_SINT, NONE, WZYX),
159
160 VT(R8G8B8A8_UNORM, 8_8_8_8_UNORM, R8G8B8A8_UNORM, WZYX),
161 _T(R8G8B8X8_UNORM, 8_8_8_8_UNORM, R8G8B8A8_UNORM, WZYX),
162 _T(R8G8B8A8_SRGB, 8_8_8_8_UNORM, R8G8B8A8_UNORM, WZYX),
163 _T(R8G8B8X8_SRGB, 8_8_8_8_UNORM, R8G8B8A8_UNORM, WZYX),
164 VT(R8G8B8A8_SNORM, 8_8_8_8_SNORM, R8G8B8A8_SNORM, WZYX),
165 VT(R8G8B8A8_UINT, 8_8_8_8_UINT, R8G8B8A8_UINT, WZYX),
166 VT(R8G8B8A8_SINT, 8_8_8_8_SINT, R8G8B8A8_SINT, WZYX),
167 V_(R8G8B8A8_USCALED, 8_8_8_8_UINT, NONE, WZYX),
168 V_(R8G8B8A8_SSCALED, 8_8_8_8_SINT, NONE, WZYX),
169
170 VT(B8G8R8A8_UNORM, 8_8_8_8_UNORM, R8G8B8A8_UNORM, WXYZ),
171 _T(B8G8R8X8_UNORM, 8_8_8_8_UNORM, R8G8B8A8_UNORM, WXYZ),
172 VT(B8G8R8A8_SRGB, 8_8_8_8_UNORM, R8G8B8A8_UNORM, WXYZ),
173 _T(B8G8R8X8_SRGB, 8_8_8_8_UNORM, R8G8B8A8_UNORM, WXYZ),
174
175 VT(A8B8G8R8_UNORM, 8_8_8_8_UNORM, R8G8B8A8_UNORM, XYZW),
176 _T(X8B8G8R8_UNORM, 8_8_8_8_UNORM, R8G8B8A8_UNORM, XYZW),
177 _T(A8B8G8R8_SRGB, 8_8_8_8_UNORM, R8G8B8A8_UNORM, XYZW),
178 _T(X8B8G8R8_SRGB, 8_8_8_8_UNORM, R8G8B8A8_UNORM, XYZW),
179
180 VT(A8R8G8B8_UNORM, 8_8_8_8_UNORM, R8G8B8A8_UNORM, ZYXW),
181 _T(X8R8G8B8_UNORM, 8_8_8_8_UNORM, R8G8B8A8_UNORM, ZYXW),
182 _T(A8R8G8B8_SRGB, 8_8_8_8_UNORM, R8G8B8A8_UNORM, ZYXW),
183 _T(X8R8G8B8_SRGB, 8_8_8_8_UNORM, R8G8B8A8_UNORM, ZYXW),
184
185 VT(R10G10B10A2_UNORM, 10_10_10_2_UNORM, R10G10B10A2_UNORM, WZYX),
186 VT(B10G10R10A2_UNORM, 10_10_10_2_UNORM, R10G10B10A2_UNORM, WXYZ),
187 _T(B10G10R10X2_UNORM, 10_10_10_2_UNORM, R10G10B10A2_UNORM, WXYZ),
188 V_(R10G10B10A2_SNORM, 10_10_10_2_SNORM, NONE, WZYX),
189 V_(B10G10R10A2_SNORM, 10_10_10_2_SNORM, NONE, WXYZ),
190 VT(R10G10B10A2_UINT, 10_10_10_2_UINT, NONE, WZYX),
191 V_(B10G10R10A2_UINT, 10_10_10_2_UINT, NONE, WXYZ),
192 V_(R10G10B10A2_USCALED, 10_10_10_2_UINT, NONE, WZYX),
193 V_(B10G10R10A2_USCALED, 10_10_10_2_UINT, NONE, WXYZ),
194 V_(R10G10B10A2_SSCALED, 10_10_10_2_SINT, NONE, WZYX),
195 V_(B10G10R10A2_SSCALED, 10_10_10_2_SINT, NONE, WXYZ),
196
197 _T(R11G11B10_FLOAT, 11_11_10_FLOAT, R11G11B10_FLOAT, WZYX),
198 _T(R9G9B9E5_FLOAT, 9_9_9_E5_FLOAT, NONE, WZYX),
199
200 _T(Z24X8_UNORM, X8Z24_UNORM, R8G8B8A8_UNORM, WZYX),
201 _T(Z24_UNORM_S8_UINT, X8Z24_UNORM, R8G8B8A8_UNORM, WZYX),
202 _T(Z32_FLOAT, Z32_FLOAT, R8G8B8A8_UNORM, WZYX),
203 _T(Z32_FLOAT_S8X24_UINT, Z32_FLOAT,R8G8B8A8_UNORM, WZYX),
204
205 /* 48-bit */
206 V_(R16G16B16_UNORM, 16_16_16_UNORM, NONE, WZYX),
207 V_(R16G16B16_SNORM, 16_16_16_SNORM, NONE, WZYX),
208 V_(R16G16B16_UINT, 16_16_16_UINT, NONE, WZYX),
209 V_(R16G16B16_SINT, 16_16_16_SINT, NONE, WZYX),
210 V_(R16G16B16_USCALED, 16_16_16_UINT, NONE, WZYX),
211 V_(R16G16B16_SSCALED, 16_16_16_SINT, NONE, WZYX),
212 V_(R16G16B16_FLOAT, 16_16_16_FLOAT, NONE, WZYX),
213
214 /* 64-bit */
215 VT(R16G16B16A16_UNORM, 16_16_16_16_UNORM, NONE, WZYX),
216 VT(R16G16B16A16_SNORM, 16_16_16_16_SNORM, NONE, WZYX),
217 VT(R16G16B16A16_UINT, 16_16_16_16_UINT, R16G16B16A16_UINT, WZYX),
218 _T(R16G16B16X16_UINT, 16_16_16_16_UINT, R16G16B16A16_UINT, WZYX),
219 VT(R16G16B16A16_SINT, 16_16_16_16_SINT, R16G16B16A16_SINT, WZYX),
220 _T(R16G16B16X16_SINT, 16_16_16_16_SINT, R16G16B16A16_SINT, WZYX),
221 V_(R16G16B16A16_USCALED, 16_16_16_16_UINT, NONE, WZYX),
222 V_(R16G16B16A16_SSCALED, 16_16_16_16_SINT, NONE, WZYX),
223 VT(R16G16B16A16_FLOAT, 16_16_16_16_FLOAT, R16G16B16A16_FLOAT, WZYX),
224 _T(R16G16B16X16_FLOAT, 16_16_16_16_FLOAT, R16G16B16A16_FLOAT, WZYX),
225
226 VT(R32G32_UINT, 32_32_UINT, R32G32_UINT, WZYX),
227 VT(R32G32_SINT, 32_32_SINT, R32G32_SINT, WZYX),
228 V_(R32G32_USCALED, 32_32_UINT, NONE, WZYX),
229 V_(R32G32_SSCALED, 32_32_SINT, NONE, WZYX),
230 VT(R32G32_FLOAT, 32_32_FLOAT, R32G32_FLOAT,WZYX),
231 V_(R32G32_FIXED, 32_32_FIXED, NONE, WZYX),
232
233 _T(L32A32_UINT, 32_32_UINT, NONE, WZYX),
234 _T(L32A32_SINT, 32_32_SINT, NONE, WZYX),
235
236 /* 96-bit */
237 V_(R32G32B32_UINT, 32_32_32_UINT, NONE, WZYX),
238 V_(R32G32B32_SINT, 32_32_32_SINT, NONE, WZYX),
239 V_(R32G32B32_USCALED, 32_32_32_UINT, NONE, WZYX),
240 V_(R32G32B32_SSCALED, 32_32_32_SINT, NONE, WZYX),
241 V_(R32G32B32_FLOAT, 32_32_32_FLOAT, NONE, WZYX),
242 V_(R32G32B32_FIXED, 32_32_32_FIXED, NONE, WZYX),
243
244 /* 128-bit */
245 VT(R32G32B32A32_UINT, 32_32_32_32_UINT, R32G32B32A32_UINT, WZYX),
246 _T(R32G32B32X32_UINT, 32_32_32_32_UINT, R32G32B32A32_UINT, WZYX),
247 VT(R32G32B32A32_SINT, 32_32_32_32_SINT, R32G32B32A32_SINT, WZYX),
248 _T(R32G32B32X32_SINT, 32_32_32_32_SINT, R32G32B32A32_SINT, WZYX),
249 V_(R32G32B32A32_USCALED, 32_32_32_32_UINT, NONE, WZYX),
250 V_(R32G32B32A32_SSCALED, 32_32_32_32_SINT, NONE, WZYX),
251 VT(R32G32B32A32_FLOAT, 32_32_32_32_FLOAT, R32G32B32A32_FLOAT, WZYX),
252 _T(R32G32B32X32_FLOAT, 32_32_32_32_FLOAT, R32G32B32A32_FLOAT, WZYX),
253 V_(R32G32B32A32_FIXED, 32_32_32_32_FIXED, NONE, WZYX),
254
255 /* compressed */
256 _T(ETC1_RGB8, ETC1, NONE, WZYX),
257 _T(ETC2_RGB8, ETC2_RGB8, NONE, WZYX),
258 _T(ETC2_SRGB8, ETC2_RGB8, NONE, WZYX),
259 _T(ETC2_RGB8A1, ETC2_RGB8A1, NONE, WZYX),
260 _T(ETC2_SRGB8A1, ETC2_RGB8A1, NONE, WZYX),
261 _T(ETC2_RGBA8, ETC2_RGBA8, NONE, WZYX),
262 _T(ETC2_SRGBA8, ETC2_RGBA8, NONE, WZYX),
263 _T(ETC2_R11_UNORM, ETC2_R11_UNORM, NONE, WZYX),
264 _T(ETC2_R11_SNORM, ETC2_R11_SNORM, NONE, WZYX),
265 _T(ETC2_RG11_UNORM, ETC2_RG11_UNORM, NONE, WZYX),
266 _T(ETC2_RG11_SNORM, ETC2_RG11_SNORM, NONE, WZYX),
267
268 _T(DXT1_RGB, DXT1, NONE, WZYX),
269 _T(DXT1_SRGB, DXT1, NONE, WZYX),
270 _T(DXT1_RGBA, DXT1, NONE, WZYX),
271 _T(DXT1_SRGBA, DXT1, NONE, WZYX),
272 _T(DXT3_RGBA, DXT3, NONE, WZYX),
273 _T(DXT3_SRGBA, DXT3, NONE, WZYX),
274 _T(DXT5_RGBA, DXT5, NONE, WZYX),
275 _T(DXT5_SRGBA, DXT5, NONE, WZYX),
276
277 /* faked */
278 _T(RGTC1_UNORM, 8_8_8_8_UNORM, NONE, WZYX),
279 _T(RGTC1_SNORM, 8_8_8_8_SNORM, NONE, WZYX),
280 _T(RGTC2_UNORM, 8_8_8_8_UNORM, NONE, WZYX),
281 _T(RGTC2_SNORM, 8_8_8_8_SNORM, NONE, WZYX),
282 _T(LATC1_UNORM, 8_8_8_8_UNORM, NONE, WZYX),
283 _T(LATC1_SNORM, 8_8_8_8_SNORM, NONE, WZYX),
284 _T(LATC2_UNORM, 8_8_8_8_UNORM, NONE, WZYX),
285 _T(LATC2_SNORM, 8_8_8_8_SNORM, NONE, WZYX),
286
287 _T(ATC_RGB, ATC_RGB, NONE, WZYX),
288 _T(ATC_RGBA_EXPLICIT, ATC_RGBA_EXPLICIT, NONE, WZYX),
289 _T(ATC_RGBA_INTERPOLATED, ATC_RGBA_INTERPOLATED, NONE, WZYX),
290 };
291
292 enum a3xx_vtx_fmt
fd3_pipe2vtx(enum pipe_format format)293 fd3_pipe2vtx(enum pipe_format format)
294 {
295 if (!formats[format].present)
296 return VFMT_NONE;
297 return formats[format].vtx;
298 }
299
300 enum a3xx_tex_fmt
fd3_pipe2tex(enum pipe_format format)301 fd3_pipe2tex(enum pipe_format format)
302 {
303 if (!formats[format].present)
304 return TFMT_NONE;
305 return formats[format].tex;
306 }
307
308 enum a3xx_color_fmt
fd3_pipe2color(enum pipe_format format)309 fd3_pipe2color(enum pipe_format format)
310 {
311 if (!formats[format].present)
312 return RB_NONE;
313 return formats[format].rb;
314 }
315
316 enum a3xx_color_swap
fd3_pipe2swap(enum pipe_format format)317 fd3_pipe2swap(enum pipe_format format)
318 {
319 if (!formats[format].present)
320 return WZYX;
321 return formats[format].swap;
322 }
323
324 enum a3xx_color_fmt
fd3_fs_output_format(enum pipe_format format)325 fd3_fs_output_format(enum pipe_format format)
326 {
327 if (util_format_is_srgb(format))
328 return RB_R16G16B16A16_FLOAT;
329 switch (format) {
330 case PIPE_FORMAT_R16_FLOAT:
331 case PIPE_FORMAT_R16G16_FLOAT:
332 case PIPE_FORMAT_R11G11B10_FLOAT:
333 return RB_R16G16B16A16_FLOAT;
334 case PIPE_FORMAT_L8_UNORM:
335 return RB_R8G8B8A8_UNORM;
336 default:
337 return fd3_pipe2color(format);
338 }
339 }
340
341 static inline enum a3xx_tex_swiz
tex_swiz(unsigned swiz)342 tex_swiz(unsigned swiz)
343 {
344 switch (swiz) {
345 default:
346 case PIPE_SWIZZLE_X: return A3XX_TEX_X;
347 case PIPE_SWIZZLE_Y: return A3XX_TEX_Y;
348 case PIPE_SWIZZLE_Z: return A3XX_TEX_Z;
349 case PIPE_SWIZZLE_W: return A3XX_TEX_W;
350 case PIPE_SWIZZLE_0: return A3XX_TEX_ZERO;
351 case PIPE_SWIZZLE_1: return A3XX_TEX_ONE;
352 }
353 }
354
355 uint32_t
fd3_tex_swiz(enum pipe_format format,unsigned swizzle_r,unsigned swizzle_g,unsigned swizzle_b,unsigned swizzle_a)356 fd3_tex_swiz(enum pipe_format format, unsigned swizzle_r, unsigned swizzle_g,
357 unsigned swizzle_b, unsigned swizzle_a)
358 {
359 const struct util_format_description *desc =
360 util_format_description(format);
361 unsigned char swiz[4] = {
362 swizzle_r, swizzle_g, swizzle_b, swizzle_a,
363 }, rswiz[4];
364
365 util_format_compose_swizzles(desc->swizzle, swiz, rswiz);
366
367 return A3XX_TEX_CONST_0_SWIZ_X(tex_swiz(rswiz[0])) |
368 A3XX_TEX_CONST_0_SWIZ_Y(tex_swiz(rswiz[1])) |
369 A3XX_TEX_CONST_0_SWIZ_Z(tex_swiz(rswiz[2])) |
370 A3XX_TEX_CONST_0_SWIZ_W(tex_swiz(rswiz[3]));
371 }
372