Browse Source

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.

fsr2
Nico de Poel 10 months ago
parent
commit
167433ba31
  1. 21
      FSR2UnityPlugin.cpp
  2. 4
      FSR2UnityTypes.h

21
FSR2UnityPlugin.cpp

@ -23,8 +23,6 @@
#include "FSR2UnityTypes.h" #include "FSR2UnityTypes.h"
static const int32_t BaseEventId = 212;
static IUnityInterfaces* s_UnityInterfaces = nullptr; static IUnityInterfaces* s_UnityInterfaces = nullptr;
static IUnityLog* s_Log = nullptr; static IUnityLog* s_Log = nullptr;
static IUnityGraphics* s_Graphics = nullptr; static IUnityGraphics* s_Graphics = nullptr;
@ -33,6 +31,7 @@ static IUnityGraphicsD3D12v7* s_GraphicsD3D12 = nullptr;
static IUnityGraphicsVulkanV2* s_GraphicsVulkan = nullptr; static IUnityGraphicsVulkanV2* s_GraphicsVulkan = nullptr;
static UnityGfxRenderer s_RendererType = kUnityGfxRendererNull; static UnityGfxRenderer s_RendererType = kUnityGfxRendererNull;
static int32_t s_BaseEventId = 0;
static FfxDevice s_Device = nullptr; static FfxDevice s_Device = nullptr;
static FfxFsr2Interface s_Fsr2Interface; static FfxFsr2Interface s_Fsr2Interface;
@ -64,6 +63,8 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnit
s_Log = unityInterfaces->Get<IUnityLog>(); s_Log = unityInterfaces->Get<IUnityLog>();
s_Graphics = unityInterfaces->Get<IUnityGraphics>(); s_Graphics = unityInterfaces->Get<IUnityGraphics>();
s_BaseEventId = s_Graphics->ReserveEventIDRange((int)ePluginEventCount);
s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent); s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
// Run OnGraphicsDeviceEvent(initialize) manually on plugin load // Run OnGraphicsDeviceEvent(initialize) manually on plugin load
@ -111,7 +112,7 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev
eventConfig.graphicsQueueAccess = kUnityD3D12GraphicsQueueAccess_DontCare; eventConfig.graphicsQueueAccess = kUnityD3D12GraphicsQueueAccess_DontCare;
eventConfig.flags = kUnityD3D12EventConfigFlag_EnsurePreviousFrameSubmission | kUnityD3D12EventConfigFlag_FlushCommandBuffers | kUnityD3D12EventConfigFlag_SyncWorkerThreads | kUnityD3D12EventConfigFlag_ModifiesCommandBuffersState; eventConfig.flags = kUnityD3D12EventConfigFlag_EnsurePreviousFrameSubmission | kUnityD3D12EventConfigFlag_FlushCommandBuffers | kUnityD3D12EventConfigFlag_SyncWorkerThreads | kUnityD3D12EventConfigFlag_ModifiesCommandBuffersState;
eventConfig.ensureActiveRenderTextureIsBound = false; eventConfig.ensureActiveRenderTextureIsBound = false;
s_GraphicsD3D12->ConfigureEvent(BaseEventId + FSR2PluginEvent::eExecute, &eventConfig);
s_GraphicsD3D12->ConfigureEvent(s_BaseEventId + FSR2PluginEvent::eExecute, &eventConfig);
break; break;
} }
case kUnityGfxRendererVulkan: case kUnityGfxRendererVulkan:
@ -127,7 +128,7 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev
eventConfig.graphicsQueueAccess = kUnityVulkanGraphicsQueueAccess_DontCare; eventConfig.graphicsQueueAccess = kUnityVulkanGraphicsQueueAccess_DontCare;
eventConfig.flags = kUnityVulkanEventConfigFlag_EnsurePreviousFrameSubmission | kUnityVulkanEventConfigFlag_FlushCommandBuffers | kUnityVulkanEventConfigFlag_SyncWorkerThreads | kUnityVulkanEventConfigFlag_ModifiesCommandBuffersState; eventConfig.flags = kUnityVulkanEventConfigFlag_EnsurePreviousFrameSubmission | kUnityVulkanEventConfigFlag_FlushCommandBuffers | kUnityVulkanEventConfigFlag_SyncWorkerThreads | kUnityVulkanEventConfigFlag_ModifiesCommandBuffersState;
eventConfig.renderPassPrecondition = kUnityVulkanRenderPass_EnsureInside; eventConfig.renderPassPrecondition = kUnityVulkanRenderPass_EnsureInside;
s_GraphicsVulkan->ConfigureEvent(BaseEventId + FSR2PluginEvent::eExecute, &eventConfig);
s_GraphicsVulkan->ConfigureEvent(s_BaseEventId + FSR2PluginEvent::eExecute, &eventConfig);
break; break;
} }
}; };
@ -360,7 +361,7 @@ extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetRenderResolu
extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetBaseEventId() extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetBaseEventId()
{ {
return BaseEventId;
return s_BaseEventId;
} }
static FfxResource GetVulkanTextureResource(UnityVulkanInstance& instance, FSR2Feature& feature, FSR2TextureDesc& texture, FfxResourceStates state = FFX_RESOURCE_STATE_COMPUTE_READ) static FfxResource GetVulkanTextureResource(UnityVulkanInstance& instance, FSR2Feature& feature, FSR2TextureDesc& texture, FfxResourceStates state = FFX_RESOURCE_STATE_COMPUTE_READ)
@ -401,9 +402,9 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
return; return;
// User rendering code // User rendering code
switch (eventID)
switch (eventID - s_BaseEventId)
{ {
case BaseEventId + FSR2PluginEvent::eDestroyFeature:
case FSR2PluginEvent::eDestroyFeature:
{ {
uint32_t featureSlot = (uint32_t)(int64_t)data; uint32_t featureSlot = (uint32_t)(int64_t)data;
if (featureSlot < 0 || featureSlot >= s_Features.size()) if (featureSlot < 0 || featureSlot >= s_Features.size())
@ -412,7 +413,7 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
DestroyFeature(featureSlot); DestroyFeature(featureSlot);
break; break;
} }
case BaseEventId + FSR2PluginEvent::eExecute:
case FSR2PluginEvent::eExecute:
{ {
std::lock_guard<std::mutex> lock(s_FeatureMutex); std::lock_guard<std::mutex> lock(s_FeatureMutex);
@ -512,7 +513,7 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
ffxFsr2ContextDispatch(&feature.upscalingContext, &dispatchDescription); ffxFsr2ContextDispatch(&feature.upscalingContext, &dispatchDescription);
break; break;
} }
case BaseEventId + FSR2PluginEvent::ePostExecute:
case FSR2PluginEvent::ePostExecute:
{ {
std::lock_guard<std::mutex> lock(s_FeatureMutex); std::lock_guard<std::mutex> lock(s_FeatureMutex);
@ -536,7 +537,7 @@ static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
break; break;
} }
case BaseEventId + FSR2PluginEvent::eInit:
case FSR2PluginEvent::eInit:
{ {
std::lock_guard<std::mutex> lock(s_FeatureMutex); std::lock_guard<std::mutex> lock(s_FeatureMutex);

4
FSR2UnityTypes.h

@ -5,7 +5,9 @@ enum FSR2PluginEvent : int32_t
eDestroyFeature, eDestroyFeature,
eExecute, eExecute,
ePostExecute, ePostExecute,
eInit
eInit,
ePluginEventCount
}; };
enum FSR2Quality: int32_t enum FSR2Quality: int32_t

Loading…
Cancel
Save