-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.h
367 lines (339 loc) · 9.2 KB
/
common.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
362
363
364
365
366
367
#pragma once
// std
#include <iostream>
// anari
#include <anari/anari_cpp.hpp>
// visionaray
#include "visionaray/math/math.h"
// ==================================================================
// common traits
// ==================================================================
namespace visionaray {
// ==================================================================
// RNG
// ==================================================================
template<unsigned int N=4>
struct LCG
{
inline VSNRAY_FUNC LCG()
{ /* intentionally empty so we can use it in device vars that
don't allow dynamic initialization (ie, PRD) */
}
inline VSNRAY_FUNC LCG(unsigned int val0, unsigned int val1)
{ init(val0,val1); }
inline VSNRAY_FUNC LCG(const vec2i &seed)
{ init((unsigned)seed.x,(unsigned)seed.y); }
inline VSNRAY_FUNC LCG(const vec2ui &seed)
{ init(seed.x,seed.y); }
inline VSNRAY_FUNC void init(unsigned int val0, unsigned int val1)
{
unsigned int v0 = val0;
unsigned int v1 = val1;
unsigned int s0 = 0;
for (unsigned int n = 0; n < N; n++) {
s0 += 0x9e3779b9;
v0 += ((v1<<4)+0xa341316c)^(v1+s0)^((v1>>5)+0xc8013ea4);
v1 += ((v0<<4)+0xad90777d)^(v0+s0)^((v0>>5)+0x7e95761e);
}
state = v0;
}
// Generate random unsigned int in [0, 2^24)
inline VSNRAY_FUNC float operator() ()
{
const uint32_t LCG_A = 1664525u;
const uint32_t LCG_C = 1013904223u;
state = (LCG_A * state + LCG_C);
return (state & 0x00FFFFFF) / (float) 0x01000000;
}
// For compat. with visionaray
inline VSNRAY_FUNC float next()
{
return operator()();
}
uint32_t state;
};
typedef LCG<4> Random;
VSNRAY_FUNC
inline vec4 randomColor(unsigned idx)
{
unsigned int r = (unsigned int)(idx*13*17 + 0x234235);
unsigned int g = (unsigned int)(idx*7*3*5 + 0x773477);
unsigned int b = (unsigned int)(idx*11*19 + 0x223766);
return vec4{(r&255)/255.f,
(g&255)/255.f,
(b&255)/255.f,
1.f};
}
struct TypeInfo
{
ANARIDataType dataType;
unsigned sizeInBytes;
unsigned numComponents;
bool fixed;
bool sRGB;
};
VSNRAY_FUNC
constexpr TypeInfo getInfo(ANARIDataType type)
{
TypeInfo ti{};
if (type == ANARI_UFIXED8) {
ti.dataType = type;
ti.sizeInBytes = 1;
ti.numComponents = 1;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED8_VEC2) {
ti.dataType = type;
ti.sizeInBytes = 2;
ti.numComponents = 2;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED8_VEC3) {
ti.dataType = type;
ti.sizeInBytes = 3;
ti.numComponents = 3;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED8_VEC4) {
ti.dataType = type;
ti.sizeInBytes = 4;
ti.numComponents = 4;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED16) {
ti.dataType = type;
ti.sizeInBytes = 2;
ti.numComponents = 1;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED16_VEC2) {
ti.dataType = type;
ti.sizeInBytes = 4;
ti.numComponents = 2;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED16_VEC3) {
ti.dataType = type;
ti.sizeInBytes = 6;
ti.numComponents = 3;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED16_VEC4) {
ti.dataType = type;
ti.sizeInBytes = 8;
ti.numComponents = 4;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED32) {
ti.dataType = type;
ti.sizeInBytes = 4;
ti.numComponents = 1;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED32_VEC2) {
ti.dataType = type;
ti.sizeInBytes = 8;
ti.numComponents = 2;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED32_VEC3) {
ti.dataType = type;
ti.sizeInBytes = 12;
ti.numComponents = 3;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_UFIXED32_VEC4) {
ti.dataType = type;
ti.sizeInBytes = 16;
ti.numComponents = 4;
ti.fixed = true;
ti.sRGB = false;
}
else if (type == ANARI_FLOAT32) {
ti.dataType = type;
ti.sizeInBytes = 4;
ti.numComponents = 1;
ti.fixed = false;
ti.sRGB = false;
}
else if (type == ANARI_FLOAT32_VEC2) {
ti.dataType = type;
ti.sizeInBytes = 8;
ti.numComponents = 2;
ti.fixed = false;
ti.sRGB = false;
}
else if (type == ANARI_FLOAT32_VEC3) {
ti.dataType = type;
ti.sizeInBytes = 12;
ti.numComponents = 3;
ti.fixed = false;
ti.sRGB = false;
}
else if (type == ANARI_FLOAT32_VEC4) {
ti.dataType = type;
ti.sizeInBytes = 16;
ti.numComponents = 4;
ti.fixed = false;
ti.sRGB = false;
}
return ti;
}
VSNRAY_FUNC
inline vec4 toRGBA(const uint8_t *source, const TypeInfo &ti)
{
vec4 result{0.f, 0.f, 0.f, 1.f};
if (ti.fixed) {
switch (ti.dataType) {
case ANARI_UFIXED8: {
unorm<8> u8;
memcpy(&u8, source, sizeof(u8));
result.x = float(u8);
break;
}
case ANARI_UFIXED8_VEC2: {
vector<2, unorm<8>> u8;
memcpy(&u8, source, sizeof(u8));
result.x = float(u8.x);
result.y = float(u8.y);
break;
}
case ANARI_UFIXED8_VEC3: {
vector<3, unorm<8>> u8;
memcpy(&u8, source, sizeof(u8));
result.x = float(u8.x);
result.y = float(u8.y);
result.z = float(u8.z);
break;
}
case ANARI_UFIXED8_VEC4: {
vector<4, unorm<8>> u8;
memcpy(&u8, source, sizeof(u8));
result.x = float(u8.x);
result.y = float(u8.y);
result.z = float(u8.z);
result.w = float(u8.w);
break;
}
case ANARI_UFIXED16: {
unorm<16> u16;
memcpy(&u16, source, sizeof(u16));
result.x = float(u16);
break;
}
case ANARI_UFIXED16_VEC2: {
vector<2, unorm<16>> u16;
memcpy(&u16, source, sizeof(u16));
result.x = float(u16.x);
result.y = float(u16.y);
break;
}
case ANARI_UFIXED16_VEC3: {
vector<3, unorm<16>> u16;
memcpy(&u16, source, sizeof(u16));
result.x = float(u16.x);
result.y = float(u16.y);
result.z = float(u16.z);
break;
}
case ANARI_UFIXED16_VEC4: {
vector<4, unorm<16>> u16;
memcpy(&u16, source, sizeof(u16));
result.x = float(u16.x);
result.y = float(u16.y);
result.z = float(u16.z);
result.w = float(u16.w);
break;
}
case ANARI_UFIXED32: {
unorm<32> u32;
memcpy(&u32, source, sizeof(u32));
result.x = float(u32);
break;
}
case ANARI_UFIXED32_VEC2: {
vector<2, unorm<32>> u32;
memcpy(&u32, source, sizeof(u32));
result.x = float(u32.x);
result.y = float(u32.y);
break;
}
case ANARI_UFIXED32_VEC3: {
vector<3, unorm<32>> u32;
memcpy(&u32, source, sizeof(u32));
result.x = float(u32.x);
result.y = float(u32.y);
result.z = float(u32.z);
break;
}
case ANARI_UFIXED32_VEC4: {
vector<4, unorm<32>> u32;
memcpy(&u32, source, sizeof(u32));
result.x = float(u32.x);
result.y = float(u32.y);
result.z = float(u32.z);
result.w = float(u32.w);
break;
}
}
} else {
memcpy(&result, source, ti.sizeInBytes);
}
if (ti.sRGB) {
for (unsigned c=0; c<ti.numComponents; ++c) {
}
}
return result;
}
} // namespace visionaray
// ==================================================================
// math types
// ==================================================================
namespace visionaray {
using int2 = vec2i;
using int3 = vec3i;
using int4 = vec4i;
using uint2 = vec2ui;
using uint3 = vec3ui;
using uint4 = vec4ui;
using float2 = vec2f;
using float3 = vec3f;
using float4 = vec4f;
} // namespace visionaray
namespace anari {
ANARI_TYPEFOR_SPECIALIZATION(visionaray::uint2, ANARI_UINT32_VEC2);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::uint3, ANARI_UINT32_VEC3);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::uint4, ANARI_UINT32_VEC4);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::float2, ANARI_FLOAT32_VEC2);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::float3, ANARI_FLOAT32_VEC3);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::float4, ANARI_FLOAT32_VEC4);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::box1, ANARI_FLOAT32_BOX1);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::box2, ANARI_FLOAT32_BOX2);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::aabb, ANARI_FLOAT32_BOX3);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::aabbi, ANARI_INT32_BOX3);
ANARI_TYPEFOR_SPECIALIZATION(visionaray::mat4, ANARI_FLOAT32_MAT4);
#ifdef HELIDE_ANARI_DEFINITIONS
ANARI_TYPEFOR_DEFINITION(visionaray::uint2);
ANARI_TYPEFOR_DEFINITION(visionaray::uint3);
ANARI_TYPEFOR_DEFINITION(visionaray::uint4);
ANARI_TYPEFOR_DEFINITION(visionaray::float2);
ANARI_TYPEFOR_DEFINITION(visionaray::float3);
ANARI_TYPEFOR_DEFINITION(visionaray::float4);
ANARI_TYPEFOR_DEFINITION(visionaray::box1);
ANARI_TYPEFOR_DEFINITION(visionaray::box2);
ANARI_TYPEFOR_DEFINITION(visionaray::aabb);
ANARI_TYPEFOR_DEFINITION(visionaray::aabbi);
ANARI_TYPEFOR_DEFINITION(visionaray::mat4);
#endif
} // namespace anari