Search Unity

ScriptableRenderPass not changing render target

Discussion in 'Universal Render Pipeline' started by Gnoblar_agency, Feb 25, 2021.

  1. Gnoblar_agency

    Gnoblar_agency

    Joined:
    Sep 20, 2012
    Posts:
    8
    Hi all

    I've created a custom render pass that renders all geometry with a specific pass into a temporary render target. When doing this, I need to make use of the pre-existing depth buffer from the DrawOpaqueObjects pass for depth testing.

    However, when I configure the pass using
     ConfigureTarget(_DistortionRT.id, new RenderTargetIdentifier(BuiltinRenderTextureType.Depth));
    it renders my objects into the _CameraColorTexture instead of my temporary render target. If I remove the depth attachment parameter, it changes target but then I the depth information is not present.

    Is this a bug? Or am I doing something that impossible? I'd rather carry over the depth buffer instead of manually doing the clipping with a depth texture in the shader.

    Here is the full code for the RenderPass. I'm using URP 10.2.2

    Code (CSharp):
    1. class DistortionRenderPass : ScriptableRenderPass
    2.     {
    3.         public const string DISTORTION_TEXTURE_NAME = "_CameraDistortionTexture";
    4.  
    5.         #region Private members
    6.         private const string _ProfilerTag = "Distortion";
    7.  
    8.         private RenderTextureDescriptor _BaseDescriptor;
    9.         private RenderTargetHandle _DistortionRT;
    10.  
    11.         private List<ShaderTagId> _DistortionTagIdList = new List<ShaderTagId>();
    12.  
    13.         private FilteringSettings _DistortionFilteringSettings;
    14.         #endregion
    15.  
    16.  
    17.         // This method is called before executing the render pass.
    18.         // It can be used to configure render targets and their clear state. Also to create temporary render target textures.
    19.         // When empty this render pass will render to the active camera render target.
    20.         // You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
    21.         // The render pipeline will ensure target setup and clearing happens in an performance manner.
    22.         public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
    23.         {
    24.             //Store the descriptor for the camera render
    25.             _BaseDescriptor = cameraTextureDescriptor;
    26.            
    27.             _DistortionRT = new RenderTargetHandle();
    28.             _DistortionRT.Init(_ProfilerTag);
    29.  
    30.             //Get a temporary Render Texture into which we will render the object's distortion pass
    31.             cmd.GetTemporaryRT(_DistortionRT.id, _BaseDescriptor.width, _BaseDescriptor.height, 0, FilterMode.Bilinear, RenderTextureFormat.RGFloat);
    32.  
    33.             //Set Distortion as the new render target, keep the depth existing depth
    34.             //  This fails to change the target!
    35.             ConfigureTarget(_DistortionRT.id, new RenderTargetIdentifier(BuiltinRenderTextureType.Depth));
    36.             //  This changes the target but the existing depth is missing
    37.             //ConfigureTarget(_DistortionRT.id);
    38.  
    39.             //Clear the color, maintain the depth info
    40.             ConfigureClear(ClearFlag.Color, Color.clear);
    41.  
    42.             //Prepare any filtering settings
    43.             _DistortionFilteringSettings = FilteringSettings.defaultValue;
    44.  
    45.             //Setup our valid shader tags for rendering
    46.             _DistortionTagIdList.Clear();
    47.             _DistortionTagIdList.Add(new ShaderTagId("Distortion"));
    48.         }
    49.  
    50.         // Here you can implement the rendering logic.
    51.         // Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers
    52.         // https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html
    53.         // You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
    54.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    55.         {
    56.             CommandBuffer cmd = CommandBufferPool.Get(_ProfilerTag);
    57.  
    58.             ProfilingSampler profilingSampler = new ProfilingSampler(_ProfilerTag);
    59.             using (new ProfilingScope(cmd, profilingSampler))
    60.             {
    61.                 //Execute the initial config commands?
    62.                 context.ExecuteCommandBuffer(cmd);
    63.                 cmd.Clear();
    64.  
    65.                 // We want the same rendering result as the main opaque render
    66.                 var sortFlags = SortingCriteria.CommonTransparent;
    67.  
    68.                 DrawingSettings drawSettings = CreateDrawingSettings(_DistortionTagIdList, ref renderingData, sortFlags);
    69.  
    70.                 context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref _DistortionFilteringSettings);
    71.  
    72.                 cmd.SetGlobalTexture(DISTORTION_TEXTURE_NAME, _DistortionRT.id);
    73.             }
    74.  
    75.             context.ExecuteCommandBuffer(cmd);
    76.             CommandBufferPool.Release(cmd);
    77.         }
    78.  
    79.         /// Cleanup any allocated resources that were created during the execution of this render pass.
    80.         public override void FrameCleanup(CommandBuffer cmd)
    81.         {
    82.             base.FrameCleanup(cmd);
    83.  
    84.             cmd.ReleaseTemporaryRT(_DistortionRT.id);
    85.         }
    86.     }
    Screenshot_214.png
    Screenshot_215.png
     
    zhuhaiyia1 likes this.
  2. andrea_i

    andrea_i

    Joined:
    Nov 18, 2012
    Posts:
    32
    Hi, I'm in the exact same troubles.

    EDIT, solved at least for my specific case:
    I want to render to a pre-allocated renderTexture that I then want to feed to my material, to do that I use RenderPassEvent.BeforeRenderingPrepasses.

    From ScriptableRenderPass.Configure, I call myScriptableRenderPass.ConfigureTarget(myRenderTextureIds, myRenderTextureIds[0].depthBuffer);

    Then from ScriptableRenderPass.Execute , I call
    myScriptableRenderPass.ConfigureClear(ClearFlag.All, Color.black);

    I'm not using the depth attachment yet, so I don't know if the depth will be cleared or not, but the color textures are all coming out correctly.
     
    Last edited: Mar 3, 2021