mesa: s/INLINE/inline/
[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 (void) dstImageOffsets;
178
179 if (srcFormat != GL_RGB ||
180 srcType != GL_UNSIGNED_BYTE ||
181 ctx->_ImageTransferState ||
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 = (const GLubyte *) srcAddr;
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 GLubyte *pixels;
226 GLubyte *dst;
227 const GLint texWidth = dstRowStride * 4 / 8; /* a bit of a hack */
228 const GLubyte *tempImage = NULL;
229
230 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT1 ||
231 dstFormat == MESA_FORMAT_SRGBA_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 != GL_UNSIGNED_BYTE ||
240 ctx->_ImageTransferState ||
241 srcPacking->SwapBytes) {
242 /* convert image to RGBA/GLubyte */
243 tempImage = _mesa_make_temp_ubyte_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 srcFormat = GL_RGBA;
253 }
254 else {
255 pixels = (const GLubyte *) srcAddr;
256 }
257
258 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
259 dstFormat,
260 texWidth, (GLubyte *) dstAddr);
261 if (ext_tx_compress_dxtn) {
262 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
263 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
264 dst, dstRowStride);
265 }
266 else {
267 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt1");
268 }
269
270 if (tempImage)
271 free((void*) tempImage);
272
273 return GL_TRUE;
274 }
275
276
277 /**
278 * Store user's image in rgba_dxt3 format.
279 */
280 GLboolean
281 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS)
282 {
283 const GLubyte *pixels;
284 GLubyte *dst;
285 const GLint texWidth = dstRowStride * 4 / 16; /* a bit of a hack */
286 const GLubyte *tempImage = NULL;
287
288 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT3 ||
289 dstFormat == MESA_FORMAT_SRGBA_DXT3);
290 ASSERT(dstXoffset % 4 == 0);
291 ASSERT(dstYoffset % 4 == 0);
292 ASSERT(dstZoffset % 4 == 0);
293 (void) dstZoffset;
294 (void) dstImageOffsets;
295
296 if (srcFormat != GL_RGBA ||
297 srcType != GL_UNSIGNED_BYTE ||
298 ctx->_ImageTransferState ||
299 srcPacking->SwapBytes) {
300 /* convert image to RGBA/GLubyte */
301 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
302 baseInternalFormat,
303 _mesa_get_format_base_format(dstFormat),
304 srcWidth, srcHeight, srcDepth,
305 srcFormat, srcType, srcAddr,
306 srcPacking);
307 if (!tempImage)
308 return GL_FALSE; /* out of memory */
309 pixels = tempImage;
310 }
311 else {
312 pixels = (const GLubyte *) srcAddr;
313 }
314
315 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
316 dstFormat,
317 texWidth, (GLubyte *) dstAddr);
318 if (ext_tx_compress_dxtn) {
319 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
320 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
321 dst, dstRowStride);
322 }
323 else {
324 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt3");
325 }
326
327 if (tempImage)
328 free((void *) tempImage);
329
330 return GL_TRUE;
331 }
332
333
334 /**
335 * Store user's image in rgba_dxt5 format.
336 */
337 GLboolean
338 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS)
339 {
340 const GLubyte *pixels;
341 GLubyte *dst;
342 const GLint texWidth = dstRowStride * 4 / 16; /* a bit of a hack */
343 const GLubyte *tempImage = NULL;
344
345 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT5 ||
346 dstFormat == MESA_FORMAT_SRGBA_DXT5);
347 ASSERT(dstXoffset % 4 == 0);
348 ASSERT(dstYoffset % 4 == 0);
349 ASSERT(dstZoffset % 4 == 0);
350 (void) dstZoffset;
351 (void) dstImageOffsets;
352
353 if (srcFormat != GL_RGBA ||
354 srcType != GL_UNSIGNED_BYTE ||
355 ctx->_ImageTransferState ||
356 srcPacking->SwapBytes) {
357 /* convert image to RGBA/GLubyte */
358 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
359 baseInternalFormat,
360 _mesa_get_format_base_format(dstFormat),
361 srcWidth, srcHeight, srcDepth,
362 srcFormat, srcType, srcAddr,
363 srcPacking);
364 if (!tempImage)
365 return GL_FALSE; /* out of memory */
366 pixels = tempImage;
367 }
368 else {
369 pixels = (const GLubyte *) srcAddr;
370 }
371
372 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
373 dstFormat,
374 texWidth, (GLubyte *) dstAddr);
375 if (ext_tx_compress_dxtn) {
376 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
377 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
378 dst, dstRowStride);
379 }
380 else {
381 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt5");
382 }
383
384 if (tempImage)
385 free((void *) tempImage);
386
387 return GL_TRUE;
388 }
389
390
391 static void
392 fetch_texel_2d_rgb_dxt1( const struct swrast_texture_image *texImage,
393 GLint i, GLint j, GLint k, GLubyte *texel )
394 {
395 (void) k;
396 if (fetch_ext_rgb_dxt1) {
397 fetch_ext_rgb_dxt1(texImage->Base.RowStride,
398 (GLubyte *)(texImage)->Base.Data, i, j, texel);
399 }
400 else
401 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgb_dxt1");
402 }
403
404
405 void
406 _mesa_fetch_texel_2d_f_rgb_dxt1(const struct swrast_texture_image *texImage,
407 GLint i, GLint j, GLint k, GLfloat *texel)
408 {
409 /* just sample as GLubyte and convert to float here */
410 GLubyte rgba[4];
411 fetch_texel_2d_rgb_dxt1(texImage, i, j, k, rgba);
412 texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
413 texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
414 texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
415 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
416 }
417
418
419 static void
420 fetch_texel_2d_rgba_dxt1( const struct swrast_texture_image *texImage,
421 GLint i, GLint j, GLint k, GLubyte *texel )
422 {
423 (void) k;
424 if (fetch_ext_rgba_dxt1) {
425 fetch_ext_rgba_dxt1(texImage->Base.RowStride,
426 (GLubyte *)(texImage)->Base.Data, i, j, texel);
427 }
428 else
429 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt1\n");
430 }
431
432
433 void
434 _mesa_fetch_texel_2d_f_rgba_dxt1(const struct swrast_texture_image *texImage,
435 GLint i, GLint j, GLint k, GLfloat *texel)
436 {
437 /* just sample as GLubyte and convert to float here */
438 GLubyte rgba[4];
439 fetch_texel_2d_rgba_dxt1(texImage, i, j, k, rgba);
440 texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
441 texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
442 texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
443 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
444 }
445
446
447 static void
448 fetch_texel_2d_rgba_dxt3( const struct swrast_texture_image *texImage,
449 GLint i, GLint j, GLint k, GLubyte *texel )
450 {
451 (void) k;
452 if (fetch_ext_rgba_dxt3) {
453 fetch_ext_rgba_dxt3(texImage->Base.RowStride,
454 (GLubyte *)(texImage)->Base.Data,
455 i, j, texel);
456 }
457 else
458 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt3\n");
459 }
460
461
462 void
463 _mesa_fetch_texel_2d_f_rgba_dxt3(const struct swrast_texture_image *texImage,
464 GLint i, GLint j, GLint k, GLfloat *texel)
465 {
466 /* just sample as GLubyte and convert to float here */
467 GLubyte rgba[4];
468 fetch_texel_2d_rgba_dxt3(texImage, i, j, k, rgba);
469 texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
470 texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
471 texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
472 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
473 }
474
475
476 static void
477 fetch_texel_2d_rgba_dxt5( const struct swrast_texture_image *texImage,
478 GLint i, GLint j, GLint k, GLubyte *texel )
479 {
480 (void) k;
481 if (fetch_ext_rgba_dxt5) {
482 fetch_ext_rgba_dxt5(texImage->Base.RowStride,
483 (GLubyte *)(texImage)->Base.Data,
484 i, j, texel);
485 }
486 else
487 _mesa_debug(NULL, "attempted to decode s3tc texture without library available: fetch_texel_2d_rgba_dxt5\n");
488 }
489
490
491 void
492 _mesa_fetch_texel_2d_f_rgba_dxt5(const struct swrast_texture_image *texImage,
493 GLint i, GLint j, GLint k, GLfloat *texel)
494 {
495 /* just sample as GLubyte and convert to float here */
496 GLubyte rgba[4];
497 fetch_texel_2d_rgba_dxt5(texImage, i, j, k, rgba);
498 texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
499 texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
500 texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
501 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
502 }
503
504 #if FEATURE_EXT_texture_sRGB
505 void
506 _mesa_fetch_texel_2d_f_srgb_dxt1( const struct swrast_texture_image *texImage,
507 GLint i, GLint j, GLint k, GLfloat *texel )
508 {
509 /* just sample as GLubyte and convert to float here */
510 GLubyte rgba[4];
511 fetch_texel_2d_rgb_dxt1(texImage, i, j, k, rgba);
512 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
513 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
514 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
515 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
516 }
517
518 void
519 _mesa_fetch_texel_2d_f_srgba_dxt1(const struct swrast_texture_image *texImage,
520 GLint i, GLint j, GLint k, GLfloat *texel)
521 {
522 /* just sample as GLubyte and convert to float here */
523 GLubyte rgba[4];
524 fetch_texel_2d_rgba_dxt1(texImage, i, j, k, rgba);
525 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
526 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
527 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
528 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
529 }
530
531 void
532 _mesa_fetch_texel_2d_f_srgba_dxt3(const struct swrast_texture_image *texImage,
533 GLint i, GLint j, GLint k, GLfloat *texel)
534 {
535 /* just sample as GLubyte and convert to float here */
536 GLubyte rgba[4];
537 fetch_texel_2d_rgba_dxt3(texImage, i, j, k, rgba);
538 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
539 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
540 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
541 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
542 }
543
544 void
545 _mesa_fetch_texel_2d_f_srgba_dxt5(const struct swrast_texture_image *texImage,
546 GLint i, GLint j, GLint k, GLfloat *texel)
547 {
548 /* just sample as GLubyte and convert to float here */
549 GLubyte rgba[4];
550 fetch_texel_2d_rgba_dxt5(texImage, i, j, k, rgba);
551 texel[RCOMP] = nonlinear_to_linear(rgba[RCOMP]);
552 texel[GCOMP] = nonlinear_to_linear(rgba[GCOMP]);
553 texel[BCOMP] = nonlinear_to_linear(rgba[BCOMP]);
554 texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
555 }
556 #endif /* FEATURE_EXT_texture_sRGB */
557
558
559 #endif /* FEATURE_texture_s3tc */