1 /**************************************************************************
3 * Copyright (C) 2011 Red Hat Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 **************************************************************************/
28 #include "u_format_rgtc.h"
30 static void u_format_unsigned_encode_rgtc_ubyte(uint8_t *blkaddr
, uint8_t srccolors
[4][4],
31 int numxpixels
, int numypixels
);
33 static void u_format_unsigned_fetch_texel_rgtc(unsigned srcRowStride
, const uint8_t *pixdata
,
34 unsigned i
, unsigned j
, uint8_t *value
, unsigned comps
);
36 static void u_format_signed_encode_rgtc_ubyte(int8_t *blkaddr
, int8_t srccolors
[4][4],
37 int numxpixels
, int numypixels
);
39 static void u_format_signed_fetch_texel_rgtc(unsigned srcRowStride
, const int8_t *pixdata
,
40 unsigned i
, unsigned j
, int8_t *value
, unsigned comps
);
43 util_format_rgtc1_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
45 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
52 util_format_rgtc1_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
54 const unsigned bw
= 4, bh
= 4, comps
= 4;
56 unsigned block_size
= 8;
58 for(y
= 0; y
< height
; y
+= bh
) {
59 const uint8_t *src
= src_row
;
60 for(x
= 0; x
< width
; x
+= bw
) {
61 for(j
= 0; j
< bh
; ++j
) {
62 for(i
= 0; i
< bw
; ++i
) {
63 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
64 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
72 src_row
+= src_stride
;
77 util_format_rgtc1_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
,
78 unsigned src_stride
, unsigned width
, unsigned height
)
80 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
83 for(y
= 0; y
< height
; y
+= bh
) {
84 uint8_t *dst
= dst_row
;
85 for(x
= 0; x
< width
; x
+= bw
) {
86 uint8_t tmp
[4][4]; /* [bh][bw][comps] */
87 for(j
= 0; j
< bh
; ++j
) {
88 for(i
= 0; i
< bw
; ++i
) {
89 tmp
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
92 u_format_unsigned_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
93 dst
+= bytes_per_block
;
95 dst_row
+= dst_stride
/ sizeof(*dst_row
);
100 util_format_rgtc1_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
104 for(y
= 0; y
< height
; y
+= 4) {
105 const uint8_t *src
= src_row
;
106 for(x
= 0; x
< width
; x
+= 4) {
107 for(j
= 0; j
< 4; ++j
) {
108 for(i
= 0; i
< 4; ++i
) {
109 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
111 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
112 dst
[0] = ubyte_to_float(tmp_r
);
120 src_row
+= src_stride
;
125 util_format_rgtc1_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
127 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
130 for(y
= 0; y
< height
; y
+= bh
) {
131 uint8_t *dst
= dst_row
;
132 for(x
= 0; x
< width
; x
+= bw
) {
133 uint8_t tmp
[4][4]; /* [bh][bw][comps] */
134 for(j
= 0; j
< bh
; ++j
) {
135 for(i
= 0; i
< bw
; ++i
) {
136 tmp
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
139 u_format_unsigned_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
140 dst
+= bytes_per_block
;
142 dst_row
+= dst_stride
/ sizeof(*dst_row
);
147 util_format_rgtc1_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
150 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
151 dst
[0] = ubyte_to_float(tmp_r
);
158 util_format_rgtc1_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
160 fprintf(stderr
,"%s\n", __func__
);
164 util_format_rgtc1_snorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
166 fprintf(stderr
,"%s\n", __func__
);
170 util_format_rgtc1_snorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
172 fprintf(stderr
,"%s\n", __func__
);
176 util_format_rgtc1_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
178 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
181 for(y
= 0; y
< height
; y
+= bh
) {
182 int8_t *dst
= (int8_t *)dst_row
;
183 for(x
= 0; x
< width
; x
+= bw
) {
184 int8_t tmp
[4][4]; /* [bh][bw][comps] */
185 for(j
= 0; j
< bh
; ++j
) {
186 for(i
= 0; i
< bw
; ++i
) {
187 tmp
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
190 u_format_signed_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
191 dst
+= bytes_per_block
;
193 dst_row
+= dst_stride
/ sizeof(*dst_row
);
198 util_format_rgtc1_snorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
202 for(y
= 0; y
< height
; y
+= 4) {
203 const int8_t *src
= (int8_t *)src_row
;
204 for(x
= 0; x
< width
; x
+= 4) {
205 for(j
= 0; j
< 4; ++j
) {
206 for(i
= 0; i
< 4; ++i
) {
207 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
209 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
210 dst
[0] = byte_to_float_tex(tmp_r
);
218 src_row
+= src_stride
;
223 util_format_rgtc1_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
226 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 1);
227 dst
[0] = byte_to_float_tex(tmp_r
);
235 util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
237 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
238 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
244 util_format_rgtc2_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
246 const unsigned bw
= 4, bh
= 4, comps
= 4;
248 unsigned block_size
= 16;
250 for(y
= 0; y
< height
; y
+= bh
) {
251 const uint8_t *src
= src_row
;
252 for(x
= 0; x
< width
; x
+= bw
) {
253 for(j
= 0; j
< bh
; ++j
) {
254 for(i
= 0; i
< bw
; ++i
) {
255 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
256 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
257 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
264 src_row
+= src_stride
;
269 util_format_rgtc2_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
271 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
274 for(y
= 0; y
< height
; y
+= bh
) {
275 uint8_t *dst
= dst_row
;
276 for(x
= 0; x
< width
; x
+= bw
) {
277 uint8_t tmp_r
[4][4]; /* [bh][bw] */
278 uint8_t tmp_g
[4][4]; /* [bh][bw] */
279 for(j
= 0; j
< bh
; ++j
) {
280 for(i
= 0; i
< bw
; ++i
) {
281 tmp_r
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
282 tmp_g
[j
][i
] = src_row
[((y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4) + 1];
285 u_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
286 u_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
287 dst
+= bytes_per_block
;
289 dst_row
+= dst_stride
/ sizeof(*dst_row
);
294 util_format_rxtc2_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
, unsigned chan2off
)
296 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
299 for(y
= 0; y
< height
; y
+= bh
) {
300 uint8_t *dst
= dst_row
;
301 for(x
= 0; x
< width
; x
+= bw
) {
302 uint8_t tmp_r
[4][4]; /* [bh][bw][comps] */
303 uint8_t tmp_g
[4][4]; /* [bh][bw][comps] */
304 for(j
= 0; j
< bh
; ++j
) {
305 for(i
= 0; i
< bw
; ++i
) {
306 tmp_r
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
307 tmp_g
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
310 u_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
311 u_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
312 dst
+= bytes_per_block
;
314 dst_row
+= dst_stride
/ sizeof(*dst_row
);
319 util_format_rgtc2_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
321 util_format_rxtc2_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
325 util_format_rgtc2_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
329 for(y
= 0; y
< height
; y
+= 4) {
330 const uint8_t *src
= src_row
;
331 for(x
= 0; x
< width
; x
+= 4) {
332 for(j
= 0; j
< 4; ++j
) {
333 for(i
= 0; i
< 4; ++i
) {
334 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
335 uint8_t tmp_r
, tmp_g
;
336 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
337 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
338 dst
[0] = ubyte_to_float(tmp_r
);
339 dst
[1] = ubyte_to_float(tmp_g
);
346 src_row
+= src_stride
;
351 util_format_rgtc2_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
353 uint8_t tmp_r
, tmp_g
;
354 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
355 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
356 dst
[0] = ubyte_to_float(tmp_r
);
357 dst
[1] = ubyte_to_float(tmp_g
);
364 util_format_rgtc2_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
366 fprintf(stderr
,"%s\n", __func__
);
370 util_format_rgtc2_snorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
372 fprintf(stderr
,"%s\n", __func__
);
376 util_format_rgtc2_snorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
378 fprintf(stderr
,"%s\n", __func__
);
382 util_format_rgtc2_snorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
386 for(y
= 0; y
< height
; y
+= 4) {
387 const int8_t *src
= (int8_t *)src_row
;
388 for(x
= 0; x
< width
; x
+= 4) {
389 for(j
= 0; j
< 4; ++j
) {
390 for(i
= 0; i
< 4; ++i
) {
391 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
393 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
394 u_format_signed_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
395 dst
[0] = byte_to_float_tex(tmp_r
);
396 dst
[1] = byte_to_float_tex(tmp_g
);
403 src_row
+= src_stride
;
408 util_format_rxtc2_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
, unsigned chan2off
)
410 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
413 for(y
= 0; y
< height
; y
+= bh
) {
414 int8_t *dst
= (int8_t *)dst_row
;
415 for(x
= 0; x
< width
; x
+= bw
) {
416 int8_t tmp_r
[4][4]; /* [bh][bw][comps] */
417 int8_t tmp_g
[4][4]; /* [bh][bw][comps] */
418 for(j
= 0; j
< bh
; ++j
) {
419 for(i
= 0; i
< bw
; ++i
) {
420 tmp_r
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
421 tmp_g
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
424 u_format_signed_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
425 u_format_signed_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
426 dst
+= bytes_per_block
;
428 dst_row
+= dst_stride
/ sizeof(*dst_row
);
433 util_format_rgtc2_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
435 util_format_rxtc2_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
439 util_format_rgtc2_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
442 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 2);
443 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
+ 8, i
, j
, &tmp_g
, 2);
444 dst
[0] = byte_to_float_tex(tmp_r
);
445 dst
[1] = byte_to_float_tex(tmp_g
);
451 #define TAG(x) u_format_unsigned_##x
456 #include "../../../mesa/main/texcompress_rgtc_tmp.h"
464 #define TAG(x) u_format_signed_##x
466 #define T_MIN (int8_t)-128
467 #define T_MAX (int8_t)127
469 #include "../../../mesa/main/texcompress_rgtc_tmp.h"