• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2 
3 /*
4  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28 
29 #include "pipe/p_defines.h"
30 #include "util/u_format.h"
31 
32 #include "fd2_util.h"
33 
34 enum a2xx_sq_surfaceformat
fd2_pipe2surface(enum pipe_format format)35 fd2_pipe2surface(enum pipe_format format)
36 {
37 	switch (format) {
38 	/* 8-bit buffers. */
39 	case PIPE_FORMAT_A8_UNORM:
40 	case PIPE_FORMAT_A8_SNORM:
41 	case PIPE_FORMAT_A8_UINT:
42 	case PIPE_FORMAT_A8_SINT:
43 	case PIPE_FORMAT_I8_UNORM:
44 	case PIPE_FORMAT_I8_SNORM:
45 	case PIPE_FORMAT_I8_UINT:
46 	case PIPE_FORMAT_I8_SINT:
47 	case PIPE_FORMAT_L8_UNORM:
48 	case PIPE_FORMAT_L8_SNORM:
49 	case PIPE_FORMAT_L8_UINT:
50 	case PIPE_FORMAT_L8_SINT:
51 	case PIPE_FORMAT_L8_SRGB:
52 	case PIPE_FORMAT_R8_UNORM:
53 	case PIPE_FORMAT_R8_SNORM:
54 	case PIPE_FORMAT_R8_UINT:
55 	case PIPE_FORMAT_R8_SINT:
56 		return FMT_8;
57 
58 	/* 16-bit buffers. */
59 	case PIPE_FORMAT_B5G6R5_UNORM:
60 		return FMT_5_6_5;
61 	case PIPE_FORMAT_B5G5R5A1_UNORM:
62 	case PIPE_FORMAT_B5G5R5X1_UNORM:
63 		return FMT_1_5_5_5;
64 	case PIPE_FORMAT_B4G4R4A4_UNORM:
65 	case PIPE_FORMAT_B4G4R4X4_UNORM:
66 		return FMT_4_4_4_4;
67 	case PIPE_FORMAT_Z16_UNORM:
68 		return FMT_16;
69 	case PIPE_FORMAT_L8A8_UNORM:
70 	case PIPE_FORMAT_L8A8_SNORM:
71 	case PIPE_FORMAT_L8A8_UINT:
72 	case PIPE_FORMAT_L8A8_SINT:
73 	case PIPE_FORMAT_L8A8_SRGB:
74 	case PIPE_FORMAT_R8G8_UNORM:
75 	case PIPE_FORMAT_R8G8_SNORM:
76 	case PIPE_FORMAT_R8G8_UINT:
77 	case PIPE_FORMAT_R8G8_SINT:
78 		return FMT_8_8;
79 	case PIPE_FORMAT_R16_UNORM:
80 	case PIPE_FORMAT_R16_SNORM:
81 	case PIPE_FORMAT_R16_UINT:
82 	case PIPE_FORMAT_R16_SINT:
83 	case PIPE_FORMAT_A16_UNORM:
84 	case PIPE_FORMAT_A16_SNORM:
85 	case PIPE_FORMAT_A16_UINT:
86 	case PIPE_FORMAT_A16_SINT:
87 	case PIPE_FORMAT_L16_UNORM:
88 	case PIPE_FORMAT_L16_SNORM:
89 	case PIPE_FORMAT_L16_UINT:
90 	case PIPE_FORMAT_L16_SINT:
91 	case PIPE_FORMAT_I16_UNORM:
92 	case PIPE_FORMAT_I16_SNORM:
93 	case PIPE_FORMAT_I16_UINT:
94 	case PIPE_FORMAT_I16_SINT:
95 		return FMT_16;
96 	case PIPE_FORMAT_R16_FLOAT:
97 	case PIPE_FORMAT_A16_FLOAT:
98 	case PIPE_FORMAT_L16_FLOAT:
99 	case PIPE_FORMAT_I16_FLOAT:
100 		return FMT_16_FLOAT;
101 
102 	/* 32-bit buffers. */
103 	case PIPE_FORMAT_A8B8G8R8_SRGB:
104 	case PIPE_FORMAT_A8B8G8R8_UNORM:
105 	case PIPE_FORMAT_A8R8G8B8_UNORM:
106 	case PIPE_FORMAT_B8G8R8A8_SRGB:
107 	case PIPE_FORMAT_B8G8R8A8_UNORM:
108 	case PIPE_FORMAT_B8G8R8X8_UNORM:
109 	case PIPE_FORMAT_R8G8B8A8_SNORM:
110 	case PIPE_FORMAT_R8G8B8A8_UNORM:
111 	case PIPE_FORMAT_R8G8B8X8_UNORM:
112 	case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
113 	case PIPE_FORMAT_X8B8G8R8_UNORM:
114 	case PIPE_FORMAT_X8R8G8B8_UNORM:
115 	case PIPE_FORMAT_R8G8B8_UNORM:
116 	case PIPE_FORMAT_R8G8B8A8_SINT:
117 	case PIPE_FORMAT_R8G8B8A8_UINT:
118 		return FMT_8_8_8_8;
119 	case PIPE_FORMAT_R10G10B10A2_UNORM:
120 	case PIPE_FORMAT_R10G10B10X2_SNORM:
121 	case PIPE_FORMAT_B10G10R10A2_UNORM:
122 	case PIPE_FORMAT_B10G10R10A2_UINT:
123 	case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
124 		return FMT_2_10_10_10;
125 	case PIPE_FORMAT_Z24X8_UNORM:
126 	case PIPE_FORMAT_Z24_UNORM_S8_UINT:
127 		return FMT_24_8;
128 	case PIPE_FORMAT_R32_UINT:
129 	case PIPE_FORMAT_R32_SINT:
130 	case PIPE_FORMAT_A32_UINT:
131 	case PIPE_FORMAT_A32_SINT:
132 	case PIPE_FORMAT_L32_UINT:
133 	case PIPE_FORMAT_L32_SINT:
134 	case PIPE_FORMAT_I32_UINT:
135 	case PIPE_FORMAT_I32_SINT:
136 		return FMT_32;
137 	case PIPE_FORMAT_R32_FLOAT:
138 	case PIPE_FORMAT_A32_FLOAT:
139 	case PIPE_FORMAT_L32_FLOAT:
140 	case PIPE_FORMAT_I32_FLOAT:
141 	case PIPE_FORMAT_Z32_FLOAT:
142 		return FMT_32_FLOAT;
143 	case PIPE_FORMAT_R16G16_FLOAT:
144 	case PIPE_FORMAT_L16A16_FLOAT:
145 		return FMT_16_16_FLOAT;
146 	case PIPE_FORMAT_R16G16_UNORM:
147 	case PIPE_FORMAT_R16G16_SNORM:
148 	case PIPE_FORMAT_R16G16_UINT:
149 	case PIPE_FORMAT_R16G16_SINT:
150 	case PIPE_FORMAT_L16A16_UNORM:
151 	case PIPE_FORMAT_L16A16_SNORM:
152 	case PIPE_FORMAT_L16A16_UINT:
153 	case PIPE_FORMAT_L16A16_SINT:
154 		return FMT_16_16;
155 
156 	/* 64-bit buffers. */
157 	case PIPE_FORMAT_R16G16B16A16_UINT:
158 	case PIPE_FORMAT_R16G16B16A16_SINT:
159 	case PIPE_FORMAT_R16G16B16A16_UNORM:
160 	case PIPE_FORMAT_R16G16B16A16_SNORM:
161 		return FMT_16_16_16_16;
162 	case PIPE_FORMAT_R16G16B16A16_FLOAT:
163 		return FMT_16_16_16_16_FLOAT;
164 	case PIPE_FORMAT_R32G32_FLOAT:
165 	case PIPE_FORMAT_L32A32_FLOAT:
166 		return FMT_32_32_FLOAT;
167 	case PIPE_FORMAT_R32G32_SINT:
168 	case PIPE_FORMAT_R32G32_UINT:
169 	case PIPE_FORMAT_L32A32_UINT:
170 	case PIPE_FORMAT_L32A32_SINT:
171 		return FMT_32_32;
172 
173 	/* 96-bit buffers. */
174 	case PIPE_FORMAT_R32G32B32_FLOAT:
175 		return FMT_32_32_32_FLOAT;
176 
177 	/* 128-bit buffers. */
178 	case PIPE_FORMAT_R32G32B32A32_SNORM:
179 	case PIPE_FORMAT_R32G32B32A32_UNORM:
180 	case PIPE_FORMAT_R32G32B32A32_SINT:
181 	case PIPE_FORMAT_R32G32B32A32_UINT:
182 		return FMT_32_32_32_32;
183 	case PIPE_FORMAT_R32G32B32A32_FLOAT:
184 		return FMT_32_32_32_32_FLOAT;
185 
186 	/* YUV buffers. */
187 	case PIPE_FORMAT_UYVY:
188 		return FMT_Cr_Y1_Cb_Y0;
189 	case PIPE_FORMAT_YUYV:
190 		return FMT_Y1_Cr_Y0_Cb;
191 
192 	default:
193 		return ~0;
194 	}
195 }
196 
197 enum a2xx_colorformatx
fd2_pipe2color(enum pipe_format format)198 fd2_pipe2color(enum pipe_format format)
199 {
200 	switch (format) {
201 	/* 8-bit buffers. */
202 	case PIPE_FORMAT_A8_UNORM:
203 	case PIPE_FORMAT_A8_SNORM:
204 	case PIPE_FORMAT_A8_UINT:
205 	case PIPE_FORMAT_A8_SINT:
206 	case PIPE_FORMAT_I8_UNORM:
207 	case PIPE_FORMAT_I8_SNORM:
208 	case PIPE_FORMAT_I8_UINT:
209 	case PIPE_FORMAT_I8_SINT:
210 	case PIPE_FORMAT_L8_UNORM:
211 	case PIPE_FORMAT_L8_SNORM:
212 	case PIPE_FORMAT_L8_UINT:
213 	case PIPE_FORMAT_L8_SINT:
214 	case PIPE_FORMAT_L8_SRGB:
215 	case PIPE_FORMAT_R8_UNORM:
216 	case PIPE_FORMAT_R8_SNORM:
217 	case PIPE_FORMAT_R8_UINT:
218 	case PIPE_FORMAT_R8_SINT:
219 		return COLORX_8;
220 
221 	/* 16-bit buffers. */
222 	case PIPE_FORMAT_B5G6R5_UNORM:
223 		return COLORX_5_6_5;
224 	case PIPE_FORMAT_B5G5R5A1_UNORM:
225 	case PIPE_FORMAT_B5G5R5X1_UNORM:
226 		return COLORX_1_5_5_5;
227 	case PIPE_FORMAT_B4G4R4A4_UNORM:
228 	case PIPE_FORMAT_B4G4R4X4_UNORM:
229 		return COLORX_4_4_4_4;
230 	case PIPE_FORMAT_L8A8_UNORM:
231 	case PIPE_FORMAT_L8A8_SNORM:
232 	case PIPE_FORMAT_L8A8_UINT:
233 	case PIPE_FORMAT_L8A8_SINT:
234 	case PIPE_FORMAT_L8A8_SRGB:
235 	case PIPE_FORMAT_R8G8_UNORM:
236 	case PIPE_FORMAT_R8G8_SNORM:
237 	case PIPE_FORMAT_R8G8_UINT:
238 	case PIPE_FORMAT_R8G8_SINT:
239 	case PIPE_FORMAT_Z16_UNORM:
240 		return COLORX_8_8;
241 	case PIPE_FORMAT_R16_FLOAT:
242 	case PIPE_FORMAT_A16_FLOAT:
243 	case PIPE_FORMAT_L16_FLOAT:
244 	case PIPE_FORMAT_I16_FLOAT:
245 		return COLORX_16_FLOAT;
246 
247 	/* 32-bit buffers. */
248 	case PIPE_FORMAT_A8B8G8R8_SRGB:
249 	case PIPE_FORMAT_A8B8G8R8_UNORM:
250 	case PIPE_FORMAT_A8R8G8B8_UNORM:
251 	case PIPE_FORMAT_B8G8R8A8_SRGB:
252 	case PIPE_FORMAT_B8G8R8A8_UNORM:
253 	case PIPE_FORMAT_B8G8R8X8_UNORM:
254 	case PIPE_FORMAT_R8G8B8A8_SNORM:
255 	case PIPE_FORMAT_R8G8B8A8_UNORM:
256 	case PIPE_FORMAT_R8G8B8X8_UNORM:
257 	case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
258 	case PIPE_FORMAT_X8B8G8R8_UNORM:
259 	case PIPE_FORMAT_X8R8G8B8_UNORM:
260 	case PIPE_FORMAT_R8G8B8_UNORM:
261 	case PIPE_FORMAT_R8G8B8A8_SINT:
262 	case PIPE_FORMAT_R8G8B8A8_UINT:
263 	case PIPE_FORMAT_Z24_UNORM_S8_UINT:
264 	case PIPE_FORMAT_Z24X8_UNORM:
265 		return COLORX_8_8_8_8;
266 	case PIPE_FORMAT_R32_FLOAT:
267 	case PIPE_FORMAT_A32_FLOAT:
268 	case PIPE_FORMAT_L32_FLOAT:
269 	case PIPE_FORMAT_I32_FLOAT:
270 	case PIPE_FORMAT_Z32_FLOAT:
271 		return COLORX_32_FLOAT;
272 	case PIPE_FORMAT_R16G16_FLOAT:
273 	case PIPE_FORMAT_L16A16_FLOAT:
274 		return COLORX_16_16_FLOAT;
275 
276 	/* 64-bit buffers. */
277 	case PIPE_FORMAT_R16G16B16A16_FLOAT:
278 		return COLORX_16_16_16_16_FLOAT;
279 	case PIPE_FORMAT_R32G32_FLOAT:
280 	case PIPE_FORMAT_L32A32_FLOAT:
281 		return COLORX_32_32_FLOAT;
282 
283 	/* 128-bit buffers. */
284 	case PIPE_FORMAT_R32G32B32A32_FLOAT:
285 		return COLORX_32_32_32_32_FLOAT;
286 
287 	default:
288 		return ~0;
289 	}
290 }
291 
292 static inline enum sq_tex_swiz
tex_swiz(unsigned swiz)293 tex_swiz(unsigned swiz)
294 {
295 	switch (swiz) {
296 	default:
297 	case PIPE_SWIZZLE_X: return SQ_TEX_X;
298 	case PIPE_SWIZZLE_Y: return SQ_TEX_Y;
299 	case PIPE_SWIZZLE_Z: return SQ_TEX_Z;
300 	case PIPE_SWIZZLE_W: return SQ_TEX_W;
301 	case PIPE_SWIZZLE_0: return SQ_TEX_ZERO;
302 	case PIPE_SWIZZLE_1: return SQ_TEX_ONE;
303 	}
304 }
305 
306 uint32_t
fd2_tex_swiz(enum pipe_format format,unsigned swizzle_r,unsigned swizzle_g,unsigned swizzle_b,unsigned swizzle_a)307 fd2_tex_swiz(enum pipe_format format, unsigned swizzle_r, unsigned swizzle_g,
308 		unsigned swizzle_b, unsigned swizzle_a)
309 {
310 	const struct util_format_description *desc =
311 			util_format_description(format);
312 	uint8_t swiz[] = {
313 			swizzle_r, swizzle_g, swizzle_b, swizzle_a,
314 			PIPE_SWIZZLE_0, PIPE_SWIZZLE_1,
315 			PIPE_SWIZZLE_1, PIPE_SWIZZLE_1,
316 	};
317 
318 	return A2XX_SQ_TEX_3_SWIZ_X(tex_swiz(swiz[desc->swizzle[0]])) |
319 			A2XX_SQ_TEX_3_SWIZ_Y(tex_swiz(swiz[desc->swizzle[1]])) |
320 			A2XX_SQ_TEX_3_SWIZ_Z(tex_swiz(swiz[desc->swizzle[2]])) |
321 			A2XX_SQ_TEX_3_SWIZ_W(tex_swiz(swiz[desc->swizzle[3]]));
322 }
323