Search Unity

Bug URP beginContextRendering called twice

Discussion in 'Universal Render Pipeline' started by alexpana_, Jul 21, 2022.

  1. alexpana_

    alexpana_

    Joined:
    Mar 5, 2017
    Posts:
    10
    I'm using Unity 2021.3.6f1 (MacOS silicon) and URP Version 12.1.7 - June 30, 2022

    I'm registering a delegate with
    RenderPipelineManager.beginContextRendering += OnBeginContextRendering;


    Code (CSharp):
    1.     private void OnBeginContextRendering(ScriptableRenderContext context, List<Camera> cameras)
    2.     {
    3.         Debug.Log($"OnBeginContextRendering {Time.frameCount}, {context.GetHashCode()}");
    4.     }
    Which gets called twice.

    First invocation:

    Code (csharp):
    1.     OnBeginContextRendering 44, 1805453000
    2.     UnityEngine.Debug:Log (object)
    3.     RenderingManager:OnBeginContextRendering (UnityEngine.Rendering.ScriptableRenderContext,System.Collections.Generic.List`1<UnityEngine.Camera>) (at Assets/Scripts/Graphics/RenderingManager.cs:110)
    4.     UnityEngine.Rendering.RenderPipelineManager:DoRenderLoop_Internal (UnityEngine.Rendering.RenderPipelineAsset,intptr,System.Collections.Generic.List`1<UnityEngine.Camera/RenderRequest>,Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle) (at /Users/bokken/buildslave/unity/build/Runtime/Export/RenderPipeline/RenderPipelineManager.cs:115)
    Second invocation:

    Code (csharp):
    1.     OnBeginContextRendering 44, 1805420448
    2.     UnityEngine.Debug:Log (object)
    3.     RenderingManager:OnBeginContextRendering (UnityEngine.Rendering.ScriptableRenderContext,System.Collections.Generic.List`1<UnityEngine.Camera>) (at Assets/Scripts/Graphics/RenderingManager.cs:110)
    4.     UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&) (at /Users/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)
    Is this a bug? How should I discrimate between the two invocations? Why is a GUIUtility invoking a SRP delegate?
     
  2. peterbay

    peterbay

    Unity Technologies

    Joined:
    Nov 2, 2017
    Posts:
    100
    What does your editor layout look like in this case? It could be coming from a material preview, or if you have both game and scene view open.
     
  3. alexpana_

    alexpana_

    Joined:
    Mar 5, 2017
    Posts:
    10
    Thank you for responding.

    I closed everything except the game window, it still reproduces. I've disabled the two canvases and the event system and it still reproduces.

    It also reproduces in release builds.
     
  4. alexpana_

    alexpana_

    Joined:
    Mar 5, 2017
    Posts:
    10
    I can also reproduce it with unity 2022.1.10f1 and URP 13.1.8
     
  5. peterbay

    peterbay

    Unity Technologies

    Joined:
    Nov 2, 2017
    Posts:
    100
    Hmm, that's odd. Can you make a bug report about this? Then we can take a look at it :)
     
  6. t-kuhn

    t-kuhn

    Joined:
    Dec 17, 2018
    Posts:
    8
    Same Problem here. We recently upgraded to Unity 2022.1.16f and switched from the Built-in Render Pipeline to URP. Our application includes 2 in-game cameras and 1 HMD camera. OpenVR is our XR plugin.

    So we switched to URP and subscribed a delegate to the RenderPipelineManager.beginContextRendering event to execute some code at the start of RenderPipeline.Render. Now much to our surprise, our delegate was executed twice per frame. Checking the stacktraces revealed that the beginContextRendering event was fired from these two places every frame:

    UnityEngine.Rendering.RenderPipelineManager:DoRenderLoop_Internal
    UnityEngine.GUIUtility:ProcessEvent

    Now that seemed a bit weird, so we decided to see what we get with a release build (above was in the Editor); Again, our delegate subscribed to RenderPipelineManager.beginContextRendering was executed twice per frame, but this time, the event was fired from the same place both times:

    UnityEngine.Rendering.RenderPipelineManager:DoRenderLoop_Internal

    So why is the RenderPipelineManager.beginContextRendering event firing twice per frame? Shouldn't there be only 1 RenderPipeline running exactly once per frame doing all the rendering for all the cameras involved? So it turns out, that this isn't the case with our setup (1 HMD Camera + 2 other cameras.)

    Connecting the profiler to a debug build yielded this:
    image (5).png

    Notice how there are two DoRenderLoop_Internal and both of them contain their own beginContextRender. That explains why our delegate was executed twice! But it still seems strange that there isn't just one RenderLoop handling all the Rendering for all the cameras (currently, the first Renderloop handles in-game cameras and the second one handles the HMD camera rendering.) It seems like performance might be better if there only was one Renderloop?
     
    TigerHix and chexPK like this.
  7. segant

    segant

    Joined:
    May 3, 2017
    Posts:
    196
    I think it is not solved. Same problem here.
     
  8. ekakiya

    ekakiya

    Joined:
    Jul 25, 2011
    Posts:
    79
    RenderPipeline.Render can be called multi times per frame on editor, one call for GameView with the list of camera instances, other calls for each of SceneView, material(,mesh or animation) preview, material icon, selected camera instance's preview on GameView, and so on.