updated demos -> ALT + mouse uses Maya-style controls, replaced BMF_Fonts by GLDebugFont
fix debug drawing of btMultiSphereShape added box2d demo added experimental gpu 2d demo
This commit is contained in:
585
src/BulletMultiThreaded/btGpu3DGridBroadphase.cpp
Normal file
585
src/BulletMultiThreaded/btGpu3DGridBroadphase.cpp
Normal file
@@ -0,0 +1,585 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
|
||||
Copyright (C) 2006, 2009 Sony Computer Entertainment 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.
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
#include "LinearMath/btAlignedAllocator.h"
|
||||
#include "LinearMath/btQuickprof.h"
|
||||
#include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
#include "btGpuDefines.h"
|
||||
#include "btGpuUtilsSharedDefs.h"
|
||||
|
||||
#include "btGpu3DGridBroadphaseSharedDefs.h"
|
||||
|
||||
#include "btGpu3DGridBroadphase.h"
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
static bt3DGridBroadphaseParams s3DGridBroadphaseParams;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
btGpu3DGridBroadphase::btGpu3DGridBroadphase( const btVector3& worldAabbMin,const btVector3& worldAabbMax,
|
||||
int gridSizeX, int gridSizeY, int gridSizeZ,
|
||||
int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
|
||||
int maxBodiesPerCell,
|
||||
btScalar cellFactorAABB) :
|
||||
btSimpleBroadphase(maxSmallProxies,
|
||||
// new (btAlignedAlloc(sizeof(btSortedOverlappingPairCache),16)) btSortedOverlappingPairCache),
|
||||
new (btAlignedAlloc(sizeof(btHashedOverlappingPairCache),16)) btHashedOverlappingPairCache),
|
||||
m_bInitialized(false),
|
||||
m_numBodies(0)
|
||||
{
|
||||
_initialize(worldAabbMin, worldAabbMax, gridSizeX, gridSizeY, gridSizeZ,
|
||||
maxSmallProxies, maxLargeProxies, maxPairsPerBody,
|
||||
maxBodiesPerCell, cellFactorAABB);
|
||||
} // btGpu3DGridBroadphase::btGpu3DGridBroadphase()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
btGpu3DGridBroadphase::btGpu3DGridBroadphase( btOverlappingPairCache* overlappingPairCache,
|
||||
const btVector3& worldAabbMin,const btVector3& worldAabbMax,
|
||||
int gridSizeX, int gridSizeY, int gridSizeZ,
|
||||
int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
|
||||
int maxBodiesPerCell,
|
||||
btScalar cellFactorAABB) :
|
||||
btSimpleBroadphase(maxSmallProxies, overlappingPairCache),
|
||||
m_bInitialized(false),
|
||||
m_numBodies(0)
|
||||
{
|
||||
_initialize(worldAabbMin, worldAabbMax, gridSizeX, gridSizeY, gridSizeZ,
|
||||
maxSmallProxies, maxLargeProxies, maxPairsPerBody,
|
||||
maxBodiesPerCell, cellFactorAABB);
|
||||
} // btGpu3DGridBroadphase::btGpu3DGridBroadphase()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
btGpu3DGridBroadphase::~btGpu3DGridBroadphase()
|
||||
{
|
||||
//btSimpleBroadphase will free memory of btSortedOverlappingPairCache, because m_ownsPairCache
|
||||
assert(m_bInitialized);
|
||||
_finalize();
|
||||
} // btGpu3DGridBroadphase::~btGpu3DGridBroadphase()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::_initialize( const btVector3& worldAabbMin,const btVector3& worldAabbMax,
|
||||
int gridSizeX, int gridSizeY, int gridSizeZ,
|
||||
int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
|
||||
int maxBodiesPerCell,
|
||||
btScalar cellFactorAABB)
|
||||
{
|
||||
// set various paramerers
|
||||
m_ownsPairCache = true;
|
||||
m_params.m_gridSizeX = gridSizeX;
|
||||
m_params.m_gridSizeY = gridSizeY;
|
||||
m_params.m_gridSizeZ = gridSizeZ;
|
||||
m_params.m_numCells = m_params.m_gridSizeX * m_params.m_gridSizeY * m_params.m_gridSizeZ;
|
||||
btVector3 w_org = worldAabbMin;
|
||||
m_params.m_worldOriginX = w_org.getX();
|
||||
m_params.m_worldOriginY = w_org.getY();
|
||||
m_params.m_worldOriginZ = w_org.getZ();
|
||||
btVector3 w_size = worldAabbMax - worldAabbMin;
|
||||
m_params.m_cellSizeX = w_size.getX() / m_params.m_gridSizeX;
|
||||
m_params.m_cellSizeY = w_size.getY() / m_params.m_gridSizeY;
|
||||
m_params.m_cellSizeZ = w_size.getZ() / m_params.m_gridSizeZ;
|
||||
m_maxRadius = btMin(btMin(m_params.m_cellSizeX, m_params.m_cellSizeY), m_params.m_cellSizeZ);
|
||||
m_maxRadius *= btScalar(0.5f);
|
||||
m_params.m_numBodies = m_numBodies;
|
||||
m_params.m_maxBodiesPerCell = maxBodiesPerCell;
|
||||
|
||||
m_numLargeHandles = 0;
|
||||
m_maxLargeHandles = maxLargeProxies;
|
||||
|
||||
m_maxPairsPerBody = maxPairsPerBody;
|
||||
|
||||
m_cellFactorAABB = cellFactorAABB;
|
||||
|
||||
m_LastLargeHandleIndex = -1;
|
||||
|
||||
assert(!m_bInitialized);
|
||||
// allocate host storage
|
||||
m_hBodiesHash = new unsigned int[m_maxHandles * 2];
|
||||
memset(m_hBodiesHash, 0x00, m_maxHandles*2*sizeof(unsigned int));
|
||||
|
||||
m_hCellStart = new unsigned int[m_params.m_numCells];
|
||||
memset(m_hCellStart, 0x00, m_params.m_numCells * sizeof(unsigned int));
|
||||
|
||||
m_hPairBuffStartCurr = new unsigned int[m_maxHandles * 2 + 2];
|
||||
// --------------- for now, init with m_maxPairsPerBody for each body
|
||||
m_hPairBuffStartCurr[0] = 0;
|
||||
m_hPairBuffStartCurr[1] = 0;
|
||||
for(int i = 1; i <= m_maxHandles; i++)
|
||||
{
|
||||
m_hPairBuffStartCurr[i * 2] = m_hPairBuffStartCurr[(i-1) * 2] + m_maxPairsPerBody;
|
||||
m_hPairBuffStartCurr[i * 2 + 1] = 0;
|
||||
}
|
||||
//----------------
|
||||
unsigned int numAABB = m_maxHandles + m_maxLargeHandles;
|
||||
m_hAABB = new bt3DGrid3F1U[numAABB * 2]; // AABB Min & Max
|
||||
|
||||
m_hPairBuff = new unsigned int[m_maxHandles * m_maxPairsPerBody];
|
||||
memset(m_hPairBuff, 0x00, m_maxHandles * m_maxPairsPerBody * sizeof(unsigned int)); // needed?
|
||||
|
||||
m_hPairScan = new unsigned int[m_maxHandles + 1];
|
||||
|
||||
m_hPairOut = new unsigned int[m_maxHandles * m_maxPairsPerBody];
|
||||
|
||||
// large proxies
|
||||
|
||||
// allocate handles buffer and put all handles on free list
|
||||
m_pLargeHandlesRawPtr = btAlignedAlloc(sizeof(btSimpleBroadphaseProxy) * m_maxLargeHandles, 16);
|
||||
m_pLargeHandles = new(m_pLargeHandlesRawPtr) btSimpleBroadphaseProxy[m_maxLargeHandles];
|
||||
m_firstFreeLargeHandle = 0;
|
||||
{
|
||||
for (int i = m_firstFreeLargeHandle; i < m_maxLargeHandles; i++)
|
||||
{
|
||||
m_pLargeHandles[i].SetNextFree(i + 1);
|
||||
m_pLargeHandles[i].m_uniqueId = m_maxHandles+2+i;
|
||||
}
|
||||
m_pLargeHandles[m_maxLargeHandles - 1].SetNextFree(0);
|
||||
}
|
||||
|
||||
// debug data
|
||||
m_numPairsAdded = 0;
|
||||
m_numOverflows = 0;
|
||||
|
||||
m_bInitialized = true;
|
||||
} // btGpu3DGridBroadphase::_initialize()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::_finalize()
|
||||
{
|
||||
assert(m_bInitialized);
|
||||
delete [] m_hBodiesHash;
|
||||
delete [] m_hCellStart;
|
||||
delete [] m_hPairBuffStartCurr;
|
||||
delete [] m_hAABB;
|
||||
delete [] m_hPairBuff;
|
||||
delete [] m_hPairScan;
|
||||
delete [] m_hPairOut;
|
||||
btAlignedFree(m_pLargeHandlesRawPtr);
|
||||
m_bInitialized = false;
|
||||
} // btGpu3DGridBroadphase::_finalize()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::calculateOverlappingPairs(btDispatcher* dispatcher)
|
||||
{
|
||||
if(m_numHandles <= 0)
|
||||
{
|
||||
BT_PROFILE("addLarge2LargePairsToCache");
|
||||
addLarge2LargePairsToCache(dispatcher);
|
||||
return;
|
||||
}
|
||||
// update constants
|
||||
setParameters(&m_params);
|
||||
// prepare AABB array
|
||||
prepareAABB();
|
||||
// calculate hash
|
||||
calcHashAABB();
|
||||
// sort bodies based on hash
|
||||
sortHash();
|
||||
// find start of each cell
|
||||
findCellStart();
|
||||
// findOverlappingPairs (small/small)
|
||||
findOverlappingPairs();
|
||||
// findOverlappingPairs (small/large)
|
||||
findPairsLarge();
|
||||
// add pairs to CPU cache
|
||||
computePairCacheChanges();
|
||||
scanOverlappingPairBuff();
|
||||
squeezeOverlappingPairBuff();
|
||||
addPairsToCache(dispatcher);
|
||||
// find and add large/large pairs to CPU cache
|
||||
addLarge2LargePairsToCache(dispatcher);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::calculateOverlappingPairs()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::addPairsToCache(btDispatcher* dispatcher)
|
||||
{
|
||||
m_numPairsAdded = 0;
|
||||
m_numPairsRemoved = 0;
|
||||
for(int i = 0; i < m_numHandles; i++)
|
||||
{
|
||||
unsigned int num = m_hPairScan[i+1] - m_hPairScan[i];
|
||||
if(!num)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
unsigned int* pInp = m_hPairOut + m_hPairScan[i];
|
||||
unsigned int index0 = m_hAABB[i * 2].uw;
|
||||
btSimpleBroadphaseProxy* proxy0 = &m_pHandles[index0];
|
||||
for(unsigned int j = 0; j < num; j++)
|
||||
{
|
||||
unsigned int indx1_s = pInp[j];
|
||||
unsigned int index1 = indx1_s & (~BT_3DGRID_PAIR_ANY_FLG);
|
||||
btSimpleBroadphaseProxy* proxy1;
|
||||
if(index1 < (unsigned int)m_maxHandles)
|
||||
{
|
||||
proxy1 = &m_pHandles[index1];
|
||||
}
|
||||
else
|
||||
{
|
||||
index1 -= m_maxHandles;
|
||||
btAssert((index1 >= 0) && (index1 < (unsigned int)m_maxLargeHandles));
|
||||
proxy1 = &m_pLargeHandles[index1];
|
||||
}
|
||||
if(indx1_s & BT_3DGRID_PAIR_NEW_FLG)
|
||||
{
|
||||
m_pairCache->addOverlappingPair(proxy0,proxy1);
|
||||
m_numPairsAdded++;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_pairCache->removeOverlappingPair(proxy0,proxy1,dispatcher);
|
||||
m_numPairsRemoved++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // btGpu3DGridBroadphase::addPairsToCache()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
btBroadphaseProxy* btGpu3DGridBroadphase::createProxy( const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr ,short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher,void* multiSapProxy)
|
||||
{
|
||||
btBroadphaseProxy* proxy;
|
||||
bool bIsLarge = isLargeProxy(aabbMin, aabbMax);
|
||||
if(bIsLarge)
|
||||
{
|
||||
if (m_numLargeHandles >= m_maxLargeHandles)
|
||||
{
|
||||
///you have to increase the cell size, so 'large' proxies become 'small' proxies (fitting a cell)
|
||||
btAssert(0);
|
||||
return 0; //should never happen, but don't let the game crash ;-)
|
||||
}
|
||||
btAssert((aabbMin[0]<= aabbMax[0]) && (aabbMin[1]<= aabbMax[1]) && (aabbMin[2]<= aabbMax[2]));
|
||||
int newHandleIndex = allocLargeHandle();
|
||||
proxy = new (&m_pLargeHandles[newHandleIndex])btSimpleBroadphaseProxy(aabbMin,aabbMax,shapeType,userPtr,collisionFilterGroup,collisionFilterMask,multiSapProxy);
|
||||
}
|
||||
else
|
||||
{
|
||||
proxy = btSimpleBroadphase::createProxy(aabbMin, aabbMax, shapeType, userPtr, collisionFilterGroup, collisionFilterMask, dispatcher, multiSapProxy);
|
||||
}
|
||||
return proxy;
|
||||
} // btGpu3DGridBroadphase::createProxy()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::destroyProxy(btBroadphaseProxy* proxy, btDispatcher* dispatcher)
|
||||
{
|
||||
bool bIsLarge = isLargeProxy(proxy);
|
||||
if(bIsLarge)
|
||||
{
|
||||
|
||||
btSimpleBroadphaseProxy* proxy0 = static_cast<btSimpleBroadphaseProxy*>(proxy);
|
||||
freeLargeHandle(proxy0);
|
||||
m_pairCache->removeOverlappingPairsContainingProxy(proxy,dispatcher);
|
||||
}
|
||||
else
|
||||
{
|
||||
btSimpleBroadphase::destroyProxy(proxy, dispatcher);
|
||||
}
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::destroyProxy()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::resetPool(btDispatcher* dispatcher)
|
||||
{
|
||||
m_hPairBuffStartCurr[0] = 0;
|
||||
m_hPairBuffStartCurr[1] = 0;
|
||||
for(int i = 1; i <= m_maxHandles; i++)
|
||||
{
|
||||
m_hPairBuffStartCurr[i * 2] = m_hPairBuffStartCurr[(i-1) * 2] + m_maxPairsPerBody;
|
||||
m_hPairBuffStartCurr[i * 2 + 1] = 0;
|
||||
}
|
||||
} // btGpu3DGridBroadphase::resetPool()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
bool btGpu3DGridBroadphase::isLargeProxy(const btVector3& aabbMin, const btVector3& aabbMax)
|
||||
{
|
||||
btVector3 diag = aabbMax - aabbMin;
|
||||
|
||||
///use the bounding sphere radius of this bounding box, to include rotation
|
||||
btScalar radius = diag.length() * btScalar(0.5f);
|
||||
radius *= m_cellFactorAABB; // user-defined factor
|
||||
|
||||
return (radius > m_maxRadius);
|
||||
} // btGpu3DGridBroadphase::isLargeProxy()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
bool btGpu3DGridBroadphase::isLargeProxy(btBroadphaseProxy* proxy)
|
||||
{
|
||||
return (proxy->getUid() >= (m_maxHandles+2));
|
||||
} // btGpu3DGridBroadphase::isLargeProxy()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::addLarge2LargePairsToCache(btDispatcher* dispatcher)
|
||||
{
|
||||
int i,j;
|
||||
if (m_numLargeHandles <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int new_largest_index = -1;
|
||||
for(i = 0; i <= m_LastLargeHandleIndex; i++)
|
||||
{
|
||||
btSimpleBroadphaseProxy* proxy0 = &m_pLargeHandles[i];
|
||||
if(!proxy0->m_clientObject)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
new_largest_index = i;
|
||||
for(j = i + 1; j <= m_LastLargeHandleIndex; j++)
|
||||
{
|
||||
btSimpleBroadphaseProxy* proxy1 = &m_pLargeHandles[j];
|
||||
if(!proxy1->m_clientObject)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
btAssert(proxy0 != proxy1);
|
||||
btSimpleBroadphaseProxy* p0 = getSimpleProxyFromProxy(proxy0);
|
||||
btSimpleBroadphaseProxy* p1 = getSimpleProxyFromProxy(proxy1);
|
||||
if(aabbOverlap(p0,p1))
|
||||
{
|
||||
if (!m_pairCache->findPair(proxy0,proxy1))
|
||||
{
|
||||
m_pairCache->addOverlappingPair(proxy0,proxy1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(m_pairCache->findPair(proxy0,proxy1))
|
||||
{
|
||||
m_pairCache->removeOverlappingPair(proxy0,proxy1,dispatcher);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_LastLargeHandleIndex = new_largest_index;
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::addLarge2LargePairsToCache()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback)
|
||||
{
|
||||
btSimpleBroadphase::rayTest(rayFrom, rayTo, rayCallback);
|
||||
for (int i=0; i <= m_LastLargeHandleIndex; i++)
|
||||
{
|
||||
btSimpleBroadphaseProxy* proxy = &m_pLargeHandles[i];
|
||||
if(!proxy->m_clientObject)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
rayCallback.process(proxy);
|
||||
}
|
||||
} // btGpu3DGridBroadphase::rayTest()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
//--------------------------------------------------------------------------
|
||||
//
|
||||
// overrides for CPU version
|
||||
//
|
||||
//--------------------------------------------------------------------------
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::prepareAABB()
|
||||
{
|
||||
BT_PROFILE("prepareAABB");
|
||||
bt3DGrid3F1U* pBB = m_hAABB;
|
||||
int i;
|
||||
int new_largest_index = -1;
|
||||
unsigned int num_small = 0;
|
||||
for(i = 0; i <= m_LastHandleIndex; i++)
|
||||
{
|
||||
btSimpleBroadphaseProxy* proxy0 = &m_pHandles[i];
|
||||
if(!proxy0->m_clientObject)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
new_largest_index = i;
|
||||
pBB->fx = proxy0->m_aabbMin.getX();
|
||||
pBB->fy = proxy0->m_aabbMin.getY();
|
||||
pBB->fz = proxy0->m_aabbMin.getZ();
|
||||
pBB->uw = i;
|
||||
pBB++;
|
||||
pBB->fx = proxy0->m_aabbMax.getX();
|
||||
pBB->fy = proxy0->m_aabbMax.getY();
|
||||
pBB->fz = proxy0->m_aabbMax.getZ();
|
||||
pBB->uw = num_small;
|
||||
pBB++;
|
||||
num_small++;
|
||||
}
|
||||
m_LastHandleIndex = new_largest_index;
|
||||
new_largest_index = -1;
|
||||
unsigned int num_large = 0;
|
||||
for(i = 0; i <= m_LastLargeHandleIndex; i++)
|
||||
{
|
||||
btSimpleBroadphaseProxy* proxy0 = &m_pLargeHandles[i];
|
||||
if(!proxy0->m_clientObject)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
new_largest_index = i;
|
||||
pBB->fx = proxy0->m_aabbMin.getX();
|
||||
pBB->fy = proxy0->m_aabbMin.getY();
|
||||
pBB->fz = proxy0->m_aabbMin.getZ();
|
||||
pBB->uw = i + m_maxHandles;
|
||||
pBB++;
|
||||
pBB->fx = proxy0->m_aabbMax.getX();
|
||||
pBB->fy = proxy0->m_aabbMax.getY();
|
||||
pBB->fz = proxy0->m_aabbMax.getZ();
|
||||
pBB->uw = num_large + m_maxHandles;
|
||||
pBB++;
|
||||
num_large++;
|
||||
}
|
||||
m_LastLargeHandleIndex = new_largest_index;
|
||||
// paranoid checks
|
||||
btAssert(num_small == m_numHandles);
|
||||
btAssert(num_large == m_numLargeHandles);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::prepareAABB()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::setParameters(bt3DGridBroadphaseParams* hostParams)
|
||||
{
|
||||
s3DGridBroadphaseParams = *hostParams;
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::setParameters()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::calcHashAABB()
|
||||
{
|
||||
BT_PROFILE("bt3DGrid_calcHashAABB");
|
||||
btGpu_calcHashAABB(m_hAABB, m_hBodiesHash, m_numHandles);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::calcHashAABB()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::sortHash()
|
||||
{
|
||||
class bt3DGridHashKey
|
||||
{
|
||||
public:
|
||||
unsigned int hash;
|
||||
unsigned int index;
|
||||
void quickSort(bt3DGridHashKey* pData, int lo, int hi)
|
||||
{
|
||||
int i=lo, j=hi;
|
||||
bt3DGridHashKey x = pData[(lo+hi)/2];
|
||||
do
|
||||
{
|
||||
while(pData[i].hash > x.hash) i++;
|
||||
while(x.hash > pData[j].hash) j--;
|
||||
if(i <= j)
|
||||
{
|
||||
bt3DGridHashKey t = pData[i];
|
||||
pData[i] = pData[j];
|
||||
pData[j] = t;
|
||||
i++; j--;
|
||||
}
|
||||
} while(i <= j);
|
||||
if(lo < j) pData->quickSort(pData, lo, j);
|
||||
if(i < hi) pData->quickSort(pData, i, hi);
|
||||
}
|
||||
};
|
||||
BT_PROFILE("bt3DGrid_sortHash");
|
||||
bt3DGridHashKey* pHash = (bt3DGridHashKey*)m_hBodiesHash;
|
||||
pHash->quickSort(pHash, 0, m_numHandles - 1);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::sortHash()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::findCellStart()
|
||||
{
|
||||
BT_PROFILE("bt3DGrid_findCellStart");
|
||||
btGpu_findCellStart(m_hBodiesHash, m_hCellStart, m_numHandles, m_params.m_numCells);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::findCellStart()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::findOverlappingPairs()
|
||||
{
|
||||
BT_PROFILE("bt3DGrid_findOverlappingPairs");
|
||||
btGpu_findOverlappingPairs(m_hAABB, m_hBodiesHash, m_hCellStart, m_hPairBuff, m_hPairBuffStartCurr, m_numHandles);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::findOverlappingPairs()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::findPairsLarge()
|
||||
{
|
||||
BT_PROFILE("bt3DGrid_findPairsLarge");
|
||||
btGpu_findPairsLarge(m_hAABB, m_hBodiesHash, m_hCellStart, m_hPairBuff, m_hPairBuffStartCurr, m_numHandles, m_numLargeHandles);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::findPairsLarge()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::computePairCacheChanges()
|
||||
{
|
||||
BT_PROFILE("bt3DGrid_computePairCacheChanges");
|
||||
btGpu_computePairCacheChanges(m_hPairBuff, m_hPairBuffStartCurr, m_hPairScan, m_hAABB, m_numHandles);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::computePairCacheChanges()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::scanOverlappingPairBuff()
|
||||
{
|
||||
BT_PROFILE("bt3DGrid_scanOverlappingPairBuff");
|
||||
m_hPairScan[0] = 0;
|
||||
for(int i = 1; i <= m_numHandles; i++)
|
||||
{
|
||||
unsigned int delta = m_hPairScan[i];
|
||||
m_hPairScan[i] = m_hPairScan[i-1] + delta;
|
||||
}
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::scanOverlappingPairBuff()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void btGpu3DGridBroadphase::squeezeOverlappingPairBuff()
|
||||
{
|
||||
BT_PROFILE("bt3DGrid_squeezeOverlappingPairBuff");
|
||||
btGpu_squeezeOverlappingPairBuff(m_hPairBuff, m_hPairBuffStartCurr, m_hPairScan, m_hPairOut, m_hAABB, m_numHandles);
|
||||
return;
|
||||
} // btGpu3DGridBroadphase::squeezeOverlappingPairBuff()
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
#include "btGpu3DGridBroadphaseSharedCode.h"
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
138
src/BulletMultiThreaded/btGpu3DGridBroadphase.h
Normal file
138
src/BulletMultiThreaded/btGpu3DGridBroadphase.h
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
|
||||
Copyright (C) 2006, 2009 Sony Computer Entertainment 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.
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#ifndef BTGPU3DGRIDBROADPHASE_H
|
||||
#define BTGPU3DGRIDBROADPHASE_H
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
|
||||
|
||||
#include "btGpu3DGridBroadphaseSharedTypes.h"
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
///The btGpu3DGridBroadphase uses GPU-style code compiled for CPU to compute overlapping pairs
|
||||
|
||||
class btGpu3DGridBroadphase : public btSimpleBroadphase
|
||||
{
|
||||
protected:
|
||||
bool m_bInitialized;
|
||||
unsigned int m_numBodies;
|
||||
unsigned int m_numCells;
|
||||
unsigned int m_maxPairsPerBody;
|
||||
btScalar m_cellFactorAABB;
|
||||
unsigned int m_maxBodiesPerCell;
|
||||
bt3DGridBroadphaseParams m_params;
|
||||
btScalar m_maxRadius;
|
||||
// CPU data
|
||||
unsigned int* m_hBodiesHash;
|
||||
unsigned int* m_hCellStart;
|
||||
unsigned int* m_hPairBuffStartCurr;
|
||||
bt3DGrid3F1U* m_hAABB;
|
||||
unsigned int* m_hPairBuff;
|
||||
unsigned int* m_hPairScan;
|
||||
unsigned int* m_hPairOut;
|
||||
// large proxies
|
||||
int m_numLargeHandles;
|
||||
int m_maxLargeHandles;
|
||||
int m_LastLargeHandleIndex;
|
||||
btSimpleBroadphaseProxy* m_pLargeHandles;
|
||||
void* m_pLargeHandlesRawPtr;
|
||||
int m_firstFreeLargeHandle;
|
||||
int allocLargeHandle()
|
||||
{
|
||||
btAssert(m_numLargeHandles < m_maxLargeHandles);
|
||||
int freeLargeHandle = m_firstFreeLargeHandle;
|
||||
m_firstFreeLargeHandle = m_pLargeHandles[freeLargeHandle].GetNextFree();
|
||||
m_numLargeHandles++;
|
||||
if(freeLargeHandle > m_LastLargeHandleIndex)
|
||||
{
|
||||
m_LastLargeHandleIndex = freeLargeHandle;
|
||||
}
|
||||
return freeLargeHandle;
|
||||
}
|
||||
void freeLargeHandle(btSimpleBroadphaseProxy* proxy)
|
||||
{
|
||||
int handle = int(proxy - m_pLargeHandles);
|
||||
btAssert((handle >= 0) && (handle < m_maxHandles));
|
||||
if(handle == m_LastLargeHandleIndex)
|
||||
{
|
||||
m_LastLargeHandleIndex--;
|
||||
}
|
||||
proxy->SetNextFree(m_firstFreeLargeHandle);
|
||||
m_firstFreeLargeHandle = handle;
|
||||
proxy->m_clientObject = 0;
|
||||
m_numLargeHandles--;
|
||||
}
|
||||
bool isLargeProxy(const btVector3& aabbMin, const btVector3& aabbMax);
|
||||
bool isLargeProxy(btBroadphaseProxy* proxy);
|
||||
// debug
|
||||
unsigned int m_numPairsAdded;
|
||||
unsigned int m_numPairsRemoved;
|
||||
unsigned int m_numOverflows;
|
||||
//
|
||||
public:
|
||||
btGpu3DGridBroadphase(const btVector3& worldAabbMin,const btVector3& worldAabbMax,
|
||||
int gridSizeX, int gridSizeY, int gridSizeZ,
|
||||
int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
|
||||
int maxBodiesPerCell = 8,
|
||||
btScalar cellFactorAABB = btScalar(1.0f));
|
||||
btGpu3DGridBroadphase( btOverlappingPairCache* overlappingPairCache,
|
||||
const btVector3& worldAabbMin,const btVector3& worldAabbMax,
|
||||
int gridSizeX, int gridSizeY, int gridSizeZ,
|
||||
int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
|
||||
int maxBodiesPerCell = 8,
|
||||
btScalar cellFactorAABB = btScalar(1.0f));
|
||||
virtual ~btGpu3DGridBroadphase();
|
||||
virtual void calculateOverlappingPairs(btDispatcher* dispatcher);
|
||||
|
||||
virtual btBroadphaseProxy* createProxy(const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr ,short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher,void* multiSapProxy);
|
||||
virtual void destroyProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
|
||||
virtual void rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback);
|
||||
virtual void resetPool(btDispatcher* dispatcher);
|
||||
|
||||
protected:
|
||||
void _initialize( const btVector3& worldAabbMin,const btVector3& worldAabbMax,
|
||||
int gridSizeX, int gridSizeY, int gridSizeZ,
|
||||
int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
|
||||
int maxBodiesPerCell = 8,
|
||||
btScalar cellFactorAABB = btScalar(1.0f));
|
||||
void _finalize();
|
||||
void addPairsToCache(btDispatcher* dispatcher);
|
||||
void addLarge2LargePairsToCache(btDispatcher* dispatcher);
|
||||
|
||||
// overrides for CPU version
|
||||
virtual void setParameters(bt3DGridBroadphaseParams* hostParams);
|
||||
virtual void prepareAABB();
|
||||
virtual void calcHashAABB();
|
||||
virtual void sortHash();
|
||||
virtual void findCellStart();
|
||||
virtual void findOverlappingPairs();
|
||||
virtual void findPairsLarge();
|
||||
virtual void computePairCacheChanges();
|
||||
virtual void scanOverlappingPairBuff();
|
||||
virtual void squeezeOverlappingPairBuff();
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#endif //BTGPU3DGRIDBROADPHASE_H
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
430
src/BulletMultiThreaded/btGpu3DGridBroadphaseSharedCode.h
Normal file
430
src/BulletMultiThreaded/btGpu3DGridBroadphaseSharedCode.h
Normal file
@@ -0,0 +1,430 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
|
||||
Copyright (C) 2006, 2009 Sony Computer Entertainment 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.
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
// K E R N E L F U N C T I O N S
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
// calculate position in uniform grid
|
||||
BT_GPU___device__ int3 bt3DGrid_calcGridPos(float4 p)
|
||||
{
|
||||
int3 gridPos;
|
||||
gridPos.x = (int)floor((p.x - BT_GPU_params.m_worldOriginX) / BT_GPU_params.m_cellSizeX);
|
||||
gridPos.y = (int)floor((p.y - BT_GPU_params.m_worldOriginY) / BT_GPU_params.m_cellSizeY);
|
||||
gridPos.z = (int)floor((p.z - BT_GPU_params.m_worldOriginZ) / BT_GPU_params.m_cellSizeZ);
|
||||
return gridPos;
|
||||
} // bt3DGrid_calcGridPos()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
// calculate address in grid from position (clamping to edges)
|
||||
BT_GPU___device__ uint bt3DGrid_calcGridHash(int3 gridPos)
|
||||
{
|
||||
gridPos.x = BT_GPU_max(0, BT_GPU_min(gridPos.x, (int)BT_GPU_params.m_gridSizeX - 1));
|
||||
gridPos.y = BT_GPU_max(0, BT_GPU_min(gridPos.y, (int)BT_GPU_params.m_gridSizeY - 1));
|
||||
gridPos.z = BT_GPU_max(0, BT_GPU_min(gridPos.z, (int)BT_GPU_params.m_gridSizeZ - 1));
|
||||
return BT_GPU___mul24(BT_GPU___mul24(gridPos.z, BT_GPU_params.m_gridSizeY), BT_GPU_params.m_gridSizeX) + BT_GPU___mul24(gridPos.y, BT_GPU_params.m_gridSizeX) + gridPos.x;
|
||||
} // bt3DGrid_calcGridHash()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
// calculate grid hash value for each body using its AABB
|
||||
BT_GPU___global__ void calcHashAABBD(bt3DGrid3F1U* pAABB, uint2* pHash, uint numBodies)
|
||||
{
|
||||
int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
|
||||
if(index >= (int)numBodies)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bt3DGrid3F1U bbMin = pAABB[index*2];
|
||||
bt3DGrid3F1U bbMax = pAABB[index*2 + 1];
|
||||
float4 pos;
|
||||
pos.x = (bbMin.fx + bbMax.fx) * 0.5f;
|
||||
pos.y = (bbMin.fy + bbMax.fy) * 0.5f;
|
||||
pos.z = (bbMin.fz + bbMax.fz) * 0.5f;
|
||||
// get address in grid
|
||||
int3 gridPos = bt3DGrid_calcGridPos(pos);
|
||||
uint gridHash = bt3DGrid_calcGridHash(gridPos);
|
||||
// store grid hash and body index
|
||||
pHash[index] = BT_GPU_make_uint2(gridHash, index);
|
||||
} // calcHashAABBD()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
BT_GPU___global__ void findCellStartD(uint2* pHash, uint* cellStart, uint numBodies)
|
||||
{
|
||||
int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
|
||||
if(index >= (int)numBodies)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint2 sortedData = pHash[index];
|
||||
// Load hash data into shared memory so that we can look
|
||||
// at neighboring body's hash value without loading
|
||||
// two hash values per thread
|
||||
BT_GPU___shared__ uint sharedHash[257];
|
||||
sharedHash[BT_GPU_threadIdx.x+1] = sortedData.x;
|
||||
if((index > 0) && (BT_GPU_threadIdx.x == 0))
|
||||
{
|
||||
// first thread in block must load neighbor body hash
|
||||
volatile uint2 prevData = pHash[index-1];
|
||||
sharedHash[0] = prevData.x;
|
||||
}
|
||||
BT_GPU___syncthreads();
|
||||
if((index == 0) || (sortedData.x != sharedHash[BT_GPU_threadIdx.x]))
|
||||
{
|
||||
cellStart[sortedData.x] = index;
|
||||
}
|
||||
} // findCellStartD()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
BT_GPU___device__ uint cudaTestAABBOverlap(bt3DGrid3F1U min0, bt3DGrid3F1U max0, bt3DGrid3F1U min1, bt3DGrid3F1U max1)
|
||||
{
|
||||
return (min0.fx <= max1.fx)&& (min1.fx <= max0.fx) &&
|
||||
(min0.fy <= max1.fy)&& (min1.fy <= max0.fy) &&
|
||||
(min0.fz <= max1.fz)&& (min1.fz <= max0.fz);
|
||||
} // cudaTestAABBOverlap()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
BT_GPU___device__ void findPairsInCell( int3 gridPos,
|
||||
uint index,
|
||||
uint2* pHash,
|
||||
uint* pCellStart,
|
||||
bt3DGrid3F1U* pAABB,
|
||||
uint* pPairBuff,
|
||||
uint2* pPairBuffStartCurr,
|
||||
uint numBodies)
|
||||
{
|
||||
if ( (gridPos.x < 0) || (gridPos.x > (int)BT_GPU_params.m_gridSizeX - 1)
|
||||
|| (gridPos.y < 0) || (gridPos.y > (int)BT_GPU_params.m_gridSizeY - 1)
|
||||
|| (gridPos.z < 0) || (gridPos.z > (int)BT_GPU_params.m_gridSizeZ - 1))
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint gridHash = bt3DGrid_calcGridHash(gridPos);
|
||||
// get start of bucket for this cell
|
||||
uint bucketStart = pCellStart[gridHash];
|
||||
if (bucketStart == 0xffffffff)
|
||||
{
|
||||
return; // cell empty
|
||||
}
|
||||
// iterate over bodies in this cell
|
||||
uint2 sortedData = pHash[index];
|
||||
uint unsorted_indx = sortedData.y;
|
||||
bt3DGrid3F1U min0 = BT_GPU_FETCH(pAABB, unsorted_indx*2);
|
||||
bt3DGrid3F1U max0 = BT_GPU_FETCH(pAABB, unsorted_indx*2 + 1);
|
||||
uint handleIndex = min0.uw;
|
||||
uint2 start_curr = pPairBuffStartCurr[handleIndex];
|
||||
uint start = start_curr.x;
|
||||
uint curr = start_curr.y;
|
||||
uint2 start_curr_next = pPairBuffStartCurr[handleIndex+1];
|
||||
uint curr_max = start_curr_next.x - start - 1;
|
||||
uint bucketEnd = bucketStart + BT_GPU_params.m_maxBodiesPerCell;
|
||||
bucketEnd = (bucketEnd > numBodies) ? numBodies : bucketEnd;
|
||||
for(uint index2 = bucketStart; index2 < bucketEnd; index2++)
|
||||
{
|
||||
uint2 cellData = pHash[index2];
|
||||
if (cellData.x != gridHash)
|
||||
{
|
||||
break; // no longer in same bucket
|
||||
}
|
||||
uint unsorted_indx2 = cellData.y;
|
||||
if (unsorted_indx2 < unsorted_indx) // check not colliding with self
|
||||
{
|
||||
bt3DGrid3F1U min1 = BT_GPU_FETCH(pAABB, unsorted_indx2*2);
|
||||
bt3DGrid3F1U max1 = BT_GPU_FETCH(pAABB, unsorted_indx2*2 + 1);
|
||||
if(cudaTestAABBOverlap(min0, max0, min1, max1))
|
||||
{
|
||||
uint handleIndex2 = min1.uw;
|
||||
uint k;
|
||||
for(k = 0; k < curr; k++)
|
||||
{
|
||||
uint old_pair = pPairBuff[start+k] & (~BT_3DGRID_PAIR_ANY_FLG);
|
||||
if(old_pair == handleIndex2)
|
||||
{
|
||||
pPairBuff[start+k] |= BT_3DGRID_PAIR_FOUND_FLG;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(k == curr)
|
||||
{
|
||||
if(curr >= curr_max)
|
||||
{ // not a good solution, but let's avoid crash
|
||||
break;
|
||||
}
|
||||
pPairBuff[start+curr] = handleIndex2 | BT_3DGRID_PAIR_NEW_FLG;
|
||||
curr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pPairBuffStartCurr[handleIndex] = BT_GPU_make_uint2(start, curr);
|
||||
return;
|
||||
} // findPairsInCell()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
BT_GPU___global__ void findOverlappingPairsD( bt3DGrid3F1U* pAABB, uint2* pHash, uint* pCellStart,
|
||||
uint* pPairBuff, uint2* pPairBuffStartCurr, uint numBodies)
|
||||
{
|
||||
int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
|
||||
if(index >= (int)numBodies)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint2 sortedData = pHash[index];
|
||||
uint unsorted_indx = sortedData.y;
|
||||
bt3DGrid3F1U bbMin = BT_GPU_FETCH(pAABB, unsorted_indx*2);
|
||||
bt3DGrid3F1U bbMax = BT_GPU_FETCH(pAABB, unsorted_indx*2 + 1);
|
||||
float4 pos;
|
||||
pos.x = (bbMin.fx + bbMax.fx) * 0.5f;
|
||||
pos.y = (bbMin.fy + bbMax.fy) * 0.5f;
|
||||
pos.z = (bbMin.fz + bbMax.fz) * 0.5f;
|
||||
// get address in grid
|
||||
int3 gridPos = bt3DGrid_calcGridPos(pos);
|
||||
// examine only neighbouring cells
|
||||
for(int z=-1; z<=1; z++) {
|
||||
for(int y=-1; y<=1; y++) {
|
||||
for(int x=-1; x<=1; x++) {
|
||||
findPairsInCell(gridPos + BT_GPU_make_int3(x, y, z), index, pHash, pCellStart, pAABB, pPairBuff, pPairBuffStartCurr, numBodies);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // findOverlappingPairsD()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
BT_GPU___global__ void findPairsLargeD( bt3DGrid3F1U* pAABB, uint2* pHash, uint* pCellStart, uint* pPairBuff,
|
||||
uint2* pPairBuffStartCurr, uint numBodies, uint numLarge)
|
||||
{
|
||||
int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
|
||||
if(index >= (int)numBodies)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint2 sortedData = pHash[index];
|
||||
uint unsorted_indx = sortedData.y;
|
||||
bt3DGrid3F1U min0 = BT_GPU_FETCH(pAABB, unsorted_indx*2);
|
||||
bt3DGrid3F1U max0 = BT_GPU_FETCH(pAABB, unsorted_indx*2 + 1);
|
||||
uint handleIndex = min0.uw;
|
||||
uint2 start_curr = pPairBuffStartCurr[handleIndex];
|
||||
uint start = start_curr.x;
|
||||
uint curr = start_curr.y;
|
||||
uint2 start_curr_next = pPairBuffStartCurr[handleIndex+1];
|
||||
uint curr_max = start_curr_next.x - start - 1;
|
||||
for(uint i = 0; i < numLarge; i++)
|
||||
{
|
||||
uint indx2 = numBodies + i;
|
||||
bt3DGrid3F1U min1 = BT_GPU_FETCH(pAABB, indx2*2);
|
||||
bt3DGrid3F1U max1 = BT_GPU_FETCH(pAABB, indx2*2 + 1);
|
||||
if(cudaTestAABBOverlap(min0, max0, min1, max1))
|
||||
{
|
||||
uint k;
|
||||
uint handleIndex2 = min1.uw;
|
||||
for(k = 0; k < curr; k++)
|
||||
{
|
||||
uint old_pair = pPairBuff[start+k] & (~BT_3DGRID_PAIR_ANY_FLG);
|
||||
if(old_pair == handleIndex2)
|
||||
{
|
||||
pPairBuff[start+k] |= BT_3DGRID_PAIR_FOUND_FLG;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(k == curr)
|
||||
{
|
||||
pPairBuff[start+curr] = handleIndex2 | BT_3DGRID_PAIR_NEW_FLG;
|
||||
if(curr >= curr_max)
|
||||
{ // not a good solution, but let's avoid crash
|
||||
break;
|
||||
}
|
||||
curr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
pPairBuffStartCurr[handleIndex] = BT_GPU_make_uint2(start, curr);
|
||||
return;
|
||||
} // findPairsLargeD()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
BT_GPU___global__ void computePairCacheChangesD(uint* pPairBuff, uint2* pPairBuffStartCurr,
|
||||
uint* pPairScan, bt3DGrid3F1U* pAABB, uint numBodies)
|
||||
{
|
||||
int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
|
||||
if(index >= (int)numBodies)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bt3DGrid3F1U bbMin = pAABB[index * 2];
|
||||
uint handleIndex = bbMin.uw;
|
||||
uint2 start_curr = pPairBuffStartCurr[handleIndex];
|
||||
uint start = start_curr.x;
|
||||
uint curr = start_curr.y;
|
||||
uint *pInp = pPairBuff + start;
|
||||
uint num_changes = 0;
|
||||
for(uint k = 0; k < curr; k++, pInp++)
|
||||
{
|
||||
if(!((*pInp) & BT_3DGRID_PAIR_FOUND_FLG))
|
||||
{
|
||||
num_changes++;
|
||||
}
|
||||
}
|
||||
pPairScan[index+1] = num_changes;
|
||||
} // computePairCacheChangesD()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
BT_GPU___global__ void squeezeOverlappingPairBuffD(uint* pPairBuff, uint2* pPairBuffStartCurr, uint* pPairScan,
|
||||
uint* pPairOut, bt3DGrid3F1U* pAABB, uint numBodies)
|
||||
{
|
||||
int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
|
||||
if(index >= (int)numBodies)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bt3DGrid3F1U bbMin = pAABB[index * 2];
|
||||
uint handleIndex = bbMin.uw;
|
||||
uint2 start_curr = pPairBuffStartCurr[handleIndex];
|
||||
uint start = start_curr.x;
|
||||
uint curr = start_curr.y;
|
||||
uint* pInp = pPairBuff + start;
|
||||
uint* pOut = pPairOut + pPairScan[index];
|
||||
uint* pOut2 = pInp;
|
||||
uint num = 0;
|
||||
for(uint k = 0; k < curr; k++, pInp++)
|
||||
{
|
||||
if(!((*pInp) & BT_3DGRID_PAIR_FOUND_FLG))
|
||||
{
|
||||
*pOut = *pInp;
|
||||
pOut++;
|
||||
}
|
||||
if((*pInp) & BT_3DGRID_PAIR_ANY_FLG)
|
||||
{
|
||||
*pOut2 = (*pInp) & (~BT_3DGRID_PAIR_ANY_FLG);
|
||||
pOut2++;
|
||||
num++;
|
||||
}
|
||||
}
|
||||
pPairBuffStartCurr[handleIndex] = BT_GPU_make_uint2(start, num);
|
||||
} // squeezeOverlappingPairBuffD()
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
// E N D O F K E R N E L F U N C T I O N S
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
void BT_GPU_PREF(calcHashAABB)(bt3DGrid3F1U* pAABB, unsigned int* hash, unsigned int numBodies)
|
||||
{
|
||||
int numThreads, numBlocks;
|
||||
BT_GPU_PREF(computeGridSize)(numBodies, 256, numBlocks, numThreads);
|
||||
// execute the kernel
|
||||
BT_GPU_EXECKERNEL(numBlocks, numThreads, calcHashAABBD, (pAABB, (uint2*)hash, numBodies));
|
||||
// check if kernel invocation generated an error
|
||||
BT_GPU_CHECK_ERROR("calcHashAABBD kernel execution failed");
|
||||
} // calcHashAABB()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
void BT_GPU_PREF(findCellStart(unsigned int* hash, unsigned int* cellStart, unsigned int numBodies, unsigned int numCells))
|
||||
{
|
||||
int numThreads, numBlocks;
|
||||
BT_GPU_PREF(computeGridSize)(numBodies, 256, numBlocks, numThreads);
|
||||
BT_GPU_SAFE_CALL(BT_GPU_Memset(cellStart, 0xffffffff, numCells*sizeof(uint)));
|
||||
BT_GPU_EXECKERNEL(numBlocks, numThreads, findCellStartD, ((uint2*)hash, (uint*)cellStart, numBodies));
|
||||
BT_GPU_CHECK_ERROR("Kernel execution failed: findCellStartD");
|
||||
} // findCellStart()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
void BT_GPU_PREF(findOverlappingPairs(bt3DGrid3F1U* pAABB, unsigned int* pHash, unsigned int* pCellStart, unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int numBodies))
|
||||
{
|
||||
#if B_CUDA_USE_TEX
|
||||
BT_GPU_SAFE_CALL(cudaBindTexture(0, pAABBTex, pAABB, numBodies * 2 * sizeof(bt3DGrid3F1U)));
|
||||
#endif
|
||||
int numThreads, numBlocks;
|
||||
BT_GPU_PREF(computeGridSize)(numBodies, 64, numBlocks, numThreads);
|
||||
BT_GPU_EXECKERNEL(numBlocks, numThreads, findOverlappingPairsD, (pAABB,(uint2*)pHash,(uint*)pCellStart,(uint*)pPairBuff,(uint2*)pPairBuffStartCurr,numBodies));
|
||||
BT_GPU_CHECK_ERROR("Kernel execution failed: bt_CudaFindOverlappingPairsD");
|
||||
#if B_CUDA_USE_TEX
|
||||
BT_GPU_SAFE_CALL(cudaUnbindTexture(pAABBTex));
|
||||
#endif
|
||||
} // findOverlappingPairs()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
void BT_GPU_PREF(findPairsLarge(bt3DGrid3F1U* pAABB, unsigned int* pHash, unsigned int* pCellStart, unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int numBodies, unsigned int numLarge))
|
||||
{
|
||||
#if B_CUDA_USE_TEX
|
||||
BT_GPU_SAFE_CALL(cudaBindTexture(0, pAABBTex, pAABB, (numBodies+numLarge) * 2 * sizeof(bt3DGrid3F1U)));
|
||||
#endif
|
||||
int numThreads, numBlocks;
|
||||
BT_GPU_PREF(computeGridSize)(numBodies, 64, numBlocks, numThreads);
|
||||
BT_GPU_EXECKERNEL(numBlocks, numThreads, findPairsLargeD, (pAABB,(uint2*)pHash,(uint*)pCellStart,(uint*)pPairBuff,(uint2*)pPairBuffStartCurr,numBodies,numLarge));
|
||||
BT_GPU_CHECK_ERROR("Kernel execution failed: btCuda_findPairsLargeD");
|
||||
#if B_CUDA_USE_TEX
|
||||
BT_GPU_SAFE_CALL(cudaUnbindTexture(pAABBTex));
|
||||
#endif
|
||||
} // findPairsLarge()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
void BT_GPU_PREF(computePairCacheChanges(unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int* pPairScan, bt3DGrid3F1U* pAABB, unsigned int numBodies))
|
||||
{
|
||||
int numThreads, numBlocks;
|
||||
BT_GPU_PREF(computeGridSize)(numBodies, 256, numBlocks, numThreads);
|
||||
BT_GPU_EXECKERNEL(numBlocks, numThreads, computePairCacheChangesD, ((uint*)pPairBuff,(uint2*)pPairBuffStartCurr,(uint*)pPairScan,pAABB,numBodies));
|
||||
BT_GPU_CHECK_ERROR("Kernel execution failed: btCudaComputePairCacheChangesD");
|
||||
} // computePairCacheChanges()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
void BT_GPU_PREF(squeezeOverlappingPairBuff(unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int* pPairScan, unsigned int* pPairOut, bt3DGrid3F1U* pAABB, unsigned int numBodies))
|
||||
{
|
||||
int numThreads, numBlocks;
|
||||
BT_GPU_PREF(computeGridSize)(numBodies, 256, numBlocks, numThreads);
|
||||
BT_GPU_EXECKERNEL(numBlocks, numThreads, squeezeOverlappingPairBuffD, ((uint*)pPairBuff,(uint2*)pPairBuffStartCurr,(uint*)pPairScan,(uint*)pPairOut,pAABB,numBodies));
|
||||
BT_GPU_CHECK_ERROR("Kernel execution failed: btCudaSqueezeOverlappingPairBuffD");
|
||||
} // btCuda_squeezeOverlappingPairBuff()
|
||||
|
||||
//------------------------------------------------------------------------------------------------
|
||||
|
||||
} // extern "C"
|
||||
|
||||
//------------------------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------------------------
|
||||
60
src/BulletMultiThreaded/btGpu3DGridBroadphaseSharedDefs.h
Normal file
60
src/BulletMultiThreaded/btGpu3DGridBroadphaseSharedDefs.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
|
||||
Copyright (C) 2006, 2009 Sony Computer Entertainment 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.
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
// Shared definitions for GPU-based 3D Grid collision detection broadphase
|
||||
|
||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
// Keep this file free from Bullet headers
|
||||
// it is included into both CUDA and CPU code
|
||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#ifndef BTGPU3DGRIDBROADPHASESHAREDDEFS_H
|
||||
#define BTGPU3DGRIDBROADPHASESHAREDDEFS_H
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#include "btGpu3DGridBroadphaseSharedTypes.h"
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
void BT_GPU_PREF(calcHashAABB)(bt3DGrid3F1U* pAABB, unsigned int* hash, unsigned int numBodies);
|
||||
|
||||
void BT_GPU_PREF(findCellStart)(unsigned int* hash, unsigned int* cellStart, unsigned int numBodies, unsigned int numCells);
|
||||
|
||||
void BT_GPU_PREF(findOverlappingPairs)(bt3DGrid3F1U* pAABB, unsigned int* pHash, unsigned int* pCellStart, unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int numBodies);
|
||||
|
||||
void BT_GPU_PREF(findPairsLarge)(bt3DGrid3F1U* pAABB, unsigned int* pHash, unsigned int* pCellStart, unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int numBodies, unsigned int numLarge);
|
||||
|
||||
void BT_GPU_PREF(computePairCacheChanges)(unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int* pPairScan, bt3DGrid3F1U* pAABB, unsigned int numBodies);
|
||||
|
||||
void BT_GPU_PREF(squeezeOverlappingPairBuff)(unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int* pPairScan, unsigned int* pPairOut, bt3DGrid3F1U* pAABB, unsigned int numBodies);
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
} // extern "C"
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#endif // BTGPU3DGRIDBROADPHASESHAREDDEFS_H
|
||||
66
src/BulletMultiThreaded/btGpu3DGridBroadphaseSharedTypes.h
Normal file
66
src/BulletMultiThreaded/btGpu3DGridBroadphaseSharedTypes.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
|
||||
Copyright (C) 2006, 2009 Sony Computer Entertainment 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.
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
// Shared definitions for GPU-based 3D Grid collision detection broadphase
|
||||
|
||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
// Keep this file free from Bullet headers
|
||||
// it is included into both CUDA and CPU code
|
||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#ifndef BTGPU3DGRIDBROADPHASESHAREDTYPES_H
|
||||
#define BTGPU3DGRIDBROADPHASESHAREDTYPES_H
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#define BT_3DGRID_PAIR_FOUND_FLG (0x40000000)
|
||||
#define BT_3DGRID_PAIR_NEW_FLG (0x20000000)
|
||||
#define BT_3DGRID_PAIR_ANY_FLG (BT_3DGRID_PAIR_FOUND_FLG | BT_3DGRID_PAIR_NEW_FLG)
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
struct bt3DGridBroadphaseParams
|
||||
{
|
||||
unsigned int m_gridSizeX;
|
||||
unsigned int m_gridSizeY;
|
||||
unsigned int m_gridSizeZ;
|
||||
unsigned int m_numCells;
|
||||
float m_worldOriginX;
|
||||
float m_worldOriginY;
|
||||
float m_worldOriginZ;
|
||||
float m_cellSizeX;
|
||||
float m_cellSizeY;
|
||||
float m_cellSizeZ;
|
||||
unsigned int m_numBodies;
|
||||
unsigned int m_maxBodiesPerCell;
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
struct bt3DGrid3F1U
|
||||
{
|
||||
float fx;
|
||||
float fy;
|
||||
float fz;
|
||||
unsigned int uw;
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#endif // BTGPU3DGRIDBROADPHASESHAREDTYPES_H
|
||||
221
src/BulletMultiThreaded/btGpuDefines.h
Normal file
221
src/BulletMultiThreaded/btGpuDefines.h
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
|
||||
Copyright (C) 2006, 2009 Sony Computer Entertainment 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.
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
// definitions for "GPU on CPU" code
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
#ifndef BT_GPU_DEFINES_H
|
||||
#define BT_GPU_DEFINES_H
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
struct int2
|
||||
{
|
||||
int x, y;
|
||||
};
|
||||
|
||||
struct uint2
|
||||
{
|
||||
unsigned int x, y;
|
||||
};
|
||||
|
||||
struct int3
|
||||
{
|
||||
int x, y, z;
|
||||
};
|
||||
|
||||
struct uint3
|
||||
{
|
||||
unsigned int x, y, z;
|
||||
};
|
||||
|
||||
struct float4
|
||||
{
|
||||
float x, y, z, w;
|
||||
};
|
||||
|
||||
struct float3
|
||||
{
|
||||
float x, y, z;
|
||||
};
|
||||
|
||||
|
||||
#define BT_GPU___device__ inline
|
||||
#define BT_GPU___devdata__
|
||||
#define BT_GPU___constant__
|
||||
#define BT_GPU_max(a, b) ((a) > (b) ? (a) : (b))
|
||||
#define BT_GPU_min(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define BT_GPU_params s3DGridBroadphaseParams
|
||||
#define BT_GPU___mul24(a, b) ((a)*(b))
|
||||
#define BT_GPU___global__ inline
|
||||
#define BT_GPU___shared__ static
|
||||
#define BT_GPU___syncthreads()
|
||||
#define CUDART_PI_F SIMD_PI
|
||||
|
||||
static inline uint2 bt3dGrid_make_uint2(unsigned int x, unsigned int y)
|
||||
{
|
||||
uint2 t; t.x = x; t.y = y; return t;
|
||||
}
|
||||
#define BT_GPU_make_uint2(x, y) bt3dGrid_make_uint2(x, y)
|
||||
|
||||
static inline int3 bt3dGrid_make_int3(int x, int y, int z)
|
||||
{
|
||||
int3 t; t.x = x; t.y = y; t.z = z; return t;
|
||||
}
|
||||
#define BT_GPU_make_int3(x, y, z) bt3dGrid_make_int3(x, y, z)
|
||||
|
||||
static inline float3 bt3dGrid_make_float3(float x, float y, float z)
|
||||
{
|
||||
float3 t; t.x = x; t.y = y; t.z = z; return t;
|
||||
}
|
||||
#define BT_GPU_make_float3(x, y, z) bt3dGrid_make_float3(x, y, z)
|
||||
|
||||
static inline float3 bt3dGrid_make_float34(float4 f)
|
||||
{
|
||||
float3 t; t.x = f.x; t.y = f.y; t.z = f.z; return t;
|
||||
}
|
||||
#define BT_GPU_make_float34(f) bt3dGrid_make_float34(f)
|
||||
|
||||
static inline float3 bt3dGrid_make_float31(float f)
|
||||
{
|
||||
float3 t; t.x = t.y = t.z = f; return t;
|
||||
}
|
||||
#define BT_GPU_make_float31(x) bt3dGrid_make_float31(x)
|
||||
|
||||
static inline float4 bt3dGrid_make_float42(float3 v, float f)
|
||||
{
|
||||
float4 t; t.x = v.x; t.y = v.y; t.z = v.z; t.w = f; return t;
|
||||
}
|
||||
#define BT_GPU_make_float42(a, b) bt3dGrid_make_float42(a, b)
|
||||
|
||||
static inline float4 bt3dGrid_make_float44(float a, float b, float c, float d)
|
||||
{
|
||||
float4 t; t.x = a; t.y = b; t.z = c; t.w = d; return t;
|
||||
}
|
||||
#define BT_GPU_make_float44(a, b, c, d) bt3dGrid_make_float44(a, b, c, d)
|
||||
|
||||
inline int3 operator+(int3 a, int3 b)
|
||||
{
|
||||
return bt3dGrid_make_int3(a.x + b.x, a.y + b.y, a.z + b.z);
|
||||
}
|
||||
|
||||
inline float4 operator+(float4& a, float4& b)
|
||||
{
|
||||
float4 r; r.x = a.x+b.x; r.y = a.y+b.y; r.z = a.z+b.z; r.w = a.w+b.w; return r;
|
||||
}
|
||||
inline float4 operator*(float4& a, float fact)
|
||||
{
|
||||
float4 r; r.x = a.x*fact; r.y = a.y*fact; r.z = a.z*fact; r.w = a.w*fact; return r;
|
||||
}
|
||||
inline float4 operator*(float fact, float4& a)
|
||||
{
|
||||
return (a * fact);
|
||||
}
|
||||
inline float4& operator*=(float4& a, float fact)
|
||||
{
|
||||
a = fact * a;
|
||||
return a;
|
||||
}
|
||||
inline float4& operator+=(float4& a, float4& b)
|
||||
{
|
||||
a = a + b;
|
||||
return a;
|
||||
}
|
||||
|
||||
inline float3 operator+(float3& a, float3& b)
|
||||
{
|
||||
float3 r; r.x = a.x+b.x; r.y = a.y+b.y; r.z = a.z+b.z; return r;
|
||||
}
|
||||
inline float3 operator+(const float3& a, const float3& b)
|
||||
{
|
||||
float3 r; r.x = a.x+b.x; r.y = a.y+b.y; r.z = a.z+b.z; return r;
|
||||
}
|
||||
inline float3 operator-(float3& a, float3& b)
|
||||
{
|
||||
float3 r; r.x = a.x-b.x; r.y = a.y-b.y; r.z = a.z-b.z; return r;
|
||||
}
|
||||
static inline float bt3dGrid_dot(float3& a, float3& b)
|
||||
{
|
||||
return a.x*b.x+a.y*b.y+a.z*b.z;
|
||||
}
|
||||
#define BT_GPU_dot(a,b) bt3dGrid_dot(a,b)
|
||||
|
||||
static inline float bt3dGrid_dot4(float4& a, float4& b)
|
||||
{
|
||||
return a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w;
|
||||
}
|
||||
#define BT_GPU_dot4(a,b) bt3dGrid_dot4(a,b)
|
||||
|
||||
static inline float3 bt3dGrid_cross(float3& a, float3& b)
|
||||
{
|
||||
float3 r; r.x = a.y*b.z-a.z*b.y; r.y = -a.x*b.z+a.z*b.x; r.z = a.x*b.y-a.y*b.x; return r;
|
||||
}
|
||||
#define BT_GPU_cross(a,b) bt3dGrid_cross(a,b)
|
||||
|
||||
inline float3 operator*(float3& a, float fact)
|
||||
{
|
||||
float3 r; r.x = a.x*fact; r.y = a.y*fact; r.z = a.z*fact; return r;
|
||||
}
|
||||
inline float3 operator*(const float3& a, float fact)
|
||||
{
|
||||
float3 r; r.x = a.x*fact; r.y = a.y*fact; r.z = a.z*fact; return r;
|
||||
}
|
||||
|
||||
|
||||
inline float3& operator+=(float3& a, float3& b)
|
||||
{
|
||||
a = a + b;
|
||||
return a;
|
||||
}
|
||||
inline float3& operator-=(float3& a, float3& b)
|
||||
{
|
||||
a = a - b;
|
||||
return a;
|
||||
}
|
||||
inline float3& operator*=(float3& a, float fact)
|
||||
{
|
||||
a = a * fact;
|
||||
return a;
|
||||
}
|
||||
inline float3 operator-(const float3& v)
|
||||
{
|
||||
float3 r; r.x = -v.x; r.y = -v.y; r.z = -v.z; return r;
|
||||
}
|
||||
|
||||
|
||||
#define BT_GPU_FETCH(a, b) a[b]
|
||||
#define BT_GPU_FETCH4(a, b) a[b]
|
||||
#define BT_GPU_PREF(func) btGpu_##func
|
||||
#define BT_GPU_SAFE_CALL(func) func
|
||||
#define BT_GPU_Memset memset
|
||||
#define BT_GPU_MemcpyToSymbol(a, b, c) memcpy(a, b, c)
|
||||
#define BT_GPU_BindTexture(a, b, c, d)
|
||||
#define BT_GPU_UnbindTexture(a)
|
||||
|
||||
static uint2 s_blockIdx, s_blockDim, s_threadIdx;
|
||||
#define BT_GPU_blockIdx s_blockIdx
|
||||
#define BT_GPU_blockDim s_blockDim
|
||||
#define BT_GPU_threadIdx s_threadIdx
|
||||
#define BT_GPU_EXECKERNEL(numb, numt, kfunc, args) {s_blockDim.x=numt;for(int nb=0;nb<numb;nb++){s_blockIdx.x=nb;for(int nt=0;nt<numt;nt++){s_threadIdx.x=nt;kfunc args;}}}
|
||||
|
||||
#define BT_GPU_CHECK_ERROR(s)
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
//--------------------------------------------------------------------------
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
#endif //BT_GPU_DEFINES_H
|
||||
54
src/BulletMultiThreaded/btGpuUtilsSharedCode.h
Normal file
54
src/BulletMultiThreaded/btGpuUtilsSharedCode.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
|
||||
Copyright (C) 2006, 2009 Sony Computer Entertainment 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.
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
// Shared code for GPU-based utilities
|
||||
|
||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
// Keep this file free from Bullet headers
|
||||
// will be compiled by both CPU and CUDA compilers
|
||||
// file with definitions of BT_GPU_xxx should be included first
|
||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#include "btGpuUtilsSharedDefs.h"
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
//Round a / b to nearest higher integer value
|
||||
int BT_GPU_PREF(iDivUp)(int a, int b)
|
||||
{
|
||||
return (a % b != 0) ? (a / b + 1) : (a / b);
|
||||
} // iDivUp()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
// compute grid and thread block size for a given number of elements
|
||||
void BT_GPU_PREF(computeGridSize)(int n, int blockSize, int &numBlocks, int &numThreads)
|
||||
{
|
||||
numThreads = BT_GPU_min(blockSize, n);
|
||||
numBlocks = BT_GPU_PREF(iDivUp)(n, numThreads);
|
||||
} // computeGridSize()
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
} // extern "C"
|
||||
62
src/BulletMultiThreaded/btGpuUtilsSharedDefs.h
Normal file
62
src/BulletMultiThreaded/btGpuUtilsSharedDefs.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
|
||||
Copyright (C) 2006, 2009 Sony Computer Entertainment 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.
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
// Shared definitions for GPU-based utilities
|
||||
|
||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
// Keep this file free from Bullet headers
|
||||
// it is included into both CUDA and CPU code
|
||||
// file with definitions of BT_GPU_xxx should be included first
|
||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#ifndef BTGPUUTILSDHAREDDEFS_H
|
||||
#define BTGPUUTILSDHAREDDEFS_H
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
//Round a / b to nearest higher integer value
|
||||
int BT_GPU_PREF(iDivUp)(int a, int b);
|
||||
|
||||
// compute grid and thread block size for a given number of elements
|
||||
void BT_GPU_PREF(computeGridSize)(int n, int blockSize, int &numBlocks, int &numThreads);
|
||||
|
||||
void BT_GPU_PREF(allocateArray)(void** devPtr, unsigned int size);
|
||||
void BT_GPU_PREF(freeArray)(void* devPtr);
|
||||
void BT_GPU_PREF(copyArrayFromDevice)(void* host, const void* device, unsigned int size);
|
||||
void BT_GPU_PREF(copyArrayToDevice)(void* device, const void* host, unsigned int size);
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
} // extern "C"
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#endif // BTGPUUTILSDHAREDDEFS_H
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------------
|
||||
Reference in New Issue
Block a user