1/*
2 * Copyright (C) 2012 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 * Authors:
24 *    Rob Clark <robclark@freedesktop.org>
25 */
26
27#include "pipe/p_defines.h"
28#include "util/u_format.h"
29
30#include "fd2_util.h"
31
32enum a2xx_sq_surfaceformat
33fd2_pipe2surface(enum pipe_format format)
34{
35	switch (format) {
36	/* 8-bit buffers. */
37	case PIPE_FORMAT_A8_UNORM:
38	case PIPE_FORMAT_A8_SNORM:
39	case PIPE_FORMAT_A8_UINT:
40	case PIPE_FORMAT_A8_SINT:
41	case PIPE_FORMAT_I8_UNORM:
42	case PIPE_FORMAT_I8_SNORM:
43	case PIPE_FORMAT_I8_UINT:
44	case PIPE_FORMAT_I8_SINT:
45	case PIPE_FORMAT_L8_UNORM:
46	case PIPE_FORMAT_L8_SNORM:
47	case PIPE_FORMAT_L8_UINT:
48	case PIPE_FORMAT_L8_SINT:
49	case PIPE_FORMAT_L8_SRGB:
50	case PIPE_FORMAT_R8_UNORM:
51	case PIPE_FORMAT_R8_SNORM:
52	case PIPE_FORMAT_R8_UINT:
53	case PIPE_FORMAT_R8_SINT:
54		return FMT_8;
55
56	/* 16-bit buffers. */
57	case PIPE_FORMAT_B5G6R5_UNORM:
58		return FMT_5_6_5;
59	case PIPE_FORMAT_B5G5R5A1_UNORM:
60	case PIPE_FORMAT_B5G5R5X1_UNORM:
61		return FMT_1_5_5_5;
62	case PIPE_FORMAT_B4G4R4A4_UNORM:
63	case PIPE_FORMAT_B4G4R4X4_UNORM:
64		return FMT_4_4_4_4;
65	case PIPE_FORMAT_Z16_UNORM:
66		return FMT_16;
67	case PIPE_FORMAT_L8A8_UNORM:
68	case PIPE_FORMAT_L8A8_SNORM:
69	case PIPE_FORMAT_L8A8_UINT:
70	case PIPE_FORMAT_L8A8_SINT:
71	case PIPE_FORMAT_L8A8_SRGB:
72	case PIPE_FORMAT_R8G8_UNORM:
73	case PIPE_FORMAT_R8G8_SNORM:
74	case PIPE_FORMAT_R8G8_UINT:
75	case PIPE_FORMAT_R8G8_SINT:
76		return FMT_8_8;
77	case PIPE_FORMAT_R16_UNORM:
78	case PIPE_FORMAT_R16_SNORM:
79	case PIPE_FORMAT_R16_UINT:
80	case PIPE_FORMAT_R16_SINT:
81	case PIPE_FORMAT_A16_UNORM:
82	case PIPE_FORMAT_A16_SNORM:
83	case PIPE_FORMAT_A16_UINT:
84	case PIPE_FORMAT_A16_SINT:
85	case PIPE_FORMAT_L16_UNORM:
86	case PIPE_FORMAT_L16_SNORM:
87	case PIPE_FORMAT_L16_UINT:
88	case PIPE_FORMAT_L16_SINT:
89	case PIPE_FORMAT_I16_UNORM:
90	case PIPE_FORMAT_I16_SNORM:
91	case PIPE_FORMAT_I16_UINT:
92	case PIPE_FORMAT_I16_SINT:
93		return FMT_16;
94	case PIPE_FORMAT_R16_FLOAT:
95	case PIPE_FORMAT_A16_FLOAT:
96	case PIPE_FORMAT_L16_FLOAT:
97	case PIPE_FORMAT_I16_FLOAT:
98		return FMT_16_FLOAT;
99
100	/* 32-bit buffers. */
101	case PIPE_FORMAT_A8B8G8R8_SRGB:
102	case PIPE_FORMAT_A8B8G8R8_UNORM:
103	case PIPE_FORMAT_A8R8G8B8_UNORM:
104	case PIPE_FORMAT_B8G8R8A8_SRGB:
105	case PIPE_FORMAT_B8G8R8A8_UNORM:
106	case PIPE_FORMAT_B8G8R8X8_UNORM:
107	case PIPE_FORMAT_R8G8B8A8_SNORM:
108	case PIPE_FORMAT_R8G8B8A8_UNORM:
109	case PIPE_FORMAT_R8G8B8X8_UNORM:
110	case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
111	case PIPE_FORMAT_X8B8G8R8_UNORM:
112	case PIPE_FORMAT_X8R8G8B8_UNORM:
113	case PIPE_FORMAT_R8G8B8_UNORM:
114	case PIPE_FORMAT_R8G8B8A8_SINT:
115	case PIPE_FORMAT_R8G8B8A8_UINT:
116		return FMT_8_8_8_8;
117	case PIPE_FORMAT_R10G10B10A2_UNORM:
118	case PIPE_FORMAT_R10G10B10X2_SNORM:
119	case PIPE_FORMAT_B10G10R10A2_UNORM:
120	case PIPE_FORMAT_B10G10R10A2_UINT:
121	case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
122		return FMT_2_10_10_10;
123	case PIPE_FORMAT_Z24X8_UNORM:
124	case PIPE_FORMAT_Z24_UNORM_S8_UINT:
125		return FMT_24_8;
126	case PIPE_FORMAT_R32_UINT:
127	case PIPE_FORMAT_R32_SINT:
128	case PIPE_FORMAT_A32_UINT:
129	case PIPE_FORMAT_A32_SINT:
130	case PIPE_FORMAT_L32_UINT:
131	case PIPE_FORMAT_L32_SINT:
132	case PIPE_FORMAT_I32_UINT:
133	case PIPE_FORMAT_I32_SINT:
134		return FMT_32;
135	case PIPE_FORMAT_R32_FLOAT:
136	case PIPE_FORMAT_A32_FLOAT:
137	case PIPE_FORMAT_L32_FLOAT:
138	case PIPE_FORMAT_I32_FLOAT:
139	case PIPE_FORMAT_Z32_FLOAT:
140		return FMT_32_FLOAT;
141	case PIPE_FORMAT_R16G16_FLOAT:
142	case PIPE_FORMAT_L16A16_FLOAT:
143		return FMT_16_16_FLOAT;
144	case PIPE_FORMAT_R16G16_UNORM:
145	case PIPE_FORMAT_R16G16_SNORM:
146	case PIPE_FORMAT_R16G16_UINT:
147	case PIPE_FORMAT_R16G16_SINT:
148	case PIPE_FORMAT_L16A16_UNORM:
149	case PIPE_FORMAT_L16A16_SNORM:
150	case PIPE_FORMAT_L16A16_UINT:
151	case PIPE_FORMAT_L16A16_SINT:
152		return FMT_16_16;
153
154	/* 64-bit buffers. */
155	case PIPE_FORMAT_R16G16B16A16_UINT:
156	case PIPE_FORMAT_R16G16B16A16_SINT:
157	case PIPE_FORMAT_R16G16B16A16_UNORM:
158	case PIPE_FORMAT_R16G16B16A16_SNORM:
159		return FMT_16_16_16_16;
160	case PIPE_FORMAT_R16G16B16A16_FLOAT:
161		return FMT_16_16_16_16_FLOAT;
162	case PIPE_FORMAT_R32G32_FLOAT:
163	case PIPE_FORMAT_L32A32_FLOAT:
164		return FMT_32_32_FLOAT;
165	case PIPE_FORMAT_R32G32_SINT:
166	case PIPE_FORMAT_R32G32_UINT:
167	case PIPE_FORMAT_L32A32_UINT:
168	case PIPE_FORMAT_L32A32_SINT:
169		return FMT_32_32;
170
171	/* 96-bit buffers. */
172	case PIPE_FORMAT_R32G32B32_FLOAT:
173		return FMT_32_32_32_FLOAT;
174
175	/* 128-bit buffers. */
176	case PIPE_FORMAT_R32G32B32A32_SNORM:
177	case PIPE_FORMAT_R32G32B32A32_UNORM:
178	case PIPE_FORMAT_R32G32B32A32_SINT:
179	case PIPE_FORMAT_R32G32B32A32_UINT:
180		return FMT_32_32_32_32;
181	case PIPE_FORMAT_R32G32B32A32_FLOAT:
182		return FMT_32_32_32_32_FLOAT;
183
184	/* Compressed textures. */
185	case PIPE_FORMAT_ETC1_RGB8:
186		return FMT_ETC1_RGB;
187	case PIPE_FORMAT_DXT1_RGB:
188	case PIPE_FORMAT_DXT1_RGBA:
189		return FMT_DXT1;
190	case PIPE_FORMAT_DXT3_RGBA:
191		return FMT_DXT2_3;
192	case PIPE_FORMAT_DXT5_RGBA:
193		return FMT_DXT4_5;
194	case PIPE_FORMAT_ATC_RGB:
195		return FMT_ATI_TC_555_565_RGB;
196	case PIPE_FORMAT_ATC_RGBA_EXPLICIT:
197		return FMT_ATI_TC_555_565_RGBA;
198	case PIPE_FORMAT_ATC_RGBA_INTERPOLATED:
199		return FMT_ATI_TC_555_565_RGBA_INTERP;
200
201	/* YUV buffers. */
202	case PIPE_FORMAT_UYVY:
203		return FMT_Cr_Y1_Cb_Y0;
204	case PIPE_FORMAT_YUYV:
205		return FMT_Y1_Cr_Y0_Cb;
206
207	default:
208		return ~0;
209	}
210}
211
212enum a2xx_colorformatx
213fd2_pipe2color(enum pipe_format format)
214{
215	switch (format) {
216	/* 8-bit buffers. */
217	case PIPE_FORMAT_A8_UNORM:
218	case PIPE_FORMAT_A8_SNORM:
219	case PIPE_FORMAT_A8_UINT:
220	case PIPE_FORMAT_A8_SINT:
221	case PIPE_FORMAT_I8_UNORM:
222	case PIPE_FORMAT_I8_SNORM:
223	case PIPE_FORMAT_I8_UINT:
224	case PIPE_FORMAT_I8_SINT:
225	case PIPE_FORMAT_L8_UNORM:
226	case PIPE_FORMAT_L8_SNORM:
227	case PIPE_FORMAT_L8_UINT:
228	case PIPE_FORMAT_L8_SINT:
229	case PIPE_FORMAT_L8_SRGB:
230	case PIPE_FORMAT_R8_UNORM:
231	case PIPE_FORMAT_R8_SNORM:
232	case PIPE_FORMAT_R8_UINT:
233	case PIPE_FORMAT_R8_SINT:
234		return COLORX_8;
235
236	/* 16-bit buffers. */
237	case PIPE_FORMAT_B5G6R5_UNORM:
238		return COLORX_5_6_5;
239	case PIPE_FORMAT_B5G5R5A1_UNORM:
240	case PIPE_FORMAT_B5G5R5X1_UNORM:
241		return COLORX_1_5_5_5;
242	case PIPE_FORMAT_B4G4R4A4_UNORM:
243	case PIPE_FORMAT_B4G4R4X4_UNORM:
244		return COLORX_4_4_4_4;
245	case PIPE_FORMAT_L8A8_UNORM:
246	case PIPE_FORMAT_L8A8_SNORM:
247	case PIPE_FORMAT_L8A8_UINT:
248	case PIPE_FORMAT_L8A8_SINT:
249	case PIPE_FORMAT_L8A8_SRGB:
250	case PIPE_FORMAT_R8G8_UNORM:
251	case PIPE_FORMAT_R8G8_SNORM:
252	case PIPE_FORMAT_R8G8_UINT:
253	case PIPE_FORMAT_R8G8_SINT:
254	case PIPE_FORMAT_Z16_UNORM:
255		return COLORX_8_8;
256	case PIPE_FORMAT_R16_FLOAT:
257	case PIPE_FORMAT_A16_FLOAT:
258	case PIPE_FORMAT_L16_FLOAT:
259	case PIPE_FORMAT_I16_FLOAT:
260		return COLORX_16_FLOAT;
261
262	/* 32-bit buffers. */
263	case PIPE_FORMAT_A8B8G8R8_SRGB:
264	case PIPE_FORMAT_A8B8G8R8_UNORM:
265	case PIPE_FORMAT_A8R8G8B8_UNORM:
266	case PIPE_FORMAT_B8G8R8A8_SRGB:
267	case PIPE_FORMAT_B8G8R8A8_UNORM:
268	case PIPE_FORMAT_B8G8R8X8_UNORM:
269	case PIPE_FORMAT_R8G8B8A8_SNORM:
270	case PIPE_FORMAT_R8G8B8A8_UNORM:
271	case PIPE_FORMAT_R8G8B8X8_UNORM:
272	case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
273	case PIPE_FORMAT_X8B8G8R8_UNORM:
274	case PIPE_FORMAT_X8R8G8B8_UNORM:
275	case PIPE_FORMAT_R8G8B8_UNORM:
276	case PIPE_FORMAT_R8G8B8A8_SINT:
277	case PIPE_FORMAT_R8G8B8A8_UINT:
278	case PIPE_FORMAT_Z24_UNORM_S8_UINT:
279	case PIPE_FORMAT_Z24X8_UNORM:
280		return COLORX_8_8_8_8;
281	case PIPE_FORMAT_R32_FLOAT:
282	case PIPE_FORMAT_A32_FLOAT:
283	case PIPE_FORMAT_L32_FLOAT:
284	case PIPE_FORMAT_I32_FLOAT:
285	case PIPE_FORMAT_Z32_FLOAT:
286		return COLORX_32_FLOAT;
287	case PIPE_FORMAT_R16G16_FLOAT:
288	case PIPE_FORMAT_L16A16_FLOAT:
289		return COLORX_16_16_FLOAT;
290
291	/* 64-bit buffers. */
292	case PIPE_FORMAT_R16G16B16A16_FLOAT:
293		return COLORX_16_16_16_16_FLOAT;
294	case PIPE_FORMAT_R32G32_FLOAT:
295	case PIPE_FORMAT_L32A32_FLOAT:
296		return COLORX_32_32_FLOAT;
297
298	/* 128-bit buffers. */
299	case PIPE_FORMAT_R32G32B32A32_FLOAT:
300		return COLORX_32_32_32_32_FLOAT;
301
302	default:
303		return ~0;
304	}
305}
306
307static inline enum sq_tex_swiz
308tex_swiz(unsigned swiz)
309{
310	switch (swiz) {
311	default:
312	case PIPE_SWIZZLE_X: return SQ_TEX_X;
313	case PIPE_SWIZZLE_Y: return SQ_TEX_Y;
314	case PIPE_SWIZZLE_Z: return SQ_TEX_Z;
315	case PIPE_SWIZZLE_W: return SQ_TEX_W;
316	case PIPE_SWIZZLE_0: return SQ_TEX_ZERO;
317	case PIPE_SWIZZLE_1: return SQ_TEX_ONE;
318	}
319}
320
321uint32_t
322fd2_tex_swiz(enum pipe_format format, unsigned swizzle_r, unsigned swizzle_g,
323		unsigned swizzle_b, unsigned swizzle_a)
324{
325	const struct util_format_description *desc =
326			util_format_description(format);
327	unsigned char swiz[4] = {
328		swizzle_r, swizzle_g, swizzle_b, swizzle_a,
329	}, rswiz[4];
330
331	util_format_compose_swizzles(desc->swizzle, swiz, rswiz);
332
333	return A2XX_SQ_TEX_3_SWIZ_X(tex_swiz(rswiz[0])) |
334			A2XX_SQ_TEX_3_SWIZ_Y(tex_swiz(rswiz[1])) |
335			A2XX_SQ_TEX_3_SWIZ_Z(tex_swiz(rswiz[2])) |
336			A2XX_SQ_TEX_3_SWIZ_W(tex_swiz(rswiz[3]));
337}
338