-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptix_kernels.cu
93 lines (69 loc) · 3.09 KB
/
optix_kernels.cu
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
#include "multi_level_instancing_shared.h"
using namespace Shared;
RT_PIPELINE_LAUNCH_PARAMETERS PipelineLaunchParameters plp;
struct HitPointParameter {
float b1, b2;
int32_t primIndex;
CUDA_DEVICE_FUNCTION CUDA_INLINE static HitPointParameter get() {
HitPointParameter ret;
float2 bc = optixGetTriangleBarycentrics();
ret.b1 = bc.x;
ret.b2 = bc.y;
ret.primIndex = optixGetPrimitiveIndex();
return ret;
}
};
struct HitGroupSBTRecordData {
GeometryData geomData;
CUDA_DEVICE_FUNCTION CUDA_INLINE static const HitGroupSBTRecordData &get() {
return *reinterpret_cast<HitGroupSBTRecordData*>(optixGetSbtDataPointer());
}
};
CUDA_DEVICE_KERNEL void RT_RG_NAME(raygen)() {
uint2 launchIndex = make_uint2(optixGetLaunchIndex().x, optixGetLaunchIndex().y);
PCG32RNG rng = plp.rngBuffer[launchIndex];
float x = static_cast<float>(launchIndex.x + rng.getFloat0cTo1o()) / plp.imageSize.x;
float y = static_cast<float>(launchIndex.y + rng.getFloat0cTo1o()) / plp.imageSize.y;
float vh = 2 * std::tan(plp.camera.fovY * 0.5f);
float vw = plp.camera.aspect * vh;
float3 origin = plp.camera.position;
float3 direction = normalize(plp.camera.orientation * make_float3(vw * (0.5f - x), vh * (0.5f - y), 1));
// JP: モーションブラーの効果をわかりやすくするために画像中の位置ごとに時間幅を変える。
// EN: Use different duration depending on position in the image to make it easy to see
// the motion blur effect.
float timeRange = plp.timeEnd - plp.timeBegin;
timeRange *= ((y < 0.5f ? 2 : 0) + (x > 0.5f ? 1 : 0)) / 3.0f;
float time = plp.timeBegin + timeRange * rng.getFloat0cTo1o();
float3 color;
MyPayloadSignature::trace(
plp.travHandle, origin, direction,
0.0f, FLT_MAX, time, 0xFF, OPTIX_RAY_FLAG_NONE,
RayType_Primary, NumRayTypes, RayType_Primary,
color);
plp.rngBuffer[launchIndex] = rng;
float3 curResult = color;
float curWeight = 1.0f / (1 + plp.numAccumFrames);
float3 prevResult = getXYZ(plp.accumBuffer[launchIndex]);
curResult = (1 - curWeight) * prevResult + curWeight * curResult;
plp.accumBuffer[launchIndex] = make_float4(curResult, 1.0f);
}
CUDA_DEVICE_KERNEL void RT_MS_NAME(miss)() {
float3 color = make_float3(0, 0, 0.1f);
MyPayloadSignature::set(&color);
}
CUDA_DEVICE_KERNEL void RT_CH_NAME(closesthit)() {
auto sbtr = HitGroupSBTRecordData::get();
const GeometryData &geom = sbtr.geomData;
auto hp = HitPointParameter::get();
const Triangle &triangle = geom.triangleBuffer[hp.primIndex];
const Vertex &v0 = geom.vertexBuffer[triangle.index0];
const Vertex &v1 = geom.vertexBuffer[triangle.index1];
const Vertex &v2 = geom.vertexBuffer[triangle.index2];
float b0 = 1 - (hp.b1 + hp.b2);
float3 sn = b0 * v0.normal + hp.b1 * v1.normal + hp.b2 * v2.normal;
sn = normalize(sn);
// JP: 法線を可視化。
// EN: Visualize the normal.
float3 color = 0.5f * sn + make_float3(0.5f);
MyPayloadSignature::set(&color);
}