some texture compression odds & ends
[mesa.git] / src / mesa / main / texcompress.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 #include "glheader.h"
27 #include "imports.h"
28 #include "context.h"
29 #include "image.h"
30 #include "texcompress.h"
31 #include "texformat.h"
32
33
34 /**
35 * Get the list of supported internal compression formats.
36 * \param formats - the results list (may be NULL)
37 * \return number of formats.
38 */
39 GLuint
40 _mesa_get_compressed_formats( GLcontext *ctx, GLint *formats )
41 {
42 GLuint n = 0;
43 if (ctx->Extensions.ARB_texture_compression) {
44 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
45 if (formats) {
46 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
47 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
48 }
49 else {
50 n += 2;
51 }
52 }
53 if (ctx->Extensions.EXT_texture_compression_s3tc) {
54 if (formats) {
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;
60 */
61 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
62 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
63 }
64 else {
65 n += 3;
66 }
67 }
68 }
69 return n;
70 }
71
72
73
74 /**
75 * Return bytes of storage needed for the given texture size and compressed
76 * format.
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
80 */
81 GLuint
82 _mesa_compressed_texture_size( GLcontext *ctx,
83 GLsizei width, GLsizei height, GLsizei depth,
84 GLenum format )
85 {
86 GLuint size;
87
88 switch (format) {
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;
93 return size;
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;
99 ASSERT(depth == 1);
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
103 * take 8 bytes.
104 */
105 if (size < 8)
106 size = 8;
107 return size;
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;
113 ASSERT(depth == 1);
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
117 * take 16 bytes.
118 */
119 if (size < 16)
120 size = 16;
121 return size;
122 default:
123 _mesa_problem(ctx, "bad texformat in compressed_texture_size");
124 return 0;
125 }
126 }
127
128
129 /**
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
135 */
136 GLint
137 _mesa_compressed_row_stride(GLenum format, GLsizei width)
138 {
139 GLint bytesPerTile, stride;
140
141 switch (format) {
142 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
143 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
144 bytesPerTile = 8;
145 break;
146 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
147 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
148 bytesPerTile = 16;
149 break;
150 default:
151 return 0;
152 }
153
154 stride = ((width + 3) / 4) * bytesPerTile;
155 return stride;
156 }
157
158
159 /**
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)
167 */
168 GLubyte *
169 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
170 GLenum format,
171 GLsizei width, const GLubyte *image)
172 {
173 GLint bytesPerTile, stride;
174 GLubyte *addr;
175
176 ASSERT((row & 3) == 0);
177 ASSERT((col & 3) == 0);
178 (void) img;
179
180 switch (format) {
181 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
182 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
183 bytesPerTile = 8;
184 break;
185 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
186 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
187 bytesPerTile = 16;
188 break;
189 default:
190 return 0;
191 }
192
193 stride = ((width + 3) / 4) * bytesPerTile;
194
195 addr = (GLubyte *) image + (row / 4) * stride + (col / 4) * bytesPerTile;
196 return addr;
197 }
198
199
200
201 /*
202 * \param srcRowStride - source stride, in pixels
203 */
204 void
205 _mesa_compress_teximage( GLcontext *ctx, GLsizei width, GLsizei height,
206 GLenum srcFormat, const GLchan *source,
207 GLint srcRowStride,
208 const struct gl_texture_format *dstFormat,
209 GLubyte *dest, GLint dstRowStride )
210 {
211 switch (dstFormat->MesaFormat) {
212 default:
213 _mesa_problem(ctx, "Bad dstFormat in _mesa_compress_teximage()");
214 return;
215 }
216 }