Skip to content

Update RenderingPlugin.cpp: postponed initialization to OnRenderEvent() to avoid invalid GL Context handle #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions PluginSource/source/RenderingPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down