From 8251ec205a0397dbf5562f9e983243a4e1bb712e Mon Sep 17 00:00:00 2001 From: cdozdila Date: Sun, 4 May 2025 23:17:35 +0300 Subject: [PATCH] Added CBV tracking option --- OptiScaler/Config.cpp | 3 + OptiScaler/Config.h | 4 +- OptiScaler/menu/menu_common.cpp | 12 ++++ .../resource_tracking/ResTrack_dx12.cpp | 66 ++++++++++--------- OptiScaler/resource_tracking/ResTrack_dx12.h | 2 + 5 files changed, 54 insertions(+), 33 deletions(-) diff --git a/OptiScaler/Config.cpp b/OptiScaler/Config.cpp index 19eb7110..5322df28 100644 --- a/OptiScaler/Config.cpp +++ b/OptiScaler/Config.cpp @@ -96,6 +96,7 @@ bool Config::Reload(std::filesystem::path iniPath) FGHudfixHalfSync.set_from_config(readBool("OptiFG", "HudfixHalfSync")); FGHudfixFullSync.set_from_config(readBool("OptiFG", "HudfixFullSync")); FGHudfixTrackRelease.set_from_config(readBool("OptiFG", "HudfixTrackRelease")); + FGHudfixCBVTrackMode.set_from_config(readInt("OptiFG", "HudfixCBVTrackMode")); } // Framerate @@ -609,6 +610,8 @@ bool Config::SaveIni() ini.SetValue("OptiFG", "HudfixHalfSync", GetBoolValue(Instance()->FGHudfixHalfSync.value_for_config()).c_str()); ini.SetValue("OptiFG", "HudfixFullSync", GetBoolValue(Instance()->FGHudfixFullSync.value_for_config()).c_str()); ini.SetValue("OptiFG", "HudfixTrackRelease", GetBoolValue(Instance()->FGHudfixTrackRelease.value_for_config()).c_str()); + + ini.SetValue("OptiFG", "HudfixCBVTrackMode", GetIntValue(Instance()->FGHudfixCBVTrackMode.value_for_config()).c_str()); } // Framerate diff --git a/OptiScaler/Config.h b/OptiScaler/Config.h index 0ca210ea..f036880a 100644 --- a/OptiScaler/Config.h +++ b/OptiScaler/Config.h @@ -341,12 +341,14 @@ public: CustomOptional FGFramePacingTuning{ true }; CustomOptional FGFPTSafetyMarginInMs{ 0.01f }; CustomOptional FGFPTVarianceFactor{ 0.3f }; - CustomOptional FGFPTAllowHybridSpin{ true }; + CustomOptional FGFPTAllowHybridSpin{ false }; CustomOptional FGFPTHybridSpinTime{ 2 }; CustomOptional FGFPTAllowWaitForSingleObjectOnFence{ false }; CustomOptional FGHudfixHalfSync{ false }; CustomOptional FGHudfixFullSync{ false }; CustomOptional FGHudfixTrackRelease{ false }; + CustomOptional FGHudfixCBVTrackMode{ 0 }; // 0 off, 1 after upscaler, 2 full + // DLSS Enabler std::optional DE_FramerateLimit; // off - vsync - number diff --git a/OptiScaler/menu/menu_common.cpp b/OptiScaler/menu/menu_common.cpp index f57bb7c6..46406e53 100644 --- a/OptiScaler/menu/menu_common.cpp +++ b/OptiScaler/menu/menu_common.cpp @@ -1690,6 +1690,18 @@ bool MenuCommon::RenderMenu() } ShowHelpMarker("Always track resources, might cause performace issues\nbut also might fix HudFix related crashes!"); + const char* cbv[] = { "Not Tracking", "Track After Upscaling", "Always Track" }; + if (ImGui::BeginCombo("CBV Tracking", cbv[Config::Instance()->FGHudfixCBVTrackMode.value_or_default()])) + { + for (size_t i = 0; i < 3; i++) + { + if (ImGui::Selectable(cbv[i], (Config::Instance()->FGHudfixCBVTrackMode.value_or_default() == i))) + Config::Instance()->FGHudfixCBVTrackMode = i; + } + + ImGui::EndCombo(); + } + auto tr = Config::Instance()->FGHudfixTrackRelease.value_or_default(); if (ImGui::Checkbox("Track Resource Release", &tr)) { diff --git a/OptiScaler/resource_tracking/ResTrack_dx12.cpp b/OptiScaler/resource_tracking/ResTrack_dx12.cpp index 4b138d19..76b98eb6 100644 --- a/OptiScaler/resource_tracking/ResTrack_dx12.cpp +++ b/OptiScaler/resource_tracking/ResTrack_dx12.cpp @@ -103,12 +103,12 @@ struct HeapCacheTLS unsigned genSeen = 0; }; -static thread_local HeapCacheTLS cache; -static thread_local HeapCacheTLS cacheRTV; -static thread_local HeapCacheTLS cacheCBV; -static thread_local HeapCacheTLS cacheSRV; -static thread_local HeapCacheTLS cacheUAV; -static std::atomic gHeapGeneration{ 1 }; +static thread_local HeapCacheTLS cache; +static thread_local HeapCacheTLS cacheRTV; +static thread_local HeapCacheTLS cacheCBV; +static thread_local HeapCacheTLS cacheSRV; +static thread_local HeapCacheTLS cacheUAV; +static std::atomic gHeapGeneration{ 1 }; #ifdef USE_RESOURCE_DISCARD // created resources @@ -567,16 +567,19 @@ static void hkCreateDepthStencilView(ID3D12Device* This, ID3D12Resource* pResour //} } -static void hkCreateConstantBufferView(ID3D12Device* This, const D3D12_CONSTANT_BUFFER_VIEW_DESC* pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor) +void ResTrack_Dx12::hkCreateConstantBufferView(ID3D12Device* This, const D3D12_CONSTANT_BUFFER_VIEW_DESC* pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor) { o_CreateConstantBufferView(This, pDesc, DestDescriptor); - if (DestDescriptor.ptr != 0) + if (Config::Instance()->FGHudfixCBVTrackMode.value_or_default() == 2 || (Config::Instance()->FGHudfixCBVTrackMode.value_or_default() == 1 && IsHudFixActive())) { - auto heap = GetHeapByCpuHandleCBV(DestDescriptor.ptr); + if (DestDescriptor.ptr != 0) + { + auto heap = GetHeapByCpuHandleCBV(DestDescriptor.ptr); - if (heap != nullptr) - heap->ClearByCpuHandle(DestDescriptor.ptr); + if (heap != nullptr) + heap->ClearByCpuHandle(DestDescriptor.ptr); + } } } @@ -2008,6 +2011,9 @@ static void HookToQueue(ID3D12Device* InDevice) void ResTrack_Dx12::HookDevice(ID3D12Device* device) { + if (Config::Instance()->FGType.value_or_default() != OptiFG || !Config::Instance()->OverlayMenu.value_or_default()) + return; + if (o_CreateDescriptorHeap != nullptr || device == nullptr) return; @@ -2032,7 +2038,7 @@ void ResTrack_Dx12::HookDevice(ID3D12Device* device) o_CopyDescriptorsSimple = (PFN_CopyDescriptorsSimple)pVTable[24]; // Apply the detour - if (o_CreateDescriptorHeap != nullptr || o_CreateRenderTargetView != nullptr) + if (o_CreateDescriptorHeap != nullptr) { DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); @@ -2040,33 +2046,29 @@ void ResTrack_Dx12::HookDevice(ID3D12Device* device) if (o_CreateDescriptorHeap != nullptr) DetourAttach(&(PVOID&)o_CreateDescriptorHeap, hkCreateDescriptorHeap); - if (Config::Instance()->FGType.value_or_default() == OptiFG && Config::Instance()->OverlayMenu.value_or_default()) - { - if (o_CreateConstantBufferView != nullptr) - DetourAttach(&(PVOID&)o_CreateConstantBufferView, hkCreateConstantBufferView); + if (o_CreateConstantBufferView != nullptr) + DetourAttach(&(PVOID&)o_CreateConstantBufferView, hkCreateConstantBufferView); - if (o_CreateRenderTargetView != nullptr) - DetourAttach(&(PVOID&)o_CreateRenderTargetView, hkCreateRenderTargetView); + if (o_CreateRenderTargetView != nullptr) + DetourAttach(&(PVOID&)o_CreateRenderTargetView, hkCreateRenderTargetView); - //if (o_CreateDepthStencilView != nullptr) - // DetourAttach(&(PVOID&)o_CreateDepthStencilView, hkCreateDepthStencilView); + //if (o_CreateDepthStencilView != nullptr) + // DetourAttach(&(PVOID&)o_CreateDepthStencilView, hkCreateDepthStencilView); - //if (o_CreateSampler != nullptr) - // DetourAttach(&(PVOID&)o_CreateSampler, hkCreateSampler); + //if (o_CreateSampler != nullptr) + // DetourAttach(&(PVOID&)o_CreateSampler, hkCreateSampler); - if (o_CreateShaderResourceView != nullptr) - DetourAttach(&(PVOID&)o_CreateShaderResourceView, hkCreateShaderResourceView); + if (o_CreateShaderResourceView != nullptr) + DetourAttach(&(PVOID&)o_CreateShaderResourceView, hkCreateShaderResourceView); - if (o_CreateUnorderedAccessView != nullptr) - DetourAttach(&(PVOID&)o_CreateUnorderedAccessView, hkCreateUnorderedAccessView); + if (o_CreateUnorderedAccessView != nullptr) + DetourAttach(&(PVOID&)o_CreateUnorderedAccessView, hkCreateUnorderedAccessView); - if (o_CopyDescriptors != nullptr) - DetourAttach(&(PVOID&)o_CopyDescriptors, hkCopyDescriptors); + if (o_CopyDescriptors != nullptr) + DetourAttach(&(PVOID&)o_CopyDescriptors, hkCopyDescriptors); - if (o_CopyDescriptorsSimple != nullptr) - DetourAttach(&(PVOID&)o_CopyDescriptorsSimple, hkCopyDescriptorsSimple); - - } + if (o_CopyDescriptorsSimple != nullptr) + DetourAttach(&(PVOID&)o_CopyDescriptorsSimple, hkCopyDescriptorsSimple); DetourTransactionCommit(); } diff --git a/OptiScaler/resource_tracking/ResTrack_dx12.h b/OptiScaler/resource_tracking/ResTrack_dx12.h index 33a07289..0e6277cd 100644 --- a/OptiScaler/resource_tracking/ResTrack_dx12.h +++ b/OptiScaler/resource_tracking/ResTrack_dx12.h @@ -179,6 +179,8 @@ private: static void hkExecuteBundle(ID3D12GraphicsCommandList* This, ID3D12GraphicsCommandList* pCommandList); + static void hkCreateConstantBufferView(ID3D12Device* This, const D3D12_CONSTANT_BUFFER_VIEW_DESC* pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor); + static ULONG hkRelease(ID3D12Resource* This); static void HookCommandList(ID3D12Device* InDevice);