mesa: remove old swrast-based compressed texel fetch code
[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 "format_unpack.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 typedef void (*dxtFetchTexelFuncExt)( GLint srcRowstride, const GLubyte *pixdata, GLint col, GLint row, GLvoid *texelOut );
61
62 static dxtFetchTexelFuncExt fetch_ext_rgb_dxt1 = NULL;
63 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt1 = NULL;
64 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt3 = NULL;
65 static dxtFetchTexelFuncExt fetch_ext_rgba_dxt5 = NULL;
66
67 typedef void (*dxtCompressTexFuncExt)(GLint srccomps, GLint width,
68 GLint height, const GLubyte *srcPixData,
69 GLenum destformat, GLubyte *dest,
70 GLint dstRowStride);
71
72 static dxtCompressTexFuncExt ext_tx_compress_dxtn = NULL;
73
74 static void *dxtlibhandle = NULL;
75
76
77 void
78 _mesa_init_texture_s3tc( struct gl_context *ctx )
79 {
80 /* called during context initialization */
81 ctx->Mesa_DXTn = GL_FALSE;
82 #if USE_EXTERNAL_DXTN_LIB
83 if (!dxtlibhandle) {
84 dxtlibhandle = _mesa_dlopen(DXTN_LIBNAME, 0);
85 if (!dxtlibhandle) {
86 _mesa_warning(ctx, "couldn't open " DXTN_LIBNAME ", software DXTn "
87 "compression/decompression unavailable");
88 }
89 else {
90 /* the fetch functions are not per context! Might be problematic... */
91 fetch_ext_rgb_dxt1 = (dxtFetchTexelFuncExt)
92 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgb_dxt1");
93 fetch_ext_rgba_dxt1 = (dxtFetchTexelFuncExt)
94 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt1");
95 fetch_ext_rgba_dxt3 = (dxtFetchTexelFuncExt)
96 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt3");
97 fetch_ext_rgba_dxt5 = (dxtFetchTexelFuncExt)
98 _mesa_dlsym(dxtlibhandle, "fetch_2d_texel_rgba_dxt5");
99 ext_tx_compress_dxtn = (dxtCompressTexFuncExt)
100 _mesa_dlsym(dxtlibhandle, "tx_compress_dxtn");
101
102 if (!fetch_ext_rgb_dxt1 ||
103 !fetch_ext_rgba_dxt1 ||
104 !fetch_ext_rgba_dxt3 ||
105 !fetch_ext_rgba_dxt5 ||
106 !ext_tx_compress_dxtn) {
107 _mesa_warning(ctx, "couldn't reference all symbols in "
108 DXTN_LIBNAME ", software DXTn compression/decompression "
109 "unavailable");
110 fetch_ext_rgb_dxt1 = NULL;
111 fetch_ext_rgba_dxt1 = NULL;
112 fetch_ext_rgba_dxt3 = NULL;
113 fetch_ext_rgba_dxt5 = NULL;
114 ext_tx_compress_dxtn = NULL;
115 _mesa_dlclose(dxtlibhandle);
116 dxtlibhandle = NULL;
117 }
118 }
119 }
120 if (dxtlibhandle) {
121 ctx->Mesa_DXTn = GL_TRUE;
122 }
123 #else
124 (void) ctx;
125 #endif
126 }
127
128 /**
129 * Store user's image in rgb_dxt1 format.
130 */
131 GLboolean
132 _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS)
133 {
134 const GLubyte *pixels;
135 GLubyte *dst;
136 const GLubyte *tempImage = NULL;
137
138 ASSERT(dstFormat == MESA_FORMAT_RGB_DXT1 ||
139 dstFormat == MESA_FORMAT_SRGB_DXT1);
140
141 if (srcFormat != GL_RGB ||
142 srcType != GL_UNSIGNED_BYTE ||
143 ctx->_ImageTransferState ||
144 srcPacking->RowLength != srcWidth ||
145 srcPacking->SwapBytes) {
146 /* convert image to RGB/GLubyte */
147 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
148 baseInternalFormat,
149 _mesa_get_format_base_format(dstFormat),
150 srcWidth, srcHeight, srcDepth,
151 srcFormat, srcType, srcAddr,
152 srcPacking);
153 if (!tempImage)
154 return GL_FALSE; /* out of memory */
155 pixels = tempImage;
156 srcFormat = GL_RGB;
157 }
158 else {
159 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
160 srcFormat, srcType, 0, 0);
161 }
162
163 dst = dstSlices[0];
164
165 if (ext_tx_compress_dxtn) {
166 (*ext_tx_compress_dxtn)(3, srcWidth, srcHeight, pixels,
167 GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
168 dst, dstRowStride);
169 }
170 else {
171 _mesa_warning(ctx, "external dxt library not available: texstore_rgb_dxt1");
172 }
173
174 free((void *) tempImage);
175
176 return GL_TRUE;
177 }
178
179
180 /**
181 * Store user's image in rgba_dxt1 format.
182 */
183 GLboolean
184 _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS)
185 {
186 const GLubyte *pixels;
187 GLubyte *dst;
188 const GLubyte *tempImage = NULL;
189
190 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT1 ||
191 dstFormat == MESA_FORMAT_SRGBA_DXT1);
192
193 if (srcFormat != GL_RGBA ||
194 srcType != GL_UNSIGNED_BYTE ||
195 ctx->_ImageTransferState ||
196 srcPacking->RowLength != srcWidth ||
197 srcPacking->SwapBytes) {
198 /* convert image to RGBA/GLubyte */
199 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
200 baseInternalFormat,
201 _mesa_get_format_base_format(dstFormat),
202 srcWidth, srcHeight, srcDepth,
203 srcFormat, srcType, srcAddr,
204 srcPacking);
205 if (!tempImage)
206 return GL_FALSE; /* out of memory */
207 pixels = tempImage;
208 srcFormat = GL_RGBA;
209 }
210 else {
211 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
212 srcFormat, srcType, 0, 0);
213 }
214
215 dst = dstSlices[0];
216
217 if (ext_tx_compress_dxtn) {
218 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
219 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
220 dst, dstRowStride);
221 }
222 else {
223 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt1");
224 }
225
226 free((void*) tempImage);
227
228 return GL_TRUE;
229 }
230
231
232 /**
233 * Store user's image in rgba_dxt3 format.
234 */
235 GLboolean
236 _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS)
237 {
238 const GLubyte *pixels;
239 GLubyte *dst;
240 const GLubyte *tempImage = NULL;
241
242 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT3 ||
243 dstFormat == MESA_FORMAT_SRGBA_DXT3);
244
245 if (srcFormat != GL_RGBA ||
246 srcType != GL_UNSIGNED_BYTE ||
247 ctx->_ImageTransferState ||
248 srcPacking->RowLength != srcWidth ||
249 srcPacking->SwapBytes) {
250 /* convert image to RGBA/GLubyte */
251 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
252 baseInternalFormat,
253 _mesa_get_format_base_format(dstFormat),
254 srcWidth, srcHeight, srcDepth,
255 srcFormat, srcType, srcAddr,
256 srcPacking);
257 if (!tempImage)
258 return GL_FALSE; /* out of memory */
259 pixels = tempImage;
260 }
261 else {
262 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
263 srcFormat, srcType, 0, 0);
264 }
265
266 dst = dstSlices[0];
267
268 if (ext_tx_compress_dxtn) {
269 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
270 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
271 dst, dstRowStride);
272 }
273 else {
274 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt3");
275 }
276
277 free((void *) tempImage);
278
279 return GL_TRUE;
280 }
281
282
283 /**
284 * Store user's image in rgba_dxt5 format.
285 */
286 GLboolean
287 _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS)
288 {
289 const GLubyte *pixels;
290 GLubyte *dst;
291 const GLubyte *tempImage = NULL;
292
293 ASSERT(dstFormat == MESA_FORMAT_RGBA_DXT5 ||
294 dstFormat == MESA_FORMAT_SRGBA_DXT5);
295
296 if (srcFormat != GL_RGBA ||
297 srcType != GL_UNSIGNED_BYTE ||
298 ctx->_ImageTransferState ||
299 srcPacking->RowLength != srcWidth ||
300 srcPacking->SwapBytes) {
301 /* convert image to RGBA/GLubyte */
302 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
303 baseInternalFormat,
304 _mesa_get_format_base_format(dstFormat),
305 srcWidth, srcHeight, srcDepth,
306 srcFormat, srcType, srcAddr,
307 srcPacking);
308 if (!tempImage)
309 return GL_FALSE; /* out of memory */
310 pixels = tempImage;
311 }
312 else {
313 pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
314 srcFormat, srcType, 0, 0);
315 }
316
317 dst = dstSlices[0];
318
319 if (ext_tx_compress_dxtn) {
320 (*ext_tx_compress_dxtn)(4, srcWidth, srcHeight, pixels,
321 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
322 dst, dstRowStride);
323 }
324 else {
325 _mesa_warning(ctx, "external dxt library not available: texstore_rgba_dxt5");
326 }
327
328 free((void *) tempImage);
329
330 return GL_TRUE;
331 }
332
333
334 /** Report problem with dxt texture decompression, once */
335 static void
336 problem(const char *func)
337 {
338 static GLboolean warned = GL_FALSE;
339 if (!warned) {
340 _mesa_debug(NULL, "attempted to decode DXT texture without "
341 "library available: %s\n", func);
342 warned = GL_TRUE;
343 }
344 }
345
346
347 static void
348 fetch_rgb_dxt1(const GLubyte *map, const GLuint imageOffsets[],
349 GLint rowStride, GLint i, GLint j, GLint k, GLfloat *texel)
350 {
351 if (fetch_ext_rgb_dxt1) {
352 GLuint sliceOffset = k ? imageOffsets[k] / 2 : 0;
353 GLubyte tex[4];
354 fetch_ext_rgb_dxt1(rowStride, map + sliceOffset, i, j, tex);
355 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
356 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
357 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
358 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
359 }
360 else {
361 problem("rgb_dxt1");
362 }
363 }
364
365 static void
366 fetch_rgba_dxt1(const GLubyte *map, const GLuint imageOffsets[],
367 GLint rowStride, GLint i, GLint j, GLint k, GLfloat *texel)
368 {
369 if (fetch_ext_rgba_dxt1) {
370 GLuint sliceOffset = k ? imageOffsets[k] / 2 : 0;
371 GLubyte tex[4];
372 fetch_ext_rgba_dxt1(rowStride, map + sliceOffset, i, j, tex);
373 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
374 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
375 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
376 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
377 }
378 else {
379 problem("rgba_dxt1");
380 }
381 }
382
383 static void
384 fetch_rgba_dxt3(const GLubyte *map, const GLuint imageOffsets[],
385 GLint rowStride, GLint i, GLint j, GLint k, GLfloat *texel)
386 {
387 if (fetch_ext_rgba_dxt3) {
388 GLuint sliceOffset = k ? imageOffsets[k] : 0;
389 GLubyte tex[4];
390 fetch_ext_rgba_dxt3(rowStride, map + sliceOffset, i, j, tex);
391 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
392 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
393 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
394 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
395 }
396 else {
397 problem("rgba_dxt3");
398 }
399 }
400
401 static void
402 fetch_rgba_dxt5(const GLubyte *map, const GLuint imageOffsets[],
403 GLint rowStride, GLint i, GLint j, GLint k, GLfloat *texel)
404 {
405 if (fetch_ext_rgba_dxt5) {
406 GLuint sliceOffset = k ? imageOffsets[k] : 0;
407 GLubyte tex[4];
408 fetch_ext_rgba_dxt5(rowStride, map + sliceOffset, i, j, tex);
409 texel[RCOMP] = UBYTE_TO_FLOAT(tex[RCOMP]);
410 texel[GCOMP] = UBYTE_TO_FLOAT(tex[GCOMP]);
411 texel[BCOMP] = UBYTE_TO_FLOAT(tex[BCOMP]);
412 texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
413 }
414 else {
415 problem("rgba_dxt5");
416 }
417 }
418
419
420 compressed_fetch_func
421 _mesa_get_dxt_fetch_func(gl_format format)
422 {
423 switch (format) {
424 case MESA_FORMAT_RGB_DXT1:
425 return fetch_rgb_dxt1;
426 case MESA_FORMAT_RGBA_DXT1:
427 return fetch_rgba_dxt1;
428 case MESA_FORMAT_RGBA_DXT3:
429 return fetch_rgba_dxt3;
430 case MESA_FORMAT_RGBA_DXT5:
431 return fetch_rgba_dxt5;
432 default:
433 return NULL;
434 }
435 }