2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008 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.
29 * Helper functions for texture compression.
37 #include "mfeatures.h"
39 #include "texcompress.h"
43 * Get the GL base format of a specified GL compressed texture format
45 * From page 232 of the OpenGL 3.3 (Compatiblity Profile) spec:
47 * "Compressed Internal Format Base Internal Format Type
48 * --------------------------- -------------------- ---------
49 * COMPRESSED_ALPHA ALPHA Generic
50 * COMPRESSED_LUMINANCE LUMINANCE Generic
51 * COMPRESSED_LUMINANCE_ALPHA LUMINANCE_ALPHA Generic
52 * COMPRESSED_INTENSITY INTENSITY Generic
53 * COMPRESSED_RED RED Generic
54 * COMPRESSED_RG RG Generic
55 * COMPRESSED_RGB RGB Generic
56 * COMPRESSED_RGBA RGBA Generic
57 * COMPRESSED_SRGB RGB Generic
58 * COMPRESSED_SRGB_ALPHA RGBA Generic
59 * COMPRESSED_SLUMINANCE LUMINANCE Generic
60 * COMPRESSED_SLUMINANCE_ALPHA LUMINANCE_ALPHA Generic
61 * COMPRESSED_RED_RGTC1 RED Specific
62 * COMPRESSED_SIGNED_RED_RGTC1 RED Specific
63 * COMPRESSED_RG_RGTC2 RG Specific
64 * COMPRESSED_SIGNED_RG_RGTC2 RG Specific"
67 * The base format of \c format if \c format is a compressed format (either
68 * generic or specific. Otherwise 0 is returned.
71 _mesa_gl_compressed_format_base_format(GLenum format
)
74 case GL_COMPRESSED_RED
:
75 case GL_COMPRESSED_RED_RGTC1
:
76 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
79 case GL_COMPRESSED_RG
:
80 case GL_COMPRESSED_RG_RGTC2
:
81 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
84 case GL_COMPRESSED_RGB
:
85 case GL_COMPRESSED_SRGB
:
86 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
87 case GL_COMPRESSED_RGB_FXT1_3DFX
:
88 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
91 case GL_COMPRESSED_RGBA
:
92 case GL_COMPRESSED_SRGB_ALPHA
:
93 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB
:
94 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB
:
95 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB
:
96 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB
:
97 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
98 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
99 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
100 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
101 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
102 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
103 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
106 case GL_COMPRESSED_ALPHA
:
109 case GL_COMPRESSED_LUMINANCE
:
110 case GL_COMPRESSED_SLUMINANCE
:
111 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
112 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
115 case GL_COMPRESSED_LUMINANCE_ALPHA
:
116 case GL_COMPRESSED_SLUMINANCE_ALPHA
:
117 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
118 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
119 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
120 return GL_LUMINANCE_ALPHA
;
122 case GL_COMPRESSED_INTENSITY
:
131 * Return list of (and count of) all specific texture compression
132 * formats that are supported.
134 * \param ctx the GL context
135 * \param formats the resulting format list (may be NULL).
136 * \param all if true return all formats, even those with some kind
137 * of restrictions/limitations (See GL_ARB_texture_compression
138 * spec for more info).
140 * \return number of formats.
143 _mesa_get_compressed_formats(struct gl_context
*ctx
, GLint
*formats
, GLboolean all
)
146 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
148 formats
[n
++] = GL_COMPRESSED_RGB_FXT1_3DFX
;
149 formats
[n
++] = GL_COMPRESSED_RGBA_FXT1_3DFX
;
155 /* don't return RGTC - ARB_texture_compression_rgtc query 19 */
156 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
158 formats
[n
++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
159 /* This format has some restrictions/limitations and so should
160 * not be returned via the GL_COMPRESSED_TEXTURE_FORMATS query.
161 * Specifically, all transparent pixels become black. NVIDIA
162 * omits this format too.
165 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
;
166 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
167 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
175 if (ctx
->Extensions
.S3_s3tc
) {
177 formats
[n
++] = GL_RGB_S3TC
;
178 formats
[n
++] = GL_RGB4_S3TC
;
179 formats
[n
++] = GL_RGBA_S3TC
;
180 formats
[n
++] = GL_RGBA4_S3TC
;
186 #if FEATURE_EXT_texture_sRGB
187 if (ctx
->Extensions
.EXT_texture_sRGB
) {
189 formats
[n
++] = GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
;
190 formats
[n
++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
;
191 formats
[n
++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
;
192 formats
[n
++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
;
198 #endif /* FEATURE_EXT_texture_sRGB */
201 #if FEATURE_ES1 || FEATURE_ES2
203 formats
[n
++] = GL_PALETTE4_RGB8_OES
;
204 formats
[n
++] = GL_PALETTE4_RGBA8_OES
;
205 formats
[n
++] = GL_PALETTE4_R5_G6_B5_OES
;
206 formats
[n
++] = GL_PALETTE4_RGBA4_OES
;
207 formats
[n
++] = GL_PALETTE4_RGB5_A1_OES
;
208 formats
[n
++] = GL_PALETTE8_RGB8_OES
;
209 formats
[n
++] = GL_PALETTE8_RGBA8_OES
;
210 formats
[n
++] = GL_PALETTE8_R5_G6_B5_OES
;
211 formats
[n
++] = GL_PALETTE8_RGBA4_OES
;
212 formats
[n
++] = GL_PALETTE8_RGB5_A1_OES
;
222 * Convert a compressed MESA_FORMAT_x to a GLenum.
225 _mesa_glenum_to_compressed_format(GLenum format
)
228 case GL_COMPRESSED_RGB_FXT1_3DFX
:
229 return MESA_FORMAT_RGB_FXT1
;
230 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
231 return MESA_FORMAT_RGBA_FXT1
;
233 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
235 return MESA_FORMAT_RGB_DXT1
;
236 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
238 return MESA_FORMAT_RGBA_DXT1
;
239 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
241 return MESA_FORMAT_RGBA_DXT3
;
242 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
244 return MESA_FORMAT_RGBA_DXT5
;
246 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
247 return MESA_FORMAT_SRGB_DXT1
;
248 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
249 return MESA_FORMAT_SRGBA_DXT1
;
250 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
251 return MESA_FORMAT_SRGBA_DXT3
;
252 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
253 return MESA_FORMAT_SRGBA_DXT5
;
255 case GL_COMPRESSED_RED_RGTC1
:
256 return MESA_FORMAT_RED_RGTC1
;
257 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
258 return MESA_FORMAT_SIGNED_RED_RGTC1
;
259 case GL_COMPRESSED_RG_RGTC2
:
260 return MESA_FORMAT_RG_RGTC2
;
261 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
262 return MESA_FORMAT_SIGNED_RG_RGTC2
;
264 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
265 return MESA_FORMAT_L_LATC1
;
266 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
267 return MESA_FORMAT_SIGNED_L_LATC1
;
268 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
269 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
270 return MESA_FORMAT_LA_LATC2
;
271 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
272 return MESA_FORMAT_SIGNED_LA_LATC2
;
275 return MESA_FORMAT_NONE
;
281 * Given a compressed MESA_FORMAT_x value, return the corresponding
282 * GLenum for that format.
283 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
284 * which must return the specific texture format used when the user might
285 * have originally specified a generic compressed format in their
286 * glTexImage2D() call.
287 * For non-compressed textures, we always return the user-specified
288 * internal format unchanged.
291 _mesa_compressed_format_to_glenum(struct gl_context
*ctx
, GLuint mesaFormat
)
293 switch (mesaFormat
) {
294 #if FEATURE_texture_fxt1
295 case MESA_FORMAT_RGB_FXT1
:
296 return GL_COMPRESSED_RGB_FXT1_3DFX
;
297 case MESA_FORMAT_RGBA_FXT1
:
298 return GL_COMPRESSED_RGBA_FXT1_3DFX
;
300 #if FEATURE_texture_s3tc
301 case MESA_FORMAT_RGB_DXT1
:
302 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
303 case MESA_FORMAT_RGBA_DXT1
:
304 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
;
305 case MESA_FORMAT_RGBA_DXT3
:
306 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
307 case MESA_FORMAT_RGBA_DXT5
:
308 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
309 #if FEATURE_EXT_texture_sRGB
310 case MESA_FORMAT_SRGB_DXT1
:
311 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
;
312 case MESA_FORMAT_SRGBA_DXT1
:
313 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
;
314 case MESA_FORMAT_SRGBA_DXT3
:
315 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
;
316 case MESA_FORMAT_SRGBA_DXT5
:
317 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
;
321 case MESA_FORMAT_RED_RGTC1
:
322 return GL_COMPRESSED_RED_RGTC1
;
323 case MESA_FORMAT_SIGNED_RED_RGTC1
:
324 return GL_COMPRESSED_SIGNED_RED_RGTC1
;
325 case MESA_FORMAT_RG_RGTC2
:
326 return GL_COMPRESSED_RG_RGTC2
;
327 case MESA_FORMAT_SIGNED_RG_RGTC2
:
328 return GL_COMPRESSED_SIGNED_RG_RGTC2
;
330 case MESA_FORMAT_L_LATC1
:
331 return GL_COMPRESSED_LUMINANCE_LATC1_EXT
;
332 case MESA_FORMAT_SIGNED_L_LATC1
:
333 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
;
334 case MESA_FORMAT_LA_LATC2
:
335 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
;
336 case MESA_FORMAT_SIGNED_LA_LATC2
:
337 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
;
340 _mesa_problem(ctx
, "Unexpected mesa texture format in"
341 " _mesa_compressed_format_to_glenum()");
348 * Return the address of the pixel at (col, row, img) in a
349 * compressed texture image.
350 * \param col, row, img - image position (3D), should be a multiple of the
351 * format's block size.
352 * \param format - compressed image format
353 * \param width - image width (stride) in pixels
354 * \param image - the image address
355 * \return address of pixel at (row, col, img)
358 _mesa_compressed_image_address(GLint col
, GLint row
, GLint img
,
359 gl_format mesaFormat
,
360 GLsizei width
, const GLubyte
*image
)
362 /* XXX only 2D images implemented, not 3D */
363 const GLuint blockSize
= _mesa_get_format_bytes(mesaFormat
);
367 _mesa_get_format_block_size(mesaFormat
, &bw
, &bh
);
369 ASSERT(col
% bw
== 0);
370 ASSERT(row
% bh
== 0);
372 offset
= ((width
+ bw
- 1) / bw
) * (row
/ bh
) + col
/ bw
;
375 return (GLubyte
*) image
+ offset
;