Skip to content

Commit

Permalink
D3D11: re-use shadow buffer for temporary staging
Browse files Browse the repository at this point in the history
as we never use both at the same time
  • Loading branch information
paroj committed Sep 5, 2020
1 parent a869d23 commit f87ef02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
2 changes: 0 additions & 2 deletions RenderSystems/Direct3D11/include/OgreD3D11HardwareBuffer.h
Expand Up @@ -49,8 +49,6 @@ namespace Ogre {
protected:
ComPtr<ID3D11Buffer> mlpD3DBuffer;
bool mUseTempStagingBuffer;
D3D11HardwareBuffer* mpTempStagingBuffer;
bool mStagingUploadNeeded;
BufferType mBufferType;
D3D11Device & mDevice;
D3D11_BUFFER_DESC mDesc;
Expand Down
43 changes: 13 additions & 30 deletions RenderSystems/Direct3D11/src/OgreD3D11HardwareBuffer.cpp
Expand Up @@ -37,7 +37,6 @@ namespace Ogre {
HardwareBuffer::Usage usage, D3D11Device & device,
bool useShadowBuffer, bool streamOut)
: HardwareBuffer(usage, false, useShadowBuffer),
mpTempStagingBuffer(0),
mUseTempStagingBuffer(false),
mBufferType(btype),
mDevice(device)
Expand Down Expand Up @@ -94,23 +93,11 @@ namespace Ogre {

}
//---------------------------------------------------------------------
D3D11HardwareBuffer::~D3D11HardwareBuffer()
{
SAFE_DELETE(mpTempStagingBuffer); // should never be nonzero unless destroyed while locked
mShadowBuffer.reset();
}
D3D11HardwareBuffer::~D3D11HardwareBuffer() {}
//---------------------------------------------------------------------
void* D3D11HardwareBuffer::lockImpl(size_t offset,
size_t length, LockOptions options)
{
if (length > mSizeInBytes)
{
// need to realloc
mDesc.ByteWidth = static_cast<UINT>(mSizeInBytes);
OGRE_CHECK_DX_ERROR(mDevice->CreateBuffer(&mDesc, 0, mlpD3DBuffer.ReleaseAndGetAddressOf()));
}


if (mUsage == HBU_CPU_ONLY || (mUsage & HardwareBuffer::HBU_DYNAMIC &&
(options == HardwareBuffer::HBL_DISCARD || options == HardwareBuffer::HBL_NO_OVERWRITE)))
{
Expand Down Expand Up @@ -157,23 +144,20 @@ namespace Ogre {
else
{
mUseTempStagingBuffer = true;
if (!mpTempStagingBuffer)
{
// create another buffer instance but use system memory
mpTempStagingBuffer = new D3D11HardwareBuffer(mBufferType,
mSizeInBytes, HBU_CPU_ONLY, mDevice, false, false);
}
OgreAssertDbg(!mShadowBuffer,
"we should never arrive here, when already having a shadow buffer");
// create temporary shadow buffer
mShadowBuffer.reset(new D3D11HardwareBuffer(mBufferType,
mSizeInBytes, HBU_CPU_ONLY, mDevice, false, false));

// schedule a copy to the staging
if (options != HBL_DISCARD)
mpTempStagingBuffer->copyData(*this, 0, 0, mSizeInBytes, true);
mShadowBuffer->copyData(*this, offset, offset, length, true);

// register whether we'll need to upload on unlock
mStagingUploadNeeded = (options != HBL_READ_ONLY);

return mpTempStagingBuffer->lock(offset, length, options);

mShadowUpdated = (options != HBL_READ_ONLY);

return mShadowBuffer->lock(offset, length, options);
}
}
//---------------------------------------------------------------------
Expand All @@ -185,16 +169,15 @@ namespace Ogre {
mUseTempStagingBuffer = false;

// ok, we locked the staging buffer
mpTempStagingBuffer->unlock();
mShadowBuffer->unlock();

// copy data if needed
// this is async but driver should keep reference
if (mStagingUploadNeeded)
copyData(*mpTempStagingBuffer, 0, 0, mSizeInBytes, true);
if (mShadowUpdated)
copyDataImpl(*mShadowBuffer, mLockStart, mLockStart, mLockSize, true);

// delete
// not that efficient, but we should not be locking often
SAFE_DELETE(mpTempStagingBuffer);
mShadowBuffer.reset();
}
else
{
Expand Down

0 comments on commit f87ef02

Please sign in to comment.