Revert "etnaviv: add support for snorm textures"
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_format.c
1 /*
2 * Copyright (c) 2016 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Christian Gmeiner <christian.gmeiner@gmail.com>
25 */
26
27 #include "etnaviv_format.h"
28
29 #include "hw/state.xml.h"
30 #include "hw/state_3d.xml.h"
31
32 #include "pipe/p_defines.h"
33
34 /* Specifies the table of all the formats and their features. Also supplies
35 * the helpers that look up various data in those tables.
36 */
37
38 struct etna_format {
39 unsigned vtx;
40 unsigned tex;
41 unsigned rs;
42 boolean present;
43 const unsigned char tex_swiz[4];
44 };
45
46 #define RS_FORMAT_NONE ~0
47
48 #define RS_FORMAT_MASK 0xf
49 #define RS_FORMAT(x) ((x) & RS_FORMAT_MASK)
50 #define RS_FORMAT_RB_SWAP 0x10
51
52 #define RS_FORMAT_X8B8G8R8 (RS_FORMAT_X8R8G8B8 | RS_FORMAT_RB_SWAP)
53 #define RS_FORMAT_A8B8G8R8 (RS_FORMAT_A8R8G8B8 | RS_FORMAT_RB_SWAP)
54
55 #define SWIZ(x,y,z,w) { \
56 PIPE_SWIZZLE_##x, \
57 PIPE_SWIZZLE_##y, \
58 PIPE_SWIZZLE_##z, \
59 PIPE_SWIZZLE_##w \
60 }
61
62 /* vertex + texture */
63 #define VT(pipe, vtxfmt, texfmt, texswiz, rsfmt) \
64 [PIPE_FORMAT_##pipe] = { \
65 .vtx = VIVS_FE_VERTEX_ELEMENT_CONFIG_TYPE_##vtxfmt, \
66 .tex = TEXTURE_FORMAT_##texfmt, \
67 .rs = RS_FORMAT_##rsfmt, \
68 .present = 1, \
69 .tex_swiz = texswiz, \
70 }
71
72 /* texture-only */
73 #define _T(pipe, fmt, swiz, rsfmt) \
74 [PIPE_FORMAT_##pipe] = { \
75 .vtx = ETNA_NO_MATCH, \
76 .tex = TEXTURE_FORMAT_##fmt, \
77 .rs = RS_FORMAT_##rsfmt, \
78 .present = 1, \
79 .tex_swiz = swiz, \
80 }
81
82 /* vertex-only */
83 #define V_(pipe, fmt, rsfmt) \
84 [PIPE_FORMAT_##pipe] = { \
85 .vtx = VIVS_FE_VERTEX_ELEMENT_CONFIG_TYPE_##fmt, \
86 .tex = ETNA_NO_MATCH, \
87 .rs = RS_FORMAT_##rsfmt, \
88 .present = 1, \
89 }
90
91 static struct etna_format formats[PIPE_FORMAT_COUNT] = {
92 /* 8-bit */
93 V_(R8_UNORM, UNSIGNED_BYTE, NONE),
94 V_(R8_SNORM, BYTE, NONE),
95 V_(R8_UINT, UNSIGNED_BYTE, NONE),
96 V_(R8_SINT, BYTE, NONE),
97 V_(R8_USCALED, UNSIGNED_BYTE, NONE),
98 V_(R8_SSCALED, BYTE, NONE),
99
100 _T(A8_UNORM, A8, SWIZ(X, Y, Z, W), NONE),
101 _T(L8_UNORM, L8, SWIZ(X, Y, Z, W), NONE),
102 _T(I8_UNORM, I8, SWIZ(X, Y, Z, W), NONE),
103
104 /* 16-bit */
105 V_(R16_UNORM, UNSIGNED_SHORT, NONE),
106 V_(R16_SNORM, SHORT, NONE),
107 V_(R16_UINT, UNSIGNED_SHORT, NONE),
108 V_(R16_SINT, SHORT, NONE),
109 V_(R16_USCALED, UNSIGNED_SHORT, NONE),
110 V_(R16_SSCALED, SHORT, NONE),
111 V_(R16_FLOAT, HALF_FLOAT, NONE),
112
113 _T(B4G4R4A4_UNORM, A4R4G4B4, SWIZ(X, Y, Z, W), A4R4G4B4),
114 _T(B4G4R4X4_UNORM, X4R4G4B4, SWIZ(X, Y, Z, W), X4R4G4B4),
115
116 _T(L8A8_UNORM, A8L8, SWIZ(X, Y, Z, W), NONE),
117
118 _T(Z16_UNORM, D16, SWIZ(X, Y, Z, W), A4R4G4B4),
119 _T(B5G6R5_UNORM, R5G6B5, SWIZ(X, Y, Z, W), R5G6B5),
120 _T(B5G5R5A1_UNORM, A1R5G5B5, SWIZ(X, Y, Z, W), A1R5G5B5),
121 _T(B5G5R5X1_UNORM, X1R5G5B5, SWIZ(X, Y, Z, W), X1R5G5B5),
122
123 VT(R8G8_UNORM, UNSIGNED_BYTE, EXT_G8R8 | EXT_FORMAT, SWIZ(X, Y, 0, 1), NONE),
124 V_(R8G8_SNORM, BYTE, NONE),
125 V_(R8G8_UINT, UNSIGNED_BYTE, NONE),
126 V_(R8G8_SINT, BYTE, NONE),
127 V_(R8G8_USCALED, UNSIGNED_BYTE, NONE),
128 V_(R8G8_SSCALED, BYTE, NONE),
129
130 /* 24-bit */
131 V_(R8G8B8_UNORM, UNSIGNED_BYTE, NONE),
132 V_(R8G8B8_SNORM, BYTE, NONE),
133 V_(R8G8B8_UINT, UNSIGNED_BYTE, NONE),
134 V_(R8G8B8_SINT, BYTE, NONE),
135 V_(R8G8B8_USCALED, UNSIGNED_BYTE, NONE),
136 V_(R8G8B8_SSCALED, BYTE, NONE),
137
138 /* 32-bit */
139 V_(R32_UNORM, UNSIGNED_INT, NONE),
140 V_(R32_SNORM, INT, NONE),
141 V_(R32_SINT, INT, NONE),
142 V_(R32_UINT, UNSIGNED_INT, NONE),
143 V_(R32_USCALED, UNSIGNED_INT, NONE),
144 V_(R32_SSCALED, INT, NONE),
145 V_(R32_FLOAT, FLOAT, NONE),
146 V_(R32_FIXED, FIXED, NONE),
147
148 V_(R16G16_UNORM, UNSIGNED_SHORT, NONE),
149 V_(R16G16_SNORM, SHORT, NONE),
150 V_(R16G16_UINT, UNSIGNED_SHORT, NONE),
151 V_(R16G16_SINT, SHORT, NONE),
152 V_(R16G16_USCALED, UNSIGNED_SHORT, NONE),
153 V_(R16G16_SSCALED, SHORT, NONE),
154 V_(R16G16_FLOAT, HALF_FLOAT, NONE),
155
156 V_(A8B8G8R8_UNORM, UNSIGNED_BYTE, NONE),
157
158 V_(R8G8B8A8_UNORM, UNSIGNED_BYTE, A8B8G8R8),
159 V_(R8G8B8A8_SNORM, BYTE, A8B8G8R8),
160 _T(R8G8B8X8_UNORM, X8B8G8R8, SWIZ(X, Y, Z, W), X8B8G8R8),
161 V_(R8G8B8A8_UINT, UNSIGNED_BYTE, A8B8G8R8),
162 V_(R8G8B8A8_SINT, BYTE, A8B8G8R8),
163 V_(R8G8B8A8_USCALED, UNSIGNED_BYTE, A8B8G8R8),
164 V_(R8G8B8A8_SSCALED, BYTE, A8B8G8R8),
165
166 _T(R8G8B8A8_UNORM, A8B8G8R8, SWIZ(X, Y, Z, W), A8B8G8R8),
167 _T(R8G8B8X8_UNORM, X8B8G8R8, SWIZ(X, Y, Z, W), X8B8G8R8),
168
169 _T(B8G8R8A8_UNORM, A8R8G8B8, SWIZ(X, Y, Z, W), A8R8G8B8),
170 _T(B8G8R8X8_UNORM, X8R8G8B8, SWIZ(X, Y, Z, W), X8R8G8B8),
171
172 V_(R10G10B10A2_UNORM, UNSIGNED_INT_10_10_10_2, NONE),
173 V_(R10G10B10A2_SNORM, INT_10_10_10_2, NONE),
174 V_(R10G10B10A2_USCALED, UNSIGNED_INT_10_10_10_2, NONE),
175 V_(R10G10B10A2_SSCALED, INT_10_10_10_2, NONE),
176
177 _T(X8Z24_UNORM, D24S8, SWIZ(X, Y, Z, W), A8R8G8B8),
178 _T(S8_UINT_Z24_UNORM, D24S8, SWIZ(X, Y, Z, W), A8R8G8B8),
179
180 /* 48-bit */
181 V_(R16G16B16_UNORM, UNSIGNED_SHORT, NONE),
182 V_(R16G16B16_SNORM, SHORT, NONE),
183 V_(R16G16B16_UINT, UNSIGNED_SHORT, NONE),
184 V_(R16G16B16_SINT, SHORT, NONE),
185 V_(R16G16B16_USCALED, UNSIGNED_SHORT, NONE),
186 V_(R16G16B16_SSCALED, SHORT, NONE),
187 V_(R16G16B16_FLOAT, HALF_FLOAT, NONE),
188
189 /* 64-bit */
190 V_(R16G16B16A16_UNORM, UNSIGNED_SHORT, NONE),
191 V_(R16G16B16A16_SNORM, SHORT, NONE),
192 V_(R16G16B16A16_UINT, UNSIGNED_SHORT, NONE),
193 V_(R16G16B16A16_SINT, SHORT, NONE),
194 V_(R16G16B16A16_USCALED, UNSIGNED_SHORT, NONE),
195 V_(R16G16B16A16_SSCALED, SHORT, NONE),
196 V_(R16G16B16A16_FLOAT, HALF_FLOAT, NONE),
197
198 V_(R32G32_UNORM, UNSIGNED_INT, NONE),
199 V_(R32G32_SNORM, INT, NONE),
200 V_(R32G32_UINT, UNSIGNED_INT, NONE),
201 V_(R32G32_SINT, INT, NONE),
202 V_(R32G32_USCALED, UNSIGNED_INT, NONE),
203 V_(R32G32_SSCALED, INT, NONE),
204 V_(R32G32_FLOAT, FLOAT, NONE),
205 V_(R32G32_FIXED, FIXED, NONE),
206
207 /* 96-bit */
208 V_(R32G32B32_UNORM, UNSIGNED_INT, NONE),
209 V_(R32G32B32_SNORM, INT, NONE),
210 V_(R32G32B32_UINT, UNSIGNED_INT, NONE),
211 V_(R32G32B32_SINT, INT, NONE),
212 V_(R32G32B32_USCALED, UNSIGNED_INT, NONE),
213 V_(R32G32B32_SSCALED, INT, NONE),
214 V_(R32G32B32_FLOAT, FLOAT, NONE),
215 V_(R32G32B32_FIXED, FIXED, NONE),
216
217 /* 128-bit */
218 V_(R32G32B32A32_UNORM, UNSIGNED_INT, NONE),
219 V_(R32G32B32A32_SNORM, INT, NONE),
220 V_(R32G32B32A32_UINT, UNSIGNED_INT, NONE),
221 V_(R32G32B32A32_SINT, INT, NONE),
222 V_(R32G32B32A32_USCALED, UNSIGNED_INT, NONE),
223 V_(R32G32B32A32_SSCALED, INT, NONE),
224 V_(R32G32B32A32_FLOAT, FLOAT, NONE),
225 V_(R32G32B32A32_FIXED, FIXED, NONE),
226
227 /* compressed */
228 _T(ETC1_RGB8, ETC1, SWIZ(X, Y, Z, W), NONE),
229
230 _T(DXT1_RGB, DXT1, SWIZ(X, Y, Z, W), NONE),
231 _T(DXT1_RGBA, DXT1, SWIZ(X, Y, Z, W), NONE),
232 _T(DXT3_RGBA, DXT2_DXT3, SWIZ(X, Y, Z, W), NONE),
233 _T(DXT3_RGBA, DXT2_DXT3, SWIZ(X, Y, Z, W), NONE),
234 _T(DXT5_RGBA, DXT4_DXT5, SWIZ(X, Y, Z, W), NONE),
235
236 /* YUV */
237 _T(YUYV, YUY2, SWIZ(X, Y, Z, W), YUY2),
238 _T(UYVY, UYVY, SWIZ(X, Y, Z, W), NONE),
239 };
240
241 uint32_t
242 translate_texture_format(enum pipe_format fmt)
243 {
244 if (!formats[fmt].present)
245 return ETNA_NO_MATCH;
246
247 return formats[fmt].tex;
248 }
249
250 bool
251 texture_format_needs_swiz(enum pipe_format fmt)
252 {
253 static const unsigned char def[4] = SWIZ(X, Y, Z, W);
254 bool swiz = false;
255
256 if (formats[fmt].present)
257 swiz = !memcmp(def, formats[fmt].tex_swiz, sizeof(formats[fmt].tex_swiz));
258
259 return swiz;
260 }
261
262 uint32_t
263 get_texture_swiz(enum pipe_format fmt, unsigned swizzle_r,
264 unsigned swizzle_g, unsigned swizzle_b, unsigned swizzle_a)
265 {
266 unsigned char swiz[4] = {
267 swizzle_r, swizzle_g, swizzle_b, swizzle_a,
268 }, rswiz[4];
269
270 assert(formats[fmt].present);
271 util_format_compose_swizzles(formats[fmt].tex_swiz, swiz, rswiz);
272
273 /* PIPE_SWIZZLE_ maps 1:1 to TEXTURE_SWIZZLE_ */
274 STATIC_ASSERT(PIPE_SWIZZLE_X == TEXTURE_SWIZZLE_RED);
275 STATIC_ASSERT(PIPE_SWIZZLE_Y == TEXTURE_SWIZZLE_GREEN);
276 STATIC_ASSERT(PIPE_SWIZZLE_Z == TEXTURE_SWIZZLE_BLUE);
277 STATIC_ASSERT(PIPE_SWIZZLE_W == TEXTURE_SWIZZLE_ALPHA);
278 STATIC_ASSERT(PIPE_SWIZZLE_0 == TEXTURE_SWIZZLE_ZERO);
279 STATIC_ASSERT(PIPE_SWIZZLE_1 == TEXTURE_SWIZZLE_ONE);
280
281 return VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_R(rswiz[0]) |
282 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_G(rswiz[1]) |
283 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_B(rswiz[2]) |
284 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_A(rswiz[3]);
285 }
286
287 uint32_t
288 translate_rs_format(enum pipe_format fmt)
289 {
290 if (!formats[fmt].present)
291 return ETNA_NO_MATCH;
292
293 if (formats[fmt].rs == ETNA_NO_MATCH)
294 return ETNA_NO_MATCH;
295
296 return RS_FORMAT(formats[fmt].rs);
297 }
298
299 int
300 translate_rs_format_rb_swap(enum pipe_format fmt)
301 {
302 assert(formats[fmt].present);
303
304 return formats[fmt].rs & RS_FORMAT_RB_SWAP;
305 }
306
307 /* Return type flags for vertex element format */
308 uint32_t
309 translate_vertex_format_type(enum pipe_format fmt)
310 {
311 if (!formats[fmt].present)
312 return ETNA_NO_MATCH;
313
314 return formats[fmt].vtx;
315 }