forked from nmoehrle/libcacc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracing.cu
122 lines (106 loc) · 3.71 KB
/
tracing.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
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
/*
* Copyright (C) 2015, Nils Moehrle
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#include "tracing.h"
#include "primitives.h"
texture<uint4, 1> nodes;
texture<float4, 1> aabbs;
texture<float4, 1> tris;
CACC_NAMESPACE_BEGIN
TRACING_NAMESPACE_BEGIN
void bind_textures(BVHTree<DEVICE>::Data const bvh_tree) {
assert(sizeof(BVHTree<DEVICE>::Node) == sizeof(uint4));
assert(sizeof(AABB) == 2 * sizeof(float4));
assert(sizeof(Tri) == 3 * sizeof(float4));
CHECK(cudaBindTexture(NULL, nodes, bvh_tree.nodes_ptr,
bvh_tree.num_nodes * sizeof(BVHTree<DEVICE>::Node)));
CHECK(cudaBindTexture(NULL, aabbs, bvh_tree.aabbs_ptr,
bvh_tree.num_nodes * 2 * sizeof(float4)));
CHECK(cudaBindTexture(NULL, tris, bvh_tree.tris_ptr,
bvh_tree.num_faces * 3 * sizeof(float4)));
}
__device__ __forceinline__
BVHTree<DEVICE>::Node load_node(uint idx) {
BVHTree<DEVICE>::Node node;
node.rllf = tex1Dfetch(nodes, idx);
return node;
}
__device__ __forceinline__
AABB load_aabb(uint idx) {
AABB aabb;
float4 min = tex1Dfetch(aabbs, 2 * idx + 0);
aabb.min = Vec3f(min.x, min.y, min.z);
float4 max = tex1Dfetch(aabbs, 2 * idx + 1);
aabb.max = Vec3f(max.x, max.y, max.z);
return aabb;
}
__device__ __forceinline__
Tri load_tri(uint idx) {
Tri tri;
float4 a = tex1Dfetch(tris, 3 * idx + 0);
tri.a = Vec3f(a.x, a.y, a.z);
float4 b = tex1Dfetch(tris, 3 * idx + 1);
tri.b = Vec3f(b.x, b.y, b.z);
float4 c = tex1Dfetch(tris, 3 * idx + 2);
tri.c = Vec3f(c.x, c.y, c.z);
return tri;
}
__device__
void trace(BVHTree<DEVICE>::Data const bvh_tree,
Ray const ray, uint * hit_face_id_ptr) {
const int tx = threadIdx.x;
float t = inf;
uint hit_face_id = NAI;
uint gstack[GSTACK_SIZE];
uint __shared__ sstack[SSTACK_SIZE * TRACE_BLOCK_SIZE];
uint node_idx = 0;
int stack_idx = -1;
while (true) {
BVHTree<DEVICE>::Node node;
node = load_node(node_idx);
if (node.left != NAI && node.right != NAI) {
float tmin_left, tmin_right;
AABB aabb_left = load_aabb(node.left);
bool left = intersect(ray, aabb_left, &tmin_left);
AABB aabb_right = load_aabb(node.right);
bool right = intersect(ray, aabb_right, &tmin_right);
if (left && right) {
uint other;
if (tmin_left < tmin_right) {
other = node.right;
node_idx = node.left;
} else {
other = node.left;
node_idx = node.right;
}
if (++stack_idx < SSTACK_SIZE) sstack[SSTACK_SIZE * tx + stack_idx] = other;
else gstack[stack_idx] = other;
} else {
if (right) node_idx = node.right;
if (left) node_idx = node.left;
}
if (!left && !right) {
if (stack_idx < 0) break;
if (stack_idx < SSTACK_SIZE) node_idx = sstack[SSTACK_SIZE * tx + stack_idx--];
else node_idx = gstack[stack_idx--];
}
} else {
for (uint i = node.first; i < node.last; ++i) {
Tri tri = load_tri(i);
if (intersect(ray, tri, &t)) {
hit_face_id = bvh_tree.indices_ptr[i];
}
}
if (stack_idx < 0) break;
if (stack_idx < SSTACK_SIZE) node_idx = sstack[SSTACK_SIZE * tx + stack_idx--];
else node_idx = gstack[stack_idx--];
}
}
*hit_face_id_ptr = hit_face_id;
}
TRACING_NAMESPACE_END
CACC_NAMESPACE_END