From 9b7a5dee5c201aa5bbe9a1f9d57e5ea5ecced489 Mon Sep 17 00:00:00 2001 From: cdozdil Date: Thu, 21 Nov 2024 23:54:01 +0300 Subject: [PATCH] tried to remove/disable sleeps --- OptiScaler/NVNGX_DLSS_Dx12.cpp | 24 ++++++------ OptiScaler/hooks/HooksDx.cpp | 53 ++++++++++++++------------ OptiScaler/hooks/HooksDx.h | 4 +- OptiScaler/hooks/wrapped_swapchain.cpp | 38 ++++++------------ OptiScaler/hooks/wrapped_swapchain.h | 2 + OptiScaler/resource.h | 2 +- 6 files changed, 59 insertions(+), 64 deletions(-) diff --git a/OptiScaler/NVNGX_DLSS_Dx12.cpp b/OptiScaler/NVNGX_DLSS_Dx12.cpp index 2416dc9a..f557cbf8 100644 --- a/OptiScaler/NVNGX_DLSS_Dx12.cpp +++ b/OptiScaler/NVNGX_DLSS_Dx12.cpp @@ -1240,7 +1240,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom #ifdef USE_COPY_QUEUE_FOR_FG FrameGen_Dx12::fgCopyCommandAllocator->Reset(); FrameGen_Dx12::fgCopyCommandList->Reset(FrameGen_Dx12::fgCopyCommandAllocator, nullptr); - + commandList = FrameGen_Dx12::fgCopyCommandList; #else commandList = InCmdList; @@ -1423,7 +1423,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom auto fIndex = fgCallbackFrameIndex; // check for status - if (!Config::Instance()->FGEnabled.value_or(false) || + if (!Config::Instance()->FGEnabled.value_or(false) || FrameGen_Dx12::fgContext == nullptr || Config::Instance()->SCChanged #ifdef USE_QUEUE_FOR_FG || FrameGen_Dx12::fgCommandList[fIndex] == nullptr || FrameGen_Dx12::fgCommandQueue == nullptr @@ -1437,7 +1437,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom // If fg is active but upscaling paused if (Config::Instance()->CurrentFeature == nullptr || !FrameGen_Dx12::fgIsActive || - Config::Instance()->FGChanged || fgLastFGFrame == Config::Instance()->CurrentFeature->FrameCount() || + Config::Instance()->FGChanged || fgLastFGFrame == Config::Instance()->CurrentFeature->FrameCount() || Config::Instance()->CurrentFeature->FrameCount() == 0) { LOG_WARN("(FG) Callback without active FG! fIndex:{}", fIndex); @@ -1457,6 +1457,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom auto dispatchResult = FfxApiProxy::D3D12_Dispatch()(reinterpret_cast(pUserCtx), ¶ms->header); LOG_DEBUG("(FG) D3D12_Dispatch result: {}, fIndex: {}", (UINT)dispatchResult, fIndex); + if (dispatchResult == FFX_API_RETURN_OK) { #ifdef USE_QUEUE_FOR_FG @@ -1569,14 +1570,15 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom dfgPrepare.frameTimeDelta = FrameGen_Dx12::fgFrameTime; #ifdef USE_MUTEX_FOR_FFX - FrameGen_Dx12::ffxMutex.lock(); + { + std::unique_lock lock(FrameGen_Dx12::ffxMutex); #endif - Config::Instance()->dxgiSkipSpoofing = true; - retCode = FfxApiProxy::D3D12_Dispatch()(&FrameGen_Dx12::fgContext, &dfgPrepare.header); - Config::Instance()->dxgiSkipSpoofing = false; + Config::Instance()->dxgiSkipSpoofing = true; + retCode = FfxApiProxy::D3D12_Dispatch()(&FrameGen_Dx12::fgContext, &dfgPrepare.header); + Config::Instance()->dxgiSkipSpoofing = false; #ifdef USE_MUTEX_FOR_FFX - FrameGen_Dx12::ffxMutex.unlock(); + } #endif if (retCode != FFX_API_RETURN_OK) @@ -1584,14 +1586,14 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom else LOG_DEBUG("(FG) Dispatch ok."); } + } } - } return NVSDK_NGX_Result_Success; - } + } return NVSDK_NGX_Result_Fail; -} + } #pragma endregion diff --git a/OptiScaler/hooks/HooksDx.cpp b/OptiScaler/hooks/HooksDx.cpp index 4e08aafa..69fc3b7d 100644 --- a/OptiScaler/hooks/HooksDx.cpp +++ b/OptiScaler/hooks/HooksDx.cpp @@ -13,7 +13,6 @@ #pragma region FG definitions #include -#include #include // Clear heap info when ResourceDiscard is called @@ -360,7 +359,7 @@ static bool CreateBufferResource(ID3D12Device* InDevice, ResourceInfo* InSource, return false; } - D3D12_RESOURCE_DESC texDesc = InSource->buffer->GetDesc(); + D3D12_RESOURCE_DESC texDesc = InSource->buffer->GetDesc(); texDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; hr = InDevice->CreateCommittedResource(&heapProperties, D3D12_HEAP_FLAG_NONE, &texDesc, InState, nullptr, IID_PPV_ARGS(OutResource)); @@ -554,8 +553,8 @@ static void GetHudless(ID3D12GraphicsCommandList* This) auto fIndex = fgCallbackFrameIndex; // check for status - if (!Config::Instance()->FGEnabled.value_or(false) || !Config::Instance()->FGHUDFix.value_or(false) || - FrameGen_Dx12::fgContext == nullptr || FrameGen_Dx12::fgCommandList[fIndex] == nullptr || + if (!Config::Instance()->FGEnabled.value_or(false) || !Config::Instance()->FGHUDFix.value_or(false) || + FrameGen_Dx12::fgContext == nullptr || FrameGen_Dx12::fgCommandList[fIndex] == nullptr || FrameGen_Dx12::fgCommandQueue == nullptr || Config::Instance()->SCChanged) { LOG_WARN("Cancel async dispatch"); @@ -566,7 +565,7 @@ static void GetHudless(ID3D12GraphicsCommandList* This) // If fg is active but upscaling paused if (!fgDispatchCalled || Config::Instance()->CurrentFeature == nullptr || Config::Instance()->FGChanged || - fgLastFGFrame == Config::Instance()->CurrentFeature->FrameCount() || !FrameGen_Dx12::fgIsActive || + fgLastFGFrame == Config::Instance()->CurrentFeature->FrameCount() || !FrameGen_Dx12::fgIsActive || Config::Instance()->CurrentFeature->FrameCount() == 0) { LOG_WARN("Callback without hudless! frameID: {}", params->frameID); @@ -656,13 +655,15 @@ static void GetHudless(ID3D12GraphicsCommandList* This) } #ifdef USE_MUTEX_FOR_FFX - FrameGen_Dx12::ffxMutex.lock(); + { + std::unique_lock lock(FrameGen_Dx12::ffxMutex); #endif - Config::Instance()->dxgiSkipSpoofing = true; - retCode = FfxApiProxy::D3D12_Dispatch()(&FrameGen_Dx12::fgContext, &dfgPrepare.header); - Config::Instance()->dxgiSkipSpoofing = false; + Config::Instance()->dxgiSkipSpoofing = true; + retCode = FfxApiProxy::D3D12_Dispatch()(&FrameGen_Dx12::fgContext, &dfgPrepare.header); + Config::Instance()->dxgiSkipSpoofing = false; + #ifdef USE_MUTEX_FOR_FFX - FrameGen_Dx12::ffxMutex.unlock(); + } #endif fgDispatchCalled = true; LOG_DEBUG("D3D12_Dispatch result: {0}, frame: {1}", retCode, frame); @@ -1256,7 +1257,7 @@ static void hkCopyDescriptors(ID3D12Device* This, continue; auto buffer = srcHeap->GetByCpuHandle(srcHandle); - + // destination auto destHandle = destRangeStarts[destRangeIndex].ptr + destIndex * size; auto dstHeap = GetHeapByCpuHandle(destHandle); @@ -1784,6 +1785,8 @@ static void hkDispatch(ID3D12GraphicsCommandList* This, UINT ThreadGroupCountX, #ifdef USE_MUTEX_FOR_FFX static HRESULT hkFGPresent(void* This, UINT SyncInterval, UINT Flags) { + std::unique_lock lock(FrameGen_Dx12::ffxMutex); + auto fIndex = fgFrameIndex; LOG_DEBUG("{}", fIndex); @@ -1814,12 +1817,9 @@ static HRESULT hkFGPresent(void* This, UINT SyncInterval, UINT Flags) FrameGen_Dx12::fgContext != nullptr && HooksDx::currentSwapchain != nullptr && CheckCapture(__FUNCTION__)) { LOG_WARN("Can't capture hudless, calling HudFix dispatch!"); - GetHudless(nullptr); } - FrameGen_Dx12::ffxMutex.lock(); - auto result = o_FGSCPresent(This, SyncInterval, Flags); #ifdef USE_PRESENT_FOR_FT @@ -1839,7 +1839,6 @@ static HRESULT hkFGPresent(void* This, UINT SyncInterval, UINT Flags) #endif FrameGen_Dx12::upscaleRan = false; - FrameGen_Dx12::ffxMutex.unlock(); return result; } @@ -2002,8 +2001,8 @@ static HRESULT Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags } // death stranding fix??? - if (frameCounter < 5) - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + //if (frameCounter < 5) + // std::this_thread::sleep_for(std::chrono::milliseconds(500)); frameCounter++; @@ -3221,7 +3220,7 @@ static HRESULT hkD3D12CreateDevice(IDXGIAdapter* pAdapter, D3D_FEATURE_LEVEL Min LOG_WARN("GPU Based Validation active!"); debugController->SetEnableGPUBasedValidation(TRUE); #endif -} + } #endif Config::Instance()->dxgiSkipSpoofing = true; @@ -3550,29 +3549,35 @@ static void ClearNextFrame() void FrameGen_Dx12::ReleaseFGSwapchain(HWND hWnd) { +#ifdef USE_MUTEX_FOR_FFX + std::unique_lock lock(FrameGen_Dx12::ffxMutex); +#endif + ImGuiOverlayDx::CleanupRenderTarget(true, hWnd); +#ifndef USE_MUTEX_FOR_FFX std::this_thread::sleep_for(std::chrono::milliseconds(250)); +#endif if (FrameGen_Dx12::fgSwapChainContext != nullptr) { -#ifdef USE_MUTEX_FOR_FFX - FrameGen_Dx12::ffxMutex.lock(); -#endif auto result = FfxApiProxy::D3D12_DestroyContext()(&FrameGen_Dx12::fgSwapChainContext, nullptr); -#ifdef USE_MUTEX_FOR_FFX - FrameGen_Dx12::ffxMutex.unlock(); -#endif LOG_INFO("Destroy Ffx Swapchain Result: {}({})", result, FfxApiProxy::ReturnCodeToString(result)); + FrameGen_Dx12::fgSwapChainContext = nullptr; fgSwapChains.erase(hWnd); +#ifndef USE_MUTEX_FOR_FFX std::this_thread::sleep_for(std::chrono::milliseconds(250)); +#endif } if (FrameGen_Dx12::fgContext != nullptr) { FrameGen_Dx12::StopAndDestroyFGContext(true, false); + +#ifndef USE_MUTEX_FOR_FFX std::this_thread::sleep_for(std::chrono::milliseconds(250)); +#endif } } diff --git a/OptiScaler/hooks/HooksDx.h b/OptiScaler/hooks/HooksDx.h index 47629e3b..5144e23e 100644 --- a/OptiScaler/hooks/HooksDx.h +++ b/OptiScaler/hooks/HooksDx.h @@ -7,6 +7,7 @@ #include #include #include +#include // According to https://gpuopen.com/manuals/fidelityfx_sdk/fidelityfx_sdk-page_techniques_super-resolution-interpolation/#id11 // Will use mutex to prevent race condutions @@ -32,6 +33,7 @@ #include #include + namespace HooksDx { inline ID3D12QueryHeap* queryHeap = nullptr; @@ -88,7 +90,7 @@ namespace FrameGen_Dx12 inline bool fgIsActive = false; #ifdef USE_MUTEX_FOR_FFX - inline std::mutex ffxMutex; + inline std::shared_mutex ffxMutex; #endif UINT NewFrame(); diff --git a/OptiScaler/hooks/wrapped_swapchain.cpp b/OptiScaler/hooks/wrapped_swapchain.cpp index 16eea1dd..ad264304 100644 --- a/OptiScaler/hooks/wrapped_swapchain.cpp +++ b/OptiScaler/hooks/wrapped_swapchain.cpp @@ -118,7 +118,8 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers(UINT BufferCount, UINT Width, UINT { LOG_DEBUG(""); - FrameGen_Dx12::ffxMutex.lock(); + std::lock_guard lock(_localMutex); + std::unique_lock lock2(FrameGen_Dx12::ffxMutex); HRESULT result; DXGI_SWAP_CHAIN_DESC desc{}; @@ -131,8 +132,6 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers(UINT BufferCount, UINT Width, UINT Config::Instance()->FGOnlyUseCapturedResources = false; } - std::this_thread::sleep_for(std::chrono::milliseconds(150)); - if (Config::Instance()->CurrentFeature != nullptr) Config::Instance()->FGChanged = true; @@ -141,8 +140,6 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers(UINT BufferCount, UINT Width, UINT Config::Instance()->SCChanged = true; - std::this_thread::sleep_for(std::chrono::milliseconds(150)); - LOG_DEBUG("BufferCount: {0}, Width: {1}, Height: {2}, NewFormat: {3}, SwapChainFlags: {4:X}", BufferCount, Width, Height, (UINT)NewFormat, SwapChainFlags); result = m_pReal->ResizeBuffers(BufferCount, Width, Height, NewFormat, SwapChainFlags); @@ -151,9 +148,7 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers(UINT BufferCount, UINT Width, UINT Config::Instance()->ScreenWidth = Width; Config::Instance()->ScreenHeight = Height; } - - std::this_thread::sleep_for(std::chrono::milliseconds(150)); - + // Crude implementation of EndlesslyFlowering's AutoHDR-ReShade // https://github.com/EndlesslyFlowering/AutoHDR-ReShade if (Config::Instance()->forceHdr.value_or(false)) @@ -223,8 +218,6 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers(UINT BufferCount, UINT Width, UINT LOG_DEBUG("result: {0:X}", (UINT)result); - FrameGen_Dx12::ffxMutex.unlock(); - return result; } @@ -236,7 +229,8 @@ HRESULT STDMETHODCALLTYPE WrappedIDXGISwapChain4::GetContainingOutput(IDXGIOutpu HRESULT WrappedIDXGISwapChain4::ResizeBuffers1(UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT Format, UINT SwapChainFlags, const UINT* pCreationNodeMask, IUnknown* const* ppPresentQueue) { - FrameGen_Dx12::ffxMutex.lock(); + std::lock_guard lock(_localMutex); + std::unique_lock lock2(FrameGen_Dx12::ffxMutex); LOG_DEBUG(""); @@ -251,8 +245,6 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers1(UINT BufferCount, UINT Width, UIN Config::Instance()->FGOnlyUseCapturedResources = false; } - std::this_thread::sleep_for(std::chrono::milliseconds(150)); - if (Config::Instance()->CurrentFeature != nullptr) Config::Instance()->FGChanged = true; @@ -261,8 +253,6 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers1(UINT BufferCount, UINT Width, UIN Config::Instance()->SCChanged = true; - std::this_thread::sleep_for(std::chrono::milliseconds(150)); - LOG_DEBUG("BufferCount: {0}, Width: {1}, Height: {2}, NewFormat: {3}, SwapChainFlags: {4:X}, pCreationNodeMask: {5}", BufferCount, Width, Height, (UINT)Format, SwapChainFlags, *pCreationNodeMask); result = m_pReal3->ResizeBuffers1(BufferCount, Width, Height, Format, SwapChainFlags, pCreationNodeMask, ppPresentQueue); @@ -272,8 +262,6 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers1(UINT BufferCount, UINT Width, UIN Config::Instance()->ScreenHeight = Height; } - std::this_thread::sleep_for(std::chrono::milliseconds(150)); - // Crude implementation of EndlesslyFlowering's AutoHDR-ReShade // https://github.com/EndlesslyFlowering/AutoHDR-ReShade if (Config::Instance()->forceHdr.value_or(false)) @@ -340,14 +328,13 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers1(UINT BufferCount, UINT Width, UIN LOG_DEBUG("result: {0:X}", (UINT)result); - FrameGen_Dx12::ffxMutex.unlock(); - return result; } HRESULT WrappedIDXGISwapChain4::SetFullscreenState(BOOL Fullscreen, IDXGIOutput* pTarget) { - FrameGen_Dx12::ffxMutex.lock(); + std::lock_guard lock(_localMutex); + std::unique_lock lock2(FrameGen_Dx12::ffxMutex); auto result = m_pReal->SetFullscreenState(Fullscreen, pTarget); @@ -360,8 +347,6 @@ HRESULT WrappedIDXGISwapChain4::SetFullscreenState(BOOL Fullscreen, IDXGIOutput* Config::Instance()->FGOnlyUseCapturedResources = false; } - std::this_thread::sleep_for(std::chrono::milliseconds(150)); - if (Config::Instance()->CurrentFeature != nullptr) Config::Instance()->FGChanged = true; @@ -369,9 +354,6 @@ HRESULT WrappedIDXGISwapChain4::SetFullscreenState(BOOL Fullscreen, IDXGIOutput* ClearTrig(true, Handle); Config::Instance()->SCChanged = true; - - std::this_thread::sleep_for(std::chrono::milliseconds(150)); - Config::Instance()->scBuffers.clear(); UINT bc = 0; @@ -396,8 +378,6 @@ HRESULT WrappedIDXGISwapChain4::SetFullscreenState(BOOL Fullscreen, IDXGIOutput* LOG_DEBUG("result: {0:X}", (UINT)result); - FrameGen_Dx12::ffxMutex.unlock(); - return result; } @@ -421,6 +401,8 @@ HRESULT WrappedIDXGISwapChain4::Present(UINT SyncInterval, UINT Flags) if (m_pReal == nullptr) return DXGI_ERROR_DEVICE_REMOVED; + std::lock_guard lock(_localMutex); + HRESULT result; if (!(Flags & DXGI_PRESENT_TEST || Flags & DXGI_PRESENT_RESTART) && RenderTrig != nullptr) @@ -436,6 +418,8 @@ HRESULT WrappedIDXGISwapChain4::Present1(UINT SyncInterval, UINT Flags, const DX if (m_pReal1 == nullptr) return DXGI_ERROR_DEVICE_REMOVED; + std::lock_guard lock(_localMutex); + HRESULT result; if (!(Flags & DXGI_PRESENT_TEST || Flags & DXGI_PRESENT_RESTART) && RenderTrig != nullptr) diff --git a/OptiScaler/hooks/wrapped_swapchain.h b/OptiScaler/hooks/wrapped_swapchain.h index 1cea57db..15e1a263 100644 --- a/OptiScaler/hooks/wrapped_swapchain.h +++ b/OptiScaler/hooks/wrapped_swapchain.h @@ -260,5 +260,7 @@ struct DECLSPEC_UUID("3af622a3-82d0-49cd-994f-cce05122c222") WrappedIDXGISwapCha PFN_SC_Release ReleaseTrig = nullptr; HWND Handle = nullptr; + std::mutex _localMutex; + int id = 0; }; diff --git a/OptiScaler/resource.h b/OptiScaler/resource.h index 637eba82..1acc0144 100644 --- a/OptiScaler/resource.h +++ b/OptiScaler/resource.h @@ -21,7 +21,7 @@ #define VER_MAJOR_VERSION 0 #define VER_MINOR_VERSION 7 #define VER_HOTFIX_VERSION 0 -#define VER_BUILD_NUMBER 67 +#define VER_BUILD_NUMBER 68 #define VER_PRE_RELEASE