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 #if FEATURE_texture_s3tc
51 #if defined(_WIN32) || defined(WIN32)
52 #define DXTN_LIBNAME "dxtn.dll"
55 #elif defined(__DJGPP__)
56 #define DXTN_LIBNAME "dxtn.dxe"
58 #define DXTN_LIBNAME "libtxc_dxtn.so"
61 #if FEATURE_EXT_texture_sRGB
63 * Convert an 8-bit sRGB value from non-linear space to a
64 * linear RGB value in [0, 1].
65 * Implemented with a 256-entry lookup table.
68 nonlinear_to_linear(GLubyte cs8
)
70 static GLfloat table
[256];
71 static GLboolean tableReady
= GL_FALSE
;
73 /* compute lookup table now */
75 for (i
= 0; i
< 256; i
++) {
76 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
78 table
[i
] = cs
/ 12.92f
;
81 table
[i
] = (GLfloat
) pow((cs
+ 0.055) / 1.055, 2.4);
88 #endif /* FEATURE_EXT_texture_sRGB */
90 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
92 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
93 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
94 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
95 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
97 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
98 GLint height
, const GLchan
*srcPixData
,
99 GLenum destformat
, GLubyte
*dest
,
102 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
104 static void *dxtlibhandle
= NULL
;
108 _mesa_init_texture_s3tc( GLcontext
*ctx
)
110 /* called during context initialization */
111 ctx
->Mesa_DXTn
= GL_FALSE
;
112 #if USE_EXTERNAL_DXTN_LIB
114 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
116 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
117 "compression/decompression unavailable");
120 /* the fetch functions are not per context! Might be problematic... */
121 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
122 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
123 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
124 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
125 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
126 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
127 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
128 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
129 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
130 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
132 if (!fetch_ext_rgb_dxt1
||
133 !fetch_ext_rgba_dxt1
||
134 !fetch_ext_rgba_dxt3
||
135 !fetch_ext_rgba_dxt5
||
136 !ext_tx_compress_dxtn
) {
137 _mesa_warning(ctx
, "couldn't reference all symbols in "
138 DXTN_LIBNAME
", software DXTn compression/decompression "
140 fetch_ext_rgb_dxt1
= NULL
;
141 fetch_ext_rgba_dxt1
= NULL
;
142 fetch_ext_rgba_dxt3
= NULL
;
143 fetch_ext_rgba_dxt5
= NULL
;
144 ext_tx_compress_dxtn
= NULL
;
145 _mesa_dlclose(dxtlibhandle
);
151 ctx
->Mesa_DXTn
= GL_TRUE
;
159 * Store user's image in rgb_dxt1 format.
162 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
164 const GLchan
*pixels
;
167 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
168 const GLchan
*tempImage
= NULL
;
170 ASSERT(dstFormat
== MESA_FORMAT_RGB_DXT1
);
171 ASSERT(dstXoffset
% 4 == 0);
172 ASSERT(dstYoffset
% 4 == 0);
173 ASSERT(dstZoffset
% 4 == 0);
175 (void) dstImageOffsets
;
177 if (srcFormat
!= GL_RGB
||
178 srcType
!= CHAN_TYPE
||
179 ctx
->_ImageTransferState
||
180 srcPacking
->SwapBytes
) {
181 /* convert image to RGB/GLchan */
182 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
184 _mesa_get_format_base_format(dstFormat
),
185 srcWidth
, srcHeight
, srcDepth
,
186 srcFormat
, srcType
, srcAddr
,
189 return GL_FALSE
; /* out of memory */
190 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
192 srcRowStride
= 3 * srcWidth
;
196 pixels
= (const GLchan
*) srcAddr
;
197 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
198 srcType
) / sizeof(GLchan
);
201 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
203 texWidth
, (GLubyte
*) dstAddr
);
205 if (ext_tx_compress_dxtn
) {
206 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
207 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
211 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
215 free((void *) tempImage
);
222 * Store user's image in rgba_dxt1 format.
225 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
227 const GLchan
*pixels
;
230 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
231 const GLchan
*tempImage
= NULL
;
233 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT1
);
234 ASSERT(dstXoffset
% 4 == 0);
235 ASSERT(dstYoffset
% 4 == 0);
236 ASSERT(dstZoffset
% 4 == 0);
238 (void) dstImageOffsets
;
240 if (srcFormat
!= GL_RGBA
||
241 srcType
!= CHAN_TYPE
||
242 ctx
->_ImageTransferState
||
243 srcPacking
->SwapBytes
) {
244 /* convert image to RGBA/GLchan */
245 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
247 _mesa_get_format_base_format(dstFormat
),
248 srcWidth
, srcHeight
, srcDepth
,
249 srcFormat
, srcType
, srcAddr
,
252 return GL_FALSE
; /* out of memory */
253 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
255 srcRowStride
= 4 * srcWidth
;
259 pixels
= (const GLchan
*) srcAddr
;
260 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
261 srcType
) / sizeof(GLchan
);
264 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
266 texWidth
, (GLubyte
*) dstAddr
);
267 if (ext_tx_compress_dxtn
) {
268 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
269 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
273 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
277 free((void*) tempImage
);
284 * Store user's image in rgba_dxt3 format.
287 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
289 const GLchan
*pixels
;
292 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
293 const GLchan
*tempImage
= NULL
;
295 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT3
);
296 ASSERT(dstXoffset
% 4 == 0);
297 ASSERT(dstYoffset
% 4 == 0);
298 ASSERT(dstZoffset
% 4 == 0);
300 (void) dstImageOffsets
;
302 if (srcFormat
!= GL_RGBA
||
303 srcType
!= CHAN_TYPE
||
304 ctx
->_ImageTransferState
||
305 srcPacking
->SwapBytes
) {
306 /* convert image to RGBA/GLchan */
307 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
309 _mesa_get_format_base_format(dstFormat
),
310 srcWidth
, srcHeight
, srcDepth
,
311 srcFormat
, srcType
, srcAddr
,
314 return GL_FALSE
; /* out of memory */
315 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
317 srcRowStride
= 4 * srcWidth
;
320 pixels
= (const GLchan
*) srcAddr
;
321 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
322 srcType
) / sizeof(GLchan
);
325 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
327 texWidth
, (GLubyte
*) dstAddr
);
328 if (ext_tx_compress_dxtn
) {
329 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
330 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
334 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
338 free((void *) tempImage
);
345 * Store user's image in rgba_dxt5 format.
348 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
350 const GLchan
*pixels
;
353 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
354 const GLchan
*tempImage
= NULL
;
356 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT5
);
357 ASSERT(dstXoffset
% 4 == 0);
358 ASSERT(dstYoffset
% 4 == 0);
359 ASSERT(dstZoffset
% 4 == 0);
361 (void) dstImageOffsets
;
363 if (srcFormat
!= GL_RGBA
||
364 srcType
!= CHAN_TYPE
||
365 ctx
->_ImageTransferState
||
366 srcPacking
->SwapBytes
) {
367 /* convert image to RGBA/GLchan */
368 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
370 _mesa_get_format_base_format(dstFormat
),
371 srcWidth
, srcHeight
, srcDepth
,
372 srcFormat
, srcType
, srcAddr
,
375 return GL_FALSE
; /* out of memory */
376 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
378 srcRowStride
= 4 * srcWidth
;
381 pixels
= (const GLchan
*) srcAddr
;
382 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
383 srcType
) / sizeof(GLchan
);
386 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
388 texWidth
, (GLubyte
*) dstAddr
);
389 if (ext_tx_compress_dxtn
) {
390 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
391 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
395 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
399 free((void *) tempImage
);
406 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
407 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
410 if (fetch_ext_rgb_dxt1
) {
411 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
412 fetch_ext_rgb_dxt1(texImage
->RowStride
,
413 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
416 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
421 _mesa_fetch_texel_2d_f_rgb_dxt1(const struct gl_texture_image
*texImage
,
422 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
424 /* just sample as GLchan and convert to float here */
426 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
427 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
428 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
429 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
430 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
435 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image
*texImage
,
436 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
439 if (fetch_ext_rgba_dxt1
) {
440 fetch_ext_rgba_dxt1(texImage
->RowStride
,
441 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
444 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
449 _mesa_fetch_texel_2d_f_rgba_dxt1(const struct gl_texture_image
*texImage
,
450 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
452 /* just sample as GLchan and convert to float here */
454 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
455 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
456 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
457 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
458 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
463 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
464 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
467 if (fetch_ext_rgba_dxt3
) {
468 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
469 fetch_ext_rgba_dxt3(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
473 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
478 _mesa_fetch_texel_2d_f_rgba_dxt3(const struct gl_texture_image
*texImage
,
479 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
481 /* just sample as GLchan and convert to float here */
483 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
484 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
485 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
486 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
487 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
492 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image
*texImage
,
493 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
496 if (fetch_ext_rgba_dxt5
) {
497 fetch_ext_rgba_dxt5(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
501 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
506 _mesa_fetch_texel_2d_f_rgba_dxt5(const struct gl_texture_image
*texImage
,
507 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
509 /* just sample as GLchan and convert to float here */
511 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
512 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
513 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
514 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
515 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
518 #if FEATURE_EXT_texture_sRGB
520 _mesa_fetch_texel_2d_f_srgb_dxt1( const struct gl_texture_image
*texImage
,
521 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
523 /* just sample as GLchan and convert to float here */
525 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
526 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
527 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
528 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
529 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
533 _mesa_fetch_texel_2d_f_srgba_dxt1(const struct gl_texture_image
*texImage
,
534 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
536 /* just sample as GLchan and convert to float here */
538 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
539 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
540 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
541 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
542 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
546 _mesa_fetch_texel_2d_f_srgba_dxt3(const struct gl_texture_image
*texImage
,
547 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
549 /* just sample as GLchan and convert to float here */
551 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
552 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
553 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
554 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
555 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
559 _mesa_fetch_texel_2d_f_srgba_dxt5(const struct gl_texture_image
*texImage
,
560 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
562 /* just sample as GLchan and convert to float here */
564 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
565 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
566 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
567 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
568 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
570 #endif /* FEATURE_EXT_texture_sRGB */
573 #endif /* FEATURE_texture_s3tc */