Merge branch 'mesa_7_5_branch'
[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.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.
122 *
123 * \param width texture width in texels.
124 * \param height texture height in texels.
125 * \param depth texture depth in texels.
126 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
127 *
128 * \return size in bytes, or zero if bad format
129 */
130 GLuint
131 _mesa_compressed_texture_size( GLcontext *ctx,
132 GLsizei width, GLsizei height, GLsizei depth,
133 GLuint mesaFormat )
134 {
135 GLuint size;
136
137 ASSERT(depth == 1);
138 (void) depth;
139 (void) size;
140
141 switch (mesaFormat) {
142 #if FEATURE_texture_fxt1
143 case MESA_FORMAT_RGB_FXT1:
144 case MESA_FORMAT_RGBA_FXT1:
145 /* round up width to next multiple of 8, height to next multiple of 4 */
146 width = (width + 7) & ~7;
147 height = (height + 3) & ~3;
148 /* 16 bytes per 8x4 tile of RGB[A] texels */
149 size = width * height / 2;
150 /* Textures smaller than 8x4 will effectively be made into 8x4 and
151 * take 16 bytes.
152 */
153 return size;
154 #endif
155 #if FEATURE_texture_s3tc
156 case MESA_FORMAT_RGB_DXT1:
157 case MESA_FORMAT_RGBA_DXT1:
158 #if FEATURE_EXT_texture_sRGB
159 case MESA_FORMAT_SRGB_DXT1:
160 case MESA_FORMAT_SRGBA_DXT1:
161 #endif
162 /* round up width, height to next multiple of 4 */
163 width = (width + 3) & ~3;
164 height = (height + 3) & ~3;
165 /* 8 bytes per 4x4 tile of RGB[A] texels */
166 size = width * height / 2;
167 /* Textures smaller than 4x4 will effectively be made into 4x4 and
168 * take 8 bytes.
169 */
170 return size;
171 case MESA_FORMAT_RGBA_DXT3:
172 case MESA_FORMAT_RGBA_DXT5:
173 #if FEATURE_EXT_texture_sRGB
174 case MESA_FORMAT_SRGBA_DXT3:
175 case MESA_FORMAT_SRGBA_DXT5:
176 #endif
177 /* round up width, height to next multiple of 4 */
178 width = (width + 3) & ~3;
179 height = (height + 3) & ~3;
180 /* 16 bytes per 4x4 tile of RGBA texels */
181 size = width * height; /* simple! */
182 /* Textures smaller than 4x4 will effectively be made into 4x4 and
183 * take 16 bytes.
184 */
185 return size;
186 #endif
187 default:
188 _mesa_problem(ctx, "bad mesaFormat in _mesa_compressed_texture_size");
189 return 0;
190 }
191 }
192
193
194 /**
195 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
196 *
197 * Note: This function CAN NOT return a padded hardware texture size.
198 * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
199 *
200 * We use this function to validate the <imageSize> parameter
201 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
202 */
203 GLuint
204 _mesa_compressed_texture_size_glenum(GLcontext *ctx,
205 GLsizei width, GLsizei height,
206 GLsizei depth, GLenum glformat)
207 {
208 GLuint mesaFormat;
209
210 switch (glformat) {
211 #if FEATURE_texture_fxt1
212 case GL_COMPRESSED_RGB_FXT1_3DFX:
213 mesaFormat = MESA_FORMAT_RGB_FXT1;
214 break;
215 case GL_COMPRESSED_RGBA_FXT1_3DFX:
216 mesaFormat = MESA_FORMAT_RGBA_FXT1;
217 break;
218 #endif
219 #if FEATURE_texture_s3tc
220 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
221 case GL_RGB_S3TC:
222 mesaFormat = MESA_FORMAT_RGB_DXT1;
223 break;
224 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
225 case GL_RGB4_S3TC:
226 mesaFormat = MESA_FORMAT_RGBA_DXT1;
227 break;
228 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
229 case GL_RGBA_S3TC:
230 mesaFormat = MESA_FORMAT_RGBA_DXT3;
231 break;
232 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
233 case GL_RGBA4_S3TC:
234 mesaFormat = MESA_FORMAT_RGBA_DXT5;
235 break;
236 #if FEATURE_EXT_texture_sRGB
237 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
238 mesaFormat = MESA_FORMAT_SRGB_DXT1;
239 break;
240 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
241 mesaFormat = MESA_FORMAT_SRGBA_DXT1;
242 break;
243 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
244 mesaFormat = MESA_FORMAT_SRGBA_DXT3;
245 break;
246 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
247 mesaFormat = MESA_FORMAT_SRGBA_DXT5;
248 break;
249 #endif
250 #endif
251 default:
252 return 0;
253 }
254
255 return _mesa_compressed_texture_size(ctx, width, height, depth, mesaFormat);
256 }
257
258
259 /*
260 * Compute the bytes per row in a compressed texture image.
261 * We use this for computing the destination address for sub-texture updates.
262 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
263 * \param width image width in pixels
264 * \return stride, in bytes, between rows for compressed image
265 */
266 GLint
267 _mesa_compressed_row_stride(GLuint mesaFormat, GLsizei width)
268 {
269 GLint stride;
270
271 switch (mesaFormat) {
272 #if FEATURE_texture_fxt1
273 case MESA_FORMAT_RGB_FXT1:
274 case MESA_FORMAT_RGBA_FXT1:
275 stride = ((width + 7) / 8) * 16; /* 16 bytes per 8x4 tile */
276 break;
277 #endif
278 #if FEATURE_texture_s3tc
279 case MESA_FORMAT_RGB_DXT1:
280 case MESA_FORMAT_RGBA_DXT1:
281 #if FEATURE_EXT_texture_sRGB
282 case MESA_FORMAT_SRGB_DXT1:
283 case MESA_FORMAT_SRGBA_DXT1:
284 #endif
285 stride = ((width + 3) / 4) * 8; /* 8 bytes per 4x4 tile */
286 break;
287 case MESA_FORMAT_RGBA_DXT3:
288 case MESA_FORMAT_RGBA_DXT5:
289 #if FEATURE_EXT_texture_sRGB
290 case MESA_FORMAT_SRGBA_DXT3:
291 case MESA_FORMAT_SRGBA_DXT5:
292 #endif
293 stride = ((width + 3) / 4) * 16; /* 16 bytes per 4x4 tile */
294 break;
295 #endif
296 default:
297 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_row_stride");
298 return 0;
299 }
300
301 return stride;
302 }
303
304
305 /*
306 * Return the address of the pixel at (col, row, img) in a
307 * compressed texture image.
308 * \param col, row, img - image position (3D)
309 * \param format - compressed image format
310 * \param width - image width
311 * \param image - the image address
312 * \return address of pixel at (row, col)
313 */
314 GLubyte *
315 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
316 GLuint mesaFormat,
317 GLsizei width, const GLubyte *image)
318 {
319 GLubyte *addr;
320
321 (void) img;
322
323 /* We try to spot a "complete" subtexture "above" ROW, COL;
324 * this texture is given by appropriate rounding of WIDTH x ROW.
325 * Then we just add the amount left (usually on the left).
326 *
327 * Example for X*Y microtiles (Z bytes each)
328 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
329 */
330
331 switch (mesaFormat) {
332 #if FEATURE_texture_fxt1
333 case MESA_FORMAT_RGB_FXT1:
334 case MESA_FORMAT_RGBA_FXT1:
335 addr = (GLubyte *) image + 16 * (((width + 7) / 8) * (row / 4) + col / 8);
336 break;
337 #endif
338 #if FEATURE_texture_s3tc
339 case MESA_FORMAT_RGB_DXT1:
340 case MESA_FORMAT_RGBA_DXT1:
341 #if FEATURE_EXT_texture_sRGB
342 case MESA_FORMAT_SRGB_DXT1:
343 case MESA_FORMAT_SRGBA_DXT1:
344 #endif
345 addr = (GLubyte *) image + 8 * (((width + 3) / 4) * (row / 4) + col / 4);
346 break;
347 case MESA_FORMAT_RGBA_DXT3:
348 case MESA_FORMAT_RGBA_DXT5:
349 #if FEATURE_EXT_texture_sRGB
350 case MESA_FORMAT_SRGBA_DXT3:
351 case MESA_FORMAT_SRGBA_DXT5:
352 #endif
353 addr = (GLubyte *) image + 16 * (((width + 3) / 4) * (row / 4) + col / 4);
354 break;
355 #endif
356 default:
357 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_image_address");
358 addr = NULL;
359 }
360
361 return addr;
362 }