3 * A collection of format conversion utility functions.
7 * Mesa 3-D graphics library
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
10 * Copyright (C) 2014 Intel Corporation All Rights Reserved.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
31 #ifndef FORMAT_UTILS_H
32 #define FORMAT_UTILS_H
37 extern const mesa_array_format RGBA32_FLOAT
;
38 extern const mesa_array_format RGBA8_UBYTE
;
39 extern const mesa_array_format RGBA32_UINT
;
40 extern const mesa_array_format RGBA32_INT
;
42 /* Only guaranteed to work for BITS <= 32 */
43 #define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1))
44 #define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1))
45 #define MIN_INT(BITS) ((BITS) == 32 ? INT32_MIN : (-(1 << (BITS - 1))))
47 /* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */
48 #define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \
49 (((X) * (int)(MAX_UINT(DST_BITS) / MAX_UINT(SRC_BITS))) + \
50 ((DST_BITS % SRC_BITS) ? ((X) >> (SRC_BITS - DST_BITS % SRC_BITS)) : 0))
53 _mesa_unorm_to_float(unsigned x
, unsigned src_bits
)
55 return x
* (1.0f
/ (float)MAX_UINT(src_bits
));
59 _mesa_snorm_to_float(int x
, unsigned src_bits
)
61 if (x
<= -MAX_INT(src_bits
))
64 return x
* (1.0f
/ (float)MAX_INT(src_bits
));
67 static inline uint16_t
68 _mesa_unorm_to_half(unsigned x
, unsigned src_bits
)
70 return _mesa_float_to_half(_mesa_unorm_to_float(x
, src_bits
));
73 static inline uint16_t
74 _mesa_snorm_to_half(int x
, unsigned src_bits
)
76 return _mesa_float_to_half(_mesa_snorm_to_float(x
, src_bits
));
79 static inline unsigned
80 _mesa_float_to_unorm(float x
, unsigned dst_bits
)
85 return MAX_UINT(dst_bits
);
87 return F_TO_I(x
* MAX_UINT(dst_bits
));
90 static inline unsigned
91 _mesa_half_to_unorm(uint16_t x
, unsigned dst_bits
)
93 return _mesa_float_to_unorm(_mesa_half_to_float(x
), dst_bits
);
96 static inline unsigned
97 _mesa_unorm_to_unorm(unsigned x
, unsigned src_bits
, unsigned dst_bits
)
99 if (src_bits
< dst_bits
) {
100 return EXTEND_NORMALIZED_INT(x
, src_bits
, dst_bits
);
102 unsigned src_half
= (1 << (src_bits
- 1)) - 1;
104 if (src_bits
+ dst_bits
> sizeof(x
) * 8) {
105 assert(src_bits
+ dst_bits
<= sizeof(uint64_t) * 8);
106 return (((uint64_t) x
* MAX_UINT(dst_bits
) + src_half
) /
109 return (x
* MAX_UINT(dst_bits
) + src_half
) / MAX_UINT(src_bits
);
114 static inline unsigned
115 _mesa_snorm_to_unorm(int x
, unsigned src_bits
, unsigned dst_bits
)
120 return _mesa_unorm_to_unorm(x
, src_bits
- 1, dst_bits
);
124 _mesa_float_to_snorm(float x
, unsigned dst_bits
)
127 return -MAX_INT(dst_bits
);
129 return MAX_INT(dst_bits
);
131 return F_TO_I(x
* MAX_INT(dst_bits
));
135 _mesa_half_to_snorm(uint16_t x
, unsigned dst_bits
)
137 return _mesa_float_to_snorm(_mesa_half_to_float(x
), dst_bits
);
141 _mesa_unorm_to_snorm(unsigned x
, unsigned src_bits
, unsigned dst_bits
)
143 return _mesa_unorm_to_unorm(x
, src_bits
, dst_bits
- 1);
147 _mesa_snorm_to_snorm(int x
, unsigned src_bits
, unsigned dst_bits
)
149 if (x
< -MAX_INT(src_bits
))
150 return -MAX_INT(dst_bits
);
151 else if (src_bits
< dst_bits
)
152 return EXTEND_NORMALIZED_INT(x
, src_bits
- 1, dst_bits
- 1);
154 return x
>> (src_bits
- dst_bits
);
157 static inline unsigned
158 _mesa_unsigned_to_unsigned(unsigned src
, unsigned dst_size
)
160 return MIN2(src
, MAX_UINT(dst_size
));
164 _mesa_unsigned_to_signed(unsigned src
, unsigned dst_size
)
166 return MIN2(src
, MAX_INT(dst_size
));
170 _mesa_signed_to_signed(int src
, unsigned dst_size
)
172 return CLAMP(src
, MIN_INT(dst_size
), MAX_INT(dst_size
));
175 static inline unsigned
176 _mesa_signed_to_unsigned(int src
, unsigned dst_size
)
178 return CLAMP(src
, 0, MAX_UINT(dst_size
));
181 static inline unsigned
182 _mesa_float_to_unsigned(float src
, unsigned dst_bits
)
186 if (src
> (float)MAX_UINT(dst_bits
))
187 return MAX_UINT(dst_bits
);
188 return _mesa_signed_to_unsigned(src
, dst_bits
);
191 static inline unsigned
192 _mesa_float_to_signed(float src
, unsigned dst_bits
)
194 if (src
< (float)(-MAX_INT(dst_bits
)))
195 return -MAX_INT(dst_bits
);
196 if (src
> (float)MAX_INT(dst_bits
))
197 return MAX_INT(dst_bits
);
198 return _mesa_signed_to_signed(src
, dst_bits
);
201 static inline unsigned
202 _mesa_half_to_unsigned(uint16_t src
, unsigned dst_bits
)
204 if (_mesa_half_is_negative(src
))
206 return _mesa_unsigned_to_unsigned(_mesa_float_to_half(src
), dst_bits
);
209 static inline unsigned
210 _mesa_half_to_signed(uint16_t src
, unsigned dst_bits
)
212 return _mesa_float_to_signed(_mesa_half_to_float(src
), dst_bits
);
216 _mesa_format_to_array(mesa_format
, GLenum
*type
, int *num_components
,
217 uint8_t swizzle
[4], bool *normalized
);
220 _mesa_swizzle_and_convert(void *dst
,
221 enum mesa_array_format_datatype dst_type
,
222 int num_dst_channels
,
224 enum mesa_array_format_datatype src_type
,
225 int num_src_channels
,
226 const uint8_t swizzle
[4], bool normalized
, int count
);
229 _mesa_compute_rgba2base2rgba_component_mapping(GLenum baseFormat
, uint8_t *map
);
232 _mesa_format_convert(void *void_dst
, uint32_t dst_format
, size_t dst_stride
,
233 void *void_src
, uint32_t src_format
, size_t src_stride
,
234 size_t width
, size_t height
, uint8_t *rebase_swizzle
);