[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3OpenCL / RigidBody / b3Solver.h
1 /*
2 Copyright (c) 2012 Advanced Micro Devices, Inc.  
3
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose, 
7 including commercial applications, and to alter it and redistribute it freely, 
8 subject to the following restrictions:
9
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14 //Originally written by Takahiro Harada
15
16 #ifndef __ADL_SOLVER_H
17 #define __ADL_SOLVER_H
18
19 #include "Bullet3OpenCL/ParallelPrimitives/b3OpenCLArray.h"
20 #include "b3GpuConstraint4.h"
21
22 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
23 #include "Bullet3Collision/NarrowPhaseCollision/b3Contact4.h"
24
25 #include "Bullet3OpenCL/ParallelPrimitives/b3PrefixScanCL.h"
26 #include "Bullet3OpenCL/ParallelPrimitives/b3RadixSort32CL.h"
27 #include "Bullet3OpenCL/ParallelPrimitives/b3BoundSearchCL.h"
28
29 #include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
30
31 #define B3NEXTMULTIPLEOF(num, alignment) (((num) / (alignment) + (((num) % (alignment) == 0) ? 0 : 1)) * (alignment))
32
33 enum
34 {
35         B3_SOLVER_N_SPLIT_X = 8,  //16,//4,
36         B3_SOLVER_N_SPLIT_Y = 4,  //16,//4,
37         B3_SOLVER_N_SPLIT_Z = 8,  //,
38         B3_SOLVER_N_CELLS = B3_SOLVER_N_SPLIT_X * B3_SOLVER_N_SPLIT_Y * B3_SOLVER_N_SPLIT_Z,
39         B3_SOLVER_N_BATCHES = 8,  //4,//8,//4,
40         B3_MAX_NUM_BATCHES = 128,
41 };
42
43 class b3SolverBase
44 {
45 public:
46         struct ConstraintCfg
47         {
48                 ConstraintCfg(float dt = 0.f) : m_positionDrift(0.005f), m_positionConstraintCoeff(0.2f), m_dt(dt), m_staticIdx(-1) {}
49
50                 float m_positionDrift;
51                 float m_positionConstraintCoeff;
52                 float m_dt;
53                 bool m_enableParallelSolve;
54                 float m_batchCellSize;
55                 int m_staticIdx;
56         };
57 };
58
59 class b3Solver : public b3SolverBase
60 {
61 public:
62         cl_context m_context;
63         cl_device_id m_device;
64         cl_command_queue m_queue;
65
66         b3OpenCLArray<unsigned int>* m_numConstraints;
67         b3OpenCLArray<unsigned int>* m_offsets;
68         b3OpenCLArray<int> m_batchSizes;
69
70         int m_nIterations;
71         cl_kernel m_batchingKernel;
72         cl_kernel m_batchingKernelNew;
73         cl_kernel m_solveContactKernel;
74         cl_kernel m_solveFrictionKernel;
75         cl_kernel m_contactToConstraintKernel;
76         cl_kernel m_setSortDataKernel;
77         cl_kernel m_reorderContactKernel;
78         cl_kernel m_copyConstraintKernel;
79
80         class b3RadixSort32CL* m_sort32;
81         class b3BoundSearchCL* m_search;
82         class b3PrefixScanCL* m_scan;
83
84         b3OpenCLArray<b3SortData>* m_sortDataBuffer;
85         b3OpenCLArray<b3Contact4>* m_contactBuffer2;
86
87         enum
88         {
89                 DYNAMIC_CONTACT_ALLOCATION_THRESHOLD = 2000000,
90         };
91
92         b3Solver(cl_context ctx, cl_device_id device, cl_command_queue queue, int pairCapacity);
93
94         virtual ~b3Solver();
95
96         void solveContactConstraint(const b3OpenCLArray<b3RigidBodyData>* bodyBuf, const b3OpenCLArray<b3InertiaData>* inertiaBuf,
97                                                                 b3OpenCLArray<b3GpuConstraint4>* constraint, void* additionalData, int n, int maxNumBatches);
98
99         void solveContactConstraintHost(b3OpenCLArray<b3RigidBodyData>* bodyBuf, b3OpenCLArray<b3InertiaData>* shapeBuf,
100                                                                         b3OpenCLArray<b3GpuConstraint4>* constraint, void* additionalData, int n, int maxNumBatches, b3AlignedObjectArray<int>* batchSizes);
101
102         void convertToConstraints(const b3OpenCLArray<b3RigidBodyData>* bodyBuf,
103                                                           const b3OpenCLArray<b3InertiaData>* shapeBuf,
104                                                           b3OpenCLArray<b3Contact4>* contactsIn, b3OpenCLArray<b3GpuConstraint4>* contactCOut, void* additionalData,
105                                                           int nContacts, const ConstraintCfg& cfg);
106
107         void batchContacts(b3OpenCLArray<b3Contact4>* contacts, int nContacts, b3OpenCLArray<unsigned int>* n, b3OpenCLArray<unsigned int>* offsets, int staticIdx);
108 };
109
110 #endif  //__ADL_SOLVER_H