Fix gcc version checks for _mesa_bitcount
[mesa.git] / src / mesa / main / texcompress_s3tc.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008 VMware, Inc.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file texcompress_s3tc.c
29 * GL_EXT_texture_compression_s3tc support.
30 */
31
32 #ifndef USE_EXTERNAL_DXTN_LIB
33 #define USE_EXTERNAL_DXTN_LIB 1
34 #endif
35
36 #include "glheader.h"
37 #include "imports.h"
38 #include "colormac.h"
39 #include "dlopen.h"
40 #include "image.h"
41 #include "macros.h"
42 #include "mfeatures.h"
43 #include "mtypes.h"
44 #include "texcompress.h"
45 #include "texcompress_s3tc.h"
46 #include "texstore.h"
47 #include "swrast/s_context.h"
48
49
50 #if FEATURE_texture_s3tc
51
52
53 #if defined(_WIN32) || defined(WIN32)
54 #define DXTN_LIBNAME "dxtn.dll"
55 #define RTLD_LAZY 0
56 #define RTLD_GLOBAL 0
57 #elif defined(__DJGPP__)
58 #define DXTN_LIBNAME "dxtn.dxe"
59 #else
60 #define DXTN_LIBNAME "libtxc_dxtn.so"
61 #endif
62
63 #if FEATURE_EXT_texture_sRGB
64 /**
65 * Convert an 8-bit sRGB value from non-linear space to a
66 * linear RGB value in [0, 1].
67 * Implemented with a 256-entry lookup table.
68 */
69 static inline GLfloat
70 nonlinear_to_linear(GLubyte cs8)
71 {
72 static GLfloat table[256];
73 static GLboolean tableReady = GL_FALSE;
74 if (!tableReady) {
75 /* compute lookup table now */
76 GLuint i;
77 for (i = 0; i < 256; i++) {
78 const GLfloat cs = UBYTE_TO_FLOAT(i);
79 if (cs <= 0.04045) {
80 table[i] = cs / 12.92f;
81 }
82 else {
83 table[i] = (GLfloat) pow((cs + 0.055) / 1.055, 2.4);
84 }
85 }
86 tableReady = GL_TRUE;
87 }
88 return table[cs8];
89 }
90 #endif /* FEATURE_EXT_texture_sRGB */
91
92 typedef void (*dxtFetchTexelFuncExt)( GLint srcRowstride, GLubyte *pixdata, GLint col, GLint row, GLvoid *texelOut );
93
94 dxtFetchTexelFuncExt fetch_ext_rgb_dxt1 = NULL;
95 dxtFetchTexelFuncExt fetch_ext_rgba_dxt1 = NULL;
96 dxtFetchTexelFuncExt fetch_ext_rgba_dxt3 = NULL;
97 dxtFetchTexelFuncExt fetch_ext_rgba_dxt5 = NULL;
98
99 typedef void (*dxtCompressTexFuncExt)(GLint srccomps, GLint width,
100 GLint height, const GLubyte *srcPixData,
101 GLenum destformat, GLubyte *dest,
102 GLint dstRowStride);
103
104 static dxtCompressTexFuncExt ext_tx_compress_dxtn = NULL;
105
106 static void *dxtlibhandle = NULL;
107
108
109 void
110 _mesa_init_texture_s3tc( struct gl_context *ctx )
111 {
112 /* called during context initialization */
113 ctx->Mesa_DXTn = GL_FALSE;
114 #if USE_EXTERNAL_DXTN_LIB
115 if (!dxtlibhandle) {
116 dxtlibhandle = _mesa_dlopen(DXTN_LIBNAME, 0);
117 if (!dxtlibhandle) {
118 _mesa_warning(ctx, "couldn't open " DXTN_LIBNAME ", software DXTn "
119 "compression/decompression unavailable");
120 }
121 else {
122 /* the fetch functions are not per context! Might be problematic... */
123 fetch_ext_rgb_dxt1 = (dxtFetchTexelFuncExt)
124 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgb_dxt1");
125 fetch_ext_rgba_dxt1 = (dxtFetchTexelFuncExt)
126 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt1");
127 fetch_ext_rgba_dxt3 = (dxtFetchTexelFuncExt)
128 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt3");
129 fetch_ext_rgba_dxt5 = (dxtFetchTexelFuncExt)
130 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt5");
131 ext_tx_compress_dxtn = (dxtCompressTexFuncExt)
132 _mesa_dlsym(dxtlibhandle, "tx_compress_dxtn");
133
134 if (!fetch_ext_rgb_dxt1 ||
135 !fetch_ext_rgba_dxt1 ||
136 !fetch_ext_rgba_dxt3 ||
137 !fetch_ext_rgba_dxt5 ||
138 !ext_tx_compress_dxtn) {
139 _mesa_warning(ctx, "couldn't reference all symbols in "
140 DXTN_LIBNAME ", software DXTn compression/decompression "
141 "unavailable");
142 fetch_ext_rgb_dxt1 = NULL;
143 fetch_ext_rgba_dxt1 = NULL;
144 fetch_ext_rgba_dxt3 = NULL;
145 fetch_ext_rgba_dxt5 = NULL;
146 ext_tx_compress_dxtn = NULL;
147 _mesa_dlclose(dxtlibhandle);
148 dxtlibhandle = NULL;
149 }
150 }
151 }
152 if (dxtlibhandle) {
153 ctx->Mesa_DXTn = GL_TRUE;
154 }
155 #else
156 (void) ctx;
157 #endif
158 }
159
160 /**
161 * Store user's image in rgb_dxt1 format.
162 */
163 GLboolean
164 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS)
165 {
166 const GLubyte *pixels;
167 GLubyte *dst;
168 const GLint texWidth = dstRowStride * 4 / 8; /* a bit of a hack */
169 const GLubyte *tempImage = NULL;
170
171 ASSERT(dstFormat == MESA_FORMAT_RGB_DXT1 ||
172 dstFormat == MESA_FORMAT_SRGB_DXT1);
173 ASSERT(dstXoffset % 4 == 0);
174 ASSERT(dstYoffset % 4 == 0);
175 ASSERT(dstZoffset % 4 == 0);
176 (void) dstZoffset;
177
178 if (srcFormat != GL_RGB ||
179 srcType != GL_UNSIGNED_BYTE ||
180 ctx->_ImageTransferState ||
181 srcPacking->RowLength != srcWidth ||
182 srcPacking->SwapBytes) {
183 /* convert image to RGB/GLubyte */
184 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
185 baseInternalFormat,
186 _mesa_get_format_base_format(dstFormat),
187 srcWidth, srcHeight, srcDepth,
188 srcFormat, srcType, srcAddr,
189 srcPacking);
190 if (!tempImage)
191 return GL_FALSE; /* out of memory */
192 pixels = tempImage;
193 srcFormat = GL_RGB;
194 }
195 else {
196 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
197 srcFormat, srcType, 0, 0);
198 }
199
200 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
201 dstFormat,
202 texWidth, dstSlices[0]);
203
204 if (ext_tx_compress_dxtn) {
205 (*ext_tx_compress_dxtn)(3, srcWidth, srcHeight, pixels,
206 GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
207 dst, dstRowStride);
208 }
209 else {
210 _mesa_warning(ctx, "external dxt library not available: texstore_rgb_dxt1");
211 }
212
213 if (tempImage)
214 free((void *) tempImage);
215
216 return GL_TRUE;
217 }
218
219
220 /**
221 * Store user's image in rgba_dxt1 format.
222 */
223 GLboolean
224 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS)
225 {
226 const GLubyte *pixels;
227 GLubyte *dst;
228 const GLint texWidth = dstRowStride * 4 / 8; /* a bit of a hack */
229 const GLubyte *tempImage = NULL;
230
231 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT1 ||
232 dstFormat == MESA_FORMAT_SRGBA_DXT1);
233 ASSERT(dstXoffset % 4 == 0);
234 ASSERT(dstYoffset % 4 == 0);
235 ASSERT(dstZoffset % 4 == 0);
236 (void) dstZoffset;
237
238 if (srcFormat != GL_RGBA ||
239 srcType != GL_UNSIGNED_BYTE ||
240 ctx->_ImageTransferState ||
241 srcPacking->RowLength != srcWidth ||
242 srcPacking->SwapBytes) {
243 /* convert image to RGBA/GLubyte */
244 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
245 baseInternalFormat,
246 _mesa_get_format_base_format(dstFormat),
247 srcWidth, srcHeight, srcDepth,
248 srcFormat, srcType, srcAddr,
249 srcPacking);
250 if (!tempImage)
251 return GL_FALSE; /* out of memory */
252 pixels = tempImage;
253 srcFormat = GL_RGBA;
254 }
255 else {
256 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
257 srcFormat, srcType, 0, 0);
258 }
259
260 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
261 dstFormat,
262 texWidth, dstSlices[0]);
263 if (ext_tx_compress_dxtn) {
264 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
265 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
266 dst, dstRowStride);
267 }
268 else {
269 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt1");
270 }
271
272 if (tempImage)
273 free((void*) tempImage);
274
275 return GL_TRUE;
276 }
277
278
279 /**
280 * Store user's image in rgba_dxt3 format.
281 */
282 GLboolean
283 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS)
284 {
285 const GLubyte *pixels;
286 GLubyte *dst;
287 const GLint texWidth = dstRowStride * 4 / 16; /* a bit of a hack */
288 const GLubyte *tempImage = NULL;
289
290 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT3 ||
291 dstFormat == MESA_FORMAT_SRGBA_DXT3);
292 ASSERT(dstXoffset % 4 == 0);
293 ASSERT(dstYoffset % 4 == 0);
294 ASSERT(dstZoffset % 4 == 0);
295 (void) dstZoffset;
296
297 if (srcFormat != GL_RGBA ||
298 srcType != GL_UNSIGNED_BYTE ||
299 ctx->_ImageTransferState ||
300 srcPacking->RowLength != srcWidth ||
301 srcPacking->SwapBytes) {
302 /* convert image to RGBA/GLubyte */
303 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
304 baseInternalFormat,
305 _mesa_get_format_base_format(dstFormat),
306 srcWidth, srcHeight, srcDepth,
307 srcFormat, srcType, srcAddr,
308 srcPacking);
309 if (!tempImage)
310 return GL_FALSE; /* out of memory */
311 pixels = tempImage;
312 }
313 else {
314 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
315 srcFormat, srcType, 0, 0);
316 }
317
318 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
319 dstFormat,
320 texWidth, dstSlices[0]);
321 if (ext_tx_compress_dxtn) {
322 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
323 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
324 dst, dstRowStride);
325 }
326 else {
327 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt3");
328 }
329
330 if (tempImage)
331 free((void *) tempImage);
332
333 return GL_TRUE;
334 }
335
336
337 /**
338 * Store user's image in rgba_dxt5 format.
339 */
340 GLboolean
341 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS)
342 {
343 const GLubyte *pixels;
344 GLubyte *dst;
345 const GLint texWidth = dstRowStride * 4 / 16; /* a bit of a hack */
346 const GLubyte *tempImage = NULL;
347
348 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT5 ||
349 dstFormat == MESA_FORMAT_SRGBA_DXT5);
350 ASSERT(dstXoffset % 4 == 0);
351 ASSERT(dstYoffset % 4 == 0);
352 ASSERT(dstZoffset % 4 == 0);
353 (void) dstZoffset;
354
355 if (srcFormat != GL_RGBA ||
356 srcType != GL_UNSIGNED_BYTE ||
357 ctx->_ImageTransferState ||
358 srcPacking->RowLength != srcWidth ||
359 srcPacking->SwapBytes) {
360 /* convert image to RGBA/GLubyte */
361 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
362 baseInternalFormat,
363 _mesa_get_format_base_format(dstFormat),
364 srcWidth, srcHeight, srcDepth,
365 srcFormat, srcType, srcAddr,
366 srcPacking);
367 if (!tempImage)
368 return GL_FALSE; /* out of memory */
369 pixels = tempImage;
370 }
371 else {
372 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
373 srcFormat, srcType, 0, 0);
374 }
375
376 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
377 dstFormat,
378 texWidth, dstSlices[0]);
379 if (ext_tx_compress_dxtn) {
380 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
381 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
382 dst, dstRowStride);
383 }
384 else {
385 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt5");
386 }
387
388 if (tempImage)
389 free((void *) tempImage);
390
391 return GL_TRUE;
392 }
393
394
395 static void
396 fetch_texel_2d_rgb_dxt1( const struct swrast_texture_image *texImage,
397 GLint i, GLint j, GLint k, GLubyte *texel )
398 {
399 (void) k;
400 if (fetch_ext_rgb_dxt1) {
401 fetch_ext_rgb_dxt1(texImage->Base.RowStride,
402 (GLubyte *)(texImage)->Base.Data, i, j, texel);
403 }
404 else
405 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
406 }
407
408
409 void
410 _mesa_fetch_texel_2d_f_rgb_dxt1(const struct swrast_texture_image *texImage,
411 GLint i, GLint j, GLint k, GLfloat *texel)
412 {
413 /* just sample as GLubyte and convert to float here */
414 GLubyte rgba[4];
415 fetch_texel_2d_rgb_dxt1(texImage, i, j, k, rgba);
416 texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
417 texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
418 texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
419 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
420 }
421
422
423 static void
424 fetch_texel_2d_rgba_dxt1( const struct swrast_texture_image *texImage,
425 GLint i, GLint j, GLint k, GLubyte *texel )
426 {
427 (void) k;
428 if (fetch_ext_rgba_dxt1) {
429 fetch_ext_rgba_dxt1(texImage->Base.RowStride,
430 (GLubyte *)(texImage)->Base.Data, i, j, texel);
431 }
432 else
433 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
434 }
435
436
437 void
438 _mesa_fetch_texel_2d_f_rgba_dxt1(const struct swrast_texture_image *texImage,
439 GLint i, GLint j, GLint k, GLfloat *texel)
440 {
441 /* just sample as GLubyte and convert to float here */
442 GLubyte rgba[4];
443 fetch_texel_2d_rgba_dxt1(texImage, i, j, k, rgba);
444 texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
445 texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
446 texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
447 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
448 }
449
450
451 static void
452 fetch_texel_2d_rgba_dxt3( const struct swrast_texture_image *texImage,
453 GLint i, GLint j, GLint k, GLubyte *texel )
454 {
455 (void) k;
456 if (fetch_ext_rgba_dxt3) {
457 fetch_ext_rgba_dxt3(texImage->Base.RowStride,
458 (GLubyte *)(texImage)->Base.Data,
459 i, j, texel);
460 }
461 else
462 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
463 }
464
465
466 void
467 _mesa_fetch_texel_2d_f_rgba_dxt3(const struct swrast_texture_image *texImage,
468 GLint i, GLint j, GLint k, GLfloat *texel)
469 {
470 /* just sample as GLubyte and convert to float here */
471 GLubyte rgba[4];
472 fetch_texel_2d_rgba_dxt3(texImage, i, j, k, rgba);
473 texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
474 texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
475 texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
476 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
477 }
478
479
480 static void
481 fetch_texel_2d_rgba_dxt5( const struct swrast_texture_image *texImage,
482 GLint i, GLint j, GLint k, GLubyte *texel )
483 {
484 (void) k;
485 if (fetch_ext_rgba_dxt5) {
486 fetch_ext_rgba_dxt5(texImage->Base.RowStride,
487 (GLubyte *)(texImage)->Base.Data,
488 i, j, texel);
489 }
490 else
491 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
492 }
493
494
495 void
496 _mesa_fetch_texel_2d_f_rgba_dxt5(const struct swrast_texture_image *texImage,
497 GLint i, GLint j, GLint k, GLfloat *texel)
498 {
499 /* just sample as GLubyte and convert to float here */
500 GLubyte rgba[4];
501 fetch_texel_2d_rgba_dxt5(texImage, i, j, k, rgba);
502 texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
503 texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
504 texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
505 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
506 }
507
508 #if FEATURE_EXT_texture_sRGB
509 void
510 _mesa_fetch_texel_2d_f_srgb_dxt1( const struct swrast_texture_image *texImage,
511 GLint i, GLint j, GLint k, GLfloat *texel )
512 {
513 /* just sample as GLubyte and convert to float here */
514 GLubyte rgba[4];
515 fetch_texel_2d_rgb_dxt1(texImage, i, j, k, rgba);
516 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
517 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
518 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
519 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
520 }
521
522 void
523 _mesa_fetch_texel_2d_f_srgba_dxt1(const struct swrast_texture_image *texImage,
524 GLint i, GLint j, GLint k, GLfloat *texel)
525 {
526 /* just sample as GLubyte and convert to float here */
527 GLubyte rgba[4];
528 fetch_texel_2d_rgba_dxt1(texImage, i, j, k, rgba);
529 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
530 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
531 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
532 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
533 }
534
535 void
536 _mesa_fetch_texel_2d_f_srgba_dxt3(const struct swrast_texture_image *texImage,
537 GLint i, GLint j, GLint k, GLfloat *texel)
538 {
539 /* just sample as GLubyte and convert to float here */
540 GLubyte rgba[4];
541 fetch_texel_2d_rgba_dxt3(texImage, i, j, k, rgba);
542 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
543 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
544 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
545 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
546 }
547
548 void
549 _mesa_fetch_texel_2d_f_srgba_dxt5(const struct swrast_texture_image *texImage,
550 GLint i, GLint j, GLint k, GLfloat *texel)
551 {
552 /* just sample as GLubyte and convert to float here */
553 GLubyte rgba[4];
554 fetch_texel_2d_rgba_dxt5(texImage, i, j, k, rgba);
555 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
556 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
557 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
558 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
559 }
560 #endif /* FEATURE_EXT_texture_sRGB */
561
562
563 #endif /* FEATURE_texture_s3tc */