diff --git a/OptiScaler/FSR2_Dx12.cpp b/OptiScaler/FSR2_Dx12.cpp index 45d5d06f..daef6379 100644 --- a/OptiScaler/FSR2_Dx12.cpp +++ b/OptiScaler/FSR2_Dx12.cpp @@ -61,6 +61,7 @@ typedef Fsr212::FfxErrorCode(*PFN_ffxFsr2ContextGenerateReactiveMask)(Fsr212::Ff typedef Fsr212::FfxErrorCode(*PFN_ffxFsr2ContextDestroy)(Fsr212::FfxFsr2Context* context); typedef float(*PFN_ffxFsr2GetUpscaleRatioFromQualityMode)(Fsr212::FfxFsr2QualityMode qualityMode); typedef Fsr212::FfxErrorCode(*PFN_ffxFsr2GetRenderResolutionFromQualityMode)(uint32_t* renderWidth, uint32_t* renderHeight, uint32_t displayWidth, uint32_t displayHeight, Fsr212::FfxFsr2QualityMode qualityMode); +typedef bool(*PFN_ffxFsr2ResourceIsNull_Dx12)(FfxResourceBase resource); // Dx12 typedef size_t(*PFN_ffxFsr2GetScratchMemorySizeDX12)(); @@ -78,13 +79,13 @@ static PFN_ffxFsr2GetScratchMemorySizeDX12 o_ffxFsr2GetScratchMemorySizeDX12 = n static PFN_ffxFsr2GetInterfaceDX12 o_ffxFsr2GetInterfaceDX12 = nullptr; static PFN_ffxGetResourceDX12 o_ffxGetResourceDX12 = nullptr; static PFN_ffxGetDX12ResourcePtr o_ffxGetDX12ResourcePtr = nullptr; +static PFN_ffxFsr2ResourceIsNull_Dx12 o_ffxFsr2ResourceIsNull_Dx12 = nullptr; -static uint32_t _handleCounter = 0; static std::optional _version20; -static std::map _initParams; -static std::map _nvParams; -static std::map _contexts; +static std::map _initParams; +static std::map _nvParams; +static std::map _contexts; static ID3D12Device* _d3d12Device = nullptr; static bool _nvnxgInited = false; static float qualityRatios[] = { 1.0, 1.5, 1.7, 2.0, 3.0 }; @@ -93,14 +94,12 @@ static bool CreateDLSSContext(Fsr212::FfxFsr2Context* handle, const Fsr212::FfxF { LOG_DEBUG(""); - auto handleId = handle->data[1]; - - if (!_nvParams.contains(handleId)) + if (!_nvParams.contains(handle)) return false; NVSDK_NGX_Handle* nvHandle = nullptr; - auto params = _nvParams[handleId]; - auto initParams = &_initParams[handleId]; + auto params = _nvParams[handle]; + auto initParams = &_initParams[handle]; auto commandList = (ID3D12GraphicsCommandList*)pExecParams->commandList; UINT initFlags = 0; @@ -145,7 +144,7 @@ static bool CreateDLSSContext(Fsr212::FfxFsr2Context* handle, const Fsr212::FfxF if (NVSDK_NGX_D3D12_CreateFeature(commandList, NVSDK_NGX_Feature_SuperSampling, params, &nvHandle) != NVSDK_NGX_Result_Success) return false; - _contexts[handleId] = nvHandle; + _contexts[handle] = nvHandle; return true; } @@ -154,14 +153,12 @@ static bool CreateDLSSContext20(Fsr212::FfxFsr2Context* handle, const FfxFsr20Di { LOG_DEBUG(""); - auto handleId = handle->data[1]; - - if (!_nvParams.contains(handleId)) + if (!_nvParams.contains(handle)) return false; NVSDK_NGX_Handle* nvHandle = nullptr; - auto params = _nvParams[handleId]; - auto initParams = &_initParams[handleId]; + auto params = _nvParams[handle]; + auto initParams = &_initParams[handle]; auto commandList = (ID3D12GraphicsCommandList*)pExecParams->commandList; UINT initFlags = 0; @@ -206,7 +203,7 @@ static bool CreateDLSSContext20(Fsr212::FfxFsr2Context* handle, const FfxFsr20Di if (NVSDK_NGX_D3D12_CreateFeature(commandList, NVSDK_NGX_Feature_SuperSampling, params, &nvHandle) != NVSDK_NGX_Result_Success) return false; - _contexts[handleId] = nvHandle; + _contexts[handle] = nvHandle; return true; } @@ -268,7 +265,16 @@ static Fsr212::FfxErrorCode ffxFsr2ContextCreate_Dx12(Fsr212::FfxFsr2Context* co if (contextDescription == nullptr || contextDescription->device == nullptr) return Fsr212::FFX_ERROR_INVALID_ARGUMENT; - _d3d12Device = (ID3D12Device*)contextDescription->device; + if (_d3d12Device == nullptr) + _d3d12Device = (ID3D12Device*)contextDescription->device; + + auto ccResult = o_ffxFsr2ContextCreate_Dx12(context, contextDescription); + + if (ccResult != Fsr212::FFX_OK) + { + LOG_ERROR("ccResult: {:X}", (UINT)ccResult); + return ccResult; + } NVSDK_NGX_FeatureCommonInfo fcInfo{}; wchar_t const** paths = new const wchar_t* [1]; @@ -289,24 +295,20 @@ static Fsr212::FfxErrorCode ffxFsr2ContextCreate_Dx12(Fsr212::FfxFsr2Context* co _nvnxgInited = true; } - *context = {}; - (*context).data[0] = 0x1337; - (*context).data[1] = ++_handleCounter; - NVSDK_NGX_Parameter* params = nullptr; if (NVSDK_NGX_D3D12_GetCapabilityParameters(¶ms) != NVSDK_NGX_Result_Success) return Fsr212::FFX_ERROR_BACKEND_API_ERROR; - _nvParams[_handleCounter] = params; + _nvParams[context] = params; Fsr212::FfxFsr2ContextDescription ccd{}; ccd.flags = contextDescription->flags; ccd.maxRenderSize = contextDescription->maxRenderSize; ccd.displaySize = contextDescription->displaySize; - _initParams[_handleCounter] = ccd; + _initParams[context] = ccd; - LOG_INFO("context created: {:X}", (size_t)_handleCounter); + LOG_INFO("context created: {:X}", (size_t)context); return Fsr212::FFX_OK; } @@ -316,16 +318,12 @@ static Fsr212::FfxErrorCode ffxFsr2ContextDispatch_Dx12(Fsr212::FfxFsr2Context* if (dispatchDescription == nullptr || context == nullptr || dispatchDescription->commandList == nullptr) return Fsr212::FFX_ERROR_INVALID_ARGUMENT; - auto contextId = (*context).data[1]; - - LOG_DEBUG("context: {:X}", contextId); - // If not in contexts list create and add context - if (!_contexts.contains(contextId) && _initParams.contains(contextId) && !CreateDLSSContext(context, dispatchDescription)) + if (!_contexts.contains(context) && _initParams.contains(context) && !CreateDLSSContext(context, dispatchDescription)) return Fsr212::FFX_ERROR_INVALID_ARGUMENT; - NVSDK_NGX_Parameter* params = _nvParams[contextId]; - NVSDK_NGX_Handle* handle = _contexts[contextId]; + NVSDK_NGX_Parameter* params = _nvParams[context]; + NVSDK_NGX_Handle* handle = _contexts[context]; params->Set(NVSDK_NGX_Parameter_Jitter_Offset_X, dispatchDescription->jitterOffset.x); params->Set(NVSDK_NGX_Parameter_Jitter_Offset_Y, dispatchDescription->jitterOffset.y); @@ -370,16 +368,12 @@ static Fsr212::FfxErrorCode ffxFsr20ContextDispatch_Dx12(Fsr212::FfxFsr2Context* if (dispatchDescription == nullptr || context == nullptr || dispatchDescription->commandList == nullptr) return Fsr212::FFX_ERROR_INVALID_ARGUMENT; - auto contextId = (*context).data[1]; - - LOG_DEBUG("context: {:X}", contextId); - // If not in contexts list create and add context - if (!_contexts.contains(contextId) && _initParams.contains(contextId) && !CreateDLSSContext20(context, dispatchDescription)) + if (!_contexts.contains(context) && _initParams.contains(context) && !CreateDLSSContext20(context, dispatchDescription)) return Fsr212::FFX_ERROR_INVALID_ARGUMENT; - NVSDK_NGX_Parameter* params = _nvParams[contextId]; - NVSDK_NGX_Handle* handle = _contexts[contextId]; + NVSDK_NGX_Parameter* params = _nvParams[context]; + NVSDK_NGX_Handle* handle = _contexts[context]; params->Set(NVSDK_NGX_Parameter_Jitter_Offset_X, dispatchDescription->jitterOffset.x); params->Set(NVSDK_NGX_Parameter_Jitter_Offset_Y, dispatchDescription->jitterOffset.y); @@ -445,8 +439,9 @@ static Fsr212::FfxErrorCode ffxFsr2ContextDispatchBase_Dx12(Fsr212::FfxFsr2Conte static Fsr212::FfxErrorCode ffxFsr2ContextGenerateReactiveMask_Dx12(Fsr212::FfxFsr2Context* context, const Fsr212::FfxFsr2GenerateReactiveDescription* params) { - LOG_WARN(""); - return Fsr212::FFX_OK; + auto result = o_ffxFsr2ContextGenerateReactiveMask_Dx12(context, params); + LOG_WARN("result: {:X}", (size_t)result); + return result; } static Fsr212::FfxErrorCode ffxFsr2ContextDestroy_Dx12(Fsr212::FfxFsr2Context* context) @@ -454,18 +449,18 @@ static Fsr212::FfxErrorCode ffxFsr2ContextDestroy_Dx12(Fsr212::FfxFsr2Context* c if (context == nullptr) return Fsr212::FFX_ERROR_INVALID_ARGUMENT; - auto contextId = (*context).data[1]; - LOG_DEBUG("context: {:X}", contextId); - - if (!_initParams.contains(contextId)) + if (!_initParams.contains(context)) return Fsr212::FFX_ERROR_INVALID_ARGUMENT; - if (_contexts.contains(contextId)) - NVSDK_NGX_D3D12_ReleaseFeature(_contexts[contextId]); + auto cdResult = ffxFsr2ContextDestroy_Dx12(context); + LOG_INFO("result: {:X}", (UINT)cdResult); - _contexts.erase(contextId); - _nvParams.erase(contextId); - _initParams.erase(contextId); + if (_contexts.contains(context)) + NVSDK_NGX_D3D12_ReleaseFeature(_contexts[context]); + + _contexts.erase(context); + _nvParams.erase(context); + _initParams.erase(context); return Fsr212::FFX_OK; } @@ -498,18 +493,24 @@ static Fsr212::FfxErrorCode ffxFsr2GetRenderResolutionFromQualityMode_Dx12(uint3 return Fsr212::FFX_ERROR_INVALID_ARGUMENT; } +static bool ffxFsr2ResourceIsNull_Dx12(FfxResourceBase resource) +{ + return resource.resource == nullptr; +} + // Dx12 Backend static size_t hk_ffxFsr2GetScratchMemorySizeDX12() { LOG_WARN(""); - return 1920 * 1080 * 31; + return o_ffxFsr2GetScratchMemorySizeDX12(); } static Fsr212::FfxErrorCode hk_ffxFsr2GetInterfaceDX12(Fsr212::FfxFsr2Interface212* fsr2Interface, ID3D12Device* device, void* scratchBuffer, size_t scratchBufferSize) { LOG_DEBUG(""); - return Fsr212::FFX_OK; + _d3d12Device = device; + return o_ffxFsr2GetInterfaceDX12(fsr2Interface, device, scratchBuffer, scratchBufferSize); } static FfxResourceBase hk_ffxGetResourceBaseDX12(Fsr212::FfxFsr2Context* context, ID3D12Resource* resDx12, const wchar_t* name = nullptr, @@ -573,6 +574,10 @@ void HookFSR2ExeInputs() if (o_ffxFsr2GetRenderResolutionFromQualityMode_Dx12 == nullptr) o_ffxFsr2GetRenderResolutionFromQualityMode_Dx12 = (PFN_ffxFsr2GetRenderResolutionFromQualityMode)DetourFindFunction(exeName.c_str(), "?ffxFsr2GetRenderResolutionFromQualityMode@@YAHPEAI0IIW4FfxFsr2QualityMode@@@Z"); + o_ffxFsr2ResourceIsNull_Dx12 = (PFN_ffxFsr2ResourceIsNull_Dx12)DetourFindFunction(exeName.c_str(), "ffxFsr2ResourceIsNull"); + //if (o_ffxFsr2GetRenderResolutionFromQualityMode_Dx12 == nullptr) + // o_ffxFsr2GetRenderResolutionFromQualityMode_Dx12 = (PFN_ffxFsr2GetRenderResolutionFromQualityMode)DetourFindFunction(exeName.c_str(), "?ffxFsr2GetRenderResolutionFromQualityMode@@YAHPEAI0IIW4FfxFsr2QualityMode@@@Z"); + if (o_ffxFsr2GetInterfaceDX12 != nullptr || o_ffxFsr2ContextCreate_Dx12 != nullptr) { LOG_INFO("FSR2 methods found, now hooking"); @@ -610,6 +615,9 @@ void HookFSR2ExeInputs() if (o_ffxFsr2GetRenderResolutionFromQualityMode_Dx12 != nullptr) DetourAttach(&(PVOID&)o_ffxFsr2GetRenderResolutionFromQualityMode_Dx12, ffxFsr2GetRenderResolutionFromQualityMode_Dx12); + if (o_ffxFsr2ResourceIsNull_Dx12 != nullptr) + DetourAttach(&(PVOID&)o_ffxFsr2ResourceIsNull_Dx12, ffxFsr2ResourceIsNull_Dx12); + DetourTransactionCommit(); } } @@ -618,12 +626,16 @@ void HookFSR2Dx12Inputs(HMODULE module) { LOG_INFO("Trying to hook FSR2 methods"); + if (o_ffxFsr2GetScratchMemorySizeDX12 != nullptr) + return; + if (module != nullptr) { o_ffxFsr2GetScratchMemorySizeDX12 = (PFN_ffxFsr2GetScratchMemorySizeDX12)GetProcAddress(module, "ffxFsr2GetScratchMemorySizeDX12"); o_ffxFsr2GetInterfaceDX12 = (PFN_ffxFsr2GetInterfaceDX12)GetProcAddress(module, "ffxFsr2GetInterfaceDX12"); o_ffxGetResourceDX12 = (PFN_ffxGetResourceDX12)GetProcAddress(module, "ffxGetResourceDX12"); o_ffxGetDX12ResourcePtr = (PFN_ffxGetDX12ResourcePtr)GetProcAddress(module, "ffxGetDX12ResourcePtr"); + o_ffxFsr2ResourceIsNull_Dx12 = (PFN_ffxFsr2ResourceIsNull_Dx12)GetProcAddress(module, "ffxFsr2ResourceIsNull"); } if (o_ffxFsr2GetScratchMemorySizeDX12 != nullptr) @@ -645,6 +657,9 @@ void HookFSR2Dx12Inputs(HMODULE module) if (o_ffxGetDX12ResourcePtr != nullptr) DetourAttach(&(PVOID&)o_ffxGetDX12ResourcePtr, hk_ffxGetDX12ResourcePtr); + if (o_ffxFsr2ResourceIsNull_Dx12 != nullptr) + DetourAttach(&(PVOID&)o_ffxFsr2ResourceIsNull_Dx12, ffxFsr2ResourceIsNull_Dx12); + DetourTransactionCommit(); } } @@ -653,6 +668,9 @@ void HookFSR2Inputs(HMODULE module) { LOG_INFO("Trying to hook FSR2 methods"); + if (o_ffxFsr2ContextCreate_Dx12 != nullptr) + return; + if (module != nullptr) { o_ffxFsr2ContextCreate_Dx12 = (PFN_ffxFsr2ContextCreate)GetProcAddress(module, "ffxFsr2ContextCreate"); diff --git a/OptiScaler/FSR3_Dx12.cpp b/OptiScaler/FSR3_Dx12.cpp index 1e0ec3d8..73ac89cd 100644 --- a/OptiScaler/FSR3_Dx12.cpp +++ b/OptiScaler/FSR3_Dx12.cpp @@ -12,14 +12,16 @@ // FSR3 typedef Fsr3::FfxErrorCode(*PFN_ffxFsr3UpscalerContextCreate)(Fsr3::FfxFsr3UpscalerContext* pContext, const Fsr3::FfxFsr3UpscalerContextDescription* pContextDescription); +typedef Fsr3::FfxErrorCode(*PFN_ffxFsr3UpscalerGetSharedResourceDescriptions)(Fsr3::FfxFsr3UpscalerContext* context, Fsr3::FfxFsr3UpscalerSharedResourceDescriptions* SharedResources); typedef Fsr3::FfxErrorCode(*PFN_ffxFsr3UpscalerContextDispatch)(Fsr3::FfxFsr3UpscalerContext* pContext, const Fsr3::FfxFsr3UpscalerDispatchDescription* pDispatchDescription); typedef Fsr3::FfxErrorCode(*PFN_ffxFsr3UpscalerContextGenerateReactiveMask)(Fsr3::FfxFsr3UpscalerContext* pContext, const Fsr3::FfxFsr3UpscalerGenerateReactiveDescription* pParams); typedef Fsr3::FfxErrorCode(*PFN_ffxFsr3UpscalerContextDestroy)(Fsr3::FfxFsr3UpscalerContext* pContext); typedef float(*PFN_ffxFsr3UpscalerGetUpscaleRatioFromQualityMode)(Fsr3::FfxFsr3UpscalerQualityMode qualityMode); typedef Fsr3::FfxErrorCode(*PFN_ffxFsr3UpscalerGetRenderResolutionFromQualityMode)(uint32_t* pRenderWidth, uint32_t* pRenderHeight, uint32_t displayWidth, uint32_t displayHeight, Fsr3::FfxFsr3UpscalerQualityMode qualityMode); -typedef Fsr3::FfxErrorCode(*PFN_ffxFsr3UpscalerGetSharedResourceDescriptions)(Fsr3::FfxFsr3UpscalerContext* context, Fsr3::FfxFsr3UpscalerSharedResourceDescriptions* SharedResources); +typedef bool(*PFN_ffxFsr3UpscalerResourceIsNull)(Fsr3::FfxResource resource); + // Dx12 -typedef size_t(*PFN_ffxFSR3GetScratchMemorySizeDX12)(); +typedef size_t(*PFN_ffxFSR3GetScratchMemorySizeDX12)(size_t maxContexts); typedef Fsr3::FfxErrorCode(*PFN_ffxFSR3GetInterfaceDX12)(Fsr3::FfxInterface* backendInterface, Fsr3::FfxDevice device, void* scratchBuffer, size_t scratchBufferSize, uint32_t maxContexts); typedef Fsr3::FfxResource(*PFN_ffxFSR3GetResourceDX12)(const ID3D12Resource* dx12Resource, Fsr3::FfxResourceDescription ffxResDescription, wchar_t* ffxResName, Fsr3::FfxResourceStates state); @@ -30,15 +32,15 @@ static PFN_ffxFsr3UpscalerContextDestroy o_ffxFsr3UpscalerContextDestroy_Dx12 = static PFN_ffxFsr3UpscalerGetUpscaleRatioFromQualityMode o_ffxFsr3UpscalerGetUpscaleRatioFromQualityMode_Dx12 = nullptr; static PFN_ffxFsr3UpscalerGetRenderResolutionFromQualityMode o_ffxFsr3UpscalerGetRenderResolutionFromQualityMode_Dx12 = nullptr; static PFN_ffxFsr3UpscalerGetSharedResourceDescriptions o_ffxFsr3UpscalerGetSharedResourceDescriptions = nullptr; +static PFN_ffxFsr3UpscalerResourceIsNull o_ffxFsr3UpscalerResourceIsNull = nullptr; + static PFN_ffxFSR3GetScratchMemorySizeDX12 o_ffxFSR3GetScratchMemorySizeDX12 = nullptr; static PFN_ffxFSR3GetInterfaceDX12 o_ffxFSR3GetInterfaceDX12 = nullptr; static PFN_ffxFSR3GetResourceDX12 o_ffxGetFSR3ResourceDX12 = nullptr; -static uint32_t _handleCounter = 0; - -static std::map _initParams; -static std::map _nvParams; -static std::map _contexts; +static std::map _initParams; +static std::map _nvParams; +static std::map _contexts; static ID3D12Device* _d3d12Device = nullptr; static bool _nvnxgInited = false; static float qualityRatios[] = { 1.0, 1.5, 1.7, 2.0, 3.0 }; @@ -47,14 +49,12 @@ static bool CreateDLSSContext(Fsr3::FfxFsr3UpscalerContext* handle, const Fsr3:: { LOG_DEBUG(""); - auto handleId = handle->data[1]; - - if (!_nvParams.contains(handleId)) + if (!_nvParams.contains(handle)) return false; NVSDK_NGX_Handle* nvHandle = nullptr; - auto params = _nvParams[handleId]; - auto initParams = &_initParams[handleId]; + auto params = _nvParams[handle]; + auto initParams = &_initParams[handle]; auto commandList = (ID3D12GraphicsCommandList*)pExecParams->commandList; UINT initFlags = 0; @@ -99,7 +99,7 @@ static bool CreateDLSSContext(Fsr3::FfxFsr3UpscalerContext* handle, const Fsr3:: if (NVSDK_NGX_D3D12_CreateFeature(commandList, NVSDK_NGX_Feature_SuperSampling, params, &nvHandle) != NVSDK_NGX_Result_Success) return false; - _contexts[handleId] = nvHandle; + _contexts[handle] = nvHandle; return true; } @@ -170,6 +170,14 @@ static Fsr3::FfxErrorCode ffxFsr3ContextCreate_Dx12(Fsr3::FfxFsr3UpscalerContext if (_d3d12Device == nullptr) _d3d12Device = (ID3D12Device*)pContextDescription->backendInterface.device; + auto ccResult = o_ffxFsr3UpscalerContextCreate_Dx12(pContext, pContextDescription); + + if (ccResult != Fsr3::FFX_OK) + { + LOG_ERROR("create error: {}", (UINT)ccResult); + return ccResult; + } + NVSDK_NGX_FeatureCommonInfo fcInfo{}; wchar_t const** paths = new const wchar_t* [1]; auto dllPath = Util::DllPath().remove_filename().wstring(); @@ -189,44 +197,36 @@ static Fsr3::FfxErrorCode ffxFsr3ContextCreate_Dx12(Fsr3::FfxFsr3UpscalerContext _nvnxgInited = true; } - //*pContext = {}; - (*pContext).data[0] = 0x1337; - (*pContext).data[1] = ++_handleCounter; - NVSDK_NGX_Parameter* params = nullptr; if (NVSDK_NGX_D3D12_GetCapabilityParameters(¶ms) != NVSDK_NGX_Result_Success) return Fsr3::FFX_ERROR_BACKEND_API_ERROR; - _nvParams[_handleCounter] = params; + _nvParams[pContext] = params; Fsr3::FfxFsr3UpscalerContextDescription ccd{}; ccd.flags = pContextDescription->flags; ccd.maxRenderSize = pContextDescription->maxRenderSize; ccd.displaySize = pContextDescription->displaySize; ccd.backendInterface.device = pContextDescription->backendInterface.device; - _initParams[_handleCounter] = ccd; + _initParams[pContext] = ccd; - LOG_INFO("context created: {:X}", (size_t)_handleCounter); + LOG_INFO("context created: {:X}", (size_t)pContext); return Fsr3::FFX_OK; } -static Fsr3::FfxErrorCode ffxFsr3ContextDispatch_Dx12(Fsr3::FfxFsr3UpscalerContext* pContext, const Fsr3::FfxFsr3UpscalerDispatchDescription* pDispatchDescription) +static Fsr3::FfxErrorCode ffxFsr3ContextDispatch_Dx12(Fsr3::FfxFsr3UpscalerContext* pContext, Fsr3::FfxFsr3UpscalerDispatchDescription* pDispatchDescription) { if (pDispatchDescription == nullptr || pContext == nullptr || pDispatchDescription->commandList == nullptr) return Fsr3::FFX_ERROR_INVALID_ARGUMENT; - auto contextId = (*pContext).data[1]; - - LOG_DEBUG("context: {:X}", contextId); - // If not in contexts list create and add context - if (!_contexts.contains(contextId) && _initParams.contains(contextId) && !CreateDLSSContext(pContext, pDispatchDescription)) + if (!_contexts.contains(pContext) && _initParams.contains(pContext) && !CreateDLSSContext(pContext, pDispatchDescription)) return Fsr3::FFX_ERROR_INVALID_ARGUMENT; - NVSDK_NGX_Parameter* params = _nvParams[contextId]; - NVSDK_NGX_Handle* handle = _contexts[contextId]; + NVSDK_NGX_Parameter* params = _nvParams[pContext]; + NVSDK_NGX_Handle* handle = _contexts[pContext]; params->Set(NVSDK_NGX_Parameter_Jitter_Offset_X, pDispatchDescription->jitterOffset.x); params->Set(NVSDK_NGX_Parameter_Jitter_Offset_Y, pDispatchDescription->jitterOffset.y); @@ -269,8 +269,9 @@ static Fsr3::FfxErrorCode ffxFsr3ContextDispatch_Dx12(Fsr3::FfxFsr3UpscalerConte static Fsr3::FfxErrorCode ffxFsr3ContextGenerateReactiveMask_Dx12(Fsr3::FfxFsr3UpscalerContext* pContext, const Fsr3::FfxFsr3UpscalerGenerateReactiveDescription* pParams) { - LOG_WARN(""); - return Fsr3::FFX_OK; + LOG_DEBUG(""); + return o_ffxFsr3UpscalerContextGenerateReactiveMask_Dx12(pContext, pParams); + //return Fsr3::FFX_OK; } static Fsr3::FfxErrorCode ffxFsr3ContextDestroy_Dx12(Fsr3::FfxFsr3UpscalerContext* pContext) @@ -278,18 +279,20 @@ static Fsr3::FfxErrorCode ffxFsr3ContextDestroy_Dx12(Fsr3::FfxFsr3UpscalerContex if (pContext == nullptr) return Fsr3::FFX_ERROR_INVALID_ARGUMENT; - auto contextId = (*pContext).data[1]; - LOG_DEBUG("context: {:X}", contextId); + LOG_DEBUG("context: {:X}", (size_t)pContext); - if (!_initParams.contains(contextId)) + if (!_initParams.contains(pContext)) return Fsr3::FFX_ERROR_INVALID_ARGUMENT; - if (_contexts.contains(contextId)) - NVSDK_NGX_D3D12_ReleaseFeature(_contexts[contextId]); + auto cdResult = ffxFsr3ContextDestroy_Dx12(pContext); + LOG_INFO("result: {:X}", (UINT)cdResult); - _contexts.erase(contextId); - _nvParams.erase(contextId); - _initParams.erase(contextId); + if (_contexts.contains(pContext)) + NVSDK_NGX_D3D12_ReleaseFeature(_contexts[pContext]); + + _contexts.erase(pContext); + _nvParams.erase(pContext); + _initParams.erase(pContext); return Fsr3::FFX_OK; } @@ -306,10 +309,10 @@ static Fsr3::FfxErrorCode ffxFsr3GetRenderResolutionFromQualityMode_Dx12(uint32_ auto ratio = GetQualityOverrideRatioFfx(qualityMode).value_or(qualityRatios[(UINT)qualityMode]); if (pRenderWidth != nullptr) - *pRenderWidth = (uint32_t)((float)displayHeight / ratio); + *pRenderWidth = (uint32_t)((float)displayWidth / ratio); if (pRenderHeight != nullptr) - *pRenderHeight = (uint32_t)((float)displayWidth / ratio); + *pRenderHeight = (uint32_t)((float)displayHeight / ratio); if (pRenderWidth != nullptr && pRenderHeight != nullptr) { @@ -328,26 +331,33 @@ static Fsr3::FfxErrorCode ffxFsr3UpscalerGetSharedResourceDescriptions(Fsr3::Ffx if (pContext == nullptr || SharedResources == nullptr) return Fsr3::FFX_ERROR_INVALID_POINTER; - auto contextId = (*pContext).data[1]; - auto initParams = _initParams[contextId]; + auto initParams = _initParams[pContext]; SharedResources->dilatedDepth = { Fsr3::FFX_HEAP_TYPE_DEFAULT, { Fsr3::FFX_RESOURCE_TYPE_TEXTURE2D, Fsr3::FFX_SURFACE_FORMAT_R32_FLOAT, initParams.maxRenderSize.width, initParams.maxRenderSize.height, 1, 1, Fsr3::FFX_RESOURCE_FLAGS_ALIASABLE, (Fsr3::FfxResourceUsage)(Fsr3::FFX_RESOURCE_USAGE_RENDERTARGET | Fsr3::FFX_RESOURCE_USAGE_UAV) }, Fsr3::FFX_RESOURCE_STATE_UNORDERED_ACCESS, 0, nullptr, L"FSR3UPSCALER_DilatedDepth", FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_DILATED_DEPTH }; + SharedResources->dilatedMotionVectors = { Fsr3::FFX_HEAP_TYPE_DEFAULT, { Fsr3::FFX_RESOURCE_TYPE_TEXTURE2D, Fsr3::FFX_SURFACE_FORMAT_R16G16_FLOAT, initParams.maxRenderSize.width, initParams.maxRenderSize.height, 1, 1, Fsr3::FFX_RESOURCE_FLAGS_ALIASABLE, (Fsr3::FfxResourceUsage)(Fsr3::FFX_RESOURCE_USAGE_RENDERTARGET | Fsr3::FFX_RESOURCE_USAGE_UAV) }, Fsr3::FFX_RESOURCE_STATE_UNORDERED_ACCESS, 0, nullptr, L"FSR3UPSCALER_DilatedVelocity", FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS }; + SharedResources->reconstructedPrevNearestDepth = { Fsr3::FFX_HEAP_TYPE_DEFAULT, { Fsr3::FFX_RESOURCE_TYPE_TEXTURE2D, Fsr3::FFX_SURFACE_FORMAT_R32_UINT, initParams.maxRenderSize.width, initParams.maxRenderSize.height, 1, 1, Fsr3::FFX_RESOURCE_FLAGS_ALIASABLE, (Fsr3::FfxResourceUsage)(Fsr3::FFX_RESOURCE_USAGE_UAV) }, Fsr3::FFX_RESOURCE_STATE_UNORDERED_ACCESS, 0, nullptr, L"FSR3UPSCALER_ReconstructedPrevNearestDepth", FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH }; return Fsr3::FFX_OK; } -// Dx12 Backend -//static size_t hk_ffxFsr3GetScratchMemorySizeDX12() -//{ -// LOG_WARN(""); -// return 1920 * 1080 * 31; -//} +static bool ffxFsr3UpscalerResourceIsNull(Fsr3::FfxResource resource) +{ + return resource.resource == nullptr; +} +static size_t hk_ffxFsr3GetScratchMemorySizeDX12(size_t maxContexts) +{ + auto result = o_ffxFSR3GetScratchMemorySizeDX12(maxContexts); + LOG_WARN("result: {} ", result); + return result; +} + +// Dx12 Backend static Fsr3::FfxErrorCode hk_ffxFsr3GetInterfaceDX12(Fsr3::FfxInterface* backendInterface, Fsr3::FfxDevice device, void* scratchBuffer, size_t scratchBufferSize, uint32_t maxContexts) { LOG_DEBUG(""); @@ -388,6 +398,7 @@ void HookFSR3ExeInputs() o_ffxFsr3UpscalerGetUpscaleRatioFromQualityMode_Dx12 = (PFN_ffxFsr3UpscalerGetUpscaleRatioFromQualityMode)DetourFindFunction(exeName.c_str(), "ffxFsr3UpscalerGetUpscaleRatioFromQualityMode"); o_ffxFsr3UpscalerGetRenderResolutionFromQualityMode_Dx12 = (PFN_ffxFsr3UpscalerGetRenderResolutionFromQualityMode)DetourFindFunction(exeName.c_str(), "ffxFsr3UpscalerGetRenderResolutionFromQualityMode"); o_ffxFsr3UpscalerGetSharedResourceDescriptions = (PFN_ffxFsr3UpscalerGetSharedResourceDescriptions)DetourFindFunction(exeName.c_str(), "ffxFsr3UpscalerGetSharedResourceDescriptions"); + o_ffxFsr3UpscalerResourceIsNull = (PFN_ffxFsr3UpscalerResourceIsNull)DetourFindFunction(exeName.c_str(), "ffxFsr3UpscalerResourceIsNull"); if (o_ffxFSR3GetInterfaceDX12 != nullptr || o_ffxFsr3UpscalerContextCreate_Dx12 != nullptr) { @@ -396,8 +407,8 @@ void HookFSR3ExeInputs() DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); - //if (o_ffxFSR3GetScratchMemorySizeDX12 != nullptr) - // DetourAttach(&(PVOID&)o_ffxFSR3GetScratchMemorySizeDX12, hk_ffxFsr3GetScratchMemorySizeDX12); + if (o_ffxFSR3GetScratchMemorySizeDX12 != nullptr) + DetourAttach(&(PVOID&)o_ffxFSR3GetScratchMemorySizeDX12, hk_ffxFsr3GetScratchMemorySizeDX12); if (o_ffxFSR3GetInterfaceDX12 != nullptr) DetourAttach(&(PVOID&)o_ffxFSR3GetInterfaceDX12, hk_ffxFsr3GetInterfaceDX12); @@ -429,6 +440,9 @@ void HookFSR3ExeInputs() if (o_ffxFsr3UpscalerGetSharedResourceDescriptions != nullptr) DetourAttach(&(PVOID&)o_ffxFsr3UpscalerGetSharedResourceDescriptions, ffxFsr3UpscalerGetSharedResourceDescriptions); + if (o_ffxFsr3UpscalerResourceIsNull != nullptr) + DetourAttach(&(PVOID&)o_ffxFsr3UpscalerResourceIsNull, ffxFsr3UpscalerResourceIsNull); + DetourTransactionCommit(); } } @@ -437,6 +451,9 @@ void HookFSR3Dx12Inputs(HMODULE module) { LOG_INFO("Trying to hook FSR3 methods"); + if (o_ffxFSR3GetInterfaceDX12 != nullptr) + return; + if (module != nullptr) { o_ffxFSR3GetScratchMemorySizeDX12 = (PFN_ffxFSR3GetScratchMemorySizeDX12)GetProcAddress(module, "ffxGetScratchMemorySizeDX12"); @@ -451,8 +468,8 @@ void HookFSR3Dx12Inputs(HMODULE module) DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); - //if (o_ffxFSR3GetScratchMemorySizeDX12 != nullptr) - // DetourAttach(&(PVOID&)o_ffxFSR3GetScratchMemorySizeDX12, hk_ffxFsr3GetScratchMemorySizeDX12); + if (o_ffxFSR3GetScratchMemorySizeDX12 != nullptr) + DetourAttach(&(PVOID&)o_ffxFSR3GetScratchMemorySizeDX12, hk_ffxFsr3GetScratchMemorySizeDX12); if (o_ffxFSR3GetInterfaceDX12 != nullptr) DetourAttach(&(PVOID&)o_ffxFSR3GetInterfaceDX12, hk_ffxFsr3GetInterfaceDX12); @@ -468,6 +485,9 @@ void HookFSR3Inputs(HMODULE module) { LOG_INFO("Trying to hook FSR3 methods"); + if (o_ffxFsr3UpscalerContextCreate_Dx12 != nullptr) + return; + if (module != nullptr) { o_ffxFsr3UpscalerContextCreate_Dx12 = (PFN_ffxFsr3UpscalerContextCreate)GetProcAddress(module, "ffxFsr3UpscalerContextCreate"); @@ -477,6 +497,7 @@ void HookFSR3Inputs(HMODULE module) o_ffxFsr3UpscalerGetUpscaleRatioFromQualityMode_Dx12 = (PFN_ffxFsr3UpscalerGetUpscaleRatioFromQualityMode)GetProcAddress(module, "ffxFsr3UpscalerGetUpscaleRatioFromQualityMode"); o_ffxFsr3UpscalerGetRenderResolutionFromQualityMode_Dx12 = (PFN_ffxFsr3UpscalerGetRenderResolutionFromQualityMode)GetProcAddress(module, "ffxFsr3UpscalerGetRenderResolutionFromQualityMode"); o_ffxFsr3UpscalerGetSharedResourceDescriptions = (PFN_ffxFsr3UpscalerGetSharedResourceDescriptions)GetProcAddress(module, "ffxFsr3UpscalerGetSharedResourceDescriptions"); + o_ffxFsr3UpscalerResourceIsNull = (PFN_ffxFsr3UpscalerResourceIsNull)GetProcAddress(module, "ffxFsr3UpscalerResourceIsNull"); } if (o_ffxFsr3UpscalerContextCreate_Dx12 != nullptr) @@ -510,6 +531,9 @@ void HookFSR3Inputs(HMODULE module) if (o_ffxFsr3UpscalerGetSharedResourceDescriptions != nullptr) DetourAttach(&(PVOID&)o_ffxFsr3UpscalerGetSharedResourceDescriptions, ffxFsr3UpscalerGetSharedResourceDescriptions); + if (o_ffxFsr3UpscalerResourceIsNull != nullptr) + DetourAttach(&(PVOID&)o_ffxFsr3UpscalerResourceIsNull, ffxFsr3UpscalerResourceIsNull); + DetourTransactionCommit(); } } diff --git a/OptiScaler/OptiScaler.vcxproj b/OptiScaler/OptiScaler.vcxproj index 573cb268..0a3100de 100644 --- a/OptiScaler/OptiScaler.vcxproj +++ b/OptiScaler/OptiScaler.vcxproj @@ -82,7 +82,7 @@ $(SolutionDir)external\vulkan\include;$(SolutionDir)external\nvngx_dlss_sdk;$(SolutionDir)external\xess\inc\xess;$(SolutionDir)external\FidelityFX-SDK\ffx-api\include\ffx_api;$(SolutionDir)external\simpleini;$(SolutionDir)external\unordered_dense\include;$(SolutionDir)external\spdlog\include;$(SolutionDir)external\freetype;$(IncludePath) $(ProjectDir)fsr2\lib;$(ProjectDir)fsr2_212\lib;$(ProjectDir)fsr31\lib;$(ProjectDir)vulkan;$(ProjectDir)d3dx;$(ProjectDir)detours;$(SolutionDir)external\xess\lib;$(SolutionDir)external\freetype;$(LibraryPath) dxgi - G:\The Last of Us Part I\ + D:\Folders\Games\Banishers Ghosts of New Eden\NWD\Binaries\Win64\ .\x64\Debug .dll diff --git a/OptiScaler/backends/fsr31/FSR31Feature_Dx12.h b/OptiScaler/backends/fsr31/FSR31Feature_Dx12.h index e4ed3376..c3380a54 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature_Dx12.h +++ b/OptiScaler/backends/fsr31/FSR31Feature_Dx12.h @@ -19,7 +19,7 @@ public: ~FSR31FeatureDx12() { - if(_context != nullptr) + if(!Config::Instance()->IsShuttingDown && _context != nullptr) FfxApiProxy::D3D12_DestroyContext()(&_context, NULL); } }; \ No newline at end of file