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
util_format_r9g9b9e5_float_unpack_rgba_float(void * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)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
util_format_r9g9b9e5_float_pack_rgba_float(uint8_t * dst_row,unsigned dst_stride,const float * src_row,unsigned src_stride,unsigned width,unsigned height)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
util_format_r9g9b9e5_float_fetch_rgba(void * in_dst,const uint8_t * src,UNUSED unsigned i,UNUSED unsigned j)77 util_format_r9g9b9e5_float_fetch_rgba(void *in_dst, const uint8_t *src,
78                                        UNUSED unsigned i, UNUSED unsigned j)
79 {
80    float *dst = in_dst;
81    uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
82    rgb9e5_to_float3(value, dst);
83    dst[3] = 1; /* a */
84 }
85 
86 
87 void
util_format_r9g9b9e5_float_unpack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)88 util_format_r9g9b9e5_float_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
89                                          const uint8_t *src_row, unsigned src_stride,
90                                          unsigned width, unsigned height)
91 {
92    unsigned x, y;
93    float p[3];
94    for(y = 0; y < height; y += 1) {
95       uint8_t *dst = dst_row;
96       const uint8_t *src = src_row;
97       for(x = 0; x < width; x += 1) {
98          uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
99          rgb9e5_to_float3(value, p);
100          dst[0] = float_to_ubyte(p[0]); /* r */
101          dst[1] = float_to_ubyte(p[1]); /* g */
102          dst[2] = float_to_ubyte(p[2]); /* b */
103          dst[3] = 255; /* a */
104          src += 4;
105          dst += 4;
106       }
107       src_row += src_stride;
108       dst_row += dst_stride/sizeof(*dst_row);
109    }
110 }
111 
112 
113 void
util_format_r9g9b9e5_float_pack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)114 util_format_r9g9b9e5_float_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
115                                        const uint8_t *src_row, unsigned src_stride,
116                                        unsigned width, unsigned height)
117 {
118    unsigned x, y;
119    float p[3];
120    for(y = 0; y < height; y += 1) {
121       const uint8_t *src = src_row;
122       uint8_t *dst = dst_row;
123       for(x = 0; x < width; x += 1) {
124          uint32_t value;
125          p[0] = ubyte_to_float(src[0]);
126          p[1] = ubyte_to_float(src[1]);
127          p[2] = ubyte_to_float(src[2]);
128          value = util_cpu_to_le32(float3_to_rgb9e5(p));
129          *(uint32_t *)dst = value;
130          src += 4;
131          dst += 4;
132       }
133       dst_row += dst_stride;
134       src_row += src_stride/sizeof(*src_row);
135    }
136 }
137 
138 
139 void
util_format_r11g11b10_float_unpack_rgba_float(void * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)140 util_format_r11g11b10_float_unpack_rgba_float(void *dst_row, unsigned dst_stride,
141                                         const uint8_t *src_row, unsigned src_stride,
142                                         unsigned width, unsigned height)
143 {
144    unsigned x, y;
145    for(y = 0; y < height; y += 1) {
146       float *dst = dst_row;
147       const uint8_t *src = src_row;
148       for(x = 0; x < width; x += 1) {
149          uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
150          r11g11b10f_to_float3(value, dst);
151          dst[3] = 1; /* a */
152          src += 4;
153          dst += 4;
154       }
155       src_row += src_stride;
156       dst_row = (uint8_t *)dst_row + dst_stride;
157    }
158 }
159 
160 void
util_format_r11g11b10_float_pack_rgba_float(uint8_t * dst_row,unsigned dst_stride,const float * src_row,unsigned src_stride,unsigned width,unsigned height)161 util_format_r11g11b10_float_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
162                                       const float *src_row, unsigned src_stride,
163                                       unsigned width, unsigned height)
164 {
165    unsigned x, y;
166    for(y = 0; y < height; y += 1) {
167       const float *src = src_row;
168       uint8_t *dst = dst_row;
169       for(x = 0; x < width; x += 1) {
170          uint32_t value = util_cpu_to_le32(float3_to_r11g11b10f(src));
171          *(uint32_t *)dst = value;
172          src += 4;
173          dst += 4;
174       }
175       dst_row += dst_stride;
176       src_row += src_stride/sizeof(*src_row);
177    }
178 }
179 
180 void
util_format_r11g11b10_float_fetch_rgba(void * in_dst,const uint8_t * src,UNUSED unsigned i,UNUSED unsigned j)181 util_format_r11g11b10_float_fetch_rgba(void *in_dst, const uint8_t *src,
182                                         UNUSED unsigned i, UNUSED unsigned j)
183 {
184    float *dst = in_dst;
185    uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
186    r11g11b10f_to_float3(value, dst);
187    dst[3] = 1; /* a */
188 }
189 
190 
191 void
util_format_r11g11b10_float_unpack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)192 util_format_r11g11b10_float_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
193                                          const uint8_t *src_row, unsigned src_stride,
194                                          unsigned width, unsigned height)
195 {
196    unsigned x, y;
197    float p[3];
198    for(y = 0; y < height; y += 1) {
199       uint8_t *dst = dst_row;
200       const uint8_t *src = src_row;
201       for(x = 0; x < width; x += 1) {
202          uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);
203          r11g11b10f_to_float3(value, p);
204          dst[0] = float_to_ubyte(p[0]); /* r */
205          dst[1] = float_to_ubyte(p[1]); /* g */
206          dst[2] = float_to_ubyte(p[2]); /* b */
207          dst[3] = 255; /* a */
208          src += 4;
209          dst += 4;
210       }
211       src_row += src_stride;
212       dst_row += dst_stride/sizeof(*dst_row);
213    }
214 }
215 
216 
217 void
util_format_r11g11b10_float_pack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)218 util_format_r11g11b10_float_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
219                                        const uint8_t *src_row, unsigned src_stride,
220                                        unsigned width, unsigned height)
221 {
222    unsigned x, y;
223    float p[3];
224    for(y = 0; y < height; y += 1) {
225       const uint8_t *src = src_row;
226       uint8_t *dst = dst_row;
227       for(x = 0; x < width; x += 1) {
228          uint32_t value;
229          p[0] = ubyte_to_float(src[0]);
230          p[1] = ubyte_to_float(src[1]);
231          p[2] = ubyte_to_float(src[2]);
232          value = util_cpu_to_le32(float3_to_r11g11b10f(p));
233          *(uint32_t *)dst = value;
234          src += 4;
235          dst += 4;
236       }
237       dst_row += dst_stride;
238       src_row += src_stride/sizeof(*src_row);
239    }
240 }
241 
242 /*
243  * PIPE_FORMAT_R8G8Bx_SNORM
244  *
245  * A.k.a. D3DFMT_CxV8U8
246  */
247 
248 static uint8_t
r8g8bx_derive(int16_t r,int16_t g)249 r8g8bx_derive(int16_t r, int16_t g)
250 {
251    /* Derive blue from red and green components.
252     * Apparently, we must always use integers to perform calculations,
253     * otherwise the results won't match D3D's CxV8U8 definition.
254     */
255    return (uint8_t)sqrtf(0x7f * 0x7f - r * r - g * g) * 0xff / 0x7f;
256 }
257 
258 void
util_format_r8g8bx_snorm_unpack_rgba_float(void * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)259 util_format_r8g8bx_snorm_unpack_rgba_float(void *dst_row, unsigned dst_stride,
260                                       const uint8_t *src_row, unsigned src_stride,
261                                       unsigned width, unsigned height)
262 {
263    unsigned x, y;
264 
265    for(y = 0; y < height; y += 1) {
266       float *dst = dst_row;
267       const uint16_t *src = (const uint16_t *)src_row;
268       for(x = 0; x < width; x += 1) {
269          uint16_t value = util_cpu_to_le16(*src++);
270          int16_t r, g;
271 
272          r = ((int16_t)(value << 8)) >> 8;
273          g = ((int16_t)(value << 0)) >> 8;
274 
275          dst[0] = (float)(r * (1.0f/0x7f)); /* r */
276          dst[1] = (float)(g * (1.0f/0x7f)); /* g */
277          dst[2] = r8g8bx_derive(r, g) * (1.0f/0xff); /* b */
278          dst[3] = 1.0f; /* a */
279          dst += 4;
280       }
281       src_row += src_stride;
282       dst_row = (uint8_t *)dst_row + dst_stride;
283    }
284 }
285 
286 
287 void
util_format_r8g8bx_snorm_unpack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)288 util_format_r8g8bx_snorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
289                                        const uint8_t *src_row, unsigned src_stride,
290                                        unsigned width, unsigned height)
291 {
292    unsigned x, y;
293    for(y = 0; y < height; y += 1) {
294       uint8_t *dst = dst_row;
295       const uint16_t *src = (const uint16_t *)src_row;
296       for(x = 0; x < width; x += 1) {
297          uint16_t value = util_cpu_to_le16(*src++);
298          int16_t r, g;
299 
300          r = ((int16_t)(value << 8)) >> 8;
301          g = ((int16_t)(value << 0)) >> 8;
302 
303          dst[0] = (uint8_t)(((uint16_t)MAX2(r, 0)) * 0xff / 0x7f); /* r */
304          dst[1] = (uint8_t)(((uint16_t)MAX2(g, 0)) * 0xff / 0x7f); /* g */
305          dst[2] = r8g8bx_derive(r, g); /* b */
306          dst[3] = 255; /* a */
307          dst += 4;
308       }
309       src_row += src_stride;
310       dst_row += dst_stride/sizeof(*dst_row);
311    }
312 }
313 
314 
315 void
util_format_r8g8bx_snorm_pack_rgba_float(uint8_t * dst_row,unsigned dst_stride,const float * src_row,unsigned src_stride,unsigned width,unsigned height)316 util_format_r8g8bx_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
317                                     const float *src_row, unsigned src_stride,
318                                     unsigned width, unsigned height)
319 {
320    unsigned x, y;
321    for(y = 0; y < height; y += 1) {
322       const float *src = src_row;
323       uint16_t *dst = (uint16_t *)dst_row;
324       for(x = 0; x < width; x += 1) {
325          uint16_t value = 0;
326 
327          value |= (uint16_t)(((int8_t)(CLAMP(src[0], -1, 1) * 0x7f)) & 0xff) ;
328          value |= (uint16_t)((((int8_t)(CLAMP(src[1], -1, 1) * 0x7f)) & 0xff) << 8) ;
329 
330          *dst++ = util_le16_to_cpu(value);
331 
332          src += 4;
333       }
334       dst_row += dst_stride;
335       src_row += src_stride/sizeof(*src_row);
336    }
337 }
338 
339 
340 void
util_format_r8g8bx_snorm_pack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)341 util_format_r8g8bx_snorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
342                                      const uint8_t *src_row, unsigned src_stride,
343                                      unsigned width, unsigned height)
344 {
345    unsigned x, y;
346 
347    for(y = 0; y < height; y += 1) {
348       const uint8_t *src = src_row;
349       uint16_t *dst = (uint16_t *)dst_row;
350       for(x = 0; x < width; x += 1) {
351          uint16_t value = 0;
352 
353          value |= src[0] >> 1;
354          value |= (src[1] >> 1) << 8;
355 
356          *dst++ = util_le16_to_cpu(value);
357 
358          src += 4;
359       }
360       dst_row += dst_stride;
361       src_row += src_stride/sizeof(*src_row);
362    }
363 }
364 
365 
366 void
util_format_r8g8bx_snorm_fetch_rgba(void * in_dst,const uint8_t * src,UNUSED unsigned i,UNUSED unsigned j)367 util_format_r8g8bx_snorm_fetch_rgba(void *in_dst, const uint8_t *src,
368                                      UNUSED unsigned i, UNUSED unsigned j)
369 {
370    float *dst = in_dst;
371    uint16_t value = util_cpu_to_le16(*(const uint16_t *)src);
372    int16_t r, g;
373 
374    r = ((int16_t)(value << 8)) >> 8;
375    g = ((int16_t)(value << 0)) >> 8;
376 
377    dst[0] = r * (1.0f/0x7f); /* r */
378    dst[1] = g * (1.0f/0x7f); /* g */
379    dst[2] = r8g8bx_derive(r, g) * (1.0f/0xff); /* b */
380    dst[3] = 1.0f; /* a */
381 }
382