fix an error string, refactor _mesa_GetCompressedTexImageARB() to get rid of a goto
[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 *
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file texcompress.c
28 * Helper functions for texture compression.
29 */
30
31
32 #include "glheader.h"
33 #include "imports.h"
34 #include "colormac.h"
35 #include "context.h"
36 #include "image.h"
37 #include "mipmap.h"
38 #include "texcompress.h"
39 #include "texformat.h"
40 #include "texstore.h"
41
42
43 /**
44 * Return list of (and count of) all specific texture compression
45 * formats that are supported.
46 *
47 * \param ctx the GL context
48 * \param formats the resulting format list (may be NULL).
49 * \param all if true return all formats, even those with some kind
50 * of restrictions/limitations (See GL_ARB_texture_compression
51 * spec for more info).
52 *
53 * \return number of formats.
54 */
55 GLuint
56 _mesa_get_compressed_formats(GLcontext *ctx, GLint *formats, GLboolean all)
57 {
58 GLuint n = 0;
59 if (ctx->Extensions.ARB_texture_compression) {
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 }
113 return n;
114 }
115
116
117
118 /**
119 * Return number of bytes needed to store a texture of the given size
120 * using the specified compressed format.
121 * This is called via the ctx->Driver.CompressedTextureSize function,
122 * unless a device driver overrides it.
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 GLuint size;
137
138 ASSERT(depth == 1);
139 (void) depth;
140
141 switch (mesaFormat) {
142 case MESA_FORMAT_RGB_FXT1:
143 case MESA_FORMAT_RGBA_FXT1:
144 /* round up width to next multiple of 8, height to next multiple of 4 */
145 width = (width + 7) & ~7;
146 height = (height + 3) & ~3;
147 /* 16 bytes per 8x4 tile of RGB[A] texels */
148 size = width * height / 2;
149 /* Textures smaller than 8x4 will effectively be made into 8x4 and
150 * take 16 bytes.
151 */
152 if (size < 16)
153 size = 16;
154 return size;
155 case MESA_FORMAT_RGB_DXT1:
156 case MESA_FORMAT_RGBA_DXT1:
157 /* round up width, height to next multiple of 4 */
158 width = (width + 3) & ~3;
159 height = (height + 3) & ~3;
160 /* 8 bytes per 4x4 tile of RGB[A] texels */
161 size = width * height / 2;
162 /* Textures smaller than 4x4 will effectively be made into 4x4 and
163 * take 8 bytes.
164 */
165 if (size < 8)
166 size = 8;
167 return size;
168 case MESA_FORMAT_RGBA_DXT3:
169 case MESA_FORMAT_RGBA_DXT5:
170 /* round up width, height to next multiple of 4 */
171 width = (width + 3) & ~3;
172 height = (height + 3) & ~3;
173 /* 16 bytes per 4x4 tile of RGBA texels */
174 size = width * height; /* simple! */
175 /* Textures smaller than 4x4 will effectively be made into 4x4 and
176 * take 16 bytes.
177 */
178 if (size < 16)
179 size = 16;
180 return size;
181 default:
182 _mesa_problem(ctx, "bad mesaFormat in _mesa_compressed_texture_size");
183 return 0;
184 }
185 }
186
187
188 /**
189 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
190 *
191 * Note: This function CAN NOT return a padded hardware texture size.
192 * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
193 *
194 * We use this function to validate the <imageSize> parameter
195 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
196 */
197 GLuint
198 _mesa_compressed_texture_size_glenum(GLcontext *ctx,
199 GLsizei width, GLsizei height,
200 GLsizei depth, GLenum glformat)
201 {
202 GLuint mesaFormat;
203
204 switch (glformat) {
205 case GL_COMPRESSED_RGB_FXT1_3DFX:
206 mesaFormat = MESA_FORMAT_RGB_FXT1;
207 break;
208 case GL_COMPRESSED_RGBA_FXT1_3DFX:
209 mesaFormat = MESA_FORMAT_RGBA_FXT1;
210 break;
211 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
212 case GL_RGB_S3TC:
213 mesaFormat = MESA_FORMAT_RGB_DXT1;
214 break;
215 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
216 case GL_RGB4_S3TC:
217 mesaFormat = MESA_FORMAT_RGBA_DXT1;
218 break;
219 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
220 case GL_RGBA_S3TC:
221 mesaFormat = MESA_FORMAT_RGBA_DXT3;
222 break;
223 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
224 case GL_RGBA4_S3TC:
225 mesaFormat = MESA_FORMAT_RGBA_DXT5;
226 break;
227 default:
228 return 0;
229 }
230
231 return _mesa_compressed_texture_size(ctx, width, height, depth, mesaFormat);
232 }
233
234
235 /*
236 * Compute the bytes per row in a compressed texture image.
237 * We use this for computing the destination address for sub-texture updates.
238 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
239 * \param width image width in pixels
240 * \return stride, in bytes, between rows for compressed image
241 */
242 GLint
243 _mesa_compressed_row_stride(GLuint mesaFormat, GLsizei width)
244 {
245 GLint stride;
246
247 switch (mesaFormat) {
248 case MESA_FORMAT_RGB_FXT1:
249 case MESA_FORMAT_RGBA_FXT1:
250 stride = ((width + 7) / 8) * 16; /* 16 bytes per 8x4 tile */
251 break;
252 case MESA_FORMAT_RGB_DXT1:
253 case MESA_FORMAT_RGBA_DXT1:
254 stride = ((width + 3) / 4) * 8; /* 8 bytes per 4x4 tile */
255 break;
256 case MESA_FORMAT_RGBA_DXT3:
257 case MESA_FORMAT_RGBA_DXT5:
258 stride = ((width + 3) / 4) * 16; /* 16 bytes per 4x4 tile */
259 break;
260 default:
261 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_row_stride");
262 return 0;
263 }
264
265 return stride;
266 }
267
268
269 /*
270 * Return the address of the pixel at (col, row, img) in a
271 * compressed texture image.
272 * \param col, row, img - image position (3D)
273 * \param format - compressed image format
274 * \param width - image width
275 * \param image - the image address
276 * \return address of pixel at (row, col)
277 */
278 GLubyte *
279 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
280 GLuint mesaFormat,
281 GLsizei width, const GLubyte *image)
282 {
283 GLubyte *addr;
284
285 (void) img;
286
287 /* We try to spot a "complete" subtexture "above" ROW, COL;
288 * this texture is given by appropriate rounding of WIDTH x ROW.
289 * Then we just add the amount left (usually on the left).
290 *
291 * Example for X*Y microtiles (Z bytes each)
292 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
293 */
294
295 switch (mesaFormat) {
296 case MESA_FORMAT_RGB_FXT1:
297 case MESA_FORMAT_RGBA_FXT1:
298 addr = (GLubyte *) image + 16 * (((width + 7) / 8) * (row / 4) + col / 8);
299 break;
300 case MESA_FORMAT_RGB_DXT1:
301 case MESA_FORMAT_RGBA_DXT1:
302 addr = (GLubyte *) image + 8 * (((width + 3) / 4) * (row / 4) + col / 4);
303 break;
304 case MESA_FORMAT_RGBA_DXT3:
305 case MESA_FORMAT_RGBA_DXT5:
306 addr = (GLubyte *) image + 16 * (((width + 3) / 4) * (row / 4) + col / 4);
307 break;
308 default:
309 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_image_address");
310 addr = NULL;
311 }
312
313 return addr;
314 }