mesa: don't include texformat.h
[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 /**
118  * Return number of bytes needed to store a texture of the given size
119  * using the specified (compressed?) format.
120  * This is called via the ctx->Driver.CompressedTextureSize function,
121  * unless a device driver overrides it.  A driver might override this
122  * if it needs to use an unusual or padded texture memory layout.
123  *
124  * \param width texture width in texels.
125  * \param height texture height in texels.
126  * \param depth texture depth in texels.
127  * \param mesaFormat  one of the MESA_FORMAT_* compressed formats
128  *
129  * \return size in bytes, or zero if bad format
130  */
131 GLuint
132 _mesa_compressed_texture_size( GLcontext *ctx,
133                                GLsizei width, GLsizei height, GLsizei depth,
134                                GLuint mesaFormat )
135 {
136    return _mesa_format_image_size(mesaFormat, width, height, depth);
137 }
138
139
140 /**
141  * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
142  *
143  * Note: This function CAN NOT return a padded hardware texture size.
144  * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
145  *
146  * We use this function to validate the <imageSize> parameter
147  * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
148  */
149 GLuint
150 _mesa_compressed_texture_size_glenum(GLcontext *ctx,
151                                      GLsizei width, GLsizei height,
152                                      GLsizei depth, GLenum glformat)
153 {
154    gl_format mesaFormat;
155
156    switch (glformat) {
157 #if FEATURE_texture_fxt1
158    case GL_COMPRESSED_RGB_FXT1_3DFX:
159       mesaFormat = MESA_FORMAT_RGB_FXT1;
160       break;
161    case GL_COMPRESSED_RGBA_FXT1_3DFX:
162       mesaFormat = MESA_FORMAT_RGBA_FXT1;
163       break;
164 #endif
165 #if FEATURE_texture_s3tc
166    case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
167    case GL_RGB_S3TC:
168       mesaFormat = MESA_FORMAT_RGB_DXT1;
169       break;
170    case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
171    case GL_RGB4_S3TC:
172       mesaFormat = MESA_FORMAT_RGBA_DXT1;
173       break;
174    case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
175    case GL_RGBA_S3TC:
176       mesaFormat = MESA_FORMAT_RGBA_DXT3;
177       break;
178    case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
179    case GL_RGBA4_S3TC:
180       mesaFormat = MESA_FORMAT_RGBA_DXT5;
181       break;
182 #if FEATURE_EXT_texture_sRGB
183    case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
184       mesaFormat = MESA_FORMAT_SRGB_DXT1;
185       break;
186    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
187       mesaFormat = MESA_FORMAT_SRGBA_DXT1;
188       break;
189    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
190       mesaFormat = MESA_FORMAT_SRGBA_DXT3;
191       break;
192    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
193       mesaFormat = MESA_FORMAT_SRGBA_DXT5;
194       break;
195 #endif
196 #endif
197    default:
198       return 0;
199    }
200
201    return _mesa_format_image_size(mesaFormat, width, height, depth);
202 }
203
204
205 /*
206  * Compute the bytes per row in a compressed texture image.
207  * We use this for computing the destination address for sub-texture updates.
208  * \param mesaFormat  one of the MESA_FORMAT_* compressed formats
209  * \param width  image width in pixels
210  * \return stride, in bytes, between rows for compressed image
211  */
212 GLint
213 _mesa_compressed_row_stride(GLuint mesaFormat, GLsizei width)
214 {
215    GLint stride;
216
217    switch (mesaFormat) {
218 #if FEATURE_texture_fxt1
219    case MESA_FORMAT_RGB_FXT1:
220    case MESA_FORMAT_RGBA_FXT1:
221       stride = ((width + 7) / 8) * 16; /* 16 bytes per 8x4 tile */
222       break;
223 #endif
224 #if FEATURE_texture_s3tc
225    case MESA_FORMAT_RGB_DXT1:
226    case MESA_FORMAT_RGBA_DXT1:
227 #if FEATURE_EXT_texture_sRGB
228    case MESA_FORMAT_SRGB_DXT1:
229    case MESA_FORMAT_SRGBA_DXT1:
230 #endif
231       stride = ((width + 3) / 4) * 8; /* 8 bytes per 4x4 tile */
232       break;
233    case MESA_FORMAT_RGBA_DXT3:
234    case MESA_FORMAT_RGBA_DXT5:
235 #if FEATURE_EXT_texture_sRGB
236    case MESA_FORMAT_SRGBA_DXT3:
237    case MESA_FORMAT_SRGBA_DXT5:
238 #endif
239       stride = ((width + 3) / 4) * 16; /* 16 bytes per 4x4 tile */
240       break;
241 #endif
242    default:
243       _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_row_stride");
244       return 0;
245    }
246
247    assert(stride == _mesa_format_row_stride(mesaFormat, width));
248
249    return stride;
250 }
251
252
253 /*
254  * Return the address of the pixel at (col, row, img) in a
255  * compressed texture image.
256  * \param col, row, img - image position (3D)
257  * \param format - compressed image format
258  * \param width - image width
259  * \param image - the image address
260  * \return address of pixel at (row, col)
261  */
262 GLubyte *
263 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
264                                GLuint mesaFormat,
265                                GLsizei width, const GLubyte *image)
266 {
267    GLubyte *addr;
268
269    (void) img;
270
271    /* We try to spot a "complete" subtexture "above" ROW, COL;
272     * this texture is given by appropriate rounding of WIDTH x ROW.
273     * Then we just add the amount left (usually on the left).
274     *
275     * Example for X*Y microtiles (Z bytes each)
276     * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
277     */
278
279    switch (mesaFormat) {
280 #if FEATURE_texture_fxt1
281    case MESA_FORMAT_RGB_FXT1:
282    case MESA_FORMAT_RGBA_FXT1:
283       addr = (GLubyte *) image + 16 * (((width + 7) / 8) * (row / 4) + col / 8);
284       break;
285 #endif
286 #if FEATURE_texture_s3tc
287    case MESA_FORMAT_RGB_DXT1:
288    case MESA_FORMAT_RGBA_DXT1:
289 #if FEATURE_EXT_texture_sRGB
290    case MESA_FORMAT_SRGB_DXT1:
291    case MESA_FORMAT_SRGBA_DXT1:
292 #endif
293       addr = (GLubyte *) image + 8 * (((width + 3) / 4) * (row / 4) + col / 4);
294       break;
295    case MESA_FORMAT_RGBA_DXT3:
296    case MESA_FORMAT_RGBA_DXT5:
297 #if FEATURE_EXT_texture_sRGB
298    case MESA_FORMAT_SRGBA_DXT3:
299    case MESA_FORMAT_SRGBA_DXT5:
300 #endif
301       addr = (GLubyte *) image + 16 * (((width + 3) / 4) * (row / 4) + col / 4);
302       break;
303 #endif
304    default:
305       _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_image_address");
306       addr = NULL;
307    }
308
309    return addr;
310 }