2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008-2009 VMware, Inc.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
31 * \author Gareth Hughes
37 #include "texcompress.h"
38 #include "texcompress_fxt1.h"
39 #include "texcompress_s3tc.h"
40 #include "texformat.h"
44 * Choose an appropriate texture format given the format, type and
45 * internalFormat parameters passed to glTexImage().
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.
52 * \return a pointer to a gl_texture_format object which describes the
53 * choosen texture format, or NULL on failure.
55 * This is called via dd_function_table::ChooseTextureFormat. Hardware drivers
56 * will typically override this function with a specialized version.
59 _mesa_choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
60 GLenum format
, GLenum type
)
65 switch (internalFormat
) {
73 return MESA_FORMAT_RGBA8888
;
75 return MESA_FORMAT_ARGB1555
;
77 return MESA_FORMAT_ARGB4444_REV
; /* just to test another format*/
79 return MESA_FORMAT_ARGB4444
;
88 return MESA_FORMAT_RGB888
;
90 return MESA_FORMAT_RGB332
;
92 return MESA_FORMAT_RGB565_REV
; /* just to test another format */
94 return MESA_FORMAT_RGB565
;
102 return MESA_FORMAT_A8
;
104 /* Luminance formats */
111 return MESA_FORMAT_L8
;
113 /* Luminance/Alpha formats */
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
;
129 return MESA_FORMAT_I8
;
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
;
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
;
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
;
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
;
189 return MESA_FORMAT_YCBCR_REV
;
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
;
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
;
222 if (ctx
->Extensions
.S3_s3tc
) {
223 switch (internalFormat
) {
226 return MESA_FORMAT_RGB_DXT1
;
229 return MESA_FORMAT_RGBA_DXT3
;
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
;
255 return MESA_FORMAT_RGB_FLOAT16
;
257 return MESA_FORMAT_RGB_FLOAT32
;
259 return MESA_FORMAT_RGBA_FLOAT16
;
261 return MESA_FORMAT_RGBA_FLOAT32
;
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
;
277 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
278 switch (internalFormat
) {
281 return MESA_FORMAT_DUDV8
;
287 if (ctx
->Extensions
.MESA_texture_signed_rgba
) {
288 switch (internalFormat
) {
291 return MESA_FORMAT_SIGNED_RGBA8888
;
298 #if FEATURE_EXT_texture_sRGB
299 if (ctx
->Extensions
.EXT_texture_sRGB
) {
300 switch (internalFormat
) {
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
;
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 */
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
;
334 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
335 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
336 return MESA_FORMAT_SRGBA_DXT1
;
338 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
339 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
340 return MESA_FORMAT_SRGBA_DXT3
;
342 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
343 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
344 return MESA_FORMAT_SRGBA_DXT5
;
351 #endif /* FEATURE_EXT_texture_sRGB */
353 _mesa_problem(ctx
, "unexpected format in _mesa_choose_tex_format()");
354 return MESA_FORMAT_NONE
;