2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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.
38 #include "texcompress.h"
39 #include "texformat.h"
44 * Return list of (and count of) all specific texture compression
45 * formats that are supported.
47 * \param ctx the GL context
48 * \param formats the resulting format list (may be NULL).
49 * \param all if true return all formats, even those with some kind
50 * of restrictions/limitations (See GL_ARB_texture_compression
51 * spec for more info).
53 * \return number of formats.
56 _mesa_get_compressed_formats(GLcontext
*ctx
, GLint
*formats
, GLboolean all
)
59 if (ctx
->Extensions
.ARB_texture_compression
) {
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 */
119 * Return number of bytes needed to store a texture of the given size
120 * using the specified compressed format.
121 * This is called via the ctx->Driver.CompressedTextureSize function,
122 * unless a device driver overrides it.
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
,
142 switch (mesaFormat
) {
143 #if FEATURE_texture_fxt1
144 case MESA_FORMAT_RGB_FXT1
:
145 case MESA_FORMAT_RGBA_FXT1
:
146 /* round up width to next multiple of 8, height to next multiple of 4 */
147 width
= (width
+ 7) & ~7;
148 height
= (height
+ 3) & ~3;
149 /* 16 bytes per 8x4 tile of RGB[A] texels */
150 size
= width
* height
/ 2;
151 /* Textures smaller than 8x4 will effectively be made into 8x4 and
158 #if FEATURE_texture_s3tc
159 case MESA_FORMAT_RGB_DXT1
:
160 case MESA_FORMAT_RGBA_DXT1
:
161 /* round up width, height to next multiple of 4 */
162 width
= (width
+ 3) & ~3;
163 height
= (height
+ 3) & ~3;
164 /* 8 bytes per 4x4 tile of RGB[A] texels */
165 size
= width
* height
/ 2;
166 /* Textures smaller than 4x4 will effectively be made into 4x4 and
172 case MESA_FORMAT_RGBA_DXT3
:
173 case MESA_FORMAT_RGBA_DXT5
:
174 /* round up width, height to next multiple of 4 */
175 width
= (width
+ 3) & ~3;
176 height
= (height
+ 3) & ~3;
177 /* 16 bytes per 4x4 tile of RGBA texels */
178 size
= width
* height
; /* simple! */
179 /* Textures smaller than 4x4 will effectively be made into 4x4 and
187 _mesa_problem(ctx
, "bad mesaFormat in _mesa_compressed_texture_size");
194 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
196 * Note: This function CAN NOT return a padded hardware texture size.
197 * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
199 * We use this function to validate the <imageSize> parameter
200 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
203 _mesa_compressed_texture_size_glenum(GLcontext
*ctx
,
204 GLsizei width
, GLsizei height
,
205 GLsizei depth
, GLenum glformat
)
210 #if FEATURE_texture_fxt1
211 case GL_COMPRESSED_RGB_FXT1_3DFX
:
212 mesaFormat
= MESA_FORMAT_RGB_FXT1
;
214 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
215 mesaFormat
= MESA_FORMAT_RGBA_FXT1
;
218 #if FEATURE_texture_s3tc
219 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
221 mesaFormat
= MESA_FORMAT_RGB_DXT1
;
223 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
225 mesaFormat
= MESA_FORMAT_RGBA_DXT1
;
227 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
229 mesaFormat
= MESA_FORMAT_RGBA_DXT3
;
231 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
233 mesaFormat
= MESA_FORMAT_RGBA_DXT5
;
240 return _mesa_compressed_texture_size(ctx
, width
, height
, depth
, mesaFormat
);
245 * Compute the bytes per row in a compressed texture image.
246 * We use this for computing the destination address for sub-texture updates.
247 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
248 * \param width image width in pixels
249 * \return stride, in bytes, between rows for compressed image
252 _mesa_compressed_row_stride(GLuint mesaFormat
, GLsizei width
)
256 switch (mesaFormat
) {
257 #if FEATURE_texture_fxt1
258 case MESA_FORMAT_RGB_FXT1
:
259 case MESA_FORMAT_RGBA_FXT1
:
260 stride
= ((width
+ 7) / 8) * 16; /* 16 bytes per 8x4 tile */
263 #if FEATURE_texture_s3tc
264 case MESA_FORMAT_RGB_DXT1
:
265 case MESA_FORMAT_RGBA_DXT1
:
266 stride
= ((width
+ 3) / 4) * 8; /* 8 bytes per 4x4 tile */
268 case MESA_FORMAT_RGBA_DXT3
:
269 case MESA_FORMAT_RGBA_DXT5
:
270 stride
= ((width
+ 3) / 4) * 16; /* 16 bytes per 4x4 tile */
274 _mesa_problem(NULL
, "bad mesaFormat in _mesa_compressed_row_stride");
283 * Return the address of the pixel at (col, row, img) in a
284 * compressed texture image.
285 * \param col, row, img - image position (3D)
286 * \param format - compressed image format
287 * \param width - image width
288 * \param image - the image address
289 * \return address of pixel at (row, col)
292 _mesa_compressed_image_address(GLint col
, GLint row
, GLint img
,
294 GLsizei width
, const GLubyte
*image
)
300 /* We try to spot a "complete" subtexture "above" ROW, COL;
301 * this texture is given by appropriate rounding of WIDTH x ROW.
302 * Then we just add the amount left (usually on the left).
304 * Example for X*Y microtiles (Z bytes each)
305 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
308 switch (mesaFormat
) {
309 #if FEATURE_texture_fxt1
310 case MESA_FORMAT_RGB_FXT1
:
311 case MESA_FORMAT_RGBA_FXT1
:
312 addr
= (GLubyte
*) image
+ 16 * (((width
+ 7) / 8) * (row
/ 4) + col
/ 8);
315 #if FEATURE_texture_s3tc
316 case MESA_FORMAT_RGB_DXT1
:
317 case MESA_FORMAT_RGBA_DXT1
:
318 addr
= (GLubyte
*) image
+ 8 * (((width
+ 3) / 4) * (row
/ 4) + col
/ 4);
320 case MESA_FORMAT_RGBA_DXT3
:
321 case MESA_FORMAT_RGBA_DXT5
:
322 addr
= (GLubyte
*) image
+ 16 * (((width
+ 3) / 4) * (row
/ 4) + col
/ 4);
326 _mesa_problem(NULL
, "bad mesaFormat in _mesa_compressed_image_address");