d820ae92747c5f7a2135a301a25b697285ee597f
[mesa.git] / src / mesa / main / texcompress.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008 VMware, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file texcompress.c
29 * Helper functions for texture compression.
30 */
31
32
33 #include "glheader.h"
34 #include "imports.h"
35 #include "colormac.h"
36 #include "formats.h"
37 #include "mfeatures.h"
38 #include "mtypes.h"
39 #include "texcompress.h"
40
41
42 /**
43 * Return list of (and count of) all specific texture compression
44 * formats that are supported.
45 *
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).
51 *
52 * \return number of formats.
53 */
54 GLuint
55 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats, GLboolean all)
56 {
57 GLuint n = 0;
58 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
59 if (formats) {
60 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
61 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
62 }
63 else {
64 n += 2;
65 }
66 }
67 /* don't return RGTC - ARB_texture_compression_rgtc query 19 */
68 if (ctx->Extensions.EXT_texture_compression_s3tc) {
69 if (formats) {
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.
75 */
76 if (all)
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;
80 }
81 else {
82 n += 3;
83 if (all)
84 n += 1;
85 }
86 }
87 if (ctx->Extensions.S3_s3tc) {
88 if (formats) {
89 formats[n++] = GL_RGB_S3TC;
90 formats[n++] = GL_RGB4_S3TC;
91 formats[n++] = GL_RGBA_S3TC;
92 formats[n++] = GL_RGBA4_S3TC;
93 }
94 else {
95 n += 4;
96 }
97 }
98 #if FEATURE_EXT_texture_sRGB
99 if (ctx->Extensions.EXT_texture_sRGB) {
100 if (formats) {
101 formats[n++] = GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
102 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
103 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
104 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
105 }
106 else {
107 n += 4;
108 }
109 }
110 #endif /* FEATURE_EXT_texture_sRGB */
111 return n;
112
113 #if FEATURE_ES1 || FEATURE_ES2
114 if (formats) {
115 formats[n++] = GL_PALETTE4_RGB8_OES;
116 formats[n++] = GL_PALETTE4_RGBA8_OES;
117 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
118 formats[n++] = GL_PALETTE4_RGBA4_OES;
119 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
120 formats[n++] = GL_PALETTE8_RGB8_OES;
121 formats[n++] = GL_PALETTE8_RGBA8_OES;
122 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
123 formats[n++] = GL_PALETTE8_RGBA4_OES;
124 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
125 }
126 else {
127 n += 10;
128 }
129 #endif
130 }
131
132
133 /**
134 * Convert a compressed MESA_FORMAT_x to a GLenum.
135 */
136 gl_format
137 _mesa_glenum_to_compressed_format(GLenum format)
138 {
139 switch (format) {
140 case GL_COMPRESSED_RGB_FXT1_3DFX:
141 return MESA_FORMAT_RGB_FXT1;
142 case GL_COMPRESSED_RGBA_FXT1_3DFX:
143 return MESA_FORMAT_RGBA_FXT1;
144
145 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
146 case GL_RGB_S3TC:
147 return MESA_FORMAT_RGB_DXT1;
148 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
149 case GL_RGB4_S3TC:
150 return MESA_FORMAT_RGBA_DXT1;
151 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
152 case GL_RGBA_S3TC:
153 return MESA_FORMAT_RGBA_DXT3;
154 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
155 case GL_RGBA4_S3TC:
156 return MESA_FORMAT_RGBA_DXT5;
157
158 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
159 return MESA_FORMAT_SRGB_DXT1;
160 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
161 return MESA_FORMAT_SRGBA_DXT1;
162 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
163 return MESA_FORMAT_SRGBA_DXT3;
164 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
165 return MESA_FORMAT_SRGBA_DXT5;
166
167 case GL_COMPRESSED_RED_RGTC1:
168 return MESA_FORMAT_RED_RGTC1;
169 case GL_COMPRESSED_SIGNED_RED_RGTC1:
170 return MESA_FORMAT_SIGNED_RED_RGTC1;
171 case GL_COMPRESSED_RG_RGTC2:
172 return MESA_FORMAT_RG_RGTC2;
173 case GL_COMPRESSED_SIGNED_RG_RGTC2:
174 return MESA_FORMAT_SIGNED_RG_RGTC2;
175
176 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
177 return MESA_FORMAT_L_LATC1;
178 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
179 return MESA_FORMAT_SIGNED_L_LATC1;
180 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
181 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
182 return MESA_FORMAT_LA_LATC2;
183 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
184 return MESA_FORMAT_SIGNED_LA_LATC2;
185
186 default:
187 return MESA_FORMAT_NONE;
188 }
189 }
190
191
192 /**
193 * Given a compressed MESA_FORMAT_x value, return the corresponding
194 * GLenum for that format.
195 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
196 * which must return the specific texture format used when the user might
197 * have originally specified a generic compressed format in their
198 * glTexImage2D() call.
199 * For non-compressed textures, we always return the user-specified
200 * internal format unchanged.
201 */
202 GLenum
203 _mesa_compressed_format_to_glenum(struct gl_context *ctx, GLuint mesaFormat)
204 {
205 switch (mesaFormat) {
206 #if FEATURE_texture_fxt1
207 case MESA_FORMAT_RGB_FXT1:
208 return GL_COMPRESSED_RGB_FXT1_3DFX;
209 case MESA_FORMAT_RGBA_FXT1:
210 return GL_COMPRESSED_RGBA_FXT1_3DFX;
211 #endif
212 #if FEATURE_texture_s3tc
213 case MESA_FORMAT_RGB_DXT1:
214 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
215 case MESA_FORMAT_RGBA_DXT1:
216 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
217 case MESA_FORMAT_RGBA_DXT3:
218 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
219 case MESA_FORMAT_RGBA_DXT5:
220 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
221 #if FEATURE_EXT_texture_sRGB
222 case MESA_FORMAT_SRGB_DXT1:
223 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
224 case MESA_FORMAT_SRGBA_DXT1:
225 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
226 case MESA_FORMAT_SRGBA_DXT3:
227 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
228 case MESA_FORMAT_SRGBA_DXT5:
229 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
230 #endif
231 #endif
232
233 case MESA_FORMAT_RED_RGTC1:
234 return GL_COMPRESSED_RED_RGTC1;
235 case MESA_FORMAT_SIGNED_RED_RGTC1:
236 return GL_COMPRESSED_SIGNED_RED_RGTC1;
237 case MESA_FORMAT_RG_RGTC2:
238 return GL_COMPRESSED_RG_RGTC2;
239 case MESA_FORMAT_SIGNED_RG_RGTC2:
240 return GL_COMPRESSED_SIGNED_RG_RGTC2;
241
242 case MESA_FORMAT_L_LATC1:
243 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
244 case MESA_FORMAT_SIGNED_L_LATC1:
245 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
246 case MESA_FORMAT_LA_LATC2:
247 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
248 case MESA_FORMAT_SIGNED_LA_LATC2:
249 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
250
251 default:
252 _mesa_problem(ctx, "Unexpected mesa texture format in"
253 " _mesa_compressed_format_to_glenum()");
254 return 0;
255 }
256 }
257
258
259 /*
260 * Return the address of the pixel at (col, row, img) in a
261 * compressed texture image.
262 * \param col, row, img - image position (3D), should be a multiple of the
263 * format's block size.
264 * \param format - compressed image format
265 * \param width - image width (stride) in pixels
266 * \param image - the image address
267 * \return address of pixel at (row, col, img)
268 */
269 GLubyte *
270 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
271 gl_format mesaFormat,
272 GLsizei width, const GLubyte *image)
273 {
274 /* XXX only 2D images implemented, not 3D */
275 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
276 GLuint bw, bh;
277 GLint offset;
278
279 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
280
281 ASSERT(col % bw == 0);
282 ASSERT(row % bh == 0);
283
284 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
285 offset *= blockSize;
286
287 return (GLubyte *) image + offset;
288 }