1 /**************************************************************************
2  *
3  * Copyright (C) 2011 Red Hat Inc.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
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.
22  *
23  **************************************************************************/
24 
25 #include <stdio.h>
26 #include "util/format/u_format.h"
27 #include "util/format/u_format_rgtc.h"
28 #include "util/format/u_format_latc.h"
29 #include "util/rgtc.h"
30 #include "util/u_math.h"
31 
32 void
util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t * restrict dst,const uint8_t * restrict src,unsigned i,unsigned j)33 util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t *restrict dst, const uint8_t *restrict src, unsigned i, unsigned j)
34 {
35    /* Fix warnings here: */
36    (void) util_format_unsigned_encode_rgtc_ubyte;
37    (void) util_format_signed_encode_rgtc_ubyte;
38 
39    util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1);
40    dst[1] = dst[0];
41    dst[2] = dst[0];
42    dst[3] = 255;
43 }
44 
45 void
util_format_latc1_unorm_unpack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)46 util_format_latc1_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
47 {
48    util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
49 }
50 
51 void
util_format_latc1_unorm_pack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)52 util_format_latc1_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row,
53 					 unsigned src_stride, unsigned width, unsigned height)
54 {
55    util_format_rgtc1_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
56 }
57 
58 void
util_format_latc1_unorm_unpack_rgba_float(void * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)59 util_format_latc1_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
60 {
61    unsigned x, y, i, j;
62    int block_size = 8;
63 
64    for(y = 0; y < height; y += 4) {
65       const uint8_t *src = src_row;
66       for(x = 0; x < width; x += 4) {
67          for(j = 0; j < 4; ++j) {
68             for(i = 0; i < 4; ++i) {
69                float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);
70                uint8_t tmp_r;
71                util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
72                dst[0] =
73                dst[1] =
74                dst[2] = ubyte_to_float(tmp_r);
75                dst[3] = 1.0;
76             }
77          }
78          src += block_size;
79       }
80       src_row += src_stride;
81    }
82 }
83 
84 void
util_format_latc1_unorm_pack_rgba_float(uint8_t * restrict dst_row,unsigned dst_stride,const float * restrict src_row,unsigned src_stride,unsigned width,unsigned height)85 util_format_latc1_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
86 {
87    util_format_rgtc1_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
88 }
89 
90 void
util_format_latc1_unorm_fetch_rgba(void * restrict in_dst,const uint8_t * restrict src,unsigned i,unsigned j)91 util_format_latc1_unorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)
92 {
93    float *dst = in_dst;
94    uint8_t tmp_r;
95 
96    util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
97    dst[0] =
98    dst[1] =
99    dst[2] = ubyte_to_float(tmp_r);
100    dst[3] = 1.0;
101 }
102 
103 void
util_format_latc1_snorm_fetch_rgba_8unorm(UNUSED uint8_t * restrict dst,UNUSED const uint8_t * restrict src,UNUSED unsigned i,UNUSED unsigned j)104 util_format_latc1_snorm_fetch_rgba_8unorm(UNUSED uint8_t *restrict dst, UNUSED const uint8_t *restrict src,
105                                           UNUSED unsigned i, UNUSED unsigned j)
106 {
107    fprintf(stderr,"%s\n", __func__);
108 }
109 
110 void
util_format_latc1_snorm_unpack_rgba_8unorm(UNUSED uint8_t * restrict dst_row,UNUSED unsigned dst_stride,UNUSED const uint8_t * restrict src_row,UNUSED unsigned src_stride,UNUSED unsigned width,UNUSED unsigned height)111 util_format_latc1_snorm_unpack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,
112                                            UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,
113                                            UNUSED unsigned width, UNUSED unsigned height)
114 {
115    fprintf(stderr,"%s\n", __func__);
116 }
117 
118 void
util_format_latc1_snorm_pack_rgba_8unorm(UNUSED uint8_t * restrict dst_row,UNUSED unsigned dst_stride,UNUSED const uint8_t * restrict src_row,UNUSED unsigned src_stride,UNUSED unsigned width,UNUSED unsigned height)119 util_format_latc1_snorm_pack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,
120                                          UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,
121                                          UNUSED unsigned width, UNUSED unsigned height)
122 {
123    fprintf(stderr,"%s\n", __func__);
124 }
125 
126 void
util_format_latc1_snorm_pack_rgba_float(uint8_t * restrict dst_row,unsigned dst_stride,const float * restrict src_row,unsigned src_stride,unsigned width,unsigned height)127 util_format_latc1_snorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
128 {
129    util_format_rgtc1_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
130 }
131 
132 void
util_format_latc1_snorm_unpack_rgba_float(void * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)133 util_format_latc1_snorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
134 {
135    unsigned x, y, i, j;
136    int block_size = 8;
137 
138    for(y = 0; y < height; y += 4) {
139       const int8_t *src = (int8_t *)src_row;
140       for(x = 0; x < width; x += 4) {
141          for(j = 0; j < 4; ++j) {
142             for(i = 0; i < 4; ++i) {
143                float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);
144                int8_t tmp_r;
145                util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
146                dst[0] =
147                dst[1] =
148                dst[2] = byte_to_float_tex(tmp_r);
149                dst[3] = 1.0;
150             }
151          }
152          src += block_size;
153       }
154       src_row += src_stride;
155    }
156 }
157 
158 void
util_format_latc1_snorm_fetch_rgba(void * restrict in_dst,const uint8_t * restrict src,unsigned i,unsigned j)159 util_format_latc1_snorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)
160 {
161    float *dst = in_dst;
162    int8_t tmp_r;
163 
164    util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 1);
165    dst[0] =
166    dst[1] =
167    dst[2] = byte_to_float_tex(tmp_r);
168    dst[3] = 1.0;
169 }
170 
171 
172 void
util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t * restrict dst,const uint8_t * restrict src,unsigned i,unsigned j)173 util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *restrict dst, const uint8_t *restrict src, unsigned i, unsigned j)
174 {
175    util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);
176    dst[1] = dst[0];
177    dst[2] = dst[0];
178    util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 3, 2);
179 }
180 
181 void
util_format_latc2_unorm_unpack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)182 util_format_latc2_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
183 {
184    util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
185 }
186 
187 void
util_format_latc2_unorm_pack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)188 util_format_latc2_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
189 {
190    util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
191 }
192 
193 void
util_format_latc2_unorm_pack_rgba_float(uint8_t * restrict dst_row,unsigned dst_stride,const float * restrict src_row,unsigned src_stride,unsigned width,unsigned height)194 util_format_latc2_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
195 {
196    util_format_rxtc2_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
197 }
198 
199 void
util_format_latc2_unorm_unpack_rgba_float(void * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)200 util_format_latc2_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
201 {
202    unsigned x, y, i, j;
203    int block_size = 16;
204 
205    for(y = 0; y < height; y += 4) {
206       const uint8_t *src = src_row;
207       for(x = 0; x < width; x += 4) {
208          for(j = 0; j < 4; ++j) {
209             for(i = 0; i < 4; ++i) {
210                float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);
211                uint8_t tmp_r, tmp_g;
212                util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
213                util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
214                dst[0] =
215                dst[1] =
216                dst[2] = ubyte_to_float(tmp_r);
217                dst[3] = ubyte_to_float(tmp_g);
218             }
219          }
220          src += block_size;
221       }
222       src_row += src_stride;
223    }
224 }
225 
226 void
util_format_latc2_unorm_fetch_rgba(void * restrict in_dst,const uint8_t * restrict src,unsigned i,unsigned j)227 util_format_latc2_unorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)
228 {
229    float *dst = in_dst;
230    uint8_t tmp_r, tmp_g;
231 
232    util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
233    util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
234    dst[0] =
235    dst[1] =
236    dst[2] = ubyte_to_float(tmp_r);
237    dst[3] = ubyte_to_float(tmp_g);
238 }
239 
240 
241 void
util_format_latc2_snorm_fetch_rgba_8unorm(UNUSED uint8_t * restrict dst,UNUSED const uint8_t * restrict src,UNUSED unsigned i,UNUSED unsigned j)242 util_format_latc2_snorm_fetch_rgba_8unorm(UNUSED uint8_t *restrict dst, UNUSED const uint8_t *restrict src,
243                                           UNUSED unsigned i, UNUSED unsigned j)
244 {
245    fprintf(stderr,"%s\n", __func__);
246 }
247 
248 void
util_format_latc2_snorm_unpack_rgba_8unorm(UNUSED uint8_t * restrict dst_row,UNUSED unsigned dst_stride,UNUSED const uint8_t * restrict src_row,UNUSED unsigned src_stride,UNUSED unsigned width,UNUSED unsigned height)249 util_format_latc2_snorm_unpack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,
250                                            UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,
251                                            UNUSED unsigned width, UNUSED unsigned height)
252 {
253    fprintf(stderr,"%s\n", __func__);
254 }
255 
256 void
util_format_latc2_snorm_pack_rgba_8unorm(UNUSED uint8_t * restrict dst_row,UNUSED unsigned dst_stride,UNUSED const uint8_t * restrict src_row,UNUSED unsigned src_stride,UNUSED unsigned width,UNUSED unsigned height)257 util_format_latc2_snorm_pack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,
258                                          UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,
259                                          UNUSED unsigned width, UNUSED unsigned height)
260 {
261    fprintf(stderr,"%s\n", __func__);
262 }
263 
264 void
util_format_latc2_snorm_unpack_rgba_float(void * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)265 util_format_latc2_snorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
266 {
267    unsigned x, y, i, j;
268    int block_size = 16;
269 
270    for(y = 0; y < height; y += 4) {
271       const int8_t *src = (int8_t *)src_row;
272       for(x = 0; x < width; x += 4) {
273          for(j = 0; j < 4; ++j) {
274             for(i = 0; i < 4; ++i) {
275                float *dst = (float *)(uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16;
276                int8_t tmp_r, tmp_g;
277                util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
278                util_format_signed_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
279                dst[0] =
280                dst[1] =
281                dst[2] = byte_to_float_tex(tmp_r);
282                dst[3] = byte_to_float_tex(tmp_g);
283             }
284          }
285          src += block_size;
286       }
287       src_row += src_stride;
288    }
289 }
290 
291 void
util_format_latc2_snorm_pack_rgba_float(uint8_t * restrict dst_row,unsigned dst_stride,const float * restrict src_row,unsigned src_stride,unsigned width,unsigned height)292 util_format_latc2_snorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)
293 {
294    util_format_rxtc2_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
295 }
296 
297 void
util_format_latc2_snorm_fetch_rgba(void * restrict in_dst,const uint8_t * restrict src,unsigned i,unsigned j)298 util_format_latc2_snorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)
299 {
300    float *dst = in_dst;
301    int8_t tmp_r, tmp_g;
302 
303    util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 2);
304    util_format_signed_fetch_texel_rgtc(0, (int8_t *)src + 8, i, j, &tmp_g, 2);
305    dst[0] =
306    dst[1] =
307    dst[2] = byte_to_float_tex(tmp_r);
308    dst[3] = byte_to_float_tex(tmp_g);
309 }
310 
311