Jump to content

Texture hasAlpha performance issue


Dreik
 Share

Recommended Posts

Recently I enountered an issue with texture has alpha performance. Public attribute 'hasAlpha' is 100 times slower than '_hasAlpha'. Does anyone know why is that and if there is a way to easily fix it? I can't release code to production with private attribute setter.

I made two playground to show the problem.

http://www.babylonjs-playground.com/#9FMJCS - public hasAlpha
80411 ms

http://www.babylonjs-playground.com/#R8QY81 - private _hasAlpha
787 ms

Link to comment
Share on other sites

https://github.com/BabylonJS/Babylon.js/blob/4b63e18d3dbd31d400fff1cba68de2981f8ba2ee/src/Materials/Textures/babylon.baseTexture.ts#L16

https://github.com/BabylonJS/Babylon.js/blob/4b63e18d3dbd31d400fff1cba68de2981f8ba2ee/src/babylon.scene.ts#L6001

Setting has alpha seems to mark all materials in the scene as dirty. So in this case it's calling markAllMaterialsAsDirty a thousand times, each time with a larger materials array to loop over.

You could get a compromise of behaviour by just setting the private _hasAlpha and then calling markAllMaterialsAsDirty after the loop.

http://www.babylonjs-playground.com/#R8QY81#1

Link to comment
Share on other sites

If you're meaning it won't build (i.e. you're using typescript) you can cast to any to get around that (the only downside being if the variable name or behaviour changes in a future version of babylonjs).

Link to comment
Share on other sites

Hi,

If you don't need the shader recompile,
you can also add another prototype that doesn't call markAllMaterialsAsDirty

http://www.babylonjs-playground.com/#9FMJCS#1
tests; 3085, 2970, 3156

And even if you do need the recompile, you should be able to just call it after the last one, to only call markAllMaterialsAsDirty once.

http://www.babylonjs-playground.com/#9FMJCS#2
tests; 3432, 3462, 3486

Link to comment
Share on other sites

Ahh typeScript.. i think.. that would require you adding it to the baseTexture source..
I don't know if you can add properties from outside the object in typeScript.
 

public set hasAlphaOverwrite(value: boolean) {
  if (this._hasAlpha === value) {
    return;
  }
  this._hasAlpha = value;
}
public get hasAlphaOverwrite(): boolean {
  return this._hasAlpha;
}


If casting to any also does the trick, then that would probably be better.
Have a nice day :) 

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...