2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
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
43 #include "texcompress.h"
44 #include "texcompress_s3tc.h"
46 #include "format_unpack.h"
49 #if defined(_WIN32) || defined(WIN32)
50 #define DXTN_LIBNAME "dxtn.dll"
53 #elif defined(__DJGPP__)
54 #define DXTN_LIBNAME "dxtn.dxe"
56 #define DXTN_LIBNAME "libtxc_dxtn.so"
59 typedef void (*dxtFetchTexelFuncExt)( GLint srcRowstride, const GLubyte *pixdata, GLint col, GLint row, GLvoid *texelOut );
61 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1 = NULL;
62 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1 = NULL;
63 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3 = NULL;
64 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5 = NULL;
66 typedef void (*dxtCompressTexFuncExt)(GLint srccomps, GLint width,
67 GLint height, const GLubyte *srcPixData,
68 GLenum destformat, GLubyte *dest,
71 static dxtCompressTexFuncExt ext_tx_compress_dxtn = NULL;
73 static void *dxtlibhandle = NULL;
77 _mesa_init_texture_s3tc( struct gl_context *ctx )
79 /* called during context initialization */
80 ctx->Mesa_DXTn = GL_FALSE;
81 #if USE_EXTERNAL_DXTN_LIB
83 dxtlibhandle = _mesa_dlopen(DXTN_LIBNAME, 0);
85 _mesa_warning(ctx, "couldn't open " DXTN_LIBNAME ", software DXTn "
86 "compression/decompression unavailable");
89 /* the fetch functions are not per context! Might be problematic... */
90 fetch_ext_rgb_dxt1 = (dxtFetchTexelFuncExt)
91 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgb_dxt1");
92 fetch_ext_rgba_dxt1 = (dxtFetchTexelFuncExt)
93 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt1");
94 fetch_ext_rgba_dxt3 = (dxtFetchTexelFuncExt)
95 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt3");
96 fetch_ext_rgba_dxt5 = (dxtFetchTexelFuncExt)
97 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt5");
98 ext_tx_compress_dxtn = (dxtCompressTexFuncExt)
99 _mesa_dlsym(dxtlibhandle, "tx_compress_dxtn");
101 if (!fetch_ext_rgb_dxt1 ||
102 !fetch_ext_rgba_dxt1 ||
103 !fetch_ext_rgba_dxt3 ||
104 !fetch_ext_rgba_dxt5 ||
105 !ext_tx_compress_dxtn) {
106 _mesa_warning(ctx, "couldn't reference all symbols in "
107 DXTN_LIBNAME ", software DXTn compression/decompression "
109 fetch_ext_rgb_dxt1 = NULL;
110 fetch_ext_rgba_dxt1 = NULL;
111 fetch_ext_rgba_dxt3 = NULL;
112 fetch_ext_rgba_dxt5 = NULL;
113 ext_tx_compress_dxtn = NULL;
114 _mesa_dlclose(dxtlibhandle);
120 ctx->Mesa_DXTn = GL_TRUE;
128 * Store user's image in rgb_dxt1 format.
131 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS)
133 const GLubyte *pixels;
135 const GLubyte *tempImage = NULL;
137 ASSERT(dstFormat == MESA_FORMAT_RGB_DXT1 ||
138 dstFormat == MESA_FORMAT_SRGB_DXT1);
140 if (srcFormat != GL_RGB ||
141 srcType != GL_UNSIGNED_BYTE ||
142 ctx->_ImageTransferState ||
143 srcPacking->RowLength != srcWidth ||
144 srcPacking->SwapBytes) {
145 /* convert image to RGB/GLubyte */
146 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
148 _mesa_get_format_base_format(dstFormat),
149 srcWidth, srcHeight, srcDepth,
150 srcFormat, srcType, srcAddr,
153 return GL_FALSE; /* out of memory */
158 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
159 srcFormat, srcType, 0, 0);
164 if (ext_tx_compress_dxtn) {
165 (*ext_tx_compress_dxtn)(3, srcWidth, srcHeight, pixels,
166 GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
170 _mesa_warning(ctx, "external dxt library not available: texstore_rgb_dxt1");
173 free((void *) tempImage);
180 * Store user's image in rgba_dxt1 format.
183 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS)
185 const GLubyte *pixels;
187 const GLubyte *tempImage = NULL;
189 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT1 ||
190 dstFormat == MESA_FORMAT_SRGBA_DXT1);
192 if (srcFormat != GL_RGBA ||
193 srcType != GL_UNSIGNED_BYTE ||
194 ctx->_ImageTransferState ||
195 srcPacking->RowLength != srcWidth ||
196 srcPacking->SwapBytes) {
197 /* convert image to RGBA/GLubyte */
198 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
200 _mesa_get_format_base_format(dstFormat),
201 srcWidth, srcHeight, srcDepth,
202 srcFormat, srcType, srcAddr,
205 return GL_FALSE; /* out of memory */
210 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
211 srcFormat, srcType, 0, 0);
216 if (ext_tx_compress_dxtn) {
217 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
218 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
222 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt1");
225 free((void*) tempImage);
232 * Store user's image in rgba_dxt3 format.
235 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS)
237 const GLubyte *pixels;
239 const GLubyte *tempImage = NULL;
241 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT3 ||
242 dstFormat == MESA_FORMAT_SRGBA_DXT3);
244 if (srcFormat != GL_RGBA ||
245 srcType != GL_UNSIGNED_BYTE ||
246 ctx->_ImageTransferState ||
247 srcPacking->RowLength != srcWidth ||
248 srcPacking->SwapBytes) {
249 /* convert image to RGBA/GLubyte */
250 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
252 _mesa_get_format_base_format(dstFormat),
253 srcWidth, srcHeight, srcDepth,
254 srcFormat, srcType, srcAddr,
257 return GL_FALSE; /* out of memory */
261 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
262 srcFormat, srcType, 0, 0);
267 if (ext_tx_compress_dxtn) {
268 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
269 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
273 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt3");
276 free((void *) tempImage);
283 * Store user's image in rgba_dxt5 format.
286 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS)
288 const GLubyte *pixels;
290 const GLubyte *tempImage = NULL;
292 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT5 ||
293 dstFormat == MESA_FORMAT_SRGBA_DXT5);
295 if (srcFormat != GL_RGBA ||
296 srcType != GL_UNSIGNED_BYTE ||
297 ctx->_ImageTransferState ||
298 srcPacking->RowLength != srcWidth ||
299 srcPacking->SwapBytes) {
300 /* convert image to RGBA/GLubyte */
301 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
303 _mesa_get_format_base_format(dstFormat),
304 srcWidth, srcHeight, srcDepth,
305 srcFormat, srcType, srcAddr,
308 return GL_FALSE; /* out of memory */
312 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
313 srcFormat, srcType, 0, 0);
318 if (ext_tx_compress_dxtn) {
319 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
320 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
324 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt5");
327 free((void *) tempImage);
333 /** Report problem with dxt texture decompression, once */
335 problem(const char *func)
337 static GLboolean warned = GL_FALSE;
339 _mesa_debug(NULL, "attempted to decode DXT texture without "
340 "library available: %s\n", func);
347 fetch_rgb_dxt1(const GLubyte *map,
348 GLint rowStride, GLint i, GLint j, GLfloat *texel)
350 if (fetch_ext_rgb_dxt1) {
352 fetch_ext_rgb_dxt1(rowStride, map, i, j, tex);
353 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
354 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
355 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
356 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
364 fetch_rgba_dxt1(const GLubyte *map,
365 GLint rowStride, GLint i, GLint j, GLfloat *texel)
367 if (fetch_ext_rgba_dxt1) {
369 fetch_ext_rgba_dxt1(rowStride, map, i, j, tex);
370 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
371 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
372 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
373 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
376 problem("rgba_dxt1");
381 fetch_rgba_dxt3(const GLubyte *map,
382 GLint rowStride, GLint i, GLint j, GLfloat *texel)
384 if (fetch_ext_rgba_dxt3) {
386 fetch_ext_rgba_dxt3(rowStride, map, i, j, tex);
387 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
388 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
389 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
390 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
393 problem("rgba_dxt3");
398 fetch_rgba_dxt5(const GLubyte *map,
399 GLint rowStride, GLint i, GLint j, GLfloat *texel)
401 if (fetch_ext_rgba_dxt5) {
403 fetch_ext_rgba_dxt5(rowStride, map, i, j, tex);
404 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
405 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
406 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
407 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
410 problem("rgba_dxt5");
416 fetch_srgb_dxt1(const GLubyte *map,
417 GLint rowStride, GLint i, GLint j, GLfloat *texel)
419 if (fetch_ext_rgb_dxt1) {
421 fetch_ext_rgb_dxt1(rowStride, map, i, j, tex);
422 texel[RCOMP] = _mesa_nonlinear_to_linear(tex[RCOMP]);
423 texel[GCOMP] = _mesa_nonlinear_to_linear(tex[GCOMP]);
424 texel[BCOMP] = _mesa_nonlinear_to_linear(tex[BCOMP]);
425 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
428 problem("srgb_dxt1");
433 fetch_srgba_dxt1(const GLubyte *map,
434 GLint rowStride, GLint i, GLint j, GLfloat *texel)
436 if (fetch_ext_rgba_dxt1) {
438 fetch_ext_rgba_dxt1(rowStride, map, i, j, tex);
439 texel[RCOMP] = _mesa_nonlinear_to_linear(tex[RCOMP]);
440 texel[GCOMP] = _mesa_nonlinear_to_linear(tex[GCOMP]);
441 texel[BCOMP] = _mesa_nonlinear_to_linear(tex[BCOMP]);
442 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
445 problem("srgba_dxt1");
450 fetch_srgba_dxt3(const GLubyte *map,
451 GLint rowStride, GLint i, GLint j, GLfloat *texel)
453 if (fetch_ext_rgba_dxt3) {
455 fetch_ext_rgba_dxt3(rowStride, map, i, j, tex);
456 texel[RCOMP] = _mesa_nonlinear_to_linear(tex[RCOMP]);
457 texel[GCOMP] = _mesa_nonlinear_to_linear(tex[GCOMP]);
458 texel[BCOMP] = _mesa_nonlinear_to_linear(tex[BCOMP]);
459 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
462 problem("srgba_dxt3");
467 fetch_srgba_dxt5(const GLubyte *map,
468 GLint rowStride, GLint i, GLint j, GLfloat *texel)
470 if (fetch_ext_rgba_dxt5) {
472 fetch_ext_rgba_dxt5(rowStride, map, i, j, tex);
473 texel[RCOMP] = _mesa_nonlinear_to_linear(tex[RCOMP]);
474 texel[GCOMP] = _mesa_nonlinear_to_linear(tex[GCOMP]);
475 texel[BCOMP] = _mesa_nonlinear_to_linear(tex[BCOMP]);
476 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
479 problem("srgba_dxt5");
485 compressed_fetch_func
486 _mesa_get_dxt_fetch_func(gl_format format)
489 case MESA_FORMAT_RGB_DXT1:
490 return fetch_rgb_dxt1;
491 case MESA_FORMAT_RGBA_DXT1:
492 return fetch_rgba_dxt1;
493 case MESA_FORMAT_RGBA_DXT3:
494 return fetch_rgba_dxt3;
495 case MESA_FORMAT_RGBA_DXT5:
496 return fetch_rgba_dxt5;
497 case MESA_FORMAT_SRGB_DXT1:
498 return fetch_srgb_dxt1;
499 case MESA_FORMAT_SRGBA_DXT1:
500 return fetch_srgba_dxt1;
501 case MESA_FORMAT_SRGBA_DXT3:
502 return fetch_srgba_dxt3;
503 case MESA_FORMAT_SRGBA_DXT5:
504 return fetch_srgba_dxt5;