2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008 VMware, Inc.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
28 * \file texcompress_s3tc.c
29 * GL_EXT_texture_compression_s3tc support.
32 #ifndef USE_EXTERNAL_DXTN_LIB
33 #define USE_EXTERNAL_DXTN_LIB 1
42 #include "mfeatures.h"
44 #include "texcompress.h"
45 #include "texcompress_s3tc.h"
49 #if FEATURE_texture_s3tc
52 #if defined(_WIN32) || defined(WIN32)
53 #define DXTN_LIBNAME "dxtn.dll"
56 #elif defined(__DJGPP__)
57 #define DXTN_LIBNAME "dxtn.dxe"
59 #define DXTN_LIBNAME "libtxc_dxtn.so"
62 #if FEATURE_EXT_texture_sRGB
64 * Convert an 8-bit sRGB value from non-linear space to a
65 * linear RGB value in [0, 1].
66 * Implemented with a 256-entry lookup table.
69 nonlinear_to_linear(GLubyte cs8)
71 static GLfloat table[256];
72 static GLboolean tableReady = GL_FALSE;
74 /* compute lookup table now */
76 for (i = 0; i < 256; i++) {
77 const GLfloat cs = UBYTE_TO_FLOAT(i);
79 table[i] = cs / 12.92f;
82 table[i] = (GLfloat) pow((cs + 0.055) / 1.055, 2.4);
89 #endif /* FEATURE_EXT_texture_sRGB */
91 typedef void (*dxtFetchTexelFuncExt)( GLint srcRowstride, GLubyte *pixdata, GLint col, GLint row, GLvoid *texelOut );
93 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1 = NULL;
94 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1 = NULL;
95 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3 = NULL;
96 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5 = NULL;
98 typedef void (*dxtCompressTexFuncExt)(GLint srccomps, GLint width,
99 GLint height, const GLchan *srcPixData,
100 GLenum destformat, GLubyte *dest,
103 static dxtCompressTexFuncExt ext_tx_compress_dxtn = NULL;
105 static void *dxtlibhandle = NULL;
109 _mesa_init_texture_s3tc( struct gl_context *ctx )
111 /* called during context initialization */
112 ctx->Mesa_DXTn = GL_FALSE;
113 #if USE_EXTERNAL_DXTN_LIB
115 dxtlibhandle = _mesa_dlopen(DXTN_LIBNAME, 0);
117 _mesa_warning(ctx, "couldn't open " DXTN_LIBNAME ", software DXTn "
118 "compression/decompression unavailable");
121 /* the fetch functions are not per context! Might be problematic... */
122 fetch_ext_rgb_dxt1 = (dxtFetchTexelFuncExt)
123 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgb_dxt1");
124 fetch_ext_rgba_dxt1 = (dxtFetchTexelFuncExt)
125 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt1");
126 fetch_ext_rgba_dxt3 = (dxtFetchTexelFuncExt)
127 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt3");
128 fetch_ext_rgba_dxt5 = (dxtFetchTexelFuncExt)
129 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt5");
130 ext_tx_compress_dxtn = (dxtCompressTexFuncExt)
131 _mesa_dlsym(dxtlibhandle, "tx_compress_dxtn");
133 if (!fetch_ext_rgb_dxt1 ||
134 !fetch_ext_rgba_dxt1 ||
135 !fetch_ext_rgba_dxt3 ||
136 !fetch_ext_rgba_dxt5 ||
137 !ext_tx_compress_dxtn) {
138 _mesa_warning(ctx, "couldn't reference all symbols in "
139 DXTN_LIBNAME ", software DXTn compression/decompression "
141 fetch_ext_rgb_dxt1 = NULL;
142 fetch_ext_rgba_dxt1 = NULL;
143 fetch_ext_rgba_dxt3 = NULL;
144 fetch_ext_rgba_dxt5 = NULL;
145 ext_tx_compress_dxtn = NULL;
146 _mesa_dlclose(dxtlibhandle);
152 ctx->Mesa_DXTn = GL_TRUE;
160 * Store user's image in rgb_dxt1 format.
163 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS)
165 const GLchan *pixels;
167 const GLint texWidth = dstRowStride * 4 / 8; /* a bit of a hack */
168 const GLchan *tempImage = NULL;
170 ASSERT(dstFormat == MESA_FORMAT_RGB_DXT1 ||
171 dstFormat == MESA_FORMAT_SRGB_DXT1);
172 ASSERT(dstXoffset % 4 == 0);
173 ASSERT(dstYoffset % 4 == 0);
174 ASSERT(dstZoffset % 4 == 0);
176 (void) dstImageOffsets;
178 if (srcFormat != GL_RGB ||
179 srcType != CHAN_TYPE ||
180 ctx->_ImageTransferState ||
181 srcPacking->SwapBytes) {
182 /* convert image to RGB/GLchan */
183 tempImage = _mesa_make_temp_chan_image(ctx, dims,
185 _mesa_get_format_base_format(dstFormat),
186 srcWidth, srcHeight, srcDepth,
187 srcFormat, srcType, srcAddr,
190 return GL_FALSE; /* out of memory */
195 pixels = (const GLchan *) srcAddr;
198 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
200 texWidth, (GLubyte *) dstAddr);
202 if (ext_tx_compress_dxtn) {
203 (*ext_tx_compress_dxtn)(3, srcWidth, srcHeight, pixels,
204 GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
208 _mesa_warning(ctx, "external dxt library not available: texstore_rgb_dxt1");
212 free((void *) tempImage);
219 * Store user's image in rgba_dxt1 format.
222 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS)
224 const GLchan *pixels;
226 const GLint texWidth = dstRowStride * 4 / 8; /* a bit of a hack */
227 const GLchan *tempImage = NULL;
229 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT1 ||
230 dstFormat == MESA_FORMAT_SRGBA_DXT1);
231 ASSERT(dstXoffset % 4 == 0);
232 ASSERT(dstYoffset % 4 == 0);
233 ASSERT(dstZoffset % 4 == 0);
235 (void) dstImageOffsets;
237 if (srcFormat != GL_RGBA ||
238 srcType != CHAN_TYPE ||
239 ctx->_ImageTransferState ||
240 srcPacking->SwapBytes) {
241 /* convert image to RGBA/GLchan */
242 tempImage = _mesa_make_temp_chan_image(ctx, dims,
244 _mesa_get_format_base_format(dstFormat),
245 srcWidth, srcHeight, srcDepth,
246 srcFormat, srcType, srcAddr,
249 return GL_FALSE; /* out of memory */
254 pixels = (const GLchan *) srcAddr;
257 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
259 texWidth, (GLubyte *) dstAddr);
260 if (ext_tx_compress_dxtn) {
261 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
262 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
266 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt1");
270 free((void*) tempImage);
277 * Store user's image in rgba_dxt3 format.
280 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS)
282 const GLchan *pixels;
284 const GLint texWidth = dstRowStride * 4 / 16; /* a bit of a hack */
285 const GLchan *tempImage = NULL;
287 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT3 ||
288 dstFormat == MESA_FORMAT_SRGBA_DXT3);
289 ASSERT(dstXoffset % 4 == 0);
290 ASSERT(dstYoffset % 4 == 0);
291 ASSERT(dstZoffset % 4 == 0);
293 (void) dstImageOffsets;
295 if (srcFormat != GL_RGBA ||
296 srcType != CHAN_TYPE ||
297 ctx->_ImageTransferState ||
298 srcPacking->SwapBytes) {
299 /* convert image to RGBA/GLchan */
300 tempImage = _mesa_make_temp_chan_image(ctx, dims,
302 _mesa_get_format_base_format(dstFormat),
303 srcWidth, srcHeight, srcDepth,
304 srcFormat, srcType, srcAddr,
307 return GL_FALSE; /* out of memory */
311 pixels = (const GLchan *) srcAddr;
314 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
316 texWidth, (GLubyte *) dstAddr);
317 if (ext_tx_compress_dxtn) {
318 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
319 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
323 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt3");
327 free((void *) tempImage);
334 * Store user's image in rgba_dxt5 format.
337 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS)
339 const GLchan *pixels;
341 const GLint texWidth = dstRowStride * 4 / 16; /* a bit of a hack */
342 const GLchan *tempImage = NULL;
344 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT5 ||
345 dstFormat == MESA_FORMAT_SRGBA_DXT5);
346 ASSERT(dstXoffset % 4 == 0);
347 ASSERT(dstYoffset % 4 == 0);
348 ASSERT(dstZoffset % 4 == 0);
350 (void) dstImageOffsets;
352 if (srcFormat != GL_RGBA ||
353 srcType != CHAN_TYPE ||
354 ctx->_ImageTransferState ||
355 srcPacking->SwapBytes) {
356 /* convert image to RGBA/GLchan */
357 tempImage = _mesa_make_temp_chan_image(ctx, dims,
359 _mesa_get_format_base_format(dstFormat),
360 srcWidth, srcHeight, srcDepth,
361 srcFormat, srcType, srcAddr,
364 return GL_FALSE; /* out of memory */
368 pixels = (const GLchan *) srcAddr;
371 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
373 texWidth, (GLubyte *) dstAddr);
374 if (ext_tx_compress_dxtn) {
375 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
376 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
380 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt5");
384 free((void *) tempImage);
391 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image *texImage,
392 GLint i, GLint j, GLint k, GLchan *texel )
395 if (fetch_ext_rgb_dxt1) {
396 ASSERT (sizeof(GLchan) == sizeof(GLubyte));
397 fetch_ext_rgb_dxt1(texImage->RowStride,
398 (GLubyte *)(texImage)->Data, i, j, texel);
401 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
406 _mesa_fetch_texel_2d_f_rgb_dxt1(const struct gl_texture_image *texImage,
407 GLint i, GLint j, GLint k, GLfloat *texel)
409 /* just sample as GLchan and convert to float here */
411 fetch_texel_2d_rgb_dxt1(texImage, i, j, k, rgba);
412 texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
413 texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
414 texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
415 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
420 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image *texImage,
421 GLint i, GLint j, GLint k, GLchan *texel )
424 if (fetch_ext_rgba_dxt1) {
425 fetch_ext_rgba_dxt1(texImage->RowStride,
426 (GLubyte *)(texImage)->Data, i, j, texel);
429 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
434 _mesa_fetch_texel_2d_f_rgba_dxt1(const struct gl_texture_image *texImage,
435 GLint i, GLint j, GLint k, GLfloat *texel)
437 /* just sample as GLchan and convert to float here */
439 fetch_texel_2d_rgba_dxt1(texImage, i, j, k, rgba);
440 texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
441 texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
442 texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
443 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
448 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image *texImage,
449 GLint i, GLint j, GLint k, GLchan *texel )
452 if (fetch_ext_rgba_dxt3) {
453 ASSERT (sizeof(GLchan) == sizeof(GLubyte));
454 fetch_ext_rgba_dxt3(texImage->RowStride, (GLubyte *)(texImage)->Data,
458 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
463 _mesa_fetch_texel_2d_f_rgba_dxt3(const struct gl_texture_image *texImage,
464 GLint i, GLint j, GLint k, GLfloat *texel)
466 /* just sample as GLchan and convert to float here */
468 fetch_texel_2d_rgba_dxt3(texImage, i, j, k, rgba);
469 texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
470 texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
471 texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
472 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
477 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image *texImage,
478 GLint i, GLint j, GLint k, GLchan *texel )
481 if (fetch_ext_rgba_dxt5) {
482 fetch_ext_rgba_dxt5(texImage->RowStride, (GLubyte *)(texImage)->Data,
486 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
491 _mesa_fetch_texel_2d_f_rgba_dxt5(const struct gl_texture_image *texImage,
492 GLint i, GLint j, GLint k, GLfloat *texel)
494 /* just sample as GLchan and convert to float here */
496 fetch_texel_2d_rgba_dxt5(texImage, i, j, k, rgba);
497 texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
498 texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
499 texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
500 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
503 #if FEATURE_EXT_texture_sRGB
505 _mesa_fetch_texel_2d_f_srgb_dxt1( const struct gl_texture_image *texImage,
506 GLint i, GLint j, GLint k, GLfloat *texel )
508 /* just sample as GLchan and convert to float here */
510 fetch_texel_2d_rgb_dxt1(texImage, i, j, k, rgba);
511 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
512 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
513 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
514 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
518 _mesa_fetch_texel_2d_f_srgba_dxt1(const struct gl_texture_image *texImage,
519 GLint i, GLint j, GLint k, GLfloat *texel)
521 /* just sample as GLchan and convert to float here */
523 fetch_texel_2d_rgba_dxt1(texImage, i, j, k, rgba);
524 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
525 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
526 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
527 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
531 _mesa_fetch_texel_2d_f_srgba_dxt3(const struct gl_texture_image *texImage,
532 GLint i, GLint j, GLint k, GLfloat *texel)
534 /* just sample as GLchan and convert to float here */
536 fetch_texel_2d_rgba_dxt3(texImage, i, j, k, rgba);
537 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
538 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
539 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
540 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
544 _mesa_fetch_texel_2d_f_srgba_dxt5(const struct gl_texture_image *texImage,
545 GLint i, GLint j, GLint k, GLfloat *texel)
547 /* just sample as GLchan and convert to float here */
549 fetch_texel_2d_rgba_dxt5(texImage, i, j, k, rgba);
550 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
551 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
552 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
553 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
555 #endif /* FEATURE_EXT_texture_sRGB */
558 #endif /* FEATURE_texture_s3tc */