Clearing a RT on custom render feature pass only on MainCamera

I’m trying to create a render feature that is essentially a mask to be used in a global shader property. I’m fairly new to rendering and game dev in general so I’m having difficult wrapping my head around things due to the lack of information… Basically I only want to clear an RTHandle once, not for every camera but I don’t know how to approach it. And to be honest, I’m not even sure it’s my problem.

Perhaps my question really is, how can I limit my pass to only take place on the MainCamera?

For context, In my current script, I’m using the command buffer to draw a render layer and then use SetGlobalTexture() during the pass’ Execute method to make it accessible to a shader.

In OnCameraSetup I added my RTHandle like this:

if (m_Settings.layerTextureID != “”)
{
RenderingUtils.ReAllocateIfNeeded(ref m_ColorRT, colorDescriptor, name: m_Settings.layerTextureID);
}
else
{
m_ColorRT = renderingData.cameraData.renderer.cameraColorTargetHandle;
}

And I’ve defined the RenderTargetDescriptor using the cameraData.cameraTargetDescriptor

var colorDescriptor = renderingData.cameraData.cameraTargetDescriptor;
colorDescriptor.colorFormat = RenderTextureFormat.ARGB32;

I’m using ARGB32 because Ii want to use the alpha channel as a mask.

However, if I set ConfigureClear(), according to the FrameDebugger, my pass occurs for each camera in the scene but only in the first, MainCamera is the texture being written to RTHandle, with pass then appearing in the other cameras, with I’m assuming, cleared color

ConfigureTarget(m_ColorRT, m_DepthRT);
ConfigureClear(ClearFlag.Color, Color.clear);

The problem is that I can remove this ConfigureClear and it works when not playing the playing Preview. But once I do, all the frames of the texture just keep being written to the RTHandle. I’ve tried shifting the clear around to OnCameraCleanup and Dispose methods. m_ColorRT?.Release() also doesn’t seem to do much either.

I’d appreciate any help. Please let me know if there’s any further info I can provide or if my analysis seems off

I’m not sure if anyone else would have the same problem as me but I managed to come up with a solution by adding a null check in the AddRenderPasses override method in the Feature class. Basically I just check to see if if the camera has a base camera, and if it does I return, otherwise enqueue the pass

  public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  {
    if (renderingData.cameraData.baseCamera != null) return;
 
    renderer.EnqueuePass(m_ScriptablePass);
  }
2 Likes