Rendering: Spatial Acceleration Structures Bernhard Kerbl Research - - PowerPoint PPT Presentation

rendering spatial acceleration structures
SMART_READER_LITE
LIVE PREVIEW

Rendering: Spatial Acceleration Structures Bernhard Kerbl Research - - PowerPoint PPT Presentation

Rendering: Spatial Acceleration Structures Bernhard Kerbl Research Division of Computer Graphics Institute of Visual Computing & Human-Centered Technology TU Wien, Austria With slides based on material by Jaakko Lehtinen, used with


slide-1
SLIDE 1

Rendering: Spatial Acceleration Structures

Bernhard Kerbl

Research Division of Computer Graphics Institute of Visual Computing & Human-Centered Technology TU Wien, Austria

With slides based on material by Jaakko Lehtinen, used with permission

slide-2
SLIDE 2

How to produce an image?

A good image needs realistic intensity and visibility

Intensity creates stimulus of optic nerve (black, white, color) Visibility makes sure that objects adhere to depth

How would you process the scene on the right to make sure the rendered output image is correct?

(Naïve) Ray-Casting Render Loop

Shoot a ray through each pixel into the scene Iterate over all objects and test for intersection Record the closest intersection (visibility) Compute color and write to pixel (intensity)

Rendering – Spatial Acceleration Structures 2

Source: Wojciech Mula, Wikipedia “Painter's algorithm”

slide-3
SLIDE 3

Render Loop

Rendering – Spatial Acceleration Structures 3

void render(Camera cam) { for(Pixel& pix : pixels) { pix.Color = background; Intersection closest; closest.Distance = INFINITY; Ray ray = rayThroughPixel(cam, pix); for (Triangle& tri : triangles) { Intersection sect = findClosestIntersection(ray, tri); if(sect.Distance < closest.Distance) { closest = sect; } } if(closest.Distance != INFINITY) { pix.Color = computeColor(closest); } } }

slide-4
SLIDE 4

void render(Camera cam) { for(Pixel& pix : pixels) { pix.Color = background; Intersection closest; closest.Distance = INFINITY; Ray ray = rayThroughPixel(cam, pix); for (Triangle& tri : triangles) { Intersection sect = findClosestIntersection(ray, tri); if(sect.Distance < closest.Distance) { closest = sect; } } if(closest.Distance != INFINITY) { pix.Color = computeColor(closest); } } }

Spatial Aliasing

Rendering – Spatial Acceleration Structures 4

Those are your dad’s pixels!

Source: renderstuff.com/

slide-5
SLIDE 5

Supersampling

Instead of a single ray through each pixel, use multiple „samples“

Rendering – Spatial Acceleration Structures 5

Source: Parcly Taxel, Wikipedia “Supersampling”

slide-6
SLIDE 6

Supersampling

Rendering – Spatial Acceleration Structures 6

void render(Camera cam) { for(Pixel& pix : pixels) { pix.Color = background; Intersection closest; closest.Distance = INFINITY; Ray ray = rayThroughPixel(cam, pix); for (Triangle& tri : triangles) { Intersection sect = findClosestIntersection(ray, tri); if(sect.Distance < closest.Distance) { closest = sect; } } if(closest.Distance != INFINITY) { pix.Color = computeColor(closest); } } }

Antialiased

Source: renderstuff.com/

slide-7
SLIDE 7

Updated Render Loop

7

pix.Color = background; Intersection closest; closest.Distance = INFINITY; for(int s = 0; s < NUM_SAMPLES; s++) { SampleInfo sInfo = drawSample(); Ray ray = rayThroughSample(cam, sInfo.Location); for (Triangle& tri : triangles) { Intersection sect = findClosestIntersection(ray, tri); if(sect.Distance < closest.Distance) { closest = sect; } } if(closest.Distance != INFINITY) { RGBColor sample = computeColor(closest); pix.Color += filter(sInfo.Filter, RGBWColor(sample, 1)); } } pix.Color /= pixColor.w;

Rendering – Spatial Acceleration Structures

slide-8
SLIDE 8

pix.Color = background; Intersection closest; closest.Distance = INFINITY; for(int s = 0; s < NUM_SAMPLES; s++) { SampleInfo sInfo = drawSample(); Ray ray = rayThroughSample(cam, sInfo.Location); for (Triangle& tri : triangles) { Intersection sect = findClosestIntersection(ray, tri); if(sect.Distance < closest.Distance) { closest = sect; } } if(closest.Distance != INFINITY) { RGBColor sample = computeColor(closest); pix.Color += filter(sInfo.Filter, RGBWColor(sample, 1)); } } pix.Color /= pixColor.w;

Updated Render Loop

8

Color and Light Sample Integration Rendering Equation Filtering Sampling

Rendering – Spatial Acceleration Structures

slide-9
SLIDE 9

Let‘s look at the basic runtime (single sample per pixel)

Render Loop Run Time

9 Rendering – Spatial Acceleration Structures

void render(Camera cam) { for(Pixel& pix : pixels) { … for (Triangle& tri : triangles) { … } … } }

slide-10
SLIDE 10

Let‘s look at the basic runtime (single sample per pixel) This is 𝒫(𝑂 ⋅ 𝑁), but even worse, it’s Ω 𝑂 ⋅ 𝑁 !

Render Loop Run Time

10 Rendering – Spatial Acceleration Structures

void render(Camera cam) { for(Pixel& pix : pixels)  𝑂 { … for (Triangle& tri : triangles)  𝑁 { … } … } }

slide-11
SLIDE 11

Is That Actually a Problem?

Run time complexity quickly becomes a limiting factor High-quality scenes can have several million triangles per object Current screens and displays are moving towards 4k resolution

Rendering – Spatial Acceleration Structures 11

slide-12
SLIDE 12

Rendering – Spatial Acceleration Structures 12

Amazon Lumberyard “Bistro” 3,780,244 triangles 1200x675 pixels 3 trillion ray/triangle intersection tests? At 10M per second, one shot will take ~4 days. Good luck with your movie!

Picture provide through Creative Commons CC-BY.4.0

slide-13
SLIDE 13

What can we do about it?

For rendering, we will want to learn to run before we can walk Find ways to speed up the basic loop for visibility resolution Enter “spatial acceleration structures” Essentially, pre-process the scene geometry into a structure that reduces expected traversal time to something more reasonable

Rendering – Spatial Acceleration Structures 13

slide-14
SLIDE 14

Spatial Acceleration Structures

Rendering – Spatial Acceleration Structures 14

Structure Additional Memory Building Time Traversal Time none none none abysmal

slide-15
SLIDE 15

Consider a group of triangles Which ones should we test?

Speeding Up Intersection Tests

Rendering – Spatial Acceleration Structures 15

slide-16
SLIDE 16

Overlay scene with regular grid Sort triangles into cells Traverse cells and test against their contents

Regular Grids

Rendering – Spatial Acceleration Structures 16

slide-17
SLIDE 17

Overlay scene with regular grid Sort triangles into cells Traverse cells and test against their contents

Regular Grids

Rendering – Spatial Acceleration Structures 17

slide-18
SLIDE 18

Overlay scene with regular grid Sort triangles into cells Traverse cells and test against their contents

Regular Grids

Rendering – Spatial Acceleration Structures 18

slide-19
SLIDE 19

Geometry is usually not uniform Comes in clusters (buildings, characters, vegetation…)

Regular Grids

19 Rendering – Spatial Acceleration Structures

slide-20
SLIDE 20

Geometry is usually not uniform Comes in clusters (buildings, characters, vegetation…) Almost all triangles in one cell! Hitting this cell will be costly!

Regular Grids

20 Rendering – Spatial Acceleration Structures

slide-21
SLIDE 21

Regular Grids

21 Rendering – Spatial Acceleration Structures

Geometry is usually not uniform Comes in clusters (buildings, characters, vegetation…) Almost all triangles in one cell! Hitting this cell will be costly! Using a finer grid works

slide-22
SLIDE 22

Regular Grids

22 Rendering – Spatial Acceleration Structures

Geometry is usually not uniform Comes in clusters (buildings, characters, vegetation…) Almost all triangles in one cell! Hitting this cell will be costly! Using a finer grid works, but most of its cells are unused!

slide-23
SLIDE 23

Spatial Acceleration Structures

Rendering – Spatial Acceleration Structures 23

Structure Memory Consumption Building Time (Expected) Traversal Time none none none abysmal Regular Grid low – high (resolution) low uniform scene: ok

  • therwise: bad
slide-24
SLIDE 24

Quadtrees and Octrees

24 Rendering – Spatial Acceleration Structures

Start with scene bounds, do finer subdivisions only if needed Define parameters S𝑛𝑏𝑦, 𝑂𝑚𝑓𝑏𝑔 Recursively split bounds into quadrants (2D) or octants (3D) Stop after S𝑛𝑏𝑦 subdivisions or if no cell has > 𝑂𝑚𝑓𝑏𝑔 triangles

slide-25
SLIDE 25

Quad and Octrees: 𝑂𝑚𝑓𝑏𝑔 = 4

Start with scene bounds, do finer subdivisions only if needed Define parameters S𝑛𝑏𝑦, 𝑂𝑚𝑓𝑏𝑔 Recursively split bounds into quadrants (2D) or octants (3D) Stop after S𝑛𝑏𝑦 subdivisions or if no cell has > 𝑂𝑚𝑓𝑏𝑔 triangles

25 Rendering – Spatial Acceleration Structures

slide-26
SLIDE 26

Quad and Octrees: 𝑂𝑚𝑓𝑏𝑔 = 4

Start with scene bounds, do finer subdivisions only if needed Define parameters S𝑛𝑏𝑦, 𝑂𝑚𝑓𝑏𝑔 Recursively split bounds into quadrants (2D) or octants (3D) Stop after S𝑛𝑏𝑦 subdivisions or if no cell has > 𝑂𝑚𝑓𝑏𝑔 triangles

26 Rendering – Spatial Acceleration Structures

1 2 3 4

slide-27
SLIDE 27

Quad and Octrees: 𝑂𝑚𝑓𝑏𝑔 = 4

27 Rendering – Spatial Acceleration Structures

. 3 4 1 2

1 2 3 4

slide-28
SLIDE 28

Quad and Octrees: 𝑂𝑚𝑓𝑏𝑔 = 4

28 Rendering – Spatial Acceleration Structures

1 2 3 4

. 3 4 1 2 3 4 1 2

slide-29
SLIDE 29

Quad and Octrees: 𝑂𝑚𝑓𝑏𝑔 = 4

29 Rendering – Spatial Acceleration Structures

1 2 3 4

. 3 4 1 2 3 4 1 2

slide-30
SLIDE 30

Quad and Octrees: 𝑂𝑚𝑓𝑏𝑔 = 4

30 Rendering – Spatial Acceleration Structures

. 3 4 1 2 3 4 1 2 3 4 1 2

1 2 3 4

slide-31
SLIDE 31

Quad and Octrees: 𝑂𝑚𝑓𝑏𝑔 = 4

31 Rendering – Spatial Acceleration Structures

. 3 4 1 2 3 4 1 2 3 4 1 2

1 2 3 4

slide-32
SLIDE 32

Quad and Octrees

Triangles may not be contained within a quadrant or octant Triangles must be referenced in all overlapping cells or split at the border into smaller ones

32 Rendering – Spatial Acceleration Structures

slide-33
SLIDE 33

Quad and Octrees

Triangles may not be contained within a quadrant or octant Triangles must be referenced in all overlapping cells or split at the border into smaller ones Can drastically increase memory consumption!

33 Rendering – Spatial Acceleration Structures

slide-34
SLIDE 34

Spatial Acceleration Structures

Structure Memory Consumption Building Time (Expected) Traversal Time none none none abysmal Regular Grid low – high (resolution) low uniform scene: ok

  • therwise: bad

Quadtree/Octree low – high (overlap/uniformity) low good

Rendering – Spatial Acceleration Structures 34

slide-35
SLIDE 35

BSP Trees & K-d Trees

Binary Space Partition Tree

Recursive split via hyperplanes Left/right child nodes treat

  • bjects in each half-space

Splits can be arbitrary!

35 Rendering – Spatial Acceleration Structures

slide-36
SLIDE 36

BSP Trees & K-d Trees, 𝑂𝑚𝑓𝑏𝑔 = 4

Binary Space Partition Tree

Recursive split via hyperplanes Left/right child nodes treat

  • bjects in each half-space

Splits can be arbitrary!

36 Rendering – Spatial Acceleration Structures

.

A B

A B

slide-37
SLIDE 37

BSP Trees & K-d Trees, 𝑂𝑚𝑓𝑏𝑔 = 4

Binary Space Partition Tree

Recursive split via hyperplanes Left/right child nodes treat

  • bjects in each half-space

Splits can be arbitrary!

37 Rendering – Spatial Acceleration Structures

.

A B

C D

C D

E F

E F

slide-38
SLIDE 38

BSP Trees & K-d Trees, 𝑂𝑚𝑓𝑏𝑔 = 4

Binary Space Partition Tree

Recursive split via hyperplanes Left/right child nodes treat

  • bjects in each half-space

Splits can be arbitrary!

38 Rendering – Spatial Acceleration Structures

.

A B C D E F

G H

G H

C F E

slide-39
SLIDE 39

BSP Trees & K-d Trees

Binary Space Partition Tree

Recursive split via hyperplanes Left/right child nodes treat

  • bjects in each half-space

Splits can be arbitrary!

K-dimensional (K-d) Tree

Every hyperplane must be perpendicular to a base axis Limits search space for splits

39 Rendering – Spatial Acceleration Structures

slide-40
SLIDE 40

BSP Trees & K-d Trees, 𝑂𝑚𝑓𝑏𝑔 = 4

Binary Space Partition Tree

Recursive split via hyperplanes Left/right child nodes treat

  • bjects in each half-space

Splits can be arbitrary!

K-dimensional (K-d) Tree

Every hyperplane must be perpendicular to a base axis Limits search space for splits

40 Rendering – Spatial Acceleration Structures

slide-41
SLIDE 41

BSP Trees & K-d Trees, 𝑂𝑚𝑓𝑏𝑔 = 4

Binary Space Partition Tree

Recursive split via hyperplanes Left/right child nodes treat

  • bjects in each half-space

Splits can be arbitrary!

K-dimensional (K-d) Tree

Every hyperplane must be perpendicular to a base axis Limits search space for splits

41 Rendering – Spatial Acceleration Structures

slide-42
SLIDE 42

BSP Trees & K-d Trees, 𝑂𝑚𝑓𝑏𝑔 = 4

Binary Space Partition Tree

Recursive split via hyperplanes Left/right child nodes treat

  • bjects in each half-space

Splits can be arbitrary!

K-dimensional (K-d) Tree

Every hyperplane must be perpendicular to a base axis Limits search space for splits

42 Rendering – Spatial Acceleration Structures

slide-43
SLIDE 43

Spatial Acceleration Structures

Structure Memory Consumption Building Time (Expected) Traversal Time none none none abysmal Regular Grid low – high (resolution) low uniform scene: ok

  • therwise: bad

Quadtree/Octree low – high (overlap/uniformity) low good K-d Tree low – high (overlap) low – high good – excellent

Rendering – Spatial Acceleration Structures 43

slide-44
SLIDE 44

Find enclosing (“conservative”) volumes that are easier to test Ideally: tight, but easy to check for intersection with ray Common choices:

Bounding Spheres Bounding Boxes

Axis-aligned (AABB) Oriented (OBB)

Saves on computational effort if reject

Bounding Volumes

Rendering – Spatial Acceleration Structures 44

slide-45
SLIDE 45

Axis-Aligned Bounding Boxes (AABBs)

AABBs are defined by their two extrema (min/max) Linear run time to compute

Iterate over all vertices Keep min/max values for each dimension Done!

Rendering – Spatial Acceleration Structures 45

slide-46
SLIDE 46

Find the AABB that encloses multiple, smaller AABBs Operates only on extrema of each smaller AABB Merging process is commutative

Merging AABBs

Rendering – Spatial Acceleration Structures 46

slide-47
SLIDE 47

Bounding Spheres

Bounding spheres need a center Ԧ 𝑑 and a radius 𝑠 For Ԧ 𝑑, can pick the mean vertex position or center of AABB Once center is chosen, find vertex position Ԧ 𝑤𝑛𝑏𝑦 farthest from it 𝑠 = |Ԧ 𝑑 − Ԧ 𝑤𝑛𝑏𝑦|

Rendering – Spatial Acceleration Structures 47

slide-48
SLIDE 48

How to Use Bounding Volumes

Can also be applied to entire objects Reject entire object if volume is not hit Good start, but what if…

…scene is not partitioned into objects? …objects are extremely large (terrain)? …objects are extremely detailed (characters)? …there are millions of objects with ∼ 2 triangles each (leaves)?

Rendering – Spatial Acceleration Structures 48

slide-49
SLIDE 49

Bounding Volume Hierarchy (BVH)

Each node of the hierarchy has its own bounding volume Every node can be

An inner node: references child nodes A leaf node: references triangles

Each node’s bounding volume is a subset of its parent’s bounding volume (i.e., child nodes are spatially contained by their parents)

49 Rendering – Spatial Acceleration Structures

slide-50
SLIDE 50

Bounding Volume Hierarchy (BVH)

The final hierarchy is (again) a tree structure with 𝑂 leaf nodes Leaf nodes can be

Individual triangles Clusters (e.g., ≤ 10Δ)

Total number of nodes for a binary tree: 2𝑂 − 1

If balanced, it takes ∼ log 𝑂 steps to reach a leaf from the root If trees have more than 2 branches, they require fewer nodes

51

Source: Schreiberx, Wikipedia “Bounding Volume Hierarchy”

Rendering – Spatial Acceleration Structures

slide-51
SLIDE 51

What makes BVHs special?

Important feature: bounding volumes can overlap! No duplicate references or split triangles necessary! Implicitly limits the amount

  • f memory required

52 Rendering – Spatial Acceleration Structures

slide-52
SLIDE 52

BVH Building

Generating BVH and tree for input triangle geometry CPU: usually top-down GPU: usually bottom-up From here on out, we will consider box BVHs only

Rendering – Spatial Acceleration Structures 53

slide-53
SLIDE 53

BVH Building, Top-Down

Rendering – Spatial Acceleration Structures 54

Define 𝑂𝑚𝑓𝑏𝑔 for leaves For each node, do the following:

Compute bounding box that fully encloses triangles & store Holds ≤ 𝑂𝑚𝑓𝑏𝑔 triangles? Stop. Else, split into child groups Make one new node per group Set them as children of current Repeat with child nodes

slide-54
SLIDE 54

BVH Building, Top-Down, 𝑂𝑚𝑓𝑏𝑔 = 4

Rendering – Spatial Acceleration Structures 55

Define 𝑂𝑚𝑓𝑏𝑔 for leaves For each node, do the following:

Compute bounding box that fully encloses triangles & store Holds ≤ 𝑂𝑚𝑓𝑏𝑔 triangles? Stop. Else, split into child groups Make one new node per group Set them as children of current Repeat with child nodes

slide-55
SLIDE 55

BVH Building, Top-Down, 𝑂𝑚𝑓𝑏𝑔 = 4

Rendering – Spatial Acceleration Structures 56

A B Define 𝑂𝑚𝑓𝑏𝑔 for leaves For each node, do the following:

Compute bounding box that fully encloses triangles & store Holds ≤ 𝑂𝑚𝑓𝑏𝑔 triangles? Stop. Else, split into child groups Make one new node per group Set them as children of current Repeat with child nodes

slide-56
SLIDE 56

BVH Building, Top-Down, 𝑂𝑚𝑓𝑏𝑔 = 4

Rendering – Spatial Acceleration Structures 57

. B A

A B

slide-57
SLIDE 57

BVH Building, Top-Down, 𝑂𝑚𝑓𝑏𝑔 = 4

Rendering – Spatial Acceleration Structures 58

. B A

C D E F

D C F E

slide-58
SLIDE 58

BVH Building, Top-Down, 𝑂𝑚𝑓𝑏𝑔 = 4

Rendering – Spatial Acceleration Structures 59

. B A D C F E H G

G H

slide-59
SLIDE 59

How to split a node?

Which axes to consider for building bounding boxes/splitting?

Basis vectors 1,0,0 , 0,1,0 , (0,0,1) only Oriented basis vectors only Arbitrary

Where to split?

Spatial median Object median Something more elaborate...

Rendering – Spatial Acceleration Structures 60

slide-60
SLIDE 60

How to split a node?

Which axes to consider for building bounding boxes/splitting?

Basis vectors 1,0,0 , 0,1,0 , (0,0,1) only Oriented basis vectors only Arbitrary

Where to split?

Spatial median Object median Something more elaborate...

Rendering – Spatial Acceleration Structures 61

Algorithms exist (e.g. “separating axis theorem”), but usually very slow!

slide-61
SLIDE 61

Splitting at spatial median

Pick the longest axis (X/Y/Z)

  • f current node bounds

Find the midpoint on that axis Assign triangles to A/B based

  • n which side of the midpoint

each triangle’s centroid lies on Continue recursion with A/B

Rendering – Spatial Acceleration Structures 62

slide-62
SLIDE 62

Splitting at spatial median

Careful: can result in infinite recursion! All triangles are assigned again to

  • ne node, none in the other

Can guard against it in several ways

Limit max. number of split attempts Try other axes if one node is empty Compute box over triangle centroids and split that on longest axis instead

Rendering – Spatial Acceleration Structures 63

slide-63
SLIDE 63

Pick an axis. Can try them all, don’t pick the same every time Sort triangles according to their centroid’s position on that axis Assign first half of the sorted triangles to A, the second to B Continue recursion with A/B

Splitting at object median

Rendering – Spatial Acceleration Structures 64

slide-64
SLIDE 64

BVH Traversal

  • 0. Set 𝑢𝑛𝑗𝑜 = ∞. Start at root node, return if it doesn’t intersect ray.
  • 1. Process node if its closest intersection with ray is closer than 𝑢𝑛𝑗𝑜
  • 2. If it’s an inner node, run from 1. for child nodes that intersect ray

Process the closest node first Keep others on stack to process further ones later (recursion works)

  • 3. If it’s a leaf, check triangles and update 𝑢𝑛𝑗𝑜 in case of closer hit

Rendering – Spatial Acceleration Structures 65

slide-65
SLIDE 65

BVH Traversal Example

  • 1. Process node if its closest intersection with ray is closer than 𝑢𝑛𝑗𝑜

Rendering – Spatial Acceleration Structures 66

slide-66
SLIDE 66

BVH Traversal Example

  • 1. Process node if its closest intersection with ray is closer than 𝑢𝑛𝑗𝑜

Rendering – Spatial Acceleration Structures 67

slide-67
SLIDE 67

BVH Traversal Example

  • 2. If it’s an inner node, run from 1. for child nodes that intersect ray

Process the closest node first Keep others on stack to process further ones later

Rendering – Spatial Acceleration Structures 68

slide-68
SLIDE 68

BVH Traversal Example

  • 1. Process node if its closest intersection with ray is closer than 𝑢𝑛𝑗𝑜

Rendering – Spatial Acceleration Structures 69

slide-69
SLIDE 69

BVH Traversal Example

  • 1. Process node if its closest intersection with ray is closer than 𝑢𝑛𝑗𝑜

Rendering – Spatial Acceleration Structures 70

slide-70
SLIDE 70

BVH Traversal Example

  • 3. If it’s a leaf, check triangles and update 𝑢𝑛𝑗𝑜 in case of closer hit

Rendering – Spatial Acceleration Structures 71

slide-71
SLIDE 71

BVH Traversal Example

  • 3. If it’s a leaf, check triangles and update 𝑢𝑛𝑗𝑜 in case of closer hit

Rendering – Spatial Acceleration Structures 72

slide-72
SLIDE 72

BVH Traversal Example

  • 2. If it’s an inner node, run from 1. for child nodes that intersect ray

Process the closest node first Keep others on stack to process further ones later

Rendering – Spatial Acceleration Structures 73

slide-73
SLIDE 73

BVH Traversal Example

  • 1. Process node if its closest intersection with ray is closer than 𝑢𝑛𝑗𝑜

Rendering – Spatial Acceleration Structures 74

slide-74
SLIDE 74

BVH Traversal Example

  • 1. Process node if its closest intersection with ray is closer than 𝑢𝑛𝑗𝑜

Rendering – Spatial Acceleration Structures 75

slide-75
SLIDE 75

BVH Traversal Example

  • 3. If it’s a leaf, check triangles and update 𝑢𝑛𝑗𝑜 in case of closer hit ( )

Rendering – Spatial Acceleration Structures 76

slide-76
SLIDE 76

BVH Traversal Example

  • 3. If it’s a leaf, check triangles and update 𝑢𝑛𝑗𝑜 in case of closer hit ( )

Rendering – Spatial Acceleration Structures 77

slide-77
SLIDE 77

BVH Traversal Example

  • 3. If it’s a leaf, check triangles and update 𝑢𝑛𝑗𝑜 in case of closer hit ( )

Rendering – Spatial Acceleration Structures 78

slide-78
SLIDE 78

BVH Traversal Example

  • 3. If it’s a leaf, check triangles and update 𝑢𝑛𝑗𝑜 in case of closer hit ( )

Rendering – Spatial Acceleration Structures 79

slide-79
SLIDE 79

BVH Traversal Example

  • 3. If it’s a leaf, check triangles and update 𝑢𝑛𝑗𝑜 in case of closer hit ( )

Rendering – Spatial Acceleration Structures 80

slide-80
SLIDE 80

The Surface Area Heuristic [1]

Simple, but powerful heuristic for choosing splits Created with traversal in mind, based on the following ideas:

Assume rays are uniformly distributed in space Probability of a ray hitting a node is proportional to its surface area Cost of traversing it depends on the number of triangles in its leaves Hence, avoid large nodes with many triangles, because:

They have a tendency to get checked often Getting a definite result (reject or closest hit) is likely to be expensive

81 Rendering – Spatial Acceleration Structures

slide-81
SLIDE 81

Applying the Surface Area Heuristic

Goal: To split a node, find the hyperplane 𝑐 that minimizes 𝑔 𝑐 = 𝑀𝑇𝐵 𝑐 ⋅ 𝑀(𝑐) + 𝑆𝑇𝐵(𝑐) ⋅ (𝑂 − 𝑀 𝑐 ), where

  • 𝑀𝑇𝐵 𝑐 /𝑆𝑇𝐵(𝑐) are the surface area of the nodes that enclose the

triangles whose centroid is on the “left”/“right” of the split plane 𝑐

  • 𝑀(𝑐) is the number of primitives on the “left” of 𝑐
  • N is the total number of primitives in the node

82 Rendering – Spatial Acceleration Structures

slide-82
SLIDE 82

The Sweep SAH BVH

We want to constrain the search space for a good split Pick a set of axes to test (e.g., 3D basis vectors X/Y/Z) When splitting a node with 𝑂 triangles, for each axis

Sort all triangles by their centroid’s position on that axis Find the index 𝑗 that minimizes 𝑔 𝑗 = 𝑀𝑇𝐵 𝑗 ⋅ 𝑗 + 𝑆𝑇𝐵(𝑗) ⋅ (𝑂 − 𝑗), where

  • 𝑀𝑇𝐵(𝑗) is the surface area of the AABB over sorted triangles [0, 𝑗)
  • 𝑆𝑇𝐵(𝑗) is the surface area of the AABB over sorted triangles [𝑗, 𝑂)

Select the axis and index 𝑗 with the best 𝑔 𝑗 for the split overall!

83 Rendering – Spatial Acceleration Structures

slide-83
SLIDE 83

Importance of Optimizing Splits

Important trade-off: building time vs. traversal time

Given the same tracing/traversal code, the quality of a BVH tree may have a big impact on performance! Can be as high as 2x compared to naïve splitting

Benefits depend on the parameters of your rendering scenario

How big is your scene and how are triangles distributed? How long will your BVH be valid? What are the quality requirements for your images?

Rendering – Spatial Acceleration Structures 84

slide-84
SLIDE 84

Evaluation of Combined Building + Traversal [2]

Rendering – Spatial Acceleration Structures 85 Check out the paper this comparison came from https://users.aalto.fi/~ailat1/publications/karras2013hpg_paper.pdf

slide-85
SLIDE 85

Evaluation of Combined Building + Traversal [2]

Rendering – Spatial Acceleration Structures 86 Check out the paper this comparison came from https://users.aalto.fi/~ailat1/publications/karras2013hpg_paper.pdf

slide-86
SLIDE 86

Evaluation of Combined Building + Traversal [2]

Rendering – Spatial Acceleration Structures 87 Check out the paper this comparison came from https://users.aalto.fi/~ailat1/publications/karras2013hpg_paper.pdf

slide-87
SLIDE 87

Spatial Acceleration Structures

Structure Memory Consumption Building Time (Expected) Traversal Time none none none abysmal Regular Grid low – high (resolution) low uniform scene: ok

  • therwise: bad

Quadtree/Octree low – high (overlap/uniformity) low good K-d Tree low – high (overlap) low – high good – excellent BVH low low – high good – excellent

Rendering – Spatial Acceleration Structures 88

slide-88
SLIDE 88

BVH Building Hints

Rendering – Spatial Acceleration Structures

For each split, sort the node’s portion of the triangle list L in-place When constructing child nodes, pass them L and start/end indices

89

slide-89
SLIDE 89

BVH Building Hints

Rendering – Spatial Acceleration Structures

For each split, sort the node’s portion of the triangle list L in-place When constructing child nodes, pass them L and start/end indices

90

slide-90
SLIDE 90

BVH Building Hints

Rendering – Spatial Acceleration Structures

For each split, sort the node’s portion of the triangle list L in-place When constructing child nodes, pass them L and start/end indices

91

slide-91
SLIDE 91

SAH Coding Hints

Rendering – Spatial Acceleration Structures

Don’t loop over triangles at each 𝑗 to get 𝑀𝑇𝐵 𝑗 and 𝑆𝑇𝐵(𝑗)! Precompute them once per node and axis instead

Create two 0-volume bounding boxes 𝐶𝐶𝑀, 𝐶𝐶𝑆 Allocate N+1 entries for 𝑀𝑇𝐵/𝑆𝑇𝐵, set 𝑀𝑇𝐵 0 = 𝑆𝑇𝐵 𝑂 = 0 Iterate 𝑗 over range [1, 𝑂], for each 𝑗:

Merge 𝐶𝐶𝑀 with the AABB of sorted triangle with index (𝑗 − 1) Store surface area of 𝐶𝐶𝑀 as value for 𝑀𝑇𝐵(𝑗) Merge 𝐶𝐶𝑆 with the AABB of sorted triangle with index (𝑂 − 𝑗) Store surface area of 𝐶𝐶𝑆 as value for 𝑆𝑇𝐵(𝑂 − 𝑗)

92

slide-92
SLIDE 92

BVH Building Hints (C++)

Consider using stdlib container (e.g., vector) Try to avoid dynamic memory allocation 2𝑂 − 1 is an upper bound for the total number of nodes you need std::sort(<first>, <last>, <predicate>) std::nth_element(<first>, <nth>, <last>, <predicate>)

Can be used for splitting if you don‘t need exact sorting Reorders the 𝑂-sized vector such that:

𝑜 smallest elements are on the left 𝑂 − 𝑜 biggest are on the right

Faster than sorting!

93 Rendering – Spatial Acceleration Structures

slide-93
SLIDE 93

BVH vs K-d Tree vs Others

Each have their specializations, strengths and weaknesses E.g., K-d Trees with ropes do not require a stack for traversal [5] Which acceleration structure is the best is contentious Currently, BVHs are extremely widespread and well-understood

Rendering – Spatial Acceleration Structures 94

slide-94
SLIDE 94

State-of-the-Art Variants and Trends

Higher child counts (>2) per node, mixed nodes (children + triangles) Actually DO split triangles sometimes to get maximal performance Build BVHs bottom-up in parallel on the GPU [3] In animated scenes, reuse BVHs, update those parts that change Actually use built-in traversal logic of GPU hardware (NVIDIA RTX!)

Rendering – Spatial Acceleration Structures 95

slide-95
SLIDE 95

References and Further Reading

Interesting topics: BVHs for animation, LBVH, SIMD/packet/stackless traversal, Turing RTX architecture [1] Heuristics for Ray Tracing Using Space Subdivision, J. David MacDonald and Kellogg S. Booth, 1990 [2] On Quality Metrics of Bounding Volume Hierarchies, Timo Aila, Tero Karras, and Samuli Laine, 2013 [3] Parallel BVH generation on the GPU, Tero Karras and Timo Aila, 2012 [4] Fast Parallel Construction of High-Quality Bounding Volume Hierarchies, Tero Karras and Timo Aila, 2013 [5] Stackless KD-Tree Traversal for High Performance GPU Ray Tracing, Stefan Popov, Johannes Günther, Hans-Peter Seidel and Philipp Slusallek, 2007 [6] Realtime Ray Tracing and Interactive Global Illumination, Phd Thesis, Ingo Wald, 2004 [7] Bonsai: Rapid Bounding Volume Hierarchy Generation using Mini Trees, P. Ganestam, R. Barringer, M. Doggett, and T. Akenine-Möller, 2015

Rendering – Spatial Acceleration Structures 96