Robinseibold / Unity-URP-Outlines

A custom renderer feature for screen space outlines
MIT License
590 stars 51 forks source link

Huge memory leak and crash in Unity 2023.2.2 #26

Closed XtroTheArctic closed 8 months ago

XtroTheArctic commented 10 months ago

Hello. Is this project still active and alive?

I was using it in my Unity 2022 project for a while but now it's causing huge memory leak and crashing the Unity 2023.2.2 (sometime entire computer).

Are you going to be able to fix it for the new Unity versions?

Robinseibold commented 10 months ago

Hello. Is this project still active and alive?

Yes, I'm still using it in my project (Unity 2021.2 I think) and have added a few improvements, all though none of which has made it to this repository yet.

I was using it in my Unity 2022 project for a while but now it's causing huge memory leak and crashing the Unity 2023.2.2 (sometime entire computer).

That doesn't sound good, are you sure that this renderer feature is what causes it, if so, do you have more information that would make it easier for me to reproduce or debug?

XtroTheArctic commented 10 months ago

Oh great! It's good to hear from you.

When I upgraded my project to 2023.2.2, I had few big problems. The biggest one was this memory leak and crash issue. I was very demoralized because I thought it was caused by Unity itself. Later I was able to narrow the problem down to this outline renderer. Since I removed the rendered from my URP asset, my project is working fine and I was able to fix other big problems too.

Now that you asked, I re-added the outline renderer to URP asset and it crashed my computer again.

This is how my game and memory usage look like normally: image

And this is how it looks after adding outline renderer: image

Please see that the game view is empty. No 3D object is being rendered except some light emitting window frames. Memory usage is growing rapidly for like 10-15 seconds before Unity and Explorer.exe crashes along with multiple screen blacks-out in a very flickery fashion. Scary stuff.

Were you able to try it in 2023.2.2 by yourself and had no problem?

Robinseibold commented 10 months ago

I haven't used this renderer feature in any other unity version than 2021.2 (it's this version I'm using for my project).

From information I've gathered however, some methods in this project seems to be obsolete/deprecated in newer versions of Unity (see for instance #15), so I assume you have done some changes/updates to make it run at all on 2023.2.2?

XtroTheArctic commented 10 months ago

No I haven't made any changes. I wouldn't know how. I'm not a graphics/shader programmer. All I experience with it is the instant huge memory leak and crash.

Would you be kind enough to update it for newer Unity versions like 2023.2.2.

I have no idea how this scriptable render pipeline thing works.

Robinseibold commented 10 months ago

Okey! That might have something to do with it.

Currently I'm buried in work, but will hopefully have some time in February to update this project.

Others are of course welcome to create a pull request if they have created an updated version.

XtroTheArctic commented 10 months ago

Hmm... Like I said, I'm not a graphics programmer but I can try to spend some time on this if you can guide me a little.

Today I noticed that the memory leak happens much faster when I increase the outline scale from 1 to 1.8. I think I remember setting it up like 4 caused my computer to explode so I am scared to go above 1.8 while testing this problem.

Do you have any idea what may be causing this and where I can start looking at?

XtroTheArctic commented 10 months ago

Oh by the way!!! I remembered I really made some changes on it to be able to get rid of some compiler errors. I totally forgot about that when you first asked.

image image

XtroTheArctic commented 10 months ago

Maybe I need to deallocate for those new alloc calls, do you think? In the old Unity version, the methods you called (Init and new RenderTargetIdentifier) don't allocate anything I guess, or they are getting deallocated automatically somehow. Does this make sense?

XtroTheArctic commented 10 months ago

https://docs.unity3d.com/Packages/com.unity.render-pipelines.core@12.0/manual/rthandle-system-using.html

In this documentation, it says we need to call myRTHandle.Release but when would we need to call this? Maybe in OnCameraCleanup ?

XtroTheArctic commented 10 months ago

Hey!! I think I fixed the memory leak problem. By looking at the related changes in this official repo, I called normals.Release in Dispose override and I called temporaryBuffer.Release in OnCameraCleanup. https://github.com/Unity-Technologies/Graphics/commit/d48795e93b2e62d936ca6fca61364922f8c91285#diff-922e8cb79285d375cad88e469448eebfc9aac59f470810a027d6d70113ed6a4b

Now I need to understand why would my game look like this: image

Do you have any idea?

XtroTheArctic commented 10 months ago

Here is the modified cs file for 2023.2.2 in case you wanna try it by yourself and try to debug the visual problem I posted above: ScreenSpaceOutlines.zip

XtroTheArctic commented 10 months ago

I made a discovery about the visual problem.

In an empty scene, the camera with solid color background renders a cube just fine. image

But as soon as I change the camera background to "skybox", it just renders the skybox and not the cube. image

This happens only when ScreenSpaceOutlines is enabled on the renderer asset.

XtroTheArctic commented 10 months ago

Oh my!! I found another problem which is very weird.

When ScreenSpaceOutlines is enabled on the renderer asset, the buttons and everything else on the top bar of the scene view disappear! And a white background box appears behind the coordinate system thing. This happens even in Edit mode. All other problems I reported were only happening in Play mode.

image

XtroTheArctic commented 10 months ago

Hello again. I'm stuck in my attempt of fixing the outline effect. I tried many things but can't get my head around the problem I showed in my screenshots above.

Can you please check the modified script in the zip file?

XtroTheArctic commented 10 months ago

You can see my further attempt here: https://forum.unity.com/threads/rendererfeature-shader-got-broken-after-upgrading-to-unity-2023-2-2.1536004/#post-9588430

But the problem persists :(

Here are the latest changes and the latest version of the script: image

ScreenSpaceOutlines.zip

adrian-miasik commented 9 months ago

https://docs.unity3d.com/Packages/com.unity.render-pipelines.core@12.0/manual/rthandle-system-using.html

In this documentation, it says we need to call myRTHandle.Release but when would we need to call this? Maybe in OnCameraCleanup ?

I've noticed calling release of the render texture handle in OnCameraCleanup seems to break the Unity editor toolbar UI. Seems like common practice now is to dispose of the handles when the rendererfeature gets disposed of. Has an override. See: https://www.cyanilux.com/tutorials/custom-renderer-features/#rthandle

XtroTheArctic commented 9 months ago

Hi again @Robinseibold.

As you may see in the conversation of the other issue ticket, Chishikii's version of your outline shader doesn't work as it should. Please see my city builder screenshots in the other issue.

Chishikii says that the outline shader in the new Unity version must be a fullscreen shader instead of unlit. I'm no expert in this area but I feel like we still need an unlit shader. Do you have any idea about solving this problem?

XtroTheArctic commented 9 months ago

Chishikii fixed my problem in his version of the outline feature: https://github.com/Chishikii/URP-Render-Features/issues/1