MaterialSystem: decouple texture streaming code

This commit is contained in:
Kawe Mazidjatari 2025-01-06 00:44:23 +01:00
parent aba1ed26b5
commit aefebd846e
6 changed files with 72 additions and 41 deletions

View File

@ -44,6 +44,7 @@
#include "materialsystem/cmaterialsystem.h"
#ifndef DEDICATED
#include "materialsystem/cmaterialglue.h"
#include "materialsystem/texturestreaming.h"
#include "vgui/vgui_baseui_interface.h"
#include "vgui/vgui_debugpanel.h"
#include "vgui/vgui_fpspanel.h"
@ -561,6 +562,7 @@ void DetourRegister() // Register detour classes to be searched and hooked.
#ifndef DEDICATED
REGISTER(VMaterialGlue);
REGISTER(VShaderGlue);
REGISTER(VTextureStreaming);
// Studio
REGISTER(VStudioRenderContext);

View File

@ -10,6 +10,8 @@ add_sources( SOURCE_GROUP "Private"
"cmaterialsystem.h"
"cshaderglue.cpp"
"cshaderglue.h"
"texturestreaming.cpp"
"texturestreaming.h"
)
add_sources( SOURCE_GROUP "Public"

View File

@ -94,35 +94,6 @@ int CMaterialSystem::Shutdown(CMaterialSystem* thisptr)
}
#ifndef MATERIALSYSTEM_NODX
//---------------------------------------------------------------------------------
// Purpose: loads and processes STBSP files
// (overrides level name if stbsp field has value in prerequisites file)
// Input : *pszLevelName -
//---------------------------------------------------------------------------------
static void StreamDB_Init(const char* const pszLevelName)
{
KeyValues* const pSettingsKV = Mod_GetLevelSettings(pszLevelName);
const char* targetStreamDB = pszLevelName;
if (pSettingsKV)
{
KeyValues* const pStreamKV = pSettingsKV->FindKey("StreamDB");
if (pStreamKV)
targetStreamDB = pStreamKV->GetString();
}
v_StreamDB_Init(targetStreamDB);
// If the requested STBSP file doesn't exist, then enable the GPU driven
// texture streaming system.
const bool gpuDriven = s_streamDataBase->fileHandle == FS_ASYNC_FILE_INVALID;
gpu_driven_tex_stream->SetValue(gpuDriven);
if (!gpuDriven)
Msg(eDLL_T::MS, "StreamDB_Init: Loaded STBSP file '%s.stbsp'\n", targetStreamDB);
}
static ConVar stream_overlay_memory("stream_overlay_memory", "524288", FCVAR_DEVELOPMENTONLY, "Total string memory to allocate for the texture streaming debug overlay.");
/*
@ -240,7 +211,6 @@ void VMaterialSystem::Detour(const bool bAttach) const
DetourSetup(&CMaterialSystem__SwapBuffers, &CMaterialSystem::SwapBuffers, bAttach);
DetourSetup(&CMaterialSystem__FindMaterialEx, &CMaterialSystem::FindMaterialEx, bAttach);
DetourSetup(&v_StreamDB_Init, &StreamDB_Init, bAttach);
DetourSetup(&v_DispatchDrawCall, &DispatchDrawCall, bAttach);
DetourSetup(&v_SpinPresent, &SpinPresent, bAttach);
#endif // !MATERIALSYSTEM_NODX

View File

@ -2,9 +2,6 @@
#define MATERIALSYSTEM_H
#include "cmaterialglue.h"
#include "public/imaterialsystem.h"
#ifndef MATERIALSYSTEM_NODX
#include "public/rtech/istreamdb.h"
#endif // !MATERIALSYSTEM_NODX
class CMaterialSystem
{
@ -82,15 +79,12 @@ inline void(*CMaterialSystem__GetStreamOverlay)(const char* mode, char* buf, siz
inline const char*(*CMaterialSystem__DrawStreamOverlay)(void* thisptr, uint8_t* a2, void* unused, void* a4);
#endif // !MATERIALSYSTEM_NODX
inline void(*v_StreamDB_Init)(const char* const pszLevelName);
#ifndef MATERIALSYSTEM_NODX
inline ssize_t* g_nTotalStreamingTextureMemory = nullptr;
inline ssize_t* g_nUnfreeStreamingTextureMemory = nullptr;
inline ssize_t* g_nUnusableStreamingTextureMemory = nullptr;
inline void** s_pRenderContext; // NOTE: This is some CMaterial instance or array.
inline StreamDB_s* s_streamDataBase;
#endif // !MATERIALSYSTEM_NODX
// TODO: move to materialsystem_global.h!
@ -119,14 +113,12 @@ class VMaterialSystem : public IDetour
LogFunAdr("DispatchDrawCall", v_DispatchDrawCall);
LogFunAdr("SpinPresent", v_SpinPresent);
#endif // !MATERIALSYSTEM_NODX
LogFunAdr("StreamDB_Init", v_StreamDB_Init);
#ifndef MATERIALSYSTEM_NODX
LogVarAdr("g_nTotalStreamingTextureMemory", g_nTotalStreamingTextureMemory);
LogVarAdr("g_nUnfreeStreamingTextureMemory", g_nUnfreeStreamingTextureMemory);
LogVarAdr("g_nUnusableStreamingTextureMemory", g_nUnusableStreamingTextureMemory);
LogVarAdr("s_pRenderContext", s_pRenderContext);
LogVarAdr("s_streamDataBase", s_streamDataBase);
LogVarAdr("g_MaterialAdapterMgr", g_pMaterialAdapterMgr);
#endif // !MATERIALSYSTEM_NODX
LogVarAdr("g_pMaterialSystem", g_pMaterialSystem);
@ -149,8 +141,6 @@ class VMaterialSystem : public IDetour
g_GameDll.FindPatternSIMD("44 89 4C 24 ?? 44 89 44 24 ?? 48 89 4C 24 ?? 55 53 56").GetPtr(v_DispatchDrawCall);
g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 74 24 ?? 57 48 81 EC ?? ?? ?? ?? 8B 15 ?? ?? ?? ??").GetPtr(v_SpinPresent);
#endif // !MATERIALSYSTEM_NODX
g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 41 54 41 56 41 57 48 83 EC 40 48 8B E9").GetPtr(v_StreamDB_Init);
}
virtual void GetVar(void) const
{
@ -160,7 +150,6 @@ class VMaterialSystem : public IDetour
CMemory(CMaterialSystem__DrawStreamOverlay).Offset(0x50).FindPatternSelf("48 8B 05", CMemory::Direction::DOWN).ResolveRelativeAddressSelf(0x3, 0x7).GetPtr(g_nUnusableStreamingTextureMemory);
CMemory(v_DispatchDrawCall).FindPattern("48 8B ?? ?? ?? ?? 01").ResolveRelativeAddressSelf(0x3, 0x7).GetPtr(s_pRenderContext);
CMemory(v_StreamDB_Init).FindPattern("C6 05").ResolveRelativeAddressSelf(0x2, 0x7).GetPtr(s_streamDataBase);
CMemory(CMaterialSystem__Disconnect).FindPattern("48 8D").ResolveRelativeAddressSelf(0x3, 0x7).GetPtr(g_pMaterialAdapterMgr);
#endif // !MATERIALSYSTEM_NODX
g_pMaterialSystem = g_GameDll.FindPatternSIMD("8B 41 28 85 C0 7F 18").FindPatternSelf("48 8D 0D").ResolveRelativeAddressSelf(3, 7).RCast<CMaterialSystem*>();

View File

@ -0,0 +1,37 @@
#include "tier1/keyvalues.h"
#include "engine/cmodel_bsp.h"
#include "materialsystem/texturestreaming.h"
//---------------------------------------------------------------------------------
// Purpose: loads and processes STBSP files
// (overrides level name if stbsp field has value in prerequisites file)
// Input : *pszLevelName -
//---------------------------------------------------------------------------------
static void StreamDB_Init(const char* const pszLevelName)
{
KeyValues* const pSettingsKV = Mod_GetLevelSettings(pszLevelName);
const char* targetStreamDB = pszLevelName;
if (pSettingsKV)
{
KeyValues* const pStreamKV = pSettingsKV->FindKey("StreamDB");
if (pStreamKV)
targetStreamDB = pStreamKV->GetString();
}
v_StreamDB_Init(targetStreamDB);
// If the requested STBSP file doesn't exist, then enable the GPU driven
// texture streaming system.
const bool gpuDriven = s_streamDataBase->fileHandle == FS_ASYNC_FILE_INVALID;
gpu_driven_tex_stream->SetValue(gpuDriven);
if (!gpuDriven)
Msg(eDLL_T::MS, "StreamDB_Init: Loaded STBSP file '%s.stbsp'\n", targetStreamDB);
}
void VTextureStreaming::Detour(const bool bAttach) const
{
DetourSetup(&v_StreamDB_Init, &StreamDB_Init, bAttach);
}

View File

@ -0,0 +1,31 @@
#ifndef TEXTURESTREAMING_H
#define TEXTURESTREAMING_H
#include "public/rtech/istreamdb.h"
inline void(*v_StreamDB_Init)(const char* const pszLevelName);
inline StreamDB_s* s_streamDataBase;
///////////////////////////////////////////////////////////////////////////////
class VTextureStreaming : public IDetour
{
virtual void GetAdr(void) const
{
LogFunAdr("StreamDB_Init", v_StreamDB_Init);
LogVarAdr("s_streamDataBase", s_streamDataBase);
}
virtual void GetFun(void) const
{
g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 41 54 41 56 41 57 48 83 EC 40 48 8B E9").GetPtr(v_StreamDB_Init);
}
virtual void GetVar(void) const
{
CMemory(v_StreamDB_Init).FindPattern("C6 05").ResolveRelativeAddressSelf(0x2, 0x7).GetPtr(s_streamDataBase);
}
virtual void GetCon(void) const
{ }
virtual void Detour(const bool bAttach) const;
};
///////////////////////////////////////////////////////////////////////////////
#endif // TEXTURESTREAMING_H