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

RenderPipeline.InternalRender() doing per frame GC Alloc with 2+ Render Texture cameras

Discussion in 'Universal Render Pipeline' started by Unusual-Cadence, Mar 26, 2020.

  1. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Hello! I'm getting some per-frame GC allocs coming from the Universal Render Pipeline, specifically the GetCameras() and SortCameras() functions. I have 3 cameras set to 'base' that are outputting to RenderTextures which I'm combining in funky ways for some 2D lighting and reflection effects, and one camera rendering the combined rendertexture at the end. This combination of cameras seems to be making CetCameras() and SortCameras() calls happen every frame, which is allocating unnecessary memory!

    Is this a bug, or something I can fix on my end? It happens in both the foward renderer and the 2D renderer setups.
     

    Attached Files:

  2. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Edit: Thought I had a genius insight in to what this is, but it wasn't genius at all, haha. The problem still stands, having multiple cameras wth at least one going to a Render Texture causes Get/SortCameras() to create memory allocations when doing an Array.Resize. I submitted a bug for this with a test project regarding the 2D Renderer, but it stands in the Forward Renderer too.

    This is tested on URP 7.3.1 and Unity 2019.3.7
     
    Last edited: Mar 28, 2020
  3. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Still getting this issue with URP 7.4.1 and Unity 2019.4.1 - if I have more that 2 cameras rendering (Base, Overlay, RenderTexture) I get RenderPipelineManager.GetCameras() creating garbage every frame. I can't believe I'm the only person using the URP that is running in to this issue - is anyone else getting this, or knows how to overcome it?

    The issue is tracked here but it seems to be attributed to the 2D Renderer rather than the Forward Renderer as a whole -

    https://issuetracker.unity3d.com/is...a-render-texture-creates-gc-alloc-every-frame

    Is this going to get backported to 7 any time soon?
     
    Meatloaf4 likes this.
  4. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    It looks like a fix has been checked in for the issue, hooray! https://github.com/Unity-Technologies/Graphics/pull/268/commits

    It's supposed to be in 7.4.1 looking at the changelog, however, but I'm still getting the issue - Sort Cameras isn't creating garbage now, but GetCameras() still is!

    gcalloc.png

    Weirdly I can't find the GetCameras() function anywhere in the URP .cs files... @phil_lira given you made the fix, do you have any idea what I can do to get rid of this remaining GC allocation every frame?
     
    Last edited: Jun 30, 2020
  5. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    173
    Unfortunately, I couldn't stand it and gave up URP, 2d lighting, shader graph and new post processing. I come back to using LWRP and post processing stack + lighting 3d.
     
  6. Elvar_Orn

    Elvar_Orn

    Unity Technologies

    Joined:
    Dec 9, 2019
    Posts:
    158
    Heyhey!
    Can you report this as a bug for me?
    The fix for this needs to be done in core and I believe I know where the culprit is. It would speed up my work to have a good repro if possible.
     
  7. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Hello! I have submitted this as a bug and sent some additional repro steps in an email. It's really easy to recreate though, it happens right out of the box:

    With 2019.4.1 and URP 7.4.1 installed do the following:

    * Create a new project with the 'Universal Render Pipeline' template.
    * Hit 'play' in the editor and check the profiler - there will be no GC Allocs made every frame with one camera in the scene.
    * Add an additional camera in the editor (removing the audio listener) and hit play again. Observe that 'RenderPipelineManager.DoRenderLoop_Internal() is now allocating memory every frame.
    * Continue to add additional cameras to increase the per-frame memory allocation.

    You can have one additional camera targeting a rendertexture to avoid this.
     
    Elvar_Orn likes this.
  8. Elvar_Orn

    Elvar_Orn

    Unity Technologies

    Joined:
    Dec 9, 2019
    Posts:
    158
    Thank you!
    Will take a look at this. Can you give me the case ID?
     
  9. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Elvar_Orn likes this.
  10. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Any news or updates on this?

    I've spotted that in the frame debugger, the camera that is causing GC alloc per frame when enabled is being rolled in to the primary Render Camera's drawing stack, and I'm not sure why that is the case:

    camera gc.png

    I'm not exactly sure what I've done to get my 'final composite' camera gets it's own Render Camera hierarchy but the Minimap doesn't. They're all using separate ForwardRenderer objects so should be unique!
     
  11. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Bumping again. Still an issue in 2020.1 with the URP 8.2.0 or the 9.0.0.35 packages.
     
  12. Elvar_Orn

    Elvar_Orn

    Unity Technologies

    Joined:
    Dec 9, 2019
    Posts:
    158
    Hey,
    I just poked the dev about this. I'll hopefully be able to give you some updates later this week.
     
  13. pelegk11

    pelegk11

    Joined:
    Jun 20, 2019
    Posts:
    1
    Hey guys, any news on this issue?
    im having 128B allocated every frame on android, samsung s20+,
    using unity ver 2019.4.5f1.

    i also have multiple cameras in the scene (one regular, one overlay)
     
  14. Personuo

    Personuo

    Joined:
    Mar 19, 2014
    Posts:
    122
    Same with me in unity 2020.2.4
     
  15. Personuo

    Personuo

    Joined:
    Mar 19, 2014
    Posts:
    122
    I use camera.render to control the rendering process, but I get a 188b gc every render.
     
  16. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
  17. Ramnjit

    Ramnjit

    Joined:
    Nov 13, 2016
    Posts:
    4
    Any updates on this? I'm getting 188b gc on every frame.
     
  18. Justin15620

    Justin15620

    Joined:
    Jun 22, 2020
    Posts:
    2
  19. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    659
    I think this must be fixed -I really hope it will be backported soon.

    I am also getting this. Has anyone come up with any work around?
     
    Last edited: Aug 26, 2021
  20. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    75
    Same issue , 78.6KB in empty scene (10.7.0) . Any ideas ?

    Screenshot 2021-10-15 233552.png
     
  21. Nileshk1080

    Nileshk1080

    Joined:
    Jul 4, 2022
    Posts:
    3
    Same issue I getting 140 kB GC
     
  22. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    75
    As far as I remind , the problem was the simulator tab (you can see in my editor , was open) .
     
  23. horatiu665

    horatiu665

    Joined:
    Nov 22, 2012
    Posts:
    24
    I still get GC Allocs from this internal URP function on android builds, so it is probably not the simulator. but I haven't figured out what it is yet. I do seem to have a SimulatorCamera in the editor play mode, which I hope doesn't get spawned in a build (I haven't checked yet) but I do have a camera that renders particles to a render texture to be used in UI, so I guess that means I have more than one camera.
    Unity 2022.2.19f1 and URP 14.0.7