255 lines
6.6 KiB
C++
Raw Normal View History

2024-11-01 16:55:46 +08:00
// Example low level rendering Unity plugin
#include "PlatformBase.h"
#include "RenderAPI.h"
#include "IUnityLog.h"
#include "IUnityRenderingExtensions.h"
#include <assert.h>
#include <math.h>
#include <vector>
// --------------------------------------------------------------------------
// UnitySetInterfaces
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);
2024-11-22 12:09:31 +08:00
static IUnityInterfaces *s_unity_interfaces = NULL;
static IUnityGraphics *s_graphics = NULL;
static IUnityLog *s_unity_logptr = nullptr;
2024-11-01 16:55:46 +08:00
2024-11-22 12:09:31 +08:00
void unityLog(const char *msg)
2024-11-01 16:55:46 +08:00
{
2024-11-22 12:09:31 +08:00
UNITY_LOG(s_unity_logptr, msg);
2024-11-01 16:55:46 +08:00
}
2024-11-22 12:09:31 +08:00
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces *unityInterfaces)
2024-11-01 16:55:46 +08:00
{
2024-11-22 12:09:31 +08:00
s_unity_interfaces = unityInterfaces;
s_graphics = s_unity_interfaces->Get<IUnityGraphics>();
s_graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
s_unity_logptr = s_unity_interfaces->Get<IUnityLog>();
unityLog("UnityPluginLoad");
2024-11-01 16:55:46 +08:00
#if SUPPORT_VULKAN
2024-11-22 12:09:31 +08:00
if (s_graphics->GetRenderer() == kUnityGfxRendererNull)
2024-11-01 16:55:46 +08:00
{
2024-11-22 12:09:31 +08:00
extern void RenderAPI_Vulkan_OnPluginLoad(IUnityInterfaces *);
2024-11-01 16:55:46 +08:00
RenderAPI_Vulkan_OnPluginLoad(unityInterfaces);
}
#endif // SUPPORT_VULKAN
// Run OnGraphicsDeviceEvent(initialize) manually on plugin load
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
{
2024-11-22 12:09:31 +08:00
s_graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);
2024-11-01 16:55:46 +08:00
}
#if UNITY_WEBGL
2024-11-22 12:09:31 +08:00
typedef void(UNITY_INTERFACE_API *PluginLoadFunc)(IUnityInterfaces *unityInterfaces);
typedef void(UNITY_INTERFACE_API *PluginUnloadFunc)();
2024-11-01 16:55:46 +08:00
2024-11-22 12:09:31 +08:00
extern "C" void UnityRegisterRenderingPlugin(PluginLoadFunc loadPlugin, PluginUnloadFunc unloadPlugin);
2024-11-01 16:55:46 +08:00
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API RegisterPlugin()
{
UnityRegisterRenderingPlugin(UnityPluginLoad, UnityPluginUnload);
}
#endif
// --------------------------------------------------------------------------
// GraphicsDeviceEvent
2024-11-22 12:09:31 +08:00
static RenderAPI *s_current_api = NULL;
static UnityGfxRenderer s_device_type = kUnityGfxRendererNull;
2024-11-01 16:55:46 +08:00
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType)
{
// Create graphics API implementation upon initialization
if (eventType == kUnityGfxDeviceEventInitialize)
{
2024-11-22 12:09:31 +08:00
assert(s_current_api == NULL);
2024-12-20 16:10:04 +08:00
unityLog("kUnityGfxDeviceEventInitialize");
2024-11-22 12:09:31 +08:00
s_device_type = s_graphics->GetRenderer();
s_current_api = createRenderAPI(s_device_type);
2024-11-01 16:55:46 +08:00
}
// Let the implementation process the device related events
2024-11-22 12:09:31 +08:00
if (s_current_api)
2024-11-01 16:55:46 +08:00
{
2024-11-22 12:09:31 +08:00
s_current_api->processDeviceEvent(eventType, s_unity_interfaces);
2024-11-01 16:55:46 +08:00
}
// Cleanup graphics API implementation upon shutdown
if (eventType == kUnityGfxDeviceEventShutdown)
{
2024-11-22 12:09:31 +08:00
delete s_current_api;
s_current_api = NULL;
s_device_type = kUnityGfxRendererNull;
2024-11-01 16:55:46 +08:00
}
}
// --------------------------------------------------------------------------
// OnRenderEvent
// This will be called for GL.IssuePluginEvent script calls; eventID will
// be the integer passed to IssuePluginEvent. In this example, we just ignore
// that value.
enum NativeRenderingEvent
{
EnableVRS = 1,
DisableVRS,
2024-11-22 14:45:23 +08:00
EnableFGExtrapolation,
PreFGExtrapolation,
2024-11-22 12:09:31 +08:00
DoFGExtrapolation,
2024-11-22 14:45:23 +08:00
PostFGExtrapolation,
DisableFGExtrapolation,
2024-12-17 20:50:36 +08:00
SpatialUpScale,
2024-12-12 17:41:33 +08:00
EnableXESS1,
DoXESS1,
UpdateXESS1Config,
DisableXESS1,
2024-11-01 16:55:46 +08:00
};
2024-11-22 12:09:31 +08:00
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void *data)
2024-11-01 16:55:46 +08:00
{
2024-12-20 16:10:04 +08:00
// unityLog("OnRenderEventAndData0");
2024-11-01 16:55:46 +08:00
// Unknown / unsupported graphics device type? Do nothing
2024-11-22 12:09:31 +08:00
if (s_current_api == NULL)
2024-11-01 16:55:46 +08:00
return;
switch ((NativeRenderingEvent)eventID)
{
case NativeRenderingEvent::EnableVRS:
{
2024-12-18 17:53:48 +08:00
s_current_api->enableVRS(data);
2024-11-01 16:55:46 +08:00
break;
}
case NativeRenderingEvent::DisableVRS:
{
2024-11-22 12:09:31 +08:00
s_current_api->disableVRS();
2024-11-01 16:55:46 +08:00
break;
}
2024-11-22 14:45:23 +08:00
case NativeRenderingEvent::EnableFGExtrapolation:
{
2024-12-11 17:42:08 +08:00
s_current_api->enableFGExtrapolation(data);
2024-11-22 14:45:23 +08:00
break;
}
case NativeRenderingEvent::DisableFGExtrapolation:
{
break;
}
case NativeRenderingEvent::PreFGExtrapolation:
{
break;
}
case NativeRenderingEvent::PostFGExtrapolation:
{
break;
}
2024-12-17 20:50:36 +08:00
case NativeRenderingEvent::SpatialUpScale:
{
s_current_api->spatialUpScale(data);
break;
}
2024-11-22 12:09:31 +08:00
case NativeRenderingEvent::DoFGExtrapolation:
{
2024-11-22 14:45:23 +08:00
AFMEParam *param = (AFMEParam *)data;
2024-11-22 12:09:31 +08:00
s_current_api->doFGExtrapolation(param->src, param->data, param->dst);
2024-11-22 14:45:23 +08:00
break;
2024-11-22 12:09:31 +08:00
}
2024-12-12 17:41:33 +08:00
case NativeRenderingEvent::EnableXESS1:
{
s_current_api->enableXESS1(data);
unityLog("enableXESS1");
2024-12-12 17:41:33 +08:00
break;
}
case NativeRenderingEvent::DisableXESS1:
{
unityLog("disableXESS0");
2024-12-12 17:41:33 +08:00
s_current_api->disableXESS1();
unityLog("disableXESS1");
2024-12-12 17:41:33 +08:00
break;
}
case NativeRenderingEvent::UpdateXESS1Config:
{
unityLog("configXESS1 0");
2024-12-12 17:41:33 +08:00
s_current_api->configXESS1(data);
unityLog("configXESS1 1");
2024-12-12 17:41:33 +08:00
break;
}
case NativeRenderingEvent::DoXESS1:
{
s_current_api->doXESS1(data);
break;
}
2024-11-01 16:55:46 +08:00
default:
break;
}
}
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventAndDataFunc()
{
return OnRenderEventAndData;
}
2024-11-22 12:09:31 +08:00
extern "C" UNITY_INTERFACE_EXPORT bool GetFeatureSupport(int feature)
{
if (s_current_api == NULL)
return false;
return s_current_api->getFeatureSupport((GraphicsFeature)feature);
}
2024-12-16 20:33:34 +08:00
extern "C" UNITY_INTERFACE_EXPORT bool GetInputResolution(uint32_t outw, uint32_t outh, int quality, uint32_t& width, uint32_t& height)
{
return s_current_api->getInputResolution(outw, outh, quality, width, height);
}
extern "C" UNITY_INTERFACE_EXPORT void SetVKPSOHook(bool enable, int blend_state_index)
{
s_current_api->SetVKPSOHook(enable, blend_state_index);
}
2024-11-01 16:55:46 +08:00
// --------------------------------------------------------------------------
// DX12 plugin specific
// --------------------------------------------------------------------------
2024-12-11 17:42:08 +08:00
//extern "C" UNITY_INTERFACE_EXPORT void *UNITY_INTERFACE_API GetRenderTexture()
//{
// return s_current_api->getRenderTexture();
//}
//
//extern "C" UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetRenderTexture(UnityRenderBuffer rb)
//{
// s_current_api->setRenderTextureResource(rb);
//}
//
//extern "C" UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsSwapChainAvailable()
//{
// return s_current_api->isSwapChainAvailable();
//}
//
//extern "C" UNITY_INTERFACE_EXPORT unsigned int UNITY_INTERFACE_API GetPresentFlags()
//{
// return s_current_api->getPresentFlags();
//}
//
//extern "C" UNITY_INTERFACE_EXPORT unsigned int UNITY_INTERFACE_API GetSyncInterval()
//{
// return s_current_api->getSyncInterval();
//}
//
//extern "C" UNITY_INTERFACE_EXPORT unsigned int UNITY_INTERFACE_API GetBackBufferWidth()
//{
// return s_current_api->getBackbufferHeight();
//}
//
//extern "C" UNITY_INTERFACE_EXPORT unsigned int UNITY_INTERFACE_API GetBackBufferHeight()
//{
// return s_current_api->getBackbufferWidth();
//}