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_chan(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_chan(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);
48 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
)
50 const unsigned bw
= 4, bh
= 4, comps
= 4;
52 unsigned block_size
= 8;
54 for(y
= 0; y
< height
; y
+= bh
) {
55 const uint8_t *src
= src_row
;
56 for(x
= 0; x
< width
; x
+= bw
) {
57 for(j
= 0; j
< bh
; ++j
) {
58 for(i
= 0; i
< bw
; ++i
) {
59 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
60 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
65 src_row
+= src_stride
;
70 util_format_rgtc1_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
,
71 unsigned src_stride
, unsigned width
, unsigned height
)
73 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
76 for(y
= 0; y
< height
; y
+= bh
) {
77 uint8_t *dst
= dst_row
;
78 for(x
= 0; x
< width
; x
+= bw
) {
79 uint8_t tmp
[4][4]; /* [bh][bw][comps] */
80 for(j
= 0; j
< bh
; ++j
) {
81 for(i
= 0; i
< bw
; ++i
) {
82 tmp
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
85 u_format_unsigned_encode_rgtc_chan(dst
, tmp
, 4, 4);
86 dst
+= bytes_per_block
;
88 dst_row
+= dst_stride
/ sizeof(*dst_row
);
93 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
)
97 for(y
= 0; y
< height
; y
+= 4) {
98 const uint8_t *src
= src_row
;
99 for(x
= 0; x
< width
; x
+= 4) {
100 for(j
= 0; j
< 4; ++j
) {
101 for(i
= 0; i
< 4; ++i
) {
102 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
104 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
105 dst
[0] = ubyte_to_float(tmp_r
);
113 src_row
+= src_stride
;
118 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
)
120 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
123 for(y
= 0; y
< height
; y
+= bh
) {
124 uint8_t *dst
= dst_row
;
125 for(x
= 0; x
< width
; x
+= bw
) {
126 uint8_t tmp
[4][4]; /* [bh][bw][comps] */
127 for(j
= 0; j
< bh
; ++j
) {
128 for(i
= 0; i
< bw
; ++i
) {
129 tmp
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
132 u_format_unsigned_encode_rgtc_chan(dst
, tmp
, 4, 4);
133 dst
+= bytes_per_block
;
135 dst_row
+= dst_stride
/ sizeof(*dst_row
);
140 util_format_rgtc1_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
143 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
144 dst
[0] = ubyte_to_float(tmp_r
);
151 util_format_rgtc1_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
153 fprintf(stderr
,"%s\n", __func__
);
157 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
)
159 fprintf(stderr
,"%s\n", __func__
);
163 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
)
165 fprintf(stderr
,"%s\n", __func__
);
169 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
)
171 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
174 for(y
= 0; y
< height
; y
+= bh
) {
175 int8_t *dst
= (int8_t *)dst_row
;
176 for(x
= 0; x
< width
; x
+= bw
) {
177 int8_t tmp
[4][4]; /* [bh][bw][comps] */
178 for(j
= 0; j
< bh
; ++j
) {
179 for(i
= 0; i
< bw
; ++i
) {
180 tmp
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
183 u_format_signed_encode_rgtc_chan(dst
, tmp
, 4, 4);
184 dst
+= bytes_per_block
;
186 dst_row
+= dst_stride
/ sizeof(*dst_row
);
191 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
)
195 for(y
= 0; y
< height
; y
+= 4) {
196 const int8_t *src
= (int8_t *)src_row
;
197 for(x
= 0; x
< width
; x
+= 4) {
198 for(j
= 0; j
< 4; ++j
) {
199 for(i
= 0; i
< 4; ++i
) {
200 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
202 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
203 dst
[0] = byte_to_float_tex(tmp_r
);
211 src_row
+= src_stride
;
216 util_format_rgtc1_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
219 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 1);
220 dst
[0] = byte_to_float_tex(tmp_r
);
228 util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
230 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
231 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
235 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
)
237 const unsigned bw
= 4, bh
= 4, comps
= 4;
239 unsigned block_size
= 16;
241 for(y
= 0; y
< height
; y
+= bh
) {
242 const uint8_t *src
= src_row
;
243 for(x
= 0; x
< width
; x
+= bw
) {
244 for(j
= 0; j
< bh
; ++j
) {
245 for(i
= 0; i
< bw
; ++i
) {
246 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
247 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
248 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
254 src_row
+= src_stride
;
259 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
)
261 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
264 for(y
= 0; y
< height
; y
+= bh
) {
265 uint8_t *dst
= dst_row
;
266 for(x
= 0; x
< width
; x
+= bw
) {
267 uint8_t tmp_r
[4][4]; /* [bh][bw] */
268 uint8_t tmp_g
[4][4]; /* [bh][bw] */
269 for(j
= 0; j
< bh
; ++j
) {
270 for(i
= 0; i
< bw
; ++i
) {
271 tmp_r
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
272 tmp_g
[j
][i
] = src_row
[((y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4) + 1];
275 u_format_unsigned_encode_rgtc_chan(dst
, tmp_r
, 4, 4);
276 u_format_unsigned_encode_rgtc_chan(dst
+ 8, tmp_g
, 4, 4);
277 dst
+= bytes_per_block
;
279 dst_row
+= dst_stride
/ sizeof(*dst_row
);
284 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
)
286 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
289 for(y
= 0; y
< height
; y
+= bh
) {
290 uint8_t *dst
= dst_row
;
291 for(x
= 0; x
< width
; x
+= bw
) {
292 uint8_t tmp_r
[4][4]; /* [bh][bw][comps] */
293 uint8_t tmp_g
[4][4]; /* [bh][bw][comps] */
294 for(j
= 0; j
< bh
; ++j
) {
295 for(i
= 0; i
< bw
; ++i
) {
296 tmp_r
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
297 tmp_g
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + 1]);
300 u_format_unsigned_encode_rgtc_chan(dst
, tmp_r
, 4, 4);
301 u_format_unsigned_encode_rgtc_chan(dst
+ 8, tmp_g
, 4, 4);
302 dst
+= bytes_per_block
;
304 dst_row
+= dst_stride
/ sizeof(*dst_row
);
309 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
)
313 for(y
= 0; y
< height
; y
+= 4) {
314 const uint8_t *src
= src_row
;
315 for(x
= 0; x
< width
; x
+= 4) {
316 for(j
= 0; j
< 4; ++j
) {
317 for(i
= 0; i
< 4; ++i
) {
318 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
319 uint8_t tmp_r
, tmp_g
;
320 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
321 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
322 dst
[0] = ubyte_to_float(tmp_r
);
323 dst
[1] = ubyte_to_float(tmp_g
);
330 src_row
+= src_stride
;
335 util_format_rgtc2_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
337 uint8_t tmp_r
, tmp_g
;
338 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
339 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
340 dst
[0] = ubyte_to_float(tmp_r
);
341 dst
[1] = ubyte_to_float(tmp_g
);
348 util_format_rgtc2_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
350 fprintf(stderr
,"%s\n", __func__
);
354 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
)
356 fprintf(stderr
,"%s\n", __func__
);
360 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
)
362 fprintf(stderr
,"%s\n", __func__
);
366 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
)
370 for(y
= 0; y
< height
; y
+= 4) {
371 const int8_t *src
= (int8_t *)src_row
;
372 for(x
= 0; x
< width
; x
+= 4) {
373 for(j
= 0; j
< 4; ++j
) {
374 for(i
= 0; i
< 4; ++i
) {
375 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
377 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
378 u_format_signed_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
379 dst
[0] = byte_to_float_tex(tmp_r
);
380 dst
[1] = byte_to_float_tex(tmp_g
);
387 src_row
+= src_stride
;
392 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
)
394 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
397 for(y
= 0; y
< height
; y
+= bh
) {
398 int8_t *dst
= (int8_t *)dst_row
;
399 for(x
= 0; x
< width
; x
+= bw
) {
400 int8_t tmp_r
[4][4]; /* [bh][bw][comps] */
401 int8_t tmp_g
[4][4]; /* [bh][bw][comps] */
402 for(j
= 0; j
< bh
; ++j
) {
403 for(i
= 0; i
< bw
; ++i
) {
404 tmp_r
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
405 tmp_g
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + 1]);
408 u_format_signed_encode_rgtc_chan(dst
, tmp_r
, 4, 4);
409 u_format_signed_encode_rgtc_chan(dst
+ 8, tmp_g
, 4, 4);
410 dst
+= bytes_per_block
;
412 dst_row
+= dst_stride
/ sizeof(*dst_row
);
417 util_format_rgtc2_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
420 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 2);
421 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
+ 8, i
, j
, &tmp_g
, 2);
422 dst
[0] = byte_to_float_tex(tmp_r
);
423 dst
[1] = byte_to_float_tex(tmp_g
);
429 #define TAG(x) u_format_unsigned_##x
434 #include "../../../mesa/main/texcompress_rgtc_tmp.h"
442 #define TAG(x) u_format_signed_##x
444 #define T_MIN (int8_t)-128
445 #define T_MAX (int8_t)127
447 #include "../../../mesa/main/texcompress_rgtc_tmp.h"