From fe0e2cd266cf1db3e736c8576d65e7cb60867d5d Mon Sep 17 00:00:00 2001 From: id0x1234 Date: Thu, 11 Sep 2008 15:20:55 +0000 Subject: [PATCH] Improved DbvtBroadphase performance for sleeping world (when no bodies are moving). Improved DbvtBroadphase velocity prediction. --- .../BroadphaseCollision/btDbvtBroadphase.cpp | 66 ++++++++++++------- .../BroadphaseCollision/btDbvtBroadphase.h | 13 ++-- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.cpp b/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.cpp index 6af3b8fc6..9959cfd50 100644 --- a/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.cpp +++ b/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.cpp @@ -116,11 +116,13 @@ btDbvtBroadphase::btDbvtBroadphase(btOverlappingPairCache* paircache) { m_initialize = true; m_deferedcollide = true; +m_needcleanup = true; m_releasepaircache = (paircache!=0)?false:true; -m_predictedframes = 15; +m_prediction = 1/(btScalar)2; m_stageCurrent = 0; +m_fixedleft = 0; m_fupdates = 1; -m_dupdates = 1; +m_dupdates = 0; m_cupdates = 10; m_newpairs = 1; m_updates_call = 0; @@ -170,6 +172,7 @@ proxy->stage = m_stageCurrent; proxy->m_uniqueId = ++m_gid; listappend(proxy,m_stageRoots[m_stageCurrent]); setAabb(proxy,aabbMin,aabbMax,dispatcher); +m_needcleanup=true; return(proxy); } @@ -185,6 +188,7 @@ if(proxy->stage==STAGECOUNT) listremove(proxy,m_stageRoots[proxy->stage]); m_paircache->removeOverlappingPairsContainingProxy(proxy,dispatcher); btAlignedFree(proxy); +m_needcleanup=true; } // @@ -195,26 +199,32 @@ void btDbvtBroadphase::setAabb( btBroadphaseProxy* absproxy, { btDbvtProxy* proxy=(btDbvtProxy*)absproxy; btDbvtVolume aabb=btDbvtVolume::FromMM(aabbMin,aabbMax); -//if(NotEqual(aabb,proxy->leaf->volume)) +#if DBVT_BP_PREVENTFALSEUPDATE +if(NotEqual(aabb,proxy->leaf->volume)) +#endif { bool docollide=false; if(proxy->stage==STAGECOUNT) {/* fixed -> dynamic set */ m_sets[1].remove(proxy->leaf); proxy->leaf=m_sets[0].insert(aabb,proxy); - docollide=true; + docollide=true; } else {/* dynamic set */ ++m_updates_call; if(Intersect(proxy->leaf->volume,aabb)) {/* Moving */ - const btVector3 delta=(aabbMin+aabbMax)/2-proxy->aabb.Center(); + const btVector3 delta=aabbMin-proxy->aabb.Mins(); + btVector3 velocity(aabb.Extents()*m_prediction); + if(delta[0]<0) velocity[0]=-velocity[0]; + if(delta[1]<0) velocity[1]=-velocity[1]; + if(delta[2]<0) velocity[2]=-velocity[2]; if ( - #ifdef DBVT_BP_MARGIN - m_sets[0].update(proxy->leaf,aabb,delta*m_predictedframes,DBVT_BP_MARGIN) + #ifdef DBVT_BP_MARGIN + m_sets[0].update(proxy->leaf,aabb,velocity,DBVT_BP_MARGIN) #else - m_sets[0].update(proxy->leaf,aabb,delta*m_predictedframes) + m_sets[0].update(proxy->leaf,aabb,velocity) #endif ) { @@ -233,12 +243,16 @@ btDbvtVolume aabb=btDbvtVolume::FromMM(aabbMin,aabbMax); proxy->aabb = aabb; proxy->stage = m_stageCurrent; listappend(proxy,m_stageRoots[m_stageCurrent]); - if(docollide&&(!m_deferedcollide)) + if(docollide) { - btDbvtTreeCollider collider(this); - btDbvt::collideTT(m_sets[1].m_root,proxy->leaf,collider); - btDbvt::collideTT(m_sets[0].m_root,proxy->leaf,collider); - } + m_needcleanup=true; + if(!m_deferedcollide) + { + btDbvtTreeCollider collider(this); + btDbvt::collideTT(m_sets[1].m_root,proxy->leaf,collider); + btDbvt::collideTT(m_sets[0].m_root,proxy->leaf,collider); + } + } } } @@ -273,7 +287,12 @@ void btDbvtBroadphase::collide(btDispatcher* dispatcher) SPC(m_profiling.m_total); /* optimize */ m_sets[0].optimizeIncremental(1+(m_sets[0].m_leaves*m_dupdates)/100); -m_sets[1].optimizeIncremental(1+(m_sets[1].m_leaves*m_fupdates)/100); +if(m_fixedleft) + { + const int count=1+(m_sets[1].m_leaves*m_fupdates)/100; + m_sets[1].optimizeIncremental(1+(m_sets[1].m_leaves*m_fupdates)/100); + m_fixedleft=btMax(0,m_fixedleft-count); + } /* dynamic -> fixed set */ m_stageCurrent=(m_stageCurrent+1)%STAGECOUNT; btDbvtProxy* current=m_stageRoots[m_stageCurrent]; @@ -290,6 +309,8 @@ if(current) current->stage = STAGECOUNT; current = next; } while(current); + m_fixedleft=m_sets[1].m_leaves; + m_needcleanup=true; } /* collide dynamics */ { @@ -306,6 +327,7 @@ if(current) } } /* clean up */ +if(m_needcleanup) { SPC(m_profiling.m_cleanup); btBroadphasePairArray& pairs=m_paircache->getOverlappingPairArray(); @@ -318,16 +340,13 @@ if(current) btBroadphasePair& p=pairs[(m_cid+i)%ci]; btDbvtProxy* pa=(btDbvtProxy*)p.m_pProxy0; btDbvtProxy* pb=(btDbvtProxy*)p.m_pProxy1; - //if((pa->stage==0)||(pb->stage==0)) + if(!Intersect(pa->leaf->volume,pb->leaf->volume)) { - if(!Intersect(pa->leaf->volume,pb->leaf->volume)) - { - #if DBVT_BP_SORTPAIRS - if(pa>pb) btSwap(pa,pb); - #endif - m_paircache->removeOverlappingPair(pa,pb,dispatcher); - --ni;--i; - } + #if DBVT_BP_SORTPAIRS + if(pa>pb) btSwap(pa,pb); + #endif + m_paircache->removeOverlappingPair(pa,pb,dispatcher); + --ni;--i; } } if(pairs.size()>0) m_cid=(m_cid+ni)%pairs.size(); else m_cid=0; @@ -335,6 +354,7 @@ if(current) } ++m_pid; m_newpairs=1; +m_needcleanup=false; if(m_updates_call>0) { m_updates_ratio=m_updates_done/(btScalar)m_updates_call; } else diff --git a/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.h b/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.h index 54ad51a71..8314a82ad 100644 --- a/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.h +++ b/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.h @@ -23,10 +23,11 @@ subject to the following restrictions: // Compile time config // -#define DBVT_BP_PROFILE 0 -#define DBVT_BP_SORTPAIRS 1 -#define DBVT_BP_ENABLE_BENCHMARK 0 -#define DBVT_BP_MARGIN (btScalar)0.05 +#define DBVT_BP_PROFILE 0 +#define DBVT_BP_SORTPAIRS 1 +#define DBVT_BP_PREVENTFALSEUPDATE 0 +#define DBVT_BP_ENABLE_BENCHMARK 0 +#define DBVT_BP_MARGIN (btScalar)0.05 #if DBVT_BP_PROFILE #define DBVT_BP_PROFILING_RATE 256 @@ -68,12 +69,13 @@ enum { btDbvt m_sets[2]; // Dbvt sets btDbvtProxy* m_stageRoots[STAGECOUNT+1]; // Stages list btOverlappingPairCache* m_paircache; // Pair cache -btScalar m_predictedframes; // Frames predicted +btScalar m_prediction; // Velocity prediction int m_stageCurrent; // Current stage int m_fupdates; // % of fixed updates per frame int m_dupdates; // % of dynamic updates per frame int m_cupdates; // % of cleanup updates per frame int m_newpairs; // Number of pairs created +int m_fixedleft; // Fixed optimization left unsigned m_updates_call; // Number of updates call unsigned m_updates_done; // Number of updates done btScalar m_updates_ratio; // m_updates_done/m_updates_call @@ -82,6 +84,7 @@ int m_cid; // Cleanup index int m_gid; // Gen id bool m_releasepaircache; // Release pair cache on delete bool m_deferedcollide; // Defere dynamic/static collision to collide call +bool m_needcleanup; // Need to run cleanup? bool m_initialize; // Initialization #if DBVT_BP_PROFILE btClock m_clock;