You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
360 lines
12 KiB
360 lines
12 KiB
#include <stdint.h>
|
|
|
|
#include <vector>
|
|
#include <queue>
|
|
#include <mutex>
|
|
|
|
#include "UnityPluginAPI/IUnityInterface.h"
|
|
#include "UnityPluginAPI/IUnityLog.h"
|
|
#include "UnityPluginAPI/IUnityGraphics.h"
|
|
#include "UnityPluginAPI/IUnityRenderingExtensions.h"
|
|
|
|
#include <d3d12.h>
|
|
#include <dxgi.h>
|
|
#include "UnityPluginAPI/IUnityGraphicsD3D12.h"
|
|
|
|
#include "ffx-fsr2-api/ffx_fsr2.h"
|
|
#include "ffx-fsr2-api/dx12/ffx_fsr2_dx12.h"
|
|
|
|
#include "FSR3UnityTypes.h"
|
|
|
|
static const int32_t BaseEventId = 0;
|
|
|
|
static IUnityInterfaces* s_UnityInterfaces = nullptr;
|
|
static IUnityLog* s_Log = nullptr;
|
|
static IUnityGraphics* s_Graphics = nullptr;
|
|
static IUnityGraphicsD3D12v7* s_GraphicsD3D12 = nullptr;
|
|
static UnityGfxRenderer s_RendererType = kUnityGfxRendererNull;
|
|
|
|
static FfxDevice s_Device = nullptr;
|
|
static FfxFsr2Interface s_Fsr2Interface;
|
|
|
|
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);
|
|
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data);
|
|
static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data);
|
|
|
|
struct FSR3Feature
|
|
{
|
|
FfxFsr2Context upscalingContext;
|
|
uint32_t flags;
|
|
bool isValid;
|
|
|
|
FSR3TextureTable textureTable;
|
|
};
|
|
|
|
static std::vector<FSR3Feature> s_Features;
|
|
static std::queue<uint32_t> s_FeatureSlots;
|
|
static std::mutex s_FeatureMutex;
|
|
|
|
// Unity plugin load event
|
|
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
|
|
{
|
|
s_UnityInterfaces = unityInterfaces;
|
|
s_Log = unityInterfaces->Get<IUnityLog>();
|
|
s_Graphics = unityInterfaces->Get<IUnityGraphics>();
|
|
|
|
s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
|
|
|
|
// Run OnGraphicsDeviceEvent(initialize) manually on plugin load
|
|
// to not miss the event in case the graphics device is already initialized
|
|
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
|
|
}
|
|
|
|
// Unity plugin unload event
|
|
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
|
|
{
|
|
s_Graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);
|
|
}
|
|
|
|
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType)
|
|
{
|
|
switch (eventType)
|
|
{
|
|
case kUnityGfxDeviceEventInitialize:
|
|
{
|
|
s_RendererType = s_Graphics->GetRenderer();
|
|
if (s_RendererType != kUnityGfxRendererD3D12)
|
|
return;
|
|
|
|
s_GraphicsD3D12 = s_UnityInterfaces->Get<IUnityGraphicsD3D12v7>();
|
|
if (s_GraphicsD3D12 == nullptr)
|
|
{
|
|
UNITY_LOG_ERROR(s_Log, "Could not obtain D3D12 Graphics interface!");
|
|
return;
|
|
}
|
|
|
|
break;
|
|
}
|
|
case kUnityGfxDeviceEventShutdown:
|
|
{
|
|
s_GraphicsD3D12 = nullptr;
|
|
s_RendererType = kUnityGfxRendererNull;
|
|
break;
|
|
}
|
|
case kUnityGfxDeviceEventBeforeReset:
|
|
{
|
|
break;
|
|
}
|
|
case kUnityGfxDeviceEventAfterReset:
|
|
{
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
|
|
// Thread-safe allocation of feature slot
|
|
static uint32_t AllocateFeatureSlot()
|
|
{
|
|
std::lock_guard<std::mutex> lock(s_FeatureMutex);
|
|
|
|
if (s_FeatureSlots.empty())
|
|
{
|
|
// Create a new feature if there are no free slots
|
|
uint32_t featureSlot = (uint32_t)s_Features.size();
|
|
s_Features.push_back(std::move(FSR3Feature()));
|
|
return featureSlot;
|
|
}
|
|
|
|
// Reallocate an existing free slot
|
|
uint32_t featureSlot = s_FeatureSlots.front();
|
|
s_FeatureSlots.pop();
|
|
return featureSlot;
|
|
}
|
|
|
|
// Thread-safe freeing and clearing of feature slot
|
|
static void FreeFeatureSlot(uint32_t featureSlot)
|
|
{
|
|
std::lock_guard<std::mutex> lock(s_FeatureMutex);
|
|
|
|
s_FeatureSlots.push(featureSlot);
|
|
memset(&s_Features[featureSlot], 0, sizeof(FSR3Feature));
|
|
}
|
|
|
|
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_InitApi()
|
|
{
|
|
if (s_GraphicsD3D12 == nullptr)
|
|
return false;
|
|
|
|
ID3D12Device* device = s_GraphicsD3D12->GetDevice();
|
|
if (device == nullptr)
|
|
return false;
|
|
|
|
s_Device = ffxGetDeviceDX12(device);
|
|
|
|
size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeDX12();
|
|
void* scratchBuffer = malloc(scratchBufferSize);
|
|
ffxFsr2GetInterfaceDX12(&s_Fsr2Interface, device, scratchBuffer, scratchBufferSize);
|
|
|
|
return true;
|
|
}
|
|
|
|
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_ShutdownApi()
|
|
{
|
|
for (uint32_t slot = 0; slot < s_Features.size(); ++slot)
|
|
{
|
|
auto& feature = s_Features[slot];
|
|
if (feature.isValid)
|
|
{
|
|
ffxFsr2ContextDestroy(&feature.upscalingContext);
|
|
FreeFeatureSlot(slot);
|
|
}
|
|
}
|
|
|
|
if (s_Fsr2Interface.scratchBuffer != nullptr)
|
|
{
|
|
free(s_Fsr2Interface.scratchBuffer);
|
|
s_Fsr2Interface.scratchBuffer = nullptr;
|
|
}
|
|
|
|
s_Device = nullptr;
|
|
}
|
|
|
|
extern "C" uint32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetDeviceVersion()
|
|
{
|
|
return 0x0u;
|
|
}
|
|
|
|
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetRenderEventCallback()
|
|
{
|
|
return OnRenderEventAndData;
|
|
}
|
|
|
|
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetSetTextureEventCallback()
|
|
{
|
|
return OnSetTextureEvent;
|
|
}
|
|
|
|
extern "C" uint32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_CreateFeatureSlot()
|
|
{
|
|
return AllocateFeatureSlot();
|
|
}
|
|
|
|
extern "C" float UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetUpscaleRatioFromQualityMode(FSR3Quality qualityMode)
|
|
{
|
|
switch (qualityMode)
|
|
{
|
|
case FSR3Quality::qQuality:
|
|
return 1.5f;
|
|
case FSR3Quality::qBalanced:
|
|
return 1.7f;
|
|
case FSR3Quality::qPerformance:
|
|
return 2.0f;
|
|
case FSR3Quality::qUltraPerformance:
|
|
return 3.0f;
|
|
default:
|
|
return 1.0f;
|
|
}
|
|
}
|
|
|
|
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetRenderResolutionFromQualityMode(FSR3Quality qualityMode, uint32_t displayWidth, uint32_t displayHeight, uint32_t* renderWidth, uint32_t* renderHeight)
|
|
{
|
|
if (renderWidth == nullptr || renderHeight == nullptr)
|
|
return false;
|
|
|
|
float ratio = AMDUP_GetUpscaleRatioFromQualityMode(qualityMode);
|
|
*renderWidth = (uint32_t)roundf(displayWidth / ratio);
|
|
*renderHeight = (uint32_t)roundf(displayHeight / ratio);
|
|
return true;
|
|
}
|
|
|
|
extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetBaseEventId()
|
|
{
|
|
return BaseEventId;
|
|
}
|
|
|
|
// Plugin function to handle a specific rendering event
|
|
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
|
|
{
|
|
if (s_GraphicsD3D12 == nullptr || s_Device == nullptr)
|
|
return;
|
|
|
|
// User rendering code
|
|
switch (eventID)
|
|
{
|
|
case BaseEventId + FSR3PluginEvent::eDestroyFeature:
|
|
{
|
|
uint32_t featureSlot = (uint32_t)(int64_t)data;
|
|
if (featureSlot < 0 || featureSlot >= s_Features.size())
|
|
return;
|
|
|
|
auto& feature = s_Features[featureSlot];
|
|
if (feature.isValid)
|
|
{
|
|
ffxFsr2ContextDestroy(&feature.upscalingContext);
|
|
FreeFeatureSlot(featureSlot);
|
|
}
|
|
break;
|
|
}
|
|
case BaseEventId + FSR3PluginEvent::eExecute:
|
|
{
|
|
auto* params = (FSR3CommandExecutionData*)data;
|
|
if (params->featureSlot < 0 || params->featureSlot >= s_Features.size())
|
|
return;
|
|
|
|
auto& feature = s_Features[params->featureSlot];
|
|
|
|
FfxFsr2DispatchDescription dispatchDescription{};
|
|
|
|
UnityGraphicsD3D12RecordingState state;
|
|
s_GraphicsD3D12->CommandRecordingState(&state);
|
|
dispatchDescription.commandList = ffxGetCommandListDX12(state.commandList);
|
|
|
|
dispatchDescription.color = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.colorInput);
|
|
dispatchDescription.depth = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.depth);
|
|
dispatchDescription.motionVectors = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.motionVectors);
|
|
dispatchDescription.exposure = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.exposureTexture);
|
|
dispatchDescription.reactive = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.reactiveMask);
|
|
dispatchDescription.transparencyAndComposition = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.transparencyMask);
|
|
dispatchDescription.output = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.colorOutput, nullptr, FFX_RESOURCE_STATE_UNORDERED_ACCESS);
|
|
|
|
dispatchDescription.jitterOffset.x = params->jitterOffsetX;
|
|
dispatchDescription.jitterOffset.y = params->jitterOffsetY;
|
|
dispatchDescription.motionVectorScale.x = params->MVScaleX;
|
|
dispatchDescription.motionVectorScale.y = params->MVScaleY;
|
|
dispatchDescription.reset = params->reset;
|
|
dispatchDescription.enableSharpening = params->enableSharpening;
|
|
dispatchDescription.sharpness = params->sharpness;
|
|
dispatchDescription.frameTimeDelta = params->frameTimeDelta;
|
|
dispatchDescription.preExposure = params->preExposure;
|
|
dispatchDescription.renderSize.width = params->renderSizeWidth;
|
|
dispatchDescription.renderSize.height = params->renderSizeHeight;
|
|
dispatchDescription.cameraFovAngleVertical = params->cameraFovAngleVertical;
|
|
|
|
if (feature.flags & FFX_FSR2_ENABLE_DEPTH_INVERTED)
|
|
{
|
|
dispatchDescription.cameraFar = params->cameraNear;
|
|
dispatchDescription.cameraNear = params->cameraFar;
|
|
}
|
|
else
|
|
{
|
|
dispatchDescription.cameraFar = params->cameraFar;
|
|
dispatchDescription.cameraNear = params->cameraNear;
|
|
}
|
|
|
|
ffxFsr2ContextDispatch(&feature.upscalingContext, &dispatchDescription);
|
|
break;
|
|
}
|
|
case BaseEventId + FSR3PluginEvent::ePostExecute:
|
|
{
|
|
auto* params = (FSR3CommandExecutionData*)data;
|
|
break;
|
|
}
|
|
case BaseEventId + FSR3PluginEvent::eInit:
|
|
{
|
|
auto* params = (FSR3CommandInitializationData*)data;
|
|
if (params->featureSlot < 0 || params->featureSlot >= s_Features.size())
|
|
return;
|
|
|
|
auto& feature = s_Features[params->featureSlot];
|
|
feature.flags = params->flags;
|
|
|
|
FfxFsr2ContextDescription contextDescription{};
|
|
contextDescription.callbacks = s_Fsr2Interface;
|
|
contextDescription.device = s_Device;
|
|
contextDescription.displaySize = { params->displaySizeWidth, params->displaySizeHeight };
|
|
contextDescription.maxRenderSize = { params->maxRenderSizeWidth, params->maxRenderSizeHeight };
|
|
contextDescription.flags = params->flags;
|
|
|
|
feature.isValid = ffxFsr2ContextCreate(&feature.upscalingContext, &contextDescription) == FFX_OK;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data)
|
|
{
|
|
if (s_GraphicsD3D12 == nullptr)
|
|
return;
|
|
|
|
auto* params = (UnityRenderingExtTextureUpdateParamsV2*)data;
|
|
|
|
// userData = (featureId & (int) ushort.MaxValue) << 16 | (textureSlot & (int) short.MaxValue) << 1 | (clearTextureTable ? 1 : 0);
|
|
uint32_t featureSlot = (params->userData >> 16) & 0xFFFF;
|
|
uint32_t textureSlot = (params->userData >> 1) & 0x7FFF;
|
|
uint32_t clearTextureTable = params->userData & 0x1;
|
|
|
|
if (featureSlot < 0 || featureSlot >= s_Features.size())
|
|
return;
|
|
|
|
auto& feature = s_Features[featureSlot];
|
|
|
|
// User rendering code
|
|
switch (eventID)
|
|
{
|
|
case kUnityRenderingExtEventUpdateTextureBeginV2:
|
|
{
|
|
if (clearTextureTable)
|
|
{
|
|
memset(&feature.textureTable, 0, sizeof(FSR3TextureTable));
|
|
}
|
|
break;
|
|
}
|
|
case kUnityRenderingExtEventUpdateTextureEndV2:
|
|
{
|
|
// We organized the texture table struct to be ordered the same as the texture slot enum
|
|
// This way we can use the texture slot value simply as a pointer offset into the texture table
|
|
*(((intptr_t*)&feature.textureTable) + textureSlot) = (intptr_t)s_GraphicsD3D12->TextureFromNativeTexture((UnityTextureID)params->textureID);
|
|
break;
|
|
}
|
|
}
|
|
}
|