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):

Overview of native plugin interface:

The GL.IssuePluginEvent function:

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.]

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

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.

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