2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * \file texcompress_s3tc.c
28 * GL_EXT_texture_compression_s3tc support.
31 #ifndef USE_EXTERNAL_DXTN_LIB
32 #define USE_EXTERNAL_DXTN_LIB 0
41 #include "texcompress.h"
42 #include "texformat.h"
45 #if USE_EXTERNAL_DXTN_LIB
48 #define DXTN_EXT "dxtn.dll"
49 #define DXTN_PREFIX ""
50 #define dlopen(name, mode) LoadLibrary(name)
51 #define dlsym(hndl, proc) GetProcAddress(hndl, proc)
52 #define dlclose(hndl) FreeLibrary(hndl)
53 #elif defined(__DJGPP__)
54 /* has dlopen, but doesn't like the names */
56 #define DXTN_EXT "dxtn.dxe"
57 #define DXTN_PREFIX "_"
61 #define DXTN_EXT "libtxc_dxtn.so"
62 #define DXTN_PREFIX ""
64 #endif /* USE_EXTERNAL_DXTN_LIB */
66 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
67 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
68 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
69 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
70 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
72 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
73 GLint height
, const GLchan
*srcPixData
,
74 GLenum destformat
, GLubyte
*dest
,
76 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
78 static void *dxtlibhandle
= NULL
;
82 _mesa_init_texture_s3tc( GLcontext
*ctx
)
84 /* called during context initialization */
85 ctx
->Mesa_DXTn
= GL_FALSE
;
86 #if USE_EXTERNAL_DXTN_LIB
88 dxtlibhandle
= dlopen (DXTN_EXT
, RTLD_LAZY
| RTLD_GLOBAL
);
90 _mesa_warning(ctx
, "couldn't open " DXTN_EXT
", software DXTn "
91 "compression/decompression unavailable");
94 /* the fetch functions are not per context! Might be problematic... */
95 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"fetch_2d_texel_rgb_dxt1");
96 if (fetch_ext_rgb_dxt1
!= NULL
) {
97 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"fetch_2d_texel_rgba_dxt1");
99 if (fetch_ext_rgba_dxt1
!= NULL
) {
100 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"fetch_2d_texel_rgba_dxt3");
102 if (fetch_ext_rgba_dxt3
!= NULL
) {
103 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"fetch_2d_texel_rgba_dxt5");
105 if (fetch_ext_rgba_dxt5
!= NULL
) {
106 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"tx_compress_dxtn");
109 if (ext_tx_compress_dxtn
== NULL
) {
110 _mesa_warning(ctx
, "couldn't reference all symbols in "
111 DXTN_EXT
", software DXTn compression/decompression "
113 fetch_ext_rgb_dxt1
= NULL
;
114 fetch_ext_rgba_dxt1
= NULL
;
115 fetch_ext_rgba_dxt3
= NULL
;
116 fetch_ext_rgba_dxt5
= NULL
;
117 ext_tx_compress_dxtn
= NULL
;
118 dlclose(dxtlibhandle
);
124 ctx
->Mesa_DXTn
= GL_TRUE
;
125 _mesa_warning(ctx
, "software DXTn compression/decompression available");
133 * Called via TexFormat->StoreImage to store an RGB_DXT1 texture.
136 texstore_rgb_dxt1(TEXSTORE_PARAMS
)
138 const GLchan
*pixels
;
141 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
142 const GLchan
*tempImage
= NULL
;
144 ASSERT(dstFormat
== &_mesa_texformat_rgb_dxt1
);
145 ASSERT(dstXoffset
% 4 == 0);
146 ASSERT(dstYoffset
% 4 == 0);
147 ASSERT(dstZoffset
% 4 == 0);
149 (void) dstImageOffsets
;
151 if (srcFormat
!= GL_RGB
||
152 srcType
!= CHAN_TYPE
||
153 ctx
->_ImageTransferState
||
154 srcPacking
->SwapBytes
) {
155 /* convert image to RGB/GLchan */
156 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
158 dstFormat
->BaseFormat
,
159 srcWidth
, srcHeight
, srcDepth
,
160 srcFormat
, srcType
, srcAddr
,
163 return GL_FALSE
; /* out of memory */
164 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
166 srcRowStride
= 3 * srcWidth
;
170 pixels
= (const GLchan
*) srcAddr
;
171 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
172 srcType
) / sizeof(GLchan
);
175 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
176 dstFormat
->MesaFormat
,
177 texWidth
, (GLubyte
*) dstAddr
);
179 if (ext_tx_compress_dxtn
) {
180 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
181 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
185 _mesa_problem(ctx
, "external dxt library not available");
189 _mesa_free((void *) tempImage
);
196 * Called via TexFormat->StoreImage to store an RGBA_DXT1 texture.
199 texstore_rgba_dxt1(TEXSTORE_PARAMS
)
201 const GLchan
*pixels
;
204 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
205 const GLchan
*tempImage
= NULL
;
207 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt1
);
208 ASSERT(dstXoffset
% 4 == 0);
209 ASSERT(dstYoffset
% 4 == 0);
210 ASSERT(dstZoffset
% 4 == 0);
212 (void) dstImageOffsets
;
214 if (srcFormat
!= GL_RGBA
||
215 srcType
!= CHAN_TYPE
||
216 ctx
->_ImageTransferState
||
217 srcPacking
->SwapBytes
) {
218 /* convert image to RGBA/GLchan */
219 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
221 dstFormat
->BaseFormat
,
222 srcWidth
, srcHeight
, srcDepth
,
223 srcFormat
, srcType
, srcAddr
,
226 return GL_FALSE
; /* out of memory */
227 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
229 srcRowStride
= 4 * srcWidth
;
233 pixels
= (const GLchan
*) srcAddr
;
234 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
235 srcType
) / sizeof(GLchan
);
238 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
239 dstFormat
->MesaFormat
,
240 texWidth
, (GLubyte
*) dstAddr
);
241 if (ext_tx_compress_dxtn
) {
242 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
243 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
247 _mesa_problem(ctx
, "external dxt library not available");
251 _mesa_free((void*) tempImage
);
258 * Called via TexFormat->StoreImage to store an RGBA_DXT3 texture.
261 texstore_rgba_dxt3(TEXSTORE_PARAMS
)
263 const GLchan
*pixels
;
266 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
267 const GLchan
*tempImage
= NULL
;
269 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt3
);
270 ASSERT(dstXoffset
% 4 == 0);
271 ASSERT(dstYoffset
% 4 == 0);
272 ASSERT(dstZoffset
% 4 == 0);
274 (void) dstImageOffsets
;
276 if (srcFormat
!= GL_RGBA
||
277 srcType
!= CHAN_TYPE
||
278 ctx
->_ImageTransferState
||
279 srcPacking
->SwapBytes
) {
280 /* convert image to RGBA/GLchan */
281 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
283 dstFormat
->BaseFormat
,
284 srcWidth
, srcHeight
, srcDepth
,
285 srcFormat
, srcType
, srcAddr
,
288 return GL_FALSE
; /* out of memory */
289 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
291 srcRowStride
= 4 * srcWidth
;
294 pixels
= (const GLchan
*) srcAddr
;
295 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
296 srcType
) / sizeof(GLchan
);
299 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
300 dstFormat
->MesaFormat
,
301 texWidth
, (GLubyte
*) dstAddr
);
302 if (ext_tx_compress_dxtn
) {
303 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
304 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
308 _mesa_problem(ctx
, "external dxt library not available");
312 _mesa_free((void *) tempImage
);
319 * Called via TexFormat->StoreImage to store an RGBA_DXT5 texture.
322 texstore_rgba_dxt5(TEXSTORE_PARAMS
)
324 const GLchan
*pixels
;
327 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
328 const GLchan
*tempImage
= NULL
;
330 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt5
);
331 ASSERT(dstXoffset
% 4 == 0);
332 ASSERT(dstYoffset
% 4 == 0);
333 ASSERT(dstZoffset
% 4 == 0);
335 (void) dstImageOffsets
;
337 if (srcFormat
!= GL_RGBA
||
338 srcType
!= CHAN_TYPE
||
339 ctx
->_ImageTransferState
||
340 srcPacking
->SwapBytes
) {
341 /* convert image to RGBA/GLchan */
342 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
344 dstFormat
->BaseFormat
,
345 srcWidth
, srcHeight
, srcDepth
,
346 srcFormat
, srcType
, srcAddr
,
349 return GL_FALSE
; /* out of memory */
350 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
352 srcRowStride
= 4 * srcWidth
;
355 pixels
= (const GLchan
*) srcAddr
;
356 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
357 srcType
) / sizeof(GLchan
);
360 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
361 dstFormat
->MesaFormat
,
362 texWidth
, (GLubyte
*) dstAddr
);
363 if (ext_tx_compress_dxtn
) {
364 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
365 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
369 _mesa_problem(ctx
, "external dxt library not available");
373 _mesa_free((void *) tempImage
);
380 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
381 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
384 if (fetch_ext_rgb_dxt1
) {
385 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
386 fetch_ext_rgb_dxt1(texImage
->RowStride
,
387 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
390 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
395 fetch_texel_2d_f_rgb_dxt1( const struct gl_texture_image
*texImage
,
396 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
398 /* just sample as GLchan and convert to float here */
400 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
401 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
402 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
403 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
404 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
409 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image
*texImage
,
410 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
413 if (fetch_ext_rgba_dxt1
) {
414 fetch_ext_rgba_dxt1(texImage
->RowStride
,
415 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
418 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
423 fetch_texel_2d_f_rgba_dxt1( const struct gl_texture_image
*texImage
,
424 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
426 /* just sample as GLchan and convert to float here */
428 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
429 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
430 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
431 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
432 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
437 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
438 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
441 if (fetch_ext_rgba_dxt3
) {
442 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
443 fetch_ext_rgba_dxt3(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
447 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
452 fetch_texel_2d_f_rgba_dxt3( const struct gl_texture_image
*texImage
,
453 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
455 /* just sample as GLchan and convert to float here */
457 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
458 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
459 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
460 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
461 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
466 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image
*texImage
,
467 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
470 if (fetch_ext_rgba_dxt5
) {
471 fetch_ext_rgba_dxt5(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
475 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
480 fetch_texel_2d_f_rgba_dxt5( const struct gl_texture_image
*texImage
,
481 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
483 /* just sample as GLchan and convert to float here */
485 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
486 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
487 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
488 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
489 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
493 const struct gl_texture_format _mesa_texformat_rgb_dxt1
= {
494 MESA_FORMAT_RGB_DXT1
, /* MesaFormat */
495 GL_RGB
, /* BaseFormat */
496 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
497 4, /*approx*/ /* RedBits */
498 4, /*approx*/ /* GreenBits */
499 4, /*approx*/ /* BlueBits */
501 0, /* LuminanceBits */
502 0, /* IntensityBits */
507 texstore_rgb_dxt1
, /* StoreTexImageFunc */
508 NULL
, /*impossible*/ /* FetchTexel1D */
509 fetch_texel_2d_rgb_dxt1
, /* FetchTexel2D */
510 NULL
, /*impossible*/ /* FetchTexel3D */
511 NULL
, /*impossible*/ /* FetchTexel1Df */
512 fetch_texel_2d_f_rgb_dxt1
, /* FetchTexel2Df */
513 NULL
, /*impossible*/ /* FetchTexel3Df */
514 NULL
/* StoreTexel */
517 const struct gl_texture_format _mesa_texformat_rgba_dxt1
= {
518 MESA_FORMAT_RGBA_DXT1
, /* MesaFormat */
519 GL_RGBA
, /* BaseFormat */
520 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
521 4, /*approx*/ /* RedBits */
522 4, /*approx*/ /* GreenBits */
523 4, /*approx*/ /* BlueBits */
524 1, /*approx*/ /* AlphaBits */
525 0, /* LuminanceBits */
526 0, /* IntensityBits */
531 texstore_rgba_dxt1
, /* StoreTexImageFunc */
532 NULL
, /*impossible*/ /* FetchTexel1D */
533 fetch_texel_2d_rgba_dxt1
, /* FetchTexel2D */
534 NULL
, /*impossible*/ /* FetchTexel3D */
535 NULL
, /*impossible*/ /* FetchTexel1Df */
536 fetch_texel_2d_f_rgba_dxt1
, /* FetchTexel2Df */
537 NULL
, /*impossible*/ /* FetchTexel3Df */
538 NULL
/* StoreTexel */
541 const struct gl_texture_format _mesa_texformat_rgba_dxt3
= {
542 MESA_FORMAT_RGBA_DXT3
, /* MesaFormat */
543 GL_RGBA
, /* BaseFormat */
544 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
545 4, /*approx*/ /* RedBits */
546 4, /*approx*/ /* GreenBits */
547 4, /*approx*/ /* BlueBits */
548 4, /*approx*/ /* AlphaBits */
549 0, /* LuminanceBits */
550 0, /* IntensityBits */
555 texstore_rgba_dxt3
, /* StoreTexImageFunc */
556 NULL
, /*impossible*/ /* FetchTexel1D */
557 fetch_texel_2d_rgba_dxt3
, /* FetchTexel2D */
558 NULL
, /*impossible*/ /* FetchTexel3D */
559 NULL
, /*impossible*/ /* FetchTexel1Df */
560 fetch_texel_2d_f_rgba_dxt3
, /* FetchTexel2Df */
561 NULL
, /*impossible*/ /* FetchTexel3Df */
562 NULL
/* StoreTexel */
565 const struct gl_texture_format _mesa_texformat_rgba_dxt5
= {
566 MESA_FORMAT_RGBA_DXT5
, /* MesaFormat */
567 GL_RGBA
, /* BaseFormat */
568 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
569 4,/*approx*/ /* RedBits */
570 4,/*approx*/ /* GreenBits */
571 4,/*approx*/ /* BlueBits */
572 4,/*approx*/ /* AlphaBits */
573 0, /* LuminanceBits */
574 0, /* IntensityBits */
579 texstore_rgba_dxt5
, /* StoreTexImageFunc */
580 NULL
, /*impossible*/ /* FetchTexel1D */
581 fetch_texel_2d_rgba_dxt5
, /* FetchTexel2D */
582 NULL
, /*impossible*/ /* FetchTexel3D */
583 NULL
, /*impossible*/ /* FetchTexel1Df */
584 fetch_texel_2d_f_rgba_dxt5
, /* FetchTexel2Df */
585 NULL
, /*impossible*/ /* FetchTexel3Df */
586 NULL
/* StoreTexel */