28
CUDA Particle-based Fluid Simulation Simon Green

CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

Embed Size (px)

Citation preview

Page 1: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

CUDA Particle-based Fluid Simulation

Simon Green

Page 2: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Overview

Fluid Simulation TechniquesCUDA particle simulationSpatial subdivision techniquesRendering methodsFuture

Page 3: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Fluid Simulation Techniques

Various approaches:

Grid based (Eulerian)Stable fluidsParticle level set

Particle based (Lagrangian)SPH (smoothed particle hydrodynamics)MPS (Moving-Particle Semi-Implicit)

Height fieldFFT (Tessendorf)Wave propagation – e.g. Kass and Miller

Page 4: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

CUDA N-Body Demo

Computes gravitational attraction between n bodiesComputes all n2 interactionsUses shared memory to reduce memory bandwidth

16K bodies @ 44 FPSx 20 FLOPS / interactionx 16K2 interactions /

frame= 240 GFLOP/s

GeForce 8800 GTX

Page 5: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Particle-based Fluid Simulation

AdvantagesConservation of mass is trivialEasy to track free surfaceOnly performs computation where necessaryNot necessarily constrained to a finite gridEasy to parallelize

DisadvantagesHard to extract smooth surface from particlesRequires large number of particles for realistic results

Page 6: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Particle Fluid Simulation Papers

Particle-Based FluidSimulation for InteractiveApplications,M. Müller, 20033000 particles, 5fps

Particle-based Viscoelastic Fluid Simulation,Clavet et al, 20051000 particles, 10fps20,000 particles,2 secs / frame

Page 7: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

CUDA SDK Particles Demo

Particles with simplecollisionsUses uniform gridbased on sortingUses fast CUDA radixsort

Current performance:>100 fps for 65Kinteracting particleson 8800 GT

Page 8: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Uniform Grid

Particle interaction requires finding neighbouring particlesExhaustive search requires n^2 comparisonsSolution: use spatial subdivision structureUniform grid is simplest possible subdivision

Divide world into cubical grid (cell size = particle size)Put particles in cellsOnly have to compare each particle with the particles in neighbouring cells

Building data structures is hard on data parallelmachines like the GPU

possible in OpenGL (using stencil routing technique)easier using CUDA (fast sorting, scattered writes)

Page 9: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Grid is built from scratch each frameFuture work: incremental updates?

Algorithm:Compute which grid cell each particle falls in (based on center)Calculate cell indexSort particles based on cell indexFind start of each bucket in sorted list (store in array)Process collisions by looking at 3x3x3 = 27 neighbouring grid cells of each particle

Advantagessupports unlimited number of particles per grid cellSorting improves memory coherence during collisions

Uniform Grid using Sorting

Page 10: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

0

Example: Grid using Sorting

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

3

2

0

1

45

unsorted list(cell id, particle id)

sorted bycell id

cell start

0: (9, 0)1: (6, 1)2: (6, 2)3: (4, 3)4: (6, 4)5: (4, 5)

0: (4, 3) 1: (4, 5)2: (6, 1)3: (6, 2)4: (6, 4)5: (9, 0)

0: -1: -2: -3: -4: 05: -6: 27: -8: -9: 510: -...15: -

Page 11: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Fluid Rendering Methods

3D isosurface extraction (marching cubes)2.5D isosurfaces (Ageia screen-space meshes)3D texture ray marching (expensive)Image-space tricks (blur normals in screen space)

Page 12: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Marching Cubes

Popular method for extracting isosurfaces from volume data

Lorensen and Cline (Siggraph 1987!)Polygonizes a scalar fieldIsosurface is surface where field == n

Divides volume in cubical voxelsOutputs triangles based on field values at cornersInterpolates points along edges based onfield valuesBased on look-up tables

Image courtesy Wikipedia

Page 13: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Isosurface Extraction on the GPU

Difficult on GPUs because of variable output0-5 triangles per voxel

Implementations on previous hardware generations performed a lot of redundant computationsPossible on DirectX 10 class hardware using geometry shader

Marching tetrahedrons matches hardware best (4 inputs)Can we also do this in CUDA?

Yes, using prefix sums (scan) for stream compactionUses CUDPP library (Harris et al)

Page 14: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

CUDA Marching Cubes

Algorithm consists of several stagestables are stored in 1D textures

Execute classifyVoxel kernelcomputes number of vertices voxel will generateevaluates field at each corners of each voxelone thread per voxelwrites voxelOccupied flag and voxelVertices to global memory

Scan voxelVertices arraygives start address for vertex data for each voxel

Read back total number of vertices from GPU to CPUlast element in scanned array

Page 15: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

CUDA Marching Cubes (cont.)

Scan voxelOccupied arrayRead back total number of occupied voxels from GPU to CPUCompact voxelOccupied array to get rid of empty voxelsExecute generateTriangles kernel

runs only on occupied voxelslooks up field values againgenerates triangles, using results of scan to write output to correct addresses

Render geometryusing number of vertices from readback

Page 16: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Marching Cubes Performance

Up to 8x faster than OpenGLgeometry shader implementationusing marching tetrahedraBut still requires evaluating fieldfunction at every point in space

E.g. 1283 = 2M pointsVery expensive

Page 17: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Density-based Shading

Can calculate per-particle density and normal based on field function

SPH simulations often already have this dataUsually need to look at a larger neighbourhood (e.g. 5x5x5 cells) to get good results – expensive

Can use density and normal for point sprite shadingNormal only well defined when particles are close to each other

treat isolated particles separately – e.g. render as spray

Page 18: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Particle Density

Page 19: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Particle Normal

Page 20: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Flat Shaded Point Sprites

Page 21: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Blended Points Sprites (Splats)

Scale up point sizeso they overlapAdd alpha to pointswith Gaussian falloffRequires sortingfrom back to frontHas effect ofinterpolating shadingbetween pointsFill-rate intensive,but interactive

Page 22: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Alternative Shading (Lava)

Modifies particlecolor based ondensity

Page 23: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Motion Blur

Create quads between previous and current particle position

Using geometry shaderTry and orient quad towards view directionImproves look of rapidly moving fluids (eliminates gaps between particles)

p

p2

Page 24: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Spheres

Page 25: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Motion Blurred Spheres

Page 26: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Oriented Discs

Page 27: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

The Future

Practical interactive fluids will need to combine particle, height field, and grid techniquesGPU performance continues to double every12 months – lots of room for improvement!

Two way coupled SPH and particle level set fluid simulation, Losasso, F., Talton, J., Kwatra, N. and Fedkiw, R

Adaptively Sampled Particle Fluids, Adams 2007

Page 28: CUDA Particle-based Fluid Simulation - TU Dortmundgoeddeke/arcs2008/D2_Fluids.pdf · Particle-based Fluid Simulation Advantages ... Usually need to look at a larger neighbourhood

© NVIDIA Corporation 2008

Questions?