Browse Source

Isolated DX12-specific code, so that it can easily be replaced with alternative DX11 and Vulkan code.

fsr2
Nico de Poel 11 months ago
parent
commit
f4af0819c7
  1. 35
      FSR2UnityPlugin.cpp

35
FSR2UnityPlugin.cpp

@ -73,15 +73,19 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev
case kUnityGfxDeviceEventInitialize: case kUnityGfxDeviceEventInitialize:
{ {
s_RendererType = s_Graphics->GetRenderer(); s_RendererType = s_Graphics->GetRenderer();
if (s_RendererType != kUnityGfxRendererD3D12)
return;
switch (s_RendererType)
{
case kUnityGfxRendererD3D12:
{
s_GraphicsD3D12 = s_UnityInterfaces->Get<IUnityGraphicsD3D12v7>(); s_GraphicsD3D12 = s_UnityInterfaces->Get<IUnityGraphicsD3D12v7>();
if (s_GraphicsD3D12 == nullptr) if (s_GraphicsD3D12 == nullptr)
{ {
UNITY_LOG_ERROR(s_Log, "Could not obtain D3D12 Graphics interface!"); UNITY_LOG_ERROR(s_Log, "Could not obtain D3D12 Graphics interface!");
return; return;
} }
break;
}
};
break; break;
} }
@ -132,9 +136,8 @@ static void FreeFeatureSlot(uint32_t featureSlot)
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_InitApi() extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_InitApi()
{ {
if (s_GraphicsD3D12 == nullptr)
return false;
if (s_GraphicsD3D12 != nullptr)
{
ID3D12Device* device = s_GraphicsD3D12->GetDevice(); ID3D12Device* device = s_GraphicsD3D12->GetDevice();
if (device == nullptr) if (device == nullptr)
return false; return false;
@ -144,10 +147,12 @@ extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_InitApi()
size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeDX12(); size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeDX12();
void* scratchBuffer = malloc(scratchBufferSize); void* scratchBuffer = malloc(scratchBufferSize);
ffxFsr2GetInterfaceDX12(&s_Fsr2Interface, device, scratchBuffer, scratchBufferSize); ffxFsr2GetInterfaceDX12(&s_Fsr2Interface, device, scratchBuffer, scratchBufferSize);
return true; return true;
} }
return false;
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_ShutdownApi() extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_ShutdownApi()
{ {
for (uint32_t slot = 0; slot < s_Features.size(); ++slot) for (uint32_t slot = 0; slot < s_Features.size(); ++slot)
@ -225,7 +230,7 @@ extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetBaseEvent
// Plugin function to handle a specific rendering event // Plugin function to handle a specific rendering event
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
{ {
if (s_GraphicsD3D12 == nullptr || s_Device == nullptr)
if (s_Device == nullptr)
return; return;
// User rendering code // User rendering code
@ -255,6 +260,8 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
FfxFsr2DispatchDescription dispatchDescription{}; FfxFsr2DispatchDescription dispatchDescription{};
if (s_GraphicsD3D12 != nullptr)
{
UnityGraphicsD3D12RecordingState state; UnityGraphicsD3D12RecordingState state;
s_GraphicsD3D12->CommandRecordingState(&state); s_GraphicsD3D12->CommandRecordingState(&state);
dispatchDescription.commandList = ffxGetCommandListDX12(state.commandList); dispatchDescription.commandList = ffxGetCommandListDX12(state.commandList);
@ -266,6 +273,7 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
dispatchDescription.reactive = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.reactiveMask); dispatchDescription.reactive = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.reactiveMask);
dispatchDescription.transparencyAndComposition = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.transparencyMask); 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.output = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.colorOutput, nullptr, FFX_RESOURCE_STATE_UNORDERED_ACCESS);
}
dispatchDescription.jitterOffset.x = params->jitterOffsetX; dispatchDescription.jitterOffset.x = params->jitterOffsetX;
dispatchDescription.jitterOffset.y = params->jitterOffsetY; dispatchDescription.jitterOffset.y = params->jitterOffsetY;
@ -323,9 +331,6 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data) static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data)
{ {
if (s_GraphicsD3D12 == nullptr)
return;
auto* params = (UnityRenderingExtTextureUpdateParamsV2*)data; auto* params = (UnityRenderingExtTextureUpdateParamsV2*)data;
// userData = (featureId & (int) ushort.MaxValue) << 16 | (textureSlot & (int) short.MaxValue) << 1 | (clearTextureTable ? 1 : 0); // userData = (featureId & (int) ushort.MaxValue) << 16 | (textureSlot & (int) short.MaxValue) << 1 | (clearTextureTable ? 1 : 0);
@ -351,9 +356,15 @@ static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data)
} }
case kUnityRenderingExtEventUpdateTextureEndV2: case kUnityRenderingExtEventUpdateTextureEndV2:
{ {
intptr_t texturePtr = 0;
if (s_GraphicsD3D12 != nullptr)
{
texturePtr = (intptr_t)s_GraphicsD3D12->TextureFromNativeTexture((UnityTextureID)params->textureID);
}
// We organized the texture table struct to be ordered the same as the texture slot enum // 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 // 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);
*(((intptr_t*)&feature.textureTable) + textureSlot) = texturePtr;
break; break;
} }
} }

Loading…
Cancel
Save