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( struct gl_context
*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 dstFormat
== MESA_FORMAT_SRGB_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 */
191 srcRowStride
= 3 * srcWidth
;
195 pixels
= (const GLchan
*) srcAddr
;
196 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
197 srcType
) / sizeof(GLchan
);
200 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
202 texWidth
, (GLubyte
*) dstAddr
);
204 if (ext_tx_compress_dxtn
) {
205 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
206 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
210 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
214 free((void *) tempImage
);
221 * Store user's image in rgba_dxt1 format.
224 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
226 const GLchan
*pixels
;
229 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
230 const GLchan
*tempImage
= NULL
;
232 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT1
||
233 dstFormat
== MESA_FORMAT_SRGBA_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 */
254 srcRowStride
= 4 * srcWidth
;
258 pixels
= (const GLchan
*) srcAddr
;
259 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
260 srcType
) / sizeof(GLchan
);
263 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
265 texWidth
, (GLubyte
*) dstAddr
);
266 if (ext_tx_compress_dxtn
) {
267 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
268 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
272 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
276 free((void*) tempImage
);
283 * Store user's image in rgba_dxt3 format.
286 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
288 const GLchan
*pixels
;
291 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
292 const GLchan
*tempImage
= NULL
;
294 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT3
||
295 dstFormat
== MESA_FORMAT_SRGBA_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 */
316 srcRowStride
= 4 * srcWidth
;
319 pixels
= (const GLchan
*) srcAddr
;
320 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
321 srcType
) / sizeof(GLchan
);
324 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
326 texWidth
, (GLubyte
*) dstAddr
);
327 if (ext_tx_compress_dxtn
) {
328 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
329 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
333 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
337 free((void *) tempImage
);
344 * Store user's image in rgba_dxt5 format.
347 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
349 const GLchan
*pixels
;
352 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
353 const GLchan
*tempImage
= NULL
;
355 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT5
||
356 dstFormat
== MESA_FORMAT_SRGBA_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 */
377 srcRowStride
= 4 * srcWidth
;
380 pixels
= (const GLchan
*) srcAddr
;
381 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
382 srcType
) / sizeof(GLchan
);
385 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
387 texWidth
, (GLubyte
*) dstAddr
);
388 if (ext_tx_compress_dxtn
) {
389 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
390 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
394 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
398 free((void *) tempImage
);
405 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
406 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
409 if (fetch_ext_rgb_dxt1
) {
410 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
411 fetch_ext_rgb_dxt1(texImage
->RowStride
,
412 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
415 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
420 _mesa_fetch_texel_2d_f_rgb_dxt1(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_rgb_dxt1(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_dxt1( const struct gl_texture_image
*texImage
,
435 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
438 if (fetch_ext_rgba_dxt1
) {
439 fetch_ext_rgba_dxt1(texImage
->RowStride
,
440 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
443 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
448 _mesa_fetch_texel_2d_f_rgba_dxt1(const struct gl_texture_image
*texImage
,
449 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
451 /* just sample as GLchan and convert to float here */
453 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
454 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
455 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
456 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
457 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
462 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
463 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
466 if (fetch_ext_rgba_dxt3
) {
467 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
468 fetch_ext_rgba_dxt3(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
472 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
477 _mesa_fetch_texel_2d_f_rgba_dxt3(const struct gl_texture_image
*texImage
,
478 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
480 /* just sample as GLchan and convert to float here */
482 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
483 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
484 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
485 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
486 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
491 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image
*texImage
,
492 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
495 if (fetch_ext_rgba_dxt5
) {
496 fetch_ext_rgba_dxt5(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
500 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
505 _mesa_fetch_texel_2d_f_rgba_dxt5(const struct gl_texture_image
*texImage
,
506 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
508 /* just sample as GLchan and convert to float here */
510 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
511 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
512 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
513 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
514 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
517 #if FEATURE_EXT_texture_sRGB
519 _mesa_fetch_texel_2d_f_srgb_dxt1( const struct gl_texture_image
*texImage
,
520 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
522 /* just sample as GLchan and convert to float here */
524 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
525 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
526 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
527 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
528 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
532 _mesa_fetch_texel_2d_f_srgba_dxt1(const struct gl_texture_image
*texImage
,
533 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
535 /* just sample as GLchan and convert to float here */
537 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
538 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
539 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
540 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
541 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
545 _mesa_fetch_texel_2d_f_srgba_dxt3(const struct gl_texture_image
*texImage
,
546 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
548 /* just sample as GLchan and convert to float here */
550 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
551 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
552 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
553 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
554 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
558 _mesa_fetch_texel_2d_f_srgba_dxt5(const struct gl_texture_image
*texImage
,
559 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
561 /* just sample as GLchan and convert to float here */
563 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
564 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
565 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
566 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
567 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
569 #endif /* FEATURE_EXT_texture_sRGB */
572 #endif /* FEATURE_texture_s3tc */