Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.JS: Reference of texture mipmaps and webGL

I have a model with lots of high quality textures and I try hard to keep the overall memory usage down. One of the things I tried, is to remove the mipmaps after they got pushed to the GPU, in order to releadse the texture data from common RAM. When doing so, the model is still rendered with the once uploaded mipmaps texture. So thats fine, but the memory doesnt drop at all.

material.mipmaps.length = 0;

So my question is:

Is there a reference to the mipmaps kept by ThreeJS, that the garbace collector can't release the memory. Or is the texture referenced by webGL itself, which seems kind of strange, as webGL lets me think textures are always used in dedicated memory and must therefore be copied. If webGL keeps a reference to the original texture in the RAM, does webGL, would behave different on a desktop with a dedicated graphic card and a laptop with an onboard graphic card sharing the common RAM.

Would be really glad if some one can explain me whats going on inside of threeJS/webGL for texture references.

like image 453
Markus Siebeneicher Avatar asked Nov 11 '22 06:11

Markus Siebeneicher


1 Answers

That's a good question.

Let's go down there...

So normally you'd dispose() a texture when you want it to be kicked out of the VRAM. Tracing what that does might bring us to an answer. So what does dispose do?

https://github.com/mrdoob/three.js/blob/2d59713328c421c3edfc3feda1b116af13140b94/src/textures/Texture.js#L103-L107

Alright, so it dispatches an event. Alright. Where's that handled?

https://github.com/mrdoob/three.js/blob/2d59713328c421c3edfc3feda1b116af13140b94/src/renderers/WebGLRenderer.js#L654-L665

Aha, so finally:

https://github.com/mrdoob/three.js/blob/2d59713328c421c3edfc3feda1b116af13140b94/src/renderers/WebGLRenderer.js#L834-L837

And that suggests that we're leaving THREE.js and enter the world of raw WebGL. Digging a bit in the WebGL specs here (sections 3.7.1 / 3.7.2) and a tutorial on raw WebGL here and here shows that WebGL is keeping a reference in memory, but that isn't a public property of the THREE.js texture.

Now, why that goes into RAM and not the VRAM I don't know... did you test that on a machine with dedicated or shared GPU RAM?

like image 67
geekonaut Avatar answered Nov 15 '22 05:11

geekonaut