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:
{
s_RendererType = s_Graphics->GetRenderer();
if (s_RendererType != kUnityGfxRendererD3D12)
return;
switch (s_RendererType)
{
case kUnityGfxRendererD3D12:
{
s_GraphicsD3D12 = s_UnityInterfaces->Get<IUnityGraphicsD3D12v7>();
if (s_GraphicsD3D12 == nullptr)
{
UNITY_LOG_ERROR(s_Log, "Could not obtain D3D12 Graphics interface!");
return;
}
break;
}
};
break;
}
@ -132,9 +136,8 @@ static void FreeFeatureSlot(uint32_t featureSlot)
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();
if (device == nullptr)
return false;
@ -144,10 +147,12 @@ extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_InitApi()
size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeDX12();
void* scratchBuffer = malloc(scratchBufferSize);
ffxFsr2GetInterfaceDX12(&s_Fsr2Interface, device, scratchBuffer, scratchBufferSize);
return true;
}
return false;
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_ShutdownApi()
{
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
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
{
if (s_GraphicsD3D12 == nullptr || s_Device == nullptr)
if (s_Device == nullptr)
return;
// User rendering code
@ -255,6 +260,8 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
FfxFsr2DispatchDescription dispatchDescription{};
if (s_GraphicsD3D12 != nullptr)
{
UnityGraphicsD3D12RecordingState state;
s_GraphicsD3D12->CommandRecordingState(&state);
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.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;
@ -323,9 +331,6 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
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);
@ -351,9 +356,15 @@ static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data)
}
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
// 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;
}
}

Loading…
Cancel
Save