Merge remote-tracking branch 'mesa-public/master' 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 return GL_RGB;
104
105 case GL_COMPRESSED_RGBA:
106 case GL_COMPRESSED_SRGB_ALPHA:
107 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
108 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
109 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
110 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
111 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
112 case GL_COMPRESSED_RGBA_FXT1_3DFX:
113 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
114 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
115 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
116 case GL_COMPRESSED_RGBA8_ETC2_EAC:
117 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
118 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
119 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
120 return GL_RGBA;
121
122 case GL_COMPRESSED_ALPHA:
123 return GL_ALPHA;
124
125 case GL_COMPRESSED_LUMINANCE:
126 case GL_COMPRESSED_SLUMINANCE:
127 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
128 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
129 return GL_LUMINANCE;
130
131 case GL_COMPRESSED_LUMINANCE_ALPHA:
132 case GL_COMPRESSED_SLUMINANCE_ALPHA:
133 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
134 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
135 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
136 return GL_LUMINANCE_ALPHA;
137
138 case GL_COMPRESSED_INTENSITY:
139 return GL_INTENSITY;
140
141 default:
142 return 0;
143 }
144 }
145
146 /**
147 * Return list of (and count of) all specific texture compression
148 * formats that are supported.
149 *
150 * Some formats are \b not returned by this function. The
151 * \c GL_COMPRESSED_TEXTURE_FORMATS query only returns formats that are
152 * "suitable for general-purpose usage." All texture compression extensions
153 * have taken this to mean either linear RGB or linear RGBA.
154 *
155 * The GL_ARB_texture_compress_rgtc spec says:
156 *
157 * "19) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
158 * GL_COMPRESSED_TEXTURE_FORMATS queries return the RGTC formats?
159 *
160 * RESOLVED: No.
161 *
162 * The OpenGL 2.1 specification says "The only values returned
163 * by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
164 * corresponding to formats suitable for general-purpose usage.
165 * The renderer will not enumerate formats with restrictions that
166 * need to be specifically understood prior to use."
167 *
168 * Compressed textures with just red or red-green components are
169 * not general-purpose so should not be returned by these queries
170 * because they have restrictions.
171 *
172 * Applications that seek to use the RGTC formats should do so
173 * by looking for this extension's name in the string returned by
174 * glGetString(GL_EXTENSIONS) rather than
175 * what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
176 * GL_COMPRESSED_TEXTURE_FORMATS return."
177 *
178 * There is nearly identical wording in the GL_EXT_texture_compression_rgtc
179 * spec.
180 *
181 * The GL_EXT_texture_rRGB spec says:
182 *
183 * "22) Should the new COMPRESSED_SRGB_* formats be listed in an
184 * implementation's GL_COMPRESSED_TEXTURE_FORMATS list?
185 *
186 * RESOLVED: No. Section 3.8.1 says formats listed by
187 * GL_COMPRESSED_TEXTURE_FORMATS are "suitable for general-purpose
188 * usage." The non-linear distribution of red, green, and
189 * blue for these sRGB compressed formats makes them not really
190 * general-purpose."
191 *
192 * The GL_EXT_texture_compression_latc spec says:
193 *
194 * "16) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
195 * GL_COMPRESSED_TEXTURE_FORMATS queries return the LATC formats?
196 *
197 * RESOLVED: No.
198 *
199 * The OpenGL 2.1 specification says "The only values returned
200 * by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
201 * corresponding to formats suitable for general-purpose usage.
202 * The renderer will not enumerate formats with restrictions that
203 * need to be specifically understood prior to use."
204 *
205 * Historically, OpenGL implementation have advertised the RGB and
206 * RGBA versions of the S3TC extensions compressed format tokens
207 * through this mechanism.
208 *
209 * The specification is not sufficiently clear about what "suitable
210 * for general-purpose usage" means. Historically that seems to mean
211 * unsigned RGB or unsigned RGBA. The DXT1 format supporting alpha
212 * (GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) is not exposed in the list (at
213 * least for NVIDIA drivers) because the alpha is always 1.0 expect
214 * when it is 0.0 when RGB is required to be black. NVIDIA's even
215 * limits itself to true linear RGB or RGBA formats, specifically
216 * not including EXT_texture_sRGB's sRGB S3TC compressed formats.
217 *
218 * Adding luminance and luminance-alpha texture formats (and
219 * certainly signed versions of luminance and luminance-alpha
220 * formats!) invites potential comptaibility problems with old
221 * applications using this mechanism since old applications are
222 * unlikely to expect non-RGB or non-RGBA formats to be advertised
223 * through this mechanism. However no specific misinteractions
224 * with old applications is known.
225 *
226 * Applications that seek to use the LATC formats should do so
227 * by looking for this extension's name in the string returned by
228 * glGetString(GL_EXTENSIONS) rather than
229 * what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
230 * GL_COMPRESSED_TEXTURE_FORMATS return."
231 *
232 * The KHR_texture_compression_astc_hdr spec says:
233 *
234 * "Interactions with OpenGL 4.2
235 *
236 * OpenGL 4.2 supports the feature that compressed textures can be
237 * compressed online, by passing the compressed texture format enum as
238 * the internal format when uploading a texture using TexImage1D,
239 * TexImage2D or TexImage3D (see Section 3.9.3, Texture Image
240 * Specification, subsection Encoding of Special Internal Formats).
241 *
242 * Due to the complexity of the ASTC compression algorithm, it is not
243 * usually suitable for online use, and therefore ASTC support will be
244 * limited to pre-compressed textures only. Where on-device compression
245 * is required, a domain-specific limited compressor will typically
246 * be used, and this is therefore not suitable for implementation in
247 * the driver.
248 *
249 * In particular, the ASTC format specifiers will not be added to
250 * Table 3.14, and thus will not be accepted by the TexImage*D
251 * functions, and will not be returned by the (already deprecated)
252 * COMPRESSED_TEXTURE_FORMATS query."
253 *
254 * There is no formal spec for GL_ATI_texture_compression_3dc. Since the
255 * formats added by this extension are luminance-alpha formats, it is
256 * reasonable to expect them to follow the same rules as
257 * GL_EXT_texture_compression_latc. At the very least, Catalyst 11.6 does not
258 * expose the 3dc formats through this mechanism.
259 *
260 * The spec for GL_ARB_texture_compression_bptc doesn't mention whether it
261 * should be included in GL_COMPRESSED_TEXTURE_FORMATS. However as it takes a
262 * very long time to compress the textures in this format it's probably not
263 * very useful as a general format where the GL will have to compress it on
264 * the fly.
265 *
266 * \param ctx the GL context
267 * \param formats the resulting format list (may be NULL).
268 *
269 * \return number of formats.
270 */
271 GLuint
272 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats)
273 {
274 GLuint n = 0;
275 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
276 if (formats) {
277 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
278 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
279 }
280 else {
281 n += 2;
282 }
283 }
284
285 if (ctx->Extensions.EXT_texture_compression_s3tc) {
286 if (formats) {
287 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
288 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
289 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
290 }
291 else {
292 n += 3;
293 }
294
295 /* The ES and desktop GL specs diverge here.
296 *
297 * In desktop OpenGL, the driver can perform online compression of
298 * uncompressed texture data. GL_NUM_COMPRESSED_TEXTURE_FORMATS and
299 * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
300 * formats that it could ask the driver to compress with some
301 * expectation of quality. The GL_ARB_texture_compression spec
302 * calls this "suitable for general-purpose usage." As noted
303 * above, this means GL_COMPRESSED_RGBA_S3TC_DXT1_EXT is not
304 * included in the list.
305 *
306 * In OpenGL ES, the driver never performs compression.
307 * GL_NUM_COMPRESSED_TEXTURE_FORMATS and
308 * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
309 * formats that the driver can receive from the application. It
310 * is the *complete* list of formats. The
311 * GL_EXT_texture_compression_s3tc spec says:
312 *
313 * "New State for OpenGL ES 2.0.25 and 3.0.2 Specifications
314 *
315 * The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
316 * COMPRESSED_TEXTURE_FORMATS include
317 * COMPRESSED_RGB_S3TC_DXT1_EXT,
318 * COMPRESSED_RGBA_S3TC_DXT1_EXT,
319 * COMPRESSED_RGBA_S3TC_DXT3_EXT, and
320 * COMPRESSED_RGBA_S3TC_DXT5_EXT."
321 *
322 * Note that the addition is only to the OpenGL ES specification!
323 */
324 if (_mesa_is_gles(ctx)) {
325 if (formats) {
326 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
327 } else {
328 n += 1;
329 }
330 }
331 }
332
333 /* The GL_OES_compressed_ETC1_RGB8_texture spec says:
334 *
335 * "New State
336 *
337 * The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
338 * COMPRESSED_TEXTURE_FORMATS include ETC1_RGB8_OES."
339 */
340 if (_mesa_is_gles(ctx)
341 && ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
342 if (formats) {
343 formats[n++] = GL_ETC1_RGB8_OES;
344 }
345 else {
346 n += 1;
347 }
348 }
349
350 if (ctx->API == API_OPENGLES) {
351 if (formats) {
352 formats[n++] = GL_PALETTE4_RGB8_OES;
353 formats[n++] = GL_PALETTE4_RGBA8_OES;
354 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
355 formats[n++] = GL_PALETTE4_RGBA4_OES;
356 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
357 formats[n++] = GL_PALETTE8_RGB8_OES;
358 formats[n++] = GL_PALETTE8_RGBA8_OES;
359 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
360 formats[n++] = GL_PALETTE8_RGBA4_OES;
361 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
362 }
363 else {
364 n += 10;
365 }
366 }
367
368 if (_mesa_is_gles3(ctx)) {
369 if (formats) {
370 formats[n++] = GL_COMPRESSED_RGB8_ETC2;
371 formats[n++] = GL_COMPRESSED_SRGB8_ETC2;
372 formats[n++] = GL_COMPRESSED_RGBA8_ETC2_EAC;
373 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
374 formats[n++] = GL_COMPRESSED_R11_EAC;
375 formats[n++] = GL_COMPRESSED_RG11_EAC;
376 formats[n++] = GL_COMPRESSED_SIGNED_R11_EAC;
377 formats[n++] = GL_COMPRESSED_SIGNED_RG11_EAC;
378 formats[n++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
379 formats[n++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
380 }
381 else {
382 n += 10;
383 }
384 }
385 return n;
386 }
387
388
389 /**
390 * Convert a compressed MESA_FORMAT_x to a GLenum.
391 */
392 mesa_format
393 _mesa_glenum_to_compressed_format(GLenum format)
394 {
395 switch (format) {
396 case GL_COMPRESSED_RGB_FXT1_3DFX:
397 return MESA_FORMAT_RGB_FXT1;
398 case GL_COMPRESSED_RGBA_FXT1_3DFX:
399 return MESA_FORMAT_RGBA_FXT1;
400
401 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
402 case GL_RGB_S3TC:
403 case GL_RGB4_S3TC:
404 return MESA_FORMAT_RGB_DXT1;
405 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
406 return MESA_FORMAT_RGBA_DXT1;
407 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
408 case GL_RGBA_S3TC:
409 case GL_RGBA4_S3TC:
410 return MESA_FORMAT_RGBA_DXT3;
411 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
412 return MESA_FORMAT_RGBA_DXT5;
413
414 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
415 return MESA_FORMAT_SRGB_DXT1;
416 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
417 return MESA_FORMAT_SRGBA_DXT1;
418 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
419 return MESA_FORMAT_SRGBA_DXT3;
420 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
421 return MESA_FORMAT_SRGBA_DXT5;
422
423 case GL_COMPRESSED_RED_RGTC1:
424 return MESA_FORMAT_R_RGTC1_UNORM;
425 case GL_COMPRESSED_SIGNED_RED_RGTC1:
426 return MESA_FORMAT_R_RGTC1_SNORM;
427 case GL_COMPRESSED_RG_RGTC2:
428 return MESA_FORMAT_RG_RGTC2_UNORM;
429 case GL_COMPRESSED_SIGNED_RG_RGTC2:
430 return MESA_FORMAT_RG_RGTC2_SNORM;
431
432 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
433 return MESA_FORMAT_L_LATC1_UNORM;
434 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
435 return MESA_FORMAT_L_LATC1_SNORM;
436 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
437 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
438 return MESA_FORMAT_LA_LATC2_UNORM;
439 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
440 return MESA_FORMAT_LA_LATC2_SNORM;
441
442 case GL_ETC1_RGB8_OES:
443 return MESA_FORMAT_ETC1_RGB8;
444 case GL_COMPRESSED_RGB8_ETC2:
445 return MESA_FORMAT_ETC2_RGB8;
446 case GL_COMPRESSED_SRGB8_ETC2:
447 return MESA_FORMAT_ETC2_SRGB8;
448 case GL_COMPRESSED_RGBA8_ETC2_EAC:
449 return MESA_FORMAT_ETC2_RGBA8_EAC;
450 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
451 return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
452 case GL_COMPRESSED_R11_EAC:
453 return MESA_FORMAT_ETC2_R11_EAC;
454 case GL_COMPRESSED_RG11_EAC:
455 return MESA_FORMAT_ETC2_RG11_EAC;
456 case GL_COMPRESSED_SIGNED_R11_EAC:
457 return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
458 case GL_COMPRESSED_SIGNED_RG11_EAC:
459 return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
460 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
461 return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
462 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
463 return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
464
465 case GL_COMPRESSED_RGBA_BPTC_UNORM:
466 return MESA_FORMAT_BPTC_RGBA_UNORM;
467 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
468 return MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM;
469 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
470 return MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT;
471 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
472 return MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT;
473
474 case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
475 return MESA_FORMAT_RGBA_ASTC_4x4;
476 case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
477 return MESA_FORMAT_RGBA_ASTC_5x4;
478 case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
479 return MESA_FORMAT_RGBA_ASTC_5x5;
480 case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
481 return MESA_FORMAT_RGBA_ASTC_6x5;
482 case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
483 return MESA_FORMAT_RGBA_ASTC_6x6;
484 case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
485 return MESA_FORMAT_RGBA_ASTC_8x5;
486 case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
487 return MESA_FORMAT_RGBA_ASTC_8x6;
488 case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
489 return MESA_FORMAT_RGBA_ASTC_8x8;
490 case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
491 return MESA_FORMAT_RGBA_ASTC_10x5;
492 case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
493 return MESA_FORMAT_RGBA_ASTC_10x6;
494 case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
495 return MESA_FORMAT_RGBA_ASTC_10x8;
496 case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
497 return MESA_FORMAT_RGBA_ASTC_10x10;
498 case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
499 return MESA_FORMAT_RGBA_ASTC_12x10;
500 case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
501 return MESA_FORMAT_RGBA_ASTC_12x12;
502 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
503 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4;
504 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
505 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4;
506 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
507 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5;
508 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
509 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5;
510 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
511 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6;
512 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
513 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5;
514 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
515 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6;
516 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
517 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8;
518 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
519 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5;
520 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
521 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6;
522 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
523 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8;
524 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
525 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10;
526 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
527 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10;
528 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
529 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12;
530
531 default:
532 return MESA_FORMAT_NONE;
533 }
534 }
535
536
537 /**
538 * Given a compressed MESA_FORMAT_x value, return the corresponding
539 * GLenum for that format.
540 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
541 * which must return the specific texture format used when the user might
542 * have originally specified a generic compressed format in their
543 * glTexImage2D() call.
544 * For non-compressed textures, we always return the user-specified
545 * internal format unchanged.
546 */
547 GLenum
548 _mesa_compressed_format_to_glenum(struct gl_context *ctx, mesa_format mesaFormat)
549 {
550 switch (mesaFormat) {
551 case MESA_FORMAT_RGB_FXT1:
552 return GL_COMPRESSED_RGB_FXT1_3DFX;
553 case MESA_FORMAT_RGBA_FXT1:
554 return GL_COMPRESSED_RGBA_FXT1_3DFX;
555 case MESA_FORMAT_RGB_DXT1:
556 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
557 case MESA_FORMAT_RGBA_DXT1:
558 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
559 case MESA_FORMAT_RGBA_DXT3:
560 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
561 case MESA_FORMAT_RGBA_DXT5:
562 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
563 case MESA_FORMAT_SRGB_DXT1:
564 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
565 case MESA_FORMAT_SRGBA_DXT1:
566 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
567 case MESA_FORMAT_SRGBA_DXT3:
568 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
569 case MESA_FORMAT_SRGBA_DXT5:
570 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
571 case MESA_FORMAT_R_RGTC1_UNORM:
572 return GL_COMPRESSED_RED_RGTC1;
573 case MESA_FORMAT_R_RGTC1_SNORM:
574 return GL_COMPRESSED_SIGNED_RED_RGTC1;
575 case MESA_FORMAT_RG_RGTC2_UNORM:
576 return GL_COMPRESSED_RG_RGTC2;
577 case MESA_FORMAT_RG_RGTC2_SNORM:
578 return GL_COMPRESSED_SIGNED_RG_RGTC2;
579
580 case MESA_FORMAT_L_LATC1_UNORM:
581 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
582 case MESA_FORMAT_L_LATC1_SNORM:
583 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
584 case MESA_FORMAT_LA_LATC2_UNORM:
585 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
586 case MESA_FORMAT_LA_LATC2_SNORM:
587 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
588
589 case MESA_FORMAT_ETC1_RGB8:
590 return GL_ETC1_RGB8_OES;
591 case MESA_FORMAT_ETC2_RGB8:
592 return GL_COMPRESSED_RGB8_ETC2;
593 case MESA_FORMAT_ETC2_SRGB8:
594 return GL_COMPRESSED_SRGB8_ETC2;
595 case MESA_FORMAT_ETC2_RGBA8_EAC:
596 return GL_COMPRESSED_RGBA8_ETC2_EAC;
597 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
598 return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
599 case MESA_FORMAT_ETC2_R11_EAC:
600 return GL_COMPRESSED_R11_EAC;
601 case MESA_FORMAT_ETC2_RG11_EAC:
602 return GL_COMPRESSED_RG11_EAC;
603 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
604 return GL_COMPRESSED_SIGNED_R11_EAC;
605 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
606 return GL_COMPRESSED_SIGNED_RG11_EAC;
607 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
608 return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
609 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
610 return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
611
612 case MESA_FORMAT_BPTC_RGBA_UNORM:
613 return GL_COMPRESSED_RGBA_BPTC_UNORM;
614 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
615 return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
616 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
617 return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
618 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
619 return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
620
621 case MESA_FORMAT_RGBA_ASTC_4x4:
622 return GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
623 case MESA_FORMAT_RGBA_ASTC_5x4:
624 return GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
625 case MESA_FORMAT_RGBA_ASTC_5x5:
626 return GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
627 case MESA_FORMAT_RGBA_ASTC_6x5:
628 return GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
629 case MESA_FORMAT_RGBA_ASTC_6x6:
630 return GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
631 case MESA_FORMAT_RGBA_ASTC_8x5:
632 return GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
633 case MESA_FORMAT_RGBA_ASTC_8x6:
634 return GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
635 case MESA_FORMAT_RGBA_ASTC_8x8:
636 return GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
637 case MESA_FORMAT_RGBA_ASTC_10x5:
638 return GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
639 case MESA_FORMAT_RGBA_ASTC_10x6:
640 return GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
641 case MESA_FORMAT_RGBA_ASTC_10x8:
642 return GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
643 case MESA_FORMAT_RGBA_ASTC_10x10:
644 return GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
645 case MESA_FORMAT_RGBA_ASTC_12x10:
646 return GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
647 case MESA_FORMAT_RGBA_ASTC_12x12:
648 return GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
649 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
650 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
651 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
652 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
653 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
654 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
655 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
656 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
657 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
658 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
659 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
660 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
661 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
662 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
663 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
664 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
665 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
666 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
667 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
668 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
669 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
670 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
671 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
672 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
673 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
674 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
675 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
676 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
677
678 default:
679 _mesa_problem(ctx, "Unexpected mesa texture format in"
680 " _mesa_compressed_format_to_glenum()");
681 return 0;
682 }
683 }
684
685
686 /*
687 * Return the address of the pixel at (col, row, img) in a
688 * compressed texture image.
689 * \param col, row, img - image position (3D), should be a multiple of the
690 * format's block size.
691 * \param format - compressed image format
692 * \param width - image width (stride) in pixels
693 * \param image - the image address
694 * \return address of pixel at (row, col, img)
695 */
696 GLubyte *
697 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
698 mesa_format mesaFormat,
699 GLsizei width, const GLubyte *image)
700 {
701 /* XXX only 2D images implemented, not 3D */
702 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
703 GLuint bw, bh;
704 GLint offset;
705
706 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
707
708 assert(col % bw == 0);
709 assert(row % bh == 0);
710
711 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
712 offset *= blockSize;
713
714 return (GLubyte *) image + offset;
715 }
716
717
718 /**
719 * Return a texel-fetch function for the given format, or NULL if
720 * invalid format.
721 */
722 compressed_fetch_func
723 _mesa_get_compressed_fetch_func(mesa_format format)
724 {
725 switch (_mesa_get_format_layout(format)) {
726 case MESA_FORMAT_LAYOUT_S3TC:
727 return _mesa_get_dxt_fetch_func(format);
728 case MESA_FORMAT_LAYOUT_FXT1:
729 return _mesa_get_fxt_fetch_func(format);
730 case MESA_FORMAT_LAYOUT_RGTC:
731 return _mesa_get_compressed_rgtc_func(format);
732 case MESA_FORMAT_LAYOUT_ETC1:
733 return _mesa_get_etc_fetch_func(format);
734 case MESA_FORMAT_LAYOUT_BPTC:
735 return _mesa_get_bptc_fetch_func(format);
736 default:
737 return NULL;
738 }
739 }
740
741
742 /**
743 * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
744 * \param srcRowStride stride in bytes between rows of blocks in the
745 * compressed source image.
746 */
747 void
748 _mesa_decompress_image(mesa_format format, GLuint width, GLuint height,
749 const GLubyte *src, GLint srcRowStride,
750 GLfloat *dest)
751 {
752 compressed_fetch_func fetch;
753 GLuint i, j;
754 GLuint bytes, bw, bh;
755 GLint stride;
756
757 bytes = _mesa_get_format_bytes(format);
758 _mesa_get_format_block_size(format, &bw, &bh);
759
760 fetch = _mesa_get_compressed_fetch_func(format);
761 if (!fetch) {
762 _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
763 return;
764 }
765
766 stride = srcRowStride * bh / bytes;
767
768 for (j = 0; j < height; j++) {
769 for (i = 0; i < width; i++) {
770 fetch(src, stride, i, j, dest);
771 dest += 4;
772 }
773 }
774 }