6dd66c7d346d87eadb7150104ccb8cbf9a8ffc86
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_float(float *dst
, const uint8_t *src
,
78 UNUSED
unsigned i
, UNUSED
unsigned j
)
80 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
81 rgb9e5_to_float3(value
, dst
);
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
)
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 */
106 src_row
+= src_stride
;
107 dst_row
+= dst_stride
/sizeof(*dst_row
);
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
)
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) {
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
;
132 dst_row
+= dst_stride
;
133 src_row
+= src_stride
/sizeof(*src_row
);
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
)
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
);
154 src_row
+= src_stride
;
155 dst_row
= (uint8_t *)dst_row
+ dst_stride
;
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
)
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
;
174 dst_row
+= dst_stride
;
175 src_row
+= src_stride
/sizeof(*src_row
);
180 util_format_r11g11b10_float_fetch_rgba_float(float *dst
, const uint8_t *src
,
181 UNUSED
unsigned i
, UNUSED
unsigned j
)
183 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
184 r11g11b10f_to_float3(value
, dst
);
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
)
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 */
209 src_row
+= src_stride
;
210 dst_row
+= dst_stride
/sizeof(*dst_row
);
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
)
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) {
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
;
235 dst_row
+= dst_stride
;
236 src_row
+= src_stride
/sizeof(*src_row
);
241 * PIPE_FORMAT_R8G8Bx_SNORM
243 * A.k.a. D3DFMT_CxV8U8
247 r8g8bx_derive(int16_t r
, int16_t g
)
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.
253 return (uint8_t)sqrtf(0x7f * 0x7f - r
* r
- g
* g
) * 0xff / 0x7f;
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
)
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
++);
270 r
= ((int16_t)(value
<< 8)) >> 8;
271 g
= ((int16_t)(value
<< 0)) >> 8;
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 */
279 src_row
+= src_stride
;
280 dst_row
= (uint8_t *)dst_row
+ dst_stride
;
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
)
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
++);
298 r
= ((int16_t)(value
<< 8)) >> 8;
299 g
= ((int16_t)(value
<< 0)) >> 8;
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 */
307 src_row
+= src_stride
;
308 dst_row
+= dst_stride
/sizeof(*dst_row
);
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
)
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) {
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) ;
328 *dst
++ = util_le16_to_cpu(value
);
332 dst_row
+= dst_stride
;
333 src_row
+= src_stride
/sizeof(*src_row
);
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
)
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) {
351 value
|= src
[0] >> 1;
352 value
|= (src
[1] >> 1) << 8;
354 *dst
++ = util_le16_to_cpu(value
);
358 dst_row
+= dst_stride
;
359 src_row
+= src_stride
/sizeof(*src_row
);
365 util_format_r8g8bx_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
,
366 UNUSED
unsigned i
, UNUSED
unsigned j
)
368 uint16_t value
= util_cpu_to_le16(*(const uint16_t *)src
);
371 r
= ((int16_t)(value
<< 8)) >> 8;
372 g
= ((int16_t)(value
<< 0)) >> 8;
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 */