Acceleration Structures CS 6965 Fall 2011 Program 2 Run Program - - PowerPoint PPT Presentation

acceleration structures
SMART_READER_LITE
LIVE PREVIEW

Acceleration Structures CS 6965 Fall 2011 Program 2 Run Program - - PowerPoint PPT Presentation

Acceleration Structures CS 6965 Fall 2011 Program 2 Run Program 1 in simhwrt Also run Program 2 and include that output Lab time? Inheritance probably doesnt work CS 6965 Fall 2011 2 Boxes Axis aligned boxes


slide-1
SLIDE 1

Acceleration Structures

CS 6965 Fall 2011

slide-2
SLIDE 2

Fall 2011 CS 6965

Program 2

  • Run Program 1 in simhwrt
  • Also run Program 2 and include that output
  • Lab time?
  • Inheritance probably doesn’t work

2

slide-3
SLIDE 3

Fall 2011 CS 6965

Boxes

  • Axis aligned boxes
  • Parallelepiped
  • 12 triangles?
  • 6 planes with squares?

3

slide-4
SLIDE 4

Fall 2011 CS 6965

Ray-box intersection

N  ⋅ P   − d = 0 t = d − N  ⋅O   N  ⋅V   x plane: N  = 1

[ ]

t = d − Ox Vx d1 = P

1x,d2 = P 2x

tx1 = P

1x − Ox

Vx ,tx2 = P

2x − Ox

Vx Same for y, z planes O  

4

P

1

 P

2

  O   V   tx1 tx2

slide-5
SLIDE 5

Fall 2011 CS 6965

Intersection of intervals

5

P

1

 P

2

  O   x interval y interval Intersection occurs where x interval overlaps y interval x interval: min(tx1,tx2) ≤ t ≤ max(tx1,tx2) y interval: min(ty1,ty2) ≤ t ≤ max(ty1,ty2) intersection: max(minx,miny) ≤ t ≤ min(maxx,maxy)

In 3D also check z interval

slide-6
SLIDE 6

Fall 2011 CS 6965

Improved Box

6

  • http://www.cs.utah.edu/~awilliam/box/
slide-7
SLIDE 7

Fall 2011 CS 6965

Ray tracing

  • ptimization
  • Faster rays
  • CPU optimization techniques (CS6620 lecture)
  • Fewer rays
  • Adaptive supersampling
  • Ray tree pruning
  • Faster ray-object intersection tests
  • High-order surfaces
  • Fewer ray-object intersection tests
  • Acceleration structures

Adapted from Arvo/Kirk “A survey of ray tracing acceleration techniques”

7

slide-8
SLIDE 8

Fall 2011 CS 6965

Acceleration structures

  • Current ray tracer is O(# objects)
  • Large # of objects can be SLOW
  • Solution: intelligently determine which objects to intersect
  • Good news: <<O(N) achievable!
  • Real-time ray tracer exploits this to do very large models:
  • 262144x262144 heightfield (10+billion patches)
  • 35 million spheres
  • (1.1 million spheres + volume rendering) * 170 timesteps

8

slide-9
SLIDE 9

Fall 2011 CS 6965

Acceleration structures

  • Three main types
  • Tree-based:
  • Binary Space Partitioning (BSP) tree
  • Bounding

Volume Hierarchy

  • Grid-based:
  • Uniform grid
  • Hierarchical grid
  • Octree
  • Directional

9

slide-10
SLIDE 10

Fall 2011 CS 6965

Uniform grid

  • Split space up in a grid
  • A heightfield is a specialized

acceleration structure

  • A grid is more general

10

ray

slide-11
SLIDE 11

Fall 2011 CS 6965

Grid traversal

  • Just like a 2D Grid but in 3D

11

ray

slide-12
SLIDE 12

Fall 2011 CS 6965

Heightfield traversal

  • Step 1: Compute a few derived values
  • Diagonal: D = Pmax-Pmin
  • Cell Size: cellsize = D/(nx, ny, 1)
  • Data min, max: Zmin, Zmax
  • Grid:
  • Add nz
  • Cell size 3D
  • Don’t need Zmin/Zmax
  • Data located in cells, not at corners

12

Pmax Pmin

slide-13
SLIDE 13

Fall 2011 CS 6965

Grid traversal

  • Step 2-8: straightforward extension to 3D

13

slide-14
SLIDE 14

Fall 2011 CS 6965

Heightfield traversal

  • Step 2: Compute tnear
  • Use ray-box intersection
  • Be careful with rays that begin inside (set tnear=0)

14

tnear Pmax Pmin

slide-15
SLIDE 15

Fall 2011 CS 6965

Heightfield traversal

  • Step 3: Compute lattice coordinates of near point
  • World space: P = O+tnear

V

  • Lattice space: L = (int)((P-Pmin)/cellsize)
  • Be careful of
  • roundoff error

15

Pmax Pmin P,L

slide-16
SLIDE 16

Fall 2011 CS 6965

Heightfield traversal

  • Step 4: Determine how ray marching changes index
  • diy = D.y*V.y>0?1:-1
  • stopy = D.y*V.y>0?yres:-1
  • similar for x

16

Pmax Pmin diy=1 diy=-1

slide-17
SLIDE 17

Fall 2011 CS 6965

Heightfield traversal

  • Step 5: Determine how t value changes with ray marching:
  • dtdx = Abs(cellsize.x/V.x)
  • dtdy = Abs(cellsize.y/V.y)

17

dtdy dtdx

slide-18
SLIDE 18

Fall 2011 CS 6965

Heightfield traversal

  • Step 6: Determine the far edges of the cell
  • if dix == 1:
  • far.x=(L.x+1)*cellsize.x+Pmin
  • if dix == -1:
  • far.x=L.x*cellsize.x+Pmin
  • Similar for y

18

far near far near L.x L.x

slide-19
SLIDE 19

Fall 2011 CS 6965

Heightfield traversal

  • Step 7: Determine t value of far slabs
  • tnext_x = (far.x-O.x)/V.x
  • tnext_y = (far.y-O.y)/V.y

19

far tnext_y tnext_x

slide-20
SLIDE 20

Fall 2011 CS 6965

Heightfield traversal

  • Step 8: Beginning of loop Compute range of Z values
  • zenter = O.z+tnear*V.z
  • texit = Min(next_x, next_y)
  • zexit = O.z+texit*V.z

20

zenter zexit Side view

slide-21
SLIDE 21

Fall 2011 CS 6965

Heightfield traversal

  • Step 8: Beginning of loop Compute range of Z values
  • zenter = O.z+tnear*V.z
  • texit = Min(next_x, next_y)
  • zexit = O.z+texit*V.z
  • Grid: not needed

21

zenter zexit Side view

slide-22
SLIDE 22

Fall 2011 CS 6965

Heightfield traversal

  • Step 9: Determine overlap of z range
  • datamin = Min(data[L.x][L.y],
  • data[L.x+1][L.y],
  • data[L.x][L.y+1],
  • data[L.x+1][L.y+1])
  • zmin = Min(zenter, zexit)
  • Similar for max
  • if zmin > datamax || zmax<datamin
  • skip to step 11
  • Grid: not needed (skip cell if no objects in cell)

22

datamax datamin zmax zmin

slide-23
SLIDE 23

Fall 2011 CS 6965

Step 10:

  • Intersect ray with cell
  • 2 triangles, 4 triangles, Bilinear

patch, Bicubic patch

  • Grid: intersect with list of objects

that partially overlap cell

23

slide-24
SLIDE 24

Fall 2011 CS 6965

Heightfield traversal

  • Step 11: March to next cell
  • if tnext_x < tnext_y
  • tnear = tnext_x
  • tnext_x += dtdx
  • L.x += dix
  • else
  • Similar for y
  • Grid: 3 way minimum

24

Pmin far tnext_y tnext_x

slide-25
SLIDE 25

Fall 2011 CS 6965

Heightfield traversal

25

  • Step 12: Decide if it is time to stop

– Stop if hit the surface at t>epsilon – Stop if L.x == stop_x or L.y == stop_y – Stop if tnear > tfar – Otherwise, back to step 8

  • Grid:

– Cannot stop if you find a hit! – Stop if hit.minT < new tnear – Stop if L.x == stop_x or L.y == stop_y or L.z == stop_z – tnear > tfar condition is redundant

Pmax Pmin

slide-26
SLIDE 26

Fall 2011 CS 6965

Stopping example

  • When intersecting cell 1, ray

finds yellow triangle, t outside

  • f cell
  • Proceed to next cell and

intersect again, ray finds blue circle with smaller t

  • Second example shows the
  • pposite

1 2 3 1 2 3

slide-27
SLIDE 27

Fall 2011 CS 6965

Extra work

  • This example highlights a common grid problem: redundant

intersections

  • Ray can intersect yellow triangle up to three times!
  • Worst case: ground polygon
  • Gets worse with increased grid size

27

1 2 3

slide-28
SLIDE 28

Fall 2011 CS 6965

Avoiding extra work

  • Don’t worry about it? (not always effective)
  • Mailbox: store unique ray ID with each object, don’t call

intersect if ray ID == last ray ID for object (NOT thread safe!)

  • Remember last N intersected objects, don’t re-intersect (extra
  • verhead)
  • Hierarchical grid

28

1 2 3

slide-29
SLIDE 29

Fall 2011 CS 6965

Building a Grid

29

slide-30
SLIDE 30

CS6620 Spring 07

Building a grid

  • Add two methods to your object class:

–Required:

// Get the bounding box for this object // Expand the input bounding box void getBounds(BoundingBox& bbox);

–Optional:

// Does the object intersect this box? // always return true if you aren’t sure bool intersects(BoundingBox& cell_bbox);

  • Methods to find these later
slide-31
SLIDE 31

CS6620 Spring 07

Building a grid

Foreach object:

Compute bounding box Transform extents to index space (careful with rounding)

Rounding: Lower: round down Upper: round up lx hx hy ly

slide-32
SLIDE 32

CS6620 Spring 07

Building a grid

Foreach object:

Compute bounding box Transform extents to index space (careful with rounding) 3D loop lx,ly,lz to hx,hy,hz:

Add object to each cell Loop over green area lx hx hy ly

slide-33
SLIDE 33

CS6620 Spring 07

More efficient

Foreach object:

Compute bounding box Transform extents to index space (careful with rounding) 3D loop lx,ly,lz to hx,hy,hz:

If(object intersects cell boundary) Add object to each cell Blue cells won’t get added with an additional check lx hx hy ly

slide-34
SLIDE 34

CS6620 Spring 07

Still more efficient

  • Two pass algorithm:

–First pass: count objects in each cell –Allocate memory all at once –Second pass: insert objects into list

  • Huge memory savings over linked lists

(2X to 8X or more)

  • Huge memory coherence improvement
slide-35
SLIDE 35

CS6620 Spring 07

Other improvements

  • Memory tiling (or bricking in 3D)
  • Pseudo-tiling of contiguous object lists
  • Hierarchical:

–Objects only at bottom levels –Objects mixed throughout the tree –Lots of variations

  • Octree:

–Theoretically optimal –BUT traversal across grid is much faster than up/down grid

slide-36
SLIDE 36

Fall 2011 CS 6965

Grid summary

  • Grids work very well for objects of uniform size
  • Should be easy if you understood the heightfield traversal
  • Build is straightforward
  • Possibly large memory requirements
  • Grid spacing requires tuning (tradeoff memory consumption

and redundant intersections vs. efficiency)

36

slide-37
SLIDE 37

Fall 2011 CS 6965

Bounding primitives

  • Optimize intersections
  • Enclose expensive objects in a simpler primitive
  • If a ray misses the bounds, it misses the object

37

slide-38
SLIDE 38

Fall 2011 CS 6965

Inner bounds

  • Can also be used to know

that a ray hits a particular

  • bject
  • Only good for shadows
  • Rarely used

38

slide-39
SLIDE 39

Fall 2011 CS 6965

Bounding primitives

  • Tradeoff: tight bounds vs. intersection speed
  • Box
  • Spheres
  • Ellipsoids
  • Slabs
  • Box is most common
  • Depends on the relative costs of intersection

39

slide-40
SLIDE 40

Fall 2011 CS 6965

Bounding boxes

  • Box: easy
  • bounds = Min(p1, p2), Max(p1, p2)
  • Sphere:
  • bounds = C-Vector(radius, radius,radius)
  • C+Vector(radius, radius, radius)

40

P1 P2 C

slide-41
SLIDE 41

Fall 2011 CS 6965

Bounding Boxes

  • Triangle:
  • bounds = Min(p1,p2,p3), Max(p1,p2,p3)
  • Plane
  • infinite bounds
  • You may want to consider removing planes from your

renderer at this point

  • Or keep them outside of your accel structures

41

P1 P2 P3

slide-42
SLIDE 42

Fall 2011 CS 6965

Bounding boxes

  • Disc/Ring:
  • Group:
  • Union of object bounding boxes
  • min of mins
  • max of maxs

Cx ± rad Ny

2 + Nz 2

N  = 1 Similar for y/z

42

slide-43
SLIDE 43

Fall 2011 CS 6965

Uses for bounding boxes

  • Quick reject for expensive primitives
  • To fill in grid cells
  • Directly in Bounding

Volume Hierarchy or similar

43

slide-44
SLIDE 44

Fall 2011 CS 6965

Bounding Volume Hierarchy

  • Observe that intersection is like a search
  • We know that searching is O(n) for unsorted lists but O(log n)

for sorted lists

  • How do we sort objects from all directions simultaneously?

44

slide-45
SLIDE 45

Fall 2011 CS 6965

Bounding volume hierarchy

  • Organize objects into a tree
  • Group objects in the tree based on

spatial relationships

  • Each node in the tree contains a

bounding box of all the objects below it

45

slide-46
SLIDE 46

Fall 2011 CS 6965

BVH traversal

  • At each level of the tree,

intersect the ray with the bounding box

  • miss: ray misses the entire

subtree

  • hit: recurse to both

children

46

slide-47
SLIDE 47

Fall 2011 CS 6965

BVH traversal

  • At each level of the tree,

intersect the ray with the bounding box

  • miss: ray misses the entire

subtree

  • hit: recurse to both

children

47

slide-48
SLIDE 48

Fall 2011 CS 6965

BVH traversal

  • At each level of the tree,

intersect the ray with the bounding box

  • miss: ray misses the entire

subtree

  • hit: recurse to both

children

48

slide-49
SLIDE 49

Fall 2011 CS 6965

BVH traversal

  • At each level of the tree,

intersect the ray with the bounding box

  • miss: ray misses the entire

subtree

  • hit: recurse to both

children

49

slide-50
SLIDE 50

Fall 2011 CS 6965

BVH optimizations

  • Stop if the current T value is

closer than the BVH node

50

slide-51
SLIDE 51

Fall 2011 CS 6965

BVH optimizations

  • Stop if the current T value is

closer than the BVH node

  • Traverse down side of tree

that is closer to origin of the ray first

51

slide-52
SLIDE 52

Fall 2011 CS 6965

BVH optimizations

  • Stop if the current T value is

closer than the BVH node

  • Traverse down side of tree

that is closer to origin of the ray first

  • Three or more way split

52

slide-53
SLIDE 53

Fall 2011 CS 6965

Building a BVH

  • Determining optimal BVH structure is

NP-hard problem

  • Heuristic approaches:
  • Cost models (minimize volume or

surface area)

  • Spatial models
  • Categories of approaches:
  • Top down
  • Bottom up

53

  • kay
  • kay

bad

slide-54
SLIDE 54

Fall 2011 CS 6965

Median cut BVH construction

  • Top down approach:
  • Sort objects by position
  • n axis
  • cycle through x,y,z
  • use center of

bounding box

  • Insert tree node with

half of objects on left and half on right

54

slide-55
SLIDE 55

Fall 2011 CS 6965

Weghorst BVH construction

  • Bottom up construction
  • Add objects one at a time to

tree

  • Insert to subtree that would

cause smallest increase to area

55

slide-56
SLIDE 56

Fall 2011 CS 6965

Weghorst BVH construction

  • Bottom up construction
  • Add objects one at a time to

tree

  • Insert to subtree that would

cause smallest increase to area

56

slide-57
SLIDE 57

Fall 2011 CS 6965

Weghorst BVH construction

  • Bottom up construction
  • Add objects one at a time to

tree

  • Insert to subtree that would

cause smallest increase to area

57

slide-58
SLIDE 58

Fall 2011 CS 6965

Weghorst BVH construction

  • Bottom up construction
  • Add objects one at a time to

tree

  • Insert to subtree that would

cause smallest increase to area

58

slide-59
SLIDE 59

Fall 2011 CS 6965

k-d tree

  • Recursively divide space in half
  • Alternate coordinate axes
  • Cycle through axes or store axis split in each

node

  • What do you do with objects split by the

plane?

  • Hard part: where do you split in each

dimension?

59

slide-60
SLIDE 60

Fall 2011 CS 6965

BSP tree

  • Like a k-d tree where splitting planes

can be arbitrarily located

  • Hard part: where do you split in each

dimension?

  • Harder part: how do you orient each

plane?

  • More storage/computation, tighter

bounds

60

slide-61
SLIDE 61

Fall 2011 CS 6965

BSP/k-d tree tradeoffs

  • Build is NP-hard problem
  • Heuristic approaches are always used
  • Traversal is quick
  • Storage can be lower than BVH or grid

61

slide-62
SLIDE 62

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • min: 0
  • max: ∞
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children with updated intervals

62

slide-63
SLIDE 63

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children with updated intervals

63

Min: 0 Max: ∞

slide-64
SLIDE 64

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children

with updated intervals

64

Min: 0 Max: ∞ Split: 1.0 Stack: 1 New min: 0 New max: 1.0 1 2

slide-65
SLIDE 65

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children

with updated intervals

65

Min: 0 Max: 1.0 Split: 1.3 Stack: 1 New min: 0 New max: 1.0 3 4

slide-66
SLIDE 66

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children

with updated intervals

66

Min: 0 Max: 1.0 Split: 0.4 Stack: 1 5 New min: 0 New max: 0.4 5 6

slide-67
SLIDE 67

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children

with updated intervals

67

Min: 0.4 Max: 1.0 Split: 0.7 Stack: 1 8 New min: 0.4 New max: 0.6 7 8

slide-68
SLIDE 68

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children

with updated intervals

68

Min: 0.7 Max: 1.0 Split: Stack: 1 New min: 0.4 New max: 0.6 7 8

slide-69
SLIDE 69

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children

with updated intervals

69

Min: 1.0 Max: ∞ Split: 1.5 Stack: 10 New min: 1.0 New max: 1.5 9 10

slide-70
SLIDE 70

Fall 2011 CS 6965

K-d traversal

  • Keep track of intervals on ray:
  • Compute t of split plane
  • If t > max: goto near child
  • If t < min: goto far child
  • Otherwise: goto both children

with updated intervals

70

Min: 1.0 Max: 1.5 Split: 1.4 Stack: 10 11 New min: 1.0 New max: 1.4 11 12

slide-71
SLIDE 71

Fall 2011 CS 6965

K-d tree build

  • Optimal solution is NP-hard
  • Spatial median split (a little like octree)
  • Object median split (makes balanced trees)
  • Cost model based (better)
  • Cost of traversal
  • Cost of intersection
  • Probability of hit

71

slide-72
SLIDE 72

Fall 2011 CS 6965

K-d tree build

  • Havran ‘01:
  • Start with bounding box of scene
  • Select split plane along each axis
  • Start with spatial median
  • Move toward bounding box of child nodes
  • Recurse
  • Stop if node contains 2-3 objects
  • Stop if depth > max (20-30)
  • Backtrack
  • Combine children with identical content

72

slide-73
SLIDE 73

Fall 2011 CS 6965

Acceleration Structure Summary

  • Most common: Grid, Hierarchical Grid, k-d tree, BVH
  • Grid based:
  • P time deterministic build
  • Grid resolution tradeoff
  • Tree based:
  • NP hard build
  • Heuristic approaches

73