mesa/formatquery: Add support for READ_PIXELS query
[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 * 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
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)) {
362 if (formats) {
363 formats[n++] = GL_COMPRESSED_RGB8_ETC2;
364 formats[n++] = GL_COMPRESSED_SRGB8_ETC2;
365 formats[n++] = GL_COMPRESSED_RGBA8_ETC2_EAC;
366 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
367 formats[n++] = GL_COMPRESSED_R11_EAC;
368 formats[n++] = GL_COMPRESSED_RG11_EAC;
369 formats[n++] = GL_COMPRESSED_SIGNED_R11_EAC;
370 formats[n++] = GL_COMPRESSED_SIGNED_RG11_EAC;
371 formats[n++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
372 formats[n++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
373 }
374 else {
375 n += 10;
376 }
377 }
378
379 /* The KHR_texture_compression_astc_hdr spec says:
380 *
381 * "Interactions with OpenGL 4.2
382 *
383 * OpenGL 4.2 supports the feature that compressed textures can be
384 * compressed online, by passing the compressed texture format enum as
385 * the internal format when uploading a texture using TexImage1D,
386 * TexImage2D or TexImage3D (see Section 3.9.3, Texture Image
387 * Specification, subsection Encoding of Special Internal Formats).
388 *
389 * Due to the complexity of the ASTC compression algorithm, it is not
390 * usually suitable for online use, and therefore ASTC support will be
391 * limited to pre-compressed textures only. Where on-device compression
392 * is required, a domain-specific limited compressor will typically
393 * be used, and this is therefore not suitable for implementation in
394 * the driver.
395 *
396 * In particular, the ASTC format specifiers will not be added to
397 * Table 3.14, and thus will not be accepted by the TexImage*D
398 * functions, and will not be returned by the (already deprecated)
399 * COMPRESSED_TEXTURE_FORMATS query."
400 *
401 * The ES and the desktop specs diverge here. In OpenGL ES, the COMPRESSED_TEXTURE_FORMATS
402 * query returns the set of supported specific compressed formats.
403 */
404 if (ctx->API == API_OPENGLES2 &&
405 ctx->Extensions.KHR_texture_compression_astc_ldr) {
406 if (formats) {
407 formats[n++] = GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
408 formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
409 formats[n++] = GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
410 formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
411 formats[n++] = GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
412 formats[n++] = GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
413 formats[n++] = GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
414 formats[n++] = GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
415 formats[n++] = GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
416 formats[n++] = GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
417 formats[n++] = GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
418 formats[n++] = GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
419 formats[n++] = GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
420 formats[n++] = GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
421 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
422 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
423 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
424 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
425 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
426 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
427 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
428 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
429 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
430 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
431 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
432 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
433 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
434 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
435 }
436 else {
437 n += 28;
438 }
439 }
440
441 return n;
442 }
443
444
445 /**
446 * Convert GLenum to a compressed MESA_FORMAT_x.
447 */
448 mesa_format
449 _mesa_glenum_to_compressed_format(GLenum format)
450 {
451 switch (format) {
452 case GL_COMPRESSED_RGB_FXT1_3DFX:
453 return MESA_FORMAT_RGB_FXT1;
454 case GL_COMPRESSED_RGBA_FXT1_3DFX:
455 return MESA_FORMAT_RGBA_FXT1;
456
457 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
458 case GL_RGB_S3TC:
459 case GL_RGB4_S3TC:
460 return MESA_FORMAT_RGB_DXT1;
461 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
462 return MESA_FORMAT_RGBA_DXT1;
463 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
464 case GL_RGBA_S3TC:
465 case GL_RGBA4_S3TC:
466 return MESA_FORMAT_RGBA_DXT3;
467 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
468 return MESA_FORMAT_RGBA_DXT5;
469
470 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
471 return MESA_FORMAT_SRGB_DXT1;
472 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
473 return MESA_FORMAT_SRGBA_DXT1;
474 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
475 return MESA_FORMAT_SRGBA_DXT3;
476 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
477 return MESA_FORMAT_SRGBA_DXT5;
478
479 case GL_COMPRESSED_RED_RGTC1:
480 return MESA_FORMAT_R_RGTC1_UNORM;
481 case GL_COMPRESSED_SIGNED_RED_RGTC1:
482 return MESA_FORMAT_R_RGTC1_SNORM;
483 case GL_COMPRESSED_RG_RGTC2:
484 return MESA_FORMAT_RG_RGTC2_UNORM;
485 case GL_COMPRESSED_SIGNED_RG_RGTC2:
486 return MESA_FORMAT_RG_RGTC2_SNORM;
487
488 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
489 return MESA_FORMAT_L_LATC1_UNORM;
490 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
491 return MESA_FORMAT_L_LATC1_SNORM;
492 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
493 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
494 return MESA_FORMAT_LA_LATC2_UNORM;
495 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
496 return MESA_FORMAT_LA_LATC2_SNORM;
497
498 case GL_ETC1_RGB8_OES:
499 return MESA_FORMAT_ETC1_RGB8;
500 case GL_COMPRESSED_RGB8_ETC2:
501 return MESA_FORMAT_ETC2_RGB8;
502 case GL_COMPRESSED_SRGB8_ETC2:
503 return MESA_FORMAT_ETC2_SRGB8;
504 case GL_COMPRESSED_RGBA8_ETC2_EAC:
505 return MESA_FORMAT_ETC2_RGBA8_EAC;
506 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
507 return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
508 case GL_COMPRESSED_R11_EAC:
509 return MESA_FORMAT_ETC2_R11_EAC;
510 case GL_COMPRESSED_RG11_EAC:
511 return MESA_FORMAT_ETC2_RG11_EAC;
512 case GL_COMPRESSED_SIGNED_R11_EAC:
513 return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
514 case GL_COMPRESSED_SIGNED_RG11_EAC:
515 return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
516 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
517 return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
518 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
519 return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
520
521 case GL_COMPRESSED_RGBA_BPTC_UNORM:
522 return MESA_FORMAT_BPTC_RGBA_UNORM;
523 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
524 return MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM;
525 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
526 return MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT;
527 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
528 return MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT;
529
530 case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
531 return MESA_FORMAT_RGBA_ASTC_4x4;
532 case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
533 return MESA_FORMAT_RGBA_ASTC_5x4;
534 case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
535 return MESA_FORMAT_RGBA_ASTC_5x5;
536 case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
537 return MESA_FORMAT_RGBA_ASTC_6x5;
538 case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
539 return MESA_FORMAT_RGBA_ASTC_6x6;
540 case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
541 return MESA_FORMAT_RGBA_ASTC_8x5;
542 case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
543 return MESA_FORMAT_RGBA_ASTC_8x6;
544 case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
545 return MESA_FORMAT_RGBA_ASTC_8x8;
546 case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
547 return MESA_FORMAT_RGBA_ASTC_10x5;
548 case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
549 return MESA_FORMAT_RGBA_ASTC_10x6;
550 case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
551 return MESA_FORMAT_RGBA_ASTC_10x8;
552 case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
553 return MESA_FORMAT_RGBA_ASTC_10x10;
554 case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
555 return MESA_FORMAT_RGBA_ASTC_12x10;
556 case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
557 return MESA_FORMAT_RGBA_ASTC_12x12;
558 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
559 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4;
560 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
561 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4;
562 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
563 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5;
564 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
565 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5;
566 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
567 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6;
568 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
569 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5;
570 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
571 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6;
572 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
573 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8;
574 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
575 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5;
576 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
577 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6;
578 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
579 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8;
580 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
581 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10;
582 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
583 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10;
584 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
585 return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12;
586
587 default:
588 return MESA_FORMAT_NONE;
589 }
590 }
591
592
593 /**
594 * Given a compressed MESA_FORMAT_x value, return the corresponding
595 * GLenum for that format.
596 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
597 * which must return the specific texture format used when the user might
598 * have originally specified a generic compressed format in their
599 * glTexImage2D() call.
600 * For non-compressed textures, we always return the user-specified
601 * internal format unchanged.
602 */
603 GLenum
604 _mesa_compressed_format_to_glenum(struct gl_context *ctx, mesa_format mesaFormat)
605 {
606 switch (mesaFormat) {
607 case MESA_FORMAT_RGB_FXT1:
608 return GL_COMPRESSED_RGB_FXT1_3DFX;
609 case MESA_FORMAT_RGBA_FXT1:
610 return GL_COMPRESSED_RGBA_FXT1_3DFX;
611 case MESA_FORMAT_RGB_DXT1:
612 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
613 case MESA_FORMAT_RGBA_DXT1:
614 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
615 case MESA_FORMAT_RGBA_DXT3:
616 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
617 case MESA_FORMAT_RGBA_DXT5:
618 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
619 case MESA_FORMAT_SRGB_DXT1:
620 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
621 case MESA_FORMAT_SRGBA_DXT1:
622 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
623 case MESA_FORMAT_SRGBA_DXT3:
624 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
625 case MESA_FORMAT_SRGBA_DXT5:
626 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
627 case MESA_FORMAT_R_RGTC1_UNORM:
628 return GL_COMPRESSED_RED_RGTC1;
629 case MESA_FORMAT_R_RGTC1_SNORM:
630 return GL_COMPRESSED_SIGNED_RED_RGTC1;
631 case MESA_FORMAT_RG_RGTC2_UNORM:
632 return GL_COMPRESSED_RG_RGTC2;
633 case MESA_FORMAT_RG_RGTC2_SNORM:
634 return GL_COMPRESSED_SIGNED_RG_RGTC2;
635
636 case MESA_FORMAT_L_LATC1_UNORM:
637 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
638 case MESA_FORMAT_L_LATC1_SNORM:
639 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
640 case MESA_FORMAT_LA_LATC2_UNORM:
641 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
642 case MESA_FORMAT_LA_LATC2_SNORM:
643 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
644
645 case MESA_FORMAT_ETC1_RGB8:
646 return GL_ETC1_RGB8_OES;
647 case MESA_FORMAT_ETC2_RGB8:
648 return GL_COMPRESSED_RGB8_ETC2;
649 case MESA_FORMAT_ETC2_SRGB8:
650 return GL_COMPRESSED_SRGB8_ETC2;
651 case MESA_FORMAT_ETC2_RGBA8_EAC:
652 return GL_COMPRESSED_RGBA8_ETC2_EAC;
653 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
654 return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
655 case MESA_FORMAT_ETC2_R11_EAC:
656 return GL_COMPRESSED_R11_EAC;
657 case MESA_FORMAT_ETC2_RG11_EAC:
658 return GL_COMPRESSED_RG11_EAC;
659 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
660 return GL_COMPRESSED_SIGNED_R11_EAC;
661 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
662 return GL_COMPRESSED_SIGNED_RG11_EAC;
663 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
664 return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
665 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
666 return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
667
668 case MESA_FORMAT_BPTC_RGBA_UNORM:
669 return GL_COMPRESSED_RGBA_BPTC_UNORM;
670 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
671 return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
672 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
673 return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
674 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
675 return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
676
677 case MESA_FORMAT_RGBA_ASTC_4x4:
678 return GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
679 case MESA_FORMAT_RGBA_ASTC_5x4:
680 return GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
681 case MESA_FORMAT_RGBA_ASTC_5x5:
682 return GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
683 case MESA_FORMAT_RGBA_ASTC_6x5:
684 return GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
685 case MESA_FORMAT_RGBA_ASTC_6x6:
686 return GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
687 case MESA_FORMAT_RGBA_ASTC_8x5:
688 return GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
689 case MESA_FORMAT_RGBA_ASTC_8x6:
690 return GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
691 case MESA_FORMAT_RGBA_ASTC_8x8:
692 return GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
693 case MESA_FORMAT_RGBA_ASTC_10x5:
694 return GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
695 case MESA_FORMAT_RGBA_ASTC_10x6:
696 return GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
697 case MESA_FORMAT_RGBA_ASTC_10x8:
698 return GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
699 case MESA_FORMAT_RGBA_ASTC_10x10:
700 return GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
701 case MESA_FORMAT_RGBA_ASTC_12x10:
702 return GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
703 case MESA_FORMAT_RGBA_ASTC_12x12:
704 return GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
705 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
706 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
707 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
708 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
709 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
710 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
711 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
712 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
713 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
714 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
715 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
716 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
717 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
718 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
719 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
720 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
721 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
722 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
723 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
724 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
725 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
726 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
727 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
728 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
729 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
730 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
731 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
732 return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
733
734 default:
735 _mesa_problem(ctx, "Unexpected mesa texture format in"
736 " _mesa_compressed_format_to_glenum()");
737 return 0;
738 }
739 }
740
741
742 /*
743 * Return the address of the pixel at (col, row, img) in a
744 * compressed texture image.
745 * \param col, row, img - image position (3D), should be a multiple of the
746 * format's block size.
747 * \param format - compressed image format
748 * \param width - image width (stride) in pixels
749 * \param image - the image address
750 * \return address of pixel at (row, col, img)
751 */
752 GLubyte *
753 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
754 mesa_format mesaFormat,
755 GLsizei width, const GLubyte *image)
756 {
757 /* XXX only 2D images implemented, not 3D */
758 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
759 GLuint bw, bh;
760 GLint offset;
761
762 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
763
764 assert(col % bw == 0);
765 assert(row % bh == 0);
766
767 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
768 offset *= blockSize;
769
770 return (GLubyte *) image + offset;
771 }
772
773
774 /**
775 * Return a texel-fetch function for the given format, or NULL if
776 * invalid format.
777 */
778 compressed_fetch_func
779 _mesa_get_compressed_fetch_func(mesa_format format)
780 {
781 switch (_mesa_get_format_layout(format)) {
782 case MESA_FORMAT_LAYOUT_S3TC:
783 return _mesa_get_dxt_fetch_func(format);
784 case MESA_FORMAT_LAYOUT_FXT1:
785 return _mesa_get_fxt_fetch_func(format);
786 case MESA_FORMAT_LAYOUT_RGTC:
787 case MESA_FORMAT_LAYOUT_LATC:
788 return _mesa_get_compressed_rgtc_func(format);
789 case MESA_FORMAT_LAYOUT_ETC1:
790 return _mesa_get_etc_fetch_func(format);
791 case MESA_FORMAT_LAYOUT_BPTC:
792 return _mesa_get_bptc_fetch_func(format);
793 default:
794 return NULL;
795 }
796 }
797
798
799 /**
800 * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
801 * \param srcRowStride stride in bytes between rows of blocks in the
802 * compressed source image.
803 */
804 void
805 _mesa_decompress_image(mesa_format format, GLuint width, GLuint height,
806 const GLubyte *src, GLint srcRowStride,
807 GLfloat *dest)
808 {
809 compressed_fetch_func fetch;
810 GLuint i, j;
811 GLuint bytes, bw, bh;
812 GLint stride;
813
814 bytes = _mesa_get_format_bytes(format);
815 _mesa_get_format_block_size(format, &bw, &bh);
816
817 fetch = _mesa_get_compressed_fetch_func(format);
818 if (!fetch) {
819 _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
820 return;
821 }
822
823 stride = srcRowStride * bh / bytes;
824
825 for (j = 0; j < height; j++) {
826 for (i = 0; i < width; i++) {
827 fetch(src, stride, i, j, dest);
828 dest += 4;
829 }
830 }
831 }