mesa: add missing texel fetch code for sRGB DXT formats
[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
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_S3TC_DXT1_EXT:
96 case GL_COMPRESSED_RGB_FXT1_3DFX:
97 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
98 case GL_ETC1_RGB8_OES:
99 case GL_COMPRESSED_RGB8_ETC2:
100 case GL_COMPRESSED_SRGB8_ETC2:
101 return GL_RGB;
102
103 case GL_COMPRESSED_RGBA:
104 case GL_COMPRESSED_SRGB_ALPHA:
105 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
106 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
107 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB:
108 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_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 * \param ctx the GL context
239 * \param formats the resulting format list (may be NULL).
240 *
241 * \return number of formats.
242 */
243 GLuint
244 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats)
245 {
246 GLuint n = 0;
247 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
248 if (formats) {
249 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
250 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
251 }
252 else {
253 n += 2;
254 }
255 }
256
257 if (ctx->Extensions.EXT_texture_compression_s3tc) {
258 if (formats) {
259 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
260 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
261 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
262 }
263 else {
264 n += 3;
265 }
266 }
267 if (ctx->Extensions.S3_s3tc) {
268 if (formats) {
269 formats[n++] = GL_RGB_S3TC;
270 formats[n++] = GL_RGB4_S3TC;
271 formats[n++] = GL_RGBA_S3TC;
272 formats[n++] = GL_RGBA4_S3TC;
273 }
274 else {
275 n += 4;
276 }
277 }
278
279 if (ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
280 if (formats) {
281 formats[n++] = GL_ETC1_RGB8_OES;
282 }
283 else {
284 n += 1;
285 }
286 }
287
288 if (ctx->API == API_OPENGLES) {
289 if (formats) {
290 formats[n++] = GL_PALETTE4_RGB8_OES;
291 formats[n++] = GL_PALETTE4_RGBA8_OES;
292 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
293 formats[n++] = GL_PALETTE4_RGBA4_OES;
294 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
295 formats[n++] = GL_PALETTE8_RGB8_OES;
296 formats[n++] = GL_PALETTE8_RGBA8_OES;
297 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
298 formats[n++] = GL_PALETTE8_RGBA4_OES;
299 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
300 }
301 else {
302 n += 10;
303 }
304 }
305
306 if (_mesa_is_gles3(ctx)) {
307 if (formats) {
308 formats[n++] = GL_COMPRESSED_RGB8_ETC2;
309 formats[n++] = GL_COMPRESSED_SRGB8_ETC2;
310 formats[n++] = GL_COMPRESSED_RGBA8_ETC2_EAC;
311 formats[n++] = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
312 formats[n++] = GL_COMPRESSED_R11_EAC;
313 formats[n++] = GL_COMPRESSED_RG11_EAC;
314 formats[n++] = GL_COMPRESSED_SIGNED_R11_EAC;
315 formats[n++] = GL_COMPRESSED_SIGNED_RG11_EAC;
316 formats[n++] = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
317 formats[n++] = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
318 }
319 else {
320 n += 10;
321 }
322 }
323 return n;
324 }
325
326
327 /**
328 * Convert a compressed MESA_FORMAT_x to a GLenum.
329 */
330 gl_format
331 _mesa_glenum_to_compressed_format(GLenum format)
332 {
333 switch (format) {
334 case GL_COMPRESSED_RGB_FXT1_3DFX:
335 return MESA_FORMAT_RGB_FXT1;
336 case GL_COMPRESSED_RGBA_FXT1_3DFX:
337 return MESA_FORMAT_RGBA_FXT1;
338
339 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
340 case GL_RGB_S3TC:
341 return MESA_FORMAT_RGB_DXT1;
342 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
343 case GL_RGB4_S3TC:
344 return MESA_FORMAT_RGBA_DXT1;
345 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
346 case GL_RGBA_S3TC:
347 return MESA_FORMAT_RGBA_DXT3;
348 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
349 case GL_RGBA4_S3TC:
350 return MESA_FORMAT_RGBA_DXT5;
351
352 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
353 return MESA_FORMAT_SRGB_DXT1;
354 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
355 return MESA_FORMAT_SRGBA_DXT1;
356 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
357 return MESA_FORMAT_SRGBA_DXT3;
358 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
359 return MESA_FORMAT_SRGBA_DXT5;
360
361 case GL_COMPRESSED_RED_RGTC1:
362 return MESA_FORMAT_RED_RGTC1;
363 case GL_COMPRESSED_SIGNED_RED_RGTC1:
364 return MESA_FORMAT_SIGNED_RED_RGTC1;
365 case GL_COMPRESSED_RG_RGTC2:
366 return MESA_FORMAT_RG_RGTC2;
367 case GL_COMPRESSED_SIGNED_RG_RGTC2:
368 return MESA_FORMAT_SIGNED_RG_RGTC2;
369
370 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
371 return MESA_FORMAT_L_LATC1;
372 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
373 return MESA_FORMAT_SIGNED_L_LATC1;
374 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
375 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
376 return MESA_FORMAT_LA_LATC2;
377 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
378 return MESA_FORMAT_SIGNED_LA_LATC2;
379
380 case GL_ETC1_RGB8_OES:
381 return MESA_FORMAT_ETC1_RGB8;
382 case GL_COMPRESSED_RGB8_ETC2:
383 return MESA_FORMAT_ETC2_RGB8;
384 case GL_COMPRESSED_SRGB8_ETC2:
385 return MESA_FORMAT_ETC2_SRGB8;
386 case GL_COMPRESSED_RGBA8_ETC2_EAC:
387 return MESA_FORMAT_ETC2_RGBA8_EAC;
388 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
389 return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
390 case GL_COMPRESSED_R11_EAC:
391 return MESA_FORMAT_ETC2_R11_EAC;
392 case GL_COMPRESSED_RG11_EAC:
393 return MESA_FORMAT_ETC2_RG11_EAC;
394 case GL_COMPRESSED_SIGNED_R11_EAC:
395 return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
396 case GL_COMPRESSED_SIGNED_RG11_EAC:
397 return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
398 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
399 return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
400 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
401 return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
402
403 default:
404 return MESA_FORMAT_NONE;
405 }
406 }
407
408
409 /**
410 * Given a compressed MESA_FORMAT_x value, return the corresponding
411 * GLenum for that format.
412 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
413 * which must return the specific texture format used when the user might
414 * have originally specified a generic compressed format in their
415 * glTexImage2D() call.
416 * For non-compressed textures, we always return the user-specified
417 * internal format unchanged.
418 */
419 GLenum
420 _mesa_compressed_format_to_glenum(struct gl_context *ctx, gl_format mesaFormat)
421 {
422 switch (mesaFormat) {
423 case MESA_FORMAT_RGB_FXT1:
424 return GL_COMPRESSED_RGB_FXT1_3DFX;
425 case MESA_FORMAT_RGBA_FXT1:
426 return GL_COMPRESSED_RGBA_FXT1_3DFX;
427 case MESA_FORMAT_RGB_DXT1:
428 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
429 case MESA_FORMAT_RGBA_DXT1:
430 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
431 case MESA_FORMAT_RGBA_DXT3:
432 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
433 case MESA_FORMAT_RGBA_DXT5:
434 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
435 case MESA_FORMAT_SRGB_DXT1:
436 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
437 case MESA_FORMAT_SRGBA_DXT1:
438 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
439 case MESA_FORMAT_SRGBA_DXT3:
440 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
441 case MESA_FORMAT_SRGBA_DXT5:
442 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
443 case MESA_FORMAT_RED_RGTC1:
444 return GL_COMPRESSED_RED_RGTC1;
445 case MESA_FORMAT_SIGNED_RED_RGTC1:
446 return GL_COMPRESSED_SIGNED_RED_RGTC1;
447 case MESA_FORMAT_RG_RGTC2:
448 return GL_COMPRESSED_RG_RGTC2;
449 case MESA_FORMAT_SIGNED_RG_RGTC2:
450 return GL_COMPRESSED_SIGNED_RG_RGTC2;
451
452 case MESA_FORMAT_L_LATC1:
453 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
454 case MESA_FORMAT_SIGNED_L_LATC1:
455 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
456 case MESA_FORMAT_LA_LATC2:
457 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
458 case MESA_FORMAT_SIGNED_LA_LATC2:
459 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
460
461 case MESA_FORMAT_ETC1_RGB8:
462 return GL_ETC1_RGB8_OES;
463 case MESA_FORMAT_ETC2_RGB8:
464 return GL_COMPRESSED_RGB8_ETC2;
465 case MESA_FORMAT_ETC2_SRGB8:
466 return GL_COMPRESSED_SRGB8_ETC2;
467 case MESA_FORMAT_ETC2_RGBA8_EAC:
468 return GL_COMPRESSED_RGBA8_ETC2_EAC;
469 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
470 return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
471 case MESA_FORMAT_ETC2_R11_EAC:
472 return GL_COMPRESSED_R11_EAC;
473 case MESA_FORMAT_ETC2_RG11_EAC:
474 return GL_COMPRESSED_RG11_EAC;
475 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
476 return GL_COMPRESSED_SIGNED_R11_EAC;
477 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
478 return GL_COMPRESSED_SIGNED_RG11_EAC;
479 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
480 return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
481 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
482 return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
483
484 default:
485 _mesa_problem(ctx, "Unexpected mesa texture format in"
486 " _mesa_compressed_format_to_glenum()");
487 return 0;
488 }
489 }
490
491
492 /*
493 * Return the address of the pixel at (col, row, img) in a
494 * compressed texture image.
495 * \param col, row, img - image position (3D), should be a multiple of the
496 * format's block size.
497 * \param format - compressed image format
498 * \param width - image width (stride) in pixels
499 * \param image - the image address
500 * \return address of pixel at (row, col, img)
501 */
502 GLubyte *
503 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
504 gl_format mesaFormat,
505 GLsizei width, const GLubyte *image)
506 {
507 /* XXX only 2D images implemented, not 3D */
508 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
509 GLuint bw, bh;
510 GLint offset;
511
512 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
513
514 ASSERT(col % bw == 0);
515 ASSERT(row % bh == 0);
516
517 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
518 offset *= blockSize;
519
520 return (GLubyte *) image + offset;
521 }
522
523
524 /**
525 * Return a texel-fetch function for the given format, or NULL if
526 * invalid format.
527 */
528 compressed_fetch_func
529 _mesa_get_compressed_fetch_func(gl_format format)
530 {
531 switch (format) {
532 case MESA_FORMAT_RGB_DXT1:
533 case MESA_FORMAT_RGBA_DXT1:
534 case MESA_FORMAT_RGBA_DXT3:
535 case MESA_FORMAT_RGBA_DXT5:
536 case MESA_FORMAT_SRGB_DXT1:
537 case MESA_FORMAT_SRGBA_DXT1:
538 case MESA_FORMAT_SRGBA_DXT3:
539 case MESA_FORMAT_SRGBA_DXT5:
540 return _mesa_get_dxt_fetch_func(format);
541 case MESA_FORMAT_RGB_FXT1:
542 case MESA_FORMAT_RGBA_FXT1:
543 return _mesa_get_fxt_fetch_func(format);
544 case MESA_FORMAT_RED_RGTC1:
545 case MESA_FORMAT_L_LATC1:
546 case MESA_FORMAT_SIGNED_RED_RGTC1:
547 case MESA_FORMAT_SIGNED_L_LATC1:
548 case MESA_FORMAT_RG_RGTC2:
549 case MESA_FORMAT_LA_LATC2:
550 case MESA_FORMAT_SIGNED_RG_RGTC2:
551 case MESA_FORMAT_SIGNED_LA_LATC2:
552 return _mesa_get_compressed_rgtc_func(format);
553 case MESA_FORMAT_ETC1_RGB8:
554 return _mesa_get_etc_fetch_func(format);
555 default:
556 return NULL;
557 }
558 }
559
560
561 /**
562 * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
563 * \param srcRowStride stride in bytes between rows of blocks in the
564 * compressed source image.
565 */
566 void
567 _mesa_decompress_image(gl_format format, GLuint width, GLuint height,
568 const GLubyte *src, GLint srcRowStride,
569 GLfloat *dest)
570 {
571 compressed_fetch_func fetch;
572 GLuint i, j;
573 GLuint bytes, bw, bh;
574 GLint stride;
575
576 bytes = _mesa_get_format_bytes(format);
577 _mesa_get_format_block_size(format, &bw, &bh);
578
579 fetch = _mesa_get_compressed_fetch_func(format);
580 if (!fetch) {
581 _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
582 return;
583 }
584
585 stride = srcRowStride * bh / bytes;
586
587 for (j = 0; j < height; j++) {
588 for (i = 0; i < width; i++) {
589 fetch(src, NULL, stride, i, j, 0, dest);
590 dest += 4;
591 }
592 }
593 }