How is RenderGraph.CreateSharedTexture() supposed to be used?

Hi,

I have a custom ScriptableRenderPass that creates and renders to a global texture for use in my shaders. The texture doesn’t need to change every frame, so I only want to add the respective RasterRenderPass once.

As far as I can tell, I would need to call renderGraph.CreateSharedTexture() to create a texture that persists across the remaining lifetime of this render pass, not just a single frame. However, when calling this Method in RecordRenderGraph() I get the following exception:
InvalidOperationException: A shared texture can only be created outside of render graph execution.

Now, I get why this is the case, but I don’t understand where/when else I’m supposed to call that method.
The docs don’t elaborate and the package examples don’t use it either.

Is there another way to achieve what I want?

1 Like

You might want to use a custom ContextItem and only assign textures to a material when you actually need it.

Context Items can store data in a frame and can also carry data over to the next frame:

public class CustomPassData : ContextItem
{

        public TextureHandle color = TextureHandle.nullHandle;
        public TextureHandle depth = TextureHandle.nullHandle;

        // Reset function required by ContextItem. It should reset all variables not carried
        // over to next frame.
        public override void Reset()
        {
            //only reset the parts we want
            depth = TextureHandle.nullHandle;
        }
}

You can than create and assign textures like this: (in RecordRenderGraph)

var customPassData = frameData.GetOrCreate<CustomPassData>();

var descriptor = renderGraph.GetTextureDesc(resourceData.activeColorTexture);
descriptor.name = $"CustomPassData Color ";
descriptor.clearBuffer = true;
customPassData.color = renderGraph.CreateTexture(descriptor);

//...

And access them again with frameData.get<CustomPassData>()

I hope this helps