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
,
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\n");
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\n");
133 * Called via TexFormat->StoreImage to store an RGB_DXT1 texture.
136 texstore_rgb_dxt1(STORE_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);
148 (void) dstZoffset
; (void) dstImageStride
;
150 if (srcFormat
!= GL_RGB
||
151 srcType
!= CHAN_TYPE
||
152 ctx
->_ImageTransferState
||
153 srcPacking
->SwapBytes
) {
154 /* convert image to RGB/GLchan */
155 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
157 dstFormat
->BaseFormat
,
158 srcWidth
, srcHeight
, srcDepth
,
159 srcFormat
, srcType
, srcAddr
,
162 return GL_FALSE
; /* out of memory */
163 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
165 srcRowStride
= 3 * srcWidth
;
169 pixels
= (const GLchan
*) srcAddr
;
170 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
171 srcType
) / sizeof(GLchan
);
174 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
175 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
176 texWidth
, (GLubyte
*) dstAddr
);
178 if (ext_tx_compress_dxtn
) {
179 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGB_S3TC_DXT1_EXT
, dst
, dstRowStride
);
182 _mesa_problem(ctx
, "external dxt library not available");
186 _mesa_free((void *) tempImage
);
193 * Called via TexFormat->StoreImage to store an RGBA_DXT1 texture.
196 texstore_rgba_dxt1(STORE_PARAMS
)
198 const GLchan
*pixels
;
201 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
202 const GLchan
*tempImage
= NULL
;
204 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt1
);
205 ASSERT(dstXoffset
% 4 == 0);
206 ASSERT(dstYoffset
% 4 == 0);
207 ASSERT(dstZoffset
% 4 == 0);
208 (void) dstZoffset
; (void) dstImageStride
;
210 if (srcFormat
!= GL_RGBA
||
211 srcType
!= CHAN_TYPE
||
212 ctx
->_ImageTransferState
||
213 srcPacking
->SwapBytes
) {
214 /* convert image to RGBA/GLchan */
215 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
217 dstFormat
->BaseFormat
,
218 srcWidth
, srcHeight
, srcDepth
,
219 srcFormat
, srcType
, srcAddr
,
222 return GL_FALSE
; /* out of memory */
223 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
225 srcRowStride
= 4 * srcWidth
;
229 pixels
= (const GLchan
*) srcAddr
;
230 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
231 srcType
) / sizeof(GLchan
);
234 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
235 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
236 texWidth
, (GLubyte
*) dstAddr
);
237 if (ext_tx_compress_dxtn
) {
238 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
, dst
, dstRowStride
);
241 _mesa_problem(ctx
, "external dxt library not available");
245 _mesa_free((void*) tempImage
);
252 * Called via TexFormat->StoreImage to store an RGBA_DXT3 texture.
255 texstore_rgba_dxt3(STORE_PARAMS
)
257 const GLchan
*pixels
;
260 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
261 const GLchan
*tempImage
= NULL
;
263 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt3
);
264 ASSERT(dstXoffset
% 4 == 0);
265 ASSERT(dstYoffset
% 4 == 0);
266 ASSERT(dstZoffset
% 4 == 0);
267 (void) dstZoffset
; (void) dstImageStride
;
269 if (srcFormat
!= GL_RGBA
||
270 srcType
!= CHAN_TYPE
||
271 ctx
->_ImageTransferState
||
272 srcPacking
->SwapBytes
) {
273 /* convert image to RGBA/GLchan */
274 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
276 dstFormat
->BaseFormat
,
277 srcWidth
, srcHeight
, srcDepth
,
278 srcFormat
, srcType
, srcAddr
,
281 return GL_FALSE
; /* out of memory */
282 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
284 srcRowStride
= 4 * srcWidth
;
287 pixels
= (const GLchan
*) srcAddr
;
288 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
289 srcType
) / sizeof(GLchan
);
292 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
293 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
294 texWidth
, (GLubyte
*) dstAddr
);
295 if (ext_tx_compress_dxtn
) {
296 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
, dst
, dstRowStride
);
299 _mesa_problem(ctx
, "external dxt library not available");
303 _mesa_free((void *) tempImage
);
310 * Called via TexFormat->StoreImage to store an RGBA_DXT5 texture.
313 texstore_rgba_dxt5(STORE_PARAMS
)
315 const GLchan
*pixels
;
318 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
319 const GLchan
*tempImage
= NULL
;
321 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt5
);
322 ASSERT(dstXoffset
% 4 == 0);
323 ASSERT(dstYoffset
% 4 == 0);
324 ASSERT(dstZoffset
% 4 == 0);
325 (void) dstZoffset
; (void) dstImageStride
;
327 if (srcFormat
!= GL_RGBA
||
328 srcType
!= CHAN_TYPE
||
329 ctx
->_ImageTransferState
||
330 srcPacking
->SwapBytes
) {
331 /* convert image to RGBA/GLchan */
332 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
334 dstFormat
->BaseFormat
,
335 srcWidth
, srcHeight
, srcDepth
,
336 srcFormat
, srcType
, srcAddr
,
339 return GL_FALSE
; /* out of memory */
340 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
342 srcRowStride
= 4 * srcWidth
;
345 pixels
= (const GLchan
*) srcAddr
;
346 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
347 srcType
) / sizeof(GLchan
);
350 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
351 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
352 texWidth
, (GLubyte
*) dstAddr
);
353 if (ext_tx_compress_dxtn
) {
354 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
, dst
, dstRowStride
);
357 _mesa_problem(ctx
, "external dxt library not available");
361 _mesa_free((void *) tempImage
);
368 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
369 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
372 if (fetch_ext_rgb_dxt1
) {
373 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
374 (*fetch_ext_rgb_dxt1
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
376 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
381 fetch_texel_2d_f_rgb_dxt1( const struct gl_texture_image
*texImage
,
382 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
384 /* just sample as GLchan and convert to float here */
386 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
387 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
388 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
389 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
390 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
395 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image
*texImage
,
396 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
399 if (fetch_ext_rgba_dxt1
) {
400 (*fetch_ext_rgba_dxt1
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
402 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
407 fetch_texel_2d_f_rgba_dxt1( const struct gl_texture_image
*texImage
,
408 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
410 /* just sample as GLchan and convert to float here */
412 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
413 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
414 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
415 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
416 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
421 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
422 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
425 if (fetch_ext_rgba_dxt3
) {
426 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
427 (*fetch_ext_rgba_dxt3
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
429 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
434 fetch_texel_2d_f_rgba_dxt3( 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_dxt3(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_dxt5( const struct gl_texture_image
*texImage
,
449 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
452 if (fetch_ext_rgba_dxt5
) {
453 (*fetch_ext_rgba_dxt5
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
455 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
460 fetch_texel_2d_f_rgba_dxt5( const struct gl_texture_image
*texImage
,
461 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
463 /* just sample as GLchan and convert to float here */
465 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
466 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
467 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
468 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
469 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
473 const struct gl_texture_format _mesa_texformat_rgb_dxt1
= {
474 MESA_FORMAT_RGB_DXT1
, /* MesaFormat */
475 GL_RGB
, /* BaseFormat */
476 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
477 4, /*approx*/ /* RedBits */
478 4, /*approx*/ /* GreenBits */
479 4, /*approx*/ /* BlueBits */
481 0, /* LuminanceBits */
482 0, /* IntensityBits */
486 texstore_rgb_dxt1
, /* StoreTexImageFunc */
487 NULL
, /*impossible*/ /* FetchTexel1D */
488 fetch_texel_2d_rgb_dxt1
, /* FetchTexel2D */
489 NULL
, /*impossible*/ /* FetchTexel3D */
490 NULL
, /*impossible*/ /* FetchTexel1Df */
491 fetch_texel_2d_f_rgb_dxt1
, /* FetchTexel2Df */
492 NULL
, /*impossible*/ /* FetchTexel3Df */
495 const struct gl_texture_format _mesa_texformat_rgba_dxt1
= {
496 MESA_FORMAT_RGBA_DXT1
, /* MesaFormat */
497 GL_RGBA
, /* BaseFormat */
498 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
499 4, /*approx*/ /* RedBits */
500 4, /*approx*/ /* GreenBits */
501 4, /*approx*/ /* BlueBits */
502 1, /*approx*/ /* AlphaBits */
503 0, /* LuminanceBits */
504 0, /* IntensityBits */
508 texstore_rgba_dxt1
, /* StoreTexImageFunc */
509 NULL
, /*impossible*/ /* FetchTexel1D */
510 fetch_texel_2d_rgba_dxt1
, /* FetchTexel2D */
511 NULL
, /*impossible*/ /* FetchTexel3D */
512 NULL
, /*impossible*/ /* FetchTexel1Df */
513 fetch_texel_2d_f_rgba_dxt1
, /* FetchTexel2Df */
514 NULL
, /*impossible*/ /* FetchTexel3Df */
517 const struct gl_texture_format _mesa_texformat_rgba_dxt3
= {
518 MESA_FORMAT_RGBA_DXT3
, /* MesaFormat */
519 GL_RGBA
, /* BaseFormat */
520 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
521 4, /*approx*/ /* RedBits */
522 4, /*approx*/ /* GreenBits */
523 4, /*approx*/ /* BlueBits */
524 4, /*approx*/ /* AlphaBits */
525 0, /* LuminanceBits */
526 0, /* IntensityBits */
530 texstore_rgba_dxt3
, /* StoreTexImageFunc */
531 NULL
, /*impossible*/ /* FetchTexel1D */
532 fetch_texel_2d_rgba_dxt3
, /* FetchTexel2D */
533 NULL
, /*impossible*/ /* FetchTexel3D */
534 NULL
, /*impossible*/ /* FetchTexel1Df */
535 fetch_texel_2d_f_rgba_dxt3
, /* FetchTexel2Df */
536 NULL
, /*impossible*/ /* FetchTexel3Df */
539 const struct gl_texture_format _mesa_texformat_rgba_dxt5
= {
540 MESA_FORMAT_RGBA_DXT5
, /* MesaFormat */
541 GL_RGBA
, /* BaseFormat */
542 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
543 4,/*approx*/ /* RedBits */
544 4,/*approx*/ /* GreenBits */
545 4,/*approx*/ /* BlueBits */
546 4,/*approx*/ /* AlphaBits */
547 0, /* LuminanceBits */
548 0, /* IntensityBits */
552 texstore_rgba_dxt5
, /* StoreTexImageFunc */
553 NULL
, /*impossible*/ /* FetchTexel1D */
554 fetch_texel_2d_rgba_dxt5
, /* FetchTexel2D */
555 NULL
, /*impossible*/ /* FetchTexel3D */
556 NULL
, /*impossible*/ /* FetchTexel1Df */
557 fetch_texel_2d_f_rgba_dxt5
, /* FetchTexel2Df */
558 NULL
, /*impossible*/ /* FetchTexel3Df */