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 daa6888
Show file tree
Hide file tree
Showing 26 changed files with 121 additions and 119 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 _OgreExport 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 _OgreExport 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
8 changes: 4 additions & 4 deletions RenderSystems/Direct3D11/include/OgreD3D11HLSLProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ THE SOFTWARE.

#include "OgreD3D11Prerequisites.h"

#include "OgreD3D11DeviceResource.h"
#include "OgreDeviceDependedResource.h"
#include "OgreHighLevelGpuProgram.h"
#include "OgreString.h"
#include "Vao/OgreVertexBufferPacked.h"
Expand All @@ -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
8 changes: 4 additions & 4 deletions RenderSystems/Direct3D11/include/OgreD3D11HardwareBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ THE SOFTWARE.

#include "OgreD3D11Prerequisites.h"

#include "OgreD3D11DeviceResource.h"
#include "OgreDeviceDependedResource.h"
#include "OgreHardwareBuffer.h"

namespace Ogre
Expand All @@ -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 @@ -32,7 +32,7 @@ THE SOFTWARE.
#include "OgreD3D11Prerequisites.h"

#include "OgreCommon.h"
#include "OgreD3D11DeviceResource.h"
#include "OgreDeviceDependedResource.h"
#include "OgreRenderPassDescriptor.h"
#include "OgreRenderSystem.h"

Expand Down 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
5 changes: 3 additions & 2 deletions RenderSystems/Direct3D11/include/OgreD3D11RenderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ THE SOFTWARE.
#include "OgreRenderSystem.h"

#include "OgreD3D11Device.h"
#include "OgreD3D11DeviceResource.h"
#include "OgreD3D11Driver.h"
#include "OgreD3D11PixelFormatToShaderType.h"
#include "OgreD3D11RenderPassDescriptor.h"
#include "OgreDeviceDependedResource.h"

namespace Ogre
{
Expand All @@ -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
8 changes: 4 additions & 4 deletions RenderSystems/Direct3D11/include/OgreD3D11TextureGpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ THE SOFTWARE.

#include "OgreD3D11Prerequisites.h"

#include "OgreD3D11DeviceResource.h"
#include "OgreDescriptorSetTexture.h"
#include "OgreDescriptorSetUav.h"
#include "OgreDeviceDependedResource.h"
#include "OgreTextureGpu.h"

#include "OgreHeaderPrefix.h"

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
8 changes: 4 additions & 4 deletions RenderSystems/Direct3D11/include/OgreD3D11Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ THE SOFTWARE.
#include "OgreD3D11Prerequisites.h"

#include "OgreCommon.h"
#include "OgreD3D11DeviceResource.h"
#include "OgreDeviceDependedResource.h"
#include "OgreWindow.h"

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 @@ -31,7 +31,7 @@ THE SOFTWARE.

#include "OgreD3D11Prerequisites.h"

#include "OgreD3D11DeviceResource.h"
#include "OgreDeviceDependedResource.h"
#include "Vao/OgreBufferInterface.h"

namespace Ogre
Expand All @@ -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
Loading

0 comments on commit daa6888

Please sign in to comment.