2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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 "texcompress.h"
43 #include "texcompress_s3tc.h"
45 #include "format_unpack.h"
46 #include "util/format_srgb.h"
49 #if defined(_WIN32) || defined(WIN32)
50 #define DXTN_LIBNAME "dxtn.dll"
54 #define DXTN_LIBNAME "libtxc_dxtn.so"
57 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, const GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
59 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
60 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
61 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
62 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
64 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
65 GLint height
, const GLubyte
*srcPixData
,
66 GLenum destformat
, GLubyte
*dest
,
69 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
71 static void *dxtlibhandle
= NULL
;
75 _mesa_init_texture_s3tc( struct gl_context
*ctx
)
77 /* called during context initialization */
78 ctx
->Mesa_DXTn
= GL_FALSE
;
79 #if USE_EXTERNAL_DXTN_LIB
81 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
83 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
84 "compression/decompression unavailable");
87 /* the fetch functions are not per context! Might be problematic... */
88 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
89 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
90 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
91 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
92 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
93 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
94 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
95 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
96 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
97 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
99 if (!fetch_ext_rgb_dxt1
||
100 !fetch_ext_rgba_dxt1
||
101 !fetch_ext_rgba_dxt3
||
102 !fetch_ext_rgba_dxt5
||
103 !ext_tx_compress_dxtn
) {
104 _mesa_warning(ctx
, "couldn't reference all symbols in "
105 DXTN_LIBNAME
", software DXTn compression/decompression "
107 fetch_ext_rgb_dxt1
= NULL
;
108 fetch_ext_rgba_dxt1
= NULL
;
109 fetch_ext_rgba_dxt3
= NULL
;
110 fetch_ext_rgba_dxt5
= NULL
;
111 ext_tx_compress_dxtn
= NULL
;
112 _mesa_dlclose(dxtlibhandle
);
118 ctx
->Mesa_DXTn
= GL_TRUE
;
126 * Store user's image in rgb_dxt1 format.
129 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
131 const GLubyte
*pixels
;
133 const GLubyte
*tempImage
= NULL
;
135 assert(dstFormat
== MESA_FORMAT_RGB_DXT1
||
136 dstFormat
== MESA_FORMAT_SRGB_DXT1
);
138 if (srcFormat
!= GL_RGB
||
139 srcType
!= GL_UNSIGNED_BYTE
||
140 ctx
->_ImageTransferState
||
141 srcPacking
->RowLength
!= srcWidth
||
142 srcPacking
->SwapBytes
) {
143 /* convert image to RGB/GLubyte */
144 GLubyte
*tempImageSlices
[1];
145 int rgbRowStride
= 3 * srcWidth
* sizeof(GLubyte
);
146 tempImage
= malloc(srcWidth
* srcHeight
* 3 * sizeof(GLubyte
));
148 return GL_FALSE
; /* out of memory */
149 tempImageSlices
[0] = (GLubyte
*) tempImage
;
150 _mesa_texstore(ctx
, dims
,
152 MESA_FORMAT_RGB_UNORM8
,
153 rgbRowStride
, tempImageSlices
,
154 srcWidth
, srcHeight
, srcDepth
,
155 srcFormat
, srcType
, srcAddr
,
161 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
162 srcFormat
, srcType
, 0, 0);
167 if (ext_tx_compress_dxtn
) {
168 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
169 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
173 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
176 free((void *) tempImage
);
183 * Store user's image in rgba_dxt1 format.
186 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
188 const GLubyte
*pixels
;
190 const GLubyte
*tempImage
= NULL
;
192 assert(dstFormat
== MESA_FORMAT_RGBA_DXT1
||
193 dstFormat
== MESA_FORMAT_SRGBA_DXT1
);
195 if (srcFormat
!= GL_RGBA
||
196 srcType
!= GL_UNSIGNED_BYTE
||
197 ctx
->_ImageTransferState
||
198 srcPacking
->RowLength
!= srcWidth
||
199 srcPacking
->SwapBytes
) {
200 /* convert image to RGBA/GLubyte */
201 GLubyte
*tempImageSlices
[1];
202 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
203 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
205 return GL_FALSE
; /* out of memory */
206 tempImageSlices
[0] = (GLubyte
*) tempImage
;
207 _mesa_texstore(ctx
, dims
,
209 MESA_FORMAT_R8G8B8A8_UNORM
,
210 rgbaRowStride
, tempImageSlices
,
211 srcWidth
, srcHeight
, srcDepth
,
212 srcFormat
, srcType
, srcAddr
,
218 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
219 srcFormat
, srcType
, 0, 0);
224 if (ext_tx_compress_dxtn
) {
225 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
226 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
230 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
233 free((void*) tempImage
);
240 * Store user's image in rgba_dxt3 format.
243 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
245 const GLubyte
*pixels
;
247 const GLubyte
*tempImage
= NULL
;
249 assert(dstFormat
== MESA_FORMAT_RGBA_DXT3
||
250 dstFormat
== MESA_FORMAT_SRGBA_DXT3
);
252 if (srcFormat
!= GL_RGBA
||
253 srcType
!= GL_UNSIGNED_BYTE
||
254 ctx
->_ImageTransferState
||
255 srcPacking
->RowLength
!= srcWidth
||
256 srcPacking
->SwapBytes
) {
257 /* convert image to RGBA/GLubyte */
258 GLubyte
*tempImageSlices
[1];
259 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
260 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
262 return GL_FALSE
; /* out of memory */
263 tempImageSlices
[0] = (GLubyte
*) tempImage
;
264 _mesa_texstore(ctx
, dims
,
266 MESA_FORMAT_R8G8B8A8_UNORM
,
267 rgbaRowStride
, tempImageSlices
,
268 srcWidth
, srcHeight
, srcDepth
,
269 srcFormat
, srcType
, srcAddr
,
274 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
275 srcFormat
, srcType
, 0, 0);
280 if (ext_tx_compress_dxtn
) {
281 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
282 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
286 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
289 free((void *) tempImage
);
296 * Store user's image in rgba_dxt5 format.
299 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
301 const GLubyte
*pixels
;
303 const GLubyte
*tempImage
= NULL
;
305 assert(dstFormat
== MESA_FORMAT_RGBA_DXT5
||
306 dstFormat
== MESA_FORMAT_SRGBA_DXT5
);
308 if (srcFormat
!= GL_RGBA
||
309 srcType
!= GL_UNSIGNED_BYTE
||
310 ctx
->_ImageTransferState
||
311 srcPacking
->RowLength
!= srcWidth
||
312 srcPacking
->SwapBytes
) {
313 /* convert image to RGBA/GLubyte */
314 GLubyte
*tempImageSlices
[1];
315 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
316 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
318 return GL_FALSE
; /* out of memory */
319 tempImageSlices
[0] = (GLubyte
*) tempImage
;
320 _mesa_texstore(ctx
, dims
,
322 MESA_FORMAT_R8G8B8A8_UNORM
,
323 rgbaRowStride
, tempImageSlices
,
324 srcWidth
, srcHeight
, srcDepth
,
325 srcFormat
, srcType
, srcAddr
,
330 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
331 srcFormat
, srcType
, 0, 0);
336 if (ext_tx_compress_dxtn
) {
337 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
338 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
342 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
345 free((void *) tempImage
);
351 /** Report problem with dxt texture decompression, once */
353 problem(const char *func
)
355 static GLboolean warned
= GL_FALSE
;
357 _mesa_debug(NULL
, "attempted to decode DXT texture without "
358 "library available: %s\n", func
);
365 fetch_rgb_dxt1(const GLubyte
*map
,
366 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
368 if (fetch_ext_rgb_dxt1
) {
370 fetch_ext_rgb_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
]);
382 fetch_rgba_dxt1(const GLubyte
*map
,
383 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
385 if (fetch_ext_rgba_dxt1
) {
387 fetch_ext_rgba_dxt1(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_dxt1");
399 fetch_rgba_dxt3(const GLubyte
*map
,
400 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
402 if (fetch_ext_rgba_dxt3
) {
404 fetch_ext_rgba_dxt3(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_dxt3");
416 fetch_rgba_dxt5(const GLubyte
*map
,
417 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
419 if (fetch_ext_rgba_dxt5
) {
421 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
422 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
423 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
424 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
425 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
428 problem("rgba_dxt5");
434 fetch_srgb_dxt1(const GLubyte
*map
,
435 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
437 if (fetch_ext_rgb_dxt1
) {
439 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
440 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
441 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
442 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
443 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
446 problem("srgb_dxt1");
451 fetch_srgba_dxt1(const GLubyte
*map
,
452 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
454 if (fetch_ext_rgba_dxt1
) {
456 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
457 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
458 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
459 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
460 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
463 problem("srgba_dxt1");
468 fetch_srgba_dxt3(const GLubyte
*map
,
469 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
471 if (fetch_ext_rgba_dxt3
) {
473 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
474 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
475 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
476 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
477 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
480 problem("srgba_dxt3");
485 fetch_srgba_dxt5(const GLubyte
*map
,
486 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
488 if (fetch_ext_rgba_dxt5
) {
490 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
491 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
492 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
493 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
494 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
497 problem("srgba_dxt5");
503 compressed_fetch_func
504 _mesa_get_dxt_fetch_func(mesa_format format
)
507 case MESA_FORMAT_RGB_DXT1
:
508 return fetch_rgb_dxt1
;
509 case MESA_FORMAT_RGBA_DXT1
:
510 return fetch_rgba_dxt1
;
511 case MESA_FORMAT_RGBA_DXT3
:
512 return fetch_rgba_dxt3
;
513 case MESA_FORMAT_RGBA_DXT5
:
514 return fetch_rgba_dxt5
;
515 case MESA_FORMAT_SRGB_DXT1
:
516 return fetch_srgb_dxt1
;
517 case MESA_FORMAT_SRGBA_DXT1
:
518 return fetch_srgba_dxt1
;
519 case MESA_FORMAT_SRGBA_DXT3
:
520 return fetch_srgba_dxt3
;
521 case MESA_FORMAT_SRGBA_DXT5
:
522 return fetch_srgba_dxt5
;