diff --git a/FSR3UnityPlugin.cpp b/FSR3UnityPlugin.cpp index 4ce8f14..fbf4074 100644 --- a/FSR3UnityPlugin.cpp +++ b/FSR3UnityPlugin.cpp @@ -13,6 +13,9 @@ #include #include "UnityPluginAPI/IUnityGraphicsD3D12.h" +#include "ffx_api/ffx_upscale.hpp" +#include "ffx_api/dx12/ffx_api_dx12.hpp" + #include "FSR3UnityTypes.h" static const int32_t BaseEventId = 0; @@ -27,15 +30,18 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data); static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data); -struct FSR3Context +struct FSR3Feature { - // TODO: contains FSR3 context object + related metadata (texture table etc) + ffx::Context upscalingContext; + FSR3TextureTable textureTable; }; -static std::vector s_Contexts; +static std::vector s_Features; static std::queue s_FeatureSlots; static std::mutex s_FeatureMutex; +static ffx::CreateBackendDX12Desc s_BackendDesc{}; + // Unity plugin load event extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces) { @@ -92,15 +98,15 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev }; } -// Thread-safe allocation of FSR3 Context feature slot +// Thread-safe allocation of FSR3 feature slot static uint32_t AllocateFeatureSlot() { std::lock_guard lock(s_FeatureMutex); if (s_FeatureSlots.empty()) { - uint32_t featureSlot = (uint32_t)s_Contexts.size(); - s_Contexts.push_back(std::move(FSR3Context())); + uint32_t featureSlot = (uint32_t)s_Features.size(); + s_Features.push_back(std::move(FSR3Feature())); return featureSlot; } @@ -109,24 +115,32 @@ static uint32_t AllocateFeatureSlot() return featureSlot; } -// Thread-safe freeing and clearing of FSR3 Context feature slot +// Thread-safe freeing and clearing of FSR3 feature slot static void FreeFeatureSlot(uint32_t featureSlot) { std::lock_guard lock(s_FeatureMutex); s_FeatureSlots.push(featureSlot); - memset(&s_Contexts[featureSlot], 0, sizeof(FSR3Context)); + memset(&s_Features[featureSlot], 0, sizeof(FSR3Feature)); } extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_InitApi() { - // TODO: implement - return false; + if (s_GraphicsD3D12 == nullptr) + return false; + + ID3D12Device* device = s_GraphicsD3D12->GetDevice(); + if (device == nullptr) + return false; + + s_BackendDesc.device = device; + return true; } extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_ShutdownApi() { - // TODO: implement + // TODO: destroy any remaining FSR3Contexts? + s_BackendDesc.device = nullptr; } extern "C" uint32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetDeviceVersion() @@ -191,7 +205,21 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) } case BaseEventId + FSR3PluginEvent::eInit: { + if (s_BackendDesc.device == nullptr) + return; + auto* params = (FSR3CommandInitializationData*)data; + if (params->featureSlot < 0 || params->featureSlot >= s_Features.size()) + return; + + auto& feature = s_Features[params->featureSlot]; + + ffx::CreateContextDescUpscale createUpscaling; + createUpscaling.maxUpscaleSize = { params->displaySizeWidth, params->displaySizeHeight }; + createUpscaling.maxRenderSize = { params->maxRenderSizeWidth, params->maxRenderSizeHeight }; + createUpscaling.flags = params->flags; + + ffx::CreateContext(feature.upscalingContext, nullptr, createUpscaling, s_BackendDesc); break; } } @@ -205,6 +233,8 @@ static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data) uint32_t textureSlot = (params->userData >> 1) & 0x7FFF; // TODO: could just use as numeric index to calculate a pointer offset uint32_t clearTextureTable = params->userData & 0x1; + // TODO: should be able to just cast params->textureID directly to a D3D12Resource* + // User rendering code switch (eventID) { diff --git a/FSR3UnityPlugin.vcxproj b/FSR3UnityPlugin.vcxproj index 06c8bc0..8f4928e 100644 --- a/FSR3UnityPlugin.vcxproj +++ b/FSR3UnityPlugin.vcxproj @@ -123,6 +123,7 @@ Windows true false + $(CoreLibraryDependencies);%(AdditionalDependencies);lib\amd_fidelityfx_dx12.lib @@ -142,6 +143,7 @@ true true false + $(CoreLibraryDependencies);%(AdditionalDependencies);lib\amd_fidelityfx_dx12.lib diff --git a/FSR3UnityTypes.h b/FSR3UnityTypes.h index 60d9426..3219108 100644 --- a/FSR3UnityTypes.h +++ b/FSR3UnityTypes.h @@ -56,3 +56,15 @@ struct FSR3CommandExecutionData float cameraFovAngleVertical; uint32_t featureSlot; }; + +struct FSR3TextureTable +{ + intptr_t colorInput; + intptr_t colorOutput; + intptr_t depth; + intptr_t motionVectors; + intptr_t transparencyMask; + intptr_t exposureTexture; + intptr_t reactiveMask; + intptr_t biasColorMask; +};