Search Unity

Drawing geometry in a custom pipeline (SRP)

Discussion in 'General Graphics' started by luckyleo769, Dec 28, 2021.

  1. luckyleo769

    luckyleo769

    Joined:
    Aug 30, 2020
    Posts:
    2
    Hello everyone!
    In the code below, I'm trying to render a scene with custom shader to RenderTextures and then copy one of them to the back buffer.

    In theory I should see a world positions map, but only clearing works correct. Switching source texture I can see empty texture with certain color and no geometry. Rendering works if only I set BuiltinRenderTextureType.CameraTarget instead of my render textures.

    I tried to use only one render target, with and without depth, use CommandBuffer.DrawMesh with custom triangle, nothing helps. Btw, somehow DrawMesh doesn't works even with BuiltinRenderTextureType.CameraTarget.

    Could you tell me what did I miss?

    Code (CSharp):
    1. public class ExampleRenderPipeline : RenderPipeline
    2. {
    3.     RenderTexture m_GBuffer0;
    4.     RenderTexture m_GBuffer1;
    5.     RenderTexture m_Depth;
    6.  
    7.     RenderTargetIdentifier m_GBuffer0_id;
    8.     RenderTargetIdentifier m_GBuffer1_id;
    9.     RenderTargetIdentifier m_Depth_id;
    10.     int width = 0, height = 0;
    11.  
    12.     protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    13.     {
    14.         Camera camera = cameras[0];
    15.  
    16.         if (width != camera.pixelWidth || height != camera.pixelHeight)
    17.         {
    18.             m_GBuffer0 = new RenderTexture(camera.pixelWidth, camera.pixelHeight, 1, RenderTextureFormat.Default);
    19.             m_GBuffer1 = new RenderTexture(camera.pixelWidth, camera.pixelHeight, 1, RenderTextureFormat.Default);
    20.             m_Depth = new RenderTexture(camera.pixelWidth, camera.pixelHeight, 1, RenderTextureFormat.Depth);
    21.  
    22.             m_GBuffer0_id = new RenderTargetIdentifier(m_GBuffer0);
    23.             m_GBuffer1_id = new RenderTargetIdentifier(m_GBuffer1);
    24.             m_Depth_id = new RenderTargetIdentifier(m_Depth);
    25.  
    26.             width = camera.pixelWidth;
    27.             height = camera.pixelHeight;
    28.         }
    29.  
    30.         // Clearing render targets with different colors (for debug)
    31.         var cmd = new CommandBuffer();
    32.  
    33.         // Clear the first RT with blue
    34.         cmd.SetRenderTarget(m_GBuffer0_id);
    35.         cmd.ClearRenderTarget(true, true, Color.blue);
    36.  
    37.         // Clear the second RT with green
    38.         cmd.SetRenderTarget(m_GBuffer1_id);
    39.         cmd.ClearRenderTarget(true, true, Color.green);
    40.  
    41.         // Select both RTs and Depth buffer, clear only depth
    42.         cmd.SetRenderTarget(new[] { m_GBuffer0_id, m_GBuffer1_id }, m_Depth);
    43.         cmd.ClearRenderTarget(true, false, Color.clear, 1);
    44.  
    45.         context.ExecuteCommandBuffer(cmd);
    46.         cmd.Release();
    47.  
    48.         // Drawing renderers
    49.         ...
    50.         context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
    51.  
    52.         //Blit to back buffer
    53.         CommandBuffer cmdBlit = new CommandBuffer();
    54.         cmdBlit.Blit(m_GBuffer0_id, BuiltinRenderTextureType.CameraTarget);
    55.         context.ExecuteCommandBuffer(cmdBlit);
    56.         cmdBlit.Release();
    57.  
    58.         context.Submit();
    59.     }
    60. }
     
  2. luckyleo769

    luckyleo769

    Joined:
    Aug 30, 2020
    Posts:
    2
    Update:

    I found out that ClearRenderTarget call clears depth buffer to 0.0 regardless of 1.0 being set.
    RenderDoc shows the call as ClearDepthStencilView(D=0.0, S=00)

    Pretty sure this is a reason of empty render targets.

    Did anybody face such issue? Is it known?
     
  3. Railled

    Railled

    Joined:
    Feb 10, 2021
    Posts:
    1
    +bump

    Experienced users, please, help!