Merge commit 'origin/master' into gallium-0.2
[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 "image.h"
38 #include "mipmap.h"
39 #include "texcompress.h"
40 #include "texformat.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.ARB_texture_compression) {
61 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
62 if (formats) {
63 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
64 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
65 }
66 else {
67 n += 2;
68 }
69 }
70 if (ctx->Extensions.EXT_texture_compression_s3tc) {
71 if (formats) {
72 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
73 /* This format has some restrictions/limitations and so should
74 * not be returned via the GL_COMPRESSED_TEXTURE_FORMATS query.
75 * Specifically, all transparent pixels become black. NVIDIA
76 * omits this format too.
77 */
78 if (all)
79 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
80 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
81 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
82 }
83 else {
84 n += 3;
85 if (all)
86 n += 1;
87 }
88 #if FEATURE_EXT_texture_sRGB
89 if (ctx->Extensions.EXT_texture_sRGB) {
90 if (formats) {
91 if (all) {
92 /* according to sRGB spec, these should not be returned
93 via the GL_COMPRESSED_TEXTURE_FORMATS query as they
94 aren't really general purpose */
95 formats[n++] = GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
96 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
97 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
98 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
99 }
100 }
101 else {
102 if (all)
103 n += 4;
104 }
105 }
106 #endif /* FEATURE_EXT_texture_sRGB */
107 }
108 if (ctx->Extensions.S3_s3tc) {
109 if (formats) {
110 formats[n++] = GL_RGB_S3TC;
111 formats[n++] = GL_RGB4_S3TC;
112 formats[n++] = GL_RGBA_S3TC;
113 formats[n++] = GL_RGBA4_S3TC;
114 }
115 else {
116 n += 4;
117 }
118 }
119 }
120 return n;
121 }
122
123
124
125 /**
126 * Return number of bytes needed to store a texture of the given size
127 * using the specified compressed format.
128 * This is called via the ctx->Driver.CompressedTextureSize function,
129 * unless a device driver overrides it.
130 *
131 * \param width texture width in texels.
132 * \param height texture height in texels.
133 * \param depth texture depth in texels.
134 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
135 *
136 * \return size in bytes, or zero if bad format
137 */
138 GLuint
139 _mesa_compressed_texture_size( GLcontext *ctx,
140 GLsizei width, GLsizei height, GLsizei depth,
141 GLuint mesaFormat )
142 {
143 GLuint size;
144
145 ASSERT(depth == 1);
146 (void) depth;
147 (void) size;
148
149 switch (mesaFormat) {
150 #if FEATURE_texture_fxt1
151 case MESA_FORMAT_RGB_FXT1:
152 case MESA_FORMAT_RGBA_FXT1:
153 /* round up width to next multiple of 8, height to next multiple of 4 */
154 width = (width + 7) & ~7;
155 height = (height + 3) & ~3;
156 /* 16 bytes per 8x4 tile of RGB[A] texels */
157 size = width * height / 2;
158 /* Textures smaller than 8x4 will effectively be made into 8x4 and
159 * take 16 bytes.
160 */
161 return size;
162 #endif
163 #if FEATURE_texture_s3tc
164 case MESA_FORMAT_RGB_DXT1:
165 case MESA_FORMAT_RGBA_DXT1:
166 #if FEATURE_EXT_texture_sRGB
167 case MESA_FORMAT_SRGB_DXT1:
168 case MESA_FORMAT_SRGBA_DXT1:
169 #endif
170 /* round up width, height to next multiple of 4 */
171 width = (width + 3) & ~3;
172 height = (height + 3) & ~3;
173 /* 8 bytes per 4x4 tile of RGB[A] texels */
174 size = width * height / 2;
175 /* Textures smaller than 4x4 will effectively be made into 4x4 and
176 * take 8 bytes.
177 */
178 return size;
179 case MESA_FORMAT_RGBA_DXT3:
180 case MESA_FORMAT_RGBA_DXT5:
181 #if FEATURE_EXT_texture_sRGB
182 case MESA_FORMAT_SRGBA_DXT3:
183 case MESA_FORMAT_SRGBA_DXT5:
184 #endif
185 /* round up width, height to next multiple of 4 */
186 width = (width + 3) & ~3;
187 height = (height + 3) & ~3;
188 /* 16 bytes per 4x4 tile of RGBA texels */
189 size = width * height; /* simple! */
190 /* Textures smaller than 4x4 will effectively be made into 4x4 and
191 * take 16 bytes.
192 */
193 return size;
194 #endif
195 default:
196 _mesa_problem(ctx, "bad mesaFormat in _mesa_compressed_texture_size");
197 return 0;
198 }
199 }
200
201
202 /**
203 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
204 *
205 * Note: This function CAN NOT return a padded hardware texture size.
206 * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
207 *
208 * We use this function to validate the <imageSize> parameter
209 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
210 */
211 GLuint
212 _mesa_compressed_texture_size_glenum(GLcontext *ctx,
213 GLsizei width, GLsizei height,
214 GLsizei depth, GLenum glformat)
215 {
216 GLuint mesaFormat;
217
218 switch (glformat) {
219 #if FEATURE_texture_fxt1
220 case GL_COMPRESSED_RGB_FXT1_3DFX:
221 mesaFormat = MESA_FORMAT_RGB_FXT1;
222 break;
223 case GL_COMPRESSED_RGBA_FXT1_3DFX:
224 mesaFormat = MESA_FORMAT_RGBA_FXT1;
225 break;
226 #endif
227 #if FEATURE_texture_s3tc
228 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
229 case GL_RGB_S3TC:
230 mesaFormat = MESA_FORMAT_RGB_DXT1;
231 break;
232 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
233 case GL_RGB4_S3TC:
234 mesaFormat = MESA_FORMAT_RGBA_DXT1;
235 break;
236 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
237 case GL_RGBA_S3TC:
238 mesaFormat = MESA_FORMAT_RGBA_DXT3;
239 break;
240 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
241 case GL_RGBA4_S3TC:
242 mesaFormat = MESA_FORMAT_RGBA_DXT5;
243 break;
244 #if FEATURE_EXT_texture_sRGB
245 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
246 mesaFormat = MESA_FORMAT_SRGB_DXT1;
247 break;
248 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
249 mesaFormat = MESA_FORMAT_SRGBA_DXT1;
250 break;
251 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
252 mesaFormat = MESA_FORMAT_SRGBA_DXT3;
253 break;
254 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
255 mesaFormat = MESA_FORMAT_SRGBA_DXT5;
256 break;
257 #endif
258 #endif
259 default:
260 return 0;
261 }
262
263 return _mesa_compressed_texture_size(ctx, width, height, depth, mesaFormat);
264 }
265
266
267 /*
268 * Compute the bytes per row in a compressed texture image.
269 * We use this for computing the destination address for sub-texture updates.
270 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
271 * \param width image width in pixels
272 * \return stride, in bytes, between rows for compressed image
273 */
274 GLint
275 _mesa_compressed_row_stride(GLuint mesaFormat, GLsizei width)
276 {
277 GLint stride;
278
279 switch (mesaFormat) {
280 #if FEATURE_texture_fxt1
281 case MESA_FORMAT_RGB_FXT1:
282 case MESA_FORMAT_RGBA_FXT1:
283 stride = ((width + 7) / 8) * 16; /* 16 bytes per 8x4 tile */
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 stride = ((width + 3) / 4) * 8; /* 8 bytes per 4x4 tile */
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 stride = ((width + 3) / 4) * 16; /* 16 bytes per 4x4 tile */
302 break;
303 #endif
304 default:
305 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_row_stride");
306 return 0;
307 }
308
309 return stride;
310 }
311
312
313 /*
314 * Return the address of the pixel at (col, row, img) in a
315 * compressed texture image.
316 * \param col, row, img - image position (3D)
317 * \param format - compressed image format
318 * \param width - image width
319 * \param image - the image address
320 * \return address of pixel at (row, col)
321 */
322 GLubyte *
323 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
324 GLuint mesaFormat,
325 GLsizei width, const GLubyte *image)
326 {
327 GLubyte *addr;
328
329 (void) img;
330
331 /* We try to spot a "complete" subtexture "above" ROW, COL;
332 * this texture is given by appropriate rounding of WIDTH x ROW.
333 * Then we just add the amount left (usually on the left).
334 *
335 * Example for X*Y microtiles (Z bytes each)
336 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
337 */
338
339 switch (mesaFormat) {
340 #if FEATURE_texture_fxt1
341 case MESA_FORMAT_RGB_FXT1:
342 case MESA_FORMAT_RGBA_FXT1:
343 addr = (GLubyte *) image + 16 * (((width + 7) / 8) * (row / 4) + col / 8);
344 break;
345 #endif
346 #if FEATURE_texture_s3tc
347 case MESA_FORMAT_RGB_DXT1:
348 case MESA_FORMAT_RGBA_DXT1:
349 #if FEATURE_EXT_texture_sRGB
350 case MESA_FORMAT_SRGB_DXT1:
351 case MESA_FORMAT_SRGBA_DXT1:
352 #endif
353 addr = (GLubyte *) image + 8 * (((width + 3) / 4) * (row / 4) + col / 4);
354 break;
355 case MESA_FORMAT_RGBA_DXT3:
356 case MESA_FORMAT_RGBA_DXT5:
357 #if FEATURE_EXT_texture_sRGB
358 case MESA_FORMAT_SRGBA_DXT3:
359 case MESA_FORMAT_SRGBA_DXT5:
360 #endif
361 addr = (GLubyte *) image + 16 * (((width + 3) / 4) * (row / 4) + col / 4);
362 break;
363 #endif
364 default:
365 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_image_address");
366 addr = NULL;
367 }
368
369 return addr;
370 }