Possible to "export"/"save" TextureHandles inside a custom render pass in Unity's Render Graph API?

Hi, I’m new to the forums so please let me know if this post is in the wrong place!

I’ve been learning how to use the Render Graph feature and have tried writing some custom renderer features using it. One thing I’ve been struggling with though is trying to maintain a texture “state” within a custom render pass.

Basically what I’m trying to achieve is hand-rolled time-averaging of a certain texture, so basically TexturePreviousFrame * t + TextureCurrentFrame * (1-t) for example. I can create a TextureCurrentFrame pretty easily just by using UniversalRenderer.CreateRenderGraphTexture(), but to have a TexturePreviousFrame I’d need to put TextureCurrentFrame into some variable outside the render pass. My question basically is: is this possible?

Some things I’ve tried:

  1. Having an RTHandle as a field of my custom render pass class. In the RecordRenderGraph function I derive a TextureHandle from it using renderGraph.ImportTexture. However, when I try to update the TextureHandle (by blitting to it), changes to the texture aren’t reflected back into the RTHandle.
  2. Same as above, but attempting to directly blit my TextureCurrentFrame handle to the RTHandle. Unfortunately I get an error: InvalidOperationException: Current Render Graph Resource Registry is not set. You are probably trying to cast a Render Graph handle to a resource outside of a Render Graph Pass.
  3. Trying to create a shared texture. A shared texture seems like exactly what I want (a TextureHandle whose lifetime isn’t confined to a single render graph instance/execution). However, the CreateSharedTexture method belongs to the renderGraph instance, and I can’t use the method inside of the RecordRenderGraph method. So where do I use it?

An example of what I’m trying to acheive:

class MyRenderPass : ScriptableRenderPass {
  private Material mat;
  private ??? prevFrameTexture;    // Not sure what type to use here

  public MyRenderPass(Material mat) {
    this.mat = mat;
  }

  public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData) {
    // Some set up goes here:
    // blah blah blah

    if (prevFrameTexture != null) {
      mat.SetTexture("Previous Frame", prevFrameTexture);
    }

    RenderGraphUtils.BlitMaterialParameters params = new RenderGraphUtils.BlitMaterialParameters(
      srcTextureHandle, destTextureHandle, mat, 0
    ); 
    renderGraph.AddBlitPass(params, "My Blit Pass");

    prevFrameTexture = destTextureHandle;     // This line doesn't work as is, and is what I can't figure out.
  }
}
2 Likes