gpu rigid body simulation
play

GPU Rigid Body Simulation Erwin Coumans Principal Engineer @ - PowerPoint PPT Presentation

GPU Rigid Body Simulation Erwin Coumans Principal Engineer @ http://bulletphysics.org Erwin Coumans Leading the Bullet Physics SDK project http://bulletphysics.org Doing GPGPU physics R&D at AMD, open source at


  1. GPU Rigid Body Simulation Erwin Coumans Principal Engineer @ http://bulletphysics.org

  2. Erwin Coumans • Leading the Bullet Physics SDK project http://bulletphysics.org • Doing GPGPU physics R&D at AMD, open source at http://github.com/erwincoumans/experiments • Previously at Sony SCEA US R&D and Havok

  3. GPU Cloth (2009)

  4. GPU Hair (2012/2013)

  5. GPU Rigid Body (2008-2013)

  6. Rigid Bodies • Position (Center of mass, float3) • Orientation (Inertia basis frame, float4)

  7. Updating the transform • Linear velocity (float3) • Angular velocity (float3)

  8. Update Position in C/C++ void integrateTransformsKernel(Body* bodies, int nodeID, float timeStep) { if( bodies[nodeID].m_invMass != 0.f) { bodies[nodeID].m_pos += bodies[nodeID].m_linVel * timeStep; //linear velocity } }

  9. Update Position in OpenCL ™ __kernel void integrateTransformsKernel( __global Body* bodies,const int numNodes, float timeStep) { int nodeID = get_global_id(0); if( nodeID < numNodes && (bodies[nodeID].m_invMass != 0.f)) { bodies[nodeID].m_pos += bodies[nodeID].m_linVel * timeStep; //linear velocity } } See opencl/gpu_rigidbody/kernels/integrateKernel.cl

  10. Apply Gravity __kernel void integrateTransformsKernel( __global Body* bodies,const int numNodes, float timeStep, float angularDamping, float4 gravityAcceleration) { int nodeID = get_global_id(0); if( nodeID < numNodes && (bodies[nodeID].m_invMass != 0.f)) { bodies[nodeID].m_pos += bodies[nodeID].m_linVel * timeStep; //linear velocity bodies[nodeID].m_linVel += gravityAcceleration * timeStep; //apply gravity } } See opencl/gpu_rigidbody/kernels/integrateKernel.cl

  11. Update Orientation __kernel void integrateTransformsKernel( __global Body* bodies,const int numNodes, float timeStep, float angularDamping, float4 gravityAcceleration) { int nodeID = get_global_id(0); if( nodeID < numNodes && (bodies[nodeID].m_invMass != 0.f)) { bodies[nodeID].m_pos += bodies[nodeID].m_linVel * timeStep; //linear velocity bodies[nodeID].m_linVel += gravityAcceleration * timeStep; //apply gravity float4 angvel = bodies[nodeID].m_angVel; //angular velocity bodies[nodeID].m_angVel *= angularDamping; //add some angular damping float4 axis; float fAngle = native_sqrt(dot(angvel, angvel)); if(fAngle*timeStep> BT_GPU_ANGULAR_MOTION_THRESHOLD) //limit the angular motion fAngle = BT_GPU_ANGULAR_MOTION_THRESHOLD / timeStep; if(fAngle < 0.001f) axis = angvel * (0.5f*timeStep-(timeStep*timeStep*timeStep)*0.020833333333f * fAngle * fAngle); else axis = angvel * ( native_sin(0.5f * fAngle * timeStep) / fAngle); float4 dorn = axis; dorn.w = native_cos(fAngle * timeStep * 0.5f); float4 orn0 = bodies[nodeID].m_quat; float4 predictedOrn = quatMult(dorn, orn0); predictedOrn = quatNorm(predictedOrn); bodies[nodeID].m_quat=predictedOrn; //update the orientation } } See opencl/gpu_rigidbody/kernels/integrateKernel.cl

  12. Update Transforms, Host Setup ciErrNum = clSetKernelArg(g_integrateTransformsKernel, 0, sizeof(cl_mem), &bodies); ciErrNum = clSetKernelArg(g_integrateTransformsKernel, 1, sizeof(int), &numBodies); ciErrNum = clSetKernelArg(g_integrateTransformsKernel, 1, sizeof(float), &deltaTime); ciErrNum = clSetKernelArg(g_integrateTransformsKernel, 1, sizeof(float), &angularDamping); ciErrNum = clSetKernelArg(g_integrateTransformsKernel, 1, sizeof(float4), &gravityAcceleration); size_t workGroupSize = 64; size_t numWorkItems = workGroupSize*((m_numPhysicsInstances + (workGroupSize)) / workGroupSize); if (workGroupSize>numWorkItems) workGroupSize=numWorkItems; ciErrNum = clEnqueueNDRangeKernel(g_cqCommandQue, g_integrateTransformsKernel, 1, NULL, &numWorkItems, &workGroupSize,0 ,0 ,0);

  13. Physics pipeline Collision Data Dynamics Data World Constraints Collision Object Overlapping Contact Mass transforms (contacts, shapes AABBs pairs points Inertia velocities joints) time Start End Compute Apply Predict Compute Detect Setup Solve Integrate contact gravity transforms AABBs pairs constraints constraints position points Forward Dynamics Collision Detection Forward Dynamics Computation Computation Computation

  14. All 50 OpenCL ™ kernels AddOffsetKernel AverageVelocitiesKern BatchSolveKernelCont BatchSolveKernelFricti ClearVelocitiesKernel ContactToConstraintK el act on ernel ContactToConstraintS CopyConstraintKernel CountBodiesKernel CreateBatches CreateBatchesNew FillFloatKernel plitKernel FillInt2Kernel FillIntKernel FillUnsignedIntKernel LocalScanKernel PrefixScanKernel ReorderContactKernel SearchSortDataLower SearchSortDataUpper SetSortDataKernel SolveContactJacobiKer SolveFrictionJacobiKer SortAndScatterKernel Kernel Kernel nel nel SortAndScatterSortDa StreamCountKernel StreamCountSortData SubtractKernel TopLevelScanKernel UpdateBodyVelocities taKernel Kernel Kernel bvhTraversalKernel clipCompoundsHullHu clipFacesAndContactR clipHullHullConcaveCo clipHullHullKernel computePairsKernel llKernel eductionKernel nvexKernel computePairsKernelT copyAabbsKernel copyTransformsToVB extractManifoldAndA findClippingFacesKern findCompoundPairsKe woArrays OKernel ddContactKernel el rnel findConcaveSeparatin findSeparatingAxisKer flipFloatKernel initializeGpuAabbsFull integrateTransformsK newContactReduction gAxisKernel nel ernel Kernel processCompoundPair scatterKernel sKernel

  15. Host and Device Host Device (GPU) CPU L2 cache Global Host Memory Global Device Memory

  16. GPU in a nutshell Private Memory (registers) Shared Local Memory Compute Unit Shared Local Memory Shared Local Memory Global Device Memory

  17. Windows GPU and CPU OpenCL Devices • Support for AMD Radeon, NVIDIA and Intel HD4000

  18. Apple Mac OSX OpenCL Devices

  19. Other GPGPU Devices • Nexus 4 and 10 with ARM OpenCL SDK • Apple iPad has a private OpenCL framework • Sony Playstation 4 and other future game consoles

  20. 1 st GPU rigid body pipeline (~2008-2010) Compute Detect Contact Setup Contact Solve contact pairs constraints constraints points B D 0 1 2 3 1 4 F C E A 5 7 B D A B C D A 8 10 11 1 1 3 3 4 2 2 4 12 13 14 15 Uniform grid Spherical Voxelization CPU batch and GPU solve (dispatched from CPU)

  21. Uniform Grid 0 1 2 3 F C E 5 7 B D A 8 10 11 12 13 14 15 • Particle is also its own bounding volume (sphere) • Each particle computes its cell index (hash) • Each particle iterates over its own cell and neighborns

  22. Uniform Grid and Parallel Primitives • Radix Sort the particles based on their cell index • Use a prefix scan to compute the cell size and offset • Fast OpenCL and DirectX11 Direct Compute implementation

  23. Contact Generation

  24. Constraint Generation B D 1 4 A

  25. Reordering Constraints B D 1 4 A A B C D 1 1 A B C D 2 2 Batch 0 1 1 3 3 3 3 4 4 Batch 1 4 2 2 4 • Also known as Graph Coloring or Batching

  26. CPU sequential batch creation while( nIdxSrc ) { nIdxDst = 0; int nCurrentBatch = 0; for(int i=0; i<N_FLG/32; i++) flg[i] = 0; //clear flag for(int i=0; i<nIdxSrc; i++) { int idx = idxSrc[i]; btAssert( idx < n ); //check if it can go int aIdx = cs[idx].m_bodyAPtr & FLG_MASK; int bIdx = cs[idx].m_bodyBPtr & FLG_MASK; u32 aUnavailable = flg[ aIdx/32 ] & (1<<(aIdx&31));u32 bUnavailable = flg[ bIdx/32 ] & (1<<(bIdx&31)); if( aUnavailable==0 && bUnavailable==0 ) { flg[ aIdx/32 ] |= (1<<(aIdx&31)); flg[ bIdx/32 ] |= (1<<(bIdx&31)); cs[idx].getBatchIdx() = batchIdx; sortData[idx].m_key = batchIdx; sortData[idx].m_value = idx; nCurrentBatch++; if( nCurrentBatch == simdWidth ) { nCurrentBatch = 0; for(int i=0; i<N_FLG/32; i++) flg[i] = 0; } } else { idxDst[nIdxDst++] = idx; } } swap2( idxSrc, idxDst ); swap2( nIdxSrc, nIdxDst ); batchIdx ++; }

  27. Naïve GPU batch creation • Use a single Compute Unit • All threads in the Compute Unit synchronize the locking of bodies using atomics and barriers • Didn’t scale well for larger scale simulations (>~30k)

  28. 2 nd GPU rigid body pipeline (~2010-2011) Compute Detect Setup Solve contact pairs constraints constraints points B D 1 4 A Mixed GPU/CPU Dual Surface/ Dual Grid/ broadphase and Heightfield GPU batching & dispatch 1-axis parallel gSAP

  29. Axis aligned bounding boxes (AABBs) X min X max MIN (X,Y,Z) Y min Y max Z min Z max * Object ID MAX (X,Y,Z)

  30. Axis aligned bounding box

  31. Support mapping    ( ) max{ : } S v v x x C c

  32. Support map for primitives h y • Box with half extents h h  ( ) ( ( ) ( ) ( ) ) x S v sign v h sign v h sign v h , , x x y y z z box r r  ( ) S v v sphere | | v • Sphere with radius r

  33. Support map for convex polyhedra    ( ) max{ : } S v v x x C c • Brute force uniform operations (dot/max) on vertices are suitable for GPU – Outperforms Dobkin Kirkpatrick hierarchical optimization in practice • Fast approximation using precomputed support cube map

  34. Worldspace AABB from Localspace AABB  Affine transform   t ( ) ( ( )) S v B S B v c  Bx c  Fast approximation using precomputed local aabb  See opencl/gpu_rigidbody/kernels/updateAabbsKernel.cl

Recommend


More recommend