Is there a way to skip (or optimize) RenderTexture.GrabPixels when implementing an image effect?

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?

What are the clear flags on your camera?

Hi @jvo3dc , thanks for replying. So, it currently is set as Depth Only precisely because I tried to follow that link (note that it’s the same I myself had referenced in the question above). But I also tried other settings, like solid color (which would be the ideal since most of the background is one color, but I can do that directly in the shader too) or " don’t clear".

However, none of these setting seem to work: nothing appears on the screen and RenderTexture.GrabPixels is still being executed no matter if the camera is set to render-to-texture and its RenderTexture is directly passed to the image effect script.

The point of that link is that Depth Only leads to reduced performance. So you should remove the RenderTarget and use the standard image effect workflow. Then make sure the clear flags are not Depth Only and then there should be no need to optimize any further.