2 Bullet Continuous Collision Detection and Physics Library
\r
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
\r
5 This software is provided 'as-is', without any express or implied warranty.
\r
6 In no event will the authors be held liable for any damages arising from the use of this software.
\r
7 Permission is granted to anyone to use this software for any purpose,
\r
8 including commercial applications, and to alter it and redistribute it freely,
\r
9 subject to the following restrictions:
\r
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
\r
13 3. This notice may not be removed or altered from any source distribution.
\r
16 #ifndef BT_SOFT_BODY_SOLVER_OPENCL_H
\r
17 #define BT_SOFT_BODY_SOLVER_OPENCL_H
\r
19 #include "stddef.h" //for size_t
\r
20 #include "vectormath/vmInclude.h"
\r
22 #include "BulletSoftBody/btSoftBodySolvers.h"
\r
23 #include "BulletSoftBody/btSoftBody.h"
\r
24 #include "btSoftBodySolverBuffer_OpenCL.h"
\r
25 #include "btSoftBodySolverLinkData_OpenCL.h"
\r
26 #include "btSoftBodySolverVertexData_OpenCL.h"
\r
27 #include "btSoftBodySolverTriangleData_OpenCL.h"
\r
32 cl_command_queue m_cqCommandQue;
\r
33 cl_context m_cxMainContext;
\r
35 int m_kernelCompilationFailures;
\r
39 CLFunctions(cl_command_queue cqCommandQue, cl_context cxMainContext) :
\r
40 m_cqCommandQue( cqCommandQue ),
\r
41 m_cxMainContext( cxMainContext ),
\r
42 m_kernelCompilationFailures(0)
\r
46 int getKernelCompilationFailures() const
\r
48 return m_kernelCompilationFailures;
\r
52 * Compile a compute shader kernel from a string and return the appropriate cl_kernel object.
\r
54 virtual cl_kernel compileCLKernelFromString( const char* kernelSource, const char* kernelName, const char* additionalMacros, const char* srcFileNameForCaching);
\r
56 void clearKernelCompilationFailures()
\r
58 m_kernelCompilationFailures=0;
\r
63 * Entry in the collision shape array.
\r
64 * Specifies the shape type, the transform matrix and the necessary details of the collisionShape.
\r
66 struct CollisionShapeDescription
\r
68 Vectormath::Aos::Transform3 shapeTransform;
\r
69 Vectormath::Aos::Vector3 linearVelocity;
\r
70 Vectormath::Aos::Vector3 angularVelocity;
\r
72 int softBodyIdentifier;
\r
73 int collisionShapeType;
\r
75 // Both needed for capsule
\r
83 CollisionShapeDescription()
\r
85 collisionShapeType = 0;
\r
92 * SoftBody class to maintain information about a soft body instance
\r
94 * This data addresses the main solver arrays.
\r
96 class btOpenCLAcceleratedSoftBodyInterface
\r
99 /** Current number of vertices that are part of this cloth */
\r
101 /** Maximum number of vertices allocated to be part of this cloth */
\r
103 /** Current number of triangles that are part of this cloth */
\r
104 int m_numTriangles;
\r
105 /** Maximum number of triangles allocated to be part of this cloth */
\r
106 int m_maxTriangles;
\r
107 /** Index of first vertex in the world allocated to this cloth */
\r
109 /** Index of first triangle in the world allocated to this cloth */
\r
110 int m_firstTriangle;
\r
111 /** Index of first link in the world allocated to this cloth */
\r
113 /** Maximum number of links allocated to this cloth */
\r
115 /** Current number of links allocated to this cloth */
\r
118 /** The actual soft body this data represents */
\r
119 btSoftBody *m_softBody;
\r
123 btOpenCLAcceleratedSoftBodyInterface( btSoftBody *softBody ) :
\r
124 m_softBody( softBody )
\r
128 m_numTriangles = 0;
\r
129 m_maxTriangles = 0;
\r
131 m_firstTriangle = 0;
\r
136 int getNumVertices()
\r
138 return m_numVertices;
\r
141 int getNumTriangles()
\r
143 return m_numTriangles;
\r
146 int getMaxVertices()
\r
148 return m_maxVertices;
\r
151 int getMaxTriangles()
\r
153 return m_maxTriangles;
\r
156 int getFirstVertex()
\r
158 return m_firstVertex;
\r
161 int getFirstTriangle()
\r
163 return m_firstTriangle;
\r
167 * Update the bounds in the btSoftBody object
\r
169 void updateBounds( const btVector3 &lowerBound, const btVector3 &upperBound );
\r
171 // TODO: All of these set functions will have to do checks and
\r
172 // update the world because restructuring of the arrays will be necessary
\r
173 // Reasonable use of "friend"?
\r
174 void setNumVertices( int numVertices )
\r
176 m_numVertices = numVertices;
\r
179 void setNumTriangles( int numTriangles )
\r
181 m_numTriangles = numTriangles;
\r
184 void setMaxVertices( int maxVertices )
\r
186 m_maxVertices = maxVertices;
\r
189 void setMaxTriangles( int maxTriangles )
\r
191 m_maxTriangles = maxTriangles;
\r
194 void setFirstVertex( int firstVertex )
\r
196 m_firstVertex = firstVertex;
\r
199 void setFirstTriangle( int firstTriangle )
\r
201 m_firstTriangle = firstTriangle;
\r
204 void setMaxLinks( int maxLinks )
\r
206 m_maxLinks = maxLinks;
\r
209 void setNumLinks( int numLinks )
\r
211 m_numLinks = numLinks;
\r
214 void setFirstLink( int firstLink )
\r
216 m_firstLink = firstLink;
\r
231 return m_firstLink;
\r
234 btSoftBody* getSoftBody()
\r
243 class btOpenCLSoftBodySolver : public btSoftBodySolver
\r
258 UIntVector3( unsigned int x_, unsigned int y_, unsigned int z_ )
\r
269 unsigned int _padding;
\r
272 struct CollisionObjectIndices
\r
274 CollisionObjectIndices( int f, int e )
\r
284 btSoftBodyLinkDataOpenCL m_linkData;
\r
285 btSoftBodyVertexDataOpenCL m_vertexData;
\r
286 btSoftBodyTriangleDataOpenCL m_triangleData;
\r
290 CLFunctions m_defaultCLFunctions;
\r
291 CLFunctions* m_currentCLFunctions;
\r
293 /** Variable to define whether we need to update solver constants on the next iteration */
\r
294 bool m_updateSolverConstants;
\r
296 bool m_shadersInitialized;
\r
299 * Cloths owned by this solver.
\r
300 * Only our cloths are in this array.
\r
302 btAlignedObjectArray< btOpenCLAcceleratedSoftBodyInterface * > m_softBodySet;
\r
304 /** Acceleration value to be applied to all non-static vertices in the solver.
\r
305 * Index n is cloth n, array sized by number of cloths in the world not the solver.
\r
307 btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothAcceleration;
\r
308 btOpenCLBuffer<Vectormath::Aos::Vector3> m_clPerClothAcceleration;
\r
310 /** Wind velocity to be applied normal to all non-static vertices in the solver.
\r
311 * Index n is cloth n, array sized by number of cloths in the world not the solver.
\r
313 btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothWindVelocity;
\r
314 btOpenCLBuffer<Vectormath::Aos::Vector3> m_clPerClothWindVelocity;
\r
316 /** Velocity damping factor */
\r
317 btAlignedObjectArray< float > m_perClothDampingFactor;
\r
318 btOpenCLBuffer<float> m_clPerClothDampingFactor;
\r
320 /** Velocity correction coefficient */
\r
321 btAlignedObjectArray< float > m_perClothVelocityCorrectionCoefficient;
\r
322 btOpenCLBuffer<float> m_clPerClothVelocityCorrectionCoefficient;
\r
324 /** Lift parameter for wind effect on cloth. */
\r
325 btAlignedObjectArray< float > m_perClothLiftFactor;
\r
326 btOpenCLBuffer<float> m_clPerClothLiftFactor;
\r
328 /** Drag parameter for wind effect on cloth. */
\r
329 btAlignedObjectArray< float > m_perClothDragFactor;
\r
330 btOpenCLBuffer<float> m_clPerClothDragFactor;
\r
332 /** Density of the medium in which each cloth sits */
\r
333 btAlignedObjectArray< float > m_perClothMediumDensity;
\r
334 btOpenCLBuffer<float> m_clPerClothMediumDensity;
\r
337 * Collision shape details: pair of index of first collision shape for the cloth and number of collision objects.
\r
339 btAlignedObjectArray< CollisionObjectIndices > m_perClothCollisionObjects;
\r
340 btOpenCLBuffer<CollisionObjectIndices> m_clPerClothCollisionObjects;
\r
343 * Collision shapes being passed across to the cloths in this solver.
\r
345 btAlignedObjectArray< CollisionShapeDescription > m_collisionObjectDetails;
\r
346 btOpenCLBuffer< CollisionShapeDescription > m_clCollisionObjectDetails;
\r
351 * Friction coefficient for each cloth
\r
353 btAlignedObjectArray< float > m_perClothFriction;
\r
354 btOpenCLBuffer< float > m_clPerClothFriction;
\r
356 // anchor node info
\r
357 struct AnchorNodeInfoCL
\r
360 btSoftBody::Node* pNode;
\r
363 btAlignedObjectArray<AnchorNodeInfoCL> m_anchorNodeInfoArray;
\r
364 btAlignedObjectArray<Vectormath::Aos::Point3> m_anchorPosition;
\r
365 btOpenCLBuffer<Vectormath::Aos::Point3> m_clAnchorPosition;
\r
366 btAlignedObjectArray<int> m_anchorIndex;
\r
367 btOpenCLBuffer<int> m_clAnchorIndex;
\r
369 bool m_bUpdateAnchoredNodePos;
\r
371 cl_kernel m_prepareLinksKernel;
\r
372 cl_kernel m_solvePositionsFromLinksKernel;
\r
373 cl_kernel m_updateConstantsKernel;
\r
374 cl_kernel m_integrateKernel;
\r
375 cl_kernel m_addVelocityKernel;
\r
376 cl_kernel m_updatePositionsFromVelocitiesKernel;
\r
377 cl_kernel m_updateVelocitiesFromPositionsWithoutVelocitiesKernel;
\r
378 cl_kernel m_updateVelocitiesFromPositionsWithVelocitiesKernel;
\r
379 cl_kernel m_vSolveLinksKernel;
\r
380 cl_kernel m_solveCollisionsAndUpdateVelocitiesKernel;
\r
381 cl_kernel m_resetNormalsAndAreasKernel;
\r
382 cl_kernel m_normalizeNormalsAndAreasKernel;
\r
383 cl_kernel m_updateSoftBodiesKernel;
\r
385 cl_kernel m_outputToVertexArrayKernel;
\r
386 cl_kernel m_applyForcesKernel;
\r
387 cl_kernel m_updateFixedVertexPositionsKernel;
\r
389 cl_command_queue m_cqCommandQue;
\r
390 cl_context m_cxMainContext;
\r
392 size_t m_defaultWorkGroupSize;
\r
395 virtual bool buildShaders();
\r
397 void resetNormalsAndAreas( int numVertices );
\r
399 void normalizeNormalsAndAreas( int numVertices );
\r
401 void executeUpdateSoftBodies( int firstTriangle, int numTriangles );
\r
403 void prepareCollisionConstraints();
\r
405 Vectormath::Aos::Vector3 ProjectOnAxis( const Vectormath::Aos::Vector3 &v, const Vectormath::Aos::Vector3 &a );
\r
407 void ApplyClampedForce( float solverdt, const Vectormath::Aos::Vector3 &force, const Vectormath::Aos::Vector3 &vertexVelocity, float inverseMass, Vectormath::Aos::Vector3 &vertexForce );
\r
410 int findSoftBodyIndex( const btSoftBody* const softBody );
\r
412 virtual void applyForces( float solverdt );
\r
414 void updateFixedVertexPositions();
\r
417 * Integrate motion on the solver.
\r
419 virtual void integrate( float solverdt );
\r
421 virtual void updateConstants( float timeStep );
\r
423 float computeTriangleArea(
\r
424 const Vectormath::Aos::Point3 &vertex0,
\r
425 const Vectormath::Aos::Point3 &vertex1,
\r
426 const Vectormath::Aos::Point3 &vertex2 );
\r
429 //////////////////////////////////////
\r
430 // Kernel dispatches
\r
431 void prepareLinks();
\r
433 void solveLinksForVelocity( int startLink, int numLinks, float kst );
\r
435 void updatePositionsFromVelocities( float solverdt );
\r
437 virtual void solveLinksForPosition( int startLink, int numLinks, float kst, float ti );
\r
439 void updateVelocitiesFromPositionsWithVelocities( float isolverdt );
\r
441 void updateVelocitiesFromPositionsWithoutVelocities( float isolverdt );
\r
442 virtual void solveCollisionsAndUpdateVelocities( float isolverdt );
\r
444 // End kernel dispatches
\r
445 /////////////////////////////////////
\r
447 void updateBounds();
\r
449 void releaseKernels();
\r
452 btOpenCLSoftBodySolver(cl_command_queue queue,cl_context ctx, bool bUpdateAchchoredNodePos = false);
\r
454 virtual ~btOpenCLSoftBodySolver();
\r
458 btOpenCLAcceleratedSoftBodyInterface *findSoftBodyInterface( const btSoftBody* const softBody );
\r
460 virtual btSoftBodyLinkData &getLinkData();
\r
462 virtual btSoftBodyVertexData &getVertexData();
\r
464 virtual btSoftBodyTriangleData &getTriangleData();
\r
466 virtual SolverTypes getSolverType() const
\r
472 virtual bool checkInitialized();
\r
474 virtual void updateSoftBodies( );
\r
476 virtual void optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate=false);
\r
478 virtual void copyBackToSoftBodies(bool bMove = true);
\r
480 virtual void solveConstraints( float solverdt );
\r
482 virtual void predictMotion( float solverdt );
\r
484 virtual void processCollision( btSoftBody *, btCollisionObject* );
\r
486 virtual void processCollision( btSoftBody*, btSoftBody* );
\r
488 virtual void setDefaultWorkgroupSize(size_t workGroupSize)
\r
490 m_defaultWorkGroupSize = workGroupSize;
\r
492 virtual size_t getDefaultWorkGroupSize() const
\r
494 return m_defaultWorkGroupSize;
\r
497 void setCLFunctions(CLFunctions* funcs)
\r
500 m_currentCLFunctions = funcs;
\r
502 m_currentCLFunctions = &m_defaultCLFunctions;
\r
505 }; // btOpenCLSoftBodySolver
\r
509 * Class to manage movement of data from a solver to a given target.
\r
510 * This version is the CL to CPU version.
\r
512 class btSoftBodySolverOutputCLtoCPU : public btSoftBodySolverOutput
\r
517 btSoftBodySolverOutputCLtoCPU()
\r
521 /** Output current computed vertex data to the vertex buffers for all cloths in the solver. */
\r
522 virtual void copySoftBodyToVertexBuffer( const btSoftBody * const softBody, btVertexBufferDescriptor *vertexBuffer );
\r
527 #endif // #ifndef BT_SOFT_BODY_SOLVER_OPENCL_H
\r