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"
29 #include "u_format_latc.h"
31 static void u_format_unsigned_encode_rgtc_ubyte(uint8_t *blkaddr
, uint8_t srccolors
[4][4],
32 int numxpixels
, int numypixels
);
34 static void u_format_unsigned_fetch_texel_rgtc(unsigned srcRowStride
, const uint8_t *pixdata
,
35 unsigned i
, unsigned j
, uint8_t *value
, unsigned comps
);
37 static void u_format_signed_encode_rgtc_ubyte(int8_t *blkaddr
, int8_t srccolors
[4][4],
38 int numxpixels
, int numypixels
);
40 static void u_format_signed_fetch_texel_rgtc(unsigned srcRowStride
, const int8_t *pixdata
,
41 unsigned i
, unsigned j
, int8_t *value
, unsigned comps
);
44 util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
46 /* Fix warnings here: */
47 (void) u_format_unsigned_encode_rgtc_ubyte
;
48 (void) u_format_signed_encode_rgtc_ubyte
;
50 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
57 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
)
59 util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
63 util_format_latc1_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
,
64 unsigned src_stride
, unsigned width
, unsigned height
)
66 util_format_rgtc1_unorm_pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
70 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
)
75 for(y
= 0; y
< height
; y
+= 4) {
76 const uint8_t *src
= src_row
;
77 for(x
= 0; x
< width
; x
+= 4) {
78 for(j
= 0; j
< 4; ++j
) {
79 for(i
= 0; i
< 4; ++i
) {
80 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
82 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
85 dst
[2] = ubyte_to_float(tmp_r
);
91 src_row
+= src_stride
;
96 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
)
98 util_format_rgtc1_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
102 util_format_latc1_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
106 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
109 dst
[2] = ubyte_to_float(tmp_r
);
114 util_format_latc1_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
116 fprintf(stderr
,"%s\n", __func__
);
120 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
)
122 fprintf(stderr
,"%s\n", __func__
);
126 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
)
128 fprintf(stderr
,"%s\n", __func__
);
132 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
)
134 util_format_rgtc1_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
138 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
)
143 for(y
= 0; y
< height
; y
+= 4) {
144 const int8_t *src
= (int8_t *)src_row
;
145 for(x
= 0; x
< width
; x
+= 4) {
146 for(j
= 0; j
< 4; ++j
) {
147 for(i
= 0; i
< 4; ++i
) {
148 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
150 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
153 dst
[2] = byte_to_float_tex(tmp_r
);
159 src_row
+= src_stride
;
164 util_format_latc1_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
168 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 1);
171 dst
[2] = byte_to_float_tex(tmp_r
);
177 util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
179 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
182 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 3, 2);
186 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
)
188 util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
192 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
)
194 util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
);
198 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
)
200 util_format_rxtc2_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 3);
204 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
)
209 for(y
= 0; y
< height
; y
+= 4) {
210 const uint8_t *src
= src_row
;
211 for(x
= 0; x
< width
; x
+= 4) {
212 for(j
= 0; j
< 4; ++j
) {
213 for(i
= 0; i
< 4; ++i
) {
214 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
215 uint8_t tmp_r
, tmp_g
;
216 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
217 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
220 dst
[2] = ubyte_to_float(tmp_r
);
221 dst
[3] = ubyte_to_float(tmp_g
);
226 src_row
+= src_stride
;
231 util_format_latc2_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
233 uint8_t tmp_r
, tmp_g
;
235 u_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
236 u_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
239 dst
[2] = ubyte_to_float(tmp_r
);
240 dst
[3] = ubyte_to_float(tmp_g
);
245 util_format_latc2_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
247 fprintf(stderr
,"%s\n", __func__
);
251 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
)
253 fprintf(stderr
,"%s\n", __func__
);
257 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
)
259 fprintf(stderr
,"%s\n", __func__
);
263 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
)
268 for(y
= 0; y
< height
; y
+= 4) {
269 const int8_t *src
= (int8_t *)src_row
;
270 for(x
= 0; x
< width
; x
+= 4) {
271 for(j
= 0; j
< 4; ++j
) {
272 for(i
= 0; i
< 4; ++i
) {
273 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
275 u_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
276 u_format_signed_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
279 dst
[2] = byte_to_float_tex(tmp_r
);
280 dst
[3] = byte_to_float_tex(tmp_g
);
285 src_row
+= src_stride
;
290 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
)
292 util_format_rxtc2_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 3);
296 util_format_latc2_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
300 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 2);
301 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src
+ 8, i
, j
, &tmp_g
, 2);
304 dst
[2] = byte_to_float_tex(tmp_r
);
305 dst
[3] = byte_to_float_tex(tmp_g
);
309 #define TAG(x) u_format_unsigned_##x
314 #include "../../../mesa/main/texcompress_rgtc_tmp.h"
322 #define TAG(x) u_format_signed_##x
324 #define T_MIN (int8_t)-128
325 #define T_MAX (int8_t)127
327 #include "../../../mesa/main/texcompress_rgtc_tmp.h"