Merge branch 'llvm-cliptest-viewport'
[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 "texcompress.h"
38
39
40 /**
41 * Return list of (and count of) all specific texture compression
42 * formats that are supported.
43 *
44 * \param ctx the GL context
45 * \param formats the resulting format list (may be NULL).
46 * \param all if true return all formats, even those with some kind
47 * of restrictions/limitations (See GL_ARB_texture_compression
48 * spec for more info).
49 *
50 * \return number of formats.
51 */
52 GLuint
53 _mesa_get_compressed_formats(struct gl_context *ctx, GLint *formats, GLboolean all)
54 {
55 GLuint n = 0;
56 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
57 if (formats) {
58 formats[n++] = GL_COMPRESSED_RGB_FXT1_3DFX;
59 formats[n++] = GL_COMPRESSED_RGBA_FXT1_3DFX;
60 }
61 else {
62 n += 2;
63 }
64 }
65 if (ctx->Extensions.EXT_texture_compression_s3tc) {
66 if (formats) {
67 formats[n++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
68 /* This format has some restrictions/limitations and so should
69 * not be returned via the GL_COMPRESSED_TEXTURE_FORMATS query.
70 * Specifically, all transparent pixels become black. NVIDIA
71 * omits this format too.
72 */
73 if (all)
74 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
75 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
76 formats[n++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
77 }
78 else {
79 n += 3;
80 if (all)
81 n += 1;
82 }
83 }
84 if (ctx->Extensions.S3_s3tc) {
85 if (formats) {
86 formats[n++] = GL_RGB_S3TC;
87 formats[n++] = GL_RGB4_S3TC;
88 formats[n++] = GL_RGBA_S3TC;
89 formats[n++] = GL_RGBA4_S3TC;
90 }
91 else {
92 n += 4;
93 }
94 }
95 #if FEATURE_EXT_texture_sRGB
96 if (ctx->Extensions.EXT_texture_sRGB) {
97 if (formats) {
98 formats[n++] = GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
99 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
100 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
101 formats[n++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
102 }
103 else {
104 n += 4;
105 }
106 }
107 #endif /* FEATURE_EXT_texture_sRGB */
108 return n;
109
110 #if FEATURE_ES1 || FEATURE_ES2
111 if (formats) {
112 formats[n++] = GL_PALETTE4_RGB8_OES;
113 formats[n++] = GL_PALETTE4_RGBA8_OES;
114 formats[n++] = GL_PALETTE4_R5_G6_B5_OES;
115 formats[n++] = GL_PALETTE4_RGBA4_OES;
116 formats[n++] = GL_PALETTE4_RGB5_A1_OES;
117 formats[n++] = GL_PALETTE8_RGB8_OES;
118 formats[n++] = GL_PALETTE8_RGBA8_OES;
119 formats[n++] = GL_PALETTE8_R5_G6_B5_OES;
120 formats[n++] = GL_PALETTE8_RGBA4_OES;
121 formats[n++] = GL_PALETTE8_RGB5_A1_OES;
122 }
123 else {
124 n += 10;
125 }
126 #endif
127 }
128
129
130 /**
131 * Convert a compressed MESA_FORMAT_x to a GLenum.
132 */
133 gl_format
134 _mesa_glenum_to_compressed_format(GLenum format)
135 {
136 switch (format) {
137 case GL_COMPRESSED_RGB_FXT1_3DFX:
138 return MESA_FORMAT_RGB_FXT1;
139 case GL_COMPRESSED_RGBA_FXT1_3DFX:
140 return MESA_FORMAT_RGBA_FXT1;
141
142 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
143 case GL_RGB_S3TC:
144 return MESA_FORMAT_RGB_DXT1;
145 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
146 case GL_RGB4_S3TC:
147 return MESA_FORMAT_RGBA_DXT1;
148 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
149 case GL_RGBA_S3TC:
150 return MESA_FORMAT_RGBA_DXT3;
151 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
152 case GL_RGBA4_S3TC:
153 return MESA_FORMAT_RGBA_DXT5;
154
155 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
156 return MESA_FORMAT_SRGB_DXT1;
157 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
158 return MESA_FORMAT_SRGBA_DXT1;
159 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
160 return MESA_FORMAT_SRGBA_DXT3;
161 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
162 return MESA_FORMAT_SRGBA_DXT5;
163
164 default:
165 return MESA_FORMAT_NONE;
166 }
167 }
168
169
170 /**
171 * Given a compressed MESA_FORMAT_x value, return the corresponding
172 * GLenum for that format.
173 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
174 * which must return the specific texture format used when the user might
175 * have originally specified a generic compressed format in their
176 * glTexImage2D() call.
177 * For non-compressed textures, we always return the user-specified
178 * internal format unchanged.
179 */
180 GLenum
181 _mesa_compressed_format_to_glenum(struct gl_context *ctx, GLuint mesaFormat)
182 {
183 switch (mesaFormat) {
184 #if FEATURE_texture_fxt1
185 case MESA_FORMAT_RGB_FXT1:
186 return GL_COMPRESSED_RGB_FXT1_3DFX;
187 case MESA_FORMAT_RGBA_FXT1:
188 return GL_COMPRESSED_RGBA_FXT1_3DFX;
189 #endif
190 #if FEATURE_texture_s3tc
191 case MESA_FORMAT_RGB_DXT1:
192 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
193 case MESA_FORMAT_RGBA_DXT1:
194 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
195 case MESA_FORMAT_RGBA_DXT3:
196 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
197 case MESA_FORMAT_RGBA_DXT5:
198 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
199 #if FEATURE_EXT_texture_sRGB
200 case MESA_FORMAT_SRGB_DXT1:
201 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
202 case MESA_FORMAT_SRGBA_DXT1:
203 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
204 case MESA_FORMAT_SRGBA_DXT3:
205 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
206 case MESA_FORMAT_SRGBA_DXT5:
207 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
208 #endif
209 #endif
210 default:
211 _mesa_problem(ctx, "Unexpected mesa texture format in"
212 " _mesa_compressed_format_to_glenum()");
213 return 0;
214 }
215 }
216
217
218 /*
219 * Return the address of the pixel at (col, row, img) in a
220 * compressed texture image.
221 * \param col, row, img - image position (3D), should be a multiple of the
222 * format's block size.
223 * \param format - compressed image format
224 * \param width - image width (stride) in pixels
225 * \param image - the image address
226 * \return address of pixel at (row, col, img)
227 */
228 GLubyte *
229 _mesa_compressed_image_address(GLint col, GLint row, GLint img,
230 gl_format mesaFormat,
231 GLsizei width, const GLubyte *image)
232 {
233 /* XXX only 2D images implemented, not 3D */
234 const GLuint blockSize = _mesa_get_format_bytes(mesaFormat);
235 GLuint bw, bh;
236 GLint offset;
237
238 _mesa_get_format_block_size(mesaFormat, &bw, &bh);
239
240 ASSERT(col % bw == 0);
241 ASSERT(row % bh == 0);
242
243 offset = ((width + bw - 1) / bw) * (row / bh) + col / bw;
244 offset *= blockSize;
245
246 return (GLubyte *) image + offset;
247 }