1 of 51

Nima Kalantari

CSCE 441 - Computer Graphics

Ray Tracing (Acceleration)

Some slides from Ren Ng

2 of 51

Ray Tracing – Performance Challenges

  • Simple ray-scene intersection
    • Exhaustively test ray-intersection with every object
  • Problem:
    • Exhaustive = #pixels ⨉ #objects (triangles)
    • Very slow!

3 of 51

Ray Tracing – Performance Challenges

San Miguel Scene, 10.7M triangles

Jun Yan, Tracy Renderer

4 of 51

Ray Tracing – Performance Challenges

Plant Ecosystem, 20M triangles

Deussen et al; Pharr & Humphreys, PBRT

5 of 51

Outline

  • Bounding Volumes
  • Uniform Spatial Subdivision
  • Non-uniform Spatial Subdivision
  • Bounding Volume Hierarchy (BVH)

6 of 51

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 bounding volume first, then test object if it hits

7 of 51

Ray-Intersection With Box

  • Could intersect with 6 faces individually
  • Better way: box is the intersection of 3 slabs

8 of 51

Ray Intersection with Axis-Aligned Box

  • 2D example; 3D is the same! Compute intersections with slabs and take intersection of tmin/tmax intervals

txmin

txmax

tymin

tymax

Note: tymin < 0

Intersections with y planes

Intersections with x planes

tmin

tmax

Final intersection result

How do we know when the ray misses the box?

9 of 51

Optimize Ray-Plane Intersection For Axis-Aligned Planes?

3 subtractions, 6 multiplies, 1 division

1 subtraction, 1 division

General

Perpendicular�to x-axis

10 of 51

Outline

  • Bounding Volumes
  • Uniform Spatial Subdivision
  • Non-uniform Spatial Subdivision
  • Bounding Volume Hierarchy (BVH)

11 of 51

Uniform Spatial Partitions (Grids)

12 of 51

Preprocess – Build Acceleration Grid

  1. Find bounding box

13 of 51

Preprocess – Build Acceleration Grid

  1. Find bounding box
  2. Create grid

14 of 51

Preprocess – Build Acceleration Grid

  1. Find bounding box
  2. Create grid
  3. Store each object�in overlapping cells

15 of 51

Ray-Scene Intersection

Step through grid in ray traversal order

For each grid cell test intersection with all objects stored at that cell

16 of 51

Grid Resolution?

One cell

    • No speedup

17 of 51

Grid Resolution?

Too many cells

    • Inefficiency due to extraneous grid traversal

18 of 51

Careful! Objects Overlapping Multiple Cells

What goes wrong here?

    • First intersection found (red) is not the nearest!

Solution?

    • Check intersection point is inside cell

19 of 51

Uniform Grids – When They Work Well

Deussen et al; Pharr & Humphreys, PBRT

Grids work well on large collections of objects�that are distributed evenly in size and space

20 of 51

Uniform Grids – When They Fail

Jun Yan, Tracy Renderer

“Teapot in a stadium” problem

21 of 51

Outline

  • Bounding Volumes
  • Uniform Spatial Subdivision
  • Non-uniform Spatial Subdivision
  • Bounding Volume Hierarchy (BVH)

22 of 51

Spatial Hierarchies

A

A

23 of 51

Spatial Hierarchies

B

A

A

B

24 of 51

Spatial Hierarchies

4

5

D

3

2

C

A

B

C

D

1

2

3

4

5

1

B

A

25 of 51

Spatial Partitioning Variants

BSP-Tree

KD-Tree

Oct-Tree

26 of 51

KD-Tree Pre-Processing

  • Find bounding box
  • Recursively split cells, axis-aligned planes
  • Until termination criteria met
  • Store obj references with each leaf node

A

B

C

D

27 of 51

KD-Tree Pre-Processing

4

5

D

3

2

C

A

B

C

D

1

B

A

Root

Internal Nodes

Leaf Nodes

Only leaf nodes store �references to geometry

1

2

3

4

5

28 of 51

KD-Tree Pre-Processing

  • Choosing the set partition
    • Simple #1: Split objects around spatial midpoint
    • Simple #2: Split at location of median object

  • Termination criteria?
    • Simple: stop when node contains few elements (e.g. 5)

29 of 51

Spatial Split (KD-Tree)

Spatial Midpoint

30 of 51

Spatial Split (KD-Tree)

Median Object

31 of 51

Top-Down Recursive In-Order Traversal

4

5

D

3

2

C

A

B

C

D

1

B

A

32 of 51

Top-Down Recursive In-Order Traversal

4

5

D

3

2

C

A

B

C

1

B

A

Internal node: split

D

33 of 51

Top-Down Recursive In-Order Traversal

4

5

D

3

2

C

B

C

1

B

A

Leaf node: intersect�all objects

D

A

34 of 51

Top-Down Recursive In-Order Traversal

4

5

D

3

2

C

B

C

1

B

A

Internal node: split

D

A

35 of 51

Top-Down Recursive In-Order Traversal

4

5

D

3

2

C

B

C

1

B

A

Leaf node: intersect�all objects

D

A

36 of 51

Top-Down Recursive In-Order Traversal

4

5

D

3

2

C

B

C

1

B

A

Internal node: split

D

A

37 of 51

Top-Down Recursive In-Order Traversal

4

5

D

3

2

C

B

C

1

B

A

A

Leaf node: intersect�all objects

D

38 of 51

Top-Down Recursive In-Order Traversal

4

5

D

3

2

C

B

C

D

1

B

A

Intersection found

A

39 of 51

Outline

  • Bounding Volumes
  • Uniform Spatial Subdivision
  • Non-uniform Spatial Subdivision
  • Bounding Volume Hierarchy (BVH)

40 of 51

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

41 of 51

Bounding Volume Hierarchy (BVH)

Root

42 of 51

Bounding Volume Hierarchy (BVH)

43 of 51

Bounding Volume Hierarchy (BVH)

44 of 51

Bounding Volume Hierarchy (BVH)

C

D

B

A

A

B

C

D

45 of 51

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

46 of 51

BVH Pre-Processing

  • Find bounding box
  • Recursively split set of objects in two subsets
  • Stop when there are just a few objects in each set
  • Store obj reference(s) in each leaf node

47 of 51

BVH Pre-Processing

  • Find bounding box

48 of 51

BVH Pre-Processing

  • Sort triangles in terms of x
  • Divide them into two groups
  • Find bounding box for each

49 of 51

BVH Pre-Processing

  • Sort triangles in terms of y
  • Divide them into two groups
  • Find bounding box for each

50 of 51

BVH Pre-Processing

  • Sort triangles in terms of y
  • Divide them into two groups
  • Find bounding box for each

51 of 51

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 closer of hit1, hit2;

}

node

child1

child2