CS180: Introduction to Computer GraphicsSpring 2019, Lingqi Yan, UC Santa Barbara
Ray Tracing 2 (Acceleration) Lecture 13:
http://www.cs.ucsb.edu/~lingqi/teaching/cs180.html
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Last Lecture• Shadow mapping for rasterizers
• Introduction to ray tracing
• Ray generation
• Ray object intersection- Ray sphere intersection
- Ray implicit surface intersection
- Ray triangle intersection = Ray plane intersection + inside test
!2
Accelerating Ray-Surface Intersection
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Ray Tracing – Performance Challenges
Simple ray-scene intersection
• Exhaustively test ray-intersection with every triangle
• Find the closest hit (with minimum t)
Problem:
• Naive algorithm = #pixels ⨉ # traingles (⨉ #bounces)
• Very slow!
For generality, we use the term objects instead of triangles later (but doesn’t necessarily mean entire objects)
!4
Ray Tracing – Performance Challenges
San Miguel Scene, 10.7M triangles
Jun Yan, Tracy Renderer
Ray Tracing – Performance Challenges
Plant Ecosystem, 20M triangles
Deussen et al; Pharr & Humphreys, PBRT
Bounding Volumes
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Bounding Volumes
Quick way to avoid intersections: bound complex object with a simple volume
• Object is fully contained in the volume
• If it doesn’t hit the volume, it doesn’t hit the object
• So test bvol first, then test object if it hits
!8
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Ray-Intersection With Box
Could intersect with 6 faces individually Better way: box is the intersection of 3 pairs of slabs
!9
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Ray Intersection with Axis-Aligned Box2D example; 3D is the same! Compute intersections with slabs and take intersection of tmin/tmax intervals
!10
o,do,d
x0 � x1 � y0 � y1x0 � x1 � y0 � y1
x0 � x1 � y0 � y1
x0 � x1 � y0 � y1
tmin
tmax
o,do,d
x0 � x1 � y0 � y1x0 � x1 � y0 � y1
x0 � x1 � y0 � y1
x0 � x1 � y0 � y1
tmin
tmax
Note: tmin < 0
Intersections with y planes Intersections with x planes
o,do,d
x0 � x1 � y0 � y1x0 � x1 � y0 � y1
x0 � x1 � y0 � y1
x0 � x1 � y0 � y1
tmin
tmax
Final intersection resultHow do we know when the ray misses the box?
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
• Recall: a box (3D) = three pairs of infinitely large slabs
• Key ideas
- The ray enters the box only when it enters all pairs of slabs
- The ray exits the box as long as it exits any pair of slabs
• For each pair, calculate the tmin and tmax (negative is fine)
• For the 3D box, tenter = max{tmin}, texit = min{tmax}
• If tenter < texit, we know the ray stays a while in the box(so they must intersect!) (not done yet, see the next slide)
!11
Ray Intersection with Axis-Aligned Box
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
• However, ray is not a line
- Time t should always be positive for physical correctness!
• What if texit < 0?
- The box is “behind” the ray — no intersection!
• What if tenter < 0?
- The ray’s origin is inside the box — still intersection!
!12
Ray Intersection with Axis-Aligned Box
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Optimize Ray-Plane Intersection For Axis-Aligned Planes?
!13
r(t) = o+ td, 0 t < 1p : (p� p0) ·N = 0
ax+ by + cz + d = 0
Set p = r(t) and solve for t
(p� p0) ·N = (o+ td� p0) ·N = 0
t =p0 � o) ·N
d ·N
r(t) = o+ td, 0 t < 1p : (p� p0) ·N = 0
ax+ by + cz + d = 0
Set p = r(t) and solve for t
(p� p0) ·N = (o+ td� p0) ·N = 0
t =(p0 � o) ·N
d ·N
3 subtractions, 6 multiplies, 1 division
t =(p0 � o) ·N
d ·N =) t =p0
x � ox
dx
r(t) = o+ td, 0 t < 1p : (p� p0) ·N = 0
ax+ by + cz + d = 0
Set p = r(t) and solve for t
(p� p0) ·N = (o+ td� p0) ·N = 0
t =p0 � o) ·N
d ·N1 subtraction, 1 division
General
Perpendicularto x-axis
Uniform Spatial Partitions (Grids)
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Preprocess – Build Acceleration Grid
!15
1. Find bounding box
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Preprocess – Build Acceleration Grid
!16
1. Find bounding box 2. Create grid
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Preprocess – Build Acceleration Grid
!17
1. Find bounding box 2. Create grid 3. Store each object
in overlapping cells
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Ray-Scene Intersection
!18
Step through grid in ray traversal order
For each grid cell Test intersection with all objects stored at that cell
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Grid Resolution?
One cell
• No speedup
!19
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Grid Resolution?
Too many cells
• Inefficiency due to extraneous grid traversal
!20
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Grid Resolution?
Heuristic:
• #cells = C * #objs
• C ≈ 27 in 3D
!21
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Uniform Grids – When They Work Well
!22
Deussen et al; Pharr & Humphreys, PBRT
Grids work well on large collections of objectsthat are distributed evenly in size and space
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Jun Yan, Tracy Renderer
Uniform Grids – When They Fail
!23
“Teapot in a stadium” problem
Spatial Partitions
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Spatial Hierarchies
!25
A
A
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
B
A
Spatial Hierarchies
!26
A
B
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
4 5
D 3
2 C
Spatial Hierarchies
!27
A
BC
D
1
2
34
5
1 B
A
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Spatial Partitioning Examples
!28
BSP-TreeKD-Tree
Note: you could have these in both 2D and 3D. In lecture we will illustrate principles in 2D.
Oct-Tree
Object Partitions &
Bounding Volume Hierarchy (BVH)
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Spatial vs Object Partitions
Spatial partition (e.g.KD-tree) • Partition space into
non-overlapping regions • Objects can be contained in
multiple regions
Object partition (e.g. BVH) • Partition set of objects into
disjoint subsets • Bounding boxes for each
set may overlap in space
!30
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Bounding Volume Hierarchy (BVH)
!31
Root
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Bounding Volume Hierarchy (BVH)
!32
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Bounding Volume Hierarchy (BVH)
!33
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
C D
B
Bounding Volume Hierarchy (BVH)
!34
A
AB
C
D
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
Bounding Volume Hierarchy (BVH)
Internal nodes store
• Bounding box
• Children: reference to child nodes Leaf nodes store
• Bounding box
• List of objects Nodes represent subset of primitives in scene
• All objects in subtree
!35
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
BVH Pre-Processing
• Find bounding box
• Recursively split set of objects in two subsets
• Recompute the bounding box of the subsets
• Stop when there are just a few objects in each set
• Store obj reference(s) in each leaf node
!36
Ren NgCS184/284A
BVH Pre-Processing
Choosing the set partition
• Choose a dimension to split
• Heuristic #1: Always choose the longest axis in node
• Heuristic #2: Split node at location of median object
Termination criteria?
• Heuristic: stop when node contains few elements (e.g. 5)
Slide courtesy of Prof. Ren Ng, UC Berkeley
CS180, Spring 2019 Lingqi Yan, UC Santa Barbara
BVH Recursive Traversal
Intersect(Ray ray, BVH node) {
if (ray misses node.bbox) return;
if (node is a leaf node)
test intersection with all objs;
return closest intersection;
hit1 = Intersect(ray, node.child1);
hit2 = Intersect(ray, node.child2);
return the closer of hit1, hit2;
}
!38
node
child1 child2
Thank you!