1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
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"
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
)
41 for(y
= 0; y
< height
; y
+= 1) {
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
);
51 src_row
+= src_stride
;
52 dst_row
= (uint8_t *)dst_row
+ dst_stride
;
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
)
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
;
71 dst_row
+= dst_stride
;
72 src_row
+= src_stride
/sizeof(*src_row
);
77 util_format_r9g9b9e5_float_fetch_rgba(void *in_dst
, const uint8_t *src
,
78 UNUSED
unsigned i
, UNUSED
unsigned j
)
81 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
82 rgb9e5_to_float3(value
, dst
);
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
)
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 */
107 src_row
+= src_stride
;
108 dst_row
+= dst_stride
/sizeof(*dst_row
);
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
)
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) {
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
;
133 dst_row
+= dst_stride
;
134 src_row
+= src_stride
/sizeof(*src_row
);
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
)
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
);
155 src_row
+= src_stride
;
156 dst_row
= (uint8_t *)dst_row
+ dst_stride
;
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
)
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
;
175 dst_row
+= dst_stride
;
176 src_row
+= src_stride
/sizeof(*src_row
);
181 util_format_r11g11b10_float_fetch_rgba(void *in_dst
, const uint8_t *src
,
182 UNUSED
unsigned i
, UNUSED
unsigned j
)
185 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
186 r11g11b10f_to_float3(value
, dst
);
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
)
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 */
211 src_row
+= src_stride
;
212 dst_row
+= dst_stride
/sizeof(*dst_row
);
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
)
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) {
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
;
237 dst_row
+= dst_stride
;
238 src_row
+= src_stride
/sizeof(*src_row
);
243 * PIPE_FORMAT_R8G8Bx_SNORM
245 * A.k.a. D3DFMT_CxV8U8
249 r8g8bx_derive(int16_t r
, int16_t g
)
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.
255 return (uint8_t)sqrtf(0x7f * 0x7f - r
* r
- g
* g
) * 0xff / 0x7f;
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
)
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
++);
272 r
= ((int16_t)(value
<< 8)) >> 8;
273 g
= ((int16_t)(value
<< 0)) >> 8;
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 */
281 src_row
+= src_stride
;
282 dst_row
= (uint8_t *)dst_row
+ dst_stride
;
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
)
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
++);
300 r
= ((int16_t)(value
<< 8)) >> 8;
301 g
= ((int16_t)(value
<< 0)) >> 8;
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 */
309 src_row
+= src_stride
;
310 dst_row
+= dst_stride
/sizeof(*dst_row
);
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
)
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) {
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) ;
330 *dst
++ = util_le16_to_cpu(value
);
334 dst_row
+= dst_stride
;
335 src_row
+= src_stride
/sizeof(*src_row
);
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
)
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) {
353 value
|= src
[0] >> 1;
354 value
|= (src
[1] >> 1) << 8;
356 *dst
++ = util_le16_to_cpu(value
);
360 dst_row
+= dst_stride
;
361 src_row
+= src_stride
/sizeof(*src_row
);
367 util_format_r8g8bx_snorm_fetch_rgba(void *in_dst
, const uint8_t *src
,
368 UNUSED
unsigned i
, UNUSED
unsigned j
)
371 uint16_t value
= util_cpu_to_le16(*(const uint16_t *)src
);
374 r
= ((int16_t)(value
<< 8)) >> 8;
375 g
= ((int16_t)(value
<< 0)) >> 8;
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 */