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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Allocations with multiple cameras

Discussion in 'Universal Render Pipeline' started by vinchkovsky, Feb 26, 2023.

  1. vinchkovsky

    vinchkovsky

    Joined:
    Oct 9, 2016
    Posts:
    14
    Hi,

    I'm using Unity 2022.2.8f1 and I've noticed that using multiple cameras causes huge reallocations depending on camera's resolution (around 15KB/frame in my camera setup). Additional cameras are rendering into textures.

    I found similar complains, like this: https://forum.unity.com/threads/just-by-having-a-second-camera-causes-gc-kick-in-urp.930594/

    But it seems I have completely different issue. Some textures like m_CameraDepthAttachment are reallocated each frame for each camera in UniversalRenderer.Setup(). Either because textures have different size (in case of cameras are sharing the same renderer) or are disposed after previous camera (in case of all cameras have their own renderer)

    I tried different approaches without success. Then I checked official URP samples, like TrailEffect, and found the same issues there. So it seems I'm doing everything alight.

    So I have few questions:

    1. Could you please confirm that these allocations are not okay? Or maybe that's an expected behavior?
    2. If first, is there a way to fix this?

    [Edit] Just checked same demo in 2021.3.18f1 and allocation is 0KB per frame. So is this a bug?

    Thanks
     

    Attached Files:

    Last edited: Feb 26, 2023
    jacknero likes this.
  2. JohnHacker

    JohnHacker

    Joined:
    Oct 27, 2012
    Posts:
    44
    Hi! I saw this behavior too and it's caused by this piece of new code on the UniversalRenderPipeline.cs script:

    Code (CSharp):
    1. if (!isStackedRendering)
    2.                 {
    3.                     for (int i = 0; i < rendererCount; ++i)
    4.                     {
    5.                         var currRenderer = asset.GetRenderer(i);
    6.                         if (baseCameraData.renderer != null && !currRenderer.hasReleasedRTs && baseCameraData.renderer != currRenderer)
    7.                         {
    8.                             currRenderer.ReleaseRenderTargets();
    9.                         }
    10.                     }
    11.                 }
    This code is making the RTs to be released every frame so they should be requested again (every frame, meaning GC). Also, even fixing this bug (which is pretty big), the FPS are worse in 2022.2.8 (URP 14) than in 2022.1.17 (URP 13). It would be great to have official info about these issues. Thank you!
     
    ElliotB and dencesar37 like this.
  3. blankkeys

    blankkeys

    Joined:
    Sep 21, 2019
    Posts:
    7
    Running into this issue as well, with 18kb allocations per frame as soon as a second camera is enabled. I managed to track it down to the same change mentioned by JohnHacker above and thought based on the final condition `&& baseCameraData.renderer != currRenderer` that I'd be able to avoid the allocations by giving the two cameras the same renderer asset but alas, didn't work out.

    This is a pretty big issue for my game as I had up to three cameras active. I was able to refactor one camera away (yay!) but that's about as far as I can take it. The garbage generated and RTs being recreated every frame here is tanking performance.

    As an aside, the second camera even seems to generate shadow textures (Inl_Setup Additional Shadows & Inl_Setup Main Shadowmap) even though it has Render Shadows = false o_O
     
  4. Jonas_Sid

    Jonas_Sid

    Mobile Graphics Developer Unity Technologies

    Joined:
    May 13, 2015
    Posts:
    44
    Hey,

    I'm sorry for all the inconvenience - we have caught this issue already and the fix is on the way and we'll be backporting it to all versions this change affects
     
    funkyCoty and KimmoFactor like this.
  5. blankkeys

    blankkeys

    Joined:
    Sep 21, 2019
    Posts:
    7
    Thanks @Jonas_Sid, that's great news. Any idea of a release timeline or too soon to say?
     
  6. Jonas_Sid

    Jonas_Sid

    Mobile Graphics Developer Unity Technologies

    Joined:
    May 13, 2015
    Posts:
    44
    A bit too soon - if all goes fine, the fixes should land next week on our end but I can't say when it'll be actually released in a public release
     
    blankkeys likes this.
  7. KimmoFactor

    KimmoFactor

    Joined:
    Dec 29, 2020
    Posts:
    38
    Can anyone confirm if the fix has landed in a public release (2022.2.14)? I've been waiting for this before trying 2022.2 again and haven't been able to spot anything in release notes, but I might just be blind. Any updates @Jonas_Sid?
     
    Last edited: Apr 12, 2023
  8. Jonas_Sid

    Jonas_Sid

    Mobile Graphics Developer Unity Technologies

    Joined:
    May 13, 2015
    Posts:
    44
    Hi! Yeah, it has landed in 2022.2.15f1
     
    KimmoFactor likes this.
  9. WavyRancheros

    WavyRancheros

    Joined:
    Feb 5, 2013
    Posts:
    176
    Are you certain this is fixed? I use 2022.3.4f1 and I added a second camera rendering into a RenderTexture for a mini map, and I get the most horrible performance drops ever witnessed.
     
  10. victornor

    victornor

    Joined:
    Jan 17, 2014
    Posts:
    46
    Same issue here on 2022.3.4f1.
    Main camera uses 1/3rd fps of my second camera, which renders to RenderTexture for water reflections...
     
    dencesar37 likes this.
  11. nagasaidandu

    nagasaidandu

    Joined:
    Sep 26, 2015
    Posts:
    3
    I'm using 2022.3.4f1, when I add a secondary camera to render my character into renderTexture. Frame rates are getting drops from 300+ to 30 frames. Is there any solution for this ?
     
    dencesar37 likes this.
  12. omerfirat

    omerfirat

    Joined:
    Aug 1, 2017
    Posts:
    4
    I would like to point out that this problem persists in version 2022.3.4f1. Currently, Render Texture cannot be used in any way and there is an incredible fps drop in used situations. I hope there will be an update on this topic soon.
     
    dencesar37 likes this.
  13. dar_in3d

    dar_in3d

    Joined:
    Aug 2, 2022
    Posts:
    1
    Is anyone aware of when Unity is planning to release a fix in version 2022.3?
     
  14. dencesar37

    dencesar37

    Joined:
    Aug 23, 2021
    Posts:
    5
    Hi guys, 2022.3.4f1 the same problem. FPS drops to unstable 8-12-20-40 from around 150 when I add/activate a render camera to render a particle system for my water shader. It's a game breaking bug. Unity please fix in the next update.
     
  15. Viva-La-Baz

    Viva-La-Baz

    Joined:
    Jun 21, 2014
    Posts:
    37
    issue still persists in 2022.3.5f
     
    dencesar37 likes this.
  16. ameree2

    ameree2

    Joined:
    May 21, 2018
    Posts:
    9
    Bumping this thread because it may be related to mine. I was seeing reasonably large allocations when a second camera is enabled, but it seems to have something to do with strings...
     
  17. dencesar37

    dencesar37

    Joined:
    Aug 23, 2021
    Posts:
    5
    Does the situation get equally worse if you enable the 3rd camera?
     
  18. Tanknspank

    Tanknspank

    Joined:
    Oct 25, 2017
    Posts:
    2
    Hey all,
    Seem to be experiencing this issue on 2022.3.5f1 as well.
    Game runs fine on ~150fps (in editor) until I open my UI and render a camera to texture / raw image, it then drops to 6~25 fps range.

    (To clarify: If the camera is active in the scene the issue occurs, regardless of if the UI is active or not, I just disable it when the UI is not active at the moment).

    16.7KB CG Alloc for PlayerLoop at the moment.


    Anyone found a fix for this, or any additional info?
     
    Last edited: Sep 1, 2023
  19. Tanknspank

    Tanknspank

    Joined:
    Oct 25, 2017
    Posts:
    2
    Well I upgraded to 2022.3.8f1, This particular issue is still there

    However I made an attempt to build my project, and it seems to run relatively well outside of the editor. Can anyone else confirm this is an editor only issue?
    I didn't run any profiling for the build itself so I can't tell whether or not this will cause me any problems in the future.
     
  20. dencesar37

    dencesar37

    Joined:
    Aug 23, 2021
    Posts:
    5
    No fix, but new pricing lol. I'm likely switching to unreal...
     
  21. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    150
    I can agree on the first part! Unity please fix this!

    https://gyazo.com/9eef34275ed5f8f7963556752c76193b

    4.8 GB of render textures? I only have a camera stack in my scene and in this particular snapshot there was a sight attachment on a weapon that is using a camera rendering on a render texture. That long DBufferDepth_1024x1024 is it. And it's repeating forever in the memory profiler, allocating 8mb each time.

    Also i've noticed extra render textures for the camera when switching resolutions, as someone mentioned above.

    Also my game takes 2.6gb ram in total if i launch it and play it, if i open up the menu it increases to 3 (no extra camera, nothing just the camera now sees UI, the canvas is screenspace and it renders on that very same camera). If i close the menu this memory is not released.

    I will probably open a new thread with more in-depth details, but let me clarify this:


    https://gyazo.com/d7d10a9961ea79a020c64055d7faa054

    B is if i open the game, play for hours or whatever and not open the menu. So the main camera renders the world with render textures worth of 0.7gb.

    After i open the menu (see A) the game gets flooded with render textures that are never released.

    Please fix this! Our game has been called a bitcoin miner for this crappy memory management. We've implemented addressables to unload memory from 25+ GB and now we have 5GB worth of render textures?
     
    Last edited: Sep 15, 2023
  22. islaitala

    islaitala

    Joined:
    Sep 6, 2017
    Posts:
    3
    Having the exact same problem in 2022.3.4f1
    Generating around ~10KB of garbage/frame with minimap camera & base.