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 "mfeatures.h"
44 #include "texcompress.h"
45 #include "texcompress_s3tc.h"
47 #include "swrast/s_context.h"
50 #if FEATURE_texture_s3tc
53 #if defined(_WIN32) || defined(WIN32)
54 #define DXTN_LIBNAME "dxtn.dll"
57 #elif defined(__DJGPP__)
58 #define DXTN_LIBNAME "dxtn.dxe"
60 #define DXTN_LIBNAME "libtxc_dxtn.so"
63 #if FEATURE_EXT_texture_sRGB
65 * Convert an 8-bit sRGB value from non-linear space to a
66 * linear RGB value in [0, 1].
67 * Implemented with a 256-entry lookup table.
70 nonlinear_to_linear(GLubyte cs8
)
72 static GLfloat table
[256];
73 static GLboolean tableReady
= GL_FALSE
;
75 /* compute lookup table now */
77 for (i
= 0; i
< 256; i
++) {
78 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
80 table
[i
] = cs
/ 12.92f
;
83 table
[i
] = (GLfloat
) pow((cs
+ 0.055) / 1.055, 2.4);
90 #endif /* FEATURE_EXT_texture_sRGB */
92 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
94 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
95 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
96 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
97 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
99 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
100 GLint height
, const GLubyte
*srcPixData
,
101 GLenum destformat
, GLubyte
*dest
,
104 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
106 static void *dxtlibhandle
= NULL
;
110 _mesa_init_texture_s3tc( struct gl_context
*ctx
)
112 /* called during context initialization */
113 ctx
->Mesa_DXTn
= GL_FALSE
;
114 #if USE_EXTERNAL_DXTN_LIB
116 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
118 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
119 "compression/decompression unavailable");
122 /* the fetch functions are not per context! Might be problematic... */
123 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
124 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
125 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
126 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
127 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
128 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
129 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
130 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
131 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
132 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
134 if (!fetch_ext_rgb_dxt1
||
135 !fetch_ext_rgba_dxt1
||
136 !fetch_ext_rgba_dxt3
||
137 !fetch_ext_rgba_dxt5
||
138 !ext_tx_compress_dxtn
) {
139 _mesa_warning(ctx
, "couldn't reference all symbols in "
140 DXTN_LIBNAME
", software DXTn compression/decompression "
142 fetch_ext_rgb_dxt1
= NULL
;
143 fetch_ext_rgba_dxt1
= NULL
;
144 fetch_ext_rgba_dxt3
= NULL
;
145 fetch_ext_rgba_dxt5
= NULL
;
146 ext_tx_compress_dxtn
= NULL
;
147 _mesa_dlclose(dxtlibhandle
);
153 ctx
->Mesa_DXTn
= GL_TRUE
;
161 * Store user's image in rgb_dxt1 format.
164 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
166 const GLubyte
*pixels
;
168 const GLubyte
*tempImage
= NULL
;
170 ASSERT(dstFormat
== MESA_FORMAT_RGB_DXT1
||
171 dstFormat
== MESA_FORMAT_SRGB_DXT1
);
173 if (srcFormat
!= GL_RGB
||
174 srcType
!= GL_UNSIGNED_BYTE
||
175 ctx
->_ImageTransferState
||
176 srcPacking
->RowLength
!= srcWidth
||
177 srcPacking
->SwapBytes
) {
178 /* convert image to RGB/GLubyte */
179 tempImage
= _mesa_make_temp_ubyte_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 */
191 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
192 srcFormat
, srcType
, 0, 0);
197 if (ext_tx_compress_dxtn
) {
198 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
199 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
203 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
206 free((void *) tempImage
);
213 * Store user's image in rgba_dxt1 format.
216 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
218 const GLubyte
*pixels
;
220 const GLubyte
*tempImage
= NULL
;
222 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT1
||
223 dstFormat
== MESA_FORMAT_SRGBA_DXT1
);
225 if (srcFormat
!= GL_RGBA
||
226 srcType
!= GL_UNSIGNED_BYTE
||
227 ctx
->_ImageTransferState
||
228 srcPacking
->RowLength
!= srcWidth
||
229 srcPacking
->SwapBytes
) {
230 /* convert image to RGBA/GLubyte */
231 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
233 _mesa_get_format_base_format(dstFormat
),
234 srcWidth
, srcHeight
, srcDepth
,
235 srcFormat
, srcType
, srcAddr
,
238 return GL_FALSE
; /* out of memory */
243 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
244 srcFormat
, srcType
, 0, 0);
249 if (ext_tx_compress_dxtn
) {
250 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
251 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
255 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
258 free((void*) tempImage
);
265 * Store user's image in rgba_dxt3 format.
268 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
270 const GLubyte
*pixels
;
272 const GLubyte
*tempImage
= NULL
;
274 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT3
||
275 dstFormat
== MESA_FORMAT_SRGBA_DXT3
);
277 if (srcFormat
!= GL_RGBA
||
278 srcType
!= GL_UNSIGNED_BYTE
||
279 ctx
->_ImageTransferState
||
280 srcPacking
->RowLength
!= srcWidth
||
281 srcPacking
->SwapBytes
) {
282 /* convert image to RGBA/GLubyte */
283 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
285 _mesa_get_format_base_format(dstFormat
),
286 srcWidth
, srcHeight
, srcDepth
,
287 srcFormat
, srcType
, srcAddr
,
290 return GL_FALSE
; /* out of memory */
294 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
295 srcFormat
, srcType
, 0, 0);
300 if (ext_tx_compress_dxtn
) {
301 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
302 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
306 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
309 free((void *) tempImage
);
316 * Store user's image in rgba_dxt5 format.
319 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
321 const GLubyte
*pixels
;
323 const GLubyte
*tempImage
= NULL
;
325 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT5
||
326 dstFormat
== MESA_FORMAT_SRGBA_DXT5
);
328 if (srcFormat
!= GL_RGBA
||
329 srcType
!= GL_UNSIGNED_BYTE
||
330 ctx
->_ImageTransferState
||
331 srcPacking
->RowLength
!= srcWidth
||
332 srcPacking
->SwapBytes
) {
333 /* convert image to RGBA/GLubyte */
334 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
336 _mesa_get_format_base_format(dstFormat
),
337 srcWidth
, srcHeight
, srcDepth
,
338 srcFormat
, srcType
, srcAddr
,
341 return GL_FALSE
; /* out of memory */
345 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
346 srcFormat
, srcType
, 0, 0);
351 if (ext_tx_compress_dxtn
) {
352 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
353 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
357 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
360 free((void *) tempImage
);
367 fetch_texel_2d_rgb_dxt1(const struct swrast_texture_image
*texImage
,
368 GLint i
, GLint j
, GLint k
, GLubyte
*texel
)
371 if (fetch_ext_rgb_dxt1
) {
372 GLint sliceOffset
= k
? texImage
->ImageOffsets
[k
] / 2 : 0;
373 fetch_ext_rgb_dxt1(texImage
->RowStride
,
374 texImage
->Map
+ sliceOffset
, i
, j
, texel
);
377 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
382 _mesa_fetch_texel_rgb_dxt1(const struct swrast_texture_image
*texImage
,
383 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
385 /* just sample as GLubyte and convert to float here */
387 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
388 texel
[RCOMP
] = UBYTE_TO_FLOAT(rgba
[RCOMP
]);
389 texel
[GCOMP
] = UBYTE_TO_FLOAT(rgba
[GCOMP
]);
390 texel
[BCOMP
] = UBYTE_TO_FLOAT(rgba
[BCOMP
]);
391 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
396 fetch_texel_2d_rgba_dxt1(const struct swrast_texture_image
*texImage
,
397 GLint i
, GLint j
, GLint k
, GLubyte
*texel
)
400 if (fetch_ext_rgba_dxt1
) {
401 GLint sliceOffset
= k
? texImage
->ImageOffsets
[k
] / 2 : 0;
402 fetch_ext_rgba_dxt1(texImage
->RowStride
,
403 texImage
->Map
+ sliceOffset
, i
, j
, texel
);
406 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
411 _mesa_fetch_texel_rgba_dxt1(const struct swrast_texture_image
*texImage
,
412 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
414 /* just sample as GLubyte and convert to float here */
416 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
417 texel
[RCOMP
] = UBYTE_TO_FLOAT(rgba
[RCOMP
]);
418 texel
[GCOMP
] = UBYTE_TO_FLOAT(rgba
[GCOMP
]);
419 texel
[BCOMP
] = UBYTE_TO_FLOAT(rgba
[BCOMP
]);
420 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
425 fetch_texel_2d_rgba_dxt3(const struct swrast_texture_image
*texImage
,
426 GLint i
, GLint j
, GLint k
, GLubyte
*texel
)
429 if (fetch_ext_rgba_dxt3
) {
430 GLint sliceOffset
= k
? texImage
->ImageOffsets
[k
] : 0;
431 fetch_ext_rgba_dxt3(texImage
->RowStride
,
432 texImage
->Map
+ sliceOffset
, i
, j
, texel
);
435 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
440 _mesa_fetch_texel_rgba_dxt3(const struct swrast_texture_image
*texImage
,
441 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
443 /* just sample as GLubyte and convert to float here */
445 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
446 texel
[RCOMP
] = UBYTE_TO_FLOAT(rgba
[RCOMP
]);
447 texel
[GCOMP
] = UBYTE_TO_FLOAT(rgba
[GCOMP
]);
448 texel
[BCOMP
] = UBYTE_TO_FLOAT(rgba
[BCOMP
]);
449 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
454 fetch_texel_2d_rgba_dxt5(const struct swrast_texture_image
*texImage
,
455 GLint i
, GLint j
, GLint k
, GLubyte
*texel
)
458 if (fetch_ext_rgba_dxt5
) {
459 GLint sliceOffset
= k
? texImage
->ImageOffsets
[k
] : 0;
460 fetch_ext_rgba_dxt5(texImage
->RowStride
,
461 texImage
->Map
+ sliceOffset
, i
, j
, texel
);
464 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
469 _mesa_fetch_texel_rgba_dxt5(const struct swrast_texture_image
*texImage
,
470 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
472 /* just sample as GLubyte and convert to float here */
474 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
475 texel
[RCOMP
] = UBYTE_TO_FLOAT(rgba
[RCOMP
]);
476 texel
[GCOMP
] = UBYTE_TO_FLOAT(rgba
[GCOMP
]);
477 texel
[BCOMP
] = UBYTE_TO_FLOAT(rgba
[BCOMP
]);
478 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
481 #if FEATURE_EXT_texture_sRGB
483 _mesa_fetch_texel_srgb_dxt1(const struct swrast_texture_image
*texImage
,
484 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
486 /* just sample as GLubyte and convert to float here */
488 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
489 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
490 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
491 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
492 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
496 _mesa_fetch_texel_srgba_dxt1(const struct swrast_texture_image
*texImage
,
497 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
499 /* just sample as GLubyte and convert to float here */
501 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
502 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
503 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
504 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
505 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
509 _mesa_fetch_texel_srgba_dxt3(const struct swrast_texture_image
*texImage
,
510 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
512 /* just sample as GLubyte and convert to float here */
514 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
515 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
516 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
517 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
518 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
522 _mesa_fetch_texel_srgba_dxt5(const struct swrast_texture_image
*texImage
,
523 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
525 /* just sample as GLubyte and convert to float here */
527 fetch_texel_2d_rgba_dxt5(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
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
533 #endif /* FEATURE_EXT_texture_sRGB */
536 #endif /* FEATURE_texture_s3tc */