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 "format_unpack.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"
60 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, const GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
62 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
63 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
64 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
65 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
67 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
68 GLint height
, const GLubyte
*srcPixData
,
69 GLenum destformat
, GLubyte
*dest
,
72 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
74 static void *dxtlibhandle
= NULL
;
78 _mesa_init_texture_s3tc( struct gl_context
*ctx
)
80 /* called during context initialization */
81 ctx
->Mesa_DXTn
= GL_FALSE
;
82 #if USE_EXTERNAL_DXTN_LIB
84 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
86 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
87 "compression/decompression unavailable");
90 /* the fetch functions are not per context! Might be problematic... */
91 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
92 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
93 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
94 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
95 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
96 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
97 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
98 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
99 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
100 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
102 if (!fetch_ext_rgb_dxt1
||
103 !fetch_ext_rgba_dxt1
||
104 !fetch_ext_rgba_dxt3
||
105 !fetch_ext_rgba_dxt5
||
106 !ext_tx_compress_dxtn
) {
107 _mesa_warning(ctx
, "couldn't reference all symbols in "
108 DXTN_LIBNAME
", software DXTn compression/decompression "
110 fetch_ext_rgb_dxt1
= NULL
;
111 fetch_ext_rgba_dxt1
= NULL
;
112 fetch_ext_rgba_dxt3
= NULL
;
113 fetch_ext_rgba_dxt5
= NULL
;
114 ext_tx_compress_dxtn
= NULL
;
115 _mesa_dlclose(dxtlibhandle
);
121 ctx
->Mesa_DXTn
= GL_TRUE
;
129 * Store user's image in rgb_dxt1 format.
132 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
134 const GLubyte
*pixels
;
136 const GLubyte
*tempImage
= NULL
;
138 ASSERT(dstFormat
== MESA_FORMAT_RGB_DXT1
||
139 dstFormat
== MESA_FORMAT_SRGB_DXT1
);
141 if (srcFormat
!= GL_RGB
||
142 srcType
!= GL_UNSIGNED_BYTE
||
143 ctx
->_ImageTransferState
||
144 srcPacking
->RowLength
!= srcWidth
||
145 srcPacking
->SwapBytes
) {
146 /* convert image to RGB/GLubyte */
147 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
149 _mesa_get_format_base_format(dstFormat
),
150 srcWidth
, srcHeight
, srcDepth
,
151 srcFormat
, srcType
, srcAddr
,
154 return GL_FALSE
; /* out of memory */
159 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
160 srcFormat
, srcType
, 0, 0);
165 if (ext_tx_compress_dxtn
) {
166 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
167 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
171 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
174 free((void *) tempImage
);
181 * Store user's image in rgba_dxt1 format.
184 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
186 const GLubyte
*pixels
;
188 const GLubyte
*tempImage
= NULL
;
190 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT1
||
191 dstFormat
== MESA_FORMAT_SRGBA_DXT1
);
193 if (srcFormat
!= GL_RGBA
||
194 srcType
!= GL_UNSIGNED_BYTE
||
195 ctx
->_ImageTransferState
||
196 srcPacking
->RowLength
!= srcWidth
||
197 srcPacking
->SwapBytes
) {
198 /* convert image to RGBA/GLubyte */
199 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
201 _mesa_get_format_base_format(dstFormat
),
202 srcWidth
, srcHeight
, srcDepth
,
203 srcFormat
, srcType
, srcAddr
,
206 return GL_FALSE
; /* out of memory */
211 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
212 srcFormat
, srcType
, 0, 0);
217 if (ext_tx_compress_dxtn
) {
218 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
219 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
223 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
226 free((void*) tempImage
);
233 * Store user's image in rgba_dxt3 format.
236 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
238 const GLubyte
*pixels
;
240 const GLubyte
*tempImage
= NULL
;
242 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT3
||
243 dstFormat
== MESA_FORMAT_SRGBA_DXT3
);
245 if (srcFormat
!= GL_RGBA
||
246 srcType
!= GL_UNSIGNED_BYTE
||
247 ctx
->_ImageTransferState
||
248 srcPacking
->RowLength
!= srcWidth
||
249 srcPacking
->SwapBytes
) {
250 /* convert image to RGBA/GLubyte */
251 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
253 _mesa_get_format_base_format(dstFormat
),
254 srcWidth
, srcHeight
, srcDepth
,
255 srcFormat
, srcType
, srcAddr
,
258 return GL_FALSE
; /* out of memory */
262 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
263 srcFormat
, srcType
, 0, 0);
268 if (ext_tx_compress_dxtn
) {
269 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
270 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
274 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
277 free((void *) tempImage
);
284 * Store user's image in rgba_dxt5 format.
287 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
289 const GLubyte
*pixels
;
291 const GLubyte
*tempImage
= NULL
;
293 ASSERT(dstFormat
== MESA_FORMAT_RGBA_DXT5
||
294 dstFormat
== MESA_FORMAT_SRGBA_DXT5
);
296 if (srcFormat
!= GL_RGBA
||
297 srcType
!= GL_UNSIGNED_BYTE
||
298 ctx
->_ImageTransferState
||
299 srcPacking
->RowLength
!= srcWidth
||
300 srcPacking
->SwapBytes
) {
301 /* convert image to RGBA/GLubyte */
302 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
304 _mesa_get_format_base_format(dstFormat
),
305 srcWidth
, srcHeight
, srcDepth
,
306 srcFormat
, srcType
, srcAddr
,
309 return GL_FALSE
; /* out of memory */
313 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
314 srcFormat
, srcType
, 0, 0);
319 if (ext_tx_compress_dxtn
) {
320 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
321 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
325 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
328 free((void *) tempImage
);
334 /** Report problem with dxt texture decompression, once */
336 problem(const char *func
)
338 static GLboolean warned
= GL_FALSE
;
340 _mesa_debug(NULL
, "attempted to decode DXT texture without "
341 "library available: %s\n", func
);
348 fetch_rgb_dxt1(const GLubyte
*map
, const GLuint imageOffsets
[],
349 GLint rowStride
, GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
351 if (fetch_ext_rgb_dxt1
) {
352 GLuint sliceOffset
= k
? imageOffsets
[k
] / 2 : 0;
354 fetch_ext_rgb_dxt1(rowStride
, map
+ sliceOffset
, i
, j
, tex
);
355 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
356 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
357 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
358 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
366 fetch_rgba_dxt1(const GLubyte
*map
, const GLuint imageOffsets
[],
367 GLint rowStride
, GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
369 if (fetch_ext_rgba_dxt1
) {
370 GLuint sliceOffset
= k
? imageOffsets
[k
] / 2 : 0;
372 fetch_ext_rgba_dxt1(rowStride
, map
+ sliceOffset
, i
, j
, tex
);
373 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
374 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
375 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
376 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
379 problem("rgba_dxt1");
384 fetch_rgba_dxt3(const GLubyte
*map
, const GLuint imageOffsets
[],
385 GLint rowStride
, GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
387 if (fetch_ext_rgba_dxt3
) {
388 GLuint sliceOffset
= k
? imageOffsets
[k
] : 0;
390 fetch_ext_rgba_dxt3(rowStride
, map
+ sliceOffset
, i
, j
, tex
);
391 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
392 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
393 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
394 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
397 problem("rgba_dxt3");
402 fetch_rgba_dxt5(const GLubyte
*map
, const GLuint imageOffsets
[],
403 GLint rowStride
, GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
405 if (fetch_ext_rgba_dxt5
) {
406 GLuint sliceOffset
= k
? imageOffsets
[k
] : 0;
408 fetch_ext_rgba_dxt5(rowStride
, map
+ sliceOffset
, i
, j
, tex
);
409 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
410 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
411 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
412 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
415 problem("rgba_dxt5");
421 fetch_srgb_dxt1(const GLubyte
*map
, const GLuint imageOffsets
[],
422 GLint rowStride
, GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
424 if (fetch_ext_rgb_dxt1
) {
425 GLuint sliceOffset
= k
? imageOffsets
[k
] / 2 : 0;
427 fetch_ext_rgb_dxt1(rowStride
, map
+ sliceOffset
, i
, j
, tex
);
428 texel
[RCOMP
] = _mesa_nonlinear_to_linear(tex
[RCOMP
]);
429 texel
[GCOMP
] = _mesa_nonlinear_to_linear(tex
[GCOMP
]);
430 texel
[BCOMP
] = _mesa_nonlinear_to_linear(tex
[BCOMP
]);
431 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
434 problem("srgb_dxt1");
439 fetch_srgba_dxt1(const GLubyte
*map
, const GLuint imageOffsets
[],
440 GLint rowStride
, GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
442 if (fetch_ext_rgba_dxt1
) {
443 GLuint sliceOffset
= k
? imageOffsets
[k
] / 2 : 0;
445 fetch_ext_rgba_dxt1(rowStride
, map
+ sliceOffset
, i
, j
, tex
);
446 texel
[RCOMP
] = _mesa_nonlinear_to_linear(tex
[RCOMP
]);
447 texel
[GCOMP
] = _mesa_nonlinear_to_linear(tex
[GCOMP
]);
448 texel
[BCOMP
] = _mesa_nonlinear_to_linear(tex
[BCOMP
]);
449 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
452 problem("srgba_dxt1");
457 fetch_srgba_dxt3(const GLubyte
*map
, const GLuint imageOffsets
[],
458 GLint rowStride
, GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
460 if (fetch_ext_rgba_dxt3
) {
461 GLuint sliceOffset
= k
? imageOffsets
[k
] : 0;
463 fetch_ext_rgba_dxt3(rowStride
, map
+ sliceOffset
, i
, j
, tex
);
464 texel
[RCOMP
] = _mesa_nonlinear_to_linear(tex
[RCOMP
]);
465 texel
[GCOMP
] = _mesa_nonlinear_to_linear(tex
[GCOMP
]);
466 texel
[BCOMP
] = _mesa_nonlinear_to_linear(tex
[BCOMP
]);
467 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
470 problem("srgba_dxt3");
475 fetch_srgba_dxt5(const GLubyte
*map
, const GLuint imageOffsets
[],
476 GLint rowStride
, GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
478 if (fetch_ext_rgba_dxt5
) {
479 GLuint sliceOffset
= k
? imageOffsets
[k
] : 0;
481 fetch_ext_rgba_dxt5(rowStride
, map
+ sliceOffset
, i
, j
, tex
);
482 texel
[RCOMP
] = _mesa_nonlinear_to_linear(tex
[RCOMP
]);
483 texel
[GCOMP
] = _mesa_nonlinear_to_linear(tex
[GCOMP
]);
484 texel
[BCOMP
] = _mesa_nonlinear_to_linear(tex
[BCOMP
]);
485 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
488 problem("srgba_dxt5");
494 compressed_fetch_func
495 _mesa_get_dxt_fetch_func(gl_format format
)
498 case MESA_FORMAT_RGB_DXT1
:
499 return fetch_rgb_dxt1
;
500 case MESA_FORMAT_RGBA_DXT1
:
501 return fetch_rgba_dxt1
;
502 case MESA_FORMAT_RGBA_DXT3
:
503 return fetch_rgba_dxt3
;
504 case MESA_FORMAT_RGBA_DXT5
:
505 return fetch_rgba_dxt5
;
506 case MESA_FORMAT_SRGB_DXT1
:
507 return fetch_srgb_dxt1
;
508 case MESA_FORMAT_SRGBA_DXT1
:
509 return fetch_srgba_dxt1
;
510 case MESA_FORMAT_SRGBA_DXT3
:
511 return fetch_srgba_dxt3
;
512 case MESA_FORMAT_SRGBA_DXT5
:
513 return fetch_srgba_dxt5
;