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
49 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
50 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
;
51 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
;
52 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
;
53 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
;
55 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
, GLint height
, const GLubyte
*srcPixData
, GLenum destformat
, GLubyte
*dest
);
56 dxtCompressTexFuncExt ext_tx_compress_dxtn
;
58 void *dxtlibhandle
= NULL
;
61 _mesa_init_texture_s3tc( GLcontext
*ctx
)
63 /* called during context initialization */
64 ctx
->Mesa_DXTn
= GL_FALSE
;
65 #if USE_EXTERNAL_DXTN_LIB
69 dxtlibhandle
= dlopen ("libtxc_dxtn.so", RTLD_LAZY
| RTLD_GLOBAL
);
71 _mesa_warning(ctx
, "couldn't open libtxc_dxtn.so, software DXTn"
72 "compression/decompression unavailable\n");
75 /* the fetch functions are not per context! Might be problematic... */
76 fetch_ext_rgb_dxt1
= dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
79 fetch_ext_rgba_dxt1
= dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
83 fetch_ext_rgba_dxt3
= dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
87 fetch_ext_rgba_dxt5
= dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
91 ext_tx_compress_dxtn
= dlsym(dxtlibhandle
, "tx_compress_dxtn");
96 _mesa_warning(ctx
, "couldn't reference all symbols in "
97 "libtxc_dxtn.so, software DXTn compression/decompression "
99 fetch_ext_rgb_dxt1
= NULL
;
100 fetch_ext_rgba_dxt1
= NULL
;
101 fetch_ext_rgba_dxt3
= NULL
;
102 fetch_ext_rgba_dxt5
= NULL
;
103 ext_tx_compress_dxtn
= NULL
;
104 dlclose(dxtlibhandle
);
110 ctx
->Mesa_DXTn
= GL_TRUE
;
111 _mesa_warning(ctx
, "software DXTn compression/decompression available\n");
119 * Called via TexFormat->StoreImage to store an RGB_DXT1 texture.
122 texstore_rgb_dxt1(STORE_PARAMS
)
124 const GLchan
*pixels
;
127 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
128 const GLchan
*tempImage
= NULL
;
130 ASSERT(dstFormat
== &_mesa_texformat_rgb_dxt1
);
131 ASSERT(dstXoffset
% 4 == 0);
132 ASSERT(dstYoffset
% 4 == 0);
133 ASSERT(dstZoffset
% 4 == 0);
134 (void) dstZoffset
; (void) dstImageStride
;
136 if (srcFormat
!= GL_RGB
||
137 srcType
!= CHAN_TYPE
||
138 ctx
->_ImageTransferState
||
139 srcPacking
->SwapBytes
) {
140 /* convert image to RGB/GLchan */
141 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
143 dstFormat
->BaseFormat
,
144 srcWidth
, srcHeight
, srcDepth
,
145 srcFormat
, srcType
, srcAddr
,
148 return GL_FALSE
; /* out of memory */
149 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
151 srcRowStride
= 3 * srcWidth
;
155 pixels
= (const GLchan
*) srcAddr
;
156 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
157 srcType
) / sizeof(GLchan
);
160 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
161 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
162 texWidth
, (GLubyte
*) dstAddr
);
164 if (ext_tx_compress_dxtn
) {
165 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGB_S3TC_DXT1_EXT
, dst
);
168 _mesa_problem(ctx
, "external dxt library not available");
172 _mesa_free((void *) tempImage
);
179 * Called via TexFormat->StoreImage to store an RGBA_DXT1 texture.
182 texstore_rgba_dxt1(STORE_PARAMS
)
184 const GLchan
*pixels
;
187 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
188 const GLchan
*tempImage
= NULL
;
190 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt1
);
191 ASSERT(dstXoffset
% 4 == 0);
192 ASSERT(dstYoffset
% 4 == 0);
193 ASSERT(dstZoffset
% 4 == 0);
194 (void) dstZoffset
; (void) dstImageStride
;
196 if (srcFormat
!= GL_RGBA
||
197 srcType
!= CHAN_TYPE
||
198 ctx
->_ImageTransferState
||
199 srcPacking
->SwapBytes
) {
200 /* convert image to RGBA/GLchan */
201 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
203 dstFormat
->BaseFormat
,
204 srcWidth
, srcHeight
, srcDepth
,
205 srcFormat
, srcType
, srcAddr
,
208 return GL_FALSE
; /* out of memory */
209 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
211 srcRowStride
= 4 * srcWidth
;
215 pixels
= (const GLchan
*) srcAddr
;
216 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
217 srcType
) / sizeof(GLchan
);
220 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
221 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
222 texWidth
, (GLubyte
*) dstAddr
);
223 if (ext_tx_compress_dxtn
) {
224 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
, dst
);
227 _mesa_problem(ctx
, "external dxt library not available");
231 _mesa_free((void*) tempImage
);
238 * Called via TexFormat->StoreImage to store an RGBA_DXT3 texture.
241 texstore_rgba_dxt3(STORE_PARAMS
)
243 const GLchan
*pixels
;
246 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
247 const GLchan
*tempImage
= NULL
;
249 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt3
);
250 ASSERT(dstXoffset
% 4 == 0);
251 ASSERT(dstYoffset
% 4 == 0);
252 ASSERT(dstZoffset
% 4 == 0);
253 (void) dstZoffset
; (void) dstImageStride
;
255 if (srcFormat
!= GL_RGBA
||
256 srcType
!= CHAN_TYPE
||
257 ctx
->_ImageTransferState
||
258 srcPacking
->SwapBytes
) {
259 /* convert image to RGBA/GLchan */
260 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
262 dstFormat
->BaseFormat
,
263 srcWidth
, srcHeight
, srcDepth
,
264 srcFormat
, srcType
, srcAddr
,
267 return GL_FALSE
; /* out of memory */
268 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
270 srcRowStride
= 4 * srcWidth
;
273 pixels
= (const GLchan
*) srcAddr
;
274 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
275 srcType
) / sizeof(GLchan
);
278 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
279 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
280 texWidth
, (GLubyte
*) dstAddr
);
281 if (ext_tx_compress_dxtn
) {
282 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
, dst
);
285 _mesa_problem(ctx
, "external dxt library not available");
289 _mesa_free((void *) tempImage
);
296 * Called via TexFormat->StoreImage to store an RGBA_DXT5 texture.
299 texstore_rgba_dxt5(STORE_PARAMS
)
301 const GLchan
*pixels
;
304 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
305 const GLchan
*tempImage
= NULL
;
307 ASSERT(dstFormat
== &_mesa_texformat_rgba_dxt5
);
308 ASSERT(dstXoffset
% 4 == 0);
309 ASSERT(dstYoffset
% 4 == 0);
310 ASSERT(dstZoffset
% 4 == 0);
311 (void) dstZoffset
; (void) dstImageStride
;
313 if (srcFormat
!= GL_RGBA
||
314 srcType
!= CHAN_TYPE
||
315 ctx
->_ImageTransferState
||
316 srcPacking
->SwapBytes
) {
317 /* convert image to RGBA/GLchan */
318 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
320 dstFormat
->BaseFormat
,
321 srcWidth
, srcHeight
, srcDepth
,
322 srcFormat
, srcType
, srcAddr
,
325 return GL_FALSE
; /* out of memory */
326 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
328 srcRowStride
= 4 * srcWidth
;
331 pixels
= (const GLchan
*) srcAddr
;
332 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
333 srcType
) / sizeof(GLchan
);
336 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
337 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
338 texWidth
, (GLubyte
*) dstAddr
);
339 if (ext_tx_compress_dxtn
) {
340 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
, dst
);
343 _mesa_problem(ctx
, "external dxt library not available");
347 _mesa_free((void *) tempImage
);
354 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
355 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
358 if (fetch_ext_rgb_dxt1
) {
359 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
360 (*fetch_ext_rgb_dxt1
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
362 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
367 fetch_texel_2d_f_rgb_dxt1( const struct gl_texture_image
*texImage
,
368 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
370 /* just sample as GLchan and convert to float here */
372 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
373 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
374 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
375 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
376 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
381 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image
*texImage
,
382 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
385 if (fetch_ext_rgba_dxt1
) {
386 (*fetch_ext_rgba_dxt1
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
388 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
393 fetch_texel_2d_f_rgba_dxt1( const struct gl_texture_image
*texImage
,
394 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
396 /* just sample as GLchan and convert to float here */
398 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
399 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
400 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
401 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
402 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
407 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
408 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
411 if (fetch_ext_rgba_dxt3
) {
412 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
413 (*fetch_ext_rgba_dxt3
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
415 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
420 fetch_texel_2d_f_rgba_dxt3( const struct gl_texture_image
*texImage
,
421 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
423 /* just sample as GLchan and convert to float here */
425 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
426 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
427 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
428 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
429 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
434 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image
*texImage
,
435 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
438 if (fetch_ext_rgba_dxt5
) {
439 (*fetch_ext_rgba_dxt5
)((texImage
)->RowStride
, (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
441 else _mesa_debug(NULL
, "attempted to decode s3tc texture without library available\n");
446 fetch_texel_2d_f_rgba_dxt5( const struct gl_texture_image
*texImage
,
447 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
449 /* just sample as GLchan and convert to float here */
451 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
452 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
453 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
454 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
455 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
459 const struct gl_texture_format _mesa_texformat_rgb_dxt1
= {
460 MESA_FORMAT_RGB_DXT1
, /* MesaFormat */
461 GL_RGB
, /* BaseFormat */
462 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
463 4, /*approx*/ /* RedBits */
464 4, /*approx*/ /* GreenBits */
465 4, /*approx*/ /* BlueBits */
467 0, /* LuminanceBits */
468 0, /* IntensityBits */
472 texstore_rgb_dxt1
, /* StoreTexImageFunc */
473 NULL
, /*impossible*/ /* FetchTexel1D */
474 fetch_texel_2d_rgb_dxt1
, /* FetchTexel2D */
475 NULL
, /*impossible*/ /* FetchTexel3D */
476 NULL
, /*impossible*/ /* FetchTexel1Df */
477 fetch_texel_2d_f_rgb_dxt1
, /* FetchTexel2Df */
478 NULL
, /*impossible*/ /* FetchTexel3Df */
481 const struct gl_texture_format _mesa_texformat_rgba_dxt1
= {
482 MESA_FORMAT_RGBA_DXT1
, /* MesaFormat */
483 GL_RGBA
, /* BaseFormat */
484 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
485 4, /*approx*/ /* RedBits */
486 4, /*approx*/ /* GreenBits */
487 4, /*approx*/ /* BlueBits */
488 1, /*approx*/ /* AlphaBits */
489 0, /* LuminanceBits */
490 0, /* IntensityBits */
494 texstore_rgba_dxt1
, /* StoreTexImageFunc */
495 NULL
, /*impossible*/ /* FetchTexel1D */
496 fetch_texel_2d_rgba_dxt1
, /* FetchTexel2D */
497 NULL
, /*impossible*/ /* FetchTexel3D */
498 NULL
, /*impossible*/ /* FetchTexel1Df */
499 fetch_texel_2d_f_rgba_dxt1
, /* FetchTexel2Df */
500 NULL
, /*impossible*/ /* FetchTexel3Df */
503 const struct gl_texture_format _mesa_texformat_rgba_dxt3
= {
504 MESA_FORMAT_RGBA_DXT3
, /* MesaFormat */
505 GL_RGBA
, /* BaseFormat */
506 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
507 4, /*approx*/ /* RedBits */
508 4, /*approx*/ /* GreenBits */
509 4, /*approx*/ /* BlueBits */
510 4, /*approx*/ /* AlphaBits */
511 0, /* LuminanceBits */
512 0, /* IntensityBits */
516 texstore_rgba_dxt3
, /* StoreTexImageFunc */
517 NULL
, /*impossible*/ /* FetchTexel1D */
518 fetch_texel_2d_rgba_dxt3
, /* FetchTexel2D */
519 NULL
, /*impossible*/ /* FetchTexel3D */
520 NULL
, /*impossible*/ /* FetchTexel1Df */
521 fetch_texel_2d_f_rgba_dxt3
, /* FetchTexel2Df */
522 NULL
, /*impossible*/ /* FetchTexel3Df */
525 const struct gl_texture_format _mesa_texformat_rgba_dxt5
= {
526 MESA_FORMAT_RGBA_DXT5
, /* MesaFormat */
527 GL_RGBA
, /* BaseFormat */
528 GL_UNSIGNED_NORMALIZED_ARB
, /* DataType */
529 4,/*approx*/ /* RedBits */
530 4,/*approx*/ /* GreenBits */
531 4,/*approx*/ /* BlueBits */
532 4,/*approx*/ /* AlphaBits */
533 0, /* LuminanceBits */
534 0, /* IntensityBits */
538 texstore_rgba_dxt5
, /* StoreTexImageFunc */
539 NULL
, /*impossible*/ /* FetchTexel1D */
540 fetch_texel_2d_rgba_dxt5
, /* FetchTexel2D */
541 NULL
, /*impossible*/ /* FetchTexel3D */
542 NULL
, /*impossible*/ /* FetchTexel1Df */
543 fetch_texel_2d_f_rgba_dxt5
, /* FetchTexel2Df */
544 NULL
, /*impossible*/ /* FetchTexel3Df */