mesa/format_pack: Add _mesa_pack_int_rgba_row()
[mesa.git] / src / mesa / main / format_utils.h
1 /**
2 * \file format_utils.h
3 * A collection of format conversion utility functions.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
10 * Copyright (C) 2014 Intel Corporation All Rights Reserved.
11 *
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:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
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.
29 */
30
31 #ifndef FORMAT_UTILS_H
32 #define FORMAT_UTILS_H
33
34 #include "imports.h"
35 #include "macros.h"
36
37 /* Only guaranteed to work for BITS <= 32 */
38 #define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1))
39 #define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1))
40 #define MIN_INT(BITS) ((BITS) == 32 ? INT32_MIN : (-(1 << (BITS - 1))))
41
42 /* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */
43 #define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \
44 (((X) * (int)(MAX_UINT(DST_BITS) / MAX_UINT(SRC_BITS))) + \
45 ((DST_BITS % SRC_BITS) ? ((X) >> (SRC_BITS - DST_BITS % SRC_BITS)) : 0))
46
47 static inline float
48 _mesa_unorm_to_float(unsigned x, unsigned src_bits)
49 {
50 return x * (1.0f / (float)MAX_UINT(src_bits));
51 }
52
53 static inline float
54 _mesa_snorm_to_float(int x, unsigned src_bits)
55 {
56 if (x <= -MAX_INT(src_bits))
57 return -1.0f;
58 else
59 return x * (1.0f / (float)MAX_INT(src_bits));
60 }
61
62 static inline uint16_t
63 _mesa_unorm_to_half(unsigned x, unsigned src_bits)
64 {
65 return _mesa_float_to_half(_mesa_unorm_to_float(x, src_bits));
66 }
67
68 static inline uint16_t
69 _mesa_snorm_to_half(int x, unsigned src_bits)
70 {
71 return _mesa_float_to_half(_mesa_snorm_to_float(x, src_bits));
72 }
73
74 static inline unsigned
75 _mesa_float_to_unorm(float x, unsigned dst_bits)
76 {
77 if (x < 0.0f)
78 return 0;
79 else if (x > 1.0f)
80 return MAX_UINT(dst_bits);
81 else
82 return F_TO_I(x * MAX_UINT(dst_bits));
83 }
84
85 static inline unsigned
86 _mesa_half_to_unorm(uint16_t x, unsigned dst_bits)
87 {
88 return _mesa_float_to_unorm(_mesa_half_to_float(x), dst_bits);
89 }
90
91 static inline unsigned
92 _mesa_unorm_to_unorm(unsigned x, unsigned src_bits, unsigned dst_bits)
93 {
94 if (src_bits < dst_bits)
95 return EXTEND_NORMALIZED_INT(x, src_bits, dst_bits);
96 else
97 return x >> (src_bits - dst_bits);
98 }
99
100 static inline unsigned
101 _mesa_snorm_to_unorm(int x, unsigned src_bits, unsigned dst_bits)
102 {
103 if (x < 0)
104 return 0;
105 else
106 return _mesa_unorm_to_unorm(x, src_bits - 1, dst_bits);
107 }
108
109 static inline int
110 _mesa_float_to_snorm(float x, unsigned dst_bits)
111 {
112 if (x < -1.0f)
113 return -MAX_INT(dst_bits);
114 else if (x > 1.0f)
115 return MAX_INT(dst_bits);
116 else
117 return F_TO_I(x * MAX_INT(dst_bits));
118 }
119
120 static inline int
121 _mesa_half_to_snorm(uint16_t x, unsigned dst_bits)
122 {
123 return _mesa_float_to_snorm(_mesa_half_to_float(x), dst_bits);
124 }
125
126 static inline int
127 _mesa_unorm_to_snorm(unsigned x, unsigned src_bits, unsigned dst_bits)
128 {
129 return _mesa_unorm_to_unorm(x, src_bits, dst_bits - 1);
130 }
131
132 static inline int
133 _mesa_snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits)
134 {
135 if (x < -MAX_INT(src_bits))
136 return -MAX_INT(dst_bits);
137 else if (src_bits < dst_bits)
138 return EXTEND_NORMALIZED_INT(x, src_bits - 1, dst_bits - 1);
139 else
140 return x >> (src_bits - dst_bits);
141 }
142
143 static inline unsigned
144 _mesa_unsigned_to_unsigned(unsigned src, unsigned dst_size)
145 {
146 return MIN2(src, MAX_UINT(dst_size));
147 }
148
149 static inline int
150 _mesa_unsigned_to_signed(unsigned src, unsigned dst_size)
151 {
152 return MIN2(src, MAX_INT(dst_size));
153 }
154
155 static inline int
156 _mesa_signed_to_signed(int src, unsigned dst_size)
157 {
158 return CLAMP(src, MIN_INT(dst_size), MAX_INT(dst_size));
159 }
160
161 static inline unsigned
162 _mesa_signed_to_unsigned(int src, unsigned dst_size)
163 {
164 return CLAMP(src, 0, MAX_UINT(dst_size));
165 }
166
167 static inline unsigned
168 _mesa_float_to_unsigned(float src, unsigned dst_bits)
169 {
170 if (src < 0.0f)
171 return 0;
172 if (src > (float)MAX_UINT(dst_bits))
173 return MAX_UINT(dst_bits);
174 return _mesa_signed_to_unsigned(src, dst_bits);
175 }
176
177 static inline unsigned
178 _mesa_float_to_signed(float src, unsigned dst_bits)
179 {
180 if (src < (float)(-MAX_INT(dst_bits)))
181 return -MAX_INT(dst_bits);
182 if (src > (float)MAX_INT(dst_bits))
183 return MAX_INT(dst_bits);
184 return _mesa_signed_to_signed(src, dst_bits);
185 }
186
187 static inline unsigned
188 _mesa_half_to_unsigned(uint16_t src, unsigned dst_bits)
189 {
190 if (_mesa_half_is_negative(src))
191 return 0;
192 return _mesa_unsigned_to_unsigned(_mesa_float_to_half(src), dst_bits);
193 }
194
195 static inline unsigned
196 _mesa_half_to_signed(uint16_t src, unsigned dst_bits)
197 {
198 return _mesa_float_to_signed(_mesa_half_to_float(src), dst_bits);
199 }
200
201 bool
202 _mesa_format_to_array(mesa_format, GLenum *type, int *num_components,
203 uint8_t swizzle[4], bool *normalized);
204
205 void
206 _mesa_swizzle_and_convert(void *dst, GLenum dst_type, int num_dst_channels,
207 const void *src, GLenum src_type, int num_src_channels,
208 const uint8_t swizzle[4], bool normalized, int count);
209
210 #endif