mesa: remove unneeded #include of colormac.h
[mesa.git] / src / mesa / main / texcompress_s3tc.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008 VMware, Inc.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
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 "dlopen.h"
39 #include "image.h"
40 #include "macros.h"
41 #include "mtypes.h"
42 #include "texcompress.h"
43 #include "texcompress_s3tc.h"
44 #include "texstore.h"
45 #include "format_unpack.h"
46 #include "util/format_srgb.h"
47
48
49 #if defined(_WIN32) || defined(WIN32)
50 #define DXTN_LIBNAME "dxtn.dll"
51 #define RTLD_LAZY 0
52 #define RTLD_GLOBAL 0
53 #else
54 #define DXTN_LIBNAME "libtxc_dxtn.so"
55 #endif
56
57 typedef void (*dxtFetchTexelFuncExt)( GLint srcRowstride, const GLubyte *pixdata, GLint col, GLint row, GLvoid *texelOut );
58
59 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1 = NULL;
60 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1 = NULL;
61 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3 = NULL;
62 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5 = NULL;
63
64 typedef void (*dxtCompressTexFuncExt)(GLint srccomps, GLint width,
65 GLint height, const GLubyte *srcPixData,
66 GLenum destformat, GLubyte *dest,
67 GLint dstRowStride);
68
69 static dxtCompressTexFuncExt ext_tx_compress_dxtn = NULL;
70
71 static void *dxtlibhandle = NULL;
72
73
74 void
75 _mesa_init_texture_s3tc( struct gl_context *ctx )
76 {
77 /* called during context initialization */
78 ctx->Mesa_DXTn = GL_FALSE;
79 #if USE_EXTERNAL_DXTN_LIB
80 if (!dxtlibhandle) {
81 dxtlibhandle = _mesa_dlopen(DXTN_LIBNAME, 0);
82 if (!dxtlibhandle) {
83 _mesa_warning(ctx, "couldn't open " DXTN_LIBNAME ", software DXTn "
84 "compression/decompression unavailable");
85 }
86 else {
87 /* the fetch functions are not per context! Might be problematic... */
88 fetch_ext_rgb_dxt1 = (dxtFetchTexelFuncExt)
89 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgb_dxt1");
90 fetch_ext_rgba_dxt1 = (dxtFetchTexelFuncExt)
91 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt1");
92 fetch_ext_rgba_dxt3 = (dxtFetchTexelFuncExt)
93 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt3");
94 fetch_ext_rgba_dxt5 = (dxtFetchTexelFuncExt)
95 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt5");
96 ext_tx_compress_dxtn = (dxtCompressTexFuncExt)
97 _mesa_dlsym(dxtlibhandle, "tx_compress_dxtn");
98
99 if (!fetch_ext_rgb_dxt1 ||
100 !fetch_ext_rgba_dxt1 ||
101 !fetch_ext_rgba_dxt3 ||
102 !fetch_ext_rgba_dxt5 ||
103 !ext_tx_compress_dxtn) {
104 _mesa_warning(ctx, "couldn't reference all symbols in "
105 DXTN_LIBNAME ", software DXTn compression/decompression "
106 "unavailable");
107 fetch_ext_rgb_dxt1 = NULL;
108 fetch_ext_rgba_dxt1 = NULL;
109 fetch_ext_rgba_dxt3 = NULL;
110 fetch_ext_rgba_dxt5 = NULL;
111 ext_tx_compress_dxtn = NULL;
112 _mesa_dlclose(dxtlibhandle);
113 dxtlibhandle = NULL;
114 }
115 }
116 }
117 if (dxtlibhandle) {
118 ctx->Mesa_DXTn = GL_TRUE;
119 }
120 #else
121 (void) ctx;
122 #endif
123 }
124
125 /**
126 * Store user's image in rgb_dxt1 format.
127 */
128 GLboolean
129 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS)
130 {
131 const GLubyte *pixels;
132 GLubyte *dst;
133 const GLubyte *tempImage = NULL;
134
135 assert(dstFormat == MESA_FORMAT_RGB_DXT1 ||
136 dstFormat == MESA_FORMAT_SRGB_DXT1);
137
138 if (srcFormat != GL_RGB ||
139 srcType != GL_UNSIGNED_BYTE ||
140 ctx->_ImageTransferState ||
141 srcPacking->RowLength != srcWidth ||
142 srcPacking->SwapBytes) {
143 /* convert image to RGB/GLubyte */
144 GLubyte *tempImageSlices[1];
145 int rgbRowStride = 3 * srcWidth * sizeof(GLubyte);
146 tempImage = malloc(srcWidth * srcHeight * 3 * sizeof(GLubyte));
147 if (!tempImage)
148 return GL_FALSE; /* out of memory */
149 tempImageSlices[0] = (GLubyte *) tempImage;
150 _mesa_texstore(ctx, dims,
151 baseInternalFormat,
152 MESA_FORMAT_RGB_UNORM8,
153 rgbRowStride, tempImageSlices,
154 srcWidth, srcHeight, srcDepth,
155 srcFormat, srcType, srcAddr,
156 srcPacking);
157 pixels = tempImage;
158 srcFormat = GL_RGB;
159 }
160 else {
161 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
162 srcFormat, srcType, 0, 0);
163 }
164
165 dst = dstSlices[0];
166
167 if (ext_tx_compress_dxtn) {
168 (*ext_tx_compress_dxtn)(3, srcWidth, srcHeight, pixels,
169 GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
170 dst, dstRowStride);
171 }
172 else {
173 _mesa_warning(ctx, "external dxt library not available: texstore_rgb_dxt1");
174 }
175
176 free((void *) tempImage);
177
178 return GL_TRUE;
179 }
180
181
182 /**
183 * Store user's image in rgba_dxt1 format.
184 */
185 GLboolean
186 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS)
187 {
188 const GLubyte *pixels;
189 GLubyte *dst;
190 const GLubyte *tempImage = NULL;
191
192 assert(dstFormat == MESA_FORMAT_RGBA_DXT1 ||
193 dstFormat == MESA_FORMAT_SRGBA_DXT1);
194
195 if (srcFormat != GL_RGBA ||
196 srcType != GL_UNSIGNED_BYTE ||
197 ctx->_ImageTransferState ||
198 srcPacking->RowLength != srcWidth ||
199 srcPacking->SwapBytes) {
200 /* convert image to RGBA/GLubyte */
201 GLubyte *tempImageSlices[1];
202 int rgbaRowStride = 4 * srcWidth * sizeof(GLubyte);
203 tempImage = malloc(srcWidth * srcHeight * 4 * sizeof(GLubyte));
204 if (!tempImage)
205 return GL_FALSE; /* out of memory */
206 tempImageSlices[0] = (GLubyte *) tempImage;
207 _mesa_texstore(ctx, dims,
208 baseInternalFormat,
209 MESA_FORMAT_R8G8B8A8_UNORM,
210 rgbaRowStride, tempImageSlices,
211 srcWidth, srcHeight, srcDepth,
212 srcFormat, srcType, srcAddr,
213 srcPacking);
214 pixels = tempImage;
215 srcFormat = GL_RGBA;
216 }
217 else {
218 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
219 srcFormat, srcType, 0, 0);
220 }
221
222 dst = dstSlices[0];
223
224 if (ext_tx_compress_dxtn) {
225 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
226 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
227 dst, dstRowStride);
228 }
229 else {
230 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt1");
231 }
232
233 free((void*) tempImage);
234
235 return GL_TRUE;
236 }
237
238
239 /**
240 * Store user's image in rgba_dxt3 format.
241 */
242 GLboolean
243 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS)
244 {
245 const GLubyte *pixels;
246 GLubyte *dst;
247 const GLubyte *tempImage = NULL;
248
249 assert(dstFormat == MESA_FORMAT_RGBA_DXT3 ||
250 dstFormat == MESA_FORMAT_SRGBA_DXT3);
251
252 if (srcFormat != GL_RGBA ||
253 srcType != GL_UNSIGNED_BYTE ||
254 ctx->_ImageTransferState ||
255 srcPacking->RowLength != srcWidth ||
256 srcPacking->SwapBytes) {
257 /* convert image to RGBA/GLubyte */
258 GLubyte *tempImageSlices[1];
259 int rgbaRowStride = 4 * srcWidth * sizeof(GLubyte);
260 tempImage = malloc(srcWidth * srcHeight * 4 * sizeof(GLubyte));
261 if (!tempImage)
262 return GL_FALSE; /* out of memory */
263 tempImageSlices[0] = (GLubyte *) tempImage;
264 _mesa_texstore(ctx, dims,
265 baseInternalFormat,
266 MESA_FORMAT_R8G8B8A8_UNORM,
267 rgbaRowStride, tempImageSlices,
268 srcWidth, srcHeight, srcDepth,
269 srcFormat, srcType, srcAddr,
270 srcPacking);
271 pixels = tempImage;
272 }
273 else {
274 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
275 srcFormat, srcType, 0, 0);
276 }
277
278 dst = dstSlices[0];
279
280 if (ext_tx_compress_dxtn) {
281 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
282 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
283 dst, dstRowStride);
284 }
285 else {
286 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt3");
287 }
288
289 free((void *) tempImage);
290
291 return GL_TRUE;
292 }
293
294
295 /**
296 * Store user's image in rgba_dxt5 format.
297 */
298 GLboolean
299 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS)
300 {
301 const GLubyte *pixels;
302 GLubyte *dst;
303 const GLubyte *tempImage = NULL;
304
305 assert(dstFormat == MESA_FORMAT_RGBA_DXT5 ||
306 dstFormat == MESA_FORMAT_SRGBA_DXT5);
307
308 if (srcFormat != GL_RGBA ||
309 srcType != GL_UNSIGNED_BYTE ||
310 ctx->_ImageTransferState ||
311 srcPacking->RowLength != srcWidth ||
312 srcPacking->SwapBytes) {
313 /* convert image to RGBA/GLubyte */
314 GLubyte *tempImageSlices[1];
315 int rgbaRowStride = 4 * srcWidth * sizeof(GLubyte);
316 tempImage = malloc(srcWidth * srcHeight * 4 * sizeof(GLubyte));
317 if (!tempImage)
318 return GL_FALSE; /* out of memory */
319 tempImageSlices[0] = (GLubyte *) tempImage;
320 _mesa_texstore(ctx, dims,
321 baseInternalFormat,
322 MESA_FORMAT_R8G8B8A8_UNORM,
323 rgbaRowStride, tempImageSlices,
324 srcWidth, srcHeight, srcDepth,
325 srcFormat, srcType, srcAddr,
326 srcPacking);
327 pixels = tempImage;
328 }
329 else {
330 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
331 srcFormat, srcType, 0, 0);
332 }
333
334 dst = dstSlices[0];
335
336 if (ext_tx_compress_dxtn) {
337 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
338 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
339 dst, dstRowStride);
340 }
341 else {
342 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt5");
343 }
344
345 free((void *) tempImage);
346
347 return GL_TRUE;
348 }
349
350
351 /** Report problem with dxt texture decompression, once */
352 static void
353 problem(const char *func)
354 {
355 static GLboolean warned = GL_FALSE;
356 if (!warned) {
357 _mesa_debug(NULL, "attempted to decode DXT texture without "
358 "library available: %s\n", func);
359 warned = GL_TRUE;
360 }
361 }
362
363
364 static void
365 fetch_rgb_dxt1(const GLubyte *map,
366 GLint rowStride, GLint i, GLint j, GLfloat *texel)
367 {
368 if (fetch_ext_rgb_dxt1) {
369 GLubyte tex[4];
370 fetch_ext_rgb_dxt1(rowStride, map, i, j, tex);
371 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
372 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
373 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
374 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
375 }
376 else {
377 problem("rgb_dxt1");
378 }
379 }
380
381 static void
382 fetch_rgba_dxt1(const GLubyte *map,
383 GLint rowStride, GLint i, GLint j, GLfloat *texel)
384 {
385 if (fetch_ext_rgba_dxt1) {
386 GLubyte tex[4];
387 fetch_ext_rgba_dxt1(rowStride, map, i, j, tex);
388 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
389 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
390 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
391 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
392 }
393 else {
394 problem("rgba_dxt1");
395 }
396 }
397
398 static void
399 fetch_rgba_dxt3(const GLubyte *map,
400 GLint rowStride, GLint i, GLint j, GLfloat *texel)
401 {
402 if (fetch_ext_rgba_dxt3) {
403 GLubyte tex[4];
404 fetch_ext_rgba_dxt3(rowStride, map, i, j, tex);
405 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
406 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
407 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
408 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
409 }
410 else {
411 problem("rgba_dxt3");
412 }
413 }
414
415 static void
416 fetch_rgba_dxt5(const GLubyte *map,
417 GLint rowStride, GLint i, GLint j, GLfloat *texel)
418 {
419 if (fetch_ext_rgba_dxt5) {
420 GLubyte tex[4];
421 fetch_ext_rgba_dxt5(rowStride, map, i, j, tex);
422 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
423 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
424 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
425 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
426 }
427 else {
428 problem("rgba_dxt5");
429 }
430 }
431
432
433 static void
434 fetch_srgb_dxt1(const GLubyte *map,
435 GLint rowStride, GLint i, GLint j, GLfloat *texel)
436 {
437 if (fetch_ext_rgb_dxt1) {
438 GLubyte tex[4];
439 fetch_ext_rgb_dxt1(rowStride, map, i, j, tex);
440 texel[RCOMP] = util_format_srgb_8unorm_to_linear_float(tex[RCOMP]);
441 texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(tex[GCOMP]);
442 texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(tex[BCOMP]);
443 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
444 }
445 else {
446 problem("srgb_dxt1");
447 }
448 }
449
450 static void
451 fetch_srgba_dxt1(const GLubyte *map,
452 GLint rowStride, GLint i, GLint j, GLfloat *texel)
453 {
454 if (fetch_ext_rgba_dxt1) {
455 GLubyte tex[4];
456 fetch_ext_rgba_dxt1(rowStride, map, i, j, tex);
457 texel[RCOMP] = util_format_srgb_8unorm_to_linear_float(tex[RCOMP]);
458 texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(tex[GCOMP]);
459 texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(tex[BCOMP]);
460 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
461 }
462 else {
463 problem("srgba_dxt1");
464 }
465 }
466
467 static void
468 fetch_srgba_dxt3(const GLubyte *map,
469 GLint rowStride, GLint i, GLint j, GLfloat *texel)
470 {
471 if (fetch_ext_rgba_dxt3) {
472 GLubyte tex[4];
473 fetch_ext_rgba_dxt3(rowStride, map, i, j, tex);
474 texel[RCOMP] = util_format_srgb_8unorm_to_linear_float(tex[RCOMP]);
475 texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(tex[GCOMP]);
476 texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(tex[BCOMP]);
477 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
478 }
479 else {
480 problem("srgba_dxt3");
481 }
482 }
483
484 static void
485 fetch_srgba_dxt5(const GLubyte *map,
486 GLint rowStride, GLint i, GLint j, GLfloat *texel)
487 {
488 if (fetch_ext_rgba_dxt5) {
489 GLubyte tex[4];
490 fetch_ext_rgba_dxt5(rowStride, map, i, j, tex);
491 texel[RCOMP] = util_format_srgb_8unorm_to_linear_float(tex[RCOMP]);
492 texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(tex[GCOMP]);
493 texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(tex[BCOMP]);
494 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
495 }
496 else {
497 problem("srgba_dxt5");
498 }
499 }
500
501
502
503 compressed_fetch_func
504 _mesa_get_dxt_fetch_func(mesa_format format)
505 {
506 switch (format) {
507 case MESA_FORMAT_RGB_DXT1:
508 return fetch_rgb_dxt1;
509 case MESA_FORMAT_RGBA_DXT1:
510 return fetch_rgba_dxt1;
511 case MESA_FORMAT_RGBA_DXT3:
512 return fetch_rgba_dxt3;
513 case MESA_FORMAT_RGBA_DXT5:
514 return fetch_rgba_dxt5;
515 case MESA_FORMAT_SRGB_DXT1:
516 return fetch_srgb_dxt1;
517 case MESA_FORMAT_SRGBA_DXT1:
518 return fetch_srgba_dxt1;
519 case MESA_FORMAT_SRGBA_DXT3:
520 return fetch_srgba_dxt3;
521 case MESA_FORMAT_SRGBA_DXT5:
522 return fetch_srgba_dxt5;
523 default:
524 return NULL;
525 }
526 }