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
43 #include "texcompress.h"
44 #include "texcompress_s3tc.h"
46 #include "format_unpack.h"
47 #include "util/format_srgb.h"
50 #if defined(_WIN32) || defined(WIN32)
51 #define DXTN_LIBNAME "dxtn.dll"
55 #define DXTN_LIBNAME "libtxc_dxtn.so"
58 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, const GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
60 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
61 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
62 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
63 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
65 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
66 GLint height
, const GLubyte
*srcPixData
,
67 GLenum destformat
, GLubyte
*dest
,
70 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
72 static void *dxtlibhandle
= NULL
;
76 _mesa_init_texture_s3tc( struct gl_context
*ctx
)
78 /* called during context initialization */
79 ctx
->Mesa_DXTn
= GL_FALSE
;
80 #if USE_EXTERNAL_DXTN_LIB
82 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
84 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
85 "compression/decompression unavailable");
88 /* the fetch functions are not per context! Might be problematic... */
89 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
90 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
91 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
92 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
93 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
94 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
95 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
96 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
97 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
98 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
100 if (!fetch_ext_rgb_dxt1
||
101 !fetch_ext_rgba_dxt1
||
102 !fetch_ext_rgba_dxt3
||
103 !fetch_ext_rgba_dxt5
||
104 !ext_tx_compress_dxtn
) {
105 _mesa_warning(ctx
, "couldn't reference all symbols in "
106 DXTN_LIBNAME
", software DXTn compression/decompression "
108 fetch_ext_rgb_dxt1
= NULL
;
109 fetch_ext_rgba_dxt1
= NULL
;
110 fetch_ext_rgba_dxt3
= NULL
;
111 fetch_ext_rgba_dxt5
= NULL
;
112 ext_tx_compress_dxtn
= NULL
;
113 _mesa_dlclose(dxtlibhandle
);
119 ctx
->Mesa_DXTn
= GL_TRUE
;
127 * Store user's image in rgb_dxt1 format.
130 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
132 const GLubyte
*pixels
;
134 const GLubyte
*tempImage
= NULL
;
136 assert(dstFormat
== MESA_FORMAT_RGB_DXT1
||
137 dstFormat
== MESA_FORMAT_SRGB_DXT1
);
139 if (srcFormat
!= GL_RGB
||
140 srcType
!= GL_UNSIGNED_BYTE
||
141 ctx
->_ImageTransferState
||
142 srcPacking
->RowLength
!= srcWidth
||
143 srcPacking
->SwapBytes
) {
144 /* convert image to RGB/GLubyte */
145 GLubyte
*tempImageSlices
[1];
146 int rgbRowStride
= 3 * srcWidth
* sizeof(GLubyte
);
147 tempImage
= malloc(srcWidth
* srcHeight
* 3 * sizeof(GLubyte
));
149 return GL_FALSE
; /* out of memory */
150 tempImageSlices
[0] = (GLubyte
*) tempImage
;
151 _mesa_texstore(ctx
, dims
,
153 MESA_FORMAT_RGB_UNORM8
,
154 rgbRowStride
, tempImageSlices
,
155 srcWidth
, srcHeight
, srcDepth
,
156 srcFormat
, srcType
, srcAddr
,
162 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
163 srcFormat
, srcType
, 0, 0);
168 if (ext_tx_compress_dxtn
) {
169 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
170 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
174 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
177 free((void *) tempImage
);
184 * Store user's image in rgba_dxt1 format.
187 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
189 const GLubyte
*pixels
;
191 const GLubyte
*tempImage
= NULL
;
193 assert(dstFormat
== MESA_FORMAT_RGBA_DXT1
||
194 dstFormat
== MESA_FORMAT_SRGBA_DXT1
);
196 if (srcFormat
!= GL_RGBA
||
197 srcType
!= GL_UNSIGNED_BYTE
||
198 ctx
->_ImageTransferState
||
199 srcPacking
->RowLength
!= srcWidth
||
200 srcPacking
->SwapBytes
) {
201 /* convert image to RGBA/GLubyte */
202 GLubyte
*tempImageSlices
[1];
203 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
204 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
206 return GL_FALSE
; /* out of memory */
207 tempImageSlices
[0] = (GLubyte
*) tempImage
;
208 _mesa_texstore(ctx
, dims
,
210 MESA_FORMAT_R8G8B8A8_UNORM
,
211 rgbaRowStride
, tempImageSlices
,
212 srcWidth
, srcHeight
, srcDepth
,
213 srcFormat
, srcType
, srcAddr
,
219 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
220 srcFormat
, srcType
, 0, 0);
225 if (ext_tx_compress_dxtn
) {
226 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
227 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
231 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
234 free((void*) tempImage
);
241 * Store user's image in rgba_dxt3 format.
244 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
246 const GLubyte
*pixels
;
248 const GLubyte
*tempImage
= NULL
;
250 assert(dstFormat
== MESA_FORMAT_RGBA_DXT3
||
251 dstFormat
== MESA_FORMAT_SRGBA_DXT3
);
253 if (srcFormat
!= GL_RGBA
||
254 srcType
!= GL_UNSIGNED_BYTE
||
255 ctx
->_ImageTransferState
||
256 srcPacking
->RowLength
!= srcWidth
||
257 srcPacking
->SwapBytes
) {
258 /* convert image to RGBA/GLubyte */
259 GLubyte
*tempImageSlices
[1];
260 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
261 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
263 return GL_FALSE
; /* out of memory */
264 tempImageSlices
[0] = (GLubyte
*) tempImage
;
265 _mesa_texstore(ctx
, dims
,
267 MESA_FORMAT_R8G8B8A8_UNORM
,
268 rgbaRowStride
, tempImageSlices
,
269 srcWidth
, srcHeight
, srcDepth
,
270 srcFormat
, srcType
, srcAddr
,
275 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
276 srcFormat
, srcType
, 0, 0);
281 if (ext_tx_compress_dxtn
) {
282 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
283 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
287 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
290 free((void *) tempImage
);
297 * Store user's image in rgba_dxt5 format.
300 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
302 const GLubyte
*pixels
;
304 const GLubyte
*tempImage
= NULL
;
306 assert(dstFormat
== MESA_FORMAT_RGBA_DXT5
||
307 dstFormat
== MESA_FORMAT_SRGBA_DXT5
);
309 if (srcFormat
!= GL_RGBA
||
310 srcType
!= GL_UNSIGNED_BYTE
||
311 ctx
->_ImageTransferState
||
312 srcPacking
->RowLength
!= srcWidth
||
313 srcPacking
->SwapBytes
) {
314 /* convert image to RGBA/GLubyte */
315 GLubyte
*tempImageSlices
[1];
316 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
317 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
319 return GL_FALSE
; /* out of memory */
320 tempImageSlices
[0] = (GLubyte
*) tempImage
;
321 _mesa_texstore(ctx
, dims
,
323 MESA_FORMAT_R8G8B8A8_UNORM
,
324 rgbaRowStride
, tempImageSlices
,
325 srcWidth
, srcHeight
, srcDepth
,
326 srcFormat
, srcType
, srcAddr
,
331 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
332 srcFormat
, srcType
, 0, 0);
337 if (ext_tx_compress_dxtn
) {
338 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
339 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
343 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
346 free((void *) tempImage
);
352 /** Report problem with dxt texture decompression, once */
354 problem(const char *func
)
356 static GLboolean warned
= GL_FALSE
;
358 _mesa_debug(NULL
, "attempted to decode DXT texture without "
359 "library available: %s\n", func
);
366 fetch_rgb_dxt1(const GLubyte
*map
,
367 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
369 if (fetch_ext_rgb_dxt1
) {
371 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
372 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
373 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
374 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
375 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
383 fetch_rgba_dxt1(const GLubyte
*map
,
384 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
386 if (fetch_ext_rgba_dxt1
) {
388 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
389 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
390 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
391 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
392 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
395 problem("rgba_dxt1");
400 fetch_rgba_dxt3(const GLubyte
*map
,
401 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
403 if (fetch_ext_rgba_dxt3
) {
405 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
406 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
407 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
408 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
409 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
412 problem("rgba_dxt3");
417 fetch_rgba_dxt5(const GLubyte
*map
,
418 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
420 if (fetch_ext_rgba_dxt5
) {
422 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
423 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
424 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
425 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
426 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
429 problem("rgba_dxt5");
435 fetch_srgb_dxt1(const GLubyte
*map
,
436 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
438 if (fetch_ext_rgb_dxt1
) {
440 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
441 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
442 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
443 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
444 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
447 problem("srgb_dxt1");
452 fetch_srgba_dxt1(const GLubyte
*map
,
453 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
455 if (fetch_ext_rgba_dxt1
) {
457 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
458 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
459 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
460 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
461 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
464 problem("srgba_dxt1");
469 fetch_srgba_dxt3(const GLubyte
*map
,
470 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
472 if (fetch_ext_rgba_dxt3
) {
474 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
475 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
476 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
477 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
478 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
481 problem("srgba_dxt3");
486 fetch_srgba_dxt5(const GLubyte
*map
,
487 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
489 if (fetch_ext_rgba_dxt5
) {
491 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
492 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
493 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
494 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
495 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
498 problem("srgba_dxt5");
504 compressed_fetch_func
505 _mesa_get_dxt_fetch_func(mesa_format format
)
508 case MESA_FORMAT_RGB_DXT1
:
509 return fetch_rgb_dxt1
;
510 case MESA_FORMAT_RGBA_DXT1
:
511 return fetch_rgba_dxt1
;
512 case MESA_FORMAT_RGBA_DXT3
:
513 return fetch_rgba_dxt3
;
514 case MESA_FORMAT_RGBA_DXT5
:
515 return fetch_rgba_dxt5
;
516 case MESA_FORMAT_SRGB_DXT1
:
517 return fetch_srgb_dxt1
;
518 case MESA_FORMAT_SRGBA_DXT1
:
519 return fetch_srgba_dxt1
;
520 case MESA_FORMAT_SRGBA_DXT3
:
521 return fetch_srgba_dxt3
;
522 case MESA_FORMAT_SRGBA_DXT5
:
523 return fetch_srgba_dxt5
;