spatial data structures jon mccaffrey cis 565. goals spatial data structures (construction esp.) ...

139
SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565

Upload: scott-parsons

Post on 16-Jan-2016

258 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

SPATIAL DATA STRUCTURESJon McCaffreyCIS 565

Page 2: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Goals

Spatial Data Structures (Construction esp.) Why What How

Designing Algorithms for the GPU

Page 3: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Why

Accelerate spatial queries Search problem

Target Application Today: Ray Tracing Culling

Page 4: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Real-Time

Dynamic Scenes? Tradeoffs

Construction cost Quality

Rebuild or Refit?

Page 5: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Why build on the GPU?

Alternate Reality: Build on CPU Ship down to the card

Page 6: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Why build on the GPU?

Alternate Reality: Build on CPU Ship down to the card

For static scenes, do this: Precompute on CPU. Take a long coffee break. Never look back.

Page 7: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Why build on the GPU?

Alternate Reality: Build on CPU Ship down to the card

Why not: Slow CPU builds Waste bandwidth Dynamic data may already be on the card

Page 8: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Pipeline

Construction

Ray Generation

Traversal Intersection

Shading

Page 9: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Construction Cost v. Quality

Page 10: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

References

• GPU-Accelerated Uniform Grid Construction for Ray Tracing Dynamic Scenes• Ivson

• A Parallel Algorithm for Construction of Uniform Grids• Kalojonov

• Fast BVH Construction on GPUs• Luebke et. Al.

• Real-Time KD-Tree Construction on Graphics Hardware• Zhou

Page 11: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Why Focus on Construction?

Its the hard part. Think sorting v. binary search

Page 12: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

•GPU-Accelerated Uniform Grid Construction for Ray Tracing Dynamic Scenes

• Ivson

•A Parallel Algorithm for Construction of Uniform Grids

• Kalojonov

Uniform Grid

Page 13: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Uniform Grid

Regularly subdivide space in cells Bin primitives into cells Lookup by cells

Page 14: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Traversal

While (!Done): Cell = Advance DDA If (ray hits a primitive in cell):

Done = True

Page 15: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 16: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 17: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 18: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 19: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 20: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 21: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 22: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 23: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 24: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Strengths

Simple Fast to construct

Rebuild per-frame 20 fps for 250k primitives, and I suck.

Front-to-Back Ordering Array lookup, not binary search

Page 25: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Weaknesses

Not adaptive Uniform subdivision Good for unstructured data

Page 26: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 27: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Weaknesses

Not adaptive Uniform subdivision Good for unstructured data

Page 28: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Building

Bound Scene and Divide into Cells Bin Primitives Into Cells

Page 29: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Building

Bound Scene and Divide into Cells Bin Primitives Into Cells

Page 30: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Problem

“Bin Primitives Into Cells” CPU-style

Cell = bin(primitive) Index = cell.next_slot If Index >= cell.size, resize cell Cell.next_slot ++; Cell[Index] = primitive

Needs dynamic allocation Needs global synchronization No and No.

Page 31: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Global Synchronization

Primitives write which cell(s) they are in in their own space

Sort by cell ID Each cell searches for its primitives

Page 32: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

All Together

Page 33: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Sorting

Page 34: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

End Result

Page 35: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Allocation

Solution: Two Passes Device First Pass: Compute needed

space Host: Readback and Allocate Device: Write

Page 36: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

For Each Primitive

Count overlapped Cells Exclusive Scan counts to compute

indices Write overlapped cells from indices[i] to

indices[i] + counts[i] - 1

Page 37: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Counts to Indices

33 11 11 44 22 11 33 11

0 41 2 3 5 6 7

Scan

00 33 44 55 99 1111 1212 1515 1616

Counts

Indices

Buffer Size

Page 38: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Runtime

Page 39: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Wrap-Up

Fast, Easy, Limited Want something adaptive

Take advantage of scene structure

Page 41: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Bounding Volume Hierarchies Many scenes contain hierarchical

structure Exploit

Page 42: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 43: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 44: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 45: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 46: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 47: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 48: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Traversal

traversal(ray,node): If !intersect(ray,node.bound) //Culling

Return MISS If node.leaf: //Base case

Return intersect(ray,node.primitives) Else: //Recur

Left = traversal(ray,node.left) Right = traversal(ray,node.right) Return min(left,right)

Page 49: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Constructing

Sometimes, hierarchy is obvious Articulated figure Just refit bounding volumes per frame

Sometimes not How do you construct?

Page 50: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Traditional CPU

Make the best BVH for ray tracing Surface Area Heuristic (More on this)

estimates quality Minimize SAH

Page 51: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Problem

This is expensive Starvation at top splits (More later)

Page 52: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Starvation

Page 53: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Slight Sidetrack

How to make a binary tree from a sorted list?

Page 54: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Linear BVH

Linearize Problem Use space-filling curve to reduce to 1-d

Reduces to sorting

Page 55: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Morton Curve

Page 56: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Morton Code

Cheap Preserves locality Discretize coordinates (2^k x 2^k x 2^k

grid) k-bit int per dimension

Interleave the bits (3k-bit int)

Page 57: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Splits

Split based on Morton code (Radix-2 sort) 0 or 1 at bit i => Left or Right branch at

level h

0010

Page 58: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Pairs

Question: Between each adjacent pair (in sorted Morton sequence), at what level(s) does a split exit?

Answer: At all levels after their ith bit differs Share ancestors before that

Page 59: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Split Lists

For Pair in parallel: Record each level at which a split exists index, level pairs [(I,h),(I,h+1),(I,h+2),(I,h+2), … ]

Concatenate lists Stable Sort by level!

Page 60: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Intervals

Splits form intervals on each level

Page 61: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

To-Do

Explicit top-down pointers Compute bounding volumes

Reduce up hierarchy

Page 62: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Limitations

Not optimized for ray tracing Morton code approximates locality

Page 63: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Hybrid

SAH construction starves at the top-level splits

Use LBVH for the high-level splits SAH near the leaves

Page 64: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Starvation

Page 65: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

BVH Limitations in General

Storage Space: Bounding Volume per node

Only approximate front-to-back ordering

Page 66: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Front-To-Back Ordering

Must intersect with both sub-trees

Page 67: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Performance

Page 68: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 69: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Grid Performance

Page 70: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

•Real-Time KD-Tree Construction on Graphics Hardware

• Zhou

Kd-Tree

Page 71: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

8 fps

Page 72: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

They also do Photon Mapping.

Page 73: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Everyone loves (Real-time) Caustics

Shiny!

8 f

ps!

Page 74: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Performance (Since we just saw it)

Page 75: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

K-d Tree

Axis Aligned BSP Tree

Page 76: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Strengths

Adaptive Compact Efficient Tight-fitting

Page 77: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Weaknesses

Rigid Expensive to construct Hard to refit

Page 78: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Comparison

Far away the best choice for static CPU ray-tracer

Tougher question on GPU Cost of construction Efficiency of hierarchical branching

structures Many levels Dependent memory reads

Still fast

Page 79: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 80: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Ray Tracing Performance

Zero to Millions in 45 Minutes?!

Gordon Stoll, Intel

Page 81: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

kD-Trees

Page 82: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

kD-Trees

Page 83: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

KD-Trees - http://donar.umiacs.umd.edu/quadtree/points/kdtree.html

Page 84: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

kD-Trees

Page 85: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Advantages of kD-Trees• Adaptive– Can handle the “Teapot in a Stadium”

• Compact– Relatively little memory overhead

• Cheap Traversal– One FP subtract, one FP multiply

Page 86: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Take advantage of advantages• Adaptive–You have to build a good tree

• Compact–At least use the compact node

representation (8-byte)–You can’t be fetching whole cache lines

every time• Cheap traversal–No sloppy inner loops! (one subtract, one

multiply!)

Page 87: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

“Bang for the Buck”

A basic kD-tree implementation will go pretty fast…

…but extra effort will pay off big.

Page 88: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Fast Ray Tracing w/ kD-Trees• Adaptive• Compact• Cheap traversal

Page 89: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Building kD-trees

• Given:– axis-aligned bounding box (“cell”)– list of geometric primitives (triangles?) touching

cell

• Core operation:– pick an axis-aligned plane to split the cell into

two parts– sift geometry into two batches (some

redundancy)– recurse

Page 90: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Building kD-trees

• Given:– axis-aligned bounding box (“cell”)– list of geometric primitives (triangles?) touching

cell

• Core operation:– pick an axis-aligned plane to split the cell into

two parts– sift geometry into two batches (some

redundancy)– recurse– termination criteria!

Page 91: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Building good kD-trees• What split do we really want?– Clever Idea: The one that makes ray tracing

cheap– Write down an expression of cost and minimize

it– Cost Optimization

• What is the cost of tracing a ray through a cell?Cost(cell) = C_trav + Prob(hit L) * Cost(L)

+ Prob(hit R) * Cost(R)

Page 92: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Splitting with Cost in Mind

Page 93: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Split in the middle

• Makes the L & R probabilities equal

• Pays no attention to the L & R costs

Page 94: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Split at the Median

• Makes the L & R costs equal

• Pays no attention to the L & R probabilities

Page 95: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Cost-Optimized Split

• Automatically and rapidly isolates complexity

• Produces large chunks of empty space

Page 96: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Building good kD-trees• Need the probabilities–Turns out to be proportional to surface

area

• Need the child cell costs–Simple triangle count works great (very

rough approx.)Cost(cell) = C_trav + Prob(hit L) * Cost(L) + Prob(hit R) * Cost(R)

= C_trav + SA(L) * TriCount(L) + SA(R) * TriCount(R)

Page 97: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Surface Area Heuristic

This is the Surface Area Heuristic.

Page 98: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Building good kD-trees• Basic build algorithm– Pick an axis, or optimize across all three– Build a set of “candidates” (split locations)• BBox edges or exact triangle intersections

– Sort them or bin them–Walk through candidates or bins to find

minimum cost split

• Characteristics you’re looking for– “stringy”, depth 50-100, ~2 triangle leaves,

big empty cells

Page 99: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Fast Ray Tracing w/ kD-Trees• adaptive–build a cost-optimized kD-tree w/ the

surface area heuristic• compact• cheap traversal

Page 100: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Fast Ray Tracing w/ kD-Trees• adaptive–build a cost-optimized kD-tree w/ the

surface area heuristic• compact–use an 8-byte node– lay out your memory in a cache-friendly

way• cheap traversal

Page 101: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

kD-Tree Traversal Step

split

t_split

t_min

t_max

Page 102: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

kD-Tree Traversal Step

split

t_split

t_min

t_max

Page 103: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

kD-Tree Traversal Step

split

t_split

t_min t_max

Page 104: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

kD-Tree Traversal Step Given: ray O & iV (1/V), t_min, t_max, split_location,

split_axis

t_at_split = ( split_location - ray->P[split_axis] ) * ray_iV[split_axis]

if t_at_split > t_min need to test against near child If t_at_split < t_max need to test against far child

Page 105: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Example

O= <1,4,2> V = <x,x,0.5> iV = <x,x,2> Split = (*,*,8) t = (8 -2)*2

Page 106: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

kD-Tree Traversal while ( not a leaf ) t_at_split = ( split_location - ray->P[split_axis] ) *

ray_iV[split_axis] if t_split <= t_min continue with far child // hit either far child

or none if t_split >= t_max continue with near child // hit near child only // hit both children push (far child, t_split, t_max) onto stack continue with (near child, t_min, t_split)

Page 107: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Real-Time KD-Tree Construction on Graphics Hardware

Traversing KD-Trees on the GPU can be done... (more on that later)

First efforts built the Kd-Tree offline on the CPU and shipped it down to the GPU

Too slow for dynamic scenes This saves transfer bandwidth, and

allows meshes generated GPU-side to be partitioned

Page 108: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Construction overview

Approximate Greedy Top-Down Method

Evaluate heuristic cost for all possible splitting plane candidates

Pick the plane that minimizes the heuristic

Sort triangles and pass them along to the two children

Breadth first construction

Page 109: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Heuristics Balancing cost of evaluation vs

performance Different uses need different

heuristics Ray casting likes lot of empty

space Isolate Complexity

Page 110: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Surface Area Heuristic

Good for Ray Intersection Testing Must be evaluated per potential split,

2 potential splits per primitives Minimize SAH(x) = Ct +

Cl(x)Al(x)/A+Cr(x)Ar(x)/A Heuristic is the (constant) cost of the top

level node + the cost of the left and right nodes times the probability that the ray will traverse that node

Probability is surface area of the subnode/surface area of the top level node

Is how likely a random ray is to hit the child node given that it hit the parent

Page 111: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Greedy Approximation

SAH(x) = Ct + Cl(x)Al(x)/A+Cr(x)Ar(x)/A The costs for the subnodes could only be

evaluated after the tree is built We approximate by assuming that the

subnodes are leaves Cost is simply number of primitives in

each subnode The number of intersection tests

to be performed

Page 112: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

SAH Evaluation

SAH cost for the splitting plane will always be minimized at the edge of a primitive

If the plane is in the middle of a number of primitives, we could move it to the edge of one of them, and it would only be in one node instead of two

If its in the middle of empty space, we could move it to the edge of the half with more children to minimize that half's SA

Should evaluate at both sides of every primitive in a node

Too expensive for large nodes

Page 113: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Two Levels of Parallelism

SAH is too expensive to compute on heavily populated nodes

Employ different heuristics At the high levels, there are few nodes,

but many primitives in each node Parallelize over primitives

At low levels, there are many nodes, with a small number of primitives in each

Parallelize over nodes

Page 114: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Similarity: Hybrid BVH

Very similar to previous algorithm.

Page 115: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Large Node Heuristics

Employ a combination of empty space maximizing and spatial median splitting

Place the plane at set positions (ie 25%

If one child is empty, use that position

Teapot in a stadium Otherwise use the median along the

splitting axis

Page 116: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

The Algorithm

Brace yourselves for complexity

Page 117: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 118: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

ProcessLargeNodes(active,small,next): Compute per-node bounding box

Segmented reduction Look for empty space, else split at the

median For triangles in parallel, place (and clip)

into children Count triangles in children (Segmented

Reduction) If small, add to small list If large, add to next list

Page 119: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Clipping

The fact that a triangle can belong to multiple nodes makes this a bit messy

Clipping means we can have to duplicate triangles unpredictably

Without clipping we could presort the triangles in all 3 dimensions at the beginning, and the sorted orders would be valid all the way through

Page 120: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Data Structure Triangle-Node Association List for

each list of nodes Stores triangles indices for each

node, sorted by node index Each node stores the index of its

first triangle and the number of triangles it has

Page 121: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Small Node Stage Parallelized over nodes rather than

over triangles SAH heuristic rather than

median/empty space Storage optimizations:

Triangle Lists are stored only at highest level small node (fixed size)

Sub-small nodes use a bit mask of their small ancestor

Stops clipping

Page 122: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Bit Mask

Page 123: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Triangle Bit Mask

Triangle Bit Mask allows for efficient evaluation of SAH cost, using bitwise operations

Bitwise AND current node and the split candidate masks to get bit mask for a child node

Parallel bit-counting routine to count triangles

Page 124: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Small Node Stage

If all the SAH estimates for cuts are higher than the cost of the node as is, it is left as a leaf

Now, cuts are made only at AABB boundaries

Page 125: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

PreprocessSmallNodes(smalllist) For each node in parallel:

For all split candidates s in parallel: //Bit masks S.left = triangle set on left of split S.lright = triangle set on right of split

Page 126: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

PreprocessSmallNodes(smalllist) For each node in parallel: //Block

For all split candidates s in parallel: //Threads //Bit masks S.left = triangle set on left of split S.right = triangle set on right of split

Amortize triangle loads in shared memory

Page 127: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 128: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

ProcessSmallNodes(active,next) For each node i in active in parallel

m = triangle bit mask of i SAH_o = count(m) For all splits s in parallel:

Cl = count(m & s.left) Cr = count(m & s.right) SAH_s = Cl*Al + Cr*Ar + const

If best SAH_s < SAH_o Split I Left.m = m & s.left; right.m = m&s.right Add to next

Else i is a leaf

Page 129: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Additional Uses!

K-d trees can be used for fast K-nearest neighbor lookups

The authors used this to implement GPU Photon Mapping

Generate a point set of photons Build a KNN tree Accelerate Nearest Neighbor

queries Different construction heuristics

Page 130: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU
Page 131: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Remarks

Ray tracing on the GPU, start to finish Construction v. Quality Parallel Primitives

Sort Scan Reduce

Page 132: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Mapping to Parallel

Data Parallelism may change during an algorithm

Ex. kD tree: Parallel on Primitives, then Nodes

Ex. Grid: Parallel on Primitives, then Cells

Page 133: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Realtime Raytracing

Don’t expect it in Crysis 2 Science/Engineering Apps Accelerating Production Rendering

Page 134: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Spatial Structures

Ray Tracing Photon Mapping Collision Detection Physics Simulation Nearest-Neighbor Queries Databases & Search

Page 135: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Thanks!

To Joe, for the opportunity! To the researchers, for great work! To you guys, for listening!

Page 136: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Go Forth!

And start Homework 4 now. It’s really long. No seriously, like tonight. There’s 6 parts. You write a cloth simulation.

That’s only 20 points of the homework! So go read it.

Page 137: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Things I didn’t talk about

Traversal (In –depth) Intersection Shading Repairing “damaged” data structures CPU construction CPU Ray Tracing (Is also fast) Packet Ray Tracing Surface Ray Tracing

Page 138: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

More Things

Voxel Ray Casting Ray Trace-Rasterization Hybrids Visibility Culling Octrees Sparse Grids Higher Dimensional Structures Global Illumination Distribution Ray Tracing Antialiasing

Page 139: SPATIAL DATA STRUCTURES Jon McCaffrey CIS 565. Goals  Spatial Data Structures (Construction esp.)  Why  What  How  Designing Algorithms for the GPU

Wow!

These are all interesting things.