Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[profile/ivi/mesa.git] / src / mesa / main / texcompress.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  6.5.1
4  *
5  * Copyright (C) 1999-2006  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.c
29  * Helper functions for texture compression.
30  */
31
32
33 #include "glheader.h"
34 #include "imports.h"
35 #include "colormac.h"
36 #include "context.h"
37 #include "formats.h"
38 #include "image.h"
39 #include "mipmap.h"
40 #include "texcompress.h"
41 #include "texstore.h"
42
43
44 /**
45  * Return list of (and count of) all specific texture compression
46  * formats that are supported.
47  *
48  * \param ctx  the GL context
49  * \param formats  the resulting format list (may be NULL).
50  * \param all  if true return all formats, even those with  some kind
51  *             of restrictions/limitations (See GL_ARB_texture_compression
52  *             spec for more info).
53  *
54  * \return number of formats.
55  */
56 GLuint
57 _mesa_get_compressed_formats(GLcontext *ctx, GLint *formats, GLboolean all)
58 {
59    GLuint n = 0;
60    if (ctx->Extensions.TDFX_texture_compression_FXT1) {
61       if (formats) {
62          formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
63          formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
64       }
65       else {
66          n += 2;
67       }
68    }
69    if (ctx->Extensions.EXT_texture_compression_s3tc) {
70       if (formats) {
71          formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
72          /* This format has some restrictions/limitations and so should
73           * not be returned via the GL_COMPRESSED_TEXTURE_FORMATS query.
74           * Specifically, all transparent pixels become black.  NVIDIA
75           * omits this format too.
76           */
77          if (all)
78              formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
79          formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
80          formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
81       }
82       else {
83          n += 3;
84          if (all)
85              n += 1;
86       }
87    }
88    if (ctx->Extensions.S3_s3tc) {
89       if (formats) {
90          formats[n++] = GL_RGB_S3TC;
91          formats[n++] = GL_RGB4_S3TC;
92          formats[n++] = GL_RGBA_S3TC;
93          formats[n++] = GL_RGBA4_S3TC;
94       }
95       else {
96          n += 4;
97       }
98    }
99 #if FEATURE_EXT_texture_sRGB
100    if (ctx->Extensions.EXT_texture_sRGB) {
101       if (formats) {
102          formats[n++] = GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
103          formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
104          formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
105          formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
106       }
107       else {
108          n += 4;
109       }
110    }
111 #endif /* FEATURE_EXT_texture_sRGB */
112    return n;
113 }
114
115
116 /**
117  * Convert a compressed MESA_FORMAT_x to a GLenum.
118  */
119 gl_format
120 _mesa_glenum_to_compressed_format(GLenum format)
121 {
122    switch (format) {
123    case GL_COMPRESSED_RGB_FXT1_3DFX:
124       return MESA_FORMAT_RGB_FXT1;
125    case GL_COMPRESSED_RGBA_FXT1_3DFX:
126       return MESA_FORMAT_RGBA_FXT1;
127
128    case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
129    case GL_RGB_S3TC:
130       return MESA_FORMAT_RGB_DXT1;
131    case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
132    case GL_RGB4_S3TC:
133       return MESA_FORMAT_RGBA_DXT1;
134    case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
135    case GL_RGBA_S3TC:
136       return MESA_FORMAT_RGBA_DXT3;
137    case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
138    case GL_RGBA4_S3TC:
139       return MESA_FORMAT_RGBA_DXT5;
140
141    case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
142       return MESA_FORMAT_SRGB_DXT1;
143    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
144       return MESA_FORMAT_SRGBA_DXT1;
145    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
146       return MESA_FORMAT_SRGBA_DXT3;
147    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
148       return MESA_FORMAT_SRGBA_DXT5;
149
150    default:
151       return MESA_FORMAT_NONE;
152    }
153 }
154
155
156 /**
157  * Given a compressed MESA_FORMAT_x value, return the corresponding
158  * GLenum for that format.
159  * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
160  * which must return the specific texture format used when the user might
161  * have originally specified a generic compressed format in their
162  * glTexImage2D() call.
163  * For non-compressed textures, we always return the user-specified
164  * internal format unchanged.
165  */
166 GLenum
167 _mesa_compressed_format_to_glenum(GLcontext *ctx, GLuint mesaFormat)
168 {
169    switch (mesaFormat) {
170 #if FEATURE_texture_fxt1
171    case MESA_FORMAT_RGB_FXT1:
172       return GL_COMPRESSED_RGB_FXT1_3DFX;
173    case MESA_FORMAT_RGBA_FXT1:
174       return GL_COMPRESSED_RGBA_FXT1_3DFX;
175 #endif
176 #if FEATURE_texture_s3tc
177    case MESA_FORMAT_RGB_DXT1:
178       return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
179    case MESA_FORMAT_RGBA_DXT1:
180       return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
181    case MESA_FORMAT_RGBA_DXT3:
182       return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
183    case MESA_FORMAT_RGBA_DXT5:
184       return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
185 #if FEATURE_EXT_texture_sRGB
186    case MESA_FORMAT_SRGB_DXT1:
187       return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
188    case MESA_FORMAT_SRGBA_DXT1:
189       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
190    case MESA_FORMAT_SRGBA_DXT3:
191       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
192    case MESA_FORMAT_SRGBA_DXT5:
193       return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
194 #endif
195 #endif
196    default:
197       _mesa_problem(ctx, "Unexpected mesa texture format in"
198                     " _mesa_compressed_format_to_glenum()");
199       return 0;
200    }
201 }
202
203
204 /*
205  * Return the address of the pixel at (col, row, img) in a
206  * compressed texture image.
207  * \param col, row, img - image position (3D), should be a multiple of the
208  *                        format's block size.
209  * \param format - compressed image format
210  * \param width - image width (stride) in pixels
211  * \param image - the image address
212  * \return address of pixel at (row, col, img)
213  */
214 GLubyte *
215 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
216                                gl_format mesaFormat,
217                                GLsizei width, const GLubyte *image)
218 {
219    /* XXX only 2D images implemented, not 3D */
220    const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
221    GLuint bw, bh;
222    GLint offset;
223
224    _mesa_get_format_block_size(mesaFormat, &bw, &bh);
225
226    ASSERT(col % bw == 0);
227    ASSERT(row % bh == 0);
228
229    offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
230    offset *= blockSize;
231
232    return (GLubyte *) image + offset;
233 }