• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3  * Copyright © 2018 Google, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Authors:
25  *    Rob Clark <robclark@freedesktop.org>
26  */
27 
28 #include "pipe/p_defines.h"
29 #include "util/format/u_format.h"
30 
31 #include "fd6_format.h"
32 #include "freedreno_resource.h"
33 
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 fd6_format {
40 	enum a6xx_format vtx;
41 	enum a6xx_format tex;
42 	enum a6xx_format rb;
43 	enum a3xx_color_swap swap;
44 	boolean present;
45 };
46 
47 #define FMT(pipe, vtxfmt, texfmt, rbfmt, swapfmt) \
48 	[PIPE_FORMAT_ ## pipe] = { \
49 		.present = 1, \
50 		.vtx = FMT6_ ## vtxfmt, \
51 		.tex = FMT6_ ## texfmt, \
52 		.rb = FMT6_ ## rbfmt, \
53 		.swap = swapfmt \
54 	}
55 
56 /* vertex + texture + color */
57 #define VTC(pipe, fmt, swapfmt) FMT(pipe, fmt, fmt, fmt, swapfmt)
58 
59 #define _TC(pipe, fmt, swapfmt) FMT(pipe, NONE, fmt, fmt, swapfmt)
60 #define _T_(pipe, fmt, swapfmt) FMT(pipe, NONE, fmt, NONE, swapfmt)
61 #define VT_(pipe, fmt, swapfmt) FMT(pipe, fmt, fmt, NONE, swapfmt)
62 #define V__(pipe, fmt, swapfmt) FMT(pipe, fmt, NONE, NONE, swapfmt)
63 
64 static struct fd6_format formats[PIPE_FORMAT_COUNT] = {
65 	/* 8-bit */
66 	VTC(R8_UNORM,   8_UNORM,                     WZYX),
67 	VTC(R8_SNORM,   8_SNORM,                     WZYX),
68 	VTC(R8_UINT,    8_UINT,                      WZYX),
69 	VTC(R8_SINT,    8_SINT,                      WZYX),
70 	V__(R8_USCALED, 8_UINT,                      WZYX),
71 	V__(R8_SSCALED, 8_SINT,                      WZYX),
72 
73 	FMT(A8_UNORM,   NONE, 8_UNORM, A8_UNORM,     WZYX),
74 	_TC(L8_UNORM,   8_UNORM,                     WZYX),
75 	_T_(I8_UNORM,   8_UNORM,                     WZYX),
76 
77 	_T_(A8_UINT,    8_UINT,                      WZYX),
78 	_T_(A8_SINT,    8_SINT,                      WZYX),
79 	_T_(L8_UINT,    8_UINT,                      WZYX),
80 	_T_(L8_SINT,    8_SINT,                      WZYX),
81 	_T_(I8_UINT,    8_UINT,                      WZYX),
82 	_T_(I8_SINT,    8_SINT,                      WZYX),
83 
84 	_TC(S8_UINT,    8_UINT,                      WZYX),
85 
86 	/* 16-bit */
87 	VTC(R16_UNORM,   16_UNORM,                   WZYX),
88 	VTC(R16_SNORM,   16_SNORM,                   WZYX),
89 	VTC(R16_UINT,    16_UINT,                    WZYX),
90 	VTC(R16_SINT,    16_SINT,                    WZYX),
91 	V__(R16_USCALED, 16_UINT,                    WZYX),
92 	V__(R16_SSCALED, 16_SINT,                    WZYX),
93 	VTC(R16_FLOAT,   16_FLOAT,                   WZYX),
94 	_TC(Z16_UNORM,   16_UNORM,                   WZYX),
95 
96 	_T_(A16_UNORM,   16_UNORM,                   WZYX),
97 	_T_(A16_SNORM,   16_SNORM,                   WZYX),
98 	_T_(A16_UINT,    16_UINT,                    WZYX),
99 	_T_(A16_SINT,    16_SINT,                    WZYX),
100 	_T_(L16_UNORM,   16_UNORM,                   WZYX),
101 	_T_(L16_SNORM,   16_SNORM,                   WZYX),
102 	_T_(L16_UINT,    16_UINT,                    WZYX),
103 	_T_(L16_SINT,    16_SINT,                    WZYX),
104 	_T_(I16_UNORM,   16_UNORM,                   WZYX),
105 	_T_(I16_SNORM,   16_SNORM,                   WZYX),
106 	_T_(I16_UINT,    16_UINT,                    WZYX),
107 	_T_(I16_SINT,    16_SINT,                    WZYX),
108 
109 	VTC(R8G8_UNORM,   8_8_UNORM,                 WZYX),
110 	VTC(R8G8_SNORM,   8_8_SNORM,                 WZYX),
111 	VTC(R8G8_UINT,    8_8_UINT,                  WZYX),
112 	VTC(R8G8_SINT,    8_8_SINT,                  WZYX),
113 	V__(R8G8_USCALED, 8_8_UINT,                  WZYX),
114 	V__(R8G8_SSCALED, 8_8_SINT,                  WZYX),
115 
116 	_T_(L8A8_UINT,    8_8_UINT,                  WZYX),
117 	_T_(L8A8_SINT,    8_8_SINT,                  WZYX),
118 
119 	_TC(B5G6R5_UNORM,   5_6_5_UNORM,             WXYZ),
120 	_TC(B5G5R5A1_UNORM, 5_5_5_1_UNORM,           WXYZ),
121 	_TC(B5G5R5X1_UNORM, 5_5_5_1_UNORM,           WXYZ),
122 	_TC(B4G4R4A4_UNORM, 4_4_4_4_UNORM,           WXYZ),
123 
124 	/* 24-bit */
125 	V__(R8G8B8_UNORM,   8_8_8_UNORM,             WZYX),
126 	V__(R8G8B8_SNORM,   8_8_8_SNORM,             WZYX),
127 	V__(R8G8B8_UINT,    8_8_8_UINT,              WZYX),
128 	V__(R8G8B8_SINT,    8_8_8_SINT,              WZYX),
129 	V__(R8G8B8_USCALED, 8_8_8_UINT,              WZYX),
130 	V__(R8G8B8_SSCALED, 8_8_8_SINT,              WZYX),
131 
132 	/* 32-bit */
133 	V__(R32_UNORM,   32_UNORM,                   WZYX),
134 	V__(R32_SNORM,   32_SNORM,                   WZYX),
135 	VTC(R32_UINT,    32_UINT,                    WZYX),
136 	VTC(R32_SINT,    32_SINT,                    WZYX),
137 	V__(R32_USCALED, 32_UINT,                    WZYX),
138 	V__(R32_SSCALED, 32_SINT,                    WZYX),
139 	VTC(R32_FLOAT,   32_FLOAT,                   WZYX),
140 	V__(R32_FIXED,   32_FIXED,                   WZYX),
141 
142 	_T_(A32_UINT,    32_UINT,                    WZYX),
143 	_T_(A32_SINT,    32_SINT,                    WZYX),
144 	_T_(L32_UINT,    32_UINT,                    WZYX),
145 	_T_(L32_SINT,    32_SINT,                    WZYX),
146 	_T_(I32_UINT,    32_UINT,                    WZYX),
147 	_T_(I32_SINT,    32_SINT,                    WZYX),
148 
149 	VTC(R16G16_UNORM,   16_16_UNORM,             WZYX),
150 	VTC(R16G16_SNORM,   16_16_SNORM,             WZYX),
151 	VTC(R16G16_UINT,    16_16_UINT,              WZYX),
152 	VTC(R16G16_SINT,    16_16_SINT,              WZYX),
153 	VT_(R16G16_USCALED, 16_16_UINT,              WZYX),
154 	VT_(R16G16_SSCALED, 16_16_SINT,              WZYX),
155 	VTC(R16G16_FLOAT,   16_16_FLOAT,             WZYX),
156 
157 	_T_(L16A16_UNORM,   16_16_UNORM,             WZYX),
158 	_T_(L16A16_SNORM,   16_16_SNORM,             WZYX),
159 	_T_(L16A16_UINT,    16_16_UINT,              WZYX),
160 	_T_(L16A16_SINT,    16_16_SINT,              WZYX),
161 
162 	VTC(R8G8B8A8_UNORM,   8_8_8_8_UNORM,         WZYX),
163 	_TC(R8G8B8X8_UNORM,   8_8_8_8_UNORM,         WZYX),
164 	_TC(R8G8B8A8_SRGB,    8_8_8_8_UNORM,         WZYX),
165 	_TC(R8G8B8X8_SRGB,    8_8_8_8_UNORM,         WZYX),
166 	VTC(R8G8B8A8_SNORM,   8_8_8_8_SNORM,         WZYX),
167 	VTC(R8G8B8A8_UINT,    8_8_8_8_UINT,          WZYX),
168 	VTC(R8G8B8A8_SINT,    8_8_8_8_SINT,          WZYX),
169 	V__(R8G8B8A8_USCALED, 8_8_8_8_UINT,          WZYX),
170 	V__(R8G8B8A8_SSCALED, 8_8_8_8_SINT,          WZYX),
171 
172 	VTC(B8G8R8A8_UNORM,   8_8_8_8_UNORM,         WXYZ),
173 	_TC(B8G8R8X8_UNORM,   8_8_8_8_UNORM,         WXYZ),
174 	VTC(B8G8R8A8_SRGB,    8_8_8_8_UNORM,         WXYZ),
175 	_TC(B8G8R8X8_SRGB,    8_8_8_8_UNORM,         WXYZ),
176 
177 	VTC(A8B8G8R8_UNORM,   8_8_8_8_UNORM,         XYZW),
178 	_TC(X8B8G8R8_UNORM,   8_8_8_8_UNORM,         XYZW),
179 	_TC(A8B8G8R8_SRGB,    8_8_8_8_UNORM,         XYZW),
180 	_TC(X8B8G8R8_SRGB,    8_8_8_8_UNORM,         XYZW),
181 
182 	VTC(A8R8G8B8_UNORM,   8_8_8_8_UNORM,         ZYXW),
183 	_TC(X8R8G8B8_UNORM,   8_8_8_8_UNORM,         ZYXW),
184 	_TC(A8R8G8B8_SRGB,    8_8_8_8_UNORM,         ZYXW),
185 	_TC(X8R8G8B8_SRGB,    8_8_8_8_UNORM,         ZYXW),
186 
187 	FMT(R10G10B10A2_UNORM, 10_10_10_2_UNORM, 10_10_10_2_UNORM, 10_10_10_2_UNORM_DEST, WZYX),
188 	FMT(B10G10R10A2_UNORM, 10_10_10_2_UNORM, 10_10_10_2_UNORM, 10_10_10_2_UNORM_DEST, WXYZ),
189 	FMT(B10G10R10X2_UNORM, NONE,             10_10_10_2_UNORM, 10_10_10_2_UNORM_DEST, WXYZ),
190 	V__(R10G10B10A2_SNORM,   10_10_10_2_SNORM,   WZYX),
191 	V__(B10G10R10A2_SNORM,   10_10_10_2_SNORM,   WXYZ),
192 	VTC(R10G10B10A2_UINT,    10_10_10_2_UINT,    WZYX),
193 	VTC(B10G10R10A2_UINT,    10_10_10_2_UINT,    WXYZ),
194 	V__(R10G10B10A2_USCALED, 10_10_10_2_UINT,    WZYX),
195 	V__(B10G10R10A2_USCALED, 10_10_10_2_UINT,    WXYZ),
196 	V__(R10G10B10A2_SSCALED, 10_10_10_2_SINT,    WZYX),
197 	V__(B10G10R10A2_SSCALED, 10_10_10_2_SINT,    WXYZ),
198 
199 	VTC(R11G11B10_FLOAT, 11_11_10_FLOAT,         WZYX),
200 	_T_(R9G9B9E5_FLOAT,  9_9_9_E5_FLOAT,         WZYX),
201 
202 	_TC(Z24X8_UNORM,          Z24_UNORM_S8_UINT, WZYX),
203 	_TC(X24S8_UINT,           8_8_8_8_UINT,      WZYX),
204 	_TC(Z24_UNORM_S8_UINT,    Z24_UNORM_S8_UINT, WZYX),
205 	_TC(Z32_FLOAT,            32_FLOAT,          WZYX),
206 	_TC(Z32_FLOAT_S8X24_UINT, 32_FLOAT,          WZYX),
207 	_TC(X32_S8X24_UINT,       8_UINT,            WZYX),
208 
209 	/* special format for blits: */
210 	_TC(Z24_UNORM_S8_UINT_AS_R8G8B8A8, Z24_UNORM_S8_UINT_AS_R8G8B8A8, WZYX),
211 
212 	/* 48-bit */
213 	V__(R16G16B16_UNORM,   16_16_16_UNORM,       WZYX),
214 	V__(R16G16B16_SNORM,   16_16_16_SNORM,       WZYX),
215 	V__(R16G16B16_UINT,    16_16_16_UINT,        WZYX),
216 	V__(R16G16B16_SINT,    16_16_16_SINT,        WZYX),
217 	V__(R16G16B16_USCALED, 16_16_16_UINT,        WZYX),
218 	V__(R16G16B16_SSCALED, 16_16_16_SINT,        WZYX),
219 	V__(R16G16B16_FLOAT,   16_16_16_FLOAT,       WZYX),
220 
221 	/* 64-bit */
222 	VTC(R16G16B16A16_UNORM,   16_16_16_16_UNORM, WZYX),
223 	VTC(R16G16B16X16_UNORM,   16_16_16_16_UNORM, WZYX),
224 	VTC(R16G16B16A16_SNORM,   16_16_16_16_SNORM, WZYX),
225 	VTC(R16G16B16X16_SNORM,   16_16_16_16_SNORM, WZYX),
226 	VTC(R16G16B16A16_UINT,    16_16_16_16_UINT,  WZYX),
227 	VTC(R16G16B16X16_UINT,    16_16_16_16_UINT,  WZYX),
228 	VTC(R16G16B16A16_SINT,    16_16_16_16_SINT,  WZYX),
229 	VTC(R16G16B16X16_SINT,    16_16_16_16_SINT,  WZYX),
230 	VT_(R16G16B16A16_USCALED, 16_16_16_16_UINT,  WZYX),
231 	VT_(R16G16B16A16_SSCALED, 16_16_16_16_SINT,  WZYX),
232 	VTC(R16G16B16A16_FLOAT,   16_16_16_16_FLOAT, WZYX),
233 	VTC(R16G16B16X16_FLOAT,   16_16_16_16_FLOAT, WZYX),
234 
235 	V__(R32G32_UNORM,   32_32_UNORM,             WZYX),
236 	V__(R32G32_SNORM,   32_32_SNORM,             WZYX),
237 	VTC(R32G32_UINT,    32_32_UINT,              WZYX),
238 	VTC(R32G32_SINT,    32_32_SINT,              WZYX),
239 	V__(R32G32_USCALED, 32_32_UINT,              WZYX),
240 	V__(R32G32_SSCALED, 32_32_SINT,              WZYX),
241 	VTC(R32G32_FLOAT,   32_32_FLOAT,             WZYX),
242 	V__(R32G32_FIXED,   32_32_FIXED,             WZYX),
243 
244 	_T_(L32A32_UINT,    32_32_UINT,              WZYX),
245 	_T_(L32A32_SINT,    32_32_SINT,              WZYX),
246 
247 	/* 96-bit */
248 	V__(R32G32B32_UNORM,   32_32_32_UNORM,       WZYX),
249 	V__(R32G32B32_SNORM,   32_32_32_SNORM,       WZYX),
250 	VT_(R32G32B32_UINT,    32_32_32_UINT,        WZYX),
251 	VT_(R32G32B32_SINT,    32_32_32_SINT,        WZYX),
252 	V__(R32G32B32_USCALED, 32_32_32_UINT,        WZYX),
253 	V__(R32G32B32_SSCALED, 32_32_32_SINT,        WZYX),
254 	VT_(R32G32B32_FLOAT,   32_32_32_FLOAT,       WZYX),
255 	V__(R32G32B32_FIXED,   32_32_32_FIXED,       WZYX),
256 
257 	/* 128-bit */
258 	V__(R32G32B32A32_UNORM,   32_32_32_32_UNORM, WZYX),
259 	V__(R32G32B32A32_SNORM,   32_32_32_32_SNORM, WZYX),
260 	VTC(R32G32B32A32_UINT,    32_32_32_32_UINT,  WZYX),
261 	_TC(R32G32B32X32_UINT,    32_32_32_32_UINT,  WZYX),
262 	VTC(R32G32B32A32_SINT,    32_32_32_32_SINT,  WZYX),
263 	_TC(R32G32B32X32_SINT,    32_32_32_32_SINT,  WZYX),
264 	V__(R32G32B32A32_USCALED, 32_32_32_32_UINT,  WZYX),
265 	V__(R32G32B32A32_SSCALED, 32_32_32_32_SINT,  WZYX),
266 	VTC(R32G32B32A32_FLOAT,   32_32_32_32_FLOAT, WZYX),
267 	_TC(R32G32B32X32_FLOAT,   32_32_32_32_FLOAT, WZYX),
268 	V__(R32G32B32A32_FIXED,   32_32_32_32_FIXED, WZYX),
269 
270 	/* compressed */
271 	_T_(ETC1_RGB8, ETC1,                         WZYX),
272 	_T_(ETC2_RGB8, ETC2_RGB8,                    WZYX),
273 	_T_(ETC2_SRGB8, ETC2_RGB8,                   WZYX),
274 	_T_(ETC2_RGB8A1, ETC2_RGB8A1,                WZYX),
275 	_T_(ETC2_SRGB8A1, ETC2_RGB8A1,               WZYX),
276 	_T_(ETC2_RGBA8, ETC2_RGBA8,                  WZYX),
277 	_T_(ETC2_SRGBA8, ETC2_RGBA8,                 WZYX),
278 	_T_(ETC2_R11_UNORM, ETC2_R11_UNORM,          WZYX),
279 	_T_(ETC2_R11_SNORM, ETC2_R11_SNORM,          WZYX),
280 	_T_(ETC2_RG11_UNORM, ETC2_RG11_UNORM,        WZYX),
281 	_T_(ETC2_RG11_SNORM, ETC2_RG11_SNORM,        WZYX),
282 
283 	_T_(DXT1_RGB,   DXT1,                        WZYX),
284 	_T_(DXT1_SRGB,  DXT1,                        WZYX),
285 	_T_(DXT1_RGBA,  DXT1,                        WZYX),
286 	_T_(DXT1_SRGBA, DXT1,                        WZYX),
287 	_T_(DXT3_RGBA,  DXT3,                        WZYX),
288 	_T_(DXT3_SRGBA, DXT3,                        WZYX),
289 	_T_(DXT5_RGBA,  DXT5,                        WZYX),
290 	_T_(DXT5_SRGBA, DXT5,                        WZYX),
291 
292 	_T_(BPTC_RGBA_UNORM, BPTC,                   WZYX),
293 	_T_(BPTC_SRGBA,      BPTC,                   WZYX),
294 	_T_(BPTC_RGB_FLOAT,  BPTC_FLOAT,             WZYX),
295 	_T_(BPTC_RGB_UFLOAT, BPTC_UFLOAT,            WZYX),
296 
297 	_T_(RGTC1_UNORM, RGTC1_UNORM,                WZYX),
298 	_T_(RGTC1_SNORM, RGTC1_SNORM,                WZYX),
299 	_T_(RGTC2_UNORM, RGTC2_UNORM,                WZYX),
300 	_T_(RGTC2_SNORM, RGTC2_SNORM,                WZYX),
301 	_T_(LATC1_UNORM, RGTC1_UNORM,                WZYX),
302 	_T_(LATC1_SNORM, RGTC1_SNORM,                WZYX),
303 	_T_(LATC2_UNORM, RGTC2_UNORM,                WZYX),
304 	_T_(LATC2_SNORM, RGTC2_SNORM,                WZYX),
305 
306 	_T_(ASTC_4x4,   ASTC_4x4,                    WZYX),
307 	_T_(ASTC_5x4,   ASTC_5x4,                    WZYX),
308 	_T_(ASTC_5x5,   ASTC_5x5,                    WZYX),
309 	_T_(ASTC_6x5,   ASTC_6x5,                    WZYX),
310 	_T_(ASTC_6x6,   ASTC_6x6,                    WZYX),
311 	_T_(ASTC_8x5,   ASTC_8x5,                    WZYX),
312 	_T_(ASTC_8x6,   ASTC_8x6,                    WZYX),
313 	_T_(ASTC_8x8,   ASTC_8x8,                    WZYX),
314 	_T_(ASTC_10x5,  ASTC_10x5,                   WZYX),
315 	_T_(ASTC_10x6,  ASTC_10x6,                   WZYX),
316 	_T_(ASTC_10x8,  ASTC_10x8,                   WZYX),
317 	_T_(ASTC_10x10, ASTC_10x10,                  WZYX),
318 	_T_(ASTC_12x10, ASTC_12x10,                  WZYX),
319 	_T_(ASTC_12x12, ASTC_12x12,                  WZYX),
320 
321 	_T_(ASTC_4x4_SRGB,   ASTC_4x4,               WZYX),
322 	_T_(ASTC_5x4_SRGB,   ASTC_5x4,               WZYX),
323 	_T_(ASTC_5x5_SRGB,   ASTC_5x5,               WZYX),
324 	_T_(ASTC_6x5_SRGB,   ASTC_6x5,               WZYX),
325 	_T_(ASTC_6x6_SRGB,   ASTC_6x6,               WZYX),
326 	_T_(ASTC_8x5_SRGB,   ASTC_8x5,               WZYX),
327 	_T_(ASTC_8x6_SRGB,   ASTC_8x6,               WZYX),
328 	_T_(ASTC_8x8_SRGB,   ASTC_8x8,               WZYX),
329 	_T_(ASTC_10x5_SRGB,  ASTC_10x5,              WZYX),
330 	_T_(ASTC_10x6_SRGB,  ASTC_10x6,              WZYX),
331 	_T_(ASTC_10x8_SRGB,  ASTC_10x8,              WZYX),
332 	_T_(ASTC_10x10_SRGB, ASTC_10x10,             WZYX),
333 	_T_(ASTC_12x10_SRGB, ASTC_12x10,             WZYX),
334 	_T_(ASTC_12x12_SRGB, ASTC_12x12,             WZYX),
335 
336 	_T_(R8_G8B8_420_UNORM, R8_G8B8_2PLANE_420_UNORM, WZYX),
337 };
338 
339 /* convert pipe format to vertex buffer format: */
340 enum a6xx_format
fd6_pipe2vtx(enum pipe_format format)341 fd6_pipe2vtx(enum pipe_format format)
342 {
343 	if (!formats[format].present)
344 		return FMT6_NONE;
345 	return formats[format].vtx;
346 }
347 
348 /* convert pipe format to texture sampler format: */
349 enum a6xx_format
fd6_pipe2tex(enum pipe_format format)350 fd6_pipe2tex(enum pipe_format format)
351 {
352 	if (!formats[format].present)
353 		return FMT6_NONE;
354 	return formats[format].tex;
355 }
356 
357 /* convert pipe format to MRT / copydest format used for render-target: */
358 enum a6xx_format
fd6_pipe2color(enum pipe_format format)359 fd6_pipe2color(enum pipe_format format)
360 {
361 	if (!formats[format].present)
362 		return FMT6_NONE;
363 	return formats[format].rb;
364 }
365 
366 enum a3xx_color_swap
fd6_pipe2swap(enum pipe_format format)367 fd6_pipe2swap(enum pipe_format format)
368 {
369 	if (!formats[format].present)
370 		return WZYX;
371 	return formats[format].swap;
372 }
373 
374 enum a6xx_depth_format
fd6_pipe2depth(enum pipe_format format)375 fd6_pipe2depth(enum pipe_format format)
376 {
377 	switch (format) {
378 	case PIPE_FORMAT_Z16_UNORM:
379 		return DEPTH6_16;
380 	case PIPE_FORMAT_Z24X8_UNORM:
381 	case PIPE_FORMAT_Z24_UNORM_S8_UINT:
382 	case PIPE_FORMAT_X8Z24_UNORM:
383 	case PIPE_FORMAT_S8_UINT_Z24_UNORM:
384 		return DEPTH6_24_8;
385 	case PIPE_FORMAT_Z32_FLOAT:
386 	case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
387 		return DEPTH6_32;
388 	default:
389 		return ~0;
390 	}
391 }
392 
393 enum a6xx_tex_swiz
fd6_pipe2swiz(unsigned swiz)394 fd6_pipe2swiz(unsigned swiz)
395 {
396 	switch (swiz) {
397 	default:
398 	case PIPE_SWIZZLE_X: return A6XX_TEX_X;
399 	case PIPE_SWIZZLE_Y: return A6XX_TEX_Y;
400 	case PIPE_SWIZZLE_Z: return A6XX_TEX_Z;
401 	case PIPE_SWIZZLE_W: return A6XX_TEX_W;
402 	case PIPE_SWIZZLE_0: return A6XX_TEX_ZERO;
403 	case PIPE_SWIZZLE_1: return A6XX_TEX_ONE;
404 	}
405 }
406 
407 void
fd6_tex_swiz(enum pipe_format format,unsigned char * swiz,unsigned swizzle_r,unsigned swizzle_g,unsigned swizzle_b,unsigned swizzle_a)408 fd6_tex_swiz(enum pipe_format format, unsigned char *swiz,
409 			 unsigned swizzle_r, unsigned swizzle_g,
410 			 unsigned swizzle_b, unsigned swizzle_a)
411 {
412 	const struct util_format_description *desc =
413 			util_format_description(format);
414 	const unsigned char uswiz[4] = {
415 		swizzle_r, swizzle_g, swizzle_b, swizzle_a
416 	};
417 
418 	/* Gallium expects stencil sampler to return (s,s,s,s), so massage
419 	 * the swizzle to do so.
420 	 */
421 	if (format == PIPE_FORMAT_X24S8_UINT) {
422 		const unsigned char stencil_swiz[4] = {
423 			PIPE_SWIZZLE_W, PIPE_SWIZZLE_W, PIPE_SWIZZLE_W, PIPE_SWIZZLE_W
424 		};
425 		util_format_compose_swizzles(stencil_swiz, uswiz, swiz);
426 	} else if (fd6_pipe2swap(format) != WZYX) {
427 		/* Formats with a non-pass-through swap are permutations of RGBA
428 		 * formats. We program the permutation using the swap and don't
429 		 * need to compose the format swizzle with the user swizzle.
430 		 */
431 		memcpy(swiz, uswiz, sizeof(uswiz));
432 	} else {
433 		/* Otherwise, it's an unswapped RGBA format or a format like L8 where
434 		 * we need the XXX1 swizzle from the gallium format description.
435 		 */
436 		util_format_compose_swizzles(desc->swizzle, uswiz, swiz);
437 	}
438 }
439 
440 /* Compute the TEX_CONST_0 value for texture state, including SWIZ/SWAP/etc: */
441 uint32_t
fd6_tex_const_0(struct pipe_resource * prsc,unsigned level,enum pipe_format format,unsigned swizzle_r,unsigned swizzle_g,unsigned swizzle_b,unsigned swizzle_a)442 fd6_tex_const_0(struct pipe_resource *prsc,
443 			 unsigned level, enum pipe_format format,
444 			 unsigned swizzle_r, unsigned swizzle_g,
445 			 unsigned swizzle_b, unsigned swizzle_a)
446 {
447 	struct fd_resource *rsc = fd_resource(prsc);
448 	unsigned char swiz[4];
449 
450 	fd6_tex_swiz(format, swiz,
451 			swizzle_r, swizzle_g,
452 			swizzle_b, swizzle_a);
453 
454 	return
455 		A6XX_TEX_CONST_0_FMT(fd6_pipe2tex(format)) |
456 		A6XX_TEX_CONST_0_SAMPLES(fd_msaa_samples(prsc->nr_samples)) |
457 		A6XX_TEX_CONST_0_SWAP(fd6_resource_swap(rsc, format)) |
458 		A6XX_TEX_CONST_0_TILE_MODE(fd_resource_tile_mode(prsc, level)) |
459 		COND(util_format_is_srgb(format), A6XX_TEX_CONST_0_SRGB) |
460 		A6XX_TEX_CONST_0_SWIZ_X(fd6_pipe2swiz(swiz[0])) |
461 		A6XX_TEX_CONST_0_SWIZ_Y(fd6_pipe2swiz(swiz[1])) |
462 		A6XX_TEX_CONST_0_SWIZ_Z(fd6_pipe2swiz(swiz[2])) |
463 		A6XX_TEX_CONST_0_SWIZ_W(fd6_pipe2swiz(swiz[3]));
464 }
465