Skip to content

Commit

Permalink
Workaround for Linux shaders breakage (NeotokyoRebuild#587)
Browse files Browse the repository at this point in the history
Fix NeotokyoRebuild#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:
* NeotokyoRebuild#587
* NeotokyoRebuild#438 (comment)
  • Loading branch information
Rainyan authored Sep 17, 2024
1 parent b2291c5 commit e218a48
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mp/src/game/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions mp/src/game/client/cdll_client_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!!!
Expand Down Expand Up @@ -1045,6 +1050,10 @@ int CHLClient::Init( CreateInterfaceFn appSystemFactory, CreateInterfaceFn physi
{
return false;
}

#ifdef LINUX
FixupGlShaders(filesystem, g_pCVar);
#endif
#endif

modemanager->Init( );
Expand Down
56 changes: 56 additions & 0 deletions mp/src/game/client/neo/neo_fixup_glshaders.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "neo_fixup_glshaders.h"
#ifdef LINUX

// Engine
#include "filesystem.h"
#include "icvar.h"

// Stdlib
#include <source_location>

// 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
14 changes: 14 additions & 0 deletions mp/src/game/client/neo/neo_fixup_glshaders.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e218a48

Please sign in to comment.