u_format_latc.c revision b4e6afbf7715df7473723f3e0c5d714cd5721802
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 * 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.
21 *
22 **************************************************************************/
23
24#include <stdio.h>
25#include "u_math.h"
26#include "u_format.h"
27#include "u_format_rgtc.h"
28#include "u_format_latc.h"
29
30static void u_format_unsigned_encode_rgtc_ubyte(uint8_t *blkaddr, uint8_t srccolors[4][4],
31					       int numxpixels, int numypixels);
32
33static void u_format_unsigned_fetch_texel_rgtc(unsigned srcRowStride, const uint8_t *pixdata,
34					       unsigned i, unsigned j, uint8_t *value, unsigned comps);
35
36static void u_format_signed_encode_rgtc_ubyte(int8_t *blkaddr, int8_t srccolors[4][4],
37					     int numxpixels, int numypixels);
38
39static void u_format_signed_fetch_texel_rgtc(unsigned srcRowStride, const int8_t *pixdata,
40					       unsigned i, unsigned j, int8_t *value, unsigned comps);
41
42void
43util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
44{
45   /* Fix warnings here: */
46   (void) u_format_unsigned_encode_rgtc_ubyte;
47   (void) u_format_signed_encode_rgtc_ubyte;
48
49   u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1);
50}
51
52void
53util_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)
54{
55   util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
56}
57
58void
59util_format_latc1_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row,
60					 unsigned src_stride, unsigned width, unsigned height)
61{
62   util_format_rgtc1_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
63}
64
65void
66util_format_latc1_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
67{
68   unsigned x, y, i, j;
69   int block_size = 8;
70
71   for(y = 0; y < height; y += 4) {
72      const uint8_t *src = src_row;
73      for(x = 0; x < width; x += 4) {
74         for(j = 0; j < 4; ++j) {
75            for(i = 0; i < 4; ++i) {
76               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
77               uint8_t tmp_r;
78               u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
79               dst[0] =
80               dst[1] =
81               dst[2] = ubyte_to_float(tmp_r);
82               dst[3] = 1.0;
83            }
84         }
85         src += block_size;
86      }
87      src_row += src_stride;
88   }
89}
90
91void
92util_format_latc1_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
93{
94   util_format_rgtc1_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
95}
96
97void
98util_format_latc1_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
99{
100   uint8_t tmp_r;
101
102   u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
103   dst[0] =
104   dst[1] =
105   dst[2] = ubyte_to_float(tmp_r);
106   dst[3] = 1.0;
107}
108
109void
110util_format_latc1_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
111{
112   fprintf(stderr,"%s\n", __func__);
113}
114
115void
116util_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)
117{
118   fprintf(stderr,"%s\n", __func__);
119}
120
121void
122util_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)
123{
124   fprintf(stderr,"%s\n", __func__);
125}
126
127void
128util_format_latc1_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
129{
130   util_format_rgtc1_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
131}
132
133void
134util_format_latc1_snorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
135{
136   unsigned x, y, i, j;
137   int block_size = 8;
138
139   for(y = 0; y < height; y += 4) {
140      const int8_t *src = (int8_t *)src_row;
141      for(x = 0; x < width; x += 4) {
142         for(j = 0; j < 4; ++j) {
143            for(i = 0; i < 4; ++i) {
144               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
145               int8_t tmp_r;
146               u_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
147               dst[0] =
148               dst[1] =
149               dst[2] = byte_to_float_tex(tmp_r);
150               dst[3] = 1.0;
151            }
152         }
153         src += block_size;
154      }
155      src_row += src_stride;
156   }
157}
158
159void
160util_format_latc1_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
161{
162   int8_t tmp_r;
163
164   u_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
172void
173util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
174{
175   puts(__func__);
176
177   u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);
178   u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 1, 2);
179}
180
181void
182util_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)
183{
184   util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
185}
186
187void
188util_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)
189{
190   util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
191}
192
193void
194util_format_latc2_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *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
199void
200util_format_latc2_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *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 = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
211               uint8_t tmp_r, tmp_g;
212               u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
213               u_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
226void
227util_format_latc2_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
228{
229   uint8_t tmp_r, tmp_g;
230
231   u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
232   u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
233   dst[0] =
234   dst[1] =
235   dst[2] = ubyte_to_float(tmp_r);
236   dst[3] = ubyte_to_float(tmp_g);
237}
238
239
240void
241util_format_latc2_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
242{
243   fprintf(stderr,"%s\n", __func__);
244}
245
246void
247util_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)
248{
249   fprintf(stderr,"%s\n", __func__);
250}
251
252void
253util_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)
254{
255   fprintf(stderr,"%s\n", __func__);
256}
257
258void
259util_format_latc2_snorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
260{
261   unsigned x, y, i, j;
262   int block_size = 16;
263
264   for(y = 0; y < height; y += 4) {
265      const int8_t *src = (int8_t *)src_row;
266      for(x = 0; x < width; x += 4) {
267         for(j = 0; j < 4; ++j) {
268            for(i = 0; i < 4; ++i) {
269               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
270               int8_t tmp_r, tmp_g;
271               u_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
272               u_format_signed_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
273               dst[0] =
274               dst[1] =
275               dst[2] = byte_to_float_tex(tmp_r);
276               dst[3] = byte_to_float_tex(tmp_g);
277            }
278         }
279         src += block_size;
280      }
281      src_row += src_stride;
282   }
283}
284
285void
286util_format_latc2_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
287{
288   util_format_rxtc2_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
289}
290
291void
292util_format_latc2_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
293{
294   int8_t tmp_r, tmp_g;
295
296   u_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 2);
297   u_format_signed_fetch_texel_rgtc(0, (int8_t *)src + 8, i, j, &tmp_g, 2);
298   dst[0] =
299   dst[1] =
300   dst[2] = byte_to_float_tex(tmp_r);
301   dst[3] = byte_to_float_tex(tmp_g);
302}
303
304
305#define TAG(x) u_format_unsigned_##x
306#define TYPE uint8_t
307#define T_MIN 0
308#define T_MAX 255
309
310#include "../../../mesa/main/texcompress_rgtc_tmp.h"
311
312#undef TYPE
313#undef TAG
314#undef T_MIN
315#undef T_MAX
316
317
318#define TAG(x) u_format_signed_##x
319#define TYPE int8_t
320#define T_MIN (int8_t)-128
321#define T_MAX (int8_t)127
322
323#include "../../../mesa/main/texcompress_rgtc_tmp.h"
324
325#undef TYPE
326#undef TAG
327#undef T_MIN
328#undef T_MAX
329