2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008 VMware, Inc.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
29 * Helper functions for texture compression.
40 #include "texcompress.h"
45 * Return list of (and count of) all specific texture compression
46 * formats that are supported.
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).
54 * \return number of formats.
57 _mesa_get_compressed_formats(GLcontext *ctx, GLint *formats, GLboolean all)
60 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
62 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
63 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
69 if (ctx->Extensions.EXT_texture_compression_s3tc) {
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.
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;
88 if (ctx->Extensions.S3_s3tc) {
90 formats[n++] = GL_RGB_S3TC;
91 formats[n++] = GL_RGB4_S3TC;
92 formats[n++] = GL_RGBA_S3TC;
93 formats[n++] = GL_RGBA4_S3TC;
99 #if FEATURE_EXT_texture_sRGB
100 if (ctx->Extensions.EXT_texture_sRGB) {
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;
111 #endif /* FEATURE_EXT_texture_sRGB */
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. A driver might override this
122 * if it needs to use an unusual or padded texture memory layout.
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
129 * \return size in bytes, or zero if bad format
132 _mesa_compressed_texture_size( GLcontext *ctx,
133 GLsizei width, GLsizei height, GLsizei depth,
136 return _mesa_format_image_size(mesaFormat, width, height, depth);
141 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
143 * Note: This function CAN NOT return a padded hardware texture size.
144 * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
146 * We use this function to validate the <imageSize> parameter
147 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
150 _mesa_compressed_texture_size_glenum(GLcontext *ctx,
151 GLsizei width, GLsizei height,
152 GLsizei depth, GLenum glformat)
154 gl_format mesaFormat;
157 #if FEATURE_texture_fxt1
158 case GL_COMPRESSED_RGB_FXT1_3DFX:
159 mesaFormat = MESA_FORMAT_RGB_FXT1;
161 case GL_COMPRESSED_RGBA_FXT1_3DFX:
162 mesaFormat = MESA_FORMAT_RGBA_FXT1;
165 #if FEATURE_texture_s3tc
166 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
168 mesaFormat = MESA_FORMAT_RGB_DXT1;
170 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
172 mesaFormat = MESA_FORMAT_RGBA_DXT1;
174 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
176 mesaFormat = MESA_FORMAT_RGBA_DXT3;
178 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
180 mesaFormat = MESA_FORMAT_RGBA_DXT5;
182 #if FEATURE_EXT_texture_sRGB
183 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
184 mesaFormat = MESA_FORMAT_SRGB_DXT1;
186 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
187 mesaFormat = MESA_FORMAT_SRGBA_DXT1;
189 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
190 mesaFormat = MESA_FORMAT_SRGBA_DXT3;
192 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
193 mesaFormat = MESA_FORMAT_SRGBA_DXT5;
201 return _mesa_format_image_size(mesaFormat, width, height, depth);
206 * Compute the bytes per row in a compressed texture image.
207 * We use this for computing the destination address for sub-texture updates.
208 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
209 * \param width image width in pixels
210 * \return stride, in bytes, between rows for compressed image
213 _mesa_compressed_row_stride(GLuint mesaFormat, GLsizei width)
217 switch (mesaFormat) {
218 #if FEATURE_texture_fxt1
219 case MESA_FORMAT_RGB_FXT1:
220 case MESA_FORMAT_RGBA_FXT1:
221 stride = ((width + 7) / 8) * 16; /* 16 bytes per 8x4 tile */
224 #if FEATURE_texture_s3tc
225 case MESA_FORMAT_RGB_DXT1:
226 case MESA_FORMAT_RGBA_DXT1:
227 #if FEATURE_EXT_texture_sRGB
228 case MESA_FORMAT_SRGB_DXT1:
229 case MESA_FORMAT_SRGBA_DXT1:
231 stride = ((width + 3) / 4) * 8; /* 8 bytes per 4x4 tile */
233 case MESA_FORMAT_RGBA_DXT3:
234 case MESA_FORMAT_RGBA_DXT5:
235 #if FEATURE_EXT_texture_sRGB
236 case MESA_FORMAT_SRGBA_DXT3:
237 case MESA_FORMAT_SRGBA_DXT5:
239 stride = ((width + 3) / 4) * 16; /* 16 bytes per 4x4 tile */
243 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_row_stride");
247 assert(stride == _mesa_format_row_stride(mesaFormat, width));
254 * Return the address of the pixel at (col, row, img) in a
255 * compressed texture image.
256 * \param col, row, img - image position (3D)
257 * \param format - compressed image format
258 * \param width - image width
259 * \param image - the image address
260 * \return address of pixel at (row, col)
263 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
265 GLsizei width, const GLubyte *image)
271 /* We try to spot a "complete" subtexture "above" ROW, COL;
272 * this texture is given by appropriate rounding of WIDTH x ROW.
273 * Then we just add the amount left (usually on the left).
275 * Example for X*Y microtiles (Z bytes each)
276 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
279 switch (mesaFormat) {
280 #if FEATURE_texture_fxt1
281 case MESA_FORMAT_RGB_FXT1:
282 case MESA_FORMAT_RGBA_FXT1:
283 addr = (GLubyte *) image + 16 * (((width + 7) / 8) * (row / 4) + col / 8);
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:
293 addr = (GLubyte *) image + 8 * (((width + 3) / 4) * (row / 4) + col / 4);
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:
301 addr = (GLubyte *) image + 16 * (((width + 3) / 4) * (row / 4) + col / 4);
305 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_image_address");