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
44 #include "texcompress.h"
45 #include "texcompress_s3tc.h"
49 #if FEATURE_texture_s3tc
52 #if defined(_WIN32) || defined(WIN32)
53 #define DXTN_LIBNAME "dxtn.dll"
56 #elif defined(__DJGPP__)
57 #define DXTN_LIBNAME "dxtn.dxe"
59 #define DXTN_LIBNAME "libtxc_dxtn.so"
62 #if FEATURE_EXT_texture_sRGB
64 * Convert an 8-bit sRGB value from non-linear space to a
65 * linear RGB value in [0, 1].
66 * Implemented with a 256-entry lookup table.
69 nonlinear_to_linear(GLubyte cs8
)
71 static GLfloat table
[256];
72 static GLboolean tableReady
= GL_FALSE
;
74 /* compute lookup table now */
76 for (i
= 0; i
< 256; i
++) {
77 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
79 table
[i
] = cs
/ 12.92f
;
82 table
[i
] = (GLfloat
) pow((cs
+ 0.055) / 1.055, 2.4);
89 #endif /* FEATURE_EXT_texture_sRGB */
91 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
93 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
94 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
95 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
96 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
98 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
99 GLint height
, const GLchan
*srcPixData
,
100 GLenum destformat
, GLubyte
*dest
,
103 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
105 static void *dxtlibhandle
= NULL
;
109 _mesa_init_texture_s3tc( GLcontext
*ctx
)
111 /* called during context initialization */
112 ctx
->Mesa_DXTn
= GL_FALSE
;
113 #if USE_EXTERNAL_DXTN_LIB
115 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
117 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
118 "compression/decompression unavailable");
121 /* the fetch functions are not per context! Might be problematic... */
122 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
123 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
124 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
125 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
126 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
127 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
128 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
129 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
130 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
131 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
133 if (!fetch_ext_rgb_dxt1
||
134 !fetch_ext_rgba_dxt1
||
135 !fetch_ext_rgba_dxt3
||
136 !fetch_ext_rgba_dxt5
||
137 !ext_tx_compress_dxtn
) {
138 _mesa_warning(ctx
, "couldn't reference all symbols in "
139 DXTN_LIBNAME
", software DXTn compression/decompression "
141 fetch_ext_rgb_dxt1
= NULL
;
142 fetch_ext_rgba_dxt1
= NULL
;
143 fetch_ext_rgba_dxt3
= NULL
;
144 fetch_ext_rgba_dxt5
= NULL
;
145 ext_tx_compress_dxtn
= NULL
;
146 _mesa_dlclose(dxtlibhandle
);
152 ctx
->Mesa_DXTn
= GL_TRUE
;
153 _mesa_warning(ctx
, "software DXTn compression/decompression available");
161 * Store user's image in rgb_dxt1 format.
164 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
166 const GLchan
*pixels
;
169 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
170 const GLchan
*tempImage
= NULL
;
172 ASSERT(dstFormat
== MESA_FORMAT_RGB_DXT1
);
173 ASSERT(dstXoffset
% 4 == 0);
174 ASSERT(dstYoffset
% 4 == 0);
175 ASSERT(dstZoffset
% 4 == 0);
177 (void) dstImageOffsets
;
179 if (srcFormat
!= GL_RGB
||
180 srcType
!= CHAN_TYPE
||
181 ctx
->_ImageTransferState
||
182 srcPacking
->SwapBytes
) {
183 /* convert image to RGB/GLchan */
184 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
186 _mesa_get_format_base_format(dstFormat
),
187 srcWidth
, srcHeight
, srcDepth
,
188 srcFormat
, srcType
, srcAddr
,
191 return GL_FALSE
; /* out of memory */
192 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
194 srcRowStride
= 3 * srcWidth
;
198 pixels
= (const GLchan
*) srcAddr
;
199 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
200 srcType
) / sizeof(GLchan
);
203 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
205 texWidth
, (GLubyte
*) dstAddr
);
207 if (ext_tx_compress_dxtn
) {
208 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
209 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
213 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
217 free((void *) tempImage
);
224 * Store user's image in rgba_dxt1 format.
227 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
229 const GLchan
*pixels
;
232 const GLint texWidth
= dstRowStride
* 4 / 8; /* a bit of a hack */
233 const GLchan
*tempImage
= NULL
;
235 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT1
);
236 ASSERT(dstXoffset
% 4 == 0);
237 ASSERT(dstYoffset
% 4 == 0);
238 ASSERT(dstZoffset
% 4 == 0);
240 (void) dstImageOffsets
;
242 if (srcFormat
!= GL_RGBA
||
243 srcType
!= CHAN_TYPE
||
244 ctx
->_ImageTransferState
||
245 srcPacking
->SwapBytes
) {
246 /* convert image to RGBA/GLchan */
247 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
249 _mesa_get_format_base_format(dstFormat
),
250 srcWidth
, srcHeight
, srcDepth
,
251 srcFormat
, srcType
, srcAddr
,
254 return GL_FALSE
; /* out of memory */
255 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
257 srcRowStride
= 4 * srcWidth
;
261 pixels
= (const GLchan
*) srcAddr
;
262 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
263 srcType
) / sizeof(GLchan
);
266 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
268 texWidth
, (GLubyte
*) dstAddr
);
269 if (ext_tx_compress_dxtn
) {
270 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
271 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
275 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
279 free((void*) tempImage
);
286 * Store user's image in rgba_dxt3 format.
289 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
291 const GLchan
*pixels
;
294 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
295 const GLchan
*tempImage
= NULL
;
297 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT3
);
298 ASSERT(dstXoffset
% 4 == 0);
299 ASSERT(dstYoffset
% 4 == 0);
300 ASSERT(dstZoffset
% 4 == 0);
302 (void) dstImageOffsets
;
304 if (srcFormat
!= GL_RGBA
||
305 srcType
!= CHAN_TYPE
||
306 ctx
->_ImageTransferState
||
307 srcPacking
->SwapBytes
) {
308 /* convert image to RGBA/GLchan */
309 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
311 _mesa_get_format_base_format(dstFormat
),
312 srcWidth
, srcHeight
, srcDepth
,
313 srcFormat
, srcType
, srcAddr
,
316 return GL_FALSE
; /* out of memory */
317 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
319 srcRowStride
= 4 * srcWidth
;
322 pixels
= (const GLchan
*) srcAddr
;
323 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
324 srcType
) / sizeof(GLchan
);
327 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
329 texWidth
, (GLubyte
*) dstAddr
);
330 if (ext_tx_compress_dxtn
) {
331 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
332 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
336 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
340 free((void *) tempImage
);
347 * Store user's image in rgba_dxt5 format.
350 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
352 const GLchan
*pixels
;
355 const GLint texWidth
= dstRowStride
* 4 / 16; /* a bit of a hack */
356 const GLchan
*tempImage
= NULL
;
358 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT5
);
359 ASSERT(dstXoffset
% 4 == 0);
360 ASSERT(dstYoffset
% 4 == 0);
361 ASSERT(dstZoffset
% 4 == 0);
363 (void) dstImageOffsets
;
365 if (srcFormat
!= GL_RGBA
||
366 srcType
!= CHAN_TYPE
||
367 ctx
->_ImageTransferState
||
368 srcPacking
->SwapBytes
) {
369 /* convert image to RGBA/GLchan */
370 tempImage
= _mesa_make_temp_chan_image(ctx
, dims
,
372 _mesa_get_format_base_format(dstFormat
),
373 srcWidth
, srcHeight
, srcDepth
,
374 srcFormat
, srcType
, srcAddr
,
377 return GL_FALSE
; /* out of memory */
378 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
380 srcRowStride
= 4 * srcWidth
;
383 pixels
= (const GLchan
*) srcAddr
;
384 srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
,
385 srcType
) / sizeof(GLchan
);
388 dst
= _mesa_compressed_image_address(dstXoffset
, dstYoffset
, 0,
390 texWidth
, (GLubyte
*) dstAddr
);
391 if (ext_tx_compress_dxtn
) {
392 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
393 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
397 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
401 free((void *) tempImage
);
408 fetch_texel_2d_rgb_dxt1( const struct gl_texture_image
*texImage
,
409 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
412 if (fetch_ext_rgb_dxt1
) {
413 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
414 fetch_ext_rgb_dxt1(texImage
->RowStride
,
415 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
418 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
423 _mesa_fetch_texel_2d_f_rgb_dxt1(const struct gl_texture_image
*texImage
,
424 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
426 /* just sample as GLchan and convert to float here */
428 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
429 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
430 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
431 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
432 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
437 fetch_texel_2d_rgba_dxt1( const struct gl_texture_image
*texImage
,
438 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
441 if (fetch_ext_rgba_dxt1
) {
442 fetch_ext_rgba_dxt1(texImage
->RowStride
,
443 (GLubyte
*)(texImage
)->Data
, i
, j
, texel
);
446 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
451 _mesa_fetch_texel_2d_f_rgba_dxt1(const struct gl_texture_image
*texImage
,
452 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
454 /* just sample as GLchan and convert to float here */
456 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
457 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
458 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
459 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
460 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
465 fetch_texel_2d_rgba_dxt3( const struct gl_texture_image
*texImage
,
466 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
469 if (fetch_ext_rgba_dxt3
) {
470 ASSERT (sizeof(GLchan
) == sizeof(GLubyte
));
471 fetch_ext_rgba_dxt3(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
475 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
480 _mesa_fetch_texel_2d_f_rgba_dxt3(const struct gl_texture_image
*texImage
,
481 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
483 /* just sample as GLchan and convert to float here */
485 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
486 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
487 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
488 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
489 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
494 fetch_texel_2d_rgba_dxt5( const struct gl_texture_image
*texImage
,
495 GLint i
, GLint j
, GLint k
, GLchan
*texel
)
498 if (fetch_ext_rgba_dxt5
) {
499 fetch_ext_rgba_dxt5(texImage
->RowStride
, (GLubyte
*)(texImage
)->Data
,
503 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
508 _mesa_fetch_texel_2d_f_rgba_dxt5(const struct gl_texture_image
*texImage
,
509 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
511 /* just sample as GLchan and convert to float here */
513 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
514 texel
[RCOMP
] = CHAN_TO_FLOAT(rgba
[RCOMP
]);
515 texel
[GCOMP
] = CHAN_TO_FLOAT(rgba
[GCOMP
]);
516 texel
[BCOMP
] = CHAN_TO_FLOAT(rgba
[BCOMP
]);
517 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
520 #if FEATURE_EXT_texture_sRGB
522 _mesa_fetch_texel_2d_f_srgb_dxt1( const struct gl_texture_image
*texImage
,
523 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
525 /* just sample as GLchan and convert to float here */
527 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
528 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
529 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
530 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
531 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
535 _mesa_fetch_texel_2d_f_srgba_dxt1(const struct gl_texture_image
*texImage
,
536 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
538 /* just sample as GLchan and convert to float here */
540 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
541 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
542 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
543 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
544 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
548 _mesa_fetch_texel_2d_f_srgba_dxt3(const struct gl_texture_image
*texImage
,
549 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
551 /* just sample as GLchan and convert to float here */
553 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
554 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
555 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
556 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
557 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
561 _mesa_fetch_texel_2d_f_srgba_dxt5(const struct gl_texture_image
*texImage
,
562 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
564 /* just sample as GLchan and convert to float here */
566 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
567 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
568 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
569 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
570 texel
[ACOMP
] = CHAN_TO_FLOAT(rgba
[ACOMP
]);
572 #endif /* FEATURE_EXT_texture_sRGB */
575 #endif /* FEATURE_texture_s3tc */