Skip to content

Commit

Permalink
Main: add lightweight DefaultBuffer class for shadow buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
paroj committed Dec 20, 2020
1 parent 5556acc commit 0d38895
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
16 changes: 16 additions & 0 deletions OgreMain/include/OgreDefaultHardwareBufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ namespace Ogre {
* @{
*/

/// Specialisation of Buffer using malloc e.g. for use as shadow buffer
class _OgreExport DefaultBuffer : public Buffer
{
protected:
unsigned char* mData;
void* lockImpl(size_t offset, size_t length, LockOptions options);
void unlockImpl(void);
public:
explicit DefaultBuffer(size_t sizeInBytes);
~DefaultBuffer();
void readData(size_t offset, size_t length, void* pDest);
void writeData(size_t offset, size_t length, const void* pSource, bool discardWholeBuffer = false);
void* lock(size_t offset, size_t length, LockOptions options);
void unlock(void);
};

/// Specialisation of HardwareVertexBuffer for emulation
class _OgreExport DefaultHardwareVertexBuffer : public HardwareVertexBuffer
{
Expand Down
33 changes: 33 additions & 0 deletions OgreMain/src/OgreDefaultHardwareBufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ THE SOFTWARE.
#include "OgreDefaultHardwareBufferManager.h"

namespace Ogre {
DefaultBuffer::DefaultBuffer(size_t sizeInBytes)
: Buffer(sizeInBytes, HBU_CPU_ONLY)
{
// Allocate aligned memory for better SIMD processing friendly.
mData = static_cast<unsigned char*>(AlignedMemory::allocate(mSizeInBytes));
}
//-----------------------------------------------------------------------
DefaultBuffer::~DefaultBuffer() { AlignedMemory::deallocate(mData); }
//-----------------------------------------------------------------------
void* DefaultBuffer::lockImpl(size_t offset, size_t length, LockOptions options) { return mData + offset; }
//-----------------------------------------------------------------------
void DefaultBuffer::unlockImpl() {}
//-----------------------------------------------------------------------
void* DefaultBuffer::lock(size_t offset, size_t length, LockOptions options)
{
mIsLocked = true;
return mData + offset;
}
void DefaultBuffer::unlock(void) { mIsLocked = false; }
//-----------------------------------------------------------------------
void DefaultBuffer::readData(size_t offset, size_t length, void* pDest)
{
assert((offset + length) <= mSizeInBytes);
memcpy(pDest, mData + offset, length);
}
//-----------------------------------------------------------------------
void DefaultBuffer::writeData(size_t offset, size_t length, const void* pSource, bool discardWholeBuffer)
{
assert((offset + length) <= mSizeInBytes);
// ignore discard, memory is not guaranteed to be zeroised
memcpy(mData + offset, pSource, length);
}


DefaultHardwareVertexBuffer::DefaultHardwareVertexBuffer(size_t vertexSize, size_t numVertices,
HardwareBuffer::Usage usage)
Expand Down
3 changes: 1 addition & 2 deletions OgreMain/src/OgreHardwareIndexBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ namespace Ogre {
// Create a shadow buffer if required
if (mUseShadowBuffer)
{
mShadowBuffer.reset(new DefaultHardwareIndexBuffer(mIndexType,
mNumIndexes, HardwareBuffer::HBU_DYNAMIC));
mShadowBuffer.reset(new DefaultBuffer(mSizeInBytes));
}


Expand Down
2 changes: 1 addition & 1 deletion OgreMain/src/OgreHardwareUniformBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Ogre {
// Create a shadow buffer if required
if (mUseShadowBuffer)
{
mShadowBuffer.reset(new DefaultHardwareUniformBuffer(mMgr, sizeBytes, HardwareBuffer::HBU_DYNAMIC, false));
mShadowBuffer.reset(new DefaultBuffer(sizeBytes));
}
}

Expand Down
3 changes: 1 addition & 2 deletions OgreMain/src/OgreHardwareVertexBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ namespace Ogre {
// Create a shadow buffer if required
if (mUseShadowBuffer)
{
mShadowBuffer.reset(new DefaultHardwareVertexBuffer(mMgr, mVertexSize,
mNumVertices, HardwareBuffer::HBU_DYNAMIC));
mShadowBuffer.reset(new DefaultBuffer(mSizeInBytes));
}

}
Expand Down
2 changes: 1 addition & 1 deletion RenderSystems/Direct3D11/include/OgreD3D11HardwareBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace Ogre {
/** See HardwareBuffer. We perform a hardware copy here. */
void copyData(HardwareBuffer& srcBuffer, size_t srcOffset,
size_t dstOffset, size_t length, bool discardWholeBuffer = false);
void copyDataImpl(HardwareBuffer& srcBuffer, size_t srcOffset,
void copyDataImpl(Buffer& srcBuffer, size_t srcOffset,
size_t dstOffset, size_t length, bool discardWholeBuffer = false);
/// Updates the real buffer from the shadow buffer, if required
virtual void _updateFromShadow(void);
Expand Down
2 changes: 1 addition & 1 deletion RenderSystems/Direct3D11/src/OgreD3D11HardwareBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ namespace Ogre {
copyDataImpl(srcBuffer, srcOffset, dstOffset, length, discardWholeBuffer);
}
//---------------------------------------------------------------------
void D3D11HardwareBuffer::copyDataImpl(HardwareBuffer& srcBuffer, size_t srcOffset,
void D3D11HardwareBuffer::copyDataImpl(Buffer& srcBuffer, size_t srcOffset,
size_t dstOffset, size_t length, bool discardWholeBuffer)
{
// If we're copying same-size buffers in their entirety...
Expand Down

0 comments on commit 0d38895

Please sign in to comment.