Saving off the custom stencil buffer

Hello!

I’m attempting to use a custom scene view extension to save off the stencil buffer to its own texture so it can be used later. The code looks something like this

FScreenPassTexture FSavedFramebufferSceneViewExtension::PostProcessPassAfterTonemap_RenderThread(FRDGBuilder& GraphBuilder, const FSceneView& View, const FPostProcessMaterialInputs& InOutInputs)
{
    if (InOutInputs.CustomDepthTexture)
    {
       // CustomDepthTexture is a pointer to a R8G8B8A8 texture I've created with UTextureRenderTarget2D::InitCustomFormat
       FRDGTextureRef DestinationCustomDepthTexture = RegisterExternalTexture(GraphBuilder, CustomDepthTexture->GetResource()->TextureRHI, TEXT("ORSavedFramebuffer_CustomDepth"));
    
       FIntVector CustomDepthSize = InOutInputs.CustomDepthTexture->Desc.GetSize();
    
       AddDrawTexturePass(
          GraphBuilder,
          View,
          InOutInputs.CustomDepthTexture,
          DestinationCustomDepthTexture,
          FIntPoint::ZeroValue,
          FIntPoint::ZeroValue,
          FIntPoint(CustomDepthSize.X, CustomDepthSize.Y)
       );
    }
}

However this doesn’t seem to work - I can read the depth value but the stencil value is always 0. Is there something else I need to do here? I’ve seen other places in the code create an SRV based on the CustomDepth texture using PF_X24_G8, but that didn’t seem to work all - giving me neither the stencil nor the depth.

[Attachment Removed]

Hi,

Can you please check if r.CustomDepth is set to 3, or alternatively that the Custom Depth-Stencil Pass value inside Project Settings > Postprocessing is set to Enabled with Stencil? This is required for the stencil value to be written.

Thanks,

Sam

[Attachment Removed]

Hey so since we’re on a tight timeline I ended up figuring this out on my own

[Attachment Removed]

Yes we have that set to 3

[Attachment Removed]

I can see in the output of the stencil buffer that we are getting correct values from the scene as well

[Attachment Removed]

Great, unless you have further questions, I will close this case then.

Thanks,

Sam

[Attachment Removed]