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.
39 #include "texcompress.h"
40 #include "texformat.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.ARB_texture_compression) {
61 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
63 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
64 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
70 if (ctx->Extensions.EXT_texture_compression_s3tc) {
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.
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;
88 #if FEATURE_EXT_texture_sRGB
89 if (ctx->Extensions.EXT_texture_sRGB) {
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;
106 #endif /* FEATURE_EXT_texture_sRGB */
108 if (ctx->Extensions.S3_s3tc) {
110 formats[n++] = GL_RGB_S3TC;
111 formats[n++] = GL_RGB4_S3TC;
112 formats[n++] = GL_RGBA_S3TC;
113 formats[n++] = GL_RGBA4_S3TC;
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.
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
136 * \return size in bytes, or zero if bad format
139 _mesa_compressed_texture_size( GLcontext *ctx,
140 GLsizei width, GLsizei height, GLsizei depth,
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
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:
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
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:
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
196 _mesa_problem(ctx, "bad mesaFormat in _mesa_compressed_texture_size");
203 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
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.
208 * We use this function to validate the <imageSize> parameter
209 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
212 _mesa_compressed_texture_size_glenum(GLcontext *ctx,
213 GLsizei width, GLsizei height,
214 GLsizei depth, GLenum glformat)
219 #if FEATURE_texture_fxt1
220 case GL_COMPRESSED_RGB_FXT1_3DFX:
221 mesaFormat = MESA_FORMAT_RGB_FXT1;
223 case GL_COMPRESSED_RGBA_FXT1_3DFX:
224 mesaFormat = MESA_FORMAT_RGBA_FXT1;
227 #if FEATURE_texture_s3tc
228 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
230 mesaFormat = MESA_FORMAT_RGB_DXT1;
232 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
234 mesaFormat = MESA_FORMAT_RGBA_DXT1;
236 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
238 mesaFormat = MESA_FORMAT_RGBA_DXT3;
240 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
242 mesaFormat = MESA_FORMAT_RGBA_DXT5;
244 #if FEATURE_EXT_texture_sRGB
245 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
246 mesaFormat = MESA_FORMAT_SRGB_DXT1;
248 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
249 mesaFormat = MESA_FORMAT_SRGBA_DXT1;
251 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
252 mesaFormat = MESA_FORMAT_SRGBA_DXT3;
254 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
255 mesaFormat = MESA_FORMAT_SRGBA_DXT5;
263 return _mesa_compressed_texture_size(ctx, width, height, depth, mesaFormat);
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
275 _mesa_compressed_row_stride(GLuint mesaFormat, GLsizei width)
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 */
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 stride = ((width + 3) / 4) * 8; /* 8 bytes per 4x4 tile */
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 stride = ((width + 3) / 4) * 16; /* 16 bytes per 4x4 tile */
305 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_row_stride");
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)
323 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
325 GLsizei width, const GLubyte *image)
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).
335 * Example for X*Y microtiles (Z bytes each)
336 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
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);
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:
353 addr = (GLubyte *) image + 8 * (((width + 3) / 4) * (row / 4) + col / 4);
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:
361 addr = (GLubyte *) image + 16 * (((width + 3) / 4) * (row / 4) + col / 4);
365 _mesa_problem(NULL, "bad mesaFormat in _mesa_compressed_image_address");