[[MESA]
[reactos.git] / reactos / dll / opengl / mesa / 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 "formats.h"
37 #include "mfeatures.h"
38 #include "mtypes.h"
39 #include "texcompress.h"
40 #include "texcompress_fxt1.h"
41 #include "texcompress_rgtc.h"
42 #include "texcompress_s3tc.h"
43 #include "swrast/s_context.h"
44
45
46 /**
47 * Get the GL base format of a specified GL compressed texture format
48 *
49 * From page 232 of the OpenGL 3.3 (Compatiblity Profile) spec:
50 *
51 * "Compressed Internal Format Base Internal Format Type
52 * --------------------------- -------------------- ---------
53 * COMPRESSED_ALPHA ALPHA Generic
54 * COMPRESSED_LUMINANCE LUMINANCE Generic
55 * COMPRESSED_LUMINANCE_ALPHA LUMINANCE_ALPHA Generic
56 * COMPRESSED_INTENSITY INTENSITY Generic
57 * COMPRESSED_RED RED Generic
58 * COMPRESSED_RG RG Generic
59 * COMPRESSED_RGB RGB Generic
60 * COMPRESSED_RGBA RGBA Generic
61 * COMPRESSED_SRGB RGB Generic
62 * COMPRESSED_SRGB_ALPHA RGBA Generic
63 * COMPRESSED_SLUMINANCE LUMINANCE Generic
64 * COMPRESSED_SLUMINANCE_ALPHA LUMINANCE_ALPHA Generic
65 * COMPRESSED_RED_RGTC1 RED Specific
66 * COMPRESSED_SIGNED_RED_RGTC1 RED Specific
67 * COMPRESSED_RG_RGTC2 RG Specific
68 * COMPRESSED_SIGNED_RG_RGTC2 RG Specific"
69 *
70 * \return
71 * The base format of \c format if \c format is a compressed format (either
72 * generic or specific. Otherwise 0 is returned.
73 */
74 GLenum
75 _mesa_gl_compressed_format_base_format(GLenum format)
76 {
77 switch (format) {
78 case GL_COMPRESSED_RED:
79 case GL_COMPRESSED_RED_RGTC1:
80 case GL_COMPRESSED_SIGNED_RED_RGTC1:
81 return GL_RED;
82
83 case GL_COMPRESSED_RG:
84 case GL_COMPRESSED_RG_RGTC2:
85 case GL_COMPRESSED_SIGNED_RG_RGTC2:
86 return GL_RG;
87
88 case GL_COMPRESSED_RGB:
89 case GL_COMPRESSED_SRGB:
90 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
91 case GL_COMPRESSED_RGB_FXT1_3DFX:
92 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
93 return GL_RGB;
94
95 case GL_COMPRESSED_RGBA:
96 case GL_COMPRESSED_SRGB_ALPHA:
97 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
98 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
99 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB:
100 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB:
101 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
102 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
103 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
104 case GL_COMPRESSED_RGBA_FXT1_3DFX:
105 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
106 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
107 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
108 return GL_RGBA;
109
110 case GL_COMPRESSED_ALPHA:
111 return GL_ALPHA;
112
113 case GL_COMPRESSED_LUMINANCE:
114 case GL_COMPRESSED_SLUMINANCE:
115 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
116 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
117 return GL_LUMINANCE;
118
119 case GL_COMPRESSED_LUMINANCE_ALPHA:
120 case GL_COMPRESSED_SLUMINANCE_ALPHA:
121 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
122 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
123 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
124 return GL_LUMINANCE_ALPHA;
125
126 case GL_COMPRESSED_INTENSITY:
127 return GL_INTENSITY;
128
129 default:
130 return 0;
131 }
132 }
133
134 /**
135 * Return list of (and count of) all specific texture compression
136 * formats that are supported.
137 *
138 * Some formats are \b not returned by this function. The
139 * \c GL_COMPRESSED_TEXTURE_FORMATS query only returns formats that are
140 * "suitable for general-purpose usage." All texture compression extensions
141 * have taken this to mean either linear RGB or linear RGBA.
142 *
143 * The GL_ARB_texture_compress_rgtc spec says:
144 *
145 * "19) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
146 * GL_COMPRESSED_TEXTURE_FORMATS queries return the RGTC formats?
147 *
148 * RESOLVED: No.
149 *
150 * The OpenGL 2.1 specification says "The only values returned
151 * by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
152 * corresponding to formats suitable for general-purpose usage.
153 * The renderer will not enumerate formats with restrictions that
154 * need to be specifically understood prior to use."
155 *
156 * Compressed textures with just red or red-green components are
157 * not general-purpose so should not be returned by these queries
158 * because they have restrictions.
159 *
160 * Applications that seek to use the RGTC formats should do so
161 * by looking for this extension's name in the string returned by
162 * glGetString(GL_EXTENSIONS) rather than
163 * what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
164 * GL_COMPRESSED_TEXTURE_FORMATS return."
165 *
166 * There is nearly identical wording in the GL_EXT_texture_compression_rgtc
167 * spec.
168 *
169 * The GL_EXT_texture_rRGB spec says:
170 *
171 * "22) Should the new COMPRESSED_SRGB_* formats be listed in an
172 * implementation's GL_COMPRESSED_TEXTURE_FORMATS list?
173 *
174 * RESOLVED: No. Section 3.8.1 says formats listed by
175 * GL_COMPRESSED_TEXTURE_FORMATS are "suitable for general-purpose
176 * usage." The non-linear distribution of red, green, and
177 * blue for these sRGB compressed formats makes them not really
178 * general-purpose."
179 *
180 * The GL_EXT_texture_compression_latc spec says:
181 *
182 * "16) Should the GL_NUM_COMPRESSED_TEXTURE_FORMATS and
183 * GL_COMPRESSED_TEXTURE_FORMATS queries return the LATC formats?
184 *
185 * RESOLVED: No.
186 *
187 * The OpenGL 2.1 specification says "The only values returned
188 * by this query [GL_COMPRESSED_TEXTURE_FORMATS"] are those
189 * corresponding to formats suitable for general-purpose usage.
190 * The renderer will not enumerate formats with restrictions that
191 * need to be specifically understood prior to use."
192 *
193 * Historically, OpenGL implementation have advertised the RGB and
194 * RGBA versions of the S3TC extensions compressed format tokens
195 * through this mechanism.
196 *
197 * The specification is not sufficiently clear about what "suitable
198 * for general-purpose usage" means. Historically that seems to mean
199 * unsigned RGB or unsigned RGBA. The DXT1 format supporting alpha
200 * (GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) is not exposed in the list (at
201 * least for NVIDIA drivers) because the alpha is always 1.0 expect
202 * when it is 0.0 when RGB is required to be black. NVIDIA's even
203 * limits itself to true linear RGB or RGBA formats, specifically
204 * not including EXT_texture_sRGB's sRGB S3TC compressed formats.
205 *
206 * Adding luminance and luminance-alpha texture formats (and
207 * certainly signed versions of luminance and luminance-alpha
208 * formats!) invites potential comptaibility problems with old
209 * applications using this mechanism since old applications are
210 * unlikely to expect non-RGB or non-RGBA formats to be advertised
211 * through this mechanism. However no specific misinteractions
212 * with old applications is known.
213 *
214 * Applications that seek to use the LATC formats should do so
215 * by looking for this extension's name in the string returned by
216 * glGetString(GL_EXTENSIONS) rather than
217 * what GL_NUM_COMPRESSED_TEXTURE_FORMATS and
218 * GL_COMPRESSED_TEXTURE_FORMATS return."
219 *
220 * There is no formal spec for GL_ATI_texture_compression_3dc. Since the
221 * formats added by this extension are luminance-alpha formats, it is
222 * reasonable to expect them to follow the same rules as
223 * GL_EXT_texture_compression_latc. At the very least, Catalyst 11.6 does not
224 * expose the 3dc formats through this mechanism.
225 *
226 * \param ctx the GL context
227 * \param formats the resulting format list (may be NULL).
228 *
229 * \return number of formats.
230 */
231 GLuint
232 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats)
233 {
234 GLuint n = 0;
235 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
236 if (formats) {
237 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
238 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
239 }
240 else {
241 n += 2;
242 }
243 }
244
245 if (ctx->Extensions.EXT_texture_compression_s3tc) {
246 if (formats) {
247 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
248 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
249 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
250 }
251 else {
252 n += 3;
253 }
254 }
255 if (ctx->Extensions.S3_s3tc) {
256 if (formats) {
257 formats[n++] = GL_RGB_S3TC;
258 formats[n++] = GL_RGB4_S3TC;
259 formats[n++] = GL_RGBA_S3TC;
260 formats[n++] = GL_RGBA4_S3TC;
261 }
262 else {
263 n += 4;
264 }
265 }
266
267 #if FEATURE_ES1
268 if (ctx->API == API_OPENGLES) {
269 if (formats) {
270 formats[n++] = GL_PALETTE4_RGB8_OES;
271 formats[n++] = GL_PALETTE4_RGBA8_OES;
272 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
273 formats[n++] = GL_PALETTE4_RGBA4_OES;
274 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
275 formats[n++] = GL_PALETTE8_RGB8_OES;
276 formats[n++] = GL_PALETTE8_RGBA8_OES;
277 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
278 formats[n++] = GL_PALETTE8_RGBA4_OES;
279 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
280 }
281 else {
282 n += 10;
283 }
284 }
285 #endif
286
287 return n;
288 }
289
290
291 /**
292 * Convert a compressed MESA_FORMAT_x to a GLenum.
293 */
294 gl_format
295 _mesa_glenum_to_compressed_format(GLenum format)
296 {
297 switch (format) {
298 case GL_COMPRESSED_RGB_FXT1_3DFX:
299 return MESA_FORMAT_RGB_FXT1;
300 case GL_COMPRESSED_RGBA_FXT1_3DFX:
301 return MESA_FORMAT_RGBA_FXT1;
302
303 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
304 case GL_RGB_S3TC:
305 return MESA_FORMAT_RGB_DXT1;
306 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
307 case GL_RGB4_S3TC:
308 return MESA_FORMAT_RGBA_DXT1;
309 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
310 case GL_RGBA_S3TC:
311 return MESA_FORMAT_RGBA_DXT3;
312 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
313 case GL_RGBA4_S3TC:
314 return MESA_FORMAT_RGBA_DXT5;
315
316 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
317 return MESA_FORMAT_SRGB_DXT1;
318 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
319 return MESA_FORMAT_SRGBA_DXT1;
320 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
321 return MESA_FORMAT_SRGBA_DXT3;
322 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
323 return MESA_FORMAT_SRGBA_DXT5;
324
325 case GL_COMPRESSED_RED_RGTC1:
326 return MESA_FORMAT_RED_RGTC1;
327 case GL_COMPRESSED_SIGNED_RED_RGTC1:
328 return MESA_FORMAT_SIGNED_RED_RGTC1;
329 case GL_COMPRESSED_RG_RGTC2:
330 return MESA_FORMAT_RG_RGTC2;
331 case GL_COMPRESSED_SIGNED_RG_RGTC2:
332 return MESA_FORMAT_SIGNED_RG_RGTC2;
333
334 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
335 return MESA_FORMAT_L_LATC1;
336 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
337 return MESA_FORMAT_SIGNED_L_LATC1;
338 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
339 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
340 return MESA_FORMAT_LA_LATC2;
341 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
342 return MESA_FORMAT_SIGNED_LA_LATC2;
343
344 default:
345 return MESA_FORMAT_NONE;
346 }
347 }
348
349
350 /**
351 * Given a compressed MESA_FORMAT_x value, return the corresponding
352 * GLenum for that format.
353 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
354 * which must return the specific texture format used when the user might
355 * have originally specified a generic compressed format in their
356 * glTexImage2D() call.
357 * For non-compressed textures, we always return the user-specified
358 * internal format unchanged.
359 */
360 GLenum
361 _mesa_compressed_format_to_glenum(struct gl_context *ctx, GLuint mesaFormat)
362 {
363 switch (mesaFormat) {
364 #if FEATURE_texture_fxt1
365 case MESA_FORMAT_RGB_FXT1:
366 return GL_COMPRESSED_RGB_FXT1_3DFX;
367 case MESA_FORMAT_RGBA_FXT1:
368 return GL_COMPRESSED_RGBA_FXT1_3DFX;
369 #endif
370 #if FEATURE_texture_s3tc
371 case MESA_FORMAT_RGB_DXT1:
372 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
373 case MESA_FORMAT_RGBA_DXT1:
374 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
375 case MESA_FORMAT_RGBA_DXT3:
376 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
377 case MESA_FORMAT_RGBA_DXT5:
378 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
379 #if FEATURE_EXT_texture_sRGB
380 case MESA_FORMAT_SRGB_DXT1:
381 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
382 case MESA_FORMAT_SRGBA_DXT1:
383 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
384 case MESA_FORMAT_SRGBA_DXT3:
385 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
386 case MESA_FORMAT_SRGBA_DXT5:
387 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
388 #endif
389 #endif
390
391 case MESA_FORMAT_RED_RGTC1:
392 return GL_COMPRESSED_RED_RGTC1;
393 case MESA_FORMAT_SIGNED_RED_RGTC1:
394 return GL_COMPRESSED_SIGNED_RED_RGTC1;
395 case MESA_FORMAT_RG_RGTC2:
396 return GL_COMPRESSED_RG_RGTC2;
397 case MESA_FORMAT_SIGNED_RG_RGTC2:
398 return GL_COMPRESSED_SIGNED_RG_RGTC2;
399
400 case MESA_FORMAT_L_LATC1:
401 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
402 case MESA_FORMAT_SIGNED_L_LATC1:
403 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
404 case MESA_FORMAT_LA_LATC2:
405 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
406 case MESA_FORMAT_SIGNED_LA_LATC2:
407 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
408
409 default:
410 _mesa_problem(ctx, "Unexpected mesa texture format in"
411 " _mesa_compressed_format_to_glenum()");
412 return 0;
413 }
414 }
415
416
417 /*
418 * Return the address of the pixel at (col, row, img) in a
419 * compressed texture image.
420 * \param col, row, img - image position (3D), should be a multiple of the
421 * format's block size.
422 * \param format - compressed image format
423 * \param width - image width (stride) in pixels
424 * \param image - the image address
425 * \return address of pixel at (row, col, img)
426 */
427 GLubyte *
428 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
429 gl_format mesaFormat,
430 GLsizei width, const GLubyte *image)
431 {
432 /* XXX only 2D images implemented, not 3D */
433 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
434 GLuint bw, bh;
435 GLint offset;
436
437 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
438
439 ASSERT(col % bw == 0);
440 ASSERT(row % bh == 0);
441
442 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
443 offset *= blockSize;
444
445 return (GLubyte *) image + offset;
446 }
447
448
449 /**
450 * Decompress a compressed texture image, returning a GL_RGBA/GL_FLOAT image.
451 */
452 void
453 _mesa_decompress_image(gl_format format, GLuint width, GLuint height,
454 const GLubyte *src, GLint srcRowStride,
455 GLfloat *dest)
456 {
457 void (*fetch)(const struct swrast_texture_image *texImage,
458 GLint i, GLint j, GLint k, GLfloat *texel);
459 struct swrast_texture_image texImage; /* dummy teximage */
460 GLuint i, j;
461
462 /* setup dummy texture image info */
463 memset(&texImage, 0, sizeof(texImage));
464 texImage.Map = (void *) src;
465 texImage.RowStride = srcRowStride;
466
467 switch (format) {
468 /* DXT formats */
469 case MESA_FORMAT_RGB_DXT1:
470 fetch = _mesa_fetch_texel_2d_f_rgb_dxt1;
471 break;
472 case MESA_FORMAT_RGBA_DXT1:
473 fetch = _mesa_fetch_texel_2d_f_rgba_dxt1;
474 break;
475 case MESA_FORMAT_RGBA_DXT3:
476 fetch = _mesa_fetch_texel_2d_f_rgba_dxt3;
477 break;
478 case MESA_FORMAT_RGBA_DXT5:
479 fetch = _mesa_fetch_texel_2d_f_rgba_dxt5;
480 break;
481
482 /* FXT1 formats */
483 case MESA_FORMAT_RGB_FXT1:
484 fetch = _mesa_fetch_texel_2d_f_rgb_fxt1;
485 break;
486 case MESA_FORMAT_RGBA_FXT1:
487 fetch = _mesa_fetch_texel_2d_f_rgba_fxt1;
488 break;
489
490 /* Red/RG formats */
491 case MESA_FORMAT_RED_RGTC1:
492 fetch = _mesa_fetch_texel_2d_f_red_rgtc1;
493 break;
494 case MESA_FORMAT_SIGNED_RED_RGTC1:
495 fetch = _mesa_fetch_texel_2d_f_signed_red_rgtc1;
496 break;
497 case MESA_FORMAT_RG_RGTC2:
498 fetch = _mesa_fetch_texel_2d_f_rg_rgtc2;
499 break;
500 case MESA_FORMAT_SIGNED_RG_RGTC2:
501 fetch = _mesa_fetch_texel_2d_f_signed_rg_rgtc2;
502 break;
503
504 /* L/LA formats */
505 case MESA_FORMAT_L_LATC1:
506 fetch = _mesa_fetch_texel_2d_f_l_latc1;
507 break;
508 case MESA_FORMAT_SIGNED_L_LATC1:
509 fetch = _mesa_fetch_texel_2d_f_signed_l_latc1;
510 break;
511 case MESA_FORMAT_LA_LATC2:
512 fetch = _mesa_fetch_texel_2d_f_la_latc2;
513 break;
514 case MESA_FORMAT_SIGNED_LA_LATC2:
515 fetch = _mesa_fetch_texel_2d_f_signed_la_latc2;
516 break;
517
518 default:
519 _mesa_problem(NULL, "Unexpected format in _mesa_decompress_image()");
520 return;
521 }
522
523 for (j = 0; j < height; j++) {
524 for (i = 0; i < width; i++) {
525 fetch(&texImage, i, j, 0, dest);
526 dest += 4;
527 }
528 }
529 }