summaryrefslogtreecommitdiff
path: root/src/kernel.cu
blob: 5794057a50ca8f210f07bf7785a0c00c29175f6d (plain)
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
//-*-c-*-
#include <math_constants.h>
#include <curand_kernel.h>

#include "linalg.h"
#include "matrix.h"
#include "rotate.h"
#include "intersect.h"

#define STACK_SIZE 500

/* flattened triangle mesh */
texture<float4, 1, cudaReadModeElementType> mesh;

/* lower/upper bounds for the bounding box associated with each node/leaf */
texture<float4, 1, cudaReadModeElementType> upper_bounds;
texture<float4, 1, cudaReadModeElementType> lower_bounds;

/* map to child nodes/triangles and the number of child nodes/triangles */
texture<unsigned int, 1, cudaReadModeElementType> node_map;
texture<unsigned int, 1, cudaReadModeElementType> node_length;

__device__ float3 make_float3(const float4 &a)
{
	return make_float3(a.x, a.y, a.z);
}

/* Test the intersection between a ray starting at `origin` traveling in the
   direction `direction` and the bounding box around node `i`. If the ray
   intersects the bounding box return true, else return false. */
__device__ bool intersect_node(const float3 &origin, const float3 &direction, const int &i)
{
	float3 lower_bound = make_float3(tex1Dfetch(lower_bounds, i));
	float3 upper_bound = make_float3(tex1Dfetch(upper_bounds, i));

	return intersect_box(origin, direction, lower_bound, upper_bound);
}

/* Find the intersection between a ray starting at `origin` traveling in the
   direction `direction` and the global mesh texture. If the ray intersects
   the texture return the index of the triangle which the ray intersected,
   else return -1. */
__device__ int intersect_mesh(const float3 &origin, const float3& direction, const int start_node, const int first_node)
{
	int triangle_idx = -1;

	float distance;
	float min_distance;

	if (!intersect_node(origin, direction, start_node))
		return -1;

	int stack[STACK_SIZE];

	int *head = &stack[0];
	int *node = &stack[1];
	int *tail = &stack[STACK_SIZE-1];
	*node = start_node;

	int i;

	do
	{
		int index = tex1Dfetch(node_map, *node);
		int length = tex1Dfetch(node_length, *node);

		if (*node >= first_node)
		{
			for (i=0; i < length; i++)
				if (intersect_node(origin, direction, index+i))
					*node++ = index+i;

			if (node == head)
				break;

			node--;
		}
		else // node is a leaf
		{
			for (i=0; i < length; i++)
			{
				int mesh_idx = 3*(index + i);

				float3 v0 = make_float3(tex1Dfetch(mesh, mesh_idx));
				float3 v1 = make_float3(tex1Dfetch(mesh, mesh_idx+1));
				float3 v2 = make_float3(tex1Dfetch(mesh, mesh_idx+2));

				if (intersect_triangle(origin, direction, v0, v1, v2, distance))
				{
					if (triangle_idx == -1)
					{
						triangle_idx = index + i;
						min_distance = distance;
						continue;
					}

					if (distance < min_distance)
					{
						triangle_idx = index + i;
						min_distance = distance;
					}
				}
			} // triangle loop

			node--;

		} // node is a leaf

	} // while loop
	while (node != head);

	return triangle_idx;
}

__device__ curandState rng_states[256*512];

extern "C"
{

/* Initialize random number states */
__global__ void init_rng(unsigned long long seed, unsigned long long offset)
{
	int idx = blockIdx.x*blockDim.x + threadIdx.x;
	curand_init(seed, idx, offset, rng_states+idx);
}

/* */
__global__ void uniform_sphere(int nthreads, float3 *points)
{
	int idx = blockIdx.x*blockDim.x + threadIdx.x;

	if (idx >= nthreads)
		return;

	//curandState rng = *(rng_states+idx);

}

/* Translate `points` by the vector `v` */
__global__ void translate(int nthreads, float3 *points, float3 v)
{
	int idx = blockIdx.x*blockDim.x + threadIdx.x;

	if (idx >= nthreads)
		return;

	*(points+idx) += v;
}

/* Rotate `points` through an angle `phi` counter-clockwise about the
   axis `axis` (when looking towards +infinity). */
__global__ void rotate(int nthreads, float3 *points, float phi, float3 axis)
{
	int idx = blockIdx.x*blockDim.x + threadIdx.x;

	if (idx >= nthreads)
		return;

	*(points+idx) = rotate(*(points+idx), phi, axis);
}

/* Trace the rays starting at `origins` traveling in the direction `directions`
   to their intersection with the global mesh. If the ray intersects the mesh
   set the pixel associated with the ray to a 32 bit color whose brightness is
   determined by the cosine of the angle between the ray and the normal of the
   triangle it intersected, else set the pixel to 0. */
__global__ void ray_trace(int nthreads, float3 *origins, float3 *directions, int start_node, int first_node, int *pixels)
{
	int idx = blockIdx.x*blockDim.x + threadIdx.x;

	if (idx >= nthreads)
		return;

	float3 origin = *(origins+idx);
	float3 direction = *(directions+idx);
	direction /= norm(direction);

	int intersection_idx = intersect_mesh(origin, direction, start_node, first_node);

	if (intersection_idx == -1)
	{
		*(pixels+idx) = 0;
	}
	else
	{
		int mesh_idx = 3*intersection_idx;

		float3 v0 = make_float3(tex1Dfetch(mesh, mesh_idx));
		float3 v1 = make_float3(tex1Dfetch(mesh, mesh_idx+1));
		float3 v2 = make_float3(tex1Dfetch(mesh, mesh_idx+2));

		*(pixels+idx) = get_color(direction, v0, v1, v2);
	}
} // ray_trace

/* Propagate the photons starting at `origins` traveling in the direction
   `directions` to their intersection with the global mesh. If the ray
   intersects the mesh set the hit_solid array value associated with the
   photon to the triangle index of the triangle the photon intersected, else
   set the hit_solid array value to -1. */
__global__ void propagate(int nthreads, float3 *origins, float3 *directions, int start_node, int first_node, int *hit_triangles)
{
	int idx = blockIdx.x*blockDim.x + threadIdx.x;

	if (idx >= nthreads)
		return;

	float3 origin = *(origins+idx);
	float3 direction = *(directions+idx);
	direction /= norm(direction);

	*(hit_triangles+idx) = intersect_mesh(origin, direction, start_node, first_node);

} // propagate

} // extern "c"