mesa: Add new MESA_FORMATs for ETC2 compressed textures
[mesa.git] / src / mesa / main / texcompress.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008 VMware, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 "colormac.h"
36 #include "context.h"
37 #include "formats.h"
38 #include "mfeatures.h"
39 #include "mtypes.h"
40 #include "texcompress.h"
41 #include "texcompress_fxt1.h"
42 #include "texcompress_rgtc.h"
43 #include "texcompress_s3tc.h"
44 #include "texcompress_etc.h"
45 #include "swrast/s_context.h"
46
47
48 /**
49 * Get the GL base format of a specified GL compressed texture format
50 *
51 * From page 232 of the OpenGL 3.3 (Compatiblity Profile) spec:
52 *
53 * "Compressed Internal Format Base Internal Format Type
54 * --------------------------- -------------------- ---------
55 * COMPRESSED_ALPHA ALPHA Generic
56 * COMPRESSED_LUMINANCE LUMINANCE Generic
57 * COMPRESSED_LUMINANCE_ALPHA LUMINANCE_ALPHA Generic
58 * COMPRESSED_INTENSITY INTENSITY Generic
59 * COMPRESSED_RED RED Generic
60 * COMPRESSED_RG RG Generic
61 * COMPRESSED_RGB RGB Generic
62 * COMPRESSED_RGBA RGBA Generic
63 * COMPRESSED_SRGB RGB Generic
64 * COMPRESSED_SRGB_ALPHA RGBA Generic
65 * COMPRESSED_SLUMINANCE LUMINANCE Generic
66 * COMPRESSED_SLUMINANCE_ALPHA LUMINANCE_ALPHA Generic
67 * COMPRESSED_RED_RGTC1 RED Specific
68 * COMPRESSED_SIGNED_RED_RGTC1 RED Specific
69 * COMPRESSED_RG_RGTC2 RG Specific
70 * COMPRESSED_SIGNED_RG_RGTC2 RG Specific"
71 *
72 * \return
73 * The base format of \c format if \c format is a compressed format (either
74 * generic or specific. Otherwise 0 is returned.
75 */
76 GLenum
77 _mesa_gl_compressed_format_base_format(GLenum format)
78 {
79 switch (format) {
80 case GL_COMPRESSED_RED:
81 case GL_COMPRESSED_R11_EAC:
82 case GL_COMPRESSED_RED_RGTC1:
83 case GL_COMPRESSED_SIGNED_R11_EAC:
84 case GL_COMPRESSED_SIGNED_RED_RGTC1:
85 return GL_RED;
86
87 case GL_COMPRESSED_RG:
88 case GL_COMPRESSED_RG11_EAC:
89 case GL_COMPRESSED_RG_RGTC2:
90 case GL_COMPRESSED_SIGNED_RG11_EAC:
91 case GL_COMPRESSED_SIGNED_RG_RGTC2:
92 return GL_RG;
93
94 case GL_COMPRESSED_RGB:
95 case GL_COMPRESSED_SRGB:
96 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
97 case GL_COMPRESSED_RGB_FXT1_3DFX:
98 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
99 case GL_ETC1_RGB8_OES:
100 case GL_COMPRESSED_RGB8_ETC2:
101 case GL_COMPRESSED_SRGB8_ETC2:
102 return GL_RGB;
103
104 case GL_COMPRESSED_RGBA:
105 case GL_COMPRESSED_SRGB_ALPHA:
106 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
107 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
108 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB:
109 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB:
110 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
111 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
112 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
113 case GL_COMPRESSED_RGBA_FXT1_3DFX:
114 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
115 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
116 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
117 case GL_COMPRESSED_RGBA8_ETC2_EAC:
118 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
119 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
120 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
121 return GL_RGBA;
122
123 case GL_COMPRESSED_ALPHA:
124 return GL_ALPHA;
125
126 case GL_COMPRESSED_LUMINANCE:
127 case GL_COMPRESSED_SLUMINANCE:
128 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
129 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
130 return GL_LUMINANCE;
131
132 case GL_COMPRESSED_LUMINANCE_ALPHA:
133 case GL_COMPRESSED_SLUMINANCE_ALPHA:
134 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
135 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
136 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
137 return GL_LUMINANCE_ALPHA;
138
139 case GL_COMPRESSED_INTENSITY:
140 return GL_INTENSITY;
141
142 default:
143 return 0;
144 }
145 }
146
147 /**
148 * Return list of (and count of) all specific texture compression
149 * formats that are supported.
150 *
151 * Some formats are \b not returned by this function. The
152 * \c GL_COMPRESSED_TEXTURE_FORMATS query only returns formats that are
153 * "suitable for general-purpose usage." All texture compression extensions
154 * have taken this to mean either linear RGB or linear RGBA.
155 *
156 * The GL_ARB_texture_compress_rgtc spec says:
157 *
158 * "19) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
159 * GL_COMPRESSED_TEXTURE_FORMATS queries return the RGTC formats?
160 *
161 * RESOLVED: No.
162 *
163 * The OpenGL 2.1 specification says "The only values returned
164 * by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
165 * corresponding to formats suitable for general-purpose usage.
166 * The renderer will not enumerate formats with restrictions that
167 * need to be specifically understood prior to use."
168 *
169 * Compressed textures with just red or red-green components are
170 * not general-purpose so should not be returned by these queries
171 * because they have restrictions.
172 *
173 * Applications that seek to use the RGTC formats should do so
174 * by looking for this extension's name in the string returned by
175 * glGetString(GL_EXTENSIONS) rather than
176 * what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
177 * GL_COMPRESSED_TEXTURE_FORMATS return."
178 *
179 * There is nearly identical wording in the GL_EXT_texture_compression_rgtc
180 * spec.
181 *
182 * The GL_EXT_texture_rRGB spec says:
183 *
184 * "22) Should the new COMPRESSED_SRGB_* formats be listed in an
185 * implementation's GL_COMPRESSED_TEXTURE_FORMATS list?
186 *
187 * RESOLVED: No. Section 3.8.1 says formats listed by
188 * GL_COMPRESSED_TEXTURE_FORMATS are "suitable for general-purpose
189 * usage." The non-linear distribution of red, green, and
190 * blue for these sRGB compressed formats makes them not really
191 * general-purpose."
192 *
193 * The GL_EXT_texture_compression_latc spec says:
194 *
195 * "16) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
196 * GL_COMPRESSED_TEXTURE_FORMATS queries return the LATC formats?
197 *
198 * RESOLVED: No.
199 *
200 * The OpenGL 2.1 specification says "The only values returned
201 * by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
202 * corresponding to formats suitable for general-purpose usage.
203 * The renderer will not enumerate formats with restrictions that
204 * need to be specifically understood prior to use."
205 *
206 * Historically, OpenGL implementation have advertised the RGB and
207 * RGBA versions of the S3TC extensions compressed format tokens
208 * through this mechanism.
209 *
210 * The specification is not sufficiently clear about what "suitable
211 * for general-purpose usage" means. Historically that seems to mean
212 * unsigned RGB or unsigned RGBA. The DXT1 format supporting alpha
213 * (GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) is not exposed in the list (at
214 * least for NVIDIA drivers) because the alpha is always 1.0 expect
215 * when it is 0.0 when RGB is required to be black. NVIDIA's even
216 * limits itself to true linear RGB or RGBA formats, specifically
217 * not including EXT_texture_sRGB's sRGB S3TC compressed formats.
218 *
219 * Adding luminance and luminance-alpha texture formats (and
220 * certainly signed versions of luminance and luminance-alpha
221 * formats!) invites potential comptaibility problems with old
222 * applications using this mechanism since old applications are
223 * unlikely to expect non-RGB or non-RGBA formats to be advertised
224 * through this mechanism. However no specific misinteractions
225 * with old applications is known.
226 *
227 * Applications that seek to use the LATC formats should do so
228 * by looking for this extension's name in the string returned by
229 * glGetString(GL_EXTENSIONS) rather than
230 * what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
231 * GL_COMPRESSED_TEXTURE_FORMATS return."
232 *
233 * There is no formal spec for GL_ATI_texture_compression_3dc. Since the
234 * formats added by this extension are luminance-alpha formats, it is
235 * reasonable to expect them to follow the same rules as
236 * GL_EXT_texture_compression_latc. At the very least, Catalyst 11.6 does not
237 * expose the 3dc formats through this mechanism.
238 *
239 * \param ctx the GL context
240 * \param formats the resulting format list (may be NULL).
241 *
242 * \return number of formats.
243 */
244 GLuint
245 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats)
246 {
247 GLuint n = 0;
248 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
249 if (formats) {
250 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
251 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
252 }
253 else {
254 n += 2;
255 }
256 }
257
258 if (ctx->Extensions.EXT_texture_compression_s3tc) {
259 if (formats) {
260 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
261 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
262 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
263 }
264 else {
265 n += 3;
266 }
267 }
268 if (ctx->Extensions.S3_s3tc) {
269 if (formats) {
270 formats[n++] = GL_RGB_S3TC;
271 formats[n++] = GL_RGB4_S3TC;
272 formats[n++] = GL_RGBA_S3TC;
273 formats[n++] = GL_RGBA4_S3TC;
274 }
275 else {
276 n += 4;
277 }
278 }
279
280 if (ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
281 if (formats) {
282 formats[n++] = GL_ETC1_RGB8_OES;
283 }
284 else {
285 n += 1;
286 }
287 }
288
289 if (ctx->API == API_OPENGLES) {
290 if (formats) {
291 formats[n++] = GL_PALETTE4_RGB8_OES;
292 formats[n++] = GL_PALETTE4_RGBA8_OES;
293 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
294 formats[n++] = GL_PALETTE4_RGBA4_OES;
295 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
296 formats[n++] = GL_PALETTE8_RGB8_OES;
297 formats[n++] = GL_PALETTE8_RGBA8_OES;
298 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
299 formats[n++] = GL_PALETTE8_RGBA4_OES;
300 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
301 }
302 else {
303 n += 10;
304 }
305 }
306
307 if (_mesa_is_gles3(ctx)) {
308 if (formats) {
309 formats[n++] = GL_COMPRESSED_RGB8_ETC2;
310 formats[n++] = GL_COMPRESSED_SRGB8_ETC2;
311 formats[n++] = GL_COMPRESSED_RGBA8_ETC2_EAC;
312 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
313 formats[n++] = GL_COMPRESSED_R11_EAC;
314 formats[n++] = GL_COMPRESSED_RG11_EAC;
315 formats[n++] = GL_COMPRESSED_SIGNED_R11_EAC;
316 formats[n++] = GL_COMPRESSED_SIGNED_RG11_EAC;
317 formats[n++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
318 formats[n++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
319 }
320 else {
321 n += 10;
322 }
323 }
324 return n;
325 }
326
327
328 /**
329 * Convert a compressed MESA_FORMAT_x to a GLenum.
330 */
331 gl_format
332 _mesa_glenum_to_compressed_format(GLenum format)
333 {
334 switch (format) {
335 case GL_COMPRESSED_RGB_FXT1_3DFX:
336 return MESA_FORMAT_RGB_FXT1;
337 case GL_COMPRESSED_RGBA_FXT1_3DFX:
338 return MESA_FORMAT_RGBA_FXT1;
339
340 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
341 case GL_RGB_S3TC:
342 return MESA_FORMAT_RGB_DXT1;
343 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
344 case GL_RGB4_S3TC:
345 return MESA_FORMAT_RGBA_DXT1;
346 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
347 case GL_RGBA_S3TC:
348 return MESA_FORMAT_RGBA_DXT3;
349 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
350 case GL_RGBA4_S3TC:
351 return MESA_FORMAT_RGBA_DXT5;
352
353 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
354 return MESA_FORMAT_SRGB_DXT1;
355 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
356 return MESA_FORMAT_SRGBA_DXT1;
357 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
358 return MESA_FORMAT_SRGBA_DXT3;
359 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
360 return MESA_FORMAT_SRGBA_DXT5;
361
362 case GL_COMPRESSED_RED_RGTC1:
363 return MESA_FORMAT_RED_RGTC1;
364 case GL_COMPRESSED_SIGNED_RED_RGTC1:
365 return MESA_FORMAT_SIGNED_RED_RGTC1;
366 case GL_COMPRESSED_RG_RGTC2:
367 return MESA_FORMAT_RG_RGTC2;
368 case GL_COMPRESSED_SIGNED_RG_RGTC2:
369 return MESA_FORMAT_SIGNED_RG_RGTC2;
370
371 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
372 return MESA_FORMAT_L_LATC1;
373 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
374 return MESA_FORMAT_SIGNED_L_LATC1;
375 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
376 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
377 return MESA_FORMAT_LA_LATC2;
378 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
379 return MESA_FORMAT_SIGNED_LA_LATC2;
380
381 case GL_ETC1_RGB8_OES:
382 return MESA_FORMAT_ETC1_RGB8;
383 case GL_COMPRESSED_RGB8_ETC2:
384 return MESA_FORMAT_ETC2_RGB8;
385 case GL_COMPRESSED_SRGB8_ETC2:
386 return MESA_FORMAT_ETC2_SRGB8;
387 case GL_COMPRESSED_RGBA8_ETC2_EAC:
388 return MESA_FORMAT_ETC2_RGBA8_EAC;
389 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
390 return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
391 case GL_COMPRESSED_R11_EAC:
392 return MESA_FORMAT_ETC2_R11_EAC;
393 case GL_COMPRESSED_RG11_EAC:
394 return MESA_FORMAT_ETC2_RG11_EAC;
395 case GL_COMPRESSED_SIGNED_R11_EAC:
396 return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
397 case GL_COMPRESSED_SIGNED_RG11_EAC:
398 return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
399 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
400 return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
401 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
402 return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
403
404 default:
405 return MESA_FORMAT_NONE;
406 }
407 }
408
409
410 /**
411 * Given a compressed MESA_FORMAT_x value, return the corresponding
412 * GLenum for that format.
413 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
414 * which must return the specific texture format used when the user might
415 * have originally specified a generic compressed format in their
416 * glTexImage2D() call.
417 * For non-compressed textures, we always return the user-specified
418 * internal format unchanged.
419 */
420 GLenum
421 _mesa_compressed_format_to_glenum(struct gl_context *ctx, gl_format mesaFormat)
422 {
423 switch (mesaFormat) {
424 case MESA_FORMAT_RGB_FXT1:
425 return GL_COMPRESSED_RGB_FXT1_3DFX;
426 case MESA_FORMAT_RGBA_FXT1:
427 return GL_COMPRESSED_RGBA_FXT1_3DFX;
428 case MESA_FORMAT_RGB_DXT1:
429 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
430 case MESA_FORMAT_RGBA_DXT1:
431 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
432 case MESA_FORMAT_RGBA_DXT3:
433 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
434 case MESA_FORMAT_RGBA_DXT5:
435 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
436 case MESA_FORMAT_SRGB_DXT1:
437 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
438 case MESA_FORMAT_SRGBA_DXT1:
439 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
440 case MESA_FORMAT_SRGBA_DXT3:
441 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
442 case MESA_FORMAT_SRGBA_DXT5:
443 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
444 case MESA_FORMAT_RED_RGTC1:
445 return GL_COMPRESSED_RED_RGTC1;
446 case MESA_FORMAT_SIGNED_RED_RGTC1:
447 return GL_COMPRESSED_SIGNED_RED_RGTC1;
448 case MESA_FORMAT_RG_RGTC2:
449 return GL_COMPRESSED_RG_RGTC2;
450 case MESA_FORMAT_SIGNED_RG_RGTC2:
451 return GL_COMPRESSED_SIGNED_RG_RGTC2;
452
453 case MESA_FORMAT_L_LATC1:
454 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
455 case MESA_FORMAT_SIGNED_L_LATC1:
456 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
457 case MESA_FORMAT_LA_LATC2:
458 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
459 case MESA_FORMAT_SIGNED_LA_LATC2:
460 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
461
462 case MESA_FORMAT_ETC1_RGB8:
463 return GL_ETC1_RGB8_OES;
464 case MESA_FORMAT_ETC2_RGB8:
465 return GL_COMPRESSED_RGB8_ETC2;
466 case MESA_FORMAT_ETC2_SRGB8:
467 return GL_COMPRESSED_SRGB8_ETC2;
468 case MESA_FORMAT_ETC2_RGBA8_EAC:
469 return GL_COMPRESSED_RGBA8_ETC2_EAC;
470 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
471 return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
472 case MESA_FORMAT_ETC2_R11_EAC:
473 return GL_COMPRESSED_R11_EAC;
474 case MESA_FORMAT_ETC2_RG11_EAC:
475 return GL_COMPRESSED_RG11_EAC;
476 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
477 return GL_COMPRESSED_SIGNED_R11_EAC;
478 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
479 return GL_COMPRESSED_SIGNED_RG11_EAC;
480 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
481 return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
482 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
483 return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
484
485 default:
486 _mesa_problem(ctx, "Unexpected mesa texture format in"
487 " _mesa_compressed_format_to_glenum()");
488 return 0;
489 }
490 }
491
492
493 /*
494 * Return the address of the pixel at (col, row, img) in a
495 * compressed texture image.
496 * \param col, row, img - image position (3D), should be a multiple of the
497 * format's block size.
498 * \param format - compressed image format
499 * \param width - image width (stride) in pixels
500 * \param image - the image address
501 * \return address of pixel at (row, col, img)
502 */
503 GLubyte *
504 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
505 gl_format mesaFormat,
506 GLsizei width, const GLubyte *image)
507 {
508 /* XXX only 2D images implemented, not 3D */
509 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
510 GLuint bw, bh;
511 GLint offset;
512
513 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
514
515 ASSERT(col % bw == 0);
516 ASSERT(row % bh == 0);
517
518 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
519 offset *= blockSize;
520
521 return (GLubyte *) image + offset;
522 }
523
524
525 /**
526 * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
527 * \param srcRowStride stride in bytes between rows of blocks in the
528 * compressed source image.
529 */
530 void
531 _mesa_decompress_image(gl_format format, GLuint width, GLuint height,
532 const GLubyte *src, GLint srcRowStride,
533 GLfloat *dest)
534 {
535 void (*fetch)(const struct swrast_texture_image *texImage,
536 GLint i, GLint j, GLint k, GLfloat *texel);
537 struct swrast_texture_image texImage; /* dummy teximage */
538 GLuint i, j;
539 GLuint bytes, bw, bh;
540
541 bytes = _mesa_get_format_bytes(format);
542 _mesa_get_format_block_size(format, &bw, &bh);
543
544 /* setup dummy texture image info */
545 memset(&texImage, 0, sizeof(texImage));
546 texImage.Map = (void *) src;
547
548 /* XXX This line is a bit of a hack to adapt to the row stride
549 * convention used by the texture decompression functions.
550 */
551 texImage.RowStride = srcRowStride * bh / bytes;
552
553 switch (format) {
554 /* DXT formats */
555 case MESA_FORMAT_RGB_DXT1:
556 fetch = _mesa_fetch_texel_rgb_dxt1;
557 break;
558 case MESA_FORMAT_RGBA_DXT1:
559 fetch = _mesa_fetch_texel_rgba_dxt1;
560 break;
561 case MESA_FORMAT_RGBA_DXT3:
562 fetch = _mesa_fetch_texel_rgba_dxt3;
563 break;
564 case MESA_FORMAT_RGBA_DXT5:
565 fetch = _mesa_fetch_texel_rgba_dxt5;
566 break;
567
568 /* FXT1 formats */
569 case MESA_FORMAT_RGB_FXT1:
570 fetch = _mesa_fetch_texel_2d_f_rgb_fxt1;
571 break;
572 case MESA_FORMAT_RGBA_FXT1:
573 fetch = _mesa_fetch_texel_2d_f_rgba_fxt1;
574 break;
575
576 /* Red/RG formats */
577 case MESA_FORMAT_RED_RGTC1:
578 fetch = _mesa_fetch_texel_red_rgtc1;
579 break;
580 case MESA_FORMAT_SIGNED_RED_RGTC1:
581 fetch = _mesa_fetch_texel_signed_red_rgtc1;
582 break;
583 case MESA_FORMAT_RG_RGTC2:
584 fetch = _mesa_fetch_texel_rg_rgtc2;
585 break;
586 case MESA_FORMAT_SIGNED_RG_RGTC2:
587 fetch = _mesa_fetch_texel_signed_rg_rgtc2;
588 break;
589
590 /* L/LA formats */
591 case MESA_FORMAT_L_LATC1:
592 fetch = _mesa_fetch_texel_l_latc1;
593 break;
594 case MESA_FORMAT_SIGNED_L_LATC1:
595 fetch = _mesa_fetch_texel_signed_l_latc1;
596 break;
597 case MESA_FORMAT_LA_LATC2:
598 fetch = _mesa_fetch_texel_la_latc2;
599 break;
600 case MESA_FORMAT_SIGNED_LA_LATC2:
601 fetch = _mesa_fetch_texel_signed_la_latc2;
602 break;
603
604 /* ETC1 formats */
605 case MESA_FORMAT_ETC1_RGB8:
606 fetch = _mesa_fetch_texel_2d_f_etc1_rgb8;
607 break;
608
609 /* ETC2 formats */
610 case MESA_FORMAT_ETC2_RGB8:
611 /* fetch = _mesa_fetch_texel_2d_f_etc2_rgb8; -- not implemented yet */
612 break;
613 case MESA_FORMAT_ETC2_SRGB8:
614 /* fetch = _mesa_fetch_texel_2d_f_etc2_srgb8; -- not implemented yet */
615 break;
616 case MESA_FORMAT_ETC2_RGBA8_EAC:
617 /* fetch = _mesa_fetch_texel_2d_f_etc2_rgba8_eac; -- not implemented yet */
618 break;
619 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
620 /* fetch = _mesa_fetch_texel_2d_f_etc2_srgb8_alpha8_eac; -- not implemented yet */
621 break;
622 case MESA_FORMAT_ETC2_R11_EAC:
623 /* fetch = _mesa_fetch_texel_2d_f_etc2_r11_eac; -- not implemented yet */
624 break;
625 case MESA_FORMAT_ETC2_RG11_EAC:
626 /* fetch = _mesa_fetch_texel_2d_f_etc2_rg11_eac; -- not implemented yet */
627 break;
628 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
629 /* fetch = _mesa_fetch_texel_2d_f_etc2_signed_r11_eac; -- not implemented yet */
630 break;
631 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
632 /* fetch = _mesa_fetch_texel_2d_f_etc2_signed_rg11_eac; -- not implemented yet */
633 break;
634 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
635 /* fetch = _mesa_fetch_texel_2d_f_etc2_rgb8_punchthrough_alpha1; -- not implemented yet */
636 break;
637 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
638 /* fetch = _mesa_fetch_texel_2d_f_etc2_srgb8_punchthrough_alpha1; -- not implemented yet */
639 break;
640
641 default:
642 _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
643 return;
644 }
645
646 for (j = 0; j < height; j++) {
647 for (i = 0; i < width; i++) {
648 fetch(&texImage, i, j, 0, dest);
649 dest += 4;
650 }
651 }
652 }