• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/// @ref core
2/// @file glm/detail/func_packing.inl
3
4#include "func_common.hpp"
5#include "type_half.hpp"
6#include "../fwd.hpp"
7
8namespace glm
9{
10	GLM_FUNC_QUALIFIER uint packUnorm2x16(vec2 const & v)
11	{
12		union
13		{
14			u16  in[2];
15			uint out;
16		} u;
17
18		u16vec2 result(round(clamp(v, 0.0f, 1.0f) * 65535.0f));
19
20		u.in[0] = result[0];
21		u.in[1] = result[1];
22
23		return u.out;
24	}
25
26	GLM_FUNC_QUALIFIER vec2 unpackUnorm2x16(uint p)
27	{
28		union
29		{
30			uint in;
31			u16  out[2];
32		} u;
33
34		u.in = p;
35
36		return vec2(u.out[0], u.out[1]) * 1.5259021896696421759365224689097e-5f;
37	}
38
39	GLM_FUNC_QUALIFIER uint packSnorm2x16(vec2 const & v)
40	{
41		union
42		{
43			i16  in[2];
44			uint out;
45		} u;
46
47		i16vec2 result(round(clamp(v, -1.0f, 1.0f) * 32767.0f));
48
49		u.in[0] = result[0];
50		u.in[1] = result[1];
51
52		return u.out;
53	}
54
55	GLM_FUNC_QUALIFIER vec2 unpackSnorm2x16(uint p)
56	{
57		union
58		{
59			uint in;
60			i16  out[2];
61		} u;
62
63		u.in = p;
64
65		return clamp(vec2(u.out[0], u.out[1]) * 3.0518509475997192297128208258309e-5f, -1.0f, 1.0f);
66	}
67
68	GLM_FUNC_QUALIFIER uint packUnorm4x8(vec4 const & v)
69	{
70		union
71		{
72			u8   in[4];
73			uint out;
74		} u;
75
76		u8vec4 result(round(clamp(v, 0.0f, 1.0f) * 255.0f));
77
78		u.in[0] = result[0];
79		u.in[1] = result[1];
80		u.in[2] = result[2];
81		u.in[3] = result[3];
82
83		return u.out;
84	}
85
86	GLM_FUNC_QUALIFIER vec4 unpackUnorm4x8(uint p)
87	{
88		union
89		{
90			uint in;
91			u8   out[4];
92		} u;
93
94		u.in = p;
95
96		return vec4(u.out[0], u.out[1], u.out[2], u.out[3]) * 0.0039215686274509803921568627451f;
97	}
98
99	GLM_FUNC_QUALIFIER uint packSnorm4x8(vec4 const & v)
100	{
101		union
102		{
103			i8   in[4];
104			uint out;
105		} u;
106
107		i8vec4 result(round(clamp(v, -1.0f, 1.0f) * 127.0f));
108
109		u.in[0] = result[0];
110		u.in[1] = result[1];
111		u.in[2] = result[2];
112		u.in[3] = result[3];
113
114		return u.out;
115	}
116
117	GLM_FUNC_QUALIFIER glm::vec4 unpackSnorm4x8(uint p)
118	{
119		union
120		{
121			uint in;
122			i8   out[4];
123		} u;
124
125		u.in = p;
126
127		return clamp(vec4(u.out[0], u.out[1], u.out[2], u.out[3]) * 0.0078740157480315f, -1.0f, 1.0f);
128	}
129
130	GLM_FUNC_QUALIFIER double packDouble2x32(uvec2 const & v)
131	{
132		union
133		{
134			uint   in[2];
135			double out;
136		} u;
137
138		u.in[0] = v[0];
139		u.in[1] = v[1];
140
141		return u.out;
142	}
143
144	GLM_FUNC_QUALIFIER uvec2 unpackDouble2x32(double v)
145	{
146		union
147		{
148			double in;
149			uint   out[2];
150		} u;
151
152		u.in = v;
153
154		return uvec2(u.out[0], u.out[1]);
155	}
156
157	GLM_FUNC_QUALIFIER uint packHalf2x16(vec2 const & v)
158	{
159		union
160		{
161			i16  in[2];
162			uint out;
163		} u;
164
165		u.in[0] = detail::toFloat16(v.x);
166		u.in[1] = detail::toFloat16(v.y);
167
168		return u.out;
169	}
170
171	GLM_FUNC_QUALIFIER vec2 unpackHalf2x16(uint v)
172	{
173		union
174		{
175			uint in;
176			i16  out[2];
177		} u;
178
179		u.in = v;
180
181		return vec2(
182			detail::toFloat32(u.out[0]),
183			detail::toFloat32(u.out[1]));
184	}
185}//namespace glm
186
187#if GLM_ARCH != GLM_ARCH_PURE && GLM_HAS_UNRESTRICTED_UNIONS
188#	include "func_packing_simd.inl"
189#endif
190
191