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_ACCELERATED_SOFT_BODY_DX11_SOLVER_H
17
#define BT_ACCELERATED_SOFT_BODY_DX11_SOLVER_H
20
#include "vectormath/vmInclude.h"
21
#include "BulletSoftBody/btSoftBodySolvers.h"
22
#include "btSoftBodySolverVertexBuffer_DX11.h"
23
#include "btSoftBodySolverLinkData_DX11.h"
24
#include "btSoftBodySolverVertexData_DX11.h"
25
#include "btSoftBodySolverTriangleData_DX11.h"
33
typedef HRESULT (WINAPI * CompileFromMemoryFunc)(LPCSTR,SIZE_T,LPCSTR,const D3D10_SHADER_MACRO*,LPD3D10INCLUDE,LPCSTR,LPCSTR,UINT,UINT,ID3DX11ThreadPump*,ID3D10Blob**,ID3D10Blob**,HRESULT*);
35
ID3D11Device * m_dx11Device;
36
ID3D11DeviceContext* m_dx11Context;
37
CompileFromMemoryFunc m_dx11CompileFromMemory;
39
DXFunctions(ID3D11Device *dx11Device, ID3D11DeviceContext* dx11Context, CompileFromMemoryFunc dx11CompileFromMemory) :
40
m_dx11Device( dx11Device ),
41
m_dx11Context( dx11Context ),
42
m_dx11CompileFromMemory( dx11CompileFromMemory )
53
ID3D11ComputeShader* kernel;
54
ID3D11Buffer* constBuffer;
64
// TODO: this should probably destroy its kernel but we need to be careful
65
// in case KernelDescs are copied
70
* Compile a compute shader kernel from a string and return the appropriate KernelDesc object.
72
KernelDesc compileComputeShaderFromString( const char* shaderString, const char* shaderName, int constBufferSize, D3D10_SHADER_MACRO *compileMacros = 0 );
76
class btDX11SoftBodySolver : public btSoftBodySolver
80
* Entry in the collision shape array.
81
* Specifies the shape type, the transform matrix and the necessary details of the collisionShape.
83
struct CollisionShapeDescription
85
Vectormath::Aos::Transform3 shapeTransform;
86
Vectormath::Aos::Vector3 linearVelocity;
87
Vectormath::Aos::Vector3 angularVelocity;
89
int softBodyIdentifier;
90
int collisionShapeType;
92
// Both needed for capsule
99
CollisionShapeDescription()
101
collisionShapeType = 0;
117
UIntVector3( unsigned int x_, unsigned int y_, unsigned int z_ )
128
unsigned int _padding;
135
* SoftBody class to maintain information about a soft body instance
137
* This data addresses the main solver arrays.
139
class btAcceleratedSoftBodyInterface
142
/** Current number of vertices that are part of this cloth */
144
/** Maximum number of vertices allocated to be part of this cloth */
146
/** Current number of triangles that are part of this cloth */
148
/** Maximum number of triangles allocated to be part of this cloth */
150
/** Index of first vertex in the world allocated to this cloth */
152
/** Index of first triangle in the world allocated to this cloth */
154
/** Index of first link in the world allocated to this cloth */
156
/** Maximum number of links allocated to this cloth */
158
/** Current number of links allocated to this cloth */
161
/** The actual soft body this data represents */
162
btSoftBody *m_softBody;
166
btAcceleratedSoftBodyInterface( btSoftBody *softBody ) :
167
m_softBody( softBody )
179
int getNumVertices() const
181
return m_numVertices;
184
int getNumTriangles() const
186
return m_numTriangles;
189
int getMaxVertices() const
191
return m_maxVertices;
194
int getMaxTriangles() const
196
return m_maxTriangles;
199
int getFirstVertex() const
201
return m_firstVertex;
204
int getFirstTriangle() const
206
return m_firstTriangle;
211
* Update the bounds in the btSoftBody object
213
void updateBounds( const btVector3 &lowerBound, const btVector3 &upperBound );
215
// TODO: All of these set functions will have to do checks and
216
// update the world because restructuring of the arrays will be necessary
217
// Reasonable use of "friend"?
218
void setNumVertices( int numVertices )
220
m_numVertices = numVertices;
223
void setNumTriangles( int numTriangles )
225
m_numTriangles = numTriangles;
228
void setMaxVertices( int maxVertices )
230
m_maxVertices = maxVertices;
233
void setMaxTriangles( int maxTriangles )
235
m_maxTriangles = maxTriangles;
238
void setFirstVertex( int firstVertex )
240
m_firstVertex = firstVertex;
243
void setFirstTriangle( int firstTriangle )
245
m_firstTriangle = firstTriangle;
248
void setMaxLinks( int maxLinks )
250
m_maxLinks = maxLinks;
253
void setNumLinks( int numLinks )
255
m_numLinks = numLinks;
258
void setFirstLink( int firstLink )
260
m_firstLink = firstLink;
278
btSoftBody* getSoftBody()
286
struct CollisionObjectIndices
288
CollisionObjectIndices( int f, int e )
302
struct PrepareLinksCB
310
struct SolvePositionsFromLinksKernelCB
326
struct UpdatePositionsFromVelocitiesCB
334
struct UpdateVelocitiesFromPositionsWithoutVelocitiesCB
342
struct UpdateVelocitiesFromPositionsWithVelocitiesCB
350
struct UpdateSoftBodiesCB
361
unsigned int numNodes;
387
struct ComputeBoundsCB
395
struct SolveCollisionsAndUpdateVelocitiesCB
397
unsigned int numNodes;
407
ID3D11Device * m_dx11Device;
408
ID3D11DeviceContext* m_dx11Context;
410
DXFunctions dxFunctions;
412
/** Link data for all cloths. Note that this will be sorted batch-wise for efficient computation and m_linkAddresses will maintain the addressing. */
413
btSoftBodyLinkDataDX11 m_linkData;
414
btSoftBodyVertexDataDX11 m_vertexData;
415
btSoftBodyTriangleDataDX11 m_triangleData;
419
/** Variable to define whether we need to update solver constants on the next iteration */
420
bool m_updateSolverConstants;
422
bool m_shadersInitialized;
425
* Cloths owned by this solver.
426
* Only our cloths are in this array.
428
btAlignedObjectArray< btAcceleratedSoftBodyInterface * > m_softBodySet;
430
/** Acceleration value to be applied to all non-static vertices in the solver.
431
* Index n is cloth n, array sized by number of cloths in the world not the solver.
433
btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothAcceleration;
434
btDX11Buffer<Vectormath::Aos::Vector3> m_dx11PerClothAcceleration;
436
/** Wind velocity to be applied normal to all non-static vertices in the solver.
437
* Index n is cloth n, array sized by number of cloths in the world not the solver.
439
btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothWindVelocity;
440
btDX11Buffer<Vectormath::Aos::Vector3> m_dx11PerClothWindVelocity;
442
/** Velocity damping factor */
443
btAlignedObjectArray< float > m_perClothDampingFactor;
444
btDX11Buffer<float> m_dx11PerClothDampingFactor;
446
/** Velocity correction coefficient */
447
btAlignedObjectArray< float > m_perClothVelocityCorrectionCoefficient;
448
btDX11Buffer<float> m_dx11PerClothVelocityCorrectionCoefficient;
450
/** Lift parameter for wind effect on cloth. */
451
btAlignedObjectArray< float > m_perClothLiftFactor;
452
btDX11Buffer<float> m_dx11PerClothLiftFactor;
454
/** Drag parameter for wind effect on cloth. */
455
btAlignedObjectArray< float > m_perClothDragFactor;
456
btDX11Buffer<float> m_dx11PerClothDragFactor;
458
/** Density of the medium in which each cloth sits */
459
btAlignedObjectArray< float > m_perClothMediumDensity;
460
btDX11Buffer<float> m_dx11PerClothMediumDensity;
464
* Collision shape details: pair of index of first collision shape for the cloth and number of collision objects.
466
btAlignedObjectArray< CollisionObjectIndices > m_perClothCollisionObjects;
467
btDX11Buffer<CollisionObjectIndices> m_dx11PerClothCollisionObjects;
470
* Collision shapes being passed across to the cloths in this solver.
472
btAlignedObjectArray< CollisionShapeDescription > m_collisionObjectDetails;
473
btDX11Buffer< CollisionShapeDescription > m_dx11CollisionObjectDetails;
476
* Minimum bounds for each cloth.
477
* Updated by GPU and returned for use by broad phase.
478
* These are int vectors as a reminder that they store the int representation of a float, not a float.
479
* Bit 31 is inverted - is floats are stored with int-sortable values.
481
btAlignedObjectArray< UIntVector3 > m_perClothMinBounds;
482
btDX11Buffer< UIntVector3 > m_dx11PerClothMinBounds;
485
* Maximum bounds for each cloth.
486
* Updated by GPU and returned for use by broad phase.
487
* These are int vectors as a reminder that they store the int representation of a float, not a float.
488
* Bit 31 is inverted - is floats are stored with int-sortable values.
490
btAlignedObjectArray< UIntVector3 > m_perClothMaxBounds;
491
btDX11Buffer< UIntVector3 > m_dx11PerClothMaxBounds;
495
* Friction coefficient for each cloth
497
btAlignedObjectArray< float > m_perClothFriction;
498
btDX11Buffer< float > m_dx11PerClothFriction;
500
DXFunctions::KernelDesc prepareLinksKernel;
501
DXFunctions::KernelDesc solvePositionsFromLinksKernel;
502
DXFunctions::KernelDesc vSolveLinksKernel;
503
DXFunctions::KernelDesc integrateKernel;
504
DXFunctions::KernelDesc addVelocityKernel;
505
DXFunctions::KernelDesc updatePositionsFromVelocitiesKernel;
506
DXFunctions::KernelDesc updateVelocitiesFromPositionsWithoutVelocitiesKernel;
507
DXFunctions::KernelDesc updateVelocitiesFromPositionsWithVelocitiesKernel;
508
DXFunctions::KernelDesc solveCollisionsAndUpdateVelocitiesKernel;
509
DXFunctions::KernelDesc resetNormalsAndAreasKernel;
510
DXFunctions::KernelDesc normalizeNormalsAndAreasKernel;
511
DXFunctions::KernelDesc computeBoundsKernel;
512
DXFunctions::KernelDesc updateSoftBodiesKernel;
514
DXFunctions::KernelDesc applyForcesKernel;
518
* Integrate motion on the solver.
520
virtual void integrate( float solverdt );
521
float computeTriangleArea(
522
const Vectormath::Aos::Point3 &vertex0,
523
const Vectormath::Aos::Point3 &vertex1,
524
const Vectormath::Aos::Point3 &vertex2 );
527
virtual bool buildShaders();
529
void resetNormalsAndAreas( int numVertices );
531
void normalizeNormalsAndAreas( int numVertices );
533
void executeUpdateSoftBodies( int firstTriangle, int numTriangles );
535
void prepareCollisionConstraints();
537
Vectormath::Aos::Vector3 ProjectOnAxis( const Vectormath::Aos::Vector3 &v, const Vectormath::Aos::Vector3 &a );
539
void ApplyClampedForce( float solverdt, const Vectormath::Aos::Vector3 &force, const Vectormath::Aos::Vector3 &vertexVelocity, float inverseMass, Vectormath::Aos::Vector3 &vertexForce );
541
virtual void applyForces( float solverdt );
543
virtual void updateConstants( float timeStep );
544
int findSoftBodyIndex( const btSoftBody* const softBody );
546
//////////////////////////////////////
548
virtual void prepareLinks();
550
void updatePositionsFromVelocities( float solverdt );
551
void solveLinksForPosition( int startLink, int numLinks, float kst, float ti );
552
void solveLinksForVelocity( int startLink, int numLinks, float kst );
554
void updateVelocitiesFromPositionsWithVelocities( float isolverdt );
555
void updateVelocitiesFromPositionsWithoutVelocities( float isolverdt );
556
void computeBounds( );
557
void solveCollisionsAndUpdateVelocities( float isolverdt );
559
// End kernel dispatches
560
/////////////////////////////////////
565
void releaseKernels();
568
btDX11SoftBodySolver(ID3D11Device * dx11Device, ID3D11DeviceContext* dx11Context, DXFunctions::CompileFromMemoryFunc dx11CompileFromMemory = &D3DX11CompileFromMemory);
570
virtual ~btDX11SoftBodySolver();
573
virtual SolverTypes getSolverType() const
579
virtual btSoftBodyLinkData &getLinkData();
581
virtual btSoftBodyVertexData &getVertexData();
583
virtual btSoftBodyTriangleData &getTriangleData();
589
btAcceleratedSoftBodyInterface *findSoftBodyInterface( const btSoftBody* const softBody );
590
const btAcceleratedSoftBodyInterface * const findSoftBodyInterface( const btSoftBody* const softBody ) const;
592
virtual bool checkInitialized();
594
virtual void updateSoftBodies( );
596
virtual void optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate=false);
598
virtual void copyBackToSoftBodies();
600
virtual void solveConstraints( float solverdt );
602
virtual void predictMotion( float solverdt );
605
virtual void processCollision( btSoftBody *, btCollisionObject* );
607
virtual void processCollision( btSoftBody*, btSoftBody* );
614
* Class to manage movement of data from a solver to a given target.
615
* This version is the DX to CPU version.
617
class btSoftBodySolverOutputDXtoCPU : public btSoftBodySolverOutput
622
btSoftBodySolverOutputDXtoCPU()
626
/** Output current computed vertex data to the vertex buffers for all cloths in the solver. */
627
virtual void copySoftBodyToVertexBuffer( const btSoftBody * const softBody, btVertexBufferDescriptor *vertexBuffer );
631
* Class to manage movement of data from a solver to a given target.
632
* This version is the DX to DX version and subclasses DX to CPU so that it works for that too.
634
class btSoftBodySolverOutputDXtoDX : public btSoftBodySolverOutputDXtoCPU
637
struct OutputToVertexArrayCB
650
DXFunctions dxFunctions;
651
DXFunctions::KernelDesc outputToVertexArrayWithNormalsKernel;
652
DXFunctions::KernelDesc outputToVertexArrayWithoutNormalsKernel;
655
bool m_shadersInitialized;
657
bool checkInitialized();
659
void releaseKernels();
662
btSoftBodySolverOutputDXtoDX(ID3D11Device *dx11Device, ID3D11DeviceContext* dx11Context, DXFunctions::CompileFromMemoryFunc dx11CompileFromMemory = &D3DX11CompileFromMemory) :
663
dxFunctions( dx11Device, dx11Context, dx11CompileFromMemory )
665
m_shadersInitialized = false;
668
~btSoftBodySolverOutputDXtoDX()
673
/** Output current computed vertex data to the vertex buffers for all cloths in the solver. */
674
virtual void copySoftBodyToVertexBuffer( const btSoftBody * const softBody, btVertexBufferDescriptor *vertexBuffer );
677
#endif // #ifndef BT_ACCELERATED_SOFT_BODY_DX11_SOLVER_H