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.
38 #include "texcompress.h"
39 #include "texcompress_s3tc.h"
40 #include "texcompress_s3tc_tmp.h"
42 #include "format_unpack.h"
43 #include "util/format_srgb.h"
46 #if defined(_WIN32) || defined(WIN32)
47 #define DXTN_LIBNAME "dxtn.dll"
50 #elif defined(__CYGWIN__)
51 #define DXTN_LIBNAME "cygtxc_dxtn.dll"
53 #define DXTN_LIBNAME "libtxc_dxtn.so"
56 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, const GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
58 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
59 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
60 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
61 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
63 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
64 GLint height
, const GLubyte
*srcPixData
,
65 GLenum destformat
, GLubyte
*dest
,
68 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
70 static void *dxtlibhandle
= NULL
;
74 _mesa_init_texture_s3tc( struct gl_context
*ctx
)
76 /* called during context initialization */
77 ctx
->Mesa_DXTn
= GL_FALSE
;
79 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
81 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
82 "compression/decompression unavailable");
85 /* the fetch functions are not per context! Might be problematic... */
86 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
87 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
88 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
89 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
90 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
91 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
92 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
93 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
94 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
95 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
97 if (!fetch_ext_rgb_dxt1
||
98 !fetch_ext_rgba_dxt1
||
99 !fetch_ext_rgba_dxt3
||
100 !fetch_ext_rgba_dxt5
||
101 !ext_tx_compress_dxtn
) {
102 _mesa_warning(ctx
, "couldn't reference all symbols in "
103 DXTN_LIBNAME
", software DXTn compression/decompression "
105 fetch_ext_rgb_dxt1
= NULL
;
106 fetch_ext_rgba_dxt1
= NULL
;
107 fetch_ext_rgba_dxt3
= NULL
;
108 fetch_ext_rgba_dxt5
= NULL
;
109 ext_tx_compress_dxtn
= NULL
;
110 _mesa_dlclose(dxtlibhandle
);
116 ctx
->Mesa_DXTn
= GL_TRUE
;
121 * Store user's image in rgb_dxt1 format.
124 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
126 const GLubyte
*pixels
;
128 const GLubyte
*tempImage
= NULL
;
130 assert(dstFormat
== MESA_FORMAT_RGB_DXT1
||
131 dstFormat
== MESA_FORMAT_SRGB_DXT1
);
133 if (srcFormat
!= GL_RGB
||
134 srcType
!= GL_UNSIGNED_BYTE
||
135 ctx
->_ImageTransferState
||
136 ALIGN(srcPacking
->RowLength
, srcPacking
->Alignment
) != srcWidth
||
137 srcPacking
->SwapBytes
) {
138 /* convert image to RGB/GLubyte */
139 GLubyte
*tempImageSlices
[1];
140 int rgbRowStride
= 3 * srcWidth
* sizeof(GLubyte
);
141 tempImage
= malloc(srcWidth
* srcHeight
* 3 * sizeof(GLubyte
));
143 return GL_FALSE
; /* out of memory */
144 tempImageSlices
[0] = (GLubyte
*) tempImage
;
145 _mesa_texstore(ctx
, dims
,
147 MESA_FORMAT_RGB_UNORM8
,
148 rgbRowStride
, tempImageSlices
,
149 srcWidth
, srcHeight
, srcDepth
,
150 srcFormat
, srcType
, srcAddr
,
156 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
157 srcFormat
, srcType
, 0, 0);
162 if (ext_tx_compress_dxtn
) {
163 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
164 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
168 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
171 free((void *) tempImage
);
178 * Store user's image in rgba_dxt1 format.
181 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
183 const GLubyte
*pixels
;
185 const GLubyte
*tempImage
= NULL
;
187 assert(dstFormat
== MESA_FORMAT_RGBA_DXT1
||
188 dstFormat
== MESA_FORMAT_SRGBA_DXT1
);
190 if (srcFormat
!= GL_RGBA
||
191 srcType
!= GL_UNSIGNED_BYTE
||
192 ctx
->_ImageTransferState
||
193 ALIGN(srcPacking
->RowLength
, srcPacking
->Alignment
) != srcWidth
||
194 srcPacking
->SwapBytes
) {
195 /* convert image to RGBA/GLubyte */
196 GLubyte
*tempImageSlices
[1];
197 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
198 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
200 return GL_FALSE
; /* out of memory */
201 tempImageSlices
[0] = (GLubyte
*) tempImage
;
202 _mesa_texstore(ctx
, dims
,
204 _mesa_little_endian() ? MESA_FORMAT_R8G8B8A8_UNORM
205 : MESA_FORMAT_A8B8G8R8_UNORM
,
206 rgbaRowStride
, tempImageSlices
,
207 srcWidth
, srcHeight
, srcDepth
,
208 srcFormat
, srcType
, srcAddr
,
214 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
215 srcFormat
, srcType
, 0, 0);
220 if (ext_tx_compress_dxtn
) {
221 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
222 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
,
226 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt1");
229 free((void*) tempImage
);
236 * Store user's image in rgba_dxt3 format.
239 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS
)
241 const GLubyte
*pixels
;
243 const GLubyte
*tempImage
= NULL
;
245 assert(dstFormat
== MESA_FORMAT_RGBA_DXT3
||
246 dstFormat
== MESA_FORMAT_SRGBA_DXT3
);
248 if (srcFormat
!= GL_RGBA
||
249 srcType
!= GL_UNSIGNED_BYTE
||
250 ctx
->_ImageTransferState
||
251 ALIGN(srcPacking
->RowLength
, srcPacking
->Alignment
) != srcWidth
||
252 srcPacking
->SwapBytes
) {
253 /* convert image to RGBA/GLubyte */
254 GLubyte
*tempImageSlices
[1];
255 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
256 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
258 return GL_FALSE
; /* out of memory */
259 tempImageSlices
[0] = (GLubyte
*) tempImage
;
260 _mesa_texstore(ctx
, dims
,
262 _mesa_little_endian() ? MESA_FORMAT_R8G8B8A8_UNORM
263 : MESA_FORMAT_A8B8G8R8_UNORM
,
264 rgbaRowStride
, tempImageSlices
,
265 srcWidth
, srcHeight
, srcDepth
,
266 srcFormat
, srcType
, srcAddr
,
271 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
272 srcFormat
, srcType
, 0, 0);
277 if (ext_tx_compress_dxtn
) {
278 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
279 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
283 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
286 free((void *) tempImage
);
293 * Store user's image in rgba_dxt5 format.
296 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
298 const GLubyte
*pixels
;
300 const GLubyte
*tempImage
= NULL
;
302 assert(dstFormat
== MESA_FORMAT_RGBA_DXT5
||
303 dstFormat
== MESA_FORMAT_SRGBA_DXT5
);
305 if (srcFormat
!= GL_RGBA
||
306 srcType
!= GL_UNSIGNED_BYTE
||
307 ctx
->_ImageTransferState
||
308 ALIGN(srcPacking
->RowLength
, srcPacking
->Alignment
) != srcWidth
||
309 srcPacking
->SwapBytes
) {
310 /* convert image to RGBA/GLubyte */
311 GLubyte
*tempImageSlices
[1];
312 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
313 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
315 return GL_FALSE
; /* out of memory */
316 tempImageSlices
[0] = (GLubyte
*) tempImage
;
317 _mesa_texstore(ctx
, dims
,
319 _mesa_little_endian() ? MESA_FORMAT_R8G8B8A8_UNORM
320 : MESA_FORMAT_A8B8G8R8_UNORM
,
321 rgbaRowStride
, tempImageSlices
,
322 srcWidth
, srcHeight
, srcDepth
,
323 srcFormat
, srcType
, srcAddr
,
328 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
329 srcFormat
, srcType
, 0, 0);
334 if (ext_tx_compress_dxtn
) {
335 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
336 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
340 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
343 free((void *) tempImage
);
349 /** Report problem with dxt texture decompression, once */
351 problem(const char *func
)
353 static GLboolean warned
= GL_FALSE
;
355 _mesa_debug(NULL
, "attempted to decode DXT texture without "
356 "library available: %s\n", func
);
363 fetch_rgb_dxt1(const GLubyte
*map
,
364 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
366 if (fetch_ext_rgb_dxt1
) {
368 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
369 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
370 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
371 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
372 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
380 fetch_rgba_dxt1(const GLubyte
*map
,
381 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
383 if (fetch_ext_rgba_dxt1
) {
385 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
386 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
387 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
388 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
389 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
392 problem("rgba_dxt1");
397 fetch_rgba_dxt3(const GLubyte
*map
,
398 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
400 if (fetch_ext_rgba_dxt3
) {
402 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
403 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
404 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
405 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
406 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
409 problem("rgba_dxt3");
414 fetch_rgba_dxt5(const GLubyte
*map
,
415 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
417 if (fetch_ext_rgba_dxt5
) {
419 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
420 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
421 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
422 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
423 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
426 problem("rgba_dxt5");
432 fetch_srgb_dxt1(const GLubyte
*map
,
433 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
435 if (fetch_ext_rgb_dxt1
) {
437 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
438 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
439 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
440 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
441 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
444 problem("srgb_dxt1");
449 fetch_srgba_dxt1(const GLubyte
*map
,
450 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
452 if (fetch_ext_rgba_dxt1
) {
454 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
455 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
456 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
457 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
458 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
461 problem("srgba_dxt1");
466 fetch_srgba_dxt3(const GLubyte
*map
,
467 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
469 if (fetch_ext_rgba_dxt3
) {
471 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
472 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
473 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
474 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
475 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
478 problem("srgba_dxt3");
483 fetch_srgba_dxt5(const GLubyte
*map
,
484 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
486 if (fetch_ext_rgba_dxt5
) {
488 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
489 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
490 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
491 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
492 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
495 problem("srgba_dxt5");
501 compressed_fetch_func
502 _mesa_get_dxt_fetch_func(mesa_format format
)
505 case MESA_FORMAT_RGB_DXT1
:
506 return fetch_rgb_dxt1
;
507 case MESA_FORMAT_RGBA_DXT1
:
508 return fetch_rgba_dxt1
;
509 case MESA_FORMAT_RGBA_DXT3
:
510 return fetch_rgba_dxt3
;
511 case MESA_FORMAT_RGBA_DXT5
:
512 return fetch_rgba_dxt5
;
513 case MESA_FORMAT_SRGB_DXT1
:
514 return fetch_srgb_dxt1
;
515 case MESA_FORMAT_SRGBA_DXT1
:
516 return fetch_srgba_dxt1
;
517 case MESA_FORMAT_SRGBA_DXT3
:
518 return fetch_srgba_dxt3
;
519 case MESA_FORMAT_SRGBA_DXT5
:
520 return fetch_srgba_dxt5
;