2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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
, GLint height
, const GLubyte
*srcPixData
, GLenum destformat
, GLubyte
*dest
, GLint dstRowStride
);
73 dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
75 void *dxtlibhandle
= NULL
;
78 _mesa_init_texture_s3tc( GLcontext
*ctx
)
80 /* called during context initialization */
81 ctx
->Mesa_DXTn
= GL_FALSE
;
82 #if USE_EXTERNAL_DXTN_LIB
84 dxtlibhandle
= dlopen (DXTN_EXT
, RTLD_LAZY
| RTLD_GLOBAL
);
86 _mesa_warning(ctx
, "couldn't open " DXTN_EXT
", software DXTn "
87 "compression/decompression unavailable\n");
90 /* the fetch functions are not per context! Might be problematic... */
91 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"fetch_2d_texel_rgb_dxt1");
92 if (fetch_ext_rgb_dxt1
!= NULL
) {
93 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"fetch_2d_texel_rgba_dxt1");
95 if (fetch_ext_rgba_dxt1
!= NULL
) {
96 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"fetch_2d_texel_rgba_dxt3");
98 if (fetch_ext_rgba_dxt3
!= NULL
) {
99 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"fetch_2d_texel_rgba_dxt5");
101 if (fetch_ext_rgba_dxt5
!= NULL
) {
102 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)dlsym(dxtlibhandle
, DXTN_PREFIX
"tx_compress_dxtn");
105 if (ext_tx_compress_dxtn
== NULL
) {
106 _mesa_warning(ctx
, "couldn't reference all symbols in "
107 DXTN_EXT
", 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 dlclose(dxtlibhandle
);
120 ctx
->Mesa_DXTn
= GL_TRUE
;
121 _mesa_warning(ctx
, "software DXTn compression/decompression available\n");
129 * Called via TexFormat->StoreImage to store an RGB_DXT1 texture.
132 texstore_rgb_dxt1(STORE_PARAMS
)
134 const GLchan
*pixels
;
137 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
138 const GLchan
*tempImage
= NULL
;
140 ASSERT(dstFormat
== &_mesa_texformat_rgb_dxt1
);
141 ASSERT(dstXoffset
% 4 == 0);
142 ASSERT(dstYoffset
% 4 == 0);
143 ASSERT(dstZoffset
% 4 == 0);
144 (void) dstZoffset
; (void) dstImageStride
;
146 if (srcFormat
!= GL_RGB
||
147 srcType
!= CHAN_TYPE
||
148 ctx
->_ImageTransferState
||
149 srcPacking
->SwapBytes
) {
150 /* convert image to RGB/GLchan */
151 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
153 dstFormat
->BaseFormat
,
154 srcWidth
, srcHeight
, srcDepth
,
155 srcFormat
, srcType
, srcAddr
,
158 return GL_FALSE
; /* out of memory */
159 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
161 srcRowStride
= 3 * srcWidth
;
165 pixels
= (const GLchan
*) srcAddr
;
166 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
167 srcType
) / sizeof(GLchan
);
170 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
171 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
172 texWidth
, (GLubyte
*) dstAddr
);
174 if (ext_tx_compress_dxtn
) {
175 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGB_S3TC_DXT1_EXT
, dst
, dstRowStride
);
178 _mesa_problem(ctx
, "external dxt library not available");
182 _mesa_free((void *) tempImage
);
189 * Called via TexFormat->StoreImage to store an RGBA_DXT1 texture.
192 texstore_rgba_dxt1(STORE_PARAMS
)
194 const GLchan
*pixels
;
197 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
198 const GLchan
*tempImage
= NULL
;
200 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt1
);
201 ASSERT(dstXoffset
% 4 == 0);
202 ASSERT(dstYoffset
% 4 == 0);
203 ASSERT(dstZoffset
% 4 == 0);
204 (void) dstZoffset
; (void) dstImageStride
;
206 if (srcFormat
!= GL_RGBA
||
207 srcType
!= CHAN_TYPE
||
208 ctx
->_ImageTransferState
||
209 srcPacking
->SwapBytes
) {
210 /* convert image to RGBA/GLchan */
211 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
213 dstFormat
->BaseFormat
,
214 srcWidth
, srcHeight
, srcDepth
,
215 srcFormat
, srcType
, srcAddr
,
218 return GL_FALSE
; /* out of memory */
219 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
221 srcRowStride
= 4 * srcWidth
;
225 pixels
= (const GLchan
*) srcAddr
;
226 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
227 srcType
) / sizeof(GLchan
);
230 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
231 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
232 texWidth
, (GLubyte
*) dstAddr
);
233 if (ext_tx_compress_dxtn
) {
234 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
, dst
, dstRowStride
);
237 _mesa_problem(ctx
, "external dxt library not available");
241 _mesa_free((void*) tempImage
);
248 * Called via TexFormat->StoreImage to store an RGBA_DXT3 texture.
251 texstore_rgba_dxt3(STORE_PARAMS
)
253 const GLchan
*pixels
;
256 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
257 const GLchan
*tempImage
= NULL
;
259 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt3
);
260 ASSERT(dstXoffset
% 4 == 0);
261 ASSERT(dstYoffset
% 4 == 0);
262 ASSERT(dstZoffset
% 4 == 0);
263 (void) dstZoffset
; (void) dstImageStride
;
265 if (srcFormat
!= GL_RGBA
||
266 srcType
!= CHAN_TYPE
||
267 ctx
->_ImageTransferState
||
268 srcPacking
->SwapBytes
) {
269 /* convert image to RGBA/GLchan */
270 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
272 dstFormat
->BaseFormat
,
273 srcWidth
, srcHeight
, srcDepth
,
274 srcFormat
, srcType
, srcAddr
,
277 return GL_FALSE
; /* out of memory */
278 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
280 srcRowStride
= 4 * srcWidth
;
283 pixels
= (const GLchan
*) srcAddr
;
284 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
285 srcType
) / sizeof(GLchan
);
288 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
289 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
290 texWidth
, (GLubyte
*) dstAddr
);
291 if (ext_tx_compress_dxtn
) {
292 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
, dst
, dstRowStride
);
295 _mesa_problem(ctx
, "external dxt library not available");
299 _mesa_free((void *) tempImage
);
306 * Called via TexFormat->StoreImage to store an RGBA_DXT5 texture.
309 texstore_rgba_dxt5(STORE_PARAMS
)
311 const GLchan
*pixels
;
314 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
315 const GLchan
*tempImage
= NULL
;
317 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt5
);
318 ASSERT(dstXoffset
% 4 == 0);
319 ASSERT(dstYoffset
% 4 == 0);
320 ASSERT(dstZoffset
% 4 == 0);
321 (void) dstZoffset
; (void) dstImageStride
;
323 if (srcFormat
!= GL_RGBA
||
324 srcType
!= CHAN_TYPE
||
325 ctx
->_ImageTransferState
||
326 srcPacking
->SwapBytes
) {
327 /* convert image to RGBA/GLchan */
328 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
330 dstFormat
->BaseFormat
,
331 srcWidth
, srcHeight
, srcDepth
,
332 srcFormat
, srcType
, srcAddr
,
335 return GL_FALSE
; /* out of memory */
336 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
338 srcRowStride
= 4 * srcWidth
;
341 pixels
= (const GLchan
*) srcAddr
;
342 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
343 srcType
) / sizeof(GLchan
);
346 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
347 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
348 texWidth
, (GLubyte
*) dstAddr
);
349 if (ext_tx_compress_dxtn
) {
350 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
, dst
, dstRowStride
);
353 _mesa_problem(ctx
, "external dxt library not available");
357 _mesa_free((void *) tempImage
);
364 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
365 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
368 if (fetch_ext_rgb_dxt1
) {
369 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
370 (*fetch_ext_rgb_dxt1
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
372 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
377 fetch_texel_2d_f_rgb_dxt1( const struct gl_texture_image
*texImage
,
378 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
380 /* just sample as GLchan and convert to float here */
382 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
383 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
384 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
385 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
386 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
391 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image
*texImage
,
392 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
395 if (fetch_ext_rgba_dxt1
) {
396 (*fetch_ext_rgba_dxt1
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
398 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
403 fetch_texel_2d_f_rgba_dxt1( const struct gl_texture_image
*texImage
,
404 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
406 /* just sample as GLchan and convert to float here */
408 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
409 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
410 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
411 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
412 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
417 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
418 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
421 if (fetch_ext_rgba_dxt3
) {
422 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
423 (*fetch_ext_rgba_dxt3
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
425 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
430 fetch_texel_2d_f_rgba_dxt3( const struct gl_texture_image
*texImage
,
431 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
433 /* just sample as GLchan and convert to float here */
435 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
436 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
437 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
438 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
439 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
444 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image
*texImage
,
445 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
448 if (fetch_ext_rgba_dxt5
) {
449 (*fetch_ext_rgba_dxt5
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
451 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
456 fetch_texel_2d_f_rgba_dxt5( const struct gl_texture_image
*texImage
,
457 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
459 /* just sample as GLchan and convert to float here */
461 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
462 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
463 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
464 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
465 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
469 const struct gl_texture_format _mesa_texformat_rgb_dxt1
= {
470 MESA_FORMAT_RGB_DXT1
, /* MesaFormat */
471 GL_RGB
, /* BaseFormat */
472 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
473 4, /*approx*/ /* RedBits */
474 4, /*approx*/ /* GreenBits */
475 4, /*approx*/ /* BlueBits */
477 0, /* LuminanceBits */
478 0, /* IntensityBits */
482 texstore_rgb_dxt1
, /* StoreTexImageFunc */
483 NULL
, /*impossible*/ /* FetchTexel1D */
484 fetch_texel_2d_rgb_dxt1
, /* FetchTexel2D */
485 NULL
, /*impossible*/ /* FetchTexel3D */
486 NULL
, /*impossible*/ /* FetchTexel1Df */
487 fetch_texel_2d_f_rgb_dxt1
, /* FetchTexel2Df */
488 NULL
, /*impossible*/ /* FetchTexel3Df */
491 const struct gl_texture_format _mesa_texformat_rgba_dxt1
= {
492 MESA_FORMAT_RGBA_DXT1
, /* MesaFormat */
493 GL_RGBA
, /* BaseFormat */
494 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
495 4, /*approx*/ /* RedBits */
496 4, /*approx*/ /* GreenBits */
497 4, /*approx*/ /* BlueBits */
498 1, /*approx*/ /* AlphaBits */
499 0, /* LuminanceBits */
500 0, /* IntensityBits */
504 texstore_rgba_dxt1
, /* StoreTexImageFunc */
505 NULL
, /*impossible*/ /* FetchTexel1D */
506 fetch_texel_2d_rgba_dxt1
, /* FetchTexel2D */
507 NULL
, /*impossible*/ /* FetchTexel3D */
508 NULL
, /*impossible*/ /* FetchTexel1Df */
509 fetch_texel_2d_f_rgba_dxt1
, /* FetchTexel2Df */
510 NULL
, /*impossible*/ /* FetchTexel3Df */
513 const struct gl_texture_format _mesa_texformat_rgba_dxt3
= {
514 MESA_FORMAT_RGBA_DXT3
, /* MesaFormat */
515 GL_RGBA
, /* BaseFormat */
516 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
517 4, /*approx*/ /* RedBits */
518 4, /*approx*/ /* GreenBits */
519 4, /*approx*/ /* BlueBits */
520 4, /*approx*/ /* AlphaBits */
521 0, /* LuminanceBits */
522 0, /* IntensityBits */
526 texstore_rgba_dxt3
, /* StoreTexImageFunc */
527 NULL
, /*impossible*/ /* FetchTexel1D */
528 fetch_texel_2d_rgba_dxt3
, /* FetchTexel2D */
529 NULL
, /*impossible*/ /* FetchTexel3D */
530 NULL
, /*impossible*/ /* FetchTexel1Df */
531 fetch_texel_2d_f_rgba_dxt3
, /* FetchTexel2Df */
532 NULL
, /*impossible*/ /* FetchTexel3Df */
535 const struct gl_texture_format _mesa_texformat_rgba_dxt5
= {
536 MESA_FORMAT_RGBA_DXT5
, /* MesaFormat */
537 GL_RGBA
, /* BaseFormat */
538 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
539 4,/*approx*/ /* RedBits */
540 4,/*approx*/ /* GreenBits */
541 4,/*approx*/ /* BlueBits */
542 4,/*approx*/ /* AlphaBits */
543 0, /* LuminanceBits */
544 0, /* IntensityBits */
548 texstore_rgba_dxt5
, /* StoreTexImageFunc */
549 NULL
, /*impossible*/ /* FetchTexel1D */
550 fetch_texel_2d_rgba_dxt5
, /* FetchTexel2D */
551 NULL
, /*impossible*/ /* FetchTexel3D */
552 NULL
, /*impossible*/ /* FetchTexel1Df */
553 fetch_texel_2d_f_rgba_dxt5
, /* FetchTexel2Df */
554 NULL
, /*impossible*/ /* FetchTexel3Df */