Properly init / shutdown device manager (all platforms) and SDL (non-windows) to avoid leaks

This commit is contained in:
Stephen Saunders 2023-12-07 18:16:11 -05:00 committed by Robert Beckebans
parent 8867e865e0
commit e0fe1b8bed
5 changed files with 31 additions and 20 deletions

View file

@ -41,6 +41,7 @@ If you have questions concerning this license or the applicable additional terms
#include "nvrhi/utils.h"
#include <sys/DeviceManager.h>
extern DeviceManager* deviceManager;
extern idCVar r_graphicsAPI;
idCVar r_drawFlickerBox( "r_drawFlickerBox", "0", CVAR_RENDERER | CVAR_BOOL, "visual test for dropping frames" );
idCVar stereoRender_warp( "stereoRender_warp", "0", CVAR_RENDERER | CVAR_ARCHIVE | CVAR_BOOL, "use the optical warping renderprog instead of stereoDeGhost" );
@ -160,6 +161,18 @@ void idRenderBackend::Init()
common->FatalError( "R_InitOpenGL called while active" );
}
// SRS - create deviceManager here to prevent allocation loop via R_SetNewMode( true )
nvrhi::GraphicsAPI api = nvrhi::GraphicsAPI::D3D12;
if( !idStr::Icmp( r_graphicsAPI.GetString(), "vulkan" ) )
{
api = nvrhi::GraphicsAPI::VULKAN;
}
else if( !idStr::Icmp( r_graphicsAPI.GetString(), "dx12" ) )
{
api = nvrhi::GraphicsAPI::D3D12;
}
deviceManager = DeviceManager::Create( api );
// DG: make sure SDL has setup video so getting supported modes in R_SetNewMode() works
#if defined( VULKAN_USE_PLATFORM_SDL )
VKimp_PreInit();
@ -281,10 +294,17 @@ void idRenderBackend::Shutdown()
fhImmediateMode::Shutdown();
#if defined( VULKAN_USE_PLATFORM_SDL )
VKimp_Shutdown();
VKimp_Shutdown( true ); // SRS - shutdown SDL on quit
#else
GLimp_Shutdown();
#endif
// SRS - delete deviceManager instance on backend shutdown
if( deviceManager )
{
delete deviceManager;
deviceManager = NULL;
}
}
/*

View file

@ -1387,8 +1387,8 @@ bool VKimp_Init( glimpParms_t parms );
bool VKimp_SetScreenParms( glimpParms_t parms );
// Destroys the rendering context, closes the window, resets the resolution,
// and resets the gamma ramps.
void VKimp_Shutdown();
// and resets the gamma ramps. SRS - Optionally shuts down SDL for quit.
void VKimp_Shutdown( bool shutdownSDL );
// Sets the hardware gamma ramps for gamma and brightness adjustment.
// These are now taken as 16 bit values, so we can take full advantage

View file

@ -308,7 +308,7 @@ const char* fileExten[4] = { "tga", "png", "jpg", "exr" };
const char* envDirection[6] = { "_px", "_nx", "_py", "_ny", "_pz", "_nz" };
const char* skyDirection[6] = { "_forward", "_back", "_left", "_right", "_up", "_down" };
DeviceManager* deviceManager;
DeviceManager* deviceManager = NULL;
bool R_UseTemporalAA()
@ -474,17 +474,6 @@ void R_SetNewMode( const bool fullInit )
{
// create the context as well as setting up the window
nvrhi::GraphicsAPI api = nvrhi::GraphicsAPI::D3D12;
if( !idStr::Icmp( r_graphicsAPI.GetString(), "vulkan" ) )
{
api = nvrhi::GraphicsAPI::VULKAN;
}
else if( !idStr::Icmp( r_graphicsAPI.GetString(), "dx12" ) )
{
api = nvrhi::GraphicsAPI::D3D12;
}
deviceManager = DeviceManager::Create( api );
#if defined( VULKAN_USE_PLATFORM_SDL )
if( VKimp_Init( parms ) )
#else

View file

@ -1243,7 +1243,10 @@ void DeviceManager_VK::DestroyDeviceAndSwapChain()
{
OPTICK_SHUTDOWN();
m_VulkanDevice.waitIdle();
if( m_VulkanDevice )
{
m_VulkanDevice.waitIdle();
}
m_FrameWaitQuery = nullptr;

View file

@ -275,6 +275,7 @@ bool VKimp_Init( glimpParms_t parms )
if( !deviceManager->CreateWindowDeviceAndSwapChain( createParms, GAME_NAME ) )
{
common->Warning( "Couldn't initialize Vulkan subsystem for r_fullscreen = %i", parms.fullScreen );
VKimp_Shutdown( false );
return false;
}
@ -543,15 +544,13 @@ void DeviceManager::Shutdown()
VKimp_Shutdown
===================
*/
void VKimp_Shutdown()
void VKimp_Shutdown( bool shutdownSDL )
{
common->Printf( "Shutting down Vulkan subsystem\n" );
if( deviceManager )
{
deviceManager->Shutdown();
delete deviceManager;
deviceManager = NULL;
}
if( window )
@ -560,7 +559,7 @@ void VKimp_Shutdown()
window = nullptr;
}
if( SDL_WasInit( 0 ) )
if( shutdownSDL && SDL_WasInit( 0 ) )
{
SDL_Quit();
}