Ray Intersection Acceleration CMPT 461/761 Image Synthesis Torsten Möller
Reading Chapter 2, 3, 4 of Physically Based Rendering by Pharr&Humphreys An Introduction to Ray tracing by Glassner
Topics today Review of ray-tracing acceleration approaches pbrt Object-space World-space 3
Review of ray-tracing 4
First imaging algorithm Directly derived from the synthetic camera model Follow rays of light from a point light source Determine which rays enter the lens of the camera through the imaging window Compute color of projection Why is this not a good idea? Machiraju/Zhang/Möller 5
Ray tracing When following light from a light source, many (reflected) rays do not intersect the image window and do not contribute to the scene reflected ray shadow ray rays eye pixel Image plane refracted ray Machiraju/Zhang/Möller Reverse the process Cast one ray per pixel from the eye and shoot it into the scene 6
Ray tracing: the basics A point on an object may be illuminated by Light source directly through shadow ray Light reflected off an object through reflected ray Light transmitted through a transparent object through refracted ray eye reflected ray pixel shadow ray refracted ray Machiraju/Zhang/Möller Image plane 7
Ray tracing: the algorithm for each pixel on screen determine ray from eye through pixel if ray shoots into infinity, return a background color if ray shoots into light source, return light color appropriately find closest intersection of ray with an object cast off shadow ray (towards light sources) if shadow ray hits a light source, compute light contribution according to some illumination model cast reflected and refracted ray, recursively calculate pixel color contribution return pixel color after some absorption Machiraju/Zhang/Möller Most expensive 8
Ray Tracing Shoot a ray through each pixel; Find first object intersected by ray Image plane Eye Compute ray. (More linear algebra.) Compute ray-object intersection. Spawn more rays for reflection and refraction
pbrt acceleration architecture 10
Ray Tracing Architecture Scene LRT Parse Sample Generator Film TIFF Image Primitives Camera Radiance Shape Rays intersect Eye rays Lights Material Surf Integrator Shadow, reflection, refraction rays
Optimizing Ray Tracing Main computation load is ray-object intersection 50-90% of run time when profiled Test for possible intersection before committing to computing intersections
Acceleration Strategies Fast/efficient intersection computation (single ray-single object) Not much leeway for acceleration Test fewer rays Trace fewer primary rays Fewer secondary rays Fewer intersection computations Avoid computations with far away objects Space partitioning Direction partitioning Intersection with thick rays Cone tracing Beam tracing 13
Pbrt and Intersections Primitive base class Shapes are subclasses of primitive Methods WorldBound CanIntersect Intersect IntersectP Refine First four return Intersection structures Last returns Primitives Primitives Shape Lights Material
Intersection Geometry Shape independent representation for intersections DifferentialGeometry Intersection::dg Point P Normal N Parametric (u,v) Partial derivatives Tangents: dpdu, dpdv change in normal: dndu, dndv
Object-based vs. World-based Common dichotomy in graphics objects situated in (object) space (world) space in which objects reside Bounding volumes are object-based Spatial Subdivision is world-based approach Sub-linear search
Object-space acceleration 17
Bounding Volumes Surround object with a simple volume Test ray against volume first Test object-space or world-space bound? (pros and cons) Cost model - N*cb + pi*n*co N (number of rays) is given pi fraction of rays intersecting bounding volume Minimize cb (cost of intersecting bounding volume) and co (cost of intersecting object) Reduce ray path Bounding sphere Difficult to compute good one Easy to test for intersection Reduce ray path Bounding box Easy to compute for given object More difficult to intersect
Pbrt s Bounding Boxes Virtual BBox ObjectBound() const=0; Virtual BBox WorldBound() const { return ObjectToWorld(ObjectBound()); } Bool BBox::IntersectP(const Ray &ray, float *hitt0, float *hitt1) const { }
Bounding Box Compute min/max for x,y,z 3 options Compute in world space Chance of ill fitting b-box Compute in object space and transform w/ object Object space b-box probably better fit than world space Need to intersect ray with arbitrary hexagon in world sp. Compute in object space and test in object space Inverse transform ray into object space
Bounding Sphere Find min/max points in x,y,z -> 3 pairs Use maximally separated pair to define initial sphere For each point If point is outside of current sphere, increase old sphere to just include new point
P R C R P P rad C P rad newrad = (R+rad)/2 R newc P P P newrad newrad newc C P R newc = P+(newrad/R)(C-P)
Bounding Slabs More complex to compute Better fit of object Use multiple pairs of parallel planes to bound object Can add more slabs to get tighter fit 2004 Pharr, Humphreys
Approximate Convex Hull Find highest vertex Find plane through vertex parallel to ground plane Find second vertex that makes minimum angle with first vertex and up vector Find third vertex that makes plane whose normal makes minimum angle with up vector 1989 Glassner More effort to compute, better fit
Bounding Volume Hierarchies Create tree of bounding volumes Children are contained within parent Creation preprocess From model hierarchy Automatic clustering Query intersect(node,ray,hits) { if( intersectp(node->bound,ray) if( leaf(node) ) intersect(nodeprims,ray,hits) else for each child intersect(child,ray,hits) } Return the closest of all hits!
Problem Subtrees overlap Does not contain all objects it overlaps Balance Tree Organization
Splitting strategies Choose the axis, that has the largest spread of midpoints Split based on the midpoint of the centroids of one axis Split in equal amounts Problem - overlaps 27
The Surface Area Heuristic cost of intersecting all N primitives: NX t isect (i) i=1 cost of splitting into groups N A +N B = N XN A t trav + p A t isect (a i )+ i=1 XN B i=1 t isect (b i ) t trav - traversal time of node p A / p B - prob that ray passes through each child node 28
The Surface Area Heuristic XN A t trav + p A i=1 t isect (a i )+ assume, that t isect (i) is the same for all primitives geometric probabilities XN B i=1 t isect (b i ) p(a B) = s A s B find the axis with the minimal SAH cost 29
30
BSP Trees Octree BSP tree KD tree
Example A D B B C C D A Leaves are unique regions in space Recursive search
Creating Spatial Hierarchies insert(node,prim) { if( overlap(node->bound,prim) ) } if( leaf(node) ) { if( node->nprims > MAXPRIMS && } else } node->depth < MAXDEPTH ) { subdivide(node); foreach child in node insert(child,prim) else list_insert(node->prims,prim); foreach child in node insert(child,prim) // Typically MAXDEPTH=16, MAXPRIMS=2-8
World-space acceleration 34
Spatial Enumeration Divide space into voxels Bucket sort objects in voxels they intersect Object goes into each voxel it touches Reuse results from one voxel calculation Determine voxels that a ray intersects Only deal with the objects in those voxels
Spatial Enumeration Identifying voxels hit is like a line drawing algorithm
Uniform Grids Preprocess scene Find bounding box
Uniform Grids Preprocess scene Find bounding box Determine grid resolution
Uniform Grids Preprocess scene Find bounding box Determine grid resolution Place object in cell if its bounding box overlaps the cell
Uniform Grids Preprocess scene Find bounding box Determine grid resolution Place object in cell if its bounding box overlaps the cell Check that object overlaps cell (expensive!)
Add Sorting If objects/voxels/cells are processed in front-to-back sorted order, stop processing when first intersection is detected e.g., process cells in bottom to top, left to right order and stop at first intersection
Uniform Grids Preprocess scene Traverse grid 3D line = 3D-DDA 6-connected line pbrt algorithm
Amanatides & Woo Algorithm J. Amanatides and A. Woo, "A Fast Voxel Traversal Algorithm for Ray Tracing", Proc. Eurographics '87, Amsterdam, The Netherlands, August 1987, pp 1-10.
A&W Algorithm
A&W Algorithm Results Rendering time for different levels of subdivision
Objects Across Multiple Voxels Mailboxes eliminate redundant intersection tests Objects have mailboxes Assign rays numbers check against objects last tested ray number Intersection within voxel 1989 Glassner
Hierarchical Spatial Subdivision Recursive subdivision of space 1-1 Relationship between scene points and leaf nodes Example: point location by recursive search (log time) Solves the lack-of-adaptivity problem DDA works Effective in practice
Comparison Scheme Spheres Rings Tree Uniform grid D=1 244 129 1517 D=20 38 83 781 Hierarchical grid 34 116 34 See A Proposal for Standard Graphics Environments, IEEE Computer Graphics and Applications, vol. 7, no. 11, November 1987, pp. 3-5
Questions? Scenes to render? Teapot in a stadium versus uniform distribution Constants can be important Adaptivity allows robustness Cache effects are important