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 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"
61 * Convert an 8-bit sRGB value from non-linear space to a
62 * linear RGB value in [0, 1].
63 * Implemented with a 256-entry lookup table.
66 nonlinear_to_linear(GLubyte cs8
)
68 static GLfloat table
[256];
69 static GLboolean tableReady
= GL_FALSE
;
71 /* compute lookup table now */
73 for (i
= 0; i
< 256; i
++) {
74 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
76 table
[i
] = cs
/ 12.92f
;
79 table
[i
] = (GLfloat
) pow((cs
+ 0.055) / 1.055, 2.4);
87 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
89 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
90 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
91 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
92 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
94 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
95 GLint height
, const GLubyte
*srcPixData
,
96 GLenum destformat
, GLubyte
*dest
,
99 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
101 static void *dxtlibhandle
= NULL
;
105 _mesa_init_texture_s3tc( struct gl_context
*ctx
)
107 /* called during context initialization */
108 ctx
->Mesa_DXTn
= GL_FALSE
;
109 #if USE_EXTERNAL_DXTN_LIB
111 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
113 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
114 "compression/decompression unavailable");
117 /* the fetch functions are not per context! Might be problematic... */
118 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
119 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
120 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
121 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
122 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
123 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
124 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
125 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
126 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
127 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
129 if (!fetch_ext_rgb_dxt1
||
130 !fetch_ext_rgba_dxt1
||
131 !fetch_ext_rgba_dxt3
||
132 !fetch_ext_rgba_dxt5
||
133 !ext_tx_compress_dxtn
) {
134 _mesa_warning(ctx
, "couldn't reference all symbols in "
135 DXTN_LIBNAME
", software DXTn compression/decompression "
137 fetch_ext_rgb_dxt1
= NULL
;
138 fetch_ext_rgba_dxt1
= NULL
;
139 fetch_ext_rgba_dxt3
= NULL
;
140 fetch_ext_rgba_dxt5
= NULL
;
141 ext_tx_compress_dxtn
= NULL
;
142 _mesa_dlclose(dxtlibhandle
);
148 ctx
->Mesa_DXTn
= GL_TRUE
;
156 * Store user's image in rgb_dxt1 format.
159 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
161 const GLubyte
*pixels
;
163 const GLubyte
*tempImage
= NULL
;
165 ASSERT(dstFormat
== MESA_FORMAT_RGB_DXT1
||
166 dstFormat
== MESA_FORMAT_SRGB_DXT1
);
168 if (srcFormat
!= GL_RGB
||
169 srcType
!= GL_UNSIGNED_BYTE
||
170 ctx
->_ImageTransferState
||
171 srcPacking
->RowLength
!= srcWidth
||
172 srcPacking
->SwapBytes
) {
173 /* convert image to RGB/GLubyte */
174 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
176 _mesa_get_format_base_format(dstFormat
),
177 srcWidth
, srcHeight
, srcDepth
,
178 srcFormat
, srcType
, srcAddr
,
181 return GL_FALSE
; /* out of memory */
186 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
187 srcFormat
, srcType
, 0, 0);
192 if (ext_tx_compress_dxtn
) {
193 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
194 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
198 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
201 free((void *) tempImage
);
208 * Store user's image in rgba_dxt1 format.
211 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
213 const GLubyte
*pixels
;
215 const GLubyte
*tempImage
= NULL
;
217 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT1
||
218 dstFormat
== MESA_FORMAT_SRGBA_DXT1
);
220 if (srcFormat
!= GL_RGBA
||
221 srcType
!= GL_UNSIGNED_BYTE
||
222 ctx
->_ImageTransferState
||
223 srcPacking
->RowLength
!= srcWidth
||
224 srcPacking
->SwapBytes
) {
225 /* convert image to RGBA/GLubyte */
226 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
228 _mesa_get_format_base_format(dstFormat
),
229 srcWidth
, srcHeight
, srcDepth
,
230 srcFormat
, srcType
, srcAddr
,
233 return GL_FALSE
; /* out of memory */
238 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
239 srcFormat
, srcType
, 0, 0);
244 if (ext_tx_compress_dxtn
) {
245 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
246 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
250 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
253 free((void*) tempImage
);
260 * Store user's image in rgba_dxt3 format.
263 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
265 const GLubyte
*pixels
;
267 const GLubyte
*tempImage
= NULL
;
269 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT3
||
270 dstFormat
== MESA_FORMAT_SRGBA_DXT3
);
272 if (srcFormat
!= GL_RGBA
||
273 srcType
!= GL_UNSIGNED_BYTE
||
274 ctx
->_ImageTransferState
||
275 srcPacking
->RowLength
!= srcWidth
||
276 srcPacking
->SwapBytes
) {
277 /* convert image to RGBA/GLubyte */
278 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
280 _mesa_get_format_base_format(dstFormat
),
281 srcWidth
, srcHeight
, srcDepth
,
282 srcFormat
, srcType
, srcAddr
,
285 return GL_FALSE
; /* out of memory */
289 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
290 srcFormat
, srcType
, 0, 0);
295 if (ext_tx_compress_dxtn
) {
296 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
297 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
301 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
304 free((void *) tempImage
);
311 * Store user's image in rgba_dxt5 format.
314 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
316 const GLubyte
*pixels
;
318 const GLubyte
*tempImage
= NULL
;
320 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT5
||
321 dstFormat
== MESA_FORMAT_SRGBA_DXT5
);
323 if (srcFormat
!= GL_RGBA
||
324 srcType
!= GL_UNSIGNED_BYTE
||
325 ctx
->_ImageTransferState
||
326 srcPacking
->RowLength
!= srcWidth
||
327 srcPacking
->SwapBytes
) {
328 /* convert image to RGBA/GLubyte */
329 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
331 _mesa_get_format_base_format(dstFormat
),
332 srcWidth
, srcHeight
, srcDepth
,
333 srcFormat
, srcType
, srcAddr
,
336 return GL_FALSE
; /* out of memory */
340 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
341 srcFormat
, srcType
, 0, 0);
346 if (ext_tx_compress_dxtn
) {
347 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
348 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
352 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
355 free((void *) tempImage
);
362 fetch_texel_2d_rgb_dxt1(const struct swrast_texture_image
*texImage
,
363 GLint i
, GLint j
, GLint k
, GLubyte
*texel
)
365 if (fetch_ext_rgb_dxt1
) {
366 GLint sliceOffset
= k
? texImage
->ImageOffsets
[k
] / 2 : 0;
367 fetch_ext_rgb_dxt1(texImage
->RowStride
,
368 texImage
->Map
+ sliceOffset
, i
, j
, texel
);
371 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
376 _mesa_fetch_texel_rgb_dxt1(const struct swrast_texture_image
*texImage
,
377 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
379 /* just sample as GLubyte and convert to float here */
381 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
382 texel
[RCOMP
] = UBYTE_TO_FLOAT(rgba
[RCOMP
]);
383 texel
[GCOMP
] = UBYTE_TO_FLOAT(rgba
[GCOMP
]);
384 texel
[BCOMP
] = UBYTE_TO_FLOAT(rgba
[BCOMP
]);
385 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
390 fetch_texel_2d_rgba_dxt1(const struct swrast_texture_image
*texImage
,
391 GLint i
, GLint j
, GLint k
, GLubyte
*texel
)
393 if (fetch_ext_rgba_dxt1
) {
394 GLint sliceOffset
= k
? texImage
->ImageOffsets
[k
] / 2 : 0;
395 fetch_ext_rgba_dxt1(texImage
->RowStride
,
396 texImage
->Map
+ sliceOffset
, i
, j
, texel
);
399 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
404 _mesa_fetch_texel_rgba_dxt1(const struct swrast_texture_image
*texImage
,
405 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
407 /* just sample as GLubyte and convert to float here */
409 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
410 texel
[RCOMP
] = UBYTE_TO_FLOAT(rgba
[RCOMP
]);
411 texel
[GCOMP
] = UBYTE_TO_FLOAT(rgba
[GCOMP
]);
412 texel
[BCOMP
] = UBYTE_TO_FLOAT(rgba
[BCOMP
]);
413 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
418 fetch_texel_2d_rgba_dxt3(const struct swrast_texture_image
*texImage
,
419 GLint i
, GLint j
, GLint k
, GLubyte
*texel
)
421 if (fetch_ext_rgba_dxt3
) {
422 GLint sliceOffset
= k
? texImage
->ImageOffsets
[k
] : 0;
423 fetch_ext_rgba_dxt3(texImage
->RowStride
,
424 texImage
->Map
+ sliceOffset
, i
, j
, texel
);
427 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
432 _mesa_fetch_texel_rgba_dxt3(const struct swrast_texture_image
*texImage
,
433 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
435 /* just sample as GLubyte and convert to float here */
437 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
438 texel
[RCOMP
] = UBYTE_TO_FLOAT(rgba
[RCOMP
]);
439 texel
[GCOMP
] = UBYTE_TO_FLOAT(rgba
[GCOMP
]);
440 texel
[BCOMP
] = UBYTE_TO_FLOAT(rgba
[BCOMP
]);
441 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
446 fetch_texel_2d_rgba_dxt5(const struct swrast_texture_image
*texImage
,
447 GLint i
, GLint j
, GLint k
, GLubyte
*texel
)
449 if (fetch_ext_rgba_dxt5
) {
450 GLint sliceOffset
= k
? texImage
->ImageOffsets
[k
] : 0;
451 fetch_ext_rgba_dxt5(texImage
->RowStride
,
452 texImage
->Map
+ sliceOffset
, i
, j
, texel
);
455 _mesa_debug(NULL
, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
460 _mesa_fetch_texel_rgba_dxt5(const struct swrast_texture_image
*texImage
,
461 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
463 /* just sample as GLubyte and convert to float here */
465 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
466 texel
[RCOMP
] = UBYTE_TO_FLOAT(rgba
[RCOMP
]);
467 texel
[GCOMP
] = UBYTE_TO_FLOAT(rgba
[GCOMP
]);
468 texel
[BCOMP
] = UBYTE_TO_FLOAT(rgba
[BCOMP
]);
469 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
473 _mesa_fetch_texel_srgb_dxt1(const struct swrast_texture_image
*texImage
,
474 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
476 /* just sample as GLubyte and convert to float here */
478 fetch_texel_2d_rgb_dxt1(texImage
, i
, j
, k
, rgba
);
479 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
480 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
481 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
482 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
486 _mesa_fetch_texel_srgba_dxt1(const struct swrast_texture_image
*texImage
,
487 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
489 /* just sample as GLubyte and convert to float here */
491 fetch_texel_2d_rgba_dxt1(texImage
, i
, j
, k
, rgba
);
492 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
493 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
494 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
495 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
499 _mesa_fetch_texel_srgba_dxt3(const struct swrast_texture_image
*texImage
,
500 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
502 /* just sample as GLubyte and convert to float here */
504 fetch_texel_2d_rgba_dxt3(texImage
, i
, j
, k
, rgba
);
505 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
506 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
507 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
508 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);
512 _mesa_fetch_texel_srgba_dxt5(const struct swrast_texture_image
*texImage
,
513 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
515 /* just sample as GLubyte and convert to float here */
517 fetch_texel_2d_rgba_dxt5(texImage
, i
, j
, k
, rgba
);
518 texel
[RCOMP
] = nonlinear_to_linear(rgba
[RCOMP
]);
519 texel
[GCOMP
] = nonlinear_to_linear(rgba
[GCOMP
]);
520 texel
[BCOMP
] = nonlinear_to_linear(rgba
[BCOMP
]);
521 texel
[ACOMP
] = UBYTE_TO_FLOAT(rgba
[ACOMP
]);