-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel_globals.h
118 lines (98 loc) · 2.78 KB
/
kernel_globals.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
//
// Created by 孙万捷 on 16/4/21.
//
#ifndef SUNPATHTRACER_KERNEL_GLOBALS_H
#define SUNPATHTRACER_KERNEL_GLOBALS_H
#define GLM_FORCE_INLINE
#include <glm/glm.hpp>
#include <cuda_runtime.h>
//marcos
#define IMG_BLACK glm::u8vec4(0)
//data types
struct SurfaceElement
{
float rayEpsilon;
glm::vec3 pt;
glm::vec3 normal;
unsigned int matID;
};
//functions
__inline__ __device__ void running_estimate(glm::vec3& acc_buffer, const glm::vec3& curr_est, unsigned int N)
{
acc_buffer += (curr_est - acc_buffer) / (N + 1.f);
}
__inline__ __host__ __device__ unsigned int wangHash(unsigned int a)
{
//http://raytracey.blogspot.com/2015/12/gpu-path-tracing-tutorial-2-interactive.html
a = (a ^ 61) ^ (a >> 16);
a = a + (a << 3);
a = a ^ (a >> 4);
a = a * 0x27d4eb2d;
a = a ^ (a >> 15);
return a;
}
__inline__ __host__ __device__ float illuminance(const glm::vec3& v)
{
return 0.2126f * v.x + 0.7152f * v.y + 0.0722 * v.z;
}
__device__ bool scene_intersect(const cudaScene& scene, const cudaRay& ray, SurfaceElement& se)
{
bool intersected = false;
ray.tMax = FLT_MAX;
float t = ray.tMax;
for(auto i = 0; i < scene.num_spheres; ++i)
{
const cudaSphere& sphere = scene.spheres[i];
if(sphere.Intersect(ray, &t))
{
ray.tMax = t;
intersected = true;
se.rayEpsilon = 0.0005f * ray.tMax;
se.pt = ray.PointOnRay(ray.tMax);
se.normal = sphere.GetNormal(se.pt);
se.matID = sphere.material_id;
}
}
for(auto i = 0; i < scene.num_aab; ++i)
{
const cudaAAB& aab = scene.aab[i];
if(aab.Intersect(ray, &t))
{
ray.tMax = t;
intersected = true;
se.rayEpsilon = 0.0005f * ray.tMax;
se.pt = ray.PointOnRay(ray.tMax);
se.normal = aab.GetNormal(se.pt);
se.matID = aab.material_id;
}
}
for(auto i = 0; i < scene.num_planes; ++i)
{
const cudaPlane& plane = scene.planes[i];
if(plane.Intersect(ray, &t))
{
ray.tMax = t;
intersected = true;
se.rayEpsilon = 0.001f * ray.tMax;
se.pt = ray.PointOnRay(ray.tMax);
se.normal = plane.GetNormal(se.pt);
se.matID = plane.material_id;
}
}
for(auto i = 0; i < scene.num_meshes; ++i)
{
const cudaMesh& mesh = scene.meshes[i];
int32_t id = -1;
if(mesh.Intersect(ray, &t, &id))
{
ray.tMax = t;
intersected = true;
se.rayEpsilon = 0.0005f * ray.tMax;
se.pt = ray.PointOnRay(ray.tMax);
se.normal = mesh.GetNormal(id);
se.matID = mesh.material_id;
}
}
return intersected;
}
#endif