Skip to content

Commit

Permalink
Merge pull request #1185 from moai/hotfix/partition-results-buffer
Browse files Browse the repository at this point in the history
Hotfix/partition results buffer
  • Loading branch information
halfnelson authored Feb 22, 2017
2 parents 9530271 + a8eb11b commit 31fc0be
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/moai-sim/MOAICollisionWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ void MOAICollisionWorld::ProcessOverlaps () {

// this gives us the coarse filter based on world space bounds
// TODO: find a way to utilize overlap flags?
MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

u32 totalResults = this->mPartition->GatherProps ( buffer, &prop, prop.GetBounds (), interfaceMask );

for ( u32 i = 0; i < totalResults; ++i ) {
Expand Down
9 changes: 6 additions & 3 deletions src/moai-sim/MOAILayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ int MOAILayer::_getPropViewList ( lua_State* L ) {
ZLFrustum viewVolume;
viewVolume.Init ( invViewProjMtx );

MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

u32 totalResults = 0;

Expand Down Expand Up @@ -789,7 +790,9 @@ void MOAILayer::Draw ( int subPrimID, float lod ) {
u32 interfaceMask = this->mPartition->GetInterfaceMask < MOAIGraphicsProp >();
if ( !interfaceMask ) return;

MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

const ZLFrustum& viewVolume = gfxMgr.mGfxState.GetViewVolume ();

u32 totalResults = 0;
Expand Down Expand Up @@ -1125,4 +1128,4 @@ void MOAILayer::SerializeIn ( MOAILuaState& state, MOAIDeserializer& serializer
//----------------------------------------------------------------//
void MOAILayer::SerializeOut ( MOAILuaState& state, MOAISerializer& serializer ) {
MOAIGraphicsProp::SerializeOut ( state, serializer );
}
}
18 changes: 12 additions & 6 deletions src/moai-sim/MOAIPartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ int MOAIPartition::_propForPoint ( lua_State* L ) {
u32 interfaceMask = state.GetValue < u32 >( 10, MASK_ANY );
u32 queryMask = state.GetValue < u32 >( 11, MASK_ANY );

MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

u32 total = self->GatherProps ( buffer, 0, vec, interfaceMask, queryMask );
if ( total ) {
Expand Down Expand Up @@ -152,7 +153,8 @@ int MOAIPartition::_propForRay ( lua_State* L ) {
u32 interfaceMask = state.GetValue < u32 >( 8, MASK_ANY );
u32 queryMask = state.GetValue < u32 >( 9, MASK_ANY );

MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

u32 total = self->GatherProps ( buffer, 0, vec, direction, interfaceMask, queryMask );

Expand Down Expand Up @@ -193,7 +195,8 @@ int MOAIPartition::_propList ( lua_State* L ) {
u32 interfaceMask = state.GetValue < u32 >( 7, MASK_ANY );
u32 queryMask = state.GetValue < u32 >( 8, MASK_ANY );

MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

u32 total = self->GatherProps ( buffer, 0, interfaceMask, queryMask );
if ( total ) {
Expand Down Expand Up @@ -239,7 +242,8 @@ int MOAIPartition::_propListForPoint ( lua_State* L ) {
u32 interfaceMask = state.GetValue < u32 >( 10, MASK_ANY );
u32 queryMask = state.GetValue < u32 >( 11, MASK_ANY );

MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

u32 total = self->GatherProps ( buffer, 0, vec, interfaceMask, queryMask );
if ( total ) {
Expand Down Expand Up @@ -294,7 +298,8 @@ int MOAIPartition::_propListForRay ( lua_State* L ) {
u32 interfaceMask = state.GetValue < u32 >( 13, MASK_ANY );
u32 queryMask = state.GetValue < u32 >( 14, MASK_ANY );

MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

u32 total = self->GatherProps ( buffer, 0, vec, direction, interfaceMask, queryMask );

Expand Down Expand Up @@ -345,7 +350,8 @@ int MOAIPartition::_propListForRect ( lua_State* L ) {
u32 interfaceMask = state.GetValue < u32 >( 11, MASK_ANY );
u32 queryMask = state.GetValue < u32 >( 12, MASK_ANY );

MOAIPartitionResultBuffer& buffer = MOAIPartitionResultMgr::Get ().GetBuffer ();
MOAIScopedPartitionResultBufferHandle scopedBufferHandle = MOAIPartitionResultMgr::Get ().GetBufferHandle ();
MOAIPartitionResultBuffer& buffer = scopedBufferHandle;

u32 total = self->GatherProps ( buffer, 0, rect, interfaceMask, queryMask );
if ( total ) {
Expand Down
56 changes: 56 additions & 0 deletions src/moai-sim/MOAIPartitionResultMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,70 @@
#include "pch.h"
#include <moai-sim/MOAIPartitionResultMgr.h>

//================================================================//
// MOAIScopedPartitionResultBufferHandle
//================================================================//

//----------------------------------------------------------------//
MOAIPartitionResultBuffer& MOAIScopedPartitionResultBufferHandle::Buffer () {

return *this->mBuffer;
}

//----------------------------------------------------------------//
MOAIScopedPartitionResultBufferHandle::MOAIScopedPartitionResultBufferHandle ( MOAIPartitionResultMgr& mgr ) :
mMgr ( mgr ) {
}

//----------------------------------------------------------------//
MOAIScopedPartitionResultBufferHandle::~MOAIScopedPartitionResultBufferHandle () {

if ( this->mBuffer ) {
this->mMgr.ReleaseBuffer ( this->mBuffer );
}
}

//================================================================//
// MOAIPartitionResultMgr
//================================================================//

//----------------------------------------------------------------//
MOAIScopedPartitionResultBufferHandle MOAIPartitionResultMgr::GetBufferHandle () {

MOAIScopedPartitionResultBufferHandle handle ( *this );

if ( this->mAvailable.GetTop () == 0 ) {

handle.mBuffer = new MOAIPartitionResultBuffer ();

size_t index = this->mBuffers.Size ();
this->mBuffers.Grow ( index + 1, 1, 0 );
this->mBuffers [ index ] = handle.mBuffer;

return handle;
}

handle.mBuffer = this->mAvailable.Pop ();
assert ( handle.mBuffer );
return handle;
}

//----------------------------------------------------------------//
MOAIPartitionResultMgr::MOAIPartitionResultMgr () {
}

//----------------------------------------------------------------//
MOAIPartitionResultMgr::~MOAIPartitionResultMgr () {

for ( size_t i = 0; i < this->mBuffers.Size (); ++i ) {
delete this->mBuffers [ i ];
}
}

//----------------------------------------------------------------//
void MOAIPartitionResultMgr::ReleaseBuffer ( MOAIPartitionResultBuffer* buffer ) {

if ( buffer ) {
this->mAvailable.Push ( buffer );
}
}
54 changes: 51 additions & 3 deletions src/moai-sim/MOAIPartitionResultMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,70 @@

#include <moai-sim/MOAIPartitionResultBuffer.h>

class MOAIPartitionResultMgr;

//================================================================//
// MOAIScopedPartitionResultBufferHandle
//================================================================//
class MOAIScopedPartitionResultBufferHandle {
private:

friend class MOAIPartitionResultMgr;

MOAIPartitionResultMgr& mMgr;
MOAIPartitionResultBuffer* mBuffer;

//----------------------------------------------------------------//
MOAIScopedPartitionResultBufferHandle ( MOAIPartitionResultMgr& mgr );

//----------------------------------------------------------------//
inline void operator = ( const MOAIScopedPartitionResultBufferHandle& assign ) {
this->mBuffer = assign.mBuffer;
(( MOAIScopedPartitionResultBufferHandle& )assign ).mBuffer = 0;
}

public:

//----------------------------------------------------------------//
MOAIPartitionResultBuffer& Buffer ();
~MOAIScopedPartitionResultBufferHandle ();

//----------------------------------------------------------------//
inline operator MOAIPartitionResultBuffer* () {
return this->mBuffer;
}

//----------------------------------------------------------------//
inline operator MOAIPartitionResultBuffer& () {
return *this->mBuffer;
}
};

//================================================================//
// MOAIPartitionResultMgr
//================================================================//
class MOAIPartitionResultMgr :
public MOAIGlobalClass < MOAIPartitionResultMgr > {
private:

friend class MOAIScopedPartitionResultBufferHandle;

ZLLeanArray < MOAIPartitionResultBuffer* > mBuffers;
ZLLeanStack < MOAIPartitionResultBuffer* > mAvailable;

MOAIPartitionResultBuffer mBuffer;

//----------------------------------------------------------------//
void ReleaseBuffer ( MOAIPartitionResultBuffer* buffer );

public:

GET ( MOAIPartitionResultBuffer&, Buffer, mBuffer )
typedef MOAIScopedPartitionResultBufferHandle Handle;

//----------------------------------------------------------------//
MOAIPartitionResultMgr ();
~MOAIPartitionResultMgr ();
Handle GetBufferHandle ();
MOAIPartitionResultMgr ();
~MOAIPartitionResultMgr ();
};

#endif

0 comments on commit 31fc0be

Please sign in to comment.