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 Doing a custom per-object pre-pass with URP

Discussion in 'Universal Render Pipeline' started by SgerbwdGwyn, Sep 5, 2022.

  1. SgerbwdGwyn

    SgerbwdGwyn

    Joined:
    Dec 2, 2013
    Posts:
    25
    In order to render a particular shader I'm using for everything, I need to do a pre-pass to get the lighting data and run a compute shader on the result. I've currently got this set up as a scriptable renderer feature with a scriptable render pass that tries to run a particular pass in this shader.

    Code (CSharp):
    1. Shader "Custom/CrossHatchShader"
    2. {
    3.     Properties
    4.     {
    5.         //...
    6.     }
    7.    
    8.     SubShader
    9.     {
    10.         Tags{"RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "UniversalMaterialType" = "Lit" "IgnoreProjector" = "True" "ShaderModel" = "4.5"}
    11.         LOD 300
    12.        
    13.         Pass
    14.         {
    15.             Name "ForwardLit"
    16.             Tags{"LightMode" = "UniversalForward"}
    17.             //...
    18.         }
    19.        
    20.         // The pre pass I want to achieve in SRP
    21.         Pass
    22.         {
    23.             Name "CHLightingInfo"
    24.        
    25.             // Regular vertex pass
    26.             #pragma vertex LitPassVertex
    27.             // Pass in the shader that gathers the lighting info
    28.             #pragma fragment LightingPrePassFragment
    29.         }
    30.     }
    31. }
    And my ScriptableRenderPass is as follows:
    Code (CSharp):
    1. public class CHLightingPrePass : ScriptableRenderPass {
    2.     // We will store our pass settings in this variable.
    3.     CHAutoExposureSRPFeature.PassSettings passSettings;
    4.  
    5.     RenderTargetIdentifier colorBuffer;
    6.    
    7.     static ShaderTagId CHPrePassShaderNameID = new ShaderTagId("CHLightingInfo");
    8.  
    9.     public CHLightingPrePass(CHAutoExposureSRPFeature.PassSettings passSettings) {
    10.         this.passSettings = passSettings;
    11.  
    12.         // Set the render pass event.
    13.         renderPassEvent = passSettings.renderPassEvent;
    14.     }
    15.  
    16.     public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) {
    17.         colorBuffer = renderingData.cameraData.renderer.cameraColorTarget;
    18.     }
    19.  
    20.     public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
    21.         CommandBuffer cmd = CommandBufferPool.Get("Crosshatch PrePass Commands");
    22.         using (new ProfilingScope(cmd, new ProfilingSampler(ProfilerTag))) {
    23.  
    24.             // Should draw all objects with materials using shaders that have a pass named as CHPrePassShaderNameID's content,
    25.             // and only that specific pass
    26.             cmd.SetRenderTarget(colorBuffer);
    27.             RendererListDesc desc = new RendererListDesc(CHPrePassShaderNameID, renderingData.cullResults, renderingData.cameraData.camera);
    28.             RendererList rendererList = context.CreateRendererList(desc);
    29.             cmd.DrawRendererList(rendererList);
    30.         }
    31.  
    32.         // Execute the command buffer and release it.
    33.         context.ExecuteCommandBuffer(cmd);
    34.         CommandBufferPool.Release(cmd);
    35.     }
    36.    
    37.     public override void OnCameraCleanup(CommandBuffer cmd) {
    38.        
    39.     }
    40. }
    When I look in the frame debugger, the pass appears but doesn't seem to do anything: upload_2022-9-5_11-31-13.png

    So it looks like it's not doing anything on the objects that are meant to be drawn via DrawRendererList

    Could someone more experienced with SRP clue me in on how to properly do an object pre-pass like this?
     
  2. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    176
    try adding
    Tags{"LightMode" = "CHLightingInfo"}
     
  3. SgerbwdGwyn

    SgerbwdGwyn

    Joined:
    Dec 2, 2013
    Posts:
    25
    Still nothing, unfortunately
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    You should set your own render target otherwise you'll render into current camera color attachment, what you definitely don't want in your case, you want to render it to your own buffer I suppose (for example as Unity doing with _DepthTexture) and use that buffer later.

    Also I suggest you use context for rendering. In your case in OnCameraSetup get temp RT for your buffer, set render target to your buffer (don't forget to set clear flags if you need)
    upload_2022-9-5_15-13-47.png

    And in Execute:
    upload_2022-9-5_15-10-18.png

    Dont forget to release RT for buffer in OnCameraCleanup.
    upload_2022-9-5_15-11-3.png

    As result you'll see properly filled buffer:
    upload_2022-9-5_15-20-33.png
     
    tmonestudio likes this.