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
43 #include "texcompress.h"
44 #include "texcompress_s3tc.h"
48 #define DXTN_LIBNAME "dxtn.dll"
51 #elif defined(__DJGPP__)
52 #define DXTN_LIBNAME "dxtn.dxe"
54 #define DXTN_LIBNAME "libtxc_dxtn.so"
57 #if FEATURE_EXT_texture_sRGB
59 * Convert an 8-bit sRGB value from non-linear space to a
60 * linear RGB value in [0, 1].
61 * Implemented with a 256-entry lookup table.
64 nonlinear_to_linear(GLubyte cs8
)
66 static GLfloat table
[256];
67 static GLboolean tableReady
= GL_FALSE
;
69 /* compute lookup table now */
71 for (i
= 0; i
< 256; i
++) {
72 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
74 table
[i
] = cs
/ 12.92f
;
77 table
[i
] = (GLfloat
) _mesa_pow((cs
+ 0.055) / 1.055, 2.4);
84 #endif /* FEATURE_EXT_texture_sRGB */
86 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
88 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
89 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
90 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
91 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
93 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
94 GLint height
, const GLchan
*srcPixData
,
95 GLenum destformat
, GLubyte
*dest
,
98 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
100 static void *dxtlibhandle
= NULL
;
104 _mesa_init_texture_s3tc( GLcontext
*ctx
)
106 /* called during context initialization */
107 ctx
->Mesa_DXTn
= GL_FALSE
;
108 #if USE_EXTERNAL_DXTN_LIB
110 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
112 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
113 "compression/decompression unavailable");
116 /* the fetch functions are not per context! Might be problematic... */
117 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
118 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
119 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
120 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
121 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
122 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
123 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
124 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
125 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
126 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
128 if (!fetch_ext_rgb_dxt1
||
129 !fetch_ext_rgba_dxt1
||
130 !fetch_ext_rgba_dxt3
||
131 !fetch_ext_rgba_dxt5
||
132 !ext_tx_compress_dxtn
) {
133 _mesa_warning(ctx
, "couldn't reference all symbols in "
134 DXTN_LIBNAME
", software DXTn compression/decompression "
136 fetch_ext_rgb_dxt1
= NULL
;
137 fetch_ext_rgba_dxt1
= NULL
;
138 fetch_ext_rgba_dxt3
= NULL
;
139 fetch_ext_rgba_dxt5
= NULL
;
140 ext_tx_compress_dxtn
= NULL
;
141 _mesa_dlclose(dxtlibhandle
);
147 ctx
->Mesa_DXTn
= GL_TRUE
;
148 _mesa_warning(ctx
, "software DXTn compression/decompression available");
156 * Store user's image in rgb_dxt1 format.
159 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
161 const GLchan
*pixels
;
164 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
165 const GLchan
*tempImage
= NULL
;
167 ASSERT(dstFormat
== MESA_FORMAT_RGB_DXT1
);
168 ASSERT(dstXoffset
% 4 == 0);
169 ASSERT(dstYoffset
% 4 == 0);
170 ASSERT(dstZoffset
% 4 == 0);
172 (void) dstImageOffsets
;
174 if (srcFormat
!= GL_RGB
||
175 srcType
!= CHAN_TYPE
||
176 ctx
->_ImageTransferState
||
177 srcPacking
->SwapBytes
) {
178 /* convert image to RGB/GLchan */
179 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
181 _mesa_get_format_base_format(dstFormat
),
182 srcWidth
, srcHeight
, srcDepth
,
183 srcFormat
, srcType
, srcAddr
,
186 return GL_FALSE
; /* out of memory */
187 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
189 srcRowStride
= 3 * srcWidth
;
193 pixels
= (const GLchan
*) srcAddr
;
194 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
195 srcType
) / sizeof(GLchan
);
198 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
200 texWidth
, (GLubyte
*) dstAddr
);
202 if (ext_tx_compress_dxtn
) {
203 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
204 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
208 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
212 _mesa_free((void *) tempImage
);
219 * Store user's image in rgba_dxt1 format.
222 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
224 const GLchan
*pixels
;
227 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
228 const GLchan
*tempImage
= NULL
;
230 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT1
);
231 ASSERT(dstXoffset
% 4 == 0);
232 ASSERT(dstYoffset
% 4 == 0);
233 ASSERT(dstZoffset
% 4 == 0);
235 (void) dstImageOffsets
;
237 if (srcFormat
!= GL_RGBA
||
238 srcType
!= CHAN_TYPE
||
239 ctx
->_ImageTransferState
||
240 srcPacking
->SwapBytes
) {
241 /* convert image to RGBA/GLchan */
242 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
244 _mesa_get_format_base_format(dstFormat
),
245 srcWidth
, srcHeight
, srcDepth
,
246 srcFormat
, srcType
, srcAddr
,
249 return GL_FALSE
; /* out of memory */
250 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
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 _mesa_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 */
312 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
314 srcRowStride
= 4 * srcWidth
;
317 pixels
= (const GLchan
*) srcAddr
;
318 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
319 srcType
) / sizeof(GLchan
);
322 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
324 texWidth
, (GLubyte
*) dstAddr
);
325 if (ext_tx_compress_dxtn
) {
326 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
327 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
331 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
335 _mesa_free((void *) tempImage
);
342 * Store user's image in rgba_dxt5 format.
345 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
347 const GLchan
*pixels
;
350 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
351 const GLchan
*tempImage
= NULL
;
353 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT5
);
354 ASSERT(dstXoffset
% 4 == 0);
355 ASSERT(dstYoffset
% 4 == 0);
356 ASSERT(dstZoffset
% 4 == 0);
358 (void) dstImageOffsets
;
360 if (srcFormat
!= GL_RGBA
||
361 srcType
!= CHAN_TYPE
||
362 ctx
->_ImageTransferState
||
363 srcPacking
->SwapBytes
) {
364 /* convert image to RGBA/GLchan */
365 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
367 _mesa_get_format_base_format(dstFormat
),
368 srcWidth
, srcHeight
, srcDepth
,
369 srcFormat
, srcType
, srcAddr
,
372 return GL_FALSE
; /* out of memory */
373 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
375 srcRowStride
= 4 * srcWidth
;
378 pixels
= (const GLchan
*) srcAddr
;
379 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
380 srcType
) / sizeof(GLchan
);
383 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
385 texWidth
, (GLubyte
*) dstAddr
);
386 if (ext_tx_compress_dxtn
) {
387 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
388 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
392 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
396 _mesa_free((void *) tempImage
);
403 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
404 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
407 if (fetch_ext_rgb_dxt1
) {
408 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
409 fetch_ext_rgb_dxt1(texImage
->RowStride
,
410 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
413 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
418 _mesa_fetch_texel_2d_f_rgb_dxt1(const struct gl_texture_image
*texImage
,
419 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
421 /* just sample as GLchan and convert to float here */
423 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
424 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
425 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
426 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
427 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
432 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image
*texImage
,
433 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
436 if (fetch_ext_rgba_dxt1
) {
437 fetch_ext_rgba_dxt1(texImage
->RowStride
,
438 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
441 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
446 _mesa_fetch_texel_2d_f_rgba_dxt1(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_dxt1(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
]);
460 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
461 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
464 if (fetch_ext_rgba_dxt3
) {
465 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
466 fetch_ext_rgba_dxt3(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
470 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
475 _mesa_fetch_texel_2d_f_rgba_dxt3(const struct gl_texture_image
*texImage
,
476 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
478 /* just sample as GLchan and convert to float here */
480 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
481 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
482 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
483 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
484 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
489 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image
*texImage
,
490 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
493 if (fetch_ext_rgba_dxt5
) {
494 fetch_ext_rgba_dxt5(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
498 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
503 _mesa_fetch_texel_2d_f_rgba_dxt5(const struct gl_texture_image
*texImage
,
504 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
506 /* just sample as GLchan and convert to float here */
508 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
509 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
510 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
511 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
512 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
515 #if FEATURE_EXT_texture_sRGB
517 _mesa_fetch_texel_2d_f_srgb_dxt1( const struct gl_texture_image
*texImage
,
518 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
520 /* just sample as GLchan and convert to float here */
522 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
523 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
524 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
525 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
526 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
530 _mesa_fetch_texel_2d_f_srgba_dxt1(const struct gl_texture_image
*texImage
,
531 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
533 /* just sample as GLchan and convert to float here */
535 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
536 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
537 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
538 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
539 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
543 _mesa_fetch_texel_2d_f_srgba_dxt3(const struct gl_texture_image
*texImage
,
544 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
546 /* just sample as GLchan and convert to float here */
548 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
549 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
550 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
551 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
552 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
556 _mesa_fetch_texel_2d_f_srgba_dxt5(const struct gl_texture_image
*texImage
,
557 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
559 /* just sample as GLchan and convert to float here */
561 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
562 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
563 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
564 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
565 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);