2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright (c) 2013 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 Erwin Coumans
|
|
|
|
|
|
|
|
bool useGpuInitSolverBodies = true;
|
|
|
|
bool useGpuInfo1 = true;
|
2019-01-03 13:26:51 +00:00
|
|
|
bool useGpuInfo2 = true;
|
|
|
|
bool useGpuSolveJointConstraintRows = true;
|
2017-08-01 12:30:58 +00:00
|
|
|
bool useGpuWriteBackVelocities = true;
|
|
|
|
bool gpuBreakConstraints = true;
|
|
|
|
|
|
|
|
#include "b3GpuPgsConstraintSolver.h"
|
|
|
|
|
|
|
|
#include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
|
|
|
|
|
|
|
|
#include "Bullet3Dynamics/ConstraintSolver/b3TypedConstraint.h"
|
|
|
|
#include <new>
|
|
|
|
#include "Bullet3Common/b3AlignedObjectArray.h"
|
2019-01-03 13:26:51 +00:00
|
|
|
#include <string.h> //for memset
|
2017-08-01 12:30:58 +00:00
|
|
|
#include "Bullet3Collision/NarrowPhaseCollision/b3Contact4.h"
|
|
|
|
#include "Bullet3OpenCL/ParallelPrimitives/b3OpenCLArray.h"
|
|
|
|
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
|
|
|
|
|
|
|
#include "Bullet3OpenCL/ParallelPrimitives/b3PrefixScanCL.h"
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
#include "Bullet3OpenCL/RigidBody/kernels/jointSolver.h" //solveConstraintRowsCL
|
2017-08-01 12:30:58 +00:00
|
|
|
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
|
|
|
|
|
|
|
#define B3_JOINT_SOLVER_PATH "src/Bullet3OpenCL/RigidBody/kernels/jointSolver.cl"
|
|
|
|
|
|
|
|
struct b3GpuPgsJacobiSolverInternalData
|
|
|
|
{
|
|
|
|
cl_context m_context;
|
|
|
|
cl_device_id m_device;
|
|
|
|
cl_command_queue m_queue;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3PrefixScanCL* m_prefixScan;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
cl_kernel m_solveJointConstraintRowsKernels;
|
|
|
|
cl_kernel m_initSolverBodiesKernel;
|
|
|
|
cl_kernel m_getInfo1Kernel;
|
|
|
|
cl_kernel m_initBatchConstraintsKernel;
|
|
|
|
cl_kernel m_getInfo2Kernel;
|
|
|
|
cl_kernel m_writeBackVelocitiesKernel;
|
|
|
|
cl_kernel m_breakViolatedConstraintsKernel;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3OpenCLArray<unsigned int>* m_gpuConstraintRowOffsets;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3OpenCLArray<b3GpuSolverBody>* m_gpuSolverBodies;
|
|
|
|
b3OpenCLArray<b3BatchConstraint>* m_gpuBatchConstraints;
|
|
|
|
b3OpenCLArray<b3GpuSolverConstraint>* m_gpuConstraintRows;
|
|
|
|
b3OpenCLArray<unsigned int>* m_gpuConstraintInfo1;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// b3AlignedObjectArray<b3GpuSolverBody> m_cpuSolverBodies;
|
|
|
|
b3AlignedObjectArray<b3BatchConstraint> m_cpuBatchConstraints;
|
|
|
|
b3AlignedObjectArray<b3GpuSolverConstraint> m_cpuConstraintRows;
|
|
|
|
b3AlignedObjectArray<unsigned int> m_cpuConstraintInfo1;
|
|
|
|
b3AlignedObjectArray<unsigned int> m_cpuConstraintRowOffsets;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3AlignedObjectArray<b3RigidBodyData> m_cpuBodies;
|
|
|
|
b3AlignedObjectArray<b3InertiaData> m_cpuInertias;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
b3AlignedObjectArray<b3GpuGenericConstraint> m_cpuConstraints;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3AlignedObjectArray<int> m_batchSizes;
|
2017-08-01 12:30:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
static b3Transform getWorldTransform(b3RigidBodyData* rb)
|
|
|
|
{
|
|
|
|
b3Transform newTrans;
|
|
|
|
newTrans.setOrigin(rb->m_pos);
|
|
|
|
newTrans.setRotation(rb->m_quat);
|
|
|
|
return newTrans;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const b3Matrix3x3& getInvInertiaTensorWorld(b3InertiaData* inertia)
|
|
|
|
{
|
|
|
|
return inertia->m_invInertiaWorld;
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
static const b3Vector3& getLinearVelocity(b3RigidBodyData* rb)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return rb->m_linVel;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
static const b3Vector3& getAngularVelocity(b3RigidBodyData* rb)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return rb->m_angVel;
|
|
|
|
}
|
|
|
|
|
|
|
|
b3Vector3 getVelocityInLocalPoint(b3RigidBodyData* rb, const b3Vector3& rel_pos)
|
|
|
|
{
|
|
|
|
//we also calculate lin/ang velocity for kinematic objects
|
|
|
|
return getLinearVelocity(rb) + getAngularVelocity(rb).cross(rel_pos);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3GpuPgsConstraintSolver::b3GpuPgsConstraintSolver(cl_context ctx, cl_device_id device, cl_command_queue queue, bool usePgs)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_usePgs = usePgs;
|
|
|
|
m_gpuData = new b3GpuPgsJacobiSolverInternalData();
|
|
|
|
m_gpuData->m_context = ctx;
|
|
|
|
m_gpuData->m_device = device;
|
|
|
|
m_gpuData->m_queue = queue;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
m_gpuData->m_prefixScan = new b3PrefixScanCL(ctx, device, queue);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
m_gpuData->m_gpuConstraintRowOffsets = new b3OpenCLArray<unsigned int>(m_gpuData->m_context, m_gpuData->m_queue);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
m_gpuData->m_gpuSolverBodies = new b3OpenCLArray<b3GpuSolverBody>(m_gpuData->m_context, m_gpuData->m_queue);
|
|
|
|
m_gpuData->m_gpuBatchConstraints = new b3OpenCLArray<b3BatchConstraint>(m_gpuData->m_context, m_gpuData->m_queue);
|
|
|
|
m_gpuData->m_gpuConstraintRows = new b3OpenCLArray<b3GpuSolverConstraint>(m_gpuData->m_context, m_gpuData->m_queue);
|
|
|
|
m_gpuData->m_gpuConstraintInfo1 = new b3OpenCLArray<unsigned int>(m_gpuData->m_context, m_gpuData->m_queue);
|
|
|
|
cl_int errNum = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
cl_program prog = b3OpenCLUtils::compileCLProgramFromString(m_gpuData->m_context, m_gpuData->m_device, solveConstraintRowsCL, &errNum, "", B3_JOINT_SOLVER_PATH);
|
2017-08-01 12:30:58 +00:00
|
|
|
//cl_program prog = b3OpenCLUtils::compileCLProgramFromString(m_gpuData->m_context,m_gpuData->m_device,0,&errNum,"",B3_JOINT_SOLVER_PATH,true);
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(errNum == CL_SUCCESS);
|
|
|
|
m_gpuData->m_solveJointConstraintRowsKernels = b3OpenCLUtils::compileCLKernelFromString(m_gpuData->m_context, m_gpuData->m_device, solveConstraintRowsCL, "solveJointConstraintRows", &errNum, prog);
|
|
|
|
b3Assert(errNum == CL_SUCCESS);
|
|
|
|
m_gpuData->m_initSolverBodiesKernel = b3OpenCLUtils::compileCLKernelFromString(m_gpuData->m_context, m_gpuData->m_device, solveConstraintRowsCL, "initSolverBodies", &errNum, prog);
|
|
|
|
b3Assert(errNum == CL_SUCCESS);
|
|
|
|
m_gpuData->m_getInfo1Kernel = b3OpenCLUtils::compileCLKernelFromString(m_gpuData->m_context, m_gpuData->m_device, solveConstraintRowsCL, "getInfo1Kernel", &errNum, prog);
|
|
|
|
b3Assert(errNum == CL_SUCCESS);
|
|
|
|
m_gpuData->m_initBatchConstraintsKernel = b3OpenCLUtils::compileCLKernelFromString(m_gpuData->m_context, m_gpuData->m_device, solveConstraintRowsCL, "initBatchConstraintsKernel", &errNum, prog);
|
|
|
|
b3Assert(errNum == CL_SUCCESS);
|
|
|
|
m_gpuData->m_getInfo2Kernel = b3OpenCLUtils::compileCLKernelFromString(m_gpuData->m_context, m_gpuData->m_device, solveConstraintRowsCL, "getInfo2Kernel", &errNum, prog);
|
|
|
|
b3Assert(errNum == CL_SUCCESS);
|
|
|
|
m_gpuData->m_writeBackVelocitiesKernel = b3OpenCLUtils::compileCLKernelFromString(m_gpuData->m_context, m_gpuData->m_device, solveConstraintRowsCL, "writeBackVelocitiesKernel", &errNum, prog);
|
|
|
|
b3Assert(errNum == CL_SUCCESS);
|
|
|
|
m_gpuData->m_breakViolatedConstraintsKernel = b3OpenCLUtils::compileCLKernelFromString(m_gpuData->m_context, m_gpuData->m_device, solveConstraintRowsCL, "breakViolatedConstraintsKernel", &errNum, prog);
|
|
|
|
b3Assert(errNum == CL_SUCCESS);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
clReleaseProgram(prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3GpuPgsConstraintSolver::~b3GpuPgsConstraintSolver()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
clReleaseKernel(m_gpuData->m_solveJointConstraintRowsKernels);
|
|
|
|
clReleaseKernel(m_gpuData->m_initSolverBodiesKernel);
|
|
|
|
clReleaseKernel(m_gpuData->m_getInfo1Kernel);
|
|
|
|
clReleaseKernel(m_gpuData->m_initBatchConstraintsKernel);
|
|
|
|
clReleaseKernel(m_gpuData->m_getInfo2Kernel);
|
|
|
|
clReleaseKernel(m_gpuData->m_writeBackVelocitiesKernel);
|
|
|
|
clReleaseKernel(m_gpuData->m_breakViolatedConstraintsKernel);
|
|
|
|
|
|
|
|
delete m_gpuData->m_prefixScan;
|
|
|
|
delete m_gpuData->m_gpuConstraintRowOffsets;
|
|
|
|
delete m_gpuData->m_gpuSolverBodies;
|
|
|
|
delete m_gpuData->m_gpuBatchConstraints;
|
|
|
|
delete m_gpuData->m_gpuConstraintRows;
|
|
|
|
delete m_gpuData->m_gpuConstraintInfo1;
|
|
|
|
|
|
|
|
delete m_gpuData;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct b3BatchConstraint
|
|
|
|
{
|
|
|
|
int m_bodyAPtrAndSignBit;
|
|
|
|
int m_bodyBPtrAndSignBit;
|
|
|
|
int m_originalConstraintIndex;
|
|
|
|
int m_batchId;
|
|
|
|
};
|
|
|
|
|
|
|
|
static b3AlignedObjectArray<b3BatchConstraint> batchConstraints;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void b3GpuPgsConstraintSolver::recomputeBatches()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_gpuData->m_batchSizes.clear();
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Scalar b3GpuPgsConstraintSolver::solveGroupCacheFriendlySetup(b3OpenCLArray<b3RigidBodyData>* gpuBodies, b3OpenCLArray<b3InertiaData>* gpuInertias, int numBodies, b3OpenCLArray<b3GpuGenericConstraint>* gpuConstraints, int numConstraints, const b3ContactSolverInfo& infoGlobal)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
B3_PROFILE("GPU solveGroupCacheFriendlySetup");
|
|
|
|
batchConstraints.resize(numConstraints);
|
|
|
|
m_gpuData->m_gpuBatchConstraints->resize(numConstraints);
|
|
|
|
m_staticIdx = -1;
|
|
|
|
m_maxOverrideNumSolverIterations = 0;
|
|
|
|
|
|
|
|
/* m_gpuData->m_gpuBodies->resize(numBodies);
|
|
|
|
m_gpuData->m_gpuBodies->copyFromHostPointer(bodies,numBodies);
|
|
|
|
|
|
|
|
b3OpenCLArray<b3InertiaData> gpuInertias(m_gpuData->m_context,m_gpuData->m_queue);
|
|
|
|
gpuInertias.resize(numBodies);
|
|
|
|
gpuInertias.copyFromHostPointer(inertias,numBodies);
|
|
|
|
*/
|
|
|
|
|
|
|
|
m_gpuData->m_gpuSolverBodies->resize(numBodies);
|
|
|
|
|
|
|
|
m_tmpSolverBodyPool.resize(numBodies);
|
|
|
|
{
|
|
|
|
if (useGpuInitSolverBodies)
|
|
|
|
{
|
|
|
|
B3_PROFILE("m_initSolverBodiesKernel");
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_initSolverBodiesKernel, "m_initSolverBodiesKernel");
|
2017-08-01 12:30:58 +00:00
|
|
|
launcher.setBuffer(m_gpuData->m_gpuSolverBodies->getBufferCL());
|
|
|
|
launcher.setBuffer(gpuBodies->getBufferCL());
|
|
|
|
launcher.setConst(numBodies);
|
|
|
|
launcher.launch1D(numBodies);
|
|
|
|
clFinish(m_gpuData->m_queue);
|
|
|
|
|
|
|
|
// m_gpuData->m_gpuSolverBodies->copyToHost(m_tmpSolverBodyPool);
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
gpuBodies->copyToHost(m_gpuData->m_cpuBodies);
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numBodies; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3RigidBodyData& body = m_gpuData->m_cpuBodies[i];
|
|
|
|
b3GpuSolverBody& solverBody = m_tmpSolverBodyPool[i];
|
2019-01-03 13:26:51 +00:00
|
|
|
initSolverBody(i, &solverBody, &body);
|
2017-08-01 12:30:58 +00:00
|
|
|
solverBody.m_originalBodyIndex = i;
|
|
|
|
}
|
|
|
|
m_gpuData->m_gpuSolverBodies->copyFromHost(m_tmpSolverBodyPool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// int totalBodies = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
int totalNumRows = 0;
|
|
|
|
//b3RigidBody* rb0=0,*rb1=0;
|
|
|
|
//if (1)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// int i;
|
|
|
|
|
|
|
|
m_tmpConstraintSizesPool.resizeNoInitialize(numConstraints);
|
|
|
|
|
|
|
|
// b3OpenCLArray<b3GpuGenericConstraint> gpuConstraints(m_gpuData->m_context,m_gpuData->m_queue);
|
|
|
|
|
|
|
|
if (useGpuInfo1)
|
|
|
|
{
|
|
|
|
B3_PROFILE("info1 and init batchConstraint");
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
m_gpuData->m_gpuConstraintInfo1->resize(numConstraints);
|
|
|
|
|
|
|
|
if (1)
|
|
|
|
{
|
|
|
|
B3_PROFILE("getInfo1Kernel");
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_getInfo1Kernel, "m_getInfo1Kernel");
|
2017-08-01 12:30:58 +00:00
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintInfo1->getBufferCL());
|
|
|
|
launcher.setBuffer(gpuConstraints->getBufferCL());
|
|
|
|
launcher.setConst(numConstraints);
|
|
|
|
launcher.launch1D(numConstraints);
|
|
|
|
clFinish(m_gpuData->m_queue);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_gpuData->m_batchSizes.size() == 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
B3_PROFILE("initBatchConstraintsKernel");
|
|
|
|
|
|
|
|
m_gpuData->m_gpuConstraintRowOffsets->resize(numConstraints);
|
2019-01-03 13:26:51 +00:00
|
|
|
unsigned int total = 0;
|
|
|
|
m_gpuData->m_prefixScan->execute(*m_gpuData->m_gpuConstraintInfo1, *m_gpuData->m_gpuConstraintRowOffsets, numConstraints, &total);
|
|
|
|
unsigned int lastElem = m_gpuData->m_gpuConstraintInfo1->at(numConstraints - 1);
|
|
|
|
totalNumRows = total + lastElem;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
B3_PROFILE("init batch constraints");
|
2019-01-03 13:26:51 +00:00
|
|
|
b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_initBatchConstraintsKernel, "m_initBatchConstraintsKernel");
|
2017-08-01 12:30:58 +00:00
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintInfo1->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintRowOffsets->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuBatchConstraints->getBufferCL());
|
|
|
|
launcher.setBuffer(gpuConstraints->getBufferCL());
|
|
|
|
launcher.setBuffer(gpuBodies->getBufferCL());
|
|
|
|
launcher.setConst(numConstraints);
|
|
|
|
launcher.launch1D(numConstraints);
|
|
|
|
clFinish(m_gpuData->m_queue);
|
|
|
|
}
|
|
|
|
//assume the batching happens on CPU, so copy the data
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyToHost(batchConstraints);
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
else
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
totalNumRows = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
gpuConstraints->copyToHost(m_gpuData->m_cpuConstraints);
|
|
|
|
//calculate the total number of contraint rows
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numConstraints; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
unsigned int& info1 = m_tmpConstraintSizesPool[i];
|
2017-08-01 12:30:58 +00:00
|
|
|
// unsigned int info1;
|
|
|
|
if (m_gpuData->m_cpuConstraints[i].isEnabled())
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_gpuData->m_cpuConstraints[i].getInfo1(&info1, &m_gpuData->m_cpuBodies[0]);
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
info1 = 0;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
totalNumRows += info1;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyFromHost(batchConstraints);
|
|
|
|
m_gpuData->m_gpuConstraintInfo1->copyFromHost(m_tmpConstraintSizesPool);
|
|
|
|
}
|
|
|
|
m_tmpSolverNonContactConstraintPool.resizeNoInitialize(totalNumRows);
|
|
|
|
m_gpuData->m_gpuConstraintRows->resize(totalNumRows);
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
// b3GpuConstraintArray verify;
|
|
|
|
|
|
|
|
if (useGpuInfo2)
|
|
|
|
{
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
B3_PROFILE("getInfo2Kernel");
|
|
|
|
b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_getInfo2Kernel, "m_getInfo2Kernel");
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintRows->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintInfo1->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintRowOffsets->getBufferCL());
|
|
|
|
launcher.setBuffer(gpuConstraints->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuBatchConstraints->getBufferCL());
|
|
|
|
launcher.setBuffer(gpuBodies->getBufferCL());
|
|
|
|
launcher.setBuffer(gpuInertias->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuSolverBodies->getBufferCL());
|
|
|
|
launcher.setConst(infoGlobal.m_timeStep);
|
|
|
|
launcher.setConst(infoGlobal.m_erp);
|
|
|
|
launcher.setConst(infoGlobal.m_globalCfm);
|
|
|
|
launcher.setConst(infoGlobal.m_damping);
|
|
|
|
launcher.setConst(infoGlobal.m_numIterations);
|
|
|
|
launcher.setConst(numConstraints);
|
|
|
|
launcher.launch1D(numConstraints);
|
|
|
|
clFinish(m_gpuData->m_queue);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_gpuData->m_batchSizes.size() == 0)
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyToHost(batchConstraints);
|
|
|
|
//m_gpuData->m_gpuConstraintRows->copyToHost(verify);
|
|
|
|
//m_gpuData->m_gpuConstraintRows->copyToHost(m_tmpSolverNonContactConstraintPool);
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gpuInertias->copyToHost(m_gpuData->m_cpuInertias);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
///setup the b3SolverConstraints
|
|
|
|
|
|
|
|
for (int i = 0; i < numConstraints; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const int& info1 = m_tmpConstraintSizesPool[i];
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
if (info1)
|
|
|
|
{
|
|
|
|
int constraintIndex = batchConstraints[i].m_originalConstraintIndex;
|
|
|
|
int constraintRowOffset = m_gpuData->m_cpuConstraintRowOffsets[constraintIndex];
|
|
|
|
|
|
|
|
b3GpuSolverConstraint* currentConstraintRow = &m_tmpSolverNonContactConstraintPool[constraintRowOffset];
|
|
|
|
b3GpuGenericConstraint& constraint = m_gpuData->m_cpuConstraints[i];
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3RigidBodyData& rbA = m_gpuData->m_cpuBodies[constraint.getRigidBodyA()];
|
2017-08-01 12:30:58 +00:00
|
|
|
//b3RigidBody& rbA = constraint.getRigidBodyA();
|
2019-01-03 13:26:51 +00:00
|
|
|
// b3RigidBody& rbB = constraint.getRigidBodyB();
|
|
|
|
b3RigidBodyData& rbB = m_gpuData->m_cpuBodies[constraint.getRigidBodyB()];
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
int solverBodyIdA = constraint.getRigidBodyA(); //getOrInitSolverBody(constraint.getRigidBodyA(),bodies,inertias);
|
|
|
|
int solverBodyIdB = constraint.getRigidBodyB(); //getOrInitSolverBody(constraint.getRigidBodyB(),bodies,inertias);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
b3GpuSolverBody* bodyAPtr = &m_tmpSolverBodyPool[solverBodyIdA];
|
|
|
|
b3GpuSolverBody* bodyBPtr = &m_tmpSolverBodyPool[solverBodyIdB];
|
|
|
|
|
|
|
|
if (rbA.m_invMass)
|
|
|
|
{
|
|
|
|
batchConstraints[i].m_bodyAPtrAndSignBit = solverBodyIdA;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (!solverBodyIdA)
|
|
|
|
m_staticIdx = 0;
|
|
|
|
batchConstraints[i].m_bodyAPtrAndSignBit = -solverBodyIdA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rbB.m_invMass)
|
|
|
|
{
|
|
|
|
batchConstraints[i].m_bodyBPtrAndSignBit = solverBodyIdB;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (!solverBodyIdB)
|
|
|
|
m_staticIdx = 0;
|
|
|
|
batchConstraints[i].m_bodyBPtrAndSignBit = -solverBodyIdB;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
int overrideNumSolverIterations = 0; //constraint->getOverrideNumSolverIterations() > 0 ? constraint->getOverrideNumSolverIterations() : infoGlobal.m_numIterations;
|
|
|
|
if (overrideNumSolverIterations > m_maxOverrideNumSolverIterations)
|
2017-08-01 12:30:58 +00:00
|
|
|
m_maxOverrideNumSolverIterations = overrideNumSolverIterations;
|
|
|
|
|
|
|
|
int j;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (j = 0; j < info1; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
memset(¤tConstraintRow[j], 0, sizeof(b3GpuSolverConstraint));
|
|
|
|
currentConstraintRow[j].m_angularComponentA.setValue(0, 0, 0);
|
|
|
|
currentConstraintRow[j].m_angularComponentB.setValue(0, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
currentConstraintRow[j].m_appliedImpulse = 0.f;
|
|
|
|
currentConstraintRow[j].m_appliedPushImpulse = 0.f;
|
|
|
|
currentConstraintRow[j].m_cfm = 0.f;
|
2019-01-03 13:26:51 +00:00
|
|
|
currentConstraintRow[j].m_contactNormal.setValue(0, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
currentConstraintRow[j].m_friction = 0.f;
|
|
|
|
currentConstraintRow[j].m_frictionIndex = 0;
|
|
|
|
currentConstraintRow[j].m_jacDiagABInv = 0.f;
|
|
|
|
currentConstraintRow[j].m_lowerLimit = 0.f;
|
|
|
|
currentConstraintRow[j].m_upperLimit = 0.f;
|
|
|
|
|
|
|
|
currentConstraintRow[j].m_originalContactPoint = 0;
|
|
|
|
currentConstraintRow[j].m_overrideNumSolverIterations = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
currentConstraintRow[j].m_relpos1CrossNormal.setValue(0, 0, 0);
|
|
|
|
currentConstraintRow[j].m_relpos2CrossNormal.setValue(0, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
currentConstraintRow[j].m_rhs = 0.f;
|
|
|
|
currentConstraintRow[j].m_rhsPenetration = 0.f;
|
|
|
|
currentConstraintRow[j].m_solverBodyIdA = 0;
|
|
|
|
currentConstraintRow[j].m_solverBodyIdB = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
currentConstraintRow[j].m_lowerLimit = -B3_INFINITY;
|
|
|
|
currentConstraintRow[j].m_upperLimit = B3_INFINITY;
|
|
|
|
currentConstraintRow[j].m_appliedImpulse = 0.f;
|
|
|
|
currentConstraintRow[j].m_appliedPushImpulse = 0.f;
|
|
|
|
currentConstraintRow[j].m_solverBodyIdA = solverBodyIdA;
|
|
|
|
currentConstraintRow[j].m_solverBodyIdB = solverBodyIdB;
|
|
|
|
currentConstraintRow[j].m_overrideNumSolverIterations = overrideNumSolverIterations;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyAPtr->internalGetDeltaLinearVelocity().setValue(0.f, 0.f, 0.f);
|
|
|
|
bodyAPtr->internalGetDeltaAngularVelocity().setValue(0.f, 0.f, 0.f);
|
|
|
|
bodyAPtr->internalGetPushVelocity().setValue(0.f, 0.f, 0.f);
|
|
|
|
bodyAPtr->internalGetTurnVelocity().setValue(0.f, 0.f, 0.f);
|
|
|
|
bodyBPtr->internalGetDeltaLinearVelocity().setValue(0.f, 0.f, 0.f);
|
|
|
|
bodyBPtr->internalGetDeltaAngularVelocity().setValue(0.f, 0.f, 0.f);
|
|
|
|
bodyBPtr->internalGetPushVelocity().setValue(0.f, 0.f, 0.f);
|
|
|
|
bodyBPtr->internalGetTurnVelocity().setValue(0.f, 0.f, 0.f);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
b3GpuConstraintInfo2 info2;
|
2019-01-03 13:26:51 +00:00
|
|
|
info2.fps = 1.f / infoGlobal.m_timeStep;
|
2017-08-01 12:30:58 +00:00
|
|
|
info2.erp = infoGlobal.m_erp;
|
|
|
|
info2.m_J1linearAxis = currentConstraintRow->m_contactNormal;
|
|
|
|
info2.m_J1angularAxis = currentConstraintRow->m_relpos1CrossNormal;
|
|
|
|
info2.m_J2linearAxis = 0;
|
|
|
|
info2.m_J2angularAxis = currentConstraintRow->m_relpos2CrossNormal;
|
2019-01-03 13:26:51 +00:00
|
|
|
info2.rowskip = sizeof(b3GpuSolverConstraint) / sizeof(b3Scalar); //check this
|
2017-08-01 12:30:58 +00:00
|
|
|
///the size of b3GpuSolverConstraint needs be a multiple of b3Scalar
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(info2.rowskip * sizeof(b3Scalar) == sizeof(b3GpuSolverConstraint));
|
2017-08-01 12:30:58 +00:00
|
|
|
info2.m_constraintError = ¤tConstraintRow->m_rhs;
|
|
|
|
currentConstraintRow->m_cfm = infoGlobal.m_globalCfm;
|
|
|
|
info2.m_damping = infoGlobal.m_damping;
|
|
|
|
info2.cfm = ¤tConstraintRow->m_cfm;
|
|
|
|
info2.m_lowerLimit = ¤tConstraintRow->m_lowerLimit;
|
|
|
|
info2.m_upperLimit = ¤tConstraintRow->m_upperLimit;
|
|
|
|
info2.m_numIterations = infoGlobal.m_numIterations;
|
2019-01-03 13:26:51 +00:00
|
|
|
m_gpuData->m_cpuConstraints[i].getInfo2(&info2, &m_gpuData->m_cpuBodies[0]);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
///finalize the constraint setup
|
2019-01-03 13:26:51 +00:00
|
|
|
for (j = 0; j < info1; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3GpuSolverConstraint& solverConstraint = currentConstraintRow[j];
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (solverConstraint.m_upperLimit >= m_gpuData->m_cpuConstraints[i].getBreakingImpulseThreshold())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
solverConstraint.m_upperLimit = m_gpuData->m_cpuConstraints[i].getBreakingImpulseThreshold();
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (solverConstraint.m_lowerLimit <= -m_gpuData->m_cpuConstraints[i].getBreakingImpulseThreshold())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
solverConstraint.m_lowerLimit = -m_gpuData->m_cpuConstraints[i].getBreakingImpulseThreshold();
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// solverConstraint.m_originalContactPoint = constraint;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Matrix3x3& invInertiaWorldA = m_gpuData->m_cpuInertias[constraint.getRigidBodyA()].m_invInertiaWorld;
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
//b3Vector3 angularFactorA(1,1,1);
|
|
|
|
const b3Vector3& ftorqueAxis1 = solverConstraint.m_relpos1CrossNormal;
|
2019-01-03 13:26:51 +00:00
|
|
|
solverConstraint.m_angularComponentA = invInertiaWorldA * ftorqueAxis1; //*angularFactorA;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Matrix3x3& invInertiaWorldB = m_gpuData->m_cpuInertias[constraint.getRigidBodyB()].m_invInertiaWorld;
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
const b3Vector3& ftorqueAxis2 = solverConstraint.m_relpos2CrossNormal;
|
2019-01-03 13:26:51 +00:00
|
|
|
solverConstraint.m_angularComponentB = invInertiaWorldB * ftorqueAxis2; //*constraint.getRigidBodyB().getAngularFactor();
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
//it is ok to use solverConstraint.m_contactNormal instead of -solverConstraint.m_contactNormal
|
|
|
|
//because it gets multiplied iMJlB
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Vector3 iMJlA = solverConstraint.m_contactNormal * rbA.m_invMass;
|
|
|
|
b3Vector3 iMJaA = invInertiaWorldA * solverConstraint.m_relpos1CrossNormal;
|
|
|
|
b3Vector3 iMJlB = solverConstraint.m_contactNormal * rbB.m_invMass; //sign of normal?
|
|
|
|
b3Vector3 iMJaB = invInertiaWorldB * solverConstraint.m_relpos2CrossNormal;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
b3Scalar sum = iMJlA.dot(solverConstraint.m_contactNormal);
|
|
|
|
sum += iMJaA.dot(solverConstraint.m_relpos1CrossNormal);
|
|
|
|
sum += iMJlB.dot(solverConstraint.m_contactNormal);
|
|
|
|
sum += iMJaB.dot(solverConstraint.m_relpos2CrossNormal);
|
|
|
|
b3Scalar fsum = b3Fabs(sum);
|
|
|
|
b3Assert(fsum > B3_EPSILON);
|
2019-01-03 13:26:51 +00:00
|
|
|
solverConstraint.m_jacDiagABInv = fsum > B3_EPSILON ? b3Scalar(1.) / sum : 0.f;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///fix rhs
|
|
|
|
///todo: add force/torque accelerators
|
|
|
|
{
|
|
|
|
b3Scalar rel_vel;
|
|
|
|
b3Scalar vel1Dotn = solverConstraint.m_contactNormal.dot(rbA.m_linVel) + solverConstraint.m_relpos1CrossNormal.dot(rbA.m_angVel);
|
|
|
|
b3Scalar vel2Dotn = -solverConstraint.m_contactNormal.dot(rbB.m_linVel) + solverConstraint.m_relpos2CrossNormal.dot(rbB.m_angVel);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
rel_vel = vel1Dotn + vel2Dotn;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
b3Scalar restitution = 0.f;
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Scalar positionalError = solverConstraint.m_rhs; //already filled in by getConstraintInfo2
|
|
|
|
b3Scalar velocityError = restitution - rel_vel * info2.m_damping;
|
|
|
|
b3Scalar penetrationImpulse = positionalError * solverConstraint.m_jacDiagABInv;
|
|
|
|
b3Scalar velocityImpulse = velocityError * solverConstraint.m_jacDiagABInv;
|
|
|
|
solverConstraint.m_rhs = penetrationImpulse + velocityImpulse;
|
2017-08-01 12:30:58 +00:00
|
|
|
solverConstraint.m_appliedImpulse = 0.f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_gpuData->m_gpuConstraintRows->copyFromHost(m_tmpSolverNonContactConstraintPool);
|
|
|
|
m_gpuData->m_gpuConstraintInfo1->copyFromHost(m_tmpConstraintSizesPool);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_gpuData->m_batchSizes.size() == 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
m_gpuData->m_gpuBatchConstraints->copyFromHost(batchConstraints);
|
|
|
|
else
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyToHost(batchConstraints);
|
|
|
|
|
|
|
|
m_gpuData->m_gpuSolverBodies->copyFromHost(m_tmpSolverBodyPool);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
} //end useGpuInfo2
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef B3_SUPPORT_CONTACT_CONSTRAINTS
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = 0; i < numManifolds; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3Contact4& manifold = manifoldPtr[i];
|
2019-01-03 13:26:51 +00:00
|
|
|
convertContact(bodies, inertias, &manifold, infoGlobal);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //B3_SUPPORT_CONTACT_CONSTRAINTS
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// b3ContactSolverInfo info = infoGlobal;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// int numNonContactPool = m_tmpSolverNonContactConstraintPool.size();
|
|
|
|
// int numConstraintPool = m_tmpSolverContactConstraintPool.size();
|
|
|
|
// int numFrictionPool = m_tmpSolverContactFrictionConstraintPool.size();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
///a straight copy from GPU/OpenCL kernel, for debugging
|
2019-01-03 13:26:51 +00:00
|
|
|
__inline void internalApplyImpulse(b3GpuSolverBody* body, const b3Vector3& linearComponent, const b3Vector3& angularComponent, float impulseMagnitude)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
body->m_deltaLinearVelocity += linearComponent * impulseMagnitude * body->m_linearFactor;
|
|
|
|
body->m_deltaAngularVelocity += angularComponent * (impulseMagnitude * body->m_angularFactor);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void resolveSingleConstraintRowGeneric2(b3GpuSolverBody* body1, b3GpuSolverBody* body2, b3GpuSolverConstraint* c)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
float deltaImpulse = c->m_rhs - b3Scalar(c->m_appliedImpulse) * c->m_cfm;
|
|
|
|
float deltaVel1Dotn = b3Dot(c->m_contactNormal, body1->m_deltaLinearVelocity) + b3Dot(c->m_relpos1CrossNormal, body1->m_deltaAngularVelocity);
|
|
|
|
float deltaVel2Dotn = -b3Dot(c->m_contactNormal, body2->m_deltaLinearVelocity) + b3Dot(c->m_relpos2CrossNormal, body2->m_deltaAngularVelocity);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
deltaImpulse -= deltaVel1Dotn * c->m_jacDiagABInv;
|
|
|
|
deltaImpulse -= deltaVel2Dotn * c->m_jacDiagABInv;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
float sum = b3Scalar(c->m_appliedImpulse) + deltaImpulse;
|
|
|
|
if (sum < c->m_lowerLimit)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
deltaImpulse = c->m_lowerLimit - b3Scalar(c->m_appliedImpulse);
|
2017-08-01 12:30:58 +00:00
|
|
|
c->m_appliedImpulse = c->m_lowerLimit;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (sum > c->m_upperLimit)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
deltaImpulse = c->m_upperLimit - b3Scalar(c->m_appliedImpulse);
|
2017-08-01 12:30:58 +00:00
|
|
|
c->m_appliedImpulse = c->m_upperLimit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->m_appliedImpulse = sum;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
internalApplyImpulse(body1, c->m_contactNormal * body1->m_invMass, c->m_angularComponentA, deltaImpulse);
|
|
|
|
internalApplyImpulse(body2, -c->m_contactNormal * body2->m_invMass, c->m_angularComponentB, deltaImpulse);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void b3GpuPgsConstraintSolver::initSolverBody(int bodyIndex, b3GpuSolverBody* solverBody, b3RigidBodyData* rb)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
solverBody->m_deltaLinearVelocity.setValue(0.f, 0.f, 0.f);
|
|
|
|
solverBody->m_deltaAngularVelocity.setValue(0.f, 0.f, 0.f);
|
|
|
|
solverBody->internalGetPushVelocity().setValue(0.f, 0.f, 0.f);
|
|
|
|
solverBody->internalGetTurnVelocity().setValue(0.f, 0.f, 0.f);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
b3Assert(rb);
|
2019-01-03 13:26:51 +00:00
|
|
|
// solverBody->m_worldTransform = getWorldTransform(rb);
|
|
|
|
solverBody->internalSetInvMass(b3MakeVector3(rb->m_invMass, rb->m_invMass, rb->m_invMass));
|
2017-08-01 12:30:58 +00:00
|
|
|
solverBody->m_originalBodyIndex = bodyIndex;
|
2019-01-03 13:26:51 +00:00
|
|
|
solverBody->m_angularFactor = b3MakeVector3(1, 1, 1);
|
|
|
|
solverBody->m_linearFactor = b3MakeVector3(1, 1, 1);
|
2017-08-01 12:30:58 +00:00
|
|
|
solverBody->m_linearVelocity = getLinearVelocity(rb);
|
|
|
|
solverBody->m_angularVelocity = getAngularVelocity(rb);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void b3GpuPgsConstraintSolver::averageVelocities()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Scalar b3GpuPgsConstraintSolver::solveGroupCacheFriendlyIterations(b3OpenCLArray<b3GpuGenericConstraint>* gpuConstraints1, int numConstraints, const b3ContactSolverInfo& infoGlobal)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//only create the batches once.
|
|
|
|
//@todo: incrementally update batches when constraints are added/activated and/or removed/deactivated
|
|
|
|
B3_PROFILE("GpuSolveGroupCacheFriendlyIterations");
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool createBatches = m_gpuData->m_batchSizes.size() == 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (createBatches)
|
|
|
|
{
|
|
|
|
m_gpuData->m_batchSizes.resize(0);
|
|
|
|
|
|
|
|
{
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyToHost(batchConstraints);
|
|
|
|
|
|
|
|
B3_PROFILE("batch joints");
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(batchConstraints.size() == numConstraints);
|
|
|
|
int simdWidth = numConstraints + 1;
|
2017-08-01 12:30:58 +00:00
|
|
|
int numBodies = m_tmpSolverBodyPool.size();
|
2019-01-03 13:26:51 +00:00
|
|
|
sortConstraintByBatch3(&batchConstraints[0], numConstraints, simdWidth, m_staticIdx, numBodies);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyFromHost(batchConstraints);
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
/*b3AlignedObjectArray<b3BatchConstraint> cpuCheckBatches;
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyToHost(cpuCheckBatches);
|
|
|
|
b3Assert(cpuCheckBatches.size()==batchConstraints.size());
|
|
|
|
printf(".\n");
|
|
|
|
*/
|
|
|
|
//>copyFromHost(batchConstraints);
|
|
|
|
}
|
|
|
|
int maxIterations = infoGlobal.m_numIterations;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
bool useBatching = true;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (useBatching)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (!useGpuSolveJointConstraintRows)
|
|
|
|
{
|
|
|
|
B3_PROFILE("copy to host");
|
|
|
|
m_gpuData->m_gpuSolverBodies->copyToHost(m_tmpSolverBodyPool);
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyToHost(batchConstraints);
|
|
|
|
m_gpuData->m_gpuConstraintRows->copyToHost(m_tmpSolverNonContactConstraintPool);
|
|
|
|
m_gpuData->m_gpuConstraintInfo1->copyToHost(m_gpuData->m_cpuConstraintInfo1);
|
|
|
|
m_gpuData->m_gpuConstraintRowOffsets->copyToHost(m_gpuData->m_cpuConstraintRowOffsets);
|
|
|
|
gpuConstraints1->copyToHost(m_gpuData->m_cpuConstraints);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int iteration = 0; iteration < maxIterations; iteration++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int batchOffset = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
int constraintOffset = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
int numBatches = m_gpuData->m_batchSizes.size();
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int bb = 0; bb < numBatches; bb++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int numConstraintsInBatch = m_gpuData->m_batchSizes[bb];
|
|
|
|
|
|
|
|
if (useGpuSolveJointConstraintRows)
|
|
|
|
{
|
|
|
|
B3_PROFILE("solveJointConstraintRowsKernels");
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
__kernel void solveJointConstraintRows(__global b3GpuSolverBody* solverBodies,
|
|
|
|
__global b3BatchConstraint* batchConstraints,
|
|
|
|
__global b3SolverConstraint* rows,
|
|
|
|
__global unsigned int* numConstraintRowsInfo1,
|
|
|
|
__global unsigned int* rowOffsets,
|
|
|
|
__global b3GpuGenericConstraint* constraints,
|
|
|
|
int batchOffset,
|
|
|
|
int numConstraintsInBatch*/
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_solveJointConstraintRowsKernels, "m_solveJointConstraintRowsKernels");
|
2017-08-01 12:30:58 +00:00
|
|
|
launcher.setBuffer(m_gpuData->m_gpuSolverBodies->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuBatchConstraints->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintRows->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintInfo1->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintRowOffsets->getBufferCL());
|
2019-01-03 13:26:51 +00:00
|
|
|
launcher.setBuffer(gpuConstraints1->getBufferCL()); //to detect disabled constraints
|
2017-08-01 12:30:58 +00:00
|
|
|
launcher.setConst(batchOffset);
|
|
|
|
launcher.setConst(numConstraintsInBatch);
|
|
|
|
|
|
|
|
launcher.launch1D(numConstraintsInBatch);
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else //useGpu
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int b = 0; b < numConstraintsInBatch; b++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const b3BatchConstraint& c = batchConstraints[batchOffset + b];
|
2017-08-01 12:30:58 +00:00
|
|
|
/*printf("-----------\n");
|
|
|
|
printf("bb=%d\n",bb);
|
|
|
|
printf("c.batchId = %d\n", c.m_batchId);
|
|
|
|
*/
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(c.m_batchId == bb);
|
2017-08-01 12:30:58 +00:00
|
|
|
b3GpuGenericConstraint* constraint = &m_gpuData->m_cpuConstraints[c.m_originalConstraintIndex];
|
2019-01-03 13:26:51 +00:00
|
|
|
if (constraint->m_flags & B3_CONSTRAINT_FLAG_ENABLED)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int numConstraintRows = m_gpuData->m_cpuConstraintInfo1[c.m_originalConstraintIndex];
|
|
|
|
int constraintOffset = m_gpuData->m_cpuConstraintRowOffsets[c.m_originalConstraintIndex];
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
for (int jj = 0; jj < numConstraintRows; jj++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
//
|
|
|
|
b3GpuSolverConstraint& constraint = m_tmpSolverNonContactConstraintPool[constraintOffset + jj];
|
2017-08-01 12:30:58 +00:00
|
|
|
//resolveSingleConstraintRowGenericSIMD(m_tmpSolverBodyPool[constraint.m_solverBodyIdA],m_tmpSolverBodyPool[constraint.m_solverBodyIdB],constraint);
|
2019-01-03 13:26:51 +00:00
|
|
|
resolveSingleConstraintRowGeneric2(&m_tmpSolverBodyPool[constraint.m_solverBodyIdA], &m_tmpSolverBodyPool[constraint.m_solverBodyIdB], &constraint);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
} //useGpu
|
|
|
|
batchOffset += numConstraintsInBatch;
|
|
|
|
constraintOffset += numConstraintsInBatch;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
} //for (int iteration...
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (!useGpuSolveJointConstraintRows)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
B3_PROFILE("copy from host");
|
|
|
|
m_gpuData->m_gpuSolverBodies->copyFromHost(m_tmpSolverBodyPool);
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyFromHost(batchConstraints);
|
|
|
|
m_gpuData->m_gpuConstraintRows->copyFromHost(m_tmpSolverNonContactConstraintPool);
|
|
|
|
}
|
|
|
|
|
|
|
|
//B3_PROFILE("copy to host");
|
|
|
|
//m_gpuData->m_gpuSolverBodies->copyToHost(m_tmpSolverBodyPool);
|
|
|
|
}
|
|
|
|
//int sz = sizeof(b3GpuSolverBody);
|
|
|
|
//printf("cpu sizeof(b3GpuSolverBody)=%d\n",sz);
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int iteration = 0; iteration < maxIterations; iteration++)
|
|
|
|
{
|
|
|
|
int numJoints = m_tmpSolverNonContactConstraintPool.size();
|
|
|
|
for (int j = 0; j < numJoints; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3GpuSolverConstraint& constraint = m_tmpSolverNonContactConstraintPool[j];
|
2019-01-03 13:26:51 +00:00
|
|
|
resolveSingleConstraintRowGeneric2(&m_tmpSolverBodyPool[constraint.m_solverBodyIdA], &m_tmpSolverBodyPool[constraint.m_solverBodyIdB], &constraint);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_usePgs)
|
|
|
|
{
|
|
|
|
averageVelocities();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clFinish(m_gpuData->m_queue);
|
|
|
|
return 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static b3AlignedObjectArray<int> bodyUsed;
|
|
|
|
static b3AlignedObjectArray<int> curUsed;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
inline int b3GpuPgsConstraintSolver::sortConstraintByBatch3(b3BatchConstraint* cs, int numConstraints, int simdWidth, int staticIdx, int numBodies)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//int sz = sizeof(b3BatchConstraint);
|
|
|
|
|
|
|
|
B3_PROFILE("sortConstraintByBatch3");
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
static int maxSwaps = 0;
|
|
|
|
int numSwaps = 0;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
curUsed.resize(2 * simdWidth);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
static int maxNumConstraints = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (maxNumConstraints < numConstraints)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
maxNumConstraints = numConstraints;
|
|
|
|
//printf("maxNumConstraints = %d\n",maxNumConstraints );
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
int numUsedArray = numBodies / 32 + 1;
|
2017-08-01 12:30:58 +00:00
|
|
|
bodyUsed.resize(numUsedArray);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int q = 0; q < numUsedArray; q++)
|
|
|
|
bodyUsed[q] = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
int curBodyUsed = 0;
|
|
|
|
|
|
|
|
int numIter = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
#if defined(_DEBUG)
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numConstraints; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
cs[i].m_batchId = -1;
|
|
|
|
#endif
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
int numValidConstraints = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
// int unprocessedConstraintIndex = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
int batchIdx = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
B3_PROFILE("cpu batch innerloop");
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
while (numValidConstraints < numConstraints)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
numIter++;
|
|
|
|
int nCurrentBatch = 0;
|
|
|
|
// clear flag
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < curBodyUsed; i++)
|
|
|
|
bodyUsed[curUsed[i] / 32] = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
curBodyUsed = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = numValidConstraints; i < numConstraints; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int idx = i;
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(idx < numConstraints);
|
2017-08-01 12:30:58 +00:00
|
|
|
// check if it can go
|
|
|
|
int bodyAS = cs[idx].m_bodyAPtrAndSignBit;
|
|
|
|
int bodyBS = cs[idx].m_bodyBPtrAndSignBit;
|
|
|
|
int bodyA = abs(bodyAS);
|
|
|
|
int bodyB = abs(bodyBS);
|
2019-01-03 13:26:51 +00:00
|
|
|
bool aIsStatic = (bodyAS < 0) || bodyAS == staticIdx;
|
|
|
|
bool bIsStatic = (bodyBS < 0) || bodyBS == staticIdx;
|
2017-08-01 12:30:58 +00:00
|
|
|
int aUnavailable = 0;
|
|
|
|
int bUnavailable = 0;
|
|
|
|
if (!aIsStatic)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
aUnavailable = bodyUsed[bodyA / 32] & (1 << (bodyA & 31));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
if (!aUnavailable)
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!bIsStatic)
|
|
|
|
{
|
|
|
|
bUnavailable = bodyUsed[bodyB / 32] & (1 << (bodyB & 31));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aUnavailable == 0 && bUnavailable == 0) // ok
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (!aIsStatic)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyUsed[bodyA / 32] |= (1 << (bodyA & 31));
|
|
|
|
curUsed[curBodyUsed++] = bodyA;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
if (!bIsStatic)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyUsed[bodyB / 32] |= (1 << (bodyB & 31));
|
|
|
|
curUsed[curBodyUsed++] = bodyB;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cs[idx].m_batchId = batchIdx;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (i != numValidConstraints)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Swap(cs[i], cs[numValidConstraints]);
|
2017-08-01 12:30:58 +00:00
|
|
|
numSwaps++;
|
|
|
|
}
|
|
|
|
|
|
|
|
numValidConstraints++;
|
|
|
|
{
|
|
|
|
nCurrentBatch++;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (nCurrentBatch == simdWidth)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
nCurrentBatch = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < curBodyUsed; i++)
|
|
|
|
bodyUsed[curUsed[i] / 32] = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
curBodyUsed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_gpuData->m_batchSizes.push_back(nCurrentBatch);
|
2019-01-03 13:26:51 +00:00
|
|
|
batchIdx++;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
#if defined(_DEBUG)
|
2019-01-03 13:26:51 +00:00
|
|
|
// debugPrintf( "nBatches: %d\n", batchIdx );
|
|
|
|
for (int i = 0; i < numConstraints; i++)
|
|
|
|
{
|
|
|
|
b3Assert(cs[i].m_batchId != -1);
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (maxSwaps < numSwaps)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
maxSwaps = numSwaps;
|
|
|
|
//printf("maxSwaps = %d\n", maxSwaps);
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
return batchIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// b3PgsJacobiSolver Sequentially applies impulses
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Scalar b3GpuPgsConstraintSolver::solveGroup(b3OpenCLArray<b3RigidBodyData>* gpuBodies, b3OpenCLArray<b3InertiaData>* gpuInertias,
|
|
|
|
int numBodies, b3OpenCLArray<b3GpuGenericConstraint>* gpuConstraints, int numConstraints, const b3ContactSolverInfo& infoGlobal)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
B3_PROFILE("solveJoints");
|
|
|
|
//you need to provide at least some bodies
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
solveGroupCacheFriendlySetup(gpuBodies, gpuInertias, numBodies, gpuConstraints, numConstraints, infoGlobal);
|
|
|
|
|
|
|
|
solveGroupCacheFriendlyIterations(gpuConstraints, numConstraints, infoGlobal);
|
|
|
|
|
|
|
|
solveGroupCacheFriendlyFinish(gpuBodies, gpuInertias, numBodies, gpuConstraints, numConstraints, infoGlobal);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return 0.f;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void b3GpuPgsConstraintSolver::solveJoints(int numBodies, b3OpenCLArray<b3RigidBodyData>* gpuBodies, b3OpenCLArray<b3InertiaData>* gpuInertias,
|
|
|
|
int numConstraints, b3OpenCLArray<b3GpuGenericConstraint>* gpuConstraints)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3ContactSolverInfo infoGlobal;
|
|
|
|
infoGlobal.m_splitImpulse = false;
|
2019-01-03 13:26:51 +00:00
|
|
|
infoGlobal.m_timeStep = 1.f / 60.f;
|
|
|
|
infoGlobal.m_numIterations = 4; //4;
|
|
|
|
// infoGlobal.m_solverMode|=B3_SOLVER_USE_2_FRICTION_DIRECTIONS|B3_SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS|B3_SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION;
|
2017-08-01 12:30:58 +00:00
|
|
|
//infoGlobal.m_solverMode|=B3_SOLVER_USE_2_FRICTION_DIRECTIONS|B3_SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS;
|
2019-01-03 13:26:51 +00:00
|
|
|
infoGlobal.m_solverMode |= B3_SOLVER_USE_2_FRICTION_DIRECTIONS;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//if (infoGlobal.m_solverMode & B3_SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS)
|
|
|
|
//if ((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS) && (infoGlobal.m_solverMode & B3_SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION))
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
solveGroup(gpuBodies, gpuInertias, numBodies, gpuConstraints, numConstraints, infoGlobal);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//b3AlignedObjectArray<b3RigidBodyData> testBodies;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Scalar b3GpuPgsConstraintSolver::solveGroupCacheFriendlyFinish(b3OpenCLArray<b3RigidBodyData>* gpuBodies, b3OpenCLArray<b3InertiaData>* gpuInertias, int numBodies, b3OpenCLArray<b3GpuGenericConstraint>* gpuConstraints, int numConstraints, const b3ContactSolverInfo& infoGlobal)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
B3_PROFILE("solveGroupCacheFriendlyFinish");
|
2019-01-03 13:26:51 +00:00
|
|
|
// int numPoolConstraints = m_tmpSolverContactConstraintPool.size();
|
|
|
|
// int i,j;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
if (gpuBreakConstraints)
|
|
|
|
{
|
|
|
|
B3_PROFILE("breakViolatedConstraintsKernel");
|
2019-01-03 13:26:51 +00:00
|
|
|
b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_breakViolatedConstraintsKernel, "m_breakViolatedConstraintsKernel");
|
2017-08-01 12:30:58 +00:00
|
|
|
launcher.setBuffer(gpuConstraints->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintInfo1->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintRowOffsets->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuConstraintRows->getBufferCL());
|
|
|
|
launcher.setConst(numConstraints);
|
|
|
|
launcher.launch1D(numConstraints);
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
gpuConstraints->copyToHost(m_gpuData->m_cpuConstraints);
|
|
|
|
m_gpuData->m_gpuBatchConstraints->copyToHost(m_gpuData->m_cpuBatchConstraints);
|
|
|
|
m_gpuData->m_gpuConstraintRows->copyToHost(m_gpuData->m_cpuConstraintRows);
|
|
|
|
gpuConstraints->copyToHost(m_gpuData->m_cpuConstraints);
|
|
|
|
m_gpuData->m_gpuConstraintInfo1->copyToHost(m_gpuData->m_cpuConstraintInfo1);
|
|
|
|
m_gpuData->m_gpuConstraintRowOffsets->copyToHost(m_gpuData->m_cpuConstraintRowOffsets);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int cid = 0; cid < numConstraints; cid++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int originalConstraintIndex = batchConstraints[cid].m_originalConstraintIndex;
|
|
|
|
int constraintRowOffset = m_gpuData->m_cpuConstraintRowOffsets[originalConstraintIndex];
|
|
|
|
int numRows = m_gpuData->m_cpuConstraintInfo1[originalConstraintIndex];
|
|
|
|
if (numRows)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
// printf("cid=%d, breakingThreshold =%f\n",cid,breakingThreshold);
|
|
|
|
for (int i = 0; i < numRows; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
int rowIndex = constraintRowOffset + i;
|
2017-08-01 12:30:58 +00:00
|
|
|
int orgConstraintIndex = m_gpuData->m_cpuConstraintRows[rowIndex].m_originalConstraintIndex;
|
|
|
|
float breakingThreshold = m_gpuData->m_cpuConstraints[orgConstraintIndex].m_breakingImpulseThreshold;
|
2019-01-03 13:26:51 +00:00
|
|
|
// printf("rows[%d].m_appliedImpulse=%f\n",rowIndex,rows[rowIndex].m_appliedImpulse);
|
2017-08-01 12:30:58 +00:00
|
|
|
if (b3Fabs(m_gpuData->m_cpuConstraintRows[rowIndex].m_appliedImpulse) >= breakingThreshold)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_gpuData->m_cpuConstraints[orgConstraintIndex].m_flags = 0; //&= ~B3_CONSTRAINT_FLAG_ENABLED;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gpuConstraints->copyFromHost(m_gpuData->m_cpuConstraints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
if (useGpuWriteBackVelocities)
|
|
|
|
{
|
|
|
|
B3_PROFILE("GPU write back velocities and transforms");
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_writeBackVelocitiesKernel, "m_writeBackVelocitiesKernel");
|
2017-08-01 12:30:58 +00:00
|
|
|
launcher.setBuffer(gpuBodies->getBufferCL());
|
|
|
|
launcher.setBuffer(m_gpuData->m_gpuSolverBodies->getBufferCL());
|
|
|
|
launcher.setConst(numBodies);
|
|
|
|
launcher.launch1D(numBodies);
|
|
|
|
clFinish(m_gpuData->m_queue);
|
2019-01-03 13:26:51 +00:00
|
|
|
// m_gpuData->m_gpuSolverBodies->copyToHost(m_tmpSolverBodyPool);
|
|
|
|
// m_gpuData->m_gpuBodies->copyToHostPointer(bodies,numBodies);
|
2017-08-01 12:30:58 +00:00
|
|
|
//m_gpuData->m_gpuBodies->copyToHost(testBodies);
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
B3_PROFILE("CPU write back velocities and transforms");
|
|
|
|
|
|
|
|
m_gpuData->m_gpuSolverBodies->copyToHost(m_tmpSolverBodyPool);
|
|
|
|
gpuBodies->copyToHost(m_gpuData->m_cpuBodies);
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_tmpSolverBodyPool.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int bodyIndex = m_tmpSolverBodyPool[i].m_originalBodyIndex;
|
|
|
|
//printf("bodyIndex=%d\n",bodyIndex);
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(i == bodyIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
b3RigidBodyData* body = &m_gpuData->m_cpuBodies[bodyIndex];
|
|
|
|
if (body->m_invMass)
|
|
|
|
{
|
|
|
|
if (infoGlobal.m_splitImpulse)
|
|
|
|
m_tmpSolverBodyPool[i].writebackVelocityAndTransform(infoGlobal.m_timeStep, infoGlobal.m_splitImpulseTurnErp);
|
|
|
|
else
|
|
|
|
m_tmpSolverBodyPool[i].writebackVelocity();
|
|
|
|
|
|
|
|
if (m_usePgs)
|
|
|
|
{
|
|
|
|
body->m_linVel = m_tmpSolverBodyPool[i].m_linearVelocity;
|
|
|
|
body->m_angVel = m_tmpSolverBodyPool[i].m_angularVelocity;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3Assert(0);
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
/*
|
2017-08-01 12:30:58 +00:00
|
|
|
if (infoGlobal.m_splitImpulse)
|
|
|
|
{
|
|
|
|
body->m_pos = m_tmpSolverBodyPool[i].m_worldTransform.getOrigin();
|
|
|
|
b3Quaternion orn;
|
|
|
|
orn = m_tmpSolverBodyPool[i].m_worldTransform.getRotation();
|
|
|
|
body->m_quat = orn;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
} //for
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
gpuBodies->copyFromHost(m_gpuData->m_cpuBodies);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clFinish(m_gpuData->m_queue);
|
|
|
|
|
|
|
|
m_tmpSolverContactConstraintPool.resizeNoInitialize(0);
|
|
|
|
m_tmpSolverNonContactConstraintPool.resizeNoInitialize(0);
|
|
|
|
m_tmpSolverContactFrictionConstraintPool.resizeNoInitialize(0);
|
|
|
|
m_tmpSolverContactRollingFrictionConstraintPool.resizeNoInitialize(0);
|
|
|
|
|
|
|
|
m_tmpSolverBodyPool.resizeNoInitialize(0);
|
|
|
|
return 0.f;
|
|
|
|
}
|