ImguiSystem: fix regression causing initial logs to not render

Since directx initialized a lot later then when we first start logging, and since we no longer lock and log when the system isn't initialized as per commit 0dde788aa737d2bdb85a8493b4f8f1e549c44ecb, initial logs will not be stored for rendering.

The imgui console will now instead check if the system has been explicitly disabled, or has been disabled due to an error. If an error occurs, the buffer will also be cleared. This way we still avoid locking and logging anything at all times when -noimgui was passed in.
This commit is contained in:
Kawe Mazidjatari 2024-11-17 03:15:31 +01:00
parent f5f14c153f
commit d444900b9a
5 changed files with 33 additions and 2 deletions

View File

@ -18,6 +18,7 @@
#include "mathlib/mathlib.h"
#include "launcher/launcher.h"
#include "protobuf/stubs/common.h"
#include "gameui/imgui_system.h"
#ifndef DEDICATED
#define SDK_DEFAULT_CFG "cfg/system/startup_default.cfg"
@ -124,6 +125,8 @@ void SDK_Init()
s_bConsoleInitialized = Console_Init(bAnsiColor);
}
ImguiSystem()->SetEnabled(!CommandLine()->CheckParm("-noimgui"));
SpdLog_Init(bAnsiColor);
Show_Emblem();

View File

@ -1083,7 +1083,7 @@ int CConsole::TextEditCallbackStub(ImGuiInputTextCallbackData* iData)
//-----------------------------------------------------------------------------
void CConsole::AddLog(const char* const text, const ImU32 color)
{
if (!ImguiSystem()->IsInitialized())
if (!ImguiSystem()->IsEnabled())
return;
AUTO_LOCK(m_colorTextLoggerMutex);

View File

@ -16,6 +16,7 @@
CImguiSystem::CImguiSystem()
{
m_initialized = false;
m_enabled = true;
m_hasNewFrame = false;
}
@ -28,12 +29,17 @@ bool CImguiSystem::Init()
Assert(ThreadInMainThread(), "CImguiSystem::Init() should only be called from the main thread!");
Assert(!IsInitialized(), "CImguiSystem::Init() called recursively?");
Assert(IsEnabled(), "CImguiSystem::Init() called while system was disabled!");
///////////////////////////////////////////////////////////////////////////
IMGUI_CHECKVERSION();
ImGuiContext* const context = ImGui::CreateContext();
if (!context)
{
m_enabled = false;
return false;
}
AUTO_LOCK(m_snapshotBufferMutex);
AUTO_LOCK(m_inputEventQueueMutex);
@ -53,6 +59,8 @@ bool CImguiSystem::Init()
!ImGui_ImplDX11_Init(D3D11Device(), D3D11DeviceContext()))
{
Assert(0);
m_enabled = false;
return false;
}
@ -70,6 +78,8 @@ void CImguiSystem::Shutdown()
Assert(ThreadInMainThread(), "CImguiSystem::Shutdown() should only be called from the main thread!");
Assert(IsInitialized(), "CImguiSystem::Shutdown() called recursively?");
Assert(IsEnabled(), "CImguiSystem::Shutdown() called while system was disabled!");
AUTO_LOCK(m_snapshotBufferMutex);
AUTO_LOCK(m_inputEventQueueMutex);

View File

@ -28,8 +28,14 @@ public:
static LRESULT MessageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
// inlines:
inline bool IsEnabled() const { return m_enabled; };
inline bool IsInitialized() const { return m_initialized; };
// when explicitly disabled, surfaces such as the console could query
// whether to run code that isn't directly tied to rendering, i.e. to
// check if we should store logs for rendering.
inline void SetEnabled(const bool enable) { Assert(!m_initialized); m_enabled = enable; }
private:
ImDrawDataSnapshot m_snapshotData;
CUtlVector<CImguiSurface*> m_surfaceList;
@ -45,7 +51,9 @@ private:
// lock to control access as main calls SampleFrame().
mutable CThreadMutex m_inputEventQueueMutex;
bool m_enabled;
bool m_initialized;
std::atomic_bool m_hasNewFrame;
};

View File

@ -351,7 +351,7 @@ void DirectX_Init()
Error(eDLL_T::COMMON, 0xBAD0C0DE, "Failed to detour process: error code = %08x\n", hr);
}
if (!CommandLine()->CheckParm("-noimgui"))
if (ImguiSystem()->IsEnabled())
{
if (ImguiSystem()->Init())
{
@ -359,7 +359,17 @@ void DirectX_Init()
ImguiSystem()->AddSurface(&g_Browser);
}
else
{
Error(eDLL_T::COMMON, 0, "ImguiSystem()->Init() failed!\n");
// Remove any log that was stored in the buffer for rendering
// as the console will not render past this stage due to init
// failure. Logging happens before the imgui surface system
// is initialized, as the initialization needs to happen after
// directx is initialized, but on initialization success, we
// do want the logs prior to this stage to be displayed.
g_Console.ClearLog();
}
}
}