-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgpu_ray_surface_intersect_v1.cu
More file actions
334 lines (304 loc) · 11 KB
/
Copy pathgpu_ray_surface_intersect_v1.cu
File metadata and controls
334 lines (304 loc) · 11 KB
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
//Copyright (c) 2022, Raymond Leung
//All rights reserved.
//
//This source code is licensed under the BSD-3-clause license found
//in the LICENSE.md file in the root directory of this source tree.
//
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
#include <typeinfo>
#include <vector>
using namespace std;
#define EPSILON 0.000001
//-----------------------------------------------
// This implementation corresponds to version v1
//-----------------------------------------------
static void HandleError(cudaError_t err, const char *file, int line)
{
if (err != cudaSuccess) {
printf("%s in %s at line %d\n", cudaGetErrorString( err ), file, line);
exit( EXIT_FAILURE );
}
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))
__device__ void triangleBbox(const float *v0, const float *v1,
const float *v2, float *vMin, float *vMax)
{
for (int i = 0; i < 3; i++) {
if (v0[i] > v1[i]) {
if (v0[i] > v2[i]) {
vMax[i] = v0[i];
vMin[i] = min(v1[i], v2[i]);
}
else {
vMax[i] = v2[i];
vMin[i] = v1[i];
}
}
else {
if (v1[i] > v2[i]) {
vMax[i] = v1[i];
vMin[i] = min(v0[i], v2[i]);
}
else {
vMax[i] = v2[i];
vMin[i] = v0[i];
}
}
}
}
__device__ void lineSegmentBbox(const float *p0, const float *p1,
float *vMin, float *vMax)
{
for (int i = 0; i < 3; i++) {
if (p0[i] > p1[i]) {
vMax[i] = p0[i];
vMin[i] = p1[i];
}
else {
vMax[i] = p1[i];
vMin[i] = p0[i];
}
}
}
__device__ bool notOverlap(const float *tMin, const float *tMax,
const float *rayMin, const float *rayMax)
{ //this version uses precomputed rayMin and rayMax
if (rayMin[0] > tMax[0] || rayMax[0] < tMin[0])
return true;
if (rayMin[1] > tMax[1] || rayMax[1] < tMin[1])
return true;
if (rayMin[2] > tMax[2] || rayMax[2] < tMin[2])
return true;
return false;
}
__device__ void subtract(const float *a, const float *b, float *out)
{
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
}
__device__ void dot(const float *a, const float *b, float &out)
{
out = a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}
__device__ void cross(const float *a, const float *b, float *out)
{
out[0] = a[1]*b[2] - a[2]*b[1];
out[1] = a[2]*b[0] - a[0]*b[2];
out[2] = a[0]*b[1] - a[1]*b[0];
}
__device__ int intersectMoller(
const float *v0, const float *v1, const float *v2,
const float *edge1, const float *edge2,
const float *q0, const float *q1)
{
float direction[3], avec[3], bvec[3], tvec[3], t, u, v, det, inv_det;
subtract(q1, q0, direction);
cross(direction, edge2, avec);
dot(avec, edge1, det);
if (det > EPSILON) {
subtract(q0, v0, tvec);
dot(avec, tvec, u);
if (u < 0 || u > det)
return 0;
cross(tvec, edge1, bvec);
dot(bvec, direction, v);
if (v < 0 || u + v > det)
return 0;
}
else if (det < -EPSILON) {
subtract(q0, v0, tvec);
dot(avec, tvec, u);
if (u > 0 || u < det)
return 0;
cross(tvec, edge1, bvec);
dot(bvec, direction, v);
if (v > 0 || u + v < det)
return 0;
}
else
return 0;
inv_det = 1.0 / det;
dot(bvec, edge2, t);
t *= inv_det;
if (t < 0 || t > 1) {
return 0;
}
else
return 1;
}
__global__ void rbxKernel(const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
float* __restrict__ vMin,
float* __restrict__ vMax, int numRays)
{ //Pre-compute ray bounding box for all line segments,
//instead of repeating the same in each thread-block.
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < numRays) {
const float *start = &rayFrom[3*i], *finish = &rayTo[3*i];
lineSegmentBbox(start, finish, &vMin[3*i], &vMax[3*i]);
}
}
__global__ void rsiKernel(const float* __restrict__ vertices,
const int* __restrict__ triangles,
const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
const float* __restrict__ rayMin,
const float* __restrict__ rayMax,
int* __restrict__ resultsR,
int numTriangles, int numRays)
{
//Implement the ray-segment surface intersection test strategy
//of Jimenez et al (GRAPP 2014) with intersection prescreening
const int t = blockIdx.y * gridDim.x + blockIdx.x;
const int i = threadIdx.x;
//load triangle attributes into shared memory
__shared__ float triangleVerts[9], tMin[3], tMax[3], edge1[3], edge2[3];
const float *v0 = &triangleVerts[0],
*v1 = &triangleVerts[3],
*v2 = &triangleVerts[6];
if ((i == 0) && (t < numTriangles)) {
for(int j = 0; j < 3; j++) {
int v = triangles[3*t+j];
for (int k = 0; k < 3; k++) {
triangleVerts[3*j+k] = vertices[3*v+k];
}
}
triangleBbox(v0, v1, v2, tMin, tMax);
subtract(v1, v0, edge1);
subtract(v2, v0, edge2);
}
__syncthreads();
//apply the test if triangle and ray bounding boxes overlap
if (t < numTriangles) {
for (int idx = i; idx < numRays; idx += blockDim.x) {
const float *sMin = &rayMin[3*idx], *sMax = &rayMax[3*idx];
if (!notOverlap(tMin, tMax, sMin, sMax)) {
const float *start = &rayFrom[3*idx], *finish = &rayTo[3*idx];
if (intersectMoller(v0, v1, v2, edge1, edge2, start, finish)) {
resultsR[idx] = 1;
}
}
}
}
}
template <class T>
int readData(string fname, vector<T> &v, int dim=1)
{
ifstream infile(fname.c_str(), ios::binary | ios::ate);
if (! infile) {
cerr << "File " << fname << " not found" << endl;
exit(1);
}
ifstream::pos_type nbytes = infile.tellg();
infile.seekg(0, infile.beg);
const int elements = nbytes / sizeof(T);
v.resize(elements);
infile.read(reinterpret_cast<char*>(v.data()), nbytes);
cout << fname << " contains " << nbytes << " bytes, "
<< v.size() << " <" << typeid(v.front()).name() << ">, "
<< v.size() / dim << " elements" << endl;
return elements / dim;
}
template <class T>
void writeData(string fname, vector<T> &v)
{
ofstream outfile(fname.c_str(), ios::out | ios::binary);
if (! outfile) {
cerr << "Cannot create " << fname << " for writing" << endl;
exit(1);
}
outfile.write(reinterpret_cast<char*>(v.data()), v.size() * sizeof(T));
outfile.close();
}
int main(int argc, char *argv[])
{
vector<float> h_vertices;
vector<int> h_triangles;
vector<float> h_rayFrom;
vector<float> h_rayTo;
vector<int> h_crossingDetected;
int nVertices, nTriangles, nRays;
if (argc == 2 && strcmp(argv[1], "--help")==0)
{
cout << "CUDA GPU implementation of Moller-Trumbore ray-triangle intersection test\n"
<< "Optional args:\n"
<< "[1] vertices file, (nVertices,3) as binary float32[]\n"
<< "[2] triangles file, (nTriangles,3) as binary int32[]\n"
<< "[3] segment start points, (nRays,3) as binary float32[]\n"
<< "[4] segment end points, (nRays,3) as binary float32[]\n\n";
return 0;
}
//optional arguments
std::string fileVertices(argc > 1? argv[1]: "input/vertices_f32"),
fileTriangles(argc > 2? argv[2]: "input/triangles_i32"),
fileFrom(argc > 3? argv[3]: "input/rayFrom_f32"),
fileTo(argc > 4? argv[4]: "input/rayTo_f32");
//read input data into host memory
nVertices = readData(fileVertices, h_vertices, 3);
nTriangles = readData(fileTriangles, h_triangles, 3);
nRays = readData(fileFrom, h_rayFrom, 3);
assert(readData(fileTo, h_rayTo, 3) == nRays);
h_crossingDetected.resize(nRays);
cudaEvent_t start, end;
float *d_vertices, *d_rayFrom, *d_rayTo, *d_rayMin, *d_rayMax;
int *d_triangles, *d_crossingDetected;
int sz_vertices(3 * nVertices * sizeof(float)),
sz_triangles(3 * nTriangles * sizeof(int)),
sz_rays(3 * nRays * sizeof(float));
cudaMalloc(&d_vertices, sz_vertices);
cudaMalloc(&d_triangles, sz_triangles);
cudaMalloc(&d_rayFrom, sz_rays);
cudaMalloc(&d_rayTo, sz_rays);
cudaMalloc(&d_rayMin, sz_rays);
cudaMalloc(&d_rayMax, sz_rays);
cudaMalloc(&d_crossingDetected, nRays * sizeof(int));
cudaMemcpy(d_vertices, h_vertices.data(), sz_vertices, cudaMemcpyHostToDevice);
cudaMemcpy(d_triangles, h_triangles.data(), sz_triangles, cudaMemcpyHostToDevice);
cudaMemcpy(d_rayFrom, h_rayFrom.data(), sz_rays, cudaMemcpyHostToDevice);
cudaMemcpy(d_rayTo, h_rayTo.data(), sz_rays, cudaMemcpyHostToDevice);
cudaMemset(d_crossingDetected, 0, nRays * sizeof(int));
//grid partitions: logGridX = ceil(log2(sqrt(n)));
// logGridY = ceil(log2(n)-logGridX);
// 2^(logGridX+logGridY) >= n;
int logGridX = ceil(log2f(sqrt(static_cast<float>(nTriangles))));
int logGridY = ceil(log2f(nTriangles) - logGridX);
int gridsX = static_cast<int>(pow(2, logGridX)),
gridsY = static_cast<int>(pow(2, logGridY)),
blockX = 1024;
dim3 dimGrid(gridsX, gridsY, 1);
dim3 dimBlock(blockX, 1, 1);
cout << blockX << " threads/block, grids: (" << gridsX
<< "," << gridsY << ")" << endl;
cudaEventCreate(&start);
cudaEventCreate(&end);
cudaEventRecord(start);
int gridX = (int)ceil((float)nRays / blockX);
rbxKernel<<<gridX, blockX>>>(d_rayFrom, d_rayTo, d_rayMin, d_rayMax, nRays);
cudaDeviceSynchronize();
rsiKernel<<<dimGrid, dimBlock>>>(d_vertices, d_triangles,
d_rayFrom, d_rayTo, d_rayMin, d_rayMax,
d_crossingDetected, nTriangles, nRays);
cudaEventRecord(end);
cudaEventSynchronize(end);
float time = 0;
cudaEventElapsedTime(&time, start, end);
HANDLE_ERROR(cudaMemcpy(h_crossingDetected.data(), d_crossingDetected,
nRays * sizeof(int), cudaMemcpyDeviceToHost));
printf("Device Variable Copying:\t%s\n", cudaGetErrorString(cudaGetLastError()));
ofstream fw("results_v1.csv", std::ofstream::out);
if (fw.is_open()) {
for (int i = 0; i < nRays; i++) {
fw << h_crossingDetected[i] << "\n";
}
fw.close();
}
writeData("results_i32_v1", h_crossingDetected);
cout << "Processing time: ";
cout << time << " ms" << endl;
}