2 * Copyright (C) 2011 Red Hat Inc.
4 * block compression parts are:
5 * Copyright (C) 2004 Roland Scheidegger All Rights Reserved.
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:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
31 * \file texcompress_rgtc.c
32 * GL_EXT_texture_compression_rgtc support.
41 #include "mfeatures.h"
43 #include "texcompress.h"
44 #include "texcompress_rgtc.h"
49 static void unsigned_encode_rgtc_chan(GLubyte *blkaddr, GLubyte srccolors[4][4],
50 GLint numxpixels, GLint numypixels);
51 static void signed_encode_rgtc_chan(GLbyte *blkaddr, GLbyte srccolors[4][4],
52 GLint numxpixels, GLint numypixels);
54 static void unsigned_fetch_texel_rgtc(unsigned srcRowStride, const GLubyte *pixdata,
55 unsigned i, unsigned j, GLubyte *value, unsigned comps);
57 static void signed_fetch_texel_rgtc(unsigned srcRowStride, const GLbyte *pixdata,
58 unsigned i, unsigned j, GLbyte *value, unsigned comps);
60 static void extractsrc_u( GLubyte srcpixels[4][4], const GLchan *srcaddr,
61 GLint srcRowStride, GLint numxpixels, GLint numypixels, GLint comps)
64 const GLchan *curaddr;
65 for (j = 0; j < numypixels; j++) {
66 curaddr = srcaddr + j * srcRowStride * comps;
67 for (i = 0; i < numxpixels; i++) {
68 srcpixels[j][i] = *curaddr / (CHAN_MAX / 255);
74 static void extractsrc_s( GLbyte srcpixels[4][4], const GLfloat *srcaddr,
75 GLint srcRowStride, GLint numxpixels, GLint numypixels, GLint comps)
78 const GLfloat *curaddr;
79 for (j = 0; j < numypixels; j++) {
80 curaddr = srcaddr + j * srcRowStride * comps;
81 for (i = 0; i < numxpixels; i++) {
82 srcpixels[j][i] = FLOAT_TO_BYTE_TEX(*curaddr);
90 _mesa_texstore_red_rgtc1(TEXSTORE_PARAMS)
93 const GLint texWidth = dstRowStride * 4 / 8; /* a bit of a hack */
94 const GLchan *tempImage = NULL;
96 int numxpixels, numypixels;
97 const GLchan *srcaddr;
98 GLubyte srcpixels[4][4];
101 ASSERT(dstFormat == MESA_FORMAT_RED_RGTC1 ||
102 dstFormat == MESA_FORMAT_L_LATC1);
103 ASSERT(dstXoffset % 4 == 0);
104 ASSERT(dstYoffset % 4 == 0);
105 ASSERT(dstZoffset % 4 == 0);
107 (void) dstImageOffsets;
110 tempImage = _mesa_make_temp_chan_image(ctx, dims,
112 _mesa_get_format_base_format(dstFormat),
113 srcWidth, srcHeight, srcDepth,
114 srcFormat, srcType, srcAddr,
117 return GL_FALSE; /* out of memory */
119 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
121 texWidth, (GLubyte *) dstAddr);
124 dstRowDiff = dstRowStride >= (srcWidth * 2) ? dstRowStride - (((srcWidth + 3) & ~3) * 2) : 0;
125 for (j = 0; j < srcHeight; j+=4) {
126 if (srcHeight > j + 3) numypixels = 4;
127 else numypixels = srcHeight - j;
128 srcaddr = tempImage + j * srcWidth;
129 for (i = 0; i < srcWidth; i += 4) {
130 if (srcWidth > i + 3) numxpixels = 4;
131 else numxpixels = srcWidth - i;
132 extractsrc_u(srcpixels, srcaddr, srcWidth, numxpixels, numypixels, 1);
133 unsigned_encode_rgtc_chan(blkaddr, srcpixels, numxpixels, numypixels);
134 srcaddr += numxpixels;
137 blkaddr += dstRowDiff;
140 free((void *) tempImage);
146 _mesa_texstore_signed_red_rgtc1(TEXSTORE_PARAMS)
149 const GLint texWidth = dstRowStride * 4 / 8; /* a bit of a hack */
150 const GLfloat *tempImage = NULL;
152 int numxpixels, numypixels;
153 const GLfloat *srcaddr;
154 GLbyte srcpixels[4][4];
157 ASSERT(dstFormat == MESA_FORMAT_SIGNED_RED_RGTC1 ||
158 dstFormat == MESA_FORMAT_SIGNED_L_LATC1);
159 ASSERT(dstXoffset % 4 == 0);
160 ASSERT(dstYoffset % 4 == 0);
161 ASSERT(dstZoffset % 4 == 0);
163 (void) dstImageOffsets;
165 tempImage = _mesa_make_temp_float_image(ctx, dims,
167 _mesa_get_format_base_format(dstFormat),
168 srcWidth, srcHeight, srcDepth,
169 srcFormat, srcType, srcAddr,
172 return GL_FALSE; /* out of memory */
174 dst = (GLbyte *)_mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
176 texWidth, (GLubyte *) dstAddr);
179 dstRowDiff = dstRowStride >= (srcWidth * 2) ? dstRowStride - (((srcWidth + 3) & ~3) * 2) : 0;
180 for (j = 0; j < srcHeight; j+=4) {
181 if (srcHeight > j + 3) numypixels = 4;
182 else numypixels = srcHeight - j;
183 srcaddr = tempImage + j * srcWidth;
184 for (i = 0; i < srcWidth; i += 4) {
185 if (srcWidth > i + 3) numxpixels = 4;
186 else numxpixels = srcWidth - i;
187 extractsrc_s(srcpixels, srcaddr, srcWidth, numxpixels, numypixels, 1);
188 signed_encode_rgtc_chan(blkaddr, srcpixels, numxpixels, numypixels);
189 srcaddr += numxpixels;
192 blkaddr += dstRowDiff;
195 free((void *) tempImage);
201 _mesa_texstore_rg_rgtc2(TEXSTORE_PARAMS)
204 const GLint texWidth = dstRowStride * 4 / 16; /* a bit of a hack */
205 const GLchan *tempImage = NULL;
207 int numxpixels, numypixels;
208 const GLchan *srcaddr;
209 GLubyte srcpixels[4][4];
213 ASSERT(dstFormat == MESA_FORMAT_RG_RGTC2 ||
214 dstFormat == MESA_FORMAT_LA_LATC2);
215 ASSERT(dstXoffset % 4 == 0);
216 ASSERT(dstYoffset % 4 == 0);
217 ASSERT(dstZoffset % 4 == 0);
219 (void) dstImageOffsets;
221 tempImage = _mesa_make_temp_chan_image(ctx, dims,
223 _mesa_get_format_base_format(dstFormat),
224 srcWidth, srcHeight, srcDepth,
225 srcFormat, srcType, srcAddr,
228 return GL_FALSE; /* out of memory */
230 dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
232 texWidth, (GLubyte *) dstAddr);
235 dstRowDiff = dstRowStride >= (srcWidth * 4) ? dstRowStride - (((srcWidth + 3) & ~3) * 4) : 0;
236 for (j = 0; j < srcHeight; j+=4) {
237 if (srcHeight > j + 3) numypixels = 4;
238 else numypixels = srcHeight - j;
239 srcaddr = tempImage + j * srcWidth * 2;
240 for (i = 0; i < srcWidth; i += 4) {
241 if (srcWidth > i + 3) numxpixels = 4;
242 else numxpixels = srcWidth - i;
243 extractsrc_u(srcpixels, srcaddr, srcWidth, numxpixels, numypixels, 2);
244 unsigned_encode_rgtc_chan(blkaddr, srcpixels, numxpixels, numypixels);
247 extractsrc_u(srcpixels, (GLchan *)srcaddr + 1, srcWidth, numxpixels, numypixels, 2);
248 unsigned_encode_rgtc_chan(blkaddr, srcpixels, numxpixels, numypixels);
252 srcaddr += numxpixels * 2;
254 blkaddr += dstRowDiff;
257 free((void *) tempImage);
263 _mesa_texstore_signed_rg_rgtc2(TEXSTORE_PARAMS)
266 const GLint texWidth = dstRowStride * 4 / 16; /* a bit of a hack */
267 const GLfloat *tempImage = NULL;
269 int numxpixels, numypixels;
270 const GLfloat *srcaddr;
271 GLbyte srcpixels[4][4];
275 ASSERT(dstFormat == MESA_FORMAT_SIGNED_RG_RGTC2 ||
276 dstFormat == MESA_FORMAT_SIGNED_LA_LATC2);
277 ASSERT(dstXoffset % 4 == 0);
278 ASSERT(dstYoffset % 4 == 0);
279 ASSERT(dstZoffset % 4 == 0);
281 (void) dstImageOffsets;
283 tempImage = _mesa_make_temp_float_image(ctx, dims,
285 _mesa_get_format_base_format(dstFormat),
286 srcWidth, srcHeight, srcDepth,
287 srcFormat, srcType, srcAddr,
290 return GL_FALSE; /* out of memory */
292 dst = (GLbyte *)_mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
294 texWidth, (GLubyte *) dstAddr);
297 dstRowDiff = dstRowStride >= (srcWidth * 4) ? dstRowStride - (((srcWidth + 3) & ~3) * 4) : 0;
298 for (j = 0; j < srcHeight; j += 4) {
299 if (srcHeight > j + 3) numypixels = 4;
300 else numypixels = srcHeight - j;
301 srcaddr = tempImage + j * srcWidth * 2;
302 for (i = 0; i < srcWidth; i += 4) {
303 if (srcWidth > i + 3) numxpixels = 4;
304 else numxpixels = srcWidth - i;
306 extractsrc_s(srcpixels, srcaddr, srcWidth, numxpixels, numypixels, 2);
307 signed_encode_rgtc_chan(blkaddr, srcpixels, numxpixels, numypixels);
310 extractsrc_s(srcpixels, srcaddr + 1, srcWidth, numxpixels, numypixels, 2);
311 signed_encode_rgtc_chan(blkaddr, srcpixels, numxpixels, numypixels);
314 srcaddr += numxpixels * 2;
317 blkaddr += dstRowDiff;
320 free((void *) tempImage);
326 _mesa_fetch_texel_2d_f_red_rgtc1(const struct gl_texture_image *texImage,
327 GLint i, GLint j, GLint k, GLfloat *texel)
330 unsigned_fetch_texel_rgtc(texImage->RowStride, (GLubyte *)(texImage->Data),
332 texel[RCOMP] = UBYTE_TO_FLOAT(red);
339 _mesa_fetch_texel_2d_f_signed_red_rgtc1(const struct gl_texture_image *texImage,
340 GLint i, GLint j, GLint k, GLfloat *texel)
343 signed_fetch_texel_rgtc(texImage->RowStride, (GLbyte *)(texImage->Data),
345 texel[RCOMP] = BYTE_TO_FLOAT_TEX(red);
352 _mesa_fetch_texel_2d_f_rg_rgtc2(const struct gl_texture_image *texImage,
353 GLint i, GLint j, GLint k, GLfloat *texel)
356 unsigned_fetch_texel_rgtc(texImage->RowStride, (GLubyte *)(texImage->Data),
358 unsigned_fetch_texel_rgtc(texImage->RowStride, (GLubyte *)(texImage->Data) + 8,
360 texel[RCOMP] = UBYTE_TO_FLOAT(red);
361 texel[GCOMP] = UBYTE_TO_FLOAT(green);
367 _mesa_fetch_texel_2d_f_signed_rg_rgtc2(const struct gl_texture_image *texImage,
368 GLint i, GLint j, GLint k, GLfloat *texel)
371 signed_fetch_texel_rgtc(texImage->RowStride, (GLbyte *)(texImage->Data),
373 signed_fetch_texel_rgtc(texImage->RowStride, (GLbyte *)(texImage->Data) + 8,
375 texel[RCOMP] = BYTE_TO_FLOAT_TEX(red);
376 texel[GCOMP] = BYTE_TO_FLOAT_TEX(green);
382 _mesa_fetch_texel_2d_f_l_latc1(const struct gl_texture_image *texImage,
383 GLint i, GLint j, GLint k, GLfloat *texel)
386 unsigned_fetch_texel_rgtc(texImage->RowStride, (GLubyte *)(texImage->Data),
390 texel[BCOMP] = UBYTE_TO_FLOAT(red);
395 _mesa_fetch_texel_2d_f_signed_l_latc1(const struct gl_texture_image *texImage,
396 GLint i, GLint j, GLint k, GLfloat *texel)
399 signed_fetch_texel_rgtc(texImage->RowStride, (GLbyte *)(texImage->Data),
403 texel[BCOMP] = BYTE_TO_FLOAT_TEX(red);
408 _mesa_fetch_texel_2d_f_la_latc2(const struct gl_texture_image *texImage,
409 GLint i, GLint j, GLint k, GLfloat *texel)
412 unsigned_fetch_texel_rgtc(texImage->RowStride, (GLubyte *)(texImage->Data),
414 unsigned_fetch_texel_rgtc(texImage->RowStride, (GLubyte *)(texImage->Data) + 8,
418 texel[BCOMP] = UBYTE_TO_FLOAT(red);
419 texel[ACOMP] = UBYTE_TO_FLOAT(green);
423 _mesa_fetch_texel_2d_f_signed_la_latc2(const struct gl_texture_image *texImage,
424 GLint i, GLint j, GLint k, GLfloat *texel)
427 signed_fetch_texel_rgtc(texImage->RowStride, (GLbyte *)(texImage->Data),
429 signed_fetch_texel_rgtc(texImage->RowStride, (GLbyte *)(texImage->Data) + 8,
433 texel[BCOMP] = BYTE_TO_FLOAT_TEX(red);
434 texel[ACOMP] = BYTE_TO_FLOAT_TEX(green);
437 #define TAG(x) unsigned_##x
443 #include "texcompress_rgtc_tmp.h"
450 #define TAG(x) signed_##x
452 #define T_MIN (GLbyte)-128
453 #define T_MAX (GLbyte)127
455 #include "texcompress_rgtc_tmp.h"