- Home /
When can GL.IssuePluginEvent be called?
The Unity documentation mentions that GL.IssuePluginEvent could be called by the OnPostRender function of a "camera" script.
The documentation also has a link to an example of a low-level rendering plugin, and a corresponding Unity project which calls GL.IssuePluginEvent in a coroutine which waits for all drawing for the current frame to finish (by constructing a WaitForEndOfFrame yield instruction), where the coroutine is started by the Start function of a script.
The documentation does not mention any particular limitation on when a call to GL.IssuePluginEvent could be made -- and it seems reasonable to assume that such a call would simply set a flag which the drawing thread would eventually detect, at which point the drawing thread would implicitly call UnityRenderEvent in the plugin DLL -- and if that's the case, then there would seem to be no limitation at all on when GL.IssuePluginEvent could be called. Any script could call GL.IssuePluginEvent at any time. But, is this actually the case?
Specifically, I'm interested in possibly calling GL.IssuePluginEvent in the Update, LateUpdate, or OnPreRender event handlers for an arbitrary script.
Does anyone have any experience with calling GL.IssuePluginEvent from any place other than OnPostRender or a coroutine which waits for the end of the current frame? Or is there any part of the Unity documentation which explicitly mentions the acceptability of calling GL.IssuePluginEvent from other places?
Some relevant links for people who are interested in this topic:
Determine if Unity is doing multi-threaded drawing (which would require calling GL.IssuePluginEvent): http://docs.unity3d.com/ScriptReference/SystemInfo-graphicsMultiThreaded.html
Overview of native plugin interface: http://docs.unity3d.com/Manual/NativePluginInterface.html
The GL.IssuePluginEvent function: http://docs.unity3d.com/ScriptReference/GL.IssuePluginEvent.html
Source code for a native plugin which calls native drawing API functions (e.g., DX9, DX11, OpenGL), and a Unity project showing how to use the native plugin: http://docs.unity3d.com/uploads/Examples/RenderingPluginExample45.zip [This link is found on the page for the "Native Plugin Interface", and the file name seems likely to eventually change. I only cite this specific, transient URL to point out that such examples exist in this approximate location.]
I tried calling GL.IssuePluginEvent in Start(), sadly it does not work....still investigating more into this issue....
Hello,
Have you had any success getting more information regarding the behavior GLIssuePluginEvent?
I'm trying to do something similar. I'm updating a texture from a native plugin each frame. If I invoke the event at the end of the frame as demonstrating in the native plugin interface sample, a 1 frame delay is introduced. I'd like to perform the copy before rendering begins.
Would placing the GL.IssuePluginEvent in an OnPreRender function on the camera script work? Would this break if the rendering is multi-threaded?
Thanks, Joe
Answer by AngelGabriel · Mar 21, 2017 at 08:17 AM
Two years after original post, and the documentation still does not address this...
Just tried some not-at-all scientific experiments - Using Unity 5.5 on Windows. - Rendering is multi-threaded - Test is simply retrieving a web camera image and comparing timestamps (std::chrono::high_resolution_clock::now) - GL.IssuePluginEvent can be called from Update, LateUpdate, OnPreRender, OnPostRender, even in Start.
Calling seems to be required for each rendered frame. Calling at start yielded one image that never updates. Therefore, calling clearly needs to be per-rendered frame.
Comparing time stamps to determine latency from image received in plugin to image used in Unity showed NO VARIATION. In particular, the latency in the OnPostRender stage was always greater than OnPreRender, even if the GL.IssuePluginEvent was made in OnPostRender.
All of this seems like it confirms the OP's guess:
it seems reasonable to assume that such a call would simply set a flag which the drawing thread would eventually detect, at which point the drawing thread would implicitly call UnityRenderEvent in the plugin
Answer by AngelGabriel · Mar 21, 2017 at 08:17 AM
Update: when waiting until a new frame from the webcam is ready in the OnPreRender stage, the latency is minimized when GL.IssuePluginEvent is called during the LateUpdate execution stage.
Calling GL.IssuePluginEvent in OnPreRender and then waiting yields more latency. Likewise, calling GL.IssuePluginEvent at the end of frame and waiting in OnPreRender also increases latency.
While I'm not sure how to interpret that observation, it seems like it might be a good thing for others to test as well.
Answer by blossomgames · Oct 11, 2018 at 04:06 PM
My experience is different. I do some plugin rendering. Now: - when I call plugin in Update, my data gets screwed (probably my plugin code is finishing halfway through or something) - when I call plugin in OnPostRender nothing works - when I call just like the Unity's big plugin rendering example it seems fine
Your answer
Follow this Question
Related Questions
Low-level native plugin on iOS 1 Answer
Why does deferred rendering flip the view and change the viewToWorldMatrix? 0 Answers
Is it possible to have editor-only native plugins? 0 Answers
Creating a native photo picker screen on iOS 2 Answers
Why is GL.IssuePluginEvent not listed on documentation page for "GL" class? 0 Answers