Skip to content

Shader Diffing

David Komer edited this page Jul 25, 2018 · 12 revisions

The problem

  • Scene is updated immutably and rendered as a pure function
  • Every primitive requires knowing which shader to use
  • Changes to the scene affect what the shader code should be
  • Generating and uploading a new shader is expensive

The solution

  1. A plain object called shaderConfig is derived for each scene + primitive combo.

  2. This object is hashed, resulting in a shaderHash (just a plain string). This hash is used to lookup in a global map for a compiled shader.

  3. If the shader is not found for that hash, it is compiled and stored. Compiling a shader requires passing the shaderConfig object (and nothing else)

Performance concerns

All of this happens every tick (though of course cache misses are typically only on the first tick). Therefore, creating shaderConfig and hashing it must be very fast (and they are - setting shaderConfig is mostly unrolled settings of flags, and the hash is a hand-tuned approach that was tested against several other approaches).

This is one of the reasons why extensions are hardcoded rather than an open and flexible api (though conceptually they could be dynamically loaded and just append the hash string).

At this point from some minimal tests - it seems to support a few thousand gltf objects no problem. At that point the bottleneck is almost definitely somewhere else in the system rather than diffing/hashing. Since it's all pure data, this work could be offloaded to a separate thread as well.

Clone this wiki locally