texture compression: getting warmer
[mesa.git] / src / mesa / main / texcompress_fxt1.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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 /**
27 * \file texcompress_fxt1.c
28 * GL_EXT_texture_compression_fxt1 support.
29 */
30
31
32 #include "glheader.h"
33 #include "imports.h"
34 #include "colormac.h"
35 #include "context.h"
36 #include "convolve.h"
37 #include "image.h"
38 #include "texcompress.h"
39 #include "texformat.h"
40 #include "texstore.h"
41
42
43 static GLuint
44 compress_fxt1 (GLcontext *ctx,
45 GLsizei srcWidth,
46 GLsizei srcHeight,
47 GLenum srcFormat,
48 const GLchan *source,
49 GLint srcRowStride,
50 GLubyte *dest,
51 GLint dstRowStride);
52
53
54 /**
55 * Called during context initialization.
56 */
57 void
58 _mesa_init_texture_fxt1( GLcontext *ctx )
59 {
60 }
61
62
63 /**
64 * Called via TexFormat->StoreImage to store an RGB_FXT1 texture.
65 */
66 static GLboolean
67 texstore_rgb_fxt1(STORE_PARAMS)
68 {
69 const GLchan *pixels;
70 GLint srcRowStride;
71 GLubyte *dst;
72 const GLint texWidth = dstRowStride * 8 / 16; /* a bit of a hack */
73 const GLchan *tempImage = NULL;
74
75 ASSERT(dstFormat == &_mesa_texformat_rgb_fxt1);
76 ASSERT(dstXoffset % 8 == 0);
77 ASSERT(dstYoffset % 4 == 0);
78 ASSERT(dstZoffset == 0);
79
80 /* [dBorca]
81 * we still need to pass 4byte/texel to the codec
82 */
83 if (1 || srcFormat != GL_RGB ||
84 srcType != CHAN_TYPE ||
85 ctx->_ImageTransferState ||
86 srcPacking->SwapBytes) {
87 /* convert image to RGB/GLchan */
88 tempImage = _mesa_make_temp_chan_image(ctx, dims,
89 baseInternalFormat,
90 /*dstFormat->BaseFormat*/GL_RGBA,
91 srcWidth, srcHeight, srcDepth,
92 srcFormat, srcType, srcAddr,
93 srcPacking);
94 if (!tempImage)
95 return GL_FALSE; /* out of memory */
96 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
97 pixels = tempImage;
98 srcRowStride = /*3*/4 * srcWidth;
99 srcFormat = GL_RGB;
100 }
101 else {
102 pixels = (const GLchan *) srcAddr;
103 srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat,
104 srcType) / sizeof(GLchan);
105 }
106
107 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
108 GL_COMPRESSED_RGB_FXT1_3DFX,
109 texWidth, dstAddr);
110
111 compress_fxt1(ctx, srcWidth, srcHeight, srcFormat, pixels, srcRowStride,
112 dst, dstRowStride);
113
114 if (tempImage)
115 _mesa_free((void *) tempImage);
116
117 return GL_TRUE;
118 }
119
120
121 /**
122 * Called via TexFormat->StoreImage to store an RGBA_FXT1 texture.
123 */
124 static GLboolean
125 texstore_rgba_fxt1(STORE_PARAMS)
126 {
127 const GLchan *pixels;
128 GLint srcRowStride;
129 GLubyte *dst;
130 GLint texWidth = dstRowStride * 8 / 16; /* a bit of a hack */
131 const GLchan *tempImage = NULL;
132
133 ASSERT(dstFormat == &_mesa_texformat_rgba_fxt1);
134 ASSERT(dstXoffset % 8 == 0);
135 ASSERT(dstYoffset % 4 == 0);
136 ASSERT(dstZoffset == 0);
137
138 if (srcFormat != GL_RGBA ||
139 srcType != CHAN_TYPE ||
140 ctx->_ImageTransferState ||
141 srcPacking->SwapBytes) {
142 /* convert image to RGBA/GLchan */
143 tempImage = _mesa_make_temp_chan_image(ctx, dims,
144 baseInternalFormat,
145 dstFormat->BaseFormat,
146 srcWidth, srcHeight, srcDepth,
147 srcFormat, srcType, srcAddr,
148 srcPacking);
149 if (!tempImage)
150 return GL_FALSE; /* out of memory */
151 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
152 pixels = tempImage;
153 srcRowStride = 4 * srcWidth;
154 srcFormat = GL_RGBA;
155 }
156 else {
157 pixels = (const GLchan *) srcAddr;
158 srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat,
159 srcType) / sizeof(GLchan);
160 }
161
162 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
163 GL_COMPRESSED_RGBA_FXT1_3DFX,
164 texWidth, dstAddr);
165
166 compress_fxt1(ctx, srcWidth, srcHeight, srcFormat, pixels, srcRowStride,
167 dst, dstRowStride);
168
169 if (tempImage)
170 _mesa_free((void*) tempImage);
171
172 return GL_TRUE;
173 }
174
175
176 static void
177 fetch_texel_2d_rgba_fxt1( const struct gl_texture_image *texImage,
178 GLint i, GLint j, GLint k, GLchan *texel )
179 {
180 /* XXX to do */
181 }
182
183
184 static void
185 fetch_texel_2d_f_rgba_fxt1( const struct gl_texture_image *texImage,
186 GLint i, GLint j, GLint k, GLfloat *texel )
187 {
188 /* just sample as GLchan and convert to float here */
189 GLchan rgba[4];
190 fetch_texel_2d_rgba_fxt1(texImage, i, j, k, rgba);
191 texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
192 texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
193 texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
194 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
195 }
196
197
198 static void
199 fetch_texel_2d_rgb_fxt1( const struct gl_texture_image *texImage,
200 GLint i, GLint j, GLint k, GLchan *texel )
201 {
202 /* XXX to do */
203 }
204
205
206 static void
207 fetch_texel_2d_f_rgb_fxt1( const struct gl_texture_image *texImage,
208 GLint i, GLint j, GLint k, GLfloat *texel )
209 {
210 /* just sample as GLchan and convert to float here */
211 GLchan rgba[4];
212 fetch_texel_2d_rgb_fxt1(texImage, i, j, k, rgba);
213 texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
214 texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
215 texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
216 texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
217 }
218
219
220
221 const struct gl_texture_format _mesa_texformat_rgb_fxt1 = {
222 MESA_FORMAT_RGB_FXT1, /* MesaFormat */
223 GL_RGB, /* BaseFormat */
224 GL_UNSIGNED_NORMALIZED_ARB, /* DataType */
225 4, /*approx*/ /* RedBits */
226 4, /*approx*/ /* GreenBits */
227 4, /*approx*/ /* BlueBits */
228 0, /* AlphaBits */
229 0, /* LuminanceBits */
230 0, /* IntensityBits */
231 0, /* IndexBits */
232 0, /* DepthBits */
233 0, /* TexelBytes */
234 texstore_rgb_fxt1, /* StoreTexImageFunc */
235 NULL, /*impossible*/ /* FetchTexel1D */
236 fetch_texel_2d_rgb_fxt1, /* FetchTexel2D */
237 NULL, /*impossible*/ /* FetchTexel3D */
238 NULL, /*impossible*/ /* FetchTexel1Df */
239 fetch_texel_2d_f_rgb_fxt1, /* FetchTexel2Df */
240 NULL, /*impossible*/ /* FetchTexel3Df */
241 };
242
243 const struct gl_texture_format _mesa_texformat_rgba_fxt1 = {
244 MESA_FORMAT_RGBA_FXT1, /* MesaFormat */
245 GL_RGBA, /* BaseFormat */
246 GL_UNSIGNED_NORMALIZED_ARB, /* DataType */
247 4, /*approx*/ /* RedBits */
248 4, /*approx*/ /* GreenBits */
249 4, /*approx*/ /* BlueBits */
250 1, /*approx*/ /* AlphaBits */
251 0, /* LuminanceBits */
252 0, /* IntensityBits */
253 0, /* IndexBits */
254 0, /* DepthBits */
255 0, /* TexelBytes */
256 texstore_rgba_fxt1, /* StoreTexImageFunc */
257 NULL, /*impossible*/ /* FetchTexel1D */
258 fetch_texel_2d_rgba_fxt1, /* FetchTexel2D */
259 NULL, /*impossible*/ /* FetchTexel3D */
260 NULL, /*impossible*/ /* FetchTexel1Df */
261 fetch_texel_2d_f_rgba_fxt1, /* FetchTexel2Df */
262 NULL, /*impossible*/ /* FetchTexel3Df */
263 };
264
265
266 static GLuint
267 compress_fxt1 (GLcontext *ctx,
268 GLsizei srcWidth,
269 GLsizei srcHeight,
270 GLenum srcFormat,
271 const GLchan *source,
272 GLint srcRowStride,
273 GLubyte *dest,
274 GLint dstRowStride)
275 {
276 /* here be dragons */
277 return -1;
278 }