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/common_3d.xml.h"
30 #include "hw/state.xml.h"
31 #include "hw/state_3d.xml.h"
32
33 #include "pipe/p_defines.h"
34
35 /* Specifies the table of all the formats and their features. Also supplies
36 * the helpers that look up various data in those tables.
37 */
38
39 struct etna_format {
40 unsigned vtx;
41 unsigned tex;
42 unsigned pe;
43 bool present;
44 };
45
46 #define PE_FORMAT_NONE ~0
47
48 #define PE_FORMAT_MASK 0x7f
49 #define PE_FORMAT(x) ((x) & PE_FORMAT_MASK)
50 #define PE_FORMAT_RB_SWAP 0x80
51
52 #define PE_FORMAT_X8B8G8R8 (PE_FORMAT_X8R8G8B8 | PE_FORMAT_RB_SWAP)
53 #define PE_FORMAT_A8B8G8R8 (PE_FORMAT_A8R8G8B8 | PE_FORMAT_RB_SWAP)
54
55 #define TS_SAMPLER_FORMAT_NONE ETNA_NO_MATCH
56
57 /* vertex + texture */
58 #define VT(pipe, vtxfmt, texfmt, rsfmt) \
59 [PIPE_FORMAT_##pipe] = { \
60 .vtx = FE_DATA_TYPE_##vtxfmt, \
61 .tex = TEXTURE_FORMAT_##texfmt, \
62 .pe = PE_FORMAT_##rsfmt, \
63 .present = 1, \
64 }
65
66 /* texture-only */
67 #define _T(pipe, fmt, rsfmt) \
68 [PIPE_FORMAT_##pipe] = { \
69 .vtx = ETNA_NO_MATCH, \
70 .tex = TEXTURE_FORMAT_##fmt, \
71 .pe = PE_FORMAT_##rsfmt, \
72 .present = 1, \
73 }
74
75 /* vertex-only */
76 #define V_(pipe, fmt, rsfmt) \
77 [PIPE_FORMAT_##pipe] = { \
78 .vtx = FE_DATA_TYPE_##fmt, \
79 .tex = ETNA_NO_MATCH, \
80 .pe = PE_FORMAT_##rsfmt, \
81 .present = 1, \
82 }
83
84 static struct etna_format formats[PIPE_FORMAT_COUNT] = {
85 /* 8-bit */
86 VT(R8_UNORM, UNSIGNED_BYTE, L8, R8),
87 VT(R8_SNORM, BYTE, EXT_R8_SNORM | EXT_FORMAT, NONE),
88 VT(R8_UINT, BYTE_I, EXT_R8I | EXT_FORMAT, R8I),
89 VT(R8_SINT, BYTE_I, EXT_R8I | EXT_FORMAT, R8I),
90 V_(R8_USCALED, UNSIGNED_BYTE, NONE),
91 V_(R8_SSCALED, BYTE, NONE),
92
93 _T(A8_UNORM, A8, NONE),
94 _T(L8_UNORM, L8, NONE),
95 _T(I8_UNORM, I8, NONE),
96
97 /* 16-bit */
98 V_(R16_UNORM, UNSIGNED_SHORT, NONE),
99 V_(R16_SNORM, SHORT, NONE),
100 VT(R16_UINT, SHORT_I, EXT_R16I | EXT_FORMAT, R16I),
101 VT(R16_SINT, SHORT_I, EXT_R16I | EXT_FORMAT, R16I),
102 V_(R16_USCALED, UNSIGNED_SHORT, NONE),
103 V_(R16_SSCALED, SHORT, NONE),
104 VT(R16_FLOAT, HALF_FLOAT, EXT_R16F | EXT_FORMAT, R16F),
105
106 _T(B4G4R4A4_UNORM, A4R4G4B4, A4R4G4B4),
107 _T(B4G4R4X4_UNORM, X4R4G4B4, X4R4G4B4),
108
109 _T(L8A8_UNORM, A8L8, NONE),
110
111 _T(Z16_UNORM, D16, NONE),
112 _T(B5G6R5_UNORM, R5G6B5, R5G6B5),
113 _T(B5G5R5A1_UNORM, A1R5G5B5, A1R5G5B5),
114 _T(B5G5R5X1_UNORM, X1R5G5B5, X1R5G5B5),
115
116 VT(R8G8_UNORM, UNSIGNED_BYTE, EXT_G8R8 | EXT_FORMAT, G8R8),
117 VT(R8G8_SNORM, BYTE, EXT_G8R8_SNORM | EXT_FORMAT, NONE),
118 VT(R8G8_UINT, BYTE_I, EXT_G8R8I | EXT_FORMAT, G8R8I),
119 VT(R8G8_SINT, BYTE_I, EXT_G8R8I | EXT_FORMAT, G8R8I),
120 V_(R8G8_USCALED, UNSIGNED_BYTE, NONE),
121 V_(R8G8_SSCALED, BYTE, NONE),
122
123 /* 24-bit */
124 V_(R8G8B8_UNORM, UNSIGNED_BYTE, NONE),
125 V_(R8G8B8_SNORM, BYTE, NONE),
126 V_(R8G8B8_UINT, BYTE_I, NONE),
127 V_(R8G8B8_SINT, BYTE_I, NONE),
128 V_(R8G8B8_USCALED, UNSIGNED_BYTE, NONE),
129 V_(R8G8B8_SSCALED, BYTE, NONE),
130
131 /* 32-bit */
132 V_(R32_UNORM, UNSIGNED_INT, NONE),
133 V_(R32_SNORM, INT, NONE),
134 VT(R32_SINT, FLOAT, EXT_R32F | EXT_FORMAT, R32F),
135 VT(R32_UINT, FLOAT, EXT_R32F | EXT_FORMAT, R32F),
136 V_(R32_USCALED, UNSIGNED_INT, NONE),
137 V_(R32_SSCALED, INT, NONE),
138 VT(R32_FLOAT, FLOAT, EXT_R32F | EXT_FORMAT, R32F),
139 V_(R32_FIXED, FIXED, NONE),
140
141 V_(R16G16_UNORM, UNSIGNED_SHORT, NONE),
142 V_(R16G16_SNORM, SHORT, NONE),
143 VT(R16G16_UINT, SHORT_I, EXT_G16R16I | EXT_FORMAT, G16R16I),
144 VT(R16G16_SINT, SHORT_I, EXT_G16R16I | EXT_FORMAT, G16R16I),
145 V_(R16G16_USCALED, UNSIGNED_SHORT, NONE),
146 V_(R16G16_SSCALED, SHORT, NONE),
147 VT(R16G16_FLOAT, HALF_FLOAT, EXT_G16R16F | EXT_FORMAT, G16R16F),
148
149 V_(A8B8G8R8_UNORM, UNSIGNED_BYTE, NONE),
150
151 VT(R8G8B8A8_UNORM, UNSIGNED_BYTE, A8B8G8R8, A8B8G8R8),
152 VT(R8G8B8A8_SNORM, BYTE, EXT_A8B8G8R8_SNORM | EXT_FORMAT, NONE),
153 _T(R8G8B8X8_UNORM, X8B8G8R8, X8B8G8R8),
154 _T(R8G8B8X8_SNORM, EXT_X8B8G8R8_SNORM | EXT_FORMAT, NONE),
155 VT(R8G8B8A8_UINT, BYTE_I, EXT_A8B8G8R8I | EXT_FORMAT, A8B8G8R8I),
156 VT(R8G8B8A8_SINT, BYTE_I, EXT_A8B8G8R8I | EXT_FORMAT, A8B8G8R8I),
157 V_(R8G8B8A8_USCALED, UNSIGNED_BYTE, A8B8G8R8),
158 V_(R8G8B8A8_SSCALED, BYTE, A8B8G8R8),
159
160 _T(B8G8R8A8_UNORM, A8R8G8B8, A8R8G8B8),
161 _T(B8G8R8X8_UNORM, X8R8G8B8, X8R8G8B8),
162
163 VT(R10G10B10A2_UNORM, UNSIGNED_INT_2_10_10_10_REV, EXT_A2B10G10R10 | EXT_FORMAT, A2B10G10R10),
164 _T(R10G10B10X2_UNORM, EXT_A2B10G10R10 | EXT_FORMAT, A2B10G10R10),
165 V_(R10G10B10A2_SNORM, INT_2_10_10_10_REV, NONE),
166 _T(R10G10B10A2_UINT, EXT_A2B10G10R10UI | EXT_FORMAT, A2B10G10R10UI),
167 V_(R10G10B10A2_USCALED, UNSIGNED_INT_2_10_10_10_REV, NONE),
168 V_(R10G10B10A2_SSCALED, INT_2_10_10_10_REV, NONE),
169
170 _T(X8Z24_UNORM, D24X8, NONE),
171 _T(S8_UINT_Z24_UNORM, D24X8, NONE),
172
173 _T(R9G9B9E5_FLOAT, E5B9G9R9, NONE),
174 _T(R11G11B10_FLOAT, EXT_B10G11R11F | EXT_FORMAT, B10G11R11F),
175
176 /* 48-bit */
177 V_(R16G16B16_UNORM, UNSIGNED_SHORT, NONE),
178 V_(R16G16B16_SNORM, SHORT, NONE),
179 V_(R16G16B16_UINT, SHORT_I, NONE),
180 V_(R16G16B16_SINT, SHORT_I, NONE),
181 V_(R16G16B16_USCALED, UNSIGNED_SHORT, NONE),
182 V_(R16G16B16_SSCALED, SHORT, NONE),
183 V_(R16G16B16_FLOAT, HALF_FLOAT, NONE),
184
185 /* 64-bit */
186 V_(R16G16B16A16_UNORM, UNSIGNED_SHORT, NONE),
187 V_(R16G16B16A16_SNORM, SHORT, NONE),
188 VT(R16G16B16A16_UINT, SHORT_I, EXT_A16B16G16R16I | EXT_FORMAT, A16B16G16R16I),
189 VT(R16G16B16A16_SINT, SHORT_I, EXT_A16B16G16R16I | EXT_FORMAT, A16B16G16R16I),
190 V_(R16G16B16A16_USCALED, UNSIGNED_SHORT, NONE),
191 V_(R16G16B16A16_SSCALED, SHORT, NONE),
192 VT(R16G16B16A16_FLOAT, HALF_FLOAT, EXT_A16B16G16R16F | EXT_FORMAT, A16B16G16R16F),
193
194 V_(R32G32_UNORM, UNSIGNED_INT, NONE),
195 V_(R32G32_SNORM, INT, NONE),
196 VT(R32G32_UINT, FLOAT, EXT_G32R32F | EXT_FORMAT, G32R32F),
197 VT(R32G32_SINT, FLOAT, EXT_G32R32F | EXT_FORMAT, G32R32F),
198 V_(R32G32_USCALED, UNSIGNED_INT, NONE),
199 V_(R32G32_SSCALED, INT, NONE),
200 VT(R32G32_FLOAT, FLOAT, EXT_G32R32F | EXT_FORMAT, G32R32F),
201 V_(R32G32_FIXED, FIXED, NONE),
202
203 /* 96-bit */
204 V_(R32G32B32_UNORM, UNSIGNED_INT, NONE),
205 V_(R32G32B32_SNORM, INT, NONE),
206 V_(R32G32B32_UINT, FLOAT, NONE),
207 V_(R32G32B32_SINT, FLOAT, NONE),
208 V_(R32G32B32_USCALED, UNSIGNED_INT, NONE),
209 V_(R32G32B32_SSCALED, INT, NONE),
210 V_(R32G32B32_FLOAT, FLOAT, NONE),
211 V_(R32G32B32_FIXED, FIXED, NONE),
212
213 /* 128-bit */
214 V_(R32G32B32A32_UNORM, UNSIGNED_INT, NONE),
215 V_(R32G32B32A32_SNORM, INT, NONE),
216 V_(R32G32B32A32_UINT, FLOAT, NONE),
217 V_(R32G32B32A32_SINT, FLOAT, NONE),
218 V_(R32G32B32A32_USCALED, UNSIGNED_INT, NONE),
219 V_(R32G32B32A32_SSCALED, INT, NONE),
220 V_(R32G32B32A32_FLOAT, FLOAT, NONE),
221 V_(R32G32B32A32_FIXED, FIXED, NONE),
222
223 /* compressed */
224 _T(ETC1_RGB8, ETC1, NONE),
225
226 _T(DXT1_RGB, DXT1, NONE),
227 _T(DXT1_RGBA, DXT1, NONE),
228 _T(DXT3_RGBA, DXT2_DXT3, NONE),
229 _T(DXT5_RGBA, DXT4_DXT5, NONE),
230
231 _T(ETC2_RGB8, EXT_NONE | EXT_FORMAT, NONE), /* Extd. format NONE doubles as ETC2_RGB8 */
232 _T(ETC2_RGB8A1, EXT_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 | EXT_FORMAT, NONE),
233 _T(ETC2_RGBA8, EXT_RGBA8_ETC2_EAC | EXT_FORMAT, NONE),
234 _T(ETC2_R11_UNORM, EXT_R11_EAC | EXT_FORMAT, NONE),
235 _T(ETC2_R11_SNORM, EXT_SIGNED_R11_EAC | EXT_FORMAT, NONE),
236 _T(ETC2_RG11_UNORM, EXT_RG11_EAC | EXT_FORMAT, NONE),
237 _T(ETC2_RG11_SNORM, EXT_SIGNED_RG11_EAC | EXT_FORMAT, NONE),
238
239 _T(ASTC_4x4, ASTC_RGBA_4x4 | ASTC_FORMAT, NONE),
240 _T(ASTC_5x4, ASTC_RGBA_5x4 | ASTC_FORMAT, NONE),
241 _T(ASTC_5x5, ASTC_RGBA_5x5 | ASTC_FORMAT, NONE),
242 _T(ASTC_6x5, ASTC_RGBA_6x5 | ASTC_FORMAT, NONE),
243 _T(ASTC_6x6, ASTC_RGBA_6x6 | ASTC_FORMAT, NONE),
244 _T(ASTC_8x5, ASTC_RGBA_8x5 | ASTC_FORMAT, NONE),
245 _T(ASTC_8x6, ASTC_RGBA_8x6 | ASTC_FORMAT, NONE),
246 _T(ASTC_8x8, ASTC_RGBA_8x8 | ASTC_FORMAT, NONE),
247 _T(ASTC_10x5, ASTC_RGBA_10x5 | ASTC_FORMAT, NONE),
248 _T(ASTC_10x6, ASTC_RGBA_10x6 | ASTC_FORMAT, NONE),
249 _T(ASTC_10x8, ASTC_RGBA_10x8 | ASTC_FORMAT, NONE),
250 _T(ASTC_10x10, ASTC_RGBA_10x10 | ASTC_FORMAT, NONE),
251 _T(ASTC_12x10, ASTC_RGBA_12x10 | ASTC_FORMAT, NONE),
252 _T(ASTC_12x12, ASTC_RGBA_12x12 | ASTC_FORMAT, NONE),
253
254 /* YUV */
255 _T(YUYV, YUY2, YUY2),
256 _T(UYVY, UYVY, NONE),
257 };
258
259 uint32_t
translate_texture_format(enum pipe_format fmt)260 translate_texture_format(enum pipe_format fmt)
261 {
262 fmt = util_format_linear(fmt);
263
264 if (!formats[fmt].present)
265 return ETNA_NO_MATCH;
266
267 return formats[fmt].tex;
268 }
269
270 bool
texture_use_int_filter(const struct pipe_sampler_view * sv,const struct pipe_sampler_state * ss,bool tex_desc)271 texture_use_int_filter(const struct pipe_sampler_view *sv,
272 const struct pipe_sampler_state *ss,
273 bool tex_desc)
274 {
275 switch (sv->target) {
276 case PIPE_TEXTURE_1D_ARRAY:
277 case PIPE_TEXTURE_2D_ARRAY:
278 if (tex_desc)
279 break;
280 /* fallthrough */
281 case PIPE_TEXTURE_3D:
282 return false;
283 default:
284 break;
285 }
286
287 /* only unorm formats can use int filter */
288 if (!util_format_is_unorm(sv->format))
289 return false;
290
291 if (util_format_is_srgb(sv->format))
292 return false;
293
294 if (util_format_description(sv->format)->layout == UTIL_FORMAT_LAYOUT_ASTC)
295 return false;
296
297 if (ss->max_anisotropy > 1)
298 return false;
299
300 switch (sv->format) {
301 /* apparently D16 can't use int filter but D24 can */
302 case PIPE_FORMAT_Z16_UNORM:
303 case PIPE_FORMAT_R10G10B10A2_UNORM:
304 case PIPE_FORMAT_R10G10B10X2_UNORM:
305 case PIPE_FORMAT_ETC2_R11_UNORM:
306 case PIPE_FORMAT_ETC2_RG11_UNORM:
307 return false;
308 default:
309 return true;
310 }
311 }
312
313 bool
texture_format_needs_swiz(enum pipe_format fmt)314 texture_format_needs_swiz(enum pipe_format fmt)
315 {
316 return util_format_linear(fmt) == PIPE_FORMAT_R8_UNORM;
317 }
318
319 uint32_t
get_texture_swiz(enum pipe_format fmt,unsigned swizzle_r,unsigned swizzle_g,unsigned swizzle_b,unsigned swizzle_a)320 get_texture_swiz(enum pipe_format fmt, unsigned swizzle_r,
321 unsigned swizzle_g, unsigned swizzle_b, unsigned swizzle_a)
322 {
323 unsigned char swiz[4] = {
324 swizzle_r, swizzle_g, swizzle_b, swizzle_a,
325 };
326
327 if (util_format_linear(fmt) == PIPE_FORMAT_R8_UNORM) {
328 /* R8 is emulated with L8, needs yz channels set to zero */
329 for (unsigned i = 0; i < 4; i++) {
330 if (swiz[i] == PIPE_SWIZZLE_Y || swiz[i] == PIPE_SWIZZLE_Z)
331 swiz[i] = PIPE_SWIZZLE_0;
332 }
333 }
334
335 /* PIPE_SWIZZLE_ maps 1:1 to TEXTURE_SWIZZLE_ */
336 STATIC_ASSERT(PIPE_SWIZZLE_X == TEXTURE_SWIZZLE_RED);
337 STATIC_ASSERT(PIPE_SWIZZLE_Y == TEXTURE_SWIZZLE_GREEN);
338 STATIC_ASSERT(PIPE_SWIZZLE_Z == TEXTURE_SWIZZLE_BLUE);
339 STATIC_ASSERT(PIPE_SWIZZLE_W == TEXTURE_SWIZZLE_ALPHA);
340 STATIC_ASSERT(PIPE_SWIZZLE_0 == TEXTURE_SWIZZLE_ZERO);
341 STATIC_ASSERT(PIPE_SWIZZLE_1 == TEXTURE_SWIZZLE_ONE);
342
343 return VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_R(swiz[0]) |
344 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_G(swiz[1]) |
345 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_B(swiz[2]) |
346 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_A(swiz[3]);
347 }
348
349 uint32_t
translate_pe_format(enum pipe_format fmt)350 translate_pe_format(enum pipe_format fmt)
351 {
352 fmt = util_format_linear(fmt);
353
354 if (!formats[fmt].present)
355 return ETNA_NO_MATCH;
356
357 if (formats[fmt].pe == ETNA_NO_MATCH)
358 return ETNA_NO_MATCH;
359
360 return PE_FORMAT(formats[fmt].pe);
361 }
362
363 int
translate_pe_format_rb_swap(enum pipe_format fmt)364 translate_pe_format_rb_swap(enum pipe_format fmt)
365 {
366 fmt = util_format_linear(fmt);
367 assert(formats[fmt].present);
368
369 return formats[fmt].pe & PE_FORMAT_RB_SWAP;
370 }
371
372 /* Return type flags for vertex element format */
373 uint32_t
translate_vertex_format_type(enum pipe_format fmt)374 translate_vertex_format_type(enum pipe_format fmt)
375 {
376 if (!formats[fmt].present)
377 return ETNA_NO_MATCH;
378
379 return formats[fmt].vtx;
380 }
381