Anyone interested to test the difference betweeen OnRenderImage() & OnPreRender+OnPostRender(), here is a package to test.
using UnityEngine;
using System.Collections;
public class OnPrePostRender : MonoBehaviour
{
public Shader ImageEffectShader;
Material _mat;
Material Mat
{
get
{
if (_mat == null)
_mat = new Material(ImageEffectShader) { hideFlags = HideFlags.DontSave };
return _mat;
}
}
public enum MSAA
{
_1 = 1,
_2 = 2,
_4 = 4,
_8 = 8
}
public MSAA _MSAA;
RenderTexture mainRT;
void Start()
{
}
void OnPreRender()
{
mainRT = RenderTexture.GetTemporary(Screen.width, Screen.height, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Default, (int)_MSAA);
GetComponent<Camera>().targetTexture = mainRT;
}
void OnPostRender()
{
GetComponent<Camera>().targetTexture = null;
Graphics.Blit(mainRT, null, Mat);
RenderTexture.ReleaseTemporary(mainRT);
}
}
using UnityEngine;
using System.Collections;
public class OnRenderImageTest : MonoBehaviour
{
public Shader ImageEffectShader;
Material _mat;
Material Mat
{
get
{
if (_mat == null)
_mat = new Material(ImageEffectShader) { hideFlags = HideFlags.DontSave };
return _mat;
}
}
private void OnRenderImage(RenderTexture src,RenderTexture dest)
{
Graphics.Blit(src, dest, Mat);
}
}
Some numbers to prove the difference (MSAA on+OnRenderImage() = slow):
case1: Adreno™305, MSAAx4, SolidColor clear
OnPreRender+OnPostRender() - 60fps(16.6ms)
OnRenderImage() - 27fps(36.3ms)
case2: Adreno™305, MSAAx4, depth clear
OnPreRender+OnPostRender() - 60fps(16.6ms)
OnRenderImage() - 27fps(36.3ms)
case3: Adreno™305, MSAAx4, skybox clear
OnPreRender+OnPostRender() - 38fps(26.6ms)
OnRenderImage() - 24fps(41.3ms)
“Camera.AAResolve->Grab RenderTexture” was found in FrameDebugger in all 3 case when using OnRenderImage(), but not in OnPreRender+OnPostRender().
only the following 2 case are having idenical performance(also same actions by FrameDebugger):
MSAA off + “clear flag = solid color”
MSAA off + “clear flag = skybox”
any other cases, OnRenderImage() will be slower than OnPreRender+OnPostRender()
You need multi-thread rendering on in order to use FrameDebugger on mobile.
I will avoid OnRenderImage() from now, because I need MSAA in a mobile project.
2762498–199489–OnRenderImagePerformanceTest.unitypackage (15.8 KB)