Scriptable render pass ConfigureTarget and the depth buffer.

I’ve created a scriptable render pass to support multiple render textures (MRT).
Here is code snippet:

RenderTargetIdentifier[] rb = {
        new RenderTargetIdentifier(proximityBufferId),
        new RenderTargetIdentifier(renderViewID),
};
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor texture)
    {
        cmd.GetTemporaryRT(
            renderViewID, texture.width, texture.height,
            texture.depthBufferBits, FilterMode.Bilinear, RenderTextureFormat.Default
        );
        cmd.GetTemporaryRT(
            proximityBufferId, texture.width, texture.height,
            texture.depthBufferBits, FilterMode.Bilinear, RenderTextureFormat.Default
        );
        ConfigureTarget(rb);
        ConfigureClear(ClearFlag.None, Color.clear);
    }

public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        var camera = renderingData.cameraData.camera;
        if (camera.cameraType != CameraType.Game)
            return;

        if (m_Material == null)
            return;

        CommandBuffer cmd = CommandBufferPool.Get();
        var drawingSettings = CreateDrawingSettings(proximityShaderTagId, ref renderingData, SortingCriteria.None);
        using (new ProfilingScope(cmd, m_ProfilingSampler))
        {
            context.DrawRenderers(
                renderingData.cullResults, ref drawingSettings, ref _filteringSettings
            );
        }
        context.ExecuteCommandBuffer(cmd);
        cmd.Clear();

        CommandBufferPool.Release(cmd);
    }

It seems to be working fine except it doesn’t write to depth buffer when I view it in the frame debugger.
The shader has ZWrite on.
I was initially trying ConfigureTarget(rb, depthTexID). I am wondering, what is the depthAttachment parameter for in the function? I assume it is for passing in a depth render texture and not the “depth buffer”. But I’m confused because since I’m changing the render target to some other render texture (not the usual _CameraColorTexture), does the same depth buffer get written to or do I need to specify it somehow?

How can I write to the camera’s depth buffer and also to my own render texture at the same time?

I think I understand where the problem is but haven’t yet found a solution.
I think I need to pass in the depth buffer with ConfigureTarget(colorTargets, depthBuffer)

Where with the built in render pipeline you could get the depth buffer via:
RenderTexture.depthBuffer

But IDK how to get the depth buffer from the ScriptableRenderer.cameraColorTarget, which is a RenderTargetIdentifier.

    public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData){
        ConfigureTarget(rb, renderingData.cameraData.renderer.cameraDepthTarget);
    }

I thought maybe cameraDepthTarget would be it but it doesn’t seem so.

It seems that changing the render target to something other than default (i.e. the screen buffer), it doesn’t write depth. Can someone confirm this?

Did you ever find a solution? I cannot figure out how the new RTHandle system handles depth. I also am having issues with depth when trying to make a custom pass. If the temp RT uses depthbufferbits of 0, then depth does not work in the custom pass - if I use anything more than 0, it terms my temp RT into a depth texture format and renders a depth pass instead of color

Hi, color and depth render texture cannot be combined together in a RTHandle. You should create a color RTHandle and a depth RTHandle and use ConfigureTarget(colorHandle, depthHandle); to set the render target.

If you don’t need to write depth, you should set the target with ConfigureTarget(colorHandle, colorHandle); instead of ConfigureTarget(colorHandle);.

To create a depth RTHandle, you can try:

RTHandle depthHandle;

// Get the camera target's render texture descriptor
var desc = renderingData.cameraData.cameraTargetDescriptor;

// No need to modify the depth buffer bits, it seems that creating a depth RTHandle has a higher priority.

// Allocate a RTHandle
RenderingUtils.ReAllocateIfNeeded(ref depthHandle, desc, name: "_MyDepthTexture");

// Set render target to depth only
ConfigureTarget(depthHandle, depthHandle);
// Or provide a color RTHandle
ConfigureTarget(xxx, depthHandle);

Example to create a color RTHandle:

RTHandle colorHandle;

// Get the camera target's render texture descriptor
var desc = renderingData.cameraData.cameraTargetDescriptor;

// Modify the descriptor.
desc.depthBufferBits = 0;

// Allocate a RTHandle
RenderingUtils.ReAllocateIfNeeded(ref colorHandle, desc, name: "_MyColorTexture");

// Set render target to color only
ConfigureTarget(colorHandle, colorHandle);
// Or provide a depth RTHandle
ConfigureTarget(colorHandle, xxx);
4 Likes

@wwWwwwW1 thanks! Will test when i get home from work

1 Like

@wwWwwwW1 Yes your solution worked for me. You are a life saver. I appreciate it!

1 Like