configure.ac: Use AX_GCC_BUILTIN to check availability of __builtin_bswap32 v2
[mesa.git] / src / gallium / auxiliary / util / u_format_latc.c
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 "u_math.h"
27 #include "u_format.h"
28 #include "u_format_rgtc.h"
29 #include "u_format_latc.h"
30
31 static void u_format_unsigned_encode_rgtc_ubyte(uint8_t *blkaddr, uint8_t srccolors[4][4],
32 int numxpixels, int numypixels);
33
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);
36
37 static void u_format_signed_encode_rgtc_ubyte(int8_t *blkaddr, int8_t srccolors[4][4],
38 int numxpixels, int numypixels);
39
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);
42
43 void
44 util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
45 {
46 /* Fix warnings here: */
47 (void) u_format_unsigned_encode_rgtc_ubyte;
48 (void) u_format_signed_encode_rgtc_ubyte;
49
50 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1);
51 dst[1] = dst[0];
52 dst[2] = dst[0];
53 dst[3] = 255;
54 }
55
56 void
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)
58 {
59 util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
60 }
61
62 void
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)
65 {
66 util_format_rgtc1_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
67 }
68
69 void
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)
71 {
72 unsigned x, y, i, j;
73 int block_size = 8;
74
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;
81 uint8_t tmp_r;
82 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
83 dst[0] =
84 dst[1] =
85 dst[2] = ubyte_to_float(tmp_r);
86 dst[3] = 1.0;
87 }
88 }
89 src += block_size;
90 }
91 src_row += src_stride;
92 }
93 }
94
95 void
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)
97 {
98 util_format_rgtc1_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
99 }
100
101 void
102 util_format_latc1_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
103 {
104 uint8_t tmp_r;
105
106 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
107 dst[0] =
108 dst[1] =
109 dst[2] = ubyte_to_float(tmp_r);
110 dst[3] = 1.0;
111 }
112
113 void
114 util_format_latc1_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
115 {
116 fprintf(stderr,"%s\n", __func__);
117 }
118
119 void
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)
121 {
122 fprintf(stderr,"%s\n", __func__);
123 }
124
125 void
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)
127 {
128 fprintf(stderr,"%s\n", __func__);
129 }
130
131 void
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)
133 {
134 util_format_rgtc1_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
135 }
136
137 void
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)
139 {
140 unsigned x, y, i, j;
141 int block_size = 8;
142
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;
149 int8_t tmp_r;
150 u_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
151 dst[0] =
152 dst[1] =
153 dst[2] = byte_to_float_tex(tmp_r);
154 dst[3] = 1.0;
155 }
156 }
157 src += block_size;
158 }
159 src_row += src_stride;
160 }
161 }
162
163 void
164 util_format_latc1_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
165 {
166 int8_t tmp_r;
167
168 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 1);
169 dst[0] =
170 dst[1] =
171 dst[2] = byte_to_float_tex(tmp_r);
172 dst[3] = 1.0;
173 }
174
175
176 void
177 util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
178 {
179 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);
180 dst[1] = dst[0];
181 dst[2] = dst[0];
182 u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 3, 2);
183 }
184
185 void
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)
187 {
188 util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
189 }
190
191 void
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)
193 {
194 util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
195 }
196
197 void
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)
199 {
200 util_format_rxtc2_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
201 }
202
203 void
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)
205 {
206 unsigned x, y, i, j;
207 int block_size = 16;
208
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);
218 dst[0] =
219 dst[1] =
220 dst[2] = ubyte_to_float(tmp_r);
221 dst[3] = ubyte_to_float(tmp_g);
222 }
223 }
224 src += block_size;
225 }
226 src_row += src_stride;
227 }
228 }
229
230 void
231 util_format_latc2_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
232 {
233 uint8_t tmp_r, tmp_g;
234
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);
237 dst[0] =
238 dst[1] =
239 dst[2] = ubyte_to_float(tmp_r);
240 dst[3] = ubyte_to_float(tmp_g);
241 }
242
243
244 void
245 util_format_latc2_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
246 {
247 fprintf(stderr,"%s\n", __func__);
248 }
249
250 void
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)
252 {
253 fprintf(stderr,"%s\n", __func__);
254 }
255
256 void
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)
258 {
259 fprintf(stderr,"%s\n", __func__);
260 }
261
262 void
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)
264 {
265 unsigned x, y, i, j;
266 int block_size = 16;
267
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;
274 int8_t tmp_r, tmp_g;
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);
277 dst[0] =
278 dst[1] =
279 dst[2] = byte_to_float_tex(tmp_r);
280 dst[3] = byte_to_float_tex(tmp_g);
281 }
282 }
283 src += block_size;
284 }
285 src_row += src_stride;
286 }
287 }
288
289 void
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)
291 {
292 util_format_rxtc2_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
293 }
294
295 void
296 util_format_latc2_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
297 {
298 int8_t tmp_r, tmp_g;
299
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);
302 dst[0] =
303 dst[1] =
304 dst[2] = byte_to_float_tex(tmp_r);
305 dst[3] = byte_to_float_tex(tmp_g);
306 }
307
308
309 #define TAG(x) u_format_unsigned_##x
310 #define TYPE uint8_t
311 #define T_MIN 0
312 #define T_MAX 255
313
314 #include "../../../mesa/main/texcompress_rgtc_tmp.h"
315
316 #undef TYPE
317 #undef TAG
318 #undef T_MIN
319 #undef T_MAX
320
321
322 #define TAG(x) u_format_signed_##x
323 #define TYPE int8_t
324 #define T_MIN (int8_t)-128
325 #define T_MAX (int8_t)127
326
327 #include "../../../mesa/main/texcompress_rgtc_tmp.h"
328
329 #undef TYPE
330 #undef TAG
331 #undef T_MIN
332 #undef T_MAX