2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
28 * Helper functions for texture compression.
37 #include "texcompress.h"
38 #include "texformat.h"
42 * Get the list of supported internal compression formats.
44 * \param ctx GL context.
45 * \param formats the resulting format list (may be NULL).
47 * \return number of formats.
50 _mesa_get_compressed_formats( GLcontext
*ctx
, GLint
*formats
)
53 if (ctx
->Extensions
.ARB_texture_compression
) {
54 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
56 formats
[n
++] = GL_COMPRESSED_RGB_FXT1_3DFX
;
57 formats
[n
++] = GL_COMPRESSED_RGBA_FXT1_3DFX
;
63 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
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;
71 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
72 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
78 if (ctx
->Extensions
.S3_s3tc
) {
80 formats
[n
++] = GL_RGB_S3TC
;
81 formats
[n
++] = GL_RGB4_S3TC
;
82 formats
[n
++] = GL_RGBA_S3TC
;
83 formats
[n
++] = GL_RGBA4_S3TC
;
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.
101 * \param width texture width in texels.
102 * \param height texture height in texels.
103 * \param depth texture depth in texels.
104 * \param format - one of the specific compressed texture formats
106 * \return size in bytes, or zero if bad format
109 _mesa_compressed_texture_size( GLcontext
*ctx
,
110 GLsizei width
, GLsizei height
, GLsizei depth
,
119 case GL_COMPRESSED_RGB_FXT1_3DFX
:
120 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
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
132 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
133 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
136 /* round up width, height to next multiple of 4 */
137 width
= (width
+ 3) & ~3;
138 height
= (height
+ 3) & ~3;
139 /* 8 bytes per 4x4 tile of RGB[A] texels */
140 size
= width
* height
/ 2;
141 /* Textures smaller than 4x4 will effectively be made into 4x4 and
147 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
148 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
151 /* round up width, height to next multiple of 4 */
152 width
= (width
+ 3) & ~3;
153 height
= (height
+ 3) & ~3;
154 /* 16 bytes per 4x4 tile of RGBA texels */
155 size
= width
* height
; /* simple! */
156 /* Textures smaller than 4x4 will effectively be made into 4x4 and
163 _mesa_problem(ctx
, "bad texformat in compressed_texture_size");
170 * Compute the bytes per row in a compressed texture image.
171 * We use this for computing the destination address for sub-texture updates.
172 * \param format one of the specific texture compression formats
173 * \param width image width in pixels
174 * \return stride, in bytes, between rows for compressed image
177 _mesa_compressed_row_stride(GLenum format
, GLsizei width
)
182 case GL_COMPRESSED_RGB_FXT1_3DFX
:
183 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
184 stride
= ((width
+ 7) / 8) * 16; /* 16 bytes per 8x4 tile */
186 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
187 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
190 stride
= ((width
+ 3) / 4) * 8; /* 8 bytes per 4x4 tile */
192 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
193 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
196 stride
= ((width
+ 3) / 4) * 16; /* 16 bytes per 4x4 tile */
207 * Return the address of the pixel at (col, row, img) in a
208 * compressed texture image.
209 * \param col, row, img - image position (3D)
210 * \param format - compressed image format
211 * \param width - image width
212 * \param image - the image address
213 * \return address of pixel at (row, col)
216 _mesa_compressed_image_address(GLint col
, GLint row
, GLint img
,
218 GLsizei width
, const GLubyte
*image
)
224 /* We try to spot a "complete" subtexture "above" ROW, COL;
225 * this texture is given by appropriate rounding of WIDTH x ROW.
226 * Then we just add the amount left (usually on the left).
228 * Example for X*Y microtiles (Z bytes each)
229 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
233 case GL_COMPRESSED_RGB_FXT1_3DFX
:
234 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
235 addr
= (GLubyte
*) image
+ 16 * (((width
+ 7) / 8) * (row
/ 4) + col
/ 8);
237 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
238 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
241 addr
= (GLubyte
*) image
+ 8 * (((width
+ 3) / 4) * (row
/ 4) + col
/ 4);
243 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
244 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
247 addr
= (GLubyte
*) image
+ 16 * (((width
+ 3) / 4) * (row
/ 4) + col
/ 4);