2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 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.
30 #include "texcompress.h"
31 #include "texformat.h"
35 * Get the list of supported internal compression formats.
36 * \param formats - the results list (may be NULL)
37 * \return number of formats.
40 _mesa_get_compressed_formats( GLcontext
*ctx
, GLint
*formats
)
43 if (ctx
->Extensions
.ARB_texture_compression
) {
44 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
46 formats
[n
++] = GL_COMPRESSED_RGB_FXT1_3DFX
;
47 formats
[n
++] = GL_COMPRESSED_RGBA_FXT1_3DFX
;
53 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
55 formats
[n
++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
56 /* Skip this one because it has a restriction (all transparent
57 * pixels become black). See the texture compressions spec for
58 * a detailed explanation. This is what NVIDIA does.
59 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
61 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
62 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
75 * Return bytes of storage needed for the given texture size and compressed
77 * \param width, height, depth the texture size in texels
78 * \param texFormat one of the specific compressed format enums
79 * \return size in bytes, or zero if bad texFormat
82 _mesa_compressed_texture_size( GLcontext
*ctx
,
83 GLsizei width
, GLsizei height
, GLsizei depth
,
89 case GL_COMPRESSED_RGB_FXT1_3DFX
:
90 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
91 /* round up to multiple of 4 */
92 size
= ((width
+ 7) / 8) * ((height
+ 3) / 4) * 16;
94 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
95 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
96 /* round up width, height to next multiple of 4 */
97 width
= (width
+ 3) & ~3;
98 height
= (height
+ 3) & ~3;
100 /* 8 bytes per 4x4 tile of RGB[A] texels */
101 size
= (width
* height
* 8) / 16;
102 /* Textures smaller than 4x4 will effectively be made into 4x4 and
108 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
109 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
110 /* round up width, height to next multiple of 4 */
111 width
= (width
+ 3) & ~3;
112 height
= (height
+ 3) & ~3;
114 /* 16 bytes per 4x4 tile of RGBA texels */
115 size
= width
* height
; /* simple! */
116 /* Textures smaller than 4x4 will effectively be made into 4x4 and
123 _mesa_problem(ctx
, "bad texformat in compressed_texture_size");
130 * Compute the bytes per row in a compressed texture image.
131 * We use this for computing the destination address for sub-texture updates.
132 * \param format one of the specific texture compression formats
133 * \param width image width in pixels
134 * \return stride, in bytes, between rows for compressed image
137 _mesa_compressed_row_stride(GLenum format
, GLsizei width
)
139 GLint bytesPerTile
, stride
;
142 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
143 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
146 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
147 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
154 stride
= ((width
+ 3) / 4) * bytesPerTile
;
160 * Return the address of the pixel at (col, row, img) in a
161 * compressed texture image.
162 * \param col, row, img - image position (3D)
163 * \param format - compressed image format
164 * \param width - image width
165 * \param image - the image address
166 * \return address of pixel at (row, col)
169 _mesa_compressed_image_address(GLint col
, GLint row
, GLint img
,
171 GLsizei width
, const GLubyte
*image
)
173 GLint bytesPerTile
, stride
;
176 ASSERT((row
& 3) == 0);
177 ASSERT((col
& 3) == 0);
181 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
182 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
185 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
186 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
193 stride
= ((width
+ 3) / 4) * bytesPerTile
;
195 addr
= (GLubyte
*) image
+ (row
/ 4) * stride
+ (col
/ 4) * bytesPerTile
;
202 * \param srcRowStride - source stride, in pixels
205 _mesa_compress_teximage( GLcontext
*ctx
, GLsizei width
, GLsizei height
,
206 GLenum srcFormat
, const GLchan
*source
,
208 const struct gl_texture_format
*dstFormat
,
209 GLubyte
*dest
, GLint dstRowStride
)
211 switch (dstFormat
->MesaFormat
) {
213 _mesa_problem(ctx
, "Bad dstFormat in _mesa_compress_teximage()");