MaterialX 1.38.2
Types.h
Go to the documentation of this file.
1//
2// TM & (c) 2019 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
4//
5
6#ifndef MATERIALX_RENDER_TYPES_H
7#define MATERIALX_RENDER_TYPES_H
8
11
12#include <MaterialXCore/Types.h>
14
15namespace MaterialX
16{
17
20class MX_RENDER_API Vector3d : public VectorN<Vector3d, double, 3>
21{
22 public:
24 Vector3d() = default;
25 Vector3d(double x, double y, double z) : VectorN(Uninit{})
26 {
27 _arr = {x, y, z};
28 }
29};
30
33class MX_RENDER_API Vector4d : public VectorN<Vector4d, double, 4>
34{
35 public:
37 Vector4d() = default;
38 Vector4d(double x, double y, double z, double w) : VectorN(Uninit{})
39 {
40 _arr = {x, y, z, w};
41 }
42};
43
46class MX_RENDER_API Color3d : public VectorN<Color3d, double, 3>
47{
48 public:
50 Color3d() = default;
51 Color3d(double r, double g, double b) : VectorN(Uninit{})
52 {
53 _arr = {r, g, b};
54 }
55};
56
60class MX_RENDER_API Half
61{
62 public:
63 explicit Half(float value) : _data(toFloat16(value)) { }
64 operator float() const { return toFloat32(_data); }
65
66 bool operator==(Half rhs) const { return float(*this) == float(rhs); }
67 bool operator!=(Half rhs) const { return float(*this) != float(rhs); }
68 bool operator<(Half rhs) const { return float(*this) < float(rhs); }
69 bool operator>(Half rhs) const { return float(*this) > float(rhs); }
70 bool operator<=(Half rhs) const { return float(*this) <= float(rhs); }
71 bool operator>=(Half rhs) const { return float(*this) >= float(rhs); }
72
73 Half operator+(Half rhs) const { return Half(float(*this) + float(rhs)); }
74 Half operator-(Half rhs) const { return Half(float(*this) - float(rhs)); }
75 Half operator*(Half rhs) const { return Half(float(*this) * float(rhs)); }
76 Half operator/(Half rhs) const { return Half(float(*this) / float(rhs)); }
77
78 Half& operator+=(Half rhs) { return operator=(*this + rhs); }
79 Half& operator-=(Half rhs) { return operator=(*this - rhs); }
80 Half& operator*=(Half rhs) { return operator=(*this * rhs); }
81 Half& operator/=(Half rhs) { return operator=(*this / rhs); }
82
83 Half operator-() const { return Half(-float(*this)); }
84
85 private:
86 union Bits
87 {
88 float f;
89 int32_t si;
90 uint32_t ui;
91 };
92
93 static constexpr int const shift = 13;
94 static constexpr int const shiftSign = 16;
95
96 static constexpr int32_t const infN = 0x7F800000; // flt32 infinity
97 static constexpr int32_t const maxN = 0x477FE000; // max flt16 normal as a flt32
98 static constexpr int32_t const minN = 0x38800000; // min flt16 normal as a flt32
99 static constexpr int32_t const signN = (int32_t) 0x80000000; // flt32 sign bit
100
101 static constexpr int32_t const infC = infN >> shift;
102 static constexpr int32_t const nanN = (infC + 1) << shift; // minimum flt16 nan as a flt32
103 static constexpr int32_t const maxC = maxN >> shift;
104 static constexpr int32_t const minC = minN >> shift;
105 static constexpr int32_t const signC = (int32_t) 0x00008000; // flt16 sign bit
106
107 static constexpr int32_t const mulN = 0x52000000; // (1 << 23) / minN
108 static constexpr int32_t const mulC = 0x33800000; // minN / (1 << (23 - shift))
109
110 static constexpr int32_t const subC = 0x003FF; // max flt32 subnormal down shifted
111 static constexpr int32_t const norC = 0x00400; // min flt32 normal down shifted
112
113 static constexpr int32_t const maxD = infC - maxC - 1;
114 static constexpr int32_t const minD = minC - subC - 1;
115
116 static uint16_t toFloat16(float value)
117 {
118 Bits v, s;
119 v.f = value;
120 uint32_t sign = (uint32_t) (v.si & signN);
121 v.si ^= sign;
122 sign >>= shiftSign; // logical shift
123 s.si = mulN;
124 s.si = (int32_t) (s.f * v.f); // correct subnormals
125 v.si ^= (s.si ^ v.si) & -(minN > v.si);
126 v.si ^= (infN ^ v.si) & -((infN > v.si) & (v.si > maxN));
127 v.si ^= (nanN ^ v.si) & -((nanN > v.si) & (v.si > infN));
128 v.ui >>= shift; // logical shift
129 v.si ^= ((v.si - maxD) ^ v.si) & -(v.si > maxC);
130 v.si ^= ((v.si - minD) ^ v.si) & -(v.si > subC);
131 return (uint16_t) (v.ui | sign);
132 }
133
134 static float toFloat32(uint16_t value)
135 {
136 Bits v;
137 v.ui = value;
138 int32_t sign = v.si & signC;
139 v.si ^= sign;
140 sign <<= shiftSign;
141 v.si ^= ((v.si + minD) ^ v.si) & -(v.si > subC);
142 v.si ^= ((v.si + maxD) ^ v.si) & -(v.si > maxC);
143 Bits s;
144 s.si = mulC;
145 s.f *= float(v.si);
146 int32_t mask = (norC > v.si) ? -1 : 1;
147 v.si <<= shift;
148 v.si ^= (s.si ^ v.si) & mask;
149 v.si |= sign;
150 return v.f;
151 }
152
153 private:
154 uint16_t _data;
155};
156
157} // namespace MaterialX
158
159#endif
Data type classes.
Macros for declaring imported and exported symbols.
A three-component color value (double-precision)
Definition: Types.h:47
A lightweight 16-bit half-precision float class.
Definition: Types.h:61
A tag class for constructing vectors and matrices without initialization.
Definition: Types.h:46
A vector of three floating-point values (double-precision)
Definition: Types.h:21
A vector of four floating-point values (double-precision)
Definition: Types.h:34
The class template for vectors of scalar values.
Definition: Types.h:54