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 * There is no formal spec for GL_ATI_texture_compression_3dc. Since the
233 * formats added by this extension are luminance-alpha formats, it is
234 * reasonable to expect them to follow the same rules as
235 * GL_EXT_texture_compression_latc. At the very least, Catalyst 11.6 does not
236 * expose the 3dc formats through this mechanism.
237 *
238 * The spec for GL_ARB_texture_compression_bptc doesn't mention whether it
239 * should be included in GL_COMPRESSED_TEXTURE_FORMATS. However as it takes a
240 * very long time to compress the textures in this format it's probably not
241 * very useful as a general format where the GL will have to compress it on
242 * the fly.
243 *
244 * \param ctx the GL context
245 * \param formats the resulting format list (may be NULL).
246 *
247 * \return number of formats.
248 */
249 GLuint
250 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats)
251 {
252 GLuint n = 0;
253 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
254 if (formats) {
255 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
256 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
257 }
258 else {
259 n += 2;
260 }
261 }
262
263 if (ctx->Extensions.EXT_texture_compression_s3tc) {
264 if (formats) {
265 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
266 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
267 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
268 }
269 else {
270 n += 3;
271 }
272
273 /* The ES and desktop GL specs diverge here.
274 *
275 * In desktop OpenGL, the driver can perform online compression of
276 * uncompressed texture data. GL_NUM_COMPRESSED_TEXTURE_FORMATS and
277 * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
278 * formats that it could ask the driver to compress with some
279 * expectation of quality. The GL_ARB_texture_compression spec
280 * calls this "suitable for general-purpose usage." As noted
281 * above, this means GL_COMPRESSED_RGBA_S3TC_DXT1_EXT is not
282 * included in the list.
283 *
284 * In OpenGL ES, the driver never performs compression.
285 * GL_NUM_COMPRESSED_TEXTURE_FORMATS and
286 * GL_COMPRESSED_TEXTURE_FORMATS give the application a list of
287 * formats that the driver can receive from the application. It
288 * is the *complete* list of formats. The
289 * GL_EXT_texture_compression_s3tc spec says:
290 *
291 * "New State for OpenGL ES 2.0.25 and 3.0.2 Specifications
292 *
293 * The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
294 * COMPRESSED_TEXTURE_FORMATS include
295 * COMPRESSED_RGB_S3TC_DXT1_EXT,
296 * COMPRESSED_RGBA_S3TC_DXT1_EXT,
297 * COMPRESSED_RGBA_S3TC_DXT3_EXT, and
298 * COMPRESSED_RGBA_S3TC_DXT5_EXT."
299 *
300 * Note that the addition is only to the OpenGL ES specification!
301 */
302 if (_mesa_is_gles(ctx)) {
303 if (formats) {
304 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
305 } else {
306 n += 1;
307 }
308 }
309 }
310
311 /* The GL_OES_compressed_ETC1_RGB8_texture spec says:
312 *
313 * "New State
314 *
315 * The queries for NUM_COMPRESSED_TEXTURE_FORMATS and
316 * COMPRESSED_TEXTURE_FORMATS include ETC1_RGB8_OES."
317 */
318 if (_mesa_is_gles(ctx)
319 && ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
320 if (formats) {
321 formats[n++] = GL_ETC1_RGB8_OES;
322 }
323 else {
324 n += 1;
325 }
326 }
327
328 if (ctx->API == API_OPENGLES) {
329 if (formats) {
330 formats[n++] = GL_PALETTE4_RGB8_OES;
331 formats[n++] = GL_PALETTE4_RGBA8_OES;
332 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
333 formats[n++] = GL_PALETTE4_RGBA4_OES;
334 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
335 formats[n++] = GL_PALETTE8_RGB8_OES;
336 formats[n++] = GL_PALETTE8_RGBA8_OES;
337 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
338 formats[n++] = GL_PALETTE8_RGBA4_OES;
339 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
340 }
341 else {
342 n += 10;
343 }
344 }
345
346 if (_mesa_is_gles3(ctx)) {
347 if (formats) {
348 formats[n++] = GL_COMPRESSED_RGB8_ETC2;
349 formats[n++] = GL_COMPRESSED_SRGB8_ETC2;
350 formats[n++] = GL_COMPRESSED_RGBA8_ETC2_EAC;
351 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
352 formats[n++] = GL_COMPRESSED_R11_EAC;
353 formats[n++] = GL_COMPRESSED_RG11_EAC;
354 formats[n++] = GL_COMPRESSED_SIGNED_R11_EAC;
355 formats[n++] = GL_COMPRESSED_SIGNED_RG11_EAC;
356 formats[n++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
357 formats[n++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
358 }
359 else {
360 n += 10;
361 }
362 }
363 return n;
364 }
365
366
367 /**
368 * Convert a compressed MESA_FORMAT_x to a GLenum.
369 */
370 mesa_format
371 _mesa_glenum_to_compressed_format(GLenum format)
372 {
373 switch (format) {
374 case GL_COMPRESSED_RGB_FXT1_3DFX:
375 return MESA_FORMAT_RGB_FXT1;
376 case GL_COMPRESSED_RGBA_FXT1_3DFX:
377 return MESA_FORMAT_RGBA_FXT1;
378
379 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
380 case GL_RGB_S3TC:
381 return MESA_FORMAT_RGB_DXT1;
382 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
383 case GL_RGB4_S3TC:
384 return MESA_FORMAT_RGBA_DXT1;
385 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
386 case GL_RGBA_S3TC:
387 return MESA_FORMAT_RGBA_DXT3;
388 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
389 case GL_RGBA4_S3TC:
390 return MESA_FORMAT_RGBA_DXT5;
391
392 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
393 return MESA_FORMAT_SRGB_DXT1;
394 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
395 return MESA_FORMAT_SRGBA_DXT1;
396 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
397 return MESA_FORMAT_SRGBA_DXT3;
398 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
399 return MESA_FORMAT_SRGBA_DXT5;
400
401 case GL_COMPRESSED_RED_RGTC1:
402 return MESA_FORMAT_R_RGTC1_UNORM;
403 case GL_COMPRESSED_SIGNED_RED_RGTC1:
404 return MESA_FORMAT_R_RGTC1_SNORM;
405 case GL_COMPRESSED_RG_RGTC2:
406 return MESA_FORMAT_RG_RGTC2_UNORM;
407 case GL_COMPRESSED_SIGNED_RG_RGTC2:
408 return MESA_FORMAT_RG_RGTC2_SNORM;
409
410 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
411 return MESA_FORMAT_L_LATC1_UNORM;
412 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
413 return MESA_FORMAT_L_LATC1_SNORM;
414 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
415 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
416 return MESA_FORMAT_LA_LATC2_UNORM;
417 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
418 return MESA_FORMAT_LA_LATC2_SNORM;
419
420 case GL_ETC1_RGB8_OES:
421 return MESA_FORMAT_ETC1_RGB8;
422 case GL_COMPRESSED_RGB8_ETC2:
423 return MESA_FORMAT_ETC2_RGB8;
424 case GL_COMPRESSED_SRGB8_ETC2:
425 return MESA_FORMAT_ETC2_SRGB8;
426 case GL_COMPRESSED_RGBA8_ETC2_EAC:
427 return MESA_FORMAT_ETC2_RGBA8_EAC;
428 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
429 return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
430 case GL_COMPRESSED_R11_EAC:
431 return MESA_FORMAT_ETC2_R11_EAC;
432 case GL_COMPRESSED_RG11_EAC:
433 return MESA_FORMAT_ETC2_RG11_EAC;
434 case GL_COMPRESSED_SIGNED_R11_EAC:
435 return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
436 case GL_COMPRESSED_SIGNED_RG11_EAC:
437 return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
438 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
439 return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
440 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
441 return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
442
443 case GL_COMPRESSED_RGBA_BPTC_UNORM:
444 return MESA_FORMAT_BPTC_RGBA_UNORM;
445 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
446 return MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM;
447 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
448 return MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT;
449 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
450 return MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT;
451
452 default:
453 return MESA_FORMAT_NONE;
454 }
455 }
456
457
458 /**
459 * Given a compressed MESA_FORMAT_x value, return the corresponding
460 * GLenum for that format.
461 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
462 * which must return the specific texture format used when the user might
463 * have originally specified a generic compressed format in their
464 * glTexImage2D() call.
465 * For non-compressed textures, we always return the user-specified
466 * internal format unchanged.
467 */
468 GLenum
469 _mesa_compressed_format_to_glenum(struct gl_context *ctx, mesa_format mesaFormat)
470 {
471 switch (mesaFormat) {
472 case MESA_FORMAT_RGB_FXT1:
473 return GL_COMPRESSED_RGB_FXT1_3DFX;
474 case MESA_FORMAT_RGBA_FXT1:
475 return GL_COMPRESSED_RGBA_FXT1_3DFX;
476 case MESA_FORMAT_RGB_DXT1:
477 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
478 case MESA_FORMAT_RGBA_DXT1:
479 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
480 case MESA_FORMAT_RGBA_DXT3:
481 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
482 case MESA_FORMAT_RGBA_DXT5:
483 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
484 case MESA_FORMAT_SRGB_DXT1:
485 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
486 case MESA_FORMAT_SRGBA_DXT1:
487 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
488 case MESA_FORMAT_SRGBA_DXT3:
489 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
490 case MESA_FORMAT_SRGBA_DXT5:
491 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
492 case MESA_FORMAT_R_RGTC1_UNORM:
493 return GL_COMPRESSED_RED_RGTC1;
494 case MESA_FORMAT_R_RGTC1_SNORM:
495 return GL_COMPRESSED_SIGNED_RED_RGTC1;
496 case MESA_FORMAT_RG_RGTC2_UNORM:
497 return GL_COMPRESSED_RG_RGTC2;
498 case MESA_FORMAT_RG_RGTC2_SNORM:
499 return GL_COMPRESSED_SIGNED_RG_RGTC2;
500
501 case MESA_FORMAT_L_LATC1_UNORM:
502 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
503 case MESA_FORMAT_L_LATC1_SNORM:
504 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
505 case MESA_FORMAT_LA_LATC2_UNORM:
506 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
507 case MESA_FORMAT_LA_LATC2_SNORM:
508 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
509
510 case MESA_FORMAT_ETC1_RGB8:
511 return GL_ETC1_RGB8_OES;
512 case MESA_FORMAT_ETC2_RGB8:
513 return GL_COMPRESSED_RGB8_ETC2;
514 case MESA_FORMAT_ETC2_SRGB8:
515 return GL_COMPRESSED_SRGB8_ETC2;
516 case MESA_FORMAT_ETC2_RGBA8_EAC:
517 return GL_COMPRESSED_RGBA8_ETC2_EAC;
518 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
519 return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
520 case MESA_FORMAT_ETC2_R11_EAC:
521 return GL_COMPRESSED_R11_EAC;
522 case MESA_FORMAT_ETC2_RG11_EAC:
523 return GL_COMPRESSED_RG11_EAC;
524 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
525 return GL_COMPRESSED_SIGNED_R11_EAC;
526 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
527 return GL_COMPRESSED_SIGNED_RG11_EAC;
528 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
529 return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
530 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
531 return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
532
533 case MESA_FORMAT_BPTC_RGBA_UNORM:
534 return GL_COMPRESSED_RGBA_BPTC_UNORM;
535 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
536 return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
537 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
538 return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
539 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
540 return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
541
542 default:
543 _mesa_problem(ctx, "Unexpected mesa texture format in"
544 " _mesa_compressed_format_to_glenum()");
545 return 0;
546 }
547 }
548
549
550 /*
551 * Return the address of the pixel at (col, row, img) in a
552 * compressed texture image.
553 * \param col, row, img - image position (3D), should be a multiple of the
554 * format's block size.
555 * \param format - compressed image format
556 * \param width - image width (stride) in pixels
557 * \param image - the image address
558 * \return address of pixel at (row, col, img)
559 */
560 GLubyte *
561 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
562 mesa_format mesaFormat,
563 GLsizei width, const GLubyte *image)
564 {
565 /* XXX only 2D images implemented, not 3D */
566 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
567 GLuint bw, bh;
568 GLint offset;
569
570 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
571
572 assert(col % bw == 0);
573 assert(row % bh == 0);
574
575 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
576 offset *= blockSize;
577
578 return (GLubyte *) image + offset;
579 }
580
581
582 /**
583 * Return a texel-fetch function for the given format, or NULL if
584 * invalid format.
585 */
586 compressed_fetch_func
587 _mesa_get_compressed_fetch_func(mesa_format format)
588 {
589 switch (_mesa_get_format_layout(format)) {
590 case MESA_FORMAT_LAYOUT_S3TC:
591 return _mesa_get_dxt_fetch_func(format);
592 case MESA_FORMAT_LAYOUT_FXT1:
593 return _mesa_get_fxt_fetch_func(format);
594 case MESA_FORMAT_LAYOUT_RGTC:
595 return _mesa_get_compressed_rgtc_func(format);
596 case MESA_FORMAT_LAYOUT_ETC1:
597 return _mesa_get_etc_fetch_func(format);
598 case MESA_FORMAT_LAYOUT_BPTC:
599 return _mesa_get_bptc_fetch_func(format);
600 default:
601 return NULL;
602 }
603 }
604
605
606 /**
607 * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
608 * \param srcRowStride stride in bytes between rows of blocks in the
609 * compressed source image.
610 */
611 void
612 _mesa_decompress_image(mesa_format format, GLuint width, GLuint height,
613 const GLubyte *src, GLint srcRowStride,
614 GLfloat *dest)
615 {
616 compressed_fetch_func fetch;
617 GLuint i, j;
618 GLuint bytes, bw, bh;
619 GLint stride;
620
621 bytes = _mesa_get_format_bytes(format);
622 _mesa_get_format_block_size(format, &bw, &bh);
623
624 fetch = _mesa_get_compressed_fetch_func(format);
625 if (!fetch) {
626 _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
627 return;
628 }
629
630 stride = srcRowStride * bh / bytes;
631
632 for (j = 0; j < height; j++) {
633 for (i = 0; i < width; i++) {
634 fetch(src, stride, i, j, dest);
635 dest += 4;
636 }
637 }
638 }