2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR 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
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * Helper functions for texture compression.
39 #include "texcompress.h"
40 #include "texcompress_fxt1.h"
41 #include "texcompress_rgtc.h"
42 #include "texcompress_s3tc.h"
43 #include "texcompress_etc.h"
44 #include "texcompress_bptc.h"
48 * Get the GL base format of a specified GL compressed texture format
50 * From page 232 of the OpenGL 3.3 (Compatiblity Profile) spec:
52 * "Compressed Internal Format Base Internal Format Type
53 * --------------------------- -------------------- ---------
54 * COMPRESSED_ALPHA ALPHA Generic
55 * COMPRESSED_LUMINANCE LUMINANCE Generic
56 * COMPRESSED_LUMINANCE_ALPHA LUMINANCE_ALPHA Generic
57 * COMPRESSED_INTENSITY INTENSITY Generic
58 * COMPRESSED_RED RED Generic
59 * COMPRESSED_RG RG Generic
60 * COMPRESSED_RGB RGB Generic
61 * COMPRESSED_RGBA RGBA Generic
62 * COMPRESSED_SRGB RGB Generic
63 * COMPRESSED_SRGB_ALPHA RGBA Generic
64 * COMPRESSED_SLUMINANCE LUMINANCE Generic
65 * COMPRESSED_SLUMINANCE_ALPHA LUMINANCE_ALPHA Generic
66 * COMPRESSED_RED_RGTC1 RED Specific
67 * COMPRESSED_SIGNED_RED_RGTC1 RED Specific
68 * COMPRESSED_RG_RGTC2 RG Specific
69 * COMPRESSED_SIGNED_RG_RGTC2 RG Specific"
72 * The base format of \c format if \c format is a compressed format (either
73 * generic or specific. Otherwise 0 is returned.
76 _mesa_gl_compressed_format_base_format(GLenum format
)
79 case GL_COMPRESSED_RED
:
80 case GL_COMPRESSED_R11_EAC
:
81 case GL_COMPRESSED_RED_RGTC1
:
82 case GL_COMPRESSED_SIGNED_R11_EAC
:
83 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
86 case GL_COMPRESSED_RG
:
87 case GL_COMPRESSED_RG11_EAC
:
88 case GL_COMPRESSED_RG_RGTC2
:
89 case GL_COMPRESSED_SIGNED_RG11_EAC
:
90 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
93 case GL_COMPRESSED_RGB
:
94 case GL_COMPRESSED_SRGB
:
95 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB
:
96 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB
:
97 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
98 case GL_COMPRESSED_RGB_FXT1_3DFX
:
99 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
100 case GL_ETC1_RGB8_OES
:
101 case GL_COMPRESSED_RGB8_ETC2
:
102 case GL_COMPRESSED_SRGB8_ETC2
:
105 case GL_PALETTE4_RGB8_OES
:
106 case GL_PALETTE4_R5_G6_B5_OES
:
107 case GL_PALETTE8_RGB8_OES
:
108 case GL_PALETTE8_R5_G6_B5_OES
:
111 case GL_COMPRESSED_RGBA
:
112 case GL_COMPRESSED_SRGB_ALPHA
:
113 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB
:
114 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB
:
115 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
116 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
117 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
118 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
119 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
120 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
121 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
122 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
123 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
124 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
125 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
128 case GL_PALETTE4_RGBA8_OES
:
129 case GL_PALETTE8_RGB5_A1_OES
:
130 case GL_PALETTE4_RGBA4_OES
:
131 case GL_PALETTE4_RGB5_A1_OES
:
132 case GL_PALETTE8_RGBA8_OES
:
133 case GL_PALETTE8_RGBA4_OES
:
136 case GL_COMPRESSED_ALPHA
:
139 case GL_COMPRESSED_LUMINANCE
:
140 case GL_COMPRESSED_SLUMINANCE
:
141 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
142 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
145 case GL_COMPRESSED_LUMINANCE_ALPHA
:
146 case GL_COMPRESSED_SLUMINANCE_ALPHA
:
147 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
148 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
149 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
150 return GL_LUMINANCE_ALPHA
;
152 case GL_COMPRESSED_INTENSITY
:
161 * Return list of (and count of) all specific texture compression
162 * formats that are supported.
164 * Some formats are \b not returned by this function. The
165 * \c GL_COMPRESSED_TEXTURE_FORMATS query only returns formats that are
166 * "suitable for general-purpose usage." All texture compression extensions
167 * have taken this to mean either linear RGB or linear RGBA.
169 * The GL_ARB_texture_compress_rgtc spec says:
171 * "19) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
172 * GL_COMPRESSED_TEXTURE_FORMATS queries return the RGTC formats?
176 * The OpenGL 2.1 specification says "The only values returned
177 * by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
178 * corresponding to formats suitable for general-purpose usage.
179 * The renderer will not enumerate formats with restrictions that
180 * need to be specifically understood prior to use."
182 * Compressed textures with just red or red-green components are
183 * not general-purpose so should not be returned by these queries
184 * because they have restrictions.
186 * Applications that seek to use the RGTC formats should do so
187 * by looking for this extension's name in the string returned by
188 * glGetString(GL_EXTENSIONS) rather than
189 * what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
190 * GL_COMPRESSED_TEXTURE_FORMATS return."
192 * There is nearly identical wording in the GL_EXT_texture_compression_rgtc
195 * The GL_EXT_texture_rRGB spec says:
197 * "22) Should the new COMPRESSED_SRGB_* formats be listed in an
198 * implementation's GL_COMPRESSED_TEXTURE_FORMATS list?
200 * RESOLVED: No. Section 3.8.1 says formats listed by
201 * GL_COMPRESSED_TEXTURE_FORMATS are "suitable for general-purpose
202 * usage." The non-linear distribution of red, green, and
203 * blue for these sRGB compressed formats makes them not really
206 * The GL_EXT_texture_compression_latc spec says:
208 * "16) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
209 * GL_COMPRESSED_TEXTURE_FORMATS queries return the LATC formats?
213 * The OpenGL 2.1 specification says "The only values returned
214 * by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
215 * corresponding to formats suitable for general-purpose usage.
216 * The renderer will not enumerate formats with restrictions that
217 * need to be specifically understood prior to use."
219 * Historically, OpenGL implementation have advertised the RGB and
220 * RGBA versions of the S3TC extensions compressed format tokens
221 * through this mechanism.
223 * The specification is not sufficiently clear about what "suitable
224 * for general-purpose usage" means. Historically that seems to mean
225 * unsigned RGB or unsigned RGBA. The DXT1 format supporting alpha
226 * (GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) is not exposed in the list (at
227 * least for NVIDIA drivers) because the alpha is always 1.0 expect
228 * when it is 0.0 when RGB is required to be black. NVIDIA's even
229 * limits itself to true linear RGB or RGBA formats, specifically
230 * not including EXT_texture_sRGB's sRGB S3TC compressed formats.
232 * Adding luminance and luminance-alpha texture formats (and
233 * certainly signed versions of luminance and luminance-alpha
234 * formats!) invites potential comptaibility problems with old
235 * applications using this mechanism since old applications are
236 * unlikely to expect non-RGB or non-RGBA formats to be advertised
237 * through this mechanism. However no specific misinteractions
238 * with old applications is known.
240 * Applications that seek to use the LATC formats should do so
241 * by looking for this extension's name in the string returned by
242 * glGetString(GL_EXTENSIONS) rather than
243 * what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
244 * GL_COMPRESSED_TEXTURE_FORMATS return."
246 * There is no formal spec for GL_ATI_texture_compression_3dc. Since the
247 * formats added by this extension are luminance-alpha formats, it is
248 * reasonable to expect them to follow the same rules as
249 * GL_EXT_texture_compression_latc. At the very least, Catalyst 11.6 does not
250 * expose the 3dc formats through this mechanism.
252 * The spec for GL_ARB_texture_compression_bptc doesn't mention whether it
253 * should be included in GL_COMPRESSED_TEXTURE_FORMATS. However as it takes a
254 * very long time to compress the textures in this format it's probably not
255 * very useful as a general format where the GL will have to compress it on
258 * \param ctx the GL context
259 * \param formats the resulting format list (may be NULL).
261 * \return number of formats.
264 _mesa_get_compressed_formats(struct gl_context
*ctx
, GLint
*formats
)
267 if (_mesa_is_desktop_gl(ctx
) &&
268 ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
270 formats
[n
++] = GL_COMPRESSED_RGB_FXT1_3DFX
;
271 formats
[n
++] = GL_COMPRESSED_RGBA_FXT1_3DFX
;
278 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
280 formats
[n
++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
281 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
282 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
288 /* The ES and desktop GL specs diverge here.
290 * In desktop OpenGL, the driver can perform online compression of
291 * uncompressed texture data. GL_NUM_COMPRESSED_TEXTURE_FORMATS and
292 * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
293 * formats that it could ask the driver to compress with some
294 * expectation of quality. The GL_ARB_texture_compression spec
295 * calls this "suitable for general-purpose usage." As noted
296 * above, this means GL_COMPRESSED_RGBA_S3TC_DXT1_EXT is not
297 * included in the list.
299 * In OpenGL ES, the driver never performs compression.
300 * GL_NUM_COMPRESSED_TEXTURE_FORMATS and
301 * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
302 * formats that the driver can receive from the application. It
303 * is the *complete* list of formats. The
304 * GL_EXT_texture_compression_s3tc spec says:
306 * "New State for OpenGL ES 2.0.25 and 3.0.2 Specifications
308 * The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
309 * COMPRESSED_TEXTURE_FORMATS include
310 * COMPRESSED_RGB_S3TC_DXT1_EXT,
311 * COMPRESSED_RGBA_S3TC_DXT1_EXT,
312 * COMPRESSED_RGBA_S3TC_DXT3_EXT, and
313 * COMPRESSED_RGBA_S3TC_DXT5_EXT."
315 * Note that the addition is only to the OpenGL ES specification!
317 if (_mesa_is_gles(ctx
)) {
319 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
;
326 /* The GL_OES_compressed_ETC1_RGB8_texture spec says:
330 * The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
331 * COMPRESSED_TEXTURE_FORMATS include ETC1_RGB8_OES."
333 if (_mesa_is_gles(ctx
)
334 && ctx
->Extensions
.OES_compressed_ETC1_RGB8_texture
) {
336 formats
[n
++] = GL_ETC1_RGB8_OES
;
343 if (ctx
->API
== API_OPENGLES
) {
345 formats
[n
++] = GL_PALETTE4_RGB8_OES
;
346 formats
[n
++] = GL_PALETTE4_RGBA8_OES
;
347 formats
[n
++] = GL_PALETTE4_R5_G6_B5_OES
;
348 formats
[n
++] = GL_PALETTE4_RGBA4_OES
;
349 formats
[n
++] = GL_PALETTE4_RGB5_A1_OES
;
350 formats
[n
++] = GL_PALETTE8_RGB8_OES
;
351 formats
[n
++] = GL_PALETTE8_RGBA8_OES
;
352 formats
[n
++] = GL_PALETTE8_R5_G6_B5_OES
;
353 formats
[n
++] = GL_PALETTE8_RGBA4_OES
;
354 formats
[n
++] = GL_PALETTE8_RGB5_A1_OES
;
361 if (_mesa_is_gles3(ctx
)) {
363 formats
[n
++] = GL_COMPRESSED_RGB8_ETC2
;
364 formats
[n
++] = GL_COMPRESSED_SRGB8_ETC2
;
365 formats
[n
++] = GL_COMPRESSED_RGBA8_ETC2_EAC
;
366 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
;
367 formats
[n
++] = GL_COMPRESSED_R11_EAC
;
368 formats
[n
++] = GL_COMPRESSED_RG11_EAC
;
369 formats
[n
++] = GL_COMPRESSED_SIGNED_R11_EAC
;
370 formats
[n
++] = GL_COMPRESSED_SIGNED_RG11_EAC
;
371 formats
[n
++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
;
372 formats
[n
++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
;
379 /* The KHR_texture_compression_astc_hdr spec says:
381 * "Interactions with OpenGL 4.2
383 * OpenGL 4.2 supports the feature that compressed textures can be
384 * compressed online, by passing the compressed texture format enum as
385 * the internal format when uploading a texture using TexImage1D,
386 * TexImage2D or TexImage3D (see Section 3.9.3, Texture Image
387 * Specification, subsection Encoding of Special Internal Formats).
389 * Due to the complexity of the ASTC compression algorithm, it is not
390 * usually suitable for online use, and therefore ASTC support will be
391 * limited to pre-compressed textures only. Where on-device compression
392 * is required, a domain-specific limited compressor will typically
393 * be used, and this is therefore not suitable for implementation in
396 * In particular, the ASTC format specifiers will not be added to
397 * Table 3.14, and thus will not be accepted by the TexImage*D
398 * functions, and will not be returned by the (already deprecated)
399 * COMPRESSED_TEXTURE_FORMATS query."
401 * The ES and the desktop specs diverge here. In OpenGL ES, the COMPRESSED_TEXTURE_FORMATS
402 * query returns the set of supported specific compressed formats.
404 if (ctx
->API
== API_OPENGLES2
&&
405 ctx
->Extensions
.KHR_texture_compression_astc_ldr
) {
407 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_4x4_KHR
;
408 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_5x4_KHR
;
409 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_5x5_KHR
;
410 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_6x5_KHR
;
411 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_6x6_KHR
;
412 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_8x5_KHR
;
413 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_8x6_KHR
;
414 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_8x8_KHR
;
415 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_10x5_KHR
;
416 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_10x6_KHR
;
417 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_10x8_KHR
;
418 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_10x10_KHR
;
419 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_12x10_KHR
;
420 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_12x12_KHR
;
421 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
;
422 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR
;
423 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR
;
424 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR
;
425 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR
;
426 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR
;
427 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR
;
428 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR
;
429 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR
;
430 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR
;
431 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR
;
432 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR
;
433 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR
;
434 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR
;
441 if (_mesa_is_gles3(ctx
) &&
442 ctx
->Extensions
.OES_texture_compression_astc
) {
444 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_3x3x3_OES
;
445 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_4x3x3_OES
;
446 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_4x4x3_OES
;
447 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_4x4x4_OES
;
448 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_5x4x4_OES
;
449 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_5x5x4_OES
;
450 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_5x5x5_OES
;
451 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_6x5x5_OES
;
452 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_6x6x5_OES
;
453 formats
[n
++] = GL_COMPRESSED_RGBA_ASTC_6x6x6_OES
;
454 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES
;
455 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES
;
456 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES
;
457 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES
;
458 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES
;
459 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES
;
460 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES
;
461 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES
;
462 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES
;
463 formats
[n
++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES
;
475 * Convert GLenum to a compressed MESA_FORMAT_x.
478 _mesa_glenum_to_compressed_format(GLenum format
)
481 case GL_COMPRESSED_RGB_FXT1_3DFX
:
482 return MESA_FORMAT_RGB_FXT1
;
483 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
484 return MESA_FORMAT_RGBA_FXT1
;
486 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
489 return MESA_FORMAT_RGB_DXT1
;
490 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
491 return MESA_FORMAT_RGBA_DXT1
;
492 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
495 return MESA_FORMAT_RGBA_DXT3
;
496 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
497 return MESA_FORMAT_RGBA_DXT5
;
499 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
500 return MESA_FORMAT_SRGB_DXT1
;
501 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
502 return MESA_FORMAT_SRGBA_DXT1
;
503 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
504 return MESA_FORMAT_SRGBA_DXT3
;
505 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
506 return MESA_FORMAT_SRGBA_DXT5
;
508 case GL_COMPRESSED_RED_RGTC1
:
509 return MESA_FORMAT_R_RGTC1_UNORM
;
510 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
511 return MESA_FORMAT_R_RGTC1_SNORM
;
512 case GL_COMPRESSED_RG_RGTC2
:
513 return MESA_FORMAT_RG_RGTC2_UNORM
;
514 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
515 return MESA_FORMAT_RG_RGTC2_SNORM
;
517 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
518 return MESA_FORMAT_L_LATC1_UNORM
;
519 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
520 return MESA_FORMAT_L_LATC1_SNORM
;
521 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
522 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
523 return MESA_FORMAT_LA_LATC2_UNORM
;
524 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
525 return MESA_FORMAT_LA_LATC2_SNORM
;
527 case GL_ETC1_RGB8_OES
:
528 return MESA_FORMAT_ETC1_RGB8
;
529 case GL_COMPRESSED_RGB8_ETC2
:
530 return MESA_FORMAT_ETC2_RGB8
;
531 case GL_COMPRESSED_SRGB8_ETC2
:
532 return MESA_FORMAT_ETC2_SRGB8
;
533 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
534 return MESA_FORMAT_ETC2_RGBA8_EAC
;
535 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
536 return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
;
537 case GL_COMPRESSED_R11_EAC
:
538 return MESA_FORMAT_ETC2_R11_EAC
;
539 case GL_COMPRESSED_RG11_EAC
:
540 return MESA_FORMAT_ETC2_RG11_EAC
;
541 case GL_COMPRESSED_SIGNED_R11_EAC
:
542 return MESA_FORMAT_ETC2_SIGNED_R11_EAC
;
543 case GL_COMPRESSED_SIGNED_RG11_EAC
:
544 return MESA_FORMAT_ETC2_SIGNED_RG11_EAC
;
545 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
546 return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
;
547 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
548 return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
;
550 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
551 return MESA_FORMAT_BPTC_RGBA_UNORM
;
552 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
553 return MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM
;
554 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
555 return MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT
;
556 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
557 return MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT
;
559 case GL_COMPRESSED_RGBA_ASTC_4x4_KHR
:
560 return MESA_FORMAT_RGBA_ASTC_4x4
;
561 case GL_COMPRESSED_RGBA_ASTC_5x4_KHR
:
562 return MESA_FORMAT_RGBA_ASTC_5x4
;
563 case GL_COMPRESSED_RGBA_ASTC_5x5_KHR
:
564 return MESA_FORMAT_RGBA_ASTC_5x5
;
565 case GL_COMPRESSED_RGBA_ASTC_6x5_KHR
:
566 return MESA_FORMAT_RGBA_ASTC_6x5
;
567 case GL_COMPRESSED_RGBA_ASTC_6x6_KHR
:
568 return MESA_FORMAT_RGBA_ASTC_6x6
;
569 case GL_COMPRESSED_RGBA_ASTC_8x5_KHR
:
570 return MESA_FORMAT_RGBA_ASTC_8x5
;
571 case GL_COMPRESSED_RGBA_ASTC_8x6_KHR
:
572 return MESA_FORMAT_RGBA_ASTC_8x6
;
573 case GL_COMPRESSED_RGBA_ASTC_8x8_KHR
:
574 return MESA_FORMAT_RGBA_ASTC_8x8
;
575 case GL_COMPRESSED_RGBA_ASTC_10x5_KHR
:
576 return MESA_FORMAT_RGBA_ASTC_10x5
;
577 case GL_COMPRESSED_RGBA_ASTC_10x6_KHR
:
578 return MESA_FORMAT_RGBA_ASTC_10x6
;
579 case GL_COMPRESSED_RGBA_ASTC_10x8_KHR
:
580 return MESA_FORMAT_RGBA_ASTC_10x8
;
581 case GL_COMPRESSED_RGBA_ASTC_10x10_KHR
:
582 return MESA_FORMAT_RGBA_ASTC_10x10
;
583 case GL_COMPRESSED_RGBA_ASTC_12x10_KHR
:
584 return MESA_FORMAT_RGBA_ASTC_12x10
;
585 case GL_COMPRESSED_RGBA_ASTC_12x12_KHR
:
586 return MESA_FORMAT_RGBA_ASTC_12x12
;
587 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
:
588 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4
;
589 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR
:
590 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4
;
591 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR
:
592 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5
;
593 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR
:
594 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5
;
595 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR
:
596 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6
;
597 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR
:
598 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5
;
599 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR
:
600 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6
;
601 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR
:
602 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8
;
603 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR
:
604 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5
;
605 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR
:
606 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6
;
607 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR
:
608 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8
;
609 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR
:
610 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10
;
611 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR
:
612 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10
;
613 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR
:
614 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12
;
617 return MESA_FORMAT_NONE
;
623 * Given a compressed MESA_FORMAT_x value, return the corresponding
624 * GLenum for that format.
625 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
626 * which must return the specific texture format used when the user might
627 * have originally specified a generic compressed format in their
628 * glTexImage2D() call.
629 * For non-compressed textures, we always return the user-specified
630 * internal format unchanged.
633 _mesa_compressed_format_to_glenum(struct gl_context
*ctx
, mesa_format mesaFormat
)
635 switch (mesaFormat
) {
636 case MESA_FORMAT_RGB_FXT1
:
637 return GL_COMPRESSED_RGB_FXT1_3DFX
;
638 case MESA_FORMAT_RGBA_FXT1
:
639 return GL_COMPRESSED_RGBA_FXT1_3DFX
;
640 case MESA_FORMAT_RGB_DXT1
:
641 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
642 case MESA_FORMAT_RGBA_DXT1
:
643 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
;
644 case MESA_FORMAT_RGBA_DXT3
:
645 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
646 case MESA_FORMAT_RGBA_DXT5
:
647 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
648 case MESA_FORMAT_SRGB_DXT1
:
649 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
;
650 case MESA_FORMAT_SRGBA_DXT1
:
651 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
;
652 case MESA_FORMAT_SRGBA_DXT3
:
653 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
;
654 case MESA_FORMAT_SRGBA_DXT5
:
655 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
;
656 case MESA_FORMAT_R_RGTC1_UNORM
:
657 return GL_COMPRESSED_RED_RGTC1
;
658 case MESA_FORMAT_R_RGTC1_SNORM
:
659 return GL_COMPRESSED_SIGNED_RED_RGTC1
;
660 case MESA_FORMAT_RG_RGTC2_UNORM
:
661 return GL_COMPRESSED_RG_RGTC2
;
662 case MESA_FORMAT_RG_RGTC2_SNORM
:
663 return GL_COMPRESSED_SIGNED_RG_RGTC2
;
665 case MESA_FORMAT_L_LATC1_UNORM
:
666 return GL_COMPRESSED_LUMINANCE_LATC1_EXT
;
667 case MESA_FORMAT_L_LATC1_SNORM
:
668 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
;
669 case MESA_FORMAT_LA_LATC2_UNORM
:
670 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
;
671 case MESA_FORMAT_LA_LATC2_SNORM
:
672 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
;
674 case MESA_FORMAT_ETC1_RGB8
:
675 return GL_ETC1_RGB8_OES
;
676 case MESA_FORMAT_ETC2_RGB8
:
677 return GL_COMPRESSED_RGB8_ETC2
;
678 case MESA_FORMAT_ETC2_SRGB8
:
679 return GL_COMPRESSED_SRGB8_ETC2
;
680 case MESA_FORMAT_ETC2_RGBA8_EAC
:
681 return GL_COMPRESSED_RGBA8_ETC2_EAC
;
682 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
:
683 return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
;
684 case MESA_FORMAT_ETC2_R11_EAC
:
685 return GL_COMPRESSED_R11_EAC
;
686 case MESA_FORMAT_ETC2_RG11_EAC
:
687 return GL_COMPRESSED_RG11_EAC
;
688 case MESA_FORMAT_ETC2_SIGNED_R11_EAC
:
689 return GL_COMPRESSED_SIGNED_R11_EAC
;
690 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC
:
691 return GL_COMPRESSED_SIGNED_RG11_EAC
;
692 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
:
693 return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
;
694 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
:
695 return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
;
697 case MESA_FORMAT_BPTC_RGBA_UNORM
:
698 return GL_COMPRESSED_RGBA_BPTC_UNORM
;
699 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM
:
700 return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
;
701 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT
:
702 return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
;
703 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT
:
704 return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
;
706 case MESA_FORMAT_RGBA_ASTC_4x4
:
707 return GL_COMPRESSED_RGBA_ASTC_4x4_KHR
;
708 case MESA_FORMAT_RGBA_ASTC_5x4
:
709 return GL_COMPRESSED_RGBA_ASTC_5x4_KHR
;
710 case MESA_FORMAT_RGBA_ASTC_5x5
:
711 return GL_COMPRESSED_RGBA_ASTC_5x5_KHR
;
712 case MESA_FORMAT_RGBA_ASTC_6x5
:
713 return GL_COMPRESSED_RGBA_ASTC_6x5_KHR
;
714 case MESA_FORMAT_RGBA_ASTC_6x6
:
715 return GL_COMPRESSED_RGBA_ASTC_6x6_KHR
;
716 case MESA_FORMAT_RGBA_ASTC_8x5
:
717 return GL_COMPRESSED_RGBA_ASTC_8x5_KHR
;
718 case MESA_FORMAT_RGBA_ASTC_8x6
:
719 return GL_COMPRESSED_RGBA_ASTC_8x6_KHR
;
720 case MESA_FORMAT_RGBA_ASTC_8x8
:
721 return GL_COMPRESSED_RGBA_ASTC_8x8_KHR
;
722 case MESA_FORMAT_RGBA_ASTC_10x5
:
723 return GL_COMPRESSED_RGBA_ASTC_10x5_KHR
;
724 case MESA_FORMAT_RGBA_ASTC_10x6
:
725 return GL_COMPRESSED_RGBA_ASTC_10x6_KHR
;
726 case MESA_FORMAT_RGBA_ASTC_10x8
:
727 return GL_COMPRESSED_RGBA_ASTC_10x8_KHR
;
728 case MESA_FORMAT_RGBA_ASTC_10x10
:
729 return GL_COMPRESSED_RGBA_ASTC_10x10_KHR
;
730 case MESA_FORMAT_RGBA_ASTC_12x10
:
731 return GL_COMPRESSED_RGBA_ASTC_12x10_KHR
;
732 case MESA_FORMAT_RGBA_ASTC_12x12
:
733 return GL_COMPRESSED_RGBA_ASTC_12x12_KHR
;
734 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4
:
735 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
;
736 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4
:
737 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR
;
738 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5
:
739 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR
;
740 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5
:
741 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR
;
742 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6
:
743 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR
;
744 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5
:
745 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR
;
746 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6
:
747 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR
;
748 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8
:
749 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR
;
750 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5
:
751 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR
;
752 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6
:
753 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR
;
754 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8
:
755 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR
;
756 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10
:
757 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR
;
758 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10
:
759 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR
;
760 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12
:
761 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR
;
764 _mesa_problem(ctx
, "Unexpected mesa texture format in"
765 " _mesa_compressed_format_to_glenum()");
772 * Return the address of the pixel at (col, row, img) in a
773 * compressed texture image.
774 * \param col, row, img - image position (3D), should be a multiple of the
775 * format's block size.
776 * \param format - compressed image format
777 * \param width - image width (stride) in pixels
778 * \param image - the image address
779 * \return address of pixel at (row, col, img)
782 _mesa_compressed_image_address(GLint col
, GLint row
, GLint img
,
783 mesa_format mesaFormat
,
784 GLsizei width
, const GLubyte
*image
)
786 /* XXX only 2D images implemented, not 3D */
787 const GLuint blockSize
= _mesa_get_format_bytes(mesaFormat
);
791 _mesa_get_format_block_size(mesaFormat
, &bw
, &bh
);
793 assert(col
% bw
== 0);
794 assert(row
% bh
== 0);
796 offset
= ((width
+ bw
- 1) / bw
) * (row
/ bh
) + col
/ bw
;
799 return (GLubyte
*) image
+ offset
;
804 * Return a texel-fetch function for the given format, or NULL if
807 compressed_fetch_func
808 _mesa_get_compressed_fetch_func(mesa_format format
)
810 switch (_mesa_get_format_layout(format
)) {
811 case MESA_FORMAT_LAYOUT_S3TC
:
812 return _mesa_get_dxt_fetch_func(format
);
813 case MESA_FORMAT_LAYOUT_FXT1
:
814 return _mesa_get_fxt_fetch_func(format
);
815 case MESA_FORMAT_LAYOUT_RGTC
:
816 case MESA_FORMAT_LAYOUT_LATC
:
817 return _mesa_get_compressed_rgtc_func(format
);
818 case MESA_FORMAT_LAYOUT_ETC1
:
819 return _mesa_get_etc_fetch_func(format
);
820 case MESA_FORMAT_LAYOUT_BPTC
:
821 return _mesa_get_bptc_fetch_func(format
);
829 * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
830 * \param srcRowStride stride in bytes between rows of blocks in the
831 * compressed source image.
834 _mesa_decompress_image(mesa_format format
, GLuint width
, GLuint height
,
835 const GLubyte
*src
, GLint srcRowStride
,
838 compressed_fetch_func fetch
;
840 GLuint bytes
, bw
, bh
;
843 bytes
= _mesa_get_format_bytes(format
);
844 _mesa_get_format_block_size(format
, &bw
, &bh
);
846 fetch
= _mesa_get_compressed_fetch_func(format
);
848 _mesa_problem(NULL
, "Unexpected format in _mesa_decompress_image()");
852 stride
= srcRowStride
* bh
/ bytes
;
854 for (j
= 0; j
< height
; j
++) {
855 for (i
= 0; i
< width
; i
++) {
856 fetch(src
, stride
, i
, j
, dest
);