Search Unity

Bug: 1 Frame Delay when Rendering to Texture

Discussion in 'General Graphics' started by xx3000, Nov 11, 2015.

  1. xx3000

    xx3000

    Joined:
    Apr 5, 2013
    Posts:
    1
    Hi!

    I have discovered a bug that I can't quite explain.

    When rendering to a renderTexture manualy ( through Camera.Render() ), there is a 1-Frame delay before the texture gets updated.

    This is quite annoying when you have multiple cameras in the scene and want to composit them for image effects.

    I have attached a minimal working example to demonstrate the bug.
    You need to use a script debugger and place 3 breakpoints at the specified areas in the script.
    When you press F1 in game the camera moves and you can see the renderTexture update frame-by-frame.

    When rendering directly to screen, the backbuffer is updated on "Frame1", opposed to rendering to image first, which takes Unity until "Frame2"

    I would realy appreciate your feedback!
     

    Attached Files:

  2. Venryx

    Venryx

    Joined:
    Sep 25, 2012
    Posts:
    444
    I get the same problem.

    In the below image-effect script, the stencilAsRedTexture is correctly passed and used in the fogMaterial render call. However, it's contents are always one frame behind the current one!

    In other words, if I move to the right quickly, the first frame with the new rotation has the fogMaterial effect applied correctly, i.e. without delay, and the fog matches with the rotated visuals; but the stencilAsRedTexture that's displayed doesn't change, still having the contents it had before the rotation (for that first frame after the rotation--and likewise, for each frame after, it's contents are always lagged one frame behind).

    Code (CSharp):
    1. [ImageEffectOpaque]
    2. void OnRenderImage(RenderTexture source, RenderTexture destination) {
    3.     if (fogMaterial == null)
    4.         fogMaterial = new Material(fogShader);
    5.  
    6.     stencilAsRedTexture = RenderTexture.GetTemporary(source.width, source.height, source.depth, source.format);
    7.     stencilToRedMat = new Material(Shader.Find("ConvertStencilValueXToColorY"));
    8.     stencilToRedMat.SetFloat("_StencilValue", 1);
    9.     stencilToRedMat.color = Color.red;
    10.     Graphics.Blit(source, stencilAsRedTexture, stencilToRedMat);
    11.  
    12.     fogMaterial.SetTexture("_StencilTexture", stencilAsRedTexture);
    13.     Graphics.Blit(source, destination, fogMaterial, (int)fogMode);
    14.  
    15.     RenderTexture.ReleaseTemporary(stencilAsRedTexture);
    16. }
    I've been able to work around this issue by merging the ConvertStencilValueXToColorY and Fog shaders into one (with two passes), and calling Graphics.Blit with both passes rendering.

    But it'd be nice to not have to do this, as it wouldn't work in every case.
     
  3. mradfo21

    mradfo21

    Joined:
    May 16, 2013
    Posts:
    194
    This bug also exists for me. This is a HUGE problem unity.