wave-harmonic / crest

A class-leading water system implemented in Unity
MIT License
3.4k stars 475 forks source link

Updating ShapeFFT parameters runtime, generates a lot of garbage #1119

Closed powderek closed 1 year ago

powderek commented 1 year ago

Is there an existing issue for this?

Have you checked the documentation to resolve your problem?

Current Behavior

Changing waves fields in runtime produces a lot of RenderTexture garbage e.g. ShapeWaves._waveDirectionHeadingAngle, ShapeWaves._windSpeed as shown in profiler below. Updating wind direction during gameplay is not possible because of this issue.

ShapeWaves._weight doesn't produce garbage.

Unity_3n8EvFbpVx Unity_ki0EWS0Y30 Unity_YgJDOEnhgV

Expected Behavior

No response

Steps To Reproduce

  1. Create empty project with Crest.
  2. Create empty scene.
  3. Add OceanRenderer and ShapeFFT.
  4. Change ShapeWaves._waveDirectionHeadingAngle in Update.
  5. Observe profiler.

Unity Version

2021.3.11

Crest Version

4.17.3

Render Pipeline

Built-In

Editor or Standalone

Editor (Play Mode), Standalone

Environment

- OS: Windows 10
- GPU: RTX 3060
- Graphics API: Direct3D11
- Target Platform: Windows PC

Anything else?

No response

daleeidd commented 1 year ago

Are you able to reproduce the issue by changing the same value through the inspector?

powderek commented 1 year ago

Yes, the issue still occurs when changing values through the inspector.

I actually found the problem in the code. In FFTCompute.cs there is method GenerateDisplacements. This method is checking if new values are different from previous values and if they are it is creating new generator with new render textures, without removing the old one. Memory allocation is piling up pretty quick and is leading to imminent crash. I fixed it for my case by changing some conditions but it would be good to see a generic global solution.

daleeidd commented 1 year ago

Strange. I wasn't able to reproduce and if it does affect the UI as well then I would definitely have hit this. That method will only create a new generator if resolution or loop period has changed otherwise it repurposes the existing generator.

Is it reproducible in the main scene?

powderek commented 1 year ago

The problem doesn't exist in the main scene. I compared the setup and it turns out the problem happens only if Spectrum field on ShapeFFT component is left empty. If it is assigned then memory leaks doesn't happen.

daleeidd commented 1 year ago

Thank you for narrowing that down. Fixed with above commit.