Skip to content

Commit

Permalink
Merge pull request #27 from NarutoUA/reshade
Browse files Browse the repository at this point in the history
reshade + first person fix
  • Loading branch information
NarutoUA authored Jul 10, 2021
2 parents 4e3bc00 + 4aa8849 commit 95c524e
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 69 deletions.
2 changes: 1 addition & 1 deletion fsr_common
70 changes: 27 additions & 43 deletions gta5_fsr/CSuperResolutionMgr.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
#include "stdafx.h"
#include <numeric>

#include "CSuperResolutionMgr.h"

#include "shaders/fsr_easu_cs_d3d.h"
#include "shaders/fsr_rcas_cs_d3d.h"

constexpr auto GTA5_FSR_CFG_FILENAME = "gta5_fsr.ini";
constexpr auto GTA5_FSR_CFG_USE_FSR = "UseFidelityFxSuperResolution";
constexpr auto GTA5_FSR_CFG_AUTO_SHARPNESS = "AutoSharpness";
constexpr auto GTA5_FSR_CFG_SHARPNESS = "Sharpness";
constexpr auto GTA5_FSR_CFG_KEY_TOGGLE_FSR = "KeyToggleFSR";
constexpr auto GTA5_FSR_CFG_KEY_UPDATE_SHARPNESS = "KeyUpdateSharpness";
constexpr auto GTA5_FSR_CFG_KEY_PRINT_DEBUG = "PrintDebug";
constexpr auto GTA5_FSR_EPSILON = 0.01;
extern gta5_fsr_cfg_t g_Cfg;

CSuperResolutionMgr& CSuperResolutionMgr::instance()
{
Expand Down Expand Up @@ -49,7 +42,6 @@ void CSuperResolutionMgr::InternalInitialize(ID3D11Device* pDevice, ID3D11Device

ZeroMemory(&m_cbEASU, sizeof(m_cbEASU));

ReadConfig();
CreateResources();

print_debug("GTA5_FSR: Initialized");
Expand All @@ -59,7 +51,7 @@ bool CSuperResolutionMgr::InternalRunSuperResolutionPass(wil::com_ptr_t<ID3D11Sh
{
HandleInputKeys();

if (m_bUseSuperResolution == false || m_bResourcesAvailable == false || src == nullptr || dst == nullptr)
if (g_Cfg.UseFidelityFxSuperResolution == false || m_bResourcesAvailable == false || src == nullptr || dst == nullptr)
return false;

// Check if this is original upscale pass
Expand Down Expand Up @@ -95,8 +87,11 @@ bool CSuperResolutionMgr::InternalRunSuperResolutionPass(wil::com_ptr_t<ID3D11Sh
auto dst_height = static_cast<float>(dst_tex_desc.Height);
auto dst_ratio = dst_width / dst_height;

auto frame_scale = src_width / dst_width;

if (src_tex_desc.Width >= dst_tex_desc.Width || src_tex_desc.Height >= dst_tex_desc.Height
|| std::abs(src_ratio - dst_ratio) > GTA5_FSR_EPSILON)
|| std::abs(src_ratio - dst_ratio) > GTA5_FSR_EPSILON
|| frame_scale < (GTA5_MIN_FRAME_SCALE - GTA5_FSR_EPSILON))
return false;

DetectMSAA();
Expand Down Expand Up @@ -160,31 +155,6 @@ void CSuperResolutionMgr::InternalOnDeviceLost()
ReleaseResources();
}

void CSuperResolutionMgr::ReadConfig()
{
CSimpleIniA ini;
if (ini.LoadFile(GTA5_FSR_CFG_FILENAME) == SI_OK)
{
print_debug("GTA5_FSR: Reading config file...");

ini_read_bool(ini, "GLOBAL", GTA5_FSR_CFG_USE_FSR, m_bUseSuperResolution);
ini_read_bool(ini, "GLOBAL", GTA5_FSR_CFG_AUTO_SHARPNESS, m_bAutoSharpness);

float sharpness;
if (ini_read_numeric(ini, "GLOBAL", GTA5_FSR_CFG_SHARPNESS, sharpness) && sharpness >= 0.f && sharpness <= 1.f)
m_fSharpness = sharpness;

int code;
if (ini_read_numeric(ini, "INPUT", GTA5_FSR_CFG_KEY_TOGGLE_FSR, code))
m_keyToggleFSR = code;

if (ini_read_numeric(ini, "INPUT", GTA5_FSR_CFG_KEY_UPDATE_SHARPNESS, code))
m_keyUpdateSharpness = code;

ini_read_bool(ini, "DEBUG", GTA5_FSR_CFG_KEY_PRINT_DEBUG, g_bPrintDebug);
}
}

void CSuperResolutionMgr::ReleaseResources()
{
m_bResourcesAvailable = false;
Expand Down Expand Up @@ -254,6 +224,10 @@ bool CSuperResolutionMgr::UpdateResources(const D3D11_TEXTURE2D_DESC& src_desc,
|| m_pBufEASU == nullptr || m_pTexEASU == nullptr || m_pTexRCAS == nullptr
|| m_pUavEASU == nullptr || m_pUavRCAS == nullptr || m_pSrvRCAS == nullptr)
{
print_debug("GTA5_FSR: Updating resources... Src: [%dx%d]->[%dx%d], Dst: [%dx%d]->[%dx%d]"
, static_cast<int>(m_cbEASU.cTextureSize[0]), static_cast<int>(m_cbEASU.cTextureSize[1]), src_desc.Width, src_desc.Height
, static_cast<int>(m_cbEASU.cViewportSize[0]), static_cast<int>(m_cbEASU.cViewportSize[1]), dst_desc.Width, dst_desc.Height);

m_cbEASU.cTextureSize = { src_width, src_height, 1.f, 1.f };
m_cbEASU.cViewportSize = { dst_width, dst_height, 0.f, 0.f };

Expand Down Expand Up @@ -333,7 +307,7 @@ bool CSuperResolutionMgr::UpdateResources(const D3D11_TEXTURE2D_DESC& src_desc,

UpdateAutoSharpness(src_width / dst_width);

print_debug("GTA5_FSR: Updated some resources.");
print_debug("GTA5_FSR: Updated resources.");
};

return true;
Expand All @@ -352,12 +326,12 @@ std::pair<UINT, UINT> CSuperResolutionMgr::GetThreadGroupsXY(UINT width, UINT he

void CSuperResolutionMgr::HandleInputKeys()
{
if (m_keyToggleFSR.has_value() && (GetAsyncKeyState(m_keyToggleFSR.value()) & 0x1))
if (g_Cfg.KeyToggleFSR.has_value() && (GetAsyncKeyState(g_Cfg.KeyToggleFSR.value()) & 0x1))
{
m_bUseSuperResolution ^= true;
m_bUseSuperResolution ? print_debug("GTA5_FSR: ON") : print_debug("GTA5_FSR: OFF");
g_Cfg.UseFidelityFxSuperResolution ^= true;
g_Cfg.UseFidelityFxSuperResolution ? print_debug("GTA5_FSR: ON") : print_debug("GTA5_FSR: OFF");
}
else if (m_keyUpdateSharpness.has_value() && (GetAsyncKeyState(m_keyUpdateSharpness.value()) & 0x1))
else if (g_Cfg.KeyUpdateSharpness.has_value() && (GetAsyncKeyState(g_Cfg.KeyUpdateSharpness.value()) & 0x1))
{
CSimpleIniA ini;
if (ini.LoadFile(GTA5_FSR_CFG_FILENAME) == SI_OK)
Expand Down Expand Up @@ -391,7 +365,7 @@ void CSuperResolutionMgr::UpdateSharpness()

void CSuperResolutionMgr::UpdateAutoSharpness(float ratio)
{
if (m_bAutoSharpness)
if (g_Cfg.AutoSharpness)
{
// Performance: 0.5x = 0.7
// Balanced: 0.667x = 0.5
Expand All @@ -418,7 +392,17 @@ void CSuperResolutionMgr::DetectMSAA()
auto data = ReadConstantBuffer<gta5_main_globals_cb_t>(m_pDevice, m_pDeviceContext, pBuf.get());
if (data)
{
m_MSAA = data->msaa;
switch (data->msaa)
{
case 0:
case 2:
case 4:
case 8:
m_MSAA = data->msaa;
break;
default:
m_MSAA = 0;
}
}

m_bDetectMSAA = false;
Expand Down
7 changes: 1 addition & 6 deletions gta5_fsr/CSuperResolutionMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "shaders/defines.h"

constexpr auto GTA5_FSR_DEFAULT_SHARPNESS = 0.5f;
inline constexpr auto GTA5_FSR_DEFAULT_SHARPNESS = 0.5f;

class CSuperResolutionMgr
{
Expand All @@ -26,7 +26,6 @@ class CSuperResolutionMgr
bool InternalRunSuperResolutionPass(wil::com_ptr_t<ID3D11ShaderResourceView>&& src, wil::com_ptr_t<ID3D11RenderTargetView>&& dst, bool fxaa);
void InternalOnDeviceLost();

void ReadConfig();
void ReleaseResources();
void CreateResources();
bool UpdateResources(const D3D11_TEXTURE2D_DESC& src_desc, const D3D11_TEXTURE2D_DESC& dst_desc);
Expand All @@ -38,11 +37,7 @@ class CSuperResolutionMgr
std::pair<UINT, UINT> GetThreadGroupsXY(UINT width, UINT height);

private:
bool m_bUseSuperResolution = true;
bool m_bAutoSharpness = true;
float m_fSharpness = GTA5_FSR_DEFAULT_SHARPNESS;
std::optional<int> m_keyToggleFSR;
std::optional<int> m_keyUpdateSharpness;

bool m_bResourcesAvailable = false;
ID3D11Device* m_pDevice = nullptr;
Expand Down
43 changes: 43 additions & 0 deletions gta5_fsr/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <optional>
#include <string>

inline constexpr auto GTA5_FSR_CFG_FILENAME = "gta5_fsr.ini";

// [GLOBAL]
inline constexpr auto GTA5_FSR_CFG_USE_FSR = "UseFidelityFxSuperResolution";
inline constexpr auto GTA5_FSR_CFG_AUTO_SHARPNESS = "AutoSharpness";
inline constexpr auto GTA5_FSR_CFG_SHARPNESS = "Sharpness";

// [INPUT]
inline constexpr auto GTA5_FSR_CFG_KEY_TOGGLE_FSR = "KeyToggleFSR";
inline constexpr auto GTA5_FSR_CFG_KEY_UPDATE_SHARPNESS = "KeyUpdateSharpness";

// [DEBUG]
inline constexpr auto GTA5_FSR_CFG_KEY_PRINT_DEBUG = "PrintDebug";

// [PROXY]
inline constexpr auto GTA5_FSR_CFG_KEY_ENABLE_PROXY = "EnableProxyLibrary";
inline constexpr auto GTA5_FSR_CFG_KEY_INIT_PROXY = "InitProxyFunctions";
inline constexpr auto GTA5_FSR_CFG_KEY_PROXY_LIB = "ProxyLibrary";

struct gta5_fsr_cfg_t
{
// [GLOBAL]
bool UseFidelityFxSuperResolution = true;
bool AutoSharpness = true;
float Sharpness = 0.5f;

// [INPUT]
std::optional<int> KeyToggleFSR;
std::optional<int> KeyUpdateSharpness;

// [DEBUG]
// bool PrintDebug = true; // should be global var because of common code

// [PROXY]
bool EnableProxyLibrary = false;
bool InitProxyFunctions = false;
std::string ProxyLibrary;
};
2 changes: 2 additions & 0 deletions gta5_fsr/gta5_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ inline constexpr auto GTA5_CB_SLOT_MAIN_GLOBALS = 2u;
inline constexpr auto GTA5_CB_SLOT_POSTFX = 5u;
inline constexpr auto GTA5_CB_SLOT_POSTFX_FXAA = 6u;

inline constexpr auto GTA5_MIN_FRAME_SCALE = 0.5f;

struct gta5_main_globals_cb_t
{
std::array<float, 64> _pad;
Expand Down
5 changes: 5 additions & 0 deletions gta5_fsr/gta5_fsr.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ Sharpness=0.5
[DEBUG]
; If true, prints debug messages to debug output. Keep it disabled for better performance
PrintDebug=false

[PROXY]
EnableProxyLibrary=false
InitProxyFunctions=false
ProxyLibrary=
5 changes: 3 additions & 2 deletions gta5_fsr/gta5_fsr.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -103,6 +103,7 @@
<ClInclude Include="..\fsr_common\shaders\defines.h" />
<ClInclude Include="..\fsr_common\shaders\fsr_easu_cs_d3d.h" />
<ClInclude Include="..\fsr_common\shaders\fsr_rcas_cs_d3d.h" />
<ClInclude Include="config.h" />
<ClInclude Include="CSuperResolutionMgr.h" />
<ClInclude Include="gta5_defines.h" />
<ClInclude Include="stdafx.h" />
Expand Down
1 change: 1 addition & 0 deletions gta5_fsr/gta5_fsr.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<ClInclude Include="..\fsr_common\helpers\ini_helper.h">
<Filter>helpers</Filter>
</ClInclude>
<ClInclude Include="config.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
Expand Down
Loading

0 comments on commit 95c524e

Please sign in to comment.