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.
37 #include "texcompress.h"
38 #include "texformat.h"
43 * Return list of (and count of) all specific texture compression
44 * formats that are supported.
46 * \param ctx the GL context
47 * \param formats the resulting format list (may be NULL).
48 * \param all if true return all formats, even those with some kind
49 * of restrictions/limitations (See GL_ARB_texture_compression
50 * spec for more info).
52 * \return number of formats.
55 _mesa_get_compressed_formats(GLcontext
*ctx
, GLint
*formats
, GLboolean all
)
58 if (ctx
->Extensions
.ARB_texture_compression
) {
59 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
61 formats
[n
++] = GL_COMPRESSED_RGB_FXT1_3DFX
;
62 formats
[n
++] = GL_COMPRESSED_RGBA_FXT1_3DFX
;
68 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
70 formats
[n
++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
71 /* This format has some restrictions/limitations and so should
72 * not be returned via the GL_COMPRESSED_TEXTURE_FORMATS query.
73 * Specifically, all transparent pixels become black. NVIDIA
74 * omits this format too.
77 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
;
78 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
79 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
87 if (ctx
->Extensions
.S3_s3tc
) {
89 formats
[n
++] = GL_RGB_S3TC
;
90 formats
[n
++] = GL_RGB4_S3TC
;
91 formats
[n
++] = GL_RGBA_S3TC
;
92 formats
[n
++] = GL_RGBA4_S3TC
;
105 * Return number of bytes needed to store a texture of the given size
106 * using the specified compressed format.
107 * This is called via the ctx->Driver.CompressedTextureSize function,
108 * unless a device driver overrides it.
110 * \param width texture width in texels.
111 * \param height texture height in texels.
112 * \param depth texture depth in texels.
113 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
115 * \return size in bytes, or zero if bad format
118 _mesa_compressed_texture_size( GLcontext
*ctx
,
119 GLsizei width
, GLsizei height
, GLsizei depth
,
127 switch (mesaFormat
) {
128 case MESA_FORMAT_RGB_FXT1
:
129 case MESA_FORMAT_RGBA_FXT1
:
130 /* round up width to next multiple of 8, height to next multiple of 4 */
131 width
= (width
+ 7) & ~7;
132 height
= (height
+ 3) & ~3;
133 /* 16 bytes per 8x4 tile of RGB[A] texels */
134 size
= width
* height
/ 2;
135 /* Textures smaller than 8x4 will effectively be made into 8x4 and
141 case MESA_FORMAT_RGB_DXT1
:
142 case MESA_FORMAT_RGBA_DXT1
:
143 /* round up width, height to next multiple of 4 */
144 width
= (width
+ 3) & ~3;
145 height
= (height
+ 3) & ~3;
146 /* 8 bytes per 4x4 tile of RGB[A] texels */
147 size
= width
* height
/ 2;
148 /* Textures smaller than 4x4 will effectively be made into 4x4 and
154 case MESA_FORMAT_RGBA_DXT3
:
155 case MESA_FORMAT_RGBA_DXT5
:
156 /* round up width, height to next multiple of 4 */
157 width
= (width
+ 3) & ~3;
158 height
= (height
+ 3) & ~3;
159 /* 16 bytes per 4x4 tile of RGBA texels */
160 size
= width
* height
; /* simple! */
161 /* Textures smaller than 4x4 will effectively be made into 4x4 and
168 _mesa_problem(ctx
, "bad mesaFormat in _mesa_compressed_texture_size");
175 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
177 * Note: This function CAN NOT return a padded hardware texture size.
178 * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
180 * We use this function to validate the <imageSize> parameter
181 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
184 _mesa_compressed_texture_size_glenum(GLcontext
*ctx
,
185 GLsizei width
, GLsizei height
,
186 GLsizei depth
, GLenum glformat
)
191 case GL_COMPRESSED_RGB_FXT1_3DFX
:
192 mesaFormat
= MESA_FORMAT_RGB_FXT1
;
194 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
195 mesaFormat
= MESA_FORMAT_RGBA_FXT1
;
197 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
199 mesaFormat
= MESA_FORMAT_RGB_DXT1
;
201 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
203 mesaFormat
= MESA_FORMAT_RGBA_DXT1
;
205 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
207 mesaFormat
= MESA_FORMAT_RGBA_DXT3
;
209 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
211 mesaFormat
= MESA_FORMAT_RGBA_DXT5
;
217 return _mesa_compressed_texture_size(ctx
, width
, height
, depth
, mesaFormat
);
222 * Compute the bytes per row in a compressed texture image.
223 * We use this for computing the destination address for sub-texture updates.
224 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
225 * \param width image width in pixels
226 * \return stride, in bytes, between rows for compressed image
229 _mesa_compressed_row_stride(GLuint mesaFormat
, GLsizei width
)
233 switch (mesaFormat
) {
234 case MESA_FORMAT_RGB_FXT1
:
235 case MESA_FORMAT_RGBA_FXT1
:
236 stride
= ((width
+ 7) / 8) * 16; /* 16 bytes per 8x4 tile */
238 case MESA_FORMAT_RGB_DXT1
:
239 case MESA_FORMAT_RGBA_DXT1
:
240 stride
= ((width
+ 3) / 4) * 8; /* 8 bytes per 4x4 tile */
242 case MESA_FORMAT_RGBA_DXT3
:
243 case MESA_FORMAT_RGBA_DXT5
:
244 stride
= ((width
+ 3) / 4) * 16; /* 16 bytes per 4x4 tile */
247 _mesa_problem(NULL
, "bad mesaFormat in _mesa_compressed_row_stride");
256 * Return the address of the pixel at (col, row, img) in a
257 * compressed texture image.
258 * \param col, row, img - image position (3D)
259 * \param format - compressed image format
260 * \param width - image width
261 * \param image - the image address
262 * \return address of pixel at (row, col)
265 _mesa_compressed_image_address(GLint col
, GLint row
, GLint img
,
267 GLsizei width
, const GLubyte
*image
)
273 /* We try to spot a "complete" subtexture "above" ROW, COL;
274 * this texture is given by appropriate rounding of WIDTH x ROW.
275 * Then we just add the amount left (usually on the left).
277 * Example for X*Y microtiles (Z bytes each)
278 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
281 switch (mesaFormat
) {
282 case MESA_FORMAT_RGB_FXT1
:
283 case MESA_FORMAT_RGBA_FXT1
:
284 addr
= (GLubyte
*) image
+ 16 * (((width
+ 7) / 8) * (row
/ 4) + col
/ 8);
286 case MESA_FORMAT_RGB_DXT1
:
287 case MESA_FORMAT_RGBA_DXT1
:
288 addr
= (GLubyte
*) image
+ 8 * (((width
+ 3) / 4) * (row
/ 4) + col
/ 4);
290 case MESA_FORMAT_RGBA_DXT3
:
291 case MESA_FORMAT_RGBA_DXT5
:
292 addr
= (GLubyte
*) image
+ 16 * (((width
+ 3) / 4) * (row
/ 4) + col
/ 4);
295 _mesa_problem(NULL
, "bad mesaFormat in _mesa_compressed_image_address");