Ray Tracing Acceleration Structures Solomon Boulos 4/16/2004
How does ray tracing work? Rays shoot from eyes into scene Find first thing ray hits Shade Becomes a search problem
Why acceleration structures? Trying to find first intersection Linear search is too slow O(n) 95 % of time is spent here Interactive ray tracing Large data sets
Talk Overview Types of Structures Discussion of Three Major Structures –Definition –Construction –Usage –Performance Analysis Which to use Current Problems Current work Future Work
Two Basic Types Divide in space Divide in geometry SpatialObject Empty Space
What are our options? Three Main Types –Uniform Grid ( UG ) –Binary Space Partition ( BSP ) –Bounding Volume Hierarchy ( BVH ) Others Not Discussed –Octree ( Glassner, 1984 ) –5D Tree ( Arvo, 1987 ) –List –Variants –Dynamic
What’s a UG? 3D Grid that divides space uniformly Each cell is a Bounding Volume (BV) A BV contains all the surfaces inside it Traversal is iterative through grid
How do we build one? 1.Find the surrounding bounding box. 2.Subdivide box into grid of boxes. 3.Add primitives to grid cells.
How do we use it? 1.Test Overall BV for intersection. 2.Find first grid intersection. 3.Iteratively update position. 4.Stop when we hit a primitive. 5.Return false if we exit the grid.
Simple Example
for(;;) cell = grid[i][j][k]; if (tx_next <= ty_next && tx_next < tz_next) { r.tmin = t_current; r.tmax = tx_next; if (cell != NULL && cell->hit(r, time, rec)) return true; t_current = tx_next; tx_next += dtx; i += i_step; if (i == i_stop) return false; } // similar cases UG Traversal Code
UG Analysis Build is O(n) Traversal is O(n^( 1/3 )) Great for Dense objects Naïve memory layout is bad Data tiling improves things
What’s a BSP? Space Subdivision Binary Tree Ordered traversal KD-TreeBSP Tree
How do we build one? 1.Choose splitting plane. 2.Pick pivot that divides plane. 3.Place primitives onto each side. 4.Stop if termination conditions met.
How do we use it? Test intersections based on 4 cases Case 1 Case 2 Case 3 Case 4 Splitting Plane
Ray Direction Code Ray::Ray(const Vector3& origin,const Vector3& direction) { tmin = ; tmax = DBL_MAX; data[0] = origin; data[1] = direction; posneg[X] = (data[1].x() >= 0 ? 0 : 1); posneg[Y] = (data[1].y() >= 0 ? 0 : 1); posneg[Z] = (data[1].z() >= 0 ? 0 : 1); }
BSP Traversal Code bool BSP::hit(Ray& r, HitRecord& rec) const { float point = r.origin()[axis] + r.direction()[axis]*r.tmin; float t = (pivot - point)/(r.direction()[axis]); if (point < pivot) // cases 1 and 2 { if ( r.posneg[axis] || t > r.tmax) // ray is negative i.e. posneg = 1 return (child[0] != NULL ) && (child[0]->hit(r, time, rec)); if ( child[0] != NULL && child[0]->hit(r, time, rec) ) return true; return (child[1] != NULL && child[1]->hit(r, time, rec)); } else // symmetric for cases 3 and 4... }
BSP Analysis Build time O(nlogn) or higher usually Objects overlap regions. Traversal time O(logn) expected Build is most important Memory layout can help.
What’s a BVH? Divides objects not space. Hierarchy ( Binary Tree is easy ) Each Node is a Bounding Volume (BV)
How do we build one? Find an all-encompassing BV Split this BV in two by any method Put shapes into each child Recurse
How do we test for intersections? 1.Test root for intersection. 2.If ray hits root, check children; otherwise false. 3.Return closest hit. t =
bool BVH::hit(Ray& r, HitRecord& rec) const { // test BV for intersection if (!(bbox.rayIntersect(r, r.tmin, r.tmax))) return false; // else call hit on both branches bool isahit1 = false; bool isahit2 = false; isahit1 = child[r.posneg[axis]]->hit(r, rec); isahit2 = child[r.posneg[axis]^1]->hit(r,rec); // did we hit either child? return (isahit1 || isahit2); } BVH Traversal Code
BVH Analysis Build time varies like BSP. O(nlogn) and higher in practice. Bounding boxes overlap instead. Traversal is similar O(logn) Code is clean and simple
What affects performance? Build Strategy Scene Geometry View dependence Memory Layout
Which is best? Lots of opinions Not much evidence Typical Scenes Type of Applications
The case for the BSP Tree Ordered Traversal Lots of papers about them “algorithms for building BVHs that are well-suited for fast traversal are less well investigated than similar algorithms for BSP trees.” -- Ingo Wald, Ph.D Thesis ( 2004 )
The case for the UG Iterative traversal Handled huge data Steve Parker’s UG Excellent for dense objects
The case for the BVH Simple No copied geometry Comparable speed Ignores empty space
What’s the problem? Test Data ( SPD Scenes ) Reproduction of Results -- Gordon Mueller
Current Work Build strategy investigation Trying all major types Lots of papers to read Implementation tuning Building test scenes Documenting results
Future Work Dynamic structures Storing structures with models Hybrid structures Rigorous analysis
Any questions?