From 20b9913f8a7ac506256ab5239e4762f631620461 Mon Sep 17 00:00:00 2001 From: Ilya Shurumov Date: Sun, 12 Jun 2022 01:31:47 +0600 Subject: [PATCH] - apply anisotropic filtering - silence some message boxes about non-existent files - fix bug with file lookup --- src/IDirect3DDevice9Hook.cpp | 68 ++++++++++++++++++++++++++++++++++++ src/patches.cpp | 36 ++++++++++++------- 2 files changed, 91 insertions(+), 13 deletions(-) diff --git a/src/IDirect3DDevice9Hook.cpp b/src/IDirect3DDevice9Hook.cpp index 55764b5..0f77128 100644 --- a/src/IDirect3DDevice9Hook.cpp +++ b/src/IDirect3DDevice9Hook.cpp @@ -1,4 +1,5 @@ #include "IDirect3DDevice9Hook.h" +#include /*** IUnknown methods ***/ STDCALL(IDirect3DDevice9Hook::QueryInterface)(THIS_ REFIID riid, void** ppvObj){ @@ -28,6 +29,9 @@ STDCALL(IDirect3DDevice9Hook::EvictManagedResources)(THIS){ } STDCALL(IDirect3DDevice9Hook::GetDirect3D)(THIS_ IDirect3D9** ppD3D9){ + if (!pD3D) { + return DEVICE->GetDirect3D(ppD3D9); + } *ppD3D9 = pD3D; return D3D_OK; } @@ -298,6 +302,70 @@ STDCALL(IDirect3DDevice9Hook::GetSamplerState)(THIS_ DWORD Sampler, D3DSAMPLERST } STDCALL(IDirect3DDevice9Hook::SetSamplerState)(THIS_ DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value){ + const int anisotropicFiltering = 1; // TODO: config... + + static bool anisotropyDetectNeeded = true; + static int nMaxAnisotropy = 16; + + // Disable AntiAliasing when using point filtering + //if (Config.AntiAliasing) + { + if (Type == D3DSAMP_MINFILTER || Type == D3DSAMP_MAGFILTER) + { + if (Value == D3DTEXF_NONE || Value == D3DTEXF_POINT) + { + DEVICE->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE); + } + else + { + DEVICE->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); + } + } + } + + // detect max anisotropy + if (anisotropyDetectNeeded && (Type == D3DSAMP_MAXANISOTROPY || ((Type == D3DSAMP_MINFILTER || Type == D3DSAMP_MAGFILTER) && Value == D3DTEXF_LINEAR))) + { + anisotropyDetectNeeded = false; + + D3DCAPS9 Caps; + ZeroMemory(&Caps, sizeof(D3DCAPS9)); + if (SUCCEEDED(DEVICE->GetDeviceCaps(&Caps))) + { + nMaxAnisotropy = (anisotropicFiltering == 1) ? Caps.MaxAnisotropy : std::min((DWORD)anisotropicFiltering, Caps.MaxAnisotropy); + } + + if (nMaxAnisotropy && SUCCEEDED(DEVICE->SetSamplerState(Sampler, D3DSAMP_MAXANISOTROPY, nMaxAnisotropy))) + { + LogFile::Format("Setting Anisotropy Filtering at %dx\n", nMaxAnisotropy); + } + else + { + nMaxAnisotropy = 0; + LogFile::WriteLine("Failed to enable Anisotropy Filtering!"); + } + } + + // enable anisotropic filtering + if (nMaxAnisotropy) + { + if (Type == D3DSAMP_MAXANISOTROPY) + { + if (SUCCEEDED(DEVICE->SetSamplerState(Sampler, D3DSAMP_MAXANISOTROPY, nMaxAnisotropy))) + { + return D3D_OK; + } + } + else if ((Type == D3DSAMP_MINFILTER || Type == D3DSAMP_MAGFILTER) && Value == D3DTEXF_LINEAR) + { + if (SUCCEEDED(DEVICE->SetSamplerState(Sampler, D3DSAMP_MAXANISOTROPY, nMaxAnisotropy)) && + SUCCEEDED(DEVICE->SetSamplerState(Sampler, Type, D3DTEXF_ANISOTROPIC))) + { + return D3D_OK; + } + } + } + return DEVICE->SetSamplerState(Sampler, Type, Value); } diff --git a/src/patches.cpp b/src/patches.cpp index d78c80e..168ea6a 100644 --- a/src/patches.cpp +++ b/src/patches.cpp @@ -541,16 +541,18 @@ class effectManagerHandler : public effectManager { int InitShaderFX() { + bool loadedFromChunk = false; + if (gameversion == __DRIV3R_V100) { /* Loads shaders from AllShaders.chunk */ CFileChunker shadersChunk = CFileChunker(); - int errorCode = 0; - LogFile::WriteLine("Loading shaders chunk..."); if (shadersChunk.OpenChunks("D3Hook:shaders\\AllShaders.chunk")) { + int errorCode = 0; + int nShaders = shadersChunk.GetChunkCount(); LogFile::Format(" - Count: %d\n", nShaders); @@ -585,18 +587,20 @@ class effectManagerHandler : public effectManager { } shadersChunk.Release(); + + // something went wrong, abort + if (errorCode != 0) + return errorCode; + + loadedFromChunk = true; } else { - MessageBoxW(hamster::GetMainWindow(), L"FATAL ERROR: Cannot open the shaders file!", L"D3Hook", MB_OK | MB_ICONERROR); - return E_FAIL; + LogFile::WriteLine("ERROR: Cannot open the shaders file located at 'D3Hook:shaders\\AllShaders.chunk'"); } - - // something went wrong, abort - if (errorCode != 0) - return errorCode; } - else + + if(!loadedFromChunk) { for (auto shader : shader_programs) { /* Loads shader from EXE */ @@ -1343,7 +1347,9 @@ class rendererHandler : public renderer { }); // quick and dirty way to try fixing mipmaps - manager->globalstate.addSamplerState({ D3DSAMP_MIPMAPLODBIAS, -1.0f }); + manager->globalstate.addSamplerState({ D3DSAMP_MIPMAPLODBIAS, -1.25f }); + manager->globalstate.addSamplerState({ D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC }); + manager->globalstate.addSamplerState({ D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC }); } void registerStateParams(void) { @@ -1743,6 +1749,11 @@ class CFileSystemPC { if (!_strnicmp(vPath, "D3Hook:", 7)) { std::sprintf(buffer, "%s%s", toolBase.c_str(), &vPath[7]); } + else if (!_strnicmp(vPath, "LANG:TERR:", 10)) { // totally strange case but we have to handle it + std::sprintf(buffer, "%sTERRITORY\\%s\\LOCALE\\%s\\%s", gamePath, + config->GetTerritoryString(), + config->GetTextLanguageString(), &vPath[10]); + } else if (!_strnicmp(vPath, "TERR:", 5)) { std::sprintf(buffer, "%sTERRITORY\\%s\\%s", gamePath, config->GetTerritoryString(), &vPath[5]); @@ -1778,9 +1789,8 @@ class CFileSystemPC { } if (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES) { - char pathBuf[512]{}; - sprintf(pathBuf, "Game requested non existent file at %s", buffer); - MessageBoxA(nullptr, pathBuf, "D3Hook", MB_ICONWARNING | MB_OK); + LogFile::Format("WARNING: Game requested non existent file at %s", buffer); + LogFile::AppendLine(); } return _strupr(buffer);