Speeding up Ray-tracing - cuni.cz

49
Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 1 / 49 Speeding up Ray-tracing © 1996-2018 Josef Pelikán CGG MFF UK Praha [email protected] http://cgg.mff.cuni.cz/~pepca/

Transcript of Speeding up Ray-tracing - cuni.cz

Page 1: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 1 / 49

Speeding upRay-tracing

© 1996-2018 Josef Pelikán

CGG MFF UK Praha

[email protected]

http://cgg.mff.cuni.cz/~pepca/

Page 2: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 2 / 49

Ray-scene intersection takes most of the CPU time (Whitted: up to 95%)

➨ scene composed of elementary solids– sphere, box, cylinder, cone, triangle, polyhedron, ..– primitive solids in CSG– number of elementary solids .. N

➨ naïve algorithm tests every ray (up to the proper recursion depth H) against every elementary solid– O(N) tests for one ray

Page 3: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 3 / 49

Classification faster “ray × scene“

➨ faster “ray × solid” test» bounding solids with efficient intersection algorithms

➨ less “ray × solid” tests» bounding volume hierarchy, space subdivision (spatial data

structures), directional techniques (+2D data structures)

less rays» dynamic recursion control, adaptive anti-aliasing

generalized rays (carrying more information)» polygonal ray bundle, ray cone, ..

Page 4: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 4 / 49

Bounding solid

p1

saving case

nonsaving cases

P0

p1

P0

p1

P0

Page 5: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 5 / 49

Bounding solid intersection is [much] faster than with an original

object– sphere, box (axis-aligned “AABB” or arbitrary orientation

“OBB”), intersection of strips, ..

a bounding solid should enclose an original object as tight as possible

eficiency of a bounding solid .. middle ground between and – total asymptotic complexity is still O(N)

Page 6: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 6 / 49

Bounding solid efficiency

Expected intersection time ray vs. object:

B + p · I < I

➨ I .. intersection time with an original object

➨ B .. intersection time with a bouding solid

➨ p .. probability of hitting a bounding solid (how many rays hit a bounding solid in total)

Page 7: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 7 / 49

Bounding solid efficiency

pA/pB = 0.20

pA/pB = 0.23

pA/pB = 0.50

A

B

Page 8: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 8 / 49

Combined bounding solids

better approximation of an original shape

➨ unions and intersections of simple bounding shapes:

Page 9: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 9 / 49

d ax by cz D ax by cz

x y z T x y z T

min , max, , , ,

Convex shapes bounding solid for convex shapes

➨ intersection of strips (“k-dops” system)– strip = space between two parallel planes– efficient computation of d and D constants is necessary:

T T T

Page 10: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 10 / 49

Bounding solids – an efficient algorithm intersections with all bounding solids

intersected bounding solids are sorted in ascending order from the ray origin

original objects will be checked (intersected with the ray) in the same order

if there is an intersection and all bounding solids with closer intersection were already tested, the intersection is the closest one

Page 11: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 11 / 49

An efficient algorithm1

2

3

12

Page 12: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 12 / 49

Bounding Volume Hierarchy (BVH)

III

III

Page 13: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 13 / 49

Hierarchy ideal asymptotic complexity is O(log N)

efficient for well structured scenes– many well separated small objects / clusters– natural in CSG representation (cutting a CSG tree)

➨ automatic construction is possible– very complex optimal methods– suboptimal incremental algorithm

in case of “AABB“ it is called R-tree (Guttman, 1984)– see: database spatial query technology

Page 14: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 14 / 49

K B p I Iii

K

i ii

K

1 1

?

Efficiency of a hierarchy

pi .. probability of hitting the i-th bounding solid

Ii .. time for objects inside of the i-th bounding solid

B .. intersection time with the bounding solid

p1

p2p3

I1

I2

I3

Page 15: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 15 / 49

Efficiency of a hierarchy

S, Si .. surface area of a shape

p C C

P d

P di zá sah i zá sahi Pr |

For single direction d:

area Pi

area P

CCi

P(d), Pi(d) .. area projectedfrom the direction d

pP d dd

P d dd

SSi

i i

For every directionand convex objects:

hit hit

Page 16: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 16 / 49

Incremental construction ideas create an empty hierarchy (tree root)

take the 1st object and insert it into the root– root bounding solid must be updated

for the nth object there are options (in one node):– object will be stand-alone (w/o any bounding solid)– object will have new bounding subsolid– object will go inside an existing bounding solid

➨ order of insertion objects does matter !– some defined 3D order and random shuffle

Page 17: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 17 / 49

Bounding volume hierarchies

➨ “Sphere tree” (Palmer, Grimsdale, 1995)– simple test and transformation, worse approximation

➨ “AABB tree”, “R-tree“ (Held, Klosowski, Mitchell, '95)– simple test, comples transformation

➨ “OBB tree” (Gottschalk, Lin, Manocha, 1996)– simple transformation, more complex test, good approx.

➨ “K-dop tree” (Klosowski, Held, Mitchell, 1998)– more complex transformation and test, excellent approx.

Page 18: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 18 / 49

“Cutting” CSG tree efficient for subtractive set operations (intersection,

difference)

➨ primary bounding solids are assigned to (finite) elementary solids– analytic computation

➨ bounding solids are propagated from leaves to the root node

➨ subtractive operations can reduce bounding solids in ancestors (arguments)

Page 19: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 19 / 49

“Cutting” CSG tree

A

B2B1

A

B

B1 B2B(A)

B(B)

A-BB(A-B)

B(B)

A B1

B(A-B) B(B2) = 0

Page 20: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 20 / 49

Space subdivision (spatial directories) uniform subdivision (equal cells)

+ simple traversal & addressign– many traversal steps– big data volume

nonuniform subdivision (mostly adaptive)+ less traversal steps+ less data– more complex implementation (data struture &

traversal)

Page 21: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 21 / 49

Uniform subdivision (grid)

Cell containslist of intersectedobjects

Page 22: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 22 / 49

Grid traversal (3D DDA)

Dy

Dx

x

yLy

Lx

Page 23: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 23 / 49

Grid traversal (3D DDA)

➨ ray: P0 + t · p1 for t > 0

for the given direction p1 there are precomputed constants Dx, Dy, Dz:– distance between subsequent intersections of the ray and

the parallel wall system (perpendicular to x, y, z)

for the P0 there is an initial cell [ i, j, k ] and quantities t, Lx, Ly, Lz:– ray parameter t, distances to the closest walls in the x, y, z

system

Page 24: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 24 / 49

Grid traversal (3D DDA)

processing in the cell [ i, j, k ] (intersections)

stepping to the next cell:– D = min {Lx,Ly,Lz}; /* assumption: D = Lx */– Lx = Dx; Ly = Ly - D; Lz = Lz - D;

– i = i ± 1; /* according to the sign of P1x */

end conditions:– an actual (the closest) intersection was found

» the intersection is in the current cell– no intersection was found and the next cell is outside of

the grid domain

Page 25: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 25 / 49

Nonuniform subdivision of space

Octree:13 cells(grid: 17 cells)

(3 objects tested)

Page 26: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 26 / 49

Adaptive subdivision systems➨ octree (division in the middle)

– representation – pointers, implicit representation or hash table (Glassner)

➨ KD-tree (Bentley, 1975)– static division: in the middle, cyclic coordinate component– adaptive: both components and bounds are dynamic

➨ [general BSP-tree]– dividing planes have arbitrary orientation

Page 27: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 27 / 49

Octree storage by Glassner1

1112 13 14 15

1617 18

...

111 112

...

161162

168

1621

...

1622

Page 28: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 28 / 49

Octree storage by Glassner

➨ each individual cell has its signature– root .. 1– ancestors of the root .. 11 až 18, .. etc.– each voxel (potential cell) has its specific signature

➨ actual tree nodes are stored in sparse hash table– hash-function example: Signature mod TableSize

Page 29: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 29 / 49

Tree traversal (Glassner)

point on the ray .. [ x, y, z ]– associated voxel's signature .. [ 1 ÷ 8 ] k

➨ look for all prefixes of the code in the hash table– the 1st (shortest) found prefix defines the current cell

➨ after cell processing the point [ x, y, z ] is moved in the direction of the ray (p1)

– the new point is localized, ...

Page 30: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 30 / 49

KD-tree (static variant)

KD-tree:7 cells(5 objects tested)

I

II

III

III

IV

III VI

III

V

VVVI

II

Page 31: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 31 / 49

Adaptive subdivision criteria

limited number of objects and subdivision depth– if a cell is intersected by more than M objects (e.g.

M = 4 .. 32 ), subdivide it– maximal subdivision level is K (e.g. K = 5 .. 25)

limited number of cells or memory occupationinstead of subdivision depth limit:

– subdivision is finished after filling the whole dedicated memory

– subdivision controlled by a breadth-first traversal (FIFO data structure holding candidate cells)

Page 32: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 32 / 49

Traversing adaptive subdivision➨ marching the ray: finding the next cell from the root

(see Glassner's method)

➨ preprocessing: tree traversal used for dividing the ray into individual segments (intersections with cells)– t parameter segments for individual cells

➨ additional support data (à la “finger tree”)– pointer to the neighbour cell (on the same tree level)

recursive depth-first traversal with heap– heap: list of potentially intersected cells (ordered from the

most promising ones)

Page 33: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 33 / 49

“Mailbox” technique

A

B

C

1: nil

4: intersection

5: intersection1 2 3

45

The intersection must be in the current cell (else it is cached)

Page 34: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 34 / 49

Abstract space division

no need to test (even to access!) lists of objects, which were already tested

list of objects needs to be processed only in a cell with different (bigger) set of objects

➨ cells can share equal object lists– tested lists are marked by a special flag– processing only nonmarked lists– mailbox technique is used on the object level

Page 35: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 35 / 49

Abstract space division

A

B

C

[ A ]

[ B ]

[ A,C ]

1

23

[ ]

[ B,C ]

Page 36: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 36 / 49

Macro-cells (Miloš Šrámek)

Cell contains“safe distance”(empty space)

Page 37: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 37 / 49

Directional speedup techniques utilizing directional cube:

➨ light buffer– speeding up shadow rays to point light sources

➨ ray coherence– for all secondary rays

5D ray classification

image plane directory (visibility precomputation)– only for primary rays

Page 38: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 38 / 49

Directional cube (adaptive)

x

y

z

P0

p1

Page 39: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 39 / 49

Directional cube

axis-oriented

cube faces divided into cells– uniform or adaptive division– every cell stores list of relevant objects (can be ordered by

the distance from the cube)

➨ HW rasterization and visibility (depth-buffer) can be used for uniform division

Page 40: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 40 / 49

Light buffer

speeding up shadow rays

➨ directional cube in every point light source– possible visibility of objects from the light-source point– some cells might be covered completely by one object

(everything else is in shadow)

➨ for a shadow ray only objects projected in the relevant cell are considered

Page 41: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 41 / 49

Ray coherence

cos

1 1 2

1 2

R RS S

S1

R1

S2

R2

Page 42: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 42 / 49

Speedup utilizing coherence

for every secondary rays– reflected, refracted, shadow

assumed bounding solid: sphere

➨ directional cube placed in every center of bounding sphere– list of projected objects/light sources in every cell

» coherence condition is used» lazy evaluation!

– lists can be ordered by distance from the cube

Page 43: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 43 / 49

5D ray space rays in 3D scene:

– origin P0 - [ x, y, z ]

– direction [ , ]

5D hypercube divided into cells– every cell contains list of possible intersections for the

associated ray pencil (“beam”)– adaptive subdivision (merging neighbour cells with equal

or similar lists)

➨ 6D variant: one more quantity (time) for animations

Page 44: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 44 / 49

Ray classification

origin (2-3D) + direction (1D, 2D) = bundle / pencil

Page 45: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 45 / 49

Image plane directory

for primary rays

projection plane is (adaptively) divided into cells– possible visibility of individual objects in a cell (together

with order)

– complete coverage by one cell by one object is possible (hard to test)

➨ robust variant of used visibility method– in most pixels it can be done with complete certainty

Page 46: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 46 / 49

Generalized rays

computing more information about f(x,y)– for anti-aliasing (average color estimation) or soft shadows

(shadow ratio)

– some restrictions to a scene are necessary

➨ forms of generalized rays– rotational or elliptical cone, regular pyramid

– pyramid with polygonal cross section (polygonal scene, see the next slide)

Page 47: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 47 / 49

Polygonal scene

P0

Page 48: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 48 / 49

References A. Glassner: An Introduction to Ray Tracing,

Academic Press, London 1989, 201-262

A. Watt, M. Watt: Advanced Animation and Rendering Techniques, Addison-Wesley, Wokingham 1992, 233-248

V. Havran: Heuristic Ray Shooting Algorithms, PhD thesis, FEL ČVUT Praha, 2001

P. Konečný: Obalová tělesa v počítačové grafice, M.S. thesis, Masaryk University, Brno 1998

Page 49: Speeding up Ray-tracing - cuni.cz

Speedup 2018 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca 49 / 49

References II

J. Klosowski, M. Held, J. Mitchell, H. Sowizral, K. Zikan: Efficient collision detection using bounding volume hierarchies of k-dops, IEEE Transactions on VaCG, 21–36, January-March 1998

H. Samet: Foundations of Multidimensional and Metric Data Structures, Morgan Kaufmann, 2006

H. Samet: The Design and Analysis of Spatial Data Structures, Addison-Wesley, 1990