00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef BT_ACCELERATED_SOFT_BODY_DX11_SOLVER_H
00017 #define BT_ACCELERATED_SOFT_BODY_DX11_SOLVER_H
00018
00019
00020 #include "vectormath/vmInclude.h"
00021 #include "BulletSoftBody/btSoftBodySolvers.h"
00022 #include "btSoftBodySolverVertexBuffer_DX11.h"
00023 #include "btSoftBodySolverLinkData_DX11.h"
00024 #include "btSoftBodySolverVertexData_DX11.h"
00025 #include "btSoftBodySolverTriangleData_DX11.h"
00026
00027
00028
00029 class DXFunctions
00030 {
00031 public:
00032
00033 typedef HRESULT (WINAPI * CompileFromMemoryFunc)(LPCSTR,SIZE_T,LPCSTR,const D3D10_SHADER_MACRO*,LPD3D10INCLUDE,LPCSTR,LPCSTR,UINT,UINT,ID3DX11ThreadPump*,ID3D10Blob**,ID3D10Blob**,HRESULT*);
00034
00035 ID3D11Device * m_dx11Device;
00036 ID3D11DeviceContext* m_dx11Context;
00037 CompileFromMemoryFunc m_dx11CompileFromMemory;
00038
00039 DXFunctions(ID3D11Device *dx11Device, ID3D11DeviceContext* dx11Context, CompileFromMemoryFunc dx11CompileFromMemory) :
00040 m_dx11Device( dx11Device ),
00041 m_dx11Context( dx11Context ),
00042 m_dx11CompileFromMemory( dx11CompileFromMemory )
00043 {
00044
00045 }
00046
00047 class KernelDesc
00048 {
00049 protected:
00050
00051
00052 public:
00053 ID3D11ComputeShader* kernel;
00054 ID3D11Buffer* constBuffer;
00055
00056 KernelDesc()
00057 {
00058 kernel = 0;
00059 constBuffer = 0;
00060 }
00061
00062 virtual ~KernelDesc()
00063 {
00064
00065
00066 }
00067 };
00068
00072 KernelDesc compileComputeShaderFromString( const char* shaderString, const char* shaderName, int constBufferSize, D3D10_SHADER_MACRO *compileMacros = 0 );
00073
00074 };
00075
00076 class btDX11SoftBodySolver : public btSoftBodySolver
00077 {
00078 protected:
00083 struct CollisionShapeDescription
00084 {
00085 Vectormath::Aos::Transform3 shapeTransform;
00086 Vectormath::Aos::Vector3 linearVelocity;
00087 Vectormath::Aos::Vector3 angularVelocity;
00088
00089 int softBodyIdentifier;
00090 int collisionShapeType;
00091
00092
00093 float radius;
00094 float halfHeight;
00095
00096 float margin;
00097 float friction;
00098
00099 CollisionShapeDescription()
00100 {
00101 collisionShapeType = 0;
00102 margin = 0;
00103 friction = 0;
00104 }
00105 };
00106
00107 struct UIntVector3
00108 {
00109 UIntVector3()
00110 {
00111 x = 0;
00112 y = 0;
00113 z = 0;
00114 _padding = 0;
00115 }
00116
00117 UIntVector3( unsigned int x_, unsigned int y_, unsigned int z_ )
00118 {
00119 x = x_;
00120 y = y_;
00121 z = z_;
00122 _padding = 0;
00123 }
00124
00125 unsigned int x;
00126 unsigned int y;
00127 unsigned int z;
00128 unsigned int _padding;
00129 };
00130
00131
00132
00133 public:
00139 class btAcceleratedSoftBodyInterface
00140 {
00141 protected:
00143 int m_numVertices;
00145 int m_maxVertices;
00147 int m_numTriangles;
00149 int m_maxTriangles;
00151 int m_firstVertex;
00153 int m_firstTriangle;
00155 int m_firstLink;
00157 int m_maxLinks;
00159 int m_numLinks;
00160
00162 btSoftBody *m_softBody;
00163
00164
00165 public:
00166 btAcceleratedSoftBodyInterface( btSoftBody *softBody ) :
00167 m_softBody( softBody )
00168 {
00169 m_numVertices = 0;
00170 m_maxVertices = 0;
00171 m_numTriangles = 0;
00172 m_maxTriangles = 0;
00173 m_firstVertex = 0;
00174 m_firstTriangle = 0;
00175 m_firstLink = 0;
00176 m_maxLinks = 0;
00177 m_numLinks = 0;
00178 }
00179 int getNumVertices() const
00180 {
00181 return m_numVertices;
00182 }
00183
00184 int getNumTriangles() const
00185 {
00186 return m_numTriangles;
00187 }
00188
00189 int getMaxVertices() const
00190 {
00191 return m_maxVertices;
00192 }
00193
00194 int getMaxTriangles() const
00195 {
00196 return m_maxTriangles;
00197 }
00198
00199 int getFirstVertex() const
00200 {
00201 return m_firstVertex;
00202 }
00203
00204 int getFirstTriangle() const
00205 {
00206 return m_firstTriangle;
00207 }
00208
00209
00213 void updateBounds( const btVector3 &lowerBound, const btVector3 &upperBound );
00214
00215
00216
00217
00218
00219 void setNumVertices( int numVertices )
00220 {
00221 m_numVertices = numVertices;
00222 }
00223
00224 void setNumTriangles( int numTriangles )
00225 {
00226 m_numTriangles = numTriangles;
00227 }
00228
00229 void setMaxVertices( int maxVertices )
00230 {
00231 m_maxVertices = maxVertices;
00232 }
00233
00234 void setMaxTriangles( int maxTriangles )
00235 {
00236 m_maxTriangles = maxTriangles;
00237 }
00238
00239 void setFirstVertex( int firstVertex )
00240 {
00241 m_firstVertex = firstVertex;
00242 }
00243
00244 void setFirstTriangle( int firstTriangle )
00245 {
00246 m_firstTriangle = firstTriangle;
00247 }
00248
00249 void setMaxLinks( int maxLinks )
00250 {
00251 m_maxLinks = maxLinks;
00252 }
00253
00254 void setNumLinks( int numLinks )
00255 {
00256 m_numLinks = numLinks;
00257 }
00258
00259 void setFirstLink( int firstLink )
00260 {
00261 m_firstLink = firstLink;
00262 }
00263
00264 int getMaxLinks()
00265 {
00266 return m_maxLinks;
00267 }
00268
00269 int getNumLinks()
00270 {
00271 return m_numLinks;
00272 }
00273
00274 int getFirstLink()
00275 {
00276 return m_firstLink;
00277 }
00278
00279 btSoftBody* getSoftBody()
00280 {
00281 return m_softBody;
00282 }
00283
00284 };
00285
00286
00287 struct CollisionObjectIndices
00288 {
00289 CollisionObjectIndices( int f, int e )
00290 {
00291 firstObject = f;
00292 endObject = e;
00293 }
00294
00295 int firstObject;
00296 int endObject;
00297 };
00298
00299
00300
00301
00302
00303 struct PrepareLinksCB
00304 {
00305 int numLinks;
00306 int padding0;
00307 int padding1;
00308 int padding2;
00309 };
00310
00311 struct SolvePositionsFromLinksKernelCB
00312 {
00313 int startLink;
00314 int numLinks;
00315 float kst;
00316 float ti;
00317 };
00318
00319 struct IntegrateCB
00320 {
00321 int numNodes;
00322 float solverdt;
00323 int padding1;
00324 int padding2;
00325 };
00326
00327 struct UpdatePositionsFromVelocitiesCB
00328 {
00329 int numNodes;
00330 float solverSDT;
00331 int padding1;
00332 int padding2;
00333 };
00334
00335 struct UpdateVelocitiesFromPositionsWithoutVelocitiesCB
00336 {
00337 int numNodes;
00338 float isolverdt;
00339 int padding1;
00340 int padding2;
00341 };
00342
00343 struct UpdateVelocitiesFromPositionsWithVelocitiesCB
00344 {
00345 int numNodes;
00346 float isolverdt;
00347 int padding1;
00348 int padding2;
00349 };
00350
00351 struct UpdateSoftBodiesCB
00352 {
00353 int numNodes;
00354 int startFace;
00355 int numFaces;
00356 float epsilon;
00357 };
00358
00359
00360 struct ApplyForcesCB
00361 {
00362 unsigned int numNodes;
00363 float solverdt;
00364 float epsilon;
00365 int padding3;
00366 };
00367
00368 struct AddVelocityCB
00369 {
00370 int startNode;
00371 int lastNode;
00372 float velocityX;
00373 float velocityY;
00374 float velocityZ;
00375 int padding1;
00376 int padding2;
00377 int padding3;
00378 };
00379
00380 struct VSolveLinksCB
00381 {
00382 int startLink;
00383 int numLinks;
00384 float kst;
00385 int padding;
00386 };
00387
00388 struct ComputeBoundsCB
00389 {
00390 int numNodes;
00391 int numSoftBodies;
00392 int padding1;
00393 int padding2;
00394 };
00395
00396 struct SolveCollisionsAndUpdateVelocitiesCB
00397 {
00398 unsigned int numNodes;
00399 float isolverdt;
00400 int padding0;
00401 int padding1;
00402 };
00403
00404
00405
00406
00407 protected:
00408 ID3D11Device * m_dx11Device;
00409 ID3D11DeviceContext* m_dx11Context;
00410
00411 DXFunctions dxFunctions;
00412 public:
00414 btSoftBodyLinkDataDX11 m_linkData;
00415 btSoftBodyVertexDataDX11 m_vertexData;
00416 btSoftBodyTriangleDataDX11 m_triangleData;
00417
00418 protected:
00419
00421 bool m_updateSolverConstants;
00422
00423 bool m_shadersInitialized;
00424
00429 btAlignedObjectArray< btAcceleratedSoftBodyInterface * > m_softBodySet;
00430
00434 btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothAcceleration;
00435 btDX11Buffer<Vectormath::Aos::Vector3> m_dx11PerClothAcceleration;
00436
00440 btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothWindVelocity;
00441 btDX11Buffer<Vectormath::Aos::Vector3> m_dx11PerClothWindVelocity;
00442
00444 btAlignedObjectArray< float > m_perClothDampingFactor;
00445 btDX11Buffer<float> m_dx11PerClothDampingFactor;
00446
00448 btAlignedObjectArray< float > m_perClothVelocityCorrectionCoefficient;
00449 btDX11Buffer<float> m_dx11PerClothVelocityCorrectionCoefficient;
00450
00452 btAlignedObjectArray< float > m_perClothLiftFactor;
00453 btDX11Buffer<float> m_dx11PerClothLiftFactor;
00454
00456 btAlignedObjectArray< float > m_perClothDragFactor;
00457 btDX11Buffer<float> m_dx11PerClothDragFactor;
00458
00460 btAlignedObjectArray< float > m_perClothMediumDensity;
00461 btDX11Buffer<float> m_dx11PerClothMediumDensity;
00462
00463
00467 btAlignedObjectArray< CollisionObjectIndices > m_perClothCollisionObjects;
00468 btDX11Buffer<CollisionObjectIndices> m_dx11PerClothCollisionObjects;
00469
00473 btAlignedObjectArray< CollisionShapeDescription > m_collisionObjectDetails;
00474 btDX11Buffer< CollisionShapeDescription > m_dx11CollisionObjectDetails;
00475
00482 btAlignedObjectArray< UIntVector3 > m_perClothMinBounds;
00483 btDX11Buffer< UIntVector3 > m_dx11PerClothMinBounds;
00484
00491 btAlignedObjectArray< UIntVector3 > m_perClothMaxBounds;
00492 btDX11Buffer< UIntVector3 > m_dx11PerClothMaxBounds;
00493
00494
00498 btAlignedObjectArray< float > m_perClothFriction;
00499 btDX11Buffer< float > m_dx11PerClothFriction;
00500
00501 DXFunctions::KernelDesc prepareLinksKernel;
00502 DXFunctions::KernelDesc solvePositionsFromLinksKernel;
00503 DXFunctions::KernelDesc vSolveLinksKernel;
00504 DXFunctions::KernelDesc integrateKernel;
00505 DXFunctions::KernelDesc addVelocityKernel;
00506 DXFunctions::KernelDesc updatePositionsFromVelocitiesKernel;
00507 DXFunctions::KernelDesc updateVelocitiesFromPositionsWithoutVelocitiesKernel;
00508 DXFunctions::KernelDesc updateVelocitiesFromPositionsWithVelocitiesKernel;
00509 DXFunctions::KernelDesc solveCollisionsAndUpdateVelocitiesKernel;
00510 DXFunctions::KernelDesc resetNormalsAndAreasKernel;
00511 DXFunctions::KernelDesc normalizeNormalsAndAreasKernel;
00512 DXFunctions::KernelDesc computeBoundsKernel;
00513 DXFunctions::KernelDesc updateSoftBodiesKernel;
00514
00515 DXFunctions::KernelDesc applyForcesKernel;
00516
00517 bool m_enableUpdateBounds;
00518
00522 virtual void integrate( float solverdt );
00523 float computeTriangleArea(
00524 const Vectormath::Aos::Point3 &vertex0,
00525 const Vectormath::Aos::Point3 &vertex1,
00526 const Vectormath::Aos::Point3 &vertex2 );
00527
00528
00529 virtual bool buildShaders();
00530
00531 void resetNormalsAndAreas( int numVertices );
00532
00533 void normalizeNormalsAndAreas( int numVertices );
00534
00535 void executeUpdateSoftBodies( int firstTriangle, int numTriangles );
00536
00537 void prepareCollisionConstraints();
00538
00539 Vectormath::Aos::Vector3 ProjectOnAxis( const Vectormath::Aos::Vector3 &v, const Vectormath::Aos::Vector3 &a );
00540
00541 void ApplyClampedForce( float solverdt, const Vectormath::Aos::Vector3 &force, const Vectormath::Aos::Vector3 &vertexVelocity, float inverseMass, Vectormath::Aos::Vector3 &vertexForce );
00542
00543 virtual void applyForces( float solverdt );
00544
00545 virtual void updateConstants( float timeStep );
00546 int findSoftBodyIndex( const btSoftBody* const softBody );
00547
00549
00550 virtual void prepareLinks();
00551
00552 void updatePositionsFromVelocities( float solverdt );
00553 void solveLinksForPosition( int startLink, int numLinks, float kst, float ti );
00554 void solveLinksForVelocity( int startLink, int numLinks, float kst );
00555
00556 void updateVelocitiesFromPositionsWithVelocities( float isolverdt );
00557 void updateVelocitiesFromPositionsWithoutVelocities( float isolverdt );
00558 void computeBounds( );
00559 void solveCollisionsAndUpdateVelocities( float isolverdt );
00560
00561
00563
00564 void updateBounds();
00565
00566
00567 void releaseKernels();
00568
00569 public:
00570 btDX11SoftBodySolver(ID3D11Device * dx11Device, ID3D11DeviceContext* dx11Context, DXFunctions::CompileFromMemoryFunc dx11CompileFromMemory = &D3DX11CompileFromMemory);
00571
00572 virtual ~btDX11SoftBodySolver();
00573
00574
00575 virtual SolverTypes getSolverType() const
00576 {
00577 return DX_SOLVER;
00578 }
00579
00580 void setEnableUpdateBounds(bool enableBounds)
00581 {
00582 m_enableUpdateBounds = enableBounds;
00583 }
00584 bool getEnableUpdateBounds() const
00585 {
00586 return m_enableUpdateBounds;
00587 }
00588
00589
00590
00591 virtual btSoftBodyLinkData &getLinkData();
00592
00593 virtual btSoftBodyVertexData &getVertexData();
00594
00595 virtual btSoftBodyTriangleData &getTriangleData();
00596
00597
00598
00599
00600
00601 btAcceleratedSoftBodyInterface *findSoftBodyInterface( const btSoftBody* const softBody );
00602 const btAcceleratedSoftBodyInterface * const findSoftBodyInterface( const btSoftBody* const softBody ) const;
00603
00604 virtual bool checkInitialized();
00605
00606 virtual void updateSoftBodies( );
00607
00608 virtual void optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate=false);
00609
00610 virtual void copyBackToSoftBodies(bool bMove = true);
00611
00612 virtual void solveConstraints( float solverdt );
00613
00614 virtual void predictMotion( float solverdt );
00615
00616
00617 virtual void processCollision( btSoftBody *, const btCollisionObjectWrapper* );
00618
00619 virtual void processCollision( btSoftBody*, btSoftBody* );
00620
00621 };
00622
00623
00624
00629 class btSoftBodySolverOutputDXtoCPU : public btSoftBodySolverOutput
00630 {
00631 protected:
00632
00633 public:
00634 btSoftBodySolverOutputDXtoCPU()
00635 {
00636 }
00637
00639 virtual void copySoftBodyToVertexBuffer( const btSoftBody * const softBody, btVertexBufferDescriptor *vertexBuffer );
00640 };
00641
00646 class btSoftBodySolverOutputDXtoDX : public btSoftBodySolverOutputDXtoCPU
00647 {
00648 protected:
00649 struct OutputToVertexArrayCB
00650 {
00651 int startNode;
00652 int numNodes;
00653 int positionOffset;
00654 int positionStride;
00655
00656 int normalOffset;
00657 int normalStride;
00658 int padding1;
00659 int padding2;
00660 };
00661
00662 DXFunctions dxFunctions;
00663 DXFunctions::KernelDesc outputToVertexArrayWithNormalsKernel;
00664 DXFunctions::KernelDesc outputToVertexArrayWithoutNormalsKernel;
00665
00666
00667 bool m_shadersInitialized;
00668
00669 bool checkInitialized();
00670 bool buildShaders();
00671 void releaseKernels();
00672
00673 public:
00674 btSoftBodySolverOutputDXtoDX(ID3D11Device *dx11Device, ID3D11DeviceContext* dx11Context, DXFunctions::CompileFromMemoryFunc dx11CompileFromMemory = &D3DX11CompileFromMemory) :
00675 dxFunctions( dx11Device, dx11Context, dx11CompileFromMemory )
00676 {
00677 m_shadersInitialized = false;
00678 }
00679
00680 ~btSoftBodySolverOutputDXtoDX()
00681 {
00682 releaseKernels();
00683 }
00684
00686 virtual void copySoftBodyToVertexBuffer( const btSoftBody * const softBody, btVertexBufferDescriptor *vertexBuffer );
00687 };
00688
00689 #endif // #ifndef BT_ACCELERATED_SOFT_BODY_DX11_SOLVER_H
00690
00691