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