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