Merge branch 'master' of ../mesa into vulkan
[mesa.git] / src / mesa / main / texcompress.c
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
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 * The KHR_texture_compression_astc_hdr spec says:
247 *
248 * "Interactions with OpenGL 4.2
249 *
250 * OpenGL 4.2 supports the feature that compressed textures can be
251 * compressed online, by passing the compressed texture format enum as
252 * the internal format when uploading a texture using TexImage1D,
253 * TexImage2D or TexImage3D (see Section 3.9.3, Texture Image
254 * Specification, subsection Encoding of Special Internal Formats).
255 *
256 * Due to the complexity of the ASTC compression algorithm, it is not
257 * usually suitable for online use, and therefore ASTC support will be
258 * limited to pre-compressed textures only. Where on-device compression
259 * is required, a domain-specific limited compressor will typically
260 * be used, and this is therefore not suitable for implementation in
261 * the driver.
262 *
263 * In particular, the ASTC format specifiers will not be added to
264 * Table 3.14, and thus will not be accepted by the TexImage*D
265 * functions, and will not be returned by the (already deprecated)
266 * COMPRESSED_TEXTURE_FORMATS query."
267 *
268 * There is no formal spec for GL_ATI_texture_compression_3dc. Since the
269 * formats added by this extension are luminance-alpha formats, it is
270 * reasonable to expect them to follow the same rules as
271 * GL_EXT_texture_compression_latc. At the very least, Catalyst 11.6 does not
272 * expose the 3dc formats through this mechanism.
273 *
274 * The spec for GL_ARB_texture_compression_bptc doesn't mention whether it
275 * should be included in GL_COMPRESSED_TEXTURE_FORMATS. However as it takes a
276 * very long time to compress the textures in this format it's probably not
277 * very useful as a general format where the GL will have to compress it on
278 * the fly.
279 *
280 * \param ctx the GL context
281 * \param formats the resulting format list (may be NULL).
282 *
283 * \return number of formats.
284 */
285 GLuint
286 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats)
287 {
288 GLuint n = 0;
289 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
290 if (formats) {
291 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
292 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
293 }
294 else {
295 n += 2;
296 }
297 }
298
299 if (ctx->Extensions.EXT_texture_compression_s3tc) {
300 if (formats) {
301 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
302 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
303 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
304 }
305 else {
306 n += 3;
307 }
308
309 /* The ES and desktop GL specs diverge here.
310 *
311 * In desktop OpenGL, the driver can perform online compression of
312 * uncompressed texture data. GL_NUM_COMPRESSED_TEXTURE_FORMATS and
313 * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
314 * formats that it could ask the driver to compress with some
315 * expectation of quality. The GL_ARB_texture_compression spec
316 * calls this "suitable for general-purpose usage." As noted
317 * above, this means GL_COMPRESSED_RGBA_S3TC_DXT1_EXT is not
318 * included in the list.
319 *
320 * In OpenGL ES, the driver never performs compression.
321 * GL_NUM_COMPRESSED_TEXTURE_FORMATS and
322 * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
323 * formats that the driver can receive from the application. It
324 * is the *complete* list of formats. The
325 * GL_EXT_texture_compression_s3tc spec says:
326 *
327 * "New State for OpenGL ES 2.0.25 and 3.0.2 Specifications
328 *
329 * The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
330 * COMPRESSED_TEXTURE_FORMATS include
331 * COMPRESSED_RGB_S3TC_DXT1_EXT,
332 * COMPRESSED_RGBA_S3TC_DXT1_EXT,
333 * COMPRESSED_RGBA_S3TC_DXT3_EXT, and
334 * COMPRESSED_RGBA_S3TC_DXT5_EXT."
335 *
336 * Note that the addition is only to the OpenGL ES specification!
337 */
338 if (_mesa_is_gles(ctx)) {
339 if (formats) {
340 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
341 } else {
342 n += 1;
343 }
344 }
345 }
346
347 /* The GL_OES_compressed_ETC1_RGB8_texture spec says:
348 *
349 * "New State
350 *
351 * The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
352 * COMPRESSED_TEXTURE_FORMATS include ETC1_RGB8_OES."
353 */
354 if (_mesa_is_gles(ctx)
355 && ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
356 if (formats) {
357 formats[n++] = GL_ETC1_RGB8_OES;
358 }
359 else {
360 n += 1;
361 }
362 }
363
364 if (ctx->API == API_OPENGLES) {
365 if (formats) {
366 formats[n++] = GL_PALETTE4_RGB8_OES;
367 formats[n++] = GL_PALETTE4_RGBA8_OES;
368 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
369 formats[n++] = GL_PALETTE4_RGBA4_OES;
370 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
371 formats[n++] = GL_PALETTE8_RGB8_OES;
372 formats[n++] = GL_PALETTE8_RGBA8_OES;
373 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
374 formats[n++] = GL_PALETTE8_RGBA4_OES;
375 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
376 }
377 else {
378 n += 10;
379 }
380 }
381
382 if (_mesa_is_gles3(ctx)) {
383 if (formats) {
384 formats[n++] = GL_COMPRESSED_RGB8_ETC2;
385 formats[n++] = GL_COMPRESSED_SRGB8_ETC2;
386 formats[n++] = GL_COMPRESSED_RGBA8_ETC2_EAC;
387 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
388 formats[n++] = GL_COMPRESSED_R11_EAC;
389 formats[n++] = GL_COMPRESSED_RG11_EAC;
390 formats[n++] = GL_COMPRESSED_SIGNED_R11_EAC;
391 formats[n++] = GL_COMPRESSED_SIGNED_RG11_EAC;
392 formats[n++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
393 formats[n++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
394 }
395 else {
396 n += 10;
397 }
398 }
399 return n;
400 }
401
402
403 /**
404 * Convert a compressed MESA_FORMAT_x to a GLenum.
405 */
406 mesa_format
407 _mesa_glenum_to_compressed_format(GLenum format)
408 {
409 switch (format) {
410 case GL_COMPRESSED_RGB_FXT1_3DFX:
411 return MESA_FORMAT_RGB_FXT1;
412 case GL_COMPRESSED_RGBA_FXT1_3DFX:
413 return MESA_FORMAT_RGBA_FXT1;
414
415 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
416 case GL_RGB_S3TC:
417 case GL_RGB4_S3TC:
418 return MESA_FORMAT_RGB_DXT1;
419 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
420 return MESA_FORMAT_RGBA_DXT1;
421 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
422 case GL_RGBA_S3TC:
423 case GL_RGBA4_S3TC:
424 return MESA_FORMAT_RGBA_DXT3;
425 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
426 return MESA_FORMAT_RGBA_DXT5;
427
428 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
429 return MESA_FORMAT_SRGB_DXT1;
430 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
431 return MESA_FORMAT_SRGBA_DXT1;
432 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
433 return MESA_FORMAT_SRGBA_DXT3;
434 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
435 return MESA_FORMAT_SRGBA_DXT5;
436
437 case GL_COMPRESSED_RED_RGTC1:
438 return MESA_FORMAT_R_RGTC1_UNORM;
439 case GL_COMPRESSED_SIGNED_RED_RGTC1:
440 return MESA_FORMAT_R_RGTC1_SNORM;
441 case GL_COMPRESSED_RG_RGTC2:
442 return MESA_FORMAT_RG_RGTC2_UNORM;
443 case GL_COMPRESSED_SIGNED_RG_RGTC2:
444 return MESA_FORMAT_RG_RGTC2_SNORM;
445
446 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
447 return MESA_FORMAT_L_LATC1_UNORM;
448 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
449 return MESA_FORMAT_L_LATC1_SNORM;
450 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
451 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
452 return MESA_FORMAT_LA_LATC2_UNORM;
453 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
454 return MESA_FORMAT_LA_LATC2_SNORM;
455
456 case GL_ETC1_RGB8_OES:
457 return MESA_FORMAT_ETC1_RGB8;
458 case GL_COMPRESSED_RGB8_ETC2:
459 return MESA_FORMAT_ETC2_RGB8;
460 case GL_COMPRESSED_SRGB8_ETC2:
461 return MESA_FORMAT_ETC2_SRGB8;
462 case GL_COMPRESSED_RGBA8_ETC2_EAC:
463 return MESA_FORMAT_ETC2_RGBA8_EAC;
464 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
465 return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
466 case GL_COMPRESSED_R11_EAC:
467 return MESA_FORMAT_ETC2_R11_EAC;
468 case GL_COMPRESSED_RG11_EAC:
469 return MESA_FORMAT_ETC2_RG11_EAC;
470 case GL_COMPRESSED_SIGNED_R11_EAC:
471 return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
472 case GL_COMPRESSED_SIGNED_RG11_EAC:
473 return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
474 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
475 return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
476 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
477 return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
478
479 case GL_COMPRESSED_RGBA_BPTC_UNORM:
480 return MESA_FORMAT_BPTC_RGBA_UNORM;
481 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
482 return MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM;
483 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
484 return MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT;
485 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
486 return MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT;
487
488 case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
489 return MESA_FORMAT_RGBA_ASTC_4x4;
490 case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
491 return MESA_FORMAT_RGBA_ASTC_5x4;
492 case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
493 return MESA_FORMAT_RGBA_ASTC_5x5;
494 case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
495 return MESA_FORMAT_RGBA_ASTC_6x5;
496 case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
497 return MESA_FORMAT_RGBA_ASTC_6x6;
498 case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
499 return MESA_FORMAT_RGBA_ASTC_8x5;
500 case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
501 return MESA_FORMAT_RGBA_ASTC_8x6;
502 case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
503 return MESA_FORMAT_RGBA_ASTC_8x8;
504 case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
505 return MESA_FORMAT_RGBA_ASTC_10x5;
506 case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
507 return MESA_FORMAT_RGBA_ASTC_10x6;
508 case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
509 return MESA_FORMAT_RGBA_ASTC_10x8;
510 case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
511 return MESA_FORMAT_RGBA_ASTC_10x10;
512 case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
513 return MESA_FORMAT_RGBA_ASTC_12x10;
514 case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
515 return MESA_FORMAT_RGBA_ASTC_12x12;
516 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
517 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4;
518 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
519 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4;
520 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
521 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5;
522 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
523 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5;
524 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
525 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6;
526 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
527 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5;
528 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
529 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6;
530 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
531 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8;
532 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
533 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5;
534 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
535 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6;
536 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
537 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8;
538 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
539 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10;
540 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
541 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10;
542 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
543 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12;
544
545 default:
546 return MESA_FORMAT_NONE;
547 }
548 }
549
550
551 /**
552 * Given a compressed MESA_FORMAT_x value, return the corresponding
553 * GLenum for that format.
554 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
555 * which must return the specific texture format used when the user might
556 * have originally specified a generic compressed format in their
557 * glTexImage2D() call.
558 * For non-compressed textures, we always return the user-specified
559 * internal format unchanged.
560 */
561 GLenum
562 _mesa_compressed_format_to_glenum(struct gl_context *ctx, mesa_format mesaFormat)
563 {
564 switch (mesaFormat) {
565 case MESA_FORMAT_RGB_FXT1:
566 return GL_COMPRESSED_RGB_FXT1_3DFX;
567 case MESA_FORMAT_RGBA_FXT1:
568 return GL_COMPRESSED_RGBA_FXT1_3DFX;
569 case MESA_FORMAT_RGB_DXT1:
570 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
571 case MESA_FORMAT_RGBA_DXT1:
572 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
573 case MESA_FORMAT_RGBA_DXT3:
574 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
575 case MESA_FORMAT_RGBA_DXT5:
576 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
577 case MESA_FORMAT_SRGB_DXT1:
578 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
579 case MESA_FORMAT_SRGBA_DXT1:
580 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
581 case MESA_FORMAT_SRGBA_DXT3:
582 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
583 case MESA_FORMAT_SRGBA_DXT5:
584 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
585 case MESA_FORMAT_R_RGTC1_UNORM:
586 return GL_COMPRESSED_RED_RGTC1;
587 case MESA_FORMAT_R_RGTC1_SNORM:
588 return GL_COMPRESSED_SIGNED_RED_RGTC1;
589 case MESA_FORMAT_RG_RGTC2_UNORM:
590 return GL_COMPRESSED_RG_RGTC2;
591 case MESA_FORMAT_RG_RGTC2_SNORM:
592 return GL_COMPRESSED_SIGNED_RG_RGTC2;
593
594 case MESA_FORMAT_L_LATC1_UNORM:
595 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
596 case MESA_FORMAT_L_LATC1_SNORM:
597 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
598 case MESA_FORMAT_LA_LATC2_UNORM:
599 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
600 case MESA_FORMAT_LA_LATC2_SNORM:
601 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
602
603 case MESA_FORMAT_ETC1_RGB8:
604 return GL_ETC1_RGB8_OES;
605 case MESA_FORMAT_ETC2_RGB8:
606 return GL_COMPRESSED_RGB8_ETC2;
607 case MESA_FORMAT_ETC2_SRGB8:
608 return GL_COMPRESSED_SRGB8_ETC2;
609 case MESA_FORMAT_ETC2_RGBA8_EAC:
610 return GL_COMPRESSED_RGBA8_ETC2_EAC;
611 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
612 return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
613 case MESA_FORMAT_ETC2_R11_EAC:
614 return GL_COMPRESSED_R11_EAC;
615 case MESA_FORMAT_ETC2_RG11_EAC:
616 return GL_COMPRESSED_RG11_EAC;
617 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
618 return GL_COMPRESSED_SIGNED_R11_EAC;
619 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
620 return GL_COMPRESSED_SIGNED_RG11_EAC;
621 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
622 return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
623 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
624 return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
625
626 case MESA_FORMAT_BPTC_RGBA_UNORM:
627 return GL_COMPRESSED_RGBA_BPTC_UNORM;
628 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
629 return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
630 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
631 return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
632 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
633 return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
634
635 case MESA_FORMAT_RGBA_ASTC_4x4:
636 return GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
637 case MESA_FORMAT_RGBA_ASTC_5x4:
638 return GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
639 case MESA_FORMAT_RGBA_ASTC_5x5:
640 return GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
641 case MESA_FORMAT_RGBA_ASTC_6x5:
642 return GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
643 case MESA_FORMAT_RGBA_ASTC_6x6:
644 return GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
645 case MESA_FORMAT_RGBA_ASTC_8x5:
646 return GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
647 case MESA_FORMAT_RGBA_ASTC_8x6:
648 return GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
649 case MESA_FORMAT_RGBA_ASTC_8x8:
650 return GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
651 case MESA_FORMAT_RGBA_ASTC_10x5:
652 return GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
653 case MESA_FORMAT_RGBA_ASTC_10x6:
654 return GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
655 case MESA_FORMAT_RGBA_ASTC_10x8:
656 return GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
657 case MESA_FORMAT_RGBA_ASTC_10x10:
658 return GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
659 case MESA_FORMAT_RGBA_ASTC_12x10:
660 return GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
661 case MESA_FORMAT_RGBA_ASTC_12x12:
662 return GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
663 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
664 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
665 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
666 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
667 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
668 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
669 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
670 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
671 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
672 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
673 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
674 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
675 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
676 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
677 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
678 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
679 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
680 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
681 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
682 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
683 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
684 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
685 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
686 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
687 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
688 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
689 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
690 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
691
692 default:
693 _mesa_problem(ctx, "Unexpected mesa texture format in"
694 " _mesa_compressed_format_to_glenum()");
695 return 0;
696 }
697 }
698
699
700 /*
701 * Return the address of the pixel at (col, row, img) in a
702 * compressed texture image.
703 * \param col, row, img - image position (3D), should be a multiple of the
704 * format's block size.
705 * \param format - compressed image format
706 * \param width - image width (stride) in pixels
707 * \param image - the image address
708 * \return address of pixel at (row, col, img)
709 */
710 GLubyte *
711 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
712 mesa_format mesaFormat,
713 GLsizei width, const GLubyte *image)
714 {
715 /* XXX only 2D images implemented, not 3D */
716 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
717 GLuint bw, bh;
718 GLint offset;
719
720 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
721
722 assert(col % bw == 0);
723 assert(row % bh == 0);
724
725 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
726 offset *= blockSize;
727
728 return (GLubyte *) image + offset;
729 }
730
731
732 /**
733 * Return a texel-fetch function for the given format, or NULL if
734 * invalid format.
735 */
736 compressed_fetch_func
737 _mesa_get_compressed_fetch_func(mesa_format format)
738 {
739 switch (_mesa_get_format_layout(format)) {
740 case MESA_FORMAT_LAYOUT_S3TC:
741 return _mesa_get_dxt_fetch_func(format);
742 case MESA_FORMAT_LAYOUT_FXT1:
743 return _mesa_get_fxt_fetch_func(format);
744 case MESA_FORMAT_LAYOUT_RGTC:
745 case MESA_FORMAT_LAYOUT_LATC:
746 return _mesa_get_compressed_rgtc_func(format);
747 case MESA_FORMAT_LAYOUT_ETC1:
748 return _mesa_get_etc_fetch_func(format);
749 case MESA_FORMAT_LAYOUT_BPTC:
750 return _mesa_get_bptc_fetch_func(format);
751 default:
752 return NULL;
753 }
754 }
755
756
757 /**
758 * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
759 * \param srcRowStride stride in bytes between rows of blocks in the
760 * compressed source image.
761 */
762 void
763 _mesa_decompress_image(mesa_format format, GLuint width, GLuint height,
764 const GLubyte *src, GLint srcRowStride,
765 GLfloat *dest)
766 {
767 compressed_fetch_func fetch;
768 GLuint i, j;
769 GLuint bytes, bw, bh;
770 GLint stride;
771
772 bytes = _mesa_get_format_bytes(format);
773 _mesa_get_format_block_size(format, &bw, &bh);
774
775 fetch = _mesa_get_compressed_fetch_func(format);
776 if (!fetch) {
777 _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
778 return;
779 }
780
781 stride = srcRowStride * bh / bytes;
782
783 for (j = 0; j < height; j++) {
784 for (i = 0; i < width; i++) {
785 fetch(src, stride, i, j, dest);
786 dest += 4;
787 }
788 }
789 }