Merge branch 'texformat-rework'
[mesa.git] / src / mesa / main / texformat.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.7
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008-2009 VMware, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file texformat.c
29 * Texture formats.
30 *
31 * \author Gareth Hughes
32 * \author Brian Paul
33 */
34
35
36 #include "context.h"
37 #include "texcompress.h"
38 #include "texcompress_fxt1.h"
39 #include "texcompress_s3tc.h"
40 #include "texformat.h"
41
42
43 /**
44 * Choose an appropriate texture format given the format, type and
45 * internalFormat parameters passed to glTexImage().
46 *
47 * \param ctx the GL context.
48 * \param internalFormat user's prefered internal texture format.
49 * \param format incoming image pixel format.
50 * \param type incoming image data type.
51 *
52 * \return a pointer to a gl_texture_format object which describes the
53 * choosen texture format, or NULL on failure.
54 *
55 * This is called via dd_function_table::ChooseTextureFormat. Hardware drivers
56 * will typically override this function with a specialized version.
57 */
58 gl_format
59 _mesa_choose_tex_format( GLcontext *ctx, GLint internalFormat,
60 GLenum format, GLenum type )
61 {
62 (void) format;
63 (void) type;
64
65 switch (internalFormat) {
66 /* RGBA formats */
67 case 4:
68 case GL_RGBA:
69 case GL_RGB10_A2:
70 case GL_RGBA12:
71 case GL_RGBA16:
72 case GL_RGBA8:
73 return MESA_FORMAT_RGBA8888;
74 case GL_RGB5_A1:
75 return MESA_FORMAT_ARGB1555;
76 case GL_RGBA2:
77 return MESA_FORMAT_ARGB4444_REV; /* just to test another format*/
78 case GL_RGBA4:
79 return MESA_FORMAT_ARGB4444;
80
81 /* RGB formats */
82 case 3:
83 case GL_RGB:
84 case GL_RGB10:
85 case GL_RGB12:
86 case GL_RGB16:
87 case GL_RGB8:
88 return MESA_FORMAT_RGB888;
89 case GL_R3_G3_B2:
90 return MESA_FORMAT_RGB332;
91 case GL_RGB4:
92 return MESA_FORMAT_RGB565_REV; /* just to test another format */
93 case GL_RGB5:
94 return MESA_FORMAT_RGB565;
95
96 /* Alpha formats */
97 case GL_ALPHA:
98 case GL_ALPHA4:
99 case GL_ALPHA12:
100 case GL_ALPHA16:
101 case GL_ALPHA8:
102 return MESA_FORMAT_A8;
103
104 /* Luminance formats */
105 case 1:
106 case GL_LUMINANCE:
107 case GL_LUMINANCE4:
108 case GL_LUMINANCE12:
109 case GL_LUMINANCE16:
110 case GL_LUMINANCE8:
111 return MESA_FORMAT_L8;
112
113 /* Luminance/Alpha formats */
114 case 2:
115 case GL_LUMINANCE_ALPHA:
116 case GL_LUMINANCE4_ALPHA4:
117 case GL_LUMINANCE6_ALPHA2:
118 case GL_LUMINANCE12_ALPHA4:
119 case GL_LUMINANCE12_ALPHA12:
120 case GL_LUMINANCE16_ALPHA16:
121 case GL_LUMINANCE8_ALPHA8:
122 return MESA_FORMAT_AL88;
123
124 case GL_INTENSITY:
125 case GL_INTENSITY4:
126 case GL_INTENSITY12:
127 case GL_INTENSITY16:
128 case GL_INTENSITY8:
129 return MESA_FORMAT_I8;
130
131 case GL_COLOR_INDEX:
132 case GL_COLOR_INDEX1_EXT:
133 case GL_COLOR_INDEX2_EXT:
134 case GL_COLOR_INDEX4_EXT:
135 case GL_COLOR_INDEX12_EXT:
136 case GL_COLOR_INDEX16_EXT:
137 case GL_COLOR_INDEX8_EXT:
138 return MESA_FORMAT_CI8;
139
140 default:
141 ; /* fallthrough */
142 }
143
144 if (ctx->Extensions.ARB_depth_texture) {
145 switch (internalFormat) {
146 case GL_DEPTH_COMPONENT:
147 case GL_DEPTH_COMPONENT24:
148 case GL_DEPTH_COMPONENT32:
149 return MESA_FORMAT_Z32;
150 case GL_DEPTH_COMPONENT16:
151 return MESA_FORMAT_Z16;
152 default:
153 ; /* fallthrough */
154 }
155 }
156
157 switch (internalFormat) {
158 case GL_COMPRESSED_ALPHA_ARB:
159 return MESA_FORMAT_A8;
160 case GL_COMPRESSED_LUMINANCE_ARB:
161 return MESA_FORMAT_L8;
162 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
163 return MESA_FORMAT_AL88;
164 case GL_COMPRESSED_INTENSITY_ARB:
165 return MESA_FORMAT_I8;
166 case GL_COMPRESSED_RGB_ARB:
167 if (ctx->Extensions.EXT_texture_compression_s3tc ||
168 ctx->Extensions.S3_s3tc)
169 return MESA_FORMAT_RGB_DXT1;
170 if (ctx->Extensions.TDFX_texture_compression_FXT1)
171 return MESA_FORMAT_RGB_FXT1;
172 return MESA_FORMAT_RGB888;
173 case GL_COMPRESSED_RGBA_ARB:
174 if (ctx->Extensions.EXT_texture_compression_s3tc ||
175 ctx->Extensions.S3_s3tc)
176 return MESA_FORMAT_RGBA_DXT3; /* Not rgba_dxt1, see spec */
177 if (ctx->Extensions.TDFX_texture_compression_FXT1)
178 return MESA_FORMAT_RGBA_FXT1;
179 return MESA_FORMAT_RGBA8888;
180 default:
181 ; /* fallthrough */
182 }
183
184 if (ctx->Extensions.MESA_ycbcr_texture) {
185 if (internalFormat == GL_YCBCR_MESA) {
186 if (type == GL_UNSIGNED_SHORT_8_8_MESA)
187 return MESA_FORMAT_YCBCR;
188 else
189 return MESA_FORMAT_YCBCR_REV;
190 }
191 }
192
193 #if FEATURE_texture_fxt1
194 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
195 switch (internalFormat) {
196 case GL_COMPRESSED_RGB_FXT1_3DFX:
197 return MESA_FORMAT_RGB_FXT1;
198 case GL_COMPRESSED_RGBA_FXT1_3DFX:
199 return MESA_FORMAT_RGBA_FXT1;
200 default:
201 ; /* fallthrough */
202 }
203 }
204 #endif
205
206 #if FEATURE_texture_s3tc
207 if (ctx->Extensions.EXT_texture_compression_s3tc) {
208 switch (internalFormat) {
209 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
210 return MESA_FORMAT_RGB_DXT1;
211 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
212 return MESA_FORMAT_RGBA_DXT1;
213 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
214 return MESA_FORMAT_RGBA_DXT3;
215 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
216 return MESA_FORMAT_RGBA_DXT5;
217 default:
218 ; /* fallthrough */
219 }
220 }
221
222 if (ctx->Extensions.S3_s3tc) {
223 switch (internalFormat) {
224 case GL_RGB_S3TC:
225 case GL_RGB4_S3TC:
226 return MESA_FORMAT_RGB_DXT1;
227 case GL_RGBA_S3TC:
228 case GL_RGBA4_S3TC:
229 return MESA_FORMAT_RGBA_DXT3;
230 default:
231 ; /* fallthrough */
232 }
233 }
234 #endif
235
236 if (ctx->Extensions.ARB_texture_float) {
237 switch (internalFormat) {
238 case GL_ALPHA16F_ARB:
239 return MESA_FORMAT_ALPHA_FLOAT16;
240 case GL_ALPHA32F_ARB:
241 return MESA_FORMAT_ALPHA_FLOAT32;
242 case GL_LUMINANCE16F_ARB:
243 return MESA_FORMAT_LUMINANCE_FLOAT16;
244 case GL_LUMINANCE32F_ARB:
245 return MESA_FORMAT_LUMINANCE_FLOAT32;
246 case GL_LUMINANCE_ALPHA16F_ARB:
247 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
248 case GL_LUMINANCE_ALPHA32F_ARB:
249 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
250 case GL_INTENSITY16F_ARB:
251 return MESA_FORMAT_INTENSITY_FLOAT16;
252 case GL_INTENSITY32F_ARB:
253 return MESA_FORMAT_INTENSITY_FLOAT32;
254 case GL_RGB16F_ARB:
255 return MESA_FORMAT_RGB_FLOAT16;
256 case GL_RGB32F_ARB:
257 return MESA_FORMAT_RGB_FLOAT32;
258 case GL_RGBA16F_ARB:
259 return MESA_FORMAT_RGBA_FLOAT16;
260 case GL_RGBA32F_ARB:
261 return MESA_FORMAT_RGBA_FLOAT32;
262 default:
263 ; /* fallthrough */
264 }
265 }
266
267 if (ctx->Extensions.EXT_packed_depth_stencil) {
268 switch (internalFormat) {
269 case GL_DEPTH_STENCIL_EXT:
270 case GL_DEPTH24_STENCIL8_EXT:
271 return MESA_FORMAT_Z24_S8;
272 default:
273 ; /* fallthrough */
274 }
275 }
276
277 if (ctx->Extensions.ATI_envmap_bumpmap) {
278 switch (internalFormat) {
279 case GL_DUDV_ATI:
280 case GL_DU8DV8_ATI:
281 return MESA_FORMAT_DUDV8;
282 default:
283 ; /* fallthrough */
284 }
285 }
286
287 if (ctx->Extensions.MESA_texture_signed_rgba) {
288 switch (internalFormat) {
289 case GL_RGBA_SNORM:
290 case GL_RGBA8_SNORM:
291 return MESA_FORMAT_SIGNED_RGBA8888;
292 default:
293 ; /* fallthrough */
294 }
295 }
296
297
298 #if FEATURE_EXT_texture_sRGB
299 if (ctx->Extensions.EXT_texture_sRGB) {
300 switch (internalFormat) {
301 case GL_SRGB_EXT:
302 case GL_SRGB8_EXT:
303 return MESA_FORMAT_SRGB8;
304 case GL_SRGB_ALPHA_EXT:
305 case GL_SRGB8_ALPHA8_EXT:
306 return MESA_FORMAT_SRGBA8;
307 case GL_SLUMINANCE_EXT:
308 case GL_SLUMINANCE8_EXT:
309 return MESA_FORMAT_SL8;
310 case GL_SLUMINANCE_ALPHA_EXT:
311 case GL_SLUMINANCE8_ALPHA8_EXT:
312 return MESA_FORMAT_SLA8;
313 case GL_COMPRESSED_SLUMINANCE_EXT:
314 return MESA_FORMAT_SL8;
315 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
316 return MESA_FORMAT_SLA8;
317 case GL_COMPRESSED_SRGB_EXT:
318 #if FEATURE_texture_s3tc
319 if (ctx->Extensions.EXT_texture_compression_s3tc)
320 return MESA_FORMAT_SRGB_DXT1;
321 #endif
322 return MESA_FORMAT_SRGB8;
323 case GL_COMPRESSED_SRGB_ALPHA_EXT:
324 #if FEATURE_texture_s3tc
325 if (ctx->Extensions.EXT_texture_compression_s3tc)
326 return MESA_FORMAT_SRGBA_DXT3; /* Not srgba_dxt1, see spec */
327 #endif
328 return MESA_FORMAT_SRGBA8;
329 #if FEATURE_texture_s3tc
330 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
331 if (ctx->Extensions.EXT_texture_compression_s3tc)
332 return MESA_FORMAT_SRGB_DXT1;
333 break;
334 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
335 if (ctx->Extensions.EXT_texture_compression_s3tc)
336 return MESA_FORMAT_SRGBA_DXT1;
337 break;
338 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
339 if (ctx->Extensions.EXT_texture_compression_s3tc)
340 return MESA_FORMAT_SRGBA_DXT3;
341 break;
342 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
343 if (ctx->Extensions.EXT_texture_compression_s3tc)
344 return MESA_FORMAT_SRGBA_DXT5;
345 break;
346 #endif
347 default:
348 ; /* fallthrough */
349 }
350 }
351 #endif /* FEATURE_EXT_texture_sRGB */
352
353 _mesa_problem(ctx, "unexpected format in _mesa_choose_tex_format()");
354 return MESA_FORMAT_NONE;
355 }
356