Search Unity

Question RenderTexture Visual Feedback Effect with RTHandles

Discussion in 'High Definition Render Pipeline' started by Euromastyx, Jun 29, 2020.

  1. Euromastyx

    Euromastyx

    Joined:
    Mar 22, 2014
    Posts:
    2
    I'm trying to create a technique using frame accumulation, similar to the no clear flags effect, where the current frame is added to all the previous frames. I'm writing it as a volume component, but I can't seem to get Unity's RTHandles to work that way I'd like to.

    Ideally, I'd like to render the image normally, and also render it to a render texture. The next frame, both the new render from that frame and the render from the previous frame are added together (using a shader) to create that frame, etc, resulting in a visual feedback loop, where every frame is combined with the previous.

    Volume component code:

    Code (CSharp):
    1.    
    2. private RTHandle rth;
    3.  
    4.     public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination)
    5.     {
    6.         //Validate
    7.         if (m_Material == null)    return;
    8.         if (rth == null) rth = CreateRTH(camera);
    9.  
    10.         m_Material.SetTexture("_MainTex", source);
    11.         m_Material.SetTexture("_PrevFrame", rth);
    12.  
    13.         //Draw the frame itself
    14.         HDUtils.DrawFullScreen(cmd, m_Material, destination);
    15.         HDUtils.DrawFullScreen(cmd, m_Material, rth);
    16.     }
    17.  
    18.     private RTHandle CreateRTH(HDCamera camera)
    19.     {
    20.         return RTHandles.Alloc(camera.actualWidth, camera.actualHeight, 2, DepthBits.Depth16, colorFormat: RTFormat, FilterMode.Bilinear, TextureWrapMode.Clamp, TextureDimension.Tex2D, false, false, false, false, 2, 0, MSAASamples.None, true, true, RenderTextureMemoryless.None, "rth");
    21.     }
    Shader function:

    Code (CSharp):
    1.     TEXTURE2D_X(_MainTex);
    2.     TEXTURE2D_X(_PrevFrame);
    3.  
    4.  
    5.     float4 CustomPostProcess(Varyings input) : SV_Target
    6.     {
    7.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    8.  
    9.         uint2 positionSS = input.texcoord * _ScreenSize.xy;
    10.         float3 outColor = LOAD_TEXTURE2D_X(_MainTex, positionSS).xyz;
    11.         outColor.xy = 0;
    12.         float3 acc =  LOAD_TEXTURE2D_X(_PrevFrame, positionSS).xyz;
    13.         acc.yz = 0;
    14. //Note: The render textures are rendered on a black background, so they can simply be added together
    15.         outColor += acc;
    16.  
    17.         return float4(outColor, 1);
    18.     }
    However, I can't seem to successfully read and write into the RTHandle. Not getting any errors, but the content is always just black. I've looked through as many examples as I could find, am I missing something?
    Thanks!