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.
42 #include "texcompress.h"
43 #include "util/rgtc.h"
44 #include "texcompress_rgtc.h"
47 static void extractsrc_u( GLubyte srcpixels
[4][4], const GLubyte
*srcaddr
,
48 GLint srcRowStride
, GLint numxpixels
, GLint numypixels
, GLint comps
)
51 const GLubyte
*curaddr
;
52 for (j
= 0; j
< numypixels
; j
++) {
53 curaddr
= srcaddr
+ j
* srcRowStride
* comps
;
54 for (i
= 0; i
< numxpixels
; i
++) {
55 srcpixels
[j
][i
] = *curaddr
;
61 static void extractsrc_s( GLbyte srcpixels
[4][4], const GLfloat
*srcaddr
,
62 GLint srcRowStride
, GLint numxpixels
, GLint numypixels
, GLint comps
)
65 const GLfloat
*curaddr
;
66 for (j
= 0; j
< numypixels
; j
++) {
67 curaddr
= srcaddr
+ j
* srcRowStride
* comps
;
68 for (i
= 0; i
< numxpixels
; i
++) {
69 srcpixels
[j
][i
] = FLOAT_TO_BYTE_TEX(*curaddr
);
77 _mesa_texstore_red_rgtc1(TEXSTORE_PARAMS
)
80 const GLubyte
*tempImage
= NULL
;
82 int numxpixels
, numypixels
;
83 const GLubyte
*srcaddr
;
84 GLubyte srcpixels
[4][4];
86 GLint dstRowDiff
, redRowStride
;
87 GLubyte
*tempImageSlices
[1];
89 assert(dstFormat
== MESA_FORMAT_R_RGTC1_UNORM
||
90 dstFormat
== MESA_FORMAT_L_LATC1_UNORM
);
92 tempImage
= malloc(srcWidth
* srcHeight
* 1 * sizeof(GLubyte
));
94 return GL_FALSE
; /* out of memory */
95 redRowStride
= 1 * srcWidth
* sizeof(GLubyte
);
96 tempImageSlices
[0] = (GLubyte
*) tempImage
;
97 _mesa_texstore(ctx
, dims
,
100 redRowStride
, tempImageSlices
,
101 srcWidth
, srcHeight
, srcDepth
,
102 srcFormat
, srcType
, srcAddr
,
108 dstRowDiff
= dstRowStride
>= (srcWidth
* 2) ? dstRowStride
- (((srcWidth
+ 3) & ~3) * 2) : 0;
109 for (j
= 0; j
< srcHeight
; j
+=4) {
110 if (srcHeight
> j
+ 3) numypixels
= 4;
111 else numypixels
= srcHeight
- j
;
112 srcaddr
= tempImage
+ j
* srcWidth
;
113 for (i
= 0; i
< srcWidth
; i
+= 4) {
114 if (srcWidth
> i
+ 3) numxpixels
= 4;
115 else numxpixels
= srcWidth
- i
;
116 extractsrc_u(srcpixels
, srcaddr
, srcWidth
, numxpixels
, numypixels
, 1);
117 util_format_unsigned_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
118 srcaddr
+= numxpixels
;
121 blkaddr
+= dstRowDiff
;
124 free((void *) tempImage
);
130 _mesa_texstore_signed_red_rgtc1(TEXSTORE_PARAMS
)
133 const GLfloat
*tempImage
= NULL
;
135 int numxpixels
, numypixels
;
136 const GLfloat
*srcaddr
;
137 GLbyte srcpixels
[4][4];
139 GLint dstRowDiff
, redRowStride
;
140 GLfloat
*tempImageSlices
[1];
142 assert(dstFormat
== MESA_FORMAT_R_RGTC1_SNORM
||
143 dstFormat
== MESA_FORMAT_L_LATC1_SNORM
);
145 redRowStride
= 1 * srcWidth
* sizeof(GLfloat
);
146 tempImage
= malloc(srcWidth
* srcHeight
* 1 * sizeof(GLfloat
));
148 return GL_FALSE
; /* out of memory */
149 tempImageSlices
[0] = (GLfloat
*) tempImage
;
150 _mesa_texstore(ctx
, dims
,
152 MESA_FORMAT_R_FLOAT32
,
153 redRowStride
, (GLubyte
**)tempImageSlices
,
154 srcWidth
, srcHeight
, srcDepth
,
155 srcFormat
, srcType
, srcAddr
,
158 dst
= (GLbyte
*) dstSlices
[0];
161 dstRowDiff
= dstRowStride
>= (srcWidth
* 2) ? dstRowStride
- (((srcWidth
+ 3) & ~3) * 2) : 0;
162 for (j
= 0; j
< srcHeight
; j
+=4) {
163 if (srcHeight
> j
+ 3) numypixels
= 4;
164 else numypixels
= srcHeight
- j
;
165 srcaddr
= tempImage
+ j
* srcWidth
;
166 for (i
= 0; i
< srcWidth
; i
+= 4) {
167 if (srcWidth
> i
+ 3) numxpixels
= 4;
168 else numxpixels
= srcWidth
- i
;
169 extractsrc_s(srcpixels
, srcaddr
, srcWidth
, numxpixels
, numypixels
, 1);
170 util_format_signed_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
171 srcaddr
+= numxpixels
;
174 blkaddr
+= dstRowDiff
;
177 free((void *) tempImage
);
183 _mesa_texstore_rg_rgtc2(TEXSTORE_PARAMS
)
186 const GLubyte
*tempImage
= NULL
;
188 int numxpixels
, numypixels
;
189 const GLubyte
*srcaddr
;
190 GLubyte srcpixels
[4][4];
192 GLint dstRowDiff
, rgRowStride
;
193 mesa_format tempFormat
;
194 GLubyte
*tempImageSlices
[1];
196 assert(dstFormat
== MESA_FORMAT_RG_RGTC2_UNORM
||
197 dstFormat
== MESA_FORMAT_LA_LATC2_UNORM
);
199 if (baseInternalFormat
== GL_RG
)
200 tempFormat
= MESA_FORMAT_R8G8_UNORM
;
202 tempFormat
= MESA_FORMAT_L8A8_UNORM
;
204 rgRowStride
= 2 * srcWidth
* sizeof(GLubyte
);
205 tempImage
= malloc(srcWidth
* srcHeight
* 2 * sizeof(GLubyte
));
207 return GL_FALSE
; /* out of memory */
208 tempImageSlices
[0] = (GLubyte
*) tempImage
;
209 _mesa_texstore(ctx
, dims
,
212 rgRowStride
, tempImageSlices
,
213 srcWidth
, srcHeight
, srcDepth
,
214 srcFormat
, srcType
, srcAddr
,
220 dstRowDiff
= dstRowStride
>= (srcWidth
* 4) ? dstRowStride
- (((srcWidth
+ 3) & ~3) * 4) : 0;
221 for (j
= 0; j
< srcHeight
; j
+=4) {
222 if (srcHeight
> j
+ 3) numypixels
= 4;
223 else numypixels
= srcHeight
- j
;
224 srcaddr
= tempImage
+ j
* srcWidth
* 2;
225 for (i
= 0; i
< srcWidth
; i
+= 4) {
226 if (srcWidth
> i
+ 3) numxpixels
= 4;
227 else numxpixels
= srcWidth
- i
;
228 extractsrc_u(srcpixels
, srcaddr
, srcWidth
, numxpixels
, numypixels
, 2);
229 util_format_unsigned_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
232 extractsrc_u(srcpixels
, (GLubyte
*)srcaddr
+ 1, srcWidth
, numxpixels
, numypixels
, 2);
233 util_format_unsigned_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
237 srcaddr
+= numxpixels
* 2;
239 blkaddr
+= dstRowDiff
;
242 free((void *) tempImage
);
248 _mesa_texstore_signed_rg_rgtc2(TEXSTORE_PARAMS
)
251 const GLfloat
*tempImage
= NULL
;
253 int numxpixels
, numypixels
;
254 const GLfloat
*srcaddr
;
255 GLbyte srcpixels
[4][4];
257 GLint dstRowDiff
, rgRowStride
;
258 mesa_format tempFormat
;
259 GLfloat
*tempImageSlices
[1];
261 assert(dstFormat
== MESA_FORMAT_RG_RGTC2_SNORM
||
262 dstFormat
== MESA_FORMAT_LA_LATC2_SNORM
);
264 if (baseInternalFormat
== GL_RG
)
265 tempFormat
= MESA_FORMAT_RG_FLOAT32
;
267 tempFormat
= MESA_FORMAT_LA_FLOAT32
;
269 rgRowStride
= 2 * srcWidth
* sizeof(GLfloat
);
270 tempImage
= malloc(srcWidth
* srcHeight
* 2 * sizeof(GLfloat
));
272 return GL_FALSE
; /* out of memory */
273 tempImageSlices
[0] = (GLfloat
*) tempImage
;
274 _mesa_texstore(ctx
, dims
,
277 rgRowStride
, (GLubyte
**)tempImageSlices
,
278 srcWidth
, srcHeight
, srcDepth
,
279 srcFormat
, srcType
, srcAddr
,
282 dst
= (GLbyte
*) dstSlices
[0];
285 dstRowDiff
= dstRowStride
>= (srcWidth
* 4) ? dstRowStride
- (((srcWidth
+ 3) & ~3) * 4) : 0;
286 for (j
= 0; j
< srcHeight
; j
+= 4) {
287 if (srcHeight
> j
+ 3) numypixels
= 4;
288 else numypixels
= srcHeight
- j
;
289 srcaddr
= tempImage
+ j
* srcWidth
* 2;
290 for (i
= 0; i
< srcWidth
; i
+= 4) {
291 if (srcWidth
> i
+ 3) numxpixels
= 4;
292 else numxpixels
= srcWidth
- i
;
294 extractsrc_s(srcpixels
, srcaddr
, srcWidth
, numxpixels
, numypixels
, 2);
295 util_format_signed_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
298 extractsrc_s(srcpixels
, srcaddr
+ 1, srcWidth
, numxpixels
, numypixels
, 2);
299 util_format_signed_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
302 srcaddr
+= numxpixels
* 2;
305 blkaddr
+= dstRowDiff
;
308 free((void *) tempImage
);
314 fetch_red_rgtc1(const GLubyte
*map
,
315 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
318 util_format_unsigned_fetch_texel_rgtc(rowStride
, map
, i
, j
, &red
, 1);
319 texel
[RCOMP
] = UBYTE_TO_FLOAT(red
);
326 fetch_l_latc1(const GLubyte
*map
,
327 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
330 util_format_unsigned_fetch_texel_rgtc(rowStride
, map
, i
, j
, &red
, 1);
333 texel
[BCOMP
] = UBYTE_TO_FLOAT(red
);
338 fetch_signed_red_rgtc1(const GLubyte
*map
,
339 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
342 util_format_signed_fetch_texel_rgtc(rowStride
, (const GLbyte
*) map
,
344 texel
[RCOMP
] = BYTE_TO_FLOAT_TEX(red
);
351 fetch_signed_l_latc1(const GLubyte
*map
,
352 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
355 util_format_signed_fetch_texel_rgtc(rowStride
, (GLbyte
*) map
,
359 texel
[BCOMP
] = BYTE_TO_FLOAT(red
);
364 fetch_rg_rgtc2(const GLubyte
*map
,
365 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
368 util_format_unsigned_fetch_texel_rgtc(rowStride
,
371 util_format_unsigned_fetch_texel_rgtc(rowStride
,
374 texel
[RCOMP
] = UBYTE_TO_FLOAT(red
);
375 texel
[GCOMP
] = UBYTE_TO_FLOAT(green
);
381 fetch_la_latc2(const GLubyte
*map
,
382 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
385 util_format_unsigned_fetch_texel_rgtc(rowStride
,
388 util_format_unsigned_fetch_texel_rgtc(rowStride
,
393 texel
[BCOMP
] = UBYTE_TO_FLOAT(red
);
394 texel
[ACOMP
] = UBYTE_TO_FLOAT(green
);
399 fetch_signed_rg_rgtc2(const GLubyte
*map
,
400 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
403 util_format_signed_fetch_texel_rgtc(rowStride
,
406 util_format_signed_fetch_texel_rgtc(rowStride
,
409 texel
[RCOMP
] = BYTE_TO_FLOAT_TEX(red
);
410 texel
[GCOMP
] = BYTE_TO_FLOAT_TEX(green
);
417 fetch_signed_la_latc2(const GLubyte
*map
,
418 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
421 util_format_signed_fetch_texel_rgtc(rowStride
,
424 util_format_signed_fetch_texel_rgtc(rowStride
,
429 texel
[BCOMP
] = BYTE_TO_FLOAT_TEX(red
);
430 texel
[ACOMP
] = BYTE_TO_FLOAT_TEX(green
);
434 compressed_fetch_func
435 _mesa_get_compressed_rgtc_func(mesa_format format
)
438 case MESA_FORMAT_R_RGTC1_UNORM
:
439 return fetch_red_rgtc1
;
440 case MESA_FORMAT_L_LATC1_UNORM
:
441 return fetch_l_latc1
;
442 case MESA_FORMAT_R_RGTC1_SNORM
:
443 return fetch_signed_red_rgtc1
;
444 case MESA_FORMAT_L_LATC1_SNORM
:
445 return fetch_signed_l_latc1
;
446 case MESA_FORMAT_RG_RGTC2_UNORM
:
447 return fetch_rg_rgtc2
;
448 case MESA_FORMAT_LA_LATC2_UNORM
:
449 return fetch_la_latc2
;
450 case MESA_FORMAT_RG_RGTC2_SNORM
:
451 return fetch_signed_rg_rgtc2
;
452 case MESA_FORMAT_LA_LATC2_SNORM
:
453 return fetch_signed_la_latc2
;