Skip to content

Commit

Permalink
Move D3D11DeviceResource[Manager] => OgreMain/DeviceDependedResource[…
Browse files Browse the repository at this point in the history
…Manager], in preparation to handle device lost event in Vulkan
  • Loading branch information
eugenegff committed Nov 11, 2024
1 parent 93c3add commit 768a585
Show file tree
Hide file tree
Showing 26 changed files with 109 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,55 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef __D3D11Resource_H__
#define __D3D11Resource_H__
#ifndef __DeviceDependedResource_H__
#define __DeviceDependedResource_H__

#include "OgreD3D11Prerequisites.h"
#include "OgrePrerequisites.h"

#include "ogrestd/vector.h"

namespace Ogre
{
/** Represents a Direct3D rendering resource.
/** Represents a GPU device depended resource.
Provide unified interface to handle various device states.
This class is intended to be used as protected base.
*/
class D3D11DeviceResource
class DeviceDependedResource
{
public:
// Called immediately after the Direct3D device has entered a removed state.
// Called immediately after the device has entered a lost state.
// This is the place to release device depended resources.
virtual void notifyDeviceLost( D3D11Device *device ) = 0;
virtual void notifyDeviceLost() = 0;

// Called immediately after the Direct3D device has been reset.
// Called immediately after the device has been reset.
// This is the place to create device depended resources.
virtual void notifyDeviceRestored( D3D11Device *device, unsigned pass ) = 0;
virtual void notifyDeviceRestored( unsigned pass ) = 0;

protected:
D3D11DeviceResource();
~D3D11DeviceResource(); // protected and non-virtual
DeviceDependedResource();
~DeviceDependedResource(); // protected and non-virtual
};

/** Singleton that is used to propagate device state changed notifications.
This class is intended to be used as protected base.
*/
class D3D11DeviceResourceManager
class DeviceDependedResourceManager
{
public:
void notifyResourceCreated( D3D11DeviceResource *deviceResource );
void notifyResourceDestroyed( D3D11DeviceResource *deviceResource );
void notifyResourceCreated( DeviceDependedResource *deviceResource );
void notifyResourceDestroyed( DeviceDependedResource *deviceResource );

void notifyDeviceLost( D3D11Device *device );
void notifyDeviceRestored( D3D11Device *device );
void notifyDeviceLost();
void notifyDeviceRestored();

static D3D11DeviceResourceManager *get();
static DeviceDependedResourceManager *get();

protected:
D3D11DeviceResourceManager();
~D3D11DeviceResourceManager(); // protected and non-virtual
DeviceDependedResourceManager();
~DeviceDependedResourceManager(); // protected and non-virtual

private:
vector<D3D11DeviceResource *>::type mResources, mResourcesCopy;
vector<DeviceDependedResource *>::type mResources, mResourcesCopy;
};

} // namespace Ogre
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,90 +25,90 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "OgreD3D11DeviceResource.h"
#include "OgreStableHeaders.h"

#include "ogrestd/vector.h"
#include "OgreDeviceDependedResource.h"

namespace Ogre
{
D3D11DeviceResource::D3D11DeviceResource()
DeviceDependedResource::DeviceDependedResource()
{
D3D11DeviceResourceManager::get()->notifyResourceCreated( this );
DeviceDependedResourceManager::get()->notifyResourceCreated( this );
}

D3D11DeviceResource::~D3D11DeviceResource()
DeviceDependedResource::~DeviceDependedResource()
{
D3D11DeviceResourceManager::get()->notifyResourceDestroyed( this );
DeviceDependedResourceManager::get()->notifyResourceDestroyed( this );
}

// ------------------------------------------------------------------------
static D3D11DeviceResourceManager *gs_D3D11DeviceResourceManager = NULL;
static DeviceDependedResourceManager *gs_DeviceDependedResourceManager = NULL;

D3D11DeviceResourceManager *D3D11DeviceResourceManager::get()
DeviceDependedResourceManager *DeviceDependedResourceManager::get()
{
return gs_D3D11DeviceResourceManager;
return gs_DeviceDependedResourceManager;
}

D3D11DeviceResourceManager::D3D11DeviceResourceManager()
DeviceDependedResourceManager::DeviceDependedResourceManager()
{
assert( gs_D3D11DeviceResourceManager == NULL );
gs_D3D11DeviceResourceManager = this;
assert( gs_DeviceDependedResourceManager == NULL );
gs_DeviceDependedResourceManager = this;
}

D3D11DeviceResourceManager::~D3D11DeviceResourceManager()
DeviceDependedResourceManager::~DeviceDependedResourceManager()
{
assert( mResources.empty() );
assert( gs_D3D11DeviceResourceManager == this );
gs_D3D11DeviceResourceManager = NULL;
assert( gs_DeviceDependedResourceManager == this );
gs_DeviceDependedResourceManager = NULL;
}

void D3D11DeviceResourceManager::notifyResourceCreated( D3D11DeviceResource *deviceResource )
void DeviceDependedResourceManager::notifyResourceCreated( DeviceDependedResource *deviceResource )
{
assert( std::find( mResources.begin(), mResources.end(), deviceResource ) == mResources.end() );
mResources.push_back( deviceResource );
}

void D3D11DeviceResourceManager::notifyResourceDestroyed( D3D11DeviceResource *deviceResource )
void DeviceDependedResourceManager::notifyResourceDestroyed( DeviceDependedResource *deviceResource )
{
vector<D3D11DeviceResource *>::type::iterator it =
vector<DeviceDependedResource *>::type::iterator it =
std::find( mResources.begin(), mResources.end(), deviceResource );
assert( it != mResources.end() );
mResources.erase( it );

vector<D3D11DeviceResource *>::type::iterator itCopy =
vector<DeviceDependedResource *>::type::iterator itCopy =
std::find( mResourcesCopy.begin(), mResourcesCopy.end(), deviceResource );
if( itCopy != mResourcesCopy.end() )
*itCopy = NULL;
}

void D3D11DeviceResourceManager::notifyDeviceLost( D3D11Device *device )
void DeviceDependedResourceManager::notifyDeviceLost()
{
assert( mResourcesCopy.empty() ); // reentrancy is not expected nor supported
mResourcesCopy = mResources;

vector<D3D11DeviceResource *>::type::iterator it = mResourcesCopy.begin();
vector<D3D11DeviceResource *>::type::iterator en = mResourcesCopy.end();
vector<DeviceDependedResource *>::type::iterator it = mResourcesCopy.begin();
vector<DeviceDependedResource *>::type::iterator en = mResourcesCopy.end();
while( it != en )
{
if( D3D11DeviceResource *deviceResource = *it )
deviceResource->notifyDeviceLost( device );
if( DeviceDependedResource *deviceResource = *it )
deviceResource->notifyDeviceLost();
++it;
}
mResourcesCopy.clear();
}

void D3D11DeviceResourceManager::notifyDeviceRestored( D3D11Device *device )
void DeviceDependedResourceManager::notifyDeviceRestored()
{
assert( mResourcesCopy.empty() ); // reentrancy is not expected nor supported
mResourcesCopy = mResources;
for( unsigned pass = 0; pass < 2; ++pass )
{
vector<D3D11DeviceResource *>::type::iterator it = mResourcesCopy.begin();
vector<D3D11DeviceResource *>::type::iterator en = mResourcesCopy.end();
vector<DeviceDependedResource *>::type::iterator it = mResourcesCopy.begin();
vector<DeviceDependedResource *>::type::iterator en = mResourcesCopy.end();
while( it != en )
{
if( D3D11DeviceResource *deviceResource = *it )
deviceResource->notifyDeviceRestored( device, pass );
if( DeviceDependedResource *deviceResource = *it )
deviceResource->notifyDeviceRestored( pass );
++it;
}
}
Expand Down
6 changes: 3 additions & 3 deletions RenderSystems/Direct3D11/include/OgreD3D11HLSLProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Ogre
can produce programs for OpenGL too.
*/
class _OgreD3D11Export D3D11HLSLProgram final : public HighLevelGpuProgram,
protected D3D11DeviceResource
protected DeviceDependedResource
{
public:
/// Command object for setting entry point
Expand Down Expand Up @@ -94,8 +94,8 @@ namespace Ogre
static CmdColumnMajorMatrices msCmdColumnMajorMatrices;
static CmdEnableBackwardsCompatibility msCmdEnableBackwardsCompatibility;

void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

/** Internal method for creating an appropriate low-level program from this
high-level program, must be implemented by subclasses. */
Expand Down
6 changes: 3 additions & 3 deletions RenderSystems/Direct3D11/include/OgreD3D11HardwareBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Ogre
aspects.
*/
class _OgreD3D11Export D3D11HardwareBuffer final : public HardwareBuffer,
protected D3D11DeviceResource
protected DeviceDependedResource
{
public:
enum BufferType
Expand All @@ -65,8 +65,8 @@ namespace Ogre
/** See HardwareBuffer. */
void unlockImpl() override;

void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

public:
D3D11HardwareBuffer( BufferType btype, size_t sizeBytes, HardwareBuffer::Usage usage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace Ogre
*/
class _OgreD3D11Export D3D11RenderPassDescriptor final : public RenderPassDescriptor,
public RenderSystem::Listener,
protected D3D11DeviceResource
protected DeviceDependedResource
{
protected:
ComPtr<ID3D11RenderTargetView> mColourRtv[OGRE_MAX_MULTIPLE_RENDER_TARGETS];
Expand All @@ -74,8 +74,8 @@ namespace Ogre
D3D11Device &mDevice;
D3D11RenderSystem *mRenderSystem;

void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

void checkRenderWindowStatus();
void calculateSharedKey();
Expand Down
3 changes: 2 additions & 1 deletion RenderSystems/Direct3D11/include/OgreD3D11RenderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ namespace Ogre
/**
Implementation of DirectX11 as a rendering system.
*/
class _OgreD3D11Export D3D11RenderSystem : public RenderSystem, protected D3D11DeviceResourceManager
class _OgreD3D11Export D3D11RenderSystem : public RenderSystem,
protected DeviceDependedResourceManager
{
private:
Ogre::String mDriverName; // it`s hint rather than hard requirement, could be ignored if empty
Expand Down
6 changes: 3 additions & 3 deletions RenderSystems/Direct3D11/include/OgreD3D11TextureGpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ THE SOFTWARE.

namespace Ogre
{
class _OgreD3D11Export D3D11TextureGpu : public TextureGpu, protected D3D11DeviceResource
class _OgreD3D11Export D3D11TextureGpu : public TextureGpu, protected DeviceDependedResource
{
protected:
/// The general case is that the whole D3D11 texture will be accessed through the SRV.
Expand Down Expand Up @@ -81,8 +81,8 @@ namespace Ogre
void createInternalResourcesImpl() override;
void destroyInternalResourcesImpl() override;

void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

public:
D3D11TextureGpu( GpuPageOutStrategy::GpuPageOutStrategy pageOutStrategy, VaoManager *vaoManager,
Expand Down
6 changes: 3 additions & 3 deletions RenderSystems/Direct3D11/include/OgreD3D11Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ THE SOFTWARE.

namespace Ogre
{
class _OgreD3D11Export D3D11Window : public Window, protected D3D11DeviceResource
class _OgreD3D11Export D3D11Window : public Window, protected DeviceDependedResource
{
protected:
D3D11Device &mDevice;
Expand Down Expand Up @@ -113,8 +113,8 @@ namespace Ogre

void resizeSwapChainBuffers( uint32 width, uint32 height );
void notifyResolutionChanged();
void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

public:
D3D11WindowSwapChainBased( const String &title, uint32 width, uint32 height, bool fullscreenMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Ogre
Hence most equivalent functionality is encapsulated here.
*/
class _OgreD3D11Export D3D11BufferInterfaceBase : public BufferInterface,
protected D3D11DeviceResource
protected DeviceDependedResource
{
protected:
size_t mVboPoolIdx;
Expand All @@ -51,8 +51,8 @@ namespace Ogre
size_t alignment, ID3D11Buffer *dstBuffer, ID3D11Buffer *srcBuffer,
ID3D11DeviceContextN *context );

void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

public:
D3D11BufferInterfaceBase( size_t vboPoolIdx, ID3D11Buffer *d3dBuffer );
Expand Down
6 changes: 3 additions & 3 deletions RenderSystems/Direct3D11/include/Vao/OgreD3D11DynamicBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace Ogre
Caller is responsible for proper synchronization.
No check is performed to see if two map calls overlap.
*/
class _OgreD3D11Export D3D11DynamicBuffer final : protected D3D11DeviceResource
class _OgreD3D11Export D3D11DynamicBuffer final : protected DeviceDependedResource
{
protected:
struct MappedRange
Expand All @@ -76,8 +76,8 @@ namespace Ogre

size_t addMappedRange( size_t start, size_t count );

void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

public:
D3D11DynamicBuffer( ID3D11Buffer *vboName, size_t vboSize, D3D11Device &device );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Ogre
class D3D11BufferInterface;

class _OgreD3D11Export D3D11ReadOnlyBufferPacked final : public ReadOnlyBufferPacked,
protected D3D11DeviceResource
protected DeviceDependedResource
{
DXGI_FORMAT mInternalFormat;
D3D11Device &mDevice;
Expand All @@ -61,8 +61,8 @@ namespace Ogre
ID3D11ShaderResourceView *createResourceView( int cacheIdx, uint32 offset, uint32 sizeBytes );
ID3D11ShaderResourceView *bindBufferCommon( size_t offset, size_t sizeBytes );

void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

public:
D3D11ReadOnlyBufferPacked( size_t internalBufStartBytes, size_t numElements,
Expand Down
7 changes: 4 additions & 3 deletions RenderSystems/Direct3D11/include/Vao/OgreD3D11StagingBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ namespace Ogre
In other words, a staging buffer is an intermediate buffer to transfer data between
CPU & GPU
*/
class _OgreD3D11Export D3D11StagingBuffer final : public StagingBuffer, protected D3D11DeviceResource
class _OgreD3D11Export D3D11StagingBuffer final : public StagingBuffer,
protected DeviceDependedResource
{
protected:
/// mVboName is not deleted by us (the VaoManager does) as we may have
Expand Down Expand Up @@ -81,8 +82,8 @@ namespace Ogre

const void *_mapForReadImpl( size_t offset, size_t sizeBytes ) override;

void notifyDeviceLost( D3D11Device *device ) override;
void notifyDeviceRestored( D3D11Device *device, unsigned pass ) override;
void notifyDeviceLost() override;
void notifyDeviceRestored( unsigned pass ) override;

public:
D3D11StagingBuffer( size_t sizeBytes, VaoManager *vaoManager, bool uploadOnly,
Expand Down
Loading

0 comments on commit 768a585

Please sign in to comment.