0.8.2 API documentation
workaround.hpp
1 #pragma once
2 
3 // Removed after upgrading to GLM 0.9.8
4 namespace gli{
5 namespace workaround{
6 namespace detail
7 {
8  union u3u3u2
9  {
10  struct
11  {
12  uint x : 3;
13  uint y : 3;
14  uint z : 2;
15  } data;
16  uint8 pack;
17  };
18 
19  union u4u4
20  {
21  struct
22  {
23  uint x : 4;
24  uint y : 4;
25  } data;
26  uint8 pack;
27  };
28 
29  union u4u4u4u4
30  {
31  struct
32  {
33  uint x : 4;
34  uint y : 4;
35  uint z : 4;
36  uint w : 4;
37  } data;
38  uint16 pack;
39  };
40 
41  union u5u6u5
42  {
43  struct
44  {
45  uint x : 5;
46  uint y : 6;
47  uint z : 5;
48  } data;
49  uint16 pack;
50  };
51 
52  union u5u5u5u1
53  {
54  struct
55  {
56  uint x : 5;
57  uint y : 5;
58  uint z : 5;
59  uint w : 1;
60  } data;
61  uint16 pack;
62  };
63 
64  union u9u9u9e5
65  {
66  struct
67  {
68  uint x : 9;
69  uint y : 9;
70  uint z : 9;
71  uint w : 5;
72  } data;
73  uint32 pack;
74  };
75 
76  template <typename T, typename floatType, precision P, template <typename, precision> class vecType, bool isInteger, bool signedType>
77  struct compute_compNormalize
78  {};
79 
80  template <typename T, typename floatType, precision P, template <typename, precision> class vecType>
81  struct compute_compNormalize<T, floatType, P, vecType, true, true>
82  {
83  GLM_FUNC_QUALIFIER static vecType<floatType, P> call(vecType<T, P> const & v)
84  {
85  floatType const Min = static_cast<floatType>(std::numeric_limits<T>::min());
86  floatType const Max = static_cast<floatType>(std::numeric_limits<T>::max());
87  return (vecType<floatType, P>(v) - Min) / (Max - Min) * static_cast<floatType>(2) - static_cast<floatType>(1);
88  }
89  };
90 
91  template <typename T, typename floatType, precision P, template <typename, precision> class vecType>
92  struct compute_compNormalize<T, floatType, P, vecType, true, false>
93  {
94  GLM_FUNC_QUALIFIER static vecType<floatType, P> call(vecType<T, P> const & v)
95  {
96  return vecType<floatType, P>(v) / static_cast<floatType>(std::numeric_limits<T>::max());
97  }
98  };
99 
100  template <typename T, typename floatType, precision P, template <typename, precision> class vecType>
101  struct compute_compNormalize<T, floatType, P, vecType, false, true>
102  {
103  GLM_FUNC_QUALIFIER static vecType<floatType, P> call(vecType<T, P> const & v)
104  {
105  return v;
106  }
107  };
108 
109  template <typename T, typename floatType, precision P, template <typename, precision> class vecType, bool isInteger, bool signedType>
110  struct compute_compScale
111  {};
112 
113  template <typename T, typename floatType, precision P, template <typename, precision> class vecType>
114  struct compute_compScale<T, floatType, P, vecType, true, true>
115  {
116  GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<floatType, P> const & v)
117  {
118  floatType const Max = static_cast<floatType>(std::numeric_limits<T>::max()) + static_cast<floatType>(0.5);
119  vecType<floatType, P> const Scaled(v * Max);
120  vecType<T, P> const Result(Scaled - static_cast<floatType>(0.5));
121  return Result;
122  }
123  };
124 
125  template <typename T, typename floatType, precision P, template <typename, precision> class vecType>
126  struct compute_compScale<T, floatType, P, vecType, true, false>
127  {
128  GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<floatType, P> const & v)
129  {
130  return vecType<T, P>(vecType<floatType, P>(v) * static_cast<floatType>(std::numeric_limits<T>::max()));
131  }
132  };
133 
134  template <typename T, typename floatType, precision P, template <typename, precision> class vecType>
135  struct compute_compScale<T, floatType, P, vecType, false, true>
136  {
137  GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<floatType, P> const & v)
138  {
139  return v;
140  }
141  };
142 
143  template <precision P, template <typename, precision> class vecType>
144  struct compute_half
145  {};
146 
147  template <precision P>
148  struct compute_half<P, tvec1>
149  {
150  GLM_FUNC_QUALIFIER static tvec1<uint16, P> pack(tvec1<float, P> const & v)
151  {
152  int16 const Unpacked(glm::detail::toFloat16(v.x));
153  return tvec1<uint16, P>(reinterpret_cast<uint16 const &>(Unpacked));
154  }
155 
156  GLM_FUNC_QUALIFIER static tvec1<float, P> unpack(tvec1<uint16, P> const & v)
157  {
158  return tvec1<float, P>(glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.x)));
159  }
160  };
161 
162  template <precision P>
163  struct compute_half<P, tvec2>
164  {
165  GLM_FUNC_QUALIFIER static tvec2<uint16, P> pack(tvec2<float, P> const & v)
166  {
167  tvec2<int16, P> const Unpacked(glm::detail::toFloat16(v.x), glm::detail::toFloat16(v.y));
168  return tvec2<uint16, P>(
169  reinterpret_cast<uint16 const &>(Unpacked.x),
170  reinterpret_cast<uint16 const &>(Unpacked.y));
171  }
172 
173  GLM_FUNC_QUALIFIER static tvec2<float, P> unpack(tvec2<uint16, P> const & v)
174  {
175  return tvec2<float, P>(
176  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.x)),
177  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.y)));
178  }
179  };
180 
181  template <precision P>
182  struct compute_half<P, tvec3>
183  {
184  GLM_FUNC_QUALIFIER static tvec3<uint16, P> pack(tvec3<float, P> const & v)
185  {
186  tvec3<int16, P> const Unpacked(glm::detail::toFloat16(v.x), glm::detail::toFloat16(v.y), glm::detail::toFloat16(v.z));
187  return tvec3<uint16, P>(
188  reinterpret_cast<uint16 const &>(Unpacked.x),
189  reinterpret_cast<uint16 const &>(Unpacked.y),
190  reinterpret_cast<uint16 const &>(Unpacked.z));
191  }
192 
193  GLM_FUNC_QUALIFIER static tvec3<float, P> unpack(tvec3<uint16, P> const & v)
194  {
195  return tvec3<float, P>(
196  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.x)),
197  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.y)),
198  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.z)));
199  }
200  };
201 
202  template <precision P>
203  struct compute_half<P, tvec4>
204  {
205  GLM_FUNC_QUALIFIER static tvec4<uint16, P> pack(tvec4<float, P> const & v)
206  {
207  tvec4<int16, P> const Unpacked(glm::detail::toFloat16(v.x), glm::detail::toFloat16(v.y), glm::detail::toFloat16(v.z), glm::detail::toFloat16(v.w));
208  return tvec4<uint16, P>(
209  reinterpret_cast<uint16 const &>(Unpacked.x),
210  reinterpret_cast<uint16 const &>(Unpacked.y),
211  reinterpret_cast<uint16 const &>(Unpacked.z),
212  reinterpret_cast<uint16 const &>(Unpacked.w));
213  }
214 
215  GLM_FUNC_QUALIFIER static tvec4<float, P> unpack(tvec4<uint16, P> const & v)
216  {
217  return tvec4<float, P>(
218  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.x)),
219  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.y)),
220  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.z)),
221  glm::detail::toFloat32(reinterpret_cast<int16 const &>(v.w)));
222  }
223  };
224 }//namespace detail
225 
226  template <typename floatType, typename T, precision P, template <typename, precision> class vecType>
227  GLM_FUNC_QUALIFIER vecType<floatType, P> compNormalize(vecType<T, P> const & v)
228  {
229  GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "'compNormalize' accepts only floating-point types for 'floatType' template parameter");
230 
232  }
233 
234  template <typename T, typename floatType, precision P, template <typename, precision> class vecType>
235  GLM_FUNC_QUALIFIER vecType<T, P> compScale(vecType<floatType, P> const & v)
236  {
237  GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "'compScale' accepts only floating-point types for 'floatType' template parameter");
238 
240  }
241 
242  template <typename uintType, typename floatType, precision P, template <typename, precision> class vecType>
243  GLM_FUNC_QUALIFIER vecType<uintType, P> packUnorm(vecType<floatType, P> const & v)
244  {
245  GLM_STATIC_ASSERT(std::numeric_limits<uintType>::is_integer, "uintType must be an integer type");
246  GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
247 
248  return vecType<uintType, P>(round(clamp(v, static_cast<floatType>(0), static_cast<floatType>(1)) * static_cast<floatType>(std::numeric_limits<uintType>::max())));
249  }
250 
251  template <typename uintType, typename floatType, precision P, template <typename, precision> class vecType>
252  GLM_FUNC_QUALIFIER vecType<floatType, P> unpackUnorm(vecType<uintType, P> const & v)
253  {
254  GLM_STATIC_ASSERT(std::numeric_limits<uintType>::is_integer, "uintType must be an integer type");
255  GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
256 
257  return vecType<float, P>(v) * (static_cast<floatType>(1) / static_cast<floatType>(std::numeric_limits<uintType>::max()));
258  }
259 
260  GLM_FUNC_QUALIFIER uint8 packUnorm2x3_1x2(vec3 const & v)
261  {
262  u32vec3 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec3(7.f, 7.f, 3.f)));
263  detail::u3u3u2 Result;
264  Result.data.x = Unpack.x;
265  Result.data.y = Unpack.y;
266  Result.data.z = Unpack.z;
267  return Result.pack;
268  }
269 
270  GLM_FUNC_QUALIFIER vec3 unpackUnorm2x3_1x2(uint8 v)
271  {
272  vec3 const ScaleFactor(1.f / 7.f, 1.f / 7.f, 1.f / 3.f);
273  detail::u3u3u2 Unpack;
274  Unpack.pack = v;
275  return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * ScaleFactor;
276  }
277 
278  GLM_FUNC_QUALIFIER uint8 packUnorm2x4(vec2 const & v)
279  {
280  u32vec2 const Unpack(round(clamp(v, 0.0f, 1.0f) * 15.0f));
281  detail::u4u4 Result;
282  Result.data.x = Unpack.x;
283  Result.data.y = Unpack.y;
284  return Result.pack;
285  }
286 
287  GLM_FUNC_QUALIFIER vec2 unpackUnorm2x4(uint8 v)
288  {
289  float const ScaleFactor(1.f / 15.f);
290  detail::u4u4 Unpack;
291  Unpack.pack = v;
292  return vec2(Unpack.data.x, Unpack.data.y) * ScaleFactor;
293  }
294 
295  GLM_FUNC_QUALIFIER uint16 packUnorm4x4(vec4 const & v)
296  {
297  u32vec4 const Unpack(round(clamp(v, 0.0f, 1.0f) * 15.0f));
298  detail::u4u4u4u4 Result;
299  Result.data.x = Unpack.x;
300  Result.data.y = Unpack.y;
301  Result.data.z = Unpack.z;
302  Result.data.w = Unpack.w;
303  return Result.pack;
304  }
305 
306  GLM_FUNC_QUALIFIER vec4 unpackUnorm4x4(uint16 v)
307  {
308  float const ScaleFactor(1.f / 15.f);
309  detail::u4u4u4u4 Unpack;
310  Unpack.pack = v;
311  return vec4(Unpack.data.x, Unpack.data.y, Unpack.data.z, Unpack.data.w) * ScaleFactor;
312  }
313 
314  GLM_FUNC_QUALIFIER uint16 packUnorm3x5_1x1(vec4 const & v)
315  {
316  u32vec4 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec4(15.f, 15.f, 15.f, 1.f)));
317  detail::u5u5u5u1 Result;
318  Result.data.x = Unpack.x;
319  Result.data.y = Unpack.y;
320  Result.data.z = Unpack.z;
321  Result.data.w = Unpack.w;
322  return Result.pack;
323  }
324 
325  GLM_FUNC_QUALIFIER vec4 unpackUnorm3x5_1x1(uint16 v)
326  {
327  vec4 const ScaleFactor(1.f / 15.f, 1.f / 15.f, 1.f / 15.f, 1.f);
328  detail::u5u5u5u1 Unpack;
329  Unpack.pack = v;
330  return vec4(Unpack.data.x, Unpack.data.y, Unpack.data.z, Unpack.data.w) * ScaleFactor;
331  }
332 
333  GLM_FUNC_QUALIFIER uint16 packUnorm1x5_1x6_1x5(vec3 const & v)
334  {
335  u32vec3 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec3(15.f, 31.f, 15.f)));
336  detail::u5u6u5 Result;
337  Result.data.x = Unpack.x;
338  Result.data.y = Unpack.y;
339  Result.data.z = Unpack.z;
340  return Result.pack;
341  }
342 
343  GLM_FUNC_QUALIFIER vec3 unpackUnorm1x5_1x6_1x5(uint16 v)
344  {
345  vec3 const ScaleFactor(1.f / 15.f, 1.f / 31.f, 1.f / 15.f);
346  detail::u5u6u5 Unpack;
347  Unpack.pack = v;
348  return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * ScaleFactor;
349  }
350 
351  GLM_FUNC_QUALIFIER uint32 packF3x9_E1x5(vec3 const & v)
352  {
353  float const SharedExpMax = (pow(2.0f, 9.0f - 1.0f) / pow(2.0f, 9.0f)) * pow(2.0f, 31.f - 15.f);
354  vec3 const Color = clamp(v, 0.0f, SharedExpMax);
355  float const MaxColor = max(Color.x, max(Color.y, Color.z));
356 
357  float const ExpSharedP = max(-15.f - 1.f, floor(log2(MaxColor))) + 1.0f + 15.f;
358  float const MaxShared = floor(MaxColor / pow(2.0f, (ExpSharedP - 16.f - 9.f)) + 0.5f);
359  float const ExpShared = MaxShared == pow(2.0f, 9.0f) ? ExpSharedP + 1.0f : ExpSharedP;
360 
361  uvec3 const ColorComp(floor(Color / pow(2.f, (ExpShared - 15.f - 9.f)) + 0.5f));
362 
363  detail::u9u9u9e5 Unpack;
364  Unpack.data.x = ColorComp.x;
365  Unpack.data.y = ColorComp.y;
366  Unpack.data.z = ColorComp.z;
367  Unpack.data.w = uint(ExpShared);
368  return Unpack.pack;
369  }
370 
371  GLM_FUNC_QUALIFIER vec3 unpackF3x9_E1x5(uint32 v)
372  {
373  detail::u9u9u9e5 Unpack;
374  Unpack.pack = v;
375 
376  return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * pow(2.0f, Unpack.data.w - 15.f - 9.f);
377  }
378 
379  template <precision P, template <typename, precision> class vecType>
380  GLM_FUNC_QUALIFIER vecType<uint16, P> packHalf(vecType<float, P> const & v)
381  {
382  return detail::compute_half<P, vecType>::pack(v);
383  }
384 
385  template <precision P, template <typename, precision> class vecType>
386  GLM_FUNC_QUALIFIER vecType<float, P> unpackHalf(vecType<uint16, P> const & v)
387  {
388  return detail::compute_half<P, vecType>::unpack(v);
389  }
390 }//namespace workaround
391 }//namespace gli
392 
bool is_integer(format Format)
Evaluate whether the format is an integer format.
bool is_signed(format Format)
Evaluate whether a format is signed.
Namespace where all the classes and functions provided by GLI are exposed.
Definition: clear.hpp:6