-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathfaster.cpp
296 lines (277 loc) · 9.5 KB
/
faster.cpp
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
#include "precomp.h"
#include "faster.h"
// THIS SOURCE FILE:
// Code for the article "How to Build a BVH", part 2: faster rays.
// This version improves ray traversal speed using ordered traversal
// and better split plane orientation / positions based on the SAH
// (Surface Area Heuristic).
// Feel free to copy this code to your own framework. Absolutely no
// rights are reserved. No responsibility is accepted either.
// For updates, follow me on twitter: @j_bikker.
TheApp* CreateApp() { return new FasterRaysApp(); }
// enable the use of SSE in the AABB intersection function
#define USE_SSE
// triangle count
#define N 12582 // hardcoded for the unity vehicle mesh
// forward declarations
void Subdivide( uint nodeIdx );
void UpdateNodeBounds( uint nodeIdx );
// minimal structs
struct Tri { float3 vertex0, vertex1, vertex2; float3 centroid; };
struct BVHNode
{
union { struct { float3 aabbMin; uint leftFirst; }; __m128 aabbMin4; };
union { struct { float3 aabbMax; uint triCount; }; __m128 aabbMax4; };
bool isLeaf() { return triCount > 0; }
};
struct aabb
{
float3 bmin = 1e30f, bmax = -1e30f;
void grow( float3 p ) { bmin = fminf( bmin, p ); bmax = fmaxf( bmax, p ); }
float area()
{
float3 e = bmax - bmin; // box extent
return e.x * e.y + e.y * e.z + e.z * e.x;
}
};
__declspec(align(64)) struct Ray
{
Ray() { O4 = D4 = rD4 = _mm_set1_ps( 1 ); }
union { struct { float3 O; float dummy1; }; __m128 O4; };
union { struct { float3 D; float dummy2; }; __m128 D4; };
union { struct { float3 rD; float dummy3; }; __m128 rD4; };
float t = 1e30f;
};
// application data
Tri tri[N];
uint triIdx[N];
BVHNode* bvhNode = 0;
uint rootNodeIdx = 0, nodesUsed = 2;
// functions
void IntersectTri( Ray& ray, const Tri& tri )
{
const float3 edge1 = tri.vertex1 - tri.vertex0;
const float3 edge2 = tri.vertex2 - tri.vertex0;
const float3 h = cross( ray.D, edge2 );
const float a = dot( edge1, h );
if (a > -0.0001f && a < 0.0001f) return; // ray parallel to triangle
const float f = 1 / a;
const float3 s = ray.O - tri.vertex0;
const float u = f * dot( s, h );
if (u < 0 || u > 1) return;
const float3 q = cross( s, edge1 );
const float v = f * dot( ray.D, q );
if (v < 0 || u + v > 1) return;
const float t = f * dot( edge2, q );
if (t > 0.0001f) ray.t = min( ray.t, t );
}
inline float IntersectAABB( const Ray& ray, const float3 bmin, const float3 bmax )
{
float tx1 = (bmin.x - ray.O.x) * ray.rD.x, tx2 = (bmax.x - ray.O.x) * ray.rD.x;
float tmin = min( tx1, tx2 ), tmax = max( tx1, tx2 );
float ty1 = (bmin.y - ray.O.y) * ray.rD.y, ty2 = (bmax.y - ray.O.y) * ray.rD.y;
tmin = max( tmin, min( ty1, ty2 ) ), tmax = min( tmax, max( ty1, ty2 ) );
float tz1 = (bmin.z - ray.O.z) * ray.rD.z, tz2 = (bmax.z - ray.O.z) * ray.rD.z;
tmin = max( tmin, min( tz1, tz2 ) ), tmax = min( tmax, max( tz1, tz2 ) );
if (tmax >= tmin && tmin < ray.t && tmax > 0) return tmin; else return 1e30f;
}
float IntersectAABB_SSE( const Ray& ray, const __m128& bmin4, const __m128& bmax4 )
{
static __m128 mask4 = _mm_cmpeq_ps( _mm_setzero_ps(), _mm_set_ps( 1, 0, 0, 0 ) );
__m128 t1 = _mm_mul_ps( _mm_sub_ps( _mm_and_ps( bmin4, mask4 ), ray.O4 ), ray.rD4 );
__m128 t2 = _mm_mul_ps( _mm_sub_ps( _mm_and_ps( bmax4, mask4 ), ray.O4 ), ray.rD4 );
__m128 vmax4 = _mm_max_ps( t1, t2 ), vmin4 = _mm_min_ps( t1, t2 );
float tmax = min( vmax4.m128_f32[0], min( vmax4.m128_f32[1], vmax4.m128_f32[2] ) );
float tmin = max( vmin4.m128_f32[0], max( vmin4.m128_f32[1], vmin4.m128_f32[2] ) );
if (tmax >= tmin && tmin < ray.t && tmax > 0) return tmin; else return 1e30f;
}
void IntersectBVH( Ray& ray )
{
BVHNode* node = &bvhNode[rootNodeIdx], * stack[64];
uint stackPtr = 0;
while (1)
{
if (node->isLeaf())
{
for (uint i = 0; i < node->triCount; i++)
IntersectTri( ray, tri[triIdx[node->leftFirst + i]] );
if (stackPtr == 0) break; else node = stack[--stackPtr];
continue;
}
BVHNode* child1 = &bvhNode[node->leftFirst];
BVHNode* child2 = &bvhNode[node->leftFirst + 1];
#ifdef USE_SSE
float dist1 = IntersectAABB_SSE( ray, child1->aabbMin4, child1->aabbMax4 );
float dist2 = IntersectAABB_SSE( ray, child2->aabbMin4, child2->aabbMax4 );
#else
float dist1 = IntersectAABB( ray, child1->aabbMin, child1->aabbMax );
float dist2 = IntersectAABB( ray, child2->aabbMin, child2->aabbMax );
#endif
if (dist1 > dist2) { swap( dist1, dist2 ); swap( child1, child2 ); }
if (dist1 == 1e30f)
{
if (stackPtr == 0) break; else node = stack[--stackPtr];
}
else
{
node = child1;
if (dist2 != 1e30f) stack[stackPtr++] = child2;
}
}
}
void BuildBVH()
{
// create the BVH node pool
bvhNode = (BVHNode*)_aligned_malloc( sizeof( BVHNode ) * N * 2, 64 );
// populate triangle index array
for (int i = 0; i < N; i++) triIdx[i] = i;
// calculate triangle centroids for partitioning
for (int i = 0; i < N; i++)
tri[i].centroid = (tri[i].vertex0 + tri[i].vertex1 + tri[i].vertex2) * 0.3333f;
// assign all triangles to root node
BVHNode& root = bvhNode[rootNodeIdx];
root.leftFirst = 0, root.triCount = N;
UpdateNodeBounds( rootNodeIdx );
// subdivide recursively
Timer t;
Subdivide( rootNodeIdx );
printf( "BVH (%i nodes) constructed in %.2fms.\n", nodesUsed, t.elapsed() * 1000 );
}
void UpdateNodeBounds( uint nodeIdx )
{
BVHNode& node = bvhNode[nodeIdx];
node.aabbMin = float3( 1e30f );
node.aabbMax = float3( -1e30f );
for (uint first = node.leftFirst, i = 0; i < node.triCount; i++)
{
uint leafTriIdx = triIdx[first + i];
Tri& leafTri = tri[leafTriIdx];
node.aabbMin = fminf( node.aabbMin, leafTri.vertex0 );
node.aabbMin = fminf( node.aabbMin, leafTri.vertex1 );
node.aabbMin = fminf( node.aabbMin, leafTri.vertex2 );
node.aabbMax = fmaxf( node.aabbMax, leafTri.vertex0 );
node.aabbMax = fmaxf( node.aabbMax, leafTri.vertex1 );
node.aabbMax = fmaxf( node.aabbMax, leafTri.vertex2 );
}
}
float EvaluateSAH( BVHNode& node, int axis, float pos )
{
// determine triangle counts and bounds for this split candidate
aabb leftBox, rightBox;
int leftCount = 0, rightCount = 0;
for (uint i = 0; i < node.triCount; i++)
{
Tri& triangle = tri[triIdx[node.leftFirst + i]];
if (triangle.centroid[axis] < pos)
{
leftCount++;
leftBox.grow( triangle.vertex0 );
leftBox.grow( triangle.vertex1 );
leftBox.grow( triangle.vertex2 );
}
else
{
rightCount++;
rightBox.grow( triangle.vertex0 );
rightBox.grow( triangle.vertex1 );
rightBox.grow( triangle.vertex2 );
}
}
float cost = leftCount * leftBox.area() + rightCount * rightBox.area();
return cost > 0 ? cost : 1e30f;
}
void Subdivide( uint nodeIdx )
{
// terminate recursion
BVHNode& node = bvhNode[nodeIdx];
// determine split axis using SAH
int bestAxis = -1;
float bestPos = 0, bestCost = 1e30f;
for (int axis = 0; axis < 3; axis++) for (uint i = 0; i < node.triCount; i++)
{
Tri& triangle = tri[triIdx[node.leftFirst + i]];
float candidatePos = triangle.centroid[axis];
float cost = EvaluateSAH( node, axis, candidatePos );
if (cost < bestCost)
bestPos = candidatePos, bestAxis = axis, bestCost = cost;
}
int axis = bestAxis;
float splitPos = bestPos;
float3 e = node.aabbMax - node.aabbMin; // extent of parent
float parentArea = e.x * e.y + e.y * e.z + e.z * e.x;
float parentCost = node.triCount * parentArea;
if (bestCost >= parentCost) return;
// in-place partition
int i = node.leftFirst;
int j = i + node.triCount - 1;
while (i <= j)
{
if (tri[triIdx[i]].centroid[axis] < splitPos)
i++;
else
swap( triIdx[i], triIdx[j--] );
}
// abort split if one of the sides is empty
int leftCount = i - node.leftFirst;
if (leftCount == 0 || leftCount == node.triCount) return;
// create child nodes
int leftChildIdx = nodesUsed++;
int rightChildIdx = nodesUsed++;
bvhNode[leftChildIdx].leftFirst = node.leftFirst;
bvhNode[leftChildIdx].triCount = leftCount;
bvhNode[rightChildIdx].leftFirst = i;
bvhNode[rightChildIdx].triCount = node.triCount - leftCount;
node.leftFirst = leftChildIdx;
node.triCount = 0;
UpdateNodeBounds( leftChildIdx );
UpdateNodeBounds( rightChildIdx );
// recurse
Subdivide( leftChildIdx );
Subdivide( rightChildIdx );
}
void FasterRaysApp::Init()
{
FILE* file = fopen( "assets/unity.tri", "r" );
float a, b, c, d, e, f, g, h, i;
for (int t = 0; t < N; t++)
{
fscanf( file, "%f %f %f %f %f %f %f %f %f\n",
&a, &b, &c, &d, &e, &f, &g, &h, &i );
tri[t].vertex0 = float3( a, b, c );
tri[t].vertex1 = float3( d, e, f );
tri[t].vertex2 = float3( g, h, i );
}
fclose( file );
// construct the BVH
BuildBVH();
}
void FasterRaysApp::Tick( float deltaTime )
{
// draw the scene
screen->Clear( 0 );
// define the corners of the screen in worldspace
float3 p0( -2.5f, 0.8f, -0.5f ), p1( -0.5f, 0.8f, -0.5f ), p2( -2.5f, -1.2f, -0.5f );
Ray ray;
Timer t;
// render tiles of pixels
for (int y = 0; y < SCRHEIGHT; y += 4) for (int x = 0; x < SCRWIDTH; x += 4)
{
// render a single tile
for (int v = 0; v < 4; v++) for (int u = 0; u < 4; u++)
{
// calculate the position of a pixel on the screen in worldspace
float3 pixelPos = p0 + (p1 - p0) * ((x + u) / (float)SCRWIDTH) + (p2 - p0) * ((y + v) / (float)SCRHEIGHT);
// define the ray in worldspace
ray.O = float3( -1.5f, -0.2f, -2.5f );
ray.D = normalize( pixelPos - ray.O ), ray.t = 1e30f;
// calculare reciprocal ray directions to speedup AABB intersections
ray.rD = float3( 1 / ray.D.x, 1 / ray.D.y, 1 / ray.D.z );
IntersectBVH( ray );
uint c = 500 - (int)(ray.t * 42);
if (ray.t < 1e30f) screen->Plot( x + u, y + v, c * 0x10101 );
}
}
float elapsed = t.elapsed() * 1000;
printf( "tracing time: %.2fms (%5.2fK rays/s)\n", elapsed, sqr( 630 ) / elapsed );
}
// EOF