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];
140 ASSERT(dstFormat
== MESA_FORMAT_R_RGTC1_SNORM
||
141 dstFormat
== MESA_FORMAT_L_LATC1_SNORM
);
143 tempImage
= _mesa_make_temp_float_image(ctx
, dims
,
145 _mesa_get_format_base_format(dstFormat
),
146 srcWidth
, srcHeight
, srcDepth
,
147 srcFormat
, srcType
, srcAddr
,
150 return GL_FALSE
; /* out of memory */
152 dst
= (GLbyte
*) dstSlices
[0];
155 dstRowDiff
= dstRowStride
>= (srcWidth
* 2) ? dstRowStride
- (((srcWidth
+ 3) & ~3) * 2) : 0;
156 for (j
= 0; j
< srcHeight
; j
+=4) {
157 if (srcHeight
> j
+ 3) numypixels
= 4;
158 else numypixels
= srcHeight
- j
;
159 srcaddr
= tempImage
+ j
* srcWidth
;
160 for (i
= 0; i
< srcWidth
; i
+= 4) {
161 if (srcWidth
> i
+ 3) numxpixels
= 4;
162 else numxpixels
= srcWidth
- i
;
163 extractsrc_s(srcpixels
, srcaddr
, srcWidth
, numxpixels
, numypixels
, 1);
164 util_format_signed_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
165 srcaddr
+= numxpixels
;
168 blkaddr
+= dstRowDiff
;
171 free((void *) tempImage
);
177 _mesa_texstore_rg_rgtc2(TEXSTORE_PARAMS
)
180 const GLubyte
*tempImage
= NULL
;
182 int numxpixels
, numypixels
;
183 const GLubyte
*srcaddr
;
184 GLubyte srcpixels
[4][4];
186 GLint dstRowDiff
, rgRowStride
;
187 mesa_format tempFormat
;
188 GLubyte
*tempImageSlices
[1];
190 ASSERT(dstFormat
== MESA_FORMAT_RG_RGTC2_UNORM
||
191 dstFormat
== MESA_FORMAT_LA_LATC2_UNORM
);
193 if (baseInternalFormat
== GL_RG
)
194 tempFormat
= MESA_FORMAT_R8G8_UNORM
;
196 tempFormat
= MESA_FORMAT_L8A8_UNORM
;
198 rgRowStride
= 2 * srcWidth
* sizeof(GLubyte
);
199 tempImage
= malloc(srcWidth
* srcHeight
* 2 * sizeof(GLubyte
));
201 return GL_FALSE
; /* out of memory */
202 tempImageSlices
[0] = (GLubyte
*) tempImage
;
203 _mesa_texstore(ctx
, dims
,
206 rgRowStride
, tempImageSlices
,
207 srcWidth
, srcHeight
, srcDepth
,
208 srcFormat
, srcType
, srcAddr
,
214 dstRowDiff
= dstRowStride
>= (srcWidth
* 4) ? dstRowStride
- (((srcWidth
+ 3) & ~3) * 4) : 0;
215 for (j
= 0; j
< srcHeight
; j
+=4) {
216 if (srcHeight
> j
+ 3) numypixels
= 4;
217 else numypixels
= srcHeight
- j
;
218 srcaddr
= tempImage
+ j
* srcWidth
* 2;
219 for (i
= 0; i
< srcWidth
; i
+= 4) {
220 if (srcWidth
> i
+ 3) numxpixels
= 4;
221 else numxpixels
= srcWidth
- i
;
222 extractsrc_u(srcpixels
, srcaddr
, srcWidth
, numxpixels
, numypixels
, 2);
223 util_format_unsigned_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
226 extractsrc_u(srcpixels
, (GLubyte
*)srcaddr
+ 1, srcWidth
, numxpixels
, numypixels
, 2);
227 util_format_unsigned_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
231 srcaddr
+= numxpixels
* 2;
233 blkaddr
+= dstRowDiff
;
236 free((void *) tempImage
);
242 _mesa_texstore_signed_rg_rgtc2(TEXSTORE_PARAMS
)
245 const GLfloat
*tempImage
= NULL
;
247 int numxpixels
, numypixels
;
248 const GLfloat
*srcaddr
;
249 GLbyte srcpixels
[4][4];
253 ASSERT(dstFormat
== MESA_FORMAT_RG_RGTC2_SNORM
||
254 dstFormat
== MESA_FORMAT_LA_LATC2_SNORM
);
256 tempImage
= _mesa_make_temp_float_image(ctx
, dims
,
258 _mesa_get_format_base_format(dstFormat
),
259 srcWidth
, srcHeight
, srcDepth
,
260 srcFormat
, srcType
, srcAddr
,
263 return GL_FALSE
; /* out of memory */
265 dst
= (GLbyte
*) dstSlices
[0];
268 dstRowDiff
= dstRowStride
>= (srcWidth
* 4) ? dstRowStride
- (((srcWidth
+ 3) & ~3) * 4) : 0;
269 for (j
= 0; j
< srcHeight
; j
+= 4) {
270 if (srcHeight
> j
+ 3) numypixels
= 4;
271 else numypixels
= srcHeight
- j
;
272 srcaddr
= tempImage
+ j
* srcWidth
* 2;
273 for (i
= 0; i
< srcWidth
; i
+= 4) {
274 if (srcWidth
> i
+ 3) numxpixels
= 4;
275 else numxpixels
= srcWidth
- i
;
277 extractsrc_s(srcpixels
, srcaddr
, srcWidth
, numxpixels
, numypixels
, 2);
278 util_format_signed_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
281 extractsrc_s(srcpixels
, srcaddr
+ 1, srcWidth
, numxpixels
, numypixels
, 2);
282 util_format_signed_encode_rgtc_ubyte(blkaddr
, srcpixels
, numxpixels
, numypixels
);
285 srcaddr
+= numxpixels
* 2;
288 blkaddr
+= dstRowDiff
;
291 free((void *) tempImage
);
297 fetch_red_rgtc1(const GLubyte
*map
,
298 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
301 util_format_unsigned_fetch_texel_rgtc(rowStride
, map
, i
, j
, &red
, 1);
302 texel
[RCOMP
] = UBYTE_TO_FLOAT(red
);
309 fetch_l_latc1(const GLubyte
*map
,
310 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
313 util_format_unsigned_fetch_texel_rgtc(rowStride
, map
, i
, j
, &red
, 1);
316 texel
[BCOMP
] = UBYTE_TO_FLOAT(red
);
321 fetch_signed_red_rgtc1(const GLubyte
*map
,
322 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
325 util_format_signed_fetch_texel_rgtc(rowStride
, (const GLbyte
*) map
,
327 texel
[RCOMP
] = BYTE_TO_FLOAT_TEX(red
);
334 fetch_signed_l_latc1(const GLubyte
*map
,
335 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
338 util_format_signed_fetch_texel_rgtc(rowStride
, (GLbyte
*) map
,
342 texel
[BCOMP
] = BYTE_TO_FLOAT(red
);
347 fetch_rg_rgtc2(const GLubyte
*map
,
348 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
351 util_format_unsigned_fetch_texel_rgtc(rowStride
,
354 util_format_unsigned_fetch_texel_rgtc(rowStride
,
357 texel
[RCOMP
] = UBYTE_TO_FLOAT(red
);
358 texel
[GCOMP
] = UBYTE_TO_FLOAT(green
);
364 fetch_la_latc2(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
,
376 texel
[BCOMP
] = UBYTE_TO_FLOAT(red
);
377 texel
[ACOMP
] = UBYTE_TO_FLOAT(green
);
382 fetch_signed_rg_rgtc2(const GLubyte
*map
,
383 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
386 util_format_signed_fetch_texel_rgtc(rowStride
,
389 util_format_signed_fetch_texel_rgtc(rowStride
,
392 texel
[RCOMP
] = BYTE_TO_FLOAT_TEX(red
);
393 texel
[GCOMP
] = BYTE_TO_FLOAT_TEX(green
);
400 fetch_signed_la_latc2(const GLubyte
*map
,
401 GLint rowStride
, GLint i
, GLint j
, GLfloat
*texel
)
404 util_format_signed_fetch_texel_rgtc(rowStride
,
407 util_format_signed_fetch_texel_rgtc(rowStride
,
412 texel
[BCOMP
] = BYTE_TO_FLOAT_TEX(red
);
413 texel
[ACOMP
] = BYTE_TO_FLOAT_TEX(green
);
417 compressed_fetch_func
418 _mesa_get_compressed_rgtc_func(mesa_format format
)
421 case MESA_FORMAT_R_RGTC1_UNORM
:
422 return fetch_red_rgtc1
;
423 case MESA_FORMAT_L_LATC1_UNORM
:
424 return fetch_l_latc1
;
425 case MESA_FORMAT_R_RGTC1_SNORM
:
426 return fetch_signed_red_rgtc1
;
427 case MESA_FORMAT_L_LATC1_SNORM
:
428 return fetch_signed_l_latc1
;
429 case MESA_FORMAT_RG_RGTC2_UNORM
:
430 return fetch_rg_rgtc2
;
431 case MESA_FORMAT_LA_LATC2_UNORM
:
432 return fetch_la_latc2
;
433 case MESA_FORMAT_RG_RGTC2_SNORM
:
434 return fetch_signed_rg_rgtc2
;
435 case MESA_FORMAT_LA_LATC2_SNORM
:
436 return fetch_signed_la_latc2
;