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"
28 #include "u_format_latc.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_latc1_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
45 /* Fix warnings here: */
46 (void) u_format_unsigned_encode_rgtc_ubyte
;
47 (void) u_format_signed_encode_rgtc_ubyte
;
49 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
56 util_format_latc1_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
58 util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
62 util_format_latc1_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
,
63 unsigned src_stride
, unsigned width
, unsigned height
)
65 util_format_rgtc1_unorm_pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
69 util_format_latc1_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
74 for(y
= 0; y
< height
; y
+= 4) {
75 const uint8_t *src
= src_row
;
76 for(x
= 0; x
< width
; x
+= 4) {
77 for(j
= 0; j
< 4; ++j
) {
78 for(i
= 0; i
< 4; ++i
) {
79 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
81 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
84 dst
[2] = ubyte_to_float(tmp_r
);
90 src_row
+= src_stride
;
95 util_format_latc1_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
97 util_format_rgtc1_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
101 util_format_latc1_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
105 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
108 dst
[2] = ubyte_to_float(tmp_r
);
113 util_format_latc1_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
115 fprintf(stderr
,"%s\n", __func__
);
119 util_format_latc1_snorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
121 fprintf(stderr
,"%s\n", __func__
);
125 util_format_latc1_snorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
127 fprintf(stderr
,"%s\n", __func__
);
131 util_format_latc1_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
133 util_format_rgtc1_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
137 util_format_latc1_snorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
142 for(y
= 0; y
< height
; y
+= 4) {
143 const int8_t *src
= (int8_t *)src_row
;
144 for(x
= 0; x
< width
; x
+= 4) {
145 for(j
= 0; j
< 4; ++j
) {
146 for(i
= 0; i
< 4; ++i
) {
147 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
149 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
152 dst
[2] = byte_to_float_tex(tmp_r
);
158 src_row
+= src_stride
;
163 util_format_latc1_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
167 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 1);
170 dst
[2] = byte_to_float_tex(tmp_r
);
176 util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
178 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
181 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 3, 2);
185 util_format_latc2_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
187 util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
191 util_format_latc2_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
193 util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
197 util_format_latc2_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
199 util_format_rxtc2_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 3);
203 util_format_latc2_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
208 for(y
= 0; y
< height
; y
+= 4) {
209 const uint8_t *src
= src_row
;
210 for(x
= 0; x
< width
; x
+= 4) {
211 for(j
= 0; j
< 4; ++j
) {
212 for(i
= 0; i
< 4; ++i
) {
213 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
214 uint8_t tmp_r
, tmp_g
;
215 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
216 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
219 dst
[2] = ubyte_to_float(tmp_r
);
220 dst
[3] = ubyte_to_float(tmp_g
);
225 src_row
+= src_stride
;
230 util_format_latc2_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
232 uint8_t tmp_r
, tmp_g
;
234 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
235 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
238 dst
[2] = ubyte_to_float(tmp_r
);
239 dst
[3] = ubyte_to_float(tmp_g
);
244 util_format_latc2_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
246 fprintf(stderr
,"%s\n", __func__
);
250 util_format_latc2_snorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
252 fprintf(stderr
,"%s\n", __func__
);
256 util_format_latc2_snorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
258 fprintf(stderr
,"%s\n", __func__
);
262 util_format_latc2_snorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
267 for(y
= 0; y
< height
; y
+= 4) {
268 const int8_t *src
= (int8_t *)src_row
;
269 for(x
= 0; x
< width
; x
+= 4) {
270 for(j
= 0; j
< 4; ++j
) {
271 for(i
= 0; i
< 4; ++i
) {
272 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
274 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
275 u_format_signed_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
278 dst
[2] = byte_to_float_tex(tmp_r
);
279 dst
[3] = byte_to_float_tex(tmp_g
);
284 src_row
+= src_stride
;
289 util_format_latc2_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
291 util_format_rxtc2_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 3);
295 util_format_latc2_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
299 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 2);
300 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
+ 8, i
, j
, &tmp_g
, 2);
303 dst
[2] = byte_to_float_tex(tmp_r
);
304 dst
[3] = byte_to_float_tex(tmp_g
);
308 #define TAG(x) u_format_unsigned_##x
313 #include "../../../mesa/main/texcompress_rgtc_tmp.h"
321 #define TAG(x) u_format_signed_##x
323 #define T_MIN (int8_t)-128
324 #define T_MAX (int8_t)127
326 #include "../../../mesa/main/texcompress_rgtc_tmp.h"