Deltakosh

Material push mode: The new big optimization

Recommended Posts

Hello team!

Since the very first version of Babylon.js materials are working in a pull mode. This means that every time a mesh needs to be rendered, its material has to ask the engine about a lot of information in order to compile the best shader possible for the given mesh.

For instance, here is an incomplete list of states required to compile a shader:

- Lights affecting the mesh (taking in account inclusion and exclusion lists and masks)

- Textures and channels (including associated parameters like parallax for bump or reflection mode for reflections)

- Fog, clip planes, point rendering or wireframe

- Mesh attributes (does the mesh contains UV? tangent? colors?)

This leads to a pretty expensive function named Material.isReady.

Due to the previous reasons, the function is really CPU intensive. Here is for instance the profile of the "materials" playground:

profile1.thumb.JPG.6b4ba333f88827d6e2d2d3501a8d7706.JPG

The highlighted functions are part of the IsReady root function. The scene is extremely simple and you can see that the isReady is taking almost 13% of the CPU time

While it was ok because of the complexity of the task assigned to this function, I decided recently to change it to a clever model: The push mode.

When a material is in this mode, instead of asking on every frame for every state, the states will push updates to the material when they change.

Obviously the performance boost is immense. Look at the same profile with the new mode:

Capture.JPG.e2455939563302f705b97e9f8a3623ea.JPG

We are now at 2.51%. On bigger scene like Sponza, this function could take up to 30%:

Capture2.JPG.f03af3f6c8e2061acd005c0bd3752b04.JPG

And now with the new mechanism:

Capture3.JPG.4dd582a651f77ec13ff012d145f55298.JPG

ONLY 3%!!!!!!!!!!

But obviously with such a big change, there will be some bugs.. So please use this thread to report any issue with StandardMaterial.

If everything is fine, I'll update the PBR to push mode in one or two weeks.

Associated PR: https://github.com/BabylonJS/Babylon.js/pull/1959/

Share this post


Link to post
Share on other sites

Hello @Deltakosh,

I'm currently working on implementing UBO (uniform buffer objects), and i merged the most recent changes about StandardMaterial becoming a PushMaterial.

Everything is now working fine, but I'm encountering performance issues against the unmerged version.

My focus currently goes onto frozen materials, since they are the most optimized way to use UBO : 1 uniform buffer per material, which is static.

 

I've tested with a simple scene with 1600 visible spheres, each one having a different material, lit by a moving HemiLight, here are the results :

 

UNMERGED (StandardMaterial is still NOT a PushMaterial), with FROZEN materials :

frozen-without-push.thumb.PNG.61891c112f2e97eb530786a535d7dfd0.PNG

 

MERGED (StandardMaterial IS a PushMaterial), with FROZEN materials : 

frozen-with-push.thumb.PNG.12b9433fc74872ebbd3447c70e0cd7a2.PNG

 

MERGED (StandardMaterial IS a PushMaterial), with UNFROZEN materials : 

unfrozen-with-push.thumb.PNG.9a2a519768fd31323a0a82357b03aabd.PNG

 

As you can see on profiles, a lot of time is spent on getter functions, most likely because they contain 

get: function () {
    return this["_" + key];
},

which is really slow in JS.

 

So my question is : do you have any idea on how to prevent that slow-down to happen on frozen materials ? Maybe i am missing something in my UBO implementation code to deal with that ?

Btw, It's really likely that unfrozen material spend less time in "isReady" functions, so are still more optimized despite the heavy "get" functions. 

 

Thanks for your kind advices ;)

Share this post


Link to post
Share on other sites

We are indeed far better :) No more intensive CPU usage because of getters and GC.

I also took the initiative of replacing disableLighting and cameraColorCurves by their respective private properties _disableLighting and _cameraColorCurves.

 

Thanks for you help, I'll keep you updated of the following implementation of UBO for materials.

Share this post


Link to post
Share on other sites
4 minutes ago, reddozen said:

Is this in the latest nightly build?

Hi,

As far as I know, yes material push mode is in the latest build for Standard Material.
Not for PBRMaterial yet.
And materials from materialsLibrary (water, fur, etc.) also are in push mode. :)
 

Share this post


Link to post
Share on other sites

@Deltakosh

Hey I'm not sure I understand.  

The standardMaterial in 3.3 alpha is a push material?  Or is it, its own?, oh or did you mean standardMaterial implements pushMaterial or idk, I'm very curious...

I see that push material has its own contructor but I can't seem to set a colour to it with diffuseColor, emmisiveColor, or ambientColor.

There also doesn't seem to be a doc on the pushMaterial so I'm assuming its only meant to be part of standard material?

Also is this an effective way to increase performance of only a handful of materials or is this only for large amounts?  Say 5 different materials?  Assuming the lighting changes and nothing else(used material.freeze()).

:D 

 

 

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.