Add a new 'all' parameter to _mesa_get_compressed_formats() to indicate whether
[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 "texcompress.h"
38 #include "texformat.h"
39 #include "texstore.h"
40
41
42 /**
43 * Return list of (and count of) all specific texture compression
44 * formats that are supported.
45 *
46 * \param ctx the GL context
47 * \param formats the resulting format list (may be NULL).
48 * \param all if true return all formats, even those with some kind
49 * of restrictions/limitations (See GL_ARB_texture_compression
50 * spec for more info).
51 *
52 * \return number of formats.
53 */
54 GLuint
55 _mesa_get_compressed_formats(GLcontext *ctx, GLint *formats, GLboolean all)
56 {
57 GLuint n = 0;
58 if (ctx->Extensions.ARB_texture_compression) {
59 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
60 if (formats) {
61 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
62 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
63 }
64 else {
65 n += 2;
66 }
67 }
68 if (ctx->Extensions.EXT_texture_compression_s3tc) {
69 if (formats) {
70 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
71 /* This format has some restrictions/limitations and so should
72 * not be returned via the GL_COMPRESSED_TEXTURE_FORMATS query.
73 * Specifically, all transparent pixels become black. NVIDIA
74 * omits this format too.
75 */
76 if (all)
77 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
78 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
79 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
80 }
81 else {
82 n += 3;
83 if (all)
84 n += 1;
85 }
86 }
87 if (ctx->Extensions.S3_s3tc) {
88 if (formats) {
89 formats[n++] = GL_RGB_S3TC;
90 formats[n++] = GL_RGB4_S3TC;
91 formats[n++] = GL_RGBA_S3TC;
92 formats[n++] = GL_RGBA4_S3TC;
93 }
94 else {
95 n += 4;
96 }
97 }
98 }
99 return n;
100 }
101
102
103
104 /**
105 * Return number of bytes needed to store a texture of the given size
106 * using the specified compressed format.
107 * This is called via the ctx->Driver.CompressedTextureSize function,
108 * unless a device driver overrides it.
109 *
110 * \param width texture width in texels.
111 * \param height texture height in texels.
112 * \param depth texture depth in texels.
113 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
114 *
115 * \return size in bytes, or zero if bad format
116 */
117 GLuint
118 _mesa_compressed_texture_size( GLcontext *ctx,
119 GLsizei width, GLsizei height, GLsizei depth,
120 GLuint mesaFormat )
121 {
122 GLuint size;
123
124 ASSERT(depth == 1);
125 (void) depth;
126
127 switch (mesaFormat) {
128 case MESA_FORMAT_RGB_FXT1:
129 case MESA_FORMAT_RGBA_FXT1:
130 /* round up width to next multiple of 8, height to next multiple of 4 */
131 width = (width + 7) & ~7;
132 height = (height + 3) & ~3;
133 /* 16 bytes per 8x4 tile of RGB[A] texels */
134 size = width * height / 2;
135 /* Textures smaller than 8x4 will effectively be made into 8x4 and
136 * take 16 bytes.
137 */
138 if (size < 16)
139 size = 16;
140 return size;
141 case MESA_FORMAT_RGB_DXT1:
142 case MESA_FORMAT_RGBA_DXT1:
143 /* round up width, height to next multiple of 4 */
144 width = (width + 3) & ~3;
145 height = (height + 3) & ~3;
146 /* 8 bytes per 4x4 tile of RGB[A] texels */
147 size = width * height / 2;
148 /* Textures smaller than 4x4 will effectively be made into 4x4 and
149 * take 8 bytes.
150 */
151 if (size < 8)
152 size = 8;
153 return size;
154 case MESA_FORMAT_RGBA_DXT3:
155 case MESA_FORMAT_RGBA_DXT5:
156 /* round up width, height to next multiple of 4 */
157 width = (width + 3) & ~3;
158 height = (height + 3) & ~3;
159 /* 16 bytes per 4x4 tile of RGBA texels */
160 size = width * height; /* simple! */
161 /* Textures smaller than 4x4 will effectively be made into 4x4 and
162 * take 16 bytes.
163 */
164 if (size < 16)
165 size = 16;
166 return size;
167 default:
168 _mesa_problem(ctx, "bad mesaFormat in _mesa_compressed_texture_size");
169 return 0;
170 }
171 }
172
173
174 /**
175 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
176 *
177 * Note: This function CAN NOT return a padded hardware texture size.
178 * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
179 *
180 * We use this function to validate the <imageSize> parameter
181 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
182 */
183 GLuint
184 _mesa_compressed_texture_size_glenum(GLcontext *ctx,
185 GLsizei width, GLsizei height,
186 GLsizei depth, GLenum glformat)
187 {
188 GLuint mesaFormat;
189
190 switch (glformat) {
191 case GL_COMPRESSED_RGB_FXT1_3DFX:
192 mesaFormat = MESA_FORMAT_RGB_FXT1;
193 break;
194 case GL_COMPRESSED_RGBA_FXT1_3DFX:
195 mesaFormat = MESA_FORMAT_RGBA_FXT1;
196 break;
197 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
198 case GL_RGB_S3TC:
199 mesaFormat = MESA_FORMAT_RGB_DXT1;
200 break;
201 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
202 case GL_RGB4_S3TC:
203 mesaFormat = MESA_FORMAT_RGBA_DXT1;
204 break;
205 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
206 case GL_RGBA_S3TC:
207 mesaFormat = MESA_FORMAT_RGBA_DXT3;
208 break;
209 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
210 case GL_RGBA4_S3TC:
211 mesaFormat = MESA_FORMAT_RGBA_DXT5;
212 break;
213 default:
214 return 0;
215 }
216
217 return _mesa_compressed_texture_size(ctx, width, height, depth, mesaFormat);
218 }
219
220
221 /*
222 * Compute the bytes per row in a compressed texture image.
223 * We use this for computing the destination address for sub-texture updates.
224 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
225 * \param width image width in pixels
226 * \return stride, in bytes, between rows for compressed image
227 */
228 GLint
229 _mesa_compressed_row_stride(GLuint mesaFormat, GLsizei width)
230 {
231 GLint stride;
232
233 switch (mesaFormat) {
234 case MESA_FORMAT_RGB_FXT1:
235 case MESA_FORMAT_RGBA_FXT1:
236 stride = ((width + 7) / 8) * 16; /* 16 bytes per 8x4 tile */
237 break;
238 case MESA_FORMAT_RGB_DXT1:
239 case MESA_FORMAT_RGBA_DXT1:
240 stride = ((width + 3) / 4) * 8; /* 8 bytes per 4x4 tile */
241 break;
242 case MESA_FORMAT_RGBA_DXT3:
243 case MESA_FORMAT_RGBA_DXT5:
244 stride = ((width + 3) / 4) * 16; /* 16 bytes per 4x4 tile */
245 break;
246 default:
247 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_row_stride");
248 return 0;
249 }
250
251 return stride;
252 }
253
254
255 /*
256 * Return the address of the pixel at (col, row, img) in a
257 * compressed texture image.
258 * \param col, row, img - image position (3D)
259 * \param format - compressed image format
260 * \param width - image width
261 * \param image - the image address
262 * \return address of pixel at (row, col)
263 */
264 GLubyte *
265 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
266 GLuint mesaFormat,
267 GLsizei width, const GLubyte *image)
268 {
269 GLubyte *addr;
270
271 (void) img;
272
273 /* We try to spot a "complete" subtexture "above" ROW, COL;
274 * this texture is given by appropriate rounding of WIDTH x ROW.
275 * Then we just add the amount left (usually on the left).
276 *
277 * Example for X*Y microtiles (Z bytes each)
278 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
279 */
280
281 switch (mesaFormat) {
282 case MESA_FORMAT_RGB_FXT1:
283 case MESA_FORMAT_RGBA_FXT1:
284 addr = (GLubyte *) image + 16 * (((width + 7) / 8) * (row / 4) + col / 8);
285 break;
286 case MESA_FORMAT_RGB_DXT1:
287 case MESA_FORMAT_RGBA_DXT1:
288 addr = (GLubyte *) image + 8 * (((width + 3) / 4) * (row / 4) + col / 4);
289 break;
290 case MESA_FORMAT_RGBA_DXT3:
291 case MESA_FORMAT_RGBA_DXT5:
292 addr = (GLubyte *) image + 16 * (((width + 3) / 4) * (row / 4) + col / 4);
293 break;
294 default:
295 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_image_address");
296 addr = NULL;
297 }
298
299 return addr;
300 }