6dd66c7d346d87eadb7150104ccb8cbf9a8ffc86
[mesa.git] / src / util / format / u_format_other.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 #include "util/format/u_format_other.h"
30 #include "util/u_math.h"
31 #include "util/format_rgb9e5.h"
32 #include "util/format_r11g11b10f.h"
33
34
35 void
36 util_format_r9g9b9e5_float_unpack_rgba_float(void *dst_row, unsigned dst_stride,
37 const uint8_t *src_row, unsigned src_stride,
38 unsigned width, unsigned height)
39 {
40 unsigned x, y;
41 for(y = 0; y < height; y += 1) {
42 float *dst = dst_row;
43 const uint8_t *src = src_row;
44 for(x = 0; x < width; x += 1) {
45 uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
46 rgb9e5_to_float3(value, dst);
47 dst[3] = 1; /* a */
48 src += 4;
49 dst += 4;
50 }
51 src_row += src_stride;
52 dst_row = (uint8_t *)dst_row + dst_stride;
53 }
54 }
55
56 void
57 util_format_r9g9b9e5_float_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
58 const float *src_row, unsigned src_stride,
59 unsigned width, unsigned height)
60 {
61 unsigned x, y;
62 for(y = 0; y < height; y += 1) {
63 const float *src = src_row;
64 uint8_t *dst = dst_row;
65 for(x = 0; x < width; x += 1) {
66 uint32_t value = util_cpu_to_le32(float3_to_rgb9e5(src));
67 *(uint32_t *)dst = value;
68 src += 4;
69 dst += 4;
70 }
71 dst_row += dst_stride;
72 src_row += src_stride/sizeof(*src_row);
73 }
74 }
75
76 void
77 util_format_r9g9b9e5_float_fetch_rgba_float(float *dst, const uint8_t *src,
78 UNUSED unsigned i, UNUSED unsigned j)
79 {
80 uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
81 rgb9e5_to_float3(value, dst);
82 dst[3] = 1; /* a */
83 }
84
85
86 void
87 util_format_r9g9b9e5_float_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
88 const uint8_t *src_row, unsigned src_stride,
89 unsigned width, unsigned height)
90 {
91 unsigned x, y;
92 float p[3];
93 for(y = 0; y < height; y += 1) {
94 uint8_t *dst = dst_row;
95 const uint8_t *src = src_row;
96 for(x = 0; x < width; x += 1) {
97 uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
98 rgb9e5_to_float3(value, p);
99 dst[0] = float_to_ubyte(p[0]); /* r */
100 dst[1] = float_to_ubyte(p[1]); /* g */
101 dst[2] = float_to_ubyte(p[2]); /* b */
102 dst[3] = 255; /* a */
103 src += 4;
104 dst += 4;
105 }
106 src_row += src_stride;
107 dst_row += dst_stride/sizeof(*dst_row);
108 }
109 }
110
111
112 void
113 util_format_r9g9b9e5_float_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
114 const uint8_t *src_row, unsigned src_stride,
115 unsigned width, unsigned height)
116 {
117 unsigned x, y;
118 float p[3];
119 for(y = 0; y < height; y += 1) {
120 const uint8_t *src = src_row;
121 uint8_t *dst = dst_row;
122 for(x = 0; x < width; x += 1) {
123 uint32_t value;
124 p[0] = ubyte_to_float(src[0]);
125 p[1] = ubyte_to_float(src[1]);
126 p[2] = ubyte_to_float(src[2]);
127 value = util_cpu_to_le32(float3_to_rgb9e5(p));
128 *(uint32_t *)dst = value;
129 src += 4;
130 dst += 4;
131 }
132 dst_row += dst_stride;
133 src_row += src_stride/sizeof(*src_row);
134 }
135 }
136
137
138 void
139 util_format_r11g11b10_float_unpack_rgba_float(void *dst_row, unsigned dst_stride,
140 const uint8_t *src_row, unsigned src_stride,
141 unsigned width, unsigned height)
142 {
143 unsigned x, y;
144 for(y = 0; y < height; y += 1) {
145 float *dst = dst_row;
146 const uint8_t *src = src_row;
147 for(x = 0; x < width; x += 1) {
148 uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
149 r11g11b10f_to_float3(value, dst);
150 dst[3] = 1; /* a */
151 src += 4;
152 dst += 4;
153 }
154 src_row += src_stride;
155 dst_row = (uint8_t *)dst_row + dst_stride;
156 }
157 }
158
159 void
160 util_format_r11g11b10_float_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
161 const float *src_row, unsigned src_stride,
162 unsigned width, unsigned height)
163 {
164 unsigned x, y;
165 for(y = 0; y < height; y += 1) {
166 const float *src = src_row;
167 uint8_t *dst = dst_row;
168 for(x = 0; x < width; x += 1) {
169 uint32_t value = util_cpu_to_le32(float3_to_r11g11b10f(src));
170 *(uint32_t *)dst = value;
171 src += 4;
172 dst += 4;
173 }
174 dst_row += dst_stride;
175 src_row += src_stride/sizeof(*src_row);
176 }
177 }
178
179 void
180 util_format_r11g11b10_float_fetch_rgba_float(float *dst, const uint8_t *src,
181 UNUSED unsigned i, UNUSED unsigned j)
182 {
183 uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
184 r11g11b10f_to_float3(value, dst);
185 dst[3] = 1; /* a */
186 }
187
188
189 void
190 util_format_r11g11b10_float_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
191 const uint8_t *src_row, unsigned src_stride,
192 unsigned width, unsigned height)
193 {
194 unsigned x, y;
195 float p[3];
196 for(y = 0; y < height; y += 1) {
197 uint8_t *dst = dst_row;
198 const uint8_t *src = src_row;
199 for(x = 0; x < width; x += 1) {
200 uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
201 r11g11b10f_to_float3(value, p);
202 dst[0] = float_to_ubyte(p[0]); /* r */
203 dst[1] = float_to_ubyte(p[1]); /* g */
204 dst[2] = float_to_ubyte(p[2]); /* b */
205 dst[3] = 255; /* a */
206 src += 4;
207 dst += 4;
208 }
209 src_row += src_stride;
210 dst_row += dst_stride/sizeof(*dst_row);
211 }
212 }
213
214
215 void
216 util_format_r11g11b10_float_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
217 const uint8_t *src_row, unsigned src_stride,
218 unsigned width, unsigned height)
219 {
220 unsigned x, y;
221 float p[3];
222 for(y = 0; y < height; y += 1) {
223 const uint8_t *src = src_row;
224 uint8_t *dst = dst_row;
225 for(x = 0; x < width; x += 1) {
226 uint32_t value;
227 p[0] = ubyte_to_float(src[0]);
228 p[1] = ubyte_to_float(src[1]);
229 p[2] = ubyte_to_float(src[2]);
230 value = util_cpu_to_le32(float3_to_r11g11b10f(p));
231 *(uint32_t *)dst = value;
232 src += 4;
233 dst += 4;
234 }
235 dst_row += dst_stride;
236 src_row += src_stride/sizeof(*src_row);
237 }
238 }
239
240 /*
241 * PIPE_FORMAT_R8G8Bx_SNORM
242 *
243 * A.k.a. D3DFMT_CxV8U8
244 */
245
246 static uint8_t
247 r8g8bx_derive(int16_t r, int16_t g)
248 {
249 /* Derive blue from red and green components.
250 * Apparently, we must always use integers to perform calculations,
251 * otherwise the results won't match D3D's CxV8U8 definition.
252 */
253 return (uint8_t)sqrtf(0x7f * 0x7f - r * r - g * g) * 0xff / 0x7f;
254 }
255
256 void
257 util_format_r8g8bx_snorm_unpack_rgba_float(void *dst_row, unsigned dst_stride,
258 const uint8_t *src_row, unsigned src_stride,
259 unsigned width, unsigned height)
260 {
261 unsigned x, y;
262
263 for(y = 0; y < height; y += 1) {
264 float *dst = dst_row;
265 const uint16_t *src = (const uint16_t *)src_row;
266 for(x = 0; x < width; x += 1) {
267 uint16_t value = util_cpu_to_le16(*src++);
268 int16_t r, g;
269
270 r = ((int16_t)(value << 8)) >> 8;
271 g = ((int16_t)(value << 0)) >> 8;
272
273 dst[0] = (float)(r * (1.0f/0x7f)); /* r */
274 dst[1] = (float)(g * (1.0f/0x7f)); /* g */
275 dst[2] = r8g8bx_derive(r, g) * (1.0f/0xff); /* b */
276 dst[3] = 1.0f; /* a */
277 dst += 4;
278 }
279 src_row += src_stride;
280 dst_row = (uint8_t *)dst_row + dst_stride;
281 }
282 }
283
284
285 void
286 util_format_r8g8bx_snorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
287 const uint8_t *src_row, unsigned src_stride,
288 unsigned width, unsigned height)
289 {
290 unsigned x, y;
291 for(y = 0; y < height; y += 1) {
292 uint8_t *dst = dst_row;
293 const uint16_t *src = (const uint16_t *)src_row;
294 for(x = 0; x < width; x += 1) {
295 uint16_t value = util_cpu_to_le16(*src++);
296 int16_t r, g;
297
298 r = ((int16_t)(value << 8)) >> 8;
299 g = ((int16_t)(value << 0)) >> 8;
300
301 dst[0] = (uint8_t)(((uint16_t)MAX2(r, 0)) * 0xff / 0x7f); /* r */
302 dst[1] = (uint8_t)(((uint16_t)MAX2(g, 0)) * 0xff / 0x7f); /* g */
303 dst[2] = r8g8bx_derive(r, g); /* b */
304 dst[3] = 255; /* a */
305 dst += 4;
306 }
307 src_row += src_stride;
308 dst_row += dst_stride/sizeof(*dst_row);
309 }
310 }
311
312
313 void
314 util_format_r8g8bx_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
315 const float *src_row, unsigned src_stride,
316 unsigned width, unsigned height)
317 {
318 unsigned x, y;
319 for(y = 0; y < height; y += 1) {
320 const float *src = src_row;
321 uint16_t *dst = (uint16_t *)dst_row;
322 for(x = 0; x < width; x += 1) {
323 uint16_t value = 0;
324
325 value |= (uint16_t)(((int8_t)(CLAMP(src[0], -1, 1) * 0x7f)) & 0xff) ;
326 value |= (uint16_t)((((int8_t)(CLAMP(src[1], -1, 1) * 0x7f)) & 0xff) << 8) ;
327
328 *dst++ = util_le16_to_cpu(value);
329
330 src += 4;
331 }
332 dst_row += dst_stride;
333 src_row += src_stride/sizeof(*src_row);
334 }
335 }
336
337
338 void
339 util_format_r8g8bx_snorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
340 const uint8_t *src_row, unsigned src_stride,
341 unsigned width, unsigned height)
342 {
343 unsigned x, y;
344
345 for(y = 0; y < height; y += 1) {
346 const uint8_t *src = src_row;
347 uint16_t *dst = (uint16_t *)dst_row;
348 for(x = 0; x < width; x += 1) {
349 uint16_t value = 0;
350
351 value |= src[0] >> 1;
352 value |= (src[1] >> 1) << 8;
353
354 *dst++ = util_le16_to_cpu(value);
355
356 src += 4;
357 }
358 dst_row += dst_stride;
359 src_row += src_stride/sizeof(*src_row);
360 }
361 }
362
363
364 void
365 util_format_r8g8bx_snorm_fetch_rgba_float(float *dst, const uint8_t *src,
366 UNUSED unsigned i, UNUSED unsigned j)
367 {
368 uint16_t value = util_cpu_to_le16(*(const uint16_t *)src);
369 int16_t r, g;
370
371 r = ((int16_t)(value << 8)) >> 8;
372 g = ((int16_t)(value << 0)) >> 8;
373
374 dst[0] = r * (1.0f/0x7f); /* r */
375 dst[1] = g * (1.0f/0x7f); /* g */
376 dst[2] = r8g8bx_derive(r, g) * (1.0f/0xff); /* b */
377 dst[3] = 1.0f; /* a */
378 }