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"
41 #include "format_unpack.h"
42 #include "util/format_srgb.h"
45 #if defined(_WIN32) || defined(WIN32)
46 #define DXTN_LIBNAME "dxtn.dll"
50 #define DXTN_LIBNAME "libtxc_dxtn.so"
53 typedef void (*dxtFetchTexelFuncExt
)( GLint srcRowstride
, const GLubyte
*pixdata
, GLint col
, GLint row
, GLvoid
*texelOut
);
55 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1
= NULL
;
56 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1
= NULL
;
57 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3
= NULL
;
58 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5
= NULL
;
60 typedef void (*dxtCompressTexFuncExt
)(GLint srccomps
, GLint width
,
61 GLint height
, const GLubyte
*srcPixData
,
62 GLenum destformat
, GLubyte
*dest
,
65 static dxtCompressTexFuncExt ext_tx_compress_dxtn
= NULL
;
67 static void *dxtlibhandle
= NULL
;
71 _mesa_init_texture_s3tc( struct gl_context
*ctx
)
73 /* called during context initialization */
74 ctx
->Mesa_DXTn
= GL_FALSE
;
76 dxtlibhandle
= _mesa_dlopen(DXTN_LIBNAME
, 0);
78 _mesa_warning(ctx
, "couldn't open " DXTN_LIBNAME
", software DXTn "
79 "compression/decompression unavailable");
82 /* the fetch functions are not per context! Might be problematic... */
83 fetch_ext_rgb_dxt1
= (dxtFetchTexelFuncExt
)
84 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgb_dxt1");
85 fetch_ext_rgba_dxt1
= (dxtFetchTexelFuncExt
)
86 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt1");
87 fetch_ext_rgba_dxt3
= (dxtFetchTexelFuncExt
)
88 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt3");
89 fetch_ext_rgba_dxt5
= (dxtFetchTexelFuncExt
)
90 _mesa_dlsym(dxtlibhandle
, "fetch_2d_texel_rgba_dxt5");
91 ext_tx_compress_dxtn
= (dxtCompressTexFuncExt
)
92 _mesa_dlsym(dxtlibhandle
, "tx_compress_dxtn");
94 if (!fetch_ext_rgb_dxt1
||
95 !fetch_ext_rgba_dxt1
||
96 !fetch_ext_rgba_dxt3
||
97 !fetch_ext_rgba_dxt5
||
98 !ext_tx_compress_dxtn
) {
99 _mesa_warning(ctx
, "couldn't reference all symbols in "
100 DXTN_LIBNAME
", software DXTn compression/decompression "
102 fetch_ext_rgb_dxt1
= NULL
;
103 fetch_ext_rgba_dxt1
= NULL
;
104 fetch_ext_rgba_dxt3
= NULL
;
105 fetch_ext_rgba_dxt5
= NULL
;
106 ext_tx_compress_dxtn
= NULL
;
107 _mesa_dlclose(dxtlibhandle
);
113 ctx
->Mesa_DXTn
= GL_TRUE
;
118 * Store user's image in rgb_dxt1 format.
121 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS
)
123 const GLubyte
*pixels
;
125 const GLubyte
*tempImage
= NULL
;
127 assert(dstFormat
== MESA_FORMAT_RGB_DXT1
||
128 dstFormat
== MESA_FORMAT_SRGB_DXT1
);
130 if (srcFormat
!= GL_RGB
||
131 srcType
!= GL_UNSIGNED_BYTE
||
132 ctx
->_ImageTransferState
||
133 ALIGN(srcPacking
->RowLength
, srcPacking
->Alignment
) != srcWidth
||
134 srcPacking
->SwapBytes
) {
135 /* convert image to RGB/GLubyte */
136 GLubyte
*tempImageSlices
[1];
137 int rgbRowStride
= 3 * srcWidth
* sizeof(GLubyte
);
138 tempImage
= malloc(srcWidth
* srcHeight
* 3 * sizeof(GLubyte
));
140 return GL_FALSE
; /* out of memory */
141 tempImageSlices
[0] = (GLubyte
*) tempImage
;
142 _mesa_texstore(ctx
, dims
,
144 MESA_FORMAT_RGB_UNORM8
,
145 rgbRowStride
, tempImageSlices
,
146 srcWidth
, srcHeight
, srcDepth
,
147 srcFormat
, srcType
, srcAddr
,
153 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
154 srcFormat
, srcType
, 0, 0);
159 if (ext_tx_compress_dxtn
) {
160 (*ext_tx_compress_dxtn
)(3, srcWidth
, srcHeight
, pixels
,
161 GL_COMPRESSED_RGB_S3TC_DXT1_EXT
,
165 _mesa_warning(ctx
, "external dxt library not available: texstore_rgb_dxt1");
168 free((void *) tempImage
);
175 * Store user's image in rgba_dxt1 format.
178 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS
)
180 const GLubyte
*pixels
;
182 const GLubyte
*tempImage
= NULL
;
184 assert(dstFormat
== MESA_FORMAT_RGBA_DXT1
||
185 dstFormat
== MESA_FORMAT_SRGBA_DXT1
);
187 if (srcFormat
!= GL_RGBA
||
188 srcType
!= GL_UNSIGNED_BYTE
||
189 ctx
->_ImageTransferState
||
190 ALIGN(srcPacking
->RowLength
, srcPacking
->Alignment
) != srcWidth
||
191 srcPacking
->SwapBytes
) {
192 /* convert image to RGBA/GLubyte */
193 GLubyte
*tempImageSlices
[1];
194 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
195 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
197 return GL_FALSE
; /* out of memory */
198 tempImageSlices
[0] = (GLubyte
*) tempImage
;
199 _mesa_texstore(ctx
, dims
,
201 _mesa_little_endian() ? MESA_FORMAT_R8G8B8A8_UNORM
202 : MESA_FORMAT_A8B8G8R8_UNORM
,
203 rgbaRowStride
, tempImageSlices
,
204 srcWidth
, srcHeight
, srcDepth
,
205 srcFormat
, srcType
, srcAddr
,
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 ALIGN(srcPacking
->RowLength
, srcPacking
->Alignment
) != srcWidth
||
249 srcPacking
->SwapBytes
) {
250 /* convert image to RGBA/GLubyte */
251 GLubyte
*tempImageSlices
[1];
252 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
253 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
255 return GL_FALSE
; /* out of memory */
256 tempImageSlices
[0] = (GLubyte
*) tempImage
;
257 _mesa_texstore(ctx
, dims
,
259 _mesa_little_endian() ? MESA_FORMAT_R8G8B8A8_UNORM
260 : MESA_FORMAT_A8B8G8R8_UNORM
,
261 rgbaRowStride
, tempImageSlices
,
262 srcWidth
, srcHeight
, srcDepth
,
263 srcFormat
, srcType
, srcAddr
,
268 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
269 srcFormat
, srcType
, 0, 0);
274 if (ext_tx_compress_dxtn
) {
275 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
276 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
,
280 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt3");
283 free((void *) tempImage
);
290 * Store user's image in rgba_dxt5 format.
293 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS
)
295 const GLubyte
*pixels
;
297 const GLubyte
*tempImage
= NULL
;
299 assert(dstFormat
== MESA_FORMAT_RGBA_DXT5
||
300 dstFormat
== MESA_FORMAT_SRGBA_DXT5
);
302 if (srcFormat
!= GL_RGBA
||
303 srcType
!= GL_UNSIGNED_BYTE
||
304 ctx
->_ImageTransferState
||
305 ALIGN(srcPacking
->RowLength
, srcPacking
->Alignment
) != srcWidth
||
306 srcPacking
->SwapBytes
) {
307 /* convert image to RGBA/GLubyte */
308 GLubyte
*tempImageSlices
[1];
309 int rgbaRowStride
= 4 * srcWidth
* sizeof(GLubyte
);
310 tempImage
= malloc(srcWidth
* srcHeight
* 4 * sizeof(GLubyte
));
312 return GL_FALSE
; /* out of memory */
313 tempImageSlices
[0] = (GLubyte
*) tempImage
;
314 _mesa_texstore(ctx
, dims
,
316 _mesa_little_endian() ? MESA_FORMAT_R8G8B8A8_UNORM
317 : MESA_FORMAT_A8B8G8R8_UNORM
,
318 rgbaRowStride
, tempImageSlices
,
319 srcWidth
, srcHeight
, srcDepth
,
320 srcFormat
, srcType
, srcAddr
,
325 pixels
= _mesa_image_address2d(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
326 srcFormat
, srcType
, 0, 0);
331 if (ext_tx_compress_dxtn
) {
332 (*ext_tx_compress_dxtn
)(4, srcWidth
, srcHeight
, pixels
,
333 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
,
337 _mesa_warning(ctx
, "external dxt library not available: texstore_rgba_dxt5");
340 free((void *) tempImage
);
346 /** Report problem with dxt texture decompression, once */
348 problem(const char *func
)
350 static GLboolean warned
= GL_FALSE
;
352 _mesa_debug(NULL
, "attempted to decode DXT texture without "
353 "library available: %s\n", func
);
360 fetch_rgb_dxt1(const GLubyte
*map
,
361 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
363 if (fetch_ext_rgb_dxt1
) {
365 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
366 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
367 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
368 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
369 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
377 fetch_rgba_dxt1(const GLubyte
*map
,
378 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
380 if (fetch_ext_rgba_dxt1
) {
382 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
383 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
384 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
385 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
386 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
389 problem("rgba_dxt1");
394 fetch_rgba_dxt3(const GLubyte
*map
,
395 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
397 if (fetch_ext_rgba_dxt3
) {
399 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
400 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
401 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
402 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
403 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
406 problem("rgba_dxt3");
411 fetch_rgba_dxt5(const GLubyte
*map
,
412 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
414 if (fetch_ext_rgba_dxt5
) {
416 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
417 texel
[RCOMP
] = UBYTE_TO_FLOAT(tex
[RCOMP
]);
418 texel
[GCOMP
] = UBYTE_TO_FLOAT(tex
[GCOMP
]);
419 texel
[BCOMP
] = UBYTE_TO_FLOAT(tex
[BCOMP
]);
420 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
423 problem("rgba_dxt5");
429 fetch_srgb_dxt1(const GLubyte
*map
,
430 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
432 if (fetch_ext_rgb_dxt1
) {
434 fetch_ext_rgb_dxt1(rowStride
, map
, i
, j
, tex
);
435 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
436 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
437 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
438 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
441 problem("srgb_dxt1");
446 fetch_srgba_dxt1(const GLubyte
*map
,
447 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
449 if (fetch_ext_rgba_dxt1
) {
451 fetch_ext_rgba_dxt1(rowStride
, map
, i
, j
, tex
);
452 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
453 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
454 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
455 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
458 problem("srgba_dxt1");
463 fetch_srgba_dxt3(const GLubyte
*map
,
464 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
466 if (fetch_ext_rgba_dxt3
) {
468 fetch_ext_rgba_dxt3(rowStride
, map
, i
, j
, tex
);
469 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
470 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
471 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
472 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
475 problem("srgba_dxt3");
480 fetch_srgba_dxt5(const GLubyte
*map
,
481 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
483 if (fetch_ext_rgba_dxt5
) {
485 fetch_ext_rgba_dxt5(rowStride
, map
, i
, j
, tex
);
486 texel
[RCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[RCOMP
]);
487 texel
[GCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[GCOMP
]);
488 texel
[BCOMP
] = util_format_srgb_8unorm_to_linear_float(tex
[BCOMP
]);
489 texel
[ACOMP
] = UBYTE_TO_FLOAT(tex
[ACOMP
]);
492 problem("srgba_dxt5");
498 compressed_fetch_func
499 _mesa_get_dxt_fetch_func(mesa_format format
)
502 case MESA_FORMAT_RGB_DXT1
:
503 return fetch_rgb_dxt1
;
504 case MESA_FORMAT_RGBA_DXT1
:
505 return fetch_rgba_dxt1
;
506 case MESA_FORMAT_RGBA_DXT3
:
507 return fetch_rgba_dxt3
;
508 case MESA_FORMAT_RGBA_DXT5
:
509 return fetch_rgba_dxt5
;
510 case MESA_FORMAT_SRGB_DXT1
:
511 return fetch_srgb_dxt1
;
512 case MESA_FORMAT_SRGBA_DXT1
:
513 return fetch_srgba_dxt1
;
514 case MESA_FORMAT_SRGBA_DXT3
:
515 return fetch_srgba_dxt3
;
516 case MESA_FORMAT_SRGBA_DXT5
:
517 return fetch_srgba_dxt5
;