2017-08-01 20:30:58 +08:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012 Advanced Micro Devices, Inc.
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
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.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
//Originally written by Takahiro Harada
|
|
|
|
|
|
|
|
#include "b3Solver.h"
|
|
|
|
|
|
|
|
///useNewBatchingKernel is a rewritten kernel using just a single thread of the warp, for experiments
|
|
|
|
bool useNewBatchingKernel = true;
|
|
|
|
bool gConvertConstraintOnCpu = false;
|
|
|
|
|
|
|
|
#define B3_SOLVER_SETUP_KERNEL_PATH "src/Bullet3OpenCL/RigidBody/kernels/solverSetup.cl"
|
|
|
|
#define B3_SOLVER_SETUP2_KERNEL_PATH "src/Bullet3OpenCL/RigidBody/kernels/solverSetup2.cl"
|
|
|
|
#define B3_SOLVER_CONTACT_KERNEL_PATH "src/Bullet3OpenCL/RigidBody/kernels/solveContact.cl"
|
|
|
|
#define B3_SOLVER_FRICTION_KERNEL_PATH "src/Bullet3OpenCL/RigidBody/kernels/solveFriction.cl"
|
|
|
|
#define B3_BATCHING_PATH "src/Bullet3OpenCL/RigidBody/kernels/batchingKernels.cl"
|
|
|
|
#define B3_BATCHING_NEW_PATH "src/Bullet3OpenCL/RigidBody/kernels/batchingKernelsNew.cl"
|
|
|
|
|
|
|
|
#include "Bullet3Dynamics/shared/b3ConvertConstraint4.h"
|
|
|
|
|
|
|
|
#include "kernels/solverSetup.h"
|
|
|
|
#include "kernels/solverSetup2.h"
|
|
|
|
|
|
|
|
#include "kernels/solveContact.h"
|
|
|
|
#include "kernels/solveFriction.h"
|
|
|
|
|
|
|
|
#include "kernels/batchingKernels.h"
|
|
|
|
#include "kernels/batchingKernelsNew.h"
|
|
|
|
|
|
|
|
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
|
|
|
#include "Bullet3Common/b3Vector3.h"
|
|
|
|
|
|
|
|
struct SolverDebugInfo
|
|
|
|
{
|
|
|
|
int m_valInt0;
|
|
|
|
int m_valInt1;
|
|
|
|
int m_valInt2;
|
|
|
|
int m_valInt3;
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
int m_valInt4;
|
|
|
|
int m_valInt5;
|
|
|
|
int m_valInt6;
|
|
|
|
int m_valInt7;
|
|
|
|
|
|
|
|
int m_valInt8;
|
|
|
|
int m_valInt9;
|
|
|
|
int m_valInt10;
|
|
|
|
int m_valInt11;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
int m_valInt12;
|
|
|
|
int m_valInt13;
|
|
|
|
int m_valInt14;
|
|
|
|
int m_valInt15;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
float m_val0;
|
|
|
|
float m_val1;
|
|
|
|
float m_val2;
|
|
|
|
float m_val3;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SolverDeviceInl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct ParallelSolveData
|
|
|
|
{
|
|
|
|
b3OpenCLArray<unsigned int>* m_numConstraints;
|
|
|
|
b3OpenCLArray<unsigned int>* m_offsets;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
b3Solver::b3Solver(cl_context ctx, cl_device_id device, cl_command_queue queue, int pairCapacity)
|
2019-01-03 21:26:51 +08:00
|
|
|
: m_context(ctx),
|
|
|
|
m_device(device),
|
|
|
|
m_queue(queue),
|
|
|
|
m_batchSizes(ctx, queue),
|
|
|
|
m_nIterations(4)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
m_sort32 = new b3RadixSort32CL(ctx, device, queue);
|
|
|
|
m_scan = new b3PrefixScanCL(ctx, device, queue, B3_SOLVER_N_CELLS);
|
|
|
|
m_search = new b3BoundSearchCL(ctx, device, queue, B3_SOLVER_N_CELLS);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
const int sortSize = B3NEXTMULTIPLEOF(pairCapacity, 512);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
m_sortDataBuffer = new b3OpenCLArray<b3SortData>(ctx, queue, sortSize);
|
|
|
|
m_contactBuffer2 = new b3OpenCLArray<b3Contact4>(ctx, queue);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
m_numConstraints = new b3OpenCLArray<unsigned int>(ctx, queue, B3_SOLVER_N_CELLS);
|
2017-08-01 20:30:58 +08:00
|
|
|
m_numConstraints->resize(B3_SOLVER_N_CELLS);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
m_offsets = new b3OpenCLArray<unsigned int>(ctx, queue, B3_SOLVER_N_CELLS);
|
2017-08-01 20:30:58 +08:00
|
|
|
m_offsets->resize(B3_SOLVER_N_CELLS);
|
|
|
|
const char* additionalMacros = "";
|
2019-01-03 21:26:51 +08:00
|
|
|
// const char* srcFileNameForCaching="";
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
cl_int pErrNum;
|
|
|
|
const char* batchKernelSource = batchingKernelsCL;
|
|
|
|
const char* batchKernelNewSource = batchingKernelsNewCL;
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
const char* solverSetupSource = solverSetupCL;
|
|
|
|
const char* solverSetup2Source = solverSetup2CL;
|
|
|
|
const char* solveContactSource = solveContactCL;
|
|
|
|
const char* solveFrictionSource = solveFrictionCL;
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
cl_program solveContactProg = b3OpenCLUtils::compileCLProgramFromString(ctx, device, solveContactSource, &pErrNum, additionalMacros, B3_SOLVER_CONTACT_KERNEL_PATH);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(solveContactProg);
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
cl_program solveFrictionProg = b3OpenCLUtils::compileCLProgramFromString(ctx, device, solveFrictionSource, &pErrNum, additionalMacros, B3_SOLVER_FRICTION_KERNEL_PATH);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(solveFrictionProg);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
cl_program solverSetup2Prog = b3OpenCLUtils::compileCLProgramFromString(ctx, device, solverSetup2Source, &pErrNum, additionalMacros, B3_SOLVER_SETUP2_KERNEL_PATH);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(solverSetup2Prog);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
cl_program solverSetupProg = b3OpenCLUtils::compileCLProgramFromString(ctx, device, solverSetupSource, &pErrNum, additionalMacros, B3_SOLVER_SETUP_KERNEL_PATH);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(solverSetupProg);
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
m_solveFrictionKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, solveFrictionSource, "BatchSolveKernelFriction", &pErrNum, solveFrictionProg, additionalMacros);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(m_solveFrictionKernel);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
m_solveContactKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, solveContactSource, "BatchSolveKernelContact", &pErrNum, solveContactProg, additionalMacros);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(m_solveContactKernel);
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
m_contactToConstraintKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, solverSetupSource, "ContactToConstraintKernel", &pErrNum, solverSetupProg, additionalMacros);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(m_contactToConstraintKernel);
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
m_setSortDataKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, solverSetup2Source, "SetSortDataKernel", &pErrNum, solverSetup2Prog, additionalMacros);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(m_setSortDataKernel);
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
m_reorderContactKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, solverSetup2Source, "ReorderContactKernel", &pErrNum, solverSetup2Prog, additionalMacros);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(m_reorderContactKernel);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
m_copyConstraintKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, solverSetup2Source, "CopyConstraintKernel", &pErrNum, solverSetup2Prog, additionalMacros);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(m_copyConstraintKernel);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
cl_program batchingProg = b3OpenCLUtils::compileCLProgramFromString(ctx, device, batchKernelSource, &pErrNum, additionalMacros, B3_BATCHING_PATH);
|
2017-08-01 20:30:58 +08:00
|
|
|
//cl_program batchingProg = b3OpenCLUtils::compileCLProgramFromString( ctx, device, 0, &pErrNum,additionalMacros, B3_BATCHING_PATH,true);
|
|
|
|
b3Assert(batchingProg);
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
m_batchingKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, batchKernelSource, "CreateBatches", &pErrNum, batchingProg, additionalMacros);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(m_batchingKernel);
|
|
|
|
}
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
cl_program batchingNewProg = b3OpenCLUtils::compileCLProgramFromString(ctx, device, batchKernelNewSource, &pErrNum, additionalMacros, B3_BATCHING_NEW_PATH);
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(batchingNewProg);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
m_batchingKernelNew = b3OpenCLUtils::compileCLKernelFromString(ctx, device, batchKernelNewSource, "CreateBatchesNew", &pErrNum, batchingNewProg, additionalMacros);
|
2017-08-01 20:30:58 +08:00
|
|
|
//m_batchingKernelNew = b3OpenCLUtils::compileCLKernelFromString( ctx, device, batchKernelNewSource, "CreateBatchesBruteForce", &pErrNum, batchingNewProg,additionalMacros );
|
|
|
|
b3Assert(m_batchingKernelNew);
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Solver::~b3Solver()
|
|
|
|
{
|
|
|
|
delete m_offsets;
|
|
|
|
delete m_numConstraints;
|
|
|
|
delete m_sortDataBuffer;
|
|
|
|
delete m_contactBuffer2;
|
|
|
|
|
|
|
|
delete m_sort32;
|
|
|
|
delete m_scan;
|
|
|
|
delete m_search;
|
|
|
|
|
|
|
|
clReleaseKernel(m_batchingKernel);
|
|
|
|
clReleaseKernel(m_batchingKernelNew);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
clReleaseKernel(m_solveContactKernel);
|
|
|
|
clReleaseKernel(m_solveFrictionKernel);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
clReleaseKernel(m_contactToConstraintKernel);
|
|
|
|
clReleaseKernel(m_setSortDataKernel);
|
|
|
|
clReleaseKernel(m_reorderContactKernel);
|
|
|
|
clReleaseKernel(m_copyConstraintKernel);
|
|
|
|
}
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
template <bool JACOBI>
|
|
|
|
static __inline void solveContact(b3GpuConstraint4& cs,
|
|
|
|
const b3Vector3& posA, b3Vector3& linVelA, b3Vector3& angVelA, float invMassA, const b3Matrix3x3& invInertiaA,
|
|
|
|
const b3Vector3& posB, b3Vector3& linVelB, b3Vector3& angVelB, float invMassB, const b3Matrix3x3& invInertiaB,
|
|
|
|
float maxRambdaDt[4], float minRambdaDt[4])
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Vector3 dLinVelA;
|
|
|
|
dLinVelA.setZero();
|
|
|
|
b3Vector3 dAngVelA;
|
|
|
|
dAngVelA.setZero();
|
|
|
|
b3Vector3 dLinVelB;
|
|
|
|
dLinVelB.setZero();
|
|
|
|
b3Vector3 dAngVelB;
|
|
|
|
dAngVelB.setZero();
|
|
|
|
|
|
|
|
for (int ic = 0; ic < 4; ic++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
// dont necessary because this makes change to 0
|
2019-01-03 21:26:51 +08:00
|
|
|
if (cs.m_jacCoeffInv[ic] == 0.f) continue;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
b3Vector3 angular0, angular1, linear;
|
|
|
|
b3Vector3 r0 = cs.m_worldPos[ic] - (b3Vector3&)posA;
|
|
|
|
b3Vector3 r1 = cs.m_worldPos[ic] - (b3Vector3&)posB;
|
2019-01-03 21:26:51 +08:00
|
|
|
setLinearAndAngular((const b3Vector3&)cs.m_linear, (const b3Vector3&)r0, (const b3Vector3&)r1, &linear, &angular0, &angular1);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
float rambdaDt = calcRelVel((const b3Vector3&)cs.m_linear, (const b3Vector3&)-cs.m_linear, angular0, angular1,
|
|
|
|
linVelA, angVelA, linVelB, angVelB) +
|
|
|
|
cs.m_b[ic];
|
2017-08-01 20:30:58 +08:00
|
|
|
rambdaDt *= cs.m_jacCoeffInv[ic];
|
|
|
|
|
|
|
|
{
|
|
|
|
float prevSum = cs.m_appliedRambdaDt[ic];
|
|
|
|
float updated = prevSum;
|
|
|
|
updated += rambdaDt;
|
2019-01-03 21:26:51 +08:00
|
|
|
updated = b3Max(updated, minRambdaDt[ic]);
|
|
|
|
updated = b3Min(updated, maxRambdaDt[ic]);
|
2017-08-01 20:30:58 +08:00
|
|
|
rambdaDt = updated - prevSum;
|
|
|
|
cs.m_appliedRambdaDt[ic] = updated;
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Vector3 linImp0 = invMassA * linear * rambdaDt;
|
|
|
|
b3Vector3 linImp1 = invMassB * (-linear) * rambdaDt;
|
|
|
|
b3Vector3 angImp0 = (invInertiaA * angular0) * rambdaDt;
|
|
|
|
b3Vector3 angImp1 = (invInertiaB * angular1) * rambdaDt;
|
2017-08-01 20:30:58 +08:00
|
|
|
#ifdef _WIN32
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Assert(_finite(linImp0.getX()));
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Assert(_finite(linImp1.getX()));
|
|
|
|
#endif
|
2019-01-03 21:26:51 +08:00
|
|
|
if (JACOBI)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
dLinVelA += linImp0;
|
|
|
|
dAngVelA += angImp0;
|
|
|
|
dLinVelB += linImp1;
|
|
|
|
dAngVelB += angImp1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
linVelA += linImp0;
|
|
|
|
angVelA += angImp0;
|
|
|
|
linVelB += linImp1;
|
|
|
|
angVelB += angImp1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (JACOBI)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
linVelA += dLinVelA;
|
|
|
|
angVelA += dAngVelA;
|
|
|
|
linVelB += dLinVelB;
|
|
|
|
angVelB += dAngVelB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
static __inline void solveFriction(b3GpuConstraint4& cs,
|
|
|
|
const b3Vector3& posA, b3Vector3& linVelA, b3Vector3& angVelA, float invMassA, const b3Matrix3x3& invInertiaA,
|
|
|
|
const b3Vector3& posB, b3Vector3& linVelB, b3Vector3& angVelB, float invMassB, const b3Matrix3x3& invInertiaB,
|
|
|
|
float maxRambdaDt[4], float minRambdaDt[4])
|
|
|
|
{
|
|
|
|
if (cs.m_fJacCoeffInv[0] == 0 && cs.m_fJacCoeffInv[0] == 0) return;
|
|
|
|
const b3Vector3& center = (const b3Vector3&)cs.m_center;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Vector3 n = -(const b3Vector3&)cs.m_linear;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Vector3 tangent[2];
|
|
|
|
#if 1
|
|
|
|
b3PlaneSpace1(n, tangent[0], tangent[1]);
|
2017-08-01 20:30:58 +08:00
|
|
|
#else
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Vector3 r = cs.m_worldPos[0] - center;
|
|
|
|
tangent[0] = cross3(n, r);
|
|
|
|
tangent[1] = cross3(tangent[0], n);
|
|
|
|
tangent[0] = normalize3(tangent[0]);
|
|
|
|
tangent[1] = normalize3(tangent[1]);
|
2017-08-01 20:30:58 +08:00
|
|
|
#endif
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Vector3 angular0, angular1, linear;
|
|
|
|
b3Vector3 r0 = center - posA;
|
|
|
|
b3Vector3 r1 = center - posB;
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
setLinearAndAngular(tangent[i], r0, r1, &linear, &angular0, &angular1);
|
|
|
|
float rambdaDt = calcRelVel(linear, -linear, angular0, angular1,
|
|
|
|
linVelA, angVelA, linVelB, angVelB);
|
|
|
|
rambdaDt *= cs.m_fJacCoeffInv[i];
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
{
|
|
|
|
float prevSum = cs.m_fAppliedRambdaDt[i];
|
|
|
|
float updated = prevSum;
|
|
|
|
updated += rambdaDt;
|
|
|
|
updated = b3Max(updated, minRambdaDt[i]);
|
|
|
|
updated = b3Min(updated, maxRambdaDt[i]);
|
|
|
|
rambdaDt = updated - prevSum;
|
|
|
|
cs.m_fAppliedRambdaDt[i] = updated;
|
|
|
|
}
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Vector3 linImp0 = invMassA * linear * rambdaDt;
|
|
|
|
b3Vector3 linImp1 = invMassB * (-linear) * rambdaDt;
|
|
|
|
b3Vector3 angImp0 = (invInertiaA * angular0) * rambdaDt;
|
|
|
|
b3Vector3 angImp1 = (invInertiaB * angular1) * rambdaDt;
|
2017-08-01 20:30:58 +08:00
|
|
|
#ifdef _WIN32
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Assert(_finite(linImp0.getX()));
|
|
|
|
b3Assert(_finite(linImp1.getX()));
|
2017-08-01 20:30:58 +08:00
|
|
|
#endif
|
2019-01-03 21:26:51 +08:00
|
|
|
linVelA += linImp0;
|
|
|
|
angVelA += angImp0;
|
|
|
|
linVelB += linImp1;
|
|
|
|
angVelB += angImp1;
|
|
|
|
}
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
{ // angular damping for point constraint
|
|
|
|
b3Vector3 ab = (posB - posA).normalized();
|
|
|
|
b3Vector3 ac = (center - posA).normalized();
|
|
|
|
if (b3Dot(ab, ac) > 0.95f || (invMassA == 0.f || invMassB == 0.f))
|
|
|
|
{
|
|
|
|
float angNA = b3Dot(n, angVelA);
|
|
|
|
float angNB = b3Dot(n, angVelB);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
angVelA -= (angNA * 0.1f) * n;
|
|
|
|
angVelB -= (angNB * 0.1f) * n;
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
}
|
2017-08-01 20:30:58 +08:00
|
|
|
/*
|
|
|
|
b3AlignedObjectArray<b3RigidBodyData>& m_bodies;
|
|
|
|
b3AlignedObjectArray<b3InertiaData>& m_shapes;
|
|
|
|
b3AlignedObjectArray<b3GpuConstraint4>& m_constraints;
|
|
|
|
b3AlignedObjectArray<int>* m_batchSizes;
|
|
|
|
int m_cellIndex;
|
|
|
|
int m_curWgidx;
|
|
|
|
int m_start;
|
|
|
|
int m_nConstraints;
|
|
|
|
bool m_solveFriction;
|
|
|
|
int m_maxNumBatches;
|
|
|
|
*/
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
struct SolveTask // : public ThreadPool::Task
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
SolveTask(b3AlignedObjectArray<b3RigidBodyData>& bodies, b3AlignedObjectArray<b3InertiaData>& shapes, b3AlignedObjectArray<b3GpuConstraint4>& constraints,
|
|
|
|
int start, int nConstraints, int maxNumBatches, b3AlignedObjectArray<int>* wgUsedBodies, int curWgidx, b3AlignedObjectArray<int>* batchSizes, int cellIndex)
|
|
|
|
: m_bodies(bodies), m_shapes(shapes), m_constraints(constraints), m_batchSizes(batchSizes), m_cellIndex(cellIndex), m_curWgidx(curWgidx), m_start(start), m_nConstraints(nConstraints), m_solveFriction(true), m_maxNumBatches(maxNumBatches)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short int getType() { return 0; }
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
void run(int tIdx)
|
|
|
|
{
|
|
|
|
int offset = 0;
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int ii = 0; ii < B3_MAX_NUM_BATCHES; ii++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
int numInBatch = m_batchSizes->at(m_cellIndex * B3_MAX_NUM_BATCHES + ii);
|
2017-08-01 20:30:58 +08:00
|
|
|
if (!numInBatch)
|
|
|
|
break;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int jj = 0; jj < numInBatch; jj++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
int i = m_start + offset + jj;
|
2017-08-01 20:30:58 +08:00
|
|
|
int batchId = m_constraints[i].m_batchIdx;
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Assert(batchId == ii);
|
2017-08-01 20:30:58 +08:00
|
|
|
float frictionCoeff = m_constraints[i].getFrictionCoeff();
|
|
|
|
int aIdx = (int)m_constraints[i].m_bodyA;
|
|
|
|
int bIdx = (int)m_constraints[i].m_bodyB;
|
2019-01-03 21:26:51 +08:00
|
|
|
// int localBatch = m_constraints[i].m_batchIdx;
|
2017-08-01 20:30:58 +08:00
|
|
|
b3RigidBodyData& bodyA = m_bodies[aIdx];
|
|
|
|
b3RigidBodyData& bodyB = m_bodies[bIdx];
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (!m_solveFriction)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
float maxRambdaDt[4] = {FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX};
|
|
|
|
float minRambdaDt[4] = {0.f, 0.f, 0.f, 0.f};
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
solveContact<false>(m_constraints[i], (b3Vector3&)bodyA.m_pos, (b3Vector3&)bodyA.m_linVel, (b3Vector3&)bodyA.m_angVel, bodyA.m_invMass, (const b3Matrix3x3&)m_shapes[aIdx].m_invInertiaWorld,
|
|
|
|
(b3Vector3&)bodyB.m_pos, (b3Vector3&)bodyB.m_linVel, (b3Vector3&)bodyB.m_angVel, bodyB.m_invMass, (const b3Matrix3x3&)m_shapes[bIdx].m_invInertiaWorld,
|
|
|
|
maxRambdaDt, minRambdaDt);
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
float maxRambdaDt[4] = {FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX};
|
|
|
|
float minRambdaDt[4] = {0.f, 0.f, 0.f, 0.f};
|
2017-08-01 20:30:58 +08:00
|
|
|
float sum = 0;
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int j = 0; j < 4; j++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
sum += m_constraints[i].m_appliedRambdaDt[j];
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
frictionCoeff = 0.7f;
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int j = 0; j < 4; j++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
maxRambdaDt[j] = frictionCoeff * sum;
|
2017-08-01 20:30:58 +08:00
|
|
|
minRambdaDt[j] = -maxRambdaDt[j];
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
solveFriction(m_constraints[i], (b3Vector3&)bodyA.m_pos, (b3Vector3&)bodyA.m_linVel, (b3Vector3&)bodyA.m_angVel, bodyA.m_invMass, (const b3Matrix3x3&)m_shapes[aIdx].m_invInertiaWorld,
|
|
|
|
(b3Vector3&)bodyB.m_pos, (b3Vector3&)bodyB.m_linVel, (b3Vector3&)bodyB.m_angVel, bodyB.m_invMass, (const b3Matrix3x3&)m_shapes[bIdx].m_invInertiaWorld,
|
|
|
|
maxRambdaDt, minRambdaDt);
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
offset += numInBatch;
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
/* for (int bb=0;bb<m_maxNumBatches;bb++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
//for(int ic=m_nConstraints-1; ic>=0; ic--)
|
|
|
|
for(int ic=0; ic<m_nConstraints; ic++)
|
|
|
|
{
|
|
|
|
|
|
|
|
int i = m_start + ic;
|
|
|
|
if (m_constraints[i].m_batchIdx != bb)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float frictionCoeff = m_constraints[i].getFrictionCoeff();
|
|
|
|
int aIdx = (int)m_constraints[i].m_bodyA;
|
|
|
|
int bIdx = (int)m_constraints[i].m_bodyB;
|
|
|
|
int localBatch = m_constraints[i].m_batchIdx;
|
|
|
|
b3RigidBodyData& bodyA = m_bodies[aIdx];
|
|
|
|
b3RigidBodyData& bodyB = m_bodies[bIdx];
|
|
|
|
|
|
|
|
if( !m_solveFriction )
|
|
|
|
{
|
|
|
|
float maxRambdaDt[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX};
|
|
|
|
float minRambdaDt[4] = {0.f,0.f,0.f,0.f};
|
|
|
|
|
|
|
|
solveContact<false>( m_constraints[i], (b3Vector3&)bodyA.m_pos, (b3Vector3&)bodyA.m_linVel, (b3Vector3&)bodyA.m_angVel, bodyA.m_invMass, (const b3Matrix3x3 &)m_shapes[aIdx].m_invInertiaWorld,
|
|
|
|
(b3Vector3&)bodyB.m_pos, (b3Vector3&)bodyB.m_linVel, (b3Vector3&)bodyB.m_angVel, bodyB.m_invMass, (const b3Matrix3x3 &)m_shapes[bIdx].m_invInertiaWorld,
|
|
|
|
maxRambdaDt, minRambdaDt );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float maxRambdaDt[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX};
|
|
|
|
float minRambdaDt[4] = {0.f,0.f,0.f,0.f};
|
|
|
|
float sum = 0;
|
|
|
|
for(int j=0; j<4; j++)
|
|
|
|
{
|
|
|
|
sum +=m_constraints[i].m_appliedRambdaDt[j];
|
|
|
|
}
|
|
|
|
frictionCoeff = 0.7f;
|
|
|
|
for(int j=0; j<4; j++)
|
|
|
|
{
|
|
|
|
maxRambdaDt[j] = frictionCoeff*sum;
|
|
|
|
minRambdaDt[j] = -maxRambdaDt[j];
|
|
|
|
}
|
|
|
|
solveFriction( m_constraints[i], (b3Vector3&)bodyA.m_pos, (b3Vector3&)bodyA.m_linVel, (b3Vector3&)bodyA.m_angVel, bodyA.m_invMass,(const b3Matrix3x3 &) m_shapes[aIdx].m_invInertiaWorld,
|
|
|
|
(b3Vector3&)bodyB.m_pos, (b3Vector3&)bodyB.m_linVel, (b3Vector3&)bodyB.m_angVel, bodyB.m_invMass,(const b3Matrix3x3 &) m_shapes[bIdx].m_invInertiaWorld,
|
|
|
|
maxRambdaDt, minRambdaDt );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
b3AlignedObjectArray<b3RigidBodyData>& m_bodies;
|
|
|
|
b3AlignedObjectArray<b3InertiaData>& m_shapes;
|
|
|
|
b3AlignedObjectArray<b3GpuConstraint4>& m_constraints;
|
|
|
|
b3AlignedObjectArray<int>* m_batchSizes;
|
|
|
|
int m_cellIndex;
|
|
|
|
int m_curWgidx;
|
|
|
|
int m_start;
|
|
|
|
int m_nConstraints;
|
|
|
|
bool m_solveFriction;
|
|
|
|
int m_maxNumBatches;
|
|
|
|
};
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
void b3Solver::solveContactConstraintHost(b3OpenCLArray<b3RigidBodyData>* bodyBuf, b3OpenCLArray<b3InertiaData>* shapeBuf,
|
|
|
|
b3OpenCLArray<b3GpuConstraint4>* constraint, void* additionalData, int n, int maxNumBatches, b3AlignedObjectArray<int>* batchSizes)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
int nSplitX = B3_SOLVER_N_SPLIT_X;
|
|
|
|
int nSplitY = B3_SOLVER_N_SPLIT_Y;
|
|
|
|
int numWorkgroups = B3_SOLVER_N_CELLS/B3_SOLVER_N_BATCHES;
|
|
|
|
for (int z=0;z<4;z++)
|
|
|
|
{
|
|
|
|
for (int y=0;y<4;y++)
|
|
|
|
{
|
|
|
|
for (int x=0;x<4;x++)
|
|
|
|
{
|
|
|
|
int newIndex = (x+y*nSplitX+z*nSplitX*nSplitY);
|
|
|
|
// printf("newIndex=%d\n",newIndex);
|
|
|
|
|
|
|
|
int zIdx = newIndex/(nSplitX*nSplitY);
|
|
|
|
int remain = newIndex%(nSplitX*nSplitY);
|
|
|
|
int yIdx = remain/nSplitX;
|
|
|
|
int xIdx = remain%nSplitX;
|
|
|
|
// printf("newIndex=%d\n",newIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//for (int wgIdx=numWorkgroups-1;wgIdx>=0;wgIdx--)
|
|
|
|
for (int cellBatch=0;cellBatch<B3_SOLVER_N_BATCHES;cellBatch++)
|
|
|
|
{
|
|
|
|
for (int wgIdx=0;wgIdx<numWorkgroups;wgIdx++)
|
|
|
|
{
|
|
|
|
int zIdx = (wgIdx/((nSplitX*nSplitY)/4))*2+((cellBatch&4)>>2);
|
|
|
|
int remain= (wgIdx%((nSplitX*nSplitY)/4));
|
|
|
|
int yIdx = (remain/(nSplitX/2))*2 + ((cellBatch&2)>>1);
|
|
|
|
int xIdx = (remain%(nSplitX/2))*2 + (cellBatch&1);
|
|
|
|
|
|
|
|
/*int zIdx = newIndex/(nSplitX*nSplitY);
|
|
|
|
int remain = newIndex%(nSplitX*nSplitY);
|
|
|
|
int yIdx = remain/nSplitX;
|
|
|
|
int xIdx = remain%nSplitX;
|
|
|
|
*/
|
|
|
|
int cellIdx = xIdx+yIdx*nSplitX+zIdx*(nSplitX*nSplitY);
|
|
|
|
// printf("wgIdx %d: xIdx=%d, yIdx=%d, zIdx=%d, cellIdx=%d, cell Batch %d\n",wgIdx,xIdx,yIdx,zIdx,cellIdx,cellBatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
b3AlignedObjectArray<b3RigidBodyData> bodyNative;
|
|
|
|
bodyBuf->copyToHost(bodyNative);
|
|
|
|
b3AlignedObjectArray<b3InertiaData> shapeNative;
|
|
|
|
shapeBuf->copyToHost(shapeNative);
|
|
|
|
b3AlignedObjectArray<b3GpuConstraint4> constraintNative;
|
|
|
|
constraint->copyToHost(constraintNative);
|
|
|
|
|
|
|
|
b3AlignedObjectArray<unsigned int> numConstraintsHost;
|
|
|
|
m_numConstraints->copyToHost(numConstraintsHost);
|
|
|
|
|
|
|
|
//printf("------------------------\n");
|
|
|
|
b3AlignedObjectArray<unsigned int> offsetsHost;
|
|
|
|
m_offsets->copyToHost(offsetsHost);
|
2019-01-03 21:26:51 +08:00
|
|
|
static int frame = 0;
|
|
|
|
bool useBatches = true;
|
2017-08-01 20:30:58 +08:00
|
|
|
if (useBatches)
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int iter = 0; iter < m_nIterations; iter++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int cellBatch = 0; cellBatch < B3_SOLVER_N_BATCHES; cellBatch++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
int nSplitX = B3_SOLVER_N_SPLIT_X;
|
|
|
|
int nSplitY = B3_SOLVER_N_SPLIT_Y;
|
2019-01-03 21:26:51 +08:00
|
|
|
int numWorkgroups = B3_SOLVER_N_CELLS / B3_SOLVER_N_BATCHES;
|
2017-08-01 20:30:58 +08:00
|
|
|
//printf("cell Batch %d\n",cellBatch);
|
|
|
|
b3AlignedObjectArray<int> usedBodies[B3_SOLVER_N_CELLS];
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int i = 0; i < B3_SOLVER_N_CELLS; i++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
usedBodies[i].resize(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//for (int wgIdx=numWorkgroups-1;wgIdx>=0;wgIdx--)
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int wgIdx = 0; wgIdx < numWorkgroups; wgIdx++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
int zIdx = (wgIdx / ((nSplitX * nSplitY) / 4)) * 2 + ((cellBatch & 4) >> 2);
|
|
|
|
int remain = (wgIdx % ((nSplitX * nSplitY) / 4));
|
|
|
|
int yIdx = (remain / (nSplitX / 2)) * 2 + ((cellBatch & 2) >> 1);
|
|
|
|
int xIdx = (remain % (nSplitX / 2)) * 2 + (cellBatch & 1);
|
|
|
|
int cellIdx = xIdx + yIdx * nSplitX + zIdx * (nSplitX * nSplitY);
|
|
|
|
|
|
|
|
if (numConstraintsHost[cellIdx] == 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
//printf("wgIdx %d: xIdx=%d, yIdx=%d, zIdx=%d, cellIdx=%d, cell Batch %d\n",wgIdx,xIdx,yIdx,zIdx,cellIdx,cellBatch);
|
|
|
|
//printf("cell %d has %d constraints\n", cellIdx,numConstraintsHost[cellIdx]);
|
|
|
|
if (zIdx)
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
//printf("?\n");
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (iter == 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
//printf("frame=%d, Cell xIdx=%x, yIdx=%d ",frame, xIdx,yIdx);
|
|
|
|
//printf("cellBatch=%d, wgIdx=%d, #constraints in cell=%d\n",cellBatch,wgIdx,numConstraintsHost[cellIdx]);
|
|
|
|
}
|
|
|
|
const int start = offsetsHost[cellIdx];
|
|
|
|
int numConstraintsInCell = numConstraintsHost[cellIdx];
|
2019-01-03 21:26:51 +08:00
|
|
|
// const int end = start + numConstraintsInCell;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
SolveTask task(bodyNative, shapeNative, constraintNative, start, numConstraintsInCell, maxNumBatches, usedBodies, wgIdx, batchSizes, cellIdx);
|
2017-08-01 20:30:58 +08:00
|
|
|
task.m_solveFriction = false;
|
|
|
|
task.run(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int iter = 0; iter < m_nIterations; iter++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int cellBatch = 0; cellBatch < B3_SOLVER_N_BATCHES; cellBatch++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
int nSplitX = B3_SOLVER_N_SPLIT_X;
|
|
|
|
int nSplitY = B3_SOLVER_N_SPLIT_Y;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
int numWorkgroups = B3_SOLVER_N_CELLS / B3_SOLVER_N_BATCHES;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int wgIdx = 0; wgIdx < numWorkgroups; wgIdx++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
int zIdx = (wgIdx / ((nSplitX * nSplitY) / 4)) * 2 + ((cellBatch & 4) >> 2);
|
|
|
|
int remain = (wgIdx % ((nSplitX * nSplitY) / 4));
|
|
|
|
int yIdx = (remain / (nSplitX / 2)) * 2 + ((cellBatch & 2) >> 1);
|
|
|
|
int xIdx = (remain % (nSplitX / 2)) * 2 + (cellBatch & 1);
|
|
|
|
|
|
|
|
int cellIdx = xIdx + yIdx * nSplitX + zIdx * (nSplitX * nSplitY);
|
|
|
|
|
|
|
|
if (numConstraintsHost[cellIdx] == 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
continue;
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
//printf("yIdx=%d\n",yIdx);
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
const int start = offsetsHost[cellIdx];
|
|
|
|
int numConstraintsInCell = numConstraintsHost[cellIdx];
|
2019-01-03 21:26:51 +08:00
|
|
|
// const int end = start + numConstraintsInCell;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
SolveTask task(bodyNative, shapeNative, constraintNative, start, numConstraintsInCell, maxNumBatches, 0, 0, batchSizes, cellIdx);
|
2017-08-01 20:30:58 +08:00
|
|
|
task.m_solveFriction = true;
|
|
|
|
task.run(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int iter = 0; iter < m_nIterations; iter++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
SolveTask task(bodyNative, shapeNative, constraintNative, 0, n, maxNumBatches, 0, 0, 0, 0);
|
2017-08-01 20:30:58 +08:00
|
|
|
task.m_solveFriction = false;
|
|
|
|
task.run(0);
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int iter = 0; iter < m_nIterations; iter++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
SolveTask task(bodyNative, shapeNative, constraintNative, 0, n, maxNumBatches, 0, 0, 0, 0);
|
2017-08-01 20:30:58 +08:00
|
|
|
task.m_solveFriction = true;
|
|
|
|
task.run(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyBuf->copyFromHost(bodyNative);
|
|
|
|
shapeBuf->copyFromHost(shapeNative);
|
|
|
|
constraint->copyFromHost(constraintNative);
|
|
|
|
frame++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkConstraintBatch(const b3OpenCLArray<b3RigidBodyData>* bodyBuf,
|
2019-01-03 21:26:51 +08:00
|
|
|
const b3OpenCLArray<b3InertiaData>* shapeBuf,
|
|
|
|
b3OpenCLArray<b3GpuConstraint4>* constraint,
|
|
|
|
b3OpenCLArray<unsigned int>* m_numConstraints,
|
|
|
|
b3OpenCLArray<unsigned int>* m_offsets,
|
|
|
|
int batchId)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
// b3BufferInfoCL( m_numConstraints->getBufferCL() ),
|
|
|
|
// b3BufferInfoCL( m_offsets->getBufferCL() )
|
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
int cellBatch = batchId;
|
|
|
|
const int nn = B3_SOLVER_N_CELLS;
|
2019-01-03 21:26:51 +08:00
|
|
|
// int numWorkItems = 64*nn/B3_SOLVER_N_BATCHES;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
b3AlignedObjectArray<unsigned int> gN;
|
|
|
|
m_numConstraints->copyToHost(gN);
|
|
|
|
b3AlignedObjectArray<unsigned int> gOffsets;
|
|
|
|
m_offsets->copyToHost(gOffsets);
|
|
|
|
int nSplitX = B3_SOLVER_N_SPLIT_X;
|
|
|
|
int nSplitY = B3_SOLVER_N_SPLIT_Y;
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
// int bIdx = batchId;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
b3AlignedObjectArray<b3GpuConstraint4> cpuConstraints;
|
|
|
|
constraint->copyToHost(cpuConstraints);
|
|
|
|
|
|
|
|
printf("batch = %d\n", batchId);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
int numWorkgroups = nn / B3_SOLVER_N_BATCHES;
|
2017-08-01 20:30:58 +08:00
|
|
|
b3AlignedObjectArray<int> usedBodies;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int wgIdx = 0; wgIdx < numWorkgroups; wgIdx++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
printf("wgIdx = %d ", wgIdx);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
int zIdx = (wgIdx / ((nSplitX * nSplitY)) / 2) * 2 + ((cellBatch & 4) >> 2);
|
|
|
|
int remain = wgIdx % ((nSplitX * nSplitY));
|
|
|
|
int yIdx = (remain % (nSplitX / 2)) * 2 + ((cellBatch & 2) >> 1);
|
|
|
|
int xIdx = (remain / (nSplitX / 2)) * 2 + (cellBatch & 1);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
int cellIdx = xIdx + yIdx * nSplitX + zIdx * (nSplitX * nSplitY);
|
|
|
|
printf("cellIdx=%d\n", cellIdx);
|
|
|
|
if (gN[cellIdx] == 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const int start = gOffsets[cellIdx];
|
|
|
|
const int end = start + gN[cellIdx];
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int c = start; c < end; c++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
b3GpuConstraint4& constraint = cpuConstraints[c];
|
|
|
|
//printf("constraint (%d,%d)\n", constraint.m_bodyA,constraint.m_bodyB);
|
2019-01-03 21:26:51 +08:00
|
|
|
if (usedBodies.findLinearSearch(constraint.m_bodyA) < usedBodies.size())
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
printf("error?\n");
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
if (usedBodies.findLinearSearch(constraint.m_bodyB) < usedBodies.size())
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
printf("error?\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int c = start; c < end; c++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
b3GpuConstraint4& constraint = cpuConstraints[c];
|
|
|
|
usedBodies.push_back(constraint.m_bodyA);
|
|
|
|
usedBodies.push_back(constraint.m_bodyB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
static bool verify = false;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
void b3Solver::solveContactConstraint(const b3OpenCLArray<b3RigidBodyData>* bodyBuf, const b3OpenCLArray<b3InertiaData>* shapeBuf,
|
|
|
|
b3OpenCLArray<b3GpuConstraint4>* constraint, void* additionalData, int n, int maxNumBatches)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Int4 cdata = b3MakeInt4(n, 0, 0, 0);
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
const int nn = B3_SOLVER_N_CELLS;
|
|
|
|
|
|
|
|
cdata.x = 0;
|
2019-01-03 21:26:51 +08:00
|
|
|
cdata.y = maxNumBatches; //250;
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
int numWorkItems = 64 * nn / B3_SOLVER_N_BATCHES;
|
2017-08-01 20:30:58 +08:00
|
|
|
#ifdef DEBUG_ME
|
2019-01-03 21:26:51 +08:00
|
|
|
SolverDebugInfo* debugInfo = new SolverDebugInfo[numWorkItems];
|
|
|
|
adl::b3OpenCLArray<SolverDebugInfo> gpuDebugInfo(data->m_device, numWorkItems);
|
2017-08-01 20:30:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
B3_PROFILE("m_batchSolveKernel iterations");
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int iter = 0; iter < m_nIterations; iter++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int ib = 0; ib < B3_SOLVER_N_BATCHES; ib++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
if (verify)
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
checkConstraintBatch(bodyBuf, shapeBuf, constraint, m_numConstraints, m_offsets, ib);
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
2019-01-03 21:26:51 +08:00
|
|
|
memset(debugInfo, 0, sizeof(SolverDebugInfo) * numWorkItems);
|
|
|
|
gpuDebugInfo.write(debugInfo, numWorkItems);
|
2017-08-01 20:30:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
cdata.z = ib;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3LauncherCL launcher(m_queue, m_solveContactKernel, "m_solveContactKernel");
|
2017-08-01 20:30:58 +08:00
|
|
|
#if 1
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
b3BufferInfoCL bInfo[] = {
|
|
|
|
|
|
|
|
b3BufferInfoCL(bodyBuf->getBufferCL()),
|
|
|
|
b3BufferInfoCL(shapeBuf->getBufferCL()),
|
|
|
|
b3BufferInfoCL(constraint->getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_numConstraints->getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_offsets->getBufferCL())
|
2017-08-01 20:30:58 +08:00
|
|
|
#ifdef DEBUG_ME
|
2019-01-03 21:26:51 +08:00
|
|
|
,
|
|
|
|
b3BufferInfoCL(&gpuDebugInfo)
|
2017-08-01 20:30:58 +08:00
|
|
|
#endif
|
2019-01-03 21:26:51 +08:00
|
|
|
};
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 20:30:58 +08:00
|
|
|
//launcher.setConst( cdata.x );
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.setConst(cdata.y);
|
|
|
|
launcher.setConst(cdata.z);
|
|
|
|
b3Int4 nSplit;
|
2017-08-01 20:30:58 +08:00
|
|
|
nSplit.x = B3_SOLVER_N_SPLIT_X;
|
|
|
|
nSplit.y = B3_SOLVER_N_SPLIT_Y;
|
|
|
|
nSplit.z = B3_SOLVER_N_SPLIT_Z;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.setConst(nSplit);
|
|
|
|
launcher.launch1D(numWorkItems, 64);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
#else
|
2019-01-03 21:26:51 +08:00
|
|
|
const char* fileName = "m_batchSolveKernel.bin";
|
|
|
|
FILE* f = fopen(fileName, "rb");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
int sizeInBytes = 0;
|
|
|
|
if (fseek(f, 0, SEEK_END) || (sizeInBytes = ftell(f)) == EOF || fseek(f, 0, SEEK_SET))
|
|
|
|
{
|
|
|
|
printf("error, cannot get file size\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* buf = (unsigned char*)malloc(sizeInBytes);
|
|
|
|
fread(buf, sizeInBytes, 1, f);
|
|
|
|
int serializedBytes = launcher.deserializeArgs(buf, sizeInBytes, m_context);
|
|
|
|
int num = *(int*)&buf[serializedBytes];
|
|
|
|
|
|
|
|
launcher.launch1D(num);
|
|
|
|
|
|
|
|
//this clFinish is for testing on errors
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
clFinish(m_queue);
|
2019-01-03 21:26:51 +08:00
|
|
|
gpuDebugInfo.read(debugInfo, numWorkItems);
|
2017-08-01 20:30:58 +08:00
|
|
|
clFinish(m_queue);
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int i = 0; i < numWorkItems; i++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
if (debugInfo[i].m_valInt2 > 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
printf("debugInfo[i].m_valInt2 = %d\n", i, debugInfo[i].m_valInt2);
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (debugInfo[i].m_valInt3 > 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
printf("debugInfo[i].m_valInt3 = %d\n", i, debugInfo[i].m_valInt3);
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
#endif //DEBUG_ME
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
clFinish(m_queue);
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cdata.x = 1;
|
2019-01-03 21:26:51 +08:00
|
|
|
bool applyFriction = true;
|
2017-08-01 20:30:58 +08:00
|
|
|
if (applyFriction)
|
2019-01-03 21:26:51 +08:00
|
|
|
{
|
2017-08-01 20:30:58 +08:00
|
|
|
B3_PROFILE("m_batchSolveKernel iterations2");
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int iter = 0; iter < m_nIterations; iter++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int ib = 0; ib < B3_SOLVER_N_BATCHES; ib++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
cdata.z = ib;
|
2019-01-03 21:26:51 +08:00
|
|
|
|
|
|
|
b3BufferInfoCL bInfo[] = {
|
|
|
|
b3BufferInfoCL(bodyBuf->getBufferCL()),
|
|
|
|
b3BufferInfoCL(shapeBuf->getBufferCL()),
|
|
|
|
b3BufferInfoCL(constraint->getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_numConstraints->getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_offsets->getBufferCL())
|
2017-08-01 20:30:58 +08:00
|
|
|
#ifdef DEBUG_ME
|
2019-01-03 21:26:51 +08:00
|
|
|
,
|
|
|
|
b3BufferInfoCL(&gpuDebugInfo)
|
|
|
|
#endif //DEBUG_ME
|
2017-08-01 20:30:58 +08:00
|
|
|
};
|
2019-01-03 21:26:51 +08:00
|
|
|
b3LauncherCL launcher(m_queue, m_solveFrictionKernel, "m_solveFrictionKernel");
|
|
|
|
launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 20:30:58 +08:00
|
|
|
//launcher.setConst( cdata.x );
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.setConst(cdata.y);
|
|
|
|
launcher.setConst(cdata.z);
|
|
|
|
b3Int4 nSplit;
|
2017-08-01 20:30:58 +08:00
|
|
|
nSplit.x = B3_SOLVER_N_SPLIT_X;
|
|
|
|
nSplit.y = B3_SOLVER_N_SPLIT_Y;
|
|
|
|
nSplit.z = B3_SOLVER_N_SPLIT_Z;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.setConst(nSplit);
|
|
|
|
|
|
|
|
launcher.launch1D(64 * nn / B3_SOLVER_N_BATCHES, 64);
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
delete[] debugInfo;
|
2019-01-03 21:26:51 +08:00
|
|
|
#endif //DEBUG_ME
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
void b3Solver::convertToConstraints(const b3OpenCLArray<b3RigidBodyData>* bodyBuf,
|
|
|
|
const b3OpenCLArray<b3InertiaData>* shapeBuf,
|
|
|
|
b3OpenCLArray<b3Contact4>* contactsIn, b3OpenCLArray<b3GpuConstraint4>* contactCOut, void* additionalData,
|
|
|
|
int nContacts, const ConstraintCfg& cfg)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
// b3OpenCLArray<b3GpuConstraint4>* constraintNative =0;
|
2017-08-01 20:30:58 +08:00
|
|
|
contactCOut->resize(nContacts);
|
|
|
|
struct CB
|
|
|
|
{
|
|
|
|
int m_nContacts;
|
|
|
|
float m_dt;
|
|
|
|
float m_positionDrift;
|
|
|
|
float m_positionConstraintCoeff;
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
CB cdata;
|
|
|
|
cdata.m_nContacts = nContacts;
|
|
|
|
cdata.m_dt = cfg.m_dt;
|
|
|
|
cdata.m_positionDrift = cfg.m_positionDrift;
|
|
|
|
cdata.m_positionConstraintCoeff = cfg.m_positionConstraintCoeff;
|
|
|
|
|
|
|
|
if (gConvertConstraintOnCpu)
|
|
|
|
{
|
|
|
|
b3AlignedObjectArray<b3RigidBodyData> gBodies;
|
2019-01-03 21:26:51 +08:00
|
|
|
bodyBuf->copyToHost(gBodies);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3AlignedObjectArray<b3Contact4> gContact;
|
|
|
|
contactsIn->copyToHost(gContact);
|
|
|
|
|
|
|
|
b3AlignedObjectArray<b3InertiaData> gShapes;
|
|
|
|
shapeBuf->copyToHost(gShapes);
|
|
|
|
|
|
|
|
b3AlignedObjectArray<b3GpuConstraint4> gConstraintOut;
|
|
|
|
gConstraintOut.resize(nContacts);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
B3_PROFILE("cpu contactToConstraintKernel");
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int gIdx = 0; gIdx < nContacts; gIdx++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
int aIdx = abs(gContact[gIdx].m_bodyAPtrAndSignBit);
|
|
|
|
int bIdx = abs(gContact[gIdx].m_bodyBPtrAndSignBit);
|
|
|
|
|
|
|
|
b3Float4 posA = gBodies[aIdx].m_pos;
|
|
|
|
b3Float4 linVelA = gBodies[aIdx].m_linVel;
|
|
|
|
b3Float4 angVelA = gBodies[aIdx].m_angVel;
|
|
|
|
float invMassA = gBodies[aIdx].m_invMass;
|
|
|
|
b3Mat3x3 invInertiaA = gShapes[aIdx].m_initInvInertia;
|
|
|
|
|
|
|
|
b3Float4 posB = gBodies[bIdx].m_pos;
|
|
|
|
b3Float4 linVelB = gBodies[bIdx].m_linVel;
|
|
|
|
b3Float4 angVelB = gBodies[bIdx].m_angVel;
|
|
|
|
float invMassB = gBodies[bIdx].m_invMass;
|
|
|
|
b3Mat3x3 invInertiaB = gShapes[bIdx].m_initInvInertia;
|
|
|
|
|
|
|
|
b3ContactConstraint4_t cs;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
setConstraint4(posA, linVelA, angVelA, invMassA, invInertiaA, posB, linVelB, angVelB, invMassB, invInertiaB,
|
|
|
|
&gContact[gIdx], cdata.m_dt, cdata.m_positionDrift, cdata.m_positionConstraintCoeff,
|
|
|
|
&cs);
|
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
cs.m_batchIdx = gContact[gIdx].m_batchIdx;
|
|
|
|
|
|
|
|
gConstraintOut[gIdx] = (b3GpuConstraint4&)cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
contactCOut->copyFromHost(gConstraintOut);
|
2019-01-03 21:26:51 +08:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
B3_PROFILE("gpu m_contactToConstraintKernel");
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3BufferInfoCL bInfo[] = {b3BufferInfoCL(contactsIn->getBufferCL()), b3BufferInfoCL(bodyBuf->getBufferCL()), b3BufferInfoCL(shapeBuf->getBufferCL()),
|
|
|
|
b3BufferInfoCL(contactCOut->getBufferCL())};
|
|
|
|
b3LauncherCL launcher(m_queue, m_contactToConstraintKernel, "m_contactToConstraintKernel");
|
|
|
|
launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 20:30:58 +08:00
|
|
|
//launcher.setConst( cdata );
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
launcher.setConst(cdata.m_nContacts);
|
|
|
|
launcher.setConst(cdata.m_dt);
|
|
|
|
launcher.setConst(cdata.m_positionDrift);
|
|
|
|
launcher.setConst(cdata.m_positionConstraintCoeff);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.launch1D(nContacts, 64);
|
|
|
|
clFinish(m_queue);
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
void b3Solver::sortContacts( const b3OpenCLArray<b3RigidBodyData>* bodyBuf,
|
|
|
|
b3OpenCLArray<b3Contact4>* contactsIn, void* additionalData,
|
|
|
|
int nContacts, const b3Solver::ConstraintCfg& cfg )
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int sortAlignment = 512; // todo. get this out of sort
|
|
|
|
if( cfg.m_enableParallelSolve )
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
int sortSize = NEXTMULTIPLEOF( nContacts, sortAlignment );
|
|
|
|
|
|
|
|
b3OpenCLArray<unsigned int>* countsNative = m_numConstraints;//BufferUtils::map<TYPE_CL, false>( data->m_device, &countsHost );
|
|
|
|
b3OpenCLArray<unsigned int>* offsetsNative = m_offsets;//BufferUtils::map<TYPE_CL, false>( data->m_device, &offsetsHost );
|
|
|
|
|
|
|
|
{ // 2. set cell idx
|
|
|
|
struct CB
|
|
|
|
{
|
|
|
|
int m_nContacts;
|
|
|
|
int m_staticIdx;
|
|
|
|
float m_scale;
|
|
|
|
int m_nSplit;
|
|
|
|
};
|
|
|
|
|
|
|
|
b3Assert( sortSize%64 == 0 );
|
|
|
|
CB cdata;
|
|
|
|
cdata.m_nContacts = nContacts;
|
|
|
|
cdata.m_staticIdx = cfg.m_staticIdx;
|
|
|
|
cdata.m_scale = 1.f/(N_OBJ_PER_SPLIT*cfg.m_averageExtent);
|
|
|
|
cdata.m_nSplit = B3_SOLVER_N_SPLIT;
|
|
|
|
|
|
|
|
|
|
|
|
b3BufferInfoCL bInfo[] = { b3BufferInfoCL( contactsIn->getBufferCL() ), b3BufferInfoCL( bodyBuf->getBufferCL() ), b3BufferInfoCL( m_sortDataBuffer->getBufferCL() ) };
|
|
|
|
b3LauncherCL launcher( m_queue, m_setSortDataKernel );
|
|
|
|
launcher.setBuffers( bInfo, sizeof(bInfo)/sizeof(b3BufferInfoCL) );
|
|
|
|
launcher.setConst( cdata );
|
|
|
|
launcher.launch1D( sortSize, 64 );
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // 3. sort by cell idx
|
|
|
|
int n = B3_SOLVER_N_SPLIT*B3_SOLVER_N_SPLIT;
|
|
|
|
int sortBit = 32;
|
|
|
|
//if( n <= 0xffff ) sortBit = 16;
|
|
|
|
//if( n <= 0xff ) sortBit = 8;
|
|
|
|
m_sort32->execute(*m_sortDataBuffer,sortSize);
|
|
|
|
}
|
|
|
|
{ // 4. find entries
|
|
|
|
m_search->execute( *m_sortDataBuffer, nContacts, *countsNative, B3_SOLVER_N_SPLIT*B3_SOLVER_N_SPLIT, b3BoundSearchCL::COUNT);
|
|
|
|
|
|
|
|
m_scan->execute( *countsNative, *offsetsNative, B3_SOLVER_N_SPLIT*B3_SOLVER_N_SPLIT );
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // 5. sort constraints by cellIdx
|
|
|
|
// todo. preallocate this
|
|
|
|
// b3Assert( contactsIn->getType() == TYPE_HOST );
|
|
|
|
// b3OpenCLArray<b3Contact4>* out = BufferUtils::map<TYPE_CL, false>( data->m_device, contactsIn ); // copying contacts to this buffer
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
b3Int4 cdata; cdata.x = nContacts;
|
|
|
|
b3BufferInfoCL bInfo[] = { b3BufferInfoCL( contactsIn->getBufferCL() ), b3BufferInfoCL( m_contactBuffer->getBufferCL() ), b3BufferInfoCL( m_sortDataBuffer->getBufferCL() ) };
|
|
|
|
b3LauncherCL launcher( m_queue, m_reorderContactKernel );
|
|
|
|
launcher.setBuffers( bInfo, sizeof(bInfo)/sizeof(b3BufferInfoCL) );
|
|
|
|
launcher.setConst( cdata );
|
|
|
|
launcher.launch1D( nContacts, 64 );
|
|
|
|
}
|
|
|
|
// BufferUtils::unmap<true>( out, contactsIn, nContacts );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
2019-01-03 21:26:51 +08:00
|
|
|
void b3Solver::batchContacts(b3OpenCLArray<b3Contact4>* contacts, int nContacts, b3OpenCLArray<unsigned int>* nNative, b3OpenCLArray<unsigned int>* offsetsNative, int staticIdx)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
int numWorkItems = 64 * B3_SOLVER_N_CELLS;
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
B3_PROFILE("batch generation");
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
b3Int4 cdata;
|
|
|
|
cdata.x = nContacts;
|
|
|
|
cdata.y = 0;
|
|
|
|
cdata.z = staticIdx;
|
|
|
|
|
|
|
|
#ifdef BATCH_DEBUG
|
2019-01-03 21:26:51 +08:00
|
|
|
SolverDebugInfo* debugInfo = new SolverDebugInfo[numWorkItems];
|
|
|
|
adl::b3OpenCLArray<SolverDebugInfo> gpuDebugInfo(data->m_device, numWorkItems);
|
|
|
|
memset(debugInfo, 0, sizeof(SolverDebugInfo) * numWorkItems);
|
|
|
|
gpuDebugInfo.write(debugInfo, numWorkItems);
|
2017-08-01 20:30:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
b3BufferInfoCL bInfo[] = {
|
|
|
|
b3BufferInfoCL( contacts->getBufferCL() ),
|
|
|
|
b3BufferInfoCL( m_contactBuffer2->getBufferCL()),
|
|
|
|
b3BufferInfoCL( nNative->getBufferCL() ),
|
|
|
|
b3BufferInfoCL( offsetsNative->getBufferCL() ),
|
|
|
|
#ifdef BATCH_DEBUG
|
|
|
|
, b3BufferInfoCL(&gpuDebugInfo)
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
m_batchSizes.resize(nNative->size());
|
|
|
|
B3_PROFILE("batchingKernel");
|
|
|
|
//b3LauncherCL launcher( m_queue, m_batchingKernel);
|
|
|
|
cl_kernel k = useNewBatchingKernel ? m_batchingKernelNew : m_batchingKernel;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
b3LauncherCL launcher(m_queue, k, "*batchingKernel");
|
|
|
|
if (!useNewBatchingKernel)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.setBuffer(contacts->getBufferCL());
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.setBuffer(m_contactBuffer2->getBufferCL());
|
|
|
|
launcher.setBuffer(nNative->getBufferCL());
|
|
|
|
launcher.setBuffer(offsetsNative->getBufferCL());
|
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
launcher.setBuffer(m_batchSizes.getBufferCL());
|
|
|
|
|
|
|
|
//launcher.setConst( cdata );
|
2019-01-03 21:26:51 +08:00
|
|
|
launcher.setConst(staticIdx);
|
|
|
|
|
|
|
|
launcher.launch1D(numWorkItems, 64);
|
2017-08-01 20:30:58 +08:00
|
|
|
//clFinish(m_queue);
|
|
|
|
//b3AlignedObjectArray<int> batchSizesCPU;
|
|
|
|
//m_batchSizes.copyToHost(batchSizesCPU);
|
|
|
|
//printf(".\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BATCH_DEBUG
|
2019-01-03 21:26:51 +08:00
|
|
|
aaaa
|
|
|
|
b3Contact4* hostContacts = new b3Contact4[nContacts];
|
|
|
|
m_contactBuffer->read(hostContacts, nContacts);
|
2017-08-01 20:30:58 +08:00
|
|
|
clFinish(m_queue);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
gpuDebugInfo.read(debugInfo, numWorkItems);
|
2017-08-01 20:30:58 +08:00
|
|
|
clFinish(m_queue);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int i = 0; i < numWorkItems; i++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
if (debugInfo[i].m_valInt1 > 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
printf("catch\n");
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
if (debugInfo[i].m_valInt2 > 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
printf("catch22\n");
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (debugInfo[i].m_valInt3 > 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
printf("catch666\n");
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (debugInfo[i].m_valInt4 > 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
printf("catch777\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] debugInfo;
|
2019-01-03 21:26:51 +08:00
|
|
|
#endif //BATCH_DEBUG
|
2017-08-01 20:30:58 +08:00
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
// copy buffer to buffer
|
2017-08-01 20:30:58 +08:00
|
|
|
//b3Assert(m_contactBuffer->size()==nContacts);
|
|
|
|
//contacts->copyFromOpenCLArray( *m_contactBuffer);
|
|
|
|
//clFinish(m_queue);//needed?
|
|
|
|
}
|