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