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.

Question Render Graph pass not drawing to custom color buffer

Discussion in 'Shaders' started by TheCostco, Nov 24, 2022.

  1. TheCostco

    TheCostco

    Joined:
    Jun 1, 2018
    Posts:
    1
    For a couple days I've been working on a custom SRP with Render Graph, and have been basically unable to render anything to the screen. I have two passes so far: drawing opaque geometry to a custom color buffer, and then blitting that to the camera color target.

    The blit pass works fine, as the color buffer's clear color is rendered to the screen. However, nothing else is, even when Renderloop.Draw is present in the frame debugger and displaying the cube I'm trying to render.

    Additionally, there is no problem at all when rendering directly to the camera color texture, so I don't believe my general setup is the issue. Any ideas what could be causing this?

    upload_2022-11-23_17-32-58.png

    upload_2022-11-23_17-34-47.png

    Note:
    CameraRenderer
    is created at startup by the render pipeline, and its Render method is called each frame per camera

    Code (CSharp):
    1. public class CameraRenderer {
    2.         private readonly RenderGraph renderGraph;
    3.  
    4.         private Camera camera;
    5.         private CullingResults cull;
    6.      
    7.         private static readonly ShaderTagId gBufferPassId = new ShaderTagId("GBuffer");
    8.  
    9.         public CameraRenderer(RenderGraph renderGraph) {
    10.             this.renderGraph = renderGraph;
    11.         }
    12.      
    13.         public void Render(ScriptableRenderContext context, Camera camera) {
    14.             this.camera = camera;
    15.  
    16.             ScriptableCullingParameters cullingParams;
    17.             if (!camera.TryGetCullingParameters(out cullingParams)) return;
    18.             cull = context.Cull(ref cullingParams);
    19.          
    20.             context.SetupCameraProperties(camera);
    21.          
    22.             var cmd = CommandBufferPool.Get("Execute Render Graph");
    23.             var renderGraphParams = new RenderGraphParameters {
    24.                 scriptableRenderContext = context,
    25.                 commandBuffer = cmd,
    26.                 currentFrameIndex = Time.frameCount,
    27.             };
    28.  
    29.             using (renderGraph.RecordAndExecute(renderGraphParams)) {
    30.                 var gBuffer = GBufferPass();
    31.                 BlitPass(gBuffer);
    32.             }
    33.          
    34.             context.ExecuteCommandBuffer(cmd);
    35.             CommandBufferPool.Release(cmd);
    36.          
    37.             context.Submit();
    38.             this.camera = null;
    39.         }
    40.      
    41.         // ********************************************************************************************************** //
    42.      
    43.         private TextureHandle CreateColorTexture(string name) {
    44.             bool isSrgb = QualitySettings.activeColorSpace == ColorSpace.Gamma;
    45.             TextureDesc colorDesc = new TextureDesc(Vector2.one) {
    46.                 colorFormat = GraphicsFormatUtility.GetGraphicsFormat(RenderTextureFormat.Default, isSrgb),
    47.                 depthBufferBits = DepthBits.None,
    48.                 clearBuffer = true,
    49.                 clearColor = Color.black,
    50.                 enableRandomWrite = false,
    51.                 filterMode = FilterMode.Point,
    52.                 msaaSamples = MSAASamples.None,
    53.                 useDynamicScale = false,
    54.                 name = name
    55.             };
    56.             return renderGraph.CreateTexture(colorDesc);
    57.         }
    58.  
    59.         private TextureHandle CreateDepthTexture(string name) {
    60.             TextureDesc depthDesc = new TextureDesc(Vector2.one) {
    61.                 colorFormat = GraphicsFormat.None,
    62.                 depthBufferBits = DepthBits.Depth32,
    63.                 clearBuffer = true,
    64.                 clearColor = Color.black,
    65.                 enableRandomWrite = false,
    66.                 filterMode = FilterMode.Point,
    67.                 msaaSamples = MSAASamples.None,
    68.                 useDynamicScale = false,
    69.                 name = name
    70.             };
    71.             return renderGraph.CreateTexture(depthDesc);
    72.         }
    73.      
    74.      
    75.  
    76.         class GBufferPassData {
    77.             public GBuffer gBuffer;
    78.             public RendererListHandle gBufferRendererList;
    79.         }
    80.  
    81.         private GBuffer GBufferPass() {
    82.             using (var builder = renderGraph.AddRenderPass(
    83.                 "Geometry Pass",
    84.                 out GBufferPassData passData,
    85.                 new ProfilingSampler("GBuffer Pass Profiler")
    86.             )) {
    87.                 TextureHandle albedo = CreateColorTexture("Albedo");
    88.                 TextureHandle depth = CreateDepthTexture("Depth");
    89.                 TextureHandle normal = CreateColorTexture("Normal");
    90.  
    91.                 GBuffer gBuffer = new GBuffer(
    92.                     builder.UseColorBuffer(albedo, 0),
    93.                     builder.UseDepthBuffer(depth, DepthAccess.Write),
    94.                     builder.UseColorBuffer(normal, 1)
    95.                 );
    96.                 passData.gBuffer = gBuffer;
    97.  
    98.                 RendererListDesc gBufferRendererDesc  = new RendererListDesc(gBufferPassId, cull, camera) {
    99.                     sortingCriteria = SortingCriteria.CommonOpaque,
    100.                     renderQueueRange = RenderQueueRange.opaque
    101.                 };
    102.                 RendererListHandle gBufferRenderHandle = renderGraph.CreateRendererList(gBufferRendererDesc);
    103.                 passData.gBufferRendererList = builder.UseRendererList(gBufferRenderHandle);
    104.              
    105.                 builder.SetRenderFunc<GBufferPassData>(RenderGBufferPass);
    106.  
    107.                 return gBuffer;
    108.             }
    109.         }
    110.  
    111.         private static void RenderGBufferPass(GBufferPassData passData, RenderGraphContext context) {
    112.             CoreUtils.DrawRendererList(context.renderContext, context.cmd, passData.gBufferRendererList);
    113.         }
    114.      
    115.      
    116.  
    117.         class BlitPassData {
    118.             public GBuffer gBuffer;
    119.         }
    120.  
    121.         private void BlitPass(GBuffer gBuffer) {
    122.             using (var builder = renderGraph.AddRenderPass(
    123.                 "Blit Pass",
    124.                 out BlitPassData passData,
    125.                 new ProfilingSampler("Blit Pass Profiler")
    126.             )) {
    127.                 gBuffer.albedo = builder.ReadTexture(gBuffer.albedo);
    128.                 gBuffer.depth = builder.ReadTexture(gBuffer.depth);
    129.                 gBuffer.normal = builder.ReadTexture(gBuffer.normal);
    130.  
    131.                 passData.gBuffer = gBuffer;
    132.                 builder.SetRenderFunc<BlitPassData>(RenderBlitPass);
    133.             }
    134.         }
    135.  
    136.         private static void RenderBlitPass(BlitPassData passData, RenderGraphContext context) {
    137.             context.cmd.Blit(passData.gBuffer.albedo, BuiltinRenderTextureType.CameraTarget);
    138.         }
    139.     }