From e218a48c214df18d92476783ca281235360f3f65 Mon Sep 17 00:00:00 2001 From: Rain Date: Tue, 17 Sep 2024 13:15:02 +0300 Subject: [PATCH] Workaround for Linux shaders breakage (#587) Fix #438 This is a hack around the vision mode shaders sporadically breaking on Linux builds. It seems that for whatever reason, the shader state cached at the `glshaders.cfg` file somehow leads to the corruption upon successive runs of the game. This patch manually sets the `mat_autosave_glshaders cvar` to 0 at client initialization, such that the game won't generate the problematic file upon exit. It also deletes the `glshaders.cfg` file if it already existed (in which case it's too late to fix the problem for that launch of the game, but future launches should work correctly due to the cvar switch, and the file now being gone). This can be the case for users who didn't do a clean reinstall of the game, and have the file stored from a previous launch of the game (or who for whatever reason had an autoexec overriding the `mat_autosave_glshaders` behaviour, which would take precedence over the runtime cvar change). Ideally, we should figure out what's really going wrong here at the shader system and/or precaching level, but this should suffice for avoiding the bug. For more discussion, see: * https://github.com/NeotokyoRebuild/neo/pull/587 * https://github.com/NeotokyoRebuild/neo/issues/438#issuecomment-2351068151 --- mp/src/game/client/CMakeLists.txt | 2 + mp/src/game/client/cdll_client_int.cpp | 9 +++ .../game/client/neo/neo_fixup_glshaders.cpp | 56 +++++++++++++++++++ mp/src/game/client/neo/neo_fixup_glshaders.h | 14 +++++ 4 files changed, 81 insertions(+) create mode 100644 mp/src/game/client/neo/neo_fixup_glshaders.cpp create mode 100644 mp/src/game/client/neo/neo_fixup_glshaders.h diff --git a/mp/src/game/client/CMakeLists.txt b/mp/src/game/client/CMakeLists.txt index eb537c032..5e1ff45ee 100644 --- a/mp/src/game/client/CMakeLists.txt +++ b/mp/src/game/client/CMakeLists.txt @@ -1482,6 +1482,8 @@ target_sources_grouped( neo/c_neo_player.h neo/c_neo_te_tocflash.cpp neo/c_neo_te_tocflash.h + neo/neo_fixup_glshaders.cpp + neo/neo_fixup_glshaders.h ) target_sources_grouped( diff --git a/mp/src/game/client/cdll_client_int.cpp b/mp/src/game/client/cdll_client_int.cpp index 1f0f3addf..0c130ddf6 100644 --- a/mp/src/game/client/cdll_client_int.cpp +++ b/mp/src/game/client/cdll_client_int.cpp @@ -174,6 +174,11 @@ extern vgui::IInputInternal *g_InputInternal; #include "neo_version.h" #include "neo_mount_original.h" extern bool NeoRootCaptureESC(); + +#ifdef LINUX +#include "neo_fixup_glshaders.h" +#endif + #endif // memdbgon must be the last include file in a .cpp file!!! @@ -1045,6 +1050,10 @@ int CHLClient::Init( CreateInterfaceFn appSystemFactory, CreateInterfaceFn physi { return false; } + +#ifdef LINUX + FixupGlShaders(filesystem, g_pCVar); +#endif #endif modemanager->Init( ); diff --git a/mp/src/game/client/neo/neo_fixup_glshaders.cpp b/mp/src/game/client/neo/neo_fixup_glshaders.cpp new file mode 100644 index 000000000..f932b8de7 --- /dev/null +++ b/mp/src/game/client/neo/neo_fixup_glshaders.cpp @@ -0,0 +1,56 @@ +#include "neo_fixup_glshaders.h" +#ifdef LINUX + +// Engine +#include "filesystem.h" +#include "icvar.h" + +// Stdlib +#include + +// memdbgon must be the last include file in a .cpp file!!! +#include "tier0/memdbgon.h" + +// NEO HACK (Rain): This is a hack around a vision shaders corruption on Linux. +// For details, see: https://github.com/NeotokyoRebuild/neo/pull/587 +void FixupGlShaders(IFileSystem* filesystem, ICvar* cvar) +{ + constexpr auto filename = "glshaders.cfg", + pathID = "MOD", + funcName = std::source_location::current().function_name(); + + // This prevents the game from generating the problematic file on exit. + // There's also a "mat_autoload_glshaders", but it seems by the time we load, + // it's too late for that to take an effect, at least via this route. + // The client can still override this by including a command in their autoexec, + // if they really want to. + constexpr auto cvarname = "mat_autosave_glshaders"; + ConVar* autosaveGlShaders; + if (!(autosaveGlShaders = cvar->FindVar(cvarname))) + { + Warning("%s: cvar %s not found\n", funcName, cvarname); + return; + } + autosaveGlShaders->SetValue(false); + + // If the problematic file doesn't exist, we're done. + if (!filesystem->FileExists(filename, pathID)) + { + return; + } + + // But if it does, we're too late to fix it for this launch... + // However, we can delete the file, which together with the convar set above + // should prevent it from being created again on successive restarts of the game. + + // This should never happen, but just in case the relative mod path lookup somehow fails. + if (filesystem->IsDirectory(filename, pathID)) + { + Warning("%s: Expected to find a file at %s path %s, but it was a dir\n", + funcName, pathID, filename); + return; + } + + filesystem->RemoveFile(filename, pathID); +} +#endif // LINUX diff --git a/mp/src/game/client/neo/neo_fixup_glshaders.h b/mp/src/game/client/neo/neo_fixup_glshaders.h new file mode 100644 index 000000000..fe21a7900 --- /dev/null +++ b/mp/src/game/client/neo/neo_fixup_glshaders.h @@ -0,0 +1,14 @@ +#ifndef NEO_FIXUP_GLSHADERS_H +#define NEO_FIXUP_GLSHADERS_H +#ifdef _WIN32 +#pragma once +#endif +#ifdef LINUX + +class ICvar; +class IFileSystem; + +void FixupGlShaders(IFileSystem* filesystem, ICvar* cvar); + +#endif // LINUX +#endif // NEO_FIXUP_GLSHADERS_H