Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream...

23
Ray-Casting Algebraic Surfaces using Stream Computing Johan Seland – [email protected] Joint work with Martin Reimers 24. January Geilo Winter School 2008 Applied Mathematics 1/16

Transcript of Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream...

Page 1: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Ray-Casting Algebraic Surfaces usingStream Computing

Johan Seland – [email protected] work with Martin Reimers

24. JanuaryGeilo Winter School 2008

Applied Mathematics 1/16

Page 2: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Algebraic surfaces

Definition

For a function f : R3 → R, animplicit surface can be defined bythe level set of the equationf (x , y , z) = c , where x , y , z ∈ R.

Definition

If the function f is a polynomial,it is called algebraic. Theresulting surface is called analgebraic surface.

Applied Mathematics 2/16

Page 3: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Goals for this talk

Give a brief introduction to ray-casting

Demonstrate hybrid CPU/GPU usage

Demonstrate pre-evaluation

Applied Mathematics 3/16

Page 4: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Raycasting

Raycasting amounts to “shooting” rays inside a view frustum (VF)and determine if they intersect the surface.

Ray casting has traditionally been a very slow process

Assume a screen resolutionof (m + 1)× (n + 1) pixels.

Pixel (p, q) corresponds to aray through p and the pixelwith coordinates(p/m, q/n).

prpq(t)

far pla

ne

near pla

ne

Applied Mathematics 4/16

Page 5: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Raycasting algorithm

Along a ray rpq(t), we seek t ∈ [0, 1] such that

f ((1− t)npq + tfpq) = f (rpq(t)) = 0

Naive approach:

Work directly on f

Solve using Newton like method

Conceptually “easy”

f is expensive to evaluate

How to ensure we find the first solution?

f could be numerically unstable

Embarrassingly parallel

⇒ perfect for stream processing?

Applied Mathematics 5/16

Page 6: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Bernstein Polynomials

We would like to work on a univariate Bernstein polynomials

f (rpq(t)) =d∑

k=0

cpqkBdk (t) = 0

The Bernstein Basis:

Bdk (t) =

(dk

)td(1− t)d−k

Σdk=0B

dk (t) = 1

bdk (t) ≥ 0, t ∈ [0, 1]

Not orthogonal basis

Proved to be numerically optimal

Nice algorithms for root finding

t

B3(t)

B30

B31 B3

2

B33

Applied Mathematics 6/16

Page 7: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

The View Frustum Form

Idea: Parameterize the view frustum over the unit cube, s. t.(u, v , 0) and (u, v , 1) maps to points on the near and far plane.

p

far pla

ne

near pla

ne

u

v

w

L

A ray in the view frustum is given by: rpq(w) = L(p/m, q/n, w).We define the View Frustum Form to be:

g = f L : [0, 1]3 → R.

Applied Mathematics 7/16

Page 8: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Using the composition g = f L,

f (L(p

m,q

n, w)) = g(

p

m,q

n, w) =

d ,d ,d∑i ,j ,k=0

gijkBdi (

p

m)Bd

j (q

n)Bd

k (w)

=d∑

k=0

d ,d∑i ,j=0

gijkBdi (

p

m)Bd

j (q

n)

︸ ︷︷ ︸

cpqk

Bdk (w).

Yielding univariate ray equations of degree d ,

f (rpq(t)) =d∑

k=0

cpqkBdk (t).

Applied Mathematics 8/16

Page 9: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Computing VFF Coefficients

We choose to find the VFF coefficients G = (gijk) by solving aninterpolation problem.

Choose (d + 1)3 distinct interpolation points (up, vq, wr ) on agrid.

Solve

d ,d ,d∑i ,j ,k=0

gijkBdi (up)︸ ︷︷ ︸

Ωp

Ωq︷ ︸︸ ︷Bd

j (uq)Bdj (ur )︸ ︷︷ ︸

Ωr

= f (L(up, vq, wr ))

Needs inverse of Bernstein collocation matricesΩp = (Bd

i (up)).

Pre-evaluate: Only dependent on d

Use Chebyshev interpolation points for stability.

Not dependent on the representation of f .

Applied Mathematics 9/16

Page 10: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Computing ray coefficients

Remember g = f L:

f (L(p

m,q

n, w)) =

d∑k=0

d ,d∑i ,j=0

gijkBdi (

p

m)Bd

j (q

n)

Bdk (w).

Basis functions evaluated at every pixel

Only dependent on screen resolution and degree

Pre-evaluate Bernstein collocation matrices as well

M = (mij) = Bdi ( p

m ), N = (nij) = Bdi ( q

n )

Applied Mathematics 10/16

Page 11: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Algorithmic flow

This suggest the following “passes”:For a given d and screen resolution:

Pre-process:

Evaluate the inverse Ω matricesEvaluate the pre-evaluated ray-polynomials M, N

For each frame

Evaluate f L at (d + 1)3 interpolation pointsApply Ω−1

Calculate ray-coefficients MCkNFind first intersection of each rayShade all intersections based on normal-estimate

Applied Mathematics 11/16

Page 12: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Algorithmic flow

This suggest the following “passes”:For a given d and screen resolution:

Pre-process:

CPU: Evaluate the inverse Ω matricesCPU: Evaluate the pre-evaluated ray-polynomials M, N

For each frame

CPU: Evaluate f L at (d + 1)3 interpolation pointsCPU: Apply Ω−1

Transport ray coefficient to GPUGPU: Calculate ray-coefficients MCkNGPU: Find first intersection of each rayGPU: Shade all intersections based on normal-estimate

Applied Mathematics 11/16

Page 13: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Matrix-Tensor product

For each frame we calculate the ray coefficients

Ck = MGkN

Matrix-Tensor product is perform in a dedicated CUDA kernel

M and N are stored in texture memory

G are stored in constant memory

Coalesced read of float4 into shared memory

Blockwise matrix-multiply

Coalesced write of ray coefficients (4-components at a time)

Repeated (d/4 + 1) times

Applied Mathematics 12/16

Page 14: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Root finding

b1 = − 14

b2 = 1

b3 = br3 = − 1

6

t

f (t) =P3

i=0 bi B3i (t)

− 14

0

12

1

16

13

12

23

56

1

Init: We only know the control points

Applied Mathematics 13/16

Page 15: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Root finding

b1 = − 14

b2 = 1

b3 = br3 = − 1

6

t

f (t) =P3

i=0 bi B3i (t)

− 14

0

12

1

16

13

12

23

56

1

Find the first root of the control polygon, t = 2/9

Applied Mathematics 13/16

Page 16: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Root finding

b0 = bl0 = 1

2

bl1

bl2

bl3 = br

0br

1

br2

br3

t

f (t) =P3

i=0 bi B3i (t)

− 14

0

12

1

16

13

12

23

56

1

Subdivide at t → two new control polygons

Applied Mathematics 13/16

Page 17: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Root finding

b0 = bl0 = 1

2

bl1

bl2

bl3 = br

0br

1

br2

br3

t

f (t) =P3

i=0 bi B3i (t)

− 14

0

12

1

16

13

12

23

56

1

Again, find zero of control polygon – subdivide

Applied Mathematics 13/16

Page 18: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Root finding

t

f (t) =P3

i=0 bi B3i (t)

− 14

0

12

1

16

13

12

23

56

1

Yields two new control polygons – repeat

Applied Mathematics 13/16

Page 19: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Root finding

t

f (t) =P3

i=0 bi B3i (t)

− 14

0

12

1

16

13

12

23

56

1

Method converges quadratically

Applied Mathematics 13/16

Page 20: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

CUDA implementation of root finding

Each ray is processed by a ray

Coefficients are read (coalesced) 4-coeffients at a time

The root finding kernel is specialized per degree

Can lead to very divergent behavior within one warp

Future work: Predict behavior based on ray coefficients

Applied Mathematics 14/16

Page 21: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Results

Visualizes surfaces up to degree 18

24 FPS for d = 8, 12 FPS for d = 10, 3FPS for d = 18

Accepted to Eurographics 2008

Fierce competition

Several approaches to this problem fight for performance crown

OpenMP performance much lower due to thread startup cost

Applied Mathematics 15/16

Page 22: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

That’s all folks!

Thank you for listening

Applied Mathematics 16/16

Page 23: Ray-Casting Algebraic Surfaces using Stream ComputingRay-Casting Algebraic Surfaces using Stream Computing Johan Seland { johan.seland@sintef.no Joint work with Martin Reimers 24.

Shameless advertisement!

SINTEF has open positions if you are interested in(GP)GPU, Cell or CMP programming!

Applied Mathematics 17/16