forked from KhronosGroup/ANARI-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helium_math.h
361 lines (316 loc) · 8.78 KB
/
helium_math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2022-2024 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
#pragma once
// anari
#include <anari/anari_cpp/ext/linalg.h>
#include <anari/anari_cpp.hpp>
// std
#include <cstring>
#include <limits>
namespace helium::math {
// Types //////////////////////////////////////////////////////////////////////
template <typename T>
struct range_t
{
using element_t = T;
range_t() = default;
range_t(const T &t) : lower(t), upper(t) {}
range_t(const T &_lower, const T &_upper) : lower(_lower), upper(_upper) {}
range_t<T> &extend(const T &t)
{
lower = min(lower, t);
upper = max(upper, t);
return *this;
}
range_t<T> &extend(const range_t<T> &t)
{
lower = min(lower, t.lower);
upper = max(upper, t.upper);
return *this;
}
T lower{T(std::numeric_limits<float>::max())};
T upper{T(-std::numeric_limits<float>::max())};
};
using box1 = range_t<float>;
using box2 = range_t<anari::math::float2>;
using box3 = range_t<anari::math::float3>;
template <typename T>
inline typename range_t<T>::element_t size(const range_t<T> &r)
{
return r.upper - r.lower;
}
template <typename T>
inline typename range_t<T>::element_t clamp(
const typename range_t<T>::element_t &t, const range_t<T> &r)
{
return linalg::max(r.lower, linalg::min(t, r.upper));
}
inline float position(float v, const box1 &r)
{
return (v - r.lower) * (1.f / size(r));
}
constexpr anari::math::float4 DEFAULT_ATTRIBUTE_VALUE(0.f, 0.f, 0.f, 1.f);
enum class Attribute
{
ATTRIBUTE_0 = 0,
ATTRIBUTE_1,
ATTRIBUTE_2,
ATTRIBUTE_3,
COLOR,
NONE
};
enum WrapMode
{
CLAMP_TO_EDGE = 0,
REPEAT,
MIRROR_REPEAT,
DEFAULT
};
enum class AlphaMode
{
OPAQUE,
MASK,
BLEND
};
// Functions //////////////////////////////////////////////////////////////////
constexpr anari::math::mat3 extractRotation(const anari::math::mat4 &m)
{
return anari::math::mat3(anari::math::float3(m[0].x, m[0].y, m[0].z),
anari::math::float3(m[1].x, m[1].y, m[1].z),
anari::math::float3(m[2].x, m[2].y, m[2].z));
}
template <bool SRGB = true>
constexpr float toneMap(float v)
{
if constexpr (SRGB)
return std::pow(v, 1.f / 2.2f);
else
return v;
}
constexpr anari::math::float4 cvt_color_to_float4(uint32_t rgba)
{
const float a = ((rgba >> 24) & 0xff) / 255.f;
const float b = ((rgba >> 16) & 0xff) / 255.f;
const float g = ((rgba >> 8) & 0xff) / 255.f;
const float r = ((rgba >> 0) & 0xff) / 255.f;
return anari::math::float4(r, g, b, a);
}
constexpr uint32_t cvt_color_to_uint32(const float &f)
{
return static_cast<uint32_t>(255.f * std::clamp(f, 0.f, 1.f));
}
constexpr uint32_t cvt_color_to_uint32(const anari::math::float4 &v)
{
return (cvt_color_to_uint32(v.x) << 0) | (cvt_color_to_uint32(v.y) << 8)
| (cvt_color_to_uint32(v.z) << 16) | (cvt_color_to_uint32(v.w) << 24);
}
constexpr uint32_t cvt_color_to_uint32_srgb(const anari::math::float4 &v)
{
return cvt_color_to_uint32(
anari::math::float4(toneMap(v.x), toneMap(v.y), toneMap(v.z), v.w));
}
struct Interpolant
{
int32_t lower;
int32_t upper;
float frac;
};
inline Interpolant getInterpolant(float in, size_t size, bool texOffset = false)
{
const float scale = float(size);
const float lowf =
texOffset ? (in - (0.5f / scale)) * scale : (in * (scale - 1.f));
const int32_t low = static_cast<int32_t>(std::floor(lowf));
const int32_t high = low + 1;
const float frac = lowf - low;
return {low, high, frac};
}
template <typename T>
inline void accumulateValue(T &a, const T &b, float interp)
{
a += b * (1.f - interp);
}
inline int32_t computeMirroredRepeatIndex(int32_t x, int32_t size)
{
x = std::abs(x + (x < 0)) % (2 * size);
return x >= size ? 2 * size - x - 1 : x;
};
inline int32_t calculateWrapIndex(int32_t i, size_t size, WrapMode wrap)
{
switch (wrap) {
case WrapMode::CLAMP_TO_EDGE:
case WrapMode::DEFAULT:
default:
return linalg::clamp(i, 0, int32_t(size - 1));
break;
case WrapMode::REPEAT:
return i % size;
break;
case WrapMode::MIRROR_REPEAT:
return computeMirroredRepeatIndex(i, int32_t(size));
break;
}
}
inline Attribute attributeFromString(const std::string &str)
{
if (str == "color")
return Attribute::COLOR;
else if (str == "attribute0")
return Attribute::ATTRIBUTE_0;
else if (str == "attribute1")
return Attribute::ATTRIBUTE_1;
else if (str == "attribute2")
return Attribute::ATTRIBUTE_2;
else if (str == "attribute3")
return Attribute::ATTRIBUTE_3;
else
return Attribute::NONE;
}
inline WrapMode wrapModeFromString(const std::string &str)
{
if (str == "clampToEdge")
return WrapMode::CLAMP_TO_EDGE;
else if (str == "repeat")
return WrapMode::REPEAT;
else if (str == "mirrorRepeat")
return WrapMode::MIRROR_REPEAT;
else
return WrapMode::DEFAULT;
}
inline AlphaMode alphaModeFromString(const std::string &str)
{
if (str == "blend")
return AlphaMode::BLEND;
else if (str == "mask")
return AlphaMode::MASK;
else
return AlphaMode::OPAQUE;
}
inline float adjustOpacityFromMode(
float opacity, float maskThreshold, AlphaMode mode)
{
switch (mode) {
case AlphaMode::BLEND:
return opacity;
case AlphaMode::MASK:
return opacity >= maskThreshold ? 1.f : 0.f;
case AlphaMode::OPAQUE:
default:
return 1.f;
}
}
template <typename T>
static const T *typedOffset(const void *mem, uint64_t offset)
{
return ((const T *)mem) + offset;
}
template <typename ELEMENT_T, int NUM_COMPONENTS, bool SRGB = false>
static anari::math::float4 getAttributeArrayAt_ufixed(
const void *data, uint64_t offset)
{
constexpr float m = float(std::numeric_limits<ELEMENT_T>::max());
anari::math::float4 retval(0.f, 0.f, 0.f, 1.f);
switch (NUM_COMPONENTS) {
case 4:
retval.w = toneMap<SRGB>(
*typedOffset<ELEMENT_T>(data, NUM_COMPONENTS * offset + 3) / m);
case 3:
retval.z = toneMap<SRGB>(
*typedOffset<ELEMENT_T>(data, NUM_COMPONENTS * offset + 2) / m);
case 2:
retval.y = toneMap<SRGB>(
*typedOffset<ELEMENT_T>(data, NUM_COMPONENTS * offset + 1) / m);
case 1:
retval.x = toneMap<SRGB>(
*typedOffset<ELEMENT_T>(data, NUM_COMPONENTS * offset + 0) / m);
default:
break;
}
return retval;
}
inline anari::math::float4 readAsAttributeValueFlat(
const void *data, ANARIDataType type, uint64_t i)
{
auto retval = DEFAULT_ATTRIBUTE_VALUE;
switch (type) {
case ANARI_FLOAT32:
std::memcpy(&retval, typedOffset<float>(data, i), sizeof(float));
break;
case ANARI_FLOAT32_VEC2:
std::memcpy(&retval,
typedOffset<anari::math::float2>(data, i),
sizeof(anari::math::float2));
break;
case ANARI_FLOAT32_VEC3:
std::memcpy(&retval,
typedOffset<anari::math::float3>(data, i),
sizeof(anari::math::float3));
break;
case ANARI_FLOAT32_VEC4:
std::memcpy(&retval,
typedOffset<anari::math::float4>(data, i),
sizeof(anari::math::float4));
break;
case ANARI_UFIXED8_R_SRGB:
retval = getAttributeArrayAt_ufixed<uint8_t, 1, true>(data, i);
break;
case ANARI_UFIXED8_RA_SRGB:
retval = getAttributeArrayAt_ufixed<uint8_t, 2, true>(data, i);
break;
case ANARI_UFIXED8_RGB_SRGB:
retval = getAttributeArrayAt_ufixed<uint8_t, 3, true>(data, i);
break;
case ANARI_UFIXED8_RGBA_SRGB:
retval = getAttributeArrayAt_ufixed<uint8_t, 4, true>(data, i);
break;
case ANARI_UFIXED8:
retval = getAttributeArrayAt_ufixed<uint8_t, 1>(data, i);
break;
case ANARI_UFIXED8_VEC2:
retval = getAttributeArrayAt_ufixed<uint8_t, 2>(data, i);
break;
case ANARI_UFIXED8_VEC3:
retval = getAttributeArrayAt_ufixed<uint8_t, 3>(data, i);
break;
case ANARI_UFIXED8_VEC4:
retval = getAttributeArrayAt_ufixed<uint8_t, 4>(data, i);
break;
case ANARI_UFIXED16:
retval = getAttributeArrayAt_ufixed<uint16_t, 1>(data, i);
break;
case ANARI_UFIXED16_VEC2:
retval = getAttributeArrayAt_ufixed<uint16_t, 2>(data, i);
break;
case ANARI_UFIXED16_VEC3:
retval = getAttributeArrayAt_ufixed<uint16_t, 3>(data, i);
break;
case ANARI_UFIXED16_VEC4:
retval = getAttributeArrayAt_ufixed<uint16_t, 4>(data, i);
break;
case ANARI_UFIXED32:
retval = getAttributeArrayAt_ufixed<uint32_t, 1>(data, i);
break;
case ANARI_UFIXED32_VEC2:
retval = getAttributeArrayAt_ufixed<uint32_t, 2>(data, i);
break;
case ANARI_UFIXED32_VEC3:
retval = getAttributeArrayAt_ufixed<uint32_t, 3>(data, i);
break;
case ANARI_UFIXED32_VEC4:
retval = getAttributeArrayAt_ufixed<uint32_t, 4>(data, i);
break;
default:
break;
}
return retval;
}
} // namespace helium::math
namespace helium {
using namespace ::helium::math;
} // namespace helium
namespace anari {
ANARI_TYPEFOR_SPECIALIZATION(helium::box1, ANARI_FLOAT32_BOX1);
#ifdef helium_ANARI_DEFINITIONS
ANARI_TYPEFOR_DEFINITION(helium::box1);
#endif
} // namespace anari