question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebGLLights state and its hashing produce unintended material reinitialization

See original GitHub issue

WebGLLights is used to store lighting state. WebGLLights.hash property is used to determine whether or not a material needs to be re-built.

https://github.com/mrdoob/three.js/blob/bd63a6cc8dd7fe11b530410fab9f67a9de359be8/src/renderers/WebGLRenderer.js#L1479-L1482

The way hash is built, it includes unique reference to WebGLLights instance: https://github.com/mrdoob/three.js/blob/efeb95f10790ab4755171584139d5344177f2713/src/renderers/webgl/WebGLLights.js#L319

constructor: https://github.com/mrdoob/three.js/blob/efeb95f10790ab4755171584139d5344177f2713/src/renderers/webgl/WebGLLights.js#L103-L111

this means that if you have 2 instances of WebGLLights and they are identical, except for id - you will end up rebuilding materials as you switch between these instances - inefficient.

Another problem with the current approach is that it is probabilistic, it’s possible that hash remains the same even though lighting has changed.

I propose following:

  • remove id from hash calculation
  • use a fast hashing function to compute 64bit hash(javascript number is IEEE 64-bit Float) instead of using a string
  • pass more than just number of lights into the hash (as long as that makes sense).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
Mugen87commented, Feb 26, 2021

The renderer has changed at various places since this issue was originally reported. The performance of initMaterial() was improved via #19673, render states (and lists) are only created per scene now (#20422, #21254) and light uniforms can now be updated without triggering initMaterial() via WebGLLights.setupView(). WebGLLights also performs no string concatenation anymore (#14588) and simplified the usage of the internal hash (#16581).

Overall I think it’s best to close the issue and open a new one if there are still performance issues.

2reactions
Mugen87commented, Jul 13, 2020

Anyway, I have the feeling this discussion went a bit off-topic. Besides, the implementation of WebGLLights has slightly changed. It does not compare hashes anymore (materialProperties.lightsHash !== lights.state.hash) but just a single version number.

I actually like the idea of making initMaterial() faster when no shader compilation is required. Such optimizations might be valuable for #15047.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cryptographic hash function - Wikipedia
A cryptographic hash function (CHF) is a hash algorithm that has special properties desirable for cryptography: the probability of a particular n ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found