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