I have finished implementing an image effect, but now need to optimize it. I have managed to make the effect’s code pretty fast, so at this moment the profiler shows me that RenderTexture.GrabPixels is actually eating more of my GPU performance than the image effect shader itself.
My current setting is the following. I have two perspective cameras. The effect goes only at the one that renders the background. It is currently set to render a solid red-ish color on screen. I capture the camera in the image effect code like the following:
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
(...) //here I set some floats and other properties of the shader, but I skip this part of the code for the sake of simplicity
Graphics.Blit(source, destination, material);
}
However, I was wondering if rendering directly from the camera to a RenderTexture only would skip the GrabPixels pass? After some search, I found this older thread: RenderTexture.GrabPixels = Low FPS [SOLVED] - Unity Engine - Unity Discussions It made seem to me that yes, that would be a good idea. Then, I tried to implement it and profile myself, but with no success: I set that camera to render to a RenderTexture, and then in the call to the Graphics.Blit function in the code above, I substituted “source” with that RenderTexture to which the camera was then set to render to:
public RenderTexture RTtowhichcamrenders;
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
(...) //here I set some floats and other properties of the shader, but I skip this part of the code for the sake of simplicity
Graphics.Blit(RTtowhichcamrenders, destination, material);
}
Nothing gets displayed on screen and, oddly enough, the RenderTexture.GrabPixels is still being executed. So, what am I doing wrong? Is there a proper way to implement the image effects in order to avoid the costly GrabPixels? Or is there a way to optimize it?