GPU Gems 3: Chapter 30. Real-Time Simulation and. Rendering of Fluids. Crane
, Llamas, Tariq. ME290R Presentation by. Brian Kazian. 1 ...
GPU Gems 3: Chapter 30 Real-Time Simulation and Rendering of Fluids Crane, Llamas, Tariq ME290R Presentation by Brian Kazian 1
Outline • • • •
Existing Fluid Simulation Techniques Fluid Equations GPU Implementation Modeling Different Types of Fluids
2
Fluids Simulation Techniques • Grid Based (Eulerian) – Stable fluids over time steps – Particle level set (combines Lagrangian & Eulerian) • Particle Based (Lagrangian) – Guarantees conservation of mass – Easier to have free surface interactions (empty space) • Height Field – FFT (Tessendorf)
3
2D Waves • Used 2D wave equation – Useful for shallow water waves – Explicit solution – First implemented in 2003
4
2D Fluid Flow • 2D Navier Stokes Solver – Implicit solutions
• Model smoke, liquid interactions
CFD Example
5
GPU Particle Systems • Model gas/liquid as interacting particles • Pros: – Computation only where necessary – Not constrained to a finite grid
• Cons: – Large number of particles needed
Particle Fluid
6
3D Fluids • Solve the 3D Navier Stokes equations • Handles free surfaces of liquids – Boundary between liquid and air
• Uses tiled 2D textures • Interaction with models
Box of Smoke
7
Uses of Fluid Simulations • Aerospace applications • Medicine – Blood flow in the body
• Computer Games / Films
8
Navier-Stokes Equations
∇⋅u=0 ut =
2 k∇ u
Incompressibility
–(u⋅∇)u – ∇p + f
Diffusion
Pressure Advection
Change in Velocity
Body Forces 9
Discretization of Equation • All values live on regular grids • Initially uses an Eulerian discretization – Frame of reference is fixed to fluid elements
• • • •
Need scalar and vector fields Scalar fields: amount of smoke or dye Vector fields: fluid velocity Subtract adjacent quantities to approximate derivatives (finite differences) 10
Navier-Stokes Equations
∇⋅u=0 ut =
2 k∇ u
Incompressibility
–(u⋅∇)u – ∇p + f
Diffusion
Pressure Advection
Change in Velocity
Body Forces 11
What is Diffusion? • Property that some fluids are “thicker” than others – Honey vs. Water
• This is viscosity – Measure of how resistive a fluid is to flowing • Diffuses the momentum
12
Scalar Field (Smoke, Dye) 1.2
3.7
5.1
…
cij
13
Diffusion
cij
14
Diffusion
1
∇2c
ct = k
change in value
value relative to neighbors
1
-4
1
1 cijnew = cij + k t (ci-1j + ci+1j + cij-1 + cij+1 - 4cij) 15
Diffusion = Blurring
Original
Some Diffusion
More Diffusion 16
Vector Field Diffusion
ut =
2 k∇ u viscosity
Two separate diffusions:
uxt = k∇2ux y 2 y u t = k∇ u … blur the x-velocity and the y-velocity 17
Effect of Viscosity Low
Medium
High
Very High
• Each one is ten times higher viscosity than the last “Melting and Flowing” Mark Carlson, Peter J. Mucha, Greg Turk Symposium on Computer Animation 2002
18
Variable Viscosity • Viscosity can vary based on position • Viscosity field k can change with temperature • Need implicit solver for high viscosity
19
Wax
20
Navier-Stokes Equations
∇⋅u=0 ut =
2 k∇ u
Incompressibility
–(u⋅∇)u – ∇p + f
Diffusion
Pressure Advection
Change in Velocity
Body Forces 21
What is Advection? • Measure of the velocity of a fluid field – Field “advects” both itself and other particles through the field
22
Advection = Pushing Stuff
23
Advection
24
Advection
0.3
0.3 2.7
25
Scalar Field Advection
ct = –(u⋅∇)c change in value
advection
current values
26
Vector Field Advection
ut = –(u⋅∇)u Two separate advections:
x u t=
x –(u⋅∇)u
uyt = –(u⋅∇)uy … push around x-velocity and y-velocity 27
Advection • Easy to code • Method stable even at large time steps • Problem: numerical inaccuracy diffuses flow
28
Navier-Stokes Equations
∇⋅u=0 ut =
2 k∇ u
Incompressibility
–(u⋅∇)u – ∇p + f
Diffusion
Pressure Advection
Change in Velocity
Body Forces 29
Divergence
High divergence
Low divergence
Zero divergence 30
Enforcing Incompressibility • • • •
First do velocity diffusion and advection Find “closest” divergence-free vector field Need to calculate divergence Need to find and use pressure
31
Measuring Divergence
∇⋅u=?
uyij+1
-uxi-1j
? uxi+1j
-uyij-1
∇⋅uij=(uxi+1j - uxi-1j) + (uyij+1-uyij-1) 32
Pressure Term new u =
u – ∇p
Take divergence of both sides…
∇⋅
new u =
∇⋅ u – ∇⋅∇p
zero
∇⋅ u = ∇2p
33
Pressure Term
∇⋅ u = known
pnew
2 ∇p
= p + (∇⋅ u -
Let dij = ∇⋅ uij
1
unknown
∇2p)
1
-4
1
1
pnewij = pij + (dij - (pi-1j + pi+1j + pij-1 + pij+1 - 4pij)) 34
Pressure Term new u =
u – ∇p
…and velocity is now divergence-free
Found “nearest” divergence-free vector field to original.
35
Fluid Simulator 1) 2) 3) 4) 5) 6)
Diffuse velocity Advect velocity Add body forces (e.g. gravity, obstacles) Pressure projection Diffuse dye/smoke Advect dye/smoke
36
GPU Implementation for Fluids • Cell attributes stored in 3D textures • Run kernels (fragment shaders) over grid – Slices
• Split N-S equation into 3 parts – Advection – External Forces – Pressure Projection
• No diffusion 37
Advection Implementation • Semi-Lagrangian vs. MacCormack scheme for advection steps • Lagrangian is unconditionally stable – Can introduce numerical smoothing • MacCormack uses 2 intermediate Lagrangian steps – Not unconditionally stable • Higher order scheme is better on the GPU • Math is cheaper than bandwidth
MacCormack 128x64x64
Semi-Lagrangian 256x128x128
38
Fluid – Object Interaction • Fluid interaction with solid objects? – Model a solid object and allow for interaction – Modeling dynamic objects • Volumetric representation of object • Velocity at boundary
Movie
39
Dynamic Obstacles • Fluid behavior at dynamic boundary – Handle voxel/fluid faces which serve as boundaries
• Create a no-slip boundary condition at faces – Fluid can move freely along the surface – Same velocities normal to voxels
40
Solving Pressure with Objects • Object pressure cells – Replace value with current cell’s value
• Object velocity cells – Replace fluid velocity with object velocity
41
Velocity Correction at Boundaries • Need to enforce no-slip boundary condition • Compute normal velocity of object – Replace normal velocity of fluid with value
• For two opposing faces, select one • Large time steps, fluid “leaks” into solid – Add advection constraint to prevent this
42
Voxelization • How to determine whether a cell contains a solid object or not – Voxelize solid objects into 2 textures
43
Inside – Outside Voxelization • Use a stencil buffer and clipping planes – Increment back faces, decrement front
• Voxels inside have nonzero values • Can distinguish types of cells • Need water-tight mesh
44
Velocity Voxelization • Need velocity at each grid cell – Find velocity at each vertex • Store previous and current vertex position
• Need to interpolate velocities – Use stencil buffer procedure – Have intersection with triangles • Segment, triangle, point, or empty
45
Velocity Voxelization • Segment intersections – Draw thicker segment with a quad • Offset width = diagonal length of a texel • Offset direction = projected normal of triangle
– Linearly interpolate velocities • Ultimately assign to grid points
46
Velocity Texture
» Bright blue – next to boundary » Dark blue – outside boundary » Red/Green – velocity components
47
Optimizing Voxelization • Optimize with stream output – Can cache transformed vertices • Don’t have to recompute transformations at each slice
• Instancing to voxelize all slices in one call – Instance ID used to specify target slice
• Use low level detail mesh for obstacle – Simulation grid is coarse
48
Smoke • Vector field is boring by itself – Inject some smoke! – Need to track density & temperature
• Keep track of temperature for buoyant force • Inject 3-D Gaussian splat for smoke
49
Fire • Similar to smoke, but also need a reaction coordinate – Reaction coordinated advected through flow
50
Water • Mainly worry about the surface interactions • Level set method used – Need scalar value at each grid cell – Cells record shortest signed distance to surface – Don’t need simulation outside of fluid surface • Mask computation if level set is above 0
51
Performance Considerations • Many arithmetic kernels for each frame – Lots of transfer to & from texture memory – Little discernable difference in 16 & 32 bit textures • Arithmetic operations still in 32 bit
• No benefit from packing data into textures – No interleaving of data accesses
52
Numerical Issues • Solving pressure-Poisson system is expensive – Solving exactly enforces incompressibility and thus fluid volume – Use other solvers rather than Jacobi – For smoke/fire, not concerned with losing volume
53
Numerical Issues • Water is a different matter – Need information to travel • Can’t solve for pressure exactly in real-time simulation
– Without it, fluid disappears
• Defines surface after extended period – A is advection operator – Beta controls the damping • Lessens number of solver iterations needed
54
Volume Rendering • Have 3D texture, but no built-in display call – Use ray-marching pixel shader
• Render six simulation volume quad faces – Shoot rays through to gather densities of fluid
55
Volume Ray Casting • Need to know information about rays – Where it enters, direction, etc.
• Create a RayData texture with this information – Store depth of faces in alpha channel – RGB channel stores fragment coords. of front
56
Rendering Volume • Shader uses RayData for entry & distance – Number of samples determined by marching dist. • Each step, sample values and blend front to back • Can terminate early if color saturates
57
Limitations - Occlusion • Rays do not stop for objects inside fluid – Use minimum of back-face & scene distances
• Objects may occlude part of fluid – Compare front-face & scene distances • If front-face distance larger, use negative value in RayData
58
Limitations - Clipping • Camera may be inside fluid volume – No information about where rays enter volume – Incorrect volume depth values
• Mark pixels where no front face rendered – Use near plane instead
59
Limitations - Banding • Integer number of equally spaced samples – Take one extra sample – Weigh against distance and sample width
• Other solutions – Increasing sampling frequency – Jittering samples – Higher-order filters
60
Off Screen Ray Marching • Low resolution simulation grid w.r.t. screen resolution – Render fluid in smaller off-screen target – Composite into final scene – Does not work well for discontinuities in model
• Run edge detector to reflect problem geometry – Render in higher resolution at these points
61
Rendering Fire • Similar to rendering smoke – Accumulate values according to reaction coordinate – Map these coordinates to 1D fire texture
• Lighting effects – Sample at several locations, treat as light source • Flickering, inconsistent behavior
– Downsample reaction coords to low resolution 62
Render Liquids • March and look at values from level set – Find first value along ray where f=0
• Refraction needed for water surface – Render rear objects into background texture
• Determine ray intersections with water surface – Hit points shaded with refraction shader
• Background pixel used offset – Offset proportional to surface normal 63
Conclusions • Simulation of fluids has many application • Rendering fluids on GPU is attractive – Cheap mathematics, grid-based
• Advanced hardware leads to better simulations
64