Add an alternate allocator for DecalManager; Also fix SFX weirdness.

This commit is contained in:
James Urquhart 2023-12-10 11:57:08 +00:00
parent 915fac31b3
commit 3781c7fae5
12 changed files with 146 additions and 185 deletions

View file

@ -22,85 +22,3 @@
#include "platform/platform.h"
#include "core/dataChunker.h"
//----------------------------------------------------------------------------
DataChunker::DataChunker(S32 size)
{
mChunkSize = size;
mCurBlock = NULL;
}
DataChunker::~DataChunker()
{
freeBlocks();
}
void *DataChunker::alloc(S32 size)
{
if (size > mChunkSize)
{
DataBlock * temp = (DataBlock*)dMalloc(DataChunker::PaddDBSize + size);
AssertFatal(temp, "Malloc failed");
constructInPlace(temp);
if (mCurBlock)
{
temp->next = mCurBlock->next;
mCurBlock->next = temp;
}
else
{
mCurBlock = temp;
temp->curIndex = mChunkSize;
}
return temp->getData();
}
if(!mCurBlock || size + mCurBlock->curIndex > mChunkSize)
{
const U32 paddDBSize = (sizeof(DataBlock) + 3) & ~3;
DataBlock *temp = (DataBlock*)dMalloc(paddDBSize+ mChunkSize);
AssertFatal(temp, "Malloc failed");
constructInPlace(temp);
temp->next = mCurBlock;
mCurBlock = temp;
}
void *ret = mCurBlock->getData() + mCurBlock->curIndex;
mCurBlock->curIndex += (size + 3) & ~3; // dword align
return ret;
}
DataChunker::DataBlock::DataBlock()
{
curIndex = 0;
next = NULL;
}
DataChunker::DataBlock::~DataBlock()
{
}
void DataChunker::freeBlocks(bool keepOne)
{
while (mCurBlock && mCurBlock->next)
{
DataBlock* temp = mCurBlock->next;
dFree(mCurBlock);
mCurBlock = temp;
}
if (!keepOne)
{
if (mCurBlock)
dFree(mCurBlock);
mCurBlock = NULL;
}
else if (mCurBlock)
{
mCurBlock->curIndex = 0;
mCurBlock->next = NULL;
}
}

View file

@ -14,10 +14,13 @@
#ifndef _PLATFORMASSERT_H_
# include "platform/platformAssert.h"
#endif
#ifndef _FRAMEALLOCATOR_H_
#include "core/frameAllocator.h"
#endif
#include <algorithm>
#include <stdint.h>
#include "core/frameAllocator.h"
//#include "math/mMathFn.h" // tgemit - needed here for the moment
/// Implements a chunked data allocator.
@ -242,7 +245,7 @@ public:
mFreeListHead.push(reinterpret_cast<ChunkerFreeClassList<T>*>(item));
}
void freeBlocks(bool keepOne=false)
void freeBlocks(bool keepOne = false)
{
BaseDataChunker<T>::freeBlocks(keepOne);
}
@ -293,8 +296,106 @@ public:
mFreeListHead.push(reinterpret_cast<ChunkerFreeClassList<T>*>(item));
}
void freeBlocks(bool keepOne)
void freeBlocks(bool keepOne = false)
{
BaseDataChunker<T>::freeBlocks(keepOne);
mChunker->freeBlocks(keepOne);
}
};
template<const U32 byteSize> struct DWordDataBlob
{
U32 data[(byteSize + 3)/ 4];
};
/// Implements a three-tiered chunker
/// K1..3 should be ordered from low to high
template<class K1, class K2, class K3> class ThreeTieredChunker
{
public:
struct Handle
{
U32 tier;
void* ptr;
Handle() : tier(0), ptr(NULL) { ; }
Handle(const Handle& other) : tier(other.tier), ptr(other.ptr) { ; }
Handle(U32 in_tier, void* in_ptr) : tier(in_tier), ptr(in_ptr) { ; }
Handle& operator=(const Handle& other) {
tier = other.tier;
ptr = other.ptr;
return *this;
}
};
protected:
ClassChunker<K1> mT1;
ClassChunker<K2> mT2;
ClassChunker<K3> mT3;
public:
Handle alloc(U32 byteSize)
{
Handle outH;
if (byteSize > sizeof(K3))
{
const U32 wordSize = (byteSize + 3) / 4;
outH = Handle(0, (void*)(new U32[wordSize]));
}
else
{
if (byteSize <= sizeof(K1))
{
outH = Handle(1, (void*)mT1.alloc());
}
else if (byteSize <= sizeof(K2))
{
outH = Handle(2, (void*)mT2.alloc());
}
else if (byteSize <= sizeof(K3))
{
outH = Handle(3, (void*)mT3.alloc());
}
else
{
outH = Handle(0, NULL);
}
}
return outH;
}
void free(Handle& item)
{
if (item.ptr == NULL)
return;
switch (item.tier)
{
case 0:
delete[] ((U32*)item.ptr);
break;
case 1:
mT1.free((K1*)item.ptr);
break;
case 2:
mT2.free((K2*)item.ptr);
break;
case 3:
mT3.free((K3*)item.ptr);
break;
default:
break;
}
}
void freeBlocks(bool keepOne = false)
{
mT1.freeBlocks(keepOne);
mT2.freeBlocks(keepOne);
mT3.freeBlocks(keepOne);
}
};

View file

@ -330,22 +330,13 @@ public:
}
U32 getObjectCount(void) const { return mNumObjectsInMemory; }
/// NOTE: This will return the memory, NOT perform a ones-complement
T* operator ~() { return mMemory; };
/// NOTE: This will return the memory, NOT perform a ones-complement
const T* operator ~() const { return mMemory; };
/// NOTE: This will dereference the memory, NOT do standard unary plus behavior
T& operator +() { return *mMemory; };
/// NOTE: This will dereference the memory, NOT do standard unary plus behavior
const T& operator +() const { return *mMemory; };
U32 size(void) const { return mNumObjectsInMemory; }
T& operator *() { return *mMemory; };
const T& operator *() const { return *mMemory; };
const T& operator *() const { return *mMemory; }
T** operator &() { return &mMemory; };
const T** operator &() const { return &mMemory; };
T** operator &() { return &mMemory; }
const T** operator &() const { return &mMemory; }
operator T* () { return mMemory; }
operator const T* () const { return mMemory; }
@ -354,7 +345,7 @@ public:
operator const T& () const { return *mMemory; }
operator T() { return *mMemory; }
operator const T() const { return *mMemory;
operator const T() const { return *mMemory; }
inline T* address() const { return mMemory; }
@ -366,35 +357,6 @@ public:
const T& operator[](const S32 idx) const { return mMemory[idx]; }
};
//-----------------------------------------------------------------------------
// FrameTemp specializations for types with no constructor/destructor
#define FRAME_TEMP_NC_SPEC(type) \
template<> \
inline FrameTemp<type>::FrameTemp( const U32 count ) \
{ \
AssertFatal( count > 0, "Allocating a FrameTemp with less than one instance" ); \
mWaterMark = FrameAllocator::getWaterMark(); \
mMemory = reinterpret_cast<type *>( FrameAllocator::alloc( sizeof( type ) * count ) ); \
} \
template<>\
inline FrameTemp<type>::~FrameTemp() \
{ \
FrameAllocator::setWaterMark( mWaterMark ); \
} \
FRAME_TEMP_NC_SPEC(char);
FRAME_TEMP_NC_SPEC(float);
FRAME_TEMP_NC_SPEC(double);
FRAME_TEMP_NC_SPEC(bool);
FRAME_TEMP_NC_SPEC(int);
FRAME_TEMP_NC_SPEC(short);
FRAME_TEMP_NC_SPEC(unsigned char);
FRAME_TEMP_NC_SPEC(unsigned int);
FRAME_TEMP_NC_SPEC(unsigned short);
#undef FRAME_TEMP_NC_SPEC
//-----------------------------------------------------------------------------
#endif // _H_FRAMEALLOCATOR_

View file

@ -144,8 +144,8 @@ inline void Swizzle<T, mapLength>::InPlace( void *memory, const dsize_t size ) c
// FrameTemp should work because the PNG loading code uses the FrameAllocator, so
// it should only get used on an image w/ that size as max -patw
FrameTemp<U8> buffer( size );
dMemcpy( ~buffer, memory, size );
ToBuffer( memory, ~buffer, size );
dMemcpy( buffer.address(), memory, size);
ToBuffer( memory, buffer.address(), size);
}
}