Skip to content

Conversation

@jsantell
Copy link
Contributor

@jsantell jsantell commented Nov 28, 2018

Fixes #15288.

Due to the nature of the APIs of EquirectangularToCubeGenerator, PMREMGenerator, and PMREMCubeUVPacker, to generate multiple textures, a new instance is needed for each, where currently, each instance of these generators utilize their own scene/camera, accumulating WebGLStates for each render (see #15288 for details). This only addresses the root of the memory leak, not any of the API changes in #15288.

This change wraps each generator in a closure so that multiple instances of a generator reuse the same scene/camera, eliminating the WebGLState accumulation. The shader is also reused in EquirectangularToCubeGenerator and PMREMGenerator, and cloned in PMREMCubeUVPacker, which while the webgl program should be reused across instances, there was still some accumulation with regard to the instantiation of new materials.

Here's a rather pathological test case of generating a new EquirectangularToCubeGenerator, and passing through a PMREMGenerator and PMREMCubeUVPacker once every second; drop it in examples/:

https://gist.github.com/jsantell/c1e13139cfdd8526ba28a5078d608464
(L155-L194 is the loop of interest)

Before this patch, most memory that is allocated is retained across 3 minutes and things started to slow down:

screenshot from 2018-11-27 18-05-55

After this patch, nothing beyond compiled code is generated and retained, or at least, far less over the course of 8 minutes:

screenshot from 2018-11-27 17-57-34

Post-patch, the total retained memory hovered around 6MB, taking GC into consideration. Previously, it was hard to tell due to more GC spikes and seemingly larger peaks, but best case several more MBs.

While this could be an extreme scenario, and wasn't my main concern, the amount of allocation size saved after this patch is 98%.

screenshot from 2018-11-27 18-07-26

Can confirm with webgl_materials_envmaps_[exr|hdr].html examples for correctness

…ubeGenerator, PMREMGenerator, and PMREMCubeUVPacker to eliminate memory leak from accumulating WebGLStates. Fixes mrdoob#15288.
@mrdoob mrdoob added this to the r99 milestone Nov 28, 2018
@mrdoob mrdoob merged commit 3b8869c into mrdoob:dev Nov 28, 2018
@mrdoob
Copy link
Owner

mrdoob commented Nov 28, 2018

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants