/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 <cstdio>
#define WAVEFRONT_SIZE 32
#define WAVEFRONT_BLOCK_MULTIPLIER 2
#define GROUP_SIZE (WAVEFRONT_SIZE*WAVEFRONT_BLOCK_MULTIPLIER)
#define LINKS_PER_SIMD_LANE 16
#define STRINGIFY( S ) STRINGIFY2( S )
#define STRINGIFY2( S ) #S
#include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
#include "vectormath/vmInclude.h"
#include "btSoftBodySolverLinkData_DX11SIMDAware.h"
#include "btSoftBodySolver_DX11SIMDAware.h"
#include "btSoftBodySolverVertexBuffer_DX11.h"
#include "BulletSoftBody/btSoftBody.h"
#include "BulletCollision/CollisionShapes/btCapsuleShape.h"
#define MSTRINGIFY(A) #A
static char* UpdatePositionsFromVelocitiesHLSLString =
#include "HLSL/UpdatePositionsFromVelocities.hlsl"
static char* SolvePositionsSIMDBatchedHLSLString =
#include "HLSL/SolvePositionsSIMDBatched.hlsl"
static char* UpdateNodesHLSLString =
#include "HLSL/UpdateNodes.hlsl"
static char* UpdatePositionsHLSLString =
#include "HLSL/UpdatePositions.hlsl"
static char* UpdateConstantsHLSLString =
#include "HLSL/UpdateConstants.hlsl"
static char* IntegrateHLSLString =
#include "HLSL/Integrate.hlsl"
static char* ApplyForcesHLSLString =
#include "HLSL/ApplyForces.hlsl"
static char* UpdateNormalsHLSLString =
#include "HLSL/UpdateNormals.hlsl"
static char* OutputToVertexArrayHLSLString =
#include "HLSL/OutputToVertexArray.hlsl"
static char* VSolveLinksHLSLString =
#include "HLSL/VSolveLinks.hlsl"
static char* ComputeBoundsHLSLString =
#include "HLSL/ComputeBounds.hlsl"
static char* SolveCollisionsAndUpdateVelocitiesHLSLString =
#include "HLSL/solveCollisionsAndUpdateVelocitiesSIMDBatched.hlsl"
btSoftBodyLinkDataDX11SIMDAware::btSoftBodyLinkDataDX11SIMDAware( ID3D11Device *d3dDevice, ID3D11DeviceContext *d3dDeviceContext ) :
m_d3dDevice( d3dDevice ),
m_d3dDeviceContext( d3dDeviceContext ),
m_wavefrontSize( WAVEFRONT_SIZE ),
m_linksPerWorkItem( LINKS_PER_SIMD_LANE ),
m_maxBatchesWithinWave( 0 ),
m_maxLinksPerWavefront( m_wavefrontSize * m_linksPerWorkItem ),
m_numWavefronts( 0 ),
m_maxVertex( 0 ),
m_dx11NumBatchesAndVerticesWithinWaves( d3dDevice, d3dDeviceContext, &m_numBatchesAndVerticesWithinWaves, true ),
m_dx11WavefrontVerticesGlobalAddresses( d3dDevice, d3dDeviceContext, &m_wavefrontVerticesGlobalAddresses, true ),
m_dx11LinkVerticesLocalAddresses( d3dDevice, d3dDeviceContext, &m_linkVerticesLocalAddresses, true ),
m_dx11LinkStrength( d3dDevice, d3dDeviceContext, &m_linkStrength, true ),
m_dx11LinksMassLSC( d3dDevice, d3dDeviceContext, &m_linksMassLSC, true ),
m_dx11LinksRestLengthSquared( d3dDevice, d3dDeviceContext, &m_linksRestLengthSquared, true ),
m_dx11LinksRestLength( d3dDevice, d3dDeviceContext, &m_linksRestLength, true ),
m_dx11LinksMaterialLinearStiffnessCoefficient( d3dDevice, d3dDeviceContext, &m_linksMaterialLinearStiffnessCoefficient, true )
{
m_d3dDevice = d3dDevice;
m_d3dDeviceContext = d3dDeviceContext;
}
btSoftBodyLinkDataDX11SIMDAware::~btSoftBodyLinkDataDX11SIMDAware()
{
}
static Vectormath::Aos::Vector3 toVector3( const btVector3 &vec )
{
Vectormath::Aos::Vector3 outVec( vec.getX(), vec.getY(), vec.getZ() );
return outVec;
}
void btSoftBodyLinkDataDX11SIMDAware::createLinks( int numLinks )
{
int previousSize = m_links.size();
int newSize = previousSize + numLinks;
btSoftBodyLinkData::createLinks( numLinks );
// Resize the link addresses array as well
m_linkAddresses.resize( newSize );
}
void btSoftBodyLinkDataDX11SIMDAware::setLinkAt( const btSoftBodyLinkData::LinkDescription &link, int linkIndex )
{
btSoftBodyLinkData::setLinkAt( link, linkIndex );
if( link.getVertex0() > m_maxVertex )
m_maxVertex = link.getVertex0();
if( link.getVertex1() > m_maxVertex )
m_maxVertex = link.getVertex1();
// Set the link index correctly for initialisation
m_linkAddresses[linkIndex] = linkIndex;
}
bool btSoftBodyLinkDataDX11SIMDAware::onAccelerator()
{
return m_onGPU;
}
bool btSoftBodyLinkDataDX11SIMDAware::moveToAccelerator()
{
bool success = true;
success = success &&