Native rendering and broken graphics

Hi there,

While working on a plugin doing some advanced rendering, I’ve come across some issues regarding how it interferes with the regular graphics pipeline as soon as you go further than the typical triangle and vertex colors.
Currently I (well, mostly our customers using the plugin) have encountered the following issues on which I’d love to hear some insight. Stuff like workarounds or how to “do it right” (with the native rendering in the command buffers coming in 5.2, for instance).

For the sake of context, here is how we currently do our rendering :

  • Main pass is done with a GL.IssuePluginEvent in the camera’s OnPostRender method. This relies on the scene’s z-buffer to render correctly OR on the depth texture described below.
  • When enabled/necessary we get the scene’s depth with an additional offline camera (which by the way is really expensive, considering the apparent triviality of the task, if there’s a cheaper/more viable way, I’d love to hear about it). The resulting render texture is passed as a native handler to the main pass described above and the pass described below.
  • When enabled we render an additional offline camera with GL.IssuePluginEvent in the OnPostRender method. We use the RenderTexture in an image effect to render screen-space distortions. This pass requires the aforementioned depth texture.

Now, about the problems encountered:

  1. When the camera has a third-party image effect that adds an OnRenderImage method with the [ImageEffectOpaque] attribute, the scene’s z-buffer is empty when the native rendering is done.
    This is not an issue when retrieving the depth from the render texture but is when doing rendering relying on z-testing only because nothing gets culled.
    I’m not sure if this is a bug or the intended behaviour.

  2. When doing native rendering from the OnPostRender method, Unity’s lens flares just disappear.
    This happens as soon as the plugin event is issued in OnPostRender, regardless of the graphics’ state (even happens when UnityRenderEvent does nothing). This doesn’t happen if the plugin event is issued from a coroutine waiting for the end of the frame (like in the native rendering plugin sample from the documentation). It also won’t happen in OpenGL (at least on MacOSX), which lead me to think it was a bug, hence I reported it (here, case 723015 https://fogbugz.unity3d.com/default.asp?723015_qqv8uiudi9unbriq).

There’s also the problems relating to bad integration with the transparent materials (native rendering is always on top), but I believe this can be fixed using the command buffers when native rendering will be allowed in 5.2. I haven’t had the time to look at the beta yet but I will at some point. Although if someone has any clues regarding this, I’m all ears. :slight_smile:

Thanks in advance,

I may be mistaken here, but isn’t this what http://docs.unity3d.com/ScriptReference/Camera-depthTextureMode.html (which isn’t exposed in inspector) in combination with _CameraDepthTexture in shadervariables.cginc are for?
You could feed your own depth texture into the camera with http://docs.unity3d.com/ScriptReference/Camera.SetTargetBuffers.html so you can get a pointer to it I suppose.

And do I understand correctly that your rendering path looks like this?
Main camera renders → render depth if missing → (If Enabled) render other camera → plugin renders?

Can’t help much with the bugs you seem to be having, didn’t use native rendering myself yet.

Well, that only enables it in shaders, which would still require a copy with an image effect, which is expensive, too (especially on mobile, which is the reason why we stopped doing this).

Actually that’s what I’m doing. Thing is if I’m not mistaken, this works only for offline cameras (you have to setup a color render texture) this is why we have one in the first place.

It’s more like :
Depth cam renders if enabled → Main cam renders and then calls the native rendering with the depth texture available if necessary (some stuff use it, some stuff don’t) → Offline cam culling everything natively renders a distortion buffer if enabled (requires depth, too) → Image effect on main camera applies the distortion on top of everything.

Thanks for your help!