~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/bullet/src/BulletMultiThreaded/GpuSoftBodySolvers/OpenCL/btSoftBodySolver_OpenCL.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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"
 
27
 
 
28
class CLFunctions
 
29
{
 
30
protected:
 
31
        cl_command_queue        m_cqCommandQue;
 
32
        cl_context                      m_cxMainContext;
 
33
        
 
34
public:
 
35
        CLFunctions(cl_command_queue cqCommandQue, cl_context cxMainContext) :
 
36
                m_cqCommandQue( cqCommandQue ),
 
37
                m_cxMainContext( cxMainContext )
 
38
        {
 
39
        }
 
40
 
 
41
 
 
42
        /**
 
43
         * Compile a compute shader kernel from a string and return the appropriate cl_kernel object.
 
44
         */     
 
45
        cl_kernel compileCLKernelFromString( const char* kernelSource, const char* kernelName, const char* additionalMacros = "" );
 
46
};
 
47
 
 
48
/**
 
49
 * Entry in the collision shape array.
 
50
 * Specifies the shape type, the transform matrix and the necessary details of the collisionShape.
 
51
 */
 
52
struct CollisionShapeDescription
 
53
{
 
54
        Vectormath::Aos::Transform3 shapeTransform;
 
55
        Vectormath::Aos::Vector3 linearVelocity;
 
56
        Vectormath::Aos::Vector3 angularVelocity;
 
57
 
 
58
        int softBodyIdentifier;
 
59
        int collisionShapeType;
 
60
 
 
61
        // Both needed for capsule
 
62
        float radius;
 
63
        float halfHeight;
 
64
        int upAxis;
 
65
        
 
66
        float margin;
 
67
        float friction;
 
68
 
 
69
        CollisionShapeDescription()
 
70
        {
 
71
                collisionShapeType = 0;
 
72
                margin = 0;
 
73
                friction = 0;
 
74
        }
 
75
};
 
76
 
 
77
/**
 
78
         * SoftBody class to maintain information about a soft body instance
 
79
         * within a solver.
 
80
         * This data addresses the main solver arrays.
 
81
         */
 
82
class btOpenCLAcceleratedSoftBodyInterface
 
83
{
 
84
protected:
 
85
        /** Current number of vertices that are part of this cloth */
 
86
        int m_numVertices;
 
87
        /** Maximum number of vertices allocated to be part of this cloth */
 
88
        int m_maxVertices;
 
89
        /** Current number of triangles that are part of this cloth */
 
90
        int m_numTriangles;
 
91
        /** Maximum number of triangles allocated to be part of this cloth */
 
92
        int m_maxTriangles;
 
93
        /** Index of first vertex in the world allocated to this cloth */
 
94
        int m_firstVertex;
 
95
        /** Index of first triangle in the world allocated to this cloth */
 
96
        int m_firstTriangle;
 
97
        /** Index of first link in the world allocated to this cloth */
 
98
        int m_firstLink;
 
99
        /** Maximum number of links allocated to this cloth */
 
100
        int m_maxLinks;
 
101
        /** Current number of links allocated to this cloth */
 
102
        int m_numLinks;
 
103
 
 
104
        /** The actual soft body this data represents */
 
105
        btSoftBody *m_softBody;
 
106
 
 
107
 
 
108
public:
 
109
        btOpenCLAcceleratedSoftBodyInterface( btSoftBody *softBody ) :
 
110
          m_softBody( softBody )
 
111
        {
 
112
                m_numVertices = 0;
 
113
                m_maxVertices = 0;
 
114
                m_numTriangles = 0;
 
115
                m_maxTriangles = 0;
 
116
                m_firstVertex = 0;
 
117
                m_firstTriangle = 0;
 
118
                m_firstLink = 0;
 
119
                m_maxLinks = 0;
 
120
                m_numLinks = 0;
 
121
        }
 
122
        int getNumVertices()
 
123
        {
 
124
                return m_numVertices;
 
125
        }
 
126
 
 
127
        int getNumTriangles()
 
128
        {
 
129
                return m_numTriangles;
 
130
        }
 
131
 
 
132
        int getMaxVertices()
 
133
        {
 
134
                return m_maxVertices;
 
135
        }
 
136
 
 
137
        int getMaxTriangles()
 
138
        {
 
139
                return m_maxTriangles;
 
140
        }
 
141
 
 
142
        int getFirstVertex()
 
143
        {
 
144
                return m_firstVertex;
 
145
        }
 
146
 
 
147
        int getFirstTriangle()
 
148
        {
 
149
                return m_firstTriangle;
 
150
        }
 
151
        
 
152
        /**
 
153
         * Update the bounds in the btSoftBody object
 
154
         */
 
155
        void updateBounds( const btVector3 &lowerBound, const btVector3 &upperBound );
 
156
 
 
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 )
 
161
        {
 
162
                m_numVertices = numVertices;
 
163
        }       
 
164
 
 
165
        void setNumTriangles( int numTriangles )
 
166
        {
 
167
                m_numTriangles = numTriangles;
 
168
        }
 
169
 
 
170
        void setMaxVertices( int maxVertices )
 
171
        {
 
172
                m_maxVertices = maxVertices;
 
173
        }
 
174
 
 
175
        void setMaxTriangles( int maxTriangles )
 
176
        {
 
177
                m_maxTriangles = maxTriangles;
 
178
        }
 
179
 
 
180
        void setFirstVertex( int firstVertex )
 
181
        {
 
182
                m_firstVertex = firstVertex;
 
183
        }
 
184
 
 
185
        void setFirstTriangle( int firstTriangle )
 
186
        {
 
187
                m_firstTriangle = firstTriangle;
 
188
        }
 
189
 
 
190
        void setMaxLinks( int maxLinks )
 
191
        {
 
192
                m_maxLinks = maxLinks;
 
193
        }
 
194
 
 
195
        void setNumLinks( int numLinks )
 
196
        {
 
197
                m_numLinks = numLinks;
 
198
        }
 
199
 
 
200
        void setFirstLink( int firstLink )
 
201
        {
 
202
                m_firstLink = firstLink;
 
203
        }
 
204
 
 
205
        int getMaxLinks()
 
206
        {
 
207
                return m_maxLinks;
 
208
        }
 
209
 
 
210
        int getNumLinks()
 
211
        {
 
212
                return m_numLinks;
 
213
        }
 
214
 
 
215
        int getFirstLink()
 
216
        {
 
217
                return m_firstLink;
 
218
        }
 
219
 
 
220
        btSoftBody* getSoftBody()
 
221
        {
 
222
                return m_softBody;
 
223
        }
 
224
 
 
225
};
 
226
 
 
227
 
 
228
 
 
229
class btOpenCLSoftBodySolver : public btSoftBodySolver
 
230
{
 
231
public:
 
232
        
 
233
 
 
234
        struct UIntVector3
 
235
        {
 
236
                UIntVector3()
 
237
                {
 
238
                        x = 0;
 
239
                        y = 0;
 
240
                        z = 0;
 
241
                        _padding = 0;
 
242
                }
 
243
                
 
244
                UIntVector3( unsigned int x_, unsigned int y_, unsigned int z_ )
 
245
                {
 
246
                        x = x_;
 
247
                        y = y_;
 
248
                        z = z_;
 
249
                        _padding = 0;
 
250
                }
 
251
                        
 
252
                unsigned int x;
 
253
                unsigned int y;
 
254
                unsigned int z;
 
255
                unsigned int _padding;
 
256
        };
 
257
 
 
258
        struct CollisionObjectIndices
 
259
        {
 
260
                CollisionObjectIndices( int f, int e )
 
261
                {
 
262
                        firstObject = f;
 
263
                        endObject = e;
 
264
                }
 
265
 
 
266
                int firstObject;
 
267
                int endObject;
 
268
        };
 
269
 
 
270
        btSoftBodyLinkDataOpenCL m_linkData;
 
271
        btSoftBodyVertexDataOpenCL m_vertexData;
 
272
        btSoftBodyTriangleDataOpenCL m_triangleData;
 
273
 
 
274
protected:
 
275
 
 
276
        CLFunctions clFunctions;
 
277
 
 
278
        /** Variable to define whether we need to update solver constants on the next iteration */
 
279
        bool m_updateSolverConstants;
 
280
 
 
281
        bool m_shadersInitialized;
 
282
 
 
283
        /** 
 
284
         * Cloths owned by this solver.
 
285
         * Only our cloths are in this array.
 
286
         */
 
287
        btAlignedObjectArray< btOpenCLAcceleratedSoftBodyInterface * > m_softBodySet;
 
288
 
 
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. 
 
291
         */
 
292
        btAlignedObjectArray< Vectormath::Aos::Vector3 >        m_perClothAcceleration;
 
293
        btOpenCLBuffer<Vectormath::Aos::Vector3>                        m_clPerClothAcceleration;
 
294
 
 
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. 
 
297
         */
 
298
        btAlignedObjectArray< Vectormath::Aos::Vector3 >        m_perClothWindVelocity;
 
299
        btOpenCLBuffer<Vectormath::Aos::Vector3>                        m_clPerClothWindVelocity;
 
300
 
 
301
        /** Velocity damping factor */
 
302
        btAlignedObjectArray< float >                                           m_perClothDampingFactor;
 
303
        btOpenCLBuffer<float>                                                           m_clPerClothDampingFactor;
 
304
 
 
305
        /** Velocity correction coefficient */
 
306
        btAlignedObjectArray< float >                                           m_perClothVelocityCorrectionCoefficient;
 
307
        btOpenCLBuffer<float>                                                           m_clPerClothVelocityCorrectionCoefficient;
 
308
 
 
309
        /** Lift parameter for wind effect on cloth. */
 
310
        btAlignedObjectArray< float >                                           m_perClothLiftFactor;
 
311
        btOpenCLBuffer<float>                                                           m_clPerClothLiftFactor;
 
312
        
 
313
        /** Drag parameter for wind effect on cloth. */
 
314
        btAlignedObjectArray< float >                                           m_perClothDragFactor;
 
315
        btOpenCLBuffer<float>                                                           m_clPerClothDragFactor;
 
316
 
 
317
        /** Density of the medium in which each cloth sits */
 
318
        btAlignedObjectArray< float >                                           m_perClothMediumDensity;
 
319
        btOpenCLBuffer<float>                                                           m_clPerClothMediumDensity;
 
320
 
 
321
        /** 
 
322
         * Collision shape details: pair of index of first collision shape for the cloth and number of collision objects.
 
323
         */
 
324
        btAlignedObjectArray< CollisionObjectIndices >          m_perClothCollisionObjects;
 
325
        btOpenCLBuffer<CollisionObjectIndices>                          m_clPerClothCollisionObjects;
 
326
 
 
327
        /** 
 
328
         * Collision shapes being passed across to the cloths in this solver.
 
329
         */
 
330
        btAlignedObjectArray< CollisionShapeDescription >       m_collisionObjectDetails;
 
331
        btOpenCLBuffer< CollisionShapeDescription >                     m_clCollisionObjectDetails;
 
332
 
 
333
 
 
334
        
 
335
        /** 
 
336
         * Friction coefficient for each cloth
 
337
         */
 
338
        btAlignedObjectArray< float >   m_perClothFriction;
 
339
        btOpenCLBuffer< float >                 m_clPerClothFriction;
 
340
 
 
341
 
 
342
 
 
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;
 
356
 
 
357
        cl_kernel               outputToVertexArrayKernel;
 
358
        cl_kernel               applyForcesKernel;
 
359
 
 
360
        cl_command_queue        m_cqCommandQue;
 
361
        cl_context                      m_cxMainContext;
 
362
        
 
363
        size_t                          m_defaultWorkGroupSize;
 
364
 
 
365
 
 
366
        virtual bool buildShaders();
 
367
 
 
368
        void resetNormalsAndAreas( int numVertices );
 
369
 
 
370
        void normalizeNormalsAndAreas( int numVertices );
 
371
 
 
372
        void executeUpdateSoftBodies( int firstTriangle, int numTriangles );
 
373
 
 
374
        void prepareCollisionConstraints();
 
375
        
 
376
        Vectormath::Aos::Vector3 ProjectOnAxis( const Vectormath::Aos::Vector3 &v, const Vectormath::Aos::Vector3 &a );
 
377
 
 
378
        void ApplyClampedForce( float solverdt, const Vectormath::Aos::Vector3 &force, const Vectormath::Aos::Vector3 &vertexVelocity, float inverseMass, Vectormath::Aos::Vector3 &vertexForce );
 
379
        
 
380
 
 
381
        int findSoftBodyIndex( const btSoftBody* const softBody );
 
382
 
 
383
        virtual void applyForces( float solverdt );
 
384
 
 
385
        /**
 
386
         * Integrate motion on the solver.
 
387
         */
 
388
        virtual void integrate( float solverdt );
 
389
 
 
390
        virtual void updateConstants( float timeStep );
 
391
 
 
392
        float computeTriangleArea( 
 
393
                const Vectormath::Aos::Point3 &vertex0,
 
394
                const Vectormath::Aos::Point3 &vertex1,
 
395
                const Vectormath::Aos::Point3 &vertex2 );
 
396
 
 
397
 
 
398
        //////////////////////////////////////
 
399
        // Kernel dispatches
 
400
        void prepareLinks();
 
401
 
 
402
        void solveLinksForVelocity( int startLink, int numLinks, float kst );
 
403
 
 
404
        void updatePositionsFromVelocities( float solverdt );
 
405
 
 
406
        virtual void solveLinksForPosition( int startLink, int numLinks, float kst, float ti );
 
407
        
 
408
        void updateVelocitiesFromPositionsWithVelocities( float isolverdt );
 
409
 
 
410
        void updateVelocitiesFromPositionsWithoutVelocities( float isolverdt );
 
411
        virtual void solveCollisionsAndUpdateVelocities( float isolverdt );
 
412
 
 
413
        // End kernel dispatches
 
414
        /////////////////////////////////////
 
415
        
 
416
        void updateBounds();
 
417
 
 
418
        void releaseKernels();
 
419
 
 
420
public:
 
421
        btOpenCLSoftBodySolver(cl_command_queue queue,cl_context        ctx);
 
422
 
 
423
        virtual ~btOpenCLSoftBodySolver();
 
424
 
 
425
 
 
426
        
 
427
        btOpenCLAcceleratedSoftBodyInterface *findSoftBodyInterface( const btSoftBody* const softBody );
 
428
 
 
429
        virtual btSoftBodyLinkData &getLinkData();
 
430
 
 
431
        virtual btSoftBodyVertexData &getVertexData();
 
432
 
 
433
        virtual btSoftBodyTriangleData &getTriangleData();
 
434
 
 
435
        virtual SolverTypes getSolverType() const
 
436
        {
 
437
                return CL_SOLVER;
 
438
        }
 
439
 
 
440
 
 
441
        virtual bool checkInitialized();
 
442
 
 
443
        virtual void updateSoftBodies( );
 
444
 
 
445
        virtual void optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate=false);
 
446
 
 
447
        virtual void copyBackToSoftBodies();
 
448
 
 
449
        virtual void solveConstraints( float solverdt );
 
450
 
 
451
        virtual void predictMotion( float solverdt );
 
452
 
 
453
        virtual void processCollision( btSoftBody *, btCollisionObject* );
 
454
 
 
455
        virtual void processCollision( btSoftBody*, btSoftBody* );
 
456
 
 
457
        virtual void    setDefaultWorkgroupSize(size_t workGroupSize)
 
458
        {
 
459
                m_defaultWorkGroupSize = workGroupSize;
 
460
        }
 
461
        virtual size_t  getDefaultWorkGroupSize() const
 
462
        {
 
463
                return m_defaultWorkGroupSize;
 
464
        }
 
465
        
 
466
}; // btOpenCLSoftBodySolver
 
467
 
 
468
 
 
469
/** 
 
470
 * Class to manage movement of data from a solver to a given target.
 
471
 * This version is the CL to CPU version.
 
472
 */
 
473
class btSoftBodySolverOutputCLtoCPU : public btSoftBodySolverOutput
 
474
{
 
475
protected:
 
476
 
 
477
public:
 
478
        btSoftBodySolverOutputCLtoCPU()
 
479
        {
 
480
        }
 
481
 
 
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 );
 
484
};
 
485
 
 
486
 
 
487
 
 
488
#endif // #ifndef BT_SOFT_BODY_SOLVER_OPENCL_H