diff --git a/FSR2UnityPlugin.cpp b/FSR2UnityPlugin.cpp index f01112e..a4e3b4d 100644 --- a/FSR2UnityPlugin.cpp +++ b/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; - - s_GraphicsD3D12 = s_UnityInterfaces->Get(); - if (s_GraphicsD3D12 == nullptr) + switch (s_RendererType) { - UNITY_LOG_ERROR(s_Log, "Could not obtain D3D12 Graphics interface!"); - return; + case kUnityGfxRendererD3D12: + { + s_GraphicsD3D12 = s_UnityInterfaces->Get(); + if (s_GraphicsD3D12 == nullptr) + { + UNITY_LOG_ERROR(s_Log, "Could not obtain D3D12 Graphics interface!"); + return; + } + break; } + }; break; } @@ -132,20 +136,21 @@ static void FreeFeatureSlot(uint32_t featureSlot) 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; + if (s_GraphicsD3D12 != nullptr) + { + ID3D12Device* device = s_GraphicsD3D12->GetDevice(); + if (device == nullptr) + return false; - s_Device = ffxGetDeviceDX12(device); + s_Device = ffxGetDeviceDX12(device); - size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeDX12(); - void* scratchBuffer = malloc(scratchBufferSize); - ffxFsr2GetInterfaceDX12(&s_Fsr2Interface, device, scratchBuffer, scratchBufferSize); + size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeDX12(); + void* scratchBuffer = malloc(scratchBufferSize); + ffxFsr2GetInterfaceDX12(&s_Fsr2Interface, device, scratchBuffer, scratchBufferSize); + return true; + } - return true; + return false; } extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_ShutdownApi() @@ -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,17 +260,20 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) 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); + if (s_GraphicsD3D12 != nullptr) + { + 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; @@ -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; } }