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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 **************************************************************************/
27 #include "u_format_rgtc.h"
29 static void u_format_unsigned_encode_rgtc_ubyte(uint8_t *blkaddr
, uint8_t srccolors
[4][4],
30 int numxpixels
, int numypixels
);
32 static void u_format_unsigned_fetch_texel_rgtc(unsigned srcRowStride
, const uint8_t *pixdata
,
33 unsigned i
, unsigned j
, uint8_t *value
, unsigned comps
);
35 static void u_format_signed_encode_rgtc_ubyte(int8_t *blkaddr
, int8_t srccolors
[4][4],
36 int numxpixels
, int numypixels
);
38 static void u_format_signed_fetch_texel_rgtc(unsigned srcRowStride
, const int8_t *pixdata
,
39 unsigned i
, unsigned j
, int8_t *value
, unsigned comps
);
42 util_format_rgtc1_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
44 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
51 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
)
53 const unsigned bw
= 4, bh
= 4, comps
= 4;
55 unsigned block_size
= 8;
57 for(y
= 0; y
< height
; y
+= bh
) {
58 const uint8_t *src
= src_row
;
59 for(x
= 0; x
< width
; x
+= bw
) {
60 for(j
= 0; j
< bh
; ++j
) {
61 for(i
= 0; i
< bw
; ++i
) {
62 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
63 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
71 src_row
+= src_stride
;
76 util_format_rgtc1_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
,
77 unsigned src_stride
, unsigned width
, unsigned height
)
79 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
82 for(y
= 0; y
< height
; y
+= bh
) {
83 uint8_t *dst
= dst_row
;
84 for(x
= 0; x
< width
; x
+= bw
) {
85 uint8_t tmp
[4][4]; /* [bh][bw][comps] */
86 for(j
= 0; j
< bh
; ++j
) {
87 for(i
= 0; i
< bw
; ++i
) {
88 tmp
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
91 u_format_unsigned_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
92 dst
+= bytes_per_block
;
94 dst_row
+= dst_stride
/ sizeof(*dst_row
);
99 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
)
103 for(y
= 0; y
< height
; y
+= 4) {
104 const uint8_t *src
= src_row
;
105 for(x
= 0; x
< width
; x
+= 4) {
106 for(j
= 0; j
< 4; ++j
) {
107 for(i
= 0; i
< 4; ++i
) {
108 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
110 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
111 dst
[0] = ubyte_to_float(tmp_r
);
119 src_row
+= src_stride
;
124 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
)
126 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
129 for(y
= 0; y
< height
; y
+= bh
) {
130 uint8_t *dst
= dst_row
;
131 for(x
= 0; x
< width
; x
+= bw
) {
132 uint8_t tmp
[4][4]; /* [bh][bw][comps] */
133 for(j
= 0; j
< bh
; ++j
) {
134 for(i
= 0; i
< bw
; ++i
) {
135 tmp
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
138 u_format_unsigned_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
139 dst
+= bytes_per_block
;
141 dst_row
+= dst_stride
/ sizeof(*dst_row
);
146 util_format_rgtc1_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
149 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
150 dst
[0] = ubyte_to_float(tmp_r
);
157 util_format_rgtc1_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
159 fprintf(stderr
,"%s\n", __func__
);
163 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
)
165 fprintf(stderr
,"%s\n", __func__
);
169 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
)
171 fprintf(stderr
,"%s\n", __func__
);
175 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
)
177 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
180 for(y
= 0; y
< height
; y
+= bh
) {
181 int8_t *dst
= (int8_t *)dst_row
;
182 for(x
= 0; x
< width
; x
+= bw
) {
183 int8_t tmp
[4][4]; /* [bh][bw][comps] */
184 for(j
= 0; j
< bh
; ++j
) {
185 for(i
= 0; i
< bw
; ++i
) {
186 tmp
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
189 u_format_signed_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
190 dst
+= bytes_per_block
;
192 dst_row
+= dst_stride
/ sizeof(*dst_row
);
197 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
)
201 for(y
= 0; y
< height
; y
+= 4) {
202 const int8_t *src
= (int8_t *)src_row
;
203 for(x
= 0; x
< width
; x
+= 4) {
204 for(j
= 0; j
< 4; ++j
) {
205 for(i
= 0; i
< 4; ++i
) {
206 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
208 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
209 dst
[0] = byte_to_float_tex(tmp_r
);
217 src_row
+= src_stride
;
222 util_format_rgtc1_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
225 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 1);
226 dst
[0] = byte_to_float_tex(tmp_r
);
234 util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
236 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
237 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
243 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
)
245 const unsigned bw
= 4, bh
= 4, comps
= 4;
247 unsigned block_size
= 16;
249 for(y
= 0; y
< height
; y
+= bh
) {
250 const uint8_t *src
= src_row
;
251 for(x
= 0; x
< width
; x
+= bw
) {
252 for(j
= 0; j
< bh
; ++j
) {
253 for(i
= 0; i
< bw
; ++i
) {
254 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
255 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
256 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
263 src_row
+= src_stride
;
268 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
)
270 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
273 for(y
= 0; y
< height
; y
+= bh
) {
274 uint8_t *dst
= dst_row
;
275 for(x
= 0; x
< width
; x
+= bw
) {
276 uint8_t tmp_r
[4][4]; /* [bh][bw] */
277 uint8_t tmp_g
[4][4]; /* [bh][bw] */
278 for(j
= 0; j
< bh
; ++j
) {
279 for(i
= 0; i
< bw
; ++i
) {
280 tmp_r
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
281 tmp_g
[j
][i
] = src_row
[((y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4) + 1];
284 u_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
285 u_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
286 dst
+= bytes_per_block
;
288 dst_row
+= dst_stride
/ sizeof(*dst_row
);
293 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
)
295 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
298 for(y
= 0; y
< height
; y
+= bh
) {
299 uint8_t *dst
= dst_row
;
300 for(x
= 0; x
< width
; x
+= bw
) {
301 uint8_t tmp_r
[4][4]; /* [bh][bw][comps] */
302 uint8_t tmp_g
[4][4]; /* [bh][bw][comps] */
303 for(j
= 0; j
< bh
; ++j
) {
304 for(i
= 0; i
< bw
; ++i
) {
305 tmp_r
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
306 tmp_g
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
309 u_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
310 u_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
311 dst
+= bytes_per_block
;
313 dst_row
+= dst_stride
/ sizeof(*dst_row
);
318 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
)
320 util_format_rxtc2_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
324 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
)
328 for(y
= 0; y
< height
; y
+= 4) {
329 const uint8_t *src
= src_row
;
330 for(x
= 0; x
< width
; x
+= 4) {
331 for(j
= 0; j
< 4; ++j
) {
332 for(i
= 0; i
< 4; ++i
) {
333 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
334 uint8_t tmp_r
, tmp_g
;
335 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
336 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
337 dst
[0] = ubyte_to_float(tmp_r
);
338 dst
[1] = ubyte_to_float(tmp_g
);
345 src_row
+= src_stride
;
350 util_format_rgtc2_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
352 uint8_t tmp_r
, tmp_g
;
353 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
354 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
355 dst
[0] = ubyte_to_float(tmp_r
);
356 dst
[1] = ubyte_to_float(tmp_g
);
363 util_format_rgtc2_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
365 fprintf(stderr
,"%s\n", __func__
);
369 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
)
371 fprintf(stderr
,"%s\n", __func__
);
375 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
)
377 fprintf(stderr
,"%s\n", __func__
);
381 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
)
385 for(y
= 0; y
< height
; y
+= 4) {
386 const int8_t *src
= (int8_t *)src_row
;
387 for(x
= 0; x
< width
; x
+= 4) {
388 for(j
= 0; j
< 4; ++j
) {
389 for(i
= 0; i
< 4; ++i
) {
390 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
392 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
393 u_format_signed_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
394 dst
[0] = byte_to_float_tex(tmp_r
);
395 dst
[1] = byte_to_float_tex(tmp_g
);
402 src_row
+= src_stride
;
407 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
)
409 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
412 for(y
= 0; y
< height
; y
+= bh
) {
413 int8_t *dst
= (int8_t *)dst_row
;
414 for(x
= 0; x
< width
; x
+= bw
) {
415 int8_t tmp_r
[4][4]; /* [bh][bw][comps] */
416 int8_t tmp_g
[4][4]; /* [bh][bw][comps] */
417 for(j
= 0; j
< bh
; ++j
) {
418 for(i
= 0; i
< bw
; ++i
) {
419 tmp_r
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
420 tmp_g
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
423 u_format_signed_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
424 u_format_signed_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
425 dst
+= bytes_per_block
;
427 dst_row
+= dst_stride
/ sizeof(*dst_row
);
432 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
)
434 util_format_rxtc2_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
438 util_format_rgtc2_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
441 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 2);
442 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
+ 8, i
, j
, &tmp_g
, 2);
443 dst
[0] = byte_to_float_tex(tmp_r
);
444 dst
[1] = byte_to_float_tex(tmp_g
);
450 #define TAG(x) u_format_unsigned_##x
455 #include "../../../mesa/main/texcompress_rgtc_tmp.h"
463 #define TAG(x) u_format_signed_##x
465 #define T_MIN (int8_t)-128
466 #define T_MAX (int8_t)127
468 #include "../../../mesa/main/texcompress_rgtc_tmp.h"