util: move shared rgtc code to util (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 #include "util/rgtc.h"
31
32 void
33 util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *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
46 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)
47 {
48 util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
49 }
50
51 void
52 util_format_latc1_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *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
59 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)
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 = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
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
85 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)
86 {
87 util_format_rgtc1_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
88 }
89
90 void
91 util_format_latc1_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
92 {
93 uint8_t tmp_r;
94
95 util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
96 dst[0] =
97 dst[1] =
98 dst[2] = ubyte_to_float(tmp_r);
99 dst[3] = 1.0;
100 }
101
102 void
103 util_format_latc1_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
104 {
105 fprintf(stderr,"%s\n", __func__);
106 }
107
108 void
109 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)
110 {
111 fprintf(stderr,"%s\n", __func__);
112 }
113
114 void
115 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)
116 {
117 fprintf(stderr,"%s\n", __func__);
118 }
119
120 void
121 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)
122 {
123 util_format_rgtc1_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
124 }
125
126 void
127 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)
128 {
129 unsigned x, y, i, j;
130 int block_size = 8;
131
132 for(y = 0; y < height; y += 4) {
133 const int8_t *src = (int8_t *)src_row;
134 for(x = 0; x < width; x += 4) {
135 for(j = 0; j < 4; ++j) {
136 for(i = 0; i < 4; ++i) {
137 float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
138 int8_t tmp_r;
139 util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
140 dst[0] =
141 dst[1] =
142 dst[2] = byte_to_float_tex(tmp_r);
143 dst[3] = 1.0;
144 }
145 }
146 src += block_size;
147 }
148 src_row += src_stride;
149 }
150 }
151
152 void
153 util_format_latc1_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
154 {
155 int8_t tmp_r;
156
157 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 1);
158 dst[0] =
159 dst[1] =
160 dst[2] = byte_to_float_tex(tmp_r);
161 dst[3] = 1.0;
162 }
163
164
165 void
166 util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
167 {
168 util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);
169 dst[1] = dst[0];
170 dst[2] = dst[0];
171 util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 3, 2);
172 }
173
174 void
175 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)
176 {
177 util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
178 }
179
180 void
181 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)
182 {
183 util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
184 }
185
186 void
187 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)
188 {
189 util_format_rxtc2_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
190 }
191
192 void
193 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)
194 {
195 unsigned x, y, i, j;
196 int block_size = 16;
197
198 for(y = 0; y < height; y += 4) {
199 const uint8_t *src = src_row;
200 for(x = 0; x < width; x += 4) {
201 for(j = 0; j < 4; ++j) {
202 for(i = 0; i < 4; ++i) {
203 float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
204 uint8_t tmp_r, tmp_g;
205 util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
206 util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
207 dst[0] =
208 dst[1] =
209 dst[2] = ubyte_to_float(tmp_r);
210 dst[3] = ubyte_to_float(tmp_g);
211 }
212 }
213 src += block_size;
214 }
215 src_row += src_stride;
216 }
217 }
218
219 void
220 util_format_latc2_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
221 {
222 uint8_t tmp_r, tmp_g;
223
224 util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
225 util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
226 dst[0] =
227 dst[1] =
228 dst[2] = ubyte_to_float(tmp_r);
229 dst[3] = ubyte_to_float(tmp_g);
230 }
231
232
233 void
234 util_format_latc2_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
235 {
236 fprintf(stderr,"%s\n", __func__);
237 }
238
239 void
240 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)
241 {
242 fprintf(stderr,"%s\n", __func__);
243 }
244
245 void
246 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)
247 {
248 fprintf(stderr,"%s\n", __func__);
249 }
250
251 void
252 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)
253 {
254 unsigned x, y, i, j;
255 int block_size = 16;
256
257 for(y = 0; y < height; y += 4) {
258 const int8_t *src = (int8_t *)src_row;
259 for(x = 0; x < width; x += 4) {
260 for(j = 0; j < 4; ++j) {
261 for(i = 0; i < 4; ++i) {
262 float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
263 int8_t tmp_r, tmp_g;
264 util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
265 util_format_signed_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
266 dst[0] =
267 dst[1] =
268 dst[2] = byte_to_float_tex(tmp_r);
269 dst[3] = byte_to_float_tex(tmp_g);
270 }
271 }
272 src += block_size;
273 }
274 src_row += src_stride;
275 }
276 }
277
278 void
279 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)
280 {
281 util_format_rxtc2_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
282 }
283
284 void
285 util_format_latc2_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
286 {
287 int8_t tmp_r, tmp_g;
288
289 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 2);
290 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src + 8, i, j, &tmp_g, 2);
291 dst[0] =
292 dst[1] =
293 dst[2] = byte_to_float_tex(tmp_r);
294 dst[3] = byte_to_float_tex(tmp_g);
295 }
296