Jump to content

Disposing material nukes other materials using the same texture


fenomas
 Share

Recommended Posts

Hi Fenomas,

Yep, textures are being disposed together with the material (https://github.com/BabylonJS/Babylon.js/blob/master/src/Materials/babylon.standardMaterial.ts#L902) . There is an upside and a downside for doing that. The upside - no memory leaks when disposing materials (otherwise you will have to dispose the textures yourself). Downside is - see your PG link :) 

We'll try finding a nice solution without breaking anything.

Link to comment
Share on other sites

1 hour ago, RaananW said:

We'll try finding a nice solution without breaking anything.

Things are broken already! :P

That is, this seems like insane behavior. Disposing assets is the job of whoever is keeping track of whether they're in use - if Babylon isn't doing the latter then it clearly shouldn't be doing the former.

Link to comment
Share on other sites

Hello,

This is actually a feature :-)

Basically the way internal glTexture are shared is through the cache. In order to benefit from the cache, you need to either create another texture from the same url (this won t dl the texture twice but take it from the cache):

http://www.babylonjs-playground.com/#1VL9HD#2

or you could clone the texture to benefit from the same settings without copy pasting:

http://www.babylonjs-playground.com/#1VL9HD#5

The only drawback would be if you dynamically changes texture settings later on.

Link to comment
Share on other sites

3 hours ago, Sebavan said:

Basically the way internal glTexture are shared is through the cache. In order to benefit from the cache, you need to either create another texture from the same url

What, so if you reuse the same texture object for multiple materials then the internal glTexture doesn't get shared? Meaning multiple internal textures will get made?

Link to comment
Share on other sites

11 minutes ago, Deltakosh said:

Reusing the same texture or cloning it will share the same internal textures. Even creating a new texture with the same url will reuse the same internal texture

That's what I assumed. So is it intentional that the engine disposes assets that are still in use? I mean I can work around it, but like I said it seems like an insane default behavior.

Link to comment
Share on other sites

maybe:

public dispose(forceDisposeEffect?: boolean, keepTextures?: boolean): void {
    if (!keepTextures){
        if (this.diffuseTexture) {
            this.diffuseTexture.dispose();
        }

        if (this.ambientTexture) {
            this.ambientTexture.dispose();
        }

        if (this.opacityTexture) {
            this.opacityTexture.dispose();
        }

        if (this.reflectionTexture) {
            this.reflectionTexture.dispose();
        }

        if (this.emissiveTexture) {
            this.emissiveTexture.dispose();
        }

        if (this.specularTexture) {
            this.specularTexture.dispose();
        }

        if (this.bumpTexture) {
            this.bumpTexture.dispose();
        }

        if (this.lightmapTexture) {
            this.lightmapTexture.dispose();
        }

        if (this.refractionTexture) {
            this.refractionTexture.dispose();
        }
    }

    super.dispose(forceDisposeEffect);
}

 

Link to comment
Share on other sites

Welp, if you're all happy then I'm happy, but speaking to posterity I still think this is crazy default behavior. You wouldn't add a feature to make mesh.dispose() aggressively dispose the mesh's material right? Aggressively disposing the material's textures is the same sort of thing; it just blows up for fewer people (maybe automated things like Blender export don't reuse texture objects?).

I mean, either way works the same in a trivial demo. But in big projects assets aren't necessarily managed in the same place by the same logic - one module might create the texture and another the material. Usually you'd want the code that created an asset to manage when it's disposed, but 2.3 makes that no longer the default - it couples together what could be separate concerns.

 

Link to comment
Share on other sites

Whoa, I guess i'm being really unclear here!

DK: The change in 2.3 is shown in the PG in the first post - that disposing a material defaults to disposing its textures.

I'm not suggesting any big changes - I'm suggesting that the default should be reversed. Disposing one asset shouldn't implicitly dispose unrelated assets.

So the only change I'm suggesting is that the new "forceDisposeTextures" parameter should be false by default. (With the new name this makes even more sense - boolean parameters that default to true when omitted are just bad juju!)

JC: I don't know anything about that, but I could be overlooking something. (I don't do any serialization and I don't use any getByName methods, so I normally don't pay any attention to asset names.)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...