1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5  * Copyright (c) 2008 VMware, Inc.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
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.
24  */
25 
26 
27 /**
28  * \file texcompress.c
29  * Helper functions for texture compression.
30  */
31 
32 
33 #include "glheader.h"
34 
35 #include "context.h"
36 #include "formats.h"
37 #include "mtypes.h"
38 #include "context.h"
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"
45 
46 
47 /**
48  * Get the GL base format of a specified GL compressed texture format
49  *
50  * From page 232 of the OpenGL 3.3 (Compatiblity Profile) spec:
51  *
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"
70  *
71  * \return
72  * The base format of \c format if \c format is a compressed format (either
73  * generic or specific.  Otherwise 0 is returned.
74  */
75 GLenum
_mesa_gl_compressed_format_base_format(GLenum format)76 _mesa_gl_compressed_format_base_format(GLenum format)
77 {
78    switch (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:
84       return GL_RED;
85 
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:
91       return GL_RG;
92 
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:
103    case GL_RGB_S3TC:
104    case GL_RGB4_S3TC:
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:
109    case GL_ATC_RGB_AMD:
110       return GL_RGB;
111 
112    case GL_COMPRESSED_RGBA:
113    case GL_COMPRESSED_SRGB_ALPHA:
114    case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
115    case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
116    case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
117    case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
118    case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
119    case GL_COMPRESSED_RGBA_FXT1_3DFX:
120    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
121    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
122    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
123    case GL_COMPRESSED_RGBA8_ETC2_EAC:
124    case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
125    case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
126    case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
127    case GL_RGBA_S3TC:
128    case GL_RGBA4_S3TC:
129    case GL_PALETTE4_RGBA8_OES:
130    case GL_PALETTE8_RGB5_A1_OES:
131    case GL_PALETTE4_RGBA4_OES:
132    case GL_PALETTE4_RGB5_A1_OES:
133    case GL_PALETTE8_RGBA8_OES:
134    case GL_PALETTE8_RGBA4_OES:
135    case GL_ATC_RGBA_EXPLICIT_ALPHA_AMD:
136    case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD:
137       return GL_RGBA;
138 
139    case GL_COMPRESSED_ALPHA:
140       return GL_ALPHA;
141 
142    case GL_COMPRESSED_LUMINANCE:
143    case GL_COMPRESSED_SLUMINANCE:
144    case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
145    case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
146       return GL_LUMINANCE;
147 
148    case GL_COMPRESSED_LUMINANCE_ALPHA:
149    case GL_COMPRESSED_SLUMINANCE_ALPHA:
150    case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
151    case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
152    case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
153       return GL_LUMINANCE_ALPHA;
154 
155    case GL_COMPRESSED_INTENSITY:
156       return GL_INTENSITY;
157 
158    default:
159       return 0;
160    }
161 }
162 
163 /**
164  * Return list of (and count of) all specific texture compression
165  * formats that are supported.
166  *
167  * Some formats are \b not returned by this function.  The
168  * \c GL_COMPRESSED_TEXTURE_FORMATS query only returns formats that are
169  * "suitable for general-purpose usage."  All texture compression extensions
170  * have taken this to mean either linear RGB or linear RGBA.
171  *
172  * The GL_ARB_texture_compress_rgtc spec says:
173  *
174  *    "19) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
175  *        GL_COMPRESSED_TEXTURE_FORMATS queries return the RGTC formats?
176  *
177  *        RESOLVED:  No.
178  *
179  *        The OpenGL 2.1 specification says "The only values returned
180  *        by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
181  *        corresponding to formats suitable for general-purpose usage.
182  *        The renderer will not enumerate formats with restrictions that
183  *        need to be specifically understood prior to use."
184  *
185  *        Compressed textures with just red or red-green components are
186  *        not general-purpose so should not be returned by these queries
187  *        because they have restrictions.
188  *
189  *        Applications that seek to use the RGTC formats should do so
190  *        by looking for this extension's name in the string returned by
191  *        glGetString(GL_EXTENSIONS) rather than
192  *        what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
193  *        GL_COMPRESSED_TEXTURE_FORMATS return."
194  *
195  * There is nearly identical wording in the GL_EXT_texture_compression_rgtc
196  * spec.
197  *
198  * The GL_EXT_texture_rRGB spec says:
199  *
200  *    "22) Should the new COMPRESSED_SRGB_* formats be listed in an
201  *        implementation's GL_COMPRESSED_TEXTURE_FORMATS list?
202  *
203  *        RESOLVED:  No.  Section 3.8.1 says formats listed by
204  *        GL_COMPRESSED_TEXTURE_FORMATS are "suitable for general-purpose
205  *        usage."  The non-linear distribution of red, green, and
206  *        blue for these sRGB compressed formats makes them not really
207  *        general-purpose."
208  *
209  * The GL_EXT_texture_compression_latc spec says:
210  *
211  *    "16) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
212  *        GL_COMPRESSED_TEXTURE_FORMATS queries return the LATC formats?
213  *
214  *        RESOLVED:  No.
215  *
216  *        The OpenGL 2.1 specification says "The only values returned
217  *        by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
218  *        corresponding to formats suitable for general-purpose usage.
219  *        The renderer will not enumerate formats with restrictions that
220  *        need to be specifically understood prior to use."
221  *
222  *        Historically, OpenGL implementation have advertised the RGB and
223  *        RGBA versions of the S3TC extensions compressed format tokens
224  *        through this mechanism.
225  *
226  *        The specification is not sufficiently clear about what "suitable
227  *        for general-purpose usage" means.  Historically that seems to mean
228  *        unsigned RGB or unsigned RGBA.  The DXT1 format supporting alpha
229  *        (GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) is not exposed in the list (at
230  *        least for NVIDIA drivers) because the alpha is always 1.0 expect
231  *        when it is 0.0 when RGB is required to be black.  NVIDIA's even
232  *        limits itself to true linear RGB or RGBA formats, specifically
233  *        not including EXT_texture_sRGB's sRGB S3TC compressed formats.
234  *
235  *        Adding luminance and luminance-alpha texture formats (and
236  *        certainly signed versions of luminance and luminance-alpha
237  *        formats!) invites potential comptaibility problems with old
238  *        applications using this mechanism since old applications are
239  *        unlikely to expect non-RGB or non-RGBA formats to be advertised
240  *        through this mechanism.  However no specific misinteractions
241  *        with old applications is known.
242  *
243  *        Applications that seek to use the LATC formats should do so
244  *        by looking for this extension's name in the string returned by
245  *        glGetString(GL_EXTENSIONS) rather than
246  *        what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
247  *        GL_COMPRESSED_TEXTURE_FORMATS return."
248  *
249  * There is no formal spec for GL_ATI_texture_compression_3dc.  Since the
250  * formats added by this extension are luminance-alpha formats, it is
251  * reasonable to expect them to follow the same rules as
252  * GL_EXT_texture_compression_latc.  At the very least, Catalyst 11.6 does not
253  * expose the 3dc formats through this mechanism.
254  *
255  * The spec for GL_ARB_texture_compression_bptc doesn't mention whether it
256  * should be included in GL_COMPRESSED_TEXTURE_FORMATS. However as it takes a
257  * very long time to compress the textures in this format it's probably not
258  * very useful as a general format where the GL will have to compress it on
259  * the fly.
260  *
261  * \param ctx  the GL context
262  * \param formats  the resulting format list (may be NULL).
263  *
264  * \return number of formats.
265  */
266 GLuint
_mesa_get_compressed_formats(struct gl_context * ctx,GLint * formats)267 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats)
268 {
269    GLint discard_formats[100];
270    GLuint n = 0;
271 
272    if (!formats) {
273       formats = discard_formats;
274    }
275 
276    if (_mesa_is_desktop_gl(ctx) &&
277        ctx->Extensions.TDFX_texture_compression_FXT1) {
278       formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
279       formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
280    }
281 
282    if (ctx->Extensions.EXT_texture_compression_s3tc) {
283       formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
284       formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
285       formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
286 
287       /* The ES and desktop GL specs diverge here.
288        *
289        * In desktop OpenGL, the driver can perform online compression of
290        * uncompressed texture data.  GL_NUM_COMPRESSED_TEXTURE_FORMATS and
291        * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
292        * formats that it could ask the driver to compress with some
293        * expectation of quality.  The GL_ARB_texture_compression spec
294        * calls this "suitable for general-purpose usage."  As noted
295        * above, this means GL_COMPRESSED_RGBA_S3TC_DXT1_EXT is not
296        * included in the list.
297        *
298        * In OpenGL ES, the driver never performs compression.
299        * GL_NUM_COMPRESSED_TEXTURE_FORMATS and
300        * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
301        * formats that the driver can receive from the application.  It
302        * is the *complete* list of formats.  The
303        * GL_EXT_texture_compression_s3tc spec says:
304        *
305        *     "New State for OpenGL ES 2.0.25 and 3.0.2 Specifications
306        *
307        *         The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
308        *         COMPRESSED_TEXTURE_FORMATS include
309        *         COMPRESSED_RGB_S3TC_DXT1_EXT,
310        *         COMPRESSED_RGBA_S3TC_DXT1_EXT,
311        *         COMPRESSED_RGBA_S3TC_DXT3_EXT, and
312        *         COMPRESSED_RGBA_S3TC_DXT5_EXT."
313        *
314        * Note that the addition is only to the OpenGL ES specification!
315        */
316       if (_mesa_is_gles(ctx)) {
317          formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
318       }
319    }
320 
321    /* The GL_OES_compressed_ETC1_RGB8_texture spec says:
322     *
323     *     "New State
324     *
325     *         The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
326     *         COMPRESSED_TEXTURE_FORMATS include ETC1_RGB8_OES."
327     */
328    if (_mesa_is_gles(ctx)
329        && ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
330       formats[n++] = GL_ETC1_RGB8_OES;
331    }
332 
333    /* Required by EXT_texture_compression_bptc in GLES. */
334    if (_mesa_has_EXT_texture_compression_bptc(ctx)) {
335       formats[n++] = GL_COMPRESSED_RGBA_BPTC_UNORM;
336       formats[n++] = GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
337       formats[n++] = GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
338       formats[n++] = GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
339    }
340 
341    /* Required by EXT_texture_compression_rgtc in GLES. */
342    if (_mesa_is_gles3(ctx) &&
343        _mesa_has_EXT_texture_compression_rgtc(ctx)) {
344       formats[n++] = GL_COMPRESSED_RED_RGTC1_EXT;
345       formats[n++] = GL_COMPRESSED_SIGNED_RED_RGTC1_EXT;
346       formats[n++] = GL_COMPRESSED_RED_GREEN_RGTC2_EXT;
347       formats[n++] = GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT;
348    }
349 
350    if (ctx->API == API_OPENGLES) {
351       formats[n++] = GL_PALETTE4_RGB8_OES;
352       formats[n++] = GL_PALETTE4_RGBA8_OES;
353       formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
354       formats[n++] = GL_PALETTE4_RGBA4_OES;
355       formats[n++] = GL_PALETTE4_RGB5_A1_OES;
356       formats[n++] = GL_PALETTE8_RGB8_OES;
357       formats[n++] = GL_PALETTE8_RGBA8_OES;
358       formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
359       formats[n++] = GL_PALETTE8_RGBA4_OES;
360       formats[n++] = GL_PALETTE8_RGB5_A1_OES;
361    }
362 
363    if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {
364       formats[n++] = GL_COMPRESSED_RGB8_ETC2;
365       formats[n++] = GL_COMPRESSED_RGBA8_ETC2_EAC;
366       formats[n++] = GL_COMPRESSED_R11_EAC;
367       formats[n++] = GL_COMPRESSED_RG11_EAC;
368       formats[n++] = GL_COMPRESSED_SIGNED_R11_EAC;
369       formats[n++] = GL_COMPRESSED_SIGNED_RG11_EAC;
370       formats[n++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
371    }
372 
373    if (_mesa_is_gles3(ctx)) {
374       formats[n++] = GL_COMPRESSED_SRGB8_ETC2;
375       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
376       formats[n++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
377    }
378 
379    /* The KHR_texture_compression_astc_hdr spec says:
380     *
381     *    "Interactions with OpenGL 4.2
382     *
383     *        OpenGL 4.2 supports the feature that compressed textures can be
384     *        compressed online, by passing the compressed texture format enum
385     *        as 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).
388     *
389     *        Due to the complexity of the ASTC compression algorithm, it is
390     *        not usually suitable for online use, and therefore ASTC support
391     *        will be limited to pre-compressed textures only. Where on-device
392     *        compression is required, a domain-specific limited compressor
393     *        will typically be used, and this is therefore not suitable for
394     *        implementation in the driver.
395     *
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."
400     *
401     * The ES and the desktop specs diverge here. In OpenGL ES, the
402     * COMPRESSED_TEXTURE_FORMATS query returns the set of supported specific
403     * compressed formats.
404     */
405    if (ctx->API == API_OPENGLES2 &&
406        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;
435    }
436 
437    if (_mesa_is_gles3(ctx) &&
438        ctx->Extensions.OES_texture_compression_astc) {
439       formats[n++] = GL_COMPRESSED_RGBA_ASTC_3x3x3_OES;
440       formats[n++] = GL_COMPRESSED_RGBA_ASTC_4x3x3_OES;
441       formats[n++] = GL_COMPRESSED_RGBA_ASTC_4x4x3_OES;
442       formats[n++] = GL_COMPRESSED_RGBA_ASTC_4x4x4_OES;
443       formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x4x4_OES;
444       formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x5x4_OES;
445       formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x5x5_OES;
446       formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x5x5_OES;
447       formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x6x5_OES;
448       formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x6x6_OES;
449       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES;
450       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES;
451       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES;
452       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES;
453       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES;
454       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES;
455       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES;
456       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES;
457       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES;
458       formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES;
459    }
460 
461    /* The GL_AMD_compressed_ATC_texture spec says:
462     *
463     *     "New State
464     *
465     *         The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
466     *         COMPRESSED_TEXTURE_FORMATS include ATC_RGB_AMD,
467     *         ATC_RGBA_EXPLICIT_ALPHA_AMD, and ATC_RGBA_INTERPOLATED_ALPHA_AMD."
468     */
469    if (_mesa_has_AMD_compressed_ATC_texture(ctx)) {
470       formats[n++] = GL_ATC_RGB_AMD;
471       formats[n++] = GL_ATC_RGBA_EXPLICIT_ALPHA_AMD;
472       formats[n++] = GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD;
473    }
474 
475    assert(n <= ARRAY_SIZE(discard_formats));
476 
477    return n;
478 }
479 
480 
481 /**
482  * Convert GLenum to a compressed MESA_FORMAT_x.
483  */
484 mesa_format
_mesa_glenum_to_compressed_format(GLenum format)485 _mesa_glenum_to_compressed_format(GLenum format)
486 {
487    switch (format) {
488    case GL_COMPRESSED_RGB_FXT1_3DFX:
489       return MESA_FORMAT_RGB_FXT1;
490    case GL_COMPRESSED_RGBA_FXT1_3DFX:
491       return MESA_FORMAT_RGBA_FXT1;
492 
493    case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
494    case GL_RGB_S3TC:
495    case GL_RGB4_S3TC:
496       return MESA_FORMAT_RGB_DXT1;
497    case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
498       return MESA_FORMAT_RGBA_DXT1;
499    case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
500    case GL_RGBA_S3TC:
501    case GL_RGBA4_S3TC:
502       return MESA_FORMAT_RGBA_DXT3;
503    case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
504       return MESA_FORMAT_RGBA_DXT5;
505 
506    case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
507       return MESA_FORMAT_SRGB_DXT1;
508    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
509       return MESA_FORMAT_SRGBA_DXT1;
510    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
511       return MESA_FORMAT_SRGBA_DXT3;
512    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
513       return MESA_FORMAT_SRGBA_DXT5;
514 
515    case GL_COMPRESSED_RED_RGTC1:
516       return MESA_FORMAT_R_RGTC1_UNORM;
517    case GL_COMPRESSED_SIGNED_RED_RGTC1:
518       return MESA_FORMAT_R_RGTC1_SNORM;
519    case GL_COMPRESSED_RG_RGTC2:
520       return MESA_FORMAT_RG_RGTC2_UNORM;
521    case GL_COMPRESSED_SIGNED_RG_RGTC2:
522       return MESA_FORMAT_RG_RGTC2_SNORM;
523 
524    case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
525       return MESA_FORMAT_L_LATC1_UNORM;
526    case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
527       return MESA_FORMAT_L_LATC1_SNORM;
528    case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
529    case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
530       return MESA_FORMAT_LA_LATC2_UNORM;
531    case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
532       return MESA_FORMAT_LA_LATC2_SNORM;
533 
534    case GL_ETC1_RGB8_OES:
535       return MESA_FORMAT_ETC1_RGB8;
536    case GL_COMPRESSED_RGB8_ETC2:
537       return MESA_FORMAT_ETC2_RGB8;
538    case GL_COMPRESSED_SRGB8_ETC2:
539       return MESA_FORMAT_ETC2_SRGB8;
540    case GL_COMPRESSED_RGBA8_ETC2_EAC:
541       return MESA_FORMAT_ETC2_RGBA8_EAC;
542    case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
543       return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
544    case GL_COMPRESSED_R11_EAC:
545       return MESA_FORMAT_ETC2_R11_EAC;
546    case GL_COMPRESSED_RG11_EAC:
547       return MESA_FORMAT_ETC2_RG11_EAC;
548    case GL_COMPRESSED_SIGNED_R11_EAC:
549       return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
550    case GL_COMPRESSED_SIGNED_RG11_EAC:
551       return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
552    case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
553       return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
554    case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
555       return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
556 
557    case GL_COMPRESSED_RGBA_BPTC_UNORM:
558       return MESA_FORMAT_BPTC_RGBA_UNORM;
559    case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
560       return MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM;
561    case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
562       return MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT;
563    case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
564       return MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT;
565 
566    case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
567       return MESA_FORMAT_RGBA_ASTC_4x4;
568    case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
569       return MESA_FORMAT_RGBA_ASTC_5x4;
570    case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
571       return MESA_FORMAT_RGBA_ASTC_5x5;
572    case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
573       return MESA_FORMAT_RGBA_ASTC_6x5;
574    case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
575       return MESA_FORMAT_RGBA_ASTC_6x6;
576    case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
577       return MESA_FORMAT_RGBA_ASTC_8x5;
578    case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
579       return MESA_FORMAT_RGBA_ASTC_8x6;
580    case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
581       return MESA_FORMAT_RGBA_ASTC_8x8;
582    case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
583       return MESA_FORMAT_RGBA_ASTC_10x5;
584    case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
585       return MESA_FORMAT_RGBA_ASTC_10x6;
586    case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
587       return MESA_FORMAT_RGBA_ASTC_10x8;
588    case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
589       return MESA_FORMAT_RGBA_ASTC_10x10;
590    case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
591       return MESA_FORMAT_RGBA_ASTC_12x10;
592    case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
593       return MESA_FORMAT_RGBA_ASTC_12x12;
594    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
595       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4;
596    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
597       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4;
598    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
599       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5;
600    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
601       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5;
602    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
603       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6;
604    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
605       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5;
606    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
607       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6;
608    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
609       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8;
610    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
611       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5;
612    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
613       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6;
614    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
615       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8;
616    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
617       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10;
618    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
619       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10;
620    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
621       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12;
622    case GL_COMPRESSED_RGBA_ASTC_3x3x3_OES:
623       return MESA_FORMAT_RGBA_ASTC_3x3x3;
624    case GL_COMPRESSED_RGBA_ASTC_4x3x3_OES:
625       return MESA_FORMAT_RGBA_ASTC_4x3x3;
626    case GL_COMPRESSED_RGBA_ASTC_4x4x3_OES:
627       return MESA_FORMAT_RGBA_ASTC_4x4x3;
628    case GL_COMPRESSED_RGBA_ASTC_4x4x4_OES:
629       return MESA_FORMAT_RGBA_ASTC_4x4x4;
630    case GL_COMPRESSED_RGBA_ASTC_5x4x4_OES:
631       return MESA_FORMAT_RGBA_ASTC_5x4x4;
632    case GL_COMPRESSED_RGBA_ASTC_5x5x4_OES:
633       return MESA_FORMAT_RGBA_ASTC_5x5x4;
634    case GL_COMPRESSED_RGBA_ASTC_5x5x5_OES:
635       return MESA_FORMAT_RGBA_ASTC_5x5x5;
636    case GL_COMPRESSED_RGBA_ASTC_6x5x5_OES:
637       return MESA_FORMAT_RGBA_ASTC_6x5x5;
638    case GL_COMPRESSED_RGBA_ASTC_6x6x5_OES:
639       return MESA_FORMAT_RGBA_ASTC_6x6x5;
640    case GL_COMPRESSED_RGBA_ASTC_6x6x6_OES:
641       return MESA_FORMAT_RGBA_ASTC_6x6x6;
642    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES:
643       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_3x3x3;
644    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES:
645       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x3x3;
646    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES:
647       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4x3;
648    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES:
649       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4x4;
650    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES:
651       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4x4;
652    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES:
653       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5x4;
654    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES:
655       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5x5;
656    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES:
657       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5x5;
658    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES:
659       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6x5;
660    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES:
661       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6x6;
662 
663    case GL_ATC_RGB_AMD:
664       return MESA_FORMAT_ATC_RGB;
665    case GL_ATC_RGBA_EXPLICIT_ALPHA_AMD:
666       return MESA_FORMAT_ATC_RGBA_EXPLICIT;
667    case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD:
668       return MESA_FORMAT_ATC_RGBA_INTERPOLATED;
669 
670    default:
671       return MESA_FORMAT_NONE;
672    }
673 }
674 
675 
676 /**
677  * Given a compressed MESA_FORMAT_x value, return the corresponding
678  * GLenum for that format.
679  * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
680  * which must return the specific texture format used when the user might
681  * have originally specified a generic compressed format in their
682  * glTexImage2D() call.
683  * For non-compressed textures, we always return the user-specified
684  * internal format unchanged.
685  */
686 GLenum
_mesa_compressed_format_to_glenum(struct gl_context * ctx,mesa_format mesaFormat)687 _mesa_compressed_format_to_glenum(struct gl_context *ctx,
688                                   mesa_format mesaFormat)
689 {
690    switch (mesaFormat) {
691    case MESA_FORMAT_RGB_FXT1:
692       return GL_COMPRESSED_RGB_FXT1_3DFX;
693    case MESA_FORMAT_RGBA_FXT1:
694       return GL_COMPRESSED_RGBA_FXT1_3DFX;
695    case MESA_FORMAT_RGB_DXT1:
696       return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
697    case MESA_FORMAT_RGBA_DXT1:
698       return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
699    case MESA_FORMAT_RGBA_DXT3:
700       return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
701    case MESA_FORMAT_RGBA_DXT5:
702       return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
703    case MESA_FORMAT_SRGB_DXT1:
704       return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
705    case MESA_FORMAT_SRGBA_DXT1:
706       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
707    case MESA_FORMAT_SRGBA_DXT3:
708       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
709    case MESA_FORMAT_SRGBA_DXT5:
710       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
711    case MESA_FORMAT_R_RGTC1_UNORM:
712       return GL_COMPRESSED_RED_RGTC1;
713    case MESA_FORMAT_R_RGTC1_SNORM:
714       return GL_COMPRESSED_SIGNED_RED_RGTC1;
715    case MESA_FORMAT_RG_RGTC2_UNORM:
716       return GL_COMPRESSED_RG_RGTC2;
717    case MESA_FORMAT_RG_RGTC2_SNORM:
718       return GL_COMPRESSED_SIGNED_RG_RGTC2;
719 
720    case MESA_FORMAT_L_LATC1_UNORM:
721       return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
722    case MESA_FORMAT_L_LATC1_SNORM:
723       return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
724    case MESA_FORMAT_LA_LATC2_UNORM:
725       return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
726    case MESA_FORMAT_LA_LATC2_SNORM:
727       return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
728 
729    case MESA_FORMAT_ETC1_RGB8:
730       return GL_ETC1_RGB8_OES;
731    case MESA_FORMAT_ETC2_RGB8:
732       return GL_COMPRESSED_RGB8_ETC2;
733    case MESA_FORMAT_ETC2_SRGB8:
734       return GL_COMPRESSED_SRGB8_ETC2;
735    case MESA_FORMAT_ETC2_RGBA8_EAC:
736       return GL_COMPRESSED_RGBA8_ETC2_EAC;
737    case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
738       return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
739    case MESA_FORMAT_ETC2_R11_EAC:
740       return GL_COMPRESSED_R11_EAC;
741    case MESA_FORMAT_ETC2_RG11_EAC:
742       return GL_COMPRESSED_RG11_EAC;
743    case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
744       return GL_COMPRESSED_SIGNED_R11_EAC;
745    case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
746       return GL_COMPRESSED_SIGNED_RG11_EAC;
747    case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
748       return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
749    case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
750       return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
751 
752    case MESA_FORMAT_BPTC_RGBA_UNORM:
753       return GL_COMPRESSED_RGBA_BPTC_UNORM;
754    case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
755       return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
756    case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
757       return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
758    case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
759       return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
760 
761    case MESA_FORMAT_RGBA_ASTC_4x4:
762       return GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
763    case MESA_FORMAT_RGBA_ASTC_5x4:
764       return GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
765    case MESA_FORMAT_RGBA_ASTC_5x5:
766       return GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
767    case MESA_FORMAT_RGBA_ASTC_6x5:
768       return GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
769    case MESA_FORMAT_RGBA_ASTC_6x6:
770       return GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
771    case MESA_FORMAT_RGBA_ASTC_8x5:
772       return GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
773    case MESA_FORMAT_RGBA_ASTC_8x6:
774       return GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
775    case MESA_FORMAT_RGBA_ASTC_8x8:
776       return GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
777    case MESA_FORMAT_RGBA_ASTC_10x5:
778       return GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
779    case MESA_FORMAT_RGBA_ASTC_10x6:
780       return GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
781    case MESA_FORMAT_RGBA_ASTC_10x8:
782       return GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
783    case MESA_FORMAT_RGBA_ASTC_10x10:
784       return GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
785    case MESA_FORMAT_RGBA_ASTC_12x10:
786       return GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
787    case MESA_FORMAT_RGBA_ASTC_12x12:
788       return GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
789    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
790       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
791    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
792       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
793    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
794       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
795    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
796       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
797    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
798       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
799    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
800       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
801    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
802       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
803    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
804       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
805    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
806       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
807    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
808       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
809    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
810       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
811    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
812       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
813    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
814       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
815    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
816       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
817 
818    case MESA_FORMAT_RGBA_ASTC_3x3x3:
819       return GL_COMPRESSED_RGBA_ASTC_3x3x3_OES;
820    case MESA_FORMAT_RGBA_ASTC_4x3x3:
821       return GL_COMPRESSED_RGBA_ASTC_4x3x3_OES;
822    case MESA_FORMAT_RGBA_ASTC_4x4x3:
823       return GL_COMPRESSED_RGBA_ASTC_4x4x3_OES;
824    case MESA_FORMAT_RGBA_ASTC_4x4x4:
825       return GL_COMPRESSED_RGBA_ASTC_4x4x4_OES;
826    case MESA_FORMAT_RGBA_ASTC_5x4x4:
827       return GL_COMPRESSED_RGBA_ASTC_5x4x4_OES;
828    case MESA_FORMAT_RGBA_ASTC_5x5x4:
829       return GL_COMPRESSED_RGBA_ASTC_5x5x4_OES;
830    case MESA_FORMAT_RGBA_ASTC_5x5x5:
831       return GL_COMPRESSED_RGBA_ASTC_5x5x5_OES;
832    case MESA_FORMAT_RGBA_ASTC_6x5x5:
833       return GL_COMPRESSED_RGBA_ASTC_6x5x5_OES;
834    case MESA_FORMAT_RGBA_ASTC_6x6x5:
835       return GL_COMPRESSED_RGBA_ASTC_6x6x5_OES;
836    case MESA_FORMAT_RGBA_ASTC_6x6x6:
837       return GL_COMPRESSED_RGBA_ASTC_6x6x6_OES;
838    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_3x3x3:
839       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES;
840    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x3x3:
841       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES;
842    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4x3:
843       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES;
844    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4x4:
845       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES;
846    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4x4:
847       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES;
848    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5x4:
849       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES;
850    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5x5:
851       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES;
852    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5x5:
853       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES;
854    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6x5:
855       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES;
856    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6x6:
857       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES;
858 
859    case MESA_FORMAT_ATC_RGB:
860       return GL_ATC_RGB_AMD;
861    case MESA_FORMAT_ATC_RGBA_EXPLICIT:
862       return GL_ATC_RGBA_EXPLICIT_ALPHA_AMD;
863    case MESA_FORMAT_ATC_RGBA_INTERPOLATED:
864       return GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD;
865 
866    default:
867       _mesa_problem(ctx, "Unexpected mesa texture format in"
868                     " _mesa_compressed_format_to_glenum()");
869       return 0;
870    }
871 }
872 
873 
874 /*
875  * Return the address of the pixel at (col, row, img) in a
876  * compressed texture image.
877  * \param col, row, img - image position (3D), should be a multiple of the
878  *                        format's block size.
879  * \param format - compressed image format
880  * \param width - image width (stride) in pixels
881  * \param image - the image address
882  * \return address of pixel at (row, col, img)
883  */
884 GLubyte *
_mesa_compressed_image_address(GLint col,GLint row,GLint img,mesa_format mesaFormat,GLsizei width,const GLubyte * image)885 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
886                                mesa_format mesaFormat,
887                                GLsizei width, const GLubyte *image)
888 {
889    /* XXX only 2D images implemented, not 3D */
890    const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
891    GLuint bw, bh;
892    GLint offset;
893 
894    _mesa_get_format_block_size(mesaFormat, &bw, &bh);
895 
896    assert(col % bw == 0);
897    assert(row % bh == 0);
898 
899    offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
900    offset *= blockSize;
901 
902    return (GLubyte *) image + offset;
903 }
904 
905 
906 /**
907  * Return a texel-fetch function for the given format, or NULL if
908  * invalid format.
909  */
910 compressed_fetch_func
_mesa_get_compressed_fetch_func(mesa_format format)911 _mesa_get_compressed_fetch_func(mesa_format format)
912 {
913    switch (_mesa_get_format_layout(format)) {
914    case MESA_FORMAT_LAYOUT_S3TC:
915       return _mesa_get_dxt_fetch_func(format);
916    case MESA_FORMAT_LAYOUT_FXT1:
917       return _mesa_get_fxt_fetch_func(format);
918    case MESA_FORMAT_LAYOUT_RGTC:
919    case MESA_FORMAT_LAYOUT_LATC:
920       return _mesa_get_compressed_rgtc_func(format);
921    case MESA_FORMAT_LAYOUT_ETC1:
922       return _mesa_get_etc_fetch_func(format);
923    case MESA_FORMAT_LAYOUT_BPTC:
924       return _mesa_get_bptc_fetch_func(format);
925    default:
926       return NULL;
927    }
928 }
929 
930 
931 /**
932  * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
933  * \param srcRowStride  stride in bytes between rows of blocks in the
934  *                      compressed source image.
935  */
936 void
_mesa_decompress_image(mesa_format format,GLuint width,GLuint height,const GLubyte * src,GLint srcRowStride,GLfloat * dest)937 _mesa_decompress_image(mesa_format format, GLuint width, GLuint height,
938                        const GLubyte *src, GLint srcRowStride,
939                        GLfloat *dest)
940 {
941    compressed_fetch_func fetch;
942    GLuint i, j;
943    GLuint bytes, bw, bh;
944    GLint stride;
945 
946    bytes = _mesa_get_format_bytes(format);
947    _mesa_get_format_block_size(format, &bw, &bh);
948 
949    fetch = _mesa_get_compressed_fetch_func(format);
950    if (!fetch) {
951       _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
952       return;
953    }
954 
955    stride = srcRowStride * bh / bytes;
956 
957    for (j = 0; j < height; j++) {
958       for (i = 0; i < width; i++) {
959          fetch(src, stride, i, j, dest);
960          dest += 4;
961       }
962    }
963 }
964