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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
29 * \file texcompress_s3tc.c
30 * GL_EXT_texture_compression_s3tc support.
33 #ifndef USE_EXTERNAL_DXTN_LIB
34 #define USE_EXTERNAL_DXTN_LIB 1
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
,
349 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
351 if (fetch_ext_rgb_dxt1
) {
353 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
354 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
355 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
356 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
357 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
365 fetch_rgba_dxt1(const GLubyte
*map
,
366 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
368 if (fetch_ext_rgba_dxt1
) {
370 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
371 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
372 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
373 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
374 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
377 problem("rgba_dxt1");
382 fetch_rgba_dxt3(const GLubyte
*map
,
383 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
385 if (fetch_ext_rgba_dxt3
) {
387 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
388 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
389 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
390 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
391 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
394 problem("rgba_dxt3");
399 fetch_rgba_dxt5(const GLubyte
*map
,
400 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
402 if (fetch_ext_rgba_dxt5
) {
404 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
405 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
406 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
407 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
408 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
411 problem("rgba_dxt5");
417 fetch_srgb_dxt1(const GLubyte
*map
,
418 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
420 if (fetch_ext_rgb_dxt1
) {
422 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
423 texel
[RCOMP
] = _mesa_nonlinear_to_linear(tex
[RCOMP
]);
424 texel
[GCOMP
] = _mesa_nonlinear_to_linear(tex
[GCOMP
]);
425 texel
[BCOMP
] = _mesa_nonlinear_to_linear(tex
[BCOMP
]);
426 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
429 problem("srgb_dxt1");
434 fetch_srgba_dxt1(const GLubyte
*map
,
435 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
437 if (fetch_ext_rgba_dxt1
) {
439 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
440 texel
[RCOMP
] = _mesa_nonlinear_to_linear(tex
[RCOMP
]);
441 texel
[GCOMP
] = _mesa_nonlinear_to_linear(tex
[GCOMP
]);
442 texel
[BCOMP
] = _mesa_nonlinear_to_linear(tex
[BCOMP
]);
443 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
446 problem("srgba_dxt1");
451 fetch_srgba_dxt3(const GLubyte
*map
,
452 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
454 if (fetch_ext_rgba_dxt3
) {
456 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
457 texel
[RCOMP
] = _mesa_nonlinear_to_linear(tex
[RCOMP
]);
458 texel
[GCOMP
] = _mesa_nonlinear_to_linear(tex
[GCOMP
]);
459 texel
[BCOMP
] = _mesa_nonlinear_to_linear(tex
[BCOMP
]);
460 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
463 problem("srgba_dxt3");
468 fetch_srgba_dxt5(const GLubyte
*map
,
469 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
471 if (fetch_ext_rgba_dxt5
) {
473 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
474 texel
[RCOMP
] = _mesa_nonlinear_to_linear(tex
[RCOMP
]);
475 texel
[GCOMP
] = _mesa_nonlinear_to_linear(tex
[GCOMP
]);
476 texel
[BCOMP
] = _mesa_nonlinear_to_linear(tex
[BCOMP
]);
477 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
480 problem("srgba_dxt5");
486 compressed_fetch_func
487 _mesa_get_dxt_fetch_func(gl_format format
)
490 case MESA_FORMAT_RGB_DXT1
:
491 return fetch_rgb_dxt1
;
492 case MESA_FORMAT_RGBA_DXT1
:
493 return fetch_rgba_dxt1
;
494 case MESA_FORMAT_RGBA_DXT3
:
495 return fetch_rgba_dxt3
;
496 case MESA_FORMAT_RGBA_DXT5
:
497 return fetch_rgba_dxt5
;
498 case MESA_FORMAT_SRGB_DXT1
:
499 return fetch_srgb_dxt1
;
500 case MESA_FORMAT_SRGBA_DXT1
:
501 return fetch_srgba_dxt1
;
502 case MESA_FORMAT_SRGBA_DXT3
:
503 return fetch_srgba_dxt3
;
504 case MESA_FORMAT_SRGBA_DXT5
:
505 return fetch_srgba_dxt5
;