WebGLShaderCache poor performance
See original GitHub issueDescribe 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
- Create a RawShaderMaterial subclass with a non-trivial vertex and/or fragment shader (a few hundred lines of GLSL)
- Render 10k object instances using this material
- Record a performance trace in Chrome dev tools
- 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
andfragmentShaderKey
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:
- Created a year ago
- Reactions:2
- Comments:12 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@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)