Bullet Collision Detection & Physics Library
btSoftBodySolver_OpenCL.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 #ifndef BT_SOFT_BODY_SOLVER_OPENCL_H
17 #define BT_SOFT_BODY_SOLVER_OPENCL_H
18 
19 #include "stddef.h" //for size_t
20 #include "vectormath/vmInclude.h"
21 
28 
30 {
31 protected:
34 
36 
37 
38 public:
39  CLFunctions(cl_command_queue cqCommandQue, cl_context cxMainContext) :
40  m_cqCommandQue( cqCommandQue ),
41  m_cxMainContext( cxMainContext ),
43  {
44  }
45 
47  {
49  }
50 
54  virtual cl_kernel compileCLKernelFromString( const char* kernelSource, const char* kernelName, const char* additionalMacros, const char* srcFileNameForCaching);
55 
57  {
59  }
60 };
61 
67 {
71 
74 
75  // Both needed for capsule
76  float radius;
77  float halfHeight;
78  int upAxis;
79 
80  float margin;
81  float friction;
82 
84  {
86  margin = 0;
87  friction = 0;
88  }
89 };
90 
97 {
98 protected:
117 
120 
121 
122 public:
124  m_softBody( softBody )
125  {
126  m_numVertices = 0;
127  m_maxVertices = 0;
128  m_numTriangles = 0;
129  m_maxTriangles = 0;
130  m_firstVertex = 0;
131  m_firstTriangle = 0;
132  m_firstLink = 0;
133  m_maxLinks = 0;
134  m_numLinks = 0;
135  }
137  {
138  return m_numVertices;
139  }
140 
142  {
143  return m_numTriangles;
144  }
145 
147  {
148  return m_maxVertices;
149  }
150 
152  {
153  return m_maxTriangles;
154  }
155 
157  {
158  return m_firstVertex;
159  }
160 
162  {
163  return m_firstTriangle;
164  }
165 
169  void updateBounds( const btVector3 &lowerBound, const btVector3 &upperBound );
170 
171  // TODO: All of these set functions will have to do checks and
172  // update the world because restructuring of the arrays will be necessary
173  // Reasonable use of "friend"?
174  void setNumVertices( int numVertices )
175  {
176  m_numVertices = numVertices;
177  }
178 
179  void setNumTriangles( int numTriangles )
180  {
181  m_numTriangles = numTriangles;
182  }
183 
184  void setMaxVertices( int maxVertices )
185  {
186  m_maxVertices = maxVertices;
187  }
188 
189  void setMaxTriangles( int maxTriangles )
190  {
191  m_maxTriangles = maxTriangles;
192  }
193 
194  void setFirstVertex( int firstVertex )
195  {
196  m_firstVertex = firstVertex;
197  }
198 
199  void setFirstTriangle( int firstTriangle )
200  {
201  m_firstTriangle = firstTriangle;
202  }
203 
204  void setMaxLinks( int maxLinks )
205  {
206  m_maxLinks = maxLinks;
207  }
208 
209  void setNumLinks( int numLinks )
210  {
211  m_numLinks = numLinks;
212  }
213 
214  void setFirstLink( int firstLink )
215  {
216  m_firstLink = firstLink;
217  }
218 
220  {
221  return m_maxLinks;
222  }
223 
225  {
226  return m_numLinks;
227  }
228 
230  {
231  return m_firstLink;
232  }
233 
235  {
236  return m_softBody;
237  }
238 
239 };
240 
241 
242 
244 {
245 public:
246 
247 
248  struct UIntVector3
249  {
251  {
252  x = 0;
253  y = 0;
254  z = 0;
255  _padding = 0;
256  }
257 
258  UIntVector3( unsigned int x_, unsigned int y_, unsigned int z_ )
259  {
260  x = x_;
261  y = y_;
262  z = z_;
263  _padding = 0;
264  }
265 
266  unsigned int x;
267  unsigned int y;
268  unsigned int z;
269  unsigned int _padding;
270  };
271 
273  {
274  CollisionObjectIndices( int f, int e )
275  {
276  firstObject = f;
277  endObject = e;
278  }
279 
282  };
283 
287 
288 protected:
289 
292 
295 
297 
303 
309 
315 
319 
323 
327 
331 
335 
341 
347 
348 
349 
355 
356  // anchor node info
358  {
361  };
362 
368 
370 
384 
388 
391 
393 
394 
395  virtual bool buildShaders();
396 
397  void resetNormalsAndAreas( int numVertices );
398 
399  void normalizeNormalsAndAreas( int numVertices );
400 
401  void executeUpdateSoftBodies( int firstTriangle, int numTriangles );
402 
404 
406 
407  void ApplyClampedForce( float solverdt, const Vectormath::Aos::Vector3 &force, const Vectormath::Aos::Vector3 &vertexVelocity, float inverseMass, Vectormath::Aos::Vector3 &vertexForce );
408 
409 
410  int findSoftBodyIndex( const btSoftBody* const softBody );
411 
412  virtual void applyForces( float solverdt );
413 
415 
419  virtual void integrate( float solverdt );
420 
421  virtual void updateConstants( float timeStep );
422 
423  float computeTriangleArea(
424  const Vectormath::Aos::Point3 &vertex0,
425  const Vectormath::Aos::Point3 &vertex1,
426  const Vectormath::Aos::Point3 &vertex2 );
427 
428 
430  // Kernel dispatches
431  void prepareLinks();
432 
433  void solveLinksForVelocity( int startLink, int numLinks, float kst );
434 
435  void updatePositionsFromVelocities( float solverdt );
436 
437  virtual void solveLinksForPosition( int startLink, int numLinks, float kst, float ti );
438 
439  void updateVelocitiesFromPositionsWithVelocities( float isolverdt );
440 
441  void updateVelocitiesFromPositionsWithoutVelocities( float isolverdt );
442  virtual void solveCollisionsAndUpdateVelocities( float isolverdt );
443 
444  // End kernel dispatches
446 
447  void updateBounds();
448 
449  void releaseKernels();
450 
451 public:
452  btOpenCLSoftBodySolver(cl_command_queue queue,cl_context ctx, bool bUpdateAchchoredNodePos = false);
453 
454  virtual ~btOpenCLSoftBodySolver();
455 
456 
457 
459 
460  virtual btSoftBodyLinkData &getLinkData();
461 
463 
465 
466  virtual SolverTypes getSolverType() const
467  {
468  return CL_SOLVER;
469  }
470 
471 
472  virtual bool checkInitialized();
473 
474  virtual void updateSoftBodies( );
475 
476  virtual void optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate=false);
477 
478  virtual void copyBackToSoftBodies(bool bMove = true);
479 
480  virtual void solveConstraints( float solverdt );
481 
482  virtual void predictMotion( float solverdt );
483 
484  virtual void processCollision( btSoftBody *, const btCollisionObjectWrapper* );
485 
486  virtual void processCollision( btSoftBody*, btSoftBody* );
487 
489  {
491  }
492  virtual size_t getDefaultWorkGroupSize() const
493  {
494  return m_defaultWorkGroupSize;
495  }
496 
498  {
499  if (funcs)
500  m_currentCLFunctions = funcs;
501  else
503  }
504 
505 }; // btOpenCLSoftBodySolver
506 
507 
513 {
514 protected:
515 
516 public:
518  {
519  }
520 
522  virtual void copySoftBodyToVertexBuffer( const btSoftBody * const softBody, btVertexBufferDescriptor *vertexBuffer );
523 };
524 
525 
526 
527 #endif // #ifndef BT_SOFT_BODY_SOLVER_OPENCL_H
btOpenCLBuffer< Vectormath::Aos::Point3 > m_clAnchorPosition
btAlignedObjectArray< float > m_perClothDragFactor
Drag parameter for wind effect on cloth.
virtual void solveCollisionsAndUpdateVelocities(float isolverdt)
struct _cl_context * cl_context
Definition: cl.h:41
btOpenCLBuffer< float > m_clPerClothLiftFactor
int m_numTriangles
Current number of triangles that are part of this cloth.
Vectormath::Aos::Vector3 linearVelocity
virtual void applyForces(float solverdt)
virtual void predictMotion(float solverdt)
Predict motion of soft bodies into next timestep.
btOpenCLBuffer< float > m_clPerClothFriction
virtual size_t getDefaultWorkGroupSize() const
float computeTriangleArea(const Vectormath::Aos::Point3 &vertex0, const Vectormath::Aos::Point3 &vertex1, const Vectormath::Aos::Point3 &vertex2)
void solveLinksForVelocity(int startLink, int numLinks, float kst)
btAlignedObjectArray< CollisionObjectIndices > m_perClothCollisionObjects
Collision shape details: pair of index of first collision shape for the cloth and number of collision...
virtual void setDefaultWorkgroupSize(size_t workGroupSize)
virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
Output current computed vertex data to the vertex buffers for all cloths in the solver.
void resetNormalsAndAreas(int numVertices)
btSoftBodyLinkDataOpenCL m_linkData
virtual SolverTypes getSolverType() const
Return the type of the solver.
virtual cl_kernel compileCLKernelFromString(const char *kernelSource, const char *kernelName, const char *additionalMacros, const char *srcFileNameForCaching)
Compile a compute shader kernel from a string and return the appropriate cl_kernel object...
SoftBody class to maintain information about a soft body instance within a solver.
cl_command_queue m_cqCommandQue
void ApplyClampedForce(float solverdt, const Vectormath::Aos::Vector3 &force, const Vectormath::Aos::Vector3 &vertexVelocity, float inverseMass, Vectormath::Aos::Vector3 &vertexForce)
virtual bool checkInitialized()
Ensure that this solver is initialized.
int m_maxTriangles
Maximum number of triangles allocated to be part of this cloth.
void normalizeNormalsAndAreas(int numVertices)
int getKernelCompilationFailures() const
cl_kernel m_updateVelocitiesFromPositionsWithVelocitiesKernel
int m_maxVertices
Maximum number of vertices allocated to be part of this cloth.
btSoftBodyTriangleDataOpenCL m_triangleData
int findSoftBodyIndex(const btSoftBody *const softBody)
btAlignedObjectArray< btOpenCLAcceleratedSoftBodyInterface * > m_softBodySet
Cloths owned by this solver.
btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothWindVelocity
Wind velocity to be applied normal to all non-static vertices in the solver.
btOpenCLBuffer< CollisionShapeDescription > m_clCollisionObjectDetails
static const size_t workGroupSize
btOpenCLBuffer< Vectormath::Aos::Vector3 > m_clPerClothAcceleration
btOpenCLAcceleratedSoftBodyInterface(btSoftBody *softBody)
virtual void copyBackToSoftBodies(bool bMove=true)
Copy necessary data back to the original soft body source objects.
btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothAcceleration
Acceleration value to be applied to all non-static vertices in the solver.
btAlignedObjectArray< AnchorNodeInfoCL > m_anchorNodeInfoArray
Vectormath::Aos::Transform3 shapeTransform
btOpenCLSoftBodySolver(cl_command_queue queue, cl_context ctx, bool bUpdateAchchoredNodePos=false)
int m_numVertices
Current number of vertices that are part of this cloth.
virtual btSoftBodyLinkData & getLinkData()
Entry in the collision shape array.
struct _cl_kernel * cl_kernel
Definition: cl.h:45
CLFunctions(cl_command_queue cqCommandQue, cl_context cxMainContext)
btAlignedObjectArray< int > m_anchorIndex
int m_firstLink
Index of first link in the world allocated to this cloth.
int m_firstTriangle
Index of first triangle in the world allocated to this cloth.
virtual void integrate(float solverdt)
Integrate motion on the solver.
void updateBounds(const btVector3 &lowerBound, const btVector3 &upperBound)
Update the bounds in the btSoftBody object.
virtual void processCollision(btSoftBody *, const btCollisionObjectWrapper *)
btAlignedObjectArray< float > m_perClothDampingFactor
Velocity damping factor.
btAlignedObjectArray< float > m_perClothVelocityCorrectionCoefficient
Velocity correction coefficient.
btAlignedObjectArray< float > m_perClothFriction
Friction coefficient for each cloth.
btSoftBody * m_softBody
The actual soft body this data represents.
void updateVelocitiesFromPositionsWithVelocities(float isolverdt)
UIntVector3(unsigned int x_, unsigned int y_, unsigned int z_)
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
void prepareCollisionConstraints()
Sort the collision object details array and generate indexing into it for the per-cloth collision obj...
Vectormath::Aos::Vector3 ProjectOnAxis(const Vectormath::Aos::Vector3 &v, const Vectormath::Aos::Vector3 &a)
void clearKernelCompilationFailures()
btOpenCLBuffer< float > m_clPerClothMediumDensity
Wrapper for vertex data information.
int m_numLinks
Current number of links allocated to this cloth.
btOpenCLBuffer< float > m_clPerClothDampingFactor
virtual void solveLinksForPosition(int startLink, int numLinks, float kst, float ti)
void executeUpdateSoftBodies(int firstTriangle, int numTriangles)
int m_firstVertex
Index of first vertex in the world allocated to this cloth.
void setCLFunctions(CLFunctions *funcs)
virtual void updateConstants(float timeStep)
btOpenCLAcceleratedSoftBodyInterface * findSoftBodyInterface(const btSoftBody *const softBody)
btAlignedObjectArray< float > m_perClothLiftFactor
Lift parameter for wind effect on cloth.
void updateVelocitiesFromPositionsWithoutVelocities(float isolverdt)
Vectormath::Aos::Vector3 angularVelocity
int m_maxLinks
Maximum number of links allocated to this cloth.
Class to manage movement of data from a solver to a given target.
virtual btSoftBodyVertexData & getVertexData()
struct _cl_command_queue * cl_command_queue
Definition: cl.h:42
btOpenCLBuffer< Vectormath::Aos::Vector3 > m_clPerClothWindVelocity
Class to manage movement of data from a solver to a given target.
btAlignedObjectArray< CollisionShapeDescription > m_collisionObjectDetails
Collision shapes being passed across to the cloths in this solver.
void updatePositionsFromVelocities(float solverdt)
virtual void optimize(btAlignedObjectArray< btSoftBody * > &softBodies, bool forceUpdate=false)
Optimize soft bodies in this solver.
cl_kernel m_solveCollisionsAndUpdateVelocitiesKernel
virtual void updateSoftBodies()
Perform necessary per-step updates of soft bodies such as recomputing normals and bounding boxes...
btAlignedObjectArray< float > m_perClothMediumDensity
Density of the medium in which each cloth sits.
bool m_updateSolverConstants
Variable to define whether we need to update solver constants on the next iteration.
btOpenCLBuffer< float > m_clPerClothVelocityCorrectionCoefficient
btOpenCLBuffer< float > m_clPerClothDragFactor
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:71
virtual btSoftBodyTriangleData & getTriangleData()
btSoftBodyVertexDataOpenCL m_vertexData
btAlignedObjectArray< Vectormath::Aos::Point3 > m_anchorPosition
cl_kernel m_updateVelocitiesFromPositionsWithoutVelocitiesKernel
virtual void solveConstraints(float solverdt)
Solve constraints for a set of soft bodies.
btOpenCLBuffer< CollisionObjectIndices > m_clPerClothCollisionObjects
cl_context m_cxMainContext
btOpenCLBuffer< int > m_clAnchorIndex