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