-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtraverse_bvh2.cl
164 lines (160 loc) · 5.77 KB
/
traverse_bvh2.cl
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
// ============================================================================
//
// T R A V E R S E _ A I L A L A I N E
//
// ============================================================================
struct BVHNodeAlt
{
float4 lmin; // unsigned left in w
float4 lmax; // unsigned right in w
float4 rmin; // unsigned triCount in w
float4 rmax; // unsigned firstTri in w
};
float4 traverse_ailalaine( global struct BVHNodeAlt* altNode, global unsigned* idx, global float4* verts, const float3 O, const float3 D, const float3 rD, const float tmax )
{
// traverse BVH
float4 hit;
hit.x = tmax;
unsigned node = 0, stack[STACK_SIZE], stackPtr = 0;
while (1)
{
// fetch the node
const float4 lmin = altNode[node].lmin, lmax = altNode[node].lmax;
const float4 rmin = altNode[node].rmin, rmax = altNode[node].rmax;
const unsigned triCount = as_uint( rmin.w );
if (triCount > 0)
{
// process leaf node
const unsigned firstTri = as_uint( rmax.w );
for (unsigned i = 0; i < triCount; i++)
{
const unsigned triIdx = idx[firstTri + i];
const float4* tri = verts + 3 * triIdx;
// triangle intersection - Möller-Trumbore
const float4 edge1 = tri[1] - tri[0], edge2 = tri[2] - tri[0];
const float3 h = cross( D, edge2.xyz );
const float a = dot( edge1.xyz, h );
if (fabs( a ) < 0.0000001f) continue;
const float f = 1 / a;
const float3 s = O - tri[0].xyz;
const float u = f * dot( s, h );
if (u < 0 || u > 1) continue;
const float3 q = cross( s, edge1.xyz );
const float v = f * dot( D, q );
if (v < 0 || u + v > 1) continue;
const float d = f * dot( edge2.xyz, q );
if (d > 0.0f && d < hit.x) hit = (float4)(d, u, v, as_float( triIdx ));
}
if (stackPtr == 0) break;
node = stack[--stackPtr];
continue;
}
unsigned left = as_uint( lmin.w ), right = as_uint( lmax.w );
// child AABB intersection tests
const float3 t1a = (lmin.xyz - O) * rD, t2a = (lmax.xyz - O) * rD;
const float3 t1b = (rmin.xyz - O) * rD, t2b = (rmax.xyz - O) * rD;
const float3 minta = fmin( t1a, t2a ), maxta = fmax( t1a, t2a );
const float3 mintb = fmin( t1b, t2b ), maxtb = fmax( t1b, t2b );
const float tmina = fmax( fmax( fmax( minta.x, minta.y ), minta.z ), 0 );
const float tminb = fmax( fmax( fmax( mintb.x, mintb.y ), mintb.z ), 0 );
const float tmaxa = fmin( fmin( fmin( maxta.x, maxta.y ), maxta.z ), hit.x );
const float tmaxb = fmin( fmin( fmin( maxtb.x, maxtb.y ), maxtb.z ), hit.x );
float dist1 = tmina > tmaxa ? 1e30f : tmina;
float dist2 = tminb > tmaxb ? 1e30f : tminb;
// traverse nearest child first
if (dist1 > dist2)
{
float h = dist1; dist1 = dist2; dist2 = h;
unsigned t = left; left = right; right = t;
}
if (dist1 == 1e30f)
{
if (stackPtr == 0) break; else node = stack[--stackPtr];
}
else
{
node = left;
if (dist2 != 1e30f) stack[stackPtr++] = right;
}
}
// write back intersection result
return hit;
}
bool isoccluded_ailalaine( global struct BVHNodeAlt* altNode, global unsigned* idx, global float4* verts, const float3 O, const float3 D, const float3 rD, const float tmax )
{
// traverse BVH
unsigned node = 0, stack[STACK_SIZE], stackPtr = 0;
while (1)
{
// fetch the node
const float4 lmin = altNode[node].lmin, lmax = altNode[node].lmax;
const float4 rmin = altNode[node].rmin, rmax = altNode[node].rmax;
const unsigned triCount = as_uint( rmin.w );
if (triCount > 0)
{
// process leaf node
const unsigned firstTri = as_uint( rmax.w );
for (unsigned i = 0; i < triCount; i++)
{
const unsigned triIdx = idx[firstTri + i];
const float4* tri = verts + 3 * triIdx;
// triangle intersection - Möller-Trumbore
const float4 edge1 = tri[1] - tri[0], edge2 = tri[2] - tri[0];
const float3 h = cross( D, edge2.xyz );
const float a = dot( edge1.xyz, h );
if (fabs( a ) < 0.0000001f) continue;
const float f = 1 / a;
const float3 s = O - tri[0].xyz;
const float u = f * dot( s, h );
if (u < 0 || u > 1) continue;
const float3 q = cross( s, edge1.xyz );
const float v = f * dot( D, q );
if (v < 0 || u + v > 1) continue;
const float d = f * dot( edge2.xyz, q );
if (d > 0.0f && d < tmax) return true;
}
if (stackPtr == 0) break;
node = stack[--stackPtr];
continue;
}
unsigned left = as_uint( lmin.w ), right = as_uint( lmax.w );
// child AABB intersection tests
const float3 t1a = (lmin.xyz - O) * rD, t2a = (lmax.xyz - O) * rD;
const float3 t1b = (rmin.xyz - O) * rD, t2b = (rmax.xyz - O) * rD;
const float3 minta = fmin( t1a, t2a ), maxta = fmax( t1a, t2a );
const float3 mintb = fmin( t1b, t2b ), maxtb = fmax( t1b, t2b );
const float tmina = fmax( fmax( fmax( minta.x, minta.y ), minta.z ), 0 );
const float tminb = fmax( fmax( fmax( mintb.x, mintb.y ), mintb.z ), 0 );
const float tmaxa = fmin( fmin( fmin( maxta.x, maxta.y ), maxta.z ), tmax );
const float tmaxb = fmin( fmin( fmin( maxtb.x, maxtb.y ), maxtb.z ), tmax );
float dist1 = tmina > tmaxa ? 1e30f : tmina;
float dist2 = tminb > tmaxb ? 1e30f : tminb;
// traverse nearest child first
if (dist1 > dist2)
{
float h = dist1; dist1 = dist2; dist2 = h;
unsigned t = left; left = right; right = t;
}
if (dist1 == 1e30f)
{
if (stackPtr == 0) break; else node = stack[--stackPtr];
}
else
{
node = left;
if (dist2 != 1e30f) stack[stackPtr++] = right;
}
}
// no hit found
return false;
}
void kernel batch_ailalaine( global struct BVHNodeAlt* altNode, global unsigned* idx, global float4* verts, global struct Ray* rayData )
{
// fetch ray
const unsigned threadId = get_global_id( 0 );
const float3 O = rayData[threadId].O.xyz;
const float3 D = rayData[threadId].D.xyz;
const float3 rD = rayData[threadId].rD.xyz;
float4 hit = traverse_ailalaine( altNode, idx, verts, O, D, rD, 1e30f );
rayData[threadId].hit = hit;
}