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.

WebGLShaderCache poor performance

See original GitHub issue

Describe the bug

The WebGLShaderCache uses full vertex/fragment shader source code as keys in a Map<string, WebGLShaderStage> and performs several lookups per frame, causing noticeably poor performance.

To Reproduce

  1. Create a RawShaderMaterial subclass with a non-trivial vertex and/or fragment shader (a few hundred lines of GLSL)
  2. Render 10k object instances using this material
  3. Record a performance trace in Chrome dev tools
  4. Observe the time spent in WebGLShaderCache#_getShaderStage()

Expected behavior

  • Avoid double-lookups in the common path: instead of .has() followed by .get(), use a single .get() when possible
  • Allow materials to specify their own vertexShaderKey and fragmentShaderKey strings that will be used instead of the full shader source code

I have made both of these changes in a patched version of three.js we use in Foxglove Studio (see https://github.com/foxglove/studio/blob/main/patches/three.patch). I’d be happy to turn this into a PR for three.js if this seems like the right approach.

Platform:

  • Device: All
  • OS: All
  • Browser: All
  • Three.js version: dev

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
jhurlimancommented, Aug 7, 2022

Another option could be generating hashes of the shader sources and storing them as material properties that are used for lookups instead of the full source. I think this would be possible without an API change.

2reactions
gero3commented, Aug 9, 2022

@Mugen87

There are several examples with onBeforeCompile which set the vertex shader just before compilation, so it seems the paradigm of setting shaders after creation is something that has more impact than I first envisioned.

Examples that use onBeforeCompile to change the shader text (this is not a complete list)

  • webgl_gpgpu_birds_gltf.html
  • webgl_materials_modified.html
Read more comments on GitHub >

github_iconTop Results From Across the Web

cache results of shader compilation - Bugzilla@Mozilla
Compiling WebGL shaders is really expensive, especially on Win32; also slow on Mobile. We should cache as much as we can.. e.g. given...
Read more >
People don't understand shader cache stuttering and ... - Reddit
A lot more people are starting to become aware of Unreal 4's DX12 shader compilation stutters, but they don't understand what they are,...
Read more >
Is WebGL Shader Caching Possible? - Stack Overflow
There may be other reasons to clear the cache as the browser needs to make sure it never delivers a bad or out...
Read more >
Poor WebGL 2 performance for large draw calls on ANGLE's ...
Issue 1245448 : Poor WebGL 2 performance for large draw calls on ANGLE's OpenGL and Metal backends on AMD GPUs. Reported by schedule...
Read more >
WebGL performance - Nical
Bandwidth. Data transfer betwwen CPU and GPU is slow. Texture uploads, vertex buffers, ect. Cache getter calls like gl ...
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