From 42ce4f8845f84a4913d043f2375e1a7824c7c160 Mon Sep 17 00:00:00 2001 From: yohjimane Date: Wed, 18 Dec 2024 01:43:58 -0800 Subject: [PATCH] Port changes from OGSR to fix #1748 --- src/Include/xrRender/UIShader.h | 4 ++-- src/Layers/xrRender/dxUIRender.cpp | 4 ++++ src/Layers/xrRender/dxUIRender.h | 12 ------------ src/Layers/xrRender/dxUIShader.cpp | 24 ++++++++++++++++++++++-- src/Layers/xrRender/dxUIShader.h | 6 ++++-- src/xrGame/ui/UICellItem.cpp | 10 +++++++--- src/xrGame/ui/UILoadingScreen.cpp | 2 +- src/xrGame/ui/UIStatsIcon.cpp | 2 ++ src/xrUICore/Static/UIStatic.h | 2 ++ src/xrUICore/Static/UIStaticItem.cpp | 2 +- src/xrUICore/Static/UIStaticItem.h | 5 ++++- 11 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/Include/xrRender/UIShader.h b/src/Include/xrRender/UIShader.h index 4c042aaac37..1a7d670a92e 100644 --- a/src/Include/xrRender/UIShader.h +++ b/src/Include/xrRender/UIShader.h @@ -7,9 +7,9 @@ class IUIShader public: virtual ~IUIShader() { ; } virtual void Copy(IUIShader& _in) = 0; - virtual void create(LPCSTR sh, LPCSTR tex = nullptr) = 0; + virtual void create(LPCSTR sh, LPCSTR tex = nullptr, bool no_cache = false) = 0; virtual bool inited() = 0; - virtual void destroy() = 0; + //virtual void destroy() = 0; }; #endif // UIShader_included diff --git a/src/Layers/xrRender/dxUIRender.cpp b/src/Layers/xrRender/dxUIRender.cpp index d83862d03a5..84f4fcc4ee7 100644 --- a/src/Layers/xrRender/dxUIRender.cpp +++ b/src/Layers/xrRender/dxUIRender.cpp @@ -13,6 +13,10 @@ void dxUIRender::CreateUIGeom() void dxUIRender::DestroyUIGeom() { + for (auto& it : g_UIShadersCache) + it.second.destroy(); + g_UIShadersCache.clear(); + hGeom_TL = NULL; hGeom_LIT = NULL; } diff --git a/src/Layers/xrRender/dxUIRender.h b/src/Layers/xrRender/dxUIRender.h index b5b43c8e32e..2b9df885322 100644 --- a/src/Layers/xrRender/dxUIRender.h +++ b/src/Layers/xrRender/dxUIRender.h @@ -13,21 +13,9 @@ class dxUIRender : public IUIRender virtual void SetShader(IUIShader& shader); virtual void SetAlphaRef(int aref); - //. virtual void StartTriList(u32 iMaxVerts); - //. virtual void FlushTriList(); - //. virtual void StartTriFan(u32 iMaxVerts); - //. virtual void FlushTriFan(); - // virtual void StartTriStrip(u32 iMaxVerts); - // virtual void FlushTriStrip(); - //. virtual void StartLineStrip(u32 iMaxVerts); - //. virtual void FlushLineStrip(); - //. virtual void StartLineList(u32 iMaxVerts); - //. virtual void FlushLineList(); virtual void SetScissor(Irect* rect = nullptr); virtual void GetActiveTextureResolution(Fvector2& res); - //. virtual void PushPoint(float x, float y, u32 c, float u, float v); - // virtual void PushPoint(int x, int y, u32 c, float u, float v); virtual void PushPoint(float x, float y, float z, u32 C, float u, float v); virtual void StartPrimitive(u32 iMaxVerts, ePrimitiveType primType, ePointType pointType); diff --git a/src/Layers/xrRender/dxUIShader.cpp b/src/Layers/xrRender/dxUIShader.cpp index 4351219a5b2..dc4f1d5d2c1 100644 --- a/src/Layers/xrRender/dxUIShader.cpp +++ b/src/Layers/xrRender/dxUIShader.cpp @@ -1,6 +1,26 @@ #include "stdafx.h" #include "dxUIShader.h" +xr_unordered_map g_UIShadersCache; + +static ref_shader& GetCachedUIShader(const char* sh, const char* tex) +{ + std::string key{ tex ? tex : "" }; + key += "_"; + key += sh; + + if (const auto it = g_UIShadersCache.find(key); it != g_UIShadersCache.end()) + { + return it->second; + } + else + { + auto& shader = g_UIShadersCache[key]; + shader.create(sh, tex); + return shader; + } +} + void dxUIShader::Copy(IUIShader& _in) { *this = *((dxUIShader*)&_in); } -void dxUIShader::create(LPCSTR sh, LPCSTR tex) { hShader.create(sh, tex); } -void dxUIShader::destroy() { hShader.destroy(); } +void dxUIShader::create(LPCSTR sh, LPCSTR tex, bool no_cache) { hShader = GetCachedUIShader(sh, tex); } +//void dxUIShader::destroy() { hShader.destroy(); } diff --git a/src/Layers/xrRender/dxUIShader.h b/src/Layers/xrRender/dxUIShader.h index b2da07cfaa4..ab1f3069808 100644 --- a/src/Layers/xrRender/dxUIShader.h +++ b/src/Layers/xrRender/dxUIShader.h @@ -4,6 +4,8 @@ #include "Include/xrRender/UIShader.h" +extern xr_unordered_map g_UIShadersCache; + class dxUIShader : public IUIShader { friend class dxUIRender; @@ -14,9 +16,9 @@ class dxUIShader : public IUIShader public: virtual ~dxUIShader() { ; } virtual void Copy(IUIShader& _in); - virtual void create(LPCSTR sh, LPCSTR tex = nullptr); + virtual void create(LPCSTR sh, LPCSTR tex = nullptr, bool no_cache = false); virtual bool inited() { return hShader; } - virtual void destroy(); + //virtual void destroy(); private: ref_shader hShader; diff --git a/src/xrGame/ui/UICellItem.cpp b/src/xrGame/ui/UICellItem.cpp index 1e2997c3bf3..9e5f1d8f723 100644 --- a/src/xrGame/ui/UICellItem.cpp +++ b/src/xrGame/ui/UICellItem.cpp @@ -48,9 +48,13 @@ CUICellItem::~CUICellItem() void CUICellItem::init() { - CUIXml uiXml; - if (!uiXml.Load(CONFIG_PATH, UI_PATH, UI_PATH_DEFAULT, "actor_menu_item.xml", false)) - return; + static CUIXml uiXml; + static bool isXmlReady = false; + if (!isXmlReady) + { + uiXml.Load(CONFIG_PATH, UI_PATH, UI_PATH_DEFAULT, "actor_menu_item.xml", false); + isXmlReady = true; + } m_text = xr_new("Text"); m_text->SetAutoDelete(true); diff --git a/src/xrGame/ui/UILoadingScreen.cpp b/src/xrGame/ui/UILoadingScreen.cpp index 6caea14ed93..cd8443e9ace 100644 --- a/src/xrGame/ui/UILoadingScreen.cpp +++ b/src/xrGame/ui/UILoadingScreen.cpp @@ -135,7 +135,7 @@ void UILoadingScreen::Show(bool show) CUIWindow::Show(show); if (!show) { - loadingLogo->GetStaticItem()->GetShader()->destroy(); + // loadingLogo->GetStaticItem()->GetShader()->destroy(); if (loadingStage) loadingStage->SetText(nullptr); SetStageTip(nullptr, nullptr, nullptr); diff --git a/src/xrGame/ui/UIStatsIcon.cpp b/src/xrGame/ui/UIStatsIcon.cpp index bb1ce419449..ac201ff2588 100644 --- a/src/xrGame/ui/UIStatsIcon.cpp +++ b/src/xrGame/ui/UIStatsIcon.cpp @@ -57,6 +57,7 @@ void CUIStatsIcon::InitTexInfo() void CUIStatsIcon::FreeTexInfo() { // ranks + /* for (int i = RANK_0; i <= RANK_4; i++) { (*m_tex_info)[i][0].sh->destroy(); @@ -66,6 +67,7 @@ void CUIStatsIcon::FreeTexInfo() (*m_tex_info)[ARTEFACT][1].sh->destroy(); (*m_tex_info)[DEATH][0].sh->destroy(); (*m_tex_info)[DEATH][1].sh->destroy(); + */ xr_delete(m_tex_info); } diff --git a/src/xrUICore/Static/UIStatic.h b/src/xrUICore/Static/UIStatic.h index b5e9817059e..19cb07f3cc7 100644 --- a/src/xrUICore/Static/UIStatic.h +++ b/src/xrUICore/Static/UIStatic.h @@ -109,6 +109,8 @@ class XRUICORE_API CUIStatic : public CUIWindow, public ITextureOwner, public CU pcstr GetDebugType() override { return "CUIStatic"; } void FillDebugInfo() override; + void SetNoShaderCache(bool v) { m_UIStaticItem.SetNoShaderCache(v); } + protected: CUILines* m_pTextControl{}; diff --git a/src/xrUICore/Static/UIStaticItem.cpp b/src/xrUICore/Static/UIStaticItem.cpp index 4554547f489..a0b99c1a8e6 100644 --- a/src/xrUICore/Static/UIStaticItem.cpp +++ b/src/xrUICore/Static/UIStaticItem.cpp @@ -208,7 +208,7 @@ void CUIStaticItem::Render(float angle) void CUIStaticItem::CreateShader(LPCSTR tex, LPCSTR sh) { - hShader->create(sh, tex); + hShader->create(sh, tex, !!uFlags.test(flNoShaderCache)); #ifdef DEBUG dbg_tex_name = tex; diff --git a/src/xrUICore/Static/UIStaticItem.h b/src/xrUICore/Static/UIStaticItem.h index c9bb8a1cd48..9927bc6563e 100644 --- a/src/xrUICore/Static/UIStaticItem.h +++ b/src/xrUICore/Static/UIStaticItem.h @@ -24,6 +24,7 @@ class XRUICORE_API CUIStaticItem flValidTextureRect = (1 << 1), flValidHeadingPivot = (1 << 2), flFixedLTWhileHeading = (1 << 3), + flNoShaderCache = (1 << 4), }; Frect TextureRect; @@ -76,7 +77,9 @@ class XRUICORE_API CUIStaticItem Fvector2 GetHeadingPivot() { return vHeadingPivot; } IC void SetMirrorMode(EUIMirroring m) { eMirrorMode = m; } IC EUIMirroring GetMirrorMode() { return eMirrorMode; } - + void SetNoShaderCache(const bool v) { + uFlags.set(flNoShaderCache, v); + } private: void RenderInternal(const Fvector2& pos); void RenderInternal(float angle);