2
Bullet Continuous Collision Detection and Physics Library
3
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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:
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.
16
#ifndef BT_SOFT_BODY_SOLVER_OPENCL_H
17
#define BT_SOFT_BODY_SOLVER_OPENCL_H
19
#include "stddef.h" //for size_t
20
#include "vectormath/vmInclude.h"
22
#include "BulletSoftBody/btSoftBodySolvers.h"
23
#include "btSoftBodySolverBuffer_OpenCL.h"
24
#include "btSoftBodySolverLinkData_OpenCL.h"
25
#include "btSoftBodySolverVertexData_OpenCL.h"
26
#include "btSoftBodySolverTriangleData_OpenCL.h"
31
cl_command_queue m_cqCommandQue;
32
cl_context m_cxMainContext;
35
CLFunctions(cl_command_queue cqCommandQue, cl_context cxMainContext) :
36
m_cqCommandQue( cqCommandQue ),
37
m_cxMainContext( cxMainContext )
43
* Compile a compute shader kernel from a string and return the appropriate cl_kernel object.
45
cl_kernel compileCLKernelFromString( const char* kernelSource, const char* kernelName, const char* additionalMacros = "" );
49
* Entry in the collision shape array.
50
* Specifies the shape type, the transform matrix and the necessary details of the collisionShape.
52
struct CollisionShapeDescription
54
Vectormath::Aos::Transform3 shapeTransform;
55
Vectormath::Aos::Vector3 linearVelocity;
56
Vectormath::Aos::Vector3 angularVelocity;
58
int softBodyIdentifier;
59
int collisionShapeType;
61
// Both needed for capsule
69
CollisionShapeDescription()
71
collisionShapeType = 0;
78
* SoftBody class to maintain information about a soft body instance
80
* This data addresses the main solver arrays.
82
class btOpenCLAcceleratedSoftBodyInterface
85
/** Current number of vertices that are part of this cloth */
87
/** Maximum number of vertices allocated to be part of this cloth */
89
/** Current number of triangles that are part of this cloth */
91
/** Maximum number of triangles allocated to be part of this cloth */
93
/** Index of first vertex in the world allocated to this cloth */
95
/** Index of first triangle in the world allocated to this cloth */
97
/** Index of first link in the world allocated to this cloth */
99
/** Maximum number of links allocated to this cloth */
101
/** Current number of links allocated to this cloth */
104
/** The actual soft body this data represents */
105
btSoftBody *m_softBody;
109
btOpenCLAcceleratedSoftBodyInterface( btSoftBody *softBody ) :
110
m_softBody( softBody )
124
return m_numVertices;
127
int getNumTriangles()
129
return m_numTriangles;
134
return m_maxVertices;
137
int getMaxTriangles()
139
return m_maxTriangles;
144
return m_firstVertex;
147
int getFirstTriangle()
149
return m_firstTriangle;
153
* Update the bounds in the btSoftBody object
155
void updateBounds( const btVector3 &lowerBound, const btVector3 &upperBound );
157
// TODO: All of these set functions will have to do checks and
158
// update the world because restructuring of the arrays will be necessary
159
// Reasonable use of "friend"?
160
void setNumVertices( int numVertices )
162
m_numVertices = numVertices;
165
void setNumTriangles( int numTriangles )
167
m_numTriangles = numTriangles;
170
void setMaxVertices( int maxVertices )
172
m_maxVertices = maxVertices;
175
void setMaxTriangles( int maxTriangles )
177
m_maxTriangles = maxTriangles;
180
void setFirstVertex( int firstVertex )
182
m_firstVertex = firstVertex;
185
void setFirstTriangle( int firstTriangle )
187
m_firstTriangle = firstTriangle;
190
void setMaxLinks( int maxLinks )
192
m_maxLinks = maxLinks;
195
void setNumLinks( int numLinks )
197
m_numLinks = numLinks;
200
void setFirstLink( int firstLink )
202
m_firstLink = firstLink;
220
btSoftBody* getSoftBody()
229
class btOpenCLSoftBodySolver : public btSoftBodySolver
244
UIntVector3( unsigned int x_, unsigned int y_, unsigned int z_ )
255
unsigned int _padding;
258
struct CollisionObjectIndices
260
CollisionObjectIndices( int f, int e )
270
btSoftBodyLinkDataOpenCL m_linkData;
271
btSoftBodyVertexDataOpenCL m_vertexData;
272
btSoftBodyTriangleDataOpenCL m_triangleData;
276
CLFunctions clFunctions;
278
/** Variable to define whether we need to update solver constants on the next iteration */
279
bool m_updateSolverConstants;
281
bool m_shadersInitialized;
284
* Cloths owned by this solver.
285
* Only our cloths are in this array.
287
btAlignedObjectArray< btOpenCLAcceleratedSoftBodyInterface * > m_softBodySet;
289
/** Acceleration value to be applied to all non-static vertices in the solver.
290
* Index n is cloth n, array sized by number of cloths in the world not the solver.
292
btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothAcceleration;
293
btOpenCLBuffer<Vectormath::Aos::Vector3> m_clPerClothAcceleration;
295
/** Wind velocity to be applied normal to all non-static vertices in the solver.
296
* Index n is cloth n, array sized by number of cloths in the world not the solver.
298
btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothWindVelocity;
299
btOpenCLBuffer<Vectormath::Aos::Vector3> m_clPerClothWindVelocity;
301
/** Velocity damping factor */
302
btAlignedObjectArray< float > m_perClothDampingFactor;
303
btOpenCLBuffer<float> m_clPerClothDampingFactor;
305
/** Velocity correction coefficient */
306
btAlignedObjectArray< float > m_perClothVelocityCorrectionCoefficient;
307
btOpenCLBuffer<float> m_clPerClothVelocityCorrectionCoefficient;
309
/** Lift parameter for wind effect on cloth. */
310
btAlignedObjectArray< float > m_perClothLiftFactor;
311
btOpenCLBuffer<float> m_clPerClothLiftFactor;
313
/** Drag parameter for wind effect on cloth. */
314
btAlignedObjectArray< float > m_perClothDragFactor;
315
btOpenCLBuffer<float> m_clPerClothDragFactor;
317
/** Density of the medium in which each cloth sits */
318
btAlignedObjectArray< float > m_perClothMediumDensity;
319
btOpenCLBuffer<float> m_clPerClothMediumDensity;
322
* Collision shape details: pair of index of first collision shape for the cloth and number of collision objects.
324
btAlignedObjectArray< CollisionObjectIndices > m_perClothCollisionObjects;
325
btOpenCLBuffer<CollisionObjectIndices> m_clPerClothCollisionObjects;
328
* Collision shapes being passed across to the cloths in this solver.
330
btAlignedObjectArray< CollisionShapeDescription > m_collisionObjectDetails;
331
btOpenCLBuffer< CollisionShapeDescription > m_clCollisionObjectDetails;
336
* Friction coefficient for each cloth
338
btAlignedObjectArray< float > m_perClothFriction;
339
btOpenCLBuffer< float > m_clPerClothFriction;
343
cl_kernel prepareLinksKernel;
344
cl_kernel solvePositionsFromLinksKernel;
345
cl_kernel updateConstantsKernel;
346
cl_kernel integrateKernel;
347
cl_kernel addVelocityKernel;
348
cl_kernel updatePositionsFromVelocitiesKernel;
349
cl_kernel updateVelocitiesFromPositionsWithoutVelocitiesKernel;
350
cl_kernel updateVelocitiesFromPositionsWithVelocitiesKernel;
351
cl_kernel vSolveLinksKernel;
352
cl_kernel solveCollisionsAndUpdateVelocitiesKernel;
353
cl_kernel resetNormalsAndAreasKernel;
354
cl_kernel normalizeNormalsAndAreasKernel;
355
cl_kernel updateSoftBodiesKernel;
357
cl_kernel outputToVertexArrayKernel;
358
cl_kernel applyForcesKernel;
360
cl_command_queue m_cqCommandQue;
361
cl_context m_cxMainContext;
363
size_t m_defaultWorkGroupSize;
366
virtual bool buildShaders();
368
void resetNormalsAndAreas( int numVertices );
370
void normalizeNormalsAndAreas( int numVertices );
372
void executeUpdateSoftBodies( int firstTriangle, int numTriangles );
374
void prepareCollisionConstraints();
376
Vectormath::Aos::Vector3 ProjectOnAxis( const Vectormath::Aos::Vector3 &v, const Vectormath::Aos::Vector3 &a );
378
void ApplyClampedForce( float solverdt, const Vectormath::Aos::Vector3 &force, const Vectormath::Aos::Vector3 &vertexVelocity, float inverseMass, Vectormath::Aos::Vector3 &vertexForce );
381
int findSoftBodyIndex( const btSoftBody* const softBody );
383
virtual void applyForces( float solverdt );
386
* Integrate motion on the solver.
388
virtual void integrate( float solverdt );
390
virtual void updateConstants( float timeStep );
392
float computeTriangleArea(
393
const Vectormath::Aos::Point3 &vertex0,
394
const Vectormath::Aos::Point3 &vertex1,
395
const Vectormath::Aos::Point3 &vertex2 );
398
//////////////////////////////////////
402
void solveLinksForVelocity( int startLink, int numLinks, float kst );
404
void updatePositionsFromVelocities( float solverdt );
406
virtual void solveLinksForPosition( int startLink, int numLinks, float kst, float ti );
408
void updateVelocitiesFromPositionsWithVelocities( float isolverdt );
410
void updateVelocitiesFromPositionsWithoutVelocities( float isolverdt );
411
virtual void solveCollisionsAndUpdateVelocities( float isolverdt );
413
// End kernel dispatches
414
/////////////////////////////////////
418
void releaseKernels();
421
btOpenCLSoftBodySolver(cl_command_queue queue,cl_context ctx);
423
virtual ~btOpenCLSoftBodySolver();
427
btOpenCLAcceleratedSoftBodyInterface *findSoftBodyInterface( const btSoftBody* const softBody );
429
virtual btSoftBodyLinkData &getLinkData();
431
virtual btSoftBodyVertexData &getVertexData();
433
virtual btSoftBodyTriangleData &getTriangleData();
435
virtual SolverTypes getSolverType() const
441
virtual bool checkInitialized();
443
virtual void updateSoftBodies( );
445
virtual void optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate=false);
447
virtual void copyBackToSoftBodies();
449
virtual void solveConstraints( float solverdt );
451
virtual void predictMotion( float solverdt );
453
virtual void processCollision( btSoftBody *, btCollisionObject* );
455
virtual void processCollision( btSoftBody*, btSoftBody* );
457
virtual void setDefaultWorkgroupSize(size_t workGroupSize)
459
m_defaultWorkGroupSize = workGroupSize;
461
virtual size_t getDefaultWorkGroupSize() const
463
return m_defaultWorkGroupSize;
466
}; // btOpenCLSoftBodySolver
470
* Class to manage movement of data from a solver to a given target.
471
* This version is the CL to CPU version.
473
class btSoftBodySolverOutputCLtoCPU : public btSoftBodySolverOutput
478
btSoftBodySolverOutputCLtoCPU()
482
/** Output current computed vertex data to the vertex buffers for all cloths in the solver. */
483
virtual void copySoftBodyToVertexBuffer( const btSoftBody * const softBody, btVertexBufferDescriptor *vertexBuffer );
488
#endif // #ifndef BT_SOFT_BODY_SOLVER_OPENCL_H