Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Any render graph code examples w/ com.unity.render-pipelines.core?

Discussion in 'Graphics Experimental Previews' started by oldhighscore, May 6, 2021.

  1. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
    I am hoping someone can provide a git repo or external resource that provides working examples of utilizing the com.unity.render-pipelines.core package, in particular utilizing the experimental render graph system.

    I've read through the docs
    https://docs.unity3d.com/Packages/com.unity.render-pipelines.core@11.0/manual/index.html
    and some of
    Unity/Hub/Editor/2021.1.5f1/Editor/Data/Documentation/en/Manual/Graphics.html
    and browsed code over at
    https://github.com/Unity-Technologies/Graphics

    I'm trying to get some very barebone use cases going like rendering trig waves or generating heightfields. I've chosen to ignore the URP and HDRP pipelines for now because I want to get a lower level understanding and try and tap into some of the new packages.

    At some point I want to also utilize the dots + ecs stack and get my own pipe for querying entities and getting them rendered each frame. This is my first dive into graphics pipelines like this but have read through the first dozen of chapters in Real-Time Rendering and I am ready to get my hands dirty.

    In particular, if someone could provide an example of how to take a single DOTS entity and get it rendered utilizing srp render graph system I would highly appreciate it.

    :D
     
  2. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
    Bump.

    Here is what I have.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Experimental.Rendering;
    4. using UnityEngine.Experimental.Rendering.RenderGraphModule;
    5.  
    6. public class HlRenderPipeline : RenderPipeline
    7. {
    8.     RenderGraph m_RenderGraph;
    9.     int m_frameIndex = 0;
    10.     Material m_defaultMaterial;
    11.  
    12.     public HlRenderPipeline(Material defaultMaterial)
    13.     {
    14.         m_RenderGraph = new RenderGraph("HlRenderGraph");
    15.         m_defaultMaterial = defaultMaterial;
    16.     }
    17.  
    18.     // todo how to when & where to call this?
    19.     public void CleanupRenderGraph()
    20.     {
    21.         m_RenderGraph.Cleanup();
    22.         m_RenderGraph = null;
    23.     }
    24.  
    25.     class MyRenderPassData
    26.     {
    27.         public float Parameter;
    28.         public Material Material;
    29.         public TextureHandle InputTexture;
    30.         public TextureHandle OutputTexture;
    31.     }
    32.  
    33.     delegate void RenderFunc<MyRenderPassData>(MyRenderPassData data, RenderGraphContext renderGraphContext) where MyRenderPassData : class, new();
    34.    
    35.  
    36.     protected override void Render(ScriptableRenderContext renderContext, Camera[] cameras)
    37.     {
    38.         var commandBuffer = new CommandBuffer();
    39.         var renderGraphParams = new RenderGraphParameters()
    40.         {
    41.             scriptableRenderContext = renderContext,
    42.             commandBuffer = commandBuffer,
    43.             currentFrameIndex = m_frameIndex
    44.         };
    45.  
    46.         m_RenderGraph.Begin(renderGraphParams);
    47.  
    48.         using (var builder = m_RenderGraph.AddRenderPass<MyRenderPassData>("My Render Pass", out var passData))
    49.         {
    50.             passData.Parameter = 2.5f;
    51.             passData.Material = m_defaultMaterial;
    52.             passData.InputTexture = builder.ReadTexture(passData.InputTexture);
    53.  
    54.             var outputTexture = m_RenderGraph.CreateTexture(
    55.                 new TextureDesc(Vector2.one)
    56.                 {
    57.                     colorFormat = GraphicsFormat.R8G8B8A8_UNorm,
    58.                     clearBuffer = true,
    59.                     clearColor = Color.grey,
    60.                     name = "MyFirstOutput"
    61.                 }
    62.             );
    63.             passData.OutputTexture = builder.WriteTexture(outputTexture);
    64.             builder.SetRenderFunc((MyRenderPassData data, RenderGraphContext ctx) =>
    65.             {
    66.                 // Render Target is already set via the use of UseColorBuffer above.
    67.                 // If builder.WriteTexture was used, you'd need to do something like that:
    68.                 // CoreUtils.SetRenderTarget(ctx.cmd, data.output);
    69.  
    70.                 // Setup material for rendering
    71.                 var materialPropertyBlock = ctx.renderGraphPool.GetTempMaterialPropertyBlock();
    72.                 materialPropertyBlock.SetTexture("_MainTexture", data.InputTexture);
    73.                 materialPropertyBlock.SetFloat("_FloatParam", data.Parameter);
    74.                 CoreUtils.DrawFullScreen(ctx.cmd, data.Material, materialPropertyBlock);
    75.             });
    76.         }
    77.  
    78.         m_RenderGraph.Execute();
    79.         m_frameIndex++;
    80.     }
    81.  
    82. }
    getting errors atm. Working through it. Definitely still learning fundamentals on this thing.

    error line 52: "passData.InputTexture = builder.ReadTexture(passData.InputTexture);"
    error: "
    ArgumentException: Trying to use an invalid resource (pass My Render Pass).
    UnityEngine.Experimental.Rendering.RenderGraphModule.RenderGraphBuilder.CheckResource (UnityEngine.Experimental.Rendering.RenderGraphModule.ResourceHandle& res) (at Graphics/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphBuilder.cs:272)
    UnityEngine.Experimental.Rendering.RenderGraphModule.RenderGraphBuilder.ReadTexture (UnityEngine.Experimental.Rendering.RenderGraphModule.TextureHandle& input) (at Graphics/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphBuilder.cs:54)
    HlRenderPipeline.Render (UnityEngine.Rendering.ScriptableRenderContext renderContext, UnityEngine.Camera[] cameras) (at Assets/Graphics/HlRenderPipeline.cs:52)
    UnityEngine.Rendering.RenderPipeline.Render (UnityEngine.Rendering.ScriptableRenderContext context, System.Collections.Generic.List`1[T] cameras) (at <6a104889781c465ca00c12d0953583e2>:0)
    UnityEngine.Rendering.RenderPipeline.InternalRender (UnityEngine.Rendering.ScriptableRenderContext context, System.Collections.Generic.List`1[T] cameras) (at <6a104889781c465ca00c12d0953583e2>:0)
    UnityEngine.Rendering.RenderPipelineManager.DoRenderLoop_Internal (UnityEngine.Rendering.RenderPipelineAsset pipe, System.IntPtr loopPtr, System.Collections.Generic.List`1[T] renderRequests, Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle safety) (at <6a104889781c465ca00c12d0953583e2>:0)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr, Boolean&)
    "
     
  3. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
    Well of course the HDRP uses it! So yeah just going to be looking at that for examples as they are many. Couldn't find anyone else in these forums who has tried it yet interesting enough. I wonder if anyone is out there in the wild using it and just not around to answer my newbie questions. Once I get a basic hello cube working I'll post the complete code.
     
  4. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
    Here is where I'm at, still no luck. Just trying to get the screen to clear to a color atm, then after that to execute an actual shader program.

    heellllppp

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Experimental.Rendering;
    4. using UnityEngine.Experimental.Rendering.RenderGraphModule;
    5.  
    6. public class HlRenderPipeline : RenderPipeline
    7. {
    8.     RenderGraph m_RenderGraph;
    9.     int m_frameIndex = 0;
    10.     Material m_defaultMaterial;
    11.     CommandBuffer m_commandBuffer;
    12.  
    13.     public HlRenderPipeline(Material defaultMaterial)
    14.     {
    15.         m_RenderGraph = new RenderGraph("HlRenderGraph");
    16.         m_defaultMaterial = defaultMaterial;
    17.         m_commandBuffer = new CommandBuffer();
    18.     }
    19.  
    20.     // todo how to when & where to call this?
    21.     public void CleanupRenderGraph()
    22.     {
    23.         m_RenderGraph.Cleanup();
    24.         m_RenderGraph = null;
    25.     }
    26.  
    27.     class MyRenderPassData
    28.     {
    29.         public TextureHandle Target;
    30.         public Rect Viewport;
    31.     }
    32.  
    33.     protected override void Render(ScriptableRenderContext renderContext, Camera[] cameras)
    34.     {
    35.         var renderGraphParams = new RenderGraphParameters()
    36.         {
    37.             scriptableRenderContext = renderContext,
    38.             commandBuffer = m_commandBuffer,
    39.             currentFrameIndex = m_frameIndex
    40.         };
    41.  
    42.         m_RenderGraph.Begin(renderGraphParams);
    43.  
    44.         var outputTexture = m_RenderGraph.CreateTexture(
    45.             new TextureDesc(Vector2.one)
    46.             {
    47.                 colorFormat = GraphicsFormat.R8G8B8A8_UNorm,
    48.                 clearBuffer = true,
    49.                 clearColor = Color.red,
    50.                 name = "MyFirstOutput"
    51.             }
    52.         );
    53.  
    54.         var camera = cameras[0];
    55.  
    56.         using (var builder = m_RenderGraph.AddRenderPass<MyRenderPassData>("My Render Pass", out var passData))
    57.         {
    58.             passData.Target = builder.WriteTexture(outputTexture);
    59.             passData.Viewport = camera.pixelRect;
    60.  
    61.             builder.SetRenderFunc((MyRenderPassData data, RenderGraphContext ctx) =>
    62.             {
    63.                 ctx.cmd.SetRenderTarget(data.Target);
    64.                 ctx.cmd.SetViewport(data.Viewport);
    65.             });
    66.  
    67.         }
    68.  
    69.         m_RenderGraph.Execute();
    70.         m_frameIndex++;
    71.  
    72.         renderContext.ExecuteCommandBuffer(m_commandBuffer);
    73.         m_commandBuffer.Clear();
    74.  
    75.         renderContext.Submit();
    76.     }
    77. }
     
  5. mingwai

    mingwai

    Small Graphics Potato Unity Technologies

    Joined:
    Jan 16, 2017
    Posts:
    52
    foonix, Greexonn, razvan1024 and 2 others like this.
  6. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
  7. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
  8. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
    @Unity where is ming and what have you done with this example? is there a newer link that can be referenced?
     
  9. mingwai

    mingwai

    Small Graphics Potato Unity Technologies

    Joined:
    Jan 16, 2017
    Posts:
    52
  10. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Thank you very much! Searched something like this for two hours.
     
  11. AljoshaD

    AljoshaD

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    228
    This is a great tutorial on how to write a custom SRP using RenderGraph.

    There is also a lot of info in the thread on the URP RenderGraph adoption.