Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory usage for environment map manager #12370

Merged
merged 2 commits into from
Dec 20, 2024
Merged

Conversation

ggetz
Copy link
Contributor

@ggetz ggetz commented Dec 11, 2024

Description

Changes include:

  • Adjust default number of mipmap levels for the specular maps. This trades off a bit of visual quality for a MUCH smaller overall memory footprint by default.
  • More aggressively destroy textures rather than peristing them for future updates.
  • Fixes memory leaks.

Issue number and link

Addresses #12356

Testing plan

I found it really helpful to use this webgl memory tool for testing. Thanks to @javagl for pointing it out!

await import("https://greggman.github.io/webgl-memory/webgl-memory.js");

// Create viewer as normal

viewer.scene.debugShowFramesPerSecond = true;
Sandcastle.addToolbarButton("Check memory", function () {
  const ext = viewer.scene.context._gl.getExtension("GMAN_webgl_memory");
  if (ext) {
    // Overall memory info
    const info = ext.getMemoryInfo();
    console.log(info);
    // Every texture, it's size, a stack of where it was created and a stack of where it was last updated.
    const textures = ext.getResourcesInfo(WebGLTexture);
    console.log(textures);
  } else {
    console.log("There was an issue loading the extension");
  }
});

Texture memory results

Scenario Before (MB) After (MB) % change
Procedural IBL 110.334 91.591 -16.99%
Unlit boxes – 5 cubed 2065.118 162.067 -92.15%
without reoloading, back to 1 cube 156.448 128.029 -18.17%

Author checklist

  • I have submitted a Contributor License Agreement
  • I have added my name to CONTRIBUTORS.md
  • I have updated CHANGES.md with a short summary of my change
  • I have added or updated unit tests to ensure consistent code coverage
  • I have updated the inline documentation, and included code examples where relevant
  • I have performed a self-review of my code

@ggetz ggetz requested a review from jjspace December 11, 2024 19:38
Copy link

Thank you for the pull request, @ggetz!

✅ We can confirm we have a CLA on file for you.

@jjspace
Copy link
Contributor

jjspace commented Dec 11, 2024

@ggetz when I load the Unit box sandcastle locally and change from unlit 1 to unlit 5 back to unlit 1 I get an error that an object can't be destroyed because it already was

2024-12-11_16-27

pixelFormat: PixelFormat.RGBA,
});
manager._irradianceMapTexture = texture;
if (defined(texture)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be checking isDestroyed() first? Some places in this PR you do and some you don't. One of these might be the cause of the error I mentioned above.

Also worth noting that this is exactly why I had questions about this pattern #12266

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout this class, I've looked to set any destroyed property back to undefined to avoid the need for the check.

I think #12266 is still worth considering, but I think for the purposed of this PR and to keep the scope small I'd like to avoid addressing it here.

@jjspace
Copy link
Contributor

jjspace commented Dec 11, 2024

I will also note that I do see a pretty significant reduction in memory used on my system as a whole when loading the 100 models example from the forum thread locally

@javagl
Copy link
Contributor

javagl commented Dec 11, 2024

As a response to #12320 (comment) :

In that issue, I said "I'll probably try again", so I just tried out the current main state (!), and there, I could still cause the crash with "10 cubed".

And now I tried out this branch, and there, I could not cause that crash. So the main issue seems to be fixed.

I also saw the destroy()/isDestroyed() issue that jjspace mentioned. But I assume that this is just a detail - either of checking isDestroyed at the right place, or of handling this as suggested in #12266

I did not fully think through the code changes. But it seems like to combination of the reduced number of levels (which I think go in exponentially into the memory consumption!) and the approach of "eagerly destroy()ing things" seems to solve this one.

And... since "Testing software" should usually not mean "Checking that it works" but rather "Trying to break it", here's a small outtake from that:

Cesium EnvMapMemory 50

@ggetz
Copy link
Contributor Author

ggetz commented Dec 13, 2024

Thanks @jjspace and @javagl! The error you were seeing should now be resolved. There was an issue with how we were handling canceled commands.

@ggetz
Copy link
Contributor Author

ggetz commented Dec 17, 2024

@jjhembd or @lukemckinstry, could one of you take a final review pass and merge if happy? I think it'd be helpful to get this into main soon given the holidays.

Copy link
Contributor

@lukemckinstry lukemckinstry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test cases look much improved, and I agree with the logic of the fix 👍

@lukemckinstry lukemckinstry added this pull request to the merge queue Dec 20, 2024
Merged via the queue into main with commit 47b5d29 Dec 20, 2024
9 checks passed
@lukemckinstry lukemckinstry deleted the env-map-memory branch December 20, 2024 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants