From e58e032860b227d5982b7cd7cf86a4960969d719 Mon Sep 17 00:00:00 2001 From: AnimatorJeroen <37904600+AnimatorJeroen@users.noreply.github.com> Date: Sun, 3 Mar 2024 01:09:42 +0100 Subject: [PATCH] Update RenderingPlugin.cpp With this minor change, the example project becomes compatible with using mcpiroman's dynamic dll loading unity package. The problem was that there is no handle to the active OpenGL context during OnPluginLoad, when using the dynamic loading package. With this minor adjustment, the renderer initialization is postponed to the first OnRenderEvent(), where there is a valid GL Context. This way the openGL profile will run in combination with mcpiroman's plugin, making it possible to change the dll without restarting Unity. --- PluginSource/source/RenderingPlugin.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/PluginSource/source/RenderingPlugin.cpp b/PluginSource/source/RenderingPlugin.cpp index 6b1c577..a16a75e 100644 --- a/PluginSource/source/RenderingPlugin.cpp +++ b/PluginSource/source/RenderingPlugin.cpp @@ -105,7 +105,9 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnit #endif // SUPPORT_VULKAN // Run OnGraphicsDeviceEvent(initialize) manually on plugin load +#ifndef DEBUG //postponed to OnRenderEvent in debugMode, to have a valid GL_Context when dynamically loading the dll. Please make sure DEBUG preprocessor macro is defined in Debug configuration. OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize); +#endif } extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload() @@ -132,15 +134,17 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API RegisterPlugin() static RenderAPI* s_CurrentAPI = NULL; static UnityGfxRenderer s_DeviceType = kUnityGfxRendererNull; - +static bool s_GraphicsInitialized = false; static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType) { + // Create graphics API implementation upon initialization if (eventType == kUnityGfxDeviceEventInitialize) { assert(s_CurrentAPI == NULL); s_DeviceType = s_Graphics->GetRenderer(); s_CurrentAPI = CreateRenderAPI(s_DeviceType); + s_GraphicsInitialized = true; } // Let the implementation process the device related events @@ -185,7 +189,7 @@ static void DrawColoredTriangle() }; // Transformation matrix: rotate around Z axis based on time. - float phi = g_Time; // time set externally from Unity script + float phi = g_Time * 2.0f; // time set externally from Unity script float cosPhi = cosf(phi); float sinPhi = sinf(phi); float depth = 0.7f; @@ -303,10 +307,14 @@ static void drawToRenderTexture() static void UNITY_INTERFACE_API OnRenderEvent(int eventID) { +#ifdef DEBUG //edit to make it possible to dynamically load the dll. + if (!s_GraphicsInitialized) + OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize); +#endif // Unknown / unsupported graphics device type? Do nothing if (s_CurrentAPI == NULL) return; - + if (eventID == 1) { drawToRenderTexture();