From 9bdd2d0d4e7615f824cd3dbd92ce77134e972da1 Mon Sep 17 00:00:00 2001 From: cdozdila Date: Thu, 15 May 2025 23:10:07 +0300 Subject: [PATCH] Fixes for hungs/crashes --- OptiScaler/Config.h | 2 - OptiScaler/framegen/IFGFeature_Dx12.cpp | 5 +- OptiScaler/framegen/ffx/FSRFG_Dx12.cpp | 107 +++++------------- OptiScaler/hudfix/Hudfix_Dx12.cpp | 4 +- OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp | 3 +- .../resource_tracking/ResTrack_dx12.cpp | 12 +- OptiScaler/resource_tracking/ResTrack_dx12.h | 1 + 7 files changed, 47 insertions(+), 87 deletions(-) diff --git a/OptiScaler/Config.h b/OptiScaler/Config.h index 53ea50fe..9ecf7ade 100644 --- a/OptiScaler/Config.h +++ b/OptiScaler/Config.h @@ -353,8 +353,6 @@ public: CustomOptional FGHudfixFullSync{ false }; CustomOptional FGHudfixTrackRelease{ false }; CustomOptional FGHudfixCBVTrackMode{ 0 }; // 0 off, 1 after upscaler, 2 full - //CustomOptional FGUseCommandQueue{ true }; - // DLSS Enabler std::optional DE_FramerateLimit; // off - vsync - number diff --git a/OptiScaler/framegen/IFGFeature_Dx12.cpp b/OptiScaler/framegen/IFGFeature_Dx12.cpp index 362c5351..eb1e8518 100644 --- a/OptiScaler/framegen/IFGFeature_Dx12.cpp +++ b/OptiScaler/framegen/IFGFeature_Dx12.cpp @@ -76,7 +76,8 @@ bool IFGFeature_Dx12::CopyResource(ID3D12GraphicsCommandList* cmdList, ID3D12Res void IFGFeature_Dx12::WaitForFenceValue(ID3D12Fence* fence, UINT64 targetValue, HANDLE fenceEvent) { // Check if the fence has already been reached - if (fence->GetCompletedValue() < targetValue) + auto fenceValue = fence->GetCompletedValue(); + if (fenceValue < targetValue) { // Instruct the fence to signal the event when the target value is reached HRESULT hr = fence->SetEventOnCompletion(targetValue, fenceEvent); @@ -254,7 +255,7 @@ void IFGFeature_Dx12::CreateObjects(ID3D12Device* InDevice) queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; queueDesc.NodeMask = 0; - queueDesc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_HIGH; + queueDesc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL; HRESULT hr = InDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&queue)); if (result != S_OK) diff --git a/OptiScaler/framegen/ffx/FSRFG_Dx12.cpp b/OptiScaler/framegen/ffx/FSRFG_Dx12.cpp index eca6815f..b5fc18ea 100644 --- a/OptiScaler/framegen/ffx/FSRFG_Dx12.cpp +++ b/OptiScaler/framegen/ffx/FSRFG_Dx12.cpp @@ -259,22 +259,27 @@ bool FSRFG_Dx12::DispatchHudless(bool useHudless, double frameTime) { LOG_DEBUG("useHudless: {}, frameTime: {}", useHudless, frameTime); + if (Config::Instance()->FGUseMutexForSwaphain.value_or_default() && Mutex.getOwner() != 2) + { + LOG_TRACE("Waiting FG->Mutex 1, current: {}", Mutex.getOwner()); + Mutex.lock(1); + LOG_TRACE("Accuired FG->Mutex: {}", Mutex.getOwner()); + } + if (State::Instance().FSRFGFTPchanged) ConfigureFramePaceTuning(); - //if (Config::Instance()->FGUseCommandQueue.value_or_default()) - //{ + if (_lastFgFenceValue > 0) + WaitForFenceValue(_fgFence, _lastFgFenceValue, _fgFenceEvent); + + if (_lastCopyFenceValue > 0) + WaitForFenceValue(_copyFence, _lastCopyFenceValue, _copyFenceEvent); + + if (_lastHudlessCopyFenceValue > 0) + WaitForFenceValue(_hudlessCopyFence, _lastHudlessCopyFenceValue, _hudlessCopyFenceEvent); + if (useHudless) { - if (_lastFgFenceValue > 0) - WaitForFenceValue(_fgFence, _lastFgFenceValue, _fgFenceEvent); - - if (_lastCopyFenceValue > 0) - WaitForFenceValue(_copyFence, _lastCopyFenceValue, _copyFenceEvent); - - if (_lastHudlessCopyFenceValue > 0) - WaitForFenceValue(_hudlessCopyFence, _lastHudlessCopyFenceValue, _hudlessCopyFenceEvent); - // FG queue wait for copy operations LOG_DEBUG("FG Queue wait for copy, {}", _frameCount); _commandQueue->Wait(_copyFence, _frameCount); @@ -284,18 +289,12 @@ bool FSRFG_Dx12::DispatchHudless(bool useHudless, double frameTime) _commandQueue->Wait(_hudlessCopyFence, _frameCount); _lastHudlessCopyFenceValue = _frameCount; - LOG_DEBUG("Game Queue wait for FG, {}", _frameCount); - _gameCommandQueue->Wait(_fgFence, _frameCount); - _lastFgFenceValue = _frameCount; - - } - //} - - if (Config::Instance()->FGUseMutexForSwaphain.value_or_default() && Mutex.getOwner() != 2) - { - LOG_TRACE("Waiting FG->Mutex 1, current: {}", Mutex.getOwner()); - Mutex.lock(1); - LOG_TRACE("Accuired FG->Mutex: {}", Mutex.getOwner()); + if (State::Instance().currentCommandQueue != nullptr) + { + LOG_DEBUG("Game Queue wait for FG, {}", _frameCount); + State::Instance().currentCommandQueue->Wait(_fgFence, _frameCount); + _lastFgFenceValue = _frameCount; + } } // hudless captured for this frame @@ -361,15 +360,6 @@ bool FSRFG_Dx12::DispatchHudless(bool useHudless, double frameTime) return FFX_API_RETURN_ERROR; }; - /* - m_FrameGenerationConfig.presentCallbackUserContext = this; - m_FrameGenerationConfig.presentCallback = [](ffxCallbackDescFrameGenerationPresent* params, void* pUserCtx)->ffxReturnCode_t - { - LOG_DEBUG("frameId: {}, isGenerated: {}", params->frameID, params->isGeneratedFrame); - return FFX_API_RETURN_OK; - }; - */ - m_FrameGenerationConfig.onlyPresentGenerated = State::Instance().FGonlyGenerated; m_FrameGenerationConfig.frameID = _frameCount; m_FrameGenerationConfig.swapChain = State::Instance().currentSwapchain; @@ -410,8 +400,6 @@ bool FSRFG_Dx12::DispatchHudless(bool useHudless, double frameTime) retCode = FfxApiProxy::D3D12_Dispatch()(&_fgContext, &dfgPrepare.header); - //if (Config::Instance()->FGUseCommandQueue.value_or_default()) - //{ auto result = _commandList[fIndex]->Close(); LOG_DEBUG("_commandList[{}]->Close() result: {:X}", fIndex, (UINT)result); ID3D12CommandList* cl[] = { _commandList[fIndex] }; @@ -423,31 +411,13 @@ bool FSRFG_Dx12::DispatchHudless(bool useHudless, double frameTime) _commandQueue->Signal(_fgFence, _frameCount); LOG_DEBUG("Execute and signal from FG Queue, {}", _frameCount); } - else + else if (State::Instance().currentCommandQueue != nullptr) { //Use game queue - _gameCommandQueue->ExecuteCommandLists(1, cl); - _gameCommandQueue->Signal(_fgFence, _frameCount); + State::Instance().currentCommandQueue->ExecuteCommandLists(1, cl); + State::Instance().currentCommandQueue->Signal(_fgFence, _frameCount); LOG_DEBUG("Execute from game queue!!"); } - //} - //else - //{ - // if (!(useHudless && State::Instance().currentCommandQueue != nullptr) && - // !Config::Instance()->FGHudFixCloseAfterCallback.value_or_default()) - // { - // auto result = _commandList[fIndex]->Close(); - // LOG_DEBUG("_commandList[{}]->Close() result: {:X}", fIndex, (UINT)result); - - // if (result == S_OK) - // { - // ID3D12CommandList* cl[] = { _commandList[fIndex] }; - // // Use game queue - // _gameCommandQueue->ExecuteCommandLists(1, cl); - // LOG_DEBUG("Execute from game queue!!"); - // } - // } - //} LOG_DEBUG("D3D12_Dispatch result: {0}, frame: {1}, fIndex: {2}, commandList: {3:X}", retCode, _frameCount, fIndex, (size_t)dfgPrepare.commandList); } @@ -523,29 +493,6 @@ ffxReturnCode_t FSRFG_Dx12::HudlessDispatchCallback(ffxDispatchDescFrameGenerati LOG_DEBUG("frameID: {}, commandList: {:X}, numGeneratedFrames: {}", params->frameID, (size_t)params->commandList, params->numGeneratedFrames); - //if (!Config::Instance()->FGUseCommandQueue.value_or_default() && - // params->frameID != _lastUpscaledFrameId && Config::Instance()->FGHudFixCloseAfterCallback.value_or_default()) - //{ - // result = _commandList[fIndex]->Close(); - // LOG_DEBUG("fgCommandList[{}]->Close() result: {:X}", fIndex, (UINT)result); - - // // if there is command list error return ERROR - // if (result == S_OK) - // { - // ID3D12CommandList* cl[1] = { nullptr }; - // cl[0] = _commandList[fIndex]; - // _gameCommandQueue->ExecuteCommandLists(1, cl); - // } - // else - // { - // return FFX_API_RETURN_ERROR; - // } - //} - //else - //{ - // LOG_DEBUG("skipped close? frame id: {}", params->frameID); - //} - // check for status if (!Config::Instance()->FGEnabled.value_or_default() || !Config::Instance()->FGHUDFix.value_or_default() || _fgContext == nullptr || _commandQueue == nullptr || State::Instance().SCchanged) @@ -643,6 +590,10 @@ void FSRFG_Dx12::StopAndDestroyContext(bool destroy, bool shutDown, bool useMute WaitForFenceValue(_fgFence, _lastFgFenceValue, _fgFenceEvent); } + _lastCopyFenceValue = 0; + _lastHudlessCopyFenceValue = 0; + _lastFgFenceValue = 0; + if (!(shutDown || State::Instance().isShuttingDown) && _fgContext != nullptr) { ffxConfigureDescFrameGeneration m_FrameGenerationConfig = {}; diff --git a/OptiScaler/hudfix/Hudfix_Dx12.cpp b/OptiScaler/hudfix/Hudfix_Dx12.cpp index 0fb87dfe..a444399c 100644 --- a/OptiScaler/hudfix/Hudfix_Dx12.cpp +++ b/OptiScaler/hudfix/Hudfix_Dx12.cpp @@ -706,6 +706,8 @@ void Hudfix_Dx12::ResetCounters() _upscaleEndTime = 0.0; _targetTime = 0.0; _frameTime = 0.0; - + _hudlessList.clear(); + + LOG_DEBUG("_hudlessList: {}", _hudlessList.size()); } diff --git a/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp b/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp index c667c218..db5aaad5 100644 --- a/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp +++ b/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp @@ -1623,7 +1623,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom info.flags = desc.Flags; info.type = UAV; - Hudfix_Dx12::CheckForHudless(__FUNCTION__, InCmdList, &info, (D3D12_RESOURCE_STATES)Config::Instance()->OutputResourceBarrier.value_or(D3D12_RESOURCE_STATE_UNORDERED_ACCESS)); + if (Hudfix_Dx12::CheckForHudless(__FUNCTION__, InCmdList, &info, (D3D12_RESOURCE_STATES)Config::Instance()->OutputResourceBarrier.value_or(D3D12_RESOURCE_STATE_UNORDERED_ACCESS))) + ResTrack_Dx12::SetHudlessCmdList(InCmdList); } else { diff --git a/OptiScaler/resource_tracking/ResTrack_dx12.cpp b/OptiScaler/resource_tracking/ResTrack_dx12.cpp index fb5893f2..883146c5 100644 --- a/OptiScaler/resource_tracking/ResTrack_dx12.cpp +++ b/OptiScaler/resource_tracking/ResTrack_dx12.cpp @@ -786,7 +786,7 @@ void ResTrack_Dx12::hkCreateRenderTargetView(ID3D12Device* This, ID3D12Resource* RemoveResourceHeap(temp->buffer, DestDescriptor.ptr); } #else - pResource->SetPrivateData(GUID_Tracking, 4, &_trackMark); + //pResource->SetPrivateData(GUID_Tracking, 4, &_trackMark); #endif heap->SetByCpuHandle(DestDescriptor.ptr, resInfo); @@ -875,7 +875,7 @@ void ResTrack_Dx12::hkCreateShaderResourceView(ID3D12Device* This, ID3D12Resourc RemoveResourceHeap(temp->buffer, DestDescriptor.ptr); } #else - pResource->SetPrivateData(GUID_Tracking, 4, &_trackMark); + //pResource->SetPrivateData(GUID_Tracking, 4, &_trackMark); #endif heap->SetByCpuHandle(DestDescriptor.ptr, resInfo); @@ -963,7 +963,7 @@ void ResTrack_Dx12::hkCreateUnorderedAccessView(ID3D12Device* This, ID3D12Resour RemoveResourceHeap(temp->buffer, DestDescriptor.ptr); } #else - pResource->SetPrivateData(GUID_Tracking, 4, &_trackMark); + //pResource->SetPrivateData(GUID_Tracking, 4, &_trackMark); #endif heap->SetByCpuHandle(DestDescriptor.ptr, resInfo); @@ -2263,3 +2263,9 @@ void ResTrack_Dx12::SetUpscalerCmdList(ID3D12GraphicsCommandList* cmdList) _upscalerCommandList = cmdList; } +void ResTrack_Dx12::SetHudlessCmdList(ID3D12GraphicsCommandList* cmdList) +{ + LOG_DEBUG("cmdList: {:X}", (size_t)cmdList); + _commandList = cmdList; +} + diff --git a/OptiScaler/resource_tracking/ResTrack_dx12.h b/OptiScaler/resource_tracking/ResTrack_dx12.h index 99963a8b..a5e7b931 100644 --- a/OptiScaler/resource_tracking/ResTrack_dx12.h +++ b/OptiScaler/resource_tracking/ResTrack_dx12.h @@ -355,4 +355,5 @@ public: static void ClearPossibleHudless(); static void PresentDone(); static void SetUpscalerCmdList(ID3D12GraphicsCommandList* cmdList); + static void SetHudlessCmdList(ID3D12GraphicsCommandList* cmdList); }; \ No newline at end of file