mesa: Add utility function to get base format from a GL compressed format
[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 * Get the GL base format of a specified GL compressed texture format
44 *
45 * From page 232 of the OpenGL 3.3 (Compatiblity Profile) spec:
46 *
47 * "Compressed Internal Format Base Internal Format Type
48 * --------------------------- -------------------- ---------
49 * COMPRESSED_ALPHA ALPHA Generic
50 * COMPRESSED_LUMINANCE LUMINANCE Generic
51 * COMPRESSED_LUMINANCE_ALPHA LUMINANCE_ALPHA Generic
52 * COMPRESSED_INTENSITY INTENSITY Generic
53 * COMPRESSED_RED RED Generic
54 * COMPRESSED_RG RG Generic
55 * COMPRESSED_RGB RGB Generic
56 * COMPRESSED_RGBA RGBA Generic
57 * COMPRESSED_SRGB RGB Generic
58 * COMPRESSED_SRGB_ALPHA RGBA Generic
59 * COMPRESSED_SLUMINANCE LUMINANCE Generic
60 * COMPRESSED_SLUMINANCE_ALPHA LUMINANCE_ALPHA Generic
61 * COMPRESSED_RED_RGTC1 RED Specific
62 * COMPRESSED_SIGNED_RED_RGTC1 RED Specific
63 * COMPRESSED_RG_RGTC2 RG Specific
64 * COMPRESSED_SIGNED_RG_RGTC2 RG Specific"
65 *
66 * \return
67 * The base format of \c format if \c format is a compressed format (either
68 * generic or specific. Otherwise 0 is returned.
69 */
70 GLenum
71 _mesa_gl_compressed_format_base_format(GLenum format)
72 {
73 switch (format) {
74 case GL_COMPRESSED_RED:
75 case GL_COMPRESSED_RED_RGTC1:
76 case GL_COMPRESSED_SIGNED_RED_RGTC1:
77 return GL_RED;
78
79 case GL_COMPRESSED_RG:
80 case GL_COMPRESSED_RG_RGTC2:
81 case GL_COMPRESSED_SIGNED_RG_RGTC2:
82 return GL_RG;
83
84 case GL_COMPRESSED_RGB:
85 case GL_COMPRESSED_SRGB:
86 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
87 case GL_COMPRESSED_RGB_FXT1_3DFX:
88 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
89 return GL_RGB;
90
91 case GL_COMPRESSED_RGBA:
92 case GL_COMPRESSED_SRGB_ALPHA:
93 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
94 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
95 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB:
96 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB:
97 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
98 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
99 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
100 case GL_COMPRESSED_RGBA_FXT1_3DFX:
101 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
102 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
103 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
104 return GL_RGBA;
105
106 case GL_COMPRESSED_ALPHA:
107 return GL_ALPHA;
108
109 case GL_COMPRESSED_LUMINANCE:
110 case GL_COMPRESSED_SLUMINANCE:
111 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
112 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
113 return GL_LUMINANCE;
114
115 case GL_COMPRESSED_LUMINANCE_ALPHA:
116 case GL_COMPRESSED_SLUMINANCE_ALPHA:
117 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
118 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
119 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
120 return GL_LUMINANCE_ALPHA;
121
122 case GL_COMPRESSED_INTENSITY:
123 return GL_INTENSITY;
124
125 default:
126 return 0;
127 }
128 }
129
130 /**
131 * Return list of (and count of) all specific texture compression
132 * formats that are supported.
133 *
134 * \param ctx the GL context
135 * \param formats the resulting format list (may be NULL).
136 * \param all if true return all formats, even those with some kind
137 * of restrictions/limitations (See GL_ARB_texture_compression
138 * spec for more info).
139 *
140 * \return number of formats.
141 */
142 GLuint
143 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats, GLboolean all)
144 {
145 GLuint n = 0;
146 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
147 if (formats) {
148 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
149 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
150 }
151 else {
152 n += 2;
153 }
154 }
155 /* don't return RGTC - ARB_texture_compression_rgtc query 19 */
156 if (ctx->Extensions.EXT_texture_compression_s3tc) {
157 if (formats) {
158 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
159 /* This format has some restrictions/limitations and so should
160 * not be returned via the GL_COMPRESSED_TEXTURE_FORMATS query.
161 * Specifically, all transparent pixels become black. NVIDIA
162 * omits this format too.
163 */
164 if (all)
165 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
166 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
167 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
168 }
169 else {
170 n += 3;
171 if (all)
172 n += 1;
173 }
174 }
175 if (ctx->Extensions.S3_s3tc) {
176 if (formats) {
177 formats[n++] = GL_RGB_S3TC;
178 formats[n++] = GL_RGB4_S3TC;
179 formats[n++] = GL_RGBA_S3TC;
180 formats[n++] = GL_RGBA4_S3TC;
181 }
182 else {
183 n += 4;
184 }
185 }
186 #if FEATURE_EXT_texture_sRGB
187 if (ctx->Extensions.EXT_texture_sRGB) {
188 if (formats) {
189 formats[n++] = GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
190 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
191 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
192 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
193 }
194 else {
195 n += 4;
196 }
197 }
198 #endif /* FEATURE_EXT_texture_sRGB */
199 return n;
200
201 #if FEATURE_ES1 || FEATURE_ES2
202 if (formats) {
203 formats[n++] = GL_PALETTE4_RGB8_OES;
204 formats[n++] = GL_PALETTE4_RGBA8_OES;
205 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
206 formats[n++] = GL_PALETTE4_RGBA4_OES;
207 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
208 formats[n++] = GL_PALETTE8_RGB8_OES;
209 formats[n++] = GL_PALETTE8_RGBA8_OES;
210 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
211 formats[n++] = GL_PALETTE8_RGBA4_OES;
212 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
213 }
214 else {
215 n += 10;
216 }
217 #endif
218 }
219
220
221 /**
222 * Convert a compressed MESA_FORMAT_x to a GLenum.
223 */
224 gl_format
225 _mesa_glenum_to_compressed_format(GLenum format)
226 {
227 switch (format) {
228 case GL_COMPRESSED_RGB_FXT1_3DFX:
229 return MESA_FORMAT_RGB_FXT1;
230 case GL_COMPRESSED_RGBA_FXT1_3DFX:
231 return MESA_FORMAT_RGBA_FXT1;
232
233 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
234 case GL_RGB_S3TC:
235 return MESA_FORMAT_RGB_DXT1;
236 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
237 case GL_RGB4_S3TC:
238 return MESA_FORMAT_RGBA_DXT1;
239 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
240 case GL_RGBA_S3TC:
241 return MESA_FORMAT_RGBA_DXT3;
242 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
243 case GL_RGBA4_S3TC:
244 return MESA_FORMAT_RGBA_DXT5;
245
246 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
247 return MESA_FORMAT_SRGB_DXT1;
248 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
249 return MESA_FORMAT_SRGBA_DXT1;
250 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
251 return MESA_FORMAT_SRGBA_DXT3;
252 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
253 return MESA_FORMAT_SRGBA_DXT5;
254
255 case GL_COMPRESSED_RED_RGTC1:
256 return MESA_FORMAT_RED_RGTC1;
257 case GL_COMPRESSED_SIGNED_RED_RGTC1:
258 return MESA_FORMAT_SIGNED_RED_RGTC1;
259 case GL_COMPRESSED_RG_RGTC2:
260 return MESA_FORMAT_RG_RGTC2;
261 case GL_COMPRESSED_SIGNED_RG_RGTC2:
262 return MESA_FORMAT_SIGNED_RG_RGTC2;
263
264 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
265 return MESA_FORMAT_L_LATC1;
266 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
267 return MESA_FORMAT_SIGNED_L_LATC1;
268 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
269 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
270 return MESA_FORMAT_LA_LATC2;
271 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
272 return MESA_FORMAT_SIGNED_LA_LATC2;
273
274 default:
275 return MESA_FORMAT_NONE;
276 }
277 }
278
279
280 /**
281 * Given a compressed MESA_FORMAT_x value, return the corresponding
282 * GLenum for that format.
283 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
284 * which must return the specific texture format used when the user might
285 * have originally specified a generic compressed format in their
286 * glTexImage2D() call.
287 * For non-compressed textures, we always return the user-specified
288 * internal format unchanged.
289 */
290 GLenum
291 _mesa_compressed_format_to_glenum(struct gl_context *ctx, GLuint mesaFormat)
292 {
293 switch (mesaFormat) {
294 #if FEATURE_texture_fxt1
295 case MESA_FORMAT_RGB_FXT1:
296 return GL_COMPRESSED_RGB_FXT1_3DFX;
297 case MESA_FORMAT_RGBA_FXT1:
298 return GL_COMPRESSED_RGBA_FXT1_3DFX;
299 #endif
300 #if FEATURE_texture_s3tc
301 case MESA_FORMAT_RGB_DXT1:
302 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
303 case MESA_FORMAT_RGBA_DXT1:
304 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
305 case MESA_FORMAT_RGBA_DXT3:
306 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
307 case MESA_FORMAT_RGBA_DXT5:
308 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
309 #if FEATURE_EXT_texture_sRGB
310 case MESA_FORMAT_SRGB_DXT1:
311 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
312 case MESA_FORMAT_SRGBA_DXT1:
313 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
314 case MESA_FORMAT_SRGBA_DXT3:
315 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
316 case MESA_FORMAT_SRGBA_DXT5:
317 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
318 #endif
319 #endif
320
321 case MESA_FORMAT_RED_RGTC1:
322 return GL_COMPRESSED_RED_RGTC1;
323 case MESA_FORMAT_SIGNED_RED_RGTC1:
324 return GL_COMPRESSED_SIGNED_RED_RGTC1;
325 case MESA_FORMAT_RG_RGTC2:
326 return GL_COMPRESSED_RG_RGTC2;
327 case MESA_FORMAT_SIGNED_RG_RGTC2:
328 return GL_COMPRESSED_SIGNED_RG_RGTC2;
329
330 case MESA_FORMAT_L_LATC1:
331 return GL_COMPRESSED_LUMINANCE_LATC1_EXT;
332 case MESA_FORMAT_SIGNED_L_LATC1:
333 return GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT;
334 case MESA_FORMAT_LA_LATC2:
335 return GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
336 case MESA_FORMAT_SIGNED_LA_LATC2:
337 return GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT;
338
339 default:
340 _mesa_problem(ctx, "Unexpected mesa texture format in"
341 " _mesa_compressed_format_to_glenum()");
342 return 0;
343 }
344 }
345
346
347 /*
348 * Return the address of the pixel at (col, row, img) in a
349 * compressed texture image.
350 * \param col, row, img - image position (3D), should be a multiple of the
351 * format's block size.
352 * \param format - compressed image format
353 * \param width - image width (stride) in pixels
354 * \param image - the image address
355 * \return address of pixel at (row, col, img)
356 */
357 GLubyte *
358 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
359 gl_format mesaFormat,
360 GLsizei width, const GLubyte *image)
361 {
362 /* XXX only 2D images implemented, not 3D */
363 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
364 GLuint bw, bh;
365 GLint offset;
366
367 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
368
369 ASSERT(col % bw == 0);
370 ASSERT(row % bh == 0);
371
372 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
373 offset *= blockSize;
374
375 return (GLubyte *) image + offset;
376 }