-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathalltogether.cpp
442 lines (416 loc) · 14.1 KB
/
alltogether.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "precomp.h"
#include "alltogether.h"
// THIS SOURCE FILE:
// Code for the article "How to Build a BVH", part 6: all together now.
// This version shows how to build and maintain a BVH using
// a TLAS (top level acceleration structure) over a collection of
// BLAS'es (bottom level accstructs), with instancing.
// 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 AllTogetherApp(); }
// 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.00001f && a < 0.00001f) 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;
}
// BVH class implementation
BVH::BVH( char* triFile, int N )
{
FILE* file = fopen( triFile, "r" );
triCount = N;
tri = new Tri[N];
for (int t = 0; t < N; t++) fscanf( file, "%f %f %f %f %f %f %f %f %f\n",
&tri[t].vertex0.x, &tri[t].vertex0.y, &tri[t].vertex0.z,
&tri[t].vertex1.x, &tri[t].vertex1.y, &tri[t].vertex1.z,
&tri[t].vertex2.x, &tri[t].vertex2.y, &tri[t].vertex2.z );
bvhNode = (BVHNode*)_aligned_malloc( sizeof( BVHNode ) * N * 2, 64 );
triIdx = new uint[N];
Build();
}
void BVH::Intersect( Ray& ray )
{
BVHNode* node = &bvhNode[0], * 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 BVH::Refit()
{
Timer t;
for (int i = nodesUsed - 1; i >= 0; i--) if (i != 1)
{
BVHNode& node = bvhNode[i];
if (node.isLeaf())
{
// leaf node: adjust bounds to contained triangles
UpdateNodeBounds( i );
continue;
}
// interior node: adjust bounds to child node bounds
BVHNode& leftChild = bvhNode[node.leftFirst];
BVHNode& rightChild = bvhNode[node.leftFirst + 1];
node.aabbMin = fminf( leftChild.aabbMin, rightChild.aabbMin );
node.aabbMax = fmaxf( leftChild.aabbMax, rightChild.aabbMax );
}
printf( "BVH refitted in %.2fms ", t.elapsed() * 1000 );
}
void BVH::Build()
{
// reset node pool
nodesUsed = 2;
// populate triangle index array
for (uint i = 0; i < triCount; i++) triIdx[i] = i;
// calculate triangle centroids for partitioning
for (uint i = 0; i < triCount; i++)
tri[i].centroid = (tri[i].vertex0 + tri[i].vertex1 + tri[i].vertex2) * 0.3333f;
// assign all triangles to root node
BVHNode& root = bvhNode[0];
root.leftFirst = 0, root.triCount = triCount;
UpdateNodeBounds( 0 );
// subdivide recursively
Timer t;
Subdivide( 0 );
printf( "BVH constructed in %.2fms ", t.elapsed() * 1000 );
}
void BVH::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 BVH::FindBestSplitPlane( BVHNode& node, int& axis, float& splitPos )
{
float bestCost = 1e30f;
for (int a = 0; a < 3; a++)
{
float boundsMin = 1e30f, boundsMax = -1e30f;
for (uint i = 0; i < node.triCount; i++)
{
Tri& triangle = tri[triIdx[node.leftFirst + i]];
boundsMin = min( boundsMin, triangle.centroid[a] );
boundsMax = max( boundsMax, triangle.centroid[a] );
}
if (boundsMin == boundsMax) continue;
// populate the bins
struct Bin { aabb bounds; int triCount = 0; } bin[BINS];
float scale = BINS / (boundsMax - boundsMin);
for (uint i = 0; i < node.triCount; i++)
{
Tri& triangle = tri[triIdx[node.leftFirst + i]];
int binIdx = min( BINS - 1, (int)((triangle.centroid[a] - boundsMin) * scale) );
bin[binIdx].triCount++;
bin[binIdx].bounds.grow( triangle.vertex0 );
bin[binIdx].bounds.grow( triangle.vertex1 );
bin[binIdx].bounds.grow( triangle.vertex2 );
}
// gather data for the 7 planes between the 8 bins
float leftArea[BINS - 1], rightArea[BINS - 1];
int leftCount[BINS - 1], rightCount[BINS - 1];
aabb leftBox, rightBox;
int leftSum = 0, rightSum = 0;
for (int i = 0; i < BINS - 1; i++)
{
leftSum += bin[i].triCount;
leftCount[i] = leftSum;
leftBox.grow( bin[i].bounds );
leftArea[i] = leftBox.area();
rightSum += bin[BINS - 1 - i].triCount;
rightCount[BINS - 2 - i] = rightSum;
rightBox.grow( bin[BINS - 1 - i].bounds );
rightArea[BINS - 2 - i] = rightBox.area();
}
// calculate SAH cost for the 7 planes
scale = (boundsMax - boundsMin) / BINS;
for (int i = 0; i < BINS - 1; i++)
{
float planeCost = leftCount[i] * leftArea[i] + rightCount[i] * rightArea[i];
if (planeCost < bestCost)
axis = a, splitPos = boundsMin + scale * (i + 1), bestCost = planeCost;
}
}
return bestCost;
}
void BVH::Subdivide( uint nodeIdx )
{
// terminate recursion
BVHNode& node = bvhNode[nodeIdx];
// determine split axis using SAH
int axis;
float splitPos;
float splitCost = FindBestSplitPlane( node, axis, splitPos );
float nosplitCost = node.CalculateNodeCost();
if (splitCost >= nosplitCost) 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 );
}
// BVHInstance implementation
void BVHInstance::SetTransform( mat4& transform )
{
invTransform = transform.Inverted();
// calculate world-space bounds using the new matrix
float3 bmin = bvh->bvhNode[0].aabbMin, bmax = bvh->bvhNode[0].aabbMax;
bounds = aabb();
for (int i = 0; i < 8; i++)
bounds.grow( TransformPosition( float3( i & 1 ? bmax.x : bmin.x,
i & 2 ? bmax.y : bmin.y, i & 4 ? bmax.z : bmin.z ), transform ) );
}
void BVHInstance::Intersect( Ray& ray )
{
// backup ray and transform original
Ray backupRay = ray;
ray.O = TransformPosition( ray.O, invTransform );
ray.D = TransformVector( ray.D, invTransform );
ray.rD = float3( 1 / ray.D.x, 1 / ray.D.y, 1 / ray.D.z );
// trace ray through BVH
bvh->Intersect( ray );
// restore ray origin and direction
backupRay.t = ray.t;
ray = backupRay;
}
// TLAS implementation
TLAS::TLAS( BVHInstance* bvhList, int N )
{
// copy a pointer to the array of bottom level accstruc instances
blas = bvhList;
blasCount = N;
// allocate TLAS nodes
tlasNode = (TLASNode*)_aligned_malloc( sizeof( TLASNode ) * 2 * N, 64 );
nodesUsed = 2;
}
int TLAS::FindBestMatch( int* list, int N, int A )
{
// find BLAS B that, when joined with A, forms the smallest AABB
float smallest = 1e30f;
int bestB = -1;
for (int B = 0; B < N; B++) if (B != A)
{
float3 bmax = fmaxf( tlasNode[list[A]].aabbMax, tlasNode[list[B]].aabbMax );
float3 bmin = fminf( tlasNode[list[A]].aabbMin, tlasNode[list[B]].aabbMin );
float3 e = bmax - bmin;
float surfaceArea = e.x * e.y + e.y * e.z + e.z * e.x;
if (surfaceArea < smallest) smallest = surfaceArea, bestB = B;
}
return bestB;
}
void TLAS::Build()
{
// assign a TLASleaf node to each BLAS
int nodeIdx[256], nodeIndices = blasCount;
nodesUsed = 1;
for (uint i = 0; i < blasCount; i++)
{
nodeIdx[i] = nodesUsed;
tlasNode[nodesUsed].aabbMin = blas[i].bounds.bmin;
tlasNode[nodesUsed].aabbMax = blas[i].bounds.bmax;
tlasNode[nodesUsed].BLAS = i;
tlasNode[nodesUsed++].leftRight = 0; // makes it a leaf
}
// use agglomerative clustering to build the TLAS
int A = 0, B = FindBestMatch( nodeIdx, nodeIndices, A );
while (nodeIndices > 1)
{
int C = FindBestMatch( nodeIdx, nodeIndices, B );
if (A == C)
{
int nodeIdxA = nodeIdx[A], nodeIdxB = nodeIdx[B];
TLASNode& nodeA = tlasNode[nodeIdxA];
TLASNode& nodeB = tlasNode[nodeIdxB];
TLASNode& newNode = tlasNode[nodesUsed];
newNode.leftRight = nodeIdxA + (nodeIdxB << 16);
newNode.aabbMin = fminf( nodeA.aabbMin, nodeB.aabbMin );
newNode.aabbMax = fmaxf( nodeA.aabbMax, nodeB.aabbMax );
nodeIdx[A] = nodesUsed++;
nodeIdx[B] = nodeIdx[nodeIndices - 1];
B = FindBestMatch( nodeIdx, --nodeIndices, A );
}
else A = B, B = C;
}
tlasNode[0] = tlasNode[nodeIdx[A]];
}
void TLAS::Intersect( Ray& ray )
{
ray.rD = float3( 1 / ray.D.x, 1 / ray.D.y, 1 / ray.D.z );
TLASNode* node = &tlasNode[0], * stack[64];
uint stackPtr = 0;
while (1)
{
if (node->isLeaf())
{
blas[node->BLAS].Intersect( ray );
if (stackPtr == 0) break; else node = stack[--stackPtr];
continue;
}
TLASNode* child1 = &tlasNode[node->leftRight & 0xffff];
TLASNode* child2 = &tlasNode[node->leftRight >> 16];
float dist1 = IntersectAABB( ray, child1->aabbMin, child1->aabbMax );
float dist2 = IntersectAABB( ray, child2->aabbMin, child2->aabbMax );
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;
}
}
}
// AllTogetherApp implementation
void AllTogetherApp::Init()
{
BVH* bvh = new BVH( "assets/armadillo.tri", 30000 );
for (int i = 0; i < 256; i++)
bvhInstance[i] = BVHInstance( bvh );
tlas = TLAS( bvhInstance, 256 );
// set up spacy armadillo army
position = new float3[256];
direction = new float3[256];
orientation = new float3[256];
for( int i = 0; i < 256; i++ )
{
position[i] = float3( RandomFloat(), RandomFloat(), RandomFloat() ) - 0.5f;
position[i] *= 4;
direction[i] = normalize( position[i] ) * 0.05f;
orientation[i] = float3( RandomFloat(), RandomFloat(), RandomFloat() ) * 2.5f;
}
}
void AllTogetherApp::Tick( float deltaTime )
{
// animate the scene
for( int i = 0; i < 256; i++ )
{
mat4 R = mat4::RotateX( orientation[i].x ) *
mat4::RotateY( orientation[i].y ) *
mat4::RotateZ( orientation[i].z ) * mat4::Scale( 0.2f );
bvhInstance[i].SetTransform( mat4::Translate( position[i] ) * R );
position[i] += direction[i], orientation[i] += direction[i];
if (position[i].x < -3 || position[i].x > 3) direction[i].x *= -1;
if (position[i].y < -3 || position[i].y > 3) direction[i].y *= -1;
if (position[i].z < -3 || position[i].z > 3) direction[i].z *= -1;
}
// update the TLAS
Timer t;
tlas.Build();
float tlasTime = t.elapsed() * 1000;
// draw the scene
float3 p0( -1, 1, 2 ), p1( 1, 1, 2 ), p2( -1, -1, 2 );
#pragma omp parallel for schedule(dynamic)
for (int tile = 0; tile < (SCRWIDTH * SCRHEIGHT / 64); tile++)
{
int x = tile % (SCRWIDTH / 8), y = tile / (SCRWIDTH / 8);
Ray ray;
ray.O = float3( 0, 0, -6.5f );
for (int v = 0; v < 8; v++) for (int u = 0; u < 8; u++)
{
float3 pixelPos = ray.O + p0 +
(p1 - p0) * ((x * 8 + u) / (float)SCRWIDTH) +
(p2 - p0) * ((y * 8 + v) / (float)SCRHEIGHT);
ray.D = normalize( pixelPos - ray.O ), ray.t = 1e30f;
tlas.Intersect( ray );
uint c = ray.t < 1e30f ? (int)(255 / (1 + max( 0.f, ray.t - 4 ))) : 0;
screen->Plot( x * 8 + u, y * 8 + v, c * 0x10101 );
}
}
// report
float elapsed = t.elapsed() * 1000;
printf( "tlas build: %.2fms, tracing time: %.2fms (%5.2fK rays/s)\n", tlasTime, elapsed, sqr( 630 ) / elapsed );
}
// EOF