2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008 VMware, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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
42 #include "texcompress.h"
43 #include "texcompress_s3tc.h"
47 #if FEATURE_texture_s3tc
50 #if defined(_WIN32) || defined(WIN32)
51 #define DXTN_LIBNAME "dxtn.dll"
54 #elif defined(__DJGPP__)
55 #define DXTN_LIBNAME "dxtn.dxe"
57 #define DXTN_LIBNAME "libtxc_dxtn.so"
60 #if FEATURE_EXT_texture_sRGB
62 * Convert an 8-bit sRGB value from non-linear space to a
63 * linear RGB value in [0, 1].
64 * Implemented with a 256-entry lookup table.
67 nonlinear_to_linear(GLubyte cs8
)
69 static GLfloat table
[256];
70 static GLboolean tableReady
= GL_FALSE
;
72 /* compute lookup table now */
74 for (i
= 0; i
< 256; i
++) {
75 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
77 table
[i
] = cs
/ 12.92f
;
80 table
[i
] = (GLfloat
) pow((cs
+ 0.055) / 1.055, 2.4);
87 #endif /* FEATURE_EXT_texture_sRGB */
89 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
91 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
92 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
93 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
94 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
96 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
97 GLint height
, const GLchan
*srcPixData
,
98 GLenum destformat
, GLubyte
*dest
,
101 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
103 static void *dxtlibhandle
= NULL
;
107 _mesa_init_texture_s3tc( GLcontext
*ctx
)
109 /* called during context initialization */
110 ctx
->Mesa_DXTn
= GL_FALSE
;
111 #if USE_EXTERNAL_DXTN_LIB
113 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
115 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
116 "compression/decompression unavailable");
119 /* the fetch functions are not per context! Might be problematic... */
120 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
121 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
122 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
123 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
124 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
125 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
126 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
127 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
128 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
129 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
131 if (!fetch_ext_rgb_dxt1
||
132 !fetch_ext_rgba_dxt1
||
133 !fetch_ext_rgba_dxt3
||
134 !fetch_ext_rgba_dxt5
||
135 !ext_tx_compress_dxtn
) {
136 _mesa_warning(ctx
, "couldn't reference all symbols in "
137 DXTN_LIBNAME
", software DXTn compression/decompression "
139 fetch_ext_rgb_dxt1
= NULL
;
140 fetch_ext_rgba_dxt1
= NULL
;
141 fetch_ext_rgba_dxt3
= NULL
;
142 fetch_ext_rgba_dxt5
= NULL
;
143 ext_tx_compress_dxtn
= NULL
;
144 _mesa_dlclose(dxtlibhandle
);
150 ctx
->Mesa_DXTn
= GL_TRUE
;
158 * Store user's image in rgb_dxt1 format.
161 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
163 const GLchan
*pixels
;
166 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
167 const GLchan
*tempImage
= NULL
;
169 ASSERT(dstFormat
== MESA_FORMAT_RGB_DXT1
);
170 ASSERT(dstXoffset
% 4 == 0);
171 ASSERT(dstYoffset
% 4 == 0);
172 ASSERT(dstZoffset
% 4 == 0);
174 (void) dstImageOffsets
;
176 if (srcFormat
!= GL_RGB
||
177 srcType
!= CHAN_TYPE
||
178 ctx
->_ImageTransferState
||
179 srcPacking
->SwapBytes
) {
180 /* convert image to RGB/GLchan */
181 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
183 _mesa_get_format_base_format(dstFormat
),
184 srcWidth
, srcHeight
, srcDepth
,
185 srcFormat
, srcType
, srcAddr
,
188 return GL_FALSE
; /* out of memory */
190 srcRowStride
= 3 * srcWidth
;
194 pixels
= (const GLchan
*) srcAddr
;
195 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
196 srcType
) / sizeof(GLchan
);
199 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
201 texWidth
, (GLubyte
*) dstAddr
);
203 if (ext_tx_compress_dxtn
) {
204 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
205 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
209 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
213 free((void *) tempImage
);
220 * Store user's image in rgba_dxt1 format.
223 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
225 const GLchan
*pixels
;
228 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
229 const GLchan
*tempImage
= NULL
;
231 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT1
);
232 ASSERT(dstXoffset
% 4 == 0);
233 ASSERT(dstYoffset
% 4 == 0);
234 ASSERT(dstZoffset
% 4 == 0);
236 (void) dstImageOffsets
;
238 if (srcFormat
!= GL_RGBA
||
239 srcType
!= CHAN_TYPE
||
240 ctx
->_ImageTransferState
||
241 srcPacking
->SwapBytes
) {
242 /* convert image to RGBA/GLchan */
243 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
245 _mesa_get_format_base_format(dstFormat
),
246 srcWidth
, srcHeight
, srcDepth
,
247 srcFormat
, srcType
, srcAddr
,
250 return GL_FALSE
; /* out of memory */
252 srcRowStride
= 4 * srcWidth
;
256 pixels
= (const GLchan
*) srcAddr
;
257 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
258 srcType
) / sizeof(GLchan
);
261 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
263 texWidth
, (GLubyte
*) dstAddr
);
264 if (ext_tx_compress_dxtn
) {
265 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
266 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
270 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
274 free((void*) tempImage
);
281 * Store user's image in rgba_dxt3 format.
284 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
286 const GLchan
*pixels
;
289 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
290 const GLchan
*tempImage
= NULL
;
292 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT3
);
293 ASSERT(dstXoffset
% 4 == 0);
294 ASSERT(dstYoffset
% 4 == 0);
295 ASSERT(dstZoffset
% 4 == 0);
297 (void) dstImageOffsets
;
299 if (srcFormat
!= GL_RGBA
||
300 srcType
!= CHAN_TYPE
||
301 ctx
->_ImageTransferState
||
302 srcPacking
->SwapBytes
) {
303 /* convert image to RGBA/GLchan */
304 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
306 _mesa_get_format_base_format(dstFormat
),
307 srcWidth
, srcHeight
, srcDepth
,
308 srcFormat
, srcType
, srcAddr
,
311 return GL_FALSE
; /* out of memory */
313 srcRowStride
= 4 * srcWidth
;
316 pixels
= (const GLchan
*) srcAddr
;
317 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
318 srcType
) / sizeof(GLchan
);
321 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
323 texWidth
, (GLubyte
*) dstAddr
);
324 if (ext_tx_compress_dxtn
) {
325 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
326 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
330 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
334 free((void *) tempImage
);
341 * Store user's image in rgba_dxt5 format.
344 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
346 const GLchan
*pixels
;
349 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
350 const GLchan
*tempImage
= NULL
;
352 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT5
);
353 ASSERT(dstXoffset
% 4 == 0);
354 ASSERT(dstYoffset
% 4 == 0);
355 ASSERT(dstZoffset
% 4 == 0);
357 (void) dstImageOffsets
;
359 if (srcFormat
!= GL_RGBA
||
360 srcType
!= CHAN_TYPE
||
361 ctx
->_ImageTransferState
||
362 srcPacking
->SwapBytes
) {
363 /* convert image to RGBA/GLchan */
364 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
366 _mesa_get_format_base_format(dstFormat
),
367 srcWidth
, srcHeight
, srcDepth
,
368 srcFormat
, srcType
, srcAddr
,
371 return GL_FALSE
; /* out of memory */
373 srcRowStride
= 4 * srcWidth
;
376 pixels
= (const GLchan
*) srcAddr
;
377 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
378 srcType
) / sizeof(GLchan
);
381 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
383 texWidth
, (GLubyte
*) dstAddr
);
384 if (ext_tx_compress_dxtn
) {
385 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
386 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
390 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
394 free((void *) tempImage
);
401 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
402 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
405 if (fetch_ext_rgb_dxt1
) {
406 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
407 fetch_ext_rgb_dxt1(texImage
->RowStride
,
408 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
411 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
416 _mesa_fetch_texel_2d_f_rgb_dxt1(const struct gl_texture_image
*texImage
,
417 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
419 /* just sample as GLchan and convert to float here */
421 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
422 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
423 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
424 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
425 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
430 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image
*texImage
,
431 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
434 if (fetch_ext_rgba_dxt1
) {
435 fetch_ext_rgba_dxt1(texImage
->RowStride
,
436 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
439 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
444 _mesa_fetch_texel_2d_f_rgba_dxt1(const struct gl_texture_image
*texImage
,
445 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
447 /* just sample as GLchan and convert to float here */
449 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
450 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
451 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
452 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
453 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
458 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
459 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
462 if (fetch_ext_rgba_dxt3
) {
463 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
464 fetch_ext_rgba_dxt3(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
468 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
473 _mesa_fetch_texel_2d_f_rgba_dxt3(const struct gl_texture_image
*texImage
,
474 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
476 /* just sample as GLchan and convert to float here */
478 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
479 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
480 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
481 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
482 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
487 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image
*texImage
,
488 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
491 if (fetch_ext_rgba_dxt5
) {
492 fetch_ext_rgba_dxt5(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
496 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
501 _mesa_fetch_texel_2d_f_rgba_dxt5(const struct gl_texture_image
*texImage
,
502 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
504 /* just sample as GLchan and convert to float here */
506 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
507 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
508 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
509 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
510 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
513 #if FEATURE_EXT_texture_sRGB
515 _mesa_fetch_texel_2d_f_srgb_dxt1( const struct gl_texture_image
*texImage
,
516 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
518 /* just sample as GLchan and convert to float here */
520 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
521 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
522 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
523 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
524 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
528 _mesa_fetch_texel_2d_f_srgba_dxt1(const struct gl_texture_image
*texImage
,
529 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
531 /* just sample as GLchan and convert to float here */
533 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
534 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
535 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
536 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
537 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
541 _mesa_fetch_texel_2d_f_srgba_dxt3(const struct gl_texture_image
*texImage
,
542 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
544 /* just sample as GLchan and convert to float here */
546 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
547 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
548 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
549 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
550 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
554 _mesa_fetch_texel_2d_f_srgba_dxt5(const struct gl_texture_image
*texImage
,
555 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
557 /* just sample as GLchan and convert to float here */
559 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
560 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
561 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
562 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
563 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
565 #endif /* FEATURE_EXT_texture_sRGB */
568 #endif /* FEATURE_texture_s3tc */