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 "texformat.h"
42 * Choose an appropriate texture format given the format, type and
43 * internalFormat parameters passed to glTexImage().
45 * \param ctx the GL context.
46 * \param internalFormat user's prefered internal texture format.
47 * \param format incoming image pixel format.
48 * \param type incoming image data type.
50 * \return a pointer to a gl_texture_format object which describes the
51 * choosen texture format, or NULL on failure.
53 * This is called via dd_function_table::ChooseTextureFormat. Hardware drivers
54 * will typically override this function with a specialized version.
57 _mesa_choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
58 GLenum format
, GLenum type
)
63 switch (internalFormat
) {
71 return MESA_FORMAT_RGBA8888
;
73 return MESA_FORMAT_ARGB1555
;
75 return MESA_FORMAT_ARGB4444_REV
; /* just to test another format*/
77 return MESA_FORMAT_ARGB4444
;
86 return MESA_FORMAT_RGB888
;
88 return MESA_FORMAT_RGB332
;
90 return MESA_FORMAT_RGB565_REV
; /* just to test another format */
92 return MESA_FORMAT_RGB565
;
100 return MESA_FORMAT_A8
;
102 /* Luminance formats */
109 return MESA_FORMAT_L8
;
111 /* Luminance/Alpha formats */
113 case GL_LUMINANCE_ALPHA
:
114 case GL_LUMINANCE4_ALPHA4
:
115 case GL_LUMINANCE6_ALPHA2
:
116 case GL_LUMINANCE8_ALPHA8
:
117 return MESA_FORMAT_AL88
;
119 case GL_LUMINANCE12_ALPHA4
:
120 case GL_LUMINANCE12_ALPHA12
:
121 case GL_LUMINANCE16_ALPHA16
:
122 return MESA_FORMAT_AL1616
;
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
;
297 if (ctx
->VersionMajor
* 10 + ctx
->VersionMinor
>= 31) {
298 switch (internalFormat
) {
301 return MESA_FORMAT_SIGNED_R8
;
304 return MESA_FORMAT_SIGNED_RG88
;
307 return MESA_FORMAT_SIGNED_RGBX8888
;
310 return MESA_FORMAT_SIGNED_RGBA8888
;
312 return MESA_FORMAT_SIGNED_R_16
;
314 return MESA_FORMAT_SIGNED_RG_16
;
316 return MESA_FORMAT_SIGNED_RGB_16
;
317 case GL_RGBA16_SNORM
:
318 return MESA_FORMAT_SIGNED_RGBA_16
;
324 #if FEATURE_EXT_texture_sRGB
325 if (ctx
->Extensions
.EXT_texture_sRGB
) {
326 switch (internalFormat
) {
329 return MESA_FORMAT_SRGB8
;
330 case GL_SRGB_ALPHA_EXT
:
331 case GL_SRGB8_ALPHA8_EXT
:
332 return MESA_FORMAT_SRGBA8
;
333 case GL_SLUMINANCE_EXT
:
334 case GL_SLUMINANCE8_EXT
:
335 return MESA_FORMAT_SL8
;
336 case GL_SLUMINANCE_ALPHA_EXT
:
337 case GL_SLUMINANCE8_ALPHA8_EXT
:
338 return MESA_FORMAT_SLA8
;
339 case GL_COMPRESSED_SLUMINANCE_EXT
:
340 return MESA_FORMAT_SL8
;
341 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
342 return MESA_FORMAT_SLA8
;
343 case GL_COMPRESSED_SRGB_EXT
:
344 #if FEATURE_texture_s3tc
345 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
346 return MESA_FORMAT_SRGB_DXT1
;
348 return MESA_FORMAT_SRGB8
;
349 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
350 #if FEATURE_texture_s3tc
351 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
352 return MESA_FORMAT_SRGBA_DXT3
; /* Not srgba_dxt1, see spec */
354 return MESA_FORMAT_SRGBA8
;
355 #if FEATURE_texture_s3tc
356 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
357 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
358 return MESA_FORMAT_SRGB_DXT1
;
360 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
361 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
362 return MESA_FORMAT_SRGBA_DXT1
;
364 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
365 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
366 return MESA_FORMAT_SRGBA_DXT3
;
368 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
369 if (ctx
->Extensions
.EXT_texture_compression_s3tc
)
370 return MESA_FORMAT_SRGBA_DXT5
;
377 #endif /* FEATURE_EXT_texture_sRGB */
379 _mesa_problem(ctx
, "unexpected format in _mesa_choose_tex_format()");
380 return MESA_FORMAT_NONE
;