Skip to content

Commit

Permalink
Add renderer finalization mechanism (similar to 810692a and #804)
Browse files Browse the repository at this point in the history
Should help a bit with #1651.
  • Loading branch information
Xottab-DUTY committed May 11, 2024
1 parent 1583242 commit 4896861
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/Layers/xrRenderPC_GL/xrRender_GL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ class RGLRendererModule final : public RendererModule
#endif
xrRender_initconsole();
}

void ClearEnv() override
{
modes.clear();

if (GEnv.Render == &RImplementation)
{
GEnv.Render = nullptr;
GEnv.RenderFactory = nullptr;
GEnv.DU = nullptr;
GEnv.UIRender = nullptr;
GEnv.DRender = nullptr;
}
}
} static s_rgl_module;

extern "C"
Expand Down
14 changes: 14 additions & 0 deletions src/Layers/xrRenderPC_R4/xrRender_R4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ class R4RendererModule final : public RendererModule
#endif
xrRender_initconsole();
}

void ClearEnv() override
{
modes.clear();

if (GEnv.Render == &RImplementation)
{
GEnv.Render = nullptr;
GEnv.RenderFactory = nullptr;
GEnv.DU = nullptr;
GEnv.UIRender = nullptr;
GEnv.DRender = nullptr;
}
}
} static s_r4_module;

extern "C"
Expand Down
23 changes: 16 additions & 7 deletions src/xrEngine/EngineAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ void CEngineAPI::SelectRenderer()
}
}

// Ask current renderer to setup GEnv
CloseUnusedLibraries();
R_ASSERT2(selectedRenderer, "Can't setup renderer");

// Ask current renderer to setup GEnv
selectedRenderer->SetupEnv(selected_mode);

Log("Selected renderer:", selected_mode);
Expand All @@ -121,8 +123,6 @@ void CEngineAPI::Initialize(GameModule* game)
R_ASSERT(pCreate);
R_ASSERT(pDestroy);
}

CloseUnusedLibraries();
}

void CEngineAPI::Destroy()
Expand All @@ -132,19 +132,28 @@ void CEngineAPI::Destroy()
if (gameModule)
gameModule->finalize();

selectedRenderer = nullptr;
CloseUnusedLibraries();

pCreate = nullptr;
pDestroy = nullptr;

XRC.r_clear_compact();
}

void CEngineAPI::CloseUnusedLibraries()
void CEngineAPI::CloseUnusedLibraries() const
{
ZoneScoped;
for (RendererDesc& desc : g_render_modules)
for (auto& [_, handle, module] : g_render_modules)
{
if (desc.module != selectedRenderer)
desc.handle = nullptr;
if (!handle)
continue;
if (module == selectedRenderer)
continue;

module->ClearEnv();
module = nullptr;
handle = nullptr;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/xrEngine/EngineAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class XR_NOVTABLE RendererModule
virtual const xr_vector<pcstr>& ObtainSupportedModes() = 0;
virtual bool CheckGameRequirements() = 0;
virtual void SetupEnv(pcstr mode) = 0;
virtual void ClearEnv() = 0;
};

class ENGINE_API CEngineAPI
Expand All @@ -65,7 +66,7 @@ class ENGINE_API CEngineAPI
RendererModule* selectedRenderer{};

void SelectRenderer();
void CloseUnusedLibraries();
void CloseUnusedLibraries() const;

public:
Factory_Create* pCreate;
Expand Down
12 changes: 10 additions & 2 deletions src/xrEngine/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
#include "Render.h"

// resources
IRender_Light::~IRender_Light() { GEnv.Render->light_destroy(this); }
IRender_Glow::~IRender_Glow() { GEnv.Render->glow_destroy(this); }
IRender_Light::~IRender_Light()
{
if (GEnv.Render)
GEnv.Render->light_destroy(this);
}
IRender_Glow::~IRender_Glow()
{
if (GEnv.Render)
GEnv.Render->glow_destroy(this);
}

IRender::ScopedContext::ScopedContext(RenderContext context)
{
Expand Down

0 comments on commit 4896861

Please sign in to comment.