• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "imports.h"
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       return GL_RGB;
110 
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:
126    case GL_RGBA_S3TC:
127    case GL_RGBA4_S3TC:
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:
134       return GL_RGBA;
135 
136    case GL_COMPRESSED_ALPHA:
137       return GL_ALPHA;
138 
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:
143       return GL_LUMINANCE;
144 
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;
151 
152    case GL_COMPRESSED_INTENSITY:
153       return GL_INTENSITY;
154 
155    default:
156       return 0;
157    }
158 }
159 
160 /**
161  * Return list of (and count of) all specific texture compression
162  * formats that are supported.
163  *
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.
168  *
169  * The GL_ARB_texture_compress_rgtc spec says:
170  *
171  *    "19) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
172  *        GL_COMPRESSED_TEXTURE_FORMATS queries return the RGTC formats?
173  *
174  *        RESOLVED:  No.
175  *
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."
181  *
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.
185  *
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."
191  *
192  * There is nearly identical wording in the GL_EXT_texture_compression_rgtc
193  * spec.
194  *
195  * The GL_EXT_texture_rRGB spec says:
196  *
197  *    "22) Should the new COMPRESSED_SRGB_* formats be listed in an
198  *        implementation's GL_COMPRESSED_TEXTURE_FORMATS list?
199  *
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
204  *        general-purpose."
205  *
206  * The GL_EXT_texture_compression_latc spec says:
207  *
208  *    "16) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
209  *        GL_COMPRESSED_TEXTURE_FORMATS queries return the LATC formats?
210  *
211  *        RESOLVED:  No.
212  *
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."
218  *
219  *        Historically, OpenGL implementation have advertised the RGB and
220  *        RGBA versions of the S3TC extensions compressed format tokens
221  *        through this mechanism.
222  *
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.
231  *
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.
239  *
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."
245  *
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.
251  *
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
256  * the fly.
257  *
258  * \param ctx  the GL context
259  * \param formats  the resulting format list (may be NULL).
260  *
261  * \return number of formats.
262  */
263 GLuint
_mesa_get_compressed_formats(struct gl_context * ctx,GLint * formats)264 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats)
265 {
266    GLuint n = 0;
267    if (_mesa_is_desktop_gl(ctx) &&
268        ctx->Extensions.TDFX_texture_compression_FXT1) {
269       if (formats) {
270          formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
271          formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
272       }
273       else {
274          n += 2;
275       }
276    }
277 
278    if (ctx->Extensions.EXT_texture_compression_s3tc) {
279       if (formats) {
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;
283       }
284       else {
285          n += 3;
286       }
287 
288       /* The ES and desktop GL specs diverge here.
289        *
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.
298        *
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:
305        *
306        *     "New State for OpenGL ES 2.0.25 and 3.0.2 Specifications
307        *
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."
314        *
315        * Note that the addition is only to the OpenGL ES specification!
316        */
317       if (_mesa_is_gles(ctx)) {
318          if (formats) {
319             formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
320          } else {
321             n += 1;
322          }
323       }
324    }
325 
326    /* The GL_OES_compressed_ETC1_RGB8_texture spec says:
327     *
328     *     "New State
329     *
330     *         The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
331     *         COMPRESSED_TEXTURE_FORMATS include ETC1_RGB8_OES."
332     */
333    if (_mesa_is_gles(ctx)
334        && ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
335       if (formats) {
336          formats[n++] = GL_ETC1_RGB8_OES;
337       }
338       else {
339          n += 1;
340       }
341    }
342 
343    if (ctx->API == API_OPENGLES) {
344       if (formats) {
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;
355       }
356       else {
357 	 n += 10;
358       }
359    }
360 
361    if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {
362       if (formats) {
363          formats[n++] = GL_COMPRESSED_RGB8_ETC2;
364          formats[n++] = GL_COMPRESSED_RGBA8_ETC2_EAC;
365          formats[n++] = GL_COMPRESSED_R11_EAC;
366          formats[n++] = GL_COMPRESSED_RG11_EAC;
367          formats[n++] = GL_COMPRESSED_SIGNED_R11_EAC;
368          formats[n++] = GL_COMPRESSED_SIGNED_RG11_EAC;
369          formats[n++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
370       } else {
371          n += 7;
372       }
373    }
374 
375    if (_mesa_is_gles3(ctx)) {
376       if (formats) {
377          formats[n++] = GL_COMPRESSED_SRGB8_ETC2;
378          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
379          formats[n++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
380       } else {
381          n += 3;
382       }
383    }
384 
385    /* The KHR_texture_compression_astc_hdr spec says:
386     *
387     *    "Interactions with OpenGL 4.2
388     *
389     *        OpenGL 4.2 supports the feature that compressed textures can be
390     *        compressed online, by passing the compressed texture format enum as
391     *        the internal format when uploading a texture using TexImage1D,
392     *        TexImage2D or TexImage3D (see Section 3.9.3, Texture Image
393     *        Specification, subsection Encoding of Special Internal Formats).
394     *
395     *        Due to the complexity of the ASTC compression algorithm, it is not
396     *        usually suitable for online use, and therefore ASTC support will be
397     *        limited to pre-compressed textures only. Where on-device compression
398     *        is required, a domain-specific limited compressor will typically
399     *        be used, and this is therefore not suitable for implementation in
400     *        the driver.
401     *
402     *        In particular, the ASTC format specifiers will not be added to
403     *        Table 3.14, and thus will not be accepted by the TexImage*D
404     *        functions, and will not be returned by the (already deprecated)
405     *        COMPRESSED_TEXTURE_FORMATS query."
406     *
407     * The ES and the desktop specs diverge here. In OpenGL ES, the COMPRESSED_TEXTURE_FORMATS
408     * query returns the set of supported specific compressed formats.
409     */
410    if (ctx->API == API_OPENGLES2 &&
411        ctx->Extensions.KHR_texture_compression_astc_ldr) {
412       if (formats) {
413          formats[n++] = GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
414          formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
415          formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
416          formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
417          formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
418          formats[n++] = GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
419          formats[n++] = GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
420          formats[n++] = GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
421          formats[n++] = GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
422          formats[n++] = GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
423          formats[n++] = GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
424          formats[n++] = GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
425          formats[n++] = GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
426          formats[n++] = GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
427          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
428          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
429          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
430          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
431          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
432          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
433          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
434          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
435          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
436          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
437          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
438          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
439          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
440          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
441       }
442       else {
443          n += 28;
444       }
445    }
446 
447    if (_mesa_is_gles3(ctx) &&
448        ctx->Extensions.OES_texture_compression_astc) {
449       if (formats) {
450          formats[n++] = GL_COMPRESSED_RGBA_ASTC_3x3x3_OES;
451          formats[n++] = GL_COMPRESSED_RGBA_ASTC_4x3x3_OES;
452          formats[n++] = GL_COMPRESSED_RGBA_ASTC_4x4x3_OES;
453          formats[n++] = GL_COMPRESSED_RGBA_ASTC_4x4x4_OES;
454          formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x4x4_OES;
455          formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x5x4_OES;
456          formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x5x5_OES;
457          formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x5x5_OES;
458          formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x6x5_OES;
459          formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x6x6_OES;
460          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES;
461          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES;
462          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES;
463          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES;
464          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES;
465          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES;
466          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES;
467          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES;
468          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES;
469          formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES;
470       }
471       else {
472          n += 20;
473       }
474    }
475 
476    return n;
477 }
478 
479 
480 /**
481  * Convert GLenum to a compressed MESA_FORMAT_x.
482  */
483 mesa_format
_mesa_glenum_to_compressed_format(GLenum format)484 _mesa_glenum_to_compressed_format(GLenum format)
485 {
486    switch (format) {
487    case GL_COMPRESSED_RGB_FXT1_3DFX:
488       return MESA_FORMAT_RGB_FXT1;
489    case GL_COMPRESSED_RGBA_FXT1_3DFX:
490       return MESA_FORMAT_RGBA_FXT1;
491 
492    case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
493    case GL_RGB_S3TC:
494    case GL_RGB4_S3TC:
495       return MESA_FORMAT_RGB_DXT1;
496    case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
497       return MESA_FORMAT_RGBA_DXT1;
498    case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
499    case GL_RGBA_S3TC:
500    case GL_RGBA4_S3TC:
501       return MESA_FORMAT_RGBA_DXT3;
502    case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
503       return MESA_FORMAT_RGBA_DXT5;
504 
505    case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
506       return MESA_FORMAT_SRGB_DXT1;
507    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
508       return MESA_FORMAT_SRGBA_DXT1;
509    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
510       return MESA_FORMAT_SRGBA_DXT3;
511    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
512       return MESA_FORMAT_SRGBA_DXT5;
513 
514    case GL_COMPRESSED_RED_RGTC1:
515       return MESA_FORMAT_R_RGTC1_UNORM;
516    case GL_COMPRESSED_SIGNED_RED_RGTC1:
517       return MESA_FORMAT_R_RGTC1_SNORM;
518    case GL_COMPRESSED_RG_RGTC2:
519       return MESA_FORMAT_RG_RGTC2_UNORM;
520    case GL_COMPRESSED_SIGNED_RG_RGTC2:
521       return MESA_FORMAT_RG_RGTC2_SNORM;
522 
523    case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
524       return MESA_FORMAT_L_LATC1_UNORM;
525    case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
526       return MESA_FORMAT_L_LATC1_SNORM;
527    case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
528    case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
529       return MESA_FORMAT_LA_LATC2_UNORM;
530    case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
531       return MESA_FORMAT_LA_LATC2_SNORM;
532 
533    case GL_ETC1_RGB8_OES:
534       return MESA_FORMAT_ETC1_RGB8;
535    case GL_COMPRESSED_RGB8_ETC2:
536       return MESA_FORMAT_ETC2_RGB8;
537    case GL_COMPRESSED_SRGB8_ETC2:
538       return MESA_FORMAT_ETC2_SRGB8;
539    case GL_COMPRESSED_RGBA8_ETC2_EAC:
540       return MESA_FORMAT_ETC2_RGBA8_EAC;
541    case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
542       return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
543    case GL_COMPRESSED_R11_EAC:
544       return MESA_FORMAT_ETC2_R11_EAC;
545    case GL_COMPRESSED_RG11_EAC:
546       return MESA_FORMAT_ETC2_RG11_EAC;
547    case GL_COMPRESSED_SIGNED_R11_EAC:
548       return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
549    case GL_COMPRESSED_SIGNED_RG11_EAC:
550       return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
551    case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
552       return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
553    case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
554       return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
555 
556    case GL_COMPRESSED_RGBA_BPTC_UNORM:
557       return MESA_FORMAT_BPTC_RGBA_UNORM;
558    case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
559       return MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM;
560    case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
561       return MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT;
562    case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
563       return MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT;
564 
565    case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
566       return MESA_FORMAT_RGBA_ASTC_4x4;
567    case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
568       return MESA_FORMAT_RGBA_ASTC_5x4;
569    case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
570       return MESA_FORMAT_RGBA_ASTC_5x5;
571    case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
572       return MESA_FORMAT_RGBA_ASTC_6x5;
573    case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
574       return MESA_FORMAT_RGBA_ASTC_6x6;
575    case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
576       return MESA_FORMAT_RGBA_ASTC_8x5;
577    case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
578       return MESA_FORMAT_RGBA_ASTC_8x6;
579    case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
580       return MESA_FORMAT_RGBA_ASTC_8x8;
581    case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
582       return MESA_FORMAT_RGBA_ASTC_10x5;
583    case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
584       return MESA_FORMAT_RGBA_ASTC_10x6;
585    case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
586       return MESA_FORMAT_RGBA_ASTC_10x8;
587    case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
588       return MESA_FORMAT_RGBA_ASTC_10x10;
589    case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
590       return MESA_FORMAT_RGBA_ASTC_12x10;
591    case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
592       return MESA_FORMAT_RGBA_ASTC_12x12;
593    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
594       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4;
595    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
596       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4;
597    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
598       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5;
599    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
600       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5;
601    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
602       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6;
603    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
604       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5;
605    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
606       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6;
607    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
608       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8;
609    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
610       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5;
611    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
612       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6;
613    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
614       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8;
615    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
616       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10;
617    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
618       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10;
619    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
620       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12;
621    case GL_COMPRESSED_RGBA_ASTC_3x3x3_OES:
622       return MESA_FORMAT_RGBA_ASTC_3x3x3;
623    case GL_COMPRESSED_RGBA_ASTC_4x3x3_OES:
624       return MESA_FORMAT_RGBA_ASTC_4x3x3;
625    case GL_COMPRESSED_RGBA_ASTC_4x4x3_OES:
626       return MESA_FORMAT_RGBA_ASTC_4x4x3;
627    case GL_COMPRESSED_RGBA_ASTC_4x4x4_OES:
628       return MESA_FORMAT_RGBA_ASTC_4x4x4;
629    case GL_COMPRESSED_RGBA_ASTC_5x4x4_OES:
630       return MESA_FORMAT_RGBA_ASTC_5x4x4;
631    case GL_COMPRESSED_RGBA_ASTC_5x5x4_OES:
632       return MESA_FORMAT_RGBA_ASTC_5x5x4;
633    case GL_COMPRESSED_RGBA_ASTC_5x5x5_OES:
634       return MESA_FORMAT_RGBA_ASTC_5x5x5;
635    case GL_COMPRESSED_RGBA_ASTC_6x5x5_OES:
636       return MESA_FORMAT_RGBA_ASTC_6x5x5;
637    case GL_COMPRESSED_RGBA_ASTC_6x6x5_OES:
638       return MESA_FORMAT_RGBA_ASTC_6x6x5;
639    case GL_COMPRESSED_RGBA_ASTC_6x6x6_OES:
640       return MESA_FORMAT_RGBA_ASTC_6x6x6;
641    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES:
642       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_3x3x3;
643    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES:
644       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x3x3;
645    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES:
646       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4x3;
647    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES:
648       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4x4;
649    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES:
650       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4x4;
651    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES:
652       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5x4;
653    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES:
654       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5x5;
655    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES:
656       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5x5;
657    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES:
658       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6x5;
659    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES:
660       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6x6;
661 
662    default:
663       return MESA_FORMAT_NONE;
664    }
665 }
666 
667 
668 /**
669  * Given a compressed MESA_FORMAT_x value, return the corresponding
670  * GLenum for that format.
671  * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
672  * which must return the specific texture format used when the user might
673  * have originally specified a generic compressed format in their
674  * glTexImage2D() call.
675  * For non-compressed textures, we always return the user-specified
676  * internal format unchanged.
677  */
678 GLenum
_mesa_compressed_format_to_glenum(struct gl_context * ctx,mesa_format mesaFormat)679 _mesa_compressed_format_to_glenum(struct gl_context *ctx, mesa_format mesaFormat)
680 {
681    switch (mesaFormat) {
682    case MESA_FORMAT_RGB_FXT1:
683       return GL_COMPRESSED_RGB_FXT1_3DFX;
684    case MESA_FORMAT_RGBA_FXT1:
685       return GL_COMPRESSED_RGBA_FXT1_3DFX;
686    case MESA_FORMAT_RGB_DXT1:
687       return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
688    case MESA_FORMAT_RGBA_DXT1:
689       return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
690    case MESA_FORMAT_RGBA_DXT3:
691       return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
692    case MESA_FORMAT_RGBA_DXT5:
693       return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
694    case MESA_FORMAT_SRGB_DXT1:
695       return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
696    case MESA_FORMAT_SRGBA_DXT1:
697       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
698    case MESA_FORMAT_SRGBA_DXT3:
699       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
700    case MESA_FORMAT_SRGBA_DXT5:
701       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
702    case MESA_FORMAT_R_RGTC1_UNORM:
703       return GL_COMPRESSED_RED_RGTC1;
704    case MESA_FORMAT_R_RGTC1_SNORM:
705       return GL_COMPRESSED_SIGNED_RED_RGTC1;
706    case MESA_FORMAT_RG_RGTC2_UNORM:
707       return GL_COMPRESSED_RG_RGTC2;
708    case MESA_FORMAT_RG_RGTC2_SNORM:
709       return GL_COMPRESSED_SIGNED_RG_RGTC2;
710 
711    case MESA_FORMAT_L_LATC1_UNORM:
712       return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
713    case MESA_FORMAT_L_LATC1_SNORM:
714       return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
715    case MESA_FORMAT_LA_LATC2_UNORM:
716       return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
717    case MESA_FORMAT_LA_LATC2_SNORM:
718       return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
719 
720    case MESA_FORMAT_ETC1_RGB8:
721       return GL_ETC1_RGB8_OES;
722    case MESA_FORMAT_ETC2_RGB8:
723       return GL_COMPRESSED_RGB8_ETC2;
724    case MESA_FORMAT_ETC2_SRGB8:
725       return GL_COMPRESSED_SRGB8_ETC2;
726    case MESA_FORMAT_ETC2_RGBA8_EAC:
727       return GL_COMPRESSED_RGBA8_ETC2_EAC;
728    case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
729       return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
730    case MESA_FORMAT_ETC2_R11_EAC:
731       return GL_COMPRESSED_R11_EAC;
732    case MESA_FORMAT_ETC2_RG11_EAC:
733       return GL_COMPRESSED_RG11_EAC;
734    case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
735       return GL_COMPRESSED_SIGNED_R11_EAC;
736    case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
737       return GL_COMPRESSED_SIGNED_RG11_EAC;
738    case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
739       return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
740    case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
741       return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
742 
743    case MESA_FORMAT_BPTC_RGBA_UNORM:
744       return GL_COMPRESSED_RGBA_BPTC_UNORM;
745    case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
746       return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
747    case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
748       return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
749    case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
750       return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
751 
752    case MESA_FORMAT_RGBA_ASTC_4x4:
753       return GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
754    case MESA_FORMAT_RGBA_ASTC_5x4:
755       return GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
756    case MESA_FORMAT_RGBA_ASTC_5x5:
757       return GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
758    case MESA_FORMAT_RGBA_ASTC_6x5:
759       return GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
760    case MESA_FORMAT_RGBA_ASTC_6x6:
761       return GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
762    case MESA_FORMAT_RGBA_ASTC_8x5:
763       return GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
764    case MESA_FORMAT_RGBA_ASTC_8x6:
765       return GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
766    case MESA_FORMAT_RGBA_ASTC_8x8:
767       return GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
768    case MESA_FORMAT_RGBA_ASTC_10x5:
769       return GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
770    case MESA_FORMAT_RGBA_ASTC_10x6:
771       return GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
772    case MESA_FORMAT_RGBA_ASTC_10x8:
773       return GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
774    case MESA_FORMAT_RGBA_ASTC_10x10:
775       return GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
776    case MESA_FORMAT_RGBA_ASTC_12x10:
777       return GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
778    case MESA_FORMAT_RGBA_ASTC_12x12:
779       return GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
780    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
781       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
782    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
783       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
784    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
785       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
786    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
787       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
788    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
789       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
790    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
791       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
792    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
793       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
794    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
795       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
796    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
797       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
798    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
799       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
800    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
801       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
802    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
803       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
804    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
805       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
806    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
807       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
808 
809    case MESA_FORMAT_RGBA_ASTC_3x3x3:
810       return GL_COMPRESSED_RGBA_ASTC_3x3x3_OES;
811    case MESA_FORMAT_RGBA_ASTC_4x3x3:
812       return GL_COMPRESSED_RGBA_ASTC_4x3x3_OES;
813    case MESA_FORMAT_RGBA_ASTC_4x4x3:
814       return GL_COMPRESSED_RGBA_ASTC_4x4x3_OES;
815    case MESA_FORMAT_RGBA_ASTC_4x4x4:
816       return GL_COMPRESSED_RGBA_ASTC_4x4x4_OES;
817    case MESA_FORMAT_RGBA_ASTC_5x4x4:
818       return GL_COMPRESSED_RGBA_ASTC_5x4x4_OES;
819    case MESA_FORMAT_RGBA_ASTC_5x5x4:
820       return GL_COMPRESSED_RGBA_ASTC_5x5x4_OES;
821    case MESA_FORMAT_RGBA_ASTC_5x5x5:
822       return GL_COMPRESSED_RGBA_ASTC_5x5x5_OES;
823    case MESA_FORMAT_RGBA_ASTC_6x5x5:
824       return GL_COMPRESSED_RGBA_ASTC_6x5x5_OES;
825    case MESA_FORMAT_RGBA_ASTC_6x6x5:
826       return GL_COMPRESSED_RGBA_ASTC_6x6x5_OES;
827    case MESA_FORMAT_RGBA_ASTC_6x6x6:
828       return GL_COMPRESSED_RGBA_ASTC_6x6x6_OES;
829    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_3x3x3:
830       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES;
831    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x3x3:
832       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES;
833    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4x3:
834       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES;
835    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4x4:
836       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES;
837    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4x4:
838       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES;
839    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5x4:
840       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES;
841    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5x5:
842       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES;
843    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5x5:
844       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES;
845    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6x5:
846       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES;
847    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6x6:
848       return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES;
849    default:
850       _mesa_problem(ctx, "Unexpected mesa texture format in"
851                     " _mesa_compressed_format_to_glenum()");
852       return 0;
853    }
854 }
855 
856 
857 /*
858  * Return the address of the pixel at (col, row, img) in a
859  * compressed texture image.
860  * \param col, row, img - image position (3D), should be a multiple of the
861  *                        format's block size.
862  * \param format - compressed image format
863  * \param width - image width (stride) in pixels
864  * \param image - the image address
865  * \return address of pixel at (row, col, img)
866  */
867 GLubyte *
_mesa_compressed_image_address(GLint col,GLint row,GLint img,mesa_format mesaFormat,GLsizei width,const GLubyte * image)868 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
869                                mesa_format mesaFormat,
870                                GLsizei width, const GLubyte *image)
871 {
872    /* XXX only 2D images implemented, not 3D */
873    const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
874    GLuint bw, bh;
875    GLint offset;
876 
877    _mesa_get_format_block_size(mesaFormat, &bw, &bh);
878 
879    assert(col % bw == 0);
880    assert(row % bh == 0);
881 
882    offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
883    offset *= blockSize;
884 
885    return (GLubyte *) image + offset;
886 }
887 
888 
889 /**
890  * Return a texel-fetch function for the given format, or NULL if
891  * invalid format.
892  */
893 compressed_fetch_func
_mesa_get_compressed_fetch_func(mesa_format format)894 _mesa_get_compressed_fetch_func(mesa_format format)
895 {
896    switch (_mesa_get_format_layout(format)) {
897    case MESA_FORMAT_LAYOUT_S3TC:
898       return _mesa_get_dxt_fetch_func(format);
899    case MESA_FORMAT_LAYOUT_FXT1:
900       return _mesa_get_fxt_fetch_func(format);
901    case MESA_FORMAT_LAYOUT_RGTC:
902    case MESA_FORMAT_LAYOUT_LATC:
903       return _mesa_get_compressed_rgtc_func(format);
904    case MESA_FORMAT_LAYOUT_ETC1:
905       return _mesa_get_etc_fetch_func(format);
906    case MESA_FORMAT_LAYOUT_BPTC:
907       return _mesa_get_bptc_fetch_func(format);
908    default:
909       return NULL;
910    }
911 }
912 
913 
914 /**
915  * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
916  * \param srcRowStride  stride in bytes between rows of blocks in the
917  *                      compressed source image.
918  */
919 void
_mesa_decompress_image(mesa_format format,GLuint width,GLuint height,const GLubyte * src,GLint srcRowStride,GLfloat * dest)920 _mesa_decompress_image(mesa_format format, GLuint width, GLuint height,
921                        const GLubyte *src, GLint srcRowStride,
922                        GLfloat *dest)
923 {
924    compressed_fetch_func fetch;
925    GLuint i, j;
926    GLuint bytes, bw, bh;
927    GLint stride;
928 
929    bytes = _mesa_get_format_bytes(format);
930    _mesa_get_format_block_size(format, &bw, &bh);
931 
932    fetch = _mesa_get_compressed_fetch_func(format);
933    if (!fetch) {
934       _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
935       return;
936    }
937 
938    stride = srcRowStride * bh / bytes;
939 
940    for (j = 0; j < height; j++) {
941       for (i = 0; i < width; i++) {
942          fetch(src, stride, i, j, dest);
943          dest += 4;
944       }
945    }
946 }
947