Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Custom Render Pass lags with camera movement

Discussion in 'Universal Render Pipeline' started by JSmithIR, Sep 9, 2023.

  1. JSmithIR

    JSmithIR

    Joined:
    Apr 13, 2023
    Posts:
    84
    I am doing a very simple test in URP in a simple project. I have a cube, as well as some quads to create a ground surface and a "backdrop" wall.

    I have a scriptable render feature that simply draws the same exact scene with an override material on the objects, and blits this to a global texture called "_GeometryUpdateTexture". The override material is a basic unlit shader with a texture for color.

    The cube's material in the regular pass is a custom shader that outputs "_customPassTex" mapped to screen position in the fragment shader.

    So, my expectation was that the cube should appear the same in both passes. However, when I move the camera, there is an annoying lag in the "_GeometryUpdateTexture."

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Rendering;
    6. using UnityEngine.Rendering.Universal;
    7.  
    8.  
    9. [Serializable]
    10. public class GeometryUpdateSettings
    11. {
    12.     public Material _AnisoMaterial;
    13.  
    14. }
    15.  
    16. public class GeometryUpdate : ScriptableRendererFeature
    17. {
    18.  
    19.     private GeometryUpdatePass m_ScriptablePass;
    20.     public GeometryUpdateSettings Settings = new GeometryUpdateSettings();
    21.     class GeometryUpdatePass : ScriptableRenderPass
    22.     {
    23.  
    24.         private GeometryUpdateSettings m_Settings;
    25.  
    26.         FilteringSettings filteringSettings = new FilteringSettings(RenderQueueRange.all, 128);
    27.         FilteringSettings filteringSettings2 = new FilteringSettings(RenderQueueRange.all, 256);
    28.  
    29.  
    30.         private RenderTargetIdentifier cameraColorTargetIdent2;
    31.  
    32.         private readonly List<ShaderTagId> shaderTagIdList = new List<ShaderTagId>();
    33.  
    34.         RTHandle blur;
    35.         RTHandle m_CameraDepthTarget;
    36.         RTHandle tempTex;
    37.  
    38.         private readonly Material blurMaterial;
    39.         private readonly Material blurPass;
    40.  
    41.         public GeometryUpdatePass(GeometryUpdateSettings variables)
    42.         {
    43.  
    44.             m_Settings = variables;
    45.  
    46.             shaderTagIdList.Add(new ShaderTagId("UniversalForward"));
    47.             shaderTagIdList.Add(new ShaderTagId("UniversalForwardOnly"));
    48.             shaderTagIdList.Add(new ShaderTagId("LightweightForward"));
    49.             shaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit"));
    50.  
    51.  
    52.         }
    53.  
    54.         public void SetTarget(RTHandle cameraColorTargetHandle, RTHandle cameraDepthTargetHandle)
    55.         {
    56.             blur = cameraColorTargetHandle;
    57.             // m_CameraDepthTarget = cameraDepthTargetHandle;
    58.         }
    59.  
    60.         public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
    61.         {
    62.             RenderTextureDescriptor cameraTextureDescriptor = renderingData.cameraData.cameraTargetDescriptor;
    63.             RenderTextureDescriptor cameraTextureDescriptor2 = new RenderTextureDescriptor(cameraTextureDescriptor.width, cameraTextureDescriptor.height,
    64.                 RenderTextureFormat.RGB111110Float);
    65.             cameraTextureDescriptor2.depthBufferBits = 0;
    66.  
    67.  
    68.  
    69.  
    70.  
    71.             RenderingUtils.ReAllocateIfNeeded(ref tempTex, Vector2.one, cameraTextureDescriptor2, FilterMode.Bilinear, TextureWrapMode.Clamp, name: "_TestMap");
    72.             RenderingUtils.ReAllocateIfNeeded(ref m_CameraDepthTarget, Vector2.one, cameraTextureDescriptor, FilterMode.Bilinear, TextureWrapMode.Clamp, name: "_CameraDepthTexture");
    73.             blur = renderingData.cameraData.renderer.cameraColorTargetHandle;
    74.  
    75.             ConfigureTarget(tempTex, m_CameraDepthTarget);
    76.  
    77.             ConfigureClear(ClearFlag.Depth, Color.black);
    78.         }
    79.  
    80.         public void ReleaseTargets()
    81.         {
    82.  
    83.             tempTex?.Release();
    84.               m_CameraDepthTarget?.Release();
    85.  
    86.         }
    87.  
    88.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    89.         {
    90.  
    91.  
    92.             CommandBuffer cmd = CommandBufferPool.Get();
    93.  
    94.             using (new ProfilingScope(cmd, new ProfilingSampler(" GeometryUpdate")))
    95.             {
    96.  
    97.                 context.ExecuteCommandBuffer(cmd);
    98.                 cmd.Clear();
    99.  
    100.                 SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
    101.  
    102.                 Camera camera = renderingData.cameraData.camera;
    103.                 context.DrawSkybox(camera);
    104.  
    105.              
    106.  
    107.    
    108.  
    109.  
    110.                 DrawingSettings drawSettings = CreateDrawingSettings(shaderTagIdList, ref renderingData, sortingCriteria);
    111.        
    112.  
    113.                 context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filteringSettings);
    114.  
    115.                  context.ExecuteCommandBuffer(cmd);
    116.  
    117.                 drawSettings.overrideMaterial = m_Settings._AnisoMaterial;
    118.  
    119.                 drawSettings.overrideMaterialPassIndex = 0;
    120.  
    121.                 context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filteringSettings2);
    122.  
    123.          
    124.  
    125.                 cmd.SetGlobalTexture("_GeometryUpdateTexture", tempTex);
    126.  
    127.             }
    128.  
    129.             context.ExecuteCommandBuffer(cmd);
    130.             CommandBufferPool.Release(cmd);
    131.         }
    132.  
    133.         public override void OnCameraCleanup(CommandBuffer cmd)
    134.         {
    135.  
    136.             cmd.ReleaseTemporaryRT(Shader.PropertyToID(tempTex.name));
    137.               cmd.ReleaseTemporaryRT(Shader.PropertyToID(m_CameraDepthTarget.name));
    138.         }
    139.     }
    140.  
    141.  
    142.  
    143.     /// <inheritdoc/>
    144.     public override void Create()
    145.     {
    146.         m_ScriptablePass = new GeometryUpdatePass(Settings);
    147.         // m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingTransparents + 10;
    148.         m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
    149.  
    150.     }
    151.  
    152.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    153.     {
    154.         renderer.EnqueuePass(m_ScriptablePass);
    155.  
    156.     }
    157.  
    158.     public override void SetupRenderPasses(ScriptableRenderer renderer, in RenderingData renderingData)
    159.     {
    160.         if (renderingData.cameraData.cameraType == CameraType.Game)
    161.         {
    162.             m_ScriptablePass.ConfigureInput(ScriptableRenderPassInput.Depth | ScriptableRenderPassInput.Color);
    163.             m_ScriptablePass.SetTarget(renderer.cameraColorTargetHandle, renderer.cameraDepthTargetHandle);
    164.         }
    165.     }
    166.  
    167.     protected override void Dispose(bool disposing)
    168.     {
    169.         m_ScriptablePass.ReleaseTargets();
    170.     }
    171.  
    172.  
    173. }

    I'll be very surprised if URP is incapable of blitting a custom pass to a texture that can be used in normal pass without lag. Any pointers?
     
  2. JSmithIR

    JSmithIR

    Joined:
    Apr 13, 2023
    Posts:
    84
    A little embarassing but for anyone else in the same situation, it was because my cube material was assigned to the opaque queue and the custom render pass was assigned to the "AfterRenderingOpaques." So inevitably, it was always having to show the previous frame's custom pass.
     
    ElliotB likes this.