forked from michal1000w/GPUSmoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRendererCommon.h
149 lines (126 loc) · 3.9 KB
/
RendererCommon.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
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
#pragma once
#define _USE_MATH_DEFINES
#include <cmath>
#include <chrono>
#include <fstream>
#include <nanovdb/NanoVDB.h>
#include "openvdb/ComputePrimitives.h"
inline __hostdev__ uint32_t CompactBy1(uint32_t x)
{
x &= 0x55555555;
x = (x ^ (x >> 1)) & 0x33333333;
x = (x ^ (x >> 2)) & 0x0f0f0f0f;
x = (x ^ (x >> 4)) & 0x00ff00ff;
x = (x ^ (x >> 8)) & 0x0000ffff;
return x;
}
inline __hostdev__ uint32_t SeparateBy1(uint32_t x)
{
x &= 0x0000ffff;
x = (x ^ (x << 8)) & 0x00ff00ff;
x = (x ^ (x << 4)) & 0x0f0f0f0f;
x = (x ^ (x << 2)) & 0x33333333;
x = (x ^ (x << 1)) & 0x55555555;
return x;
}
inline __hostdev__ void mortonDecode(uint32_t code, uint32_t& x, uint32_t& y)
{
x = CompactBy1(code);
y = CompactBy1(code >> 1);
}
inline __hostdev__ void mortonEncode(uint32_t& code, uint32_t x, uint32_t y)
{
code = SeparateBy1(x) | (SeparateBy1(y) << 1);
}
template<typename RenderFn, typename GridT>
inline float renderImage(bool useCuda, const RenderFn renderOp, int width, int height, float* image, const GridT* grid)
{
using ClockT = std::chrono::high_resolution_clock;
auto t0 = ClockT::now();
computeForEach( /*512 BLOCK_SIZE*/
useCuda, width * height, /*BLOCK_SIZE*/512, __FILE__, __LINE__, [renderOp, image, grid] __hostdev__(int start, int end) {
renderOp(start, end, image, grid);
});
computeSync(useCuda, __FILE__, __LINE__);
auto t1 = ClockT::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count() / 1000.f;
return duration;
}
inline void saveImage(const std::string& filename, int width, int height, const float* image)
{
const auto isLittleEndian = []() -> bool {
static int x = 1;
static bool result = reinterpret_cast<uint8_t*>(&x)[0] == 1;
return result;
};
float scale = 1.0f;
if (isLittleEndian())
scale = -scale;
std::fstream fs(filename, std::ios::out | std::ios::binary);
if (!fs.is_open()) {
throw std::runtime_error("Unable to open file: " + filename);
}
fs << "Pf\n"
<< width << "\n"
<< height << "\n"
<< scale << "\n";
for (int i = 0; i < width * height; ++i) {
float r = image[i];
fs.write((char*)&r, sizeof(float));
}
}
template<typename Vec3T>
struct RayGenOp
{
float mWBBoxDimZ;
Vec3T mWBBoxCenter;
inline RayGenOp(float wBBoxDimZ, Vec3T wBBoxCenter)
: mWBBoxDimZ(wBBoxDimZ)
, mWBBoxCenter(wBBoxCenter)
{
}
inline __hostdev__ void operator()(int i, int w, int h, Vec3T& outOrigin, Vec3T& outDir) const
{
// perspective camera along Z-axis...
uint32_t x, y;
#if 0
mortonDecode(i, x, y);
#else
x = i % w;
y = i / w;
#endif
const float fov = 45.f;
const float u = (float(x) + 0.5f) / w;
const float v = (float(y) + 0.5f) / h;
const float aspect = w / float(h);
const float Px = (2.f * u - 1.f) * tanf(fov / 2 * 3.14159265358979323846f / 180.f) * aspect;
const float Py = (2.f * v - 1.f) * tanf(fov / 2 * 3.14159265358979323846f / 180.f);
const Vec3T origin = mWBBoxCenter + Vec3T(0, 0, mWBBoxDimZ);
Vec3T dir(Px, Py, -1.f);
dir.normalize();
outOrigin = origin;
outDir = dir;
}
};
struct CompositeOp
{
inline __hostdev__ void operator()(float* outImage, int i, int w, int h, float value, float alpha) const
{
uint32_t x, y;
int offset;
#if 0
mortonDecode(i, x, y);
offset = x + y * w;
#else
x = i % w;
y = i / w;
offset = i;
#endif
// checkerboard background...
const int mask = 1 << 7;
const float bg = ((x & mask) ^ (y & mask)) ? 1.0f : 0.5f;
outImage[offset] = alpha * value + (1.0f - alpha) * bg;
}
};