Ph i l Si l ti GPUPhysical Simulation on...

27
Ph i l Si l ti GPU Ph i l Si l ti GPU Physical Simulation on GPUs Physical Simulation on GPUs Ji V V th Jim V an V erth OpenGL Software Engineer NVIDIA NVIDIA [email protected] www.nvidia.com www.nvidia.com www.essentialmath.com

Transcript of Ph i l Si l ti GPUPhysical Simulation on...

Page 1: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

Ph i l Si l ti GPUPh i l Si l ti GPUPhysical Simulation on GPUsPhysical Simulation on GPUs

Ji V V thJim Van Verth

OpenGL Software EngineerNVIDIANVIDIA

[email protected]

www.essentialmath.com

Page 2: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

Ph i GPUPhysics on GPU

Topics of discussionWays of parallelizing physics Examples of GPU physics CUDA and you

Page 3: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

P ll li i Ph iParallelizing Physics

What we’ve talked about so far

CPU DisplayGPU

Game Logic,AI, Physics

Graphics

Awfully busy… improve performance?

Page 4: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

P ll li i Ph iParallelizing Physics

Solution 1: Multicore CPU

CPU DisplayGPU

Game Logic,AI

GraphicsPhysics

Page 5: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

P ll li i Ph iParallelizing Physics

Solution 2a: Cell processor

CPU DisplayGame Logic,AI GPU

Graphics

SPU SPU SPU

Ph sics

SPU SPU SPU

SPU SPU SPUPhysics

Page 6: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

P ll li i Ph iParallelizing Physics

Solution 2b: AGEIA processor

CPU DisplayGame Logic,AI GPU

Graphics

Physics

PPU

Physics

Page 7: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

P ll li i Ph iParallelizing Physics

Solution 3: Programmable GPU

CPU DisplayGPU

Game Logic,AI

Graphics,Physics

Page 8: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

P ll li i Ph iParallelizing Physics

Solution 3b: SLI

G

CPUDisplay

GPU

Graphics

Game Logic,AI

GPUAI

Physics

Page 9: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

GPU C tiGPU Computing

Modern GPU has many independent processors:

GeForce 8800 GTX: 128 SPsGeForce 8800 GT: 112 SPs

Mostly processing power, not cache:GeForce 8800 GTX: 300-400 GflopsG F 8800 GT 500 GflGeForce 8800 GT: 500 Gflops

A lot of parallel power for physics!

Page 10: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

GPU Ph i E lGPU Physics Example

From GPU Gems 3Takahiro Harada, “Real-time Rigid Body g ySimulation on GPUs”Simple physics engine, all running on GPU

Page 11: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

GPU Ph i E lGPU Physics Example

Idea: GPU is good at:Many similar computationsSimple data

So:Particles for collision representationGrid for collision detectionSi l lli iSimple collision response

Page 12: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

Obj t R t tiObject Representation

Global object data in texture pairs

Position Orientation LinearMomentum

AngularMomentum

Alternate frame to frame

Page 13: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

Obj t R t tiObject Representation

Collision rep: Solid (or shell) of particles

StStore asFixed radiusDisplacement from center of mass

Page 14: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

Obj t R t tiObject Representation

Smaller particles == better fitBut more processingp g

Page 15: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

Obj t R t tiObject Representation

Particle data stored in texture and three rendertargets

Update position velocity each frame from global

Displacement Position Velocity Force

Update position, velocity each frame from global object dataUpdate force from collisions

Page 16: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

Pi liPipelineU d t P ti lUpdate Particles

Calculate Grid

ComputeCollisions

IntegrateIntegrate

Page 17: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

U d t P ti lUpdate ParticlesUpdate

For each object do:Iterate through all particles

Update Particles

Update particle position, velocity Calculate Grid

CCompute CollisionsPosition Orientation Particle

Position

IntegrateLinear

MomentumAngular

Momentum

Displacement

ParticleParticleVelocity

Page 18: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

G id R t tiGrid RepresentationUpdate

Stored as slabs within 2D rendertargetUpdate

Particles

Calculate Grid

CCompute Collisions

Voxel stored as texelFour particle indices per texel

Integrate

p p

Page 19: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

G id C tiGrid CreationUpdate

For each particle doCompute grid index

Update Particles

Write particle index to appropriate component at that location

Calculate Grid

CCompute Collisions

Integrate

Page 20: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

C lli i R l tiCollision ResolutionUpdate

For each voxel doFor each particle in voxel do

Update Particles

Compute force based on particles in this and 27 neighboring voxelsRegardless of collision!

Calculate Grid

Cg

Spring forceDamping from relative vel.Tangential force

Compute Collisions

g

Integrate

Page 21: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

I t tiIntegrationUpdate

Compute new linear and angular momenta based on collision (and other) forces

Update Particles

other) forcesForce/torque on rigid body is weighted sum of forces from each particle

Calculate Grid

CCompute new position and orientation from momenta

Compute Collisions

Integrate

Page 22: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

DDemo

Page 23: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

Oth hOther approaches

Simon Green’s particlesNyland, Harris and Prins: N-body sim.y yParallelize one piece:

Ex. Broad Phase (from GPU Gems 3)Do smaller problem

Ex. Fluid dynamics (Hellfire: London)

Page 24: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

GPU C tiGPU Computing

How to program?In the past had to use Cg, GLSL, HLSLp gProblems:

Requires specialized shader knowledgeData is often texture or rendertargetCan’t “scatter” data easily

Page 25: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

CUDACUDA

Solution is CUDAStands for Compute Unified Device A hit tArchitectureExtensions on C/C++I t bl ith D3D d O GLInteroperable with D3D and OpenGLwww.nvidia.com/cudaUse it!Use it!

Page 26: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

CUDACUDA

Updating our example:Instead of Cg, use standard C++ w/CUDA extensionsInstead of textures or rendertargets just use CUDAInstead of textures or rendertargets, just use CUDA arraysInstead of vertex shader, use scatter operation

Page 27: Ph i l Si l ti GPUPhysical Simulation on GPUsdeveloper.download.nvidia.com/.../2008/GDC/GDC08_GPUPhysics.pdf · Ph i l Si l ti GPUPhysical Simulation on GPUs ... Physics PPU. Plllii

R fReferences

Takahiro Harada. “Real-Time Rigid Body Simulation on GPUs.” In GPU Gems 3, Hubert Nguyen, ed., Addison-Wesley, 2007.

Lars Nyland, Mark Harris, Jan Prins. “Fast N-Body Simulation with CUDA ” In GPU Gems 3 Hubert Nguyen ed Addison WesleyCUDA. In GPU Gems 3, Hubert Nguyen, ed., Addison-Wesley, 2007.

Scott Le Grand. “Broad-Phase Collision Detection with CUDA.” In GPU Gems 3, Hubert Nguyen, ed., Addison-Wesley, 2007.

Keenan Crane, Ignacio Llamas, Sarah Tariq. “Real-Time Simulation and Rendering of 3D Fluids.” In GPU Gems 3, Hubert Nguyen, ed., Addison-Wesley, 2007.

Simon Green. “CUDA Particles” NVIDIA whitepaper, November,Simon Green. CUDA Particles NVIDIA whitepaper, November, 2007.

NVIDIA CUDA Compute Unified Device Architecture Programming Guide Version 1.1, November 2007.