From 528558d070ecbe6f750145572f0d2919f63da255 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Fri, 11 Apr 2025 16:02:42 +0200 Subject: [PATCH] Implemented base event ID the way Unity's plugin interface actually expects you to: by reserving a range during initialization and storing the base value returned by Unity. --- src/FSR3UnityPlugin.cpp | 21 +++++++++++---------- src/FSR3UnityTypes.h | 4 +++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/FSR3UnityPlugin.cpp b/src/FSR3UnityPlugin.cpp index fe8ae1b..de7698c 100644 --- a/src/FSR3UnityPlugin.cpp +++ b/src/FSR3UnityPlugin.cpp @@ -9,8 +9,6 @@ #include "FSR3Upscaler_DX12.h" #include "FSR3Upscaler_Vulkan.h" -static const int32_t BaseEventId = 313; - static IUnityInterfaces* s_UnityInterfaces = nullptr; static IUnityLog* s_Log = nullptr; static IUnityGraphics* s_Graphics = nullptr; @@ -20,6 +18,7 @@ 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); +static int32_t s_BaseEventId = 0; static IUpscaler* s_Upscaler = nullptr; // Unity plugin load event @@ -29,6 +28,8 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnit s_Log = unityInterfaces->Get(); s_Graphics = unityInterfaces->Get(); + s_BaseEventId = s_Graphics->ReserveEventIDRange((int)ePluginEventCount); + s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent); // Run OnGraphicsDeviceEvent(initialize) manually on plugin load @@ -84,7 +85,7 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev eventConfig.graphicsQueueAccess = kUnityD3D12GraphicsQueueAccess_DontCare; eventConfig.flags = kUnityD3D12EventConfigFlag_EnsurePreviousFrameSubmission | kUnityD3D12EventConfigFlag_FlushCommandBuffers | kUnityD3D12EventConfigFlag_SyncWorkerThreads | kUnityD3D12EventConfigFlag_ModifiesCommandBuffersState; eventConfig.ensureActiveRenderTextureIsBound = false; - graphicsD3D12->ConfigureEvent(BaseEventId + FSR3PluginEvent::eExecute, &eventConfig); + graphicsD3D12->ConfigureEvent(s_BaseEventId + FSR3PluginEvent::eExecute, &eventConfig); break; } case kUnityGfxRendererVulkan: @@ -102,7 +103,7 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev eventConfig.graphicsQueueAccess = kUnityVulkanGraphicsQueueAccess_DontCare; eventConfig.flags = kUnityVulkanEventConfigFlag_EnsurePreviousFrameSubmission | kUnityVulkanEventConfigFlag_FlushCommandBuffers | kUnityVulkanEventConfigFlag_SyncWorkerThreads | kUnityVulkanEventConfigFlag_ModifiesCommandBuffersState; eventConfig.renderPassPrecondition = kUnityVulkanRenderPass_EnsureInside; - graphicsVulkan->ConfigureEvent(BaseEventId + FSR3PluginEvent::eExecute, &eventConfig); + graphicsVulkan->ConfigureEvent(s_BaseEventId + FSR3PluginEvent::eExecute, &eventConfig); break; } } @@ -204,7 +205,7 @@ extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetRenderResolu extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetBaseEventId() { - return BaseEventId; + return s_BaseEventId; } // Plugin function to handle a specific rendering event @@ -214,27 +215,27 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data) return; // User rendering code - switch (eventID) + switch (eventID - s_BaseEventId) { - case BaseEventId + FSR3PluginEvent::eDestroyFeature: + case FSR3PluginEvent::eDestroyFeature: { uint32_t featureSlot = (uint32_t)(intptr_t)data; s_Upscaler->DestroyFeature(featureSlot); break; } - case BaseEventId + FSR3PluginEvent::eExecute: + case FSR3PluginEvent::eExecute: { auto* execData = (FSR3CommandExecutionData*)data; s_Upscaler->Execute(execData); break; } - case BaseEventId + FSR3PluginEvent::ePostExecute: + case FSR3PluginEvent::ePostExecute: { auto* execData = (FSR3CommandExecutionData*)data; s_Upscaler->PostExecute(execData); break; } - case BaseEventId + FSR3PluginEvent::eInit: + case FSR3PluginEvent::eInit: { auto* initData = (FSR3CommandInitializationData*)data; s_Upscaler->InitFeature(initData); diff --git a/src/FSR3UnityTypes.h b/src/FSR3UnityTypes.h index 4f0a463..90e9972 100644 --- a/src/FSR3UnityTypes.h +++ b/src/FSR3UnityTypes.h @@ -5,7 +5,9 @@ enum FSR3PluginEvent : int32_t eDestroyFeature, eExecute, ePostExecute, - eInit + eInit, + + ePluginEventCount, }; enum FSR3Quality: int32_t