Hi all! We found a shader-cache mix-up with material plugins. When a material has two plugins, two materials whose plugin sets differ can end up drawing with the same compiled program. Depending on which one compiles first, a mesh draws in the wrong colour (black in the repro) or is not drawn at all, with a WebGL “uniform buffer too small” error. We found it while digging into shader recompiles in our game.
In short: MaterialPluginManager gives each plugin class a marker define, MATERIALPLUGIN_n, so that a material with a plugin never shares a program with one without it. That was the fix for PluginMaterial Bug (PR #12502). But _addPlugin rebuilds the define list from scratch each time and puts back only the marker of the plugin being added. With two plugins, only the last one’s marker reaches the effect key. A disabled plugin still adds its uniforms to the material’s UBO, so two materials whose plugin sets differ only by a disabled plugin get the same key, and so the same program, but different UBO layouts.
The fix is a one-line move inside _addPlugin. Details, measurements and a suggested patch are below.
Repro (Playground): Babylon.js Playground
Both boxes are StandardMaterials with the same green “Tint” plugin, so both should be green. The left box also has the Colorify plugin from the material plugins docs page, attached but switched off (its default state). The on-screen text shows each box’s effect key, whether the two boxes share a program, the centre pixel of each box and the WebGL error state. One button chooses which material compiles first. An “Apply fix” button patches _addPlugin as suggested below and rebuilds both boxes.
Measured (WebGL2, Chrome 156, Intel Iris Xe on Linux; the same results on Babylon 9.31.0 and 9.32.0):
| Compiled first | Same program? | Left box (Colorify off + Tint) | Right box (Tint only) | WebGL error |
|---|---|---|---|---|
| “Tint only” | yes | rgb(0,0,0), black | rgb(39,196,59), green | none |
| “Colorify + Tint” | yes | rgb(39,196,59), green | not drawn (background) | GL_INVALID_OPERATION: uniform buffer too small |
| either, with the fix | no | green | green | none |
Both keys are MATERIALPLUGIN_n TINT. With the fix, the left key gains Colorify’s marker, and both keys gain the built-in DetailMap plugin’s MATERIALPLUGIN_1.
Why black: the left material’s UBO holds ..., myColor, tintColor. The program it borrowed was compiled for ..., tintColor, so it reads tintColor from the slot where the left buffer holds myColor, which is zero because Colorify is off. If we write (1, 0.5, 0) into the left material’s myColor slot, the left box turns orange. On WebGPU the two boxes share one program too, but this particular scene happens to draw correctly there.
(Screenshots attached.)
Even smaller repro (console only):
const scene = new BABYLON.Scene(engine);
class A extends BABYLON.MaterialPluginBase { constructor(m) { super(m, "A", 200, {}); } getClassName() { return "A"; } }
class B extends BABYLON.MaterialPluginBase { constructor(m) { super(m, "B", 300, {}); } getClassName() { return "B"; } }
const m = new BABYLON.StandardMaterial("m", scene);
new A(m); new B(m);
console.log(Object.keys(m.pluginManager._defineNamesFromPlugins).filter((k) => k.startsWith("MATERIALPLUGIN")));
// ["MATERIALPLUGIN_3"]: B's marker only; A's (MATERIALPLUGIN_2) and the built-in DetailMap's (MATERIALPLUGIN_1) are gone
Where: materialPluginManager.pure.ts, _addPlugin, unchanged on master:
const pluginClassName = plugin.getClassName();
if (!MaterialPluginManager._MaterialPluginClassToMainDefine[pluginClassName]) {
MaterialPluginManager._MaterialPluginClassToMainDefine[pluginClassName] = "MATERIALPLUGIN_" + ++MaterialPluginManager._MaterialPluginCounter;
}
// ...
const defineNamesFromPlugins: { [name: string]: { type: string; default: any } } = {};
defineNamesFromPlugins[MaterialPluginManager._MaterialPluginClassToMainDefine[pluginClassName]] = { // only the plugin being added
type: "boolean",
default: true,
};
for (const plugin of this._plugins) { // every plugin's own defines are collected...
plugin.collectDefines(defineNamesFromPlugins);
this._collectPointNames("vertex", plugin.getCustomCode("vertex", this._material.shaderLanguage));
this._collectPointNames("fragment", plugin.getCustomCode("fragment", this._material.shaderLanguage));
}
this._defineNamesFromPlugins = defineNamesFromPlugins; // ...but only one marker survives
Meanwhile the PrepareUniformBuffer handler adds the UBO uniforms of every attached plugin (for (const plugin of this._plugins)), whether it is enabled or not. So the UBO layout depends on the full plugin set, but the key only sees the last plugin.
Suggested fix: add every plugin’s marker inside the loop that already visits every plugin:
const defineNamesFromPlugins: { [name: string]: { type: string; default: any } } = {};
for (const plugin of this._plugins) {
defineNamesFromPlugins[MaterialPluginManager._MaterialPluginClassToMainDefine[plugin.getClassName()]] = {
type: "boolean",
default: true,
};
plugin.collectDefines(defineNamesFromPlugins);
this._collectPointNames("vertex", plugin.getCustomCode("vertex", this._material.shaderLanguage));
this._collectPointNames("fragment", plugin.getCustomCode("fragment", this._material.shaderLanguage));
}
Every plugin in _plugins already has an entry in _MaterialPluginClassToMainDefine, because it went through _addPlugin. The only side effect we can see is that materials with plugins get a slightly longer key (for example, every StandardMaterial with a user plugin gains DetailMap’s marker), so they compile under a new key once.
Our workaround in the meantime: give each plugin a define whose default is true, e.g. super(material, "Colorify", 200, { COLORIFY: false, COLORIFY_ATTACHED: true }). This puts the plugin’s presence in the key even while it is disabled.
Happy to open a PR with this fix and a unit test if that helps. Thanks!
