|
|
|
@ -13,6 +13,9 @@ |
|
|
|
#include <dxgi.h>
|
|
|
|
#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<FSR3Context> s_Contexts; |
|
|
|
static std::vector<FSR3Feature> s_Features; |
|
|
|
static std::queue<uint32_t> 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<std::mutex> 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<std::mutex> 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) |
|
|
|
{ |
|
|
|
|