Search Unity

Reducing Blits

Discussion in 'Image Effects' started by Samhayne, Nov 12, 2019.

  1. Samhayne

    Samhayne

    Joined:
    Jun 15, 2009
    Posts:
    45
    Hellooo,

    I'm working on post-processing effect to draw outlines around objects.
    It already works but I'd like to optimize the amount of Blit() calls.

    My problem: Blitting from source to source won't work.
    So you have to first blit to a temporary rendertexture...

    Code (CSharp):
    1. commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, tmpRenderTexture);
    2. commandBuffer.SetGlobalTexture("_OutlineTex", outlineTexture);
    3. commandBuffer.Blit(tmpRenderTexture, BuiltinRenderTextureType.CameraTarget, overlayMaterial);
    Is there a more elegant / more performant way to do this?
    Is there no way to avoid two Blit() calls?

    I'm also thinking about drawing the outline texture on a UI.RawImage to avoid both of those Blits. Hm.
     
  2. Samhayne

    Samhayne

    Joined:
    Jun 15, 2009
    Posts:
    45
    Alright.

    I'm using OnRenderImage() now to do the final Blit() showing the result.
    Not sure if this really saves performance but... it's one less Blit() in code.

    Code (CSharp):
    1. private void OnRenderImage(RenderTexture source, RenderTexture destination)
    2.     {
    3.         Shader.SetGlobalTexture("_OutlineTex", outlineTexture);
    4.         Graphics.Blit(source, destination, overlayMaterial);
    5.     }