Search Unity

Just by having a second camera causes GC kick in. URP

Discussion in 'Universal Render Pipeline' started by FlightOfOne, Jul 13, 2020.

  1. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    I use camera stacking for my project. I have a second camera (for UI and other things that needs to get drawn over) that was set as an overlay camera in the stack.

    I figured out what causes the GC. It is just simply having a second camera. Stacked or not, base or overlay does not matter. Strange thing is, it doesn't even need to render anything. I turned off or disable everything on the camera and yet still GC shows up. Just being there is all it takes.

    This is an empty project with the default URP work bench scene.

    Am I doing something wrong here, how do I fix this?

    Take a look at the screenshot.
    upload_2020-7-13_7-22-36.png

    upload_2020-7-13_7-24-6.png
     
    Last edited: Jul 13, 2020
  2. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    I also did a bug report.
     
  3. erikabar

    erikabar

    Unity Technologies

    Joined:
    Jan 26, 2017
    Posts:
    36
    Hey! Can you share your bug report number please?
     
  4. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    @erikabar

    Here you go: Case 1262797

    I already got two responses back. One, pointing to this (https://issuetracker.unity3d.com/is...a-render-texture-creates-gc-alloc-every-frame) and stating that this is fixed for Unity 2020 and URP 9.x.x. This does not help those who have 2019.4 LTS and URP 7.x.x.

    Second, saying they opened "a backport case to the 2019.4 stream and hopefullyo_O, it will be fixed in a separate package for the 2019.4 URP line (7.x.x)".

    Which is great but the world 'hopefully' worries me though and makes me wonder if there is a chance that this would not get fixed for those with 2019.4 and 7.x.x. Which would make it impossible to use the camera stack or multiple cameras in your project without significant performance consequences, especially for those with mobile or VR.

    I will be happy to help test/troubleshoot any new packages, let me know!

    Thank you.
     
    Last edited: Jul 14, 2020
  5. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    FlightOfOne likes this.
  6. erikabar

    erikabar

    Unity Technologies

    Joined:
    Jan 26, 2017
    Posts:
    36
    sirshelley likes this.
  7. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Thank you! There will still be GC every frame in 2019.4 and URP 7.4.1 - SortCameras does not allocate GC, but GetCameras does when you have more than one camera.
     
  8. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668

    It is indeed fixed! Not only that this also fixed an issue where material previews in the project view was not showing just -always showed black.

    For a second there I thought my highly sophisticated, evil master plan to take over the VR world that entirely relied on camera stacking would crumble down to the ground, one spike at a time :D.

    Phew! I feel better now! You guys are awesome! thank you!!!
     
    Last edited: Jul 15, 2020
    erikabar likes this.
  9. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    I am not doing that thing (3 cameras outputting to one texture) you are doing with the render texture but I can confirm that I am not getting GC at all now. Have you tried outputting from only one camera to one render texture?
     
  10. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Good news that you're no longer having this problem! For me, any combination of more than one camera (whether it's base, overlay, rendertexture doesn't seem to matter) creates GC allocs in the GetCameras() function called by the RenderPipelineManager for me, even with URP 7.4.1 and 2019.4.4. What does your camera setup look like, out of interest?
     
  11. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    isn't this an internal call?

    Depends on the scene I am in, but mostly its 1 base camera + one stacked camera for UI + another stacked for VR hands.


    So if you were to start a brand new clean/empty project (with latest Unity 2019.4 and 7.41), and a scene that has only 2 cameras and nothing else, you still get GC?
     
    Last edited: Jul 15, 2020
  12. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Yes - creating a new project with the Universal Render Pipeline project and adding a new camera to the scene shows up the issue unfortunately.
     
  13. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    If you want to upload a zip file with that empty project I can take a quick look and try it on mine and see if it happens.
     
  14. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Hello! Check out this project, it's about as basic as I could make to recreate the problem. One scene, two cameras, GC Allocs in the profiler: http://www.unusualcadence.com/bugtest/URPCameraGCAlloc.zip

    urp gc alloc.png
     
  15. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    I tested it out and yes there was GC alloc! Then, I removed your URP and reinstalled it 7.4.1 and then the GC disappeared. Maybe there was something left over from 7.3.x, just remove and reinstall and it should fix the issue.
     
  16. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Hm! I've tried uninstalling and reinstalling the URP package in that test project and still get the GC allocs, nothing changes for me whatsoever. Are you sure uninstalling didn't remove the scriptable pipeline setting from the Graphics / Quality menu?
     
    Last edited: Jul 21, 2020
  17. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    All I did was remove and reinstall from the package manager.
     
  18. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Could you do me a massive favor and maybe share a test project where you have multiple cameras with no GC allocs? I'm seeing something odd in the frame debugger where draw calls from separate cameras are appearing under one 'Render Camera', and I'm not sure if that's intended behavior or not!
     
  19. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    Sure, I will later today.
     
  20. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    @Unusual-Cadence @erikabar

    So here is the thing. I was trying it out again as I was getting you the project and gc alloc showed up again! I then deleted the library, reopen and let everything reimport again -everything was fine, no gc. Then I tried on my main project and even that had the issue. Then I closed and reopened it and gc problem was no longer there... It seems to show up time to time and I just can't figure out what caused it. Then it was gone.

    I am only guessing here but some thing causes this, probably because of something we are doing (to cameras?) and when you do a reimport () it fixes it.

    @erikabar Is it possible for you to share with us what the cause and what the fix you guys did? is this somehow shader/material related? This way, instead of blindly trying everything, we can to narrow our focus to a certain area when we try to troubleshoot this?

    @Unusual-Cadence attached is the project and it does not have the GC issue, well at least when I tested it. Also, fyi -I deleted the library folder to reduce the size of this attachment and it might take extra time to rebuild/reimport.

    If you do manage to get the gc alloc again maybe share it again with Unity, so they can have a look at it? I hope we get to the bottom of this.
     

    Attached Files:

    Last edited: Jul 23, 2020
  21. Unusual-Cadence

    Unusual-Cadence

    Joined:
    Aug 12, 2015
    Posts:
    42
    Sadly I get the GC Allocs as usual with the attached project. Tested in 2019.4.5f with URP 7.4.1 and in 2020.1.1 and URP 8.2.0 for good measure. Same culprit - GetCameras().
     
  22. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    by the way I tested this on 2019.4.4.

    can you try deleting the library once more on the this project? Just trying to find a pattern.
     
  23. Justin15620

    Justin15620

    Joined:
    Jun 22, 2020
    Posts:
    2
  24. sirshelley

    sirshelley

    Joined:
    Aug 14, 2014
    Posts:
    26
    upload_2023-11-13_16-56-49.png

    Having the exact same issue on Unity 2023.1.19f1 and URP 15.0.7.
    This has completely wrecked our project, come on unity!